Re: Inception

2016-03-03 Thread Christian Gollwitzer

Hi Denis,

Am 03.03.16 um 06:01 schrieb Denis Akhiyarov:

Is it possible to embed CPython in CPython, e.g. using ctypes, cffi, or cython?



since you titled your question with a famous movie title, I take it more 
as a "philosophical" question (musing about the CPython world) than an 
actual demand. Technically, I think it cannot work with the current 
state of CPython. Embedding CPython into another application requires 
you to load libpython3.5.so (or similar depending on OS and version) and 
to call the functino sfrom within there. When a Python program runs in 
the standalone interpreter, then this DLL is already loaded, obviously, 
so the question is if the Python C API functions can be called from a 
Python script.


Possibly  ctypes or cffi can do that for you, but you would end up with 
the same interpreter that executes your main script. A DLL is loaded by 
the OS only once, which makes it possible to create plugin interfaces 
etc. using shared symbols in C.


Now, the CPython interpreter saves its state in global variables. 
Therefore only one interpreter can exist in a single process, and this 
is also the reason that multithreading is complicated and does not 
really work (GIL &c).


How useful could it be to have more than a single interpreter? For 
instance, Tcl does it in a different way. All of the interpreter state 
is stored within a Tcl_Interp struct which is passed around to every API 
function. You can create more than a single interpreter, and that even 
works from the script level using the "interp create" command. This is 
useful to shield user script evaluation from the main script. Such 
interpreters can be made "safe", i.e. to not allow file operations, or 
to limit the CPU time for script evaluation, which has its primary use 
case in things like web browser plugins or simple servers. A pythonized 
version of the API would look like this:


from pyembedding import Interp

i=Interp()
i2=Interp(safe=True)

i.eval('print("Hello world")')
# prints Hello world

i2.eval('print("Hello world")')
# raises a SafeInterpException
# I/O  is unsafe and disabled here

def greetme():
print("Hello world")

i2.alias(greet=greetme)
i2.eval('greet')
# calls back to greetme in main interpreter

This mechanism would also allow true multithreading. If you run another 
interpreter in the second thread, it will have it's own GIL.


I'm not familiar with the internals of other Python implementation, but 
I can imagine that maybe in Jython such an extension could be written.


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


merging number of csv files

2016-03-03 Thread m . t . egle
Hey!

I have been goggling around for the last few days and tried out many python 
codes.
I want to merge two csv files, say thought_probe1.csv and thought_probe2.csv. I 
want them to merge column-wise in a new csv file 'new_file.csv'.
What coding is smart to use in order to achieve it? 

I would really appreciate if someone could right a good code below.

Thanks so much!!

Best,
Tiber
-- 
https://mail.python.org/mailman/listinfo/python-list


merging two csv files

2016-03-03 Thread m . t . egle
Hey!

I want to merge column-wise two csv files, say: file1.csv and file2.csv, both 
containing two columns, into a new csv file. 

I could not find a good code for doing this. It never really worked. 

If you could show me the code with this example, I would highly appreciate it.

Best wishes,
Tiber
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: merging two csv files

2016-03-03 Thread Peter Otten
[email protected] wrote:

> I want to merge column-wise two csv files, say: file1.csv and file2.csv,
> both containing two columns, into a new csv file.
> 
> I could not find a good code for doing this. It never really worked.
> 
> If you could show me the code with this example, I would highly appreciate
> it.

Please provide really small examples of the input files. Then show what the 
resulting file exactly should look like. 

Finally: what have you tried? Show us the code you have written. We will 
help you fix it, but this is not a free coding service.

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


A mistake which almost went me mad

2016-03-03 Thread ast

Hello

This has to be told

I created a file pickle.py in order to test some files
read/write with objects and put it in a directory
which is on my python path. 


Then the nightmare began

- Idle no longer works, window no longer opens 
when double clicked, no errors messsages


- python -m pip list doesnt work, crash with an 
error message related to pickle


I uninstalled python34 and reinstalled it, same problem
I uninstalled python34 and instaled 3.5, same problem

...

I finally understood that pickle.py is the name of the file
containing the official pickle module.

This module is probably used by various python programs,
IDLE, pip ...

Instead of reading official pickle, python read my file ...



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


Re: A mistake which almost went me mad

2016-03-03 Thread Chris Angelico
On Thu, Mar 3, 2016 at 9:21 PM, ast  wrote:
> - python -m pip list doesnt work, crash with an error message related to
> pickle

At this point, you could have come to this list, asking for help - and
posting the *entire* traceback. It may have mentioned a file name,
which would give a strong clue; otherwise, there'd be an error like
this:

rosuav@sikorsky:~/tmp$ echo 'print("Hello, world!")' >pickle.py
rosuav@sikorsky:~/tmp$ python3 -m pip list
Hello, world!
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/runpy.py", line 174, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/local/lib/python3.6/runpy.py", line 133, in _get_module_details
return _get_module_details(pkg_main_name, error)
  File "/usr/local/lib/python3.6/runpy.py", line 109, in _get_module_details
__import__(pkg_name)
  File "/usr/local/lib/python3.6/site-packages/pip/__init__.py", line
15, in 
from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/usr/local/lib/python3.6/site-packages/pip/vcs/subversion.py",
line 9, in 
from pip.index import Link
  File "/usr/local/lib/python3.6/site-packages/pip/index.py", line 29,
in 
from pip.wheel import Wheel, wheel_ext
  File "/usr/local/lib/python3.6/site-packages/pip/wheel.py", line 6,
in 
import compileall
  File "/usr/local/lib/python3.6/compileall.py", line 20, in 
from concurrent.futures import ProcessPoolExecutor
  File "/usr/local/lib/python3.6/concurrent/futures/__init__.py", line
17, in 
from concurrent.futures.process import ProcessPoolExecutor
  File "/usr/local/lib/python3.6/concurrent/futures/process.py", line
55, in 
from multiprocessing.connection import wait
  File "/usr/local/lib/python3.6/multiprocessing/connection.py", line
23, in 
from . import reduction
  File "/usr/local/lib/python3.6/multiprocessing/reduction.py", line
32, in 
class ForkingPickler(pickle.Pickler):
AttributeError: module 'pickle' has no attribute 'Pickler'

And someone would have suggested changing to another directory, or
running something like this:

rosuav@sikorsky:~/tmp$ python3 -c 'import pickle; print(pickle.__file__)'
Hello, world!
/home/rosuav/tmp/pickle.py

which shows up the issue.

Don't get mad - get even! Or even better, get help! But show us the
entire exception traceback, not just "got an exception".

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


Re: Continuing indentation

2016-03-03 Thread cl
[email protected] wrote:
> On Wednesday, March 2, 2016 at 3:44:07 PM UTC-5, Skip Montanaro wrote:
> > 
> > if (some_condition and
> > some_other_condition and
> > some_final_condition):
> > play_bingo()
> 
> How about:
> 
>   continue_playing = (
>   some_condition and
>   some_other_condition and
>   some_final_condition
>   )
> 
>   if continue_playing:
>   play_bingo()
> 
> or:
> 
>   play_conditions = [
>   some_condition,
>   some_other_condition,
>   some_final_condition,
>   ]
> 
>   if all(play_conditions):
>   play_bingo()
> 
I'd much prefer the [] and () to be aligned so I can check that
beginnings have ends.  Similarly in C I prefer:-

if ( x == y )
{
z = 99;
}

rather than:-

if ( x == y ) {
z = 99;
}

Apparently the second is/was only popular because it uses fewer lines
and thus more code would appear on a single screen.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: merging two csv files

2016-03-03 Thread K. Elo

Hi!

Is this a homework or something you need a quick solution for?

For the latter: 'man paste' (on Linux) :)

Anyway, some sample data and code would be good.

BR, Kimmo

03.03.2016, 11:50, [email protected] wrote:

Hey!

I want to merge column-wise two csv files, say: file1.csv and file2.csv, both 
containing two columns, into a new csv file.

I could not find a good code for doing this. It never really worked.

If you could show me the code with this example, I would highly appreciate it.

Best wishes,
Tiber


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


Re: A mistake which almost went me mad

2016-03-03 Thread Nick Sarbicki
On Thu, Mar 3, 2016 at 10:26 AM ast  wrote:

> Hello
>
> This has to be told
>
> I created a file pickle.py
>

 You could stop there.

The number of times I've had to correct a student for naming their script
"turtle.py".

And the number of times I've caught myself doing it...



,.-'"...``~.,
.,.-"..."-.,
.,/...":,
.,?..,
.../...,}
./..,:`^`..}
.../...,:"./
..?.__.:`.../
./__.(."~-,_..,:`../
.../(_"~,_"~,_,:`_/
..{.._$;_.."=,_..."-,_...,.-~-,},.~";/}
...((.*~_..."=-._..";,,./`/"../
...,,,___.`~,.."~.,`.}../
(`=-,,...`(..;_,,-"
/.`~,..`-./
.`~.*-,.|,./.,__
,,_..}.>-._...|..`=~-,
.`=~-,__..`,.
...`=~-,,.,...
`:,,...`..__
.`=-,...,%`>--==``
_..._,-%...`
...,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A mistake which almost went me mad

2016-03-03 Thread Tim Golden
On 03/03/2016 10:43, Nick Sarbicki wrote:
> On Thu, Mar 3, 2016 at 10:26 AM ast  wrote:
> 
>> Hello
>>
>> This has to be told
>>
>> I created a file pickle.py
>>
> 
>  You could stop there.
> 
> The number of times I've had to correct a student for naming their script
> "turtle.py".

A few teachers recently were discussing this on Twitter. One suggested
that his pupils always add their initials to whatever filename they use.
That tends to avoid this issue (as well as helping work out later whose
file is whose on things like Raspberry Pi).

TJG

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


Re: looking into python...

2016-03-03 Thread crankypuss
Ben Finney wrote:

> crankypuss  writes:
> 
>> "Python code can be packaged into stand-alone executable programs for
>> some of the most popular operating systems, allowing the distribution
>> of Python-based software for use on those environments without
>> requiring the installation of a Python interpreter." (wikipedia)
>>
>> How correct is that?  Which "most popular operating systems" are
>> those?
> 
> Python's web site covers this. The Python environment is available for
> download https://www.python.org/downloads/> for all major
> operating systems.

Looking at https://www.python.org/downloads/release/python-344/
it appears that "all major operating systems" includes "Mac OS X", 
"Windows", and "Source release".  I'm seeing nothing about Android, or 
BlackBerry OS-10, or mainframes, or linux.  Presumably the source code 
has been pre-built for linux in various distro repositories.  Maybe I'm 
missing the obvious as usual.

> Python is used in a huge range of fields, in organisations large and
> small https://www.python.org/about/success/>.

So are paper-clips.

>> Is there a good site to read for a quick overview of how one would
>> use python with a qt binding to write some GUI utilities?
> 
> GUI programming is covered at the Python wiki
> https://wiki.python.org/moin/GUI%20Programming%20in%20Python>.

I found that yesterday after posting, thank you for the validation.

Referring back to the wikipedia quote at the start of the post, I'm 
interested in how Python code is packaged into stand-alone executables.

I've done some work along those lines with PHP, which amounted to 
building a source-code linker to make sure that all the required 
functions are included in one file.  It was my intent to build a 
slightly-modified version of PHP to act as a front-end, appending all 
the required functions to that copy of the interpreter.  Alas what I 
find is that when statically linked that front-end is a bit over 15 
megabytes in size, which I consider impractical for distribution.

The plan all along has been to convert the PHP code to another language, 
so that aspect of the project seems to be escalating itself in the 
priority queue.

Do you think conversion from procedural PHP (PHP that does not use the 
PHP OO facilities) to Python is a practical idea, or are the languages 
too different?

Thank you.

-- 
http://totally-portable-software.blogspot.com
  [Mon Feb 29: "Addresses - What Good Are They?"]

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


yield in try/finally case

2016-03-03 Thread 刘琦帆
I have just saw PEP 255, and it says that 

"A yield statement is not allowed in the try clause of a try/finally construct. 
 The difficulty is that there's no guarantee the generator will ever be 
resumed, hence no guarantee that the finally block will ever get executed; 
that's too much a violation of finally's purpose to bear." from 
https://www.python.org/dev/peps/pep-0255/

But, meanwhile, the code showed on that page use yield in a try/finally case.
It really puzzles me. Is there anything wrong?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: yield in try/finally case

2016-03-03 Thread Oscar Benjamin
On 3 March 2016 at 11:52, 刘琦帆  wrote:
>
> "A yield statement is not allowed in the try clause of a try/finally 
> construct.  The difficulty is that there's no guarantee the generator will 
> ever be resumed, hence no guarantee that the finally block will ever get 
> executed; that's too much a violation of finally's purpose to bear." from 
> https://www.python.org/dev/peps/pep-0255/
>
> But, meanwhile, the code showed on that page use yield in a try/finally case.
> It really puzzles me. Is there anything wrong?

I think what it means is that you can put a yield in the finally block
but not the try block so:

# Not allowed
def  f():
try:
yield 1
finally:
pass

# Allowed
def f():
try:
pass
finally:
yield 1

However that information is out of date. The restriction was removed
in some later Python version. Actually the construct is quite common
when using generator functions to implement context managers:

@contextlib.contextmanager
def replace_stdin(newstdin):
oldstdin = sys.stdin
try:
sys.stdin = newstdin
yield
finally:
sys.stdin = oldstdin

Although the restriction was removed the problem itself still remains.
There's no guarantee that a finally block will execute if there is a
yield in the try block. The same happens if you use a context manager
around a yield statement: the __exit__ method is not guaranteed to be
called. One implication of this is that in the following code it is
not guaranteed that the file will be closed:

def upperfile(filename):
with open(filename) as fin:
for line in fin:
yield line.upper()

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


Re: yield in try/finally case

2016-03-03 Thread 刘琦帆
在 2016年3月3日星期四 UTC+8下午8:14:29,Oscar Benjamin写道:
> On 3 March 2016 at 11:52, 刘琦帆  wrote:
> >
> > "A yield statement is not allowed in the try clause of a try/finally 
> > construct.  The difficulty is that there's no guarantee the generator will 
> > ever be resumed, hence no guarantee that the finally block will ever get 
> > executed; that's too much a violation of finally's purpose to bear." from 
> > https://www.python.org/dev/peps/pep-0255/
> >
> > But, meanwhile, the code showed on that page use yield in a try/finally 
> > case.
> > It really puzzles me. Is there anything wrong?
> 
> I think what it means is that you can put a yield in the finally block
> but not the try block so:
> 
> # Not allowed
> def  f():
> try:
> yield 1
> finally:
> pass
> 
> # Allowed
> def f():
> try:
> pass
> finally:
> yield 1
> 
> However that information is out of date. The restriction was removed
> in some later Python version. Actually the construct is quite common
> when using generator functions to implement context managers:
> 
> @contextlib.contextmanager
> def replace_stdin(newstdin):
> oldstdin = sys.stdin
> try:
> sys.stdin = newstdin
> yield
> finally:
> sys.stdin = oldstdin
> 
> Although the restriction was removed the problem itself still remains.
> There's no guarantee that a finally block will execute if there is a
> yield in the try block. The same happens if you use a context manager
> around a yield statement: the __exit__ method is not guaranteed to be
> called. One implication of this is that in the following code it is
> not guaranteed that the file will be closed:
> 
> def upperfile(filename):
> with open(filename) as fin:
> for line in fin:
> yield line.upper()
> 
> --
> Oscar


It really nice of you to answer the question. But I am still confused with your 
last example, is there any case that the file with not be closed? I just run 
the code and no exception occur.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A mistake which almost went me mad

2016-03-03 Thread Steven D'Aprano
On Thu, 3 Mar 2016 09:21 pm, ast wrote:

> Hello
> 
> This has to be told
> 
> I created a file pickle.py in order to test some files
> read/write with objects and put it in a directory
> which is on my python path.

[...] 

> I uninstalled python34 and reinstalled it, same problem
> I uninstalled python34 and instaled 3.5, same problem


And now you have learned a very important lesson:

Do not waste your time uninstalling and reinstalling Python until everything
else has failed.


> This module is probably used by various python programs,
> IDLE, pip ...
> 
> Instead of reading official pickle, python read my file ...

Correct.

To diagnose these sorts of problems, start by opening your operating
system's command interpreter shell (PowerShell, command.com, com.exe,
terminal, or whatever it is called in your OS), and launch IDLE from the
shell. If something is broken, you will see the error message.

This is on Linux, but it will work the same way under Windows:



[steve@ando ~]$ touch time.py  # Accidentally shadow the module
[steve@ando ~]$ idle  # you may need to give the full path
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.7/idlelib/run.py", line 7, in 
import threading
  File "/usr/local/lib/python2.7/threading.py", line 13, in 
from time import time as _time, sleep as _sleep
ImportError: cannot import name time


This tells you that there is a problem with the module time.py.

It is much easier to debug problems when you can see the error messages!




-- 
Steven

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


Re: looking into python...

2016-03-03 Thread Steven D'Aprano
On Thu, 3 Mar 2016 09:45 pm, crankypuss wrote:

> Ben Finney wrote:
> 
>> crankypuss  writes:
>> 
>>> "Python code can be packaged into stand-alone executable programs for
>>> some of the most popular operating systems, allowing the distribution
>>> of Python-based software for use on those environments without
>>> requiring the installation of a Python interpreter." (wikipedia)
>>>
>>> How correct is that?  Which "most popular operating systems" are
>>> those?
>> 
>> Python's web site covers this. The Python environment is available for
>> download https://www.python.org/downloads/> for all major
>> operating systems.
> 
> Looking at https://www.python.org/downloads/release/python-344/
> it appears that "all major operating systems" includes "Mac OS X",
> "Windows", and "Source release".  I'm seeing nothing about Android, or
> BlackBerry OS-10, or mainframes, or linux.  Presumably the source code
> has been pre-built for linux in various distro repositories.  Maybe I'm
> missing the obvious as usual.

Yes, all the major Linux distros (Red Hat, Centos, Fedora, Debian, Ubuntu,
Mint, Suse, ...) will either install Python by default or at least provide
it through their official package management software.

Likewise, FreeBSD and OpenBSD certainly have Python available.

I doubt Blackberry has Python available, but that's hardly a major or
important platform.

For Android, the answer is mixed. I don't have an Android device to try it
on, but I'm told that it is possible to install Python on Android, but it
may be difficult. Probably the easiest way is to install Kivy:

https://kivy.org/

Kivy is a cross-platform Python development system that runs on OS X, Linux,
Windows, iOS (iPad, iPhone) and Android.

For mainframes, the answer is maybe". If your mainframe supports the C89
standard, it will probably work. If it can run x86 machine code, or emulate
it in a VM, that's probably the way to go.

Some older versions of Python supported mainframes like VMS, but in 2011,
support for OS\2, VMS and Windows 2000 was dropped:

http://blog.python.org/2011/05/python-33-to-drop-support-for-os2.html

There is a Nokia project called PyS60 that runs Python on Nokia Series 60
mobile phones. I don't know if it is still maintained.

Classic Mac OS (System 9 and older) was supported in older Python versions,
but not the more recent ones.

I don't think there has ever been a version of Python that ran on DOS :-)



> Referring back to the wikipedia quote at the start of the post, I'm
> interested in how Python code is packaged into stand-alone executables.

I'm only really familiar with the situation on Linux. On Linux, you usually
wouldn't bother. Since Python is almost always already installed on the
system, and even if it is not, it's usually only a single command away
(like `yum install python`, say), there's very little point in packaging
your scripts or applications up in a stand-alone exe file.

Somewhat intermediate between stand-alone exe applications and a directory
full of scripts is to create a package, compress it into a zip file, and
run that zip file with the Python interpreter installed on your system.
Since it relies on there being an external Python interpreter, it's not
exactly stand-alone, but it may be close enough for what you're doing.

Python packages are dead-simple: create a directory called "foo", and put a
file inside it called "__init__.py". It can even be an empty file. That's a
package. Obviously there is more to it than that, but that's the basics.



-- 
Steven

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


Re: yield in try/finally case

2016-03-03 Thread Peter Otten
刘琦帆 wrote:

> 在 2016年3月3日星期四 UTC+8下午8:14:29,Oscar Benjamin写道:
>> On 3 March 2016 at 11:52, 刘琦帆  wrote:
>> >
>> > "A yield statement is not allowed in the try clause of a try/finally
>> > construct.  The difficulty is that there's no guarantee the generator
>> > will ever be resumed, hence no guarantee that the finally block will
>> > ever get executed; that's too much a violation of finally's purpose to
>> > bear." from https://www.python.org/dev/peps/pep-0255/
>> >
>> > But, meanwhile, the code showed on that page use yield in a try/finally
>> > case. It really puzzles me. Is there anything wrong?
>> 
>> I think what it means is that you can put a yield in the finally block
>> but not the try block so:
>> 
>> # Not allowed
>> def  f():
>> try:
>> yield 1
>> finally:
>> pass
>> 
>> # Allowed
>> def f():
>> try:
>> pass
>> finally:
>> yield 1
>> 
>> However that information is out of date. The restriction was removed
>> in some later Python version. Actually the construct is quite common
>> when using generator functions to implement context managers:
>> 
>> @contextlib.contextmanager
>> def replace_stdin(newstdin):
>> oldstdin = sys.stdin
>> try:
>> sys.stdin = newstdin
>> yield
>> finally:
>> sys.stdin = oldstdin
>> 
>> Although the restriction was removed the problem itself still remains.
>> There's no guarantee that a finally block will execute if there is a
>> yield in the try block. The same happens if you use a context manager
>> around a yield statement: the __exit__ method is not guaranteed to be
>> called. One implication of this is that in the following code it is
>> not guaranteed that the file will be closed:
>> 
>> def upperfile(filename):
>> with open(filename) as fin:
>> for line in fin:
>> yield line.upper()
>> 
>> --
>> Oscar
> 
> 
> It really nice of you to answer the question. But I am still confused with
> your last example, is there any case that the file with not be closed? I
> just run the code and no exception occur.

It doesn't happen easily, you have to defeat CPython's garbage collection. 
Consider the follwing script:

$ cat upper1.py
import sys

_open = open
files = []

def open(*args, **kw):
"""Use a modified open() which keeps track of opened files.

This allows us to check whether the files are properly closed and
also to defeat garbage collection.
"""
f = _open(*args, **kw)
files.append(f)
return f


for filename in sys.argv[1:]:
with open(filename) as f:
for line in f:
print(line.upper(), end="")
break

assert all(f.closed for f in files)

$ echo -e 'foo\nbar\nbaz' > tmp1.txt
$ echo -e 'hams\nspam\njam' > tmp2.txt
$ python3 upper1.py *.txt
FOO
HAMS

As expected it prints the first lines of the files provided as commandline 
args, in upper case. Now let's refactor:

$ cat upper2.py
import sys

_open = open
files = []

def open(*args, **kw):
"""Use a modified open() which keeps track of opened files.

This allows us to check whether the files are properly closed (and
also defeats garbage collection).
"""
f = _open(*args, **kw)
files.append(f)
return f

def upperfile(filename):
with open(filename) as f:
for line in f:
yield line.upper()

for uf in map(upperfile, sys.argv[1:]):
for line in uf:
print(line, end="")
break

assert all(f.closed for f in files)

The change looks harmless, we moved the with statement and the conversion 
into the generater. But when whe run it:

$ python3 upper2.py *.txt
FOO
HAMS
Traceback (most recent call last):
  File "upper2.py", line 26, in 
assert all(f.closed for f in files)
AssertionError

This is because the last generator uf = upperfile(...) is not garbage-
collected and wasn't explicitly closed either. If you do care here's one 
possible fix:

from contextlib import closing
...
for uf in map(upperfile, sys.argv[1:]):
with closing(uf):
for line in uf:
print(line, end="")
break


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


Re: A mistake which almost went me mad

2016-03-03 Thread Tim Chase
On 2016-03-03 10:43, Nick Sarbicki wrote:
> The number of times I've had to correct a student for naming their
> script "turtle.py".
> 
> And the number of times I've caught myself doing it...

I'm surprised at the number of times I find myself creating an
"email.py" DESPITE KNOWING BETTER EVERY SINGLE TIME.

-tkc




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


Re: yield in try/finally case

2016-03-03 Thread Random832
On Thu, Mar 3, 2016, at 06:52, 刘琦帆 wrote:
> I have just saw PEP 255, and it says that 
> 
> "A yield statement is not allowed in the try clause of a try/finally
> construct.  The difficulty is that there's no guarantee the generator
> will ever be resumed, hence no guarantee that the finally block will ever
> get executed; that's too much a violation of finally's purpose to bear."
> from https://www.python.org/dev/peps/pep-0255/

I'm not sure I understand this reasoning. Why not simply execute it in
__del__ if it hasn't been reached until then? AIUI that is what C# does.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: yield in try/finally case

2016-03-03 Thread Random832
On Thu, Mar 3, 2016, at 08:47, Peter Otten wrote:
> This is because the last generator uf = upperfile(...) is not garbage-
> collected and wasn't explicitly closed either.

But the program hasn't ended yet when you run your assertion.

import sys

_open = open
files = []

def myclose(self):
print("--- closed " + self.name)
self._close()

def open(*args, **kw):
f = _open(*args, **kw)
f._close = f.close
f.close = lambda: myclose(f)
files.append(f)
return f

def upperfile(filename):
with open(filename) as f:
for line in f:
yield line.upper()

for uf in map(upperfile, sys.argv[1:]):
for line in uf:
print(line, end="")
break

print("--- end of program")



FOO
--- closed tmp1.txt
HAMS
--- end of program
--- closed tmp2.txt
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Off-topic] Requests author discusses MentalHealthError exception

2016-03-03 Thread Steven D'Aprano
On Thu, 3 Mar 2016 02:57 pm, Rustom Mody wrote:


> William Blake starts Auguries of Innocence with:
> 
> To see a world in a grain of sand,
> And a heaven in a wild flower,
> Hold infinity in the palm of your hand,
> And eternity in an hour.
> 
> Reading the whole at http://www.artofeurope.com/blake/bla3.htm
> would make this discussion less academic
> 
> Kenneth (at some point) felt he had the mass of the universe.
> 
> So you can choose (one/some of)
> 
> 1. Kenneth is like Blake
> 2. Blake is a lunatic
> 3. Standards of lunacy differ from 17th century to now

Blake did suffer from mental illness. He hallucinated, he had extreme mood
swings, he suffered manic and depressive episodes. Obviously we cannot give
him a reliable diagnosis long after his death, but he may have been
schizophrenic.

http://thesecondsight.blogspot.com.au/2006/02/william-blake-schizophrenic.html
http://www.litkicks.com/Blake/


I don't know if Kenneth is significantly like Blake in any way. Not all
mental illnesses are the same.


> Likewise...
> 
> On Thursday, March 3, 2016 at 8:05:27 AM UTC+5:30, Chris Angelico wrote:
>> On Thu, Mar 3, 2016 at 1:27 PM, Steven D'Aprano  wrote:
>> > We can be absolutely certain that Kenneth weighs less than the entire
>> > universe. We don't even need a set of scales.
>> 
>> Formal proof:
>> 
>> 1) No physical object can have negative mass.
>> 2) I am a part of the universe and have positive mass.
>> 3) I am not Kenneth.
>> 4) The sum of my mass and Kenneth's mass must exceed Kenneth's mass
>> alone.
> 
> What do physical objects have to do with Kenneth's experience?

The question "What is your weight?" refers to a physical property (weight)
of a physical object (Kenneth). What did you think it referred to?


> For you (Chris) you may (choose to) see Kenneth that way
> Kenneth (at least for a while) got out of that notion

Yes, he was having a psychotic episode where his brain was no fully longer
capable of processing. Like a CPU with a short between transistors, he was
(figuratively speaking) adding 1 + 1 and getting strawberry.

It really is disturbing when people suggest that mental dysfunction is
somehow "better" or more profound than mere reality. Or "more real than
reality", as if that actually means something. It is as if somebody tries
to explain how great it must be for those lucky people who are diabetic,
since they are no longer limited to the prosaic and limiting "normal"
functioning of the pancreas. Or how wonderful it would be to have asthma
and no longer be limited to the pedestrian lung functionality that the rest
of us are limited to.

The only difference is that with some kinds of mental illness, the sufferer
is *unable to tell how badly they are affected* because their brain is not
functioning well enough to realise that there is a problem. Dunning-Kruger,
turned up to exploding point.

In some ways, it's like being a drunk who is insists that he's never felt
sharper and more alert, moments before he collapses into an alcoholic
stupor. Or a drug user who is intoxicated but can no longer distinguish
between the actual input to her senses and the hallucinations generated by
her own literally poisoned brain. Or the symptoms of acute lack of oxygen
and hypothermia:

http://www.climbing-high.com/hypothermia.html


-- 
Steven

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


Re: yield in try/finally case

2016-03-03 Thread Steven D'Aprano
On Fri, 4 Mar 2016 02:00 am, Random832 wrote:

> On Thu, Mar 3, 2016, at 06:52, 刘琦帆 wrote:
>> I have just saw PEP 255, and it says that
>> 
>> "A yield statement is not allowed in the try clause of a try/finally
>> construct.  The difficulty is that there's no guarantee the generator
>> will ever be resumed, hence no guarantee that the finally block will ever
>> get executed; that's too much a violation of finally's purpose to bear."
>> from https://www.python.org/dev/peps/pep-0255/
> 
> I'm not sure I understand this reasoning. Why not simply execute it in
> __del__ if it hasn't been reached until then? AIUI that is what C# does.

I believe that under certain circumstances, __del__ may never be executed at
all; it may be executed under rather perilous circumstances where the
interpreter is already shutting down; and even if it is executed, it is not
guaranteed to be executed at any particular time or in any particular
order. In other words, cleaning up in __del__ may be non-deterministic,
while the whole point of try...finally is that the finally block is
executed in a deterministic fashion.

In any case, PEP 255 is obsolete: that is no longer a limitation of yield.


# Python 2.7

py> def gen():
... try:
... yield 1
... finally:
... yield 2
...
py> it = gen()
py> next(it)
1
py> next(it)
2





-- 
Steven

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


Re: Explaining names vs variables in Python

2016-03-03 Thread Mark Lawrence

On 03/03/2016 02:05, Steven D'Aprano wrote:

On Thu, 3 Mar 2016 08:49 am, Mark Lawrence wrote:


On 02/03/2016 17:23, Steven D'Aprano wrote:

On Thu, 3 Mar 2016 01:11 am, Marko Rauhamaa wrote:


What is missing is the rules that are obeyed by the "is" operator.


I think what is actually missing is some common bloody sense. The Python
docs are written in English, and don't define *hundreds*, possible
*thousands* of words because they are using their normal English meaning.

The docs for `is` say:

6.10.3. Identity comparisons

The operators is and is not test for object identity: x is y is true if
and only if x and y are the same object. x is not y yields the inverse
truth value.

https://docs.python.org/3/reference/expressions.html#is-not

In this case, "same object" carries the normal English meaning of "same"
and the normal computer science meaning of "object" in the sense of
"Object Oriented Programming". There's no mystery here, no circular
definition.



Are we discussing UK (highly generalised), Geordie, Glaswegian, US,
Canadian, South African, Australian, New Zealand, or some other form of
English?


To the best of my knowledge, `is` has the same meaning in all variants of
English (although there are sometimes differences in grammatical form,
e.g. "this be that" versus "this is that"). It is a very old word, and such
old words tend to have astonishingly stable semantics and irregular
spelling.

https://en.wiktionary.org/wiki/is#English
https://en.wiktionary.org/wiki/be#English



That's all right then.

Perhaps we can now get back to the OP's question and not some bloody 
stupid philosophical discussion.


--
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: merging number of csv files

2016-03-03 Thread alister
On Thu, 03 Mar 2016 01:46:38 -0800, m.t.egle wrote:

> Hey!
> 
> I have been goggling around for the last few days and tried out many
> python codes.
that is where you are going wrong.
you need to understand the concepts of what you are trying to do and an 
understanding of how the language works. this will allow you to write 
your own code.

once you can do this it is then possible to look at other peoples code to 
see how they have achieved their goals an modify it accordingly. to start 
by looking at others code is putting the cart before the hourse 
 


> I want to merge two csv files, say thought_probe1.csv and
> thought_probe2.csv. I want them to merge column-wise in a new csv file
> 'new_file.csv'.
> What coding is smart to use in order to achieve it?
> 
> I would really appreciate if someone could right a good code below.
> 
> Thanks so much!!
> 
> Best,
> Tiber

use the CSV module

create 2 CSV reader objects and a CSV writer object
read the data from the 2 readers
manipulate the data to the format you require
wright the new data to the CSV writer



-- 
Admiration, n.:
Our polite recognition of another's resemblance to ourselves.
-- Ambrose Bierce, "The Devil's Dictionary"
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: merging number of csv files

2016-03-03 Thread Mark Lawrence

On 03/03/2016 09:46, [email protected] wrote:

Hey!

I have been goggling around for the last few days and tried out many python 
codes.
I want to merge two csv files, say thought_probe1.csv and thought_probe2.csv. I 
want them to merge column-wise in a new csv file 'new_file.csv'.
What coding is smart to use in order to achieve it?

I would really appreciate if someone could right a good code below.

Thanks so much!!

Best,
Tiber



Sorry, we don't write code for you.

Start with the csv module https://docs.python.org/3/library/csv.html

Alternatively try pandas http://pandas.pydata.org/

--
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: Continuing indentation

2016-03-03 Thread John Gordon
In <[email protected]> Marko Rauhamaa  writes:

> Ethan Furman :

> > No, it isn't.  Using '\' for line continuation is strongly discouraged.

> Why would you discourage valid syntax?

Some things that are permissible may not be desirable.

-- 
John Gordon   A is for Amy, who fell down the stairs
[email protected]  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: yield in try/finally case

2016-03-03 Thread Peter Otten
Random832 wrote:

> On Thu, Mar 3, 2016, at 08:47, Peter Otten wrote:
>> This is because the last generator uf = upperfile(...) is not garbage-
>> collected and wasn't explicitly closed either.
> 
> But the program hasn't ended yet when you run your assertion.

If your expectations are in line with Python's actual behaviour -- then 
fine. Normally someone who writes

with acquire_resource() as r:
use(r)
assert r was released

wants the resource to be released when the with suite is left.

When the with-statement is moved into a generator

def gen_resource():
with acquire_resource() as r:
yield r

for r in gen_resource():
use(r)
break # use(r) triggering an exception would have the same effect
assert r was released # may fail

you are at the mercy of the Python interpreter's garbage collection 
strategy. 

Of course you are exiting the for-suite, not the with-suite. Nevertheless 
this surprised me when Oscar pointed it out the first time.

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


Re: yield in try/finally case

2016-03-03 Thread Oscar Benjamin
On 3 March 2016 at 15:12, Random832  wrote:
> On Thu, Mar 3, 2016, at 08:47, Peter Otten wrote:
>> This is because the last generator uf = upperfile(...) is not garbage-
>> collected and wasn't explicitly closed either.
>
> But the program hasn't ended yet when you run your assertion.
>
> import sys
>
> _open = open
> files = []
>
> def myclose(self):
> print("--- closed " + self.name)
> self._close()
>
> def open(*args, **kw):
> f = _open(*args, **kw)
> f._close = f.close
> f.close = lambda: myclose(f)
> files.append(f)
> return f
>
> def upperfile(filename):
> with open(filename) as f:
> for line in f:
> yield line.upper()
>
> for uf in map(upperfile, sys.argv[1:]):
> for line in uf:
> print(line, end="")
> break
>
> print("--- end of program")
>
> 
>
> FOO
> --- closed tmp1.txt
> HAMS
> --- end of program
> --- closed tmp2.txt

If you're happy letting __del__ close the file then why bother with
the context manager in the first place? By the reasoning above we
don't even need to close the file in __del__ since all open files get
closed at process exit anyway. The point is that finally/__exit__ are
not achieving what they are supposed to achieve: the guarantee that
the file is immediately closed when you're done with it.

Try your code under pypy and you'll probably find that your context
manager doesn't fire even at process exit.

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


Re: [Off-topic] Requests author discusses MentalHealthError exception

2016-03-03 Thread Mark Lawrence

On 03/03/2016 03:57, Rustom Mody wrote:

On Thursday, March 3, 2016 at 7:59:13 AM UTC+5:30, Steven D'Aprano wrote:

On Thu, 3 Mar 2016 04:02 am, Rustom Mody wrote:


And how is [1]'s starting different from Kenneth's finding his weight
to be the weight of the universe?


Is that a trick question?

"How is a raven like a writing desk?"

"Neither of them are made of cheese cake."

We can be absolutely certain that Kenneth weighs less than the entire
universe. We don't even need a set of scales.


No trick

William Blake starts Auguries of Innocence with:

To see a world in a grain of sand,
And a heaven in a wild flower,
Hold infinity in the palm of your hand,
And eternity in an hour.

Reading the whole at http://www.artofeurope.com/blake/bla3.htm
would make this discussion less academic

Kenneth (at some point) felt he had the mass of the universe.

So you can choose (one/some of)

1. Kenneth is like Blake
2. Blake is a lunatic
3. Standards of lunacy differ from 17th century to now



I object most strongly to the use of the word "lunatic".

Can we please drop this thread as I'm, and I'm sure others, are finding 
it extremely hurtful.


--
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: A mistake which almost went me mad

2016-03-03 Thread Oscar Benjamin
On 3 March 2016 at 11:48, Tim Chase  wrote:
> On 2016-03-03 10:43, Nick Sarbicki wrote:
>> The number of times I've had to correct a student for naming their
>> script "turtle.py".
>>
>> And the number of times I've caught myself doing it...
>
> I'm surprised at the number of times I find myself creating an
> "email.py" DESPITE KNOWING BETTER EVERY SINGLE TIME.

This mistake is too easy to make and should be fixed in the language
somehow. There's no way that a novice user can know which module names
are implicitly "reserved" by being used somewhere in the stdlib or the
collection of 3rd party modules that they may happen to have
installed.

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


Re: Continuing indentation

2016-03-03 Thread Marko Rauhamaa
John Gordon :

> In <[email protected]> Marko Rauhamaa
>  writes:
>
>> Ethan Furman :
>
>> > No, it isn't.  Using '\' for line continuation is strongly discouraged.
>
>> Why would you discourage valid syntax?
>
> Some things that are permissible may not be desirable.

Line continuations are such a central part of the syntax that it would
seem silly to deprecate them.

While it is true that

   if a and \
  b:
   pass

is ugly,

   if (a and
   b):
   pass

is even uglier.


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


Re: Explaining names vs variables in Python

2016-03-03 Thread Rustom Mody
On Thursday, March 3, 2016 at 7:22:43 AM UTC+5:30, Steven D'Aprano wrote:
> On Thu, 3 Mar 2016 05:12 am, Marko Rauhamaa wrote:
> 
> > Steven D'Aprano :
> > 
> >> In this case, "same object" carries the normal English meaning of
> >> "same" and the normal computer science meaning of "object" in the
> >> sense of "Object Oriented Programming". There's no mystery here, no
> >> circular definition.
> > 
> > I see three possible ways of defining "is" / object identity (and other
> > concepts):
> > 
> >1. hand waving ("normal English")
> > 
> >2. reduction to an underlying model (a real / conceptual computer)
> > 
> >3. formal semantics
> > 
> > All methods are in use. Experienced programmers have #2 in mind but are
> > embarrassed to admit they understand Python through C. Thus, they offer
> > explanation #1 to newbies, who are too embarrassed to admit they don't
> > get the explanation.
> 
> There is no evidence that newbies "don't get the explanation". Completely
> the opposite: newbies frequently find the behaviour of `is` mysterious[1]
> *until* it is explained to them in terms of "same object", after which it
> becomes clear. (At least for those who understand what "object" means in
> OOP terms.)
> 
> And then the same two people, you and Rustom, come along and insist that the
> docs don't define `is` correctly and raising irrelevant philosophical
> objections. But there's no evidence that these issues are the least bit
> relevant to the newbies who asked the questions in the first place.

And now your English use of "same" in "same two people" is getting mysterious.
If you are suggesting that Marko are Rustom are the same person... that's 
mystical. [Maybe we should calculate their weight ? ]

If I try to find a less far out meaning, I need to find some other something
by Marko and/or Rustom...  What??

And then there's Terry:

| The 'is' operator has three uses, two intended and one not. In 
| production code, 'is' tests that an object *is* a particular singular 
| object, such as None or a sentinel instance of class object. In test 
| code, 'is' can also be used to test details of a particular 
| implementation, such as pre-allocation of small ints. New python 
| programmers also use it to confuse themselves.

>From https://mail.python.org/pipermail/python-list/2014-March/668136.html

And Peter Otten further up this same thread:
> You should not bother with object identity for objects other than None.

which, sotto voce, is saying much the same as Terry.

Is it so damn hard to be a bit honest and when asked about is in python to 
reply:

If you dont know what you are doing, dont use 'is' (None excepted)
If you know why are you asking?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Off-topic] Requests author discusses MentalHealthError exception

2016-03-03 Thread alister
On Thu, 03 Mar 2016 13:35:12 +1100, Chris Angelico wrote:

> On Thu, Mar 3, 2016 at 1:27 PM, Steven D'Aprano 
> wrote:
>> We can be absolutely certain that Kenneth weighs less than the entire
>> universe. We don't even need a set of scales.
> 
> Formal proof:
> 
> 1) No physical object can have negative mass.
> 2) I am a part of the universe and have positive mass.
> 3) I am not Kenneth.
> 4) The sum of my mass and Kenneth's mass must exceed Kenneth's mass
> alone.
> 
> Unless someone wants to dispute 1 or 2, we can be logically certain.
> 
> ChrisA

Anti Matter?



-- 
If you would understand your own age, read the works of fiction produced
in it.  People in disguise speak freely.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A mistake which almost went me mad

2016-03-03 Thread Rob Gaddi
Oscar Benjamin wrote:

> On 3 March 2016 at 11:48, Tim Chase  wrote:
>> On 2016-03-03 10:43, Nick Sarbicki wrote:
>>> The number of times I've had to correct a student for naming their
>>> script "turtle.py".
>>>
>>> And the number of times I've caught myself doing it...
>>
>> I'm surprised at the number of times I find myself creating an
>> "email.py" DESPITE KNOWING BETTER EVERY SINGLE TIME.
>
> This mistake is too easy to make and should be fixed in the language
> somehow. There's no way that a novice user can know which module names
> are implicitly "reserved" by being used somewhere in the stdlib or the
> collection of 3rd party modules that they may happen to have
> installed.
>

Inside of modules it is, under Python 3.  Inside of modules there's a
clear distinction of:
  import thingfromstdlib
  from . import thingfromlocal

A bit of a nuisance at first, but once you get used to it everything
just makes unambiguous sense.

The problem is that this same distinction doesn't get made for
"programs", only for "modules".  So, among other issues, you wind up
unable to run tests inside of module directories because the syntax
becomes wrong.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Off-topic] Requests author discusses MentalHealthError exception

2016-03-03 Thread Ian Kelly
On Thu, Mar 3, 2016 at 10:21 AM, alister  wrote:
> On Thu, 03 Mar 2016 13:35:12 +1100, Chris Angelico wrote:
>> 1) No physical object can have negative mass.
>> 2) I am a part of the universe and have positive mass.
>> 3) I am not Kenneth.
>> 4) The sum of my mass and Kenneth's mass must exceed Kenneth's mass
>> alone.
>>
>> Unless someone wants to dispute 1 or 2, we can be logically certain.
>>
>> ChrisA
>
> Anti Matter?

Antimatter has positive mass.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Continuing indentation

2016-03-03 Thread Rob Gaddi
Marko Rauhamaa wrote:

> John Gordon :
>
>> In <[email protected]> Marko Rauhamaa
>>  writes:
>>
>>> Ethan Furman :
>>
>>> > No, it isn't.  Using '\' for line continuation is strongly discouraged.
>>
>>> Why would you discourage valid syntax?
>>
>> Some things that are permissible may not be desirable.
>
> Line continuations are such a central part of the syntax that it would
> seem silly to deprecate them.
>
> While it is true that
>
>if a and \
>   b:
>pass
>
> is ugly,
>
>if (a and
>b):
>pass
>
> is even uglier.
>
>
> Marko

Not ugly, error-prone.  The first is purely aestehetic, the second
actually matters.  Let something as simple as a trailing space sneak in
after your backslash and your meaning changes.  Blank line between; same
thing.

Granted in Python that will tend to lead to a SyntaxError, rather than
silently shoot you in the foot the way this used to in C:

if (condition)
   first_action();
   section_action();
else {
   alternate action();
}

But the principle remains.  Syntactic whitespace has its ups and downs
on the leading edge of the line, but at least it's visible there.  On
the trailing end of the line it's actively inviting trouble in for
coffee and eggs.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
-- 
https://mail.python.org/mailman/listinfo/python-list


creating zipfile with symlinks

2016-03-03 Thread Larry Martell
I have a script that creates zip files of dirs containing symlinks. I
was surprised to find that the zipfiles have zipped the targets of the
links as opposed to the links themselves, which is what I wanted and
expected. Googling I found this:

https://doeidoei.wordpress.com/2010/11/23/compressing-files-with-python-symlink-trouble/

Which I don't love as a solution. Anyone know a cleaner way to get
zipfile to zip the links?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Continuing indentation

2016-03-03 Thread Marko Rauhamaa
Rob Gaddi :

> Not ugly, error-prone. The first is purely aestehetic, the second
> actually matters. Let something as simple as a trailing space sneak in
> after your backslash and your meaning changes. Blank line between;
> same thing.

Never been bitten by that.

Now, trying how emacs' indentation would react to such a syntax error, I
notice:

if some_condition \ 
and some_other_condition \
and some_final_condition:
play_bingo()

The misalignment is a surefire way to notice something is fishy (in all
programming languages).

> But the principle remains. Syntactic whitespace has its ups and downs
> on the leading edge of the line, but at least it's visible there. On
> the trailing end of the line it's actively inviting trouble in for
> coffee and eggs.

Continuation lines with backslashes are all over the place: make, bash,
C, Python. I can't remember such accidents taking place.

Now, *leading* whitespace causes all kinds of mischief including jagged
diffs and syntax errors.


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


Re: Explaining names vs variables in Python

2016-03-03 Thread Ian Kelly
On Thu, Mar 3, 2016 at 10:03 AM, Rustom Mody  wrote:
> Is it so damn hard to be a bit honest and when asked about is in python to 
> reply:
>
> If you dont know what you are doing, dont use 'is' (None excepted)
> If you know why are you asking?

That seems like a rather unhelpful response.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Off-topic] Requests author discusses MentalHealthError exception

2016-03-03 Thread alister
On Thu, 03 Mar 2016 11:03:55 -0700, Ian Kelly wrote:

> On Thu, Mar 3, 2016 at 10:21 AM, alister 
> wrote:
>> On Thu, 03 Mar 2016 13:35:12 +1100, Chris Angelico wrote:
>>> 1) No physical object can have negative mass.
>>> 2) I am a part of the universe and have positive mass.
>>> 3) I am not Kenneth.
>>> 4) The sum of my mass and Kenneth's mass must exceed Kenneth's mass
>>> alone.
>>>
>>> Unless someone wants to dispute 1 or 2, we can be logically certain.
>>>
>>> ChrisA
>>
>> Anti Matter?
> 
> Antimatter has positive mass.

Are you sure?
 mix 1 atom of hydrogen + 1 of anti hydrogen & you end up with 0 mass (+ 
LOTTS of energy)

To be honest it is all over my head



-- 
I used to be Snow White, but I drifted.
-- Mae West
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Off-topic] Requests author discusses MentalHealthError exception

2016-03-03 Thread Ian Kelly
On Thu, Mar 3, 2016 at 1:20 PM, alister  wrote:
> On Thu, 03 Mar 2016 11:03:55 -0700, Ian Kelly wrote:
>
>> On Thu, Mar 3, 2016 at 10:21 AM, alister 
>> wrote:
>>> On Thu, 03 Mar 2016 13:35:12 +1100, Chris Angelico wrote:
 1) No physical object can have negative mass.
 2) I am a part of the universe and have positive mass.
 3) I am not Kenneth.
 4) The sum of my mass and Kenneth's mass must exceed Kenneth's mass
 alone.

 Unless someone wants to dispute 1 or 2, we can be logically certain.

 ChrisA
>>>
>>> Anti Matter?
>>
>> Antimatter has positive mass.
>
> Are you sure?
>  mix 1 atom of hydrogen + 1 of anti hydrogen & you end up with 0 mass (+
> LOTTS of energy)
>
> To be honest it is all over my head

Ah, I thought you were challenging 1) or 2), not 4). Anyway, we can
sum the mass of matter and antimatter without annihilating them.
-- 
https://mail.python.org/mailman/listinfo/python-list


Caching function results

2016-03-03 Thread Pavel Volkov
Suppose, I have some resource-intensive tasks implemented as functions in 
Python.

Those are called repeatedly in my program.
It's guranteed that a call with the same arguments always produces the same 
return value.
I want to cache the arguments and return values and in case of repititive 
call immediately return the result without doing expensive calculations.


I intend to do it this way:

# module 'cache.py'

class Cache:
   def save_result(self, handle, args):
   """Save calculated result to cache."""
   <...>
   def load_result(self, handle, args, result):
   """Load previously calculated result from cache.
   Return None is it's unavailable."""
   <...>
   def save_to_file(self, filename):
   """Save all cached data to a file."""
   def __init__(self, filename=None):
   # Optionally loads previously cached data from a file
   def clear(self):
   <...>


# module 'calculations.py'

import cache
_cache = cache.Cache()

def big_task(arg1, arg2, arg3=None):
   cached_value = _cache.load_result('big_task', (arg1, arg2, arg3))
   if cached_value is not None:
   return cached_value
   
   result = <...>
   _cache.save_result('big_task', (arg1, arg2, arg3), result)
   return result

The arguments and return values are almost always:
* ints
* floats
* tuple or lists of ints or floats

I think Cache object will store data in a dictionary.
I'll convert lists to tuples before storing them.

I'd also like to limit the size of the cache (in MB) and get rid of old 
cached data.

Don't know how yet.

Do you like this design or maybe there's a better way with Python's 
included batteries?


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


Re: Caching function results

2016-03-03 Thread Martin A. Brown

Greetings Pavel,

> Suppose, I have some resource-intensive tasks implemented as 
> functions in Python. Those are called repeatedly in my program. 
> It's guranteed that a call with the same arguments always produces 
> the same return value. I want to cache the arguments and return 
> values and in case of repititive call immediately return the 
> result without doing expensive calculations.

Great problem description.  Thank you for being so clear.

[I snipped sample code...]

This is generically called memoization.

> Do you like this design or maybe there's a better way with 
> Python's included batteries?

In Python, there's an implementation available for you in the 
functools module.  It's called lru_cache.  LRU means 'Least Recently 
Used'.

> I'd also like to limit the size of the cache (in MB) and get rid 
> of old cached data. Don't know how yet.

You can also limit the size of the lru_cache provided by the 
functools module.  For this function, the size is calculated by 
number of entries--so you will need to figure out memory size to 
cache entry count.

Maybe others who have used functools.lru_cache can help you with how 
they solved the problem of mapping entry count to memory usage.

Good luck,

-Martin

 [0] https://docs.python.org/3/library/functools.html#functools.lru_cache

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


Re: Caching function results

2016-03-03 Thread Ian Kelly
On Thu, Mar 3, 2016 at 1:28 PM, Pavel Volkov  wrote:
> Suppose, I have some resource-intensive tasks implemented as functions in
> Python.
> Those are called repeatedly in my program.
> It's guranteed that a call with the same arguments always produces the same
> return value.
> I want to cache the arguments and return values and in case of repititive
> call immediately return the result without doing expensive calculations.
>
> I intend to do it this way:
>
> # module 'cache.py'
>
> class Cache:
>def save_result(self, handle, args):
>"""Save calculated result to cache."""
><...>
>def load_result(self, handle, args, result):
>"""Load previously calculated result from cache.
>Return None is it's unavailable."""
><...>
>def save_to_file(self, filename):
>"""Save all cached data to a file."""
>def __init__(self, filename=None):
># Optionally loads previously cached data from a file
>def clear(self):
><...>
>
>
> # module 'calculations.py'
>
> import cache
> _cache = cache.Cache()
>
> def big_task(arg1, arg2, arg3=None):
>cached_value = _cache.load_result('big_task', (arg1, arg2, arg3))
>if cached_value is not None:
>return cached_value
>
>result = <...>
>_cache.save_result('big_task', (arg1, arg2, arg3), result)
>return result
>
> The arguments and return values are almost always:
> * ints
> * floats
> * tuple or lists of ints or floats
>
> I think Cache object will store data in a dictionary.
> I'll convert lists to tuples before storing them.
>
> I'd also like to limit the size of the cache (in MB) and get rid of old
> cached data.
> Don't know how yet.
>
> Do you like this design or maybe there's a better way with Python's included
> batteries?

The idiomatic way is to use functools.lru_cache like so:

import functools

@functools.lru_cache(maxsize=128)
def big_task(arg1, arg2, arg3=None):
...

This does not give you the option to save the cache to a file however,
and the maxsize is specified in entries, not MB.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: creating zipfile with symlinks

2016-03-03 Thread Larry Martell
On Thu, Mar 3, 2016 at 1:37 PM, Larry Martell  wrote:
> I have a script that creates zip files of dirs containing symlinks. I
> was surprised to find that the zipfiles have zipped the targets of the
> links as opposed to the links themselves, which is what I wanted and
> expected. Googling I found this:
>
> https://doeidoei.wordpress.com/2010/11/23/compressing-files-with-python-symlink-trouble/
>
> Which I don't love as a solution. Anyone know a cleaner way to get
> zipfile to zip the links?

Actually the code on that site doesn't work at all, so it's not a solution :-(
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Caching function results

2016-03-03 Thread Peter Otten
Pavel Volkov wrote:

> Suppose, I have some resource-intensive tasks implemented as functions in
> Python.
> Those are called repeatedly in my program.
> It's guranteed that a call with the same arguments always produces the
> same return value.
> I want to cache the arguments and return values and in case of repititive
> call immediately return the result without doing expensive calculations.

> The arguments and return values are almost always:
> * ints
> * floats
> * tuple or lists of ints or floats
> 
> I think Cache object will store data in a dictionary.
> I'll convert lists to tuples before storing them.
> 
> I'd also like to limit the size of the cache (in MB) and get rid of old
> cached data.

https://docs.python.org/dev/library/functools.html#functools.lru_cache

The only thing that is missing is list-to-tuple conversion.

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


Re: A mistake which almost went me mad

2016-03-03 Thread Tim Chase
On 2016-03-03 16:29, Oscar Benjamin wrote:
> On 3 March 2016 at 11:48, Tim Chase 
> wrote:
> > On 2016-03-03 10:43, Nick Sarbicki wrote:
> >> The number of times I've had to correct a student for naming
> >> their script "turtle.py".
> >>
> >> And the number of times I've caught myself doing it...
> >
> > I'm surprised at the number of times I find myself creating an
> > "email.py" DESPITE KNOWING BETTER EVERY SINGLE TIME.
> 
> This mistake is too easy to make and should be fixed in the language
> somehow. There's no way that a novice user can know which module
> names are implicitly "reserved" by being used somewhere in the
> stdlib or the collection of 3rd party modules that they may happen
> to have installed.

I think that relative imports should ameliorate this, as I usually
hit it when I'm using smtplib which in turn imports "email" (and, in
2.x when it found my local email.py would crash and burn). If it used
a relative import that forced it to find the one in the stdlib, it
should(?) prevent it from finding my local version first.

-tkc



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


Re: creating zipfile with symlinks

2016-03-03 Thread MRAB

On 2016-03-03 20:58, Larry Martell wrote:

On Thu, Mar 3, 2016 at 1:37 PM, Larry Martell  wrote:

I have a script that creates zip files of dirs containing symlinks. I
was surprised to find that the zipfiles have zipped the targets of the
links as opposed to the links themselves, which is what I wanted and
expected. Googling I found this:

https://doeidoei.wordpress.com/2010/11/23/compressing-files-with-python-symlink-trouble/

Which I don't love as a solution. Anyone know a cleaner way to get
zipfile to zip the links?


Actually the code on that site doesn't work at all, so it's not a solution :-(


Is it even possible to zip a link?

A quick search came up with this:

Are hard links possible within a zip archive?
http://stackoverflow.com/questions/8859616/are-hard-links-possible-within-a-zip-archive

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


Re: creating zipfile with symlinks

2016-03-03 Thread Chris Angelico
On Fri, Mar 4, 2016 at 8:38 AM, MRAB  wrote:
> Is it even possible to zip a link?
>
> A quick search came up with this:
>
> Are hard links possible within a zip archive?
> http://stackoverflow.com/questions/8859616/are-hard-links-possible-within-a-zip-archive

Hard links are different. Symlinks are files containing the target
filename, with a special mode bit set. I'm not sure if it's a standard
feature of all zip archivers, but on my Debian system, I can use "zip
--symlinks" to create such a zip. How that will unzip on a system that
doesn't understand symlinks, I don't know.

rosuav@sikorsky:~/tmp$ ls -l
total 4
-rw-r--r-- 1 rosuav rosuav 162 Mar  4 08:48 aaa.zip
lrwxrwxrwx 1 rosuav rosuav   4 Mar  4 08:49 qwer -> asdf
rosuav@sikorsky:~/tmp$ unzip -l aaa.zip
Archive:  aaa.zip
  Length  DateTimeName
-  -- -   
4  2016-03-04 08:45   qwer
- ---
4 1 file


That's a broken symlink (there is no "asdf" in the directory), and zip
and unzip are both fine with that.

Now, how the Python zipfile module handles this, I don't know. The
ZipInfo shows a file mode of 'lrwxrwxrwx', but when I call extract(),
it comes out as a regular file. You might have to do some work
manually, or else just drop to an external command with --symlinks.

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


list reversal error

2016-03-03 Thread vlyamtsev
i have list of strings "data" and i am trying to build reverse list data1
data1 = []
for i in range(len(data)):
   j = len(data) - i
   data1.append(data[j])

but i have the following error:
data1.append(data[j])
IndexError: list index out of range
 
am i doing it wrong?
Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Inception

2016-03-03 Thread Denis Akhiyarov
Thank you Paul and Christian for your reply and understanding my question. I 
was actually inspired by this example for IronPython and thought that this is 
pretty useful for testing and exploring of C-API:

http://www.voidspace.org.uk/ironpython/ip_in_ip.shtml

So does it all boil down to GIL restriction in CPython?



On Thursday, March 3, 2016 at 2:10:15 AM UTC-6, Christian Gollwitzer wrote:
> Hi Denis,
> 
> Am 03.03.16 um 06:01 schrieb Denis Akhiyarov:
> > Is it possible to embed CPython in CPython, e.g. using ctypes, cffi, or 
> > cython?
> >
> 
> since you titled your question with a famous movie title, I take it more 
> as a "philosophical" question (musing about the CPython world) than an 
> actual demand. Technically, I think it cannot work with the current 
> state of CPython. Embedding CPython into another application requires 
> you to load libpython3.5.so (or similar depending on OS and version) and 
> to call the functino sfrom within there. When a Python program runs in 
> the standalone interpreter, then this DLL is already loaded, obviously, 
> so the question is if the Python C API functions can be called from a 
> Python script.
> 
> Possibly  ctypes or cffi can do that for you, but you would end up with 
> the same interpreter that executes your main script. A DLL is loaded by 
> the OS only once, which makes it possible to create plugin interfaces 
> etc. using shared symbols in C.
> 
> Now, the CPython interpreter saves its state in global variables. 
> Therefore only one interpreter can exist in a single process, and this 
> is also the reason that multithreading is complicated and does not 
> really work (GIL &c).
> 
> How useful could it be to have more than a single interpreter? For 
> instance, Tcl does it in a different way. All of the interpreter state 
> is stored within a Tcl_Interp struct which is passed around to every API 
> function. You can create more than a single interpreter, and that even 
> works from the script level using the "interp create" command. This is 
> useful to shield user script evaluation from the main script. Such 
> interpreters can be made "safe", i.e. to not allow file operations, or 
> to limit the CPU time for script evaluation, which has its primary use 
> case in things like web browser plugins or simple servers. A pythonized 
> version of the API would look like this:
> 
> from pyembedding import Interp
> 
> i=Interp()
> i2=Interp(safe=True)
> 
> i.eval('print("Hello world")')
> # prints Hello world
> 
> i2.eval('print("Hello world")')
> # raises a SafeInterpException
> # I/O  is unsafe and disabled here
> 
> def greetme():
>   print("Hello world")
> 
> i2.alias(greet=greetme)
> i2.eval('greet')
> # calls back to greetme in main interpreter
> 
> This mechanism would also allow true multithreading. If you run another 
> interpreter in the second thread, it will have it's own GIL.
> 
> I'm not familiar with the internals of other Python implementation, but 
> I can imagine that maybe in Jython such an extension could be written.
> 
>   Christian

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


Re: list reversal error

2016-03-03 Thread John Gordon
In <[email protected]> [email protected] 
writes:

> i have list of strings "data" and i am trying to build reverse list data1
> data1 = []
> for i in range(len(data)):
>j = len(data) - i
>data1.append(data[j])

> but i have the following error:
> data1.append(data[j])
> IndexError: list index out of range
>  
> am i doing it wrong?
> Thanks

Python lists are zero-indexed, meaning a list of five items will have
indexes 0 to 4.

The first time through your loop, i is 0, so

j = len(data) - i 

evaluates to

j = len(data)

which would yield 5 for a five-element list, but the last actual element
is in data[4].

-- 
John Gordon   A is for Amy, who fell down the stairs
[email protected]  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: list reversal error

2016-03-03 Thread Joel Goldstick
On Thu, Mar 3, 2016 at 6:08 PM, John Gordon  wrote:

> In <[email protected]>
> [email protected] writes:
>
> > i have list of strings "data" and i am trying to build reverse list data1
> > data1 = []
> > for i in range(len(data)):
> >j = len(data) - i
> >data1.append(data[j])
>
> > but i have the following error:
> > data1.append(data[j])
> > IndexError: list index out of range
> >
> > am i doing it wrong?
> > Thanks
>
> Python lists are zero-indexed, meaning a list of five items will have
> indexes 0 to 4.
>
> The first time through your loop, i is 0, so
>
> j = len(data) - i
>
> evaluates to
>
> j = len(data)
>
> which would yield 5 for a five-element list, but the last actual element
> is in data[4].
>

>>> s = "123"
>>> s2 = s[::-1]
>>> s2
'321'
>>>

Use reverse slice

>
> --
> John Gordon   A is for Amy, who fell down the stairs
> [email protected]  B is for Basil, assaulted by bears
> -- Edward Gorey, "The Gashlycrumb Tinies"
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com/ 
http://cc-baseballstats.info/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: list reversal error

2016-03-03 Thread MRAB

On 2016-03-03 23:08, John Gordon wrote:

In <[email protected]> [email protected] 
writes:


i have list of strings "data" and i am trying to build reverse list data1
data1 = []
for i in range(len(data)):
   j = len(data) - i
   data1.append(data[j])



but i have the following error:
data1.append(data[j])
IndexError: list index out of range

am i doing it wrong?
Thanks


Python lists are zero-indexed, meaning a list of five items will have
indexes 0 to 4.

The first time through your loop, i is 0, so

 j = len(data) - i

evaluates to

 j = len(data)

which would yield 5 for a five-element list, but the last actual element
is in data[4].


A simpler alternative is to use 'reversed':

data1 = list(reversed(data))

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


Re: list reversal error

2016-03-03 Thread Mark Lawrence

On 03/03/2016 22:51, [email protected] wrote:

i have list of strings "data" and i am trying to build reverse list data1
data1 = []
for i in range(len(data)):
j = len(data) - i
data1.append(data[j])

but i have the following error:
data1.append(data[j])
IndexError: list index out of range


At the first pass through the for loop, j is effectively set to 
len(data).  As Python is indexed from zero this will always take you 
beyond the end of data, hence the IndexError.




am i doing it wrong?
Thanks



You are doing things the difficult way.  First up, using the construct:-

for i in range(len(data)):

is usually not needed in Python.

There is a reversed function 
https://docs.python.org/3/library/functions.html#reversed which will do 
the job for you.


data1 = list(reversed(data))

Or use the slice notation

data1 = data[::-1]

--
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: list reversal error

2016-03-03 Thread Gary Herron

On 03/03/2016 02:51 PM, [email protected] wrote:

i have list of strings "data" and i am trying to build reverse list data1
data1 = []
for i in range(len(data)):
j = len(data) - i
data1.append(data[j])

but i have the following error:
data1.append(data[j])
IndexError: list index out of range
  
am i doing it wrong?

Thanks


Look at the values (say with a print) you get from your line
j = len(data) - i
You'll find that that produces (with a list of 4 elements for example) 
4,3,2,1 when in fact you want 3,2,1,0. Soo you really want

j = len(data) - i -1


Better yet, use more of Python with
data1 = list(reversed(data))

Or don't even make a new list, just reverse the original list in place
>>> L=[1,2,3]
>>> L.reverse()
>>> L
[3, 2, 1]

Or even better, if you simply want to iterate through the original list, 
but in reverse order:

for datum in reversed(data):
... whatever with datum ...
which wastes no time actually reversing the list, but simply loops 
through them back to front.



Gary Herron

--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

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


Re: [Off-topic] Requests author discusses MentalHealthError exception

2016-03-03 Thread William Ray Wing

> On Mar 3, 2016, at 3:20 PM, alister  wrote:
> 
> On Thu, 03 Mar 2016 11:03:55 -0700, Ian Kelly wrote:
> 
>> On Thu, Mar 3, 2016 at 10:21 AM, alister 
>> wrote:
>>> On Thu, 03 Mar 2016 13:35:12 +1100, Chris Angelico wrote:
 1) No physical object can have negative mass.
 2) I am a part of the universe and have positive mass.
 3) I am not Kenneth.
 4) The sum of my mass and Kenneth's mass must exceed Kenneth's mass
 alone.
 
 Unless someone wants to dispute 1 or 2, we can be logically certain.
 
 ChrisA
>>> 
>>> Anti Matter?
>> 
>> Antimatter has positive mass.
> 
> Are you sure?

The ALPHA experiment at CERN is attempting a direct measurement of the mass of 
anti-hydrogen by trapping atoms of the stuff in a penning trap at high vacuum. 
The answer isn’t definitive yet as the error bars are huge and extend past 
zero, but are centered on the positive side.

-Bill


> mix 1 atom of hydrogen + 1 of anti hydrogen & you end up with 0 mass (+ 
> LOTTS of energy)
> 
> To be honest it is all over my head
> 
> 
> 
> -- 
> I used to be Snow White, but I drifted.
>   -- Mae West
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


[Still off-top] Physics [was Requests author discusses MentalHealthError exception]

2016-03-03 Thread Steven D'Aprano
On Fri, 4 Mar 2016 07:20 am, alister wrote:

> On Thu, 03 Mar 2016 11:03:55 -0700, Ian Kelly wrote:

>> Antimatter has positive mass.
> 
> Are you sure?
>  mix 1 atom of hydrogen + 1 of anti hydrogen & you end up with 0 mass (+
> LOTTS of energy)
> 
> To be honest it is all over my head

It's good to be honest :-)


Yes, anti-matter has positive mass. There was still some tiny lingering
doubt up until (by memory) 20 years ago, at which time physicists actually
managed to make sufficient anti-matter that they could assemble it into
slow-moving atoms and observe the effect of gravity on it, and sure enough,
anti-matter falls due to gravity the same as regular matter.

(There wasn't really any serious doubt about this, since you can pull and
push anti-matter with electric and magnetic fields and it behaves exactly
the same way as regular matter. But if gravity had turned out to be
different, it would have been a truly paradigm-changing discovery.)

As far as the reaction of matter and anti-matter, we've known for about a
century that mass and energy are related and freely convertible from one to
the other. That's the famous equation by Einstein: E = m*c**2. Even tiny
amounts of energy (say, the light and heat released from a burning match)
involve a correspondingly tiny reduction in mass.


-- 
Steven

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


Re: list reversal error

2016-03-03 Thread Steven D'Aprano
On Fri, 4 Mar 2016 09:51 am, [email protected] wrote:

> i have list of strings "data" and i am trying to build reverse list data1

Use a slice with a negative step-size and defaults for the start and end
positions.


data1 = data[::-1]




-- 
Steven

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


Re: Continuing indentation

2016-03-03 Thread Steven D'Aprano
On Fri, 4 Mar 2016 03:47 am, Marko Rauhamaa wrote:

> John Gordon :
> 
>> In <[email protected]> Marko Rauhamaa
>>  writes:
>>
>>> Ethan Furman :
>>
>>> > No, it isn't.  Using '\' for line continuation is strongly
>>> > discouraged.
>>
>>> Why would you discourage valid syntax?
>>
>> Some things that are permissible may not be desirable.
> 
> Line continuations are such a central part of the syntax that it would
> seem silly to deprecate them.

What a wonderfully wrong sentence! Line continuations are not a central part
of the syntax, they've very much a minor and rarely used part.

Indentation is a central part of the syntax. Students need to learn about
indentation right from the beginning, and it is barely possible to go ten
minutes of writing Python code without using indentation. But line
continuations? It is quite possible to go years between seeing \ line
continuations in code, and it is never *necessary* to use them.

Nevertheless, they are not deprecated. They are just *discouraged*. Are you
familiar with the difference?


Deprecate: a formal or semi-formal process whereby features are removed from
a programming language.


Discourage: a social process whereby use of a feature is deterred but not
prohibited.



> While it is true that
> 
>if a and \
>   b:
>pass
> 
> is ugly,
> 
>if (a and
>b):
>pass
> 
> is even uglier.


It's silly to break such short expressions over multiple lines, and you
cannot judge the aesthetics of long expressions by looking at short ones.


class C:
def method(self):
if (result is None 
or self.some_condition()
or len(some_sequence) > 100
or some_other_condition
or page_count < 5
):
do_processing()


Looks fine to me.



-- 
Steven

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


Re: Continuing indentation

2016-03-03 Thread INADA Naoki
>
>
> class C:
> def method(self):
> if (result is None
> or self.some_condition()
> or len(some_sequence) > 100
> or some_other_condition
> or page_count < 5
> ):
> do_processing()
>
>
> Looks fine to me.
>
>
Looks nice to me too.  But...

```
$ cat > t.py
class C:
def method(self):
if (result is None
or self.some_condition()
or len(some_sequence) > 100
or some_other_condition
or page_count < 5
):
do_processing()

$ pep8 t.py
t.py:4:17: W503 line break before binary operator
t.py:5:17: W503 line break before binary operator
t.py:6:17: W503 line break before binary operator
t.py:7:17: W503 line break before binary operator
t.py:8:5: E125 continuation line with same indent as next logical line
```

pep8.py is t strict.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Continuing indentation

2016-03-03 Thread Erik

On 04/03/16 00:13, Steven D'Aprano wrote:

class C:
 def method(self):
 if (result is None
 or self.some_condition()
 or len(some_sequence) > 100
 or some_other_condition
 or page_count < 5
 ):
 do_processing()

Looks fine to me.


Indeed. I don't understand why, when splitting a condition such as this, 
people tend to put the operator at the end of each line.


In C, I also prefer (a style copied from an old colleague of mine who 
had lots of strange ideas, but I liked this one ;)) -


if (  long_condition
  &&  other_long_condition
  &&  (another_long_condition
|| yet_another_long_condition)
  || some_other_condition) {
process();
}

I just find that so much easier to grok than:

if (long_condition &&
other_long_condition &&
(another_long_condition ||
 yet_another_long_condition) ||
some_other_condition) {
process();
}

Also, it sort of lays out just what the short-circuit evaluation is 
going to do, so when those long conditions are /actually/ long and 
require a bit of mental parsing, you can scan the left hand side of the 
code and not have to read most of it as you work out which conditions 
may be true. With the second form, you have to parse every line to work 
out if the operator at the end is a top-level operator or part of a 
sub-condition.


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


Re: Continuing indentation

2016-03-03 Thread INADA Naoki
>
>
> Indeed. I don't understand why, when splitting a condition such as this,
> people tend to put the operator at the end of each line.
>
>
Because PEP8 says:

> The preferred place to break around a binary operator is after the
operator, not before it.
http://pep8.org/#maximum-line-length
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Continuing indentation

2016-03-03 Thread Erik

On 04/03/16 01:23, INADA Naoki wrote:

Indeed. I don't understand why, when splitting a condition such as this,
people tend to put the operator at the end of each line.



Because PEP8 says:


The preferred place to break around a binary operator is after the

operator, not before it.


I mean in the general scheme of things, which is why I gave a C example
too ;)

Perhaps I am challenging the wisdom of of PEP8 ;)

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


Re: creating zipfile with symlinks

2016-03-03 Thread Larry Martell
On Thu, Mar 3, 2016 at 4:58 PM, Chris Angelico  wrote:
> On Fri, Mar 4, 2016 at 8:38 AM, MRAB  wrote:
>> Is it even possible to zip a link?
>>
>> A quick search came up with this:
>>
>> Are hard links possible within a zip archive?
>> http://stackoverflow.com/questions/8859616/are-hard-links-possible-within-a-zip-archive
>
> Hard links are different. Symlinks are files containing the target
> filename, with a special mode bit set. I'm not sure if it's a standard
> feature of all zip archivers, but on my Debian system, I can use "zip
> --symlinks" to create such a zip. How that will unzip on a system that
> doesn't understand symlinks, I don't know.
>
> rosuav@sikorsky:~/tmp$ ls -l
> total 4
> -rw-r--r-- 1 rosuav rosuav 162 Mar  4 08:48 aaa.zip
> lrwxrwxrwx 1 rosuav rosuav   4 Mar  4 08:49 qwer -> asdf
> rosuav@sikorsky:~/tmp$ unzip -l aaa.zip
> Archive:  aaa.zip
>   Length  DateTimeName
> -  -- -   
> 4  2016-03-04 08:45   qwer
> - ---
> 4 1 file
>
>
> That's a broken symlink (there is no "asdf" in the directory), and zip
> and unzip are both fine with that.
>
> Now, how the Python zipfile module handles this, I don't know. The
> ZipInfo shows a file mode of 'lrwxrwxrwx', but when I call extract(),
> it comes out as a regular file. You might have to do some work
> manually, or else just drop to an external command with --symlinks.

Thanks. That's what I ended up doing.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Continuing indentation

2016-03-03 Thread Steven D'Aprano
On Fri, 4 Mar 2016 12:23 pm, INADA Naoki wrote:

>>
>>
>> Indeed. I don't understand why, when splitting a condition such as this,
>> people tend to put the operator at the end of each line.
>>
>>
> Because PEP8 says:
> 
>> The preferred place to break around a binary operator is after the
> operator, not before it.
> http://pep8.org/#maximum-line-length

PEP 8 is wrong :-)



-- 
Steven

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


Re: A mistake which almost went me mad

2016-03-03 Thread Gregory Ewing

Tim Golden wrote:

A few teachers recently were discussing this on Twitter. One suggested
that his pupils always add their initials to whatever filename they use.


Works well until Lawrence Ian Bernstein writes his own
module called "url"...

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


Re: Continuing indentation

2016-03-03 Thread Marko Rauhamaa
Steven D'Aprano :

> class C:
> def method(self):
> if (result is None 
> or self.some_condition()
> or len(some_sequence) > 100
> or some_other_condition
> or page_count < 5
> ):
> do_processing()
>
>
> Looks fine to me.

The class is aptly named "C"; the parentheses give a C-esque feel to the
statement.

Continuation lines abound in the standard lib sources. For example:


   difflib.py:

# Extend the best by non-junk elements on each end.  In particular,
# "popular" non-junk elements aren't in b2j, which greatly speeds
# the inner loop above, but also means "the best" match so far
# doesn't contain any junk *or* popular non-junk elements.
while besti > alo and bestj > blo and \
  not isbjunk(b[bestj-1]) and \
  a[besti-1] == b[bestj-1]:
besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
while besti+bestsize < ahi and bestj+bestsize < bhi and \
  not isbjunk(b[bestj+bestsize]) and \
  a[besti+bestsize] == b[bestj+bestsize]:
bestsize += 1

# Now that we have a wholly interesting match (albeit possibly
# empty!), we may as well suck up the matching junk on each
# side of it too.  Can't think of a good reason not to, and it
# saves post-processing the (possibly considerable) expense of
# figuring out what to do with it.  In the case of an empty
# interesting match, this is clearly the right thing to do,
# because no other kind of match is possible in the regions.
while besti > alo and bestj > blo and \
  isbjunk(b[bestj-1]) and \
  a[besti-1] == b[bestj-1]:
besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
while besti+bestsize < ahi and bestj+bestsize < bhi and \
  isbjunk(b[bestj+bestsize]) and \
  a[besti+bestsize] == b[bestj+bestsize]:
bestsize = bestsize + 1



   ast.py:

def literal_eval(node_or_string):
"""
Safely evaluate an expression node or a string containing a Python
expression.  The string or node provided may only consist of the following
Python literal structures: strings, bytes, numbers, tuples, lists, dicts,
sets, booleans, and None.
"""
if isinstance(node_or_string, str):
node_or_string = parse(node_or_string, mode='eval')
if isinstance(node_or_string, Expression):
node_or_string = node_or_string.body
def _convert(node):
if isinstance(node, (Str, Bytes)):
return node.s
elif isinstance(node, Num):
return node.n
elif isinstance(node, Tuple):
return tuple(map(_convert, node.elts))
elif isinstance(node, List):
return list(map(_convert, node.elts))
elif isinstance(node, Set):
return set(map(_convert, node.elts))
elif isinstance(node, Dict):
return dict((_convert(k), _convert(v)) for k, v
in zip(node.keys, node.values))
elif isinstance(node, NameConstant):
return node.value
elif isinstance(node, UnaryOp) and \
 isinstance(node.op, (UAdd, USub)) and \
 isinstance(node.operand, (Num, UnaryOp, BinOp)):
operand = _convert(node.operand)
if isinstance(node.op, UAdd):
return + operand
else:
return - operand
elif isinstance(node, BinOp) and \
 isinstance(node.op, (Add, Sub)) and \
 isinstance(node.right, (Num, UnaryOp, BinOp)) and \
 isinstance(node.left, (Num, UnaryOp, BinOp)):
left = _convert(node.left)
right = _convert(node.right)
if isinstance(node.op, Add):
return left + right
else:
return left - right
raise ValueError('malformed node or string: ' + repr(node))
return _convert(node_or_string)



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


Re: [Off-topic] Requests author discusses MentalHealthError exception

2016-03-03 Thread Gregory Ewing

alister wrote:

On Thu, 03 Mar 2016 11:03:55 -0700, Ian Kelly wrote:


On Thu, Mar 3, 2016 at 10:21 AM, alister 
wrote:

Antimatter has positive mass.


Are you sure?
 mix 1 atom of hydrogen + 1 of anti hydrogen & you end up with 0 mass


That's not because anti-hydrogen has negative mass, though.
It's just because photons happen to have zero mass. (You're
assuming here that photons are the only products, which is
not the only possible result, but it's one of the possibilities.)

Mass on its own is not a conserved quantity. The thing that's
conserved is total energy. Mass is just the energy something
has when it's standing still, so if the anti-hydrogen had
negative mass, it would also have negative energy when at
rest, and you would get a total of zero energy out of the
reaction. This is not what happens.

As far as I know, there are no negative masses anywhere in
any of our current theories of physics, nor have we observed
anything that would suggest the need for such a thing.

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


Re: [Off-topic] Requests author discusses MentalHealthError exception

2016-03-03 Thread Marko Rauhamaa
Gregory Ewing :

> Mass on its own is not a conserved quantity. The thing that's
> conserved is total energy.

Similarly, momentum is conserved.

Whether mass is conserved or not depends on the chosen terminology.

> As far as I know, there are no negative masses anywhere in any of our
> current theories of physics, nor have we observed anything that would
> suggest the need for such a thing.

What is not known is how antimatter is affected by gravity: http://news.berkeley.edu/2013/04/30/is-antimatter-anti-gravity/>.


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