so.. to put this simply:

>>> BU5 = bu(5)
>>> BU5()
0
>>> BU5(1024)
1024
>>> BU5 == bu(5)
False
>>> BU5
<__main__.bu instance at 0x00AC5E40>
>>> bu(5)
<__main__.bu instance at 0x012C6788>

BU5 == bu(5) is supposed to return True due to it's expected usage:

>>> var = bu(5)() #read big u-int from 5 bytes
>>> type(var) == bu(5)
True

I was thinking to use a dictionary and reference the first class initializer based on the byte-size
but how would I compair it properly??

here's the current code for my class:

_buTypes = {}
class bu:
    def __init__(self,byte_size):
        self.size = byte_size
        self.value = 0

        try: _buTypes[byte_size]
        except KeyError: _buTypes[byte_size] = self

    def __call__(self,value=''):
        self.value = 0 if value=='' else value #_BIT(self.size,big,bit_format,value,label='')
        return self.value

can anyone help?? :/

Edited 2 Years Ago by Tcll

also... this works off it's own Python 2.7 interpreter

so don't worry about incompatibilities between older and newer versions. ;)

Edited 2 Years Ago by Tcll

more expected results:

>>> var0 = bu(6)()
>>> var1 = bu(6)()
>>> var2 = bu(5)()
>>> type(var0) == type(var1)
True
>>> type(var0) == type(var2)
False
>>> type(var2) == bu(5)
True
>>> bu(5) == bu(5)
True
>>> bu(5) == bu(7)
False

I have another idea for a function that stores a keyd reference of a class based on the byte-size and returns it...

but this is still pending...

another one started and solved by me:

output:

>>> var = bu(5)()
>>> var
0
>>> type(var) == bu(5)
True
>>> bu(5) == bu(5)
True
>>> bu(5) == bu(6)
False
>>> 

code:

def _BUinit(this, value=''): this.value = 0 if value=='' else value #__BIT(1,0,this.size,value)
def _BUrepr(this): return '%i'%this.value
_buTypes = {}
def bu(byte_size):
    global _buTypes
    try: return _buTypes[byte_size] #return the class object
    except KeyError:
        _buTypes[byte_size] = type(
            'BU_%i'%byte_size,
            (object,),
            dict(
                size=byte_size,
                __init__=_BUinit,
                __repr__=_BUrepr
                )
            )
        return _buTypes[byte_size] #return the class object

Python Magic FTW

Edited 2 Years Ago by Tcll

This question has already been answered. Start a new discussion instead.