so basically... I have a dynamic memory sub-system that does this:

d = bu32() # a value from dynamic memory (such as 2)

print d
print type(d)

p = ref(d) # a pointer
deref( p, bu16 ).set(10) # short *p = &d; *p = 10; //(not sure if correct)

print d
print type(d)

expected results:

2
<class '__main__.bu(4)'>
10
<class '__main__.bu(2)'>

actual results:

2
<class '__main__.bu(4)'>
2
<class '__main__.bu(4)'>

the problem:

>>> d2 = deref( p, bu16 )
>>> d2
0 #0x0000 0002 #ignored
>>> type(d2)
<class '__main__.bu(2)'>
>>> d2.set(10) #0x000A 0002

it doesn't update the instance of d, so d is still a bu32 value of 2

how could I pull this off??

Recommended Answers

All 5 Replies

I just now realized I messed up on the title... >.>
it's not supposed to have that first "replacing"

anyways...
lemme make things a tad simpler:

class A(object):
    #some stuff here

class B(object):
    #some stuff here

C = A()
C.update(B)

print type(C)

expected output:

>>>
<class '__main__.B'>

what can I do to achieve this??

As always, your code violates every rule of good design. It is possible, in theory, with a statement such as

C.__class__ = B

However you may find more help if you post the code of bu32, deref, bu() etc.

lol, glad you know me well XD
it's easier for me to understand like that... heh
but heck... I'm open to any inputs on it :)

and uh... the code itself
that kinda gets complex...
(it's not hard-coded exactly)

bu32 = bu(4) # users can use either

bu() creates a new "type" class for big-unsigned ints of the given byte-size.
if the given byte-size was specified earlier, then it returns that class instead.

deref( addr, struct ) was originally built to jump to the address and read a value based on the struct and return a new instance to set.
(it does affect the current offset, which could be bad)

this was a flaw that was soon figured out...

it's now been updated to search for a used address in the current memory class and try to return the class at that address if the specified struct matches.

if they don't match, or nothing is found, it creates a new class at that address.

that's where the problem is.

now that I've found out about class, I can take the existing instance at that address and apply the new instance to it.
("instance" because it's used by outer variables in a 3rd-party script)

if I'm assuming this correctly:

instance.__class__ = struct #from deref( addr, struct )

also... while I have been coding for a few years (since 2010)
I'm still kindof a noob at things, and just beginning to understand what works better than what...
heck I figured out decorators about 2 hours ago. XD

anyways...
I could post my code for tidying and what not...
(and if you're willing to help me tidy it up) ;)
but it's kinda large
(I feel like splitting each function into separate modules)

I'm just hoping it won't kill the cpu... x.x
(things ARE designed better than the old code, but I know I'm not perfect and there's still better things I could possibly do)

I'm going to say this is partially solved...
I've tested this out in a shell window and it worked,
but the actual interface doesn't quite work yet...
(if there was a yellow "solved" tag with black text, I'd use that)

alright, I've tested, and everything works as expected ;)
(the class gets updated to the new object I need and conversions ARE applied)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.