473,395 Members | 1,639 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,395 software developers and data experts.

Is there a more elegant way to do this?

'''
Is there a more elegant way of doing this?
I would like to have the arguments to pack
automatically taken from lst.
'''

def pack(self):
lst = ['id', 'parent', 'number', 'x', 'y', 'z', 'red', \
'green', 'blue', 'size', 'rotate', 'translucency']
return struct.pack('<IIIiiiBBBHHH', \
self.id, self.parent, self.number, \
self.x, self.y, self.z, \
self.red, self.green, self.blue, \
self.size, self.rotate, self.translucency)
Jul 18 '05 #1
4 1403
Kamilche wrote:
'''
Is there a more elegant way of doing this?
I would like to have the arguments to pack
automatically taken from lst.
'''

def pack(self):
lst = ['id', 'parent', 'number', 'x', 'y', 'z', 'red', \
'green', 'blue', 'size', 'rotate', 'translucency']
return struct.pack('<IIIiiiBBBHHH', \
self.id, self.parent, self.number, \
self.x, self.y, self.z, \
self.red, self.green, self.blue, \
self.size, self.rotate, self.translucency)


There is an alternative

struct.pack("<IIIiiiBBBHHH", *[getattr(self, name) for name in lst])

and you could even add the typecodes

lst = [("I", 'id'), ("I", 'parent'), ... ]
struct.pack("<" + "".join([t for (t, n) in lst]),
*[getattr(self, n) for (t, n) in lst])
but frankly, I prefer your original version.

Peter

Jul 18 '05 #2
Kamilche wrote:
'''
Is there a more elegant way of doing this?
I would like to have the arguments to pack
automatically taken from lst.
'''

def pack(self):
lst = ['id', 'parent', 'number', 'x', 'y', 'z', 'red', \
'green', 'blue', 'size', 'rotate', 'translucency']
return struct.pack('<IIIiiiBBBHHH', \
self.id, self.parent, self.number, \
self.x, self.y, self.z, \
self.red, self.green, self.blue, \
self.size, self.rotate, self.translucency)


Note that you don't have to use backslashes when you're inside any kind
of brackets. Makes it look a little cleaner.

--
Shalabh
Jul 18 '05 #3
something like this:
def pack(self):
lst = ['id', 'parent', 'number', 'x', 'y', 'z', 'red', \
'green', 'blue', 'size', 'rotate', 'translucency']

values = [getattr(self, attr) for attr in lst]
return struct.pack('<IIIiiiBBBHHH', *values)

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA3fVyJd01MZaTXX0RAlgYAJ0UCE8OTGA4AqL/B0PxAzulzaWFgwCgppRS
BxN53Hjck5yxly91Eme1+4w=
=ea/I
-----END PGP SIGNATURE-----

Jul 18 '05 #4
Peter Otten <__*******@web.de> wrote in message news:<cb*************@news.t-online.com>...
struct.pack("<IIIiiiBBBHHH", *[getattr(self, name) for name in lst])

Thanks Peter! I knew there must be a way, but I don't comprehend list
comprehensions yet.

And I didn't know about the 'don't need backslashes when in a
bracket', thanks Shalabh.

:-)
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: steve | last post by:
Hi, In my script (phpnuke), whenever there is access to database, there is this line of code: message_die(GENERAL_ERROR, ’some error msg’, ’’, __LINE__, __FILE__, $sql); Is there a more...
6
by: Kamilche | last post by:
Is there a more elegant way to change the working directory of Python to the directory of the currently executing script, and add a folder called 'Shared' to the Python search path? This is what...
4
by: Tamir Khason | last post by:
How can I convert (elegant way) Collection to Array without looping and new instances. E.G: I want to add elements of one menu to other, so secondMenu.MenuItems.AddRange(firstMenu.MenuItems);...
8
by: Braky Wacky | last post by:
Hello, I have an ASP.NET webpage that uses an instance of System.Web.UI.HtmlControls.HtmlInputFile for uploading files to our server. I came across the documentation at MSDN for upping the...
28
by: Rob Cowie | last post by:
Hi all, I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba', 'abb', 'abc' etc. all the way to 'zzz'. How would you construct a generator to acheive this? A simple,...
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
32
by: r.z. | last post by:
class vector3 { public: union { float data; struct { float x, y, z; };
5
by: Helmut Jarausch | last post by:
Hi, I'm looking for an elegant solution to the following (quite common) problem: Given a string of substrings separated by white space, split this into tuple/list of elements. The problem...
3
by: Anonymous | last post by:
I want to be able to restrict the set of classes for which a template class can be instantiated (i.e enforce that all instantiation MUST be for classes taht derive from a base type BaseType). ...
12
by: Helmut Jarausch | last post by:
Hi, I'm looking for an elegant solution of the following tiny but common problem. I have a list of tuples (Unique_ID,Date) both of which are strings. I want to delete the tuple (element) with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.