471,337 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Python pearls required for iteration across fields of data structure

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

Jul 24 '07 #1
1 1048
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
Jul 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

36 posts views Thread by Andrea Griffini | last post: by
29 posts views Thread by Frank Millman | last post: by
5 posts views Thread by Michael Sperlle | last post: by
8 posts views Thread by sonald | last post: by
852 posts views Thread by Mark Tarver | last post: by
17 posts views Thread by Adam Olsen | last post: by
14 posts views Thread by Thomas Mlynarczyk | last post: by

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.