How to define what a class is ?

2016-02-24 Thread ast

Hi

Since a class is an object, I ask myself 
how to define rigorously what a class is.


classes are instances from type, but not all, since
a class may be an instance of a metaclass

A class is always callable

A class inherit from some others classes, so they
have a bases attribute

any thing else ?

Suppose I provide to you an object and that I
ask to you to tell me if it is a class or not. How
would you proceed ?

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


Re: How to define what a class is ?

2016-02-24 Thread Ian Kelly
On Wed, Feb 24, 2016 at 1:08 AM, ast  wrote:
> Hi
>
> Since a class is an object, I ask myself how to define rigorously what a
> class is.
>
> classes are instances from type, but not all, since
> a class may be an instance of a metaclass

All metaclasses are subclasses of type, so all classes are instances of type.

> A class is always callable

It doesn't strictly have to be. You could override the __call__ method
in the metaclass to raise a TypeError if you don't want the class to
be callable. I don't know of a use case for this, though.

> Suppose I provide to you an object and that I
> ask to you to tell me if it is a class or not. How
> would you proceed ?

import inspect
inspect.isclass(x)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to define what a class is ?

2016-02-24 Thread Ben Finney
"ast"  writes:

> Since a class is an object, I ask myself how to define rigorously what
> a class is.

A class is a type. This bears stating, because for a large part of
Python's history, the two were distinct.

A lot of Python documentation that has its roots in that history still
is careful to maintain the distinction, which in current Python is
completely obsolete.

Every type (and therefore every class) is a template for instances. The
type defines what values are possible for those instances, and also
defines what behaviour those instances will have in common.

> classes are instances from type, but not all, since a class may be an
> instance of a metaclass

Yes. Every class (every type) is an instance of a metaclass, and there
is a particular metaclass named ‘type’.

> A class is always callable

By default, calling a class creates an instance of that class, and
returns that instance to the caller.

> A class inherit from some others classes, so they have a bases
> attribute

That's not part of the definition, really. You need to know that, but
it's not necessary to say what a class is.

> any thing else ?

See the reference documentation on this topic
https://docs.python.org/3/reference/datamodel.html#objects-values-and-types>.

> Suppose I provide to you an object and that I ask to you to tell me if
> it is a class or not. How would you proceed ?

If the object is an instance of the ‘type’ metaclass, the object is a
type (i.e. a class).

Metaclasses are callable, and return a type (a class) to the caller.

The ‘type’ metaclass, if called with an object as a parameter, will
return the type of that object.

If you have the name ‘foo’ bound to an object, you can call::

type(foo)

and the metaclass ‘type’ will return the instance of that object's type.
For example::

>>> type("spam")

>>> type(None)

>>> type(3)

>>> type(int)

>>> type(type)


If you want a boolean test::

>>> isinstance(3, type)
False
>>> isinstance("spam", type)
False
>>> isinstance(int, type)
True
>>> isinstance(type, type)
True

-- 
 \“The fact of your own existence is the most astonishing fact |
  `\you'll ever have to confront. Don't dare ever see your life as |
_o__)boring, monotonous, or joyless.” —Richard Dawkins, 2010-03-10 |
Ben Finney

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


Re: exit from Tkinter mainloop Python 2.7

2016-02-24 Thread Dave Farrance
[email protected] wrote:

>from Tkinter import *
>
>def butContinue():
>root1.destroy()

As Christian said, you're destroying the root window and its children,
so instead use root1.quit() here.

> ...
>
>root1.mainloop()
>
>print entryName.get("1.0", "end-1c" )
>print entryPWord.get("1.0", "end-1c" )

And your root1.destroy() goes here instead. (The root window would
normally be destroyed on the script exit, but some IDE debuggers will
leave it open.) 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Python-ideas] How the heck does async/await work in Python 3.5

2016-02-24 Thread Joao S. O. Bueno
Today I also stumbled on this helpful "essay" from Brett Cannon about
the same subject

http://www.snarky.ca/how-the-heck-does-async-await-work-in-python-3-5

On 23 February 2016 at 18:05, Sven R. Kunze  wrote:
> On 20.02.2016 07:53, Christian Gollwitzer wrote:
>
> If you have difficulties wit hthe overall concept, and if you are open to
> discussions in another language, take a look at this video:
>
> https://channel9.msdn.com/Shows/C9-GoingNative/GoingNative-39-await-co-routines
>
> MS has added coroutine support with very similar syntax to VC++ recently,
> and the developer tries to explain it to the "stackful" programmers.
>
>
> Because of this thread, I finally finished an older post collecting valuable
> insights from last year discussions regarding concurrency modules available
> in Python: http://srkunze.blogspot.com/2016/02/concurrency-in-python.html It
> appears to me that it would fit here well.
>
> @python-ideas
> Back then, the old thread ("Concurrency Modules") was like basically meant
> to result in something useful. I hope the post covers the essence of the
> discussion.
> Some even suggested putting the table into the Python docs. I am unaware of
> the formal procedure here but I would be glad if somebody could point be at
> the right direction if that the survey table is wanted in the docs.
>
> Best,
> Sven
>
> ___
> Python-ideas mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Python-ideas] How the heck does async/await work in Python 3.5

2016-02-24 Thread Paul Moore
On 24 February 2016 at 02:37, Terry Reedy  wrote:
>
> In this essay, Brett says that asyncio added an event loop to Python. It
> did, but it was the second.  The tk event loop was added about 20 years ago
> with tkinter.

One of the things I would love to see (but don't have the time to work
on) is a GUI event loop based around async/await. It would be a very
useful example to make it clear to people that async/await isn't just
about network protocols.

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


Re: [Python-ideas] How the heck does async/await work in Python 3.5

2016-02-24 Thread Victor Stinner
See also Doug Hellmann article on asyncio, from its serie of "Python 3
Module of the Week" articles:
https://pymotw.com/3/asyncio/index.html

Victor

2016-02-23 22:25 GMT+01:00 Joao S. O. Bueno :
> Today I also stumbled on this helpful "essay" from Brett Cannon about
> the same subject
>
> http://www.snarky.ca/how-the-heck-does-async-await-work-in-python-3-5
>
> On 23 February 2016 at 18:05, Sven R. Kunze  wrote:
>> On 20.02.2016 07:53, Christian Gollwitzer wrote:
>>
>> If you have difficulties wit hthe overall concept, and if you are open to
>> discussions in another language, take a look at this video:
>>
>> https://channel9.msdn.com/Shows/C9-GoingNative/GoingNative-39-await-co-routines
>>
>> MS has added coroutine support with very similar syntax to VC++ recently,
>> and the developer tries to explain it to the "stackful" programmers.
>>
>>
>> Because of this thread, I finally finished an older post collecting valuable
>> insights from last year discussions regarding concurrency modules available
>> in Python: http://srkunze.blogspot.com/2016/02/concurrency-in-python.html It
>> appears to me that it would fit here well.
>>
>> @python-ideas
>> Back then, the old thread ("Concurrency Modules") was like basically meant
>> to result in something useful. I hope the post covers the essence of the
>> discussion.
>> Some even suggested putting the table into the Python docs. I am unaware of
>> the formal procedure here but I would be glad if somebody could point be at
>> the right direction if that the survey table is wanted in the docs.
>>
>> Best,
>> Sven
>>
>> ___
>> Python-ideas mailing list
>> [email protected]
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
> ___
> Python-ideas mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Python-ideas] How the heck does async/await work in Python 3.5

2016-02-24 Thread 王珺
It seems an event loop is required for all async programs in python, but
sometimes I need only lazy i/o. Is it possible with asyncio?


Suppose io_operation() takes 3 seconds, then how can I write something like

future = io_operation()
print('Start')
time.sleep(1)
print('Something')
time.sleep(2)
print(future.result())

that print 'Start' immediately and the result of io_operation() 3 seconds
later. The actual io operations are done by the io device that the main
(and only) thread is not interrupted at all, if my understanding is
correct.

2016-02-24 5:05 GMT+08:00 Sven R. Kunze :

> On 20.02.2016 07:53, Christian Gollwitzer wrote:
>
> If you have difficulties wit hthe overall concept, and if you are open to
> discussions in another language, take a look at this video:
>
>
> https://channel9.msdn.com/Shows/C9-GoingNative/GoingNative-39-await-co-routines
>
> MS has added coroutine support with very similar syntax to VC++ recently,
> and the developer tries to explain it to the "stackful" programmers.
>
>
> Because of this thread, I finally finished an older post collecting
> valuable insights from last year discussions regarding concurrency modules
> available in Python:
> http://srkunze.blogspot.com/2016/02/concurrency-in-python.html It appears
> to me that it would fit here well.
>
> @python-ideas
> Back then, the old thread ("Concurrency Modules") was like basically meant
> to result in something useful. I hope the post covers the essence of the
> discussion.
> Some even suggested putting the table into the Python docs. I am unaware
> of the formal procedure here but I would be glad if somebody could point be
> at the right direction if that the survey table is wanted in the docs.
>
> Best,
> Sven
>
> ___
> Python-ideas mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Could not find suitable distribution for Requirement.parse('urllib2')

2016-02-24 Thread jdege
I've been running a simple python program on my Linux Mint 13 box for some 
years that depends upon https://github.com/ryazwinski/pythings.

I'm currently moving to a new Linux Mint 17 box, and I'm trying to move the 
program. I'm having a problem with installing python.py.

The package comes with a setup.py:

from setuptools import setup
setup(
name='pythings',
py_modules=['pythings'],
version='0.1',
description='Python Library for interfacing with Withings API',
install_requires=['simplejson', 'urllib2', 'hashlib'],
)


When I run the install (sudo python setup.py install) I get an error:

Could not find suitable distribution for Requirement.parse('urllib2')

Any ideas on how I should proceed? Is there some repository that contains 
urllib2 that needs to be added to the setuptools configuration?  Is there some 
new library that has supplanted urllib2?

Help would be appreciated.

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


Re: Could not find suitable distribution for Requirement.parse('urllib2')

2016-02-24 Thread Mark Lawrence

On 24/02/2016 14:02, [email protected] wrote:

I've been running a simple python program on my Linux Mint 13 box for some 
years that depends upon https://github.com/ryazwinski/pythings.

I'm currently moving to a new Linux Mint 17 box, and I'm trying to move the 
program. I'm having a problem with installing python.py.

The package comes with a setup.py:

 from setuptools import setup
 setup(
 name='pythings',
 py_modules=['pythings'],
 version='0.1',
 description='Python Library for interfacing with Withings API',
 install_requires=['simplejson', 'urllib2', 'hashlib'],
 )


When I run the install (sudo python setup.py install) I get an error:

 Could not find suitable distribution for Requirement.parse('urllib2')

Any ideas on how I should proceed? Is there some repository that contains 
urllib2 that needs to be added to the setuptools configuration?  Is there some 
new library that has supplanted urllib2?

Help would be appreciated.



I'll guess that your new OS is running Python 3 by default, in which 
case urllib2 has gone.  If I'm correct you should to be able to run the 
code through 2to3.


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: Could not find suitable distribution for Requirement.parse('urllib2')

2016-02-24 Thread Peter Otten
[email protected] wrote:

> I've been running a simple python program on my Linux Mint 13 box for some
> years that depends upon https://github.com/ryazwinski/pythings.
> 
> I'm currently moving to a new Linux Mint 17 box, and I'm trying to move
> the program. I'm having a problem with installing python.py.
> 
> The package comes with a setup.py:
> 
> from setuptools import setup
> setup(
> name='pythings',
> py_modules=['pythings'],
> version='0.1',
> description='Python Library for interfacing with Withings API',
> install_requires=['simplejson', 'urllib2', 'hashlib'],
> )
> 
> 
> When I run the install (sudo python setup.py install) I get an error:
> 
> Could not find suitable distribution for Requirement.parse('urllib2')
> 
> Any ideas on how I should proceed? Is there some repository that contains
> urllib2 that needs to be added to the setuptools configuration?  Is there
> some new library that has supplanted urllib2?
> 
> Help would be appreciated.

If you are using Python 2 try replacing

> install_requires=['simplejson', 'urllib2', 'hashlib'],

with
  install_requires=['simplejson'],

as the last two dependencies are probably already satisfied by the standard 
library.


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


HELP

2016-02-24 Thread Reeves, Andrew
I don't know what's going on. I am running windows 10 64 bit and per the 
pictures I am providing. I am also running Replicator G 0040. Just built a new 
computer with a fresh windows install. All drivers for the USB interface are up 
to date. But what ever is trying to install on your end is failing. What can I 
do or you do or someone do to get this issue fixed for me???

Please see attached photo

Andrew Reeves,

OIT Service Desk
740-593-1222

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


Re: [Python-ideas] How the heck does async/await work in Python 3.5

2016-02-24 Thread Tem Pl
Here are some concurrency benchmarks for python vs other languages. 

https://github.com/atemerev/skynet/pull/53

Is there something wrong with this implementation?

"Hope I suck at coroutines, because the results are abysmal.

I get around 63s on my i5 MacBook Air Early 2015. For reference, the go 
version got around 700ms with GOMAXPROCS=4 and 1.7s with GOMAXPROCS=1 on 
this same machine."

On Wednesday, February 24, 2016 at 5:02:16 AM UTC-5, Victor Stinner wrote:
>
> See also Doug Hellmann article on asyncio, from its serie of "Python 3 
> Module of the Week" articles: 
> https://pymotw.com/3/asyncio/index.html 
>
> Victor 
>
> 2016-02-23 22:25 GMT+01:00 Joao S. O. Bueno  >: 
> > Today I also stumbled on this helpful "essay" from Brett Cannon about 
> > the same subject 
> > 
> > http://www.snarky.ca/how-the-heck-does-async-await-work-in-python-3-5 
> > 
> > On 23 February 2016 at 18:05, Sven R. Kunze  > wrote: 
> >> On 20.02.2016 07:53, Christian Gollwitzer wrote: 
> >> 
> >> If you have difficulties wit hthe overall concept, and if you are open 
> to 
> >> discussions in another language, take a look at this video: 
> >> 
> >> 
> https://channel9.msdn.com/Shows/C9-GoingNative/GoingNative-39-await-co-routines
>  
> >> 
> >> MS has added coroutine support with very similar syntax to VC++ 
> recently, 
> >> and the developer tries to explain it to the "stackful" programmers. 
> >> 
> >> 
> >> Because of this thread, I finally finished an older post collecting 
> valuable 
> >> insights from last year discussions regarding concurrency modules 
> available 
> >> in Python: 
> http://srkunze.blogspot.com/2016/02/concurrency-in-python.html It 
> >> appears to me that it would fit here well. 
> >> 
> >> @python-ideas 
> >> Back then, the old thread ("Concurrency Modules") was like basically 
> meant 
> >> to result in something useful. I hope the post covers the essence of 
> the 
> >> discussion. 
> >> Some even suggested putting the table into the Python docs. I am 
> unaware of 
> >> the formal procedure here but I would be glad if somebody could point 
> be at 
> >> the right direction if that the survey table is wanted in the docs. 
> >> 
> >> Best, 
> >> Sven 
> >> 
> >> ___ 
> >> Python-ideas mailing list 
> >> [email protected]  
> >> https://mail.python.org/mailman/listinfo/python-ideas 
> >> Code of Conduct: http://python.org/psf/codeofconduct/ 
> > ___ 
> > Python-ideas mailing list 
> > [email protected]  
> > https://mail.python.org/mailman/listinfo/python-ideas 
> > Code of Conduct: http://python.org/psf/codeofconduct/ 
> ___ 
> Python-ideas mailing list 
> [email protected]  
> https://mail.python.org/mailman/listinfo/python-ideas 
> Code of Conduct: http://python.org/psf/codeofconduct/ 
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: HELP

2016-02-24 Thread Mark Lawrence

On 24/02/2016 14:08, Reeves, Andrew wrote:

I don't know what's going on. I am running windows 10 64 bit and per the 
pictures I am providing. I am also running Replicator G 0040. Just built a new 
computer with a fresh windows install. All drivers for the USB interface are up 
to date. But what ever is trying to install on your end is failing. What can I 
do or you do or someone do to get this issue fixed for me???

Please see attached photo

Andrew Reeves,

OIT Service Desk
740-593-1222



Your Python problem is what exactly, or have you simply sent your 
message to the wrong place?


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: [Python-ideas] How the heck does async/await work in Python 3.5

2016-02-24 Thread Marko Rauhamaa
Tem Pl :

> Here are some concurrency benchmarks for python vs other languages. 
>
> https://github.com/atemerev/skynet/pull/53
>
> Is there something wrong with this implementation?

It's a "fork bomb".


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


Re: [Python-ideas] How the heck does async/await work in Python 3.5

2016-02-24 Thread Ian Kelly
On Wed, Feb 24, 2016 at 8:23 AM, Marko Rauhamaa  wrote:
> Tem Pl :
>
>> Here are some concurrency benchmarks for python vs other languages.
>>
>> https://github.com/atemerev/skynet/pull/53
>>
>> Is there something wrong with this implementation?
>
> It's a "fork bomb".

Isn't that the point of the benchmark?

But yeah, I tried playing with this and here are my results. It's
clearly pushing all those coroutines down into the event loop that
causes the problem. If you remove the asyncio.as_completed calls and
just await the descendant coroutines in order, then it's a lot faster
-- but this may be technically cheating because then the coroutines
are effectively just running recursively without involving the event
loop, so there's no exercise of actual concurrency.

# Straight-up recursion: 400 ms
def func(level=0, index=0):
if level < LEVELS:
sons = [func(level=level+1, index=index*SONS + x) for x in range(SONS)]
sum_ = 0
for son in sons:
sum_ += son
return sum_
else:
return index

import timeit
print(timeit.repeat('func()', 'from __main__ import func', number=10))
# [4.016848850995302, 4.1330014310078695, 4.149791953997919]

# Concurrent coroutines with event loop: 30 s
import asyncio
async def coroutine(level=0, index=0):
if level < LEVELS:
sons = [coroutine(level=level+1, index=index*SONS + x) for x
in range(SONS)]
sum_ = 0
for f in asyncio.as_completed(sons):
got = await f
sum_ += got
return sum_
else:
return index

print(timeit.repeat('loop.run_until_complete(coroutine())', 'from
__main__ import coroutine; import asyncio; loop =
asyncio.get_event_loop()', number=1))
# [29.884846250002738, 30.26590966898948, 30.716448744002264]

# Recursion with coroutines: 600 ms
async def coro2(level=0, index=0):
if level < LEVELS:
sons = [coro2(level=level+1, index=index*SONS + x) for x in range(SONS)]
sum_ = 0
for son in sons:
got = await son
sum_ += got
return sum_
else:
return index

print(timeit.repeat('loop.run_until_complete(coro2())', 'from __main__
import coro2; import asyncio; loop = asyncio.get_event_loop()',
number=1))
# [0.6264017040084582, 0.590631059021689, 0.5807875629980117]

# Recursive coroutines, no event loop: 600 ms
print(timeit.repeat('list(coro2().__await__())', 'from __main__ import
coro2', number=10))
# [6.028770218021236, 6.002665672975127, 5.987304503010819]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: HELP

2016-02-24 Thread Igor Korot
Hi,

On Wed, Feb 24, 2016 at 10:04 AM, Mark Lawrence  wrote:
> On 24/02/2016 14:08, Reeves, Andrew wrote:
>>
>> I don't know what's going on. I am running windows 10 64 bit and per the
>> pictures I am providing. I am also running Replicator G 0040. Just built a
>> new computer with a fresh windows install. All drivers for the USB interface
>> are up to date. But what ever is trying to install on your end is failing.
>> What can I do or you do or someone do to get this issue fixed for me???
>>
>> Please see attached photo
>>
>> Andrew Reeves,
>>
>> OIT Service Desk
>> 740-593-1222
>>
>
> Your Python problem is what exactly, or have you simply sent your message to
> the wrong place?
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.

 and forgot to attach a picture?

Thank you.

>
> Mark Lawrence
>
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


child.before taking almost 1 minute to execute

2016-02-24 Thread pyfreek
The following snippet alone is taking 1 minute to execute. is there any best 
way to find 'No such file' other than using child.before

if not scrutinFile.startswith('/') :
scrutinFile = '/'+ scrutinFile
scrutinFileFtp = directory + scrutinFile
filePath, file = os.path.split(scrutinFileFtp)
p.sendline('cd %s'%(filePath))
p.expect([pexpect.EOF,pexpect.TIMEOUT])
if 'No such file' in p.before:
print "No such directory exists!!"
sys.exit(1)


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


Re: HELP

2016-02-24 Thread Mark Lawrence

On 24/02/2016 15:43, Igor Korot wrote:

Hi,

On Wed, Feb 24, 2016 at 10:04 AM, Mark Lawrence  wrote:

On 24/02/2016 14:08, Reeves, Andrew wrote:


I don't know what's going on. I am running windows 10 64 bit and per the
pictures I am providing. I am also running Replicator G 0040. Just built a
new computer with a fresh windows install. All drivers for the USB interface
are up to date. But what ever is trying to install on your end is failing.
What can I do or you do or someone do to get this issue fixed for me???

Please see attached photo

Andrew Reeves,

OIT Service Desk
740-593-1222



Your Python problem is what exactly, or have you simply sent your message to
the wrong place?

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.


 and forgot to attach a picture?


Attachments quite often do not come through.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: Network Simulator

2016-02-24 Thread Martin A. Brown

>Hi...I need help to design a network simulator consisting for 5 
>routers in python...Any help would be appretiated...

Have you looked at existing network simulators?

On two different ends of the spectrum are:

  Switchyard, a small network simulator intended for pedagogy
  https://github.com/jsommers/switchyard

  NS-3, the researcher's toolkit
  https://www.nsnam.org/
  https://www.nsnam.org/wiki/Python_bindings

Good luck,

-Martin

-- 
Martin A. Brown
http://linux-ip.net/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Python-ideas] How the heck does async/await work in Python 3.5

2016-02-24 Thread Marko Rauhamaa
Ian Kelly :

> On Wed, Feb 24, 2016 at 8:23 AM, Marko Rauhamaa  wrote:
>> Tem Pl :
>>> Is there something wrong with this implementation?
>>
>> It's a "fork bomb".
>
> Isn't that the point of the benchmark?

I don't quite see the point of the program as it doesn't resemble
anything I'd ever have an urge to write.

As for benchmarks, Python is not supposed to be fast nor small. Python
is actually quite slow and the objects have a large footprint. However,
Python's strength is in its powerful expressivity.

Still, it may be that coroutines end up being especially slow; that's
how they played out in my head when I first was imagining what all those
nested generators must be doing to produce their magic. Also, I'm afraid
the expressivity part suffers a blow with the whole coroutine paradigm
-- without having the otherworldly appeal of Scheme's continuations.


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


Re: [Python-ideas] How the heck does async/await work in Python 3.5

2016-02-24 Thread Ian Kelly
On Wed, Feb 24, 2016 at 9:13 AM, Marko Rauhamaa  wrote:
> Ian Kelly :
>
>> On Wed, Feb 24, 2016 at 8:23 AM, Marko Rauhamaa  wrote:
>>> Tem Pl :
 Is there something wrong with this implementation?
>>>
>>> It's a "fork bomb".
>>
>> Isn't that the point of the benchmark?
>
> I don't quite see the point of the program as it doesn't resemble
> anything I'd ever have an urge to write.

That's reasonable. asyncio is designed for I/O-bound work, so a fair
response to the benchmark is that subjecting it to a CPU-bound stress
test is perhaps missing the point.

I was curious about how much the selector-based event loop might be
influencing the timing, so I tried instrumenting the selector.

class ProfiledSelector(selectors.DefaultSelector):
def __init__(self):
super().__init__()
self._calls = collections.Counter()
def select(self, timeout=None):
self._calls['select'] += 1
return super().select(timeout)
def register(self, fileobj, events, data=None):
self._calls['register'] += 1
return super().register(fileobj, events, data)

selector = ProfiledSelector()
loop = asyncio.SelectorEventLoop(selector)
asyncio.set_event_loop(loop)
loop.run_until_complete(coroutine())
# 4950
selector._calls
# Counter({'select': 20, 'register': 1})

Only 20 iterations of the event loop? That doesn't seem unreasonable.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Setting up/authenticating Google API?

2016-02-24 Thread Skip Montanaro
>
> Your question seems quite cinfusing to me but I think following may is what 
> you are asking for.(Read the source)
>
> https://google-mail-oauth2-tools.googlecode.com/svn/trunk/python/oauth2.py
>
> After getting the authentication you can use imaplib to get all the gmail 
> Data.

Thanks, I will take a look.

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


Re: Network Simulator

2016-02-24 Thread Aaron Christensen
On Feb 23, 2016 9:55 PM, "nikhil amraotkar" 
wrote:
>
> Hi...I need help to design a network simulator consisting for 5 routers
in python...Any help would be appretiated...
> Thanks..
> --
> https://mail.python.org/mailman/listinfo/python-list

What is the purpose for designing it in Python? I'm a network engineer and
use GNS3 and Packet Tracer.  I could see the benefit of creating Python
scripts to manage networking devices but not sure why you would want to use
Python.  I use Python to generate configuration scripts but haven't used it
for any management yet.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to define what a class is ?

2016-02-24 Thread Gregory Ewing

Ian Kelly wrote:


All metaclasses are subclasses of type, so all classes are instances of type.


I think that's about the most general definition you
can find. Almost everything else that you might think
of as being part of the classness of a class can be
overridden.

Another definition might be that it's something that
is potentially the result of type(x) for some x.
I think that's equivalent, because at the C level
type(x) returns the value of x->ob_type, which
always points to an instance of type or a subclass
thereof.

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


Re: child.before taking almost 1 minute to execute

2016-02-24 Thread Emile van Sebille

On 2/24/2016 7:42 AM, pyfreek wrote:

The following snippet alone is taking 1 minute to execute. is there any best 
way to find 'No such file' other than using child.before

 if not scrutinFile.startswith('/') :
 scrutinFile = '/'+ scrutinFile
 scrutinFileFtp = directory + scrutinFile
 filePath, file = os.path.split(scrutinFileFtp)
 p.sendline('cd %s'%(filePath))
 p.expect([pexpect.EOF,pexpect.TIMEOUT])
 if 'No such file' in p.before:
 print "No such directory exists!!"
 sys.exit(1)


I'd guess that you've got your p.expect line wrong -- it looks to me 
like you're allowing that line to complete only upon seeing an EOF or 
TIMEOUT, and that it's timing out as a result.


Emile


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


Nested List question

2016-02-24 Thread grsmith
All,

Can you have a phython list like:
['George',
'Soros',
['99 First Street',
  '33 Broadway Avenue', ['Apt 303'],
  '1 Park Avenue'],
  'New York', 'NY']

In other words how do you correctly nest the
['Apt 303'] so it goes with 33 Broadway Avenue.

Also, I tried several ways and could not figure out
how to get the ['Apt 303'] out of the list. How can 
you do that. It is the ['Apt 303'] that is the problem,
I know how to do the other elements.

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


Re: [Python-ideas] How the heck does async/await work in Python 3.5

2016-02-24 Thread Gregory Ewing

王珺 wrote:

Suppose io_operation() takes 3 seconds, then how can I write something like

future = io_operation()
print('Start')
time.sleep(1)
print('Something')
time.sleep(2)
print(future.result())

that print 'Start' immediately and the result of io_operation() 3 seconds
later.


Yes, Python can do this, but you probably need to use real
threads. The only exception would be if io_operation() were
something you could fire off with a single system call that's
guaranteed not to block, and then wait for the result later.
Opportunities for things like that are rare in unix.
(Windows might be different, I'm not sure.)

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


Re: child.before taking almost 1 minute to execute

2016-02-24 Thread Gregory Ewing

pyfreek wrote:

The following snippet alone is taking 1 minute to execute. is there any best 
way to find 'No such file' other than using child.before

if not scrutinFile.startswith('/') :
scrutinFile = '/'+ scrutinFile
scrutinFileFtp = directory + scrutinFile
filePath, file = os.path.split(scrutinFileFtp)
p.sendline('cd %s'%(filePath))
p.expect([pexpect.EOF,pexpect.TIMEOUT])
if 'No such file' in p.before:
print "No such directory exists!!"
sys.exit(1)


If you're talking to an ftp client here, you might like
to consider using the ftplib module in the standard
library. It ought to take care of the messy details
of error detection for you.

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


Re: Nested List question

2016-02-24 Thread Grant Edwards
On 2016-02-24,   wrote:
> All,
>
> Can you have a phython list like:
> ['George',
> 'Soros',
> ['99 First Street',
>   '33 Broadway Avenue', ['Apt 303'],
>   '1 Park Avenue'],
>   'New York', 'NY']

Sure:

$ python3
Python 3.4.3 (default, Feb 12 2016, 15:58:12) 
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> ['George',
... 'Soros',
... ['99 First Street',
...   '33 Broadway Avenue', ['Apt 303'],
...   '1 Park Avenue'],
...   'New York', 'NY']
['George', 'Soros', ['99 First Street', '33 Broadway Avenue', ['Apt 303'], '1 
Park Avenue'], 'New York', 'NY']

> In other words how do you correctly nest the
> ['Apt 303'] so it goes with 33 Broadway Avenue.

I'm afraid I don't know what you mean by "goes with".

> Also, I tried several ways and could not figure out how to get the
> ['Apt 303'] out of the list.  How can you do that.

Use the "del" operator:

>>> foo = ['George',
... 'Soros',
... ['99 First Street',
...   '33 Broadway Avenue', ['Apt 303'],
...   '1 Park Avenue'],
...   'New York', 'NY']
>>> foo
['George', 'Soros', ['99 First Street', '33 Broadway Avenue', ['Apt 303'], '1 
Park Avenue'], 'New York', 'NY']

>>> del foo[2][2]
>>> foo
['George', 'Soros', ['99 First Street', '33 Broadway Avenue', '1 Park Avenue'], 
'New York', 'NY']

> It is the ['Apt 303'] that is the problem, I know how to do the
> other elements.

I don't think I understand what your problem is.

-- 
Grant Edwards   grant.b.edwardsYow! I feel like I'm
  at   in a Toilet Bowl with a
  gmail.comthumbtack in my forehead!!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested List question

2016-02-24 Thread Andrew Farrell
you can indeed have a nested list. One way you could do that looks like

donor = [
'George',
'Soros',
[  #<- 2nd element of outermost list
'99 First Street',
[  #<- 1st element of middling list
  '33 Broadway Avenue',
  'Apt 303',   #<- 1st element of innermost list
],
'1 Park Avenue'
],
'New York', 'NY',
]
Then, to extract "Apt 303", you could do
donor[2][1][1]

A better explaination of nested lists can be found if you ctrl-f search
for "nested list" on this chapter
 of
the book How to Think Like a Computer Scientist with python.

On Wed, Feb 24, 2016 at 2:59 PM,  wrote:

> All,
>
> Can you have a phython list like:
> ['George',
> 'Soros',
> ['99 First Street',
>   '33 Broadway Avenue', ['Apt 303'],
>   '1 Park Avenue'],
>   'New York', 'NY']
>
> In other words how do you correctly nest the
> ['Apt 303'] so it goes with 33 Broadway Avenue.
>
> Also, I tried several ways and could not figure out
> how to get the ['Apt 303'] out of the list. How can
> you do that. It is the ['Apt 303'] that is the problem,
> I know how to do the other elements.
>
> thanks
> George
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested List question

2016-02-24 Thread Erik

On 24/02/16 20:59, [email protected] wrote:

Can you have a phython list like:
['George',
'Soros',
['99 First Street',
   '33 Broadway Avenue', ['Apt 303'],
   '1 Park Avenue'],
   'New York', 'NY']

In other words how do you correctly nest the
['Apt 303'] so it goes with 33 Broadway Avenue.


The way to keep those particular items together is to have something like:

data = \
['George', 'Soros',
  [ '99 First Street',
['33 Broadway Avenue', 'Apt 303'],
'1 Park Avenue'
  ],
  'New York', 'NY'
]

And then, guessing from the structure of your list, you'd do something like:

forename, surname, addresses, city, state = data

And then, something like:

for address in addresses:
  if type(address) == StringType:
apt = None
  else:
address, apt = address

That should work to pick apart with what you have presented above, but I 
think you'd be better off presenting the data with a bit more structure 
in the first place.



Also, I tried several ways and could not figure out
how to get the ['Apt 303'] out of the list. How can
you do that.


It would be much easier to work out what you mean by this if you'd have 
actually said what you'd tried and what you had expected to happen and 
why what did happen wasn't right for you.


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


Re: Nested List question

2016-02-24 Thread Mark Lawrence

On 24/02/2016 20:59, [email protected] wrote:

All,

Can you have a phython list like:
['George',
'Soros',
['99 First Street',
   '33 Broadway Avenue', ['Apt 303'],
   '1 Park Avenue'],
   'New York', 'NY']

In other words how do you correctly nest the
['Apt 303'] so it goes with 33 Broadway Avenue.

Also, I tried several ways and could not figure out
how to get the ['Apt 303'] out of the list. How can
you do that. It is the ['Apt 303'] that is the problem,
I know how to do the other elements.

thanks
George



As you've all ready had answers, I'd simply ask is a list the best data 
structure for your application?


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: Nested List question

2016-02-24 Thread grsmith

Erik,

Works perfectly, thanks much !!!
Mark, I am tied to data structure.

for address in addresses:
   if isinstance(address, str):
   apt = None
   print(address, apt)
   else:
   address, apt = address
   print(address, apt)


99 First Street None
33 Broadway Avenue Apt 303
1 Park Avenue None


-Original Message- 
From: Erik

Sent: Wednesday, February 24, 2016 4:28 PM
To: [email protected] ; python-list
Subject: Re: Nested List question

On 24/02/16 20:59, [email protected] wrote:

Can you have a phython list like:
['George',
'Soros',
['99 First Street',
   '33 Broadway Avenue', ['Apt 303'],
   '1 Park Avenue'],
   'New York', 'NY']

In other words how do you correctly nest the
['Apt 303'] so it goes with 33 Broadway Avenue.


The way to keep those particular items together is to have something like:

data = \
['George', 'Soros',
  [ '99 First Street',
['33 Broadway Avenue', 'Apt 303'],
'1 Park Avenue'
  ],
  'New York', 'NY'
]

And then, guessing from the structure of your list, you'd do something like:

forename, surname, addresses, city, state = data

And then, something like:

for address in addresses:
  if type(address) == StringType:
apt = None
  else:
address, apt = address

That should work to pick apart with what you have presented above, but I
think you'd be better off presenting the data with a bit more structure
in the first place.


Also, I tried several ways and could not figure out
how to get the ['Apt 303'] out of the list. How can
you do that.


It would be much easier to work out what you mean by this if you'd have
actually said what you'd tried and what you had expected to happen and
why what did happen wasn't right for you.

E. 


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


Python used in several places in LIGO effort

2016-02-24 Thread Mike S via Python-list

https://www.reddit.com/r/IAmA/comments/45g8qu/we_are_the_ligo_scientific_collaboration_and_we/czxnlux?imm_mid=0e0d97&cmp=em-data-na-na-newsltr_20160224
--
https://mail.python.org/mailman/listinfo/python-list


Re: [Python-ideas] How the heck does async/await work in Python 3.5

2016-02-24 Thread 王珺
I think this is possible if I understand what happens under the hood.
I wonder how event loop and async io functions such as
asyncio.open_connection cooperate to do async io in one thread. Maybe it
exploits low-level details and is OS or even device specific. I think I
should take a look at the source code when I have time.

2016-02-25 5:00 GMT+08:00 Gregory Ewing :

> 王珺 wrote:
>
>> Suppose io_operation() takes 3 seconds, then how can I write something
>> like
>>
>> future = io_operation()
>> print('Start')
>> time.sleep(1)
>> print('Something')
>> time.sleep(2)
>> print(future.result())
>>
>> that print 'Start' immediately and the result of io_operation() 3 seconds
>> later.
>>
>
> Yes, Python can do this, but you probably need to use real
> threads. The only exception would be if io_operation() were
> something you could fire off with a single system call that's
> guaranteed not to block, and then wait for the result later.
> Opportunities for things like that are rare in unix.
> (Windows might be different, I'm not sure.)
>
> --
> Greg
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Make a unique filesystem path, without creating the file

2016-02-24 Thread Steven D'Aprano
On Tue, 23 Feb 2016 07:22 pm, Paul Rubin wrote:

> Mark Lawrence  writes:
>> https://mail.python.org/pipermail/python-ideas/2015-September/036333.html
>> then http://www.gossamer-threads.com/lists/python/dev/1223780
> 
> Thanks.  It would be nice if those were gatewayed to usenet like this
> group is.  I can't bring myself to subscribe to mailing lists.
> 
>>> There are a few other choices in the PEP whose benefit is unclear to me,
>>> but they aren't harmful, and I guess the decisions have already been
>>> made.
>> The PEP status is draft so is subject to change.
> 
> Well they might be changeable but it sounds like there's a level of
> consensus by now, that wouldn't be helped by more bikeshedding over
> relatively minor stuff.  I might write up some further comments and post
> them here

If you're going to do so, please do so in the next few days (or write to me
off list to ask for an extension) because I intend to ask Guido for a
ruling early next week.


-- 
Steven

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


"from module import data; print(data)" vs "import module; print(module.data)"

2016-02-24 Thread Dan Stromberg
Could people please compare and contrast the two ways of doing imports
in the Subject line?

I've long favored the latter, but I'm working in a code base that
prefers the former.

Is it fair to say that the former increases the surface area of your
shared (sometimes mutable) state?

It's clear that the former saves keystrokes.

I find the latter a little more clear, because you don't have to go
look for where a symbol came from.

Anything else?

Thanks!

PS: Haskell seems better at the former than Python; Haskell tells you
if you import two identical symbols from two different places, when
you try to use one of them - not at import time.  I believe in Python,
whichever symbol you import last, wins.  Haskell does not warn you at
import time, which is fine.  Not sure about OCaml or whatever else.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to get all IP addresses in python?

2016-02-24 Thread kdiegorsantos
On Friday, July 11, 2003 at 1:54:47 AM UTC-3, Yun Mao wrote:
> Hi. I did a little homework on google for this question. The answer was:
> >>> import socket
> >>> hostname = socket.gethostname()
> >>> ip = socket.gethostbyname(hostname)
> >>> print ip
> 192.168.55.101
> But what if I have several interfaces, say eth0, eth0:1, eth1, etc. How to
> get all of them? It would be a little undesirable if people suggest to parse
> the result of ifconfig because I in particular want the code to be cross
> platform. Thanks a lot!
> 
> Yun

import socket

def get_ipaddr():
proc = subprocess.Popen(["ip addr show | egrep inet | awk '{{print $2}}' | 
awk -F'/' '{{print $1}}' | egrep -v '^127' | xargs'"],stdout=subprocess.PIPE, 
shell=True)
x = proc.communicate()[0]
return x.strip()

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


Re: HELP

2016-02-24 Thread Steven D'Aprano
On Thu, 25 Feb 2016 01:08 am, Reeves, Andrew wrote:

> I don't know what's going on.

And neither do we.

Andrew, can you please describe using words:

* what you are trying to do
* what you expect to happen
* what actually happens
* if it is not obvious, what it has to do with Python

Don't use screen shots or pictures, at least, don't use them ALONE. Some of
us are visually impaired. We have screen-readers that can deal with text,
but pictures might as well not exist. Others are reading this from a
newsgroup on Usenet, and attachments are not permitted.

If you must provide a screen shot, post it on Imgur and provide the link.
Most other photo-sharing sites are a pain to deal with for the recipient.

If you're thinking "That's a lot of effort for me to go through, just for
free technical advice!" you're absolutely right. Feel free to pay for
commercial advice from a professional computer consultant instead, who will
charge for the time they spend trying to understand your problem before
they even begin trying to solve it.

If you do decide to ask here, this is a bad way to ask for help:

"HELP! I'm running a Computertron 3000 and nothing works! PLEASE HELP!!!"



This is a good way to ask for help:


"Hi, I'm running a Computertron 3000. You might not have heard of it, so
here is a link to its home page: http:// ...

It is running Windows 10 as the OS. (I assume you've heard of that :-)

When I try to install Python, the installation seems to succeeds but running
the Python interpreter causes a Yellow Screen Of Death. Here are the steps
I took:

- downloaded the Windows installer from here http:// ...

- ran the installer by double-clicking

- at the prompt, answered "Yes" to the question "Add icons?"

There were no other messages, so I thought it was installed successfully.

When I double-click the Python icon, I get a Yellow Screen Of Death. I
expected the interpreter to run.

In case it helps, I took a picture of the YSOD and posted it on imgur here:
http:// ...

Thanks in advance."




-- 
Steven

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


Import collisions (was Re: "from module import data; print(data)")

2016-02-24 Thread Chris Angelico
On Thu, Feb 25, 2016 at 12:07 PM, Dan Stromberg  wrote:
> PS: Haskell seems better at the former than Python; Haskell tells you
> if you import two identical symbols from two different places, when
> you try to use one of them - not at import time.  I believe in Python,
> whichever symbol you import last, wins.  Haskell does not warn you at
> import time, which is fine.  Not sure about OCaml or whatever else.

In a sense, it's not the two imports that matter, but the situation
that the second import statement is rebinding a name. Would it be
possible to create a "mollyguard" import hook that snoops the globals
of the importing module and raises an ImportWarning if it detects a
rebind?

Not that I've ever been in the situation of wanting one, but just as a
point of intellectual curiosity.

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


Re: "from module import data; print(data)" vs "import module; print(module.data)"

2016-02-24 Thread Cameron Simpson

On 24Feb2016 17:07, Dan Stromberg  wrote:

Could people please compare and contrast the two ways of doing imports
in the Subject line?

I've long favored the latter, but I'm working in a code base that
prefers the former.


I largely use the former "from module import name, name..." over "import 
module". It makes your code more readable by making your calls to 
module-supplied names less verbose.


That readbility does rely on the names being not too many or not to vague. As a 
glaring counter example to my preference, consider:


 from os.path import join

"join" is a pretty generic name, so my code almost always goes either:

 import os.path

and uses "os.path.join(a, b)" or (recently, in code doing a lot of path 
manipulation):


 from os.path import join as joinpath

as with (real example):

 from os.path import abspath, normpath, join as joinpath

and then using "joinpath(a, b)".


Is it fair to say that the former increases the surface area of your
shared (sometimes mutable) state?


It can go the other way. Consider:

 import module
 module.data = 1

versus:

 from module import data
 data = 1

The former directly affects the name "data" in "module"; other users of 
"module" will see this change. The latter binds the _local_ name "data" to 1, 
leaving "module.data" as it was.


Of course both of these are things that are almost never done (look up the term 
"monkey patch" for more info, but the difference is salient.



It's clear that the former saves keystrokes.
I find the latter a little more clear, because you don't have to go
look for where a symbol came from.


I take the view that your should broadly know where a name came from, at least 
as far as what it does, and I will take shorter readably code over 
long.module.names.with.dots. And as mentioned above, is a name is vague (or 
conflicting), there is always the useful:


 from module import name as better_name


PS: Haskell seems better at the former than Python; Haskell tells you
if you import two identical symbols from two different places, when
you try to use one of them - not at import time.  I believe in Python,
whichever symbol you import last, wins.


It wins in exactly the same way that:

 x = 1
 x = 2

wins for 2: they both run, and after they've run "x" will be bound to 2. Python 
is dynamic, and this is legal. It may be that linting tools like pylint will 
warn about conflicting names from imports.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


How to get all IP addresses in python?

2016-02-24 Thread MrJean1

Try function getIPs from this module



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


Re: "from module import data; print(data)" vs "import module; print(module.data)"

2016-02-24 Thread Steven D'Aprano
On Thursday 25 February 2016 12:07, Dan Stromberg wrote:

> Could people please compare and contrast the two ways of doing imports
> in the Subject line?

from module import data; print(data)

import module; print(module.data)


> I've long favored the latter, but I'm working in a code base that
> prefers the former.
> 
> Is it fair to say that the former increases the surface area of your
> shared (sometimes mutable) state?

No, I can't say that it is. If anything, the opposite is the case: it 
decreases the surface area, because `data` now is local to the importing 
(not imported) module. Rebinding data will not affect anything else.


There are two scenarios (`from module import...` versus `import ...`), each 
with two cases: mutation, and rebinding:

(1) from module import data

(a) Mutation (only applies to mutable data like lists, dicts etc)

In place mutation affects everything relying on module.data regardless of 
how or where it is accessed.

(b) Rebinding (e.g. `data = []`)

Because data is now a local name, you only affect the local scope.

(2) import module

(a) Mutation is no different from (1)(a) above. No change.

(b) Rebinding `module.data = []` affects the imported module, and therefore 
everything that relies on it.

So the `from module import data` scenario reduces the number of ways that 
you can affect other modules. That may be important to you, but usually the 
answer to that is "well don't rebind attributes of modules unless you really 
mean to". Sometimes doing `module.data = ...` is not a bad thing.

On the other hand, seeing something like:

module.data.append(999)

should stand out to a reasonably experienced developer as possibly effecting 
other modules. But:

data.append(999)

may fool them into thinking that the change is only local to the current 
module. Dangerous things should look dangerous: this is a point in favour of 
using `import module` in preference to `from`.


> It's clear that the former saves keystrokes.

Code is read about 100 times more than it is written. Saving keystokes 
should be quite low on the list of priorities.

It also saves line width, which may sometimes impact readability. But then 
readability is only weakly correlated to line length. As you say:

> I find the latter a little more clear, because you don't have to go
> look for where a symbol came from.

which is also a very good point.

Personally, I prefer the `import module` over `from module` except when I 
don't. Consequently I will sometimes even use both in the same module:


import os
from itertools import zip_longest


depending on what's more convenient and useful for each.

I think that having a hard rule that you MUST use one or the other is silly, 
but I guess that since Python makes "brace wars" irrelevant people need to 
find something else to argue about.

http://encyclopedia2.thefreedictionary.com/1+true+brace+style


> PS: Haskell seems better at the former than Python; Haskell tells you
> if you import two identical symbols from two different places, when
> you try to use one of them - not at import time.  I believe in Python,
> whichever symbol you import last, wins.  Haskell does not warn you at
> import time, which is fine.  Not sure about OCaml or whatever else.

Haskell assumes that importing the same style from two places is an error 
that needs warning about. I don't think it is, any more than:

s = 'foo'
s = 'bar'

is necessarily an error (particular if the two rebindings are separated by 
code that does things). Both `import` and `from ... import` are rebinding 
operations, like = assignment.


-- 
Steve

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


Re: [Newbie] Tkinter Question

2016-02-24 Thread Wildman via Python-list
On Tue, 23 Feb 2016 16:19:43 -0600, Wildman wrote:

> 

Thanks to Christian and Chris.  You both gave me much
to think about and to experiment with.  That adds to
my on-going learning experience.

This is the first thing I tried:

The Exit button has this:  command=self.quit

Then I have this:

def clean_up():
 # delete temp files

root = Tk()
app = Window(root)
root.protocol("DELETE_WINDOW", clean_up)
root.mainloop()

And I also tried:

root.wm_protocol("WM_DELETE_WINDOW", clean_up)

Both worked when the window [X] button was clicked but
neither worked when the Exit button was clicked.
However, when I removed the protocol statement and
replaced with this, it worked for both:

import atexit

atexit.register(clean_up)

Nothing I tried would fire the clean_up routine when
the program was force killed.

In addition I should mention that I can't us the OS
to delete the temp files because a shelled process
creates them.  It may be possible to still do it but
my limited experience does not allow me to pursue it.

Anyway, I am happy with the outcome even though I have
not found a way to detect when the program is force
killed.  It is unlikely that would ever occur as long
as the program does not lock up.  It is up to me to
make sure that doesn't happen. :-)

Thanks again.

-- 
 GNU/Linux user #557453
"Philosophy is common sense with big words."
  -James Madison
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Make a unique filesystem path, without creating the file

2016-02-24 Thread Steven D'Aprano
On Wednesday 24 February 2016 18:20, Marko Rauhamaa wrote:

> Steven D'Aprano :
> 
>> On Tue, 23 Feb 2016 05:54 pm, Marko Rauhamaa wrote:
>>> However, when you are generating signing or encryption keys, you
>>> should use /dev/random.
>>
>> And that is where you repeat something which is rank superstition.
> 
> Can you find info to back that up. 

The links already provided go through the evidence. For example, they 
explain that /dev/random and /dev/urandom both use the exact same CSPRNG. If 
you don't believe that, you can actually read the source to Linux, FreeBSD, 
OpenBSD and NetBSD. (But not OS X, sorry.)

Put aside the known bug in Linux, where urandom will provide predictable 
values in the period following a fresh install before the OS has collected 
enough entropy. We all agree that's a problem, and that the right solution 
is to block. The question is, outside of that narrow set of circumstances, 
when it is appropriate to block?

As I mentioned, most Unixes don't block. urandom and random behave exactly 
the same way in three of the most popular Unixes (FreeBSD, OpenBSD, OS X):

https://en.wikipedia.org/wiki//dev/random

so let's consider just those that do block (Linux, and NetBSD). They both 
use the same CSPRNG. Do you dispute that? Then read the source. For one to 
be "better" than the other, there would need to be a detectable difference 
between the two. Nobody has ever found one, and nor will they, because 
they're both coming from the same CSPRNG (AES in the case of NetBSD, I'm not 
sure what in the case of Linux).

If you don't trust the CSPRNG, then you shouldn't trust it whether it comes 
from /dev/random or /dev/urandom. If you do trust it, then why would you 
want it to block? Blocking doesn't make it more random. That's not how it 
works. It just makes you vulnerable to a Denial Of Service attack.

There really doesn't seem to be any valid reason for random blocking. It's 
like warnings and timers on fans in South Korea to prevent fan death:

http://www.snopes.com/medical/freakish/fandeath.asp

(My favourite explanation is that the blades of the fan chop the oxygen 
molecules in two.)

I'm not surprised that there is so much misinformation about random/urandom. 
Here's a blog post by somebody wanting to port arc4 to Linux, so he clearly 
knows a few things about crypto. I can't judge whether arc4 is better or 
worse than what Linux already uses, but look at this quote:

http://insanecoding.blogspot.com.au/2014/05/a-good-idea-with-bad-usage-
devurandom.html

Quote:

Linux is well known for inventing and supplying two default files,
/dev/random and /dev/urandom (unlimited random). The former is 
pretty much raw entropy, while the latter is the output of a CSPRNG
function like OpenBSD's arc4random family. The former can be seen 
as more random, and the latter as less random, but the differences
are extremely hard to measure, which is why CSPRNGs work in the 
first place. Since the former is only entropy, it is limited as to
how much it can output, and one needing a lot of random data can be
stuck waiting a while for it to fill up the random buffer. Since the
latter is a CSPRNG, it can keep outputting data indefinitely, 
without any significant waiting periods."

There's that myth about urandom being "less random" than random again, but 
even this guy admits that the difference is "extremely hard" (actually: 
impossible) to measure, and that CSPRNG's "work". Which is precisely why 
OpenBSD uses arc4random for their /dev/random and /dev/urandom, and 
presumably why he wants to bring it to Linux.

This author is *completely wrong* to say that /dev/random is "pretty much 
raw entropy". If it were, it would be biased, and easily manipulated by an 
attacker. Entropy is collected from (among other things) network traffic, 
which would allow an attacker to control at least one source of entropy and 
hence (in theory) make it easier to predict the output of /dev/random.

But fortunately it is not true. Linux's random system works like this:

- entropy is collected from various sources and fed into a pool;

- entropy from that pool is fed through a CSPRNG into two separate pools, 
  one each for /dev/random and /dev/urandom;

- when you read from /dev/random or urandom, they both collect entropy
  from their own pool, and again pass it through a CSPRNG;

- /dev/random has a throttle (it blocks if you take out too much);

- /dev/urandom doesn't have a throttle.

https://events.linuxfoundation.org/images/stories/pdf/lceu2012_anvin.pdf


Somebody criticized the author for spreading this misapprehension that 
/dev/random is "raw entropy" and here is his response:

I tried giving an explanation which should be simple for a 
layman to follow of what goes on. I wouldn't take it as precise 
fact, especially when there's a washing machine involved in the
explanation ;)


Or, in other words, "When I said the moon was made of green cheese, I was

How to read from a file to an arbitrary delimiter efficiently?

2016-02-24 Thread Steven D'Aprano
I have a need to read to an arbitrary delimiter, which might be any of a 
(small) set of characters. For the sake of the exercise, lets say it is 
either ! or ? (for example).

I want to read from files reasonably efficiently. I don't mind if there is a 
little overhead, but my first attempt is 100 times slower than the built-in 
"read to the end of the line" method.

Here is the function I came up with:


# Read a chunk of bytes/characters from an open file.
def chunkiter(f, delim):
buffer = []
b = f.read(1)
while b:
buffer.append(b)
if b in delim:
yield ''.join(buffer)
buffer = []
b = f.read(1)
if buffer:
yield ''.join(buffer)



And here is some test code showing how slow it is:


# Create a test file.
FILENAME = '/tmp/foo'
s = """\
abcdefghijklmnopqrstuvwxyz!
abcdefghijklmnopqrstuvwxyz?
""" * 500
with open(FILENAME, 'w') as f:
f.write(s)


# Run some timing tests, comparing to reading lines from a file.

def readlines(f):
f.seek(0)
for line in f:
pass

def readchunks(f):
f.seek(0)
for chunk in chunkiter(f, '!?'):
pass

from timeit import Timer
SETUP = 'from __main__ import readlines, readchunks, FILENAME; '
SETUP += 'open(FILENAME)'

t1 = Timer('readlines(f)', SETUP)
t2 = Timer('readchunks(f)', SETUP)

# Time them.
x = t1.repeat(number=10)  # Ignore the first run, in case of caching issues.
x = min(t1.repeat(number=1000, repeat=9))

y = t2.repeat(number=10)
y = min(t2.repeat(number=1000, repeat=9))

print('reading lines:', x, 'reading chunks:', y)






On my laptop, the results I get are:

reading lines: 0.22584209218621254 reading chunks: 21.716224210336804


Is there a better way to read chunks from a file up to one of a set of 
arbitrary delimiters? Bonus for it working equally well with text and bytes.

(You can assume that the delimiters will be no more than one byte, or 
character, each. E.g. "!" or "?", but never "!?" or "?!".)

-- 
Steve

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


Re: Make a unique filesystem path, without creating the file

2016-02-24 Thread Marko Rauhamaa
Steven D'Aprano :

> On Wednesday 24 February 2016 18:20, Marko Rauhamaa wrote:
>> Steven D'Aprano :
>>> And that is where you repeat something which is rank superstition.
>> 
>> Can you find info to back that up. 
>
> The links already provided go through the evidence. For example, they 
> explain that /dev/random and /dev/urandom both use the exact same
> CSPRNG.

A non-issue. The question is, after the initial entropy is collected and
used to seed the CSPRNG, is any further entropy needed for any
cryptographic purposes? Are there any nagging fears that weaknesses
could be found in the deterministic sequence?

/dev/random is supposed to be hardened against such concerns by stirring
the pot constantly (if rather slowly).

Here's what Linus Torvalds said on the matter years back:

   > No, it says /dev/random is primarily useful for generating large
   > (>>160 bit) keys.

   Which is exactly what something like sshd would want to use for
   generating keys for the machine, right? That is _the_ primary reason
   to use /dev/random.

   Yet apparently our /dev/random has been too conservative to be
   actually useful, because (as you point out somewhere else) even sshd
   uses /dev/urandom for the host key generation by default.

   That is really sad. That is the _one_ application that is common and
   that should really have a reason to maybe care about /dev/random vs
   urandom. And that application uses urandom. To me that says that
   /dev/random has turned out to be less than useful in real life.

   Is there anything that actually uses /dev/random at all (except for
   clueless programs that really don't need to)?

   http://article.gmane.org/gmane.linux.kernel/47437>

> If you don't trust the CSPRNG, then you shouldn't trust it whether it comes 
> from /dev/random or /dev/urandom. If you do trust it, then why would you 
> want it to block? Blocking doesn't make it more random.

It might not make it more secure cryptographically, but the point is
that it should make it more genuinely random.

> That's not how it works. It just makes you vulnerable to a Denial Of
> Service attack.

Understood. You should not use /dev/random for any reactive purposes
(like nonces or session encryption keys).

> There's that myth about urandom being "less random" than random again,
> but even this guy admits that the difference is "extremely hard"
> (actually: impossible) to measure, and that CSPRNG's "work". Which is
> precisely why OpenBSD uses arc4random for their /dev/random and
> /dev/urandom, and presumably why he wants to bring it to Linux.

That's for the cryptographic experts to judge. CSPRNG's aren't always as
CS as one would think:

   In December 2013, a Reuters news article alleged that in 2004, before
   NIST standardized Dual_EC_DRBG, NSA paid RSA Security $10 million in
   a secret deal to use Dual_EC_DRBG as the default in the RSA BSAFE
   cryptography library, which resulted in RSA Security becoming the
   most important distributor of the insecure algorithm.

   https://en.wikipedia.org/wiki/Dual_EC_DRBG>

> The bottom line is, nobody can distinguish the output of urandom and
> random (apart from the blocking behaviour). Nobody has demonstrated
> any way to distinguish the output of either random or urandom from
> "actual randomness". There are theoretical attacks on urandom that
> random might be immune to, but if so, I haven't heard what they are.

What I'm looking for is a cryptography mailing list (or equivalent)
giving their stamp of approval. As can be seen above, NIST ain't it.

It seems, though, that cryptography researchers are not ready to declare
any scheme void of vulnerabilities. At best they can mention that there
are no *known* vulnerabilities.

> What evidence do they give that /dev/urandom is weak? If it is weak,
> why are they using it as the default?

It's a big mess, but not a mess I would disentangle. Once the crypto
libraries, utilities, facilities and the OS come to a consensus, I can
hope they've done their homework. As it stands, the STRONG vs VERY
STRONG dichotomy seems to be alive all over the place.


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


Re: How to read from a file to an arbitrary delimiter efficiently?

2016-02-24 Thread Chris Angelico
On Thu, Feb 25, 2016 at 5:50 PM, Steven D'Aprano
 wrote:
>
> # Read a chunk of bytes/characters from an open file.
> def chunkiter(f, delim):
> buffer = []
> b = f.read(1)
> while b:
> buffer.append(b)
> if b in delim:
> yield ''.join(buffer)
> buffer = []
> b = f.read(1)
> if buffer:
> yield ''.join(buffer)

How bad is it if you over-read? If it's absolutely critical that you
not read anything from the buffer that you shouldn't, then yeah, it's
going to be slow. But if you're never going to read the file using
anything other than this iterator, the best thing to do is to read
more at a time. Simple and naive method:

def chunkiter(f, delim):
"""Don't use [ or ] as the delimiter, kthx"""
buffer = ""
b = f.read(256)
while b:
buffer += b
*parts, buffer = re.split("["+delim+"]", buffer)
yield from parts
if buffer: yield buffer

How well does that perform?

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


Re: How to read from a file to an arbitrary delimiter efficiently?

2016-02-24 Thread Wolfgang Maier

On 25.02.2016 07:50, Steven D'Aprano wrote:

I have a need to read to an arbitrary delimiter, which might be any of a
(small) set of characters. For the sake of the exercise, lets say it is
either ! or ? (for example).



You are not alone with your need.

http://bugs.python.org/issue1152248 discusses the problem and has some 
code snippets that you may be interested in. While there is no trivial 
solution there are certainly faster ways than your first attempt.


Wolfgang

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


Re: How to read from a file to an arbitrary delimiter efficiently?

2016-02-24 Thread Paul Rubin
Steven D'Aprano  writes:
> while b:
> buffer.append(b)

This looks bad because of the overhead of list elements, and also the
reading of 1 char at a time.  If it's bytes that you're reading, try
using bytearray instead of list:

def chunkiter(f,delim):
buf = bytearray()
bufappend = buf.append   # avoid an attribute lookup when calling
fread = f.read# similar
while True:
c = fread(1)
bufappend(c)
if c in delim:
yield str(buf)
del buf[:]

If that's still not fast enough, you could do a more hacky thing of
reading large chunks of input at once (f.read(4096) or whatever),
splitting on the delimiter set with re.split, and yielding the split
output, refilling the buffer when you don't find more delimiters.  That
doesn't tell you what delimiters actually match: do you need that?
Maybe there is nicer a way to get at it than adding up the lengths of
the chunks to index into the buffer.  How large do you expect the chunks
to be?
-- 
https://mail.python.org/mailman/listinfo/python-list