By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,034 Members | 2,000 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,034 IT Pros & Developers. It's quick & easy.

Static parameter count

P: n/a
Hi all,
This does not feel Pythonic. Is there a better way to do the same?

Many TIA
T


# L = [1,2,3,4,5,6, etc....]
# L can contain 'n' elements
# fmt is made up to each particular specification

if len(L) == 0:
return ''
elif len(L) == 1:
return struct.pack(fmt,L[0] )
elif len(L) == 2:
return struct.pack(fmt,L[0] , L[1])
elif len(L) == 3:
return struct.pack(fmt,L[0] , L[1], L[2])
elif len(L) == 4:
return struct.pack(fmt,L[0] , L[1], L[2], L[3])
elif len(L) == 5:
return struct.pack(fmt,L[0] , L[1], L[2], L[3], L[4])
elif len(L) == 6:
return struct.pack(fmt,L[0] , L[1], L[2], L[3], L[4], L[5])

# etc... etc... etc ...
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Tertius Cronje wrote:
Hi all,
This does not feel Pythonic. Is there a better way to do the same?

Many TIA
T
# L = [1,2,3,4,5,6, etc....]
# L can contain 'n' elements
# fmt is made up to each particular specification

if len(L) == 0:
return ''
elif len(L) == 1:
return struct.pack(fmt,L[0] )
elif len(L) == 2:
return struct.pack(fmt,L[0] , L[1])
elif len(L) == 3:
return struct.pack(fmt,L[0] , L[1], L[2])
elif len(L) == 4:
return struct.pack(fmt,L[0] , L[1], L[2], L[3])
elif len(L) == 5:
return struct.pack(fmt,L[0] , L[1], L[2], L[3], L[4])
elif len(L) == 6:
return struct.pack(fmt,L[0] , L[1], L[2], L[3], L[4], L[5])

# etc... etc... etc ...


return struct.pack(fmt, *L)

Should do the trick

ola

--
--------------------------------------
Ola Natvig <ol********@infosense.no>
infoSense AS / development
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.