Having fun with python

T

Tzury

def loadResMap(self):
self.resMap = []
[[self.resMap.append(str('A2' + sim[0] + '/r' + str(x)))
for x in range(1, eval(sim[1])+1)]
for sim in [x.split(':')
for x in quickViews.smsResList.v.split(",")]]
'''
# Confused? Have this one:

data = quickViews.smsResList.v
sims, slots = [], data.split(",")
for slot in slots:
sims.append(slot.split(':'))
for sim in sims:
for x in range(1, eval(sim[1])+1):
self.resMap.append(str('A2' + sim[0] + '/r' +
str(x)))

# same functionality different approaches
# forloops vs. list comprehension
# redability vs. smartassicity
# AKA: You have read too many Lisp books
'''
 
D

Duncan Booth

Tzury said:
def loadResMap(self):
self.resMap = []
[[self.resMap.append(str('A2' + sim[0] + '/r' + str(x)))
for x in range(1, eval(sim[1])+1)]
for sim in [x.split(':')
for x in quickViews.smsResList.v.split(",")]]
'''
# Confused? Have this one:

data = quickViews.smsResList.v
sims, slots = [], data.split(",")
for slot in slots:
sims.append(slot.split(':'))
for sim in sims:
for x in range(1, eval(sim[1])+1):
self.resMap.append(str('A2' + sim[0] + '/r' +
str(x)))

# same functionality different approaches
# forloops vs. list comprehension
# redability vs. smartassicity
# AKA: You have read too many Lisp books
'''
The first one is stupid because it build a list of None objects and then
throws it away.

The second one goes too far avoiding all list comprehensions:

sims = [ slot.split(':')
for slot in quickViews.smsResList.v.split(',') ]

is perfectly manageable, although it looks like there should be a method
'getSims' on quickviews or smsResList or something that returns the data
in the correct (already split) format.

The 'eval' is almost certainly a mistake in both variants. Probably you
just meant to call 'int'. Also a format string would be clearer:

for a, b in sims:
self.resMap.extend("A2%s/r%d" % (a, x+1)) for x in range(int(b)))

However, one point you have shown very clearly: the second one is much
easier to tear apart and reassemble.
 
T

Tzury Bar Yochay

However, one point you have shown very clearly: the second one is much
easier to tear apart and reassemble.

Sure.
Zen Of Python: Readbility Counts
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top