initilize a memory zone in python

M

Mug

hello, i'm new in python, i used to program in C,
i have a small problem, i tryed to do some serial port things
manipulation
with python.
i have something like:

import sys,termios

fd = sys.stdin.fileno()
term_conf=termios.tcgetattr(fd);

now i want to modify the actuall values in term_conf zone to zero
i don't see how to do it,
in C we can do : bzero(&term_conf,sizeof(struct termios));
i want to know if it exist a similar function in python, thanks
 
D

Diez B. Roggisch

Mug said:
hello, i'm new in python, i used to program in C,
i have a small problem, i tryed to do some serial port things
manipulation
with python.
i have something like:

import sys,termios

fd = sys.stdin.fileno()
term_conf=termios.tcgetattr(fd);

now i want to modify the actuall values in term_conf zone to zero
i don't see how to do it,
in C we can do : bzero(&term_conf,sizeof(struct termios));
i want to know if it exist a similar function in python, thanks

In python you don't modify memory like that.

For the above function, you pass a value as the one you got to
tcgetattr, with values modified as you desire them.

Diez
 
M

Mug

In python you don't modify memory like that.

For the above function, you pass a value as the one you got to
tcgetattr, with values modified as you desire them.
i tryed
print term_conf
and i saw that the term_conf is actually represent with an array in
python:

zsh/3 4201 [1] % python test.py
[27906, 5, 1215, 35387, 15, 15, ['\x03', '\x1c', '\x7f', '\x15',
'\x04', '\x00', '\x01', '\xff', '\x11', '\x13', '\x1a', '\xff',
'\x12', '\x0f', '\x17', '\x16', '\xff', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00']]

it's a array of 7 elements, with the seventh element it self a array,
so in order to initialize them to zero, there's no other way than
modify them
one by one in a loop? is there a fonction like "memset" or "bzero" in
python?
 
R

r

is there a fonction like "memset" or "bzero" in python?

Not that i know of ;). Try this link for a coverage of the Python
built-in functions. You cannot write quality Python code without
them.
http://docs.python.org/3.1/library/functions.html

What you refer to as an array is called a list round here, see this
informative read
http://docs.python.org/3.1/library/stdtypes.html#sequence-types-str-bytes-bytearray-list-tuple-range

And be sure to check out list comprehentions too, just icing on the
cake ;)

python 3000 docs
http://docs.python.org/3.1/
 
D

Diez B. Roggisch

Mug said:
In python you don't modify memory like that.

For the above function, you pass a value as the one you got to
tcgetattr, with values modified as you desire them.
i tryed
print term_conf
and i saw that the term_conf is actually represent with an array in
python:

zsh/3 4201 [1] % python test.py
[27906, 5, 1215, 35387, 15, 15, ['\x03', '\x1c', '\x7f', '\x15',
'\x04', '\x00', '\x01', '\xff', '\x11', '\x13', '\x1a', '\xff',
'\x12', '\x0f', '\x17', '\x16', '\xff', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00']]

it's a array of 7 elements, with the seventh element it self a array,
so in order to initialize them to zero, there's no other way than
modify them
one by one in a loop? is there a fonction like "memset" or "bzero" in
python?

Again: no, there is no such function. But for the above, you'd easily write:

foo = [0] * 7 # only do this with immutables
foo[-1] = ['\x00'] * 16

alternatively, you modify the result you got from tcgetattr in the
places you want.



Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top