gdb python pretty printer uint64_t is interpreted signed - python

When I try to use:
class MyPrinter:
def __init__(self, val):
self.val = val
def to_string(self):
return str(self.val['fData'][0]) + ":" + "%016x" % (self.val['fData'][0]).cast(gdb.lookup_type("uint64_t"))
It prints
2929725441843584891:28a879c45a82df7b
But also
9918728419520062851:-7659990ddaef5a7d
When the most significant bit is set for val, it is interpreted as signed.
fData[] is uint64_t.
How can I avoid that? All my values should be unsigned.

At least as a workaround this works for me:
class MyPrinter:
def __init__(self, val):
self.val = val
def to_hex(self, val):
s = ''
x = 7
while (x >= 0):
s = s + "%02x" % ((val>>(x*8))&255)
x -= 1
return s
def to_string(self):
return self.to_hex(self.val['fData'][0]) + self.to_hex(self.val['fData'][1])

Related

Expected type tuple in function

My program implements a generic coerce_apply function that, given the name of an action and the two arguments as objects of types, units of measurement calculates and returns the result of the action on the arguments, by converting one of the objects to the type of the other object
Copy all the code to see the error that is in the key and I can not solve
class Centimeters(object):
def __init__(self, val):
self.val = val
def __repr__(self):
return 'Centimeters({0})'.format(self.val)
class Inches(object):
def __init__(self, val):
self.val = val
def __repr__(self):
return 'Inches({0})'.format(self.val)
class Feets(object):
def __init__(self, val):
self.val = val
def __repr__(self):
return 'Feets({0})'.format(self.val)
def Inches_to_Centimeters(C):
return Centimeters(C.Inches*2.54)
def add_Centimeters(s,o):
return Centimeters('%.20f' % (s.val + o.val))
def add_Inches(s,o):
return Inches('%.20f' % (s.val + o.val))
def add_Inches_Centimeters(i,c):
return add_Inches(i,centimeter_to_inche(c))
def add_Centimeters_Inches(c, i):
return add_Centimeters(c, inche_to_centimeter(i))
def type_tag(x):
return type_tag.tags[type(x)]
type_tag.tags = {Centimeters: 'cen', Inches: 'inc', Feets: 'fee'}
centimeter_to_inche = lambda x: Centimeters(x.val * 1/2.54)
inche_to_centimeter = lambda x: Inches(x.val * 2.54)
coercions = {('inc', 'cen'): inche_to_centimeter}
def coerce_apply(operator_name, x, y):
tx, ty = type_tag(x), type_tag(y)
if tx != ty:
if (tx, ty) in coercions:
tx, x = ty, coercions[(tx, ty)](x)
elif (ty, tx) in coercions:
ty, y = tx, coercions[(ty, tx)](y)
else:
return 'No coercion possible.'
assert tx == ty
key = (operator_name, tx)
return coerce_apply.implementations[key](x, y)
coerce_apply.implementations = {}
coerce_apply.implementations[('add', ('inc', 'cen'))] = add_Inches_Centimeters
print(coerce_apply('add',Inches(1),Centimeters(150)))
Your approach is convoluted - normally you use classes to combine data with methods to allow f.e. the Centimeter class to handle its own conversion and addition to other units.
You get
File "t.py", line 46, in <module>
print(coerce_apply('add',Inches(1),Centimeters(150)))
File "t.py", line 43, in coerce_apply
return coerce_apply.implementations[key](x, y)
KeyError: ('add', 'cen')
because you are missing the conversion to add centimeters to centimerters in your code.
You can fix it like so (I removed Feets):
class Centimeters(object):
def __init__(self, val):
self.val = val
def __repr__(self):
return 'Centimeters({0})'.format(self.val)
# enable self-addition by other centimeters
def __add__(self, other):
if not isinstance(other, Centimeters):
raise Exception("Bad __add__ call")
self.val += other.val
return Centimeters(self.val + other.val)
class Inches(object):
def __init__(self, val):
self.val = val
def __repr__(self):
return 'Inches({0})'.format(self.val)
# enable self-addition by other Inches
def __add__(self, other):
if not isinstance(other, Inches):
raise Exception("Bad __add__ call")
return Inches(self.val + other.val)
def Inches_to_Centimeters(C):
return Centimeters(C.Inches*2.54)
def add_Centimeters(s,o):
return Centimeters('%.20f' % (s.val + o.val))
def add_Inches(s,o):
return Inches('%.20f' % (s.val + o.val))
def add_Inches_Centimeters(i,c):
return add_Inches(i,centimeter_to_inche(c))
def add_Centimeters_Inches(c, i):
return add_Centimeters(c, inche_to_centimeter(i))
def type_tag(x):
return type_tag.tags[type(x)]
type_tag.tags = {Centimeters: 'cen', Inches: 'inc', Feets: 'fee'}
centimeter_to_inche = lambda x: Centimeters(x.val * 1/2.54)
inche_to_centimeter = lambda x: Inches(x.val * 2.54)
coercions = {('inc', 'cen'): inche_to_centimeter}
def coerce_apply(operator_name, x, y):
tx, ty = type_tag(x), type_tag(y)
if tx != ty:
if (tx, ty) in coercions:
tx, x = ty, coercions[(tx, ty)](x)
elif (ty, tx) in coercions:
ty, y = tx, coercions[(ty, tx)](y)
else:
return 'No coercion possible.'
assert tx == ty
key = (operator_name, (tx,ty))
return coerce_apply.implementations[key](x, y)
coerce_apply.implementations = {}
coerce_apply.implementations[('add', ('inc', 'cen'))] = add_Inches_Centimeters
# add self-addition via classes __add__ method to "conversion"
coerce_apply.implementations[('add', ('cen', 'cen'))] = Centimeters.__add__
print(coerce_apply('add',Inches(1),Centimeters(150)))
Output:
Centimeters(152.54)

Why does self variable lose value after method invocation

I have this code to implement queue and stack data structures. The QueueT by itself works as desired.
When I call st.push(1), it agreeable calls push(self, v) and this in turn calls the enqueue method.
The issue I am facing is that after the statement self._q1.enqueue(v) is executed, self._q1 does not retain the value v.
Code for class QueueT:
class QueueT:
def __init__(self):
self._CAPACITY = 6
self._data = [None] * self._CAPACITY
self._length = 0
self._first = 0
def enqueue(self, val):
if self.size() == self._CAPACITY:
self._resize(self._CAPACITY * 2)
self.enqueue(val)
else:
new_place = (self._first + self.size()) % self._CAPACITY
self._data[new_place] = val
self._length += 1
def size(self):
return self._length
def _resize(self, new_capacity):
old = self._data
old_capacity = len(old)
self._data = [None] * new_capacity
k = self._first
self._first = 0
for j in range(self._length):
self._data[j] = old[k]
k = (1 + k) % old_capacity
self._CAPACITY = new_capacity
Now code from StackFromQ:
class StackFromQ:
def __init__(self):
self._q1 = QueueT()
self._top = -1
def push(self, v):
self._q1.enqueue(v)
self._top += 1
Caller function:
def stack_of_q():
st = StackFromQ()
st.push(1)
st.push(2)
Finally invocation:
stack_of_q()

Is there any method like divide by or multiply by in python range()?

for java, we can do:
for(int i=100; i>2 ; i=i/2){things to execute}
but what if in python?
is there anything like
for i in range(100:2:something)
could solve this problem?
If you need something simple which you can have at hand at several places, you can create a generator function:
def range_divide(start, end, denominator): # TODO: Think for a better name!
value = start
while value > end:
yield value
value /= denominator
and then do
for value in range_divide(100, 2, 2):
# do_stuff
You could even flexibilize this with
def range_flexible(start, end, action):
value = start
while value > end:
yield value
value = action(value)
and do
for value in range_flexible(100, 2, lambda x: x/2):
# do_stuff
or even
def for_loop(start, cont_condition, action):
value = start
while cont_condition(value):
yield value
value = action(value)
for value in for_loop(100, lambda x: x > 2, lambda x: x/2):
# do_stuff
There isn't by using a range, you could prepopulate a list and iterate over that but you'd be better off using a while loop.
i = 100
while i > 2:
...
i = i / 2
If you want it to look more like a java (or C) for loop, you can define a function that will process the parameters as a string in the C style (at the expense of execution speed):
cachedCFor = dict()
def cFor(params):
if params in cachedCFor: return cachedCFor[params]()
setup,condition,step = [ p.strip() for p in params.split(";") ]
varName = setup.split("=",1)[0].strip()
fn = dict()
code = f"""
def iterator():
{setup}
while {condition}:
yield {varName}
{step}
"""
exec(code,{},fn)
cachedCFor[params] = fn["iterator"]
return fn["iterator"]()
for i in cFor("i=100;i>2;i=i/2"):
print(i)
100
50.0
25.0
12.5
6.25
3.125
Note that the i variable in the string parameter is internal to the iterator and is not accessible within the for loop's code. We could have written for i in cFor("x=100;x>2;x=x/2") and still use i within the loop
That being said, I would still suggest that you embrace Python's way of doing things and not try to reproduce other language's syntax (i.e. use a while statement in this particular case)
for example:
x = 100
while x > 2:
i,x = x,x/2 # using x for the next value allows this to be placed
# at the beginning of the loop (rather than at the end)
# and avoids issues with the continue statement
print(i)
# ... your code ...
Or, you could use a bit of math:
# 6 = int(math.log(100,2))
for i in [100/2**i for i in range(6)]:
print(i)
# Strangely enough, this is actually slower than the cFor() folly
Here's another approach to handle special progressions in a cleaner and more generic fashion. It is a class that implements (and hides) internal workings of a loop variable.
class Loop:
def __init__(self,start=0):
self._firstPass = True
self._value = start
#property
def value(self): return self._value
def start(self,initial):
if self._firstPass : self._value = initial
return self
def next(self,nextValue=None):
if nextValue is None : nextValue = self.value + self._increment
if self._firstPass : self._firstPass = False
else : self._value = nextValue
return self
def up(self,by=1):
return self.next(self.value+by)
def down(self,by=1):
return self.next(self.value-by)
def upTo(self,last,by=1):
if self._firstPass: self._firstPass = False
else: self._value += by
return self.value <= last
def downTo(self,last,by=1):
if self._firstPass: self._firstPass = False
else: self._value -= by
return self.value >= last
def loop(self,condition=True):
self._firstPass = False
return condition
def until(self,condition=False):
self._firstPass = False
return not condition
def __getitem__(self,index): return self.value[index]
def __str__(self): return str(self.value)
def __int__(self): return int(self.value)
def __float__(self): return float(self.value)
def __add__(self,other): return self.value + other
def __sub__(self,other): return self.value - other
def __mul__(self,other): return self.value * other
def __matmul__(self,other): return self.value.__matmul__(other)
def __divmod__(self,other): return divmod(self.value,other)
def __pow__(self,other): return self.value ** other
def __truediv__(self,other): return self.value / other
def __floordiv__(self,other): return self.value // other
def __mod__(self,other): return self.value % other
def __lshift__(self,other): return self.value << other
def __rshift__(self,other): return self.value >> other
def __lt__(self,other): return self.value < other
def __le__(self,other): return self.value <= other
def __eq__(self,other): return self.value == other
def __ne__(self,other): return self.value != other
def __gt__(self,other): return self.value > other
def __ge__(self,other): return self.value >= other
def __and__(self,other): return self.value & other
def __or__(self,other): return self.value | other
def __xor__(self,other): return self.value ^ other
def __invert__(self): return -self.value
def __neg__(self): return -self.value
def __pos__(self): return self.value
def __abs__(self): return abs(self.value)
def __radd__(self, other): return other + self.value
def __rsub__(self, other): return other - self.value
def __rmul__(self, other): return other * self.value
def __rmatmul__(self, other): return other.__matmul__(self.value)
def __rtruediv__(self, other): return other / self.value
def __rfloordiv__(self, other): return other // self.value
def __rmod__(self, other): return other % self.value
def __rdivmod__(self, other): return divmod(other,self.value)
def __rpow__(self, other): return other ** self.value
def __rlshift__(self, other): return other << self.value
def __rrshift__(self, other): return other >> self.value
def __rand__(self, other): return other & self.value
def __rxor__(self, other): return other ^ self.value
def __ror__(self, other): return other | self.value
The class is designed to work with the while statement after initializing a loop variable. The loop variable behaves like a normal int (or float, or str, etc.) when used in calculations and conditions. This allows the progression and stop condition to be expressed as you would write them for an ordinary loop variable. The class adds a few method to control the loop process allowing for non-standard increments/decrements:
For example:
i = Loop()
while i.start(100).next(i//2).loop(i>2):
print(i) # 100, 50, 25, 12, 6 ,3
# Note: to use i for assignment or as parameter use +i or i.value
# example1: j = +i
# example2: for j in range(+i)
#
# i.value cannot be modified during the loop
You can also give a start value in the constructor to make the while statement more concise. The class also has an until() function to invert the stop condition:
i = Loop(start=100)
while i.next(i//2).until(i<=2):
print(i) # 100, 50.0, 25.0, 12.5, 6.25, 3.125
Finally there are a couple of helper functions to implement the simpler loops (although a for in would probably be better in most cases):
i = Loop()
while i.start(1).upTo(10):
print(i) # 1,2,...,9,10
i = Loop()
while i.upTo(100,by=5):
print(i) # 0,5,10,15,20,...,95,100
i = Loop(100)
while i.down(by=5).until(i<20):
print(i) # 100,95,90,...,25,20

generic Number class refactoring?

I just made a generic number class.
this class is so simple, descriptions are below
from any ordered character list, make number class representing that ordered character list.
create_number_class("01") returns binary number class
create_number_class("0123456789") returns decimal number class
create_number_class("abcdefghij") return decimal number class but representing each digit as a alphabet.
belows is generic number class definition.
I think it is well-made class definition.
are there something needed improvement in that class definition?
thank you all. always.
ex)
ABC_Class = create_number_class("abc")
x = ABC_Class("baa")
y = ABC_Class("bbb")
print(x+y)
#output digits: abc, v: cbb, decimal_v: 22
below is class definition
def create_number_class(alphabet):
class temp(object):
digits = alphabet
def __init__(self, v):
self.v = v
self.decimal_v = self.to_decimal(self)
#staticmethod
def to_decimal(self):
r = 0
for i in range(0, len(self.v)):
r += len(temp.digits)**(len(self.v)-i-1)*(temp.digits.index(self.v[i]))
return r
#classmethod
def from_decimal(cls, decimal_v):
r = []
mod = len(temp.digits)
if decimal_v < mod:
return cls(temp.digits[decimal_v])
while True:
remainder = decimal_v % mod
r.append(remainder)
decimal_v = int((decimal_v - remainder)/ mod)
if decimal_v < mod:
r.append(decimal_v)
break
r = "".join(list(reversed([temp.digits[x] for x in r])))
#r = "".join(list(reversed([str(temp.digits.index(str(x))) for x in r])))
return cls(r)
def __add__(self, other):
return temp.from_decimal(self.decimal_v+other.decimal_v)
def __sub__(self, other):
return temp.from_decimal(self.decimal_v-other.decimal_v)
def __mul__(self, other):
return temp.from_decimal(self.decimal_v*other.decimal_v)
def __floordiv__(self, other):
return temp.from_decimal(self.decimal_v//other.decimal_v)
def __str__(self):
return "digits: {}, v: {}, decimal_v: {}".format(temp.digits, self.v, self.decimal_v)
def convert_to(self, new_class):
return new_class.from_decimal(self.decimal_v)
return temp
below are example
BinClass = create_number_class("01")
DecimalClass = create_number_class("0123456789")
x = BinClass("111")
x = BinClass("1000")
y = BinClass("10")
HexClass = create_number_class('0123456789ABCDEF')
x = HexClass('1')
y = HexClass('AA')
print(x+y)
print(x-y)
print(x*y)
print(x//y)
print(x.convert_to(DecimalClass))
isinstance(x, BinClass)

Python Property, How do I get this to work correctly

I am trying to use OO on python to create a nice structured class. I have an object that all functions from one class will inherit but there are sub functions (getter and setter) that may require one or two additional parameters.
How can I get this type of logic to work correctly.
class XYZ(object):
def __init__(self, cameraId):
self.cameraId = cameraId;
self.index = "";
def get_test(self):
print "Index: " + self.index + " CameraID: " + self.cameraId;
return self.cameraId;
def set_test(self, value, myValue=""):
self.cameraId = value;
self.index = myValue;
return True;
TEST_XYZ = property(get_test,set_test);
You can use tuple-typed values. Note that you don't have to use ; after your statements...
class XYZ(object):
def __init__(self, cameraId):
self.cameraId = cameraId
self.index = ""
def get_test(self):
print "Index: " + self.index + " CameraID: " + self.cameraId
return self.cameraId
def set_test(self, value):
# Require value to be a tuple!
assert(isinstance(value, tuple))
self.cameraId = value[0]
try:
self.index = value[1]
except IndexError:
self.index = ""
return True
TEST_XYZ = property(get_test, set_test)

Categories

Resources