Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   tuples within tuples (http://www.velocityreviews.com/forums/t547114-tuples-within-tuples.html)

korovev76@gmail.com 10-26-2007 10:26 AM

tuples within tuples
 
Hello everybody.

I'm wondering how to iterate over a tuple like this
[A,B,C,D]
while saving A and C in a list.

My problem is that C sometimes is a tuple of the same structure
itself...


thanks!
korovev


Larry Bates 10-26-2007 12:34 PM

Re: tuples within tuples
 
korovev76@gmail.com wrote:
> Hello everybody.
>
> I'm wondering how to iterate over a tuple like this
> [A,B,C,D]
> while saving A and C in a list.
>
> My problem is that C sometimes is a tuple of the same structure
> itself...
>
>
> thanks!
> korovev
>

First of all [A,B,C,D] is a list not a tuple. (A,B,C,D) is a tuple.

Without a better example or explanation of what you are trying to do it is
difficult, but I'll give it a try:

myTuple=(A,B,C,D)
for n, item enumerate(myTuple):
if n in (0,2):
myList.append(item)

-Larry

korovev76@gmail.com 10-26-2007 12:54 PM

Re: tuples within tuples
 

[cut]
>
> Without a better example or explanation of what you are trying to do it is
> difficult


You're right.
Actually i'm parsing an xml file using pyrxp, which returns something
like this:
(tagName, attributes, list_of_children, spare)
Where list_of_children might "be a list with elements that are 4-
tuples or plain strings".

In other terms, if I have something like this:
('<tagA><tagB>bobloblaw</tagB></tagA>')
it's parsed like this:
('tagA', None, [('tagB', None, ['bobloblaw], None)], None)...

Fact is that my xml is much more deep... and I'm not sure how to
resolve it


thanx







Marc 'BlackJack' Rintsch 10-26-2007 01:06 PM

Re: tuples within tuples
 
On Fri, 26 Oct 2007 05:54:24 -0700, korovev76 wrote:

> [cut]
>>
>> Without a better example or explanation of what you are trying to do it is
>> difficult

>
> You're right.
> Actually i'm parsing an xml file using pyrxp, which returns something
> like this:
> (tagName, attributes, list_of_children, spare)
> Where list_of_children might "be a list with elements that are 4-
> tuples or plain strings".
>
> In other terms, if I have something like this:
> ('<tagA><tagB>bobloblaw</tagB></tagA>')
> it's parsed like this:
> ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)...
>
> Fact is that my xml is much more deep... and I'm not sure how to
> resolve it


Resolve *what*? The problem isn't clear yet; at least to me. Above you
say what you get. What exactly do you want? Examples please.

Ciao,
Marc 'BlackJack' Rintsch

korovev76@gmail.com 10-26-2007 01:59 PM

Re: tuples within tuples
 

> Resolve *what*? The problem isn't clear yet; at least to me. Above you
> say what you get. What exactly do you want? Examples please.
>



Sorry for my poor english, but I meant: how can I obtain a list of A
and C starting from something like this?

(A,B,C,D)
that could be
('tagA', None, [('tagB', None, ['bobloblaw], None)], None)
but also
('tagA', None, description, None)
when I don't know if C is a tuple or not?

I guess that, at least, within the cicle I may test if C is a tuple
or not.. And then apply the same cicle for C... and so on

Am i right?


ciao
korovev






ciao
korovev


Duncan Booth 10-26-2007 02:00 PM

Re: tuples within tuples
 
korovev76@gmail.com wrote:

>
> [cut]
>>
>> Without a better example or explanation of what you are trying to do
>> it is difficult

>
> You're right.
> Actually i'm parsing an xml file using pyrxp, which returns something
> like this:
> (tagName, attributes, list_of_children, spare)
> Where list_of_children might "be a list with elements that are 4-
> tuples or plain strings".
>
> In other terms, if I have something like this:
> ('<tagA><tagB>bobloblaw</tagB></tagA>')
> it's parsed like this:
> ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)...
>
> Fact is that my xml is much more deep... and I'm not sure how to
> resolve it
>

Probably you want some sort of visitor pattern.

e.g. (warning untested pseudo code ahead)

def walkTree(tree, visitor):
tag, attrs, children, spare = tree
fn = getattr(visitor, 'visit_'+tag, None)
if not fn: fn = visitor.visitDefault
fn(tag, attrs, children, spare)

for child in children:
if isinstance(child, tuple):
walktree(child, visitor)
else:
visitor.visitContent(child)

class Visitor:
def visitDefault(self, t, a, c, s): pass
def visitContent(self, c): pass

.... then when you want to use it you subclass Visitor adding appropriate
visit_tagA, visit_tabB methods for the tags which interest you. You walk
the tree, and store whatever you want to save in your visitor subclass
instance.


J. Clifford Dyer 10-26-2007 02:14 PM

Re: tuples within tuples
 
On Fri, Oct 26, 2007 at 06:59:51AM -0700, korovev76@gmail.com wrote regarding Re: tuples within tuples:
>
> > Resolve *what*? The problem isn't clear yet; at least to me. Above you
> > say what you get. What exactly do you want? Examples please.
> >

>
>
> Sorry for my poor english, but I meant: how can I obtain a list of A
> and C starting from something like this?
>
> (A,B,C,D)
> that could be
> ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)
> but also
> ('tagA', None, description, None)
> when I don't know if C is a tuple or not?
>
> I guess that, at least, within the cicle I may test if C is a tuple
> or not.. And then apply the same cicle for C... and so on
>
> Am i right?
>
>
> ciao
> korovev


So, to clarify the piece that you still haven't explicitly said, you want to keep the tag name and children of every element in your XML document. (which is different from keeping (A, C) from tuple (A, B, C, D), because you want to modify C as well, and it's decendants.)

As a first solution, how about:

def reduceXML(node):
if type(node) == str:
return node
else:
return (node[0], reduceXML(node[2])

N.B. Beware of stack limitations.


Dennis Lee Bieber 10-26-2007 05:23 PM

Re: tuples within tuples
 
On Fri, 26 Oct 2007 06:59:51 -0700, korovev76@gmail.com declaimed the
following in comp.lang.python:

>
> (A,B,C,D)
> that could be
> ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)
> but also
> ('tagA', None, description, None)
> when I don't know if C is a tuple or not?
>
> I guess that, at least, within the cicle I may test if C is a tuple
> or not.. And then apply the same cicle for C... and so on
>

"C" isn't a tuple in your example either. It is a one-element list
(the single element INSIDE the list is a tuple whose third element is a
list containing a non-terminated string -- so the entire structure is
invalid)
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

korovev76@gmail.com 10-26-2007 07:58 PM

Re: tuples within tuples
 
On 26 Ott, 19:23, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
> > (A,B,C,D)
> > that could be
> > ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)

>
> "C" isn't a tuple in your example either. It is a one-element list
> (the single element INSIDE the list is a tuple whose third element is a
> list containing a non-terminated string -- so the entire structure is
> invalid)
>


i'm not sure what u mean with "the entire structure is invalid"...
that's exactly what I got while parsing...



Michael L Torrie 10-26-2007 08:26 PM

Re: tuples within tuples
 
korovev76@gmail.com wrote:
> On 26 Ott, 19:23, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
> > > (A,B,C,D)
>>> that could be
>>> ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)

>> "C" isn't a tuple in your example either. It is a one-element list
>> (the single element INSIDE the list is a tuple whose third element is a
>> list containing a non-terminated string -- so the entire structure is
>> invalid)
>>

>
> i'm not sure what u mean with "the entire structure is invalid"...
> that's exactly what I got while parsing...


Your structure is correct. Dennis just didn't read all the matching
parens and brackets properly.

>
>



--
Michael Torrie
Assistant CSR, System Administrator
Chemistry and Biochemistry Department
Brigham Young University
Provo, UT 84602
+1.801.422.5771



All times are GMT. The time now is 04:46 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.