I have started to write a module for typed arrays in Python27. So far, so good; but I have come to a halt. I have noticed that the Long type in python is only 4 bytes, but it should be 8 bytes on a 64-bit processor. Can I specify that?
Is there any way to clean up my __new__(self) method? I have tried to override the super constructor: __init__(self, typecode[, initializer]), but I does not let me alter the parameters.
For example, the following code does not work:
class TypedIntegerArray(array):
def __init__(self, type, size):
array.__init__(self, type, (0 for i in range(0,size)))
I would like to run a setup() method on the array, to place all the logic in __new__(self), but __new__() happens before the object is created, so I have to create a temp object to grab the properties from it before I return it.
Module
#!/usr/bin/env python
# Typecode Type Min bytes
# 'c' character 1
# 'b' signed integer 1
# 'B' unsigned integer 1
# 'h' signed integer 2
# 'H' unsigned integer 2
# 'i' signed integer 2
# 'I' unsigned integer 2
# 'l' signed integer 4
# 'L' unsigned integer 4
# 'f' floating point 4
# 'd' floating point 8
from array import array
from math import floor, log10
from sys import getsizeof, maxsize
class TypedIntegerArray(array):
def __new__(self, type, size):
arr = array.__new__(self, type, (0 for i in range(0,size)))
exp = 1 if arr.typecode.islower() else 0
self.mask = int('FF'*arr.itemsize, 16)
self.frmt = '{0:0' + str(arr.itemsize * 8) + 'b}'
self._maxValue = 2 ** (arr.numBits() - exp) - 1
self._minValue = -1 * (self._maxValue + 1) if exp == 1 else 0
return arr
def size(self):
return len(self)
def numBits(self):
return self.itemsize * 8
def intToBin(self, value):
value = (value + self.mask) + 1 if value < 0 else value
return self.frmt.format(value)
def maxValue(self):
return self._maxValue
def minValue(self):
return self._minValue
def magnitude(self):
return int(floor(log10(self.size()-1) + 1))
def getRange(self):
return 'Range[{:d}, {:d}]'.format(\
self.minValue(), self.maxValue())
def disp(self, binary=False):
type = 's' if binary else 'd'
frmt = '{:0' + str(self.magnitude()) + 'd}. {:' + type + '}'
i = 0
for item in self.tolist():
value = self.intToBin(item) if binary else item
print(frmt.format(i, value))
i+=1
def __repr__(self):
return '{:s}[{:d}]'.format(self.__class__.__name__, self.size())
class ByteArray(TypedIntegerArray):
def __new__(self, size):
return TypedIntegerArray.__new__(self, 'b', size)
class UByteArray(TypedIntegerArray):
def __new__(self, size):
return TypedIntegerArray.__new__(self, 'B', size)
class IntegerArray(TypedIntegerArray):
def __new__(self, size):
return TypedIntegerArray.__new__(self, 'h', size)
class UIntegerArray(TypedIntegerArray):
def __new__(self, size):
return TypedIntegerArray.__new__(self, 'H', size)
class LongArray(TypedIntegerArray):
def __new__(self, size):
return TypedIntegerArray.__new__(self, 'l', size)
class ULongArray(TypedIntegerArray):
def __new__(self, size):
return TypedIntegerArray.__new__(self, 'L', size)
if __name__ == '__main__':
# Create 1 of each type of array
byteArr = ByteArray(8)
ubyteArr = UByteArray(8)
intArr = IntegerArray(16)
uintArr = UIntegerArray(16)
longArr = LongArray(32)
ulongArr = ULongArray(32)
# Specify a format for the ranges
frmt = '{:17s} {:s}'
# Print each of the ranges
print frmt.format(byteArr, byteArr.getRange())
print frmt.format(ubyteArr, ubyteArr.getRange())
print frmt.format(intArr, intArr.getRange())
print frmt.format(uintArr, uintArr.getRange())
print frmt.format(longArr, longArr.getRange())
print frmt.format(ulongArr, ulongArr.getRange())
# Test array mutation and display
for i in range(byteArr.size()):
byteArr[i] = byteArr.maxValue() if i % 2 == 0 else byteArr.minValue()
byteArr.disp(True)
Output
ByteArray[8] Range[-128, 127]
UByteArray[8] Range[0, 255]
IntegerArray[16] Range[-32768, 32767]
UIntegerArray[16] Range[0, 65535]
LongArray[32] Range[-2147483648, 2147483647]
ULongArray[32] Range[0, 4294967295]
0. 01111111
1. 10000000
2. 01111111
3. 10000000
4. 01111111
5. 10000000
6. 01111111
7. 10000000
I think what you are actually trying to do is to customize the creation of your typed array classes. Things like numBits or maxValue are properties of the type, not of a single instance, and should therefore be class attributes. Actually, in your current implementation, they are class attributes: they are set in the __new__ method whenever an instance is created. Remember that the first parameter to __new__ is actually the class, not an instance. It's often called cls instead of self to make that clear.
Customization of class creation is typically done using metaclasses. The __new__ method of the metaclass is called to create the actual class. All the class attributes of the new class can be set here:
from array import array
class TypedArrayMeta(type):
def __new__(metacls, name, bases, dct):
if "typecode" in dct:
typecode = dct["typecode"]
dct["num_bits"] = 8 * array(typecode).itemsize
dct["is_signed"] = typecode.islower()
# any other class attributes go into dct
return type.__new__(metacls, name, bases, dct)
class TypedArray(array):
__metaclass__ = TypedArrayMeta
# other methods go here
You can then easily define all the custom typed array classes:
class ByteArray(TypedArray): typecode = "b"
class UByteArray(TypedArray): typecode = "B"
class IntegerArray(TypedArray): typecode = "h"
class UIntegerArray(TypedArray): typecode = "H"
class LongArray(TypedArray): typecode = "l"
class ULongArray(TypedArray): typecode = "L"
The class attributes are now set properly even before an instance is created:
print(ByteArray.num_bits, ByteArray.is_signed)
print(UByteArray.num_bits, UByteArray.is_signed)
print(IntegerArray.num_bits, IntegerArray.is_signed)
print(UIntegerArray.num_bits, UIntegerArray.is_signed)
print(LongArray.num_bits, LongArray.is_signed)
print(ULongArray.num_bits, ULongArray.is_signed)
As to your first question, on my 64-bit Linux, the "L" type is actually 8 bytes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With