Skip to content

Commit

Permalink
Add more methods to IntResponse.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkeene committed Jul 1, 2014
1 parent 4c73175 commit 48a0c79
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
22 changes: 22 additions & 0 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def it_negates(self, response):
assert -response == -12
assert -response2 == 12

def it_positives(self, response):
resp = Mock()
resp.json.return_value = {'data': -12}
response2 = IntResponse(resp)
assert +response == 12
assert +response2 == -12

def it_divmods(self, response):
assert divmod(response, 5) == (2, 2)
assert divmod(50, response) == (4, 2)
Expand All @@ -92,3 +99,18 @@ def it_lshifts(self, response):
def it_rshifts(self, response):
assert response >> 2 == 3
assert 8192 >> response == 2

def it_has_numerator(self, response):
assert response.numerator == 12

def it_has_denomerator(self, response):
assert response.denominator == 1

def it_has_real(self, response):
assert response.real == 12

def it_has_imag(self, response):
assert response.imag == 0

def it_has_bit_length(self, response):
assert response.bit_length() == 4
39 changes: 33 additions & 6 deletions ubersmith/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,31 @@ def get(self, key, default=None):

@total_ordering
class IntResponse(BaseResponse):
@property
def numerator(self):
return self.data

@property
def denominator(self):
return 1

@property
def real(self):
return self.data

@property
def imag(self):
return 0

def bit_length(self):
if hasattr(self.data, 'bit_length'):
return self.data.bit_length()
else:
return len(bin(abs(self.data))) - 2

def conjugate(self):
return self.data

def __int__(self):
return self.data
__index__ = __int__
Expand All @@ -387,10 +412,6 @@ def __eq__(self, other):
def __lt__(self, other):
return self.data < other

@property
def real(self):
return self.real

def __add__(self, other):
return int(self) + other
__radd__ = __add__
Expand Down Expand Up @@ -441,6 +462,9 @@ def __abs__(self):
def __neg__(self):
return -self.data

def __pos__(self):
return self.data

def __divmod__(self, other):
return self // other, self % other

Expand Down Expand Up @@ -472,10 +496,13 @@ def __rrshift__(self, other):
return other >> self.data

# TODO: need to add all these methods to emulate numeric type
# __pos__(self)
# __invert__(self)
# __complex__(self)
# __coerce__(self, other)
# __cmp__
# __invert__
# __long__
# __nonzero__
# __trunc__


class FileResponse(BaseResponse):
Expand Down

0 comments on commit 48a0c79

Please sign in to comment.