Re: Call for Assistance

2016-08-09 Thread Lutz Horn
Regarding the CC-BY-NC-SA license:

Am 09.08.16 um 17:42 schrieb Reto Brunner:
> What on earth isn't "free" enough about
[...]
> It is even a viral (copy left) licence, so even a fsf member should 
> be happy

The FSF considers the CC-BY-NC to *not* be a license for free
documentation[1]:

> This license does not qualify as free, because there are restrictions
> on charging money for copies. Thus, we recommend you do not use this
> license for documentation.

Using the SA variant does not help.

Lutz

[1] https://www.gnu.org/licenses/license-list.en.html#CC-BY-NC
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question regarding stdlib distutils strtobool behavior

2016-08-09 Thread Terry Reedy

On 8/9/2016 9:22 AM, Joseph Bane wrote:

Hello.

It recently came to my attention that the strtobool function in the
standard library doesn't return Python native boolean values, but
rather returns integer 0 or 1:

https://hg.python.org/cpython/file/3.5/Lib/distutils/util.py#l304

I am curious why this is the defined behavior and whether anyone can
fill me in regarding this approach. For clarity, I would expect the
code to `return True` and `return False` rather than `return 1` and
`return 0`.


I am rather sure distutils predates 2.2 and the addition of bool.  Much 
code has been converted from 0/1 to False/True.  I don't know if this 
particular holdover is intentional -- fear of breaking something -- or 
oversight/inertia.


I searched the tracker for 'strtobool' in component 'distutiles', status 
"don't care", and there were no hits.  (Search is not perfect, though.)



--
Terry Jan Reedy

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


Re: Specify the database for a Django ModelForm instance

2016-08-09 Thread Phil Boutros
Ben Finney  wrote:
> Howdy all,
>
> How can I specify which database (by its alias name) a Django ModelForm
> should use

> What is the equivalent for using='foo' when instantiating a ModelForm
> for the model, or calling its methods (ModelForm.clean, ModelForm.save,
> etc.)?

You would specify it on the save method (using the same
"using='foo'") paremeter.  Do your work between calling save with
commit=false and doing the real save.

For example:

http://stackoverflow.com/questions/37740848/use-a-specific-database-with-a-modelform

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


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

2016-08-09 Thread Juan Pablo Romero Méndez
Hello,

In online forums sometimes people complain that they end up having to test
constantly for None, or that a function's argument has a specific type /
shape (which immediately brings the following aphorism to mind: "Any
sufficiently large test suite contains an ad hoc, bug-ridden, slow
implementation of half of Haskell's type system").

Some responses I've seen to that particular complaint is that they are
doing it wrong. That they are trying to force a type system into a dynamic
language (Clojure, Ruby, Python, Javascript, etc).

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?


Thanks!

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


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

2016-08-09 Thread Chris Angelico
On Wed, Aug 10, 2016 at 4:29 AM, Juan Pablo Romero Méndez
 wrote:
> What is the best way to use the dynamic features of Python to avoid having
> to write a poor's man type system?
>

Step 1: Don't even care about it. Take whatever comes, and assume that
it's what you expect.

Step 2: When step 1 starts showing that you're getting the same
problems lots of times, slap in a try/except to deal with just that.

Most of the time, step 1 is all you need.

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-09 Thread Michael Selik
On Tue, Aug 9, 2016 at 2:31 PM Juan Pablo Romero Méndez <
[email protected]> wrote:

> Hello,
>
> In online forums sometimes people complain that they end up having to test
> constantly for None, or that a function's argument has a specific type /
> shape (which immediately brings the following aphorism to mind: "Any
> sufficiently large test suite contains an ad hoc, bug-ridden, slow
> implementation of half of Haskell's type system").
>
> Some responses I've seen to that particular complaint is that they are
> doing it wrong. That they are trying to force a type system into a dynamic
> language (Clojure, Ruby, Python, Javascript, etc).
>
> 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?
>

It'll be easiest to discuss a specific example. Can you share a chunk of
code which frustrates you?

In general, Python's TypeError and AttributeError serve much the same
purpose as other languages' type systems. Where other languages might force
you to create named, formally specified types, Python allows informally
specified interfaces.

"File-like" is a good example. Rather than go through the frustration of a
formal definition for what is file-like, stay productive and flexible with
duck typing. Objects that don't have the appropriate methods or attributes
will cause a TypeError or AttributeError. If you're passing in the wrong
kind of object, you'll find out almost as soon as you write the code
(because you test early and often), just as you would with a statically
analyzed type system.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-09 Thread Chris Angelico
On Wed, Aug 10, 2016 at 4:51 AM, Michael Selik  wrote:
> "File-like" is a good example. Rather than go through the frustration of a
> formal definition for what is file-like, stay productive and flexible with
> duck typing. Objects that don't have the appropriate methods or attributes
> will cause a TypeError or AttributeError. If you're passing in the wrong
> kind of object, you'll find out almost as soon as you write the code
> (because you test early and often), just as you would with a statically
> analyzed type system.

It's slightly delayed; with Python, you have to actually execute the
code path in question, whereas a static type system would detect it at
the compilation stage. So it may depend on the quality of your
testing. MyPy may be able to help there.

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-09 Thread Paul Rubin
Juan Pablo Romero Méndez  writes:
> In online forums sometimes people complain that they end up having to
> test constantly for None

That's something of a style issue.  You can code in a way that avoids a
lot of those tests (not all of them).

> 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?

In practice dynamically typed code often is monomorphic anyway.
Dynamism shows up in things like deserializers.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-09 Thread MRAB

On 2016-08-09 19:57, Chris Angelico wrote:

On Wed, Aug 10, 2016 at 4:51 AM, Michael Selik  wrote:

"File-like" is a good example. Rather than go through the frustration of a
formal definition for what is file-like, stay productive and flexible with
duck typing. Objects that don't have the appropriate methods or attributes
will cause a TypeError or AttributeError. If you're passing in the wrong
kind of object, you'll find out almost as soon as you write the code
(because you test early and often), just as you would with a statically
analyzed type system.


It's slightly delayed; with Python, you have to actually execute the
code path in question, whereas a static type system would detect it at
the compilation stage. So it may depend on the quality of your
testing. MyPy may be able to help there.

Static checking won't tell you if, say, you're adding when you should be 
subtracting, so you'd need to execute the code path in question anyway 
during testing!


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


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

2016-08-09 Thread Juan Pablo Romero Méndez
2016-08-09 12:06 GMT-07:00 Paul Rubin :

> Juan Pablo Romero Méndez  writes:
> > In online forums sometimes people complain that they end up having to
> > test constantly for None
>
> That's something of a style issue.  You can code in a way that avoids a
> lot of those tests (not all of them).
>


This is exactly what I'm looking for :).  Do you have any resource (blog /
book, etc) that discusses this style?


>
> > 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?
>
> In practice dynamically typed code often is monomorphic anyway.
> Dynamism shows up in things like deserializers.
> --
> 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-09 Thread Juan Pablo Romero Méndez
What static checking can actually guarantee varies depending on the
specific type system at hand (C# vs Haskell vs Idris for example). But most
of them can guarantee simple stuff like: "I'm I allowed to invoke this
function at this point?"

If you don't have that, well you can rely on tests to show that certain
code paths behave in a certain way when exercised with certain inputs.



2016-08-09 12:22 GMT-07:00 MRAB :

> On 2016-08-09 19:57, Chris Angelico wrote:
>
>> On Wed, Aug 10, 2016 at 4:51 AM, Michael Selik 
>> wrote:
>>
>>> "File-like" is a good example. Rather than go through the frustration of
>>> a
>>> formal definition for what is file-like, stay productive and flexible
>>> with
>>> duck typing. Objects that don't have the appropriate methods or
>>> attributes
>>> will cause a TypeError or AttributeError. If you're passing in the wrong
>>> kind of object, you'll find out almost as soon as you write the code
>>> (because you test early and often), just as you would with a statically
>>> analyzed type system.
>>>
>>
>> It's slightly delayed; with Python, you have to actually execute the
>> code path in question, whereas a static type system would detect it at
>> the compilation stage. So it may depend on the quality of your
>> testing. MyPy may be able to help there.
>>
>> Static checking won't tell you if, say, you're adding when you should be
> subtracting, so you'd need to execute the code path in question anyway
> during testing!
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question regarding stdlib distutils strtobool behavior

2016-08-09 Thread Joseph Bane
On 8/9/16 1:42 PM, Terry Reedy wrote:
> On 8/9/2016 9:22 AM, Joseph Bane wrote:
>> Hello.
>>
>> It recently came to my attention that the strtobool function in the
>> standard library doesn't return Python native boolean values, but
>> rather returns integer 0 or 1:
>>
>> https://hg.python.org/cpython/file/3.5/Lib/distutils/util.py#l304
>>
>> I am curious why this is the defined behavior and whether anyone can
>> fill me in regarding this approach. For clarity, I would expect the
>> code to `return True` and `return False` rather than `return 1` and
>> `return 0`.
> 
> I am rather sure distutils predates 2.2 and the addition of bool.  Much
> code has been converted from 0/1 to False/True.  I don't know if this
> particular holdover is intentional -- fear of breaking something -- or
> oversight/inertia.
> 
> I searched the tracker for 'strtobool' in component 'distutiles', status
> "don't care", and there were no hits.  (Search is not perfect, though.)
> 
> 

Thanks for the legwork here! I have created the following issue after I
read this: http://bugs.python.org/issue27721

Hopefully it will get some more discussion going. Thanks also to Michael
Selik for his great advice in his last response! I think it's great
we're having such positive conversations!

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


Subprocess Startup Error

2016-08-09 Thread Sean via Python-list
Does anyone have advice on how to resolve this message when I attempt to open 
IDLE? 


"IDLE's subprocess didn't make connection. Either IDLE can't start a subprocess 
or personal firewall software is blocking the connection."


I am running Windows 10 Home on a 64bit machine.


I am running Python 3.5.2.  


I have tried uninstalling the software and reinstalling but no luck with that.


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


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

2016-08-09 Thread Rob Gaddi
Juan Pablo Romero Méndez wrote:

> 2016-08-09 12:06 GMT-07:00 Paul Rubin :
>
>> Juan Pablo Romero Méndez  writes:
>> > In online forums sometimes people complain that they end up having to
>> > test constantly for None
>>
>> That's something of a style issue.  You can code in a way that avoids a
>> lot of those tests (not all of them).
>>
>
>
> This is exactly what I'm looking for :).  Do you have any resource (blog /
> book, etc) that discusses this style?
>

It's not a style, it's the absence of one.

def add2list(lst, elem):
lst.extend([elem, elem])
return lst

I did all the type checking I needed to there; none whatsoever.  If
passed a list, or something that behaves like one, that does the
expected thing.

If passed an ExtensionLadder, it probably does the wrong thing, but that
is no way my problem.

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


Invalid Syntax

2016-08-09 Thread Ltc Hotspot
Hi, Everyone:

What is the source of the following, 
'error message: SyntaxError: invalid syntax (, line 2)'

v. Python 3.3

Code reads:

x=1
if x==1
# indent 4 spaces
print "x = 1"

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


Re: Invalid Syntax

2016-08-09 Thread Rob Gaddi
Ltc Hotspot wrote:

> Hi, Everyone:
>
> What is the source of the following, 
> 'error message: SyntaxError: invalid syntax (, line 2)'
>
> v. Python 3.3
>
> Code reads:
>
> x=1
> if x==1
> # indent 4 spaces
> print "x = 1"
>
> Hal

A missing colon, the appropriate location of which is left as an
exercise to the reader.

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


Re: cropping a random part of an image

2016-08-09 Thread Robin Koch

Am 09.08.2016 um 14:40 schrieb [email protected]:


I'm new to python and I have 30.000 pictures.
I need to crop, let's say 100, parts of 256x256, randomly out of every picture.


Interessting task. May I ask for the purose of this?


I cant find an answer in the net, would be nice if someone could help me out!


Although I think you should follow Peters advise I have put something 
together, out of curiousity, that should do the trick.


There are several ways to improve it.
First I'm using os.listdir() instead of os.walk().
In my example scenario that's ok. If your files are spread over 
different subfolders, os.walk() is the better way to do it.


You could add a counter (see: enumerate()) to have a better overview 
over the progress.


Also you might have other preferences for the location of the tiles. 
(e.g. one folder per image).


# Cuts random tiles from pictures
#
# This version works with approx. 270 tiles per second on my machine
# (on 13.5MPx images). So 3 million tiles should take about 3 hours.

import random, os, time
from PIL import Image

INPATH = r".../images"
OUTPATH = r".../tiles"

dx = dy = 256
tilesPerImage = 100

files = os.listdir(INPATH)
numOfImages = len(files)

t = time.time()
for file in files:
  with Image.open(os.path.join(INPATH, file)) as im:
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))



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


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

2016-08-09 Thread Michael Selik
On Tue, Aug 9, 2016 at 2:59 PM Chris Angelico  wrote:

> On Wed, Aug 10, 2016 at 4:51 AM, Michael Selik 
> wrote:
> > "File-like" is a good example. Rather than go through the frustration of
> a
> > formal definition for what is file-like, stay productive and flexible
> with
> > duck typing. Objects that don't have the appropriate methods or
> attributes
> > will cause a TypeError or AttributeError. If you're passing in the wrong
> > kind of object, you'll find out almost as soon as you write the code
> > (because you test early and often), just as you would with a statically
> > analyzed type system.
>
> It's slightly delayed; with Python, you have to actually execute the
> code path in question, whereas a static type system would detect it at
> the compilation stage. So it may depend on the quality of your
> testing. MyPy may be able to help there.
>

Depends on how long it takes to compile!
https://xkcd.com/303/
:-)
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-09 Thread Juan Pablo Romero Méndez
2016-08-09 13:18 GMT-07:00 Rob Gaddi :

> Juan Pablo Romero Méndez wrote:
>
> > 2016-08-09 12:06 GMT-07:00 Paul Rubin :
> >
> >> Juan Pablo Romero Méndez  writes:
> >> > In online forums sometimes people complain that they end up having to
> >> > test constantly for None
> >>
> >> That's something of a style issue.  You can code in a way that avoids a
> >> lot of those tests (not all of them).
> >>
> >
> >
> > This is exactly what I'm looking for :).  Do you have any resource (blog
> /
> > book, etc) that discusses this style?
> >
>
> It's not a style, it's the absence of one.
>
> def add2list(lst, elem):
> lst.extend([elem, elem])
> return lst
>
> I did all the type checking I needed to there; none whatsoever.  If
> passed a list, or something that behaves like one, that does the
> expected thing.
>
> If passed an ExtensionLadder, it probably does the wrong thing, but that
> is no way my problem.
>


So as the writer of the function you expect the user to read the function
body to determine what is safe to pass or not?



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


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

2016-08-09 Thread Random832
On Tue, Aug 9, 2016, at 16:51, Juan Pablo Romero Méndez wrote:
> So as the writer of the function you expect the user to read the function
> body to determine what is safe to pass or not?

How about expecting them to read the docstring?
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-09 Thread Michael Selik
On Tue, Aug 9, 2016 at 3:22 PM Juan Pablo Romero Méndez <
[email protected]> wrote:

> I'm actually looking for ways to minimize run time errors, so that would
> include TypeError and AttributeError.
>
> In your "File-like" example having type information would prevent me from
> even passing the wrong object, so I don't need to test for that particular
> problem.
>

Let's clarify this discussion by using the word "check" for type-checks
that necessary for the application to behave as desired and using the word
"test" for checking application behavior under normal and edge cases, such
as unexpected types being passed.

Are you saying you want to minimize type-checks during the execution of
your application? Or do you want to minimize the amount of testing code you
write?

I don't check for file-likeness in the application code. If somehow a
non-file-like (file-unlike?) object makes its way into my function, I
expect (and hope) my program will immediately stop and print a nice
traceback for me. If I wanted some other behavior, I probably would already
have try/except surrounding the appropriate section to deal with not-found
or permissions errors. If I need to, I'll add TypeError and AttributeError
to that except statement.

I sometimes write tests for unexpected inputs, checking to ensure that
either a TypeError or AttributeError is raised. However, sometimes I'm not
worried about it. If the user gives me bizarre input, they should know to
expect (possibly) bizarre results. Who knows, maybe that's what they wanted.

I would add type-checking or value-checking to my application if the wrong
type or value does not raise an error but instead causes an infinite loop
or corrupts data. Those situations are fairly rare.



On Tue, Aug 9, 2016 at 4:52 PM Juan Pablo Romero Méndez <
[email protected]> wrote:

> So as the writer of the function you expect the user to read the function
> body to determine what is safe to pass or not?


No. But I usually expect them to not freak out when they see a traceback.
Or if they do freak out, I want them to freak out and send a bug report.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-09 Thread Rob Gaddi
Juan Pablo Romero Méndez wrote:

> 2016-08-09 13:18 GMT-07:00 Rob Gaddi :
>
>> Juan Pablo Romero Méndez wrote:
>>
>> > 2016-08-09 12:06 GMT-07:00 Paul Rubin :
>> >
>> >> Juan Pablo Romero Méndez  writes:
>> >> > In online forums sometimes people complain that they end up having to
>> >> > test constantly for None
>> >>
>> >> That's something of a style issue.  You can code in a way that avoids a
>> >> lot of those tests (not all of them).
>> >>
>> >
>> >
>> > This is exactly what I'm looking for :).  Do you have any resource (blog
>> /
>> > book, etc) that discusses this style?
>> >
>>
>> It's not a style, it's the absence of one.
>>
>> def add2list(lst, elem):
>> lst.extend([elem, elem])
>> return lst
>>
>> I did all the type checking I needed to there; none whatsoever.  If
>> passed a list, or something that behaves like one, that does the
>> expected thing.
>>
>> If passed an ExtensionLadder, it probably does the wrong thing, but that
>> is no way my problem.
>>
>
>
> So as the writer of the function you expect the user to read the function
> body to determine what is safe to pass or not?
>

No, but I expect them to read the function documentation.  The
alternative is to expect that the user is going to go around trying to
randomly call functions with whatever objects they happen to have lying
around in an attempt to figure out what they do.  The docstring for the
above function would probably be """Append two copies of elem to the
end of list lst."""

This goes back to the "consenting adults" principle in Python. 
Assume that the person calling your functions isn't a blithering moron
in need of protection from themselves. Sometimes you'll be wrong in
that assumption, but their life as a blithering moron is already quite
difficult and so you won't have substantially added to that by failing
to put guard rails up on your function. Conversely, if you explicitly
check isinstance(lst, list) then I can't, as one clever SOB, pass
it a list-like object and have it just work.  I instead would have to
have my object expressly subclass list, even if I had a reason not to.

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


Re: Call for Assistance

2016-08-09 Thread Charles Ross
> CC-BY-NC-SA is not a license for free (as in speech) content. Is that
> what you want?

I really appreciate all the conversation about the license. Perhaps I made a 
mistake with requiring noncommercial in the license, I don’t know, but I’ll 
look at the links provided about free documentation and free software and see 
if I can understand the issues more and perhaps make a better decision.

Honestly, I have no expectation that this book will ever be anything more than 
a (hopefully) useful resource for others like myself, as described in the 
current introduction. I might have a remote hope that it would be something 
that would receive attention from a traditional publisher, but that remote hope 
is in the same category as a remote hope that a distant rich uncle will someday 
name me in his well. So, given the minuscule odds of that happening, I’ll 
probably remove the NC portion. I’ll just do a bit of research first with those 
links.

Thanks,
Chuck
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Invalid Syntax

2016-08-09 Thread cs

On 09Aug2016 20:22, Rob Gaddi  wrote:

Ltc Hotspot wrote:

What is the source of the following,
'error message: SyntaxError: invalid syntax (, line 2)'
v. Python 3.3

Code reads:

x=1
if x==1
# indent 4 spaces
print "x = 1"


A missing colon, the appropriate location of which is left as an
exercise to the reader.


A bit unfair, it is not the most helpful of syntax complaints. Hal: all Python 
compound statements (if, while, for, with etc) end in a colon and an indented 
"suite" (enclosed chunk of statements). So:


 if x == 1:
 ..

Also, in Python 3 "print" is a function, so:

 print("x = 1")

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


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

2016-08-09 Thread Juan Pablo Romero Méndez
2016-08-09 14:01 GMT-07:00 Michael Selik :

> On Tue, Aug 9, 2016 at 3:22 PM Juan Pablo Romero Méndez <
> [email protected]> wrote:
>
>> I'm actually looking for ways to minimize run time errors, so that would
>> include TypeError and AttributeError.
>>
>> In your "File-like" example having type information would prevent me from
>> even passing the wrong object, so I don't need to test for that particular
>> problem.
>>
>
> Let's clarify this discussion by using the word "check" for type-checks
> that necessary for the application to behave as desired and using the word
> "test" for checking application behavior under normal and edge cases, such
> as unexpected types being passed.
>
> Are you saying you want to minimize type-checks during the execution of
> your application? Or do you want to minimize the amount of testing code you
> write?
>


My goal is 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)

(I'm not suggesting I want to avoid writing tests at all, since there are
many things the type checker can't possible check).



>
> I don't check for file-likeness in the application code. If somehow a
> non-file-like (file-unlike?) object makes its way into my function, I
> expect (and hope) my program will immediately stop and print a nice
> traceback for me. If I wanted some other behavior, I probably would already
> have try/except surrounding the appropriate section to deal with not-found
> or permissions errors. If I need to, I'll add TypeError and AttributeError
> to that except statement.
>
>
This is interesting. You are Ok having runtime errors?



> I sometimes write tests for unexpected inputs, checking to ensure that
> either a TypeError or AttributeError is raised. However, sometimes I'm not
> worried about it. If the user gives me bizarre input, they should know to
> expect (possibly) bizarre results. Who knows, maybe that's what they wanted.
>
> I would add type-checking or value-checking to my application if the
> wrong type or value does not raise an error but instead causes an infinite
> loop or corrupts data. Those situations are fairly rare.
>
>
>
> On Tue, Aug 9, 2016 at 4:52 PM Juan Pablo Romero Méndez <
> [email protected]> wrote:
>
>> So as the writer of the function you expect the user to read the function
>> body to determine what is safe to pass or not?
>
>
> No. But I usually expect them to not freak out when they see a traceback.
> Or if they do freak out, I want them to freak out and send a bug report.
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Invalid Syntax

2016-08-09 Thread Steven D'Aprano
On Wed, 10 Aug 2016 06:20 am, Ltc Hotspot wrote:

> Hi, Everyone:
> 
> What is the source of the following,
> 'error message: SyntaxError: invalid syntax (, line 2)'
> 
> v. Python 3.3
> 
> Code reads:
> 
> x=1
> if x==1
> # indent 4 spaces
> print "x = 1"
> 
> Hal

The error message tells you exactly which line the error is on: line 2. Have
you done a tutorial to learn how to write Python code? If you haven't, you
should:

https://docs.python.org/3/tutorial/


The error is that all statements that introduce a new indented block require
a colon:

class C:
def func():
  if condition:
   for x in sequence:
# etc.


Also, print() is a function, you need round brackets (parentheses):

print("x = 1")




-- 
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: What's the best way to minimize the need of run time checks?

2016-08-09 Thread Chris Angelico
On Wed, Aug 10, 2016 at 10:58 AM, Juan Pablo Romero Méndez
 wrote:
>> I don't check for file-likeness in the application code. If somehow a
>> non-file-like (file-unlike?) object makes its way into my function, I
>> expect (and hope) my program will immediately stop and print a nice
>> traceback for me. If I wanted some other behavior, I probably would already
>> have try/except surrounding the appropriate section to deal with not-found
>> or permissions errors. If I need to, I'll add TypeError and AttributeError
>> to that except statement.
>>
>>
> 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".

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


Re: Call for Assistance

2016-08-09 Thread Charles Ross
Well, I’ve been convinced. The license for the book is now Creative Commons 
Attribution-ShareAlike.

https://github.com/chivalry/meta-python/blob/master/LICENSE.md

Thanks,
Chuck

> On Aug 9, 2016, at 3:00 PM, Charles Ross  wrote:
> 
>> CC-BY-NC-SA is not a license for free (as in speech) content. Is that
>> what you want?
> 
> I really appreciate all the conversation about the license. Perhaps I made a 
> mistake with requiring noncommercial in the license, I don’t know, but I’ll 
> look at the links provided about free documentation and free software and see 
> if I can understand the issues more and perhaps make a better decision.
> 
> Honestly, I have no expectation that this book will ever be anything more 
> than a (hopefully) useful resource for others like myself, as described in 
> the current introduction. I might have a remote hope that it would be 
> something that would receive attention from a traditional publisher, but that 
> remote hope is in the same category as a remote hope that a distant rich 
> uncle will someday name me in his well. So, given the minuscule odds of that 
> happening, I’ll probably remove the NC portion. I’ll just do a bit of 
> research first with those links.
> 
> Thanks,
> Chuck
> -- 
> 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-09 Thread Ben Finney
Phil Boutros  writes:

> Ben Finney  wrote:
> > How can I specify which database (by its alias name) a Django
> > ModelForm should use
> 
> > What is the equivalent for using='foo' when instantiating a ModelForm
> > for the model, or calling its methods (ModelForm.clean, ModelForm.save,
> > etc.)?
>
> You would specify it on the save method (using the same
> "using='foo'") paremeter.

Thank you for responding.

> http://stackoverflow.com/questions/37740848/use-a-specific-database-with-a-modelform

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 love and treasure individuals as I meet them, I loathe and |
  `\ despise the groups they identify with and belong to.” —George |
_o__) Carlin, 2007 |
Ben Finney

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


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

2016-08-09 Thread 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".

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


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

2016-08-09 Thread Steven D'Aprano
On Wed, 10 Aug 2016 06:51 am, Juan Pablo Romero Méndez wrote:

> 2016-08-09 13:18 GMT-07:00 Rob Gaddi :

>> It's not a style, it's the absence of one.
>>
>> def add2list(lst, elem):
>> lst.extend([elem, elem])
>> return lst
>>
>> I did all the type checking I needed to there; none whatsoever.  If
>> passed a list, or something that behaves like one, that does the
>> expected thing.
>>
>> If passed an ExtensionLadder, it probably does the wrong thing, but that
>> is no way my problem.
> 
> So as the writer of the function you expect the user to read the function
> body to determine what is safe to pass or not?

No. We expect them to read the docs, which will say something like "expects
a list". Anything that behaves like a list *should* do: if something
behaves like a list, then it might as well be a list, regardless of whether
or not it inherits from the list built-in type or not. This is called Duck
Typing:

"If it quacks like a duck, swims like a duck and walks like a duck, it might
as well be a duck."

If your function wants an object it can call extend on, then why should it
demand a list? It should only care about whether or not the object has an
extend method.

Some functions may be stricter, either from some specific need, or just
because the author is a refugee from a statically typed language who thinks
that they need to type-check *everything*. (May the gods preserve us from
such people.)

In that case, your testing will quickly reveal that a list-like object is
not good enough, only an actual list will do.




-- 
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


ctypes And The WACAH Principle

2016-08-09 Thread Lawrence D’Oliveiro
GvR has said “we’re all consenting adults here”. He was referring to the 
“public/private/protected” access-control business that most object-oriented 
languages require you to go through. But there is another area where I think 
this principle applies to Python, at least in part: low-level access to 
arbitrary memory.

For the most part, Python tries to avoid you having to deal with this. One 
important exception is the wonderful and useful ctypes module 
, which by its very nature, 
requires you to worry about such things. But what’s so nice about ctypes is 
that you can wrap up such concerns in a Pythonic, high-level layer that others 
can easily use without knowing that such machinations are going on behind the 
scenes. However, precisely because of the lack of access control, the 
abstraction is leaky, so users can penetrate it if they feel they have a need 
to, and are prepared to face the consequences--so WACAH very much applies here.

When creating ctypes wrappers for libraries, one often comes across a call to 
the effect of “create object referencing given data”. That is, the caller 
supplies (and manages) the block of memory holding the data. For example, the 
Cairo graphics library has cairo_image_surface_create_for_data 
,
 which treats the supplied block of memory as an array of pixels. Cairo also 
thoughtfully provides additional mechanisms to coordinate between its accesses, 
and those of the caller, to that block of memory.

For the most part, Python doesn’t make it easy to allocate an object and get 
access to its internal memory representation. One interesting exception is 
array.array objects , which have 
the buffer_info() method, directly returning both the address and length of the 
underlying memory block. The documentation for buffer_info() says:

When using array objects from code written in C or C++ (the only way to
effectively make use of this information), it makes more sense to use the
buffer interface supported by array objects.

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.

So, for now, if I want to use Python-allocated memory with lower-level APIs via 
ctypes, the array module remains the way to go.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-09 Thread Steven D'Aprano
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?

You can say that this scenario is "rare", and you're probably right. But
that doesn't justify a statement like it is ABSOLUTELY okay to have runtime
errors (for programming bugs).

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. That's awesome. But its also
unusual. Most software isn't designed to recover from errors quietly and
silently without any fuss.

But generally speaking, the only acceptable run-time errors are those that
occur due to information that are not available until run-time. If the user
insists on passing you an non-existent file name for reading, then what
else are you going to do other than fail with a run-time error?

Anything else is a *bug*, and while bugs are a fact of life, they
aren't "acceptable". As a matter of principle[1], we shouldn't release
software with bugs if they can be detected by either compile-time checks or
unit testing.

*Especially* if the consequences of a run-time bug are severe.





[1] As opposed to a matter of pragmatics.



-- 
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: How do I make a video animation with transparent background?

2016-08-09 Thread Lawrence D’Oliveiro
On Wednesday, August 10, 2016 at 12:44:30 AM UTC+12, Martin Schöön wrote:

> What I have failed to achieve is a graph with a transparent
> background.

While it is possible to render image frames with alpha transparency channels, 
as far as I know none of the motion-video image formats supports transparency. 
They all assume that the image fills the entire frame and completely covers 
anything behind.

You could invent your own player which draws the frames one after another 
itself. Note that the JPEG image format doesn’t support transparency; you would 
have to use a bulkier format like PNG.

Another option would be to have an additional video track where the image is 
interpreted purely as transparency masking information for the “proper” video 
track. You would still need to write your own player to handle this.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-09 Thread Chris Angelico
On Wed, Aug 10, 2016 at 11:47 AM, 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?
>
> You can say that this scenario is "rare", and you're probably right. But
> that doesn't justify a statement like it is ABSOLUTELY okay to have runtime
> errors (for programming bugs).

Let me invert that statement, then, and assert:

It is NEVER acceptable to have runtime errors for programming bugs.

Okay. What does that imply? That *every single programming bug* can be
detected at compile time. Do you want to guarantee that? I certainly
don't, because it would shackle the program to "stuff we can
mathematically verify", which is a lot narrower than "stuff we can
currently code in Python".

So if you can't prove a program to be bug-free at compile time, what
*do* you want to happen with that undetected bug? I want a run-time
error, not a crash and certainly not the wrong behaviour.

Also, you'll notice that I said "the moment you run your program" -
that is, I'm talking about stuff that happens "at run time" but early
on, rather than waiting for data-specific situations. Ideally, you'll
have a test suite that exercises most, if not all, of your code paths,
and running your tests can be done at what's effectively the same as
"compile time" (with the significant difference that you can start
messing with the program without waiting for the tests to finish,
whereas it's annoyingly difficult to run a program that hasn't
finished compiling yet).

I never said that "run-time errors are preferable to compile-time
errors", because they're not; the sooner you catch something, the
better. But if you don't catch it early, yes, I am definitely okay
with a run-time error with a nice exception traceback and so on.

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


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

2016-08-09 Thread Chris Angelico
On Wed, Aug 10, 2016 at 11:50 AM, Lawrence D’Oliveiro
 wrote:
> On Wednesday, August 10, 2016 at 12:44:30 AM UTC+12, Martin Schöön wrote:
>
>> What I have failed to achieve is a graph with a transparent
>> background.
>
> While it is possible to render image frames with alpha transparency channels, 
> as far as I know none of the motion-video image formats supports 
> transparency. They all assume that the image fills the entire frame and 
> completely covers anything behind.
>

Animated GIFs support transparency. Are you happy with the framerates
you can get in a simple GIF?

But this isn't really a Python question.

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


Re: ctypes And The WACAH Principle

2016-08-09 Thread Steven D'Aprano
On Wed, 10 Aug 2016 11:45 am, Lawrence D’Oliveiro wrote:

> GvR has said “we’re all consenting adults here”. He was referring to the
> “public/private/protected” access-control business that most
> object-oriented languages require you to go through. But there is another
> area where I think this principle applies to Python, at least in part:
> low-level access to arbitrary memory.

Well, that's certainly an idiosyncratic position to take. Most people
believe that high-level languages like Python should insulate the
programmer from low-level details, and avoid the risk of memory corruption,
buffer overflows, segmentation faults and core dumps.


[...]
> For the most part, Python doesn’t make it easy to allocate an object and
> get access to its internal memory representation. One interesting
> exception is array.array objects
> , which have the
> buffer_info() method, directly returning both the address and length of
> the underlying memory block. The documentation for buffer_info() says:
> 
> When using array objects from code written in C or C++ (the only way
> to effectively make use of this information), it makes more sense to
> use the buffer interface supported by array objects.
> 
> 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.

I don't understand. The documentation says that the ONLY way to use that
information is from C or C++. You have found no other way to use that
information. How is that "wrong"? How are *you* interpreting that
documentation? What are you trying (and failing) to do?

> So, for now, if I want to use Python-allocated memory with lower-level
> APIs via ctypes, the array module remains the way to go.

I really don't understand what you are trying to say. Perhaps a working code
sample might help.






-- 
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: How do I make a video animation with transparent background?

2016-08-09 Thread Lawrence D’Oliveiro
On Wednesday, August 10, 2016 at 2:04:39 PM UTC+12, Chris Angelico wrote:

> Animated GIFs support transparency.

But they don’t do full colour.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Make sure you removed all debugging print statements error

2016-08-09 Thread Lawrence D’Oliveiro
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 
.
-- 
https://mail.python.org/mailman/listinfo/python-list


cmd prompt does not recognizes python command on Windows 7

2016-08-09 Thread sh . ajay12
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: What's the best way to minimize the need of run time checks?

2016-08-09 Thread Michael Torrie
On 08/09/2016 02:51 PM, Juan Pablo Romero Méndez wrote:
> 2016-08-09 13:18 GMT-07:00 Rob Gaddi :
>> It's not a style, it's the absence of one.
>>
>> def add2list(lst, elem):
>> lst.extend([elem, elem])
>> return lst
>>
>> I did all the type checking I needed to there; none whatsoever.  If
>> passed a list, or something that behaves like one, that does the
>> expected thing.
>>
>> If passed an ExtensionLadder, it probably does the wrong thing, but that
>> is no way my problem.
>>
> 
> 
> So as the writer of the function you expect the user to read the function
> body to determine what is safe to pass or not?

There are type annotations you can use in Python 3 that can help
document the expected type that a function requires, and that
information can be used by a linter to predict problems.  Since Python
allows duck typing, one could say that my function requires a
"file-like" object.  Not sure how that would be annotated.

Some people go to more formal extremes and rather than just rely on duck
typing they will use Java-style formal interfaces.  So the function
could require an object that implements certain interfaces.
https://pypi.python.org/pypi/zope.interface. I suppose with interfaces,
the function could query the arguments to look for these interfaces. But
really in my mind that's no different than duck typing because if an
object comes in that you can't deal with, that's by definition and
exception anyway. So either Python raises it for you or you raise it
yourself after checking for an interface.  Same effect in my mind.


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


Round-Trip Object Correspondence, Weakrefs And __new__

2016-08-09 Thread Lawrence D’Oliveiro
Qahirah  is a Pythonic API binding for the 
Cairo 2D graphics library. When designing it, I tried to imagine how the 
graphics API would look if had been created for Python in the first place, 
rather than for C.

One of the important decisions I made was to implement the binding in pure 
Python, using ctypes . ctypes is 
a wonderful library, and I recommend it to anyone else looking to create a 
Python API binding.

There was already a commonly-used Python binding for Cairo, called PyCairo, 
written as an extension module in C. But it was missing important features, and 
had been neglected for some time. I did look at filling in some of the gaps 
, before realizing that it would be easier and 
quicker to do it over again in Python.

A common principle in most ctypes bindings is: you encapsulate a lower-level 
API object in a Python object. For example, in Qahirah, a Cairo drawing context 
of type “cairo_t” is wrapped in a Python object of type “Context”. All such 
wrapper objects have an internal field “_cairobj” which contains the pointer to 
the lower-level object.

But sometimes you need to map back the other way. For example, a Context has a 
“source” property, which is the current Pattern to use when setting pixel 
values. (A Pattern can be as simple as a single solid colour, or something more 
complex like a gradient or a repeating image.) In particular, I want the 
following result (assuming “ctx” is a Context and “pat” is a pattern):

>>> ctx.source = pat
  # calls cairo_set_source 

>>> ctx.source is pat
True

One way to achieve this would be by cheating: each such settable property would 
save its value in an internal field, and the property getter would simply 
return this field value, rather than calling the underlying Cairo getter. This 
does run the risk of the Python wrapper getting out of sync with the underlying 
API object...

The better way is for the class to maintain a dictionary mapping low-level 
pointers back to Python objects. The property getter would, in this case, call 
cairo_get_source 
 as 
expected, then look up the result in this dictionary, and return the 
corresponding object.

Entries in this dictionary would automatically be made every time a new object 
was created. To manage this, the object must define a __new__ method 
 to manage 
object creation. Note the following from the docs:

If __new__() returns an instance of cls, then the new instance’s
__init__() method will be invoked.

Since in this case __new__ might be returning either a new object or an 
existing one, you only want initialization to be done in the former case, not 
the latter one. But the __new__ method is the only place which knows which is 
the case; therefore it has to handle all the initialization, you cannot have an 
__init__ method at all.

However, the problem with such a dictionary is that it leads to memory leaks: 
without some way of clearing entries from it, objects will never get deleted, 
so if the caller creates and forgets lots of them, memory usage will grow and 
grow. Clearing out unneeded objects is not a chore that the caller should have 
to manage.

Luckily, Python, in common with most sanely-defined dynamic languages, supports 
something called “weak” references 
. These are object references 
that do not count towards the usual reference management, so they do not 
prevent the object from being deleted. In particular, that module defines a 
handy class called a “WeakValueDictionary”, where the values are weak object 
references. This means I can maintain my mapping from low-level API references 
to Python objects, and when the caller forgets about the latter, they will also 
disappear automatically from my mapping.

One minor caveat is that use of weak references will cause an attribute called 
“__weakref__” to be added to your objects. If you define __slots__ (as I have 
done here), you will need to make allowance for this.

So, with all this, the basic object-creation mechanism in the Qahirah wrapper 
classes looks like

_instances = WeakValueDictionary()
  # class variable mapping low-level API references back to Python objects

def __new__(celf, _cairobj) :
self = celf._instances.get(_cairobj)
if self == None :
self = super().__new__(celf)
self._cairobj = _cairobj
... other internal setup ...
celf._instances[_cairobj] = self
else :
cairo.cairo_xxx_destroy(self._cairobj)
  # lose extra reference created by caller
#end if
return \
self
#end __new__


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

2016-08-09 Thread Steven D'Aprano
On Wednesday 10 August 2016 12:49, Lawrence D’Oliveiro wrote:

> On Wednesday, August 10, 2016 at 2:04:39 PM UTC+12, Chris Angelico wrote:
> 
>> Animated GIFs support transparency.
> 
> But they don’t do full colour.

Define "full colour".

GIFs don't directly support 16-bit or 32-bit colours in a single frame, 
but they can support more than 256 colours:

http://webmasters.stackexchange.com/questions/304/can-i-have-an-animated-gif-with-more-than-256-colours-per-frame


including what looks to the naked eye (or at least *my* naked eye) 
effectively like 32-bit colour.



-- 
Steve

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


Re: Make sure you removed all debugging print statements error

2016-08-09 Thread Steven D'Aprano
On Wednesday 10 August 2016 12:53, 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
> .



That is nice, for bicameral languages like English and French, although one 
does need to watch out for German ß (eszett) which makes it lossy.


-- 
Steve

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


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

2016-08-09 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.
>
>

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?




>
> [...]
> > 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".
>
> 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


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

2016-08-09 Thread Rustom Mody
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!
-- 
https://mail.python.org/mailman/listinfo/python-list


free python course materials for high school students available

2016-08-09 Thread Kent Tong
Hi,

I've taught a python course with some positive results to high school students 
with zero experience in programming. Now I am making these course materials 
(slides in English and lecture videos in Cantonese) freely available as a 
contribution back to the community 
(http://www.istudycenter.org/summercourses/programming-for-kids/python-materials).

Hope you find them useful :-)

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


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

2016-08-09 Thread Dale Marvin via Python-list

On 8/9/16 6:50 PM, Lawrence D’Oliveiro wrote:

On Wednesday, August 10, 2016 at 12:44:30 AM UTC+12, Martin Schöön wrote:


What I have failed to achieve is a graph with a transparent
background.


While it is possible to render image frames with alpha transparency channels, 
as far as I know none of the motion-video image formats supports transparency. 
They all assume that the image fills the entire frame and completely covers 
anything behind.

You could invent your own player which draws the frames one after another 
itself. Note that the JPEG image format doesn’t support transparency; you would 
have to use a bulkier format like PNG.

Another option would be to have an additional video track where the image is 
interpreted purely as transparency masking information for the “proper” video 
track. You would still need to write your own player to handle this.



Another option in the Python world is Vapoursynth:



I runs with Cython and generally pipes to something like ffmpeg which 
creates the final video file so it may be a more complex than you want 
but it is capable of the processing you are asking for and in a Python 
language syntax.


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


Re: Python slang

2016-08-09 Thread Lawrence D’Oliveiro
On Monday, August 8, 2016 at 9:02:15 PM UTC+12, Anders J. Munch wrote:

> It was only later that language designers fell into the notion that it
> was crucial for a new language's success to look as much like C++ as
> possible.

Nevertheless, Python copied the C misfeature of using “=” for assignment and 
“==” for equality comparison, instead of the more 1337 mathy CompSci tradition 
of “:=” for the former and “=” for the latter.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-09 Thread Michael Selik
On Wed, Aug 10, 2016 at 1:22 AM Juan Pablo Romero Méndez <
[email protected]> wrote:

> 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.
>

I suggest focusing on #4, learn to appreciate/enjoy runtime errors. When
you've come around to that way of thinking, you'll have better judgement on
when to care about catching exceptions vs letting them stop your program.

As for writing as many tests as possible... eh. Think about your code
scientifically. You can disprove a falsifiable hypothesis, but you can't
prove a hypothesis. Sometimes you can formally prove the correctness of
your code via logical deduction, but that's not testing. Sometimes you can
exhaustively test all expected inputs, but it's impossible to exhaustively
test the infinity of possible inputs. So write tests as if you're
presenting evidence for a theory.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-09 Thread 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.

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


Re: ctypes And The WACAH Principle

2016-08-09 Thread Gregory Ewing

Lawrence D’Oliveiro wrote:
When using array objects from code written in C or C++ (the only way to 
effectively make use of this information), it makes more sense to use the 
buffer interface supported by array objects.


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.


I think you must be misreading it. It's talking about accessing
the internals of an object from C or C++ code, *not* from Python.

If I understand correctly, you want to get the address of an
object's memory as a Python integer, so that you can pass it to
something via a ctypes wrapper?

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.

You could also consider using Cython instead of ctypes. It will
let you create non-leaky wrappers and provide easy access to
buffer interfaces.

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


Re: Subprocess Startup Error

2016-08-09 Thread Lawrence D’Oliveiro
On Wednesday, August 10, 2016 at 8:12:10 AM UTC+12, [email protected] wrote:

> "Either IDLE can't start a subprocess or personal firewall software is
> blocking the connection."

Q: Why does Windows print such explanatory error messages?
A: To give you a hint as to the cause of the problem.
-- 
https://mail.python.org/mailman/listinfo/python-list


Call for Assistance

2016-08-09 Thread Charles Ross
I’ve begun a new book called Meta Python that I’m looking for assistance with. 
The book is one I wish was out there, and so am writing. The book is targeted 
at experienced programmers who are novice Python users and want to move from 
that point to one where they have published to PyPI.

I’m specifically looking for three types of users to assist me:

- Expert Python developers who are willing to confirm and correct the technical 
details, since I’m getting to the material that I want to learn and therefore 
need to teach. So far that only really includes virtual environments, covered 
in chapter 2, but will soon include many other topics.
- Developers like me, who aren’t new to programming but are perhaps new to 
Python and certainly new to publishing to PyPI, and who can provide feedback on 
the clarity and usefulness of the content.
- Linux and Windows Python users who would be willing to assist in making the 
book a bit less Mac-centric.

The book is being hosted at https://github.com/chivalry/meta-python 
. The introduction and first two 
chapters have their initial drafts complete, but you’ll see a few “TODOs” in 
there. Any and all feedback is appreciated, and preferred through the GitHub 
Issues system, but feel free to email me if you prefer.

Thanks,
Chuck

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


Re: Running Python from the source repo

2016-08-09 Thread Terry Reedy

On 8/8/2016 5:16 PM, Zachary Ware wrote:

On Mon, Aug 8, 2016 at 2:25 PM, Terry Reedy  wrote:

Last January, I wrote a batch file to build all three versions with the
'optional' extensions.  I started rebuilding more often after this.

36\pcbuild\build.bat -e -d
35\pcbuild\build.bat -e -d
27\pcbuild\build.bat -e -d

Thanks for making this possible.  It initially worked, but now it stops
after the first command, even without errors.  Has a flag been changed to
treat warnings as errors?  How can I change the .bat to wrap each command
with the equivalent of try: except: pass?


I'm not sure why that would have stopped working, but the way to use a
.bat from a .bat is to 'call' it:

call 36\PCbuild\build.bat -e -d

.bat scripts don't care about the exit codes of what they run, errors
must be explicitly checked and 'exit' called if you want the script to
die early.  Try this for an unconditional build on all three branches,
with a report at the end if any of them failed:

call 36\PCbuild\build.bat -e -d
set rc36=%ERRORLEVEL%

call 35\PCbuild\build.bat -e -d
set rc35=%ERRORLEVEL%

call 27\PCbuild\build.bat -e -d
set rc27=%ERRORLEVEL%

@if %rc36% NEQ 0 (
echo 3.6 build failed, rc: %rc36%
)
@if %rc35% NEQ 0 (
echo 3.5 build failed, rc: %rc35%
)
@if %rc27% NEQ 0 (
echo 2.7 build failed, rc: %rc27%
)


This works great.  Might there be any way to collect together
the warning messages?  There were perhaps 100 for the changes in
the last few weeks.  (People on non-windows seems to routinely write 
code that msc does not like.)


--
Terry Jan Reedy

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


Re: Running Python from the source repo

2016-08-09 Thread Terry Reedy

On 8/8/2016 5:25 PM, Random832 wrote:

On Mon, Aug 8, 2016, at 15:25, Terry Reedy wrote:

Last January, I wrote a batch file to build all three versions with the
'optional' extensions.  I started rebuilding more often after this.

36\pcbuild\build.bat -e -d
35\pcbuild\build.bat -e -d
27\pcbuild\build.bat -e -d

Thanks for making this possible.  It initially worked, but now it stops
after the first command, even without errors.  Has a flag been changed
to treat warnings as errors?  How can I change the .bat to wrap each
command with the equivalent of try: except: pass?


I'm not sure how it ever worked, but you have to use "call" to run one
batch file from another batch file, otherwise it doesn't start a
recursive command interpreter and so it won't run anything else in the
outer batch file.


Perhaps I mis-remember.  Zack's revision using call works.

--
Terry Jan Reedy

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


Re: Awful code of the week

2016-08-09 Thread Steven D'Aprano
On Tuesday 09 August 2016 15:37, Chris Angelico wrote:

[...]
>> You can't be too careful with memory management.
> 
> Right. Of course, it gets very onerous, so we tend to use a context
> manager instead.
> 
> def process(self, stuff):
> with deallocate() as cleanup:
> cleanup(self)
> cleanup(stuff)
> files = self.files
> cleanup(files)
> del files
> files = []
> cleanup(files)
> files = self.files
> cleanup(files)
> for file in files:
> cleanup(file)
> file.process(stuff)
> return 1
> 
> There, isn't that so much better?

Ha! That's why I never use context managers -- they're too easy to write buggy 
code. You forgot to deallocate cleanup itself! Memory leak!



-- 
Steve

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


Re: Call for Assistance

2016-08-09 Thread Steven D'Aprano
On Tuesday 09 August 2016 11:52, Charles Ross wrote:

> I’ve begun a new book called Meta Python that I’m looking for assistance
> with.

Are you looking for people to be co-authors? Are you offering payment, or 
credit? A free copy of the book? A kick to the head? Or just looking for 
volunteers to provide content for the book while you lie back on a beach in the 
Bahamas collecting the millions[1] in royalties?





[1] Zimbabwean dollars.

-- 
Steve

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


Re: Call for Assistance

2016-08-09 Thread Charles Ross
You’re right. I should have mentioned that this is a volunteer effort on my 
part and anyone else who becomes involved. It’s being released under the 
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License 
(http://creativecommons.org/licenses/by-nc-sa/4.0/ 
) as demonstrated by the 
LICENSE.md file at the GitHub repository 
(https://github.com/chivalry/meta-python/blob/master/LICENSE.md 
).

I don’t yet know precisely how the “book” will be published. I might just leave 
it as a set of GitHub files or export the Markdown to other format to be hosted 
outside GitHub. I’m not worrying yet about publishing, only clarity and 
correctness of content.

Thanks,
Chuck

> On Aug 9, 2016, at 1:17 AM, Steven D'Aprano 
>  wrote:
> 
> On Tuesday 09 August 2016 11:52, Charles Ross wrote:
> 
>> I’ve begun a new book called Meta Python that I’m looking for assistance
>> with.
> 
> Are you looking for people to be co-authors? Are you offering payment, or 
> credit? A free copy of the book? A kick to the head? Or just looking for 
> volunteers to provide content for the book while you lie back on a beach in 
> the 
> Bahamas collecting the millions[1] in royalties?
> 
> 
> 
> 
> 
> [1] Zimbabwean dollars.
> 
> -- 
> Steve
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


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

2016-08-09 Thread berteh
Hello.

My python script should run on Linux, Win and MacOS, but I can't find a way to 
have logging.conf configuration that works out of the box accross all these 
OSes.

I don't want my users to have to edit configuration files manually.


If I use a "linux-like" handler configuration such as below it fails on Windows 
when the script is run from a directory from which the user has no write access 
(ie any subfolder or C:\Program Files, that is the default install location)

[handler_file]
class=handlers.RotatingFileHandler
args=('scribusGenerator.log','a','maxBytes=1000','backupCount=1')

And if I use a "windows-like" file location it fails (of course) in Linux and 
MacOSX

[handler_file]
class=handlers.RotatingFileHandler
args=('D:\scribusGenerator.log','a','maxBytes=1000','backupCount=1')


One option would be to set the location dynamically in the system temp 
directory (from tempfile.gettempdir())... but I can't find a way to change the 
location of the handler loaded via config module.

Any piece of advice on how to achieve cross-platform logging that does not 
require a modification by the user?

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


EuroPython 2017 On-site Teams: Call for Interest results

2016-08-09 Thread M.-A. Lemburg
We are happy to announce the results of our EuroPython 2017 On-site
Teams: Call for Interest (CFI). The CFI finished with exciting new
locations and on-site teams willing to make EuroPython 2017 possible.

The CFI proposals we received were for:

 * Milan or Como Lake, Italy,
   from the Python Italia Society (APS),

 * Brno, the Czech Republic,
   from the local Brno Python community, and

 * Bilbao, Basque Country, Spain,
   from the Python San Sebastián Society (ACPySS).

We are now reviewing the proposals and will then move forward with the
more formal and detailed CFP later in September.

Enjoy,
--
EuroPython Society
http://www.europython-society.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


EuroPython 2016: Thank you to all our organizers and volunteers

2016-08-09 Thread M.-A. Lemburg
EuroPython 2016 is now over and was an overwhelming success thanks to
everyone who helped make it happen.

Since EuroPython would not be possible without the help of our
volunteers and organizers, we’d like to say

 Thank You !!!

to all the individuals who invested time and effort into the event.

Please see our blog post for the full list of people who have
helped:


http://blog.europython.eu/post/148685512862/europython-2016-thank-you-to-all-our-organizers

If you are interested in helping with EuroPython 2017, we invite you
to join the workgroups. Please go to the EuroPython Workgroups page:

http://www.europython-society.org/workgroups

and follow the instructions there to get signed up. Many thanks !


With gravitational regards,
--
EuroPython 2016 Team
http://ep2016.europython.eu/
http://www.europython-society.org/


PS: Please forward or retweet to help us reach all interested parties:
https://twitter.com/europython/status/762968990658523137
Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-09 Thread Steven D'Aprano
On Tue, 9 Aug 2016 08:56 pm, [email protected] wrote:

> Hello.
> 
> My python script should run on Linux, Win and MacOS, but I can't find a
> way to have logging.conf configuration that works out of the box accross
> all these OSes.

Oh! Wait, I didn't pick up on the logging.conf part! D'oh!

> [handler_file]
> class=handlers.RotatingFileHandler
> args=('scribusGenerator.log','a','maxBytes=1000','backupCount=1')

Try using '~/scribusGenerator.log' to specify the user's home directory.




-- 
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: cross-platform logging.config: how to set cross platform log (handlers) file location?

2016-08-09 Thread Steven D'Aprano
On Tue, 9 Aug 2016 08:56 pm, [email protected] wrote:

> Hello.
> 
> My python script should run on Linux, Win and MacOS, but I can't find a
> way to have logging.conf configuration that works out of the box accross
> all these OSes.
> 
> I don't want my users to have to edit configuration files manually.
> 
> 
> If I use a "linux-like" handler configuration such as below it fails on
> Windows when the script is run from a directory from which the user has no
> write access (ie any subfolder or C:\Program Files, that is the default
> install location)
> 
> [handler_file]
> class=handlers.RotatingFileHandler
> args=('scribusGenerator.log','a','maxBytes=1000','backupCount=1')

That will fail on Linux too, if the current working directory is somewhere
that the user doesn't have write permission.

The *right* way to do this is:

- allow the user to optionally specify a log file location;

- if they don't, set a sensible default according to the platform, e.g.:


# untested
DEFAULTS = {
'nt': '...',
'linux': '...',
'macos': '...',
}
location = DEFAULTS.get(sys.platform, 'somewhere else')

Read the docs for the list of possible values for sys.platform.

Generally, for system tools, you should write to a system-wide location. On
Linux, that might be /var/log/FILENAME. But that's not generally writable
by unprivileged users, so you might want to log directly in the user's home
directory:

os.path.expanduser('~/FILENAME')

which ought to do the right thing on Windows, POSIX systems (including Linux
and OS X).



-- 
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


cropping a random part of an image

2016-08-09 Thread drewes . mil
Hi,

I'm new to python and I have 30.000 pictures.
I need to crop, let's say 100, parts of 256x256, randomly out of every picture.

I cant find an answer in the net, would be nice if someone could help me out!

Thanks!

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


How do I make a video animation with transparent background?

2016-08-09 Thread Martin Schöön
Here is what I want to do: I want to create an animated graph and
store it as a video file, mp4 say. This file will then be used as
an overlay on another video.

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.

Another thing I would like to do is to present numerical data
in the video or as a separate video -- think LCD display reading
out numbers up-dated every second. Once again preferably on a
transparent background. Here I have covered no ground :-(

TIA,

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


Question regarding stdlib distutils strtobool behavior

2016-08-09 Thread Joseph Bane
Hello.

It recently came to my attention that the strtobool function in the standard 
library doesn't return Python native boolean values, but rather returns integer 
0 or 1:

https://hg.python.org/cpython/file/3.5/Lib/distutils/util.py#l304

I am curious why this is the defined behavior and whether anyone can fill me in 
regarding this approach. For clarity, I would expect the code to `return True` 
and `return False` rather than `return 1` and `return 0`.

Thanks,
Joe
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-09 Thread Peter Otten
[email protected] wrote:

> Hello.
> 
> My python script should run on Linux, Win and MacOS, but I can't find a
> way to have logging.conf configuration that works out of the box accross
> all these OSes.
> 
> I don't want my users to have to edit configuration files manually.
> 
> 
> If I use a "linux-like" handler configuration such as below it fails on
> Windows when the script is run from a directory from which the user has no
> write access (ie any subfolder or C:\Program Files, that is the default
> install location)
> 
> [handler_file]
> class=handlers.RotatingFileHandler
> args=('scribusGenerator.log','a','maxBytes=1000','backupCount=1')
> 
> And if I use a "windows-like" file location it fails (of course) in Linux
> and MacOSX
> 
> [handler_file]
> class=handlers.RotatingFileHandler
> args=('D:\scribusGenerator.log','a','maxBytes=1000','backupCount=1')
> 
> 
> One option would be to set the location dynamically in the system temp
> directory (from tempfile.gettempdir())... but I can't find a way to change
> the location of the handler loaded via config module.
> 
> Any piece of advice on how to achieve cross-platform logging that does not
> require a modification by the user?

args is passed to eval(), so

args=("D:\scribusGenerator.log" if sys.platform == "win32" else 
"scribusGenerator.log", ...)

should work.

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


Re: Question regarding stdlib distutils strtobool behavior

2016-08-09 Thread Michael Selik
On Tue, Aug 9, 2016 at 9:26 AM Joseph Bane  wrote:

> Hello.
>
> It recently came to my attention that the strtobool function in the
> standard library doesn't return Python native boolean values, but rather
> returns integer 0 or 1:
>
> https://hg.python.org/cpython/file/3.5/Lib/distutils/util.py#l304
>
> I am curious why this is the defined behavior and whether anyone can fill
> me in regarding this approach. For clarity, I would expect the code to
> `return True` and `return False` rather than `return 1` and `return 0`.
>

I'll take a guess: it's probably a(n) historical artifact. Before there
were bools, returning 1 or 0 was the obvious technique. Even after bools
were added to Python 2, returning the literal 1 or 0 was faster than
looking up the names True or False. Now that True and False are keywords,
using the keyword is the obvious solution.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cropping a random part of an image

2016-08-09 Thread Peter Otten
[email protected] wrote:

> I'm new to python and I have 30.000 pictures.
> I need to crop, let's say 100, parts of 256x256, randomly out of every
> picture.
> 
> I cant find an answer in the net, would be nice if someone could help me
> out!

You can walk over the files with

https://docs.python.org/dev/library/os.html#os.walk

find out the image size and process the image with pillow

http://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.size
http://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.crop

using

https://docs.python.org/dev/library/random.html

to determine what part of an image you want to pick.

Now write some code and come back here if you run into problems you cannot 
solve yourself.

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


Re: Question regarding stdlib distutils strtobool behavior

2016-08-09 Thread Joseph Bane
On Tuesday, August 9, 2016 at 9:34:44 AM UTC-4, Michael Selik wrote:
> On Tue, Aug 9, 2016 at 9:26 AM Joseph Bane wrote:
> 
> > Hello.
> >
> > It recently came to my attention that the strtobool function in the
> > standard library doesn't return Python native boolean values, but rather
> > returns integer 0 or 1:
> >
> > https://hg.python.org/cpython/file/3.5/Lib/distutils/util.py#l304
> >
> > I am curious why this is the defined behavior and whether anyone can fill
> > me in regarding this approach. For clarity, I would expect the code to
> > `return True` and `return False` rather than `return 1` and `return 0`.
> >
> 
> I'll take a guess: it's probably a(n) historical artifact. Before there
> were bools, returning 1 or 0 was the obvious technique. Even after bools
> were added to Python 2, returning the literal 1 or 0 was faster than
> looking up the names True or False. Now that True and False are keywords,
> using the keyword is the obvious solution.

(I am sorry if this is a duplicate; the reply didn't seem to make it through 
earlier...)

Thank you for your note. I was thinking it might be just a historical artifact. 
Do you think this is the type of thing we could propose be changed in the next 
version of Python? It seems it would be more consistent with the principle of 
least surprise from the perspective of outsider Python calling code. I would be 
happy to submit a small patch for this change.

As a side note, we discovered this with some code that was checking `if var is 
True` instead of just `if var`. I know this particular style choice isn't 
really dictated by the community (as opposed to something like `if var is None` 
that is), but it seems the latter option is the (generally) preferred approach. 
Basically, it seemed surprising that the former approach failed in our specific 
scenario because of how the `is` operator works:

>>> 1 == True
True
>>> 0 == False
True
>>> 1 is True
False
>>> 0 is False
False

I am definitely not arguing that this behavior be changed, but just putting the 
larger issue into context.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question regarding stdlib distutils strtobool behavior

2016-08-09 Thread Lutz Horn

Am 08/09/2016 um 03:22 PM schrieb Joseph Bane:

It recently came to my attention that the strtobool function in the
standard library doesn't return Python native boolean values, but
rather returns integer 0 or 1:


In boolean context, 1 is True and 0 is False. So you should be able to 
use the result without problems.


Why the implementors of Python wrote strtobool this way is something I 
don't know.


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


Re: Call for Assistance

2016-08-09 Thread Lutz Horn

Am 08/09/2016 um 03:52 AM schrieb Charles Ross:

The book is being hosted at https://github.com/chivalry/meta-python


CC-BY-NC-SA is not a license for free (as in speech) content. Is that
what you want?

Lutz

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


Re: Question regarding stdlib distutils strtobool behavior

2016-08-09 Thread Joseph Bane
Thank you for your note. I was thinking it might be just a historical
artifact. Do you think this is the type of thing we could propose be
changed in the next version of Python? It seems it would be more consistent
with the principle of least surprise from the perspective of outsider
Python calling code. I would be happy to submit a small patch for this
change.

As a side note, we discovered this with some code that was checking `if var
is True` instead of just `if var`. I know this particular style choice
isn't really dictated by the community (as opposed to something like `if
var is None` that is), but it seems the latter option is the (generally)
preferred approach. Basically, it seemed surprising that the former
approach failed in our specific scenario because of how the `is` operator
works:

>>> 1 == True
True
>>> 0 == False
True
>>> 1 is True
False
>>> 0 is False
False

I am definitely not arguing that this behavior be changed, but just putting
the larger issue into context.

On Tue, Aug 9, 2016 at 9:34 AM, Michael Selik 
wrote:

> On Tue, Aug 9, 2016 at 9:26 AM Joseph Bane  wrote:
>
>> Hello.
>>
>> It recently came to my attention that the strtobool function in the
>> standard library doesn't return Python native boolean values, but rather
>> returns integer 0 or 1:
>>
>> https://hg.python.org/cpython/file/3.5/Lib/distutils/util.py#l304
>>
>> I am curious why this is the defined behavior and whether anyone can fill
>> me in regarding this approach. For clarity, I would expect the code to
>> `return True` and `return False` rather than `return 1` and `return 0`.
>>
>
> I'll take a guess: it's probably a(n) historical artifact. Before there
> were bools, returning 1 or 0 was the obvious technique. Even after bools
> were added to Python 2, returning the literal 1 or 0 was faster than
> looking up the names True or False. Now that True and False are keywords,
> using the keyword is the obvious solution.
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Call for Assistance

2016-08-09 Thread Reto Brunner
What on earth isn't "free" enough about

You are free to:
Share — copy and redistribute the material in any medium or format

Adapt — remix, transform, and build upon the material

The licensor cannot revoke these freedoms as long as you follow the license
terms.

It is even a viral (copy left) licence, so even a fsf member should be happy

On Tue, Aug 9, 2016, 16:59 Lutz Horn  wrote:

> Am 08/09/2016 um 03:52 AM schrieb Charles Ross:
> > The book is being hosted at https://github.com/chivalry/meta-python
>
> CC-BY-NC-SA is not a license for free (as in speech) content. Is that
> what you want?
>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question regarding stdlib distutils strtobool behavior

2016-08-09 Thread Michael Selik
On Tue, Aug 9, 2016 at 9:50 AM Joseph Bane  wrote:

> Thank you for your note. I was thinking it might be just a historical
> artifact.
>

That was just my guess. You can search the mailing list archives and the
bug tracker to find out if there's been any past discussion.


> Do you think this is the type of thing we could propose be changed in the
> next version of Python? It seems it would be more consistent with
> the principle of least surprise from the perspective of outsider Python
> calling code. I would be happy to submit a small patch for this change.
>

Seems non-controversial enough that you could write a bug on
http://bugs.python.org/ and then submit a patch. The question is whether or
not you can convince someone with commit access to push your change.

Even if your patch improves the code, it might not improve it enough to
justify change. There are some disadvantages to code churn.

If I were revising it, it'd be something like this...

def strtobool(val):
"""Convert a string representation of truth to true (1) or false
(0).

True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'.  Raises ValueError if
'val' is anything else.
"""
val = val.casefold()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return True
if val in ('n', 'no', 'f', 'false', 'off', '0'):
return False
raise ValueError(f'invalid truth value {val}')

For unicode, str.casefold is better than str.lower for caseless comparison. The
elif and else were unnecessary because of the returns. For fun, I thought
I'd use the new string formatting style (
https://www.python.org/dev/peps/pep-0498/).

Again, I'm not sure if those changes are worthwhile.


As a side note, we discovered this with some code that was checking `if var
> is True` instead of just `if var`.
>

The Pythonic style is to simply check ``if var`` rather than ``if var is
True``. This realizes the flexibility of duck typing -- it doesn't matter
whether the variable is True, so long as it is true (eg. has a non-false
truth value).

The core developers might thus say there's no bug here, because 1 is true.



My apologies for forgetting to use interleaved response style previously.
Google Inbox defaults to top-posting.


On Tue, Aug 9, 2016 at 9:34 AM, Michael Selik 
wrote:

> On Tue, Aug 9, 2016 at 9:26 AM Joseph Bane  wrote:
>>
>>> Hello.
>>>
>>> It recently came to my attention that the strtobool function in the
>>> standard library doesn't return Python native boolean values, but rather
>>> returns integer 0 or 1:
>>>
>>> https://hg.python.org/cpython/file/3.5/Lib/distutils/util.py#l304
>>>
>>> I am curious why this is the defined behavior and whether anyone can
>>> fill me in regarding this approach. For clarity, I would expect the code to
>>> `return True` and `return False` rather than `return 1` and `return 0`.
>>>
>>
>> I'll take a guess: it's probably a(n) historical artifact. Before there
>> were bools, returning 1 or 0 was the obvious technique. Even after bools
>> were added to Python 2, returning the literal 1 or 0 was faster than
>> looking up the names True or False. Now that True and False are keywords,
>> using the keyword is the obvious solution.
>>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Call for Assistance

2016-08-09 Thread Michael Selik
On Tue, Aug 9, 2016 at 3:23 AM Charles Ross  wrote:

> I’ve begun a new book called Meta Python that I’m looking for assistance
> with. The book is one I wish was out there, and so am writing. The book is
> targeted at experienced programmers who are novice Python users and want to
> move from that point to one where they have published to PyPI.
>

Maybe you should add a chapter discussing community, open-source,
volunteerism, and how difficult it is to get enthusiastic, productive
contributors, but how easy it is to get emailed criticism?
(no cynicism intended)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Running Python from the source repo

2016-08-09 Thread Zachary Ware
On Tue, Aug 9, 2016 at 2:55 AM, Terry Reedy  wrote:
> This works great.  Might there be any way to collect together
> the warning messages?  There were perhaps 100 for the changes in
> the last few weeks.  (People on non-windows seems to routinely write code
> that msc does not like.)

Glad it works!  Collecting warning messages is less simple, you'd
probably get the best results from writing a Python script to drive
the build.bats and do what you want with the output.  You can also
pass '-v' to build.bat to get more verbose output, which includes a
summary of warnings and errors at the end of each build.

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


Re: Call for Assistance

2016-08-09 Thread Chris Angelico
On Wed, Aug 10, 2016 at 1:42 AM, Reto Brunner  wrote:
> What on earth isn't "free" enough about
>
> You are free to:
> Share — copy and redistribute the material in any medium or format
>
> Adapt — remix, transform, and build upon the material
>
> The licensor cannot revoke these freedoms as long as you follow the license
> terms.

But NOT "use commercially". It's a restricted freedom that needs to be
bound up with that restriction forever afterward.

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


Re: Call for Assistance

2016-08-09 Thread Jussi Piitulainen
Reto Brunner  writes:

> On Tue, Aug 9, 2016, 16:59 Lutz Horn  wrote:
>
>> Am 08/09/2016 um 03:52 AM schrieb Charles Ross:
>> > The book is being hosted at https://github.com/chivalry/meta-python
>>
>> CC-BY-NC-SA is not a license for free (as in speech) content. Is that
>> what you want?
>
> What on earth isn't "free" enough about
>
> You are free to:
> Share — copy and redistribute the material in any medium or format
>
> Adapt — remix, transform, and build upon the material
>
> The licensor cannot revoke these freedoms as long as you follow the
> license terms.
>
> It is even a viral (copy left) licence, so even a fsf member should be
> happy

The FSF does not consider non-commercial licenses free. Check their
entries on licensing and philosophy at http://www.fsf.org/.

Licensing / Licenses -> http://www.fsf.org/licensing/licenses (ends up
at https://www.gnu.org/licenses/license-list.html, contains an entry on
CC-NC at https://www.gnu.org/licenses/license-list.html#CC-BY-NC)

Philosophy / The Free Software Definition ->
https://www.gnu.org/philosophy/free-sw.html

Philosophy / Selling Free Software ->
https://www.gnu.org/philosophy/selling.html

If Charles Ross gets significant contributions from others under the NC
license, I think even he would be unable to sell copies or use the book
in paid courses, unless he gets other contributors to assign their
copyrights to him so that he is not bound by the license himself. Is
that what he wants? Is that what other contributors want? Why?
-- 
https://mail.python.org/mailman/listinfo/python-list