Re: Why generators take long time?

2016-01-19 Thread Arshpreet Singh
On Tuesday, 19 January 2016 12:58:28 UTC+5:30, Arshpreet Singh  wrote:
> I was playing with Generators and found that using Generators time is bit 
> more than list-comprehensions or I am doing it wrong?
> 
> 
> Function with List comprehensions:
> 
> def sum_text(number_range):
> return sum([i*i for i in xrange(number_range)])
> 
> %timeit sum_text(1)
> 1 loops, best of 3: 14.8 s per loop
> 
> Using generator Expressions:
> 
> def sum_text(number_range):
> 
> return sum((i*i for i in xrange(number_range))) 
> 
> %timeit sum_text(1)
> 
> 1 loops, best of 3: 16.4 s per loop

Or even when I use generator function:

def sum_text(number_range):
yield sum((i*i for i in xrange(number_range)))

another function to get value:

def get_sum():
for i in sum_text(1):
return i

%timeit get_sum()
1 loops, best of 3: 16.1 s per loop

-- 
https://mail.python.org/mailman/listinfo/python-list


Fwd: Delivery Status Notification (Failure)

2016-01-19 Thread Erik Vandamme
Forwarded conversation
Subject: Python Path Wondows 10


From: *Erik Vandamme* 
Date: Tue, Jan 19, 2016 at 7:01 PM
To: [email protected]


I've installed python 3.5.1 form the org
But pygi-iao-3.18 can not find a path to it??
I can not find it myself, maybe eleveated acces required where it stores it?
When I  re-install and modify (as administrator) it claims it is going to
 c:\Users\Owner\AppData\Local\Programs\Python\Python35
it and the browse button are grayed ou??
Erik

--
From: *Mail Delivery Subsystem* 
Date: Tue, Jan 19, 2016 at 7:01 PM
To: [email protected]


Delivery to the following recipient failed permanently:

 [email protected]

Technical details of permanent failure:
Google tried to deliver your message, but it was rejected by the server for
the recipient domain python.org by mail.python.org. [188.166.95.178].

The error that the other server returned was:
550-5.1.1 : Recipient address rejected: User unknown
550 5.1.1 Contact your postmaster/admin for assistance. Please provide the
following information in your problem report: time (Jan 19 03:01:53) and
client (74.125.82.54).


- Original message -

DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20120113;
h=mime-version:date:message-id:subject:from:to:content-type;
bh=dYYeSBcA1ClZqiFWesurMsgc0sxKsqVx0fw41kEznOo=;
b=j0bYuIdiDu6yjJ9Ul19l+Dcc4Izg6YMBcMJBkyBkaMxnkhiw1hHYp+KV2i7a/nXnpP

 8fr/7D+7h74XXFxXsy9ghBd645etuBJtOl1ww4O49+uEEqiJ3/D1pbgzXR0GEtbBqud9

 EBal2yPcGI3Rdfz3Lk3bR4rJixcHvQ6ofEIk31hqDukrLKxOFFOUS0JYCqWxbhmbYGRD

 DwJc4pJDV+3fCOILn60IOxGEsPQanfB+T//3X8hhR/8/v46oZFFeIkFlEejh5X8+8Udq

 Udnj4WvmeWNsCCGuawC8UY9h88HMLtcU9VSQWcd8ZBFDi+BbNBFD2VNl8y9H+f93XwBP
 gRGQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20130820;
h=x-gm-message-state:mime-version:date:message-id:subject:from:to
 :content-type;
bh=dYYeSBcA1ClZqiFWesurMsgc0sxKsqVx0fw41kEznOo=;
b=hy+AY7FkwNtF6GzVJ+6tqGrYR9mkHYij/n/31y/qdyOqneqGSx0choyAfXNwo949e7

 eNQszWh2/YxueyxnjEhr16yaEXN4KU0H8HZDITQ28Gljxt0svOaom0UOAfezVxeA8VuY

 kJUWycrlFwrsLREb7+lBWSug6ynM/GMZfEPHweZpGRQl64YcHSzw+P+nTmhxP/pR6Maa

 Or1vYHbLzuOdRsuDXUdsV9iH2IqrVTD/E52cXHnp+IzHGyf7HcGlhSMPcMl4LPbDww7l

 9qeK4liUh3Et753GE5x0v3jRZ0LNv6AyxlxdBOc6NR2za1ZKcXWFhXHyOaBupJLIhb8J
 jBtQ==
X-Gm-Message-State:
AG10YOTuVE+bbveIDaEA1admcil9KRsgU5m7pVm6UL/nyj8Jur5TlUXqkRCYAKcU4R5cxxN0r0iVp/2Jf67Q/Q==
MIME-Version: 1.0
X-Received: by 10.28.47.213 with SMTP id v204mr16545918wmv.69.1453190513503;
 Tue, 19 Jan 2016 00:01:53 -0800 (PST)
Received: by 10.27.226.21 with HTTP; Tue, 19 Jan 2016 00:01:53 -0800 (PST)
Date: Tue, 19 Jan 2016 19:01:53 +1100
Message-ID: 
Subject: Python Path Wondows 10
From: Erik Vandamme 
To: [email protected]
Content-Type: multipart/alternative; boundary=001a11422b205935810529ab48b0
-- 
https://mail.python.org/mailman/listinfo/python-list


Use of Python logo

2016-01-19 Thread Carolina Nunez
Hi,

We at Chetu as Python developers would like to include the Python logo on our 
website, please advise if we can do so.


Thanks & regards,

Carolina Nunez
Web & Trade Show Marketing Manager
Phone: (954) 342-5676 Ext. 144
[UpdatedLogo]   [Inc5000_medallion_Email Signature] 
[cid:[email protected]]
Discover the solutions... http://www.chetu.com/
We Blog our Experience! http://www.chetu.com/blogs



-- 
https://mail.python.org/mailman/listinfo/python-list


web scraping help / better way to do it ?

2016-01-19 Thread Matt
Beginner python user (3.5) and trying to scrape this page and get the ladder
-   www.afl.com.au/ladder .  Its dynamic content so I used lynx -dump to get
a  txt file and parsing that.

Here is the code 

# import lynx -dump txt file
f = open('c:/temp/afl2.txt','r').read()

# Put import txt file into list 
afl_list = f.split(' ')

#here are the things we want to search for
search_list = ['FRE', 'WCE', 'HAW', 'SYD', 'RICH', 'WB', 'ADEL', 'NMFC',
'PORT', 'GEEL', 'GWS', 'COLL', 'MELB', 'STK', 'ESS', 'GCFC', 'BL', 'CARL']

def build_ladder():
for l in search_list:
output_num = afl_list.index(l)
list_pos = output_num -1
ladder_pos = afl_list[list_pos]
print(ladder_pos + ' ' + '-' + ' ' + l)

build_ladder()


Which outputs this.

1 - FRE
2 - WCE
3 - HAW
4 - SYD
5 - RICH
6 - WB
7 - ADEL
8 - NMFC
9 - PORT
10 - GEEL
* - GWS
12 - COLL
13 - MELB
14 - STK
15 - ESS
16 - GCFC
17 - BL
18 - CARL

Notice that number 11 is missing because my script picks up "GWS" which is
located earlier in the page.  What is the best way to skip that (and get the
"GWS" lower down in the txt file) or am I better off approaching the code in
a different way?


TIA

Matt




-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Use of Python logo

2016-01-19 Thread Jussi Piitulainen
Carolina Nunez writes:

> We at Chetu as Python developers would like to include the Python logo
> on our website, please advise if we can do so.

See here: . That's the entry
titled "Python Logo" in the Community tab at the top of the python.org
front page, also linked at the bottom of the page.

They encourage the use and provide versions of the logo and guidelines,
and there's an email address to use when still in doubt.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why generators take long time?

2016-01-19 Thread Steven D'Aprano
On Tue, 19 Jan 2016 06:27 pm, Arshpreet Singh wrote:

> 
> I was playing with Generators and found that using Generators time is bit
> more than list-comprehensions or I am doing it wrong?

Generators may have slightly more overhead than iterating over a list. They
save memory, not time.

On my machine, I see a small difference of about 15% between summing a list
comp or a generator expression:

[steve@ando ~]$ python -m timeit "sum([i for i in xrange(1000)])"
1 loops, best of 3: 102 usec per loop

[steve@ando ~]$ python -m timeit "sum(i for i in xrange(1000))"
1 loops, best of 3: 117 usec per loop



But both are MUCH slower than summing the xrange object itself:

[steve@ando ~]$ python -m timeit "sum(xrange(1000))"
1 loops, best of 3: 21.4 usec per loop



The generator has to pause execution of the code, then start it up again,
over and over again, while the list comprehension can run without
interruption, then iterate over that list.

This is, I think, the fastest way to iterate over the objects, which
eliminates as much of the overhead as possible:


[steve@ando ~]$ python -m timeit -s "from collections import deque" 
-s "it = iter([i for i in xrange(1000)])" "deque(it, maxlen=0)"
100 loops, best of 3: 0.913 usec per loop


[steve@ando ~]$ python -m timeit -s "from collections import deque" 
-s "it = (i for i in xrange(1000))" "deque(it, maxlen=0)"
100 loops, best of 3: 0.965 usec per loop


which is about a 6% difference. So if you eliminate everything else, and
just iterate over a pre-populated list as fast as possible, versus a simple
generator expression, the list is about 6% faster.

BUT there is a lot of variation in these timings. For the list comprehension
version, I ran it four times and got these four results:

100 loops, best of 3: 0.913 usec per loop
100 loops, best of 3: 0.911 usec per loop
100 loops, best of 3: 0.977 usec per loop
100 loops, best of 3: 0.938 usec per loop

and for the generator expression:

100 loops, best of 3: 0.965 usec per loop
100 loops, best of 3: 0.914 usec per loop
100 loops, best of 3: 0.981 usec per loop
100 loops, best of 3: 0.931 usec per loop

So there's plenty of random variation in the times.




-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


bgpic doesn't show background image for turtle

2016-01-19 Thread Jean Richelle
Hello,
I'm trying to have a background image for the turtle graphics.
bgpic("filename.gif") works correctly in terminal mode but I'm
getting an error when it is in a python program :

  File "VisuProg3.0.564.py", line 2206, in afficheLabyrinthe
bgpic("L1.gif")
  File "", line 8, in bgpic
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/
python3.5/turtle.py", line 1482, in bgpic
self._setbgpic(self._bgpic, self._bgpics[picname])
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/
python3.5/turtle.py", line 738, in _setbgpic
self.cv.itemconfig(item, image=image)
  File "", line 1, in itemconfig
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/
python3.5/tkinter/__init__.py", line 2417, in itemconfigure
return self._configure(('itemconfigure', tagOrId), cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/
python3.5/tkinter/__init__.py", line 1321, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage64" doesn't exist

Regards,

Jean

P.S.

Note that the image file seems to have been corretly located because 
if the file is not there the error message is telling it :

  File "VisuProg3.0.564.py", line 2206, in afficheLabyrinthe
bgpic("L1.gif")
  File "", line 8, in bgpic
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/
python3.5/turtle.py", line 1481, in bgpic
self._bgpics[picname] = self._image(picname)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/
python3.5/turtle.py", line 479, in _image
return TK.PhotoImage(file=filename)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/
python3.5/tkinter/__init__.py", line 3393, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/
python3.5/tkinter/__init__.py", line 3349, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "L1.gif": no such file or directory


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why generators take long time?

2016-01-19 Thread Oscar Benjamin
On 19 Jan 2016 10:16, "Steven D'Aprano"  wrote:
>
> [steve@ando ~]$ python -m timeit -s "from collections import deque"
> -s "it = iter([i for i in xrange(1000)])" "deque(it, maxlen=0)"
> 100 loops, best of 3: 0.913 usec per loop
>
>
> [steve@ando ~]$ python -m timeit -s "from collections import deque"
> -s "it = (i for i in xrange(1000))" "deque(it, maxlen=0)"
> 100 loops, best of 3: 0.965 usec per loop

Surely the iterator in the above benchmarks is consumed during the first of
100 loops. Subsequent loops are just calling next on an exhausted
iterator.

--
Oscar
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: web scraping help / better way to do it ?

2016-01-19 Thread Peter Otten
Matt wrote:

> Beginner python user (3.5) and trying to scrape this page and get the
> ladder
> -   www.afl.com.au/ladder .  Its dynamic content so I used lynx -dump to
> get
> a  txt file and parsing that.
> 
> Here is the code
> 
> # import lynx -dump txt file
> f = open('c:/temp/afl2.txt','r').read()
> 
> # Put import txt file into list
> afl_list = f.split(' ')
> 
> #here are the things we want to search for
> search_list = ['FRE', 'WCE', 'HAW', 'SYD', 'RICH', 'WB', 'ADEL', 'NMFC',
> 'PORT', 'GEEL', 'GWS', 'COLL', 'MELB', 'STK', 'ESS', 'GCFC', 'BL', 'CARL']
> 
> def build_ladder():
> for l in search_list:
> output_num = afl_list.index(l)
> list_pos = output_num -1
> ladder_pos = afl_list[list_pos]
> print(ladder_pos + ' ' + '-' + ' ' + l)
> 
> build_ladder()
> 
> 
> Which outputs this.
> 
> 1 - FRE
> 2 - WCE
> 3 - HAW
> 4 - SYD
> 5 - RICH
> 6 - WB
> 7 - ADEL
> 8 - NMFC
> 9 - PORT
> 10 - GEEL
> * - GWS
> 12 - COLL
> 13 - MELB
> 14 - STK
> 15 - ESS
> 16 - GCFC
> 17 - BL
> 18 - CARL
> 
> Notice that number 11 is missing because my script picks up "GWS" which is
> located earlier in the page.  What is the best way to skip that (and get
> the "GWS" lower down in the txt file) or am I better off approaching the
> code in a different way?

If you look at the html source you'll see that the desired "GWS" is inside a 
table, together with the other abbreviations. To extract (parts of) that 
table you should use a tool that understands the structure of html.

The most popular library to parse html with Python is BeautifulSoup, but my 
example uses lxml:

$ cat ladder.py
import urllib.request
import io
import lxml.html

def first(row, xpath):
return row.xpath(xpath)[0].strip()

html = urllib.request.urlopen("http://www.afl.com.au/ladder";).read()
tree = lxml.html.parse(io.BytesIO(html))

for row in tree.xpath("//tr")[1:]:
print(
first(row, ".//td[1]/span/text()"),
first(row, ".//abbr/text()"))

$ python3 ladder.py
1 FRE
2 WCE
3 HAW
4 SYD
5 RICH
6 WB
7 ADEL
8 NMFC
9 PORT
10 GEEL
11 GWS
12 COLL
13 MELB
14 STK
15 ESS
16 GCFC
17 BL
18 CARL


Someone with better knowledge of XPath could probably avoid some of the 
postprocessing I do in Python.

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: web scraping help / better way to do it ?

2016-01-19 Thread Matt


> -Original Message-
> From: Python-list [mailto:python-list-
> [email protected]] On Behalf Of Peter Otten
> Sent: Tuesday, 19 January 2016 9:30 PM
> To: [email protected]
> Subject: Re: web scraping help / better way to do it ?
> 
> Matt wrote:
> 
> > Beginner python user (3.5) and trying to scrape this page and get the
> > ladder
> > -   www.afl.com.au/ladder .  Its dynamic content so I used lynx -dump to
> > get
> > a  txt file and parsing that.
> >
> > Here is the code
> >
> > # import lynx -dump txt file
> > f = open('c:/temp/afl2.txt','r').read()
> >
> > # Put import txt file into list
> > afl_list = f.split(' ')
> >
> > #here are the things we want to search for search_list = ['FRE',
> > 'WCE', 'HAW', 'SYD', 'RICH', 'WB', 'ADEL', 'NMFC', 'PORT', 'GEEL',
> > 'GWS', 'COLL', 'MELB', 'STK', 'ESS', 'GCFC', 'BL', 'CARL']
> >
> > def build_ladder():
> > for l in search_list:
> > output_num = afl_list.index(l)
> > list_pos = output_num -1
> > ladder_pos = afl_list[list_pos]
> > print(ladder_pos + ' ' + '-' + ' ' + l)
> >
> > build_ladder()
> >
> >
> > Which outputs this.
> >
> > 1 - FRE
> > 2 - WCE
> > 3 - HAW
> > 4 - SYD
> > 5 - RICH
> > 6 - WB
> > 7 - ADEL
> > 8 - NMFC
> > 9 - PORT
> > 10 - GEEL
> > * - GWS
> > 12 - COLL
> > 13 - MELB
> > 14 - STK
> > 15 - ESS
> > 16 - GCFC
> > 17 - BL
> > 18 - CARL
> >
> > Notice that number 11 is missing because my script picks up "GWS"
> > which is located earlier in the page.  What is the best way to skip
> > that (and get the "GWS" lower down in the txt file) or am I better off
> > approaching the code in a different way?
> 
> If you look at the html source you'll see that the desired "GWS" is inside
a
> table, together with the other abbreviations. To extract (parts of) that
table
> you should use a tool that understands the structure of html.
> 
> The most popular library to parse html with Python is BeautifulSoup, but
my
> example uses lxml:
> 
> $ cat ladder.py
> import urllib.request
> import io
> import lxml.html
> 
> def first(row, xpath):
> return row.xpath(xpath)[0].strip()
> 
> html = urllib.request.urlopen("http://www.afl.com.au/ladder";).read()
> tree = lxml.html.parse(io.BytesIO(html))
> 
> for row in tree.xpath("//tr")[1:]:
> print(
> first(row, ".//td[1]/span/text()"),
> first(row, ".//abbr/text()"))
> 
> $ python3 ladder.py
> 1 FRE
> 2 WCE
> 3 HAW
> 4 SYD
> 5 RICH
> 6 WB
> 7 ADEL
> 8 NMFC
> 9 PORT
> 10 GEEL
> 11 GWS
> 12 COLL
> 13 MELB
> 14 STK
> 15 ESS
> 16 GCFC
> 17 BL
> 18 CARL
> 
> 
> Someone with better knowledge of XPath could probably avoid some of the
> postprocessing I do in Python.
> 
> --
 Thanks Peter, you opened my eyes to a half dozen things here, just what I
needed. 

Much appreciated

Cheers
- Matt


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Use of Python logo

2016-01-19 Thread Dominik Wronski
Hi,

on Python Software Foundation website you can find trademark usage policy 
https://www.python.org/psf/trademarks/ 
It’s said there:
"Use of unaltered PSF-provided logos on websites, brochures, and product 
packaging. The "intertwined snakes" graphic alone is an unaltered version, 
whether or not accompanied by the words in PSF-provided logos. Non-commercial 
uses to promote the Python programming language are allowed, as are all 
nominative uses as described in the section "Uses that Never Require Approval 
". Any 
other commercial uses require prior written permission from PSF.”

I believe that in your case you have to obtain written permission from PSF.

/Dominik

 


> On 18 Jan 2016, at 21:33, Carolina Nunez  wrote:
> 
> Hi,
> 
> We at Chetu as Python developers would like to include the Python logo on our 
> website, please advise if we can do so.
> 
> 
> Thanks & regards,
> 
> Carolina Nunez
> Web & Trade Show Marketing Manager
> Phone: (954) 342-5676 Ext. 144
> [UpdatedLogo]   [Inc5000_medallion_Email Signature] 
> [cid:[email protected]]
> Discover the solutions... http://www.chetu.com/
> We Blog our Experience! http://www.chetu.com/blogs
> 
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error handling with @parallel decorator

2016-01-19 Thread Ankur Agrawal
Thanks a lot Steven for your reply. I got the issue, it was my own
FabricException class, when I started using Exception then I could catch
the exception successfully and then I got the type of exception as well by
using your suggested type(err). Your code snippet did help me to find the
issue sooner.

Thanks,
Ankur

On Sun, Jan 17, 2016 at 11:16 PM Steven D'Aprano <
[email protected]> wrote:

> On Monday 18 January 2016 17:15, Ankur Agrawal wrote:
>
> > I am trying to catch Abort exception. When I use fabric's run(...)
> method,
> > the host it tries to connect is not available and so it aborts with
> > connect time out exception. I am not able to catch it. Following is a two
> > different ways of code snippet-
> > First I try following  -
> >
> > class FabricException(Exception):
> > pass
> >
> > with settings(abort_exception = FabricException):
> >
> > try:
> > output = run(command)
> > except FabricException:
> > print 'inside exception'
> > LOG.debug("inside exception")
> >
> > It didn't go in exception block. Instead it threw -
> > NetworkError: Timed out trying to connect to
> pqaltsnas300.corp.intuit.net
> > (tried 1 time)
> > Aborting.
> > SystemExit: 1
>
> Are you sure that the exception is being raised where you think it is being
> raised? You think that it is being raised by run(command), but it is
> possible that the exception is occurring somewhere else?
>
> Don't catch the exception at all, and read the *entire* traceback. It will
> show you what line is raising the error.
>
> Then, surround that line with:
>
> try:
> the line that fails
> except Exception as err:
> print err
> print type(err)
> LOG.debug(err)
> raise
>
> This will tell you exactly what the exception type actually is. Perhaps you
> are trying to catch the wrong thing.
>
> VERY IMPORTANT: catching all exceptions in this way should nearly always
> only be used for debugging purposes. Don't do that in production.
>
> https://realpython.com/blog/python/the-most-diabolical-python-antipattern/
>
>
>
>
> --
> Steve
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why generators take long time?

2016-01-19 Thread Arshpreet Singh
On Tuesday, 19 January 2016 15:42:16 UTC+5:30, Steven D'Aprano  wrote:
 
> [steve@ando ~]$ python -m timeit -s "from collections import deque" 
> -s "it = iter([i for i in xrange(1000)])" "deque(it, maxlen=0)"
> 100 loops, best of 3: 0.913 usec per loop
> 
> 
> [steve@ando ~]$ python -m timeit -s "from collections import deque" 
> -s "it = (i for i in xrange(1000))" "deque(it, maxlen=0)"
> 100 loops, best of 3: 0.965 usec per loop

Thanks Steven, it was really helpful answer.  

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why generators take long time?

2016-01-19 Thread Jason Swails
On Tue, Jan 19, 2016 at 2:27 AM, Arshpreet Singh  wrote:

>
> I was playing with Generators and found that using Generators time is bit
> more than list-comprehensions or I am doing it wrong?
>
>
> Function with List comprehensions:
>
> def sum_text(number_range):
> return sum([i*i for i in xrange(number_range)])
>
> %timeit sum_text(1)
> 1 loops, best of 3: 14.8 s per loop
>
> Using generator Expressions:
>
> def sum_text(number_range):
>
> return sum((i*i for i in xrange(number_range)))
>
> %timeit sum_text(1)
>
> 1 loops, best of 3: 16.4 s per loop
>

​Steven already pointed out the additional overhead in a generator
expression vs. a list comprehension.  In addition to the memory savings you
get via generator expressions, though, you can also get significant time
savings when generator expressions have the ability to short-circuit.

For instance, have a look at the following:

In [1]: import random

In [2]: %timeit all(random.random() < 0.5 for i in range(1000))
The slowest run took 4.85 times longer than the fastest. This could mean
that an intermediate result is being cached
10 loops, best of 3: 3.57 µs per loop

In [3]: %timeit all([random.random() < 0.5 for i in range(1000)])
1000 loops, best of 3: 422 µs per loop

In [4]: %timeit any(random.random() < 0.5 for i in range(1000))
10 loops, best of 3: 3.18 µs per loop

In [5]: %timeit any([random.random() < 0.5 for i in range(1000)])
1000 loops, best of 3: 408 µs per loop

This is using IPython with Python 3.5.  The difference here is that for
functions that short-circuit (like any and all), the generator expression
does not have to exhaust all of its elements (particularly since for each
element there's a 50-50 chance of being True or False in each case).  In
this case, the difference is a couple orders of magnitude.  The larger the
range argument is, the bigger this difference.

Also, in Python 2, the generator expression does not leak into the global
namespace, while the list comprehension does:

Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> list(i for i in range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> i
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'i' is not defined
>>> [i for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> i
9

Python 3 does not leak the iterator variable in either case.  However, it
would be madness to have code actually relying on this behavior :).

At the end of the day, I use list comprehensions in the following
circumstances:

- I *know* I won't blow memory with a too-large list
- I want to iterate over the object multiple times or I want/may want
non-sequential access
- I know I want all the elements I'm creating (i.e., no chance of
short-circuiting)

I use generator expressions when

- I *might* want to

All the best,
Jason

P.S. There is a "cross-over" point where the memory requirements of the
list comp passes the generator overhead.  For instance:


In [17]: %timeit sum(i for i in range(1000))
1 loops, best of 3: 2.08 s per loop

In [18]: %timeit sum([i for i in range(1000)])
1 loops, best of 3: 1.86 s per loop

In [19]: %timeit sum(i for i in range(1))
1 loops, best of 3: 21.8 s per loop

In [20]: %timeit sum([i for i in range(1)])
1 loops, best of 3: 26.1 s per loop

-- 
Jason M. Swails
BioMaPS,
Rutgers University
Postdoctoral Researcher
-- 
https://mail.python.org/mailman/listinfo/python-list


Is this an attribute?

2016-01-19 Thread Robert
Hi,

When I read a code snippet below, I find I don't know what 'self.framelogprob'
is on the child class.



// parent class
class _BaseHMM(BaseEstimator):
def __init__(self, n_components=1,
 startprob_prior=1.0, transmat_prior=1.0,
 algorithm="viterbi", random_state=None,
 n_iter=10, tol=1e-2, verbose=False,
 params=string.ascii_letters,
 init_params=string.ascii_letters):
self.n_components = n_components
..

def score_samples(self, X, lengths=None):
X = check_array(X)
n_samples = X.shape[0]
logprob = 0
posteriors = np.zeros((n_samples, self.n_components))
for i, j in iter_from_X_lengths(X, lengths):
framelogprob = self._compute_log_likelihood(X[i:j])
...
return logprob, posteriors

// child class
class StubHMM(_BaseHMM):
def _compute_log_likelihood(self, X):
return self.framelogprob
-

On Python web, it says that things after dot, such as a class name, are 
attributes. From this definition, 'framelogprob' is an attribute. But when
I run the command on Canopy:

h.framelogprob
---
AttributeErrorTraceback (most recent call last)
 in ()
> 1 h.framelogprob

AttributeError: 'StubHMM' object has no attribute 'framelogprob' 


it doesn't recognize it as an attribute. What is wrong with my 
understanding?


Thanks,


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why generators take long time?

2016-01-19 Thread Jason Swails
On Tue, Jan 19, 2016 at 3:19 PM, Jason Swails 
wrote:

>
> I use generator expressions when
>
> - I *might* want to
>

​I forgot to finish my thought here.  I use generator expressions when I
don't want to worry about memory, there's a decent chance of
short-circuiting​, or I just want to do some simple iteration where the
iterator will not be the bottleneck (which is almost always).

-- 
Jason M. Swails
BioMaPS,
Rutgers University
Postdoctoral Researcher
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is this an attribute?

2016-01-19 Thread Peter Otten
Robert wrote:

> Hi,
> 
> When I read a code snippet below, I find I don't know what
> 'self.framelogprob' is on the child class.
> 
> 
> 
> // parent class
> class _BaseHMM(BaseEstimator):
> def __init__(self, n_components=1,
>  startprob_prior=1.0, transmat_prior=1.0,
>  algorithm="viterbi", random_state=None,
>  n_iter=10, tol=1e-2, verbose=False,
>  params=string.ascii_letters,
>  init_params=string.ascii_letters):
> self.n_components = n_components
> ..
> 
> def score_samples(self, X, lengths=None):
> X = check_array(X)
> n_samples = X.shape[0]
> logprob = 0
> posteriors = np.zeros((n_samples, self.n_components))
> for i, j in iter_from_X_lengths(X, lengths):
> framelogprob = self._compute_log_likelihood(X[i:j])
> ...
> return logprob, posteriors
> 
> // child class
> class StubHMM(_BaseHMM):
> def _compute_log_likelihood(self, X):
> return self.framelogprob
> -
> 
> On Python web, it says that things after dot, such as a class name, are
> attributes. From this definition, 'framelogprob' is an attribute. But when
> I run the command on Canopy:
> 
> h.framelogprob
> 
---
> AttributeErrorTraceback (most recent call
> last)  in ()
> > 1 h.framelogprob
> 
> AttributeError: 'StubHMM' object has no attribute 'framelogprob'
> 
> 
> it doesn't recognize it as an attribute. What is wrong with my
> understanding?

When you are reading a book, do you expect to be able to understand every 
arbitrarily picked sentence without any idea about the surrounding text?

Widen your view a bit -- the StubHMM class is in a file test_base.py and 
therefore it is likely that it is supposed to help with testing rather than 
to be used standalone. When you read the complete module to understand more 
of the context or just use a tool like grep you'll find the following 
snippets:

h = StubHMM(2)
h.transmat_ = [[0.7, 0.3], [0.3, 0.7]]
h.startprob_ = [0.5, 0.5]
h.framelogprob = self.framelogprob

h = StubHMM(n_components)
h.framelogprob = self.framelogprob

So the attribute is set "from the outside". I wouldn't do that, I'd rather 
add initializer arguments, but that wasn't the question...


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is this an attribute?

2016-01-19 Thread Robert
On Tuesday, January 19, 2016 at 3:52:12 PM UTC-5, Peter Otten wrote:
> Robert wrote:
> 
> > Hi,
> > 
> > When I read a code snippet below, I find I don't know what
> > 'self.framelogprob' is on the child class.
> > 
> > 
> > 
> > // parent class
> > class _BaseHMM(BaseEstimator):
> > def __init__(self, n_components=1,
> >  startprob_prior=1.0, transmat_prior=1.0,
> >  algorithm="viterbi", random_state=None,
> >  n_iter=10, tol=1e-2, verbose=False,
> >  params=string.ascii_letters,
> >  init_params=string.ascii_letters):
> > self.n_components = n_components
> > ..
> > 
> > def score_samples(self, X, lengths=None):
> > X = check_array(X)
> > n_samples = X.shape[0]
> > logprob = 0
> > posteriors = np.zeros((n_samples, self.n_components))
> > for i, j in iter_from_X_lengths(X, lengths):
> > framelogprob = self._compute_log_likelihood(X[i:j])
> > ...
> > return logprob, posteriors
> > 
> > // child class
> > class StubHMM(_BaseHMM):
> > def _compute_log_likelihood(self, X):
> > return self.framelogprob
> > -
> > 
> > On Python web, it says that things after dot, such as a class name, are
> > attributes. From this definition, 'framelogprob' is an attribute. But when
> > I run the command on Canopy:
> > 
> > h.framelogprob
> > 
> ---
> > AttributeErrorTraceback (most recent call
> > last)  in ()
> > > 1 h.framelogprob
> > 
> > AttributeError: 'StubHMM' object has no attribute 'framelogprob'
> > 
> > 
> > it doesn't recognize it as an attribute. What is wrong with my
> > understanding?
> 
> When you are reading a book, do you expect to be able to understand every 
> arbitrarily picked sentence without any idea about the surrounding text?
> 
> Widen your view a bit -- the StubHMM class is in a file test_base.py and 
> therefore it is likely that it is supposed to help with testing rather than 
> to be used standalone. When you read the complete module to understand more 
> of the context or just use a tool like grep you'll find the following 
> snippets:
> 
> h = StubHMM(2)
> h.transmat_ = [[0.7, 0.3], [0.3, 0.7]]
> h.startprob_ = [0.5, 0.5]
> h.framelogprob = self.framelogprob
> 
> h = StubHMM(n_components)
> h.framelogprob = self.framelogprob
> 
> So the attribute is set "from the outside". I wouldn't do that, I'd rather 
> add initializer arguments, but that wasn't the question...

I did not pay attention to the code you post. The attribute has been unclear
to me for sometime, especially for a child class. I just realize that class
attribute cannot be inherited to its child. 
Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is this an attribute?

2016-01-19 Thread Robert
On Tuesday, January 19, 2016 at 3:52:12 PM UTC-5, Peter Otten wrote:
> Robert wrote:
> 
> > Hi,
> > 
> > When I read a code snippet below, I find I don't know what
> > 'self.framelogprob' is on the child class.
> > 
> > 
> > 
> > // parent class
> > class _BaseHMM(BaseEstimator):
> > def __init__(self, n_components=1,
> >  startprob_prior=1.0, transmat_prior=1.0,
> >  algorithm="viterbi", random_state=None,
> >  n_iter=10, tol=1e-2, verbose=False,
> >  params=string.ascii_letters,
> >  init_params=string.ascii_letters):
> > self.n_components = n_components
> > ..
> > 
> > def score_samples(self, X, lengths=None):
> > X = check_array(X)
> > n_samples = X.shape[0]
> > logprob = 0
> > posteriors = np.zeros((n_samples, self.n_components))
> > for i, j in iter_from_X_lengths(X, lengths):
> > framelogprob = self._compute_log_likelihood(X[i:j])
> > ...
> > return logprob, posteriors
> > 
> > // child class
> > class StubHMM(_BaseHMM):
> > def _compute_log_likelihood(self, X):
> > return self.framelogprob
> > -
> > 
> > On Python web, it says that things after dot, such as a class name, are
> > attributes. From this definition, 'framelogprob' is an attribute. But when
> > I run the command on Canopy:
> > 
> > h.framelogprob
> > 
> ---
> > AttributeErrorTraceback (most recent call
> > last)  in ()
> > > 1 h.framelogprob
> > 
> > AttributeError: 'StubHMM' object has no attribute 'framelogprob'
> > 
> > 
> > it doesn't recognize it as an attribute. What is wrong with my
> > understanding?
> 
> When you are reading a book, do you expect to be able to understand every 
> arbitrarily picked sentence without any idea about the surrounding text?
> 
> Widen your view a bit -- the StubHMM class is in a file test_base.py and 
> therefore it is likely that it is supposed to help with testing rather than 
> to be used standalone. When you read the complete module to understand more 
> of the context or just use a tool like grep you'll find the following 
> snippets:
> 
> h = StubHMM(2)
> h.transmat_ = [[0.7, 0.3], [0.3, 0.7]]
> h.startprob_ = [0.5, 0.5]
> h.framelogprob = self.framelogprob
> 
> h = StubHMM(n_components)
> h.framelogprob = self.framelogprob
> 
> So the attribute is set "from the outside". I wouldn't do that, I'd rather 
> add initializer arguments, but that wasn't the question...

Excuse me. My previous post was wrong on inheritance. 

That you point out that the out of __init__ initializer arguments, is
helpful for me. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: "x == None" vs "x is None"

2016-01-19 Thread fernando junior

> I have seen at several places "x == None" and "x is None" within
> if-statements.
> What is the difference? 
> Which term should I prefer and why?
> 
> 
> -- 
> Ullrich Horlacher  Server und Virtualisierung
> Rechenzentrum IZUS/TIK E-Mail: [email protected]
> Universitaet Stuttgart Tel:++49-711-68565868
> Allmandring 30aFax:++49-711-682357
> 70550 Stuttgart (Germany)  WWW:http://www.tik.uni-stuttgart.de/

In this case, the Style Guide for Python Code [1] recommends use "is or is not, 
never the equality operators" [2].

If you use the pep8 tool [3] to check your code, the error code that will be 
raised is E711 [4].

[1] https://www.python.org/dev/peps/pep-0008/
[2] https://www.python.org/dev/peps/pep-0008/#programming-recommendations
[3] http://pep8.readthedocs.org/en/latest/intro.html
[4] http://pep8.readthedocs.org/en/latest/intro.html#error-codes
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why generators take long time?

2016-01-19 Thread Steven D'Aprano
On Tue, 19 Jan 2016 09:24 pm, Oscar Benjamin wrote:

> On 19 Jan 2016 10:16, "Steven D'Aprano"  wrote:
>>
>> [steve@ando ~]$ python -m timeit -s "from collections import deque"
>> -s "it = iter([i for i in xrange(1000)])" "deque(it, maxlen=0)"
>> 100 loops, best of 3: 0.913 usec per loop
>>
>>
>> [steve@ando ~]$ python -m timeit -s "from collections import deque"
>> -s "it = (i for i in xrange(1000))" "deque(it, maxlen=0)"
>> 100 loops, best of 3: 0.965 usec per loop
> 
> Surely the iterator in the above benchmarks is consumed during the first
> of 100 loops. Subsequent loops are just calling next on an exhausted
> iterator.

You know, I think you're right. Damn.

I keep getting bitten by that aspects of iterators and timeit.



-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is this an attribute?

2016-01-19 Thread Steven D'Aprano
On Wed, 20 Jan 2016 07:19 am, Robert wrote:

> Hi,
> 
> When I read a code snippet below, I find I don't know what
> 'self.framelogprob' is on the child class.
[...]
> On Python web, it says that things after dot, such as a class name, are
> attributes. From this definition, 'framelogprob' is an attribute. But when
> I run the command on Canopy:
> 
> h.framelogprob
> ---
> AttributeErrorTraceback (most recent call
> last)  in ()
> > 1 h.framelogprob
> 
> AttributeError: 'StubHMM' object has no attribute 'framelogprob'
> 
> 
> it doesn't recognize it as an attribute. What is wrong with my
> understanding?


Just because you ask "give me attribute foo" doesn't mean attribute foo
exists. I can use attribute syntax to look up an attribute that doesn't
exist, and Python will raise AttributeError.


The question is, perhaps that attribute gets created elsewhere. You would
need to read all the source code, or read the documentation, to see where
and under what circumstances StubHMM objects gain a framelogprob attribute.



-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Hello.

2016-01-19 Thread Felix Almeida

Check your PATH environment variable.


On 16/01/16 04:41 PM, Hmood Js wrote:

cmd won't recognize python at all I've checked several times , and I don't 
understand what's wrong

Sent from Mail for Windows 10



--
https://mail.python.org/mailman/listinfo/python-list