Python pearls required for iteration across fields of data structure

Discussion in 'Python' started by NetHead, Jul 24, 2007.

  1. NetHead

    NetHead Guest

    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
    NetHead, Jul 24, 2007
    #1
    1. Advertising

  2. Re: Python pearls required for iteration across fields of datastructure

    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
    Marc 'BlackJack' Rintsch, Jul 24, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pradeep Yadav

    programming pearls :jon bentley

    Pradeep Yadav, Dec 23, 2004, in forum: C Programming
    Replies:
    2
    Views:
    443
    Richard Bos
    Dec 23, 2004
  2. srinivas
    Replies:
    1
    Views:
    281
    Eric Jensen
    May 25, 2006
  3. Rudi
    Replies:
    5
    Views:
    4,974
  4. ma740988

    bit fields and data structure

    ma740988, Nov 17, 2008, in forum: C++
    Replies:
    7
    Views:
    551
    ma740988
    Dec 28, 2008
  5. Matt
    Replies:
    7
    Views:
    130
    Antonie C Malan Snr
    Sep 26, 2004
Loading...

Share This Page