Another question about variable args.

S

Steven W. Orr

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
 
N

Neil Cerutti

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.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top