Re: Python, C++ interaction

2014-12-04 Thread Jeremy Sanders
Michael Kreim wrote:

 What are you using to wrap C++ classes for Python?

I'm using SIP, as it fits nicely with my PyQt user interface. 
http://www.riverbankcomputing.com/software/sip/intro

It's a pretty flexible and fast way of wrapping C++ and C.

If you want to pass numpy arrays and such, it requires a bit more work, 
however.

Jeremy


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


Re: Python, C++ interaction

2014-12-04 Thread Christian Gollwitzer

Am 03.12.14 09:29, schrieb Michael Kreim:

I did some googleing on extending Python by C++ code but I did not find
something that satisfies me. I gave SWIG a try, but several webpages
disadvised me of using it. Also my small experiments did not work.


I don't know why SWIG is discouraged; in my experience, it is a very 
easy way to generate the wrapping code around C++ classes, and also 
flexible in many directions. It can be easily extended for new datatypes 
(there is a numpy interface module, for instance), and also generates 
code for other scripting languages as well. What specifically did you 
try, and what failed?


Concerning the callbacks, I don't know about these, but hopefully you 
are aware that you can easily loose the C++ speed advantage by using 
Python lambdas, since those have to go through the Python interpreter.


Christian

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


Re: Most gratuitous comments

2014-12-04 Thread Albert van der Horst
In article 546d7505$0$12899$c3e8da3$54964...@news.astraweb.com,
Steven D'Aprano  st...@pearwood.info wrote:
And the award for the most gratuitous comments before an import goes to
one of my (former) workmates, who wrote this piece of code:

# Used for base64-decoding.
import base64
# Used for ungzipping.
import gzip

The comment lines contain genuine information. The program is
decoding or gunzipping. (And apparently not doing the encoding part)

This information may have been better conveyed by

from base64 import base64-decode
from gzip import gunzip

but anyway.

Also the comment may be misleading, but I think not.

If there are mysterious names for packages, the comment may be
actually useful.

# Auxiliary for the reverse recursion to calculate
# Chebychev coefficients.
import courseware-2014-ch11


A professor who demands from students that every import is documented
is IMO not to blame.
In a company's coding convention ... I've seen a lot of things there
that make a lot less sense.

--
Steven
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spearc.xs4all.nl =n http://home.hccnet.nl/a.w.m.van.der.horst

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


Re: Python, C++ interaction

2014-12-04 Thread Sturla Molden
Dan Stromberg drsali...@gmail.com wrote:

 1) writing in Cython+CPython (as opposed to wrapping C++ with Cython)

That is an option, but it locks the code to Cython and CPython forever. C
and C++ are at least semi-portable.

 2) using numba+CPython (It's a pretty fast decorator - I've heard it's
 faster than Cython)

Numba is usually not faster than Cython (Cython can be as fast as C), but
it can be pretty fast. Sometimes it is comparable to -O2 in C for the
subset of Python it supports, but usually a bit slower. But if you can use
it, it is easier to use than Cython. There are no extra compilation steps,
etc. Just add a couple of decorators to the Python code and it takes off
like a rocket. For anyone who are familiar with PyPy and Psyco, Numba is
far better than those. It is a Python JIT compiler that often can perform
better than the Java VM. Numba will also JIT-compile Python code that uses
ctypes or cffi to call external libraries down to almost zero overhead.

You forgot to mention using Fortran and f2py. Many scientists and engineers
prefer Fortran to C and C++ because it is easier to use. And Fortran 90 and
later standards are not anything like the loathed Fortran 66 and 77
languages. Fortran is a high-level language particularly suited for
numerical computing, C is a semi-portable high-level assembler. 

Sturla

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


Re: How is max supposed to work, especially key.

2014-12-04 Thread Albert van der Horst
In article mailman.16378.1417111312.18130.python-l...@python.org,
Peter Otten  __pete...@web.de wrote:
Albert van der Horst wrote:

 In the Rosetta code I come across this part of
 LU-decomposition.

 def pivotize(m):
 Creates the pivoting matrix for m.
 n = len(m)
 ID = [[float(i == j) for i in xrange(n)] for j in xrange(n)]
 for j in xrange(n):
 row = max(xrange(j, n), key=lambda i: abs(m[i][j]))
 if j != row:
 ID[j], ID[row] = ID[row], ID[j]
 return ID

 That it's using a cast from boolean to float and using
 at the other moment a float as a boolean, suggest that this
 code is a bit too clever for its own good, but anyway.

 My problem is with the max. I never saw a max with a key.

 In my python help(max) doesn't explain the key. It says that
 max can handle an iterator (I didn't know that), and you can
 pass and optional key=func, but that's all.

 I expect it to be something like
   elements in the iterator are taken into account only if the
   key applied to the iterator evaluates to a True value.

 However that doesn't pan out:
 
 max(xrange(100,200), key=lambda i: i%17==0 )
 102
 

 I expect the maximum number that is divisible by 17 in the
 range, not the minimum.

 Can anyone shed light on this?

Given a function f() max(items, key=f) returns the element of the `items`
sequence with the greatest f(element), e. g. for

max([a, bcd, ef], key=len)

the values 1, 3, 2 are calculated and the longest string in the list is
returned:

 max([a, bcd, ef], key=len)
'bcd'

If there is more than one item with the maximum calculated the first is
given, so for your attempt

max(xrange(100,200), key=lambda i: i%17==0 )


the values False, False, True, False, ... are calculated and because

 True  False
True

the first one with a True result is returned.


So in that case max doesn't return the maximum (True), but instead
something else.

Useful as that function may be, it shouldn't have been called max.

I don't blame myself for being misled.

Groetjes Albert
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spearc.xs4all.nl =n http://home.hccnet.nl/a.w.m.van.der.horst

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


Re: Python handles globals badly.

2014-12-04 Thread jtan
How can Skybuck use so much globals. Wouldn't that introduce a lot of
thread safety problems?

On Thu, Dec 4, 2014 at 9:32 AM, Mark Lawrence breamore...@yahoo.co.uk
wrote:

 On 03/12/2014 23:02, Skybuck Flying wrote:



 Mark Lawrence  wrote in message
 news:mailman.16534.1417610132.18130.python-l...@python.org...

 On 03/12/2014 02:27, Skybuck Flying wrote:

 Excuse is: bad programming style.

 I don't need snot telling me how to program after 20 years of
 programming experience.

 This is so far the only thing pissing me off in python.

 Now I have to declare global in front of these variables every where I
 want to use em:


 This reminds of of a quote from a colleague some 25 years ago Real time
 programming is easy, you just make all the data global.  Perhaps you
 attended the same school?


 
 Another example of a bad workman always blames his tools.
 

 Euhm, so why don't you program with just 0 and 1's then ? ;)


 I did with the M6800 in the late 70s.  Thankfully maybe 12 years ago I
 came across Python and it was love at first sight.  I've never looked back.

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

 Mark Lawrence

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




-- 
Freelance Grails http://grails.asia/ and Java http://javadevnotes.com/
 developer
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How is max supposed to work, especially key.

2014-12-04 Thread Chris Angelico
On Thu, Dec 4, 2014 at 9:09 PM, Albert van der Horst
alb...@spenarnc.xs4all.nl wrote:
If there is more than one item with the maximum calculated the first is
given, so for your attempt

max(xrange(100,200), key=lambda i: i%17==0 )


the values False, False, True, False, ... are calculated and because

 True  False
True

the first one with a True result is returned.


 So in that case max doesn't return the maximum (True), but instead
 something else.

 Useful as that function may be, it shouldn't have been called max.

 I don't blame myself for being misled.

If lots of them are equally the largest, by whatever definition of
largest you have, it has to do one of three things:

1) Raise an exception
2) Return multiple items (either as a tuple, or a generator, or something)
3) Pick one of them and return it.

Python's max() does the third, and for the picking part, uses the
first one it comes across - a decent way to do it.

If there's no clear maximum, it can't do any better than that. It's
still returning something for which there is no greater.

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


Re: Python handles globals badly.

2014-12-04 Thread Mark Lawrence

On 04/12/2014 05:03, jtan wrote:

How can Skybuck use so much globals. Wouldn't that introduce a lot of
thread safety problems?



I actually don't know.  However buying very strong thread from your 
local store and making sure that you have a very sharp needle does help 
alleviate threading safety problems.  I do know that using globals is 
almost as bad as top posting on this list.


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

Mark Lawrence

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


Re: Python handles globals badly.

2014-12-04 Thread Chris Angelico
On Thu, Dec 4, 2014 at 4:03 PM, jtan ad...@grails.asia wrote:
 How can Skybuck use so much globals. Wouldn't that introduce a lot of thread
 safety problems?

A lot of programs don't use threads, and therefore cannot have thread
safety problems - or, looking at it the other way, do not care about
thread safetiness. It's like having Neil Armstrong wear water wings to
make sure he won't drown in the Sea of Tranquility.

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


Re: Python docs disappointing

2014-12-04 Thread Albert van der Horst
In article mailman.16030.1416502295.18130.python-l...@python.org,
Joel Goldstick  joel.goldst...@gmail.com wrote:
SNIP

Or just WOW!.  Programming is hard, and people have just started to do
it.  Fifty years isn't that long.  It has only been 20 years or so
that the web has been around.  That makes it easier to find
information from a variety or sources -- the official docs, tutorials,
articles.  If you feel the docs are awful, write a tutorial based on
your knowledge level and experience.  Improve the situation.

That doesn't help. I'm a very experienced programmer and work in
routinely a dozen languages. Sometimes I do python. I want to do
numeric work. I remember the name numpy. It is important, everybody
knows it, it is all over the place. So I want to find its docs,
or some lead, whatever. I go to the official Python site,
http://docs.python.org and type in numpy in the search machine.

It is embarassing, try it!

Plain google is far superior in finding information.

And you tell me that writing yet another tutorial would improve that?
No, there is just one way. The powers that be should look critically
at their website, and test it with a beginners hat on.


I'm trying to wrap my mind around DOCUMENTION being STUPID.

--
Joel Goldstick
http://joelgoldstick.com

Groetjes Albert
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spearc.xs4all.nl =n http://home.hccnet.nl/a.w.m.van.der.horst

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


Re: Python docs disappointing

2014-12-04 Thread Chris Angelico
On Thu, Dec 4, 2014 at 9:27 PM, Albert van der Horst
alb...@spenarnc.xs4all.nl wrote:
 That doesn't help. I'm a very experienced programmer and work in
 routinely a dozen languages. Sometimes I do python. I want to do
 numeric work. I remember the name numpy. It is important, everybody
 knows it, it is all over the place. So I want to find its docs,
 or some lead, whatever. I go to the official Python site,
 http://docs.python.org and type in numpy in the search machine.

 It is embarassing, try it!

 Plain google is far superior in finding information.

Part of getting to know a language is learning what the best way to
find information about it is. When I want Pike documentation, I always
go direct to the docs (usually to my own build of them, to be sure
it'll match my locally-compiled Pike, so that's
http://sikorsky:8080/modref/index.html), but for most other languages,
it's better to use a web search. Sometimes your best result isn't the
official docs at all, but a StackOverflow post. Or maybe it's part of
the official docs, but not the module reference page (if you want to
know about argparse basics, the python.org howto is probably better
than the main page). As an added bonus, you don't need to go here,
type this into search - you just type what you want into your
browser's search box, or at very worst, pull up a link you probably
use a hundred times a day and will be cached.

 And you tell me that writing yet another tutorial would improve that?
 No, there is just one way. The powers that be should look critically
 at their website, and test it with a beginners hat on.

The powers that be implies that there's somebody else who is
obligated to provide a service to you. I don't think that's really the
case. That said, though, your other points are quite right; another
tutorial won't help, and improving search will help.

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


Re: How is max supposed to work, especially key.

2014-12-04 Thread Albert van der Horst
In article mailman.16552.1417688329.18130.python-l...@python.org,
Chris Angelico  ros...@gmail.com wrote:
On Thu, Dec 4, 2014 at 9:09 PM, Albert van der Horst
alb...@spenarnc.xs4all.nl wrote:
If there is more than one item with the maximum calculated the first is
given, so for your attempt

max(xrange(100,200), key=lambda i: i%17==0 )


the values False, False, True, False, ... are calculated and because

 True  False
True

the first one with a True result is returned.


 So in that case max doesn't return the maximum (True), but instead
 something else.

 Useful as that function may be, it shouldn't have been called max.

 I don't blame myself for being misled.

If lots of them are equally the largest, by whatever definition of
largest you have, it has to do one of three things:

1) Raise an exception
2) Return multiple items (either as a tuple, or a generator, or something)
3) Pick one of them and return it.

Python's max() does the third, and for the picking part, uses the
first one it comes across - a decent way to do it.

If there's no clear maximum, it can't do any better than that. It's
still returning something for which there is no greater.

I agree that it is a useful function and that it is doing
the right thing. What is wrong is the name.
I refer to the fact that it is not returning the maximum.
It returns the iterator value that leads to the maximum.
A function that doesn't return a maximum shouldn't be called
maximum.


ChrisA
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spearc.xs4all.nl =n http://home.hccnet.nl/a.w.m.van.der.horst

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


Re: How is max supposed to work, especially key.

2014-12-04 Thread Jussi Piitulainen
Albert van der Horst writes:

 Useful as that function [Python's max with a key] may be, it
 shouldn't have been called max.

The meaning of the key should be added to help(max), if it still isn't
- returns a maximal element or an element that maximizes the key.

In some communities they call it arg max, with a thin space between
the components of the name. Or maybe no space: argmax.

Would you also want sorted called something else when used with a key?
Because it doesn't produce a sorted list of the keys either:

   data = (short, long, average)

   sorted(data, key=len)
  ['long', 'short', 'average']
   max(data, key=len)
  'average'

   sorted(map(len, data))
  [4, 5, 7]
   max(map(len, data))
  7

While the key is not a filter, a filter is as available as the map
above:

   max(filter(lambda i : i % 17 == 0, range(100, 200)))
  187
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How is max supposed to work, especially key.

2014-12-04 Thread Jussi Piitulainen
Albert van der Horst writes:
 Chris Angelico wrote:
  If there's no clear maximum, it can't do any better than
  that. It's still returning something for which there is no
  greater.
 
 I agree that it is a useful function and that it is doing
 the right thing. What is wrong is the name.
 I refer to the fact that it is not returning the maximum.
 It returns the iterator value that leads to the maximum.
 A function that doesn't return a maximum shouldn't be called
 maximum.

It's called max. A maximal element need not be unique even in
mathematics: http://en.wikipedia.org/wiki/Maximal_element.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How is max supposed to work, especially key.

2014-12-04 Thread Peter Otten
Albert van der Horst wrote:

 In article mailman.16378.1417111312.18130.python-l...@python.org,
 Peter Otten  __pete...@web.de wrote:
Albert van der Horst wrote:

 In the Rosetta code I come across this part of
 LU-decomposition.

 def pivotize(m):
 Creates the pivoting matrix for m.
 n = len(m)
 ID = [[float(i == j) for i in xrange(n)] for j in xrange(n)]
 for j in xrange(n):
 row = max(xrange(j, n), key=lambda i: abs(m[i][j]))
 if j != row:
 ID[j], ID[row] = ID[row], ID[j]
 return ID

 That it's using a cast from boolean to float and using
 at the other moment a float as a boolean, suggest that this
 code is a bit too clever for its own good, but anyway.

 My problem is with the max. I never saw a max with a key.

 In my python help(max) doesn't explain the key. It says that
 max can handle an iterator (I didn't know that), and you can
 pass and optional key=func, but that's all.

 I expect it to be something like
   elements in the iterator are taken into account only if the
   key applied to the iterator evaluates to a True value.

 However that doesn't pan out:
 
 max(xrange(100,200), key=lambda i: i%17==0 )
 102
 

 I expect the maximum number that is divisible by 17 in the
 range, not the minimum.

 Can anyone shed light on this?

Given a function f() max(items, key=f) returns the element of the `items`
sequence with the greatest f(element), e. g. for

max([a, bcd, ef], key=len)

the values 1, 3, 2 are calculated and the longest string in the list is
returned:

 max([a, bcd, ef], key=len)
'bcd'

If there is more than one item with the maximum calculated the first is
given, so for your attempt

max(xrange(100,200), key=lambda i: i%17==0 )
 

the values False, False, True, False, ... are calculated and because

 True  False
True

the first one with a True result is returned.

 
 So in that case max doesn't return the maximum (True), but instead
 something else.
 
 Useful as that function may be, it shouldn't have been called max.
 
 I don't blame myself for being misled.

I believe you still misunderstand. Again, this time with an almost real-
world car example:

max(values key=key)

calculates key(value) for every value in values. key() can be len() or an 
attribute getter, so that

max(cars, key=lambda car: car.weight) 

finds the heaviest car and

max(cars, key=lambda car: car.speed)

finds the fastest car. (If there is a tie the first car with maximum 
weight/speed is returned.)

The advantage of this approach is that you don't have to choose a natural 
order, i. e. should

max(cars)

find the fastest, or the heaviest, or the [you name it] car?

Also, you are really interested in the car, and

max(car.speed for car in cars)

would only give you the speed of the fastest car, not the car object itself. 
To find the fastest car without a key argument you'd have to write

fastest_car = max((car.speed, car) for car in cars)[-1]

or even

fastest_car = max((car.speed, i, car) for (i, car) in enumerate(cars))[-1]

if you are thorough and want the first car in the sequence with maximum 
speed or have to deal with car objects that aren't comparable.

The example where the key returns a boolean value is very uncommon, but 
booleans are not treated specially. So while you can write

first_red_car = max(cars, key=lambda car: car.color == RED)
if first_red_car.color == RED:
print(first_red_car)
else:
print(no red cars available)


that is unidiomatic. It is also inefficient because max() always iterates 
over the whole cars sequence in search for an even redder car. False == 0, 
and True == 1, and max() cannot be sure that the key function will never 
return 2 or TruerThanTrue ;) -- but the programmer usually knows.

Better:

red_cars = (car for car in cars if car.color == RED)
first_red_car = next(red_cars, None)
if first_red_car is not None:
print(first_red_car)
else:
print(no red cars available)


PS: Another thing to consider is the similarity with sorted().
You can sort by speed 

sorted(cars, key=lambda car: car.speed)

or redness

sorted(cars, key=lambda car: car.color == RED)

but you will see the former much more often than the latter.


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


Re: How is max supposed to work, especially key.

2014-12-04 Thread Peter Otten
Albert van der Horst wrote:
 I agree that it is a useful function and that it is doing
 the right thing. What is wrong is the name.
 I refer to the fact that it is not returning the maximum.
 It returns the iterator value that leads to the maximum.
 A function that doesn't return a maximum shouldn't be called
 maximum.

But a maximum is often ambiguous. What is max(people), the oldest, tallest, 
or richest person? The key argument is a convenient way to disambiguate.

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


Re: Python handles globals badly.

2014-12-04 Thread Steven D'Aprano
jtan wrote:

 How can Skybuck use so much globals. Wouldn't that introduce a lot of
 thread safety problems?

Of course it would. But I expect that Skybuck probably doesn't even know
what threads are. Or if he does, he probably doesn't believe that they
should be used.

Thread safety is just the start of the problems with global variables:

http://c2.com/cgi/wiki?GlobalVariablesAreBad

Globals in Python are less bad than in many other languages, since they are
localised to a single module only. And the use of a few globals here and
there as needed is perfectly fine for small scripts. But using dozens of
them to avoid passing arguments to functions, that's just awful code.


-- 
Steven

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


Re: Python docs disappointing

2014-12-04 Thread Steven D'Aprano
Albert van der Horst wrote:

 That doesn't help. I'm a very experienced programmer and work in
 routinely a dozen languages. Sometimes I do python. I want to do
 numeric work. I remember the name numpy. It is important, everybody
 knows it, it is all over the place. So I want to find its docs,
 or some lead, whatever. I go to the official Python site,
 http://docs.python.org and type in numpy in the search machine.
 
 It is embarassing, try it!

Why would it be embarrassing?

Why should the Python website provide documentation for numpy? Numpy is not
part of the Python language, and it is not part of the Python standard
library.

If you buy a Compaq printer to plug in to your Hewlett-Packard computer
running Windows, would you go to the Microsoft website to find out where to
order toner for the printer? Or to the H-P site?

Numpy might be written *in* Python (*some* of numpy, not all of it) but it
is not part of Python. Why should the python language website be
responsible for numpy documentation? How about django, CherryPy, nltk, and
about two thousand other third party projects?

It is 2014, not 1977, and your first step for nearly any search should be
the global search engine of your choice: Google, DuckDuckGo, Bing, Yahoo,
Startpage, or similar. Only if you know that something is categorically
part of Python or its standard library should you limit yourself to the
Python language reference and standard library documentation.


 Plain google is far superior in finding information.

Well duh.

 And you tell me that writing yet another tutorial would improve that?

Apparently people need a tutorial to teach them to search the entire web not
just a single website...




-- 
Steven

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


Re: How is max supposed to work, especially key.

2014-12-04 Thread Steven D'Aprano
Jussi Piitulainen wrote:


 Would you also want sorted called something else when used with a key?
 Because it doesn't produce a sorted list of the keys either:
 
data = (short, long, average)
sorted(data, key=len)
   ['long', 'short', 'average']
max(data, key=len)
   'average'

I agree with the point you are making, but I disagree with the wording you
use. The result of calling sort() with key=len *is* sorted. It is sorted by
length of the word.

Same for calling max() with a key. The result is still the maximum value.
The difference is how you decide which of two elements is greater:

max(list_of_foods, key=calories)
max(list_of_foods, key=weight)
max(list_of_foods, key=cost)


That is three different ways to decide which is the maximal food in the
list: by number of calories, by weight, or by cost.




-- 
Steven

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


Re: How is max supposed to work, especially key.

2014-12-04 Thread Steven D'Aprano
Albert van der Horst wrote:

 I agree that it is a useful function and that it is doing
 the right thing. What is wrong is the name.
 I refer to the fact that it is not returning the maximum.
 It returns the iterator value that leads to the maximum.

That is incorrect. It returns the maximum value, that is, the value greater
than all the other values. What is different is the way this value is
greater than that value is tested for the purposes of calculating this
maximum.

 A function that doesn't return a maximum shouldn't be called
 maximum.

Naturally. But this does calculate the maximum, according to the given key
function.


-- 
Steven

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


Re: Python handles globals badly.

2014-12-04 Thread Marko Rauhamaa
Chris Angelico ros...@gmail.com:

 A lot of programs don't use threads, and therefore cannot have thread
 safety problems - or, looking at it the other way, do not care about
 thread safetiness. It's like having Neil Armstrong wear water wings to
 make sure he won't drown in the Sea of Tranquility.

The water wings would be too unwieldy since they'd have to be six times
as large on the moon. It's all about risk/benefit analysis.


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


Re: How is max supposed to work, especially key.

2014-12-04 Thread Jussi Piitulainen
Steven D'Aprano writes:
 Jussi Piitulainen wrote:
 
  Would you also want sorted called something else when used with a
  key?  Because it doesn't produce a sorted list of the keys either:
  
 data = (short, long, average)
 sorted(data, key=len)
['long', 'short', 'average']
 max(data, key=len)
'average'
 
 I agree with the point you are making, but I disagree with the
 wording you use. The result of calling sort() with key=len *is*
 sorted. It is sorted by length of the word.

It's sorted but it's not a list of the keys. That seemed to be a point
of contention about naming max-with-key max: it doesn't return a key.

 Same for calling max() with a key. The result is still the maximum
 value.  The difference is how you decide which of two elements is
 greater:
 
 max(list_of_foods, key=calories)
 max(list_of_foods, key=weight)
 max(list_of_foods, key=cost)
 
 That is three different ways to decide which is the maximal food in
 the list: by number of calories, by weight, or by cost.

Yes. I don't see any disagreement between us.
-- 
https://mail.python.org/mailman/listinfo/python-list


A question about setup.py

2014-12-04 Thread David Aldrich
Hi

I'm trying to install the path.py package under Python 2.7 on Windows.

I installed it using:

easy_install path.py

That worked but it didn't install path.py which is needed by my PTVS IDE for 
code completion (Intellisense).

I then tried downloading path.py-7.0.zip. I unzipped it and ran:

python setup.py install

within the extracted folder. But path.py still appears not to have been copied 
to:

C:\Python27

What am I doing wrong?

Best regards

David

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


Re: Most gratuitous comments

2014-12-04 Thread Jean-Michel Pichavant
- Original Message -
 From: sohcahto...@gmail.com
 I was trying to illustrate the point that some professors would
 demand you write code like this...
 
 # increment the line count
 lineCount += 1
 
 # Check if line count is over 10
 if lineCount  10
 # Tell the user there are too many lines
 print 'There are too many lines!
 
 ...which is obviously bad commenting style.  But I guess my original
 minimal example was too minimal.
 

The problem is not that every line is commented, the problem is that comments 
do not add any value. There's always something to tell in real life situations:

# assuming all lines look like 'v=1234\n', generate all integers provided by 
the user
values = (int(line.replace('v=', '')) for line in lines)

# See SPE-xxx: 10 line max do not change it
if len(lines)  10:
  # TODO: use the logging module
  print 'There are too many lines!


In practice, this yield to a comment every 2 or 3 lines. Of course this much 
depend on the code itself and may vary slightly from code block to code block. 
Note that I am not sanctioning the use of comment on import statements :D

To go back to your point, some professors may be right when asking a comment 
every line, because it will be easier then for someone to back off a little bit 
and comment slightly less. While students with the habit of writing no comment 
will have much trouble commenting properly.



Cheers,

JM









-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How is max supposed to work, especially key.

2014-12-04 Thread random832
On Thu, Dec 4, 2014, at 05:09, Albert van der Horst wrote:
 So in that case max doesn't return the maximum (True), but instead
 something else.

If you want to find the largest item in a list of of strings, sorted
case-insensitively, you might use str.lower or locale.strxfrm as the key
function. If two strings are case-insensitively identical, using
str.lower will return an arbitrary one (or maybe the first or last found
in the original list), using locale.strxfrm will return the one that is
in uppercase in the first position where they are different. But you
don't want it to return the all-lowercase version (or the messy binary
thing returned by strxfrm), you want it to return the string from the
original list.

Think of it this way: the key function returns a proxy object whose
comparison operators compare according to the relationship you want to
compare the originals by. Bool does not make a good proxy object.

If you want to use the original as a tiebreaker, you might use lambda x:
(str.lower(x), x).

A key function is supposed to be a function that will always return the
same result on the same input, so if you want to know the maximum
[i.e. the actual key value that was compared by the max function] you
can simply apply it again on the result.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Most gratuitous comments

2014-12-04 Thread sjmsoft
Many years ago I, too, had a couple of CS profs who forced us to include too 
many (usually innocuous) comments in our Fortran and PL/1 code.  Perhaps they 
were trying to counter the natural programmer tendency of not commenting at all?

Forty years of programming later (yikes!), I try to use comments to tell WHY 
I'm doing what I'm doing, especially when it's not obvious to someone else (or 
to me in about two weeks).  I never use comments to teach the reader what the 
Python language or libraries do.

Cheers,
  Steve J. Martin
-- 
https://mail.python.org/mailman/listinfo/python-list


time.monotonic() roll over

2014-12-04 Thread ast

Hello,

Does any body know when time.monotonic() rolls over ?

On python doc https://docs.python.org/3/library/time.html
it is said  every 49.7 days on Windows versions older than 
Vista. For more recent Windows, it is sais that  monotonic() 
is system-wide but they dont say anything about roll over,

probably it hasn't changed.

Anyway, if someone need a monotonic clock, he may think
to use either time.time() or time.monotonic().
There is no roll over problem with time.time() since the very
first one in planned far in the future, but time.time() can go
backward when a date update throught NTP server is done.

time.monotonic() is monotonic but roll over often (every 49.7 
days)


So what to do ? 

Using time.monotonic() and take some actions if a roll over 
is detected ?

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


Re: PyEval_GetLocals and unreferenced variables

2014-12-04 Thread Ian Kelly
On Wed, Dec 3, 2014 at 5:28 AM, Gregory Ewing greg.ew...@canterbury.ac.nz
wrote:

 Kasper Peeters wrote:

 That may have been the design plan, but in Python 2.7.6, I definitely
 am able to inject locals via PyEval_GetLocals() and have them be visible
 both from the C and Python side;

 What seems to be happening is that the dict created by
 PyEval_GetLocals() is kept around, so you can change it
 and have the changes be visible through locals() in
 Python.

That the dict is cached is an implementation detail. It's not advisable to
rely upon this, and as stated in the Python docs, the locals dict should
not be modified.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How is max supposed to work, especially key.

2014-12-04 Thread Terry Reedy

On 12/4/2014 5:35 AM, Albert van der Horst wrote:


I agree that it is a useful function and that it is doing
the right thing. What is wrong is the name.
I refer to the fact that it is not returning the maximum.
It returns the iterator value that leads to the maximum.
A function that doesn't return a maximum shouldn't be called
maximum.


The key function serves the same purpose as with sort.  It define a 
custom ordering of items, not the values returned.  'x  y' is defined 
as key(x)  key(y), where the comparison of keys uses the standard (and 
for numbers, natural) built-in ordering.  So max returns the max value 
according to the order you define with the key function.  The default 
key is the identity function, so the default order is the builtin order.


--
Terry Jan Reedy

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


Re: time.monotonic() roll over

2014-12-04 Thread Marko Rauhamaa
ast nom...@invalid.com:

 Does any body know when time.monotonic() rolls over ?

Never, according to the documentation you linked.

Admittedly, the documentation confuses the reader by chatting about some
irrelevant internal Windows details.

Also, the tone of the documentation raises a suspicion that this code
might return a bad value on Windows:

def fifty_days():
a = time.monotonic()
time.sleep(50 * 24 * 3600)
return time.monotonic() - a

That is, the internal integer wrap is not guarded against between the
calls to time.monotonic(), maybe.


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


Re: Python handles globals badly.

2014-12-04 Thread jtan
I wish him all the luck while having sleep deprivation trying to solve
production issues :)

On Thu, Dec 4, 2014 at 7:35 PM, Steven D'Aprano 
steve+comp.lang.pyt...@pearwood.info wrote:

 jtan wrote:

  How can Skybuck use so much globals. Wouldn't that introduce a lot of
  thread safety problems?

 Of course it would. But I expect that Skybuck probably doesn't even know
 what threads are. Or if he does, he probably doesn't believe that they
 should be used.

 Thread safety is just the start of the problems with global variables:

 http://c2.com/cgi/wiki?GlobalVariablesAreBad

 Globals in Python are less bad than in many other languages, since they are
 localised to a single module only. And the use of a few globals here and
 there as needed is perfectly fine for small scripts. But using dozens of
 them to avoid passing arguments to functions, that's just awful code.


 --
 Steven

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




-- 
Freelance Grails http://grails.asia/ and Java http://javadevnotes.com/
 developer
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: time.monotonic() roll over

2014-12-04 Thread Ian Kelly
On Dec 4, 2014 8:56 AM, Marko Rauhamaa ma...@pacujo.net wrote:

 ast nom...@invalid.com:

  Does any body know when time.monotonic() rolls over ?

 Never, according to the documentation you linked.

 Admittedly, the documentation confuses the reader by chatting about some
 irrelevant internal Windows details.

Not entirely irrelevant. The implication is that if you go more than 49
days without calling the function on old Windows systems, rollovers could
be missed, which is good to know about. The result would still be
monotonic, but it wouldn't accurately reflect the time elapsed.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: time.monotonic() roll over

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 3:05 AM, Ian Kelly ian.g.ke...@gmail.com wrote:
 On Dec 4, 2014 8:56 AM, Marko Rauhamaa ma...@pacujo.net wrote:

 ast nom...@invalid.com:

  Does any body know when time.monotonic() rolls over ?

 Never, according to the documentation you linked.

 Admittedly, the documentation confuses the reader by chatting about some
 irrelevant internal Windows details.

 Not entirely irrelevant. The implication is that if you go more than 49 days
 without calling the function on old Windows systems, rollovers could be
 missed, which is good to know about. The result would still be monotonic,
 but it wouldn't accurately reflect the time elapsed.

I don't know for sure about the newer Windowses, but I believe they
use a 64-bit counter instead of the 32-bit one used in previous
versions, so even if they do roll over, there'll be a much MUCH longer
time scale involved. Even if it stores time in nanoseconds, a 64-bit
counter would allow for hundreds of years between rollovers, which is
reasonably safe - much better than the month-and-a-bit of older ones,
which is shorter than quite a lot of my programs' uptimes. Hence the
lack of information about them; you don't have to worry. (Likewise on
Unix-derived systems, I believe, and for the same reason.)

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


Re: time.monotonic() roll over

2014-12-04 Thread Marko Rauhamaa
Ian Kelly ian.g.ke...@gmail.com:

 The implication is that if you go more than 49 days without calling
 the function on old Windows systems, rollovers could be missed, which
 is good to know about.

The implication is all but clear, but that was my suspicion. It would be
so bad that the documentation should be much more explicit about it.

No, that would be a Python standard library bug, period.


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


Re: time.monotonic() roll over

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 3:21 AM, Marko Rauhamaa ma...@pacujo.net wrote:
 Ian Kelly ian.g.ke...@gmail.com:

 The implication is that if you go more than 49 days without calling
 the function on old Windows systems, rollovers could be missed, which
 is good to know about.

 The implication is all but clear, but that was my suspicion. It would be
 so bad that the documentation should be much more explicit about it.

 No, that would be a Python standard library bug, period.

It's not a Python issue. Python can't do anything more than ask the
system, and if the system's value rolls over several times a year,
Python can't magically cure that. The information has already been
lost.

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


Re: Is Python installer/un-installer buggy on Windows?

2014-12-04 Thread Aseem Bansal
Yeah, the problem seems to be with registry as every solution seems to be 
fiddling with registry.

I know that reinstalling OS is a really bad idea. But I have tried to find a 
way to solve this for months now. I have started a bounty on superuser also for 
the same in the question Python IDLE disappeared from the right click context 
menu. And asking on these groups was the last thing I can think of.

I uninstalled via the Control Panel. I installed via the official Python 
installer for Windows.

I also don't understand how a start menu entry can begin an installation but it 
is doing just that. Now there seems to be 4 IDLE entries in my start menu. Two 
are valid (2.7.8, 3.4.2). The other two are previous installations of Python 
3.4  that I did at different locations but removed later. I have stopped 
running IDLE from the start menu due to this. Because I am never sure whether 
it will re-install Python 3.4 at those old locations or not. Even right 
clicking those entries in start menu causes the installations to start. So I 
cannot even find the physical path of those entries.

Maybe too much fiddling with the registry has caused it. Not sure about that.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: time.monotonic() roll over

2014-12-04 Thread random832
On Thu, Dec 4, 2014, at 10:50, Marko Rauhamaa wrote:
 That is, the internal integer wrap is not guarded against between the
 calls to time.monotonic(), maybe.

Looking at the code, it looks like it does guard against the rollover,
though if you let your program run for 49.7 days _without_ calling
time.monotonic during that time, it will not realize that it has passed
the original value multiple times. And of course, if it rolls over
before the first time you call monotonic in process B, process B's value
will be less than process A which called it before the rollover.

This is consistent with the documentation, and appears to be the reason
the documentation discusses this issue at all.
-- 
https://mail.python.org/mailman/listinfo/python-list


Do you like the current design of python.org?

2014-12-04 Thread Peter Otten
Did you ever hit the Socialize button? Are you eager to see the latest 
tweets when you are reading a PEP? Do you run away screaming from a page 
where nothing moves without you hitting a button? Do you appreciate the 
choice between ten or so links to the documentation?

You can probably guess my opinion -- konqueror just crashed on the PEP index 
and for some reason I'm more annoyed about the page than about the browser.


PS: Is there a twitter.com something that I can block to trade black friday 
and cyber monkey sales for a box with a good old error message?

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


Re: Most gratuitous comments

2014-12-04 Thread Rob Gaddi
On 04 Dec 2014 09:48:49 GMT
alb...@spenarnc.xs4all.nl (Albert van der Horst) wrote:

 In article 546d7505$0$12899$c3e8da3$54964...@news.astraweb.com,
 Steven D'Aprano  st...@pearwood.info wrote:
 And the award for the most gratuitous comments before an import goes to
 one of my (former) workmates, who wrote this piece of code:
 
 # Used for base64-decoding.
 import base64
 # Used for ungzipping.
 import gzip
 
 The comment lines contain genuine information. The program is
 decoding or gunzipping. (And apparently not doing the encoding part)
 
 This information may have been better conveyed by
 
 from base64 import base64-decode
 from gzip import gunzip
 
 but anyway.
 
 Also the comment may be misleading, but I think not.
 
 If there are mysterious names for packages, the comment may be
 actually useful.
 
 # Auxiliary for the reverse recursion to calculate
 # Chebychev coefficients.
 import courseware-2014-ch11
 
 
 A professor who demands from students that every import is documented
 is IMO not to blame.
 In a company's coding convention ... I've seen a lot of things there
 that make a lot less sense.
 
 --
 Steven
 -- 
 Albert van der Horst, UTRECHT,THE NETHERLANDS
 Economic growth -- being exponential -- ultimately falters.
 albert@spearc.xs4all.nl =n http://home.hccnet.nl/a.w.m.van.der.horst
 

In my code, I habitually use big old bar comments to break the imports
into three sections, standard library, third-party libraries, and local
imports.  I find it radically simplifies knowing where to start looking
for documentation.

-- 
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: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Wolfgang Maier

On 12/03/2014 12:02 PM, Chris Angelico wrote:

When importing a module from a subpackage, it's sometimes convenient
to refer to it throughout the code with a one-part name rather than
two. I'm going to use 'os.path' for the examples, but my actual
use-case is a custom package where the package name is, in the
application, quite superfluous.

Throughout the code, I want to refer to path.split(),
path.isfile(), etc, without the os. in front of them. I could do
either of these:

import os.path as path
from os import path

Which one would you recommend? Does it depend on context?



One argument not yet brought up by anyone else:

if you ever wanted to make the module part of your own package and turn 
the import into a relative one, only the second, but not the first form 
lets you replace the package name with . :


from . import path (while import .path is a SyntaxError, so you'd need a 
slightly more complicated rewrite).


Wolfgang


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


Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Jean-Michel Pichavant
- Original Message -
 From: Chris Angelico ros...@gmail.com
 To: python-list@python.org
 Sent: Wednesday, 3 December, 2014 12:02:17 PM
 Subject: Style question: Importing modules from packages - 'from' vs 'as'
 
 When importing a module from a subpackage, it's sometimes convenient
 to refer to it throughout the code with a one-part name rather than
 two. I'm going to use 'os.path' for the examples, but my actual
 use-case is a custom package where the package name is, in the
 application, quite superfluous.
 
 Throughout the code, I want to refer to path.split(),
 path.isfile(), etc, without the os. in front of them. I could do
 either of these:
 
 import os.path as path
 from os import path
 
 Which one would you recommend? Does it depend on context?
 
 An as import works only if it's a module in a package, where the
 from import can also import other objects (you can't go import
 pprint.pprint as pprint). I'm fairly sure that's an argument... on
 one side or another. :)
 
 Thoughts?
 
 ChrisA
 --
 https://mail.python.org/mailman/listinfo/python-list
 

I know you specifically stated you didn't want to do this but

import os

os.path.isfile()

is the best option imo, especially from the reader point of view (Namespaces 
are one honking great idea).



-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Ethan Furman
On 12/04/2014 09:36 AM, Jean-Michel Pichavant wrote:
 
 I know you specifically stated you didn't want to do this but
 
   import os
 
   os.path.isfile()
 
 is the best option imo, especially from the reader point of view (Namespaces 
 are one honking great idea).

But, Flat is better than nested !  ;)

--
~Ethan~



signature.asc
Description: OpenPGP digital signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Ethan Furman
On 12/03/2014 03:02 AM, Chris Angelico wrote:
 
 Throughout the code, I want to refer to path.split(),
 path.isfile(), etc, without the os. in front of them. I could do
 either of these:
 
 import os.path as path
 from os import path
 
 Which one would you recommend? Does it depend on context?

I recommend the one with less typing.  ;)

--
~Ethan~



signature.asc
Description: OpenPGP digital signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 4:36 AM, Jean-Michel Pichavant
jeanmic...@sequans.com wrote:
 I know you specifically stated you didn't want to do this but

 import os

 os.path.isfile()

 is the best option imo, especially from the reader point of view (Namespaces 
 are one honking great idea).

With os.path it definitely is. With the actual code in question, it's
a Python 2.7 project that mostly uses relative imports - inside
package.module1 is import module2 etc - and I was writing an
external script that calls on one of the modules. So it makes sense to
reference it through the code the exact same way, as module.blah
rather than package.module.blah.

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


Re: time.monotonic() roll over

2014-12-04 Thread Marko Rauhamaa
Chris Angelico ros...@gmail.com:

 It's not a Python issue. Python can't do anything more than ask the
 system, and if the system's value rolls over several times a year,
 Python can't magically cure that. The information has already been
 lost.

Sure it could by having an invisible background thread occasionally call
time.monotonic(). It could even be done on the side without a thread.

Anyway, the idea of a clock is complicated:

 * the program could be stopped by a STOP signal

 * the program could be suspended from power management

 * the program could be resurrected from a virtual machine snapshot

 * the program could be migrated from a different physical machine

So, if I call

   time.sleep(86400)

and the program is suspended for 24 hours, should time.sleep() return
right after it is resumed or after another 24 hours?


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


ORM opinion

2014-12-04 Thread Joseph L. Casale
Begrudgingly, I need to migrate away from SQLAlchemy onto a
package that has fast imports and very fast model build times.

I have a less than ideal application that uses Python as a plugin
interpreter which is not performant in this use case where its
being invoked freshly several times per operation within the
application where several operations themselves are queued.

In order to minimize latency, I need to remove anything that
either takes a noticeable amount of time to import or run. My
initial backend support is limited to only SQLite.

Anyone have any recos or opinions?

Thanks,
jlc

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


Re: Do you like the current design of python.org?

2014-12-04 Thread Ethan Furman
On 12/04/2014 09:09 AM, Peter Otten wrote:

 Did you ever hit the Socialize button? Are you eager to see the latest 
 tweets when you are reading a PEP? Do you run away screaming from a page 
 where nothing moves without you hitting a button? Do you appreciate the 
 choice between ten or so links to the documentation?
 
 You can probably guess my opinion -- konqueror just crashed on the PEP index 
 and for some reason I'm more annoyed about the page than about the browser.

Visually, it's nice; functionally... well, I just had to use google to find the 
CLA form, because the built-in search
box couldn't (and yes, it's on the site).

--
~Ethan~



signature.asc
Description: OpenPGP digital signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: time.monotonic() roll over

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 5:09 AM, Marko Rauhamaa ma...@pacujo.net wrote:
 Sure it could by having an invisible background thread occasionally call
 time.monotonic(). It could even be done on the side without a thread.

No, it can't be solved by anything in that process, because...

  * the program could be stopped by a STOP signal

... what you said  There's no way to guarantee to keep calling the
function. You have to depend on upstream.

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


Re: ORM opinion

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 5:14 AM, Joseph L. Casale
jcas...@activenetwerx.com wrote:
 Begrudgingly, I need to migrate away from SQLAlchemy onto a
 package that has fast imports and very fast model build times.

 I have a less than ideal application that uses Python as a plugin
 interpreter which is not performant in this use case where its
 being invoked freshly several times per operation within the
 application where several operations themselves are queued.

 In order to minimize latency, I need to remove anything that
 either takes a noticeable amount of time to import or run. My
 initial backend support is limited to only SQLite.

 Anyone have any recos or opinions?

First recommendation: Less layers. Instead of SQLAlchemy, just import
sqlite3 and use it directly. You should be able to switch out import
sqlite as db for import psycopg2 as db or any other Python DB API
module, and still have most/all of the benefit of the extra layer,
without any extra imports.

Secondly, see if you can keep the Python process running, instead of
separately invoking it for every operation. This will mean some
structural changes, but if you're specifying Python as the plugin
interpreter (as opposed to, say, execute this program with these
args, which is far more general), you can probably set up a wrapper
script that calls on the user code repeatedly. Perhaps you could have
a multiprocessing.Queue feeding tasks to the subprocess, via a little
parent Python process. If you can't make those kinds of changes to the
application, the next best would be a stubby driver process that feeds
jobs to your real subprocess; the real work might be done by a
Unix/TCP socket server, and the stub connects, feeds it some
information, gets back a result, and disconnects. The stub will have
an extremely minimal set of imports (the bear necessities of life, if
you like), and the real process can import as much as it likes,
because it won't be doing it for every operation.

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


Re: time.monotonic() roll over

2014-12-04 Thread Marko Rauhamaa
Chris Angelico ros...@gmail.com:

 ... what you said  There's no way to guarantee to keep calling the
 function. You have to depend on upstream.

The caveats I listed are real concerns for the modern-day programmer.
However, they are of a different nature than the complaint against
time.monotonic().


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


Not Able to Log in on XNAT server through PyXNAT

2014-12-04 Thread suyash . d . b
Hello All,

I have installed pyxnat on my mac. With pyxnat i am trying to access XNAT 
server in our university. As mentioned on the tutorial i tried both ways, 
neither is working. Following error is displayed:

 central=Interface(server='http://hd-hni-xnat.cac.cornell.edu:8443/xnat')
User: sdb99
Password: 
Traceback (most recent call last):
  File stdin, line 1, in module
  File /Library/Python/2.7/site-packages/pyxnat/core/interfaces.py, line 228, 
in __init__
self._get_entry_point()
  File /Library/Python/2.7/site-packages/pyxnat/core/interfaces.py, line 268, 
in _get_entry_point
raise e
socket.error: [Errno 54] Connection reset by peer



Before accessing XNAT using REST API, do i need to make any changes/install 
something on XNAT server/my pc(have installed Pyxnat)..?

Many Thanks in Advance..

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


Re: Can you use self in __str__

2014-12-04 Thread Jean-Michel Pichavant
- Original Message -
 From: Seymore4Head Seymore4Head@Hotmail.invalid
 To: python-list@python.org
 Sent: Friday, 28 November, 2014 4:31:50 AM
 Subject: Re: Can you use self in __str__
 
 On Thu, 27 Nov 2014 21:49:29 -0500, Dave Angel da...@davea.name
 wrote:
 
 class Hand:
 def __init__(self):
 self.hand = []
 # create Hand object
 
 def __str__(self):
 s = 'Hand contains '
 for x in self.hand:
 s = s + str(x) +  
 return s
 
 I am using 2.7 (Codeskulptor).  This is working code.  It starts with
 an empty list that gets appended from a full deck of shuffled cards.
 dealer=Hand()
 player=Hand()
 I don't really know how to post working code without posting a lot.
  I
 am not being too successful in trying to post enough code to have it
 work without posting the entire code.
 Here is the link if you want to run it.
 http://www.codeskulptor.org/#user38_Kka7mh2v9u_9.py
 The print out looks like this:
 Hand contains H4 DQ.
 
 I can (and am) currently printing the hand like this:
 print Player's,player
 print Dealer's,dealer
 
 My question is can you add (self) in the __str__ so when you issue
 the
 command print player the player part is included in the __str__.
 --
 https://mail.python.org/mailman/listinfo/python-list


I think your main problem is a design issue.

I won't go into details, it would be too long but here's a way to fix your 
problem:

# Untested code

class Player(object):
  def __init__(self, name):
self.name = name
self.hand = Hand()
  
  def __str__(self):
return '%s%s has %s' % (self.__class__.__name__, self.name, self.hand)

# A dealer is a special type of player
class Dealer(Player): pass

def deal():
global outcome, in_play,deck,dealer,player

# your code goes here
deck=Deck()
deck.shuffle()
print deck
players = [Dealer('Robert'), Player('John')]

for deal in range(2):
  for player in players:
player.hand.add_card(deck.deal_card())

for player in players:
  print str(player)
in_play = True


With the above design, the relation between player and its hand is implemented 
with the Player attribute hand.
It's a classic design where the container knows about the content, but the 
content does not know its container.

There are other solutions which do not require a new class, but I have the 
feeling you will need the Player class in the future.

Moreover The design above I gave you will be probably broken in the future when 
you add features to your code.

The same player may be a dealer or not, it may vary over time. You will 
probably need a 'Table' object which handles a collection of players, with a 
dealer, small blind, big blind etc, amount of money in the pot etc...

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: time.monotonic() roll over

2014-12-04 Thread Ian Kelly
On Thu, Dec 4, 2014 at 11:09 AM, Marko Rauhamaa ma...@pacujo.net wrote:

 Chris Angelico ros...@gmail.com:

  It's not a Python issue. Python can't do anything more than ask the
  system, and if the system's value rolls over several times a year,
  Python can't magically cure that. The information has already been
  lost.

 Sure it could by having an invisible background thread occasionally call
 time.monotonic(). It could even be done on the side without a thread.

 Anyway, the idea of a clock is complicated:

  * the program could be stopped by a STOP signal

  * the program could be suspended from power management

  * the program could be resurrected from a virtual machine snapshot

  * the program could be migrated from a different physical machine

This seems like a lot of effort to unreliably design around a problem that
will matter to only a tiny fraction of users.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Do you like the current design of python.org?

2014-12-04 Thread Akira Li
Peter Otten __pete...@web.de writes:

 Did you ever hit the Socialize button? Are you eager to see the latest 
 tweets when you are reading a PEP? Do you run away screaming from a page 
 where nothing moves without you hitting a button? Do you appreciate the 
 choice between ten or so links to the documentation?

 You can probably guess my opinion -- konqueror just crashed on the PEP index 
 and for some reason I'm more annoyed about the page than about the browser.


 PS: Is there a twitter.com something that I can block to trade black friday 
 and cyber monkey sales for a box with a good old error message?

I see that pythondotorg accepts pull requests and allows to report issues.
https://github.com/python/pythondotorg


--
Akira

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


Re: Can you use self in __str__

2014-12-04 Thread Seymore4Head
On Thu, 4 Dec 2014 20:22:11 +0100 (CET), Jean-Michel Pichavant
jeanmic...@sequans.com wrote:

- Original Message -
 From: Seymore4Head Seymore4Head@Hotmail.invalid
 To: python-list@python.org
 Sent: Friday, 28 November, 2014 4:31:50 AM
 Subject: Re: Can you use self in __str__
 
 On Thu, 27 Nov 2014 21:49:29 -0500, Dave Angel da...@davea.name
 wrote:
 
 class Hand:
 def __init__(self):
 self.hand = []
 # create Hand object
 
 def __str__(self):
 s = 'Hand contains '
 for x in self.hand:
 s = s + str(x) +  
 return s
 
 I am using 2.7 (Codeskulptor).  This is working code.  It starts with
 an empty list that gets appended from a full deck of shuffled cards.
 dealer=Hand()
 player=Hand()
 I don't really know how to post working code without posting a lot.
  I
 am not being too successful in trying to post enough code to have it
 work without posting the entire code.
 Here is the link if you want to run it.
 http://www.codeskulptor.org/#user38_Kka7mh2v9u_9.py
 The print out looks like this:
 Hand contains H4 DQ.
 
 I can (and am) currently printing the hand like this:
 print Player's,player
 print Dealer's,dealer
 
 My question is can you add (self) in the __str__ so when you issue
 the
 command print player the player part is included in the __str__.
 --
 https://mail.python.org/mailman/listinfo/python-list


I think your main problem is a design issue.

I won't go into details, it would be too long but here's a way to fix your 
problem:

# Untested code

class Player(object):
  def __init__(self, name):
self.name = name
self.hand = Hand()
  
  def __str__(self):
return '%s%s has %s' % (self.__class__.__name__, self.name, self.hand)

# A dealer is a special type of player
class Dealer(Player): pass

def deal():
global outcome, in_play,deck,dealer,player

# your code goes here
deck=Deck()
deck.shuffle()
print deck
players = [Dealer('Robert'), Player('John')]

for deal in range(2):
  for player in players:
player.hand.add_card(deck.deal_card())

for player in players:
  print str(player)
in_play = True


With the above design, the relation between player and its hand is implemented 
with the Player attribute hand.
It's a classic design where the container knows about the content, but the 
content does not know its container.

There are other solutions which do not require a new class, but I have the 
feeling you will need the Player class in the future.

Moreover The design above I gave you will be probably broken in the future 
when you add features to your code.

The same player may be a dealer or not, it may vary over time. You will 
probably need a 'Table' object which handles a collection of players, with a 
dealer, small blind, big blind etc, amount of money in the pot etc...

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also 
be privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.

Thanks for the tips
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python handles globals badly.

2014-12-04 Thread alister
On Thu, 04 Dec 2014 00:02:25 +0100, Skybuck Flying wrote:

 Mark Lawrence  wrote in message
 news:mailman.16534.1417610132.18130.python-l...@python.org...
 
 On 03/12/2014 02:27, Skybuck Flying wrote:
 Excuse is: bad programming style.

 I don't need snot telling me how to program after 20 years of
 programming experience.

 This is so far the only thing pissing me off in python.

 Now I have to declare global in front of these variables every where
 I want to use em:
 
 
 Another example of a bad workman always blames his tools.
 
 
 Euhm, so why don't you program with just 0 and 1's then ? ;)
 
 Bye,
   Skybuck :)

Because I DO know how to use my tools.



-- 
God isn't dead.  He just doesn't want to get involved.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: time.monotonic() roll over

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 5:44 AM, Marko Rauhamaa ma...@pacujo.net wrote:
 Chris Angelico ros...@gmail.com:

 ... what you said  There's no way to guarantee to keep calling the
 function. You have to depend on upstream.

 The caveats I listed are real concerns for the modern-day programmer.
 However, they are of a different nature than the complaint against
 time.monotonic().

But they're also reasons you can't have Python code around the
problem. Anyway, it's only an issue for one platform, and only for
some versions of it; basically, if you're targeting anything other
than Windows XP, you should be able to use time.monotonic() without
concerns.

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


Re: Is Python installer/un-installer buggy on Windows?

2014-12-04 Thread Terry Reedy

On 12/4/2014 11:46 AM, Aseem Bansal wrote:

Yeah, the problem seems to be with registry as every solution seems
to be fiddling with registry.


One can edit the registry directly with regedit.  If you try it, follow 
the instruction to first make a backup.  Look for a regedit tutorial on 
the web.



I know that reinstalling OS is a really bad idea. But I have tried to
find a way to solve this for months now. I have started a bounty on
superuser also for the same in the question Python IDLE disappeared
from the right click context menu. And asking on these groups was
the last thing I can think of.

I uninstalled via the Control Panel. I installed via the official
Python installer for Windows.

I also don't understand how a start menu entry can begin an
installation but it is doing just that.


To me, this specifically indicates a registry mixup.
To prevent installation, delete or rename the downloaded install files.


Now there seems to be 4 IDLE
entries in my start menu. Two are valid (2.7.8, 3.4.2). The other two
are previous installations of Python 3.4  that I did at different
locations but removed later.


Delete those two.


I have stopped running IDLE from the
start menu due to this. Because I am never sure whether it will
re-install Python 3.4 at those old locations or not. Even right
clicking those entries in start menu causes the installations to
start. So I cannot even find the physical path of those entries.


With Win7, I keep the two current installed Idles pinned to the taskbar. 
 (I also have all three Python development versions pinned.)  With 5 
different builds of Idle to edit with, I open one or more first and then 
open to edit (often with Recent files).  I almost never use 'Edit with 
Idle'.



Maybe too much fiddling with the registry has caused it. Not sure
about that.


--
Terry Jan Reedy

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


About Modifying Globals

2014-12-04 Thread LJ
Hi All,

I have a quick question regarding the modification of global variables within 
functions. To illustrate, consider the following toy example:

a={1: set()}
b=9

def gt(l):
   a[1] = a[1] | set([l])

When calling this last function and checking the a dictionary, I get:

 gt(5)
 a
{1: set([5])}


The set in the dictionary was modified. The question is, why isn't it necessary 
to declare a as global within the gt function, as apposed to a case like  

def gt2(l):
   b=b+l

where I need to declare b as global within the function to avoid:

UnboundLocalError: local variable 'b' referenced before assignment.


I apologize if this question has been answered before.

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


Re: About Modifying Globals

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 7:09 AM, LJ luisjoseno...@gmail.com wrote:
 def gt(l):
a[1] = a[1] | set([l])


 def gt2(l):
b=b+l

These two may both look like they're assigning something, but one of
them is assigning directly to the name b, while the other assigns to
a subscripted element of a. In the first function, a is still the
same object after the change; in the second, b has been replaced by
a new object. That's why your second one needs a global declaration.
It's all about names and assignment; if you're not assigning to the
actual name, there's no need to declare where the name comes from,
because you're only referencing it, not replacing it.

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


Re: About Modifying Globals

2014-12-04 Thread Ian Kelly
On Thu, Dec 4, 2014 at 1:09 PM, LJ luisjoseno...@gmail.com wrote:

 Hi All,

 I have a quick question regarding the modification of global variables
within functions. To illustrate, consider the following toy example:

 a={1: set()}
 b=9

 def gt(l):
a[1] = a[1] | set([l])

 When calling this last function and checking the a dictionary, I get:

  gt(5)
  a
 {1: set([5])}


 The set in the dictionary was modified. The question is, why isn't it
necessary to declare a as global within the gt function, as apposed to a
case like

 def gt2(l):
b=b+l

 where I need to declare b as global within the function to avoid:

 UnboundLocalError: local variable 'b' referenced before assignment.

https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python

In the first case, you never assign to a; you only modify it. Since it's
never assigned locally the compiler knows it can't be a local variable, so
it automatically makes it a global.

In the second case, the assignment to b causes the compiler to treat the
variable as local by default, so it needs to be explicitly marked as global.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: time.monotonic() roll over

2014-12-04 Thread Akira Li
Ian Kelly ian.g.ke...@gmail.com writes:

 On Thu, Dec 4, 2014 at 11:09 AM, Marko Rauhamaa ma...@pacujo.net wrote:

 Chris Angelico ros...@gmail.com:

  It's not a Python issue. Python can't do anything more than ask the
  system, and if the system's value rolls over several times a year,
  Python can't magically cure that. The information has already been
  lost.

 Sure it could by having an invisible background thread occasionally call
 time.monotonic(). It could even be done on the side without a thread.

 Anyway, the idea of a clock is complicated:

  * the program could be stopped by a STOP signal

  * the program could be suspended from power management

  * the program could be resurrected from a virtual machine snapshot

  * the program could be migrated from a different physical machine

 This seems like a lot of effort to unreliably design around a problem that
 will matter to only a tiny fraction of users.

- people's computers are mostly on batteries (laptops, tablets,
  smartphones) -- suspended from power management use case
- corporations's computations are mostly virtualized -- possible
  ressurected, migrated use case

i.e., the opposite might be true -- non-virtualized PCs connected to AC
are (becoming) minority.


--
Akira

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


Re: time.monotonic() roll over

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 7:24 AM, Akira Li 4kir4...@gmail.com wrote:
 This seems like a lot of effort to unreliably design around a problem that
 will matter to only a tiny fraction of users.

 - people's computers are mostly on batteries (laptops, tablets,
   smartphones) -- suspended from power management use case
 - corporations's computations are mostly virtualized -- possible
   ressurected, migrated use case

 i.e., the opposite might be true -- non-virtualized PCs connected to AC
 are (becoming) minority.

That's a massive over-simplification, of course, but given that my
current desktop computer is running a dozen or so VMs for various
purposes (usually not more than 3-4 concurrently), I can't disagree
with you. However, there still are plenty of computers that are always
either fully running, or fully shut down; just because people _can_
suspend with applications running doesn't mean they _will_. (Quite a
few of my VMs, for instance, do not get saved/suspended - I shut them
down whenever I'm done with them. Even when I do suspend a VM, I often
terminate applications in it, and just use suspension to save having
to boot the OS every time. But that's partly because those VMs are the
ones running Windows for specific proprietary apps, and thus are
coping with the vagaries of those apps.)

In any case, it's not at all a problem to have the protection on
systems that won't actually need it. Much better than lacking the
protection on a system that does.

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


Urgent Need Sr. SDET (System) in Redmond, WA

2014-12-04 Thread amrish . babu2
Greetings

My name is Amrish  and I'm an IT Recruiter at Talented IT. Our records show 
that you are an experienced IT professional with experience relevant to one of 
my current contract openings. The job is located in Redmond, WA with one of our 
Fortune 100 client. They are looking for SR. SDET (System) and the following is 
a more detailed description of the job.

Position Title: Sr. SDET (System)
Location: Redmond, WA
Duration: 6+months
Position Requirements:
* Strong C# or C++ or Java skills, ability to work independently to 
maintain and expand a test tools as part of a team supporting several projects
* Experience writing code to test software on several platforms 
preferred (Windows, Android, etc..) with C++, C#, Java
* Self-motivated, able to follow instructions and work with the team's 
priorities and decisions.
* Strong test planning skills, able to come up with a robust test plan 
for a light weight UI application. 
* Ability to work independently to execute sign off (manual + 
automation) for the application.
* Strong experience writing test automation in C#, MSTest, UIAutomation 
experience a plus
* Experience writing test automation in C# or C++
* Self-start - willing and able to work independently with minimum 
supervision
Additional Responsibilities:
* Create, maintain and execute a set of test cases for all phases of 
the project
* Create well written bugs and communicate issues/defects to the team
* Validate new FW updates and all associated update scenarios
* Launch and execute a host of test tools for product validation

Thanks  Warm Regards,

Amrish B
Technical Recruiter 
TalentedIT Inc
 
amr...@talentedit.com
630-352-3306
800 W, Fifth Avenue, Suite 208A
Naperville, IL - 60563

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


Re: time.monotonic() roll over

2014-12-04 Thread Marko Rauhamaa
Chris Angelico ros...@gmail.com:

 Even when I do suspend a VM, I often terminate applications in it, and
 just use suspension to save having to boot the OS every time.

One interesting detail is DHCP leases.

When it is resumed from suspension, a linux computer thinks it still has
the old IP address, which might have already been granted to some other
computer on the network. Thus, linux distros are equipped with hacky
scripts that get executed soon after the machine is resumed (and before
a lot of damage is done). One of them forces a DHCP renegotiation.

A similar scheme is missing from virtual machines: a VM that wakes up
from a snapshot doesn't automatically go through the resume scripts,
even though it arguably should.

Systemd probably shuffles the situation still more.


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


Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Wolfgang Maier

On 04.12.2014 19:05, Chris Angelico wrote:


With os.path it definitely is. With the actual code in question, it's
a Python 2.7 project that mostly uses relative imports - inside
package.module1 is import module2 etc - and I was writing an
external script that calls on one of the modules.


What ? I'm usually thinking Python 3 not 2 and I'm never sure which 
Python 2.x has backported which feature of 3, but I thought implicit 
relative imports like you seem to describe are not working in 2.7 ?


Wolfgang

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


Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 7:56 AM, Wolfgang Maier
wolfgang.ma...@biologie.uni-freiburg.de wrote:
 On 04.12.2014 19:05, Chris Angelico wrote:


 With os.path it definitely is. With the actual code in question, it's
 a Python 2.7 project that mostly uses relative imports - inside
 package.module1 is import module2 etc - and I was writing an
 external script that calls on one of the modules.


 What ? I'm usually thinking Python 3 not 2 and I'm never sure which Python
 2.x has backported which feature of 3, but I thought implicit relative
 imports like you seem to describe are not working in 2.7 ?

Hmm, I'm not sure, but certainly it does seem to work that way. Typing
import foo from inside a package will import foo.py from the package
directory. I haven't dug into the details of _why_, and if ever the
project shifts to Python 3 (which I would like it to), we might have
to change some of the import lines, but I'd still like to be able to
reference foo.bar as meaning the bar top-level object in foo.py.

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


Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Wolfgang Maier

On 04.12.2014 22:30, Chris Angelico wrote:

On Fri, Dec 5, 2014 at 7:56 AM, Wolfgang Maier
wolfgang.ma...@biologie.uni-freiburg.de wrote:

On 04.12.2014 19:05, Chris Angelico wrote:



With os.path it definitely is. With the actual code in question, it's
a Python 2.7 project that mostly uses relative imports - inside
package.module1 is import module2 etc - and I was writing an
external script that calls on one of the modules.



What ? I'm usually thinking Python 3 not 2 and I'm never sure which Python
2.x has backported which feature of 3, but I thought implicit relative
imports like you seem to describe are not working in 2.7 ?


Hmm, I'm not sure, but certainly it does seem to work that way. Typing
import foo from inside a package will import foo.py from the package
directory. I haven't dug into the details of _why_, and if ever the
project shifts to Python 3 (which I would like it to), we might have
to change some of the import lines, but I'd still like to be able to
reference foo.bar as meaning the bar top-level object in foo.py.



I checked what the docs say about this and it is totally confusing (at 
least me):


https://docs.python.org/3/howto/pyporting.html#from-future-import-absolute-import 
says:



from __future__ import absolute_import

Implicit relative imports (e.g., importing spam.bacon from within 
spam.eggs with the statement import bacon) do not work in Python 3. This 
future statement moves away from that and allows the use of explicit 
relative imports (e.g., from . import bacon).


In Python 2.5 you must use the __future__ statement to get to use 
explicit relative imports and prevent implicit ones. In Python 2.6 
explicit relative imports are available without the statement, but you 
still want the __future__ statement to prevent implicit relative 
imports. In Python 2.7 the __future__ statement is not needed. In other 
words, unless you are only supporting Python 2.7 or a version earlier 
than Python 2.5, use this __future__ statement.



which I read as there has been a stepwise transition between 2.5 and 2.7 
so that 2.7 now behaves like Python 3 even without the __future__ statement.
OTOH, I believe you, of course, if you're saying implicit relative 
imports are working just fine in 2.7, but then how to interpret the In 
Python 2.7 the __future__ statement is not needed. above ?


Wolfgang

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


Re: time.monotonic() roll over

2014-12-04 Thread Ian Kelly
On Thu, Dec 4, 2014 at 1:24 PM, Akira Li 4kir4...@gmail.com wrote:

 Ian Kelly ian.g.ke...@gmail.com writes:
  This seems like a lot of effort to unreliably design around a problem
that
  will matter to only a tiny fraction of users.

 - people's computers are mostly on batteries (laptops, tablets,
   smartphones) -- suspended from power management use case
 - corporations's computations are mostly virtualized -- possible
   ressurected, migrated use case

 i.e., the opposite might be true -- non-virtualized PCs connected to AC
 are (becoming) minority.

By tiny fraction of users I was referring to people who a) work with
Python 3.3+, b) use time.monotonic in their code, and c) possibly have said
code running on versions of Windows older than Vista; not people who
suspend their systems or use VMs.

It's not clear to me whether those cases are relevant to the rollover
concern anyway. I wouldn't be shocked if the GetTickCount() function simply
stopped increasing while the system is suspended, since after all it's not
ticking during that time.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 9:10 AM, Wolfgang Maier
wolfgang.ma...@biologie.uni-freiburg.de wrote:
 which I read as there has been a stepwise transition between 2.5 and 2.7 so
 that 2.7 now behaves like Python 3 even without the __future__ statement.
 OTOH, I believe you, of course, if you're saying implicit relative imports
 are working just fine in 2.7, but then how to interpret the In Python 2.7
 the __future__ statement is not needed. above ?

Hmm. To be honest, I'm not sure. The Python 2.7 __future__ module
claims that absolute_import became standard in 3.0, not 2.7, which
seems to conflict with what you're seeing.

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


Re: time.monotonic() roll over

2014-12-04 Thread Marko Rauhamaa
Ian Kelly ian.g.ke...@gmail.com:

 It's not clear to me whether those cases are relevant to the rollover
 concern anyway. I wouldn't be shocked if the GetTickCount() function
 simply stopped increasing while the system is suspended, since after
 all it's not ticking during that time.

So, what's the semantics of time.sleep(), select.select() et al wrt
process or machine suspension?


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


RE: ORM opinion

2014-12-04 Thread Joseph L. Casale
 First recommendation: Less layers. Instead of SQLAlchemy, just import
 sqlite3 and use it directly. You should be able to switch out import
 sqlite as db for import psycopg2 as db or any other Python DB API
 module, and still have most/all of the benefit of the extra layer,
 without any extra imports.

Yeah, I am a fan of apsw but this is what I have now done. While I am
fine with writing my own abstractions, its certainly less overhead using
an orm.

 Secondly, see if you can keep the Python process running, instead of
 separately invoking it for every operation. This will mean some
 structural changes, but if you're specifying Python as the plugin
 interpreter (as opposed to, say, execute this program with these
 args, which is far more general), you can probably set up a wrapper
 script that calls on the user code repeatedly. Perhaps you could have
 a multiprocessing.Queue feeding tasks to the subprocess, via a little
 parent Python process. If you can't make those kinds of changes to the
 application, the next best would be a stubby driver process that feeds
 jobs to your real subprocess; the real work might be done by a
 Unix/TCP socket server, and the stub connects, feeds it some
 information, gets back a result, and disconnects. The stub will have
 an extremely minimal set of imports (the bear necessities of life, if
 you like), and the real process can import as much as it likes,
 because it won't be doing it for every operation.

I am stuck with the current architecture, but the idea you propose has
been thrown around, truth is I am not certain if we are enduring the
effort of such a large rewrite that Python is the tool to use (this is a
Windows application) but regardless your idea is one that is proposed.

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


Re: ORM opinion

2014-12-04 Thread Mark Lawrence

On 04/12/2014 23:20, Joseph L. Casale wrote:

First recommendation: Less layers. Instead of SQLAlchemy, just import
sqlite3 and use it directly. You should be able to switch out import
sqlite as db for import psycopg2 as db or any other Python DB API
module, and still have most/all of the benefit of the extra layer,
without any extra imports.


Yeah, I am a fan of apsw but this is what I have now done. While I am
fine with writing my own abstractions, its certainly less overhead using
an orm.


Secondly, see if you can keep the Python process running, instead of
separately invoking it for every operation. This will mean some
structural changes, but if you're specifying Python as the plugin
interpreter (as opposed to, say, execute this program with these
args, which is far more general), you can probably set up a wrapper
script that calls on the user code repeatedly. Perhaps you could have
a multiprocessing.Queue feeding tasks to the subprocess, via a little
parent Python process. If you can't make those kinds of changes to the
application, the next best would be a stubby driver process that feeds
jobs to your real subprocess; the real work might be done by a
Unix/TCP socket server, and the stub connects, feeds it some
information, gets back a result, and disconnects. The stub will have
an extremely minimal set of imports (the bear necessities of life, if
you like), and the real process can import as much as it likes,
because it won't be doing it for every operation.


I am stuck with the current architecture, but the idea you propose has
been thrown around, truth is I am not certain if we are enduring the
effort of such a large rewrite that Python is the tool to use (this is a
Windows application) but regardless your idea is one that is proposed.

Thanks,
jlc



Anything listed here http://www.pythoncentral.io/sqlalchemy-vs-orms/ 
you've not heard about?  I found peewee easy to use although I've 
clearly no idea if it suits your needs.  There's only one way to find out :)


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

Mark Lawrence

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


Re: Python docs disappointing

2014-12-04 Thread jtan
On Thu, Dec 4, 2014 at 6:27 PM, Albert van der Horst 
alb...@spenarnc.xs4all.nl wrote:

 In article mailman.16030.1416502295.18130.python-l...@python.org,
 Joel Goldstick  joel.goldst...@gmail.com wrote:
 SNIP

 Plain google is far superior in finding information.

 And you tell me that writing yet another tutorial would improve that?
 No, there is just one way. The powers that be should look critically
 at their website, and test it with a beginners hat on.


The community is in the best place to document  language.  And community is
not limited to the official doc site.  That includes outsiders doing their
share of writing tutorials and whatnot.  I don't find anything wrong about
that.

If you have problems with the docs, why not be a part of the solution?
Write a doc for numpy and contribute it to the community.

Man, I remember using MSDN 15 years ago when working with Visual Studio.
And it's a paid product with docs. It's horrendous.



 
 I'm trying to wrap my mind around DOCUMENTION being STUPID.
 
 --
 Joel Goldstick
 http://joelgoldstick.com

 Groetjes Albert
 --
 Albert van der Horst, UTRECHT,THE NETHERLANDS
 Economic growth -- being exponential -- ultimately falters.
 albert@spearc.xs4all.nl =n http://home.hccnet.nl/a.w.m.van.der.horst

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




-- 
Freelance Grails http://grails.asia/ and Java http://javadevnotes.com/
 developer
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ORM opinion

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 10:20 AM, Joseph L. Casale
jcas...@activenetwerx.com wrote:
 I am stuck with the current architecture, but the idea you propose has
 been thrown around, truth is I am not certain if we are enduring the
 effort of such a large rewrite that Python is the tool to use (this is a
 Windows application) but regardless your idea is one that is proposed.

Windows? Then even more important to minimize the number of processes
you create. They're very costly on Windows.

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


RE: ORM opinion

2014-12-04 Thread Joseph L. Casale
 Anything listed here http://www.pythoncentral.io/sqlalchemy-vs-orms/
 you've not heard about?  I found peewee easy to use although I've
 clearly no idea if it suits your needs.  There's only one way to find out :)

Hi Mark,
I found that article before posting and some of the guys here have already
started using peewee. I don't have much time with it yet. So far all I can say
is its unfortunate some package authors take such an approach to naming.

I can't fathom sitting across the table from a CTO and CFO who are authorizing
a 7 figure expenditure and then telling them about my peewee app with its
playhouse module? Sigh...

That aside, compared to all the rest it seems to fit the requirements.

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


Re: ORM opinion

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 10:43 AM, Joseph L. Casale
jcas...@activenetwerx.com wrote:
 I found that article before posting and some of the guys here have already
 started using peewee. I don't have much time with it yet. So far all I can say
 is its unfortunate some package authors take such an approach to naming.

 I can't fathom sitting across the table from a CTO and CFO who are authorizing
 a 7 figure expenditure and then telling them about my peewee app with its
 playhouse module? Sigh...

Then never EVER get into Ruby. That seems to be the norm for gem naming.

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


Re: About Modifying Globals

2014-12-04 Thread Dave Angel

On 12/04/2014 03:09 PM, LJ wrote:

Hi All,

I have a quick question regarding the modification of global variables within 
functions. To illustrate, consider the following toy example:

a={1: set()}
b=9

def gt(l):
a[1] = a[1] | set([l])

When calling this last function and checking the a dictionary, I get:


gt(5)
a

{1: set([5])}


The set in the dictionary was modified. The question is, why isn't it necessary 
to declare a as global within the gt function, as apposed to a case like

def gt2(l):
b=b+l

where I need to declare b as global within the function to avoid:

UnboundLocalError: local variable 'b' referenced before assignment.



The reason this sort of thing seems to confuse lots of people is that we 
insist on calling these things assignments.  The thing that's global is 
the name 'a'.  It's global because it's in the global() namespace of 
some module.


The data that's assigned to it is an object.  You bind the object to 
the name 'a' with an assignment statement.  If that object is immutable, 
then the only way to change 'a' is to do another assignment.


But if the object is mutable, as a set is, then it can change as much as 
you like without rebinding the name.


A given object may have any number of names bound to it, including no 
names.  An example of that could be objects that are referenced only in 
some list.


Python doesn't have declarations, so when a function is compiled, the 
compiler has to infer what names are to be local and what are not.  The 
rule it normally uses is roughly based on whether an assignment occurs 
somewhere inside the function.



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


Re: Python docs disappointing

2014-12-04 Thread Michael Torrie
On 12/04/2014 03:27 AM, Albert van der Horst wrote:
 That doesn't help. I'm a very experienced programmer and work in
 routinely a dozen languages. Sometimes I do python. I want to do
 numeric work. I remember the name numpy. It is important, everybody
 knows it, it is all over the place. So I want to find its docs,
 or some lead, whatever. I go to the official Python site,
 http://docs.python.org and type in numpy in the search machine.

 It is embarassing, try it!

That would indeed be embarrassing if numpy was a part of Python or had
anything to do with the python.org project.  In fact it doesn't.  It's a
third-party library for use with python.  As a beginner I would type in
numpy to google and see what happens.  Would you expect to go to
Microsoft MSDN to find information on a third-party library, such as,
say, SDL or Allegro, that you use in Visual Studio?

 Plain google is far superior in finding information.

Of course, since the information you are looking is third-party to
python.org for a start.

 And you tell me that writing yet another tutorial would improve that?
 No, there is just one way. The powers that be should look critically
 at their website, and test it with a beginners hat on.

Your ire is misplaced.  The GP was saying that if there is a deficiency
in the Python docs you can help fix that. If there's a deficiency in the
numpy docs, you can ask them about fixing that's what he means.

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


Re: About Modifying Globals

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 10:54 AM, Dave Angel da...@davea.name wrote:
 Python doesn't have declarations, so when a function is compiled, the
 compiler has to infer what names are to be local and what are not.  The rule
 it normally uses is roughly based on whether an assignment occurs somewhere
 inside the function.

Not strictly true; Python just inverts the C model. In C, you declare
your locals; in Python, you declare your globals. The global x
statement is a declaration. But otherwise, yes. When a function is
compiled, the compiler has to figure out what's local and what's
global.

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


Re: time.monotonic() roll over

2014-12-04 Thread Steven D'Aprano
Marko Rauhamaa wrote:

 So, if I call
 
 time.sleep(86400)
 
 and the program is suspended for 24 hours, should time.sleep() return
 right after it is resumed or after another 24 hours?

If the program is suspended, then no time should pass for that program.
Since sleep() is given in terms of a duration, not an absolute time (sleep
until now + 24 hours), if no time passes for that program, sleep() should
sleep for 24 hours *after the program resumes*.

Unfortunately a lot of systems get that wrong. E.g. I just ran sleep 30
from my Linux shell, immediately paused it using Ctrl-Z, waited a couple of
minutes, and used fg to continue. It returned immediately.

Why is this behaviour wrong?

Consider why people might call sleep. They're normally calling it to wait
for something else to complete. Often that something else is an external
process (wait a few seconds to let the hard drive finish syncing, wait for
the web server to get less busy, wait for the user to catch up...) but it
might be another thread in the same process. If the process is suspended,
that other thread won't get to run, and so even though time on the outside
has continued to move forward, from the perspective of the process, it
hasn't.

The same applies even more so if the process is running in a virtual machine
which has just been suspended.

The same applies if the system clock is adjusted mid-sleep. If I call sleep
60, then immediately adjust the clock forward an hour (say, due to a
daylight savings adjustment), that shouldn't cause sleep to return. It
should still suspend for 60 seconds.



-- 
Steven

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


Re: time.monotonic() roll over

2014-12-04 Thread Nobody
On Thu, 04 Dec 2014 16:25:44 +0100, ast wrote:

 There is no roll over problem with time.time() since the very
 first one in planned far in the future, but time.time() can go
 backward when a date update throught NTP server is done.

 time.monotonic() is monotonic but roll over often (every 49.7 
 days)
 
 So what to do ? 
 
 Using time.monotonic() and take some actions if a roll over 
 is detected ?

One possibility: use both and monitor the skew (the difference between the
two clocks).

The first time you read them, calculate and store the epoch for
time.monotic(). On subsequent calls, repeat the calculation and check that
the epoch is approximately the same. If it isn't, the difference between
the actual and expected values should be close to an exact multiple of
2**32 milliseconds, which tells you how many times time.monotonic() has
rolled over.

Adjustments to the clock on which time.time() is based shouldn't exceed a
few seconds per year, so there's not much risk that you won't be able to
figure out the correct adjustment.

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


Re: time.monotonic() roll over

2014-12-04 Thread Dave Angel

On 12/04/2014 07:39 PM, Steven D'Aprano wrote:

Marko Rauhamaa wrote:


So, if I call

time.sleep(86400)

and the program is suspended for 24 hours, should time.sleep() return
right after it is resumed or after another 24 hours?


If the program is suspended, then no time should pass for that program.
Since sleep() is given in terms of a duration, not an absolute time (sleep
until now + 24 hours), if no time passes for that program, sleep() should
sleep for 24 hours *after the program resumes*.

Unfortunately a lot of systems get that wrong. E.g. I just ran sleep 30
from my Linux shell, immediately paused it using Ctrl-Z, waited a couple of
minutes, and used fg to continue. It returned immediately.

Why is this behaviour wrong?

Consider why people might call sleep. They're normally calling it to wait
for something else to complete. Often that something else is an external
process (wait a few seconds to let the hard drive finish syncing, wait for
the web server to get less busy, wait for the user to catch up...) but it
might be another thread in the same process. If the process is suspended,
that other thread won't get to run, and so even though time on the outside
has continued to move forward, from the perspective of the process, it
hasn't.

The same applies even more so if the process is running in a virtual machine
which has just been suspended.

The same applies if the system clock is adjusted mid-sleep. If I call sleep
60, then immediately adjust the clock forward an hour (say, due to a
daylight savings adjustment), that shouldn't cause sleep to return. It
should still suspend for 60 seconds.


And I say you have it exactly backwards.  People should NOT call sleep() 
to measure something, they should only call it to release the processor 
for up to a specified time.  That time should normally be a major 
fraction of the time that some external event is expected to take.  Then 
the code should check the external event and sleep some more if necessary.


If efficiency were not an issue, the code would just do a busy loop, 
checking the external condition repeatedly.


Since the OS has no way of knowing whether the thing being waited for is 
a thread, another process, a human being, a network operation, or the 
end of the world, the interpretation of sleep needs to be the most 
conservative one.  There are many ways of suspending a process, and some 
of them will also suspend the external event.  Since the OS cannot know 
which case is significant, it has to return control to the caller at the 
soonest of the many possible interpretations.



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


Re: About Modifying Globals

2014-12-04 Thread Steven D'Aprano
LJ wrote:

 Hi All,
 
 I have a quick question regarding the modification of global variables
 within functions. To illustrate, consider the following toy example:
 
 a={1: set()}
 b=9
 
 def gt(l):
a[1] = a[1] | set([l])

The difference between this example and your second one:

 def gt2(l):
b=b+l


is that the second is a binding operation and the first is not.

Binding operations include direct assignment to a name, deletions, imports
and a few other things:

b = 23
del b
import sys as b


are all considered binding operations, since they bind a value to a name
(or, in the case of del, unbind it). Inside functions, the compiler follows
the rule that any binding operation makes that variable a local for the
entire function, unless declared global.

So in the gt2() example, b = b + 1 is interpreted as this:

- look up local variable b;
- add one to the result;
- assign it to local variable b.

which fails because b hasn't got a value yet.

This is different from, say, Lua, where that line would be interpreted as: 

- look up local variable b;
- if that doesn't exist, look up global variable b instead;
- add one to the result;
- assign it to local variable b.

But that's not what Python does.

On the other hand, your first example doesn't include a direct assignment to
a name:

a[1] = a[1] | set([l])


looks like an assignment, but it's actually a method call! That is
approximately equivalent to:

a.__setitem__(1, X)

with X equal to the right hand side. Since that's a method call, it doesn't
count as a binding operation and the compiler doesn't treat a as a local,
it treats it as global, so the lookup on the right hand side succeeds, and
the method call modifies the set in place.




-- 
Steven

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


Re: About Modifying Globals

2014-12-04 Thread shiyao.ma
On 12/04, LJ wrote:
 Hi All,

 I have a quick question regarding the modification of global variables within 
 functions. To illustrate, consider the following toy example:

 a={1: set()}
 b=9

 def gt(l):
a[1] = a[1] | set([l])

 When calling this last function and checking the a dictionary, I get:

  gt(5)
  a
 {1: set([5])}


 The set in the dictionary was modified. The question is, why isn't it 
 necessary to declare a as global within the gt function, as apposed to a case 
 like

 def gt2(l):
b=b+l

 where I need to declare b as global within the function to avoid:

 UnboundLocalError: local variable 'b' referenced before assignment.

Well. To understand the difference, one first has to know the mechanism of 
CPython.
When you have a script, CPython will first parse it, and then generate bytecode 
representation, and then finally execute it.
If you take a look at the bytecode of both your source code, you will notice, 
that.

In the first snippet, there is something like LOAD_NAME, which loads the global 
name a.
However, in the second snippet, there would be a LOAD_FAST, which loads the 
local name b (which is, technically speaking, stored in the PyFrameObj).

The reason Python treats it differently, is, IIUC, for better semantic meaning, 
and also, for huge performance improvement.


Hope that explains.


Regards.

-- 
Shiyao Ma
http://introo.me
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: time.monotonic() roll over

2014-12-04 Thread Steven D'Aprano
Dave Angel wrote:

 On 12/04/2014 07:39 PM, Steven D'Aprano wrote:
 Marko Rauhamaa wrote:

 So, if I call

 time.sleep(86400)

 and the program is suspended for 24 hours, should time.sleep() return
 right after it is resumed or after another 24 hours?

 If the program is suspended, then no time should pass for that program.
 Since sleep() is given in terms of a duration, not an absolute time
 (sleep until now + 24 hours), if no time passes for that program,
 sleep() should sleep for 24 hours *after the program resumes*.

 Unfortunately a lot of systems get that wrong. E.g. I just ran sleep 30
 from my Linux shell, immediately paused it using Ctrl-Z, waited a couple
 of minutes, and used fg to continue. It returned immediately.

 Why is this behaviour wrong?

 Consider why people might call sleep. They're normally calling it to wait
 for something else to complete. Often that something else is an external
 process (wait a few seconds to let the hard drive finish syncing, wait
 for the web server to get less busy, wait for the user to catch up...)
 but it might be another thread in the same process. If the process is
 suspended, that other thread won't get to run, and so even though time on
 the outside has continued to move forward, from the perspective of the
 process, it hasn't.

 The same applies even more so if the process is running in a virtual
 machine which has just been suspended.

 The same applies if the system clock is adjusted mid-sleep. If I call
 sleep 60, then immediately adjust the clock forward an hour (say, due to
 a daylight savings adjustment), that shouldn't cause sleep to return. It
 should still suspend for 60 seconds.
 
 And I say you have it exactly backwards.  People should NOT call sleep()
 to measure something, 

I never suggested you call sleep to measure something. You call sleep to
*wait* for something. I even used the word wait four times in the above.


 they should only call it to release the processor 
 for up to a specified time.

That's silly. Zero is up to any positive value you want. Do you really
intend to say that this would be an acceptable implementation of sleep()?

def sleep(delay):
return



 That time should normally be a major 
 fraction of the time that some external event is expected to take.  Then
 the code should check the external event and sleep some more if necessary.
 
 If efficiency were not an issue, the code would just do a busy loop,
 checking the external condition repeatedly.

In many cases, you don't have an external condition that can be easily or
practically checked. So you estimate how long you need to wait, add a
safety margin, and write sleep 5 (for some value of 5), and hope.

If you are a sys admin writing a script that runs during boot time, you do
that a lot. Or at least the ones I work with do :-)


 Since the OS has no way of knowing whether the thing being waited for is
 a thread, another process, a human being, a network operation, or the
 end of the world, the interpretation of sleep needs to be the most
 conservative one.

I agree! And returning immediately is not the most conservative one.

The most conservative approach is to assume that while you're suspended,
*everything else* is suspended too, so when you resume you still have to
sleep for the full N seconds.


 There are many ways of suspending a process, and some 
 of them will also suspend the external event.  Since the OS cannot know
 which case is significant, it has to return control to the caller at the
 soonest of the many possible interpretations.

/s/latest/



-- 
Steven

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


help with processing text file

2014-12-04 Thread C. Ng
Hi,

Given the sample text file below (where the gibberish represent the irrelevant 
portions) :


abcddsdfffgfg
ggfhghghgfhghgh   round 5 xccdcxcfd
sdfdffdfbcvcvbbvnghg score = 0.4533 
abcddsdfffgfg round 5 level = 0.15
ggfhghghgfhghgh   round 10 dfsdfdcdsd
sdfdffdfbcvcvbbvnghg score = 0.4213 
sdsdaawsdsround 10 level = 0.13
..and so on


I would like to extract the values for round, score and level:
5 0.4533 0.15
10 0.4213 0.13
and so on...

Please advise me how it can be done, and what Python functions are useful.

Thanks.








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


Re: help with processing text file

2014-12-04 Thread Gary Herron

On 12/04/2014 08:46 PM, C. Ng wrote:

Hi,

Given the sample text file below (where the gibberish represent the irrelevant 
portions) :


abcddsdfffgfg
ggfhghghgfhghgh   round 5 xccdcxcfd
sdfdffdfbcvcvbbvnghg score = 0.4533
abcddsdfffgfg round 5 level = 0.15
ggfhghghgfhghgh   round 10 dfsdfdcdsd
sdfdffdfbcvcvbbvnghg score = 0.4213
sdsdaawsdsround 10 level = 0.13
..and so on


I would like to extract the values for round, score and level:
5 0.4533 0.15
10 0.4213 0.13
and so on...

Please advise me how it can be done, and what Python functions are useful.

Thanks.


I would use regular expressions.  See 
https://docs.python.org/3/library/re.html


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


Re: Python handles globals badly.

2014-12-04 Thread William Ray Wing

 On Dec 4, 2014, at 8:56 PM, Dennis Lee Bieber wlfr...@ix.netcom.com wrote:
 
 On Thu, 04 Dec 2014 14:51:14 +0200, Marko Rauhamaa ma...@pacujo.net
 declaimed the following:
 
 Chris Angelico ros...@gmail.com:
 
 A lot of programs don't use threads, and therefore cannot have thread
 safety problems - or, looking at it the other way, do not care about
 thread safetiness. It's like having Neil Armstrong wear water wings to
 make sure he won't drown in the Sea of Tranquility.
 
 The water wings would be too unwieldy since they'd have to be six times
 as large on the moon. It's all about risk/benefit analysis.
 
   Actually, since the pull of gravity is 1/6th that of earth, but the
 density of the water is the Sea is the same, the water wings should be
 smaller to provide the same degree of lift.
 
   Of course, the lack of atmospheric pressure is going to result in the
 water wings expanding, perhaps to the bursting point...
 -- 
   Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/
 
 -- 
 https://mail.python.org/mailman/listinfo/python-list

Wrong, again I’m afraid.  Assume for a moment you are under a dome pressurized 
to one standard atmosphere in some future moon colony and are in a swimming 
pool.  Both you AND the water are under 1/6th as much gravitational pull.  
Neither your density nor the water’s density has changed. Density is a function 
of the atomic weight of the elements making up both you and the water and the 
inter-atomic/molecular spacing of those elements.  Neither has changed.  
Therefore the volume of buoyant material (in the case of water wings, air) 
necessary to keep you above water is the same on earth as it would be on the 
moon.  

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


Re: time.monotonic() roll over

2014-12-04 Thread Dave Angel

On 12/04/2014 09:54 PM, Steven D'Aprano wrote:

Dave Angel wrote:


On 12/04/2014 07:39 PM, Steven D'Aprano wrote:

Marko Rauhamaa wrote:


So, if I call

time.sleep(86400)

and the program is suspended for 24 hours, should time.sleep() return
right after it is resumed or after another 24 hours?


If the program is suspended, then no time should pass for that program.
Since sleep() is given in terms of a duration, not an absolute time
(sleep until now + 24 hours), if no time passes for that program,
sleep() should sleep for 24 hours *after the program resumes*.

Unfortunately a lot of systems get that wrong. E.g. I just ran sleep 30
from my Linux shell, immediately paused it using Ctrl-Z, waited a couple
of minutes, and used fg to continue. It returned immediately.

Why is this behaviour wrong?

Consider why people might call sleep. They're normally calling it to wait
for something else to complete. Often that something else is an external
process (wait a few seconds to let the hard drive finish syncing, wait
for the web server to get less busy, wait for the user to catch up...)
but it might be another thread in the same process. If the process is
suspended, that other thread won't get to run, and so even though time on
the outside has continued to move forward, from the perspective of the
process, it hasn't.

The same applies even more so if the process is running in a virtual
machine which has just been suspended.

The same applies if the system clock is adjusted mid-sleep. If I call
sleep 60, then immediately adjust the clock forward an hour (say, due to
a daylight savings adjustment), that shouldn't cause sleep to return. It
should still suspend for 60 seconds.


And I say you have it exactly backwards.  People should NOT call sleep()
to measure something,


I never suggested you call sleep to measure something. You call sleep to
*wait* for something. I even used the word wait four times in the above.


I mentioned measurement because I couldn't imagine any other reason you 
would be arguing the other side.  You're certainly not waiting for the 
event, wleep has no parameters that let you specify what event you might 
be waiting for.






they should only call it to release the processor
for up to a specified time.


That's silly. Zero is up to any positive value you want. Do you really
intend to say that this would be an acceptable implementation of sleep()?

def sleep(delay):
 return



Yes, of course.  Not very machine efficient, but perfectly correct. 
Because the testing code will make its check, and turn right around and 
issue another sleep call.  This is called a busy loop, and I mention it 
below.  It is the degenerate (if inefficient) form.






That time should normally be a major
fraction of the time that some external event is expected to take.  Then
the code should check the external event and sleep some more if necessary.

If efficiency were not an issue, the code would just do a busy loop,
checking the external condition repeatedly.


In many cases, you don't have an external condition that can be easily or
practically checked. So you estimate how long you need to wait, add a
safety margin, and write sleep 5 (for some value of 5), and hope.

If you are a sys admin writing a script that runs during boot time, you do
that a lot. Or at least the ones I work with do :-)



Since the OS has no way of knowing whether the thing being waited for is
a thread, another process, a human being, a network operation, or the
end of the world, the interpretation of sleep needs to be the most
conservative one.


I agree! And returning immediately is not the most conservative one.

The most conservative approach is to assume that while you're suspended,
*everything else* is suspended too, so when you resume you still have to
sleep for the full N seconds.



There are many ways of suspending a process, and some
of them will also suspend the external event.  Since the OS cannot know
which case is significant, it has to return control to the caller at the
soonest of the many possible interpretations.


/s/latest/



I guess we'll just have to agree to disagree.  I can now see the 
usefulness of a function like you describe, I just can't imagine it 
being the sleep() we've all come to know and love.



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


The binding operator, and what gets bound to what (was: About Modifying Globals)

2014-12-04 Thread Ben Finney
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info writes:

 LJ wrote:

  def gt(l):
 a[1] = a[1] | set([l])

 The difference between this example and your second one:

  def gt2(l):
 b=b+l


 is that the second is a binding operation and the first is not.

I disagree; they're both binding operations (and they both use the
binding operator ‘=’ to express this).

The difference between the two is: what reference is being bound?

* In the first example, the reference is ‘a[1]’.

  That reference is re-bound to a different object; whatever object it
  was previously bound to is forgotten.

* In the second example, the reference is ‘b’.

  That reference is re-bound to a different object; whatever object it
  was previously bound to is forgotten.

So, both of them bind (or re-bind) a reference to an object. The only
difference between the operations is the parameters.

-- 
 \  “People always ask me, ‘Where were you when Kennedy was shot?’ |
  `\Well, I don't have an alibi.” —Emo Philips |
_o__)  |
Ben Finney

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


Re: help with processing text file

2014-12-04 Thread Dave Angel

On 12/04/2014 11:46 PM, C. Ng wrote:

Hi,

Given the sample text file below (where the gibberish represent the irrelevant 
portions) :


abcddsdfffgfg
ggfhghghgfhghgh   round 5 xccdcxcfd
sdfdffdfbcvcvbbvnghg score = 0.4533
abcddsdfffgfg round 5 level = 0.15
ggfhghghgfhghgh   round 10 dfsdfdcdsd
sdfdffdfbcvcvbbvnghg score = 0.4213
sdsdaawsdsround 10 level = 0.13
..and so on


I would like to extract the values for round, score and level:
5 0.4533 0.15
10 0.4213 0.13
and so on...

Please advise me how it can be done, and what Python functions are useful.

There's lots of ambiguity in that specification.  Can you be sure, for 
example that the gibberish does not ever include the string round, 
score, or level?


Can you be sure that the relevant 3 lines for a given record are 
adjacent, and in that order?  Do you happen to know that round always 
starts in a particular column?, and that score starts in another 
particular column?


How would you solve it by hand?  Something like the following?

OPen the file.
Skip all lines till column 19-23 contain round
find the first space delimited field starting in column 25, and call it 
round_num


On the next line, split the line into words, and save the last word into 
score_val


On the next line, take a substring of the line starting with column 23, 
parse it into words, and store the second word in level_num


Save the values round_num,score_val, and level_num in a tuple, or a 
string, or whatever you find useful, and append it to a result list.


Repeat till end of file.

Lots more error checking is possible, and advisable, but without knowing 
what the file really looks like, I see no point in guessing.


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


Re: time.monotonic() roll over

2014-12-04 Thread Marko Rauhamaa
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info:

 Unfortunately a lot of systems get that wrong. E.g. I just ran sleep
 30 from my Linux shell, immediately paused it using Ctrl-Z, waited a
 couple of minutes, and used fg to continue. It returned immediately.

 Why is this behaviour wrong?

I think the #1 thing is to specify the behavior clearly. I'm not seeing
that so it is impossible to say if the GNU coreutils sleep or
time.sleep() does what it was designed to do. I must admit I have
neglected to document that situation in some of my related designs.

Also, I think what is right or wrong depends on the use case. Ideally,
there are facilities to implement the desired semantics.

If a program spends long periods in an induced coma (which is becoming
more and more common nowadays), the whole design of the program is under
quite a bit of strain. What to do with time-based statistics collection?
Should periodic operations catch up with the lost time by repeating in a
tight loop for a zillion times? What to do with impossible,
out-of-order event sequences?


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


dict turn to list at runtime unexpected!!!

2014-12-04 Thread telnetgmike
Why the following code gives me errors??? And why the print statement run 2 
times? I'll be appreciated your helps, thanks,
addrnum_dict = {'a':1,'b':2}
def orderaddrtimes():
global addrnum_dict
print type(addrnum_dict)

addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], reverse 
= True)
#addrnum_dict = OrderedDict(sorted(addrnum_dict.items(), key=lambda t: 
t[0]))
if __name__ == '__main__':
kinds = [a,b]
for tmp_kind in kinds:
orderaddrtimes()

##
errors:
python aaa.py 
type 'dict'
type 'list'
Traceback (most recent call last):
  File aaa.py, line 16, in module
orderaddrtimes()
  File aaa.py, line 11, in orderaddrtimes
addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], reverse 
= True)
AttributeError: 'list' object has no attribute 'iteritems'
-- 
https://mail.python.org/mailman/listinfo/python-list


dict turn to list unexpected at runtime???

2014-12-04 Thread telnetgm...@gmail.com
Why the following code gives me errors??? And why the print statement run 2 
times? I'll be appreciated your helps, thanks,
addrnum_dict = {'a':1,'b':2}
def orderaddrtimes():
global addrnum_dict
print type(addrnum_dict)

addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], reverse 
= True)
#addrnum_dict = OrderedDict(sorted(addrnum_dict.items(), key=lambda t: 
t[0]))
if __name__ == '__main__':
kinds = [a,b]
for tmp_kind in kinds:
orderaddrtimes()

##
errors:
python aaa.py 
type 'dict'
type 'list'
Traceback (most recent call last):
  File aaa.py, line 16, in module
orderaddrtimes()
  File aaa.py, line 11, in orderaddrtimes
addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], reverse 
= True)
AttributeError: 'list' object has no attribute 'iteritems'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: dict turn to list unexpected at runtime???

2014-12-04 Thread Cameron Simpson

On 05Dec2014 15:01, telnetgm...@gmail.com telnetgm...@gmail.com wrote:

Why the following code gives me errors??? And why the print statement run 2 
times? I'll be appreciated your helps, thanks,
addrnum_dict = {'a':1,'b':2}
def orderaddrtimes():
   global addrnum_dict
   print type(addrnum_dict)

   addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], reverse = 
True)


Because of the line above. The return value of sorted() is a list. You assign 
that list to addrnum_dict. That name now refers to the list from sorted().


Because you call the orderaddrtimes() function twice, the second time you call 
it addrnum_dict is the list from the first call. Not a dict.


You would be best to not assign the result of sorte() to addrnum_dict.  
Assign it to something else.



   #addrnum_dict = OrderedDict(sorted(addrnum_dict.items(), key=lambda t: t[0]))
if __name__ == '__main__':
   kinds = [a,b]
   for tmp_kind in kinds:
   orderaddrtimes()

##
errors:
python aaa.py
type 'dict'
type 'list'
Traceback (most recent call last):
 File aaa.py, line 16, in module
   orderaddrtimes()
 File aaa.py, line 11, in orderaddrtimes
   addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], reverse = 
True)
AttributeError: 'list' object has no attribute 'iteritems'


Thank you for including the complete error output. This is very helpful, and 
often essential.


Cheers,
Cameron Simpson c...@zip.com.au

Who are all you people and why are you in my computer?  - Kibo
--
https://mail.python.org/mailman/listinfo/python-list


Re: dict turn to list unexpected at runtime???

2014-12-04 Thread Dan Sommers
On Fri, 05 Dec 2014 15:01:51 +0800, telnetgm...@gmail.com wrote:

 Why the following code gives me errors??? And why the print statement run 2 
 times? ...
 addrnum_dict = {'a':1,'b':2}
 def orderaddrtimes():
 global addrnum_dict
 print type(addrnum_dict)
 
 addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], 
 reverse = True)
 #addrnum_dict = OrderedDict(sorted(addrnum_dict.items(), key=lambda t: 
 t[0]))
 if __name__ == '__main__':
 kinds = [a,b]
 for tmp_kind in kinds:
 orderaddrtimes()

The print statement runs twice because you're calling orderaddrtimes
twice, once for a and once for b.

addrnum_dict becomes a list when you bind it to the return value from
sorted on the first call to orderaddrtimes; you get the error on the
second call.

HTH,
Dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Do you like the current design of python.org?

2014-12-04 Thread dieter
Peter Otten __pete...@web.de writes:

 Did you ever hit the Socialize button?

No.

 Are you eager to see the latest 
 tweets when you are reading a PEP?

No.

 Do you run away screaming from a page 
 where nothing moves without you hitting a button?

No, I like pages where I am in control.

 Do you appreciate the 
 choice between ten or so links to the documentation?

Maybe, I have bookmarked my route to the documentation - and
usually do not see the choices. But, when I need to update my
bookmark, I might like the choices.

 ...
 PS: Is there a twitter.com something that I can block to trade black friday 
 and cyber monkey sales for a box with a good old error message?

I have used a firewall to reject connections to intrusive sites
(like facebook and Google). Under Linux, I used lsof -i
to find out to which sites connections are made without my knowledge.
I then used http://www.heise.de/netze/tools/whois/; to find out to
which organisations those sites belong and banned them with
firewall rules when those connections did not seem to satisfy
a justified aim.


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


Re: dict turn to list unexpected at runtime???

2014-12-04 Thread telnetgmike
On Friday, December 5, 2014 3:20:14 PM UTC+8, Cameron Simpson wrote:
 On 05Dec2014 15:01, telnetgm...@gmail.com telnetgm...@gmail.com wrote:
 Why the following code gives me errors??? And why the print statement run 2 
 times? I'll be appreciated your helps, thanks,
 addrnum_dict = {'a':1,'b':2}
 def orderaddrtimes():
 global addrnum_dict
 print type(addrnum_dict)
 
 addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], 
  reverse = True)
 
 Because of the line above. The return value of sorted() is a list. You 
 assign 
 that list to addrnum_dict. That name now refers to the list from sorted().
 
 Because you call the orderaddrtimes() function twice, the second time you 
 call 
 it addrnum_dict is the list from the first call. Not a dict.
 
 You would be best to not assign the result of sorte() to addrnum_dict.  
 Assign it to something else.
 
 #addrnum_dict = OrderedDict(sorted(addrnum_dict.items(), key=lambda t: 
  t[0]))
 if __name__ == '__main__':
 kinds = [a,b]
 for tmp_kind in kinds:
 orderaddrtimes()
 
 ##
 errors:
 python aaa.py
 type 'dict'
 type 'list'
 Traceback (most recent call last):
   File aaa.py, line 16, in module
 orderaddrtimes()
   File aaa.py, line 11, in orderaddrtimes
 addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], 
  reverse = True)
 AttributeError: 'list' object has no attribute 'iteritems'
 
 Thank you for including the complete error output. This is very helpful, and 
 often essential.
 
 Cheers,
 Cameron Simpson c...@zip.com.au
 
 Who are all you people and why are you in my computer?  - Kibo

Thanks for your help!
I admit I have poor experience on python, and I careless also.
Thanks! 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: dict turn to list at runtime unexpected!!!

2014-12-04 Thread telnetgmike
On Friday, December 5, 2014 2:56:50 PM UTC+8, telne...@gmail.com wrote:
 Why the following code gives me errors??? And why the print statement run 2 
 times? I'll be appreciated your helps, thanks,
 addrnum_dict = {'a':1,'b':2}
 def orderaddrtimes():
 global addrnum_dict
 print type(addrnum_dict)
 
 addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], 
 reverse = True)
 #addrnum_dict = OrderedDict(sorted(addrnum_dict.items(), key=lambda t: 
 t[0]))
 if __name__ == '__main__':
 kinds = [a,b]
 for tmp_kind in kinds:
 orderaddrtimes()
 
 ##
 errors:
 python aaa.py 
 type 'dict'
 type 'list'
 Traceback (most recent call last):
   File aaa.py, line 16, in module
 orderaddrtimes()
   File aaa.py, line 11, in orderaddrtimes
 addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], 
 reverse = True)
 AttributeError: 'list' object has no attribute 'iteritems'

https://groups.google.com/forum/#!topic/comp.lang.python/eY7jecLLO48
this man helped me find out the problem.
-- 
https://mail.python.org/mailman/listinfo/python-list


  1   2   >