Need a bit of help with a list..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rh0dium

    #1

    Need a bit of help with a list..

    Hi all,


    Why doesn't this work as expected.. I expect that the the lines within
    the sections will get modified - permanently. It is modifying them
    during the first section but they never get saved to the new values..
    Can anyone help me and even better explain why it's not working..

    for section in self.sections:
    nidx = 0
    for line in self.sections[section]:
    if re.match(r"^.*\ +$",line):
    line = line[:-1]
    line = line + " " + self.sections[section][nidx+1]
    print nidx, "+ found -total lines",
    len(self.sectio ns[section]), line
    del self.sections[section][nidx+1]
    nidx += 1
    else:
    nidx += 1

    for secs in self.sections:
    print " %s" % secs
    for line in self.sections[secs]:
    print " %s" % line

    self.section[foo]=["Param_Set primitive:rdv_A IO_reg50_top_IP TOP
    id:chip_top_pad s!reg50ma +",
    " VDD18:VDD1_CR18 VDD33:VDD1_CR33 VSSA:VSSCORE", "Param_Set
    instance:chip_t op_pads!PAD_3 APAD:PIN_VTRIP_ IN + ", " RC:RC_5A
    VDDCR:VDD1_CR18 VDDIO:VDD1_IO33 VSSCR:VSSCORE +"," VDDIO5:VDD1_IO5 A
    VDDIO5DIV2:VDD1 _IO5DIV2A VSSIO:VSS_IO"]

    Many thanks!!

  • Alex Martelli

    #2
    Re: Need a bit of help with a list..

    rh0dium <steven.klass@g mail.com> wrote:
    ...[color=blue]
    > Why doesn't this work as expected.. I expect that the the lines within
    > the sections will get modified - permanently. It is modifying them
    > during the first section but they never get saved to the new values..
    > Can anyone help me and even better explain why it's not working..
    >
    > for section in self.sections:
    > nidx = 0
    > for line in self.sections[section]:[/color]

    From this constuct I assume self.sections is a dict, in which case there
    may be better way to loop over the values in the dict; but that's an
    aside and does not affect your stated problem. Rather, said problem is
    already shown in the next couple lines:
    [color=blue]
    > if re.match(r"^.*\ +$",line):
    > line = line[:-1][/color]

    A simple assignment _to a bare name_ (here, 'line') only ever affects
    that NAME itself - nothing else, and in particular not the object to
    which the name used to be bound before you re-bound it, not other names
    (or locations within a container) bound to the same object, and so on.

    To affect some item, say the i-th one, of self.sections[section], you
    will need to assign something to self.sections[section][i]. You may
    give another and nicer name to the whole objects self.sections[section],
    but you will still need to assign to whatevername[i] to rebind the i-th
    item -- assign to an indexing, not to a bare name.

    There's another problem later in this inner loop:
    [color=blue]
    > del self.sections[section][nidx+1][/color]

    ....don't alter the container you're directly looping on, for example by
    deleting some of its items: that will alter the semantics of the loop in
    way you most definitely don't want. I don't think it's biting you here,
    but in most cases it will indeed bite, and painfully.

    I would suggest restructuring your whole first nested loop, correcting
    other strangeness (which is innocuous) as we go, such as the strange re
    and the separate and identical increments of nidx along an if and an
    else branch. For example, trying to stay as close as feasible to your
    original code, we might have:

    for lines in self.sections.i tervalues():
    nidx = 0
    while nidx<len(lines) :
    line = lines[nidx]
    if line.endswith(' +'):
    lines[nidx] = line[:-1] + " " + lines[nidx+1]
    del lines[nidx+1]
    nidx += 1

    This still has several problems (crashes if there's a + at the end of
    the last line, doesn't join properly if two successive lines both end
    with +, possibly others since my code is NOT tested) but they're not
    horribly hard to fix if they're indeed problems for you.


    Alex

    Comment

    • rh0dium

      #3
      Re: Need a bit of help with a list..


      Alex Martelli wrote:
      [color=blue]
      > From this constuct I assume self.sections is a dict, in which case there
      > may be better way to loop over the values in the dict; but that's an
      > aside and does not affect your stated problem. Rather, said problem is
      > already shown in the next couple lines:
      >[color=green]
      > > if re.match(r"^.*\ +$",line):
      > > line = line[:-1][/color]
      >
      > A simple assignment _to a bare name_ (here, 'line') only ever affects
      > that NAME itself - nothing else, and in particular not the object to
      > which the name used to be bound before you re-bound it, not other names
      > (or locations within a container) bound to the same object, and so on.[/color]

      That's what I was thinking it was doing and a simple proof showed me
      this..
      [color=blue]
      > To affect some item, say the i-th one, of self.sections[section], you
      > will need to assign something to self.sections[section][i]. You may
      > give another and nicer name to the whole objects self.sections[section],
      > but you will still need to assign to whatevername[i] to rebind the i-th
      > item -- assign to an indexing, not to a bare name.
      >
      > There's another problem later in this inner loop:
      >[color=green]
      > > del self.sections[section][nidx+1][/color]
      >
      > ...don't alter the container you're directly looping on, for example by
      > deleting some of its items: that will alter the semantics of the loop in
      > way you most definitely don't want. I don't think it's biting you here,
      > but in most cases it will indeed bite, and painfully.[/color]

      Thanks so much for pointing this out - it explains some behaviour I was
      seeing!!
      [color=blue]
      > I would suggest restructuring your whole first nested loop, correcting
      > other strangeness (which is innocuous) as we go, such as the strange re
      > and the separate and identical increments of nidx along an if and an
      > else branch. For example, trying to stay as close as feasible to your
      > original code, we might have:
      >
      > for lines in self.sections.i tervalues():
      > nidx = 0
      > while nidx<len(lines) :
      > line = lines[nidx]
      > if line.endswith(' +'):
      > lines[nidx] = line[:-1] + " " + lines[nidx+1]
      > del lines[nidx+1]
      > nidx += 1
      >[/color]

      Ah - A couple new methods - Thanks so much for your insightful comments
      and concerns. I will take it from here - much appreciated!!

      Comment

      Working...