A new version (0.3.5) of python-gnupg has been released.

2013-09-01 Thread Vinay Sajip
A new version of the Python module which wraps GnuPG has been
released.

What Changed?
=
This is a minor enhancement and bug-fix release. See the project
website ( http://code.google.com/p/python-gnupg/ ) for more
information. Summary:

Added improved shell quoting to guard against shell injection attacks.
Added search_keys() and send_keys() methods to interact with keyservers.
A symmetric cipher algorithm can now be specified when encrypting.
UTF-8 encoding is used as a fall back when no other encoding can be determined.
The key length now defaults to 2048 bits.
A default Name-Comment field is no longer provided during key generation.

What Does It Do?

The gnupg module allows Python programs to make use of the
functionality provided by the Gnu Privacy Guard (abbreviated GPG or
GnuPG). Using this module, Python programs can encrypt and decrypt
data, digitally sign documents and verify digital signatures, manage
(generate, list and delete) encryption keys, using proven Public Key
Infrastructure (PKI) encryption technology based on OpenPGP.

This module is expected to be used with Python versions = 2.4, as it
makes use of the subprocess module which appeared in that version of
Python. This module is a newer version derived from earlier work by
Andrew Kuchling, Richard Jones and Steve Traugott.

A test suite using unittest is included with the source distribution.

Simple usage:

 import gnupg
 gpg = gnupg.GPG(gnupghome='/path/to/keyring/directory')
 gpg.list_keys()
[{
  ...
  'fingerprint': 'F819EE7705497D73E3CCEE65197D5DAC68F1AAB2',
  'keyid': '197D5DAC68F1AAB2',
  'length': '1024',
  'type': 'pub',
  'uids': ['', 'Gary Gross (A test user) gary.gr...@gamma.com']},
 {
  ...
  'fingerprint': '37F24DD4B918CC264D4F31D60C5FEFA7A921FC4A',
  'keyid': '0C5FEFA7A921FC4A',
  'length': '1024',
  ...
  'uids': ['', 'Danny Davis (A test user) danny.da...@delta.com']}]
 encrypted = gpg.encrypt(Hello, world!, ['0C5FEFA7A921FC4A'])
 str(encrypted)
'-BEGIN PGP MESSAGE-\nVersion: GnuPG v1.4.9 (GNU/Linux)\n
\nhQIOA/6NHMDTXUwcEAf
...
-END PGP MESSAGE-\n'
 decrypted = gpg.decrypt(str(encrypted), passphrase='secret')
 str(decrypted)
'Hello, world!'
 signed = gpg.sign(Goodbye, world!, passphrase='secret')
 verified = gpg.verify(str(signed))
 print Verified if verified else Not verified
'Verified'

For more information, visit http://code.google.com/p/python-gnupg/ -
as always, your feedback is most welcome (especially bug reports,
patches and suggestions for improvement). Enjoy!

Cheers

Vinay Sajip
Red Dove Consultants Ltd.
-- 
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations/


[ANN] numexpr 2.2 released

2013-09-01 Thread Francesc Alted
==
 Announcing Numexpr 2.2
==

Numexpr is a fast numerical expression evaluator for NumPy.  With it,
expressions that operate on arrays (like 3*a+4*b) are accelerated
and use less memory than doing the same calculation in Python.

It wears multi-threaded capabilities, as well as support for Intel's
VML library (included in Intel MKL), which allows an extremely fast
evaluation of transcendental functions (sin, cos, tan, exp, log...)
while squeezing the last drop of performance out of your multi-core
processors.

Its only dependency is NumPy (MKL is optional), so it works well as an
easy-to-deploy, easy-to-use, computational kernel for projects that
don't want to adopt other solutions that require more heavy
dependencies.

What's new
==

This release is mainly meant to fix a problem with the license the
numexpr/win32/pthread.{c,h} files emulating pthreads on Windows. After
persmission from the original authors is granted, these files adopt
the MIT license and can be redistributed without problems.  See issue
#109 for details
(https://code.google.com/p/numexpr/issues/detail?id=110).

Another important improvement is the algorithm to decide the initial
number of threads to be used.  This was necessary because by default,
numexpr was using a number of threads equal to the detected number of
cores, and this can be just too much for moder systems where this
number can be too high (and counterporductive for performance in many
cases).  Now, the 'NUMEXPR_NUM_THREADS' environment variable is
honored, and in case this is not present, a maximum number of *8*
threads are setup initially.  The new algorithm is fully described in
the Users Guide now in the note of 'General routines' section:
https://code.google.com/p/numexpr/wiki/UsersGuide#General_routines.
Closes #110.

In case you want to know more in detail what has changed in this
version, see:

http://code.google.com/p/numexpr/wiki/ReleaseNotes

or have a look at RELEASE_NOTES.txt in the tarball.

Where I can find Numexpr?
=

The project is hosted at Google code in:

http://code.google.com/p/numexpr/

You can get the packages from PyPI as well:

http://pypi.python.org/pypi/numexpr

Share your experience
=

Let us know of any bugs, suggestions, gripes, kudos, etc. you may
have.


Enjoy data!

-- 
Francesc Alted
-- 
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations/


Re: argparse - specify order of argument parsing?

2013-09-01 Thread Peter Otten
Eduardo Alvarez wrote:

 When using argparse, is there a way to specify in what order arguments
 get parsed? I am writing a script whose parameters can be modified in
 the following order:
 
 Defaults - config file - command-line switches.
 
 However, I want to give the option of specifying a config file using a
 command line switch as well, so logically, that file should be parsed
 before any other arguments are applied. However, it seems that
 parse_args() parses arguments in the order they're given, so if the
 config file switch is not given first, the config file will overwrite
 whatever was in the command-line switches, which should have higher
 priority.
 
 Thank you in advance,

If you use 

http://docs.python.org/dev/library/argparse.html#fromfile-prefix-chars

to read the configuration file it should be obvious to the user that the 
order is significant. You can even construct multiple config files with 
partially overlapping options:

$ cat load_options.py
import argparse

parser = argparse.ArgumentParser(fromfile_prefix_chars=@)
parser.add_argument(--infile)
parser.add_argument(--outfile)
parser.add_argument(--logfile)

print(parser.parse_args())
$ cat option1.txt
--infile=alpha.txt
--outfile=beta.txt
$ cat option2.txt
--outfile=GAMMA.txt
--logfile=DELTA.txt
$ python load_options.py @option1.txt @option2.txt
Namespace(infile='alpha.txt', logfile='DELTA.txt', outfile='GAMMA.txt')
$ python load_options.py @option2.txt @option1.txt
Namespace(infile='alpha.txt', logfile='DELTA.txt', outfile='beta.txt')

If you insist you could modify the argument list with the following hack:

sys.argv[1:] = sorted(sys.argv[1:], key=lambda arg: arg.startswith(@), 
reverse=True)

There might also be a way to utilize parse_known_args().

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus
Τη Σάββατο, 31 Αυγούστου 2013 9:41:27 π.μ. UTC+3, ο χρήστης Ferrous Cranus 
έγραψε:
 Suddenly my webiste superhost.gr running my main python script presents 
 
 me with this error:
 
 
 
 Code:
 
 UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
 
 \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
 
 'invalid start byte')
 
 
 
 
 
 Does anyone know what this means?
 
 
 
 
 
 -- 
 
 Webhost http://superhost.gr

Good morning Steven,

Ye i'm aware that i need to define variables before i try to make use of them.
I have study all of your examples and then re-view my code and i can *assure* 
you that the line statement that tied to set the 'host' variable is very early 
at the top of the script(of course after imports), and the cur.execute comes 
after.

The problem here is not what you say, that i try to drink k a coffee before 
actually making one first but rather than i cannot drink the coffee although i 
know *i have tried* to make one first.


i will upload the code for you to prove my sayings at pastebin.

http://pastebin.com/J97guApQ
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError issue

2013-09-01 Thread Chris Angelico
On Sun, Sep 1, 2013 at 4:50 PM, Ferrous Cranus nikos.gr...@gmail.com wrote:
 Ye i'm aware that i need to define variables before i try to make use of them.
 I have study all of your examples and then re-view my code and i can *assure* 
 you that the line statement that tied to set the 'host' variable is very 
 early at the top of the script(of course after imports), and the cur.execute 
 comes after.

 The problem here is not what you say, that i try to drink k a coffee before 
 actually making one first but rather than i cannot drink the coffee although 
 i know *i have tried* to make one first.


 i will upload the code for you to prove my sayings at pastebin.

 http://pastebin.com/J97guApQ

You're setting host inside a try/except block. Are you getting
exceptions that prevent it from being set, perhaps?

Also... do you seriously (a) block access if no Referer: header, and
(b) permit that access anyway if the user has a non-blank cookie named
'admin'? Seriously??

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 1/9/2013 10:12 πμ, ο/η Chris Angelico έγραψε:

On Sun, Sep 1, 2013 at 4:50 PM, Ferrous Cranus nikos.gr...@gmail.com wrote:

Ye i'm aware that i need to define variables before i try to make use of them.
I have study all of your examples and then re-view my code and i can *assure* 
you that the line statement that tied to set the 'host' variable is very early 
at the top of the script(of course after imports), and the cur.execute comes 
after.

The problem here is not what you say, that i try to drink k a coffee before 
actually making one first but rather than i cannot drink the coffee although i 
know *i have tried* to make one first.


i will upload the code for you to prove my sayings at pastebin.

http://pastebin.com/J97guApQ


You're setting host inside a try/except block. Are you getting
exceptions that prevent it from being set, perhaps?

Also... do you seriously (a) block access if no Referer: header, and
(b) permit that access anyway if the user has a non-blank cookie named
'admin'? Seriously??

ChrisA



I have checked th output of the erro log a tthe very moment that 
files.py tris to run and i receive this:



[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] ValueError: 
underlying buffer has been detached, referer: http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] , referer: 
http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] Original 
exception was:, referer: http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] Traceback 
(most recent call last):, referer: http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113]   File 
/home/nikos/public_html/cgi-bin/files.py, line 135, in module, 
referer: http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] 
cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES 
(%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), referer: 
http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] NameError: 
name 'host' is not defined, referer: http://superhost.gr/


Let alone that i when i try to set the 'host' variable i get this line 
at my '/tmp/err.out'



ni...@superhost.gr [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
'invalid start byte')


--
Webhost http://superhost.gr
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError issue

2013-09-01 Thread Chris Angelico
On Sun, Sep 1, 2013 at 5:23 PM, Ferrous Cranus ni...@superhost.gr wrote:
 Let alone that i when i try to set the 'host' variable i get this line at my
 '/tmp/err.out'


 ni...@superhost.gr [~]# cat /tmp/err.out

 UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
 \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
 'invalid start byte')

So host isn't being set because of this error, which you're blithely
carrying on after and assuming that things have happened. Solve this
problem, only then move on. Otherwise you WILL confuse yourself
further.

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


Re: Encapsulation unpythonic?

2013-09-01 Thread Steven D'Aprano
On Sat, 31 Aug 2013 05:57:47 -0700, Fabrice Pombet wrote:

 Steve, I think that your definition of encapsulation is too wide to give
 a reasonable answer to the question at hand. 

My definition of encapsulation is based on the plain language definition. 
It is also common in computer programming circles to talk about 
encapsulation in this way. OOP designers may (or may not) have been the 
first to declare encapsulation was a design principle, but programmers 
have been using the *principle* of encapsulation since before Grace 
Hopper was an admiral.

See, for example, coders talking about encapsulation in C and Powershell:

http://ejrh.wordpress.com/2011/04/29/encapsulation-in-c/

https://www.simple-talk.com/dotnet/.net-tools/further-down-the-rabbit-
hole-powershell-modules-and-encapsulation/

OOP languages give us *more* and *better* ways to encapsulate code and 
data, but they did not invent the principle.


 If I understand you
 correctly, you are taking encapsulation as a language characteristic, 
 rather than a principle. 

No, it is both. The coder may or may not decide to encapsulate code in 
subroutines/functions/classes, and the language may or may not allow it.

Languages differ in their ability to allow the programmer to encapsulate. 
Some languages, like early BASIC, give you no ability to encapsulate code 
or data at all. All variables are global, and there are no functions, 
just a big blob of code in a single file. There aren't even data 
structures as such, except strings, so you cannot even group related 
pieces of data into a struct or record.

Some languages, like Python, give you many ways to encapsulate code and 
data: you can group related code in a function, related functions in a 
class, related classes in a module, related modules in a package. That's 
pretty much exactly the same sort of things that you can do in Java. C++ 
has an additional namespace data structure that Python doesn't have, 
but that's just a mechanism for encapsulation.

Encapsulation and information hiding are distinct things -- you can have 
one without the other. C, for example, creates a new scope inside for-
loops, so that the for-loop variable is hidden from the rest of the 
function. Apart from a pair of braces, there is no encapsulation, but 
there is information hiding. Or you could design a language that 
encapsulated code into functions and classes, but put all variables in a 
single, global, shared namespace (little, or no, information hiding).

It is a myth, and a very obnoxious one, that encapsulation and 
information hiding were invented by OOP. What is a function but a way to 
hide the implementation of a chunk of code from the caller? What are 
local variables but a way to hide variables used by one function from 
another? Coders were using information hiding, separation of concerns, 
and encapsulation in the 1950s, long before OOP. They just didn't call 
them by those names. They just called them writing good code.

Actually, functions are *not necessarily* a way to hide implementation. 
There are languages where you can jump into the middle of a function. So 
you can encapsulate a chunk of code into a function, without hiding the 
implementation details. Just as you can encapsulate code and data into a 
class, without hiding the implementation details, if you declare 
everything public.


 Plus, you seem to forget that encapsulation is
 an OOP principle, and, forgive me if I am wrong, does not apply normally
 to functions or languages like C. 

I haven't forgotten it, because it isn't true.

One of the most obnoxious and annoying traits of OOP zealots, especially 
academics, is that they act as if programming did not exist before Java 
and C++, or if you're really lucky, Smalltalk. (Somehow they nearly 
always forget about Simula.) Perhaps because OOP was so late to be 
invented (structured programming goes back to Fortran in the 1950s, 
functional programming to Lisp only a few years after that), and because 
it was so heavily hyped as the solution to every programming 
difficulty, too many people ignore anything outside of the OOP. They 
wrongly believe that since Python isn't a pure OOP language (according 
to some bizarre understanding of pure that often considers C++ and Java 
pure), Python cannot possibly have OOP principles like encapsulation, 
information hiding, separation of concerns.

That point of view is sheerest nonsense.


 Please read Steve Holden's (in chaz')
 definition, and tell us whether you think that Python enforces strongly
 this principle, I think that would be a good basis for an agreement. 

Are you referring to this quote?

encapsulation is the idea that the only way to access or change the data 
inside an object is by calling its methods.


I disagree with that definition. That's a poor definition, one that has 
no relation to the plain English meaning of the word encapsulation, nor 
to how the word is commonly used in the great bulk of programming 
circles. By 

Re: Encapsulation unpythonic?

2013-09-01 Thread Chris Angelico
On Sun, Sep 1, 2013 at 6:10 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 Java and C++ allow you to declare members as public, so it is *not true*
 that calling methods is the only way to change members. If you accept
 Steve Holden's (wrong) definition above, Java and C++ don't have
 encapsulation either.

That said, though, when you consider the language ecosystem rather
than just the language, there is a strong tendency for Java and C++
code to wrap everything up with functions (no public data members),
whereas Python code is far more likely to have external code directly
access data inside an object. You usually will find Java code calling
methods to change members, whereas that's done in Python only when
there's a need for it.

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Steven D'Aprano
On Sat, 31 Aug 2013 23:50:23 -0700, Ferrous Cranus wrote:

 Τη Σάββατο, 31 Αυγούστου 2013 9:41:27 π.μ. UTC+3, ο χρήστης Ferrous
 Cranus έγραψε:
 Suddenly my webiste superhost.gr running my main python script presents
 
 me with this error:
 
 
 
 Code:
 
 UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
 
 \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
 
 'invalid start byte')
 
 
 
 
 
 Does anyone know what this means?
 
 
 
 
 
 --
 
 Webhost http://superhost.gr
 
 Good morning Steven,
 
 Ye i'm aware that i need to define variables before i try to make use of
 them. I have study all of your examples and then re-view my code and i
 can *assure* you that the line statement that tied to set the 'host'
 variable is very early at the top of the script(of course after
 imports), and the cur.execute comes after.
 
 The problem here is not what you say, that i try to drink k a coffee
 before actually making one first but rather than i cannot drink the
 coffee although i know *i have tried* to make one first.
 
 
 i will upload the code for you to prove my sayings at pastebin.
 
 http://pastebin.com/J97guApQ


You are mistaken. In line 20-25, you have this:

try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] 
or Proxy Detected
except Exception as e:
print( repr(e), file=open( '/tmp/err.out', 'w' ) )


An error occurs inside that block, *before* host gets set. Who knows what 
the error is? You have access to the err.out file, but apparently you 
aren't reading it to find out.

Then, 110 lines later, at line 135, you try to access the value of host 
that never got set.

Your job is to read the error in /tmp/err.out, see what is failing, and 
fix it.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Fabrice Pombet
  That said, though, when you consider the language ecosystem rather
 
 than just the language, there is a strong tendency for Java and C++
 
 code to wrap everything up with functions (no public data members),
 
 whereas Python code is far more likely to have external code directly
 
 access data inside an object. You usually will find Java code calling
 
 methods to change members, whereas that's done in Python only when
 
 there's a need for it.


Yep, this is precisely my point, if you take encapsulation as a philosophical 
principle, Java and C++ would tend to be abiding by it, as a default setting 
that you can at times change, whereas python would tend to be the contrary. In 
other words, you can set some encapsulation if and when you want to, but you 
can leave your code without it when it's not needed/inconvenient. So I guess 
that we are actually all agreeing on this one.

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Dave Angel
On 1/9/2013 03:23, Ferrous Cranus wrote:

 Στις 1/9/2013 10:12 πμ, ο/η Chris Angelico έγραψε:
 On Sun, Sep 1, 2013 at 4:50 PM, Ferrous Cranus nikos.gr...@gmail.com wrote:
 Ye i'm aware that i need to define variables before i try to make use of 
 them.
 I have study all of your examples and then re-view my code and i can 
 *assure* you that the line statement that tied to set the 'host' variable 
 is very early at the top of the script(of course after imports), and the 
 cur.execute comes after.

 The problem here is not what you say, that i try to drink k a coffee before 
 actually making one first but rather than i cannot drink the coffee 
 although i know *i have tried* to make one first.


 i will upload the code for you to prove my sayings at pastebin.

 http://pastebin.com/J97guApQ

 You're setting host inside a try/except block. Are you getting
 exceptions that prevent it from being set, perhaps?


   SNIP

 Let alone that i when i try to set the 'host' variable i get this line 
 at my '/tmp/err.out'


 ni...@superhost.gr [~]# cat /tmp/err.out
 UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
 \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
 'invalid start byte')


Let's examine the relevant code from your pastebin above:


try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or 
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or 
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or Proxy 
Detected
except Exception as e:
print( repr(e), file=open( '/tmp/err.out', 'w' ) )
 
Presumably you do realize that any exception on any of the three lines
will skip the remainder, and print that line to the /tmp/err.out file? 
And that if any exception occurs, it'll be before 'host' is initialized
at all?  Possibly before 'city' is initialized, or even 'gi' ?   So
there's no reason yet to assume that somehow setting the 'host'
variable triggers anything. When in doubt, consider including only one
line in any given try block. But let's try simpler changes.

Your file mode is 'w' which will wipe out anything written earlier.  Yet
if the exception does not happen, the file will still contain error(s)
from some earlier run.  You should open (and truncate) the file before
the first use, then use the same handle in each exception at which you
want to record any information.  Further, you ought to put more into the
file than just the repr(e) value.  For starters, the Traceback would be
nice.

If I were doing it, I'd be printing  repr(sys.exc_info()) to that file. 
I'd also have some kind of label, so that once I had more than one of
these in the code, I'd have an easyt way to tell which was which.

This is my first crack at it (untested):

errout = open(/tmp/err.out, w)   #opens and truncates the error
output file
try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
host =socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
Proxy Detected
except Exception as e:
print( Xyzzy exception-, repr(sys.exc_info()), file=errout)
errout.flush()


Note that I haven't had to use exc_info() in my own code, so I'm sure it
could be formatted prettier.  But right now, you need to stop throwing
away useful information.


-- 
DaveA


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


Re: UnicodeDecodeError issue

2013-09-01 Thread Chris Angelico
On Sun, Sep 1, 2013 at 8:35 PM, Dave Angel da...@davea.name wrote:
 Your file mode is 'w' which will wipe out anything written earlier.  Yet
 if the exception does not happen, the file will still contain error(s)
 from some earlier run.  You should open (and truncate) the file before
 the first use, then use the same handle in each exception at which you
 want to record any information.

Hmm, I'd go the other way, and append to the file every time there's
an error - otherwise, one successful page load will wipe out the error
from a previous one. But I agree that the inconsistency is not helping
him.

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


Re: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-09-01 Thread Roy Smith
In article 48c8f8ca-d8c1-4a60-ba7f-8e8b00993...@googlegroups.com,
 anntzer@gmail.com wrote:

 It is the call to gethostbyname_ex that is very slow.  The call to 
 gethostname is quick (and returns the same string as /usr/bin/hostname).

First, please stop posting with Google Groups.  It makes a total mess of 
the quoted text.

Next, what happens when you use the command-line tools (nslookup or dig) 
to translate your hostname to an IP address, and what happens if you 
then try to reverse translate that IP address back to a name?

 This issue is independent of IPython:
 
 $ time python -c 'import socket; 
 print(socket.gethostbyname_ex(socket.gethostname())[2])' 
 ['192.168.0.102']
 python -c   0.07s user 0.02s system 0% cpu 28.190 total

The real point is not that it's independent of IPython, it has nothing 
to do with Python at all.  What you've got here is a network 
configuration issue.  You would do better to recreate this problem with 
the native OS command line tools and then ask about it on a forum 
dedicated to your operating system.
-- 
http://mail.python.org/mailman/listinfo/python-list


Newbie: use of built-in exceptions

2013-09-01 Thread Rui Maciel
Are there any guidelines on the use (and abuse) of Python's built-in 
exceptions, telling where 
it's ok to raise them and where it's preferable to define custom exceptions 
instead?  


Thanks in advance,
Rui Maciel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie: use of built-in exceptions

2013-09-01 Thread Chris “Kwpolska” Warrick
On Sun, Sep 1, 2013 at 1:17 PM, Rui Maciel rui.mac...@gmail.com wrote:
 Are there any guidelines on the use (and abuse) of Python's built-in 
 exceptions, telling where
 it's ok to raise them and where it's preferable to define custom exceptions 
 instead?

There are no rules.  You should use common sense instead: if the
exception fits your needs (eg. ValueError when incorrect output
occurs) then use it.

-- 
Chris “Kwpolska” Warrick http://kwpolska.tk
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-09-01 Thread Chris Angelico
On Sun, Sep 1, 2013 at 10:03 AM,  anntzer@gmail.com wrote:
 At startup, IPython (qtconsole) calls 
 socket.gethostbyname_ex(socket.gethostname())[2] to find a list of IP 
 addresses that point to the machine. On a Linux server that I manage this 
 call is extremely slow (20s)... which I have trouble understanding as ip 
 addr show seems to give the same information nearly instantaneously. Is 
 there anything I can do to make this faster? Can this be a network 
 configuration issue (I am behind a router)?

Yes, it most definitely CAN be a network config issue. The C function
you want to be calling is getifaddrs(), and I don't think there's a
way to call that from core Python. But a Google search for 'python
getifaddrs' shows up a few third-party modules that might be of use to
you; that'd be a lot quicker and more reliable than trying to look up
your own hostname and depending on the results.

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 1/9/2013 1:35 μμ, ο/η Dave Angel έγραψε:

This is my first crack at it (untested):

errout = open(/tmp/err.out, w)   #opens and truncates the error
output file
try:
 gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
 city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
 host =socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
Proxy Detected
except Exception as e:
 print( Xyzzy exception-, repr(sys.exc_info()), file=errout)
 errout.flush()


Note that I haven't had to use exc_info() in my own code, so I'm sure it
could be formatted prettier.  But right now, you need to stop throwing
away useful information.


First of all thank you for your detailed information Dave.
I have tried all you said, the above example you provided me, but i'm 
afraid even with your approach which should have given more error 
specific information the output of the err file remains.



ni...@superhost.gr [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
'invalid start byte')




--
Webhost http://superhost.gr
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 1/9/2013 11:35 πμ, ο/η Steven D'Aprano έγραψε:

On Sat, 31 Aug 2013 23:50:23 -0700, Ferrous Cranus wrote:


Τη Σάββατο, 31 Αυγούστου 2013 9:41:27 π.μ. UTC+3, ο χρήστης Ferrous
Cranus έγραψε:

Suddenly my webiste superhost.gr running my main python script presents

me with this error:



Code:

UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef

\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,

'invalid start byte')





Does anyone know what this means?





--

Webhost http://superhost.gr


Good morning Steven,

Ye i'm aware that i need to define variables before i try to make use of
them. I have study all of your examples and then re-view my code and i
can *assure* you that the line statement that tied to set the 'host'
variable is very early at the top of the script(of course after
imports), and the cur.execute comes after.

The problem here is not what you say, that i try to drink k a coffee
before actually making one first but rather than i cannot drink the
coffee although i know *i have tried* to make one first.


i will upload the code for you to prove my sayings at pastebin.

http://pastebin.com/J97guApQ



You are mistaken. In line 20-25, you have this:

try:
 gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
 city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
 gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
 host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
 socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0]
 or Proxy Detected
except Exception as e:
 print( repr(e), file=open( '/tmp/err.out', 'w' ) )


An error occurs inside that block, *before* host gets set. Who knows what
the error is? You have access to the err.out file, but apparently you
aren't reading it to find out.

Then, 110 lines later, at line 135, you try to access the value of host
that never got set.

Your job is to read the error in /tmp/err.out, see what is failing, and
fix it.




But i'm Steven! That why i make use of it to read it immediately after 
my script run at browser time.


i have even included a sys.exit(0) after the try:/except block:

Here is it:


errout = open( '/tmp/err.out', 'w' )		# opens and truncates the error 
output file

try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
	city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or 
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
	host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or 
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or Proxy 
Detected

except Exception as e:
print( Xyzzy exception-, repr( sys.exc_info() ), file=errout )
errout.flush()

sys.exit(0)

and the output of error file is:


ni...@superhost.gr [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
'invalid start byte')


--
Webhost http://superhost.gr

--
Webhost http://superhost.gr
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 1/9/2013 5:08 μμ, ο/η Ferrous Cranus έγραψε:

Στις 1/9/2013 11:35 πμ, ο/η Steven D'Aprano έγραψε:

On Sat, 31 Aug 2013 23:50:23 -0700, Ferrous Cranus wrote:


Τη Σάββατο, 31 Αυγούστου 2013 9:41:27 π.μ. UTC+3, ο χρήστης Ferrous
Cranus έγραψε:

Suddenly my webiste superhost.gr running my main python script presents

me with this error:



Code:

UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef

\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,

'invalid start byte')





Does anyone know what this means?





--

Webhost http://superhost.gr


Good morning Steven,

Ye i'm aware that i need to define variables before i try to make use of
them. I have study all of your examples and then re-view my code and i
can *assure* you that the line statement that tied to set the 'host'
variable is very early at the top of the script(of course after
imports), and the cur.execute comes after.

The problem here is not what you say, that i try to drink k a coffee
before actually making one first but rather than i cannot drink the
coffee although i know *i have tried* to make one first.


i will upload the code for you to prove my sayings at pastebin.

http://pastebin.com/J97guApQ



You are mistaken. In line 20-25, you have this:

try:
 gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
 city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
 gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
 host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
 socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0]
 or Proxy Detected
except Exception as e:
 print( repr(e), file=open( '/tmp/err.out', 'w' ) )


An error occurs inside that block, *before* host gets set. Who knows what
the error is? You have access to the err.out file, but apparently you
aren't reading it to find out.

Then, 110 lines later, at line 135, you try to access the value of host
that never got set.

Your job is to read the error in /tmp/err.out, see what is failing, and
fix it.




But i'm Steven! That why i make use of it to read it immediately after
my script run at browser time.

i have even included a sys.exit(0) after the try:/except block:

Here is it:


errout = open( '/tmp/err.out', 'w' )# opens and truncates the
error output file
try:
 gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
 city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
 host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or Proxy
Detected
except Exception as e:
 print( Xyzzy exception-, repr( sys.exc_info() ), file=errout )
 errout.flush()

sys.exit(0)

and the output of error file is:


ni...@superhost.gr [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')




But i noticed that err.out and /usr/local/apache/logs/error_log produced 
different output.


In any case i check both:


ni...@superhost.gr [~]# chmod 777 /tmp/err2.out

ouput of error_log
ni...@superhost.gr [~]# [Sun Sep 01 14:23:46 2013] [error] [client 
173.245.49.120] Premature end of script headers: metrites.py
[Sun Sep 01 14:23:46 2013] [error] [client 173.245.49.120] File does not 
exist: /home/nikos/public_html/500.shtml




Also i have even changed output error filename.
turns out empty.

ni...@superhost.gr [~]# cat /tmp/err2.out

--
Webhost http://superhost.gr
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError issue

2013-09-01 Thread Dave Angel
On 1/9/2013 10:08, Ferrous Cranus wrote:

   snip
 Here is it:


 errout = open( '/tmp/err.out', 'w' )  # opens and truncates the error 
 output file
 try:
   gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
   city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or 
 gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
   host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or 
 socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or Proxy 
 Detected
 except Exception as e:
   print( Xyzzy exception-, repr( sys.exc_info() ), file=errout )
  errout.flush()

 sys.exit(0)

 and the output of error file is:


 ni...@superhost.gr [~]# cat /tmp/err.out
 UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
 \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
 'invalid start byte')


Nope.  The label  Xyzzy exception is not in that file, so that's not
the file you created in this run.  Further, if that line existed before,
it would have been wiped out by the open with mode w.

i suggest you add yet another write to that file, immediately after
opening it:

errout = open( '/tmp/err.out', 'w' )# opens and truncates the error 
print(starting run, file=errorout)
errout.flush()

Until you can reliably examine the same file that was logging your
errors, you're just spinning your wheels.  you might even want to write
the time to the file, so that you can tell whether it was now, or 2 days
ago that the run was made.


-- 
DaveA


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


Re: UnicodeDecodeError issue

2013-09-01 Thread Dave Angel
On 1/9/2013 09:59, Ferrous Cranus wrote:

 Στις 1/9/2013 1:35 μμ, ο/η Dave Angel έγραψε:
 This is my first crack at it (untested):

 errout = open(/tmp/err.out, w)   #opens and truncates the error
 output file
 try:
  gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
  city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
 gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
  host =socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
 socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
 Proxy Detected
 except Exception as e:
  print( Xyzzy exception-, repr(sys.exc_info()), file=errout)
  errout.flush()


 Note that I haven't had to use exc_info() in my own code, so I'm sure it
 could be formatted prettier.  But right now, you need to stop throwing
 away useful information.

 First of all thank you for your detailed information Dave.
 I have tried all you said, the above example you provided me, but i'm 
 afraid even with your approach which should have given more error 
 specific information the output of the err file remains.


 ni...@superhost.gr [~]# cat /tmp/err.out
 UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
 \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
 'invalid start byte')




See my other response.  The above file did NOT result from running the
code above.  It is missing the Xyzzy label.

-- 
Signature file not found

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 1/9/2013 6:36 μμ, ο/η Dave Angel έγραψε:

On 1/9/2013 10:08, Ferrous Cranus wrote:

snip

Here is it:


errout = open( '/tmp/err.out', 'w' )# opens and truncates the error
output file
try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or Proxy
Detected
except Exception as e:
print( Xyzzy exception-, repr( sys.exc_info() ), file=errout )
  errout.flush()

sys.exit(0)

and the output of error file is:


ni...@superhost.gr [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')



Nope.  The label  Xyzzy exception is not in that file, so that's not
the file you created in this run.  Further, if that line existed before,
it would have been wiped out by the open with mode w.

i suggest you add yet another write to that file, immediately after
opening it:

errout = open( '/tmp/err.out', 'w' )# opens and truncates the error
print(starting run, file=errorout)
errout.flush()

Until you can reliably examine the same file that was logging your
errors, you're just spinning your wheels.  you might even want to write
the time to the file, so that you can tell whether it was now, or 2 days
ago that the run was made.





I tried it and it printed nothing.
But suddenly thw ebpage sttaed to run and i get n invalid byte entried 
and no weird messge files.py is working as expcted.

what on earht?

Now i ahve thso error:

# 
=
# DATABASE INSERTS - do not increment the counter if a Cookie is set to 
the visitors browser already
# 
=
if( not vip and re.search( 
r'(msn|gator|amazon|yandex|reverse|cloudflare|who|fetch|barracuda|spider|google|crawl|pingdom)', 
host ) is None ):


print( i'm in and data is: , host )
try:
#find the needed counter for the page URL
if os.path.exists( path + page ) or os.path.exists( cgi_path + 
page ):
cur.execute('''SELECT ID FROM counters WHERE url = 
%s''', page )
data = cur.fetchone()   #URL is unique, so 
should only be one

if not data:
#first time for page; primary key is automatic, hit is 
defaulted
cur.execute('''INSERT INTO counters (url) VALUES 
(%s)''', page )
cID = cur.lastrowid#get the primary key value 
of the new record
else:
#found the page, save primary key and use it to issue 
hit UPDATE
cID = data[0]
			cur.execute('''UPDATE counters SET hits = hits + 1 WHERE ID = %s''', 
cID )


#find the visitor record for the (saved) cID and current host
		cur.execute('''SELECT * FROM visitors WHERE counterID = %s and host = 
%s''', (cID, host) )

data = cur.fetchone()#cIDhost are unique

if not data:
#first time for this host on this page, create new 
record
			cur.execute('''INSERT INTO visitors (counterID, host, city, useros, 
browser, lastvisit) VALUES (%s, %s, %s, %s, %s, %s)''', (cID, host, 
city, useros, browser, date) )

else:
#found the page, save its primary key for later use
vID = data[0]
#UPDATE record using retrieved vID
			cur.execute('''UPDATE visitors SET city = %s, useros = %s, browser = 
%s, hits = hits + 1, lastvisit = %s
	WHERE counterID = %s and host = %s''', (city, useros, browser, 
date, vID, host) )


con.commit()#if we made it here, the transaction is 
complete

except pymysql.ProgrammingError as e:
print( repr(e) )
con.rollback()  #something failed, rollback the entire 
transaction
sys.exit(0)


i get no counter increment when visitors visit my webpage.
What on eart is going on?

How the previous error with the invalid byte somehtign got solved?

--
Webhost http://superhost.gr
--
http://mail.python.org/mailman/listinfo/python-list


Re: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-09-01 Thread anntzer . lee
On Saturday, August 31, 2013 10:06:43 PM UTC-7, Michael Torrie wrote:
 On 08/31/2013 10:51 PM, anntzer@gmail.com wrote:
 
  It is the call to gethostbyname_ex that is very slow.  The call to
  gethostname is quick (and returns the same string as
  /usr/bin/hostname).
 
 What gethostbyname_ex and /usr/bin/hostname do are very different
 things.  gethostbyname_ex does a DNS lookup against a server.
 /usr/bin/hostname just checks a local computer setting.  I don't see why
 you are comparing the two.  /usr/bin/hostname is not going to help you
 find a list of IP addresses that point to a machine.

I was just replying to the previous comment name = socket.gethostname() see 
how long that takes and what it returns.  Then, assuming it returns a string 
containing your hostname (massive handwave about what that actually means), 
saying that gethostname resolves my own hostname instantaneously.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semicolon at end of python's statements

2013-09-01 Thread Antoon Pardon

Op 31-08-13 02:09, Steven D'Aprano schreef:

On Fri, 30 Aug 2013 11:32:17 +0100, Fábio Santos wrote:


On 29 Aug 2013 23:20, Ben Finney ben+pyt...@benfinney.id.au wrote:


Fábio Santos fabiosantos...@gmail.com writes:


It is a shame that this is not possible in python. for..if exists in
comprehensions and not in regular loops but that would be nice
sometimes.


So you use it in a generator expression, and iterate over the
generator:

 for foo in (spam for spam in sequence if predicate(spam)):
 process(spam)

That way, there's no need for new syntax.


The problem I have with that strategy is that it is repetitive and
hinders readability. You wrote for and in twice, and spam (a pretty
useless intermediate variable) thrice!


There is no need for spam to be intermediate, and the fact that it
shouldn't be is demonstrated by Ben's error in referring to process
(spam) instead of process(foo).

We really are spoiled for choice here. We can write any of these:

# Option 1
for spam in sequence:
 if predicate(spam):
 process(spam)

# Option 2
for spam in filter(predicate, sequence):
 process(spam)

# Option 3
for spam in (spam for spam in sequence if predicate(spam)):
 process(spam)


Adding a fourth option:

for spam in sequence if predicate(spam):
 process(spam)

saves absolutely nothing except a line and an indent level, neither of
which are in short supply, and gains nothing in readability over Option 1.


I find this rather disenginuous. Dare to suggest here that python might
benetif by incluidng end markers for its suits because it would make it 
clearer how many indent levels were done, and chances are someone here

will suggest that too many indent levels are a sign of bad coding
practice, but when someone suggests a change that would allow him
to structure his program more like how he sees it and indent levels
are not short in supply, suggesting there is no problem, no matter how
many one uses.

--
Antoon Pardon
--
http://mail.python.org/mailman/listinfo/python-list


What does mean @ sign in first of statement

2013-09-01 Thread Mohsen Pahlevanzadeh
Dear all,

What does mean @ sign in first of statement such as:

//
@hybrid_property
def fullname(self):
return self.firstname +   + self.lastname
///

Sorry for cheap question.

Yours,
Mohsen

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


Re: What does mean @ sign in first of statement

2013-09-01 Thread Chris “Kwpolska” Warrick
On Sun, Sep 1, 2013 at 8:53 PM, Mohsen Pahlevanzadeh
moh...@pahlevanzadeh.org wrote:
 Dear all,

 What does mean @ sign in first of statement such as:

 //
 @hybrid_property
 def fullname(self):
 return self.firstname +   + self.lastname
 ///

 Sorry for cheap question.

 Yours,
 Mohsen

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

@hybrid_property is a decorator.  Great resource:
http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/

--
Chris “Kwpolska” Warrick http://kwpolska.tk
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Ethan Furman

On 09/01/2013 03:09 AM, Fabrice Pombet wrote:


So I guess that we are actually all agreeing on this one.


No, we are not.

encapsulation != inaccessible except by getters/setters

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


Re: Encapsulation unpythonic?

2013-09-01 Thread Roy Smith
In article mailman.455.1378062400.19984.python-l...@python.org,
 Ethan Furman et...@stoneleaf.us wrote:

 On 09/01/2013 03:09 AM, Fabrice Pombet wrote:
 
  So I guess that we are actually all agreeing on this one.
 
 No, we are not.
 
 encapsulation != inaccessible except by getters/setters

Nothing is accessible in Python except via getters and setters.  The 
only difference between Python and, say, C++ in this regard is that the 
Python compiler writes them for you most of the time and doesn't make 
you put ()'s at the end of the name :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


connecting clicked signal to different slots depending on function executing

2013-09-01 Thread tausciam
I have one tablewidget I want to use for two different uses, audio and video. 
If cover art is displayed and the user clicks the cover art, it emits a cell 
clicked and gets the list of the songs. If video thumbnails are displayed, it 
emits a cell clicked and plays the video fullscreen.

So, I have the same signal going to two different slots. Is there any way to 
break that connection before I define it again? In other words, in the audio 
section I could have something like:

[break video signal/slot connection]
self.tableWidget.cellClicked.connect(self.audiocell_clicked)

and then for the video section:

[break audio signal/slot connection]
self.tableWidget.cellClicked.connect(self.videocell_clicked)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: connecting clicked signal to different slots depending on function executing

2013-09-01 Thread tausciam
Nevermind. I found that it would let me create the connection again then 
when I disconnected, it would disconnect all of the instances...

so I ended up with: 

self.tableWidget.cellClicked.connect(self.videocell_clicked)   
self.tableWidget.cellClicked.disconnect(self.videocell_clicked)   
self.tableWidget.cellClicked.connect(self.audiocell_clicked)

If there's a better way to do this, I'd be interested in hearing about it. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: connecting clicked signal to different slots depending on function executing

2013-09-01 Thread tausciam
I should add that I know about:

self.tableWidget.cellClicked.disconnect(self.videocell_clicked)  

but, when I do that, if the connection is not there, then the program crashes. 
So, how could I check to see if the connection is there then break it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading from stdin first, then use curses

2013-09-01 Thread Antoon Pardon

Op 11-08-13 14:05, Timo Schmiade schreef:

Hi all,

I wrote a replacement for urlview to properly extract URLs from emails.
You can find the first draft here:

   https://github.com/the-isz/pyurlview

When I call it with an email file passed to the '-f' argument, it does
pretty much what I want already. However, I intend to use it in mutt,
which pipes the message to the program like so:

macro pager \cu pipe-entry'pyurlview.py'enter 'Follow links with pyurlview'

The problem is rather obvious but - unfortunately - not so easy to solve:

* The program reads the mail from stdin
* The terminal in which it runs is a pseudo-terminal (pipe)
* curses is not able to accept user input from the pseudo-terminal

The question is:

How do I read from stdin first and afterwards allow curses to read user
input?



Well you could close fd 0. Then open /dev/tty en dup the fd to 0.
Then start curses.

--
Antoon Pardon

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


Re: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-09-01 Thread anntzer . lee
On Sunday, September 1, 2013 4:37:34 AM UTC-7, Chris Angelico wrote:

 Yes, it most definitely CAN be a network config issue. The C function
 you want to be calling is getifaddrs(), and I don't think there's a
 way to call that from core Python. But a Google search for 'python
 getifaddrs' shows up a few third-party modules that might be of use to
 you; that'd be a lot quicker and more reliable than trying to look up
 your own hostname and depending on the results.
 
 ChrisA

I tried using netifaces (https://pypi.python.org/pypi/netifaces) which seems to 
rely on getifaddrs (according to the doc, I didn't check the source).  Again, 
it returns nearly instantaneously the correct IP address.
-- 
http://mail.python.org/mailman/listinfo/python-list


MySQL data types vs Django/Python data types

2013-09-01 Thread Gary Roach

Hi all,

System:
Debian Wheezy Linux
Python 2.7
Django 1.5
MySql 5.5

I am new to Python and Django and am having trouble matching Python data 
types with those of MySQL. MySQL has about 7 basic data types including 
Blobs, Binaries, etc. It also has a rich selection of geometry types. In 
addition, types like INT have 7 or 8 different options like Primary Key, 
zero fill, auto inc, etc. I can't seem to find anything in python to 
match these. I am trying to build a model.py for an existing database 
that was created with MySQL Workbench.


I do not wish to use anything other than MySQL because of the complexity 
of my storage needs (searchable text, PDF, Audio, Video and Photos). The 
text searches will often be word phrase searches through thousands of 
documents. I need the speed and the data type flexibility.


How do I build or modify a model.py to do this. I really don't want to 
write a model.py manually. That would really be a major pain. Workbench 
to the database an import to Django and edit is my choice. 
Unfortunately, the model.py produced has lost most of the functionality 
of the original database and I can't seem to figure out how to fix it.


Any help will be sincerely appreciated.

Gary R.
--
http://mail.python.org/mailman/listinfo/python-list


Re: semicolon at end of python's statements

2013-09-01 Thread Antoon Pardon

Op 31-08-13 02:09, Steven D'Aprano schreef:

On Fri, 30 Aug 2013 11:32:17 +0100, Fábio Santos wrote:





We really are spoiled for choice here. We can write any of these:

# Option 1
for spam in sequence:
 if predicate(spam):
 process(spam)





Adding a fourth option:

for spam in sequence if predicate(spam):
 process(spam)

saves absolutely nothing except a line and an indent level, neither of
which are in short supply, and gains nothing in readability over Option 1.


So what is the big difference between this situation and the following:

| else:
| if condition:
| whatever

which in python we can write:

| elif condition:
| whatever


So either is seems this was a design mistake or a line and an indent
level can be important enough to allow a combination of controls.

--
Antoon Pardon
--
http://mail.python.org/mailman/listinfo/python-list


Re: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-09-01 Thread Chris Angelico
On Mon, Sep 2, 2013 at 6:37 AM,  anntzer@gmail.com wrote:
 On Sunday, September 1, 2013 4:37:34 AM UTC-7, Chris Angelico wrote:

 Yes, it most definitely CAN be a network config issue. The C function
 you want to be calling is getifaddrs(), and I don't think there's a
 way to call that from core Python. But a Google search for 'python
 getifaddrs' shows up a few third-party modules that might be of use to
 you; that'd be a lot quicker and more reliable than trying to look up
 your own hostname and depending on the results.

 ChrisA

 I tried using netifaces (https://pypi.python.org/pypi/netifaces) which seems 
 to rely on getifaddrs (according to the doc, I didn't check the source).  
 Again, it returns nearly instantaneously the correct IP address.

Perfect!

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


Re: Encapsulation unpythonic?

2013-09-01 Thread Ethan Furman

On 09/01/2013 12:13 PM, Roy Smith wrote:

In article mailman.455.1378062400.19984.python-l...@python.org,
  Ethan Furman et...@stoneleaf.us wrote:


On 09/01/2013 03:09 AM, Fabrice Pombet wrote:


So I guess that we are actually all agreeing on this one.


No, we are not.

encapsulation != inaccessible except by getters/setters


Nothing is accessible in Python except via getters and setters.  The
only difference between Python and, say, C++ in this regard is that the
Python compiler writes them for you most of the time and doesn't make
you put ()'s at the end of the name :-)


class Javaesque:

__value = None

def get_value(self):
return self.__value

def set_value(self, new_value):
validate(new_value)
self.__value = new_value


class ProtectedPython:

_value = None

@property
def value(self):
return self._value

@value.setter
def value(self, new_value)
validate(new_value)
self._value = new_value

class PlainPython:

value = None


In the Javaesque class we see the unPythonic way of using getters/setters; in the ProtectedPython* class we see the 
pythonic way of providing getters/setters**; in the PlainPython class we have the standard, unprotected, direct access 
to the class attribute.


No where in PlainPython is a getter/setter defined, nor does Python define one 
for us behind our backs.

If you have evidence to the contrary I'd like to see it.


* Not the best name, but oh well.
** In Python, using @property makes getter/setter usage look just like normal 
attribute usage, which is cool.

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


Re: Encapsulation unpythonic?

2013-09-01 Thread Tim Delaney
On 2 September 2013 06:33, Ethan Furman et...@stoneleaf.us wrote:


 class PlainPython:

 value = None


 In the Javaesque class we see the unPythonic way of using getters/setters;
 in the ProtectedPython* class we see the pythonic way of providing
 getters/setters**; in the PlainPython class we have the standard,
 unprotected, direct access to the class attribute.

 No where in PlainPython is a getter/setter defined, nor does Python define
 one for us behind our backs.

 If you have evidence to the contrary I'd like to see it.


I think Roy is referring to the fact that attribute access is implemented
via __getattr__ / __getattribute__ / __setattr__ / __delattr__. From one
point of view, he's absolutely correct - nearly all attributes are accessed
via getters/setters in Python.

Tim Delaney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Roy Smith
In article mailman.461.1378072496.19984.python-l...@python.org,
 Tim Delaney timothy.c.dela...@gmail.com wrote:

 On 2 September 2013 06:33, Ethan Furman et...@stoneleaf.us wrote:
 
 
  class PlainPython:
 
  value = None
 
 
  In the Javaesque class we see the unPythonic way of using getters/setters;
  in the ProtectedPython* class we see the pythonic way of providing
  getters/setters**; in the PlainPython class we have the standard,
  unprotected, direct access to the class attribute.
 
  No where in PlainPython is a getter/setter defined, nor does Python define
  one for us behind our backs.
 
  If you have evidence to the contrary I'd like to see it.
 
 
 I think Roy is referring to the fact that attribute access is implemented
 via __getattr__ / __getattribute__ / __setattr__ / __delattr__. From one
 point of view, he's absolutely correct - nearly all attributes are accessed
 via getters/setters in Python.
 
 Tim Delaney

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 1/9/2013 7:10 μμ, ο/η Ferrous Cranus έγραψε:

Στις 1/9/2013 6:36 μμ, ο/η Dave Angel έγραψε:

On 1/9/2013 10:08, Ferrous Cranus wrote:

snip

Here is it:


errout = open( '/tmp/err.out', 'w' )# opens and truncates the
error
output file
try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or Proxy
Detected
except Exception as e:
print( Xyzzy exception-, repr( sys.exc_info() ), file=errout )
  errout.flush()

sys.exit(0)

and the output of error file is:


ni...@superhost.gr [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')



Nope.  The label  Xyzzy exception is not in that file, so that's not
the file you created in this run.  Further, if that line existed before,
it would have been wiped out by the open with mode w.

i suggest you add yet another write to that file, immediately after
opening it:

errout = open( '/tmp/err.out', 'w' )# opens and truncates the
error
print(starting run, file=errorout)
errout.flush()

Until you can reliably examine the same file that was logging your
errors, you're just spinning your wheels.  you might even want to write
the time to the file, so that you can tell whether it was now, or 2 days
ago that the run was made.





I tried it and it printed nothing.
But suddenly thw ebpage sttaed to run and i get n invalid byte entried
and no weird messge files.py is working as expcted.
what on earht?

Now i ahve thso error:

#
=

# DATABASE INSERTS - do not increment the counter if a Cookie is set to
the visitors browser already
#
=

if( not vip and re.search(
r'(msn|gator|amazon|yandex|reverse|cloudflare|who|fetch|barracuda|spider|google|crawl|pingdom)',
host ) is None ):

 print( i'm in and data is: , host )
 try:
 #find the needed counter for the page URL
 if os.path.exists( path + page ) or os.path.exists( cgi_path +
page ):
 cur.execute('''SELECT ID FROM counters WHERE url = %s''',
page )
 data = cur.fetchone()#URL is unique, so should only
be one

 if not data:
 #first time for page; primary key is automatic, hit is
defaulted
 cur.execute('''INSERT INTO counters (url) VALUES (%s)''',
page )
 cID = cur.lastrowid#get the primary key value of
the new record
 else:
 #found the page, save primary key and use it to issue hit
UPDATE
 cID = data[0]
 cur.execute('''UPDATE counters SET hits = hits + 1 WHERE ID
= %s''', cID )

 #find the visitor record for the (saved) cID and current host
 cur.execute('''SELECT * FROM visitors WHERE counterID = %s and
host = %s''', (cID, host) )
 data = cur.fetchone()#cIDhost are unique

 if not data:
 #first time for this host on this page, create new record
 cur.execute('''INSERT INTO visitors (counterID, host, city,
useros, browser, lastvisit) VALUES (%s, %s, %s, %s, %s, %s)''', (cID,
host, city, useros, browser, date) )
 else:
 #found the page, save its primary key for later use
 vID = data[0]
 #UPDATE record using retrieved vID
 cur.execute('''UPDATE visitors SET city = %s, useros = %s,
browser = %s, hits = hits + 1, lastvisit = %s
 WHERE counterID = %s and host =
%s''', (city, useros, browser, date, vID, host) )

 con.commit()#if we made it here, the transaction is
complete

 except pymysql.ProgrammingError as e:
 print( repr(e) )
 con.rollback()#something failed, rollback the entire
transaction
 sys.exit(0)


i get no counter increment when visitors visit my webpage.
What on eart is going on?

How the previous error with the invalid byte somehtign got solved?


i still wonder how come the invalid byte messge dissapeared

--
Webhost http://superhost.gr
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError issue

2013-09-01 Thread Dave Angel
On 1/9/2013 18:23, Ferrous Cranus wrote:

snip

 i still wonder how come the invalid byte messge dissapeared


Too bad you never bothered to narrow it down to its source.  It could
be anywhere on those three lines.  If I had to guess, I'd figure it was
one of those environment variables.  The Linux environment variables are
strings of bytes, and the os.environ is a dict of strings.  Apparently
it converts them using utf-8, and if you've somehow set them using some
other encoding, you could be getting that error.

Have you tried to decode those bytes in various encodings other than
utf-8 ?

-- 
Signature file not found

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


Re: semicolon at end of python's statements

2013-09-01 Thread MRAB

On 01/09/2013 20:58, Antoon Pardon wrote:

Op 31-08-13 02:09, Steven D'Aprano schreef:

On Fri, 30 Aug 2013 11:32:17 +0100, Fábio Santos wrote:





We really are spoiled for choice here. We can write any of these:

# Option 1
for spam in sequence:
 if predicate(spam):
 process(spam)





Adding a fourth option:

for spam in sequence if predicate(spam):
 process(spam)

saves absolutely nothing except a line and an indent level, neither of
which are in short supply, and gains nothing in readability over Option 1.


So what is the big difference between this situation and the following:

| else:
| if condition:
| whatever

which in python we can write:

| elif condition:
| whatever


So either is seems this was a design mistake or a line and an indent
level can be important enough to allow a combination of controls.


'elif' is for cascading ifs:

if ...:
   ...
elif ...:
   ...
elif ...:
   ...
elif ...:
   ...
else:
   ...

Without 'elif' you would be forced to write:

if ...:
   ...
else:
if ...:
   ...
else:
if ...:
   ...
else:
if ...:
   ...
else:
   ...

On the other hand, for...if is much less of a problem.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Ben Finney
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info writes:

 […] programmers have been using the *principle* of encapsulation since
 before Grace Hopper was an admiral.

[…]
 Encapsulation and information hiding are distinct things -- you can
 have one without the other.

[…]
 One of the most obnoxious and annoying traits of OOP zealots,
 especially academics, is that they act as if programming did not exist
 before Java and C++, or if you're really lucky, Smalltalk. (Somehow
 they nearly always forget about Simula.)

Yes. That's something which has been pointed out to such people, even in
the Java community, for most (all?) of Java's history.

Here's a JavaWorld article from 2001, by a Java programmer, with the
clear title “Encapsulation is not information hiding”:

Encapsulation refers to the bundling of data with the methods that
operate on that data. Often that definition is misconstrued to mean
that the data is somehow hidden.


URL:http://www.javaworld.com/javaworld/jw-05-2001/jw-0518-encapsulation.html

(That site unfortunately slices up the article into many pages to
increase advertising hits and reader frustration, my apologies.)


One of the more annoying traits of humanity is that whatever context we
first encounter a term is disproportionately privileged, causing us to
irrationally dismiss better (more useful, more reasonable, more
pedagogically appropriate, more historically correct, etc.) definitions.

This thread is an excellent illustration that the field of programming
is no exception.

-- 
 \   Moriarty: “Forty thousand million billion dollars? That money |
  `\must be worth a fortune!” —The Goon Show, _The Sale of |
_o__)   Manhattan_ |
Ben Finney

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


How to split with \ character, and licence copyleft mirror of ©

2013-09-01 Thread materile11
Hello everybody
I'm trying to run this: 

code
 a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
 a.split('\')

SyntaxError: EOL while scanning string literal
/code

I think that the character '\' is the problem, but unfortunately I'm developing 
a small app for windows and I need to show only the name of the .wav file, in 
this case 'flute.wav'.

I also want to know how to mirror a character, in my case this one ©, because 
I'll use the Copyleft http://en.wikipedia.org/wiki/Copyleft to distribute my 
app.

Thanks.

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


Re: How to split with \ character, and licence copyleft mirror of ©

2013-09-01 Thread Cameron Simpson
On 01Sep2013 17:03, materil...@gmail.com materil...@gmail.com wrote:
| code
|  a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
|  a.split('\')
| SyntaxError: EOL while scanning string literal
| /code
| 
| I think that the character '\' is the problem, but unfortunately I'm 
developing a small app for windows and I need to show only the name of the .wav 
file, in this case 'flute.wav'.

Firstly, you want to say '\\' for a slosh (just as you would say '\n' for a 
linefeed).

However, you really should use the os.path module, in particular 
os.path.split().

Have a read of:
  http://docs.python.org/3/library/os.path.html#module-os.path
  http://docs.python.org/2/library/os.path.html#module-os.path

| I also want to know how to mirror a character, in my case this
| one ©, because I'll use the Copyleft http://en.wikipedia.org/wiki/Copyleft
| to distribute my app.

Isn't that a copyright symbol? I'd have a look at the uncidoedata module,
myself.

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

Just because Unix is a multiuser system doesn't mean I want to share it with
anybody!- Paul Tomblin, in rec.aviation.military
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to split with \ character, and licence copyleft mirror of ©

2013-09-01 Thread Tim Chase
On 2013-09-01 17:03, materil...@gmail.com wrote:
 Hello everybody
 I'm trying to run this: 
 
 code
  a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
  a.split('\')
 
 SyntaxError: EOL while scanning string literal
 /code
 
 I think that the character '\' is the problem, but unfortunately
 I'm developing a small app for windows and I need to show only the
 name of the .wav file, in this case 'flute.wav'.

To directly answer your question, you need to escape the \ so it's

  a.split('\\')

That said, it's far better to use Python's built-ins to do the
processing for you:

   import os
   print os.path.basename(a)
  flute.wav

which does what you want *and* works cross-platform:

  [on Linux]
   a = '/home/tkc/path/to/flute.wav'
   print os.path.basename(a)
  flute.wav

 I also want to know how to mirror a character, in my case this one
 ©, because I'll use the Copyleft

This can't be done in much of a general way:  Unicode doesn't specify
this character, and the URL you provided suggests combining two
Unicode characters to get ↄ⃝  Unfortunately, (1) it requires a
display that knows how to produce that, which many terminals can't;
and (2) it's purely visual, not semantic.  If that's what you really
want, you should be able to use:

  copyleft_symbol = u\u2184\u20DD

Just be aware that it may not always display the way you expect it to.

-tkc



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


Re: How to split with \ character, and licence copyleft mirror of ©

2013-09-01 Thread Tim Chase
On 2013-09-02 10:23, Cameron Simpson wrote:
 | I also want to know how to mirror a character, in my case this
 | one ©, because I'll use the Copyleft
 http://en.wikipedia.org/wiki/Copyleft | to distribute my app.
 
 Isn't that a copyright symbol? I'd have a look at the uncidoedata
 module, myself.

Thanks to his link (which would have been more helpful with the
URL fragment:

http://en.wikipedia.org/wiki/Copyleft#Symbol

), I suspect the he means that it should be the mirror image of a
copyright symbol.

And that would be unicodedata, not uncidoedata (I don't think
that was the reversing he was talking about ;-)

-tkc



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


Re: Encapsulation unpythonic?

2013-09-01 Thread Ethan Furman

On 09/01/2013 02:54 PM, Tim Delaney wrote:

Roy Smith wrote:


Nothing is accessible in Python except via getters and setters.  The
only difference between Python and, say, C++ in this regard is that the
Python compiler writes them for you most of the time and doesn't make
you put ()'s at the end of the name


I think Roy is referring to the fact that attribute access is implemented via 
__getattr__ / __getattribute__ /
__setattr__ / __delattr__. From one point of view, he's absolutely correct - 
nearly all attributes are accessed via
getters/setters in Python.


Seems to me there is a difference between an underlying generic protocol for data manipulation and Python writing them 
[getters/setters] for you.


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


Re: Encapsulation unpythonic?

2013-09-01 Thread Roy Smith
In article mailman.464.1378078348.19984.python-l...@python.org,
 Ben Finney ben+pyt...@benfinney.id.au wrote:

 One of the more annoying traits of humanity is that whatever context we
 first encounter a term is disproportionately privileged, causing us to
 irrationally dismiss better (more useful, more reasonable, more
 pedagogically appropriate, more historically correct, etc.) definitions.

Known in the education world as The law of primacy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Roy Smith
In article mailman.468.1378083075.19984.python-l...@python.org,
 Ethan Furman et...@stoneleaf.us wrote:

 On 09/01/2013 02:54 PM, Tim Delaney wrote:
  Roy Smith wrote:
 
  Nothing is accessible in Python except via getters and setters.  The
  only difference between Python and, say, C++ in this regard is that the
  Python compiler writes them for you most of the time and doesn't make
  you put ()'s at the end of the name
 
  I think Roy is referring to the fact that attribute access is implemented 
  via __getattr__ / __getattribute__ /
  __setattr__ / __delattr__. From one point of view, he's absolutely correct 
  - nearly all attributes are accessed via
  getters/setters in Python.
 
 Seems to me there is a difference between an underlying generic protocol for 
 data manipulation and Python writing them 
 [getters/setters] for you.

Why?  When I write foo.bar, a bunch of generic code gets run which 
figures out what value to return.  If I don't like the generic behavior, 
I can write my own __getattrr__(), etc, and make it do whatever I want.

How is that any different from, in C++, if you don't write a default 
constructor, the compiler will write one for you.  If you don't like the 
generic behavior you get from that, you can write your own and make it 
do whatever you want.
-- 
http://mail.python.org/mailman/listinfo/python-list


Simplex Algorithm

2013-09-01 Thread Tommy Vee
Anyone know where I can get an easy to use Python class or algorithm for 
the Simplex optimization algorithm?  I've tried the one in the link 
below, but I can't figure out if a) I'm using it properly, or b) where 
to get the solution.  BTW, I tried some test scenarios using MS Excel's 
Solver and just can't get this algorithm to match Excel's results 
(which is spot on).


http://taw9.hubpages.com/hub/Simplex-Algorithm-in-Python

BTW, if I can't something to work, I'm going to be forced to use the VBA 
programmatic Excel interface. That wouldn't be too bad, but the data 
comes from a DB and getting it properly positioned to use Excel's 
Solver is very painful.  A Python approach would be much cleaner.


Thanks,

TommyVee
--
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Steven D'Aprano
On Sun, 01 Sep 2013 15:13:06 -0400, Roy Smith wrote:

 In article mailman.455.1378062400.19984.python-l...@python.org,
  Ethan Furman et...@stoneleaf.us wrote:
 
 On 09/01/2013 03:09 AM, Fabrice Pombet wrote:
 
  So I guess that we are actually all agreeing on this one.
 
 No, we are not.
 
 encapsulation != inaccessible except by getters/setters
 
 Nothing is accessible in Python except via getters and setters.  The
 only difference between Python and, say, C++ in this regard is that the
 Python compiler writes them for you most of the time and doesn't make
 you put ()'s at the end of the name :-)

Very clever! Pedantic, and an unusual look at what's going on under the 
hood!

I wanted to say it was *not quite correct*, because you can read or write 
directly to the instance dict:

instance.__dict__['name'] = 42


If I understand Python's internals correctly, __dict__ is a slot, and so 
bypasses the usual getattr machinary. But even if so, __dict__['name'] 
uses the dictionary __get/setitem__ method, so it's still a getter/setter 
under the hood.

In any case, even if you are *technically* correct that Python has 
getters and setters under the hood, that's not quite what the discussion 
here is about. But I'm sure you realise that :-)


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to split with \ character, and licence copyleft mirror of �

2013-09-01 Thread Tim Roberts
materil...@gmail.com wrote:

Hello everybody
I'm trying to run this: 

code
 a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
 a.split('\')

SyntaxError: EOL while scanning string literal
/code

I think that the character '\' is the problem, but unfortunately I'm
developing a small app for windows and I need to show only the name
of the .wav file, in this case 'flute.wav'.

I assume you know that backslash has a special meaning in string constants.
For example the string '\n\r' contains exactly two characters, and no
backslashes.

When you want to use an actual backslash in an ordinary string constant,
you have to double it.  So, you could have written your code as:
a = 'E:\\Dropbox\\jjfsdjjsdklfj\\sdfjksdfkjslkj\\flute.wav'
a.split('\\')

Another altrnative is to use raw strings, in which backslashes are not
interpreted:
a = r'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
a.split(r'\')

I assume your filename is actually input to your program, and not as a
constant in your code, so that may not be a problem.  However, there is an
API to do exactly what you're asking:

   import os
   a=r'E:\Dropbox\one\two\three\flute.wav'
   os.path.split(a)
  ('E:\\Dropbox\\one\\two\\three', 'flute.wav')
   os.path.split(a)[1]
  'flute.wav'
  
  
I also want to know how to mirror a character, in my case this one ©,
because I'll use the Copyleft http://en.wikipedia.org/wiki/Copyleft
to distribute my app.

You can't mirror a character.  That is an invented glyph that is not
present in Unicode.  Fortunately, the character doesn't have any legal
meaning, so you can just include explanatory text in your description that
identifies your license.
-- 
Tim Roberts, t...@probo.com
Providenza  Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Steven D'Aprano
On Sun, 01 Sep 2013 20:59:25 -0400, Roy Smith wrote:

 In article mailman.468.1378083075.19984.python-l...@python.org,
  Ethan Furman et...@stoneleaf.us wrote:
 
 On 09/01/2013 02:54 PM, Tim Delaney wrote:
  Roy Smith wrote:
 
  Nothing is accessible in Python except via getters and setters.  The
  only difference between Python and, say, C++ in this regard is that
  the Python compiler writes them for you most of the time and doesn't
  make you put ()'s at the end of the name
 
  I think Roy is referring to the fact that attribute access is
  implemented via __getattr__ / __getattribute__ /
  __setattr__ / __delattr__. From one point of view, he's absolutely
  correct - nearly all attributes are accessed via getters/setters in
  Python.
 
 Seems to me there is a difference between an underlying generic
 protocol for data manipulation and Python writing them
 [getters/setters] for you.
 
 Why?  When I write foo.bar, a bunch of generic code gets run which
 figures out what value to return.  If I don't like the generic behavior,
 I can write my own __getattrr__(), etc, and make it do whatever I want.
 
 How is that any different from, in C++, if you don't write a default
 constructor, the compiler will write one for you.  If you don't like the
 generic behavior you get from that, you can write your own and make it
 do whatever you want.


And fundamentally, all programming is flipping bits, therefore all 
languages are exactly the same, right? :-)


I can't speak for C++, but comparing Java and Python there are 
differences:


- Java the language makes it a pain to change your mind and convert a 
public attribute to a computed attribute. Since the pain of changing your 
mind far outweighs the pain of writing trivial getters/setters up front, 
it is good defensive practice to make attribute access via getters just 
in case. Python makes it trivial to change your mind, and so YAGNI rules 
and you shouldn't write getters unless you actually need them.

- Perhaps because the focus in Java is on massive projects with large 
numbers of barely adequate coders, Java tries to protect the average 
coder from shooting themselves in the foot. Consequently, Java encourages 
a philosophy of default deny when it comes to attribute access: don't 
give your caller access to anything except the absolute minimum you know 
they need. In Python, the focus tends to be more about smaller projects 
with small teams of better than average coders, and a philosophy of 
we're all adults here. Consequently, Python code tends towards 
everything not explicitly prohibited is permitted.

- Similarly, while Java the language doesn't force you to *explicitly* 
declare members as public (if you don't declare members private, they are 
public), it strongly encourages you to think about information hiding and 
explicitly mark members as public. Python does not. There is no way to 
explicitly mark attributes as public.

- The Java compiler enforces public/private, while Python treats it as a 
convention. (At least for pure-Python code.)


But notice that they are differences of degree, not kind. Java encourages 
information hiding in classes, but does not prohibit you from making 
members public, and using reflection you can break into classes and get 
access to anything; Python allows information hiding, but trusts the 
programmer to honour private names, and reflection is such a 
fundamental part of Python that we don't even call it that.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to split with \ character, and licence copyleft mirror of (c)

2013-09-01 Thread Zero Piraeus
:

On 1 September 2013 22:40, Tim Roberts t...@probo.com wrote:
 Another altrnative is to use raw strings, in which backslashes are not
 interpreted:
 a = r'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
 a.split(r'\')

Acually, that doesn't work:

 a = r'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
 a.split('\')
  File stdin, line 1
a.split('\')
   ^
SyntaxError: EOL while scanning string literal

... because you can't end a raw string with a backslash:

http://docs.python.org/3/faq/design.html#why-can-t-raw-strings-r-strings-end-with-a-backslash

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 2/9/2013 2:14 πμ, ο/η Dave Angel έγραψε:

On 1/9/2013 18:23, Ferrous Cranus wrote:

 snip



i still wonder how come the invalid byte messge dissapeared



Too bad you never bothered to narrow it down to its source.



if only i knew how up until yesterday when they were appearing.



It could
be anywhere on those three lines.  If I had to guess, I'd figure it was
one of those environment variables.  The Linux environment variables are
strings of bytes, and the os.environ is a dict of strings.  Apparently
it converts them using utf-8, and if you've somehow set them using some
other encoding, you could be getting that error.

Have you tried to decode those bytes in various encodings other than
utf-8 ?



No, because i wasn't aware of what string/variable they were pertaining at.


--
Webhost http://superhost.gr
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to split with \ character, and licence copyleft mirror of ©

2013-09-01 Thread materile11
El domingo, 1 de septiembre de 2013 19:34:16 UTC-5, Tim Chase  escribió:
 On 2013-09-01 17:03, materil...@gmail.com wrote:
 
  Hello everybody
 
  I'm trying to run this: 
 
  
 
  code
 
   a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
 
   a.split('\')
 
  
 
  SyntaxError: EOL while scanning string literal
 
  /code
 
  
 
  I think that the character '\' is the problem, but unfortunately
 
  I'm developing a small app for windows and I need to show only the
 
  name of the .wav file, in this case 'flute.wav'.
 
 
 
 To directly answer your question, you need to escape the \ so it's
 
 
 
   a.split('\\')
 
 
 
 That said, it's far better to use Python's built-ins to do the
 
 processing for you:
 
 
 
import os
 
print os.path.basename(a)
 
   flute.wav
 
 
 
 which does what you want *and* works cross-platform:
 
 
 
   [on Linux]
 
a = '/home/tkc/path/to/flute.wav'
 
print os.path.basename(a)
 
   flute.wav
 
 
 
  I also want to know how to mirror a character, in my case this one
 
  ©, because I'll use the Copyleft
 
 
 
 This can't be done in much of a general way:  Unicode doesn't specify
 
 this character, and the URL you provided suggests combining two
 
 Unicode characters to get ↄ⃝  Unfortunately, (1) it requires a
 
 display that knows how to produce that, which many terminals can't;
 
 and (2) it's purely visual, not semantic.  If that's what you really
 
 want, you should be able to use:
 
 
 
   copyleft_symbol = u\u2184\u20DD
 
 
 
 Just be aware that it may not always display the way you expect it to.
 
 
 
 -tkc


Thank you, I've used the os.path.basename to solve my problem.
Regards.
-- 
http://mail.python.org/mailman/listinfo/python-list


A Pragmatic Case for Static Typing

2013-09-01 Thread Russ P.
I just stumbled across this video and found it interesting:

http://vimeo.com/72870631

My apologies if it has been posted here already.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Ethan Furman

On 09/01/2013 05:59 PM, Roy Smith wrote:

  Ethan Furman wrote:

On 09/01/2013 02:54 PM, Tim Delaney wrote:

Roy Smith wrote:


Nothing is accessible in Python except via getters and setters.  The
only difference between Python and, say, C++ in this regard is that the
Python compiler writes them for you most of the time and doesn't make
you put ()'s at the end of the name


I think Roy is referring to the fact that attribute access is implemented
via __getattr__ / __getattribute__ /
__setattr__ / __delattr__. From one point of view, he's absolutely correct
- nearly all attributes are accessed via
getters/setters in Python.


Seems to me there is a difference between an underlying generic protocol for
data manipulation and Python writing them
[getters/setters] for you.


Why?  When I write foo.bar, a bunch of generic code gets run which
figures out what value to return.  If I don't like the generic behavior,
I can write my own __getattrr__(), etc, and make it do whatever I want.


I don't yet know C++ so I can't speak to it.

In Python if you are writing your own __getattr__, etc., you are writing the underlying protocol itself.  To write a 
getter/setter you would just use


   def get_soemthing(...)
   def set_something(...)

or, even better

   @property
   def something(...)

Maybe, as Steven said, it's just a matter of degree:  my understanding of getters/setters is that they are per 
attribute, while __getattr__ will be called for every attribute not otherwise found, __setattr__ will be called for 
every assignment, __delattr__ will be called for every deletion, and all that is assuming you haven't written your own 
__getattribute__ and completely changed the rules for your class.


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


Re: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-09-01 Thread anntzer . lee
On Sunday, September 1, 2013 2:03:56 PM UTC-7, Chris Angelico wrote:

  I tried using netifaces (https://pypi.python.org/pypi/netifaces) which 
  seems to rely on getifaddrs (according to the doc, I didn't check the 
  source).  Again, it returns nearly instantaneously the correct IP address.
 
 Perfect!
 
 ChrisA

Not really for my use case -- it isn't that *I* want to know my public IP 
address, but rather that IPython wants to know it.  Of course I could patch 
IPython's source to use netifaces but that sounds like an overkill.

As it happens I found a better way: just add the proper entry to /etc/hosts.

Still, thanks for the suggestions.

Antony
-- 
http://mail.python.org/mailman/listinfo/python-list


[issue18821] Add .lastitem attribute to takewhile instances

2013-09-01 Thread Raymond Hettinger

Changes by Raymond Hettinger raymond.hettin...@gmail.com:


--
assignee:  - rhettinger

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18821
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18752] Make chain.from_iterable an alias for a new chain_iterable.

2013-09-01 Thread Raymond Hettinger

Changes by Raymond Hettinger raymond.hettin...@gmail.com:


--
assignee:  - rhettinger

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18752
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18313] In itertools recipes repeatfunc() defines a non-keyword argument as keyword

2013-09-01 Thread Raymond Hettinger

Changes by Raymond Hettinger raymond.hettin...@gmail.com:


--
assignee: docs@python - rhettinger

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18313
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18726] json functions have too many positional parameters

2013-09-01 Thread Raymond Hettinger

Raymond Hettinger added the comment:

-1 Once the positional arguments are in the wild, you can't take them away 
without breaking a lot of code. 

This code was available as a third-party module prior to inclusion in Python 
and had many happy users.  If the positional arguments had been a sticking 
point, it would have been known long ago.

The time to express these concerns is when a module is being added.  
Afterwards, the API churn just isn't worth it.  We don't want to create more 
obstacles to upgrading or converting from Python 2.

--
assignee:  - rhettinger

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18726
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18893] invalid exception handling in Lib/ctypes/macholib/dyld.py

2013-09-01 Thread Ned Deily

Changes by Ned Deily n...@acm.org:


--
nosy: +ronaldoussoren

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18893
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2013-09-01 Thread Charles-François Natali

Charles-François Natali added the comment:

 Please don't FUD this one to death.  Aligned memory access is
 sometimes important and we currently have no straight-forward
 way to achieve it.

I guess that a simple way to cut the discussion short would be to have a first 
implementation, and run some benchmarks to measure the benefits.

I can certainly see the benefit of cacheline-aligned data structures in 
multithreaded code (to avoid false sharing/cacheline bouncing): I'm really 
curious to see how much this would benefit in a single-threaded workload.

--
nosy: +neologix

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18835
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12304] expose signalfd(2) in the signal module

2013-09-01 Thread STINNER Victor

STINNER Victor added the comment:

 Why do you not provide a structure to decode the bytes? I thought relying
on ctypes in the stdlib was not advised...

We should expose it. The ctypes was just a poof of concept.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue12304
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18571] Implementation of the PEP 446: non-inheritable file descriptors

2013-09-01 Thread Roundup Robot

Roundup Robot added the comment:

New changeset c27527dce71e by Victor Stinner in branch 'default':
Issue #18571: Merge duplicate test code
http://hg.python.org/cpython/rev/c27527dce71e

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18571
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18844] allow weights in random.choice

2013-09-01 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

 A more efficient approach for many use-cases would do the precomputation 
 once, returning some kind of 'distribution' object from which samples can be 
 generated.

I like the idea about adding a family of distribution generators. They should 
check input parameters and make a precomputation and then generate infinite 
sequence of specially distributed random numbers.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18844
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18726] json functions have too many positional parameters

2013-09-01 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

 We don't want to create more obstacles to upgrading or converting from Python 
 2.

But these obstacles already exists.

The 10th parameter of dump() is encoding in Python 2 and simplejson, and 
default in Python 3.

The 12th parameter of dump() is use_decimal simplejson, and sort_keys in 
Python 2.

The 2nd parameter of load() is encoding in Python 2 and simplejson, and cls 
in Python 3.

Due to the fact that most arguments are boolean or accepts None some shifting 
of positional arguments can left unnoticed long time and causes subtle bugs 
when upgrading from Python 2 to Python 3 or switching between stdlib json 
module and simplejson.

Proposed change doesn't break any code at first stage, it just adds deprecation 
warning about using dangerous non-portable feature. We can defer forbidding 
positional parameters until Python 2 will gone and simplejson will be changed 
to keyword-only parameters too.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18726
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18553] os.isatty() is not Unix only

2013-09-01 Thread anatoly techtonik

anatoly techtonik added the comment:

None that I know of.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18553
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18750] '' % [1] doesn't fail

2013-09-01 Thread Andrew Svetlov

Andrew Svetlov added the comment:

Ok. Close as won't fix.
Please reopen if needed.

--
resolution:  - wont fix
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18750
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18874] Add a new tracemalloc module to trace memory allocations

2013-09-01 Thread STINNER Victor

STINNER Victor added the comment:

New patch (version 2):

- an hash table entry can now contain the data directly instead of a pointer to 
the data
- _tracemalloc._get_stats() copies the hash table to not have to lock the hash 
table too long, which will be especially useful for TRACE_RAW_MALLOC
- Add get_object_trace() function which returns a namedtuple
- get_process_memory() returns a namedtuple
- DisplayGarbage class has been removed: the PEP 442 has been implemented, it 
is no more interesting to trace uncollectable objects because they became very 
rare. If you still want to do that, get_object_trace() is available


TODO:

- TRACE_RAW_MALLOC define is still disabled because it introduces a deadlock 
when PyMem_RawMalloc() is called indirectly by Py_NewInterpreter()
- minor pending FIXME in the code
- review the high level API: add an helper to build a diff between two 
snapshots and drop colors (not portable)?


test_capi deadlock with TRACE_RAW_MALLOC:

test_subinterps (test.test_capi.SubinterpreterTest) ... ^C
Program received signal SIGINT, Interrupt.
0xb7fdd424 in __kernel_vsyscall ()
(gdb) where
...
#2  0x081abc1c in PyCOND_TIMEDWAIT (cond=0x83470e0, mut=0x8347110, us=5000) 
at Python/condvar.h:103
#3  0x081ac55c in take_gil (tstate=0x8349c78) at Python/ceval_gil.h:224
#4  0x081ad0a9 in PyEval_RestoreThread (tstate=0x8349c78) at 
Python/ceval.c:443
#5  0x081f08e3 in PyGILState_Ensure () at Python/pystate.c:786
#6  0x0808df1b in trace_get_filename (trace=0x84c26b8, gil_held=0) at 
./Modules/_tracemalloc.c:555
#7  0x0808e314 in trace_log_alloc (ptr=0x84c9f18, size=52, gil_held=0) at 
./Modules/_tracemalloc.c:698
#8  0x0808e449 in trace_malloc (ctx=0x8342890, size=52, gil_held=0) at 
./Modules/_tracemalloc.c:744
#9  0x0808e5b8 in trace_raw_malloc (ctx=0x8342890, size=52) at 
./Modules/_tracemalloc.c:811
#10 0x0805d32a in PyMem_RawMalloc (size=52) at Objects/obmalloc.c:256
#11 0x081eeb79 in PyInterpreterState_New () at Python/pystate.c:63
#12 0x08060c8b in Py_NewInterpreter () at Python/pythonrun.c:700
#13 0xb74d4b90 in run_in_subinterp ()
#14 0x080e9e7a in PyCFunction_Call ()
...

--
Added file: http://bugs.python.org/file31545/tracemalloc-2.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18874
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18885] handle EINTR in the stdlib

2013-09-01 Thread Armin Rigo

Changes by Armin Rigo ar...@users.sourceforge.net:


--
nosy: +arigo

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18885
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6299] pyexpat build failure on Solaris 10 for 2.6.1/2.6.2

2013-09-01 Thread koobs

Changes by koobs koobs.free...@gmail.com:


--
nosy: +koobs

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6299
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18844] allow weights in random.choice

2013-09-01 Thread Madison May

Madison May added the comment:

[Raymond Hettinger]
 The sticking point is going to be that we don't want to recompute the 
 cumulative weights for every call to weighted_choice.

 So there should probably be two functions:

  cw = make_cumulate_weights(weight_list) 
  x = choice(choice_list, cw)

That's pretty much how I broke things up when I decided to test out 
optimization with lru_cache.  That version of the patch is now attached.

[Serhiy Storchaka]
 I like the idea about adding a family of distribution generators. 
 They should check input parameters and make a precomputation and then  
 generate infinite sequence of specially distributed random numbers.

Would these distribution generators be implemented internally (see attached 
patch) or publicly exposed?

--
Added file: http://bugs.python.org/file31546/weighted_choice_v2.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18844
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18844] allow weights in random.choice

2013-09-01 Thread Madison May

Changes by Madison May madison@students.olin.edu:


Removed file: http://bugs.python.org/file31546/weighted_choice_v2.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18844
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18844] allow weights in random.choice

2013-09-01 Thread Madison May

Changes by Madison May madison@students.olin.edu:


Added file: http://bugs.python.org/file31547/weighted_choice_v2.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18844
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue16826] Don't check for PYTHONCASEOK if interpreter started with -E

2013-09-01 Thread Meador Inge

Changes by Meador Inge mead...@gmail.com:


--
assignee:  - meador.inge
stage: patch review - commit review

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16826
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18889] test_sax: multiple failures on Windows desktop

2013-09-01 Thread Tim Peters

Tim Peters added the comment:

Suggest caution here.  test_sax fails the same way for me too (Windows Vista), 
under both the released 3.3.2 and a Python built from the current hg default 
branch.

However, these files (test.xml and test.xml.out) have not changed since the 
Python 2.7 line - the \r\n line endings have _always_ been there, and test_sax 
works fine under (e.g.) Python 2.7.5 on Windows.

So it's not that the files have changed, it must be that Python is behaving 
differently.

Unfortunately, I don't know anything about what test_sax is trying to do.  I do 
see that under 2.7, test_sax does

xml_test_out = open(TEST_XMLFILE_OUT).read()

but 3.3 does

with open(TEST_XMLFILE_OUT, 'rb') as f:
xml_test_out = f.read()

That is, 2.7 opens the file for reading in text mode, but 3.3 opens it in 
binary mode.  That makes a big difference for text files under Windows.

It's also disturbing that the Windows buildbots don't fail.  There is no change 
in environment that should affect the bytes seen when a file is read - and 
the buildbots should be seeing, byte for byte, the same files developers and 
users see.

--
nosy: +tim.peters

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18889
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18899] make pystone.py Py3 compatible in benchmark suite

2013-09-01 Thread Stefan Behnel

New submission from Stefan Behnel:

diff --git a/performance/pystone.py b/performance/pystone.py
--- a/performance/pystone.py
+++ b/performance/pystone.py
@@ -59,9 +59,9 @@
 
 def main(loops=LOOPS):
 benchtime, stones = pystones(loops)
-print Pystone(%s) time for %d passes = %g % \
-  (__version__, loops, benchtime)
-print This machine benchmarks at %g pystones/second % stones
+print(Pystone(%s) time for %d passes = %g %
+  (__version__, loops, benchtime))
+print(This machine benchmarks at %g pystones/second % stones)
 
 
 def pystones(loops=LOOPS):
@@ -72,7 +72,7 @@
 Char1Glob = '\0'
 Char2Glob = '\0'
 Array1Glob = [0]*51
-Array2Glob = map(lambda x: x[:], [Array1Glob]*51)
+Array2Glob = list(map(lambda x: x[:], [Array1Glob]*51))
 PtrGlb = None
 PtrGlbNext = None

--
components: Benchmarks
messages: 196723
nosy: scoder
priority: normal
severity: normal
status: open
title: make pystone.py Py3 compatible in benchmark suite
type: behavior

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18899
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18899] make pystone.py Py3 compatible in benchmark suite

2013-09-01 Thread Antoine Pitrou

Antoine Pitrou added the comment:

Oh, pystone... I didn't know we had included that particular horror in the 
benchmark suite :-)

--
nosy: +brett.cannon, pitrou
stage:  - patch review
versions: +3rd party

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18899
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18553] os.isatty() is not Unix only

2013-09-01 Thread Antoine Pitrou

Antoine Pitrou added the comment:

 Are there tests for this?

Not sure what you mean: there are several tests using isatty() in test suite.

Regardless, there is no #ifdef around the isatty() definition in posixmodule.c, 
so I can only assume it exists on all platforms.

--
nosy: +pitrou

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18553
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18889] test_sax: multiple failures on Windows desktop

2013-09-01 Thread Tim Peters

Tim Peters added the comment:

Seeing as Python 3 _does_ open these files in binary mode, I fiddled my local 
.hgeol to mark the test files as BIN (then deleted the test data directory and 
did an hg revert on the directory).  Then test_sax passes.  

I don't know whether that's the proper fix, but it works ;-)

--
Added file: http://bugs.python.org/file31548/eol-patch.txt

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18889
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18900] Add the random.distrib module

2013-09-01 Thread Serhiy Storchaka

New submission from Serhiy Storchaka:

In some functions in the random module checking input arguments and 
precomputation takes a considerable portion of time. Here is a sample 
implementation of new random.distrib module which provides alternative faster 
interface to generating of random distributed values. It contains generators 
which generates values with same distributions as functions with same name in 
the random module.

Benchmark results:

random distrib
random() 0.061   0.055  1.12
randrange(0, 100, 5) 1.494   0.620  2.41
randint(1, 100)  1.283   0.551  2.33
uniform(-10.0, 10.0) 0.332   0.121  2.73
triangular(0.0, 10.0, 6.0)   0.661   0.317  2.09
gauss(5.0, 2.0)  0.707   0.280  2.53
normalvariate(5.0, 2.0)  0.867   0.553  1.57
lognormvariate(5.0, 2.0) 1.078   0.640  1.68
expovariate(0.1,)0.508   0.293  1.73
vonmisesvariate(1.0, 1.0)1.201   0.671  1.79
gammavariate(0.35, 1.45) 1.117   0.508  2.20
betavariate(2.71828, 3.14159)2.868   1.776  1.61
paretovariate(5.0,)  0.493   0.238  2.07
weibullvariate(1.0, 3.0) 0.670   0.402  1.67
choice([0, 1, 2, 3, 4, 5, 6...   0.887   0.594  1.49

Distrib functions are 1.5-2.8 times faster than random functions. Weighted 
choice() function (see issue18844) can be even dozens times faster (depends on 
size of the input).

In additional some random generators (i.e. gauss()) looks simpler when 
implemented as generators. distrib.gauss() is twice faster than 
distrib.normalvariate() (both generates numbers with same distribution) and I 
think some other generators can be implemented more efficient in generator 
style.

--
components: Library (Lib)
files: distrib.py
messages: 196727
nosy: mark.dickinson, rhettinger, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Add the random.distrib module
type: enhancement
versions: Python 3.4
Added file: http://bugs.python.org/file31549/distrib.py

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18900
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18900] Add the random.distrib module

2013-09-01 Thread Serhiy Storchaka

Changes by Serhiy Storchaka storch...@gmail.com:


Added file: http://bugs.python.org/file31550/distrib_bench.py

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18900
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18844] allow weights in random.choice

2013-09-01 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

 Would these distribution generators be implemented internally (see attached 
 patch) or publicly exposed?

See issue18900. Even if this proposition will be rejected I think we should 
publicly expose weighted choice_generator(). A generator or a builder which 
returns function are only ways how efficiently implement this feature. Use 
lru_cache isn't good because several choice generators can be used in a program 
and because it left large data in a cache long time after it was used.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18844
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18901] sunau.getparams should return a namedtuple

2013-09-01 Thread Claudiu.Popa

New submission from Claudiu.Popa:

This is similar to what was done for issue17818 and issue17487 and will gain 
for all the three audio libraries, aifc, wave and sunau a similar API. Patch 
and tests attached.

--
components: Library (Lib)
files: sunau.patch
keywords: patch
messages: 196729
nosy: Claudiu.Popa
priority: normal
severity: normal
status: open
title: sunau.getparams should return a namedtuple
versions: Python 3.4
Added file: http://bugs.python.org/file31551/sunau.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18901
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18105] ElementTree writes invalid files when UTF-16 encoding is specified

2013-09-01 Thread Serhiy Storchaka

Changes by Serhiy Storchaka storch...@gmail.com:


--
nosy: +scoder

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18105
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18844] allow weights in random.choice

2013-09-01 Thread Madison May

Madison May added the comment:

 Use lru_cache isn't good because several choice generators can be used in a 
 program and because it left large data in a cache long time after it was used.

Yeah, I just did a quick search of the stdlib and only found one instance of 
lru_cache in use -- another sign that lru_cache is a bad choice.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18844
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18900] Add the random.distrib module

2013-09-01 Thread Madison May

Madison May added the comment:

I like the core idea of a family of random generators, but it feels like a new 
module that's nearly identical to random introduces a lot of repeated code.

Perhaps adding an additional optional arg ('generator=False', for example) to 
these functions in the random module would be a bit simpler.

--
nosy: +madison.may

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18900
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18889] test_sax: multiple failures on Windows desktop

2013-09-01 Thread Antoine Pitrou

Antoine Pitrou added the comment:

Marking the files as binary (really don't touch) in .hgeol sounds 
reasonable to me.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18889
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18889] test_sax: multiple failures on Windows desktop

2013-09-01 Thread Terry J. Reedy

Terry J. Reedy added the comment:

Two things have happened with Python: we switched bytes from 'text or binary' 
to 'binary (or text)' in 3.0. We switched from svn to hg in 3.2. When did 
test_sax start failing? I have no idea. Until recently, there were much worse 
problems with the test suite on desktop windows; it sometimes quit before 
getting to 'sax'.

Email and its tests (which had the same eol issue) were heavily revised in 3.3; 
I do not know if the test that failed in 3.3 failed earlier or not. Another 
failing email test was added in 3.4.

David, Antoine: Tim questions/challenges the solution pushed in #12037.

--
nosy: +pitrou, r.david.murray

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18889
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue16531] Allow IPNetwork to take a tuple

2013-09-01 Thread Jon Foster

Changes by Jon Foster jongfos...@users.sourceforge.net:


--
nosy: +jongfoster

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16531
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18889] test_sax: multiple failures on Windows desktop

2013-09-01 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

 Nit 2: why? The test passes as is.

Currently test.xml and test.xml.out contain only ascii characters. But this can 
be changed in future. Actually I had added non-ascii characters to other test 
data and this had exposed some bugs in test suite.

 When did test_sax start failing? I have no idea.

I guess it becomes after my commit in issue1470548 a half year ago.

All proposed fixes are good enough. Feel free to commit what you prefers.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18889
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18889] test_sax: multiple failures on Windows desktop

2013-09-01 Thread Tim Peters

Tim Peters added the comment:

test_email still passed on Windows under 3.2.5 (but test_sax failed).  
test_email and test_sax both fail under 3.3.2.

I'll just push the change to .hgeol - minimally invasive, fixes the immediate 
problem, and it appears these really are binary files.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18889
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18105] ElementTree writes invalid files when UTF-16 encoding is specified

2013-09-01 Thread Stefan Behnel

Stefan Behnel added the comment:

I can well imagine that the serialiser is broken for this in Py2.x, given that 
the API accepts byte strings and stores them as such. The fix might be as 
simple as decoding byte strings in the serialiser before writing them out. 
Involves a pretty high performance regression, though (and ET's serialiser is 
known to be rather slow anyway).

Not sure if the current behaviour should be changed in 2.x.

In any case, it's a duplicate of the other ticket, which was *not* fixed for 
2.7.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18105
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18889] test_sax: multiple failures on Windows desktop

2013-09-01 Thread Terry J. Reedy

Terry J. Reedy added the comment:

Having the files be the same on all systems seems the right solution. Changing 
.hgeol should do that, at least for the repository. Let's hope the Windows .msi 
installer leave line endings alone. I should install the a,b,c releases just to 
run the test suite with them.

Side note: just above the quoted context in .hgeol is
Lib/email/test/data/msg_26.txt = BIN
which does not exist now.
I suspect it should be removed as the obsolete old version of
Lib/test/test_email/data/msg_26.txt = BIN
which is in the quoted context.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18889
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18900] Add the random.distrib module

2013-09-01 Thread Antoine Pitrou

Changes by Antoine Pitrou pit...@free.fr:


--
nosy: +tim.peters

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18900
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



  1   2   >