472,119 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Another question about variable args.

I have a structure I need to pack. I call struct.pack about a dozen times
and each call takes about 53 arguments.

I create a sequence of the arguments:
a1 = 1
a2 = 2
a3 = 3
etc...
a54 = 88
myseq = (a1, a2, a3, a4 etc... a53)
Also I made

def mpack ( fmt, *ss ):
print type(ss)
for ii in ss:
print 'ii = ', ii, 'type(ii) = ', type(ii)
for ii in list(ss):
print 'ii = ', ii, 'type(ii) = ', type(ii)
return struct.pack(fmt, *ss )

In my main, this first print statement works.
print 'main:', mpack(ff, a, b, c, d, e, f, g, h, i)

1. For the life of me, I can't figure out what to do to get the next print
to work. The idea is that I want to make my code clearer by not passing
50+ args each time and to instead pass a sequence which when evaluated
will be the desired list of args.

print 'main:', mpack(ff, subseq)

2. And then, if possible, I'd like to do it in such a way that one myseq
is defined, its value can automagically be re-eval'd so I don't have to
re-assign to myseq each time one of the 50+ variables changes prior to
calling pack.

Does this make sense?

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Aug 7 '07 #1
1 987
On 2007-08-07, Steven W. Orr <st****@syslang.netwrote:
I have a structure I need to pack. I call struct.pack about a dozen times
and each call takes about 53 arguments.

I create a sequence of the arguments:
a1 = 1
a2 = 2
a3 = 3
etc...
a54 = 88
myseq = (a1, a2, a3, a4 etc... a53)
Also I made

def mpack ( fmt, *ss ):
print type(ss)
for ii in ss:
print 'ii = ', ii, 'type(ii) = ', type(ii)
for ii in list(ss):
print 'ii = ', ii, 'type(ii) = ', type(ii)
return struct.pack(fmt, *ss )

In my main, this first print statement works.
print 'main:', mpack(ff, a, b, c, d, e, f, g, h, i)

1. For the life of me, I can't figure out what to do to get the
next print to work. The idea is that I want to make my code
clearer by not passing 50+ args each time and to instead pass a
sequence which when evaluated will be the desired list of args.

print 'main:', mpack(ff, subseq)

2. And then, if possible, I'd like to do it in such a way that
one myseq is defined, its value can automagically be re-eval'd
so I don't have to re-assign to myseq each time one of the 50+
variables changes prior to calling pack.
A functor might be a fun solution.

class mpack_maker(object):
def __init__(self, fmt, *args):
self.fmt = fmt
self.arg_list = list(args)
def __call__(self, slice=None, subseq=None):
if slice:
self.arg_list[slice] = subseq
return struct.pack(self.fmt, *self.arg_list)

You can pass in a slice object and a subsequence to change
arguments (permanently).
>>mpack = mpack_maker(ff, a, b, c, d, e, f, g, h)
mpack() # Call the function
mpack(slice(5, 7), [0, 7]) # Change args 5 and 6, and call again.
The args you can change are limited by the slicing powers of
Python, and probably combining arg-changing with calling the
function isn't wise, but that's a sketch anyhow.

--
Neil Cerutti
We couldn't beat... us. We couldn't even beat us. I was trying to think of
somebody bad, and I couldn't think of anybody else. Us. --Tim Legler
Aug 7 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by googleboy | last post: by
8 posts views Thread by Paul Cochrane | last post: by
4 posts views Thread by Augustus S.F.X Van Dusen | last post: by
45 posts views Thread by n.estner | last post: by
5 posts views Thread by Hakusa | last post: by
9 posts views Thread by happyvalley | last post: by
3 posts views Thread by fdu.xiaojf | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.