I'm working with the following code. I included some tests to make it
easy to see--if you run the code--what troubles I'm having.
Can some one *please* splain me why str(obj) works but not print obj,
and why obj.__int__() works, but not int(obj). I just don't get it. :(
BTW: The reason I'm going to all this mess is that I need a Byte object
that can
* print like a byte e.g. \x00
* adds like a string (concatinates)
* does bitwise math like a byte
* coereces to an int
* works with ''.join() -- The whole reason I'm inheriting from str
* Oh yeah, and some other classes inherit from it and have sequence
like behavior
Much thanks to any that can unveil the mystery and correct my broken
understanding.
regards
--
mthorley
<code>
import struct
class Byte(str): # Implement Bytes as str for easy adding and joining
"""Byte data type
Inherits from str so join will work, but supports bitwise
operations
via operator overloading. Given a digit it uses struct to
produce
the correct string format. Given a non digit string, it
attempts to
convert it to a digit using struct.unpack, and initialize its
self
'\\x01'
'\\x01'
'\\x01\\x01'
'\\x01\\x01'
'\\x01'
'\\x01'
0
256
1
1
<type 'int'>
1
1
1
"""
def __new__(self, val):
if type(val) == str and not val.isdigit():
val = struct.unpack(' B', val) #ensure input is valid struct
byte
self._byte = struct.pack('B' , val)
self._int = int(val)
return str.__new__(sel f, self._byte)
def __int__(self):
return self._int
def __lshift__(self , x):
return self._int << x
def __rshift__(self , x):
return self._int >x
def __len__(self):
return 1
def _test():
import doctest
doctest.testmod ()
if __name__ == '__main__':
_test()
</code>
easy to see--if you run the code--what troubles I'm having.
Can some one *please* splain me why str(obj) works but not print obj,
and why obj.__int__() works, but not int(obj). I just don't get it. :(
BTW: The reason I'm going to all this mess is that I need a Byte object
that can
* print like a byte e.g. \x00
* adds like a string (concatinates)
* does bitwise math like a byte
* coereces to an int
* works with ''.join() -- The whole reason I'm inheriting from str
* Oh yeah, and some other classes inherit from it and have sequence
like behavior
Much thanks to any that can unveil the mystery and correct my broken
understanding.
regards
--
mthorley
<code>
import struct
class Byte(str): # Implement Bytes as str for easy adding and joining
"""Byte data type
Inherits from str so join will work, but supports bitwise
operations
via operator overloading. Given a digit it uses struct to
produce
the correct string format. Given a non digit string, it
attempts to
convert it to a digit using struct.unpack, and initialize its
self
>>b = Byte(1)
>>b
>>b
>>str(b)
>>b + b
>>''.join([b,b])
>>b[0]
>>for i in b: i
>>b >8
>>b << 8
>>b._int
>>len(b)
>>type(b._int )
>>b.__int__()
>>int(b)
>>print b
"""
def __new__(self, val):
if type(val) == str and not val.isdigit():
val = struct.unpack(' B', val) #ensure input is valid struct
byte
self._byte = struct.pack('B' , val)
self._int = int(val)
return str.__new__(sel f, self._byte)
def __int__(self):
return self._int
def __lshift__(self , x):
return self._int << x
def __rshift__(self , x):
return self._int >x
def __len__(self):
return 1
def _test():
import doctest
doctest.testmod ()
if __name__ == '__main__':
_test()
</code>
Comment