Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > do you master list comprehensions?

Reply
Thread Tools

do you master list comprehensions?

 
 
Roel Schroeven
Guest
Posts: n/a
 
      12-16-2004
Stefan Behnel wrote:
>
> Nick Coghlan schrieb:
>
>>>>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>>>>> result = []
>>>>>> for d in data:

>>
>>
>> .>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>> .>>> from itertools import chain
>> .>>> result = "".join(chain(*data))
>> 'foobarbazmyyourholygrail'

>
>
> This is the first time I see that and I totally like the idea of writing
> ".>>>" instead of ">>>" at the beginning of a line. Thank you Dr. Dobb!
> It's unfortunate for c.l.py that Python uses ">>>" as the default prompt
> as it messes up the display on mail/news readers that provide "syntax
> highlighting" for quotes.


Off topic, but indeed: I use Quote Colors in Mozilla which is very nice
for reading mails or news posts with quotes, but it's very confusing
with Python's prompt.

Prepending every line with . is not an ideal solution though... I think
it gets tiresome very quickly.

--
"Codito ergo sum"
Roel Schroeven
 
Reply With Quote
 
 
 
 
Nick Coghlan
Guest
Posts: n/a
 
      12-16-2004
Roel Schroeven wrote:
> Stefan Behnel wrote:
>> This is the first time I see that and I totally like the idea of
>> writing ".>>>" instead of ">>>" at the beginning of a line. Thank you
>> Dr. Dobb! It's unfortunate for c.l.py that Python uses ">>>" as the
>> default prompt as it messes up the display on mail/news readers that
>> provide "syntax highlighting" for quotes.


I use Thunderbird, and started doing it so I could read my own posts. I did copy
it from someone, though (but I can't recall who).

The trick can also be useful for web tools that strip leading whitespace.

> Prepending every line with . is not an ideal solution though... I think
> it gets tiresome very quickly.


Aye, can't argue with that. It does have the virtues of reliability and
portability, though

Cheers,
Nick.

--
Nick Coghlan | http://www.velocityreviews.com/forums/(E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
 
 
 
Nick Coghlan
Guest
Posts: n/a
 
      12-16-2004
Nick Coghlan wrote:
> (FYI, I filed bug report #1085744 on SF about this)


And Raymond Hettinger was able to decipher my somewhat incoherent rambling (tip:
don't try to write bug reports in the wee hours of the morning) and produce a
potentially useful modification to PySequence_Tuple.

Anyway, I guess the results I got emphasizes the fact that it's important to
measure performance of the actual methods you're using on representative input
data, rather than relying on overly simple demonstrators.

And, of course, don't be *too* concerned about optimisations until you know you
actually have a performance problem. . .

Cheers,
Nick.

--
Nick Coghlan | (E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      12-16-2004
Nick Coghlan wrote:

> Roel Schroeven wrote:
>
>> Stefan Behnel wrote:
>>
>>> This is the first time I see that and I totally like the idea of
>>> writing ".>>>" instead of ">>>" at the beginning of a line. Thank you
>>> Dr. Dobb! It's unfortunate for c.l.py that Python uses ">>>" as the
>>> default prompt as it messes up the display on mail/news readers that
>>> provide "syntax highlighting" for quotes.

>
>
> I use Thunderbird, and started doing it so I could read my own posts. I
> did copy it from someone, though (but I can't recall who).
>
> The trick can also be useful for web tools that strip leading whitespace.
>
>> Prepending every line with . is not an ideal solution though... I
>> think it gets tiresome very quickly.

>
>
> Aye, can't argue with that. It does have the virtues of reliability and
> portability, though
>
> Cheers,
> Nick.
>

$ python
Python 2.4 (#1, Dec 4 2004, 20:10:33)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.ps1 = ".>>> "; sys.ps2 = ".... "

..>>> print """\
..... It isn't that hard"""
It isn't that hard
..>>>

Would it work, I wonder, with a leading space on the prompt? That might
be a valid change to the standard prompt. Let's see

>>> This line isn't really quoted three times.


regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
 
Reply With Quote
 
Keith Dart
Guest
Posts: n/a
 
      12-16-2004
Steve Holden wrote:
> Nick Coghlan wrote:
>
>> Roel Schroeven wrote:
>>
>>> Stefan Behnel wrote:
>>>
>>>> This is the first time I see that and I totally like the idea of
>>>> writing ".>>>" instead of ">>>" at the beginning of a line. Thank
>>>> you Dr. Dobb! It's unfortunate for c.l.py that Python uses ">>>" as
>>>> the default prompt as it messes up the display on mail/news readers
>>>> that provide "syntax highlighting" for quotes.

>>
>>
>>
>> I use Thunderbird, and started doing it so I could read my own posts.
>> I did copy it from someone, though (but I can't recall who).
>>
>> The trick can also be useful for web tools that strip leading whitespace.
>>
>>> Prepending every line with . is not an ideal solution though... I
>>> think it gets tiresome very quickly.

>>
>>
>>
>> Aye, can't argue with that. It does have the virtues of reliability
>> and portability, though
>>
>> Cheers,
>> Nick.
>>

> $ python
> Python 2.4 (#1, Dec 4 2004, 20:10:33)
> [GCC 3.3.3 (cygwin special)] on cygwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import sys
> >>> sys.ps1 = ".>>> "; sys.ps2 = ".... "

> .>>> print """\
> .... It isn't that hard"""
> It isn't that hard
> .>>>
>
> Would it work, I wonder, with a leading space on the prompt? That might
> be a valid change to the standard prompt. Let's see
>
> >>> This line isn't really quoted three times.


What I do is set Python's sys.ps1 variable to something else. I have a
module called "interactive" that I import implicitly by shell alias:

py='python -i -c '\''import interactive'\'

Which, among other things, sets the prompt to "Python> "

433 $ py
Python> print "This has no leader that screws up email programs."
This has no leader that screws up email programs.
Python>



--
\/ \/
(O O)
-- --------------------oOOo~(_)~oOOo----------------------------------------
Keith Dart <(E-Mail Removed)>
public key: ID: F3D288E4
================================================== ==========================
 
Reply With Quote
 
Roel Schroeven
Guest
Posts: n/a
 
      12-16-2004
Steve Holden wrote:
> Nick Coghlan wrote:
> $ python
> Python 2.4 (#1, Dec 4 2004, 20:10:33)
> [GCC 3.3.3 (cygwin special)] on cygwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import sys
> >>> sys.ps1 = ".>>> "; sys.ps2 = ".... "

> .>>> print """\
> .... It isn't that hard"""
> It isn't that hard
> .>>>
>
> Would it work, I wonder, with a leading space on the prompt? That might
> be a valid change to the standard prompt. Let's see
>
> >>> This line isn't really quoted three times.


Seems to work Now let's hope lots of people will use an approach like
that for code sent to the various Python newsgroups and mailing lists.

--
"Codito ergo sum"
Roel Schroeven
 
Reply With Quote
 
Kent Johnson
Guest
Posts: n/a
 
      12-16-2004
Keith Dart wrote:
> What I do is set Python's sys.ps1 variable to something else. I have a
> module called "interactive" that I import implicitly by shell alias:
>
> py='python -i -c '\''import interactive'\'
>
> Which, among other things, sets the prompt to "Python> "


You can do the same thing using a PYTHONSTARTUP file - see
http://docs.python.org/tut/node4.htm...00000000000000

You can change the prompts with
import sys
sys.ps1 = ' >>> '
sys.ps2 = ' ... '

Kent
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      12-16-2004
Kent Johnson wrote:
> You can do the same thing using a PYTHONSTARTUP file - see
> http://docs.python.org/tut/node4.htm...00000000000000
>
> You can change the prompts with
> import sys
> sys.ps1 = ' >>> '
> sys.ps2 = ' ... '


Very cool. I didn't know about this. Does anyone know how to make it
work with Pythonwin[1]? (Obviously, I can type the above in manually
every time, but I'd much rather have Pythonwin do this automatically for
me.)

Steve

[1] I'd do my example code at the command prompt, but I can't live
without copy-paste.
 
Reply With Quote
 
Fernando Perez
Guest
Posts: n/a
 
      12-16-2004
Kent Johnson wrote:

> Keith Dart wrote:
>> What I do is set Python's sys.ps1 variable to something else. I have a
>> module called "interactive" that I import implicitly by shell alias:
>>
>> py='python -i -c '\''import interactive'\'
>>
>> Which, among other things, sets the prompt to "Python> "

>
> You can do the same thing using a PYTHONSTARTUP file - see
> http://docs.python.org/tut/node4.htm...00000000000000
>
> You can change the prompts with
> import sys
> sys.ps1 = ' >>> '
> sys.ps2 = ' ... '


<blatant plug>

You might want to look at ipython:

http://ipython.scipy.org,

which provides you automatically with these prompts:

In [1]: for i in range(2):
...: print i,
...:
0 1

In [2]: 99*2
Out[2]: 198

In [3]: _2+1
Out[3]: 199

As a curiosity, ipython was actually born as a sys.ps1/2 hack, by assigning to
these objects whose __repr__ would give numbered prompts with results caching.
These days it's a full-blown pure python interpreter, not a $PYTHONSTARTUP
customization anymore, but it's an interesting little historical curiosity.
Especially if you start going very far with interactive customizations, you
might not want to rewrite all of ipython's 15K lines of code

</blatant plug>

Cheers,

f

 
Reply With Quote
 
Keith Dart
Guest
Posts: n/a
 
      12-17-2004
Fernando Perez wrote:

> <blatant plug>
>
> You might want to look at ipython:
>
> http://ipython.scipy.org,
>
>
> </blatant plug>
>


I did just recently install that. It looks very nice. Would make a great
interactive prompt for an IDE, as well.



--
\/ \/
(O O)
-- --------------------oOOo~(_)~oOOo----------------------------------------
Keith Dart <(E-Mail Removed)>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4 URL: <http://www.kdart.com/~kdart/public.key>
================================================== ==========================
 
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
2nd Post: VB Master-Details Edit (Separate Pages), Response Redirect to Master Page Databinding Issue... Sam ASP .Net 1 07-20-2006 06:17 PM
VB Master-Details Edit (Separate Pages), Response Redirect to Master Page Databinding Issue... Sam ASP .Net 0 07-20-2006 02:31 AM
Cross Page Post Back When Using Mixture of Master Pages and Non-Master Pages Jason@webbit.com ASP .Net 1 05-16-2006 02:00 PM
Master Pages Great! But what about Master User Controls? Electrified Research ASP .Net 5 10-11-2005 02:32 PM
seeking servlet "Master" keep getting "Master/servlet/Master" not found. Tomcat 5.0.25 Doug McCann Java 1 08-05-2004 09:16 PM



Advertisments