Friday, July 27, 2012
object oriented programming
Tuesday, July 24, 2012
python ctype resize issues
Let me summarize this issue myself. but please credit to @ecatmur and others The resize() function can be used to resize the memory buffer of an existing ctypes object. The function takes the object as first argument, and the requested size in bytes as the second argument. However, the resized object still has limited accessibility to the memory buffer based on its original size. to solve the problem. 3 different functions are defined:
def customresize1(array, new_size):
resize(array, sizeof(array._type_)*new_size)
return (array._type_*new_size).from_address(addressof(array))
def customresize2(array, new_size):
return (array._type_*new_size).from_address(addressof(array))
def customresize3(array, new_size):
base = getattr(array, 'base', array)
resize(base, sizeof(array._type_)*new_size)
new_array = (array._type_*new_size).from_address(addressof(base))
new_array.base = base
all functions return an object that shares the memory of the original owner, which does not own the memory and can not be resized (e.g., gives error in
customresize1)
customresize2does return a resized array, but keey in mind that from_address does not allocate memory for resizing..
customresize3keeps a record of the base object that owns the memory, but the returned object is not the owner of memoryAs python is dynamically allocating its memory and garbage collecting, so, if you want to resize something, just redo the size will work. eg.:
list = (c_int * NEW_SIZE)()
or you may want to keep the original values then:
list = (c_int * NEW_SIZE)(*list)
Tuesday, July 17, 2012
a review on OCT optimization algorithms
Wednesday, July 11, 2012
修行心得2012-07-11
Tuesday, July 10, 2012
deep copy vs shallow copy vs weakref
import copy
list = [ ['a'] ]
list_copy = copy.copy(list)
list_copy[0].append('b')
print list, list_copy
output is:
[['a', 'b']] [['a', 'b']]the lets try:
import copy
list = [ ['a'] ]
list_copy = copy.copy(list)
list_copy.append('b')
print list, list_copy
output is:
[['a']] [['a'], 'b']
the above shows shallow copy, which share the element, but not the obj of list itself
a deep copy will not share anything as a brand new separate obj.
so what is the weakref used for?
class LeakTest(object):
def __init__(self):
print 'Object with id %d born here.' % id(self)
def __del__(self):
print 'Object with id %d dead here.' % id(self)
def foo():
A = LeakTest()
B = LeakTest()
A.b = B
B.a = A
foo()
output is:
Object with id 71183792 born here.
Object with id 71182608 born here.
the object of A and B are not deleted, why? cus they refer to each other, cause a dead lock that can not delete the objs, that is why we need weakref:
import weakref
class LeakTest(object):
def __init__(self):
print 'Object with id %d born here.' % id(self)
def __del__(self):
print 'Object with id %d dead here.' % id(self)
def foo():
A = LeakTest()
B = LeakTest()
A.b = weakref. proxy (B)
B.a = weakref. proxy (A)
foo()
output is:
Object with id 71180816 born here.
Object with id 71181008 born here.
Object with id 71180816 dead here.
Object with id 71181008 dead here.
Monday, July 2, 2012
so python is dynamic
python immutable and mutable values
In python, immutable variable types are int, bool, string and so on, all of them are considered as a single value variable, thus making them immutable insures the function calls behavior like other languages , c for example.
But for list and dict, usually it is considered as *args and **kwdargs, which are passed as pointers, thus making them mutabe again insures the function call behaviors like other languages.
--
Zhijia
Confidentiality Notice: This message (including attachments) is a private communication solely for use of the intended recipient(s). If you are not the intended recipient(s) or believe you received this message in error, notify the sender immediately and then delete this message. Any other use, retention, dissemination or copying is prohibited and may be a violation of law, including the Electronic Communication Privacy Act of 1986."