Appending Elements in Element Tree

Discussion in 'Python' started by Ron Adam, Apr 8, 2006.

  1. Ron Adam

    Ron Adam Guest

    In my program I have a lot of statements that append elements, but
    sometimes I don't want to append the element so it requres an if
    statement to check it, and that requires assigning the returned element
    from a function to a name or calling the funtion twice.

    e = ET.Element('name')
    e.append(get_subelement(obj)) # but raises an exception on None



    This works, but I have a lot of appends.

    sube = get_element(obj)
    if sube is None:
    e.append(sube)



    Now if Elements worked more like lists I could extend an element.

    alist = []
    alist.extend([]) # returns orginal list



    With strings.

    s = ''
    s += '' # returns original string (or copy)


    So is there an identity operation with Element Tree elements that I can
    use to avoid putting a lot of if and or try statements around appending
    elements?



    I could wrap the append and do it this way...

    def eappend(e1, e2):
    if e2 is None: return e1
    e1.append(e2)
    return e1

    Then do...

    e = eappend(e, get_element(obj)) # Append if not None.


    But maybe there's a better way?


    Ron
     
    Ron Adam, Apr 8, 2006
    #1
    1. Advertising

  2. Ron Adam

    Ron Adam Guest

    Ron Adam wrote:
    >
    > In my program I have a lot of statements that append elements, but
    > sometimes I don't want to append the element so it requres an if
    > statement to check it, and that requires assigning the returned element
    > from a function to a name or calling the funtion twice.
    >
    > e = ET.Element('name')
    > e.append(get_subelement(obj)) # but raises an exception on None
    >
    >
    >
    > This works, but I have a lot of appends.
    >
    > sube = get_element(obj)
    > if sube is None:
    > e.append(sube)
    >
    >
    >
    > Now if Elements worked more like lists I could extend an element.
    >
    > alist = []
    > alist.extend([]) # returns orginal list
    >
    >
    >
    > With strings.
    >
    > s = ''
    > s += '' # returns original string (or copy)
    >
    >
    > So is there an identity operation with Element Tree elements that I can
    > use to avoid putting a lot of if and or try statements around appending
    > elements?
    >
    >
    >
    > I could wrap the append and do it this way...
    >
    > def eappend(e1, e2):
    > if e2 is None: return e1
    > e1.append(e2)
    > return e1
    >
    > Then do...
    >
    > e = eappend(e, get_element(obj)) # Append if not None.
    >
    >
    > But maybe there's a better way?




    The following worked in cases where I needed to append a list of items.

    def extend(e1, elist):
    for e in elist:
    e1.append(e)
    return e1


    Although I still need to return the list as elements aren't mutable.

    element = extend(element, elist)

    It would be nice if this was built in so I could do... Hint hint. :)

    element.extend(elist)

    Where elist is a list of elements or an element with a number of sub
    elements.


    I still need to special case individual appends or put the returned item
    in a list so I use the extend() function on the returned element.

    I guess I need to look at the Element Tree source code.

    Cheers,
    Ron
     
    Ron Adam, Apr 8, 2006
    #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. Replies:
    3
    Views:
    3,726
    Thomas 'PointedEars' Lahn
    Dec 2, 2005
  2. Red Ogden
    Replies:
    0
    Views:
    1,101
    Red Ogden
    Jul 16, 2003
  3. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,147
  4. Debajit Adhikary
    Replies:
    17
    Views:
    694
    Debajit Adhikary
    Oct 18, 2007
  5. Replies:
    3
    Views:
    191
    Thomas 'PointedEars' Lahn
    Dec 2, 2005
Loading...

Share This Page