473,419 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 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 1027
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: googleboy | last post by:
Hi there. I have defined a class called Item with several (about 30 I think) different attributes (is that the right word in this context?). An abbreviated example of the code for this is: ...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
4
by: Augustus S.F.X Van Dusen | last post by:
I have recently come across the following construction: #define P_VAR(output, string, args...) \ fprintf (output, "This is "string"\n", ##args) which can be invoked as follows: int x = 1,...
6
by: nadeem_far | last post by:
Hello All, I am working on a .Net desktop application and I am having problem displaying a form. I am using C# and version 1.1 of the framework. here is how the code looks likes and I will...
45
by: n.estner | last post by:
Having read previous discussions on python-dev I think I'm not the only Python programmer who doesn't particularly like python's "self" parameter: class Foo: def bar(self, a,b): return a+b...
5
by: Hakusa | last post by:
I have the argument items in my class room. class room: def __init__(self, name, description, items*): I thought I remembered from a tutorial I read once, and I've read so many I feel like an...
9
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
3
by: fdu.xiaojf | last post by:
Hi all, I'm not skilled at programming, so sorry for my ignorance. My questions: (1) which is the better way to calculate the value of attributes of a class ? for example: (A) def...
9
by: lorlarz | last post by:
I still have a question regarding the following code, in a commonly used routine. First, Here's the code in question: Function.prototype.bind = function(){ var fn = this, args =...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.