Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Steven D'Aprano
On Wednesday 10 August 2016 15:20, Juan Pablo Romero Méndez wrote:

> Ok, so you suggested 1) catching exceptions at the point where you care, 2)
> preemptively check for certain runtime conditions to avoid exceptions 3)
> write as many tests as possible  4) learn to live with runtime errors.
> 
> Is that a fair characterization?

(1) Not quite.

Only catch exceptions you expect and *can deal with*. By deal with, I mean 
either recover from, or convert to a user-friendly error message (perhaps 
displaying it in a dialog for GUI apps).

Your script or application may want to use a top-level exception handler to 
catch any *unexpected* exceptions, log them, perform any extra cleanup needed, 
and display a nice error message before exiting. But that counts as gold-
plating: for many applications or scripts, aimed at technical or semi-technical 
users, its acceptable to just let the traceback print and the interpreter exit.

(2) Sometimes. Its a judgement call whether it is better to "Look Before You 
Leap" or "Easier To Ask For Forgiveness Rather Than Permission". It depends on 
what you are doing, how often you expect an exceptional case, the performance 
implications, whether there is a risk of "Time Of Check To Time Of Use" bug, 
etc. In other words, that's usually a technical question, not a matter of 
style.

Google for LBYL versus EAFP for more discussion.

Occasionally it comes down to both being equally good, in which case you get to 
pick whichever you like.

(3) Yes, this is correct.

Basically, with a dynamically-typed language like Python, you have to write all 
the tests you would write with a statically-typed language, plus some more. 
There's no question that the cost of the freedom dynamic typing gives you is 
that you have to write extra tests. But not as many as you might think.

The major differences in mindset between dynamic/static typing are:

- Where the compiler would detect a type error at compile time, you get the 
same result with a small unit test that checks for TypeError or AttributeError 
instead.

(Although see also the MyPy project, which brings an optional static type-
checker to Python.)

- As much as possible, avoid thinking "I need a list", and instead think "I 
need anything which offers the list interface". (Duck-typing.)

That *especially* counts for writing library code, application code you can be 
a bit more strict about types because you're only limiting yourself, not other 
users.

- A Python runtime exception is not like a segmentation fault. Its not going to 
overflow a buffer and execute random code. It's a controlled failure, not an 
uncontrolled one.

(4) There's no necessary reason to expect that you'll get more runtime errors 
in a well-written and tested Python application than you get in a well-written 
and tested C application.

Regardless of the language, you have to deal with runtime errors:

- network is down;
- disk is full;
- user lacks permission to edit that file;
- database error;
- account not found;

etc. Python is no different. And regardless of the language, the compiler can't 
check that "add_account" actually adds the account, you need a unit-test to 
check that. So you have to write tests either way.

Basically, just relax -- static type checks aren't useless, but they do very 
little that a runtime exception won't give you. So long as you have a good 
suite of unit tests that exercises the whole program, you'll find your type 
errors when they raise TypeError, then you fix them.

You may be gathering from this that dynamic typing and Test Driven Development 
go hand in hand. That's exactly right. They complement each other very well.



-- 
Steve

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


Re: How do I make a video animation with transparent background?

2016-08-10 Thread Christian Gollwitzer

Am 09.08.16 um 14:44 schrieb Martin Schöön:

Using this example:
http://matplotlib.org/examples/animation/moviewriter.html
and this tutorial:
https://www.youtube.com/watch?v=h0F5X4b7jug
I have covered a lot of ground.

What I have failed to achieve is a graph with a transparent
background. None of the leads I have found searching the Internet
have helped me. (I may have found the needed information without
realising it...)

Any help is appreciated.



Here is a simple idea: Draw the background with a colour, that is not 
used in the graphs, like bright blue or green. Any decent video editing 
software should support colour keying, i.e. masking the green parts out, 
becuase this is used in film production (green screening)


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


Re: ctypes And The WACAH Principle

2016-08-10 Thread Lawrence D’Oliveiro
On Wednesday, August 10, 2016 at 6:35:57 PM UTC+12, Gregory Ewing wrote:

> It's talking about accessing the internals of an object from C or C++ code,
> *not* from Python.

It’s saying that’s “the only way to effectively make use of this information”, 
which it is not.

> If you're using ctypes anyway, it shouldn't be much harder to
> use ctypes to access the object's buffer interface, if it has
> one.

Yeah, I could probably figure that out ... wonder why it’s not a standard part 
of ctypes already?

> It will let you create non-leaky wrappers ...

Why would I want to do that?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cmd prompt does not recognizes python command on Windows 7

2016-08-10 Thread Wolfgang Maier
Try py instead of python. That invokes a thing called the python 
launcher (see 
https://docs.python.org/3/using/windows.html#python-launcher-for-windows 
for more details).


Best,
Wolfgang


On 10.08.2016 06:46, sh.aja...@gmail.com wrote:

Hi Everyone

i have installed python 3.5 , but the python command is not recognized

C:\Users\sharmaaj>python
'python' is not recognized as an internal or external command,
operable program or batch file.

what should i do to run python commands.

thanks everyone for reading my post.

From Ajay



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


Re: Make sure you removed all debugging print statements error

2016-08-10 Thread alister
On Tue, 09 Aug 2016 19:53:56 -0700, Lawrence D’Oliveiro wrote:

> On Tuesday, August 9, 2016 at 4:20:37 AM UTC+12, Chris Angelico wrote:
>> Firstly, the bare "except:" clause should basically never be used;
>> instead, catch a very specific exception and figure out what you want
>> to do here
> 
> Yes.
> 
>> - but secondly, don't force people's names to be subdivided.
> 
> I like the French convention
> .

I may not have a family name at all




-- 
"If once a man indulges himself in murder, very soon he comes to think
little of robbing; and from robbing he next comes to drinking and
Sabbath-breaking, and from that to incivility and procrastination."
-- Thomas De Quincey (1785 - 1859)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cropping a random part of an image

2016-08-10 Thread drewes . mil
Hi Peter, Hi Robin, 

thanks for your help, and especially for the code ;)

@Peter: thanks for the links, I know, it is always better to write the code 
than to copy and paste!

@Robin: the question, iḿ trying to answer is, if good pictures, in this case 
portraits, also have good "Parts", so if the beauty of a picture can be found 
in its crops, too.
In the and it is for a deep CNN and I hope it will work!

Thanks again!

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


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread BartC

On 10/08/2016 02:47, Steven D'Aprano wrote:

On Wed, 10 Aug 2016 11:02 am, Chris Angelico wrote:


On Wed, Aug 10, 2016 at 10:58 AM, Juan Pablo Romero Méndez
 wrote:



This is interesting. You are Ok having runtime errors?


Absolutely. In real terms, there's no difference between "compile-time
error" and "run-time error that you trigger the moment you run your
program".


That's an oversimplification.

How about the difference between getting a compile-time error immediately
you try to compile your program, and a run-time error three quarters of the
way through processing a billion records, leaving your data in a corrupted
state?


And when it is a customer (perhaps in a different country) who is in the 
middle of running your code.



I love the fact that the computer on the Apollo lunar landers was expected
to have bugs, and was designed to automatically reboot and continue the
calculation that was interrupted. By memory, it rebooted something like 30
or 40 times during the first moon landing.


Wouldn't the same error just recur each time? Or was this a random 
hardware error rather than logic?


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


Re: ctypes And The WACAH Principle

2016-08-10 Thread eryk sun
On Wed, Aug 10, 2016 at 1:45 AM, Lawrence D’Oliveiro
 wrote:
>
> Ah, I wish... I had a look at the documentation for the buffer interface and 
> memory views,
> and found no way to make use of them from pure Python. Basically, the 
> paragraph I quoted
> above is wrong in just about every important detail.

ctypes classes have from_buffer and from_buffer_copy methods that use
the buffer protocol. For example, for a read-only buffer you can use
from_buffer_copy:

>>> b = bytes(b'1234')
>>> a = (ctypes.c_char * 3).from_buffer_copy(b, 1)
>>> a[:]
b'234'

from_buffer requires a writable buffer, a bit more explanation, and a
warning for Python 2. Switch to a bytearray to allow using
from_buffer:

>>> b = bytearray(b'1234')
>>> a = (ctypes.c_char * 3).from_buffer(b, 1)
>>> a[:]
b'234'

The memory is shared:

>>> b[:] = b[::-1]
>>> a[:]
b'321'

from_buffer in Python 3 uses a memoryview to reference the source
object and exported buffer, which it stores in the _objects dict of
the ctypes data instance:

>>> type(a._objects[''])

>>> a._objects[''].obj
bytearray(b'4321')

Because of the existing export, the bytearray cannot be resized (up or down):

>>> del b[0]
Traceback (most recent call last):
  File "", line 1, in 
BufferError: Existing exports of data: object cannot be re-sized

On the other hand, from_buffer in Python 2 naively uses
PyObject_AsWriteBuffer and only keeps a reference to the source
object. Thus there's no guarantee that the memory will remain valid.
Let's demonstrate this with a segfault:

>>> b = bytearray(2**20)
>>> a = (ctypes.c_char * 2**20).from_buffer(b)
>>> b[-1] = 'z'
>>> a[-1]
'z'
>>> del b[:]
>>> a[-1]
Segmentation fault (core dumped)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Make sure you removed all debugging print statements error

2016-08-10 Thread alister
On Wed, 10 Aug 2016 09:23:40 +, alister wrote:

> On Tue, 09 Aug 2016 19:53:56 -0700, Lawrence D’Oliveiro wrote:
> 
>> On Tuesday, August 9, 2016 at 4:20:37 AM UTC+12, Chris Angelico wrote:
>>> Firstly, the bare "except:" clause should basically never be used;
>>> instead, catch a very specific exception and figure out what you want
>>> to do here
>> 
>> Yes.
>> 
>>> - but secondly, don't force people's names to be subdivided.
>> 
>> I like the French convention
>> .
> 
> I may not have a family name at all

Damn this would have been an ideal place to reference Fawlty Towers (not 
monty python but close).


Basil: Your Name please could I have your name
Lord Melbury: Melbury
Basil: Could you fill it in please
Basil: There There There
Basil: BOTH Names Please
Lord Melbury: I Beg Your pardon>
Basil: Would you put BOTH Your names please.
Lord Melbury: er I only use one.
Basil: You Don't have a first name?
Lord Melbury: No I am Lord Melbury, so i simply sign Melbury.

(simultanious Telephone conversation deleted for clarity)

Note with English titles such as Lordship, they may or May not have an 
relationship to the recipients actual family name, often they refer to 
the location of the lords residency.




-- 
IBM Advanced Systems Group -- a bunch of mindless jerks, who'll be first
against the wall when the revolution comes...
-- with regrets to D. Adams
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cmd prompt does not recognizes python command on Windows 7

2016-08-10 Thread eryk sun
On Wed, Aug 10, 2016 at 4:46 AM,   wrote:
>
> i have installed python 3.5 , but the python command is not recognized
>
> C:\Users\sharmaaj>python
> 'python' is not recognized as an internal or external command,
> operable program or batch file.
>
> what should i do to run python commands.

Modify your setup. Open "Programs and Features" from the control
panel. Right-click the entry for Python 3.5 (not "Python Launcher")
and select Uninstall/Change. In the dialog, click on "Modify". Click
"Next" and select "Add Python to environment variables". Click
"Install". When it's done, open a new command prompt and enter
"python".
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cropping a random part of an image

2016-08-10 Thread drewes . mil
Hi Robin,

I tried to understand and run your code, and I get the Error:

"File "Rand_Crop.py", line 15, in 
with Image.open(os.path.join(INPATH, file)) as im:
  File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 528, in __getattr__
raise AttributeError(name)
AttributeError: __exit__"

I looked up the Image.py and don't know, why the Name could not be read out.

If i print the names of the images out, before the first "for" everything is 
fine, but it dosen't work after line 15.

I try to figure this out, but if someone has a hint, I would be happy!


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


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Steven D'Aprano
On Wed, 10 Aug 2016 07:48 pm, BartC wrote:

> On 10/08/2016 02:47, Steven D'Aprano wrote:

>> How about the difference between getting a compile-time error immediately
>> you try to compile your program, and a run-time error three quarters of
>> the way through processing a billion records, leaving your data in a
>> corrupted state?
> 
> And when it is a customer (perhaps in a different country) who is in the
> middle of running your code.

Depends on the customer, the application, and most of all, what the bug is.

In order of priority:

- bugs which threaten people's lives;
- security bugs;
- bugs that corrupt or destroy saved data or files;
- bugs that lose unsaved data;
- bugs that cause the software to do the wrong thing;
- bugs that prevent you from doing the right thing;
- presentation bugs;
- minor UI bugs which don't impact usability.


>> I love the fact that the computer on the Apollo lunar landers was
>> expected to have bugs, and was designed to automatically reboot and
>> continue the calculation that was interrupted. By memory, it rebooted
>> something like 30 or 40 times during the first moon landing.
> 
> Wouldn't the same error just recur each time? Or was this a random
> hardware error rather than logic?

http://www.abc.net.au/science/moon/computer.htm

http://www.klabs.org/history/apollo_11_alarms/eyles_2004/eyles_2004.htm



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Make sure you removed all debugging print statements error

2016-08-10 Thread Chris Angelico
On Wed, Aug 10, 2016 at 8:21 PM, alister  wrote:
 - but secondly, don't force people's names to be subdivided.
>>>
>>> I like the French convention
>>> .
>>
>> I may not have a family name at all
>
> Damn this would have been an ideal place to reference Fawlty Towers (not
> monty python but close).
>
>
> Basil: Your Name please could I have your name
> Lord Melbury: Melbury
> Basil: Could you fill it in please
> Basil: There There There
> Basil: BOTH Names Please
> Lord Melbury: I Beg Your pardon>
> Basil: Would you put BOTH Your names please.
> Lord Melbury: er I only use one.
> Basil: You Don't have a first name?
> Lord Melbury: No I am Lord Melbury, so i simply sign Melbury.
>
> (simultanious Telephone conversation deleted for clarity)
>
> Note with English titles such as Lordship, they may or May not have an
> relationship to the recipients actual family name, often they refer to
> the location of the lords residency.

Specific example: Lord Kelvin, after whom the SI unit of temperature
is named. He's most commonly known by something that isn't his given
name OR family name ("William" and "Thomson" respectively), but by the
name of a river.

https://en.wikipedia.org/wiki/William_Thomson,_1st_Baron_Kelvin
https://en.wikipedia.org/wiki/River_Kelvin

I suppose you could put down Kelvin as a last name and either Baron or
Lord as a first name, but that'd just be cheating...

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


Re: cropping a random part of an image

2016-08-10 Thread drewes . mil
Ok, did it :)

import random, os, time
from PIL import Image

INPATH = ('/home/sdrewes/Desktop/Portaits/Bilder/Test/')
OUTPATH = ('/home/sdrewes/Desktop/Portaits/Bilder/Gut_Crop/')

dx = dy = 228
tilesPerImage = 100

files = os.listdir(INPATH)
numOfImages = len(files)
print(files)
t = time.time()
for file in files:
im = Image.open(INPATH+file)
for i in range(1, tilesPerImage+1):
newname = file.replace('.', '_{:03d}.'.format(i))
w, h = im.size
x = random.randint(0, w-dx-1)
y = random.randint(0, h-dy-1)
print("Cropping {}: {},{} -> {},{}".format(file, x,y, x+dx, 
y+dy))
im.crop((x,y, x+dx, y+dy))\
.save(os.path.join(OUTPATH, newname))

t = time.time()-t
print("Done {} images in {:.2f}s".format(numOfImages, t))
print("({:.1f} images per second)".format(numOfImages/t))
print("({:.1f} tiles per second)".format(tilesPerImage*numOfImages/t)) 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cropping a random part of an image

2016-08-10 Thread drewes . mil
Ok, now it works for me!
Thanks again!

import random, os, time
from PIL import Image

INPATH = ('/home/.../Start/')
OUTPATH = ('/home/.../Ziel/')

dx = dy = 228
tilesPerImage = 25

files = os.listdir(INPATH)
numOfImages = len(files)
print(files)
t = time.time()
for file in files:
im = Image.open(INPATH+file)
for i in range(1, tilesPerImage+1):
newname = file.replace('.', '_{:03d}.'.format(i))
w, h = im.size
x = random.randint(0, w-dx-1)
y = random.randint(0, h-dy-1)
print("Cropping {}: {},{} -> {},{}".format(file, x,y, x+dx, 
y+dy))
im.crop((x,y, x+dx, y+dy))\
.save(os.path.join(OUTPATH, newname))

t = time.time()-t
print("Done {} images in {:.2f}s".format(numOfImages, t))
print("({:.1f} images per second)".format(numOfImages/t))
print("({:.1f} tiles per second)".format(tilesPerImage*numOfImages/t)) 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cmd prompt does not recognizes python command on Windows 7

2016-08-10 Thread BartC

On 10/08/2016 11:34, eryk sun wrote:

On Wed, Aug 10, 2016 at 4:46 AM,   wrote:


i have installed python 3.5 , but the python command is not recognized

C:\Users\sharmaaj>python
'python' is not recognized as an internal or external command,
operable program or batch file.

what should i do to run python commands.


Modify your setup. Open "Programs and Features" from the control
panel. Right-click the entry for Python 3.5 (not "Python Launcher")
and select Uninstall/Change. In the dialog, click on "Modify". Click
"Next" and select "Add Python to environment variables". Click
"Install". When it's done, open a new command prompt and enter
"python".


That's an unusual way of doing it. And rather precarious as you're one 
wrong click away from deleting Python altogether!


It also, after doing the above, spent rather a long time copying files 
about when the operation shouldn't need anything of the sort.


However, I tried it and it didn't seem to work (opening a new prompt and 
type 'python'). Maybe it needs a machine restart, but I can't be 
bothered to try that on the off-chance it might work.


The method I use is this:

* Start Control Panel again

* Click System (assuming you have the 'classic' or 'icon' view otherwise 
you have to hunt for it)


* Click Advanced System Settings

* Click Environment Variables

* Highlight the "Path" variable in the top panel (User variables)

* Click Edit

* Being careful not to erase the whole lot (I hate when it highlights 
the whole thing, as the slightest false move and everything disappears), 
navigate to the front of the current settings, and type in (depending on 
where Python is installed):


   C:\Python\;

(Or navigate to the end and type ;C:\Python\, assuming there are some 
settings already.)


* Keep clicking OKs until it's done

* Open the command prompt again and type 'python' to test.

NOTE: I've assume Python is in C:\Python, but it might more typically be 
C:\Python34 etc. This way, you can choose which version to invoke if 
there are several.


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


Re: cmd prompt does not recognizes python command on Windows 7

2016-08-10 Thread Random832
On Wed, Aug 10, 2016, at 06:34, eryk sun wrote:
> On Wed, Aug 10, 2016 at 4:46 AM,   wrote:
> >
> > i have installed python 3.5 , but the python command is not recognized
> >
> > C:\Users\sharmaaj>python
> > 'python' is not recognized as an internal or external command,
> > operable program or batch file.
> >
> > what should i do to run python commands.
> 
> Modify your setup. Open "Programs and Features" from the control
> panel. Right-click the entry for Python 3.5 (not "Python Launcher")
> and select Uninstall/Change. In the dialog, click on "Modify". Click
> "Next" and select "Add Python to environment variables". Click
> "Install". When it's done, open a new command prompt and enter
> "python".

Or he could just use "py", which is the python launcher you just
mentioned, and works fine. If he has other versions of python installed
he may need "py -3".
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error running the exe file in Windows "Failed to execute script pyi_rth_pkgres"

2016-08-10 Thread Ernest Bonat, Ph.D.
Hi,

I have fixed this problem by following the steps:

1. Uninstall the PyInstaller downloaded from http://www.pyinstaller.org/
pip install pyinstaller

2. Install the PyInstaller from the GitHub developer repository
pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip

Still not clear to me why the main site http://www.pyinstaller.org/ does
not have the latest release?

Thanks

Ernest Bonat, Ph.D.
Senior Software Engineer
Senior Data Scientist

On Sun, Aug 7, 2016 at 8:03 AM, Ernest Bonat, Ph.D. 
wrote:

>  Hi,
>
>  I have created a simple Python program including the following packages:
>
> import numpy as np
>  import matplotlib.pyplot as plt
>  import pandas as pd
>
>  if __name__ == '__main__':
> print("Hi!")
>
>  I have created the installation package using PyInstaller with the
> following command line:
>
>  pyinstaller --onedir --name=appname --windowed "C:\appname.py"
>
>  The required folders (build and dist) and the spec file are created
> properly with no errors. When I run the appname.exe file I got the error
> "Failed to execute script pyi_rth_pkgres".
>
>  I'm using the following now:
>
>- Anaconda3 4.1.1.64-bit
>- Python 3.5.2
>- PyInstaller 3.2
>- Windows 10 64-bit
>
>  Any help is very appreciated. Feel free to contact me at any time you
> need.
>
>  Thank you,
>
>  Ernest Bonat, Ph.D.
>  Senior Software Engineer
>  Senior Data Scientist
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Individuals and groups (was Specify the database for a Django)

2016-08-10 Thread Ben Finney
Rustom Mody  writes:

> On Wednesday, August 10, 2016 at 6:39:31 AM UTC+5:30, Ben Finney wrote:
> > -- 
> >  \   “I love and treasure individuals as I meet them, I loathe and |
> >   `\ despise the groups they identify with and belong to.” —George |
> > _o__) Carlin, 2007 |
>
> Nice quote — thanks!

George Carlin's stand-up comedy act grew progressively nastier and more
cynical in his later years. It can give the impression that he was a
thoroughgoing misanthrope after the 1990s.

Which can make it pleasantly surprising to view some of the contemporary
interviews he gave, such as the one from which the above quote is taken.
He was never shy about stating what was wrong in the world, but given
the opportunity to speak off stage was also a surprisingly optimistic
and compassionate man.

-- 
 \   “… whoever claims any right that he is unwilling to accord to |
  `\ his fellow-men is dishonest and infamous.” —Robert G. |
_o__)   Ingersoll, _The Liberty of Man, Woman and Child_, 1877 |
Ben Finney

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


Re: Python slang

2016-08-10 Thread Anders J. Munch

Lawrence D’Oliveiro:
>> [...] as much like C++ as
>> possible.
>
> Nevertheless, Python copied the C misfeature [...]

You segued a little too easily from C++ to C.  When talking language
evolution and inspirations, they are entirely different things.

- Anders

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


Re: Python slang

2016-08-10 Thread Random832
On Wed, Aug 10, 2016, at 07:59, Dennis Lee Bieber wrote:
>   The use of = also has a long history... FORTRAN (where the comparison
> was .EQ.), BASIC (granted, K&K required assignment to start with the
> keyword LET, so the use of = was mainly a delimiter between target and
> expression being assigned).

Visual Basic actually uses = for both assignment and comparison
*without* the Let keyword, it gets by on the fact that assignment is not
an expression.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cmd prompt does not recognizes python command on Windows 7

2016-08-10 Thread BartC

On 10/08/2016 15:03, Random832 wrote:

On Wed, Aug 10, 2016, at 06:34, eryk sun wrote:

On Wed, Aug 10, 2016 at 4:46 AM,   wrote:


i have installed python 3.5 , but the python command is not recognized

C:\Users\sharmaaj>python
'python' is not recognized as an internal or external command,
operable program or batch file.

what should i do to run python commands.


Modify your setup. Open "Programs and Features" from the control
panel. Right-click the entry for Python 3.5 (not "Python Launcher")
and select Uninstall/Change. In the dialog, click on "Modify". Click
"Next" and select "Add Python to environment variables". Click
"Install". When it's done, open a new command prompt and enter
"python".


Or he could just use "py", which is the python launcher you just
mentioned, and works fine. If he has other versions of python installed
he may need "py -3".


That works on my machine and I wondered why.

It seems to do it by simply putting a special executable (py.exe) into 
C:\windows, which is the first (or second) place it will look when 
someone types the name of a program.


But a bit of a cheat I think, and probably not good practice (suppose 
all your hundreds of apps put dedicated launchers into C:\windows which 
is supposed to be for OS stuff).


(And if you're going to do that, you can also just put a one-line script 
in there to launch any program.)


--
Bartc

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


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Juan Pablo Romero Méndez
2016-08-09 23:47 GMT-07:00 Steven D'Aprano <
steve+comp.lang.pyt...@pearwood.info>:

> On Wednesday 10 August 2016 15:20, Juan Pablo Romero Méndez wrote:
>
> > Ok, so you suggested 1) catching exceptions at the point where you care,
> 2)
> > preemptively check for certain runtime conditions to avoid exceptions 3)
> > write as many tests as possible  4) learn to live with runtime errors.
> >
> > Is that a fair characterization?
>
> (1) Not quite.
>
> Only catch exceptions you expect and *can deal with*. By deal with, I mean
> either recover from, or convert to a user-friendly error message (perhaps
> displaying it in a dialog for GUI apps).
>
> Your script or application may want to use a top-level exception handler to
> catch any *unexpected* exceptions, log them, perform any extra cleanup
> needed,
> and display a nice error message before exiting. But that counts as gold-
> plating: for many applications or scripts, aimed at technical or
> semi-technical
> users, its acceptable to just let the traceback print and the interpreter
> exit.
>
> (2) Sometimes. Its a judgement call whether it is better to "Look Before
> You
> Leap" or "Easier To Ask For Forgiveness Rather Than Permission". It
> depends on
> what you are doing, how often you expect an exceptional case, the
> performance
> implications, whether there is a risk of "Time Of Check To Time Of Use"
> bug,
> etc. In other words, that's usually a technical question, not a matter of
> style.
>
> Google for LBYL versus EAFP for more discussion.
>
> Occasionally it comes down to both being equally good, in which case you
> get to
> pick whichever you like.
>
> (3) Yes, this is correct.
>
> Basically, with a dynamically-typed language like Python, you have to
> write all
> the tests you would write with a statically-typed language, plus some more.
> There's no question that the cost of the freedom dynamic typing gives you
> is
> that you have to write extra tests. But not as many as you might think.
>


Exactly what kind of freedom do you have in mind?




>
> The major differences in mindset between dynamic/static typing are:
>
> - Where the compiler would detect a type error at compile time, you get the
> same result with a small unit test that checks for TypeError or
> AttributeError
> instead.
>
> (Although see also the MyPy project, which brings an optional static type-
> checker to Python.)
>
> - As much as possible, avoid thinking "I need a list", and instead think "I
> need anything which offers the list interface". (Duck-typing.)
>
> That *especially* counts for writing library code, application code you
> can be
> a bit more strict about types because you're only limiting yourself, not
> other
> users.
>
> - A Python runtime exception is not like a segmentation fault. Its not
> going to
> overflow a buffer and execute random code. It's a controlled failure, not
> an
> uncontrolled one.
>
> (4) There's no necessary reason to expect that you'll get more runtime
> errors
> in a well-written and tested Python application than you get in a
> well-written
> and tested C application.
>
> Regardless of the language, you have to deal with runtime errors:
>
> - network is down;
> - disk is full;
> - user lacks permission to edit that file;
> - database error;
> - account not found;
>
> etc. Python is no different. And regardless of the language, the compiler
> can't
> check that "add_account" actually adds the account, you need a unit-test to
> check that. So you have to write tests either way.
>
> Basically, just relax -- static type checks aren't useless, but they do
> very
> little that a runtime exception won't give you. So long as you have a good
> suite of unit tests that exercises the whole program, you'll find your type
> errors when they raise TypeError, then you fix them.
>
> You may be gathering from this that dynamic typing and Test Driven
> Development
> go hand in hand. That's exactly right. They complement each other very
> well.
>
>
>
> --
> Steve
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Michael Selik
On Tue, Aug 9, 2016 at 9:31 PM Steven D'Aprano 
wrote:

>
> http://steve-yegge.blogspot.com.au/2008/05/dynamic-languages-strike-back.html


Great link. I enjoyed the video, too.
https://www.youtube.com/watch?v=tz-Bb-D6teE

Buried deep in the QA section, there's a comment from the audience (I'll
paraphrase), that compiler type-checking is wonderful. Yegge replies that, "I
realized early in my career that I would actually rather have a runtime
error than a compile error."
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python slang

2016-08-10 Thread Rustom Mody
On Wednesday, August 10, 2016 at 9:31:40 PM UTC+5:30, Anders J. Munch wrote:
> Lawrence D’Oliveiro:
>  >> [...] as much like C++ as
>  >> possible.
>  >
>  > Nevertheless, Python copied the C misfeature [...]
> 
> You segued a little too easily from C++ to C.  When talking language
> evolution and inspirations, they are entirely different things.

Talking of too-easy-segueing:
Python’s inspiration and origin is ABC
Whose assignment looked like
PUT expr INTO var

This has the salutary effect
- Of being l-to-r (the only other such case I know is gas mov)
- And of course of not making a misleading pun between equality and assignment

Of course from a software engineering pov, writing say a 1000 line program 
with > 500 assignments written with PUT…INTO is of course ridiculous

But from a pedagogy pov here is a recent post (set) by a beginner that shows
how much this pun costs in beginner confusion:

https://mail.python.org/pipermail/python-list/2016-June/710595.html
https://mail.python.org/pipermail/python-list/2016-June/710668.html

And for completeness (since there seems to be the notion that C and like
are the only natural ones around) here are some extant alternatives:

Pascal (and Algol family generally) var := exp
Early Basic LET var = exp
ABC  PUT exp INTO var
Lisp(scheme)  (setq var exp) (set! var exp)
Assembly  mov var, exp  (gas) mov exp, var
APL var ← exp
-- 
https://mail.python.org/mailman/listinfo/python-list


Why can't I define a variable like "miles_driven" with an underscore in Python 3.4.3 ?

2016-08-10 Thread Cai Gengyang
I managed to get this piece of code to work :

>>> print("This program calculates mpg.")
This program calculates mpg.
>>> milesdriven = input("Enter miles driven:")
Enter miles driven: 50
>>> milesdriven = float(milesdriven)
>>> gallonsused = input("Enter gallons used:")
Enter gallons used: 100
>>> gallonsused = float(gallonsused)
>>> gallonsused = float(gallonsused)
>>> mpg = milesdriven / gallonsused
>>> print("Miles per gallon:", mpg)
Miles per gallon: 0.5
>>> print("This program calculates mpg.")
This program calculates mpg.
>>> milesdriven = input("Enter miles driven:")
Enter miles driven: 100
>>> milesdriven = float(milesdriven)
>>> gallonsused = input("Enter gallons used:")
Enter gallons used: 500
>>> gallonsused = float(gallonsused)
>>> mpg = milesdriven / gallonsused
>>> print("Miles per gallon:", mpg)
Miles per gallon: 0.2

But, why can't i define a variable like "miles_driven" with an underscore in my 
Python Shell ? When I try to define it , the underscore doesn't appear at all 
and instead I get this result : 
"miles driven" , with no underscore appearing even though I have already typed 
"_" between "miles" and "driven" ?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Juan Pablo Romero Méndez
2016-08-09 23:16 GMT-07:00 Gregory Ewing :

> Juan Pablo Romero Méndez wrote:
>
> This is interesting. You are Ok having runtime errors?
>>
>
> You're going to have runtime errors in any case, whether
> they come from code you've put there yourself to check
> types, or from somewhere deeper down.
>


You are correct. What I was trying to understand is if there is a much more
relaxed attitude towards runtime errors in the Python community (even
embracing it) than others* (and of course, how to handle those errors).

(In certain communities people favor a design where exceptions / runtime
errors only happen at program boundaries)



>
> The only difference is that checks you make yourself
> *might* catch errors slightly sooner, and *might* be able
> to provide better diagnostics.
>
> However, experience has shown that, the vast majority of
> the time, type errors in Python are caught pretty much
> immediately, and the stack trace provides more than
> enough information to pin down the source of the problem.
> So, putting in manual type checks is hardly ever worth
> the effort, and can even be counterproductive, since it
> interferes with duck typing.
>
> Occasionally there will be a situation where a type
> error results in a corrupted data structure that leads
> to problems later. But those cases are relatively rare
> and best dealt with as they come up.
>
> --
> Greg
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Specify the database for a Django ModelForm instance

2016-08-10 Thread Phil Boutros
Ben Finney  wrote:
>
> The person who wrote that question must think very like myself.
>
> That doesn't address the question about ‘ModelForm.clean’ or
> ‘ModelForm.is_valid’, though; how to tell the instance which database to
> use for those?

I must admit to not using multi-DB myself, but I think this set of
answers may point you in the right direction:

http://stackoverflow.com/questions/35214706/django-model-form-is-valid-in-specific-database


Phil
-- 
AH#61  Wolf#14  BS#89  bus#1  CCB#1  SENS  KOTC#4   ph...@philb.ca
http://philb.ca EKIII rides with me:  http://eddiekieger.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why can't I define a variable like "miles_driven" with an underscore in Python 3.4.3 ?

2016-08-10 Thread Michael Selik
On Wed, Aug 10, 2016 at 1:41 PM Cai Gengyang  wrote:

> I managed to get this piece of code to work :
>
> >>> print("This program calculates mpg.")
> This program calculates mpg.
> >>> milesdriven = input("Enter miles driven:")
> Enter miles driven: 50
> >>> milesdriven = float(milesdriven)
> >>> gallonsused = input("Enter gallons used:")
> Enter gallons used: 100
> >>> gallonsused = float(gallonsused)
> >>> gallonsused = float(gallonsused)
> >>> mpg = milesdriven / gallonsused
> >>> print("Miles per gallon:", mpg)
> Miles per gallon: 0.5
> >>> print("This program calculates mpg.")
> This program calculates mpg.
> >>> milesdriven = input("Enter miles driven:")
> Enter miles driven: 100
> >>> milesdriven = float(milesdriven)
> >>> gallonsused = input("Enter gallons used:")
> Enter gallons used: 500
> >>> gallonsused = float(gallonsused)
> >>> mpg = milesdriven / gallonsused
> >>> print("Miles per gallon:", mpg)
> Miles per gallon: 0.2
>
> But, why can't i define a variable like "miles_driven" with an underscore
> in my Python Shell ? When I try to define it , the underscore doesn't
> appear at all and instead I get this result :
> "miles driven" , with no underscore appearing even though I have already
> typed "_" between "miles" and "driven" ?
>

You'll have to give more information about the system you're using.
Underscores are allowed in Python variable names.

The first thought I have is that something outside of Python is changing
your underscore to a space. Perhaps your OS spelling autocorrect is overly
aggressive?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why can't I define a variable like "miles_driven" with an underscore in Python 3.4.3 ?

2016-08-10 Thread Zachary Ware
On Wed, Aug 10, 2016 at 12:39 PM, Cai Gengyang  wrote:
> I managed to get this piece of code to work :
>
 print("This program calculates mpg.")
> This program calculates mpg.
 milesdriven = input("Enter miles driven:")
> Enter miles driven: 50
 milesdriven = float(milesdriven)
 gallonsused = input("Enter gallons used:")
> Enter gallons used: 100
 gallonsused = float(gallonsused)
 gallonsused = float(gallonsused)
 mpg = milesdriven / gallonsused
 print("Miles per gallon:", mpg)
> Miles per gallon: 0.5
 print("This program calculates mpg.")
> This program calculates mpg.
 milesdriven = input("Enter miles driven:")
> Enter miles driven: 100
 milesdriven = float(milesdriven)
 gallonsused = input("Enter gallons used:")
> Enter gallons used: 500
 gallonsused = float(gallonsused)
 mpg = milesdriven / gallonsused
 print("Miles per gallon:", mpg)
> Miles per gallon: 0.2
>
> But, why can't i define a variable like "miles_driven" with an underscore in 
> my Python Shell ? When I try to define it , the underscore doesn't appear at 
> all and instead I get this result :
> "miles driven" , with no underscore appearing even though I have already 
> typed "_" between "miles" and "driven" ?

Are you using IDLE on OSX?  Try changing your font or bumping up the
size (or switch to 3.5.2, which has better defaults).

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


Re: cmd prompt does not recognizes python command on Windows 7

2016-08-10 Thread eryk sun
On Wed, Aug 10, 2016 at 1:46 PM, BartC  wrote:
> On 10/08/2016 11:34, eryk sun wrote:
>>
>> On Wed, Aug 10, 2016 at 4:46 AM,   wrote:
>>>
>>> i have installed python 3.5 , but the python command is not recognized
>>>
>>> C:\Users\sharmaaj>python
>>> 'python' is not recognized as an internal or external command,
>>> operable program or batch file.
>>>
>>> what should i do to run python commands.
>>
>> Modify your setup. Open "Programs and Features" from the control
>> panel. Right-click the entry for Python 3.5 (not "Python Launcher")
>> and select Uninstall/Change. In the dialog, click on "Modify". Click
>> "Next" and select "Add Python to environment variables". Click
>> "Install". When it's done, open a new command prompt and enter
>> "python".
>
> That's an unusual way of doing it. And rather precarious as you're one wrong
> click away from deleting Python altogether!
>
> It also, after doing the above, spent rather a long time copying files about
> when the operation shouldn't need anything of the sort.
>
> However, I tried it and it didn't seem to work (opening a new prompt and
> type 'python'). Maybe it needs a machine restart, but I can't be bothered to
> try that on the off-chance it might work.

I did test that it works -- using the Python 3.5 installer, which was
completely rewritten. It adds Python's installation and Scripts
directories to PATH, and it doesn't need a reboot because the
installer broadcasts a WM_SETTINGCHANGE "Environment" message that
causes Explorer to reload its environment from the registry (the old
installer doesn't do this).

As to the procedure being unusual, I thought it would be simpler and
more reliable than editing PATH in the old control-panel environment
variable editor, which can be much worse if the user messes it up.
Windows 10 has an improved editor that splits the value into separate
text boxes and makes it easy to add entries and move them around, but
most people are still using Windows 7.

Also, the default installation path for a per-user installation of
Python 3.5 is in the hidden folder
"%LOCALAPPDATA%\Programs\Python\PythonXY[-32]". This can be difficult
for users to find without inspecting the start-menu shortcuts or
running `py -c "import sys;print(sys.prefix)"` -- assuming they used
the default all-users installation of the py launcher to even be able
to run py.exe.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cross-platform logging.config: how to set cross platform log (handlers) file location?

2016-08-10 Thread bertrand . courts . circuits
Thanks Peter for the insights that args are passed to eval().
Thanks Steven for the suggestion that ´~/' may actually work in windows.

Combining both I'm now successfully using the following in my logging.conf 
configuration file, that works in both *nix and Windows:

[handler_file]
class=handlers.RotatingFileHandler
args=(os.path.expanduser('~/.scribusGenerator.log'),'a','maxBytes=50','backupCount=1')

The tilde without an explicit call to expanduser() was not working in 
Windows... but with it it's working like a charm.

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


Re: cmd prompt does not recognizes python command on Windows 7

2016-08-10 Thread eryk sun
On Wed, Aug 10, 2016 at 2:03 PM, Random832  wrote:
> On Wed, Aug 10, 2016, at 06:34, eryk sun wrote:
>> On Wed, Aug 10, 2016 at 4:46 AM,   wrote:
>> >
>> > i have installed python 3.5 , but the python command is not recognized
>> >
>> > C:\Users\sharmaaj>python
>> > 'python' is not recognized as an internal or external command,
>> > operable program or batch file.
>> >
>> > what should i do to run python commands.
>>
>> Modify your setup. Open "Programs and Features" from the control
>> panel. Right-click the entry for Python 3.5 (not "Python Launcher")
>> and select Uninstall/Change. In the dialog, click on "Modify". Click
>> "Next" and select "Add Python to environment variables". Click
>> "Install". When it's done, open a new command prompt and enter
>> "python".
>
> Or he could just use "py", which is the python launcher you just
> mentioned, and works fine. If he has other versions of python installed
> he may need "py -3".

>From what I can tell in discussions that I've read, the consensus of
core developers is to not encourage novice users to use py.exe
directly. The preferred way to start Python is still via python.exe.
That said, experienced users will probably find using py.exe to be
more convenient. I do.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python slang

2016-08-10 Thread Marko Rauhamaa
Rustom Mody :

> On Wednesday, August 10, 2016 at 9:31:40 PM UTC+5:30, Anders J. Munch wrote:
> Python’s inspiration and origin is ABC
> Whose assignment looked like
> PUT expr INTO var
>
> This has the salutary effect
> - Of being l-to-r (the only other such case I know is gas mov)

Oh, there's of course COBOL:

   ADD X TO Y GIVING RESULT


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


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Juan Pablo Romero Méndez
2016-08-09 18:28 GMT-07:00 Steven D'Aprano :

> On Wed, 10 Aug 2016 04:29 am, Juan Pablo Romero Méndez wrote:
>
> > Hello,
> >
> > In online forums sometimes people complain that they end up having to
> test
> > constantly for None,
>
> Then don't return None to indicate an error. If you write this:
>
> def create_spam(arg):
> if condition:
> return Spam(arg)
> else:
> return None
>
>
> then you are doing it wrong. It sounds like they are writing C in Python,
> where they think that returning None (instead of a null pointer) is the way
> to indicate an error. Instead:
>
> def create_spam(arg):
> if condition:
> return Spam(arg)
> raise MyExceptionError('cannot create spam because reasons')
>
>
> Now you can decide on a error-handling scheme:
>
> - try...except and handle the exception exactly when it occurs;
>
> - wrap your entire application in a single try...except or
>   exception handler, and catch the exception in a single place;
>
> - don't even bother catching the exception in the application at all,
>   treat it as a debugging aid. Any time your tests generate that
>   exception, that's a bug to be fixed.
>
>
> There's no reason why you can't decide on a combination of all three:
>
> - the app catches the exception, logs it as a bug, and exits;
>
> - but individual parts of your code base treat this exception as an
>   EXPECTED error, wrapping that individual call in a try...except:
>
>
> try:
> myspam = create_spam(99)
> except MyExceptionError:
> # Only occurs when the server has hung
> reboot_server()
> myspam = create_spam(99)
>
>
> Only catch exceptions you expect, and can recover from.
>
> If your *data* could be None, well that's no different from any other
> sentinel or missing value. You have to deal with it somewhere. One way is
> to filter it early:
>
> # filter out None
> data = [obj for obj in data if data is not None]
>
> # or if you prefer
> data = filter(lambda obj: obj is not None, data)
>
>
> and now you can be sure that data doesn't contain None.
>
>
> [...]
> > Do you guys have any resources you like that addresses the following
> > issue: What is the best way to use the dynamic features of Python to
> avoid
> > having to write a poor's man type system?
>
> This is an old, old, old debate that applies to Lisp, Perl, Javascript,
> Lua,
> Ruby and others, not just Python. Google for "dynamic typing versus static
> typing" for more. Also google for "duck typing".
>
>

Yeah, I've been on both sides of the debate over my programming years ;)

What I find frustrating to be honest about this debate (and the reason it
will never die probably) is that your perspective on it really depends
mostly on anecdotal evidence, your own knowledge, experience, etc.

I've been trying to find (without success so far) an example of a situation
where the dynamic features of a language like Python provides a clear
advantage over languages with more than one type.



> One useful perspective is that languages are converging on a mix of both:
> statically typed languages are growing dynamic features, and dynamically
> typed languages are growing static checkers.
>
>
> http://steve-yegge.blogspot.com.au/2008/05/dynamic-
> languages-strike-back.html
>
>
> And some background information on the debate:
>
> https://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/
>
>
>
>
> --
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Michael Selik
On Wed, Aug 10, 2016, 4:34 PM Juan Pablo Romero Méndez <
jpablo.rom...@gmail.com> wrote:

>
> I've been trying to find (without success so far) an example of a situation
> where the dynamic features of a language like Python provides a clear
> advantage over languages with more than one type.
>

Only one type? There are a great variety of types for Python objects. Even
Python classes have types. Further, you can modify types at runtime! It's a
typing bonanza!

I suppose you could argue we're conflating types and classes. If so, it
sounds like you started this conversation with an ulterior motive and not
to learn about best practices for runtime type checking in Python.

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


Re: Python slang

2016-08-10 Thread Chris Angelico
On Thu, Aug 11, 2016 at 5:18 AM, Marko Rauhamaa  wrote:
> Rustom Mody :
>
>> On Wednesday, August 10, 2016 at 9:31:40 PM UTC+5:30, Anders J. Munch wrote:
>> Python’s inspiration and origin is ABC
>> Whose assignment looked like
>> PUT expr INTO var
>>
>> This has the salutary effect
>> - Of being l-to-r (the only other such case I know is gas mov)
>
> Oh, there's of course COBOL:
>
>ADD X TO Y GIVING RESULT

And Python:

with open(fn) as f:

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


Re: cross-platform logging.config: how to set cross platform log (handlers) file location?

2016-08-10 Thread Berteh BE
Thanks Peter for the insights that args are passed to eval().
Thanks Steven for the suggestion that ´~/' may actually work in windows.

Combining both I'm now successfully using the following in my logging.conf 
configuration file, that works in both *nix and Windows:

[handler_file]
class=handlers.RotatingFileHandler
args=(os.path.expanduser('~/.scribusGenerator.log'),'a','maxBytes=50','backupCount=1')

The tilde without an explicit call to expanduser() was not working in 
Windows... but with it it's working like a charm.

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


Re: Why can't I define a variable like "miles_driven" with an underscore in Python 3.4.3 ?

2016-08-10 Thread Cai Gengyang
Yea, using IDLE on OSX, Python 3.4.3. 

Yeah it works now ... I had to increase the font size and the indentation 
width. When that happened , it could define miles_driven 




On Thursday, August 11, 2016 at 2:09:11 AM UTC+8, Zachary Ware wrote:
> On Wed, Aug 10, 2016 at 12:39 PM, Cai Gengyang  wrote:
> > I managed to get this piece of code to work :
> >
>  print("This program calculates mpg.")
> > This program calculates mpg.
>  milesdriven = input("Enter miles driven:")
> > Enter miles driven: 50
>  milesdriven = float(milesdriven)
>  gallonsused = input("Enter gallons used:")
> > Enter gallons used: 100
>  gallonsused = float(gallonsused)
>  gallonsused = float(gallonsused)
>  mpg = milesdriven / gallonsused
>  print("Miles per gallon:", mpg)
> > Miles per gallon: 0.5
>  print("This program calculates mpg.")
> > This program calculates mpg.
>  milesdriven = input("Enter miles driven:")
> > Enter miles driven: 100
>  milesdriven = float(milesdriven)
>  gallonsused = input("Enter gallons used:")
> > Enter gallons used: 500
>  gallonsused = float(gallonsused)
>  mpg = milesdriven / gallonsused
>  print("Miles per gallon:", mpg)
> > Miles per gallon: 0.2
> >
> > But, why can't i define a variable like "miles_driven" with an underscore 
> > in my Python Shell ? When I try to define it , the underscore doesn't 
> > appear at all and instead I get this result :
> > "miles driven" , with no underscore appearing even though I have already 
> > typed "_" between "miles" and "driven" ?
> 
> Are you using IDLE on OSX?  Try changing your font or bumping up the
> size (or switch to 3.5.2, which has better defaults).
> 
> -- 
> Zach
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why can't I define a variable like "miles_driven" with an underscore in Python 3.4.3 ?

2016-08-10 Thread Ethan Furman

On 08/10/2016 01:57 PM, Cai Gengyang wrote:

Yea, using IDLE on OSX, Python 3.4.3.

Yeah it works now ... I had to increase the font size and the indentation 
width. When that happened , it could define miles_driven


I suspect you were defining it before, you just couldn't see the underscore.

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


Re: How do I make a video animation with transparent background?

2016-08-10 Thread Martin Schöön
Den 2016-08-10 skrev Christian Gollwitzer :
> Am 09.08.16 um 14:44 schrieb Martin Schöön:
>> Using this example:
>> http://matplotlib.org/examples/animation/moviewriter.html
>> and this tutorial:
>> https://www.youtube.com/watch?v=h0F5X4b7jug
>> I have covered a lot of ground.
>>
>> What I have failed to achieve is a graph with a transparent
>> background. None of the leads I have found searching the Internet
>> have helped me. (I may have found the needed information without
>> realising it...)
>>
>> Any help is appreciated.
>
>
> Here is a simple idea: Draw the background with a colour, that is not 
> used in the graphs, like bright blue or green. Any decent video editing 
> software should support colour keying, i.e. masking the green parts out, 
> becuase this is used in film production (green screening)
>
>   Christian

Thanks for all the input so far and not only Christian but all of you.

Yes, after posting I did some more research and started to realise
that I may have barked up the wrong tree when assuming my use of
Matplotlib needed to be improved to get the intended result.

Next on my TODO list is to look into the color composition idea. I
have already looked this tutorial:
https://youtu.be/sZzmksnzrX0

What I would like to end up with is something like this:
https://youtu.be/aHEyRoDmDWk
(graph in lower right corner and 'speedometer' in lower left corner)

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


Re: ctypes And The WACAH Principle

2016-08-10 Thread Lawrence D’Oliveiro
On Wednesday, August 10, 2016 at 10:13:53 PM UTC+12, eryk sun wrote:

> ctypes classes have from_buffer and from_buffer_copy methods that use
> the buffer protocol. For example, for a read-only buffer you can use
> from_buffer_copy:
> 
> >>> b = bytes(b'1234')
> >>> a = (ctypes.c_char * 3).from_buffer_copy(b, 1)
> >>> a[:]
> b'234'

Can I avoid the copying? For example, in Qahirah 
 the following method creates and fills in an 
ImageSurface from a stream of bytes in PNG format:

@classmethod
def create_from_png_bytes(celf, data) :
"creates an ImageSurface from a PNG-format data sequence. This can be" \
" of the bytes or bytearray types, or an array.array with \"B\" type 
code."

offset = 0

def read_data(_, data, length) :
nonlocal offset
if offset + length <= len(data) :
ct.memmove(data, baseadr + offset, length)
offset += length
status = CAIRO.STATUS_SUCCESS
else :
status = CAIRO.STATUS_READ_ERROR
#end if
return \
status
#end read_data

#begin create_from_png_bytes
if isinstance(data, bytes) or isinstance(data, bytearray) :
data = array.array("B", data)
elif not isinstance(data, array.array) or data.typecode != "B" :
raise TypeError("data is not bytes, bytearray or array of bytes")
#end if
baseadr = data.buffer_info()[0]
return \
celf.create_from_png_stream(read_data, None)
#end create_from_png_bytes

But it has to copy the bytes into an array.array object, then decode that. Is 
there a way it could access the bytes memory directly?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python slang

2016-08-10 Thread Lawrence D’Oliveiro
On Thursday, August 11, 2016 at 5:35:03 AM UTC+12, Rustom Mody wrote:

> On Wednesday, August 10, 2016 at 9:31:40 PM UTC+5:30, Anders J. Munch wrote:
> Python’s inspiration and origin is ABC
> Whose assignment looked like
> PUT expr INTO var

HyperTalk did that in 1987. Is that where GvR got the idea from?

In HyperTalk you could also do “put ... before/after ...” to do string 
prepending/appending.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python slang

2016-08-10 Thread Lawrence D’Oliveiro
On Thursday, August 11, 2016 at 4:01:40 AM UTC+12, Anders J. Munch wrote:

> Lawrence D’Oliveiro:
>>
>>> [...] as much like C++ as possible.
>>
>> Nevertheless, Python copied the C misfeature [...]
> 
> You segued a little too easily from C++ to C.

Because C++ copied that same C misfeature.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cmd prompt does not recognizes python command on Windows 7

2016-08-10 Thread Lawrence D’Oliveiro
On Wednesday, August 10, 2016 at 4:47:33 PM UTC+12, sh.a...@gmail.com wrote:

> i have installed python 3.5 , but the python command is not recognized  

Windows does not make it easy to install things, does it...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ctypes And The WACAH Principle

2016-08-10 Thread Gregory Ewing

Lawrence D’Oliveiro wrote:


It will let you create non-leaky wrappers ...


Why would I want to do that?


You mentioned that ctypes wrappers are leaky, which I
assumed was meant as a criticism. But if you *like*
your wrappers to be leaky, by all means go for it. :-)

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


Re: How do I make a video animation with transparent background?

2016-08-10 Thread Lawrence D’Oliveiro
On Thursday, August 11, 2016 at 9:34:33 AM UTC+12, Martin Schöön wrote:
> Next on my TODO list is to look into the color composition idea. I
> have already looked this tutorial:
> https://youtu.be/sZzmksnzrX0

If the images you generate have an alpha channel, Blender can composite them 
directly, without needing colour-keying.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Juan Pablo Romero Méndez
I've made my motives to start this conversation very clear:

"""
Determine if there is a style of writing code in a dynamic language that
minimizes the occurrence of runtime errors AND avoids two things:

1) using runtime checks all over the place (either ad hoc or using tools
like Valideer)
2) attempting to test for things that a type checker would normally do
 (not that this is completely possible anyway)
"""

And I really appreciate people taking their time to contribute!

As to why I asked that, there are several reasons: I have a very concrete
need right now to find pragmatic ways to increase code quality, reduce
number of defects, etc. in a Python code base. But also I want to
understand better the mind set and culture of Python's community.


You make a good point that my last remark is unrelated to that goal, but
please don't take it as an attack of any sort.

Languages are not monolithically good / bad. They have features that can be
individually evaluated in light of different goals (just to name a few: is
it performant enough?, is it easy to learn?, is it easy to write? is it
easy to make it correct? is it expressive? is it easy to refactor? is it
easy to deploy?, does it have good documentation? etc etc etc. ). Also
languages evolve, so features that were considered positive at some point
can become out of fashion over the years.
I'm honestly interested in finding good examples of cases where the dynamic
features of some language are a better solution than static typing. This is
of course more useful in languages that support both paradigms.

  Juan Pablo


2016-08-10 13:50 GMT-07:00 Michael Selik :

>
>
> On Wed, Aug 10, 2016, 4:34 PM Juan Pablo Romero Méndez <
> jpablo.rom...@gmail.com> wrote:
>
>>
>> I've been trying to find (without success so far) an example of a
>> situation
>> where the dynamic features of a language like Python provides a clear
>> advantage over languages with more than one type.
>>
>
> Only one type? There are a great variety of types for Python objects. Even
> Python classes have types. Further, you can modify types at runtime! It's a
> typing bonanza!
>
> I suppose you could argue we're conflating types and classes. If so, it
> sounds like you started this conversation with an ulterior motive and not
> to learn about best practices for runtime type checking in Python.
>
>>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ctypes And The WACAH Principle

2016-08-10 Thread Chris Angelico
On Thu, Aug 11, 2016 at 8:31 AM, Gregory Ewing
 wrote:
> Lawrence D’Oliveiro wrote:
>
>>> It will let you create non-leaky wrappers ...
>>
>>
>> Why would I want to do that?
>
>
> You mentioned that ctypes wrappers are leaky, which I
> assumed was meant as a criticism. But if you *like*
> your wrappers to be leaky, by all means go for it. :-)

Are they leaky in the sense of leaky abstractions, or memory leaks?

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


Re: Make sure you removed all debugging print statements error

2016-08-10 Thread Gregory Ewing

Chris Angelico wrote:

I suppose you could put down Kelvin as a last name and either Baron or
Lord as a first name, but that'd just be cheating...


Or put NULL as the first name and have fun watching
the database break.

http://www.wired.com/2015/11/null/

Or perhaps even profit:

http://www.dailymail.co.uk/travel/travel_news/article-3513652/The-cleverest-time-Man-gets-free-holidays-car-rentals-changing-surname-Null.html

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


Re: Python slang

2016-08-10 Thread Ian Kelly
On Aug 10, 2016 4:36 PM, "Lawrence D’Oliveiro" 
wrote:

On Thursday, August 11, 2016 at 5:35:03 AM UTC+12, Rustom Mody wrote:

> On Wednesday, August 10, 2016 at 9:31:40 PM UTC+5:30, Anders J. Munch
wrote:
> Python’s inspiration and origin is ABC
> Whose assignment looked like
> PUT expr INTO var

HyperTalk did that in 1987. Is that where GvR got the idea from?


Probably not, since the design of ABC dates back to at least 1976. And GvR
didn't design the language anyway.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Make sure you removed all debugging print statements error

2016-08-10 Thread Chris Angelico
On Thu, Aug 11, 2016 at 8:48 AM, Gregory Ewing
 wrote:
> Chris Angelico wrote:
>>
>> I suppose you could put down Kelvin as a last name and either Baron or
>> Lord as a first name, but that'd just be cheating...
>
>
> Or put NULL as the first name and have fun watching
> the database break.
>
> http://www.wired.com/2015/11/null/
>
> Or perhaps even profit:
>
> http://www.dailymail.co.uk/travel/travel_news/article-3513652/The-cleverest-time-Man-gets-free-holidays-car-rentals-changing-surname-Null.html

NULL should be completely different from the string 'Null' (in any
mixture of case). Failing that check means a failure in the front end,
or in how things are checked for.

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


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Gregory Ewing

Dennis Lee Bieber wrote:

Stray gamma rays flipping
bits in memory (though a bit harder to do, considering the firmware was in
some hard-wired mess of core,


I think the RAM was core too, so pretty much immune to
gamma rays as well (at least at any intensity that wouldn't
instantly kill the crew), and non-volatile.

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


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Michael Selik
On Wed, Aug 10, 2016, 6:44 PM Juan Pablo Romero Méndez <
jpablo.rom...@gmail.com> wrote:

> As to why I asked that, there are several reasons: I have a very concrete
> need right now to find pragmatic ways to increase code quality, reduce
> number of defects, etc. in a Python code base. But also I want to
> understand better the mind set and culture of Python's community.
>

That's a much better question. Arguing about type checking is somewhat of a
red herring. It's best not to fight the language, but to use its strengths.

I often find that trying to add type and value checks just makes my code
harder to read and harder to maintain, even if they solve a particular bug
in the moment. A better solution is to refactor in such a way that those
checks are unnecessary.

Are you familiar with context managers and the iterator protocol? If not, I
suggest you start by looking for setup/cleanup code or try/except/finally
blocks that you can refactor into a context manager and by looking for
awkward loops with explicit external state that you can refactor into an
iterator.

If you really must add type- and value-checks, I suggest trying to place
them in a property so that they don't clutter the more interesting code.

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


Re: Python slang

2016-08-10 Thread Michael Torrie
On 08/10/2016 10:19 AM, Random832 wrote:
> On Wed, Aug 10, 2016, at 07:59, Dennis Lee Bieber wrote:
>>  The use of = also has a long history... FORTRAN (where the comparison
>> was .EQ.), BASIC (granted, K&K required assignment to start with the
>> keyword LET, so the use of = was mainly a delimiter between target and
>> expression being assigned).
> 
> Visual Basic actually uses = for both assignment and comparison
> *without* the Let keyword, it gets by on the fact that assignment is not
> an expression.

But the grammar must still be a bit complex as sometimes the LHS of the
= is an expression, as well as the RHS.  But I guess context makes it
all work.  Still I've always thought the grammars for modern BASIC with
all its baggage must make for a complex grammar.

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


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Michael Torrie
On 08/10/2016 11:29 AM, Michael Selik wrote:
> On Tue, Aug 9, 2016 at 9:31 PM Steven D'Aprano 
> wrote:
> 
>>
>> http://steve-yegge.blogspot.com.au/2008/05/dynamic-languages-strike-back.html
> 
> 
> Great link. I enjoyed the video, too.
> https://www.youtube.com/watch?v=tz-Bb-D6teE
> 
> Buried deep in the QA section, there's a comment from the audience (I'll
> paraphrase), that compiler type-checking is wonderful. Yegge replies that, "I
> realized early in my career that I would actually rather have a runtime
> error than a compile error."

True but sometimes a compile error would save a lot of time and
frustration later on when the runtime error occurs. For example in
Python if I am setting an attribute on an object and mispell the
attribute, there is no compile error, but often I won't get a runtime
error from that, but I will get buggy behavior that sometimes but not
always occurs.  Very frustrating. Pylint catches that of course, so it's
a weaker argument for compilers than some others perhaps.

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


Re: Python slang

2016-08-10 Thread Michael Torrie
On 08/10/2016 05:57 PM, Michael Torrie wrote:
> On 08/10/2016 10:19 AM, Random832 wrote:
>> On Wed, Aug 10, 2016, at 07:59, Dennis Lee Bieber wrote:
>>> The use of = also has a long history... FORTRAN (where the comparison
>>> was .EQ.), BASIC (granted, K&K required assignment to start with the
>>> keyword LET, so the use of = was mainly a delimiter between target and
>>> expression being assigned).
>>
>> Visual Basic actually uses = for both assignment and comparison
>> *without* the Let keyword, it gets by on the fact that assignment is not
>> an expression.
> 
> But the grammar must still be a bit complex as sometimes the LHS of the
> = is an expression, as well as the RHS.  But I guess context makes it
> all work.  Still I've always thought the grammars for modern BASIC with
> all its baggage must make for a complex grammar.

Sigh.  So much for English grammar.  That should be "complex parser."

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


Re: ctypes And The WACAH Principle

2016-08-10 Thread Lawrence D’Oliveiro
On Thursday, August 11, 2016 at 10:31:36 AM UTC+12, Gregory Ewing wrote:
>
> Lawrence D’Oliveiro wrote:
> 
>>>It will let you create non-leaky wrappers ...
>>
>> Why would I want to do that?
> 
> You mentioned that ctypes wrappers are leaky, which I
> assumed was meant as a criticism.

WACAH!

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


Re: Python slang

2016-08-10 Thread Lawrence D’Oliveiro
On Thursday, August 11, 2016 at 7:18:28 AM UTC+12, Marko Rauhamaa wrote:

> Oh, there's of course COBOL:
> 
>ADD X TO Y GIVING RESULT

I think COBOL has the same usage of “=” as BASIC, as assignment or equality 
comparison, depending on context.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why can't I define a variable like "miles_driven" with an underscore in Python 3.4.3 ?

2016-08-10 Thread Steven D'Aprano
On Thu, 11 Aug 2016 06:57 am, Cai Gengyang wrote:

> Yea, using IDLE on OSX, Python 3.4.3.
> 
> Yeah it works now ... I had to increase the font size and the indentation
> width. When that happened , it could define miles_driven

You were always defining "miles_driven", it is just that the display engine
used by IDLE on OS X wasn't capable of displaying the underscore using the
combination of typeface (font), size and line height.

My guess is that you had it set to a very small size and the underscore just
wasn't visible.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: ctypes And The WACAH Principle

2016-08-10 Thread eryk sun
On Wed, Aug 10, 2016 at 10:26 PM, Lawrence D’Oliveiro
 wrote:
> But it has to copy the bytes into an array.array object, then decode that. Is 
> there a way it
> could access the bytes memory directly?

ctypes cast, string_at, wstring_at, memmove, and memset are
implemented as FFI calls. Since ctypes FFI calls convert bytes
arguments to the base address of the buffer, you can cast() bytes to
c_void_p to get the address. For example:

>>> data = b'abc'
>>> addr = ctypes.cast(data, ctypes.c_void_p).value
>>> ctypes.string_at(addr, 3)
'abc'

For bytearray, use the buffer interface to create a ctypes array. The
array is not a copy. For example:

>>> data = bytearray(b'abc')
>>> a = (ctypes.c_char * len(data)).from_buffer(data)
>>> addr = ctypes.addressof(a)
>>> ctypes.string_at(addr, 3)
'abc'
-- 
https://mail.python.org/mailman/listinfo/python-list


Vectorized functions

2016-08-10 Thread Steven D'Aprano
Its sometimes very useful to apply a function to each element of a list in
turn. One common term for that is "vectorized function".

Julia has a convenient syntactic shortcut for this:

http://julia.readthedocs.io/en/latest/manual/functions/#dot-syntax-for-vectorizing-functions

func(arg)

calls func with a single argument, "arg", while:

func.(arg)

expects "arg" to be an array of values, and returns a new array generated by
calling func(x) for each element x in the array. That's equivalent to the
list comprehension and functional syntax:

[func(x) for x in arg]
map(func, arg)


Here's a neat little decorator which decorates a function with a special
callable attribute "v" which operates somewhat like Julia's dot syntax:

def vectorize(func):
def v(seq, **kw):
return [func(x, **kw) for x in seq]
func.v = v
return func



py> @vectorize
... def add2(x):
... return x+2
...
py> add2.v([100, 200, 300, 400])
[102, 202, 302, 402]



This version is just a minimal toy: it always returns a list, regardless of
the input argument. As such it doesn't offer much more than a list
comprehension or map(). I have a more complex version which behaves more
like Python 2's map() in that it tries to return an object of the same type
as its input (e.g. a string if you call it on a string, a tuple if you call
it on a tuple, etc).

Is there any other functionality which would make this more useful?





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Vectorized functions

2016-08-10 Thread Paul Rubin
Steven D'Aprano  writes:
> Is there any other functionality which would make this more useful?

Cute, but map or listcomps work ok.  Here's the Haskell equivalent to
your example, fwiw, using the <$> operator from the Control.Applicative
module:

(+2) <$> [1,2,3]   =>   [3,4,5]

If you haven't tried Haskell yet, you might enjoy it.
http://learnyouahaskell.com is a good place to start.
-- 
https://mail.python.org/mailman/listinfo/python-list


Asynchronous programming

2016-08-10 Thread Steven D'Aprano
The latest versions of Python are introducing new keywords for asynchronous
programming, async and await. See PEP 492:

https://www.python.org/dev/peps/pep-0492/

Is there a good beginner's tutorial introducing the basics of asynchronous
programming? Starting with, why and where would you use it?

I've read "What colour is your function" and it isn't quite jelling for me.

http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/


How is this the same as, or different from, event-based programming? I'm
familiar with programming in an event-based language where the interpreter
itself provides an event loop and dispatches messages to your objects:

- I define objects such as buttons, text fields, etc., and give them methods
("handlers") which handle certain messages such as "mouseUp", etc.;

- the interpreter runs in a loop, firing off messages in response to the
user's actions;

- there's a message passing hierarchy, whereby messages are first received
by (let's say) the button the user clicked on, if not handled by a mouseUp
method it is passed on to the next object in the hierarchy (say, the
window), and then finally to the interpreter itself, at which point it
either ignores the message or raises an exception.


If I'm using async and await in Python, where's the event loop? What are the
messages, and where are they sent? Or am I on the wrong track altogether?




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Asynchronous programming

2016-08-10 Thread Terry Reedy

On 8/10/2016 11:53 PM, Steven D'Aprano wrote:

The latest versions of Python are introducing new keywords for
asynchronous programming, async and await. See PEP 492:

https://www.python.org/dev/peps/pep-0492/

Is there a good beginner's tutorial introducing the basics of
asynchronous programming? Starting with, why and where would you use
it?

I've read "What colour is your function" and it isn't quite jelling
for me.

http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/



How is this the same as, or different from, event-based programming?
I'm familiar with programming in an event-based language where the
interpreter itself provides an event loop and dispatches messages to
your objects:

- I define objects such as buttons, text fields, etc., and give them
methods ("handlers") which handle certain messages such as "mouseUp",
etc.;

- the interpreter runs in a loop, firing off messages in response to
the user's actions;

- there's a message passing hierarchy, whereby messages are first
received by (let's say) the button the user clicked on, if not
handled by a mouseUp method it is passed on to the next object in the
hierarchy (say, the window), and then finally to the interpreter
itself, at which point it either ignores the message or raises an
exception.

If I'm using async and await in Python, where's the event loop? What
are the messages, and where are they sent? Or am I on the wrong track
altogether?


You might be able to glean something from the succession of files I 
uploaded to

https://bugs.python.org/issue27546
Integrate tkinter and asyncio (and async)

I started with just mixing tk and asyncio callbacks.  After some 
struggle with similar question as you ask above, I ended up with more or 
less callback free code using async def and async for. Once I got over 
the hump, I rather like it.


--
Terry Jan Reedy

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


Re: Asynchronous programming

2016-08-10 Thread Chris Angelico
On Thu, Aug 11, 2016 at 1:53 PM, Steven D'Aprano
 wrote:
> How is this the same as, or different from, event-based programming? I'm
> familiar with programming in an event-based language where the interpreter
> itself provides an event loop and dispatches messages to your objects:
>
> - I define objects such as buttons, text fields, etc., and give them methods
> ("handlers") which handle certain messages such as "mouseUp", etc.;
>
> - the interpreter runs in a loop, firing off messages in response to the
> user's actions;
>
> - there's a message passing hierarchy, whereby messages are first received
> by (let's say) the button the user clicked on, if not handled by a mouseUp
> method it is passed on to the next object in the hierarchy (say, the
> window), and then finally to the interpreter itself, at which point it
> either ignores the message or raises an exception.
>
>
> If I'm using async and await in Python, where's the event loop? What are the
> messages, and where are they sent? Or am I on the wrong track altogether?

There'll be one somewhere, probably at some top-level main loop. Async
programming isn't usually compared with events *as such*, but with
callbacks. Consider these three ways of doing a database transaction:

def synchronous(id):
trn = conn.begin_transaction()
trn.execute("select name from people where id=%d", (id,))
name, = trn.fetchone()
trn.execute("update people set last_seen=now() where id=%d", (id,))
trn.commit()
return name

def callbacks_1(cb, id):
conn.begin_transaction(callbacks_2, cb, id)
def callbacks_2(trn, cb, id):
trn.execute("select name from people where id=%d", (id,),
callbacks_3, cb, id)
def callbacks_3(trn, cb, id):
trn.fetchone(callbacks_4, cb, id)
def callbacks_4(trn, data, cb, id):
name, = data
trn.execute("update people set last_seen=now() where id=%d",
(id,), callbacks_5, cb, name)
def callbacks_5(trn, cb, name):
trn.commit(callbacks_6, cb, name)
def callbacks_6(trn, cb, name):
cb(name)

def asynchronous(id):
trn = yield from conn.begin_transaction()
yield from trn.execute("select name from people where id=%d", (id,))
name, = yield from trn.fetchone()
yield from trn.execute("update people set last_seen=now() where
id=%d", (id,))
yield from trn.commit()
return name

Change this last one to "async def" and change the "yield from"s into
"await", and you get the new syntax, but it's still built on top of
generators. Basically, async code works by stopping execution in the
middle of a logical operation, and then resuming it later. (The
callbacks example could be improved on some by using closures, and in
JavaScript, where this is really common, you can use anonymous
functions. I still prefer the last form though.) Every time you hit an
"await", you call into that function and see what it says - in this
case, it's going to "yield" somewhere, sending back some sort of
awaitable thing, eg a half-done database operation. When that
operation completes, you go into the next step of the process.

How does this align with event handling? Not very well. Normally event
handling means non-linear execution - for instance, button clicks on a
GUI aren't going to step nicely through a progression. But the two
work well together; if you have a primary event loop that starts out
by looking for GUI events, you could have something like the above
asynchronous function, and it'll give control back to the main loop
while the database is working - thus making the GUI responsive again.
You can mess around with the front end while the back end does its
work.

Hope that helps some.

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


Send Msg To Specific Tcp Client from Tcp Server Python

2016-08-10 Thread Anil reddy reddy M
I was written a simple tcp server in python, which is nicely communicating with 
multiple tcp clients. My Tcp Server can accept multiple clients at time, each 
client as a new thread. I want send message to specific tcp client after some 
time.
I can client Address from   connction, addr = socket.accept(), but i am not 
able send msg to client with client  IP.POrT. I would be thankfull for any help 
i get

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


Re: Asynchronous programming

2016-08-10 Thread Paul Rubin
Steven D'Aprano  writes:
> Is there a good beginner's tutorial introducing the basics of asynchronous
> programming? Starting with, why and where would you use it?

You might look at some node.js tutorials since there are about a
gazillion of them and some of them must be good.  Also twistedmatrix.org
for an older Python package of that sort.

The basic reason to do it is it lets you serve a lot of concurrent i/o
channels (network connections, say) without using threads.  If you want
to read a packet, you launch a non-blocking read that returns
immediately, and then transfer control (often through some
behind-the-scenes magic) to an event loop that dispatches back to you
after your read request completes, through either a callback or a
coroutine jump depending on which framework you're using.  In Python
that gets you much higher performance than threads, plus you avoid the
usual bunch of hazards that parents used to scare their kids with about
multi-threaded programming.

> How is this the same as, or different from, event-based programming? 

The basic characteristic of asynchronous programming is that it involves
changing all your usual blocking i/o calls to non-blocking ones, so your
program can keep running as soon as your request is started.  That's not
always the case with event-based programming in general, where event
handlers might do i/o.  The basic problem you face is the inversion of
control needed to get in and out of the event loop, compared to the
sequential blocking i/o that you'd use in a threaded or non-concurrent
program.

The new thing in Python is asyncio, which uses coroutines instead of
callbacks to switch between event handlers.  It's asynchronous
programming either way, but coroutines can be seen as a cleaner
implementation technique depending on your POV.  Coroutines in the form
of "yield from" first appeared in Python 3.??? and became more fully
integrated with the new async and await keywords.  Before that, async
concurrency in Python was done using callback schemes.  Twisted used to
work like that (I don't know if it still does).  I hated it but I was in
a minority.

> If I'm using async and await in Python, where's the event loop? What
> are the messages, and where are they sent? Or am I on the wrong track
> altogether?

It's supplied by the asyncio package.  Btw, David Beazely's article
about Python coroutines is very good, though it was written before the
new keywords were added.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Asynchronous programming

2016-08-10 Thread Chris Angelico
On Thu, Aug 11, 2016 at 3:06 PM, Paul Rubin  wrote:
> The basic reason to do it is it lets you serve a lot of concurrent i/o
> channels (network connections, say) without using threads.  If you want
> to read a packet, you launch a non-blocking read that returns
> immediately, and then transfer control (often through some
> behind-the-scenes magic) to an event loop that dispatches back to you
> after your read request completes, through either a callback or a
> coroutine jump depending on which framework you're using.  In Python
> that gets you much higher performance than threads, plus you avoid the
> usual bunch of hazards that parents used to scare their kids with about
> multi-threaded programming.
>

Hmm. I'm not sure about that last bit. In order to properly support
asynchronous I/O and the concurrency and reentrancy that that implies,
you basically need all the same disciplines that you would for
threaded programming - no shared mutable state, keep your locals
local, etc. The only difference is that context switches are coarser
than they are in threading (and remember, CPython already refuses to
switch threads in the middle of one bytecode operation, so most of the
worst horrors can't happen even there). But maybe I'm too comfortable
with threads. It's entirely possible; my first taste of reentrancy was
interrupt handling in real mode 80x86 assembly language, and in
comparison to that, threads are pretty benign!

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


Generate reports in Python

2016-08-10 Thread Ernest Bonat, Ph.D.
Hi,

I'm looking for best modules/practices to generate reports (HTML, PDF, etc)
in Python. Good ideas are very appreciated.

Thanks

Ernest Bonat, Ph.D.
Senior Software Engineer
Senior Data Scientist
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Asynchronous programming

2016-08-10 Thread Paul Rudin
Steven D'Aprano  writes:

>
> Is there a good beginner's tutorial introducing the basics of asynchronous
> programming? Starting with, why and where would you use it?

You could do worse than watch Dave Beazley's pycon talk:
https://www.youtube.com/watch?v=lYe8W04ERnY
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Send Msg To Specific Tcp Client from Tcp Server Python

2016-08-10 Thread dieter
Anil reddy reddy M  writes:

> I was written a simple tcp server in python, which is nicely communicating 
> with multiple tcp clients. My Tcp Server can accept multiple clients at time, 
> each client as a new thread. I want send message to specific tcp client after 
> some time.
> I can client Address from   connction, addr = socket.accept(), but i am not 
> able send msg to client with client  IP.POrT. I would be thankfull for any 
> help i get

TCP channels are bidirectional: you can send messages in both
directions. Thus, writing to the server's socket (for a specific client)
sends the written data to the client.

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


Re: Generate reports in Python

2016-08-10 Thread Paul Rubin
"Ernest Bonat, Ph.D."  writes:
> I'm looking for best modules/practices to generate reports (HTML, PDF, etc)
> in Python. Good ideas are very appreciated.

See: http://www.reportlab.com/ for a well regarded package
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Asynchronous programming

2016-08-10 Thread Christian Gollwitzer

Am 11.08.16 um 05:53 schrieb Steven D'Aprano:

How is this the same as, or different from, event-based programming? I'm
familiar with programming in an event-based language where the interpreter
itself provides an event loop and dispatches messages to your objects


I'll just comment on  that one. Asynchronous programming is event based 
programming. In fact if you drop down to the C level, it works the same 
- you get callbacks when new data arives over a socket, or when a timer 
triggers. asyncio is just a way that makes writing stateful callback 
handlers easier, in the same way that generators are easier to write 
using "yield" than as a function with a global variable.


In typical GUI code, there are usually not that many places qhere ou 
have sequential code. A simple exmaple might be a counter. Using asyncio 
and a properly integrated GUI toolkit, you could write it as (pseudo-code)


async def counter():
for i in range(10):
button.settext("click me %d"%i)
await button.click()

button.disable()
messageBox("You reached the end!")

Writing that same thing in callback code to button.on_click() is 
obviously less fun and feels inverted.


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


Re: Asynchronous programming

2016-08-10 Thread Christian Gollwitzer

Am 11.08.16 um 06:38 schrieb Terry Reedy:

You might be able to glean something from the succession of files I
uploaded to
https://bugs.python.org/issue27546
Integrate tkinter and asyncio (and async)

I started with just mixing tk and asyncio callbacks.  After some
struggle with similar question as you ask above, I ended up with more or
less callback free code using async def and async for. Once I got over
the hump, I rather like it.


Interesting discussion! Tcl itself acquired coroutines some years ago 
(in 8.6) and people in this community are very excited with async based 
programming.


I'm convinced that it is possible to integrate Tcl's event loop with 
asyncio's loop without regular update polling. This might require a 
patch to Tkinter at the C level. For example, an easy way is to put 
Tcl/Tk in it's own thread. The Tcl event loop can be woken up from a 
different thread by the TclThreadQueueEvent() and TclTheradAlert() API 
calls. Basically, you would spawn a new thread (from the C level), when 
loading Tkinter, which loads Tk and sits in a Tcl event loop ("vwait 
forever"). tk.eval() and tk.call() would be redefined to post events 
using the aforementioned functions. When a Tcl callback arrives, it 
would send an event to asyncio in the main thread - no idea how this 
works, though. In the end, it would also allow to use Tkinter from 
different Python threads, because all calls would be rooted to the one 
instance only.


Christian

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


Re: Send Msg To Specific Tcp Client from Tcp Server Python

2016-08-10 Thread Anil reddy reddy M
On Thursday, August 11, 2016 at 11:36:47 AM UTC+5:30, dieter wrote:
> Anil reddy reddy M  writes:
> 
> > I was written a simple tcp server in python, which is nicely communicating 
> > with multiple tcp clients. My Tcp Server can accept multiple clients at 
> > time, each client as a new thread. I want send message to specific tcp 
> > client after some time.
> > I can client Address from   connction, addr = socket.accept(), but i am not 
> > able send msg to client with client  IP.POrT. I would be thankfull for any 
> > help i get
> 
> TCP channels are bidirectional: you can send messages in both
> directions. Thus, writing to the server's socket (for a specific client)
> sends the written data to the client.

Hi,
Thanks for your reply, But how can i find client socketfd when i want send 
message to specific client.I can store connected client ip, port in database. 
how can use that ip, port when i want send.

  Sincerely
   Anil,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ctypes And The WACAH Principle

2016-08-10 Thread Lawrence D’Oliveiro
On Thursday, August 11, 2016 at 3:23:58 PM UTC+12, eryk sun wrote:
>
> ...you can cast() bytes to c_void_p to get the address.
>
> ...
> 
> For bytearray, use the buffer interface to create a ctypes array. The
> array is not a copy.

Cool! Those pointers* are much appreciated.

*So to speak. :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Vectorized functions

2016-08-10 Thread Christian Gollwitzer

Am 11.08.16 um 06:02 schrieb Steven D'Aprano:

Here's a neat little decorator which decorates a function with a special
callable attribute "v" which operates somewhat like Julia's dot syntax:

def vectorize(func):
def v(seq, **kw):
return [func(x, **kw) for x in seq]
func.v = v
return func



py> @vectorize
... def add2(x):
... return x+2
...
py> add2.v([100, 200, 300, 400])
[102, 202, 302, 402]




Neat. I suspect, that in addition to the higher-level syntax, it also 
improves the speed in Julia, because the loop is implicit and it 
compiles to native code using LLVM. Numpy's ufunc comes to mind - there 
is also a vectorizer for Python functions 
http://docs.scipy.org/doc/numpy/reference/generated/numpy.frompyfunc.html#numpy.frompyfunc



Christian

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