Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python pearls required for iteration across fields of data structure

Reply
Thread Tools

Python pearls required for iteration across fields of data structure

 
 
NetHead
Guest
Posts: n/a
 
      07-24-2007
I want to apply a method (replaceFieldsAndIndices) in my class to a
number of attributes of a data structure.

Specifically, to give some context, in the variable j below, I want to
replace wildcards with values.

I wanted to avoid cluttering the code with multiple calls to the
method replaceFieldsAndIndices.

The code below is WRONG, but hopefully illustrates what I am trying to
achieve.

Any suggestions how to code this is an efficient and maintainable (and
correct) manner?

# BAD code begins
wildcards = None
if trap:
for c in translation.expressionList:
wildcards += (c.rpn, c.format)
for c in translation.conditionList:
wildcards += (c.match, c.min, c.max, c.format)
else:
for c in translation.conditionRuleList:
wildcards += (c.format, c.condition, c.expression)
for c in translation.rules:
wildcards += (c.match, c.min, c.max, c.bit, c.format)

for i in wildcards:
for j in i:
try:
j = self.replaceFieldsAndIndices(j, rcn, rcu,
rcp, indices)
except:
# Ignore errors if condition has no format.
pass
# BAD code ends

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      07-24-2007
On Tue, 24 Jul 2007 02:26:15 -0700, NetHead wrote:

> The code below is WRONG, but hopefully illustrates what I am trying to
> achieve.
>
> Any suggestions how to code this is an efficient and maintainable (and
> correct) manner?
>
> […]
> for i in wildcards:
> for j in i:
> try:
> j = self.replaceFieldsAndIndices(j, rcn, rcu,
> rcp, indices)
> except:
> # Ignore errors if condition has no format.
> pass


Maybe you shouldn't silence all exceptions with a bare except here. This
swallows any errors in `replaceFieldsAndIndices()`. Maybe some of them
are not the exception you expect, but real errors in your program!?

Ciao,
Marc 'BlackJack' Rintscj
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
Pearls of wisdom? =?Utf-8?B?SGVjdG9y?= Windows 64bit 5 10-02-2007 08:28 PM
programming pearls :jon bentley Pradeep Yadav C Programming 2 12-23-2004 10:32 AM
Twin Pearlscales - twin-pearls.JPG [0/1] Gunther Firefox 1 10-19-2004 04:20 AM
Pearls of wisdom... KS NZ Computing 4 10-22-2003 11:05 PM



Advertisments