newbie help

A

ashtonn

static
void
testme(
unsigned char *buf,
unsigned long size,
CODE code)
{
unsigned long data
switch (code)
{
// Generate some test patterns
case TEST1 :
value = 0x0;
while (size--)
{
*buf++ = (unsigned char)value++
}
break
case TEST2 :
....
}
return
}

main()
{
unsigned char buf[512];
CODE code = TEST1


# The index number is passed from the command line
# It corresponds to one of the two buffers

# if index = 1 corresponds to buf1 and if index = 2 corresponds to
buf2


buf1 = getBuf(index1);
testme( buf1, 512, code);


return 0x0;
}


This is snippet code trying to generate some test patterns.

How do i manage multiple buffers in python. Is it possible?


If the index number at command line is 2, then buffer 2 is passed to
'testme'

I went through the Python tutorial and could not find much information.

Any help with python examples is appreciated.
 
A

ashokbellur

Here's some changes to the code....

main()
{
unsigned char buf1[512];
unsigned char buf2[512];
CODE code = TEST1

# The index number is passed from the command line
# It corresponds to one of the two buffers

# if index = 1 corresponds to buf1 and if index = 2 corresponds to
buf2

buf1 = getBuf(index1);
buf2 = getBuf(index2);
testme( buf1, 512, code);
testme( buf2, 512, code);

return 0x0;
}
I just want to know if there is an equivalent way of handling multiple
buffers and how i can map index to a buffer?

Thanks.
 
M

Marc 'BlackJack' Rintsch

In <[email protected]>,
[snipped snippet]

This is snippet code trying to generate some test patterns.

That snippet of code doesn't compile. At least not with gcc.
How do i manage multiple buffers in python. Is it possible?


If the index number at command line is 2, then buffer 2 is passed to
'testme'

There is no buffer 2 in your code. Not even a buffer 1.
I went through the Python tutorial and could not find much information.

Reread it. It's full of information. Really.

And please tell us in complete sentences what you want to achieve, not in
incomplete C snippets.

Ciao,
Marc 'BlackJack' Rintsch
 
M

Marc 'BlackJack' Rintsch

ashokbellur said:
main()
{
unsigned char buf1[512];
unsigned char buf2[512];
CODE code = TEST1

# The index number is passed from the command line
# It corresponds to one of the two buffers

# if index = 1 corresponds to buf1 and if index = 2 corresponds to
buf2

buf1 = getBuf(index1);
buf2 = getBuf(index2);
testme( buf1, 512, code);
testme( buf2, 512, code);

return 0x0;
}
I just want to know if there is an equivalent way of handling multiple
buffers and how i can map index to a buffer?

Still incomplete. You don't map an index to a buffer in that code. What
is getBuf() doing?

Ciao,
Marc 'BlackJack' Rintsch
 
L

Lonnie Princehouse

How about describing what your program is supposed to /do/ instead of
posting C code? What are the "buffers" for? This group might be more
helpful if you did that.

If you really do need this sort of random-access character buffer, you
have a couple of options. Python strings aren't mutable, so they won't
work. To start with, lists of characters would suffice, or mmap (look
it up in the global module index)
 
A

ashtonn

I want to pass and index number and a file name from the command line.
This index number corresponds to a buffer. I need to maintain 2 such
buffers for my test.

In C you would do someting like this
unsigned char buf1[512];

In python is this as simple as?
buf 1 = 512
buf 2 = 512
And how do i keep track of buf1 and buf2
Thanks.

Thanks.
 
L

Lonnie Princehouse

buf1 = [None] * 512
buf2 = [None] * 512

(Not quite the same as C because the list isn't filled with
gibberish...)
 
S

Stephen Thorne

I want to pass and index number and a file name from the command line.
This index number corresponds to a buffer. I need to maintain 2 such
buffers for my test.

In C you would do someting like this
unsigned char buf1[512];

In python is this as simple as?
buf 1 = 512
buf 2 = 512
And how do i keep track of buf1 and buf2
Thanks.

Okay, I've been reading some of your posts in the last few weeks, and
it seems we're still not past the "Python doesn't treat memory like C
does" meme.

I *think* what your C code is doing is creating a 512 byte long
unsigned char containing increasing values. i.e. buf[0] == 0, buf[1]
== 2, etc.

I have no idea why, but you get that.

Anyway, in Python, the way we create a list containing values like
that is with a library function called 'range'

range(10) = [0,1,2,3,4,5,6,7,8,9]

range(1,11) = [1,2,3,4,5,6,7,8,9,10]

range(10,0,-1) = [10,9,8,7,6,5,4,3,2,1]

So in order to create a list (a list is python object,
http://docs.python.org/tut/node5.html#SECTION005140000000000000000 for
more information) that contains the numbers from 0 to 511, you would
do.

mylist = range(512)

As for your other, implied, question, about passing a filename and a
number to your program, you would do this:

import sys
filename = sys.argv[1]
code = int(sys.argv[2])


So, for instance, a program which writes the numbers from 0 to 511 to
a file, as passed on the commandline, would probably look like this:

@ import sys
@
@ filename = sys.argv[1]
@ code = int(sys.argv[2])
@
@ if code == 1:
@ f = file(filename, 'w')
@ for x in range(512):
@ f.write("%d\n" % x)
@ f.close()

(The @ marks are to preserve the indentation on usenet, I believe
google strips them).

I hope that helps you.
 
A

ashtonn

I did something like this.
index is passed from the command line.

def __getBuffer( index):
if index == 1:
buf1 = [None] * 512
print "Buffer: %s" % (buf1)
return buf1


if index == 2:
buf2 = [None] * 512
print "Buffer: %s" % (buf1)
return buf2

Is this the best way to do this?
Thanks,
-Joe
 
A

ashtonn

Here it is again.

I did something like this.
index is passed from the command line.

def __getBuffer( index):
if index == 1:
buf1 = [None] * 512
print "Buffer: %s" % (buf1)
return buf1
elif index == 2:
buf2 = [None] * 512
print "Buffer: %s" % (buf2)
return buf2

Is this the best way to do this?
Thanks,
-Joe
 
K

Kent Johnson

Here it is again.

I did something like this.
index is passed from the command line.

def __getBuffer( index):
if index == 1:
buf1 = [None] * 512
print "Buffer: %s" % (buf1)
return buf1
elif index == 2:
buf2 = [None] * 512
print "Buffer: %s" % (buf2)
return buf2

Is this the best way to do this?

It's still not clear what you are trying to accomplish. The code above will return a new buffer each
time. It is equivalent to

def __getBuffer(index):
buf = [None] * 512
print "Buffer: %s" % (buf)
return buf

If your intent is that __getBuffer(1) always returns the *same* buffer, you need to create the
buffers once. A simple way is this:

buf1 = [None] * 512
buf2 = [None] * 512

def __getBuffer(index):
if index == 1:
print "Buffer: %s" % (buf1)
return buf1
elif index == 2:
print "Buffer: %s" % (buf2)
return buf2

Kent
 
M

Marc 'BlackJack' Rintsch

In <[email protected]>,
I did something like this.
index is passed from the command line.

def __getBuffer( index):
if index == 1:
buf1 = [None] * 512
print "Buffer: %s" % (buf1)
return buf1
elif index == 2:
buf2 = [None] * 512
print "Buffer: %s" % (buf2)
return buf2

Is this the best way to do this?

Best way to do what? That could be written as:

def get_buffer():
buffer = [None] * 512
print "Buffer: %s" % buffer
return buffer

In your code, no matter if index equals 1 or 2, a list with 512 `None`s is
created, printed and then returned.

It would be really better if you describe *what* you want to achieve, not
*how*. And in words please, not in code snippets.

Ciao,
Marc 'BlackJack' Rintsch
 
D

Dennis Lee Bieber

Here it is again.

I did something like this.
index is passed from the command line.

def __getBuffer( index):
if index == 1:
buf1 = [None] * 512
print "Buffer: %s" % (buf1)
return buf1
elif index == 2:
buf2 = [None] * 512
print "Buffer: %s" % (buf2)
return buf2

Is this the best way to do this?
Thanks,
-Joe

def __getBuffer():
return [None] * 512

#main code

if index == 1:
buf1 = __getBuffer()
elif index == 2:
buf2 = __getBuffer()

# or you can forget about separate buf1 and buf2

bufs = [None] * maxBufs

bufs[index] = __getBuffer()

--
 
D

Dennis Lee Bieber

On 23 Mar 2005 13:57:10 -0800, "(e-mail address removed)" <[email protected]>
declaimed the following in comp.lang.python:

I'm jumping all the way back to the beginning, as I'm still not
sure what the requirements /are/...
static
void
testme(
unsigned char *buf,
unsigned long size,
CODE code)
{
unsigned long data
switch (code)
{
// Generate some test patterns
case TEST1 :
value = 0x0;
while (size--)
{
*buf++ = (unsigned char)value++
}
break
case TEST2 :
....
}
return
}

main()
{
unsigned char buf[512];
CODE code = TEST1


# The index number is passed from the command line
# It corresponds to one of the two buffers

# if index = 1 corresponds to buf1 and if index = 2 corresponds to
buf2


buf1 = getBuf(index1);

Where is the code for getBuf(), that might be of use in
determining the intent.

Also, you keep mentioning "index = 1" or "index = 2", but here
you have an "index1"?

And where is buf1 declared -- up above you declared a single
array named buf

testme( buf1, 512, code);

Okay, code determines the type of test pattern, if I understand
the top of the listing... C is dumb, so you have to tell it how long the
pre-allocated buffer is, and you pass it the buffer space.
How do i manage multiple buffers in python. Is it possible?
How do you manage them in C?

If the index number at command line is 2, then buffer 2 is passed to
'testme'
Where is the C code showing this?

-=-=-=-=-=-=-=-=-
import random

MAXBUFS = 3
BUFLEN = 512

buffers = [None] * MAXBUFS

def testGen(bufsize, pattern):
if pattern == 1:
# use bitwise and to restrict values to
# unsigned byte -- 0..255 -- if bufsize
# is >256
b = [(x & 0xFF) for x in xrange(bufsize)]
elif pattern == 2:
# random integers, note that "x" is not even used
b = [random.randint(0, 255) for x in xrange(bufsize)]
else:
# "error", only patterns 1 and 2 are valid
# anything else returns an empty list
b = [None] * bufsize

return b

if __name__ == "__main__":
for x in range(len(buffers)): # same as range(MAXBUFS)
# I'm using the buffer number as the pattern code
# but range runs 0..n-1, not 1..n, and pattern
# codes are 1 or 2, so the first buffer will be
# "empty"
buffers[x] = testGen(BUFLEN, x)

print buffers[1]
print buffers[2]
print buffers[0]

# do something with the buffers
for x in range(BUFLEN):
buffers[0][x] = (buffers[1][x] + buffers[2][x]) & 0xFF

print buffers[0]

--
 
A

ashtonn

Hello,
Thanks to all for great responses. Here's what iam trying to do.
I am writing a script to generate test patterns.

For this i am passing an index number, size and a opcode from the
command line
testit -i <index> -s <size> [-o <opcode>]

When the user passes the index number(1 or 2), it maps to one of the
buffers 1 or 2.
Where:
index is the number 1 or 2 which is mapped to a buffer in a function
like:
buf = getBuffer( index)
Size is 512
opcode is one of the values which matches a pattern.

Do i have to take care of freeing buffers or will python take care of
it.

Thanks,
- Joe
 
R

Robert Kern

Hello,
Thanks to all for great responses. Here's what iam trying to do.
I am writing a script to generate test patterns.

What is a test pattern?

Anyways, the time you've spent here so far could have been more
profitably spent reading the Tutorial[1], any one of the fine
introductory documentation freely available on the Web or purchasable at
a decent bookstore[2], and most importantly "How to Ask Questions the
Smart Way"[3].

[1] http://docs.python.org/tut/tut.html
[2] http://www.python.org/doc/Intros.html
[3] http://www.catb.org/~esr/faqs/smart-questions.html

Please do yourself and us a favor: take a break from the newsgroup for a
while, and spend some quality time with the documentation.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
D

Dennis Lee Bieber

For this i am passing an index number, size and a opcode from the
command line
testit -i <index> -s <size> [-o <opcode>]

When the user passes the index number(1 or 2), it maps to one of the
buffers 1 or 2.

What purpose is served by having two buffers? Based upon your
example, only one buffer is ever used at any time, and buffers do not
hang around between invocations of the program.

Now, if the buffers were memory mapped files (see mmap) shared
between multiple programs, passing an index to select which shared
buffer is to be populated could make sense, but right now "testit" could
completely ignore the index and the user would never know.


--
 

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

Forum statistics

Threads
474,228
Messages
2,571,157
Members
47,785
Latest member
deepusaini

Latest Threads

Top