Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Unpaking Tuple

Reply
Thread Tools

Unpaking Tuple

 
 
Jussi Piitulainen
Guest
Posts: n/a
 
      10-09-2012
Dave Angel writes:

> On 10/09/2012 02:07 AM, Bob Martin wrote:
> > in 682592 20121008 232126 "Prasad, Ramit" wrote:

[snip mess]
> > How does one unpack this post?

>
> Since that's not the way it arrived here, i have to ask, how do you
> get these posts? Are you subscribed to individual messages by email
> via python.org? or are you using google-groups or some other
> indirection?
>
> In any reasonable mail program, you can see the source of a message.
> Most of the troubles i've seen here have been caused by people
> trying to send html mail to a text-based mailing list. But in the
> case you quote, the original message came here as text/plain, and
> well formatted.


I see a carriage return rendered as ^M at the end of every line from
Prasad's messages. Other than that, they are well-formatted plain text
for me, too.

I guess Prasad's system sends \r\n instead of \n\r (the DOS line-end)
and \r\n gets interpreted as a stray \r followed by end-of-line.
 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      10-09-2012
On 10/09/12 02:22, Jussi Piitulainen wrote:
>>> in 682592 20121008 232126 "Prasad, Ramit" wrote:

> [snip mess]
>>> How does one unpack this post?

>>
>> Since that's not the way it arrived here, i have to ask, how do you
>> get these posts?

>
> I see a carriage return rendered as ^M at the end of every line from
> Prasad's messages. Other than that, they are well-formatted plain text
> for me, too.
>
> I guess Prasad's system sends \r\n instead of \n\r (the DOS line-end)
> and \r\n gets interpreted as a stray \r followed by end-of-line.


Prasad's system is correctly sending the "right" order (DOS
line-ends are CR+LF = \r\n, not the other way around). However, it
might be that there is no CR+LF on the last line, or that one line
is missing the CR, so your viewer heuristic (vim does this) thinks
it has Unix NL-only line-endings and shows the ^M on all the lines
that have the CR. All for one stray line without.

Prasad's email came through cleanly here (gmane + Thunderbird).

-tkc




 
Reply With Quote
 
 
 
 
Jussi Piitulainen
Guest
Posts: n/a
 
      10-09-2012
Tim Chase writes:
> On 10/09/12 02:22, Jussi Piitulainen wrote:
> >>> in 682592 20121008 232126 "Prasad, Ramit" wrote:

> > [snip mess]
> >>> How does one unpack this post?
> >>
> >> Since that's not the way it arrived here, i have to ask, how do you
> >> get these posts?

> >
> > I see a carriage return rendered as ^M at the end of every line from
> > Prasad's messages. Other than that, they are well-formatted plain text
> > for me, too.
> >
> > I guess Prasad's system sends \r\n instead of \n\r (the DOS line-end)
> > and \r\n gets interpreted as a stray \r followed by end-of-line.

>
> Prasad's system is correctly sending the "right" order (DOS
> line-ends are CR+LF = \r\n, not the other way around).


You are right. I managed to confuse myself about the order of the two
characters while staring on a source that says the opposite of what I
said (http://en.wikipedia.org/wiki/Newline).

Doubly sorry about the noise (being both off-topic and incorrect).

> However, it might be that there is no CR+LF on the last line, or
> that one line is missing the CR, so your viewer heuristic (vim does
> this) thinks it has Unix NL-only line-endings and shows the ^M on
> all the lines that have the CR. All for one stray line without.


That doesn't sound robust. The problem is still quite rare for me.

> Prasad's email came through cleanly here (gmane + Thunderbird).


I'm on Gnus in Emacs, probably a few years out of date.
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      10-09-2012
On 2012-10-09, Bob Martin <(E-Mail Removed)> wrote:
> in 682592 20121008 232126 "Prasad, Ramit" <(E-Mail Removed)> wrote:
>>Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at =
>>03:08:38PM +0000, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_=
>>tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_=

>
> How does one unpack this post?


Yea, my newsreader doesn't like those posts either -- though they're
not as bad as what yours displays. Mine just shows "^M" strings all
at the end of every line.

--
Grant Edwards grant.b.edwards Yow! BARBARA STANWYCK makes
at me nervous!!
gmail.com
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      10-09-2012
On 10/09/12 07:05, Jussi Piitulainen wrote:
> Tim Chase writes:
>> However, it might be that there is no CR+LF on the last line,
>> or that one line is missing the CR, so your viewer heuristic
>> (vim does this) thinks it has Unix NL-only line-endings and
>> shows the ^M on all the lines that have the CR. All for one
>> stray line without.

>
> That doesn't sound robust. The problem is still quite rare for
> me.


Vim's heuristic is that, if *all* the lines end in CR+LF, it's a
DOS-formatted file; otherwise it's a Unix-style (LF) file with
spurious CRs in it (they just happen to come at the end of
most-but-not-all lines). It works quite robustly, since writing the
file back out will reliably put the CRs back where they were and
leave the non-CR'ed lines as they were with only LF. Vim makes it
pretty easy to remove the spurious CRs and then change the
file-format from Unix to DOS line-endings and write it out if that's
what you want[1].

-tkc


[1]
:%s/\r$
:set ff=dos
:w

which (1) removes the spurious/inconsistent CRs, (2) tells vim that
newlines should be written as CR+LF when writing and (3) writes the
file back out to disk.
 
Reply With Quote
 
Prasad, Ramit
Guest
Posts: n/a
 
      10-09-2012
Bob Martin wrote

> in 682592 20121008 232126 "Prasad, Ramit" <(E-Mail Removed)> wrote:

> >Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at =
> >03:08:38PM +0000, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_=
> >tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_=
> >tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y=
> >ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D=
> >=0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E=
> >1] on linux2=0D=0A> Type "help", "copyright","credits" or "license" for mo=
> >re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail=
> >=3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A=

> >> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! =

> >Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b=
> >ar'?=0D=0A> =0D=0A> Regards,=0D=0A> Thomas=0D=0A> --=0D=0A=0D=0AI think yo=
> >u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea=
> >ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th=
> >e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2=
> >else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le=
> >n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e=
> >mail is confidential and subject to important disclaimers and=0D=0Aconditio=
> >ns including on offers for the purchase or sale of=0D=0Asecurities, accurac=
> >y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri=
> >vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg=
> >an=2Ecom/pages/disclosures/email=2E

>
> How does one unpack this post?
> --


Hmm, I am not sure why that happened. For reference:
http://mail.python.org/pipermail/pyt...er/632603.html
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
 
Reply With Quote
 
Robert Miles
Guest
Posts: n/a
 
      11-19-2012
On 10/9/2012 1:07 AM, Bob Martin wrote:
> in 682592 20121008 232126 "Prasad, Ramit" <(E-Mail Removed)> wrote:
>> Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at =
>> 03:08:38PM +0000, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_=
>> tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_=
>> tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y=
>> ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D=
>> =0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E=
>> 1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo=
>> re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail=
>> =3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A=
>>> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! =

>> Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b=
>> ar'?=0D=0A> =0D=0A> Regards,=0D=0A> Thomas=0D=0A> --=0D=0A=0D=0AI think yo=
>> u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea=
>> ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th=
>> e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2=
>> else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le=
>> n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e=
>> mail is confidential and subject to important disclaimers and=0D=0Aconditio=
>> ns including on offers for the purchase or sale of=0D=0Asecurities, accurac=
>> y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri=
>> vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg=
>> an=2Ecom/pages/disclosures/email=2E

>
> How does one unpack this post?


There are a number of programs for converting ends of lines between
Linux format, Windows format, and Mac formats. You could try running
all of those programs your operating system provides on that text,
then checking which one of them gives the most readable results.

 
Reply With Quote
 
Hans Mulder
Guest
Posts: n/a
 
      11-19-2012
On 9/10/12 08:07:32, Bob Martin wrote:
> in 682592 20121008 232126 "Prasad, Ramit" <(E-Mail Removed)> wrote:
>> Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at =
>> 03:08:38PM +0000, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_=
>> tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_=
>> tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y=
>> ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D=
>> =0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E=
>> 1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo=
>> re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail=
>> =3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A=
>>> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! =

>> Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b=
>> ar'?=0D=0A> =0D=0A> Regards,=0D=0A> Thomas=0D=0A> --=0D=0A=0D=0AI think yo=
>> u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea=
>> ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th=
>> e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2=
>> else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le=
>> n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e=
>> mail is confidential and subject to important disclaimers and=0D=0Aconditio=
>> ns including on offers for the purchase or sale of=0D=0Asecurities, accurac=
>> y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri=
>> vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg=
>> an=2Ecom/pages/disclosures/email=2E

>
> How does one unpack this post?


How about:

print re.sub('^>* ', '', this_post, flags=re.M).decode('quopri')


Hope this helps,

-- HansM
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unpaking sequences of unknown length Anthra Norell Python 4 08-28-2006 07:06 AM
Why tuple with one item is no tuple Gregor Horvath Python 37 03-30-2005 06:58 AM
Easily convert unicode tuple to python string tuple??? Michal Mikolajczyk Python 1 04-20-2004 08:37 PM
Re: Easily convert unicode tuple to python string tuple??? Jeff Epler Python 0 04-20-2004 03:36 PM
Re: Easily convert unicode tuple to python string tuple??? Bill Scherer Python 0 04-20-2004 03:34 PM



Advertisments