ANN: Lea 1.1 (discrete probability distributions)

2013-09-26 Thread Pierre Denis
I have the pleasure to announce the release of Lea 1.1.

Lea is a Python package aiming at working with discrete probability
distributions in an intuitive way.

It allows modelling a broad range of random discrete phenomenons. Then, it
allows calculating probabilities of events, whether atomic, aggregated or
combined through given operations. A typical example is the probabilities of
the sum of N dice having known, possibly unfair, probability distributions.

Features


Here are the main features of Lea:

- models finite discrete probability distributions
- standard distribution indicators (mean, standard deviation,.)
- arithmetic and logical operators on probability distribution 
- cartesian products, conditional probabilities, joint distributions
- generation of random samples
- open-source project, LGPL license
- pure Python module, lightweight - no package dependency
- probabilities stored as integers (no floating-point biases)

Links
-

Download (PyPi):http://pypi.python.org/pypi/lea
Project page:   http://code.google.com/p/lea/

(with wiki documentation including tutorials, examples and API)

Hoping this could be helpful in this uncertain universe...

Pierre Denis

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

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


Re: Convert namedtuple to dictionary

2013-09-26 Thread Peter Otten
trip...@gmail.com wrote:

 Need suggestions.
 
 Say, I have a namedtuple like this:
 
 {'a': brucelee(x=123, y=321), 'b': brucelee('x'=123, 'y'=321)
 
 I need to convert it to:
 
 {'a': {'x':123, 'y': 321},'b': {'x':123, 'y': 321}}
 
 Follow-up question --
 
 Which would be easier to work with if I had to later extract/manipulate
 the 'x', 'y' values? The format (dicts) above or a list of values like
 this:
 
 {'a': ['x':123, 'y': 321],'b': ['x':123, 'y': 321]}

One more:

 from collections import namedtuple
 brucelee = namedtuple(brucelee, x y)
 def to_dict(n):
... return dict(zip(n._fields, n))
... 
 data = {'a': brucelee(x=123, y=321), 'b': brucelee(x=123, y=321)}
 {k: to_dict(v) for k, v in data.items()}
{'a': {'y': 321, 'x': 123}, 'b': {'y': 321, 'x': 123}}


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


Re: python IDE and function definition

2013-09-26 Thread Ben Finney
Chris Friesen cbf...@mail.usask.ca writes:

 I'm running into issues where my current IDE (I'm playing with Komodo)
 can't seem to locate the definition, I suspect because it's too
 ambiguous.

The feature you're looking for – to start from the statement where a
function is called, then jump to the statement where that function is
defined – is implemented via “tags” across many languages and tools.

One very popular implementation is “Exuberant Ctags”. You can choose
from the tools URL:http://ctags.sourceforge.net/tools.html that
support that system, and continue using the same system when you switch
to a different language or a different tool.

 So rather than give up, I'd like to have my IDE suggest all possible
 answers.

Good luck to you in learning a development environment that is *not*
tied to the particular programming language you're writing.

-- 
 \  “… a Microsoft Certified System Engineer is to information |
  `\ technology as a McDonalds Certified Food Specialist is to the |
_o__)   culinary arts.” —Michael Bacarella |
Ben Finney

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


Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Hello,

How can i wrote the two following lines so for NOT to throw out 
KeyErrors when a key is missing?


city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or 
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or Άγνωστη Πόλη


host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or 
socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or Άγνωστη Προέλευση


I was under the impression that the 'or' operator was handling this in 
case one operand was failing but its not the case here.


I believe in a KeyError is missing the expression cannot even be 
evaluates as Truthy or Falsy.


Then i thought of os.environ.get() to default to something but then 
again we have 3 operand in the expression.

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Jussi Piitulainen
Νίκος writes:

 How can i wrote the two following lines so for NOT to throw out
 KeyErrors when a key is missing?
 
 city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
...
 I was under the impression that the 'or' operator was handling this
 in case one operand was failing but its not the case here.

f(x) or g(x) raises an exception if f(x) raises an exception, or
if f(x) returns a false value and g(x) raises an exception.

 Then i thought of os.environ.get() to default to something but then
 again we have 3 operand in the expression.

Adapt this:

   {}.get('foo') or {'empty':''}.get('empty') or 'catchall'
  'catchall'

Or nest the calls this way if an empty string is a valid value:

   {}.get('foo', {'empty':''}.get('empty', 'catchall'))
  ''

This will compute the default values even when they are not used.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Στις 26/9/2013 10:48 πμ, ο/η Jussi Piitulainen έγραψε:

Νίκος writes:


How can i wrote the two following lines so for NOT to throw out
KeyErrors when a key is missing?

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or

...

I was under the impression that the 'or' operator was handling this
in case one operand was failing but its not the case here.


f(x) or g(x) raises an exception if f(x) raises an exception, or
if f(x) returns a false value and g(x) raises an exception.


Then i thought of os.environ.get() to default to something but then
again we have 3 operand in the expression.


Adapt this:

{}.get('foo') or {'empty':''}.get('empty') or 'catchall'
   'catchall'

Or nest the calls this way if an empty string is a valid value:

{}.get('foo', {'empty':''}.get('empty', 'catchall'))
   ''

This will compute the default values even when they are not used.

I'am sorry but i do not understand the last statements at all so i can 
have chnace to adapt them.

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Jussi Piitulainen
Νίκος writes:

 Σ�ις 26/9/2013 10:48 πμ, ο/η Jussi Piitulainen
 έγ�α�ε:  ί�ος writes:
 
  How can i wrote the two following lines so for NOT to throw out
  KeyErrors when a key is missing?
 
  city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
  ...
  I was under the impression that the 'or' operator was handling this
  in case one operand was failing but its not the case here.
 
  f(x) or g(x) raises an exception if f(x) raises an exception, or
  if f(x) returns a false value and g(x) raises an exception.
 
  Then i thought of os.environ.get() to default to something but then
  again we have 3 operand in the expression.
 
  Adapt this:
 
  {}.get('foo') or {'empty':''}.get('empty') or 'catchall'
 'catchall'
 
  Or nest the calls this way if an empty string is a valid value:
 
  {}.get('foo', {'empty':''}.get('empty', 'catchall'))
 ''
 
  This will compute the default values even when they are not used.

 I'am sorry but i do not understand the last statements at all so i
 can have chnace to adapt them.

Do you know what {} is?

Do you know what {}.get('foo') is?

Do you know what x.get('foo') is if x is {}?

Do you know what {'empty':''}.get('empty') is?

Do you know what {'empty':''}.get('fruit') is?

Do you know what (None or '' or 'catchall') is?

Do you know what {}.get('foo', 'bar') is?

Do you know what {}.get('foo', {}.get('bar', 'huh')) is?

Do you know what ('foo'[3] or 'else') does?

Do you know what ('foo' or 'else'[5]) does?

Do you know how to launch an interactive Python session where you can
play with such expressions until you get the hang of it? There is no
substitute for that experience.

Do you know that you can ask for help({}.get) or help(dict.get) or
even help(os.environ.get) during such an interactive Python session,
and Python (unlike Macbeth's spirits from the vasty deep) will answer?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Στις 26/9/2013 11:12 πμ, ο/η Jussi Piitulainen έγραψε:

Νίκος writes:


Σ�ις 26/9/2013 10:48 πμ, ο/η Jussi Piitulainen
έγ�α�ε:  ί�ος writes:



How can i wrote the two following lines so for NOT to throw out
KeyErrors when a key is missing?

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or

...

I was under the impression that the 'or' operator was handling this
in case one operand was failing but its not the case here.


f(x) or g(x) raises an exception if f(x) raises an exception, or
if f(x) returns a false value and g(x) raises an exception.


Then i thought of os.environ.get() to default to something but then
again we have 3 operand in the expression.


Adapt this:

 {}.get('foo') or {'empty':''}.get('empty') or 'catchall'
'catchall'

Or nest the calls this way if an empty string is a valid value:

 {}.get('foo', {'empty':''}.get('empty', 'catchall'))
''

This will compute the default values even when they are not used.


I'am sorry but i do not understand the last statements at all so i
can have chnace to adapt them.


Do you know what {} is?

Do you know what {}.get('foo') is?

Do you know what x.get('foo') is if x is {}?

Do you know what {'empty':''}.get('empty') is?

Do you know what {'empty':''}.get('fruit') is?

Do you know what (None or '' or 'catchall') is?

Do you know what {}.get('foo', 'bar') is?

Do you know what {}.get('foo', {}.get('bar', 'huh')) is?

Do you know what ('foo'[3] or 'else') does?

Do you know what ('foo' or 'else'[5]) does?

Do you know how to launch an interactive Python session where you can
play with such expressions until you get the hang of it? There is no
substitute for that experience.

Do you know that you can ask for help({}.get) or help(dict.get) or
even help(os.environ.get) during such an interactive Python session,
and Python (unlike Macbeth's spirits from the vasty deep) will answer?


You dont have to be ironic. I dont have the experience you do.

Up until now i have this:

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or 
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or Άγνωστη Πόλη



can this be written as:

city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP', 
os.environ['REMOTE_ADDR'] )) or Άγνωστη Πόλη


It makes it more easily for me to understand this way.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Chris Angelico
On Thu, Sep 26, 2013 at 6:12 PM, Jussi Piitulainen
jpiit...@ling.helsinki.fi wrote:
 Do you know that you can ask for help({}.get) or help(dict.get) or
 even help(os.environ.get) during such an interactive Python session,
 and Python (unlike Macbeth's spirits from the vasty deep) will answer?

Speak, Python, speak!
We're all attention.
The news we seek
This moment mention!

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Tim Golden
On 26/09/2013 09:41, Chris Angelico wrote:
 On Thu, Sep 26, 2013 at 6:12 PM, Jussi Piitulainen
 jpiit...@ling.helsinki.fi wrote:
 Do you know that you can ask for help({}.get) or help(dict.get) or
 even help(os.environ.get) during such an interactive Python session,
 and Python (unlike Macbeth's spirits from the vasty deep) will answer?
 
 Speak, Python, speak!
 We're all attention.
 The news we seek
 This moment mention!

I remember when I saw that in a production in Altrincham, the Prince
threw off his cloak dramatically to step forward-- and the clasp caught
on some part of his costume, reducing the effect somewhat as the cloak
dragged along behind. (In the end some other cast member came forward
and just ripped it free).

[GS The Gondoliers, for those who weren't following along]

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Στις 26/9/2013 11:46 πμ, ο/η Νίκος έγραψε:

Στις 26/9/2013 11:39 πμ, ο/η Νίκος έγραψε:

Στις 26/9/2013 11:12 πμ, ο/η Jussi Piitulainen έγραψε:

Νίκος writes:


Σ�ις 26/9/2013 10:48 πμ, ο/η Jussi Piitulainen
έγ�α�ε:  ί�ος writes:



How can i wrote the two following lines so for NOT to throw out
KeyErrors when a key is missing?

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or

...

I was under the impression that the 'or' operator was handling this
in case one operand was failing but its not the case here.


f(x) or g(x) raises an exception if f(x) raises an exception, or
if f(x) returns a false value and g(x) raises an exception.


Then i thought of os.environ.get() to default to something but then
again we have 3 operand in the expression.


Adapt this:

 {}.get('foo') or {'empty':''}.get('empty') or 'catchall'
'catchall'

Or nest the calls this way if an empty string is a valid value:

 {}.get('foo', {'empty':''}.get('empty', 'catchall'))
''

This will compute the default values even when they are not used.


I'am sorry but i do not understand the last statements at all so i
can have chnace to adapt them.


Do you know what {} is?

Do you know what {}.get('foo') is?

Do you know what x.get('foo') is if x is {}?

Do you know what {'empty':''}.get('empty') is?

Do you know what {'empty':''}.get('fruit') is?

Do you know what (None or '' or 'catchall') is?

Do you know what {}.get('foo', 'bar') is?

Do you know what {}.get('foo', {}.get('bar', 'huh')) is?

Do you know what ('foo'[3] or 'else') does?

Do you know what ('foo' or 'else'[5]) does?

Do you know how to launch an interactive Python session where you can
play with such expressions until you get the hang of it? There is no
substitute for that experience.

Do you know that you can ask for help({}.get) or help(dict.get) or
even help(os.environ.get) during such an interactive Python session,
and Python (unlike Macbeth's spirits from the vasty deep) will answer?


You dont have to be ironic. I dont have the experience you do.

Up until now i have this:

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or Άγνωστη Πόλη


can this be written as:

city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP',
os.environ['REMOTE_ADDR'] )) or Άγνωστη Πόλη

It makes it more easily for me to understand this way.


Let me try be more specific:

i want to switch this:

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or Άγνωστη Πόλη

host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or Άγνωστη Προέλευση

because it raises KeyError exceptions to this:

city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP',
'REMOTE_ADDR') )or Άγνωστη Πόλη

host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP',
'REMOTE_ADDR') )[0] or Άγνωστη Προέλευση

But that doesnt seem to also work.
I want to use the get method because i know if it doesnt does detect a
dictionary key item than default back what we give it.


Can you please tell me why my alternative fails to work although i'am 
using the .get method to default to something?

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Nobody
On Thu, 26 Sep 2013 10:26:48 +0300, Νίκος wrote:

 How can i wrote the two following lines so for NOT to throw out
 KeyErrors when a key is missing?
 
 city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
 gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
 Άγνωστη Πόλη

tz = None
ip = os.environ.get('HTTP_CF_CONNECTING_IP')
if ip:
  tz = gi.time_zone_by_addr(ip)
if not tz:
  ip = os.environ.get('REMOTE_ADDR')
  if ip:
tz = gi.time_zone_by_addr(ip)
if not tz:
  tz = ÎγνÏÏÏη Î Ïλη

Likewise for the hostname.

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Στις 26/9/2013 11:39 πμ, ο/η Νίκος έγραψε:

Στις 26/9/2013 11:12 πμ, ο/η Jussi Piitulainen έγραψε:

Νίκος writes:


Σ�ις 26/9/2013 10:48 πμ, ο/η Jussi Piitulainen
έγ�α�ε:  ί�ος writes:



How can i wrote the two following lines so for NOT to throw out
KeyErrors when a key is missing?

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or

...

I was under the impression that the 'or' operator was handling this
in case one operand was failing but its not the case here.


f(x) or g(x) raises an exception if f(x) raises an exception, or
if f(x) returns a false value and g(x) raises an exception.


Then i thought of os.environ.get() to default to something but then
again we have 3 operand in the expression.


Adapt this:

 {}.get('foo') or {'empty':''}.get('empty') or 'catchall'
'catchall'

Or nest the calls this way if an empty string is a valid value:

 {}.get('foo', {'empty':''}.get('empty', 'catchall'))
''

This will compute the default values even when they are not used.


I'am sorry but i do not understand the last statements at all so i
can have chnace to adapt them.


Do you know what {} is?

Do you know what {}.get('foo') is?

Do you know what x.get('foo') is if x is {}?

Do you know what {'empty':''}.get('empty') is?

Do you know what {'empty':''}.get('fruit') is?

Do you know what (None or '' or 'catchall') is?

Do you know what {}.get('foo', 'bar') is?

Do you know what {}.get('foo', {}.get('bar', 'huh')) is?

Do you know what ('foo'[3] or 'else') does?

Do you know what ('foo' or 'else'[5]) does?

Do you know how to launch an interactive Python session where you can
play with such expressions until you get the hang of it? There is no
substitute for that experience.

Do you know that you can ask for help({}.get) or help(dict.get) or
even help(os.environ.get) during such an interactive Python session,
and Python (unlike Macbeth's spirits from the vasty deep) will answer?


You dont have to be ironic. I dont have the experience you do.

Up until now i have this:

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or Άγνωστη Πόλη


can this be written as:

city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP',
os.environ['REMOTE_ADDR'] )) or Άγνωστη Πόλη

It makes it more easily for me to understand this way.


Let me try be more specific:

i want to switch this:

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or 
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or Άγνωστη Πόλη


host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or 
socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or Άγνωστη Προέλευση


because it raises KeyError exceptions to this:

city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP', 
'REMOTE_ADDR') )or Άγνωστη Πόλη


host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP', 
'REMOTE_ADDR') )[0] or Άγνωστη Προέλευση


But that doesnt seem to also work.
I want to use the get method because i know if it doesnt does detect a 
dictionary key item than default back what we give it.

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Jussi Piitulainen
Νίκος writes:

 Σ�ις 26/9/2013 11:12 πμ, ο/η Jussi Piitulainen
 έγ�α�ε:  ί�ος writes:
 
  Σ�ις 26/9/2013 10:48 πμ, ο/η Jussi Piitulainen
  έγ�α�ε:  ί�ος writes:
 
  How can i wrote the two following lines so for NOT to throw out
  KeyErrors when a key is missing?
 
  city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
  ...
  I was under the impression that the 'or' operator was handling this
  in case one operand was failing but its not the case here.
 
  f(x) or g(x) raises an exception if f(x) raises an exception, or
  if f(x) returns a false value and g(x) raises an exception.
 
  Then i thought of os.environ.get() to default to something but then
  again we have 3 operand in the expression.
 
  Adapt this:
 
   {}.get('foo') or {'empty':''}.get('empty') or 'catchall'
  'catchall'
 
  Or nest the calls this way if an empty string is a valid value:
 
   {}.get('foo', {'empty':''}.get('empty', 'catchall'))
  ''
 
  This will compute the default values even when they are not used.
 
  I'am sorry but i do not understand the last statements at all so i
  can have chnace to adapt them.
 
  Do you know what {} is?
 
  Do you know what {}.get('foo') is?
 
  Do you know what x.get('foo') is if x is {}?
 
  Do you know what {'empty':''}.get('empty') is?
 
  Do you know what {'empty':''}.get('fruit') is?
 
  Do you know what (None or '' or 'catchall') is?
 
  Do you know what {}.get('foo', 'bar') is?
 
  Do you know what {}.get('foo', {}.get('bar', 'huh')) is?
 
  Do you know what ('foo'[3] or 'else') does?
 
  Do you know what ('foo' or 'else'[5]) does?
 
  Do you know how to launch an interactive Python session where you can
  play with such expressions until you get the hang of it? There is no
  substitute for that experience.
 
  Do you know that you can ask for help({}.get) or help(dict.get) or
  even help(os.environ.get) during such an interactive Python session,
  and Python (unlike Macbeth's spirits from the vasty deep) will answer?

 You dont have to be ironic. I dont have the experience you do.

Not ironic. Such questions really are how I learn and test my own
understanding, and all of the questions above are tailored to your
current specific problem. They are meant to help you.

But you should know the answers to such questions, even if you learn
them in some altogether different way. This is basic stuff.

 Up until now i have this:
 
 city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
 gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
 �γν���η Π�λη
 
 
 can this be written as:
 
 city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP',
 os.environ['REMOTE_ADDR'] )) or �γν���η
 Π�λη
 
 It makes it more easily for me to understand this way.

That will always get os.environ['REMOTE_ADDR'] and raise exception if
it doesn't exist.

Maybe you want this:

city = gi.time_zone_by_addr(os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR') or
�γν·Π�λη)

Though I would prefer a narrower layout this way:

something = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
  os.environ.get('REMOTE_ADDR') or
  �γν·Π�λη )
city = gi.time_zone_by_addr(something)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Antoon Pardon
Op 26-09-13 10:39, Νίκος schreef:
 Στις 26/9/2013 11:12 πμ, ο/η Jussi Piitulainen έγραψε:
 Νίκος writes:

 Σ�ις 26/9/2013 10:48 πμ, ο/η Jussi Piitulainen
 έγ�α�ε:  ί�ος writes:

 How can i wrote the two following lines so for NOT to throw out
 KeyErrors when a key is missing?

 city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
 ...
 I was under the impression that the 'or' operator was handling this
 in case one operand was failing but its not the case here.

 f(x) or g(x) raises an exception if f(x) raises an exception, or
 if f(x) returns a false value and g(x) raises an exception.

 Then i thought of os.environ.get() to default to something but then
 again we have 3 operand in the expression.

 Adapt this:

  {}.get('foo') or {'empty':''}.get('empty') or 'catchall'
 'catchall'

 Or nest the calls this way if an empty string is a valid value:

  {}.get('foo', {'empty':''}.get('empty', 'catchall'))
 ''

 This will compute the default values even when they are not used.

 I'am sorry but i do not understand the last statements at all so i
 can have chnace to adapt them.

 Do you know what {} is?

 Do you know what {}.get('foo') is?

 Do you know what x.get('foo') is if x is {}?

 Do you know what {'empty':''}.get('empty') is?

 Do you know what {'empty':''}.get('fruit') is?

 Do you know what (None or '' or 'catchall') is?

 Do you know what {}.get('foo', 'bar') is?

 Do you know what {}.get('foo', {}.get('bar', 'huh')) is?

 Do you know what ('foo'[3] or 'else') does?

 Do you know what ('foo' or 'else'[5]) does?

 Do you know how to launch an interactive Python session where you can
 play with such expressions until you get the hang of it? There is no
 substitute for that experience.

 Do you know that you can ask for help({}.get) or help(dict.get) or
 even help(os.environ.get) during such an interactive Python session,
 and Python (unlike Macbeth's spirits from the vasty deep) will answer?

 You dont have to be ironic. I dont have the experience you do.

That is irrelevant. For the responsibility you have taken upon yourself,
it seems you should have experience enough to figure this out. If you
can't, you just are not up for the job.

 Up until now i have this:
 
 city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
 gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or Άγνωστη Πόλη
 
 
 can this be written as:
 
 city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP',
 os.environ['REMOTE_ADDR'] )) or Άγνωστη Πόλη
 
 It makes it more easily for me to understand this way.

Experiment and find out for yourself. That is the only way you will
acquire the experience and understanding you need. Sure we could
spoon feed you the line you need, but that will only result in you
using that line without any understanding of what you are doing so
that if in a few months time something goes wrong, you again will
have no understanding of what goes on and still will have no clue
on how to handle a problem.

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Jussi Piitulainen
Νίκος writes:

  because it raises KeyError exceptions to this:
 
  city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP',
  'REMOTE_ADDR') )or �γν���η Π�λη
 
  host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP',
  'REMOTE_ADDR') )[0] or �γν���η Π�οέλε���η
 
  But that doesnt seem to also work.
  I want to use the get method because i know if it doesnt does detect a
  dictionary key item than default back what we give it.
 
 Can you please tell me why my alternative fails to work although i'am
 using the .get method to default to something?

I lost connection.

Absent HTTP_CF_CONNECTING_IP in os.environ, these reduce to

city = ( gi.time_zone_by_addr('REMOTE_ADDR') or
 �γν���η Π�λη )

host = ( socket.gethostbyaddr('REMOTE_ADDR')[0] or
 �γν���η Π�οέλε���η )

so maybe 'REMOTE_ADDR' is not meant to be the actual argument in those
calls. Earlier versions tried to get 'REMOTE_ADDR' from os.environ, I
think.

Try Nobody's piecemeal approach, maybe.
-- 
https://mail.python.org/mailman/listinfo/python-list


Gamathon By HackerRank

2013-09-26 Thread Hacker Rank
This is HackerRank's second Gamathon. 
Every month, participants get to code a bot for a new game and get to compete 
against other bots!
The winner takes the title of being the ultimate game programmer. Are you ready 
for the clash?
For more details checkout: https://www.hackerrank.com/contests
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Στις 26/9/2013 12:07 μμ, ο/η Antoon Pardon έγραψε:

Experiment and find out for yourself. That is the only way you will
acquire the experience and understanding you need. Sure we could
spoon feed you the line you need, but that will only result in you
using that line without any understanding of what you are doing so
that if in a few months time something goes wrong, you again will
have no understanding of what goes on and still will have no clue
on how to handle a problem.


I have tried code and also provided alternative code to solve my problem 
which also doesn't solve it.


So, you cannot accuse me that i'm not trying, that would be the case to 
just ask for a line without trying anything for myself, which i did twice.


Then i got entirely stuck and i had to ask to get unstuck.

Also when a solution is being provided to me i do not only want to use 
it but want to understand too, so if something similar comes up i will 
be in a position to solve it that time.

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Στις 26/9/2013 12:04 μμ, ο/η Jussi Piitulainen έγραψε:


Up until now i have this:

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
�γν���η Π�λη


can this be written as:

city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP',
os.environ['REMOTE_ADDR'] )) or �γν���η
Π�λη

It makes it more easily for me to understand this way.


That will always get os.environ['REMOTE_ADDR'] and raise exception if
it doesn't exist.

Maybe you want this:

city = gi.time_zone_by_addr(os.environ.get('HTTP_CF_CONNECTING_IP') or
 os.environ.get('REMOTE_ADDR') or
 �γν·Π�λη)


Okey that indeed doesn't hit on that lines.

Now my statements are as follows:

city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR') or Άγνωστη Πόλη )


host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )


So you include everything in aprenthseis with a get method that you know 
beforehand it will not raise an exception but continue to check the next 
operand and the next until it default to the string.


Its more clear this way: but look. this way now produced a weird error 
few line slater when 'host' is tryign to be identefied by 're'.


[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63]   File 
/home/nikos/public_html/cgi-bin/metrites.py, line 181, in module
[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63] if not 
vip and re.search( 
r'(msn|gator|amazon|yandex|reverse|who|cloudflare|fetch|barracuda|spider|google|crawl|pingdom)', 
host ) is None:
[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63]   File 
/usr/local/bin/python/lib/python3.3/re.py, line 161, in search
[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63] return 
_compile(pattern, flags).search(string)
[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63] TypeError: 
expected string or buffer


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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Maarten
On Thursday, September 26, 2013 11:31:29 AM UTC+2, Ferrous Cranus wrote:

 I have tried code and also provided alternative code to solve my problem 
 which also doesn't solve it.
 
 So, you cannot accuse me that i'm not trying, that would be the case to 
 just ask for a line without trying anything for myself, which i did twice.

I'll accuse you of being a dimwit. The questions you ask do not show you 
trying. The code that _is_ posted is not straightforward or geared towards 
readability.

 Also when a solution is being provided to me i do not only want to use 
 it but want to understand too, so if something similar comes up i will 
 be in a position to solve it that time.

If you really want to do that, then why do you insist on one-liners? Write out 
the logic with a sequence of if statements. That is easy to understand, gives 
clearer error messages. The solution by Nobody is fairly foolproof, and the 
logic is clear. What's not to like? 

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Στις 26/9/2013 11:55 πμ, ο/η Nobody έγραψε:

On Thu, 26 Sep 2013 10:26:48 +0300, Νίκος wrote:


How can i wrote the two following lines so for NOT to throw out
KeyErrors when a key is missing?

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
Άγνωστη Πόλη


tz = None
ip = os.environ.get('HTTP_CF_CONNECTING_IP')
if ip:
   tz = gi.time_zone_by_addr(ip)
if not tz:
   ip = os.environ.get('REMOTE_ADDR')
   if ip:
 tz = gi.time_zone_by_addr(ip)
if not tz:
   tz = ÎγνÏÏÏη Î Ïλη

Likewise for the hostname.



Its logic is simple and straightforward but too many lines:

host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )


this is much better in my opinion and straighforward also and more clear 
to read:


it doens't work though:

ima thinkin that the [0] is missign at the end so i cna grab the first 
item of the returnign tuple:


host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]


for the first two element if they are beign return i cna understand the 
[0] at the end but what about if the string is being return wouldnt that 
return just the 'A' of the string?


Is it correct to write it liek this?


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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Antoon Pardon
Op 26-09-13 11:31, Νίκος schreef:
 Στις 26/9/2013 12:07 μμ, ο/η Antoon Pardon έγραψε:
 Experiment and find out for yourself. That is the only way you will
 acquire the experience and understanding you need. Sure we could
 spoon feed you the line you need, but that will only result in you
 using that line without any understanding of what you are doing so
 that if in a few months time something goes wrong, you again will
 have no understanding of what goes on and still will have no clue
 on how to handle a problem.
 
 I have tried code and also provided alternative code to solve my problem
 which also doesn't solve it.

Have you tried with some kind of understanding of what you are doing or
did you just tried variations of examples you saw somewhere?

 So, you cannot accuse me that i'm not trying, that would be the case to
 just ask for a line without trying anything for myself, which i did twice.

That seems totally insufficient.

 Then i got entirely stuck and i had to ask to get unstuck.

That is your problem. You are putting all your energy in trying to
get unstuck instead of in learning so that you gain enough understanding
to get yourself unstuck.

 Also when a solution is being provided to me i do not only want to use
 it but want to understand too, so if something similar comes up i will
 be in a position to solve it that time.

You have shown no evidence that this is how you operate in reality. You
like to paint yourself like this, but in reality people still have to
explain to you that when you encouter a syntax error, you may have to
look at previous lines, while that was already explained to you a
number of times. You have a history of coming with the same kind of
problem than previous once that were solved and explained to you.

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Antoon Pardon
Op 26-09-13 11:56, Νίκος schreef:
 Στις 26/9/2013 11:55 πμ, ο/η Nobody έγραψε:
 On Thu, 26 Sep 2013 10:26:48 +0300, Νίκος wrote:

 How can i wrote the two following lines so for NOT to throw out
 KeyErrors when a key is missing?

 city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
 gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
 Άγνωστη Πόλη

 tz = None
 ip = os.environ.get('HTTP_CF_CONNECTING_IP')
 if ip:
tz = gi.time_zone_by_addr(ip)
 if not tz:
ip = os.environ.get('REMOTE_ADDR')
if ip:
  tz = gi.time_zone_by_addr(ip)
 if not tz:
tz = ÎγνÏÏÏη Î Ïλη

 Likewise for the hostname.

 
 Its logic is simple and straightforward but too many lines:
 
 host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
 os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )
 
 this is much better in my opinion and straighforward also and more clear
 to read:

No it is not and you prove that in the very next line.

 it doens't work though:

If it doesn't do what you think it should do then it is not straight
forward or clear, at least not to you.

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


Re: Referrer key missing form os.environ dictionary?

2013-09-26 Thread Robert Kern

On 2013-09-26 04:25, Chris Angelico wrote:

On Thu, Sep 26, 2013 at 11:32 AM, Terry Reedy tjre...@udel.edu wrote:

Since CGI uses stdout for the finished product, it could have used stdin for
the input.


Haven't used CGI in years, but I thought POST data came on stdin?


You could just put the whole HTTP request, headers and body, through stdin and 
just make the program parse them apart.


--
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth.
  -- Umberto Eco

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Στις 26/9/2013 1:12 μμ, ο/η Antoon Pardon έγραψε:

Op 26-09-13 11:56, Νίκος schreef:

Στις 26/9/2013 11:55 πμ, ο/η Nobody έγραψε:

On Thu, 26 Sep 2013 10:26:48 +0300, Νίκος wrote:


How can i wrote the two following lines so for NOT to throw out
KeyErrors when a key is missing?

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
Άγνωστη Πόλη


tz = None
ip = os.environ.get('HTTP_CF_CONNECTING_IP')
if ip:
tz = gi.time_zone_by_addr(ip)
if not tz:
ip = os.environ.get('REMOTE_ADDR')
if ip:
  tz = gi.time_zone_by_addr(ip)
if not tz:
tz = ÎγνÏÏÏη Î Ïλη

Likewise for the hostname.



Its logic is simple and straightforward but too many lines:

host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )

this is much better in my opinion and straighforward also and more clear
to read:


No it is not and you prove that in the very next line.


it doens't work though:


If it doesn't do what you think it should do then it is not straight
forward or clear, at least not to you.


It is far better than the ifs, even easier to read, i was just missing a 
[0] at the end.


host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]


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


Re: What minimum should a person know before saying I know Python

2013-09-26 Thread Schneider

I would say it's a little bit more:
You have to know the keywords and (basic) concepts, e.g. you really have 
to understand what it means, that everything is a class.
If you get foreign, you have to be able to understand it. And the other 
way round, given a problem, you should be able to write a working solution.


bg
Johannes

On 09/20/2013 06:23 PM, Mark Janssen wrote:

I started Python 4 months ago. Largely self-study with use of Python 
documentation, stackoverflow and google. I was thinking what is the minimum 
that I must know before I can say that I know Python?

Interesting.  I would say that you must know the keywords, how to make
a Class, how to write a loop.  That covers about 85% of it.



--
GLOBE Development GmbH
Königsberger Strasse 260
48157 MünsterGLOBE Development GmbH
Königsberger Strasse 260
48157 Münster
0251/5205 390

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Antoon Pardon
Op 26-09-13 12:18, Νίκος schreef:
 Στις 26/9/2013 1:12 μμ, ο/η Antoon Pardon έγραψε:
 Op 26-09-13 11:56, Νίκος schreef:
 Στις 26/9/2013 11:55 πμ, ο/η Nobody έγραψε:
 On Thu, 26 Sep 2013 10:26:48 +0300, Νίκος wrote:

 How can i wrote the two following lines so for NOT to throw out
 KeyErrors when a key is missing?

 city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
 gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
 Άγνωστη Πόλη

 tz = None
 ip = os.environ.get('HTTP_CF_CONNECTING_IP')
 if ip:
 tz = gi.time_zone_by_addr(ip)
 if not tz:
 ip = os.environ.get('REMOTE_ADDR')
 if ip:
   tz = gi.time_zone_by_addr(ip)
 if not tz:
 tz = ÎγνÏÏÏη Î Ïλη

 Likewise for the hostname.


 Its logic is simple and straightforward but too many lines:

 host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
 os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )

 this is much better in my opinion and straighforward also and more clear
 to read:

 No it is not and you prove that in the very next line.

 it doens't work though:

 If it doesn't do what you think it should do then it is not straight
 forward or clear, at least not to you.
 
 It is far better than the ifs, even easier to read, i was just missing a
 [0] at the end.
 
 host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
 os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]

No it is not. Your purpose was to have a line that wouldn't throw an
exception even if some environ variables were not available. That
means it shouldn't throw an exception when I execute the code. Guess
what happens:

 socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]
Traceback (most recent call last):
  File stdin, line 1, in module
socket.gaierror: [Errno -2] Name or service not known

You are just illustrating your lack of basic understaning.

-- 
Antoon Pardon

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Στις 26/9/2013 1:41 μμ, ο/η Antoon Pardon έγραψε:

Op 26-09-13 12:18, Νίκος schreef:

Στις 26/9/2013 1:12 μμ, ο/η Antoon Pardon έγραψε:

Op 26-09-13 11:56, Νίκος schreef:

Στις 26/9/2013 11:55 πμ, ο/η Nobody έγραψε:

On Thu, 26 Sep 2013 10:26:48 +0300, Νίκος wrote:


How can i wrote the two following lines so for NOT to throw out
KeyErrors when a key is missing?

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
Άγνωστη Πόλη


tz = None
ip = os.environ.get('HTTP_CF_CONNECTING_IP')
if ip:
 tz = gi.time_zone_by_addr(ip)
if not tz:
 ip = os.environ.get('REMOTE_ADDR')
 if ip:
   tz = gi.time_zone_by_addr(ip)
if not tz:
 tz = ÎγνÏÏÏη Î Ïλη

Likewise for the hostname.



Its logic is simple and straightforward but too many lines:

host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )

this is much better in my opinion and straighforward also and more clear
to read:


No it is not and you prove that in the very next line.


it doens't work though:


If it doesn't do what you think it should do then it is not straight
forward or clear, at least not to you.


It is far better than the ifs, even easier to read, i was just missing a
[0] at the end.

host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]


No it is not. Your purpose was to have a line that wouldn't throw an
exception even if some environ variables were not available. That
means it shouldn't throw an exception when I execute the code. Guess
what happens:


socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or

os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]
Traceback (most recent call last):
   File stdin, line 1, in module
socket.gaierror: [Errno -2] Name or service not known

You are just illustrating your lack of basic understaning.


I'm surepirsed, becaus eon my domain [superhost.gr] the same lien of 
code works withnout an error and it display the 'host' fileds properly.


Perhaps its failing via shell or you do not have a web server installed 
to prepare the enviromental variables or i dont know what else to hink.


But in my website this code runs at the probelm with no problem.

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread James Harris
? nikos.gr...@gmail.com wrote in message 
news:l20utg$278$1...@dont-email.me...
  26/9/2013 12:07 ??, ?/? Antoon Pardon ??:
 Experiment and find out for yourself. That is the only way you will
 acquire the experience and understanding you need. Sure we could
 spoon feed you the line you need, but that will only result in you
 using that line without any understanding of what you are doing so
 that if in a few months time something goes wrong, you again will
 have no understanding of what goes on and still will have no clue
 on how to handle a problem.

 I have tried code and also provided alternative code to solve my problem 
 which also doesn't solve it.

 So, you cannot accuse me that i'm not trying, that would be the case to 
 just ask for a line without trying anything for myself, which i did twice.

Agreed. You did post the work you had already done which seems reasonable to 
me.

For your first example, because you are worried about key errors maybe you 
could code something like the following.

  try:
city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
  except KeyError:
try:
  city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] )
except KeyError:
  city = ??? 

Does that help?

James


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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Antoon Pardon
Op 26-09-13 12:51, Νίκος schreef:
 Στις 26/9/2013 1:41 μμ, ο/η Antoon Pardon έγραψε:
 Op 26-09-13 12:18, Νίκος schreef:
 Στις 26/9/2013 1:12 μμ, ο/η Antoon Pardon έγραψε:
 Op 26-09-13 11:56, Νίκος schreef:

 It is far better than the ifs, even easier to read, i was just missing a
 [0] at the end.

 host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
 os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]

 No it is not. Your purpose was to have a line that wouldn't throw an
 exception even if some environ variables were not available. That
 means it shouldn't throw an exception when I execute the code. Guess
 what happens:

 socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
 os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]
 Traceback (most recent call last):
File stdin, line 1, in module
 socket.gaierror: [Errno -2] Name or service not known

 You are just illustrating your lack of basic understaning.
 
 I'm surepirsed, becaus eon my domain [superhost.gr] the same lien of
 code works withnout an error and it display the 'host' fileds properly.

You shoudn't be. This illustrates beautifully the difference between
understanding what one is doing and dabbling around until one stumbles
on something that currently works but that will stop working in other
circumstances.

 Perhaps its failing via shell or you do not have a web server installed
 to prepare the enviromental variables or i dont know what else to hink.

Yes it is failing via shell, but that doesn't matter. If your purpose
was to have that line working even if the environ variables were not
available, then the line should work from a shell too.

 But in my website this code runs at the probelm with no problem.

So? That just means that your website is not a good test environment
for checking whether the code works under all circumstances you want
it to work.

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Dave Angel
On 26/9/2013 06:51, Νίκος wrote:

   SNIP
 socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
 os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]
 Traceback (most recent call last):
File stdin, line 1, in module
 socket.gaierror: [Errno -2] Name or service not known

 You are just illustrating your lack of basic understaning.

 I'm surepirsed, becaus eon my domain [superhost.gr] the same lien of 
 code works withnout an error and it display the 'host' fileds properly.

 Perhaps its failing via shell or you do not have a web server installed 
 to prepare the enviromental variables or i dont know what else to hink.

 But in my website this code runs at the probelm with no problem.

In Python 3.3, but not on a web server, I get:


 import socket
 socket.gethostbyaddr(unknown)[0]
Traceback (most recent call last):
  File stdin, line 1, in module
socket.gaierror: [Errno -2] Name or service not known
 

Question, do you get the same result on your server?  (You can put the
Greek string in place of unknown of course)

If so, then the default value makes no sense;  it'll cause an exception
if it's ever used.  If you insist on sticking to expressions (no
try/catch, no if statements), then perhaps you should default to a
string representing a domain which will always exist.

-- 
DaveA


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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Στις 26/9/2013 2:07 μμ, ο/η Antoon Pardon έγραψε:

Op 26-09-13 12:51, Νίκος schreef:

Στις 26/9/2013 1:41 μμ, ο/η Antoon Pardon έγραψε:

Op 26-09-13 12:18, Νίκος schreef:

Στις 26/9/2013 1:12 μμ, ο/η Antoon Pardon έγραψε:

Op 26-09-13 11:56, Νίκος schreef:


It is far better than the ifs, even easier to read, i was just missing a
[0] at the end.

host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]


No it is not. Your purpose was to have a line that wouldn't throw an
exception even if some environ variables were not available. That
means it shouldn't throw an exception when I execute the code. Guess
what happens:


socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or

os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]
Traceback (most recent call last):
File stdin, line 1, in module
socket.gaierror: [Errno -2] Name or service not known

You are just illustrating your lack of basic understaning.


I'm surepirsed, becaus eon my domain [superhost.gr] the same lien of
code works withnout an error and it display the 'host' fileds properly.


You shoudn't be. This illustrates beautifully the difference between
understanding what one is doing and dabbling around until one stumbles
on something that currently works but that will stop working in other
circumstances.


Perhaps its failing via shell or you do not have a web server installed
to prepare the enviromental variables or i dont know what else to hink.


Yes it is failing via shell, but that doesn't matter. If your purpose
was to have that line working even if the environ variables were not
available, then the line should work from a shell too.


But in my website this code runs at the probelm with no problem.


So? That just means that your website is not a good test environment
for checking whether the code works under all circumstances you want
it to work.


Okey then please tell me, what do you see wrong in it:

socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]

the [0] at then end in case the statemnt default to the string?
please tell me in details what might go wrong with it so i cna 
understand it.

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Antoon Pardon
Op 26-09-13 13:25, Νίκος schreef:

 Okey then please tell me, what do you see wrong in it:
 
 socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
 os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]
 
 the [0] at then end in case the statemnt default to the string?
 please tell me in details what might go wrong with it so i cna
 understand it.

Startup python and try to figure it out yourself. Show us you can
really learn something.

-- 
Antoon Pardon

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


Re: Convert namedtuple to dictionary

2013-09-26 Thread Tim Chase
On 2013-09-26 01:08, Steven D'Aprano wrote:
 On Wed, 25 Sep 2013 18:41:25 -0500, Tim Chase wrote about
 namedtuple:
 
  While it uses the private member-variable _fields, you can do
 
 It's not actually private!
 
 namedtuple is documented as an exception to the rule that methods 
 starting with a single leading underscore are private. Named tuples 
 define three public methods and one data attribute. In order to
 avoid clashing with field names, they start with a single
 underscore, but they are documented as public:
 
 _make
 _asdict
 _replace
 _fields

Well dang if leading underscore suggests private/internal use
convention got tossed out the window here :)

But indeed, they are in the docs, so (to the OP), use _asdict() and
make life easy.

-tkc



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


Problem when installing Boost.Python

2013-09-26 Thread Wenlong Wang
Hi, All



I'm not sure if am asking in the right place, but since this is Python
related, I'd like to try it out.



My setup is Windows7 64-bit, Python 2.7 64bit, Boost 1.54, Visual Studio
2012 Express Version 64bit.



I am a beginner of Python, and am trying to bridge Python and C++. Thus
I download the Boost library.



I followed the instruction here:
http://www.boost.org/doc/libs/1_54_0/more/getting_started/windows.html



And successfully installed the Basic Boost.



Then I found that Boost.Python requires a separately installation,
according to another instruction here:



http://www.boost.org/doc/libs/1_54_0/libs/python/doc/building.html



I am stuck at step 3.1.4 where the instruction says to run a example:



4.  Invoke bjam. Replace the stage argument from the example
invocation from section 5 of the Getting Started Guide
http://www.boost.org/doc/libs/1_54_0/more/getting_started/index.html
with test, to build all the test targets. Also add the argument
--verbose-test to see the output generated by the tests when they are
run.

On Windows, your bjam invocation might look something like:

C:\boost_1_34_0\...\quickstart bjam toolset=msvc --verbose-test test


When I input the command line above, I have 135 unresolved external
errors! One of them is like follows:



exec.obj : error LNK2019: unresolved external symbol
__imp__PyEval_GetGlobals referenced in function

class boost::python::api::object __cdecl boost::python::eval(class
boost::python::str,class boost::python::api::object,

class
boost::python::api::object)(?eval@python@boost@@YA?AVobject@api@12@Vstr
@12@V3412@1@Z)

..\..\..\..\bin.v2\libs\python\build\msvc-11.0\debug\boost_python-vc110-
gd-1_54.dll



I know this is more C++, however, I guess someone in this mailing list
may have experienced with Boost.Python. My apologies if this mail
bothers you.



Can anyone help me to work around this?



Thank you!



Regards

Long


 
Wenlong Wang
Risk Analyst, Capquest Group Ltd

Email: ww...@capquest.co.uk
Fleet 27, Rye Close, Fleet, GU51 2QQ.
Website: http://www.capquest.co.uk/
 
This e-mail is intended solely for the addressee, is strictly confidential and 
may also be legally privileged. If you are not the addressee please do not 
read, print, re-transmit, store or act in reliance on it or any attachments. 
Instead, please email it back to the sender and then immediately permanently 
delete it. E-mail communications cannot be guaranteed to be secure or error 
free, as information could be intercepted, corrupted, amended, lost, destroyed, 
arrive late or incomplete, or contain viruses. We do not accept liability for 
any such matters or their consequences. Anyone who communicates with us by 
e-mail is taken to accept the risks in doing so. Opinions, conclusions and 
other information in this e-mail and any attachments are solely those of the 
author and do not represent those of Capquest Group Limited or any of its 
subsidiaries unless otherwise stated. Capquest Group Limited (registered number 
4936030), Capquest Debt Recovery Limited (registered number 3772278), Capquest 
Investments Limited (registered number 5245825), Capquest Asset Management 
Limited (registered number 5245829) and Capquest Mortgage Servicing Limited 
(registered number 05821008) are all limited companies registered in England 
and Wales with their registered offices at Fleet 27, Rye Close, Fleet, 
Hampshire, GU51 2QQ. Each company is a separate and independent legal entity. 
None of the companies have any liability for each other's acts or omissions. 
Group VAT registration number 844281719. Members of the Capquest group are 
regulated under the Consumer Credit Act 1974 and are members of the Credit 
Services Association. This communication is from the company named in the 
sender's details above. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

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

On 26/9/2013 06:51, Νίκος wrote:

SNIP

socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or

os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )[0]
Traceback (most recent call last):
File stdin, line 1, in module
socket.gaierror: [Errno -2] Name or service not known

You are just illustrating your lack of basic understaning.


I'm surepirsed, becaus eon my domain [superhost.gr] the same lien of
code works withnout an error and it display the 'host' fileds properly.

Perhaps its failing via shell or you do not have a web server installed
to prepare the enviromental variables or i dont know what else to hink.

But in my website this code runs at the probelm with no problem.


In Python 3.3, but not on a web server, I get:



import socket
socket.gethostbyaddr(unknown)[0]

Traceback (most recent call last):
   File stdin, line 1, in module
socket.gaierror: [Errno -2] Name or service not known




Question, do you get the same result on your server?  (You can put the
Greek string in place of unknown of course)

If so, then the default value makes no sense;  it'll cause an exception
if it's ever used.  If you insist on sticking to expressions (no
try/catch, no if statements), then perhaps you should default to a
string representing a domain which will always exist.


Yes, you are right, in my shell it fails too givign the same error 
message as yours, while on the other had in the websste is working.


Can you explain this please?
why doesnt it fail to both enviroments?

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


Re: Extracting lines from text files - script with a couple of 'side effects'

2013-09-26 Thread mstagliamonte
Hi,

Thanks for the answers! And Dave, thanks for explaining the cause of the 
problem I will keep that in mind for the future. You're right, I am doing the 
search backward, it just seemed easier for me to do it in that way. Looks like 
I need to keep practising...

Both your suggestions work, I will try and learn from them.

Have a nice day
Max
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Antoon Pardon
Op 26-09-13 14:39, Νίκος schreef:
 Yes, you are right, in my shell it fails too givign the same error
 message as yours, while on the other had in the websste is working.
 
 Can you explain this please?
 why doesnt it fail to both enviroments?

Your site and your shell are two different environments. The state
of the website environment lets your code succeed while the state
of the shell environment, doesn't.

-- 
Antoon Pardon

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Στις 26/9/2013 3:53 μμ, ο/η Antoon Pardon έγραψε:

Op 26-09-13 14:39, Νίκος schreef:

Yes, you are right, in my shell it fails too givign the same error
message as yours, while on the other had in the websste is working.

Can you explain this please?
why doesnt it fail to both enviroments?


Your site and your shell are two different environments. The state
of the website environment lets your code succeed while the state
of the shell environment, doesn't.

What modification does it need to also work in the shell environmentthe 
[0] at the end is what complicates things.

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


Re: Newline interpretation issue with MIMEApplication with binary data, Python 3.3.2

2013-09-26 Thread Neil Cerutti
On 2013-09-26, Chris Angelico ros...@gmail.com wrote:
 On Thu, Sep 26, 2013 at 2:23 PM, Nils Bunger nilsbun...@gmail.com wrote:
 Yes, it's email.mime.MIMEApplication. I've pasted a snippet
 with the imports below.

 I'm trying to use this to build a multi-part MIME message,
 with this as one part.

 I really can't figure out any way to attach a binary part like
 this to a multi-part MIME message without the encoding
 issue... any help would be greatly appreciate!

 I partly responded just to ping your thread, as I'm not
 particularly familiar with the email.mime module. But a glance
 at the docs suggests that MIMEApplication is a subclass of
 MIMENonMultipart, so might it be a problem to use that for
 multipart??

 It's designed to handle text, so you may want to use an encoder
 (like the default base64 one) rather than trying to push binary
 data through it.

 Random ideas, hopefully someone who actually knows the module
 can respond.

I got interested in it since I have never used any of the
modules. So I played with it enough to discover that the part of
the code above that converts the \r to \n is the flatten call.

I got to here and RFC 2049 and gave up.

   The following guidelines may be useful to anyone devising a data
   format (media type) that is supposed to survive the widest range of
   networking technologies and known broken MTAs unscathed.  Note that
   anything encoded in the base64 encoding will satisfy these rules, but
   that some well-known mechanisms, notably the UNIX uuencode facility,
   will not.  Note also that anything encoded in the Quoted-Printable
   encoding will survive most gateways intact, but possibly not some
   gateways to systems that use the EBCDIC character set.

(1)   Under some circumstances the encoding used for data may
  change as part of normal gateway or user agent
  operation.  In particular, conversion from base64 to
  quoted-printable and vice versa may be necessary.  This
  may result in the confusion of CRLF sequences with line
  breaks in text bodies.  As such, the persistence of
  CRLF as something other than a line break must not be
  relied on.

(2)   Many systems may elect to represent and store text data
  using local newline conventions.  Local newline
  conventions may not match the RFC822 CRLF convention --
  systems are known that use plain CR, plain LF, CRLF, or
  counted records.  The result is that isolated CR and LF
  characters are not well tolerated in general; they may
  be lost or converted to delimiters on some systems, and
  hence must not be relied on.

So putting a raw CR in a binary chunk maybe be intolerable, and
you need to use a different encoder. But I'm out of my element.

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


Re: Referrer key missing form os.environ dictionary?

2013-09-26 Thread Grant Edwards
On 2013-09-26, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:
 On Wed, 25 Sep 2013 15:18:41 +, Grant Edwards wrote:

 The Referer is not an environment variable.
 
 It is when you're writing a CGI app.
 
 How would your shell know what URL you were just browsing?
 
 Because the HTTP server sets those environment variables before invoking
 the CGI app.

 I stand corrected.

 That's a pretty shitty design though, isn't it?

On a Unix system when you invoke a program, you pass it four things:

 1) A dictionary where keys/values are both strings [enviornment variables]
 2) A list of strings [command line args]
 3) A set of open file descriptors. 
 4) The current working directory. 

You can provide input values to the program through any of these.

For interactive programs, 2 and 3 are the most convenient. For
programs intended to be invoked non-interactively via another program
the first option can be very elegent and versatile -- but it does make
use of the program interactively rather awkward.  Semantically,
passing values to a program via environment variables is very similar
to keyword arguments to a Python function, while command line
arguments are like positional arguments to a Python function.
 
 Communicating via environment variables. What is this, 1998? :-)

 Mind you, I'm not sure what other alternatives exist.

Command line arguments, open file descriptors, or files in the CWD. 
All are more difficult to use programmatically than environment
variables.

-- 
Grant Edwards   grant.b.edwardsYow! I guess you guys got
  at   BIG MUSCLES from doing too
  gmail.commuch STUDYING!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Referrer key missing form os.environ dictionary?

2013-09-26 Thread Grant Edwards
On 2013-09-26, Chris Angelico ros...@gmail.com wrote:
 On Thu, Sep 26, 2013 at 11:32 AM, Terry Reedy tjre...@udel.edu wrote:
 Since CGI uses stdout for the finished product, it could have used stdin for
 the input.

 Haven't used CGI in years, but I thought POST data came on stdin?

Yes.  The user data is read via stdin.  The stuff passed via the
environemnt dictionary is meta-data (stuff from the HTTP request
headers, and stuff synthesized by the HTTP server).

-- 
Grant Edwards   grant.b.edwardsYow! !  Now I understand
  at   advanced MICROBIOLOGY and
  gmail.comth' new TAX REFORM laws!!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Understanding how is a function evaluated using recursion

2013-09-26 Thread Neil Cerutti
On 2013-09-25, Arturo B a7xrturo...@gmail.com wrote:
 Hi, I'm doing Python exercises and I need to write a function
 to flat nested lists as this one: 

 [[1,2,3],4,5,[6,[7,8]]]

 To the result:

 [1,2,3,4,5,6,7,8]

 So I searched for example code and I found this one that uses
 recursion (that I don't understand):

 def flatten(l):
 ret = []
 for i in l:
 if isinstance(i, list) or isinstance(i, tuple):
 ret.extend(flatten(i)) #How is flatten(i) evaluated?
 else:
 ret.append(i)
 return ret

 So I know what recursion is, but I don't know how is 

flatten(i)
  
 evaluated, what value does it returns?

It only *looks* like magic, as others have explained.

I keep a file callled bikeshed.py for these occasions. The
flatten function has been to the bike shed a lot [1]. Here's a
non-recursive version of flatten for comparison:

from collections import Sequence

def flatten(seq):
stack = []
i = 0
result = []
while True:
if i = len(seq):
if stack:
seq, i = stack.pop()
else:
return result
elif isinstance(seq[i], Sequence):
stack.append((seq, i+1)) # Store the continue point
seq = seq[i]
i = 0
else:
result.append(seq[i])
i += 1

When this version encounters a nested list, it keeps a stack of
sequences and indices to continue on with after processing the
nested list. The code at the start of the while loop, when a
sequence is exhausted, pops the continue points fromt he stack,
returning if the stack is empty.

It's simpler and cleaner to call flatten on itself, as in the
recursive version, because the stack frames do all the
bookkeeping for you. CPython has a limited number of stack frames
though, so the version above might be preferable for certain
levels of nesting.

[1] http://en.wiktionary.org/wiki/bikeshed

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Dave Angel
On 26/9/2013 09:34, Νίκος wrote:

 Στις 26/9/2013 3:53 μμ, ο/η Antoon Pardon έγραψε:
 Op 26-09-13 14:39, Νίκος schreef:
 Yes, you are right, in my shell it fails too givign the same error
 message as yours, while on the other had in the websste is working.

 Can you explain this please?
 why doesnt it fail to both enviroments?

 Your site and your shell are two different environments. The state
 of the website environment lets your code succeed while the state
 of the shell environment, doesn't.

 What modification does it need to also work in the shell environmentthe 
 [0] at the end is what complicates things.

Not at all.  What complicates/confuses things is trying to do too much
in one line.

The line is trying to solve two separate things.  You might now
understand the first of them; the fetching of multiple environment
variables that may or may not exist.

The second thing is the function call to gethostbyname().  If fed
invalid data, this call will throw an exception.  So you need to either
put it in a try/catch or somehow pretend that you can know what
constitutes invalid data.  One example of invalid data is your default
string.  Another might be if one of those environment variables exists,
but isn't reasonable.  And a third might be if the domain server
temporarily refuses to recognize a valid hostname.

I'd say you need to put the gethostbyname() in a try/catch, and then
supply some reasonable bogus string for host.  Only you will know what
to use there;  depending on what you're going to use it for.


ipval = (os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR', Please throw a gaierror exception)
try:
 host = socket.gethostbyaddr(ipval) [0]
except socket.gaierror as exc;
 host = Unknown host

I don't know the gethostbyaddr(), so i don't know if there are other
exceptions you should also try to catch.

-- 
DaveA


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


Re: Understanding how is a function evaluated using recursion

2013-09-26 Thread Neil Cerutti
On 2013-09-26, Neil Cerutti ne...@norwich.edu wrote:
 def flatten(seq):

 [1] http://en.wiktionary.org/wiki/bikeshed

In that spirit, it occurs to me that given current Python
nomenclature, 'flattened' would be a better name.

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Denis McMahon
On Thu, 26 Sep 2013 12:56:19 +0300, Νίκος wrote:

 host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or
 os.environ.get('REMOTE_ADDR') or  Άγνωστη Προέλευση )

Perhaps you need something that looks more like:

some_value = some_function_of( some_value ) or some_function_of
( some_value )
if some_value:
host = some_function_of( some_value )
else:
host = some_value

or even:

try:
host = some_function_of( some_function_of( some_value ) or 
some_function_of( some_value ) )
except some_error_type [ or some_error_type  ]:
host = some_value

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposal: [... for ... while cond(x)]

2013-09-26 Thread Nick Mellor
Hi all,

It might be a risk that we create some hairy or subtle semantics, but perhaps 
this idea has legs. The 'pythonic' syntax is very attractive.

# skip the silence at the start
data = (amp for amp in signal from abs(amp)  0.05)

# drop files that are less than 600 bytes
# (already sorted into ascending order of size)
files = (file for file in filelist from os.stat(file).st_size  600)

# Analyse bunch of log lines up to a certain timestamp
cutoff = datetime.datetime.now() - datetime.timedelta(hours=1)
checklogs = (entry for entry in log while entry.timestamp = cutoff)

In use it feels along the same lines as else: clauses in try/finally and for 
loops, both of which I like-- it seems natural in Python, keeps the meaning 
clear and consistent, avoids complicated, iterative, inexpressive code.

Terry Reedy wrote:
 You're right. The syntax is ambiguous. I agree it's not a good idea,
now. :)

When while, if and from are made mutually exclusive, not mixed, on a 
comprehension clause, it looks to me like the ambiguity disappears:

 x for x in xs while cond(x) if blah(x)

not legal Python-- if and while are mutually exclusive on the same 
comprehension clause. On the one hand it's like writing x for x in xs if f(x)  
10 if g(x)  100, the illegality of which no-one is complaining about. Just 
write:

x for x in xs while cond(x) and cond2(x)

or

x for x in xs if cond(x) and cond2(x)

If there is a need to filter using if as well as while, then use two 
comprehensions:

intermediate = [x for x in xs while cond(x)]
final = [x for x in intermediate if cond2(x)]

or the same comprehension applied to itself:

y for y in x for x in xs if cond(x) while cond2(y)

The if and the while operation *should* be kept separate-- they're different 
stages of processing the data.

  x*y for x in xs while cond(x) for y in ys if cond(y)

legal Python-- the 'if cond(y)' unambiguously refers to the inner 
comprehension. 


 x for x in xs if blah(x) while cond(x)

not legal Python-- if and while are mutually exclusive in the same 
comprehension clause.

The objection to while as an abbreviation:

 e(x) for x in xs if cond(x)
 
 is an abbreviation of
 
 for x in xs:
   if cond(x)
 yield e(x)
 
 and similarly with more for and if clauses,
 whereas the analogous expansion of your proposal
 
 for x in xs:
   while cond(x):
  yield e(x)
 
 is an infinite loop and not at all what you mean.

That's because while is being used here with a different semantic to the while 
loop. It actually means:

for x in xs:
if not cond(x):
break
yield e(x)

and for 'from':


emit = False
for x in xs:
if not emit:
if cond(x):
emit = True
else:
yield e(x)


Cheers,

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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Denis McMahon
On Thu, 26 Sep 2013 14:25:55 +0300, Νίκος wrote:

 Okey then please tell me, what do you see wrong in it:
 
 socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP')

If os.environ.get('HTTP_CF_CONNECTING_IP') is false

 or os.environ.get('REMOTE_ADDR') 

and os.environ.get('REMOTE_ADDR') is false

 or  Άγνωστη Προέλευση )[0]

The you try and get the host name from the ip address Άγνωστη 
Προέλευση, but Άγνωστη Προέλευση is not an ip address, so you get an 
error.

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Denis McMahon
On Thu, 26 Sep 2013 12:56:19 +0300, Νίκος wrote:

 Its logic is simple and straightforward but too many lines:

There is no too many lines.

 this is much better in my opinion and straighforward also and more clear
 to read ... it doens't work though:

This is just a stupid attitude for a coder to have.

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposal: [... for ... while cond(x)]

2013-09-26 Thread Nick Mellor
On Friday, 27 September 2013 00:39:30 UTC+10, Nick Mellor  wrote:

[snip]
 for x in xs:
 if not emit:
 if cond(x):
 emit = True
 else:
 yield e(x)
 

Oops!

for x in xs:
   if not emit:
   if cond(x):
   emit = True
   yield e(x)
   else:
   yield e(x)
-- 
https://mail.python.org/mailman/listinfo/python-list


Download all youtube favorites with youtube-dl script

2013-09-26 Thread Bill

I have been using the script youtube-dl http://rg3.github.io/youtube-dl/

And I was wondering if there is a way to download all of a user's 
favorites or uploads.


The script has a functionality to download all videos in a txt file. So 
if there is a way using the youtube API or JSON (none of which I am 
familiar with) to bring up a list of all these videos then it'd be a 
simple case putting these urls into a file.


The problem is youtube displays favorites or user uploads in pages or 
infinite scroll. So it is difficult to access them by the BeautifulSoup 
module.



What do you suggest?
--
https://mail.python.org/mailman/listinfo/python-list


Re: Referrer key missing form os.environ dictionary?

2013-09-26 Thread Chris Angelico
On Fri, Sep 27, 2013 at 12:11 AM, Grant Edwards invalid@invalid.invalid wrote:
 On a Unix system when you invoke a program, you pass it four things:

  1) A dictionary where keys/values are both strings [enviornment variables]
  2) A list of strings [command line args]
  3) A set of open file descriptors.
  4) The current working directory.

 You can provide input values to the program through any of these.

 For interactive programs, 2 and 3 are the most convenient.

Hrm, not sure about #3 for interactive programs, unless you
specifically mean the three standard streams. With most Unix shells,
you should be able to set environment variables:

PGUSER=fred PGPASSWORD=secret psql

Not as convenient as #2, but far easier than passing an open file
descriptor. Of course, passing file descriptors around is pretty easy
programmatically, but when you say interactive I assume you're
talking also about an interactive shell.

GUI interactions of course follow their own rules completely. In most
systems, it's really easy to invoke an application with one argument,
a file name; it's generally much harder to customize the arguments at
the keyboard. OS/2 had a facility for doing that. You just put square
brackets into the configured args and it'd turn it into a prompt:

--foo=bar --mode=[Choose mode, 1-3:] %*

You'd get a nice little popup with the prompt you specified, and
whatever you type gets put into the args. Haven't seen that in any
other system - at least, not as conveniently.

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


Re: Download all youtube favorites with youtube-dl script

2013-09-26 Thread Joel Goldstick
On Thu, Sep 26, 2013 at 11:13 AM, Bill b...@bill.com wrote:

 I have been using the script youtube-dl 
 http://rg3.github.io/youtube-**dl/http://rg3.github.io/youtube-dl/

 And I was wondering if there is a way to download all of a user's
 favorites or uploads.

 The script has a functionality to download all videos in a txt file. So if
 there is a way using the youtube API or JSON (none of which I am familiar
 with) to bring up a list of all these videos then it'd be a simple case
 putting these urls into a file.

 The problem is youtube displays favorites or user uploads in pages or
 infinite scroll. So it is difficult to access them by the BeautifulSoup
 module.


 What do you suggest?
 --
 https://mail.python.org/**mailman/listinfo/python-listhttps://mail.python.org/mailman/listinfo/python-list


Have you looked at the youtube API?  That would be better than screen
scraping.  https://developers.google.com/youtube/getting_started


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


Upload Windows binaries on pypi from Linux

2013-09-26 Thread Giampaolo Rodola'
I have a Python extension module (psutil) written in C which I compile for
different Python versions (from 2.4 to 3.3) and want to upload them on pypi.

The Windows machine is not connected to internet  so after I generate the
Windows binaries (via setup.py bdist_wininst) I move the .exe files from
Windows to Linux. From there I want to upload all the *.exe files on pypi.

Apparently I can automatically do this only from Windows via setup.py
upload bdist_wininst but as I said I have no internet connection on that
box.

Is there a way to tell setup.py to upload those *.exe file on pypi from
Linux or some tool which can do that via HTTP POST or something?

Thanks in advance,

--- Giampaolo

https://code.google.com/p/pyftpdlib/
https://code.google.com/p/psutil/
https://code.google.com/p/pysendfile/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newline interpretation issue with MIMEApplication with binary data, Python 3.3.2

2013-09-26 Thread Nils Bunger
Hi Neil, 

Thanks for looking at this.  

I'm trying to create a multipart MIME for an HTTP POST request, not an email.  
This is for a third-party API that requires a multipart POST with a binary 
file, so I don't have the option to just use a different encoding.

Multipart HTTP is standardized in HTTP 1.0 and supports binary parts. Also, no 
one will re-interpret contents of HTTP on the wire, as binary is quite normal 
in HTTP.

The issue seems to be some parts of the python MIME encoder still assume it's 
for email only, where everything would be b64 encoded.

Maybe I have to roll my own to create a multipart msg with a binary file? I was 
hoping to avoid that.

Nils

ps. You probably know this, but in case anyone else reads this thread, HTTP 
requires all headers to have CRLF, not native line endings. The python MIME 
modules can do that properly as of python 3.2 (fixed as of this bug 
http://hg.python.org/cpython/rev/ebf6741a8d6e/)



 
 I got interested in it since I have never used any of the
 
 modules. So I played with it enough to discover that the part of
 
 the code above that converts the \r to \n is the flatten call.
 
 
 
 I got to here and RFC 2049 and gave up.
 
 
 
The following guidelines may be useful to anyone devising a data
 
format (media type) that is supposed to survive the widest range of
 
networking technologies and known broken MTAs unscathed.  Note that
 
anything encoded in the base64 encoding will satisfy these rules, but
 
that some well-known mechanisms, notably the UNIX uuencode facility,
 
will not.  Note also that anything encoded in the Quoted-Printable
 
encoding will survive most gateways intact, but possibly not some
 
gateways to systems that use the EBCDIC character set.
 
 
 
 (1)   Under some circumstances the encoding used for data may
 
   change as part of normal gateway or user agent
 
   operation.  In particular, conversion from base64 to
 
   quoted-printable and vice versa may be necessary.  This
 
   may result in the confusion of CRLF sequences with line
 
   breaks in text bodies.  As such, the persistence of
 
   CRLF as something other than a line break must not be
 
   relied on.
 
 
 
 (2)   Many systems may elect to represent and store text data
 
   using local newline conventions.  Local newline
 
   conventions may not match the RFC822 CRLF convention --
 
   systems are known that use plain CR, plain LF, CRLF, or
 
   counted records.  The result is that isolated CR and LF
 
   characters are not well tolerated in general; they may
 
   be lost or converted to delimiters on some systems, and
 
   hence must not be relied on.
 
 
 
 So putting a raw CR in a binary chunk maybe be intolerable, and
 
 you need to use a different encoder. But I'm out of my element.
 
 
 
 -- 
 
 Neil Cerutti
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Download all youtube favorites with youtube-dl script

2013-09-26 Thread Philipp Hagemeister
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA512

Hi Bill,

the best way to ask for features or file bugs on youtube-dl is asking
us at http://github.com/rg3/youtube-dl/issues . Luckily, I this list
from time to time ;)

Simply use

youtube-dl --username u...@gmail.com --password secret :ytfav

Best,

Philipp

On 09/26/2013 05:13 PM, Bill wrote:
 I have been using the script youtube-dl
 http://rg3.github.io/youtube-dl/
 
 And I was wondering if there is a way to download all of a user's 
 favorites or uploads.
 
 The script has a functionality to download all videos in a txt
 file. So if there is a way using the youtube API or JSON (none of
 which I am familiar with) to bring up a list of all these videos
 then it'd be a simple case putting these urls into a file.
 
 The problem is youtube displays favorites or user uploads in pages
 or infinite scroll. So it is difficult to access them by the
 BeautifulSoup module.
 
 
 What do you suggest?

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.14 (GNU/Linux)

iQIcBAEBCgAGBQJSRFQmAAoJENtLVMukgmoYaz0P/ifoPqcx4dpShXyigwGxzhsa
ht8sjIP8n3bNJ+J7Jvx0uM6Sv/Hu/U1CC09G1pT9nquHDYYvmko+MoBkXbc8JmlC
R0qPhX0I2xGH9G1EL+83J4zzte3K7p5ErLY1V1S9cWuBKot16eKtrxhIgWxTMfE1
l93az4Uz4YLzUPYEXSO7pNX9cvxyrEjsqSJE3Jftn5ZqbUO46M+7gMKG2g46C/W4
IQtg2v9cHE3xKV5c+wCFjjIHtOGg+leOTMFdiD6oIa/uNfV/3tzpiyQ1e2QgyrvU
UK4zz3TxqxWswDTrxRdc7fFNAGoNSrxU2hwyvjc/CGehORv7ktBjJJbtt3PfvHsi
nov+OEIToDFTO1nCzQ39qQP0Ibl0LpNbvJGWyJqubFsZd0uiU1EwONXFchNwHeil
Yce4DT8Tzf4zv6y3YNJCz1RCM1G308vQav539w6D7vfIUc5F28gnWKkd5NIcCpyu
URVWp0HxV7d+kCekbxhnd+Ah/XvsrHhkI3cxOHvc1QjGwToRWawJQT0LD72E6PqW
MmnOUTZhrxebyAx1HEt45l19fuW/TfwCWWzAtRscr8uFLFf0/Hwm41tSo8FjxqK2
OVZAckmVYIKP0F+u6hcSg/INl6rs6R+Co4/S8aFdKh2N16wmwQ7hevoTIpXIFwwK
hkpwAUdlQxIWzSe8uJMV
=nRz7
-END PGP SIGNATURE-
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newline interpretation issue with MIMEApplication with binary data, Python 3.3.2

2013-09-26 Thread Nils Bunger
Hi all, 

I was able to workaround this problem by encoding a unique 'marker' in the 
binary part, then replacing the marker with the actual binary content after 
generating the MIME message. 

See my answer on Stack Overflow http://stackoverflow.com/a/19033750/526098 for 
the code.

Thanks, your suggestions helped me think of this.

Nils

On Wednesday, September 25, 2013 9:38:17 AM UTC-7, Nils Bunger wrote:
 Hi, 
 
 
 
 I'm having trouble encoding a MIME message with a binary file.  Newline 
 characters are being interpreted even though the content is supposed to be 
 binary. This is using Python 3.3.2
 
 
 
 Small test case:
 
 
 
 app = MIMEApplication(b'Q\x0dQ', _encoder=encode_noop)
 
 b = io.BytesIO()
 
 g = BytesGenerator(b)
 
 g.flatten(app)
 
 for i in b.getvalue()[-3:]:
 
 print (%02x  % i, end=)
 
 print ()
 
 
 
 This prints 51 0a 51,  meaning the 0x0d character got reinterpreted as a 
 newline. 
 
 
 
 I've tried setting an email policy of HTTP policy, but that goes even 
 further, converting \r to \r\n
 
 
 
 This is for HTTP transport, so binary encoding is normal.
 
 
 
 Any thoughts how I can do this properly?

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


Re: Convert namedtuple to dictionary

2013-09-26 Thread Virendra Tripathi
Thx Tim. Your solution works. I am, as you can probably tell, a newbie at 
Python. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

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

On 26/9/2013 09:34, Νίκος wrote:


Στις 26/9/2013 3:53 μμ, ο/η Antoon Pardon έγραψε:

Op 26-09-13 14:39, Νίκος schreef:

Yes, you are right, in my shell it fails too givign the same error
message as yours, while on the other had in the websste is working.

Can you explain this please?
why doesnt it fail to both enviroments?


Your site and your shell are two different environments. The state
of the website environment lets your code succeed while the state
of the shell environment, doesn't.


What modification does it need to also work in the shell environmentthe
[0] at the end is what complicates things.


Not at all.  What complicates/confuses things is trying to do too much
in one line.

The line is trying to solve two separate things.  You might now
understand the first of them; the fetching of multiple environment
variables that may or may not exist.

The second thing is the function call to gethostbyname().  If fed
invalid data, this call will throw an exception.  So you need to either
put it in a try/catch or somehow pretend that you can know what
constitutes invalid data.  One example of invalid data is your default
string.  Another might be if one of those environment variables exists,
but isn't reasonable.  And a third might be if the domain server
temporarily refuses to recognize a valid hostname.

I'd say you need to put the gethostbyname() in a try/catch, and then
supply some reasonable bogus string for host.  Only you will know what
to use there;  depending on what you're going to use it for.


ipval = (os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR', Please throw a gaierror exception)
try:
  host = socket.gethostbyaddr(ipval) [0]
except socket.gaierror as exc;
  host = Unknown host

I don't know the gethostbyaddr(), so i don't know if there are other
exceptions you should also try to catch.



Indeed this logic you follow is hethe best i have seen so far.
But actually i have 2 variables that relay on a proper ip address.

ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR', UnKnown Origin) )

try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
city = gi.time_zone_by_addr( ipval )
host = socket.gethostbyaddr( ipval ) [0]
except socket.gaierror as e:
city = host = UnKnown Origin

But then what if in case of an error i needed different string set to be 
assigned on city and host respectively?


As i ahve it know in case of n address related error i get the smae 
sting for both of them.


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


Re: Convert namedtuple to dictionary

2013-09-26 Thread Virendra Tripathi
Hi MRAB,

Thanks. Learning a lot thanks to you guys.

I should have mentioned that the data is dynamic, in the sense that 'brucelee' 
could be 'jackiechan' , the number of named tuples - i.e., 'brucelee(…)', could 
change as well, the next time data is pulled in. The format of the data would 
however remain the same.

I am using @Tim Chase's solution for now. 

Your solution will also work but I think I'll need to create a fn to pull out 
the  n  values as in:  n = brucelee(x=123, y=321)

Thanks,

Virendra



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


Re: Convert namedtuple to dictionary

2013-09-26 Thread Virendra Tripathi
Hi Ned,

Thanks. Wouldn't I have to first create a function to pull out the 'dictdict' 
from the data? I assume 'dictdict' refers to the 'brucelee' named tuple in the 
example. That's where I was getting stuck-trying to pull out the named tuples 
from the dict and operate on it.

Thanks,

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


Re: Convert namedtuple to dictionary

2013-09-26 Thread Tim Chase
On 2013-09-26 16:42, Virendra Tripathi wrote:
 Thx Tim. Your solution works.

After Steven's reply, I recommend

  dict((k,v._asdict()) for k,v in d.iteritems())

which simplifies matters.

-tkc


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


Re: Convert namedtuple to dictionary

2013-09-26 Thread Ned Batchelder

On 9/26/13 1:17 PM, Virendra Tripathi wrote:

Hi Ned,

Thanks. Wouldn't I have to first create a function to pull out the 'dictdict' 
from the data? I assume 'dictdict' refers to the 'brucelee' named tuple in the 
example. That's where I was getting stuck-trying to pull out the named tuples 
from the dict and operate on it.

I assumed you already had the data you showed.  This is tupledict:

{'a': brucelee(x=123, y=321), 'b': brucelee('x'=123, 'y'=321)

Given that, you create dictdict with the line I showed:

dictdict = { k: nt._asdict() for k, nt in tupledict }


Thanks,

Virendra


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


Re: Download all youtube favorites with youtube-dl script

2013-09-26 Thread Bill

Joel Goldstick wrote:




On Thu, Sep 26, 2013 at 11:13 AM, Bill b...@bill.com
mailto:b...@bill.com wrote:

I have been using the script youtube-dl
http://rg3.github.io/youtube-__dl/ http://rg3.github.io/youtube-dl/

And I was wondering if there is a way to download all of a user's
favorites or uploads.

The script has a functionality to download all videos in a txt file.
So if there is a way using the youtube API or JSON (none of which I
am familiar with) to bring up a list of all these videos then it'd
be a simple case putting these urls into a file.

The problem is youtube displays favorites or user uploads in pages
or infinite scroll. So it is difficult to access them by the
BeautifulSoup module.


What do you suggest?
--
https://mail.python.org/__mailman/listinfo/python-list
https://mail.python.org/mailman/listinfo/python-list


Have you looked at the youtube API?  That would be better than screen
scraping. https://developers.google.com/youtube/getting_started


--
Joel Goldstick
http://joelgoldstick.com


I remember looking at this a while ago. These are the relevant sections 
of the API:


https://developers.google.com/youtube/2.0/developers_guide_protocol?hl=en#User_Uploaded_Videos

https://developers.google.com/youtube/2.0/developers_guide_protocol?hl=en#Favorite_Videos

The problem with the API is that with both of these, it only brings up 
25 videos e.g. https://gdata.youtube.com/feeds/api/users/scooby1961/uploads


Unless I'm missing something? There must be a way to bring up all of 
them on one page right? Or at least cycle through the rest of them.


Same problem with favorties: 
https://gdata.youtube.com/feeds/api/users/adambuxton/favorites/

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


Re: Download all youtube favorites with youtube-dl script

2013-09-26 Thread MRAB

On 26/09/2013 19:14, Bill wrote:

Joel Goldstick wrote:




On Thu, Sep 26, 2013 at 11:13 AM, Bill b...@bill.com
mailto:b...@bill.com wrote:

I have been using the script youtube-dl
http://rg3.github.io/youtube-__dl/ http://rg3.github.io/youtube-dl/

And I was wondering if there is a way to download all of a user's
favorites or uploads.

The script has a functionality to download all videos in a txt file.
So if there is a way using the youtube API or JSON (none of which I
am familiar with) to bring up a list of all these videos then it'd
be a simple case putting these urls into a file.

The problem is youtube displays favorites or user uploads in pages
or infinite scroll. So it is difficult to access them by the
BeautifulSoup module.


What do you suggest?
--
https://mail.python.org/__mailman/listinfo/python-list
https://mail.python.org/mailman/listinfo/python-list


Have you looked at the youtube API?  That would be better than screen
scraping. https://developers.google.com/youtube/getting_started


--
Joel Goldstick
http://joelgoldstick.com


I remember looking at this a while ago. These are the relevant sections
of the API:

https://developers.google.com/youtube/2.0/developers_guide_protocol?hl=en#User_Uploaded_Videos

https://developers.google.com/youtube/2.0/developers_guide_protocol?hl=en#Favorite_Videos

The problem with the API is that with both of these, it only brings up
25 videos e.g. https://gdata.youtube.com/feeds/api/users/scooby1961/uploads

Unless I'm missing something? There must be a way to bring up all of
them on one page right? Or at least cycle through the rest of them.


You _are_ missing something. That section of #User_Uploaded_Videos
mentions the parameters start-index and max-results.


Same problem with favorties:
https://gdata.youtube.com/feeds/api/users/adambuxton/favorites/



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


Re: Newline interpretation issue with MIMEApplication with binary data, Python 3.3.2

2013-09-26 Thread Piet van Oostrum
Nils Bunger nilsbun...@gmail.com writes:

 Hi Neil, 

 Thanks for looking at this.  

 I'm trying to create a multipart MIME for an HTTP POST request, not an
 email. This is for a third-party API that requires a multipart POST
 with a binary file, so I don't have the option to just use a different
 encoding.

 Multipart HTTP is standardized in HTTP 1.0 and supports binary parts.
 Also, no one will re-interpret contents of HTTP on the wire, as binary
 is quite normal in HTTP.

 The issue seems to be some parts of the python MIME encoder still
 assume it's for email only, where everything would be b64 encoded.

 Maybe I have to roll my own to create a multipart msg with a binary
 file? I was hoping to avoid that.

The email MIME stuff is not really adapted for HTTP. I would advise to
use the Requests package (http://docs.python-requests.org/en/latest/) or
the Uploading Files part from Doug Hellmann's page
(http://doughellmann.com/2009/07/pymotw-urllib2-library-for-opening-urls.html).
This is for Python2; I can send you a Python3 version if you want.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Convert namedtuple to dictionary

2013-09-26 Thread Ned Batchelder

On 9/26/13 1:42 PM, Ned Batchelder wrote:

On 9/26/13 1:17 PM, Virendra Tripathi wrote:

Hi Ned,

Thanks. Wouldn't I have to first create a function to pull out the 
'dictdict' from the data? I assume 'dictdict' refers to the 
'brucelee' named tuple in the example. That's where I was getting 
stuck-trying to pull out the named tuples from the dict and operate 
on it.

I assumed you already had the data you showed.  This is tupledict:

{'a': brucelee(x=123, y=321), 'b': brucelee('x'=123, 'y'=321)

Given that, you create dictdict with the line I showed:

dictdict = { k: nt._asdict() for k, nt in tupledict }



Oops:

dictdict = { k: nt._asdict() for k, nt in tupledict.iteritems() }

(sorry!)

--Ned.

Thanks,

Virendra




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


Re: Convert namedtuple to dictionary

2013-09-26 Thread Virendra Tripathi
Thanks guys! BTW, please ignore the follow-up question there. I had written a 
script earlier which had a dict like:

{'a':[x=10, y=23,..], 'b': [x=10, y=23,…],…..}

I was thinking about that and therefore the error. Apologies.
-- 
https://mail.python.org/mailman/listinfo/python-list


work develope matrix of difusive concentration

2013-09-26 Thread D.YAN ESCOLAR RAMBAL
Good morning all

thanks for your help. Now I`ve a question, if I need create a matrix,
method of thomas, ghost nodes, etc.. for developed one aplicative for the
ecuation of difusion in 1d, 2d, 3d, 4d. It`s better use phyton 3.3 or 2.7?,
if the answer is phyton 3.3 that interpreter is similar to aptana o better,
for start, and which, libraries are necesary for this,  I think I need
numpy, numba, math, mathexpretion, matiplot, but  I don`t know which is the
library similar to matlab.

All best for you, nice day or nigth and nice week
-- 

*Gracias, cordialmente, *

*
*

*
*

*ING. YANETH ESCOLAR RAMBAL*

*HAFI Engineering  Consulting GmbH*

*CEL: + 57 3166257245*

*TEL: + 57 (1) 6508308*

*TEL: ++43 5522 77924 0*

*Fax: ++43 5522 74938*

*Dir : Cll 72f no. 113-21 Torre 5 - 604  Bogotá Colombia*

*   Muehletorplatz 4-6, A-6800 Feldkirch, Austria*

   *www.hafi.cc/en/environ_en.htm*
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Denis McMahon
On Thu, 26 Sep 2013 19:58:02 +0300, Νίκος wrote:

 except socket.gaierror as e:
   city = host = UnKnown Origin
 
 But then what if in case of an error i needed different string set to be
 assigned on city and host respectively?

Oh FFS

Are you serious when you ask this?

Simply change:

except socket.gaierror as e:
city = host = UnKnown Origin

To:

except socket.gaierror as e:
city = Unknown City
host = Unknown Host

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Dave Angel
On 26/9/2013 12:58, Νίκος wrote:

   snip

 But actually i have 2 variables that relay on a proper ip address.

 ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or 
 os.environ.get('REMOTE_ADDR', UnKnown Origin) )
 try:
   gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
   city = gi.time_zone_by_addr( ipval )
   host = socket.gethostbyaddr( ipval ) [0]
 except socket.gaierror as e:
   city = host = UnKnown Origin

First concept:  A try/except should wrap one conceptual calculation.  In
this case, you have three.  So if the host lookup fails, you'll
overwrite the city you already presumably figured out.

You also don't do anything about an exception that might occur
calculating gi.  If one isn't possible, or if it's unrelated to the
other two, then that line should come OUT of the try-block.

What is city?  The function name implies it's a time zone, which is
presumably a float, -12  x  +12.  If this is the case, then a string
is a lousy default value.


 But then what if in case of an error i needed different string set to be 
 assigned on city and host respectively?

Nothing limits the except clause to a single line.


 As i ahve it know in case of n address related error i get the smae 
 sting for both of them.

No idea what this is trying to say.

-- 
DaveA


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


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Νίκος

Στις 26/9/2013 11:16 μμ, ο/η Denis McMahon έγραψε:

On Thu, 26 Sep 2013 19:58:02 +0300, Νίκος wrote:


except socket.gaierror as e:
city = host = UnKnown Origin

But then what if in case of an error i needed different string set to be
assigned on city and host respectively?


Oh FFS

Are you serious when you ask this?

Simply change:

except socket.gaierror as e:
 city = host = UnKnown Origin

To:

except socket.gaierror as e:
 city = Unknown City
 host = Unknown Host


Yes indeed that was an idiotic question made by me, but i was somehow 
feeling again that i should handle it in one-liner, avoid wanting to use 
2 statements.


I wonder if there is a way to assign the string Unknown Origin to the 
variable that failed in the try block to get a value.


Can i describe that somehow inside the except block?

I mean like:

except socket.gaierror as e:
what_ever_var_failed = Unknown Origin
--
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread Dave Angel
On 26/9/2013 18:14, Νίκος wrote:

 Στις 26/9/2013 11:16 μμ, ο/η Denis McMahon έγραψε:
 On Thu, 26 Sep 2013 19:58:02 +0300, Νίκος wrote:

 except socket.gaierror as e:
 city = host = UnKnown Origin

 But then what if in case of an error i needed different string set to be
 assigned on city and host respectively?

 Oh FFS

 Are you serious when you ask this?

 Simply change:

 except socket.gaierror as e:
  city = host = UnKnown Origin

 To:

 except socket.gaierror as e:
  city = Unknown City
  host = Unknown Host

 Yes indeed that was an idiotic question made by me, but i was somehow 
 feeling again that i should handle it in one-liner, avoid wanting to use 
 2 statements.

newlines are still cheap.  And they can really help readability.

 I wonder if there is a way to assign the string Unknown Origin to the 
 variable that failed in the try block to get a value.

 Can i describe that somehow inside the except block?

 I mean like:

 except socket.gaierror as e:
   what_ever_var_failed = Unknown Origin

Simply assign the default values BEFORE the try block, and use pass as
the except block.

That still doesn't get around the inadvisability of putting those 3
lines in the try block.

You still haven't dealt with the gt assignment and its possible
exception.

-- 
DaveA


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


Re: Referrer key missing form os.environ dictionary?

2013-09-26 Thread Grant Edwards
On 2013-09-26, Chris Angelico ros...@gmail.com wrote:
 On Fri, Sep 27, 2013 at 12:11 AM, Grant Edwards invalid@invalid.invalid 
 wrote:
 On a Unix system when you invoke a program, you pass it four things:

  1) A dictionary where keys/values are both strings [enviornment variables]
  2) A list of strings [command line args]
  3) A set of open file descriptors.
  4) The current working directory.

 You can provide input values to the program through any of these.

 For interactive programs, 2 and 3 are the most convenient.

 Hrm, not sure about #3 for interactive programs, unless you
 specifically mean the three standard streams.

Yes, that's what I mean: shell redirection/pipelines.  Hooking stuff
to stdin is pretty much the only example of this you'll ever see in
the wild for input data:

 foo  inputdata
 
 bar | foo
 
 foo EOF
 this is input 
 data that is passed
 to program foo using
 file descriptor 0
 EOF 

 With most Unix shells, you should be able to set environment
 variables:

 PGUSER=fred PGPASSWORD=secret psql

Yep, that's rather rarely used.  The syntax/sematics for values passed
that way is very limited compared to the syntax/semantics that can be
used for command line arguments, so the latter is much more versatile.

 Not as convenient as #2, but far easier than passing an open file
 descriptor. Of course, passing file descriptors around is pretty easy
 programmatically, but when you say interactive I assume you're
 talking also about an interactive shell.

I probably should have said stdin, but in theory you can pass data
in via multiple file descriptors.  Nobody does that except people
cooking up obscure examples for advanced shell scripting guides...
 
 GUI interactions of course follow their own rules completely. In most
 systems, it's really easy to invoke an application with one argument,
 a file name; it's generally much harder to customize the arguments at
 the keyboard. OS/2 had a facility for doing that. You just put square
 brackets into the configured args and it'd turn it into a prompt:

 --foo=bar --mode=[Choose mode, 1-3:] %*

 You'd get a nice little popup with the prompt you specified, and
 whatever you type gets put into the args. Haven't seen that in any
 other system - at least, not as conveniently.

-- 
Grant Edwards   grant.b.edwardsYow! I request a weekend in
  at   Havana with Phil Silvers!
  gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Handling 3 operands in an expression without raising an exception

2013-09-26 Thread alex23

On 26/09/2013 6:39 PM, Νίκος wrote:

You dont have to be ironic. I dont have the experience you do.


This is bulshytt. You only just a few days ago started a thread in which 
handling lookups in dictionaries with potentially non-existent keys was 
dealt with. At length. And now you're restructuring pretty much the 
exact same question to troll the list. Again.


You're either not reading what people are saying, not trying to 
understand things by working with simpler examples at the interpreter, 
or just flat out lying. In any case, you're not acting in good faith.

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


Re: work develope matrix of difusive concentration

2013-09-26 Thread rusi
On Friday, September 27, 2013 12:43:51 AM UTC+5:30, D.YAN ESCOLAR RAMBAL wrote:
 Good morning all
 
 thanks for your help. Now I`ve a question, if I need create a matrix, method 
 of thomas, ghost nodes, etc.. for developed one aplicative for the ecuation 
 of difusion in 1d, 2d, 3d, 4d. It`s better use phyton 3.3 or 2.7?, if the 
 answer is phyton 3.3 that interpreter is similar to aptana o better, for 
 start, and which, libraries are necesary for this,  I think I need numpy, 
 numba, math, mathexpretion, matiplot, but  I don`t know which is the library 
 similar to matlab.

Sage??
http://en.wikipedia.org/wiki/Sage_%28mathematics_software%29

[I dont know this area myself]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nosetests

2013-09-26 Thread melwin9
Initially I was shown pexpect, leaving that there, Can i make up 5 tests? I 
tried tests two different ways and no luck. What am I supposed to be writing up 
when I do a test and is there a particular way I can/should be referencing it 
back to its main file?

THis is what I have for the test_guess.py

from unittest import TestCase
import pexpect as pe

import guess as g
import random

random_number = random.randrange(1, 10)
correct = False

class GuessTest(TestCase):
def setUp(self):
self.intro = 'I have chosen a number from 1-10'
self.request = 'Guess a number: '
self.responseHigh = That's too high.
self.responseLow  = That's too low.
self.responseCorrect = That's right!
self.goodbye = 'Goodbye and thanks for playing!'

def test_main(self):
#cannot execute main now because it will
#require user input
from guess import main

def test_guessing_hi_low_4(self):
# Conversation assuming number is 4
child = pe.spawn('python guess.py')
child.expect(self.intro,timeout=5)
child.expect(self.request,timeout=5)
child.sendline('5')
child.expect(self.responseHigh,timeout=5)
child.sendline('3')
child.expect(self.responseLow,timeout=5)
child.sendline('4')
child.expect(self.responseCorrect,timeout=5)
child.expect(self.goodbye,timeout=5)


def __init__(self):
self.number = random.randint(0,10)
HIGH = 1
LOW = 2
OK = 3

def guess(self, number):
if number  self.number:
 return self.HIGH
if number  self.number:
 return self.LOW
return self.OK

def test_guesstoolow(self):
while not correct:
guess = input(What could it be?)
if guess == random_number:
print Congrats You Got It
correct = True
elif guess  random_number:
print To High
elif guess  random_number:
print To Low
else:
print Try Again


and the guess.py file is


intro = 'I have chosen a number from 1-10'
request = 'Guess a number: '
responseHigh = That's too high.
responseLow  = That's too low.
responseCorrect = That's right!
goodbye = 'Goodbye and thanks for playing!'

def main():
print(intro)
user_input = raw_input(request)
print(responseHigh)
print(request)
user_input = raw_input(request)
print(responseLow)
user_input = raw_input(request)
print(responseCorrect)
print(goodbye)

if __name__ == '__main__':
main()


On Wednesday, September 25, 2013 11:48:59 PM UTC-4, Roy Smith wrote:
 In article 
 
   wrote:
 
 
 
  Not sure How to proceed on with writing a few more tests with if statement 
  to 
 
  test if the value is low or high. I was told to try a command line switch 
 
  like optparse to pass the number but not sure how to do that either. 
 
 
 
 One thing I would recommend is refactoring your game to keep the game 
 
 logic distinct from the I/O.
 
 
 
 If I was designing this, I would have some sort of game engine class, 
 
 which stores all the state for a game.  I imagine the first thing you 
 
 need to do is pick a random number and remember it.  Then, you'll need a 
 
 function to take a guess and tell you if it's too high, too low, or 
 
 correct.  Something like:
 
 
 
 class NumberGuessingGame:
 
def __init__(self):
 
   self.number = random.randint(0, 10)
 
 
 
HIGH = 1
 
LOW = 2
 
OK = 3
 
 
 
def guess(self, number):
 
   if number  self.number:
 
  return self.HIGH
 
   if number  self.number:
 
  return self.LOW
 
   return self.OK
 
 
 
 Now you've got something that's easy to test without all this pexpect 
 
 crud getting in the way.  Then, your game application can create an 
 
 instance of NumberGuessingGame and wrap the required I/O operations 
 
 around that.
 
 
 
 I'd also probably add some way to bypass the random number generator and 
 
 set the number you're trying to guess directly.  This will make it a lot 
 
 easier to test edge cases.

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


Re: Nosetests

2013-09-26 Thread Roy Smith
In article b428f41a-cad9-4223-87a2-9cc602681...@googlegroups.com,
 melw...@gmail.com wrote:

 Initially I was shown pexpect, leaving that there, Can i make up 5 tests? I 
 tried tests two different ways and no luck. What am I supposed to be writing 
 up when I do a test and is there a particular way I can/should be referencing 
 it back to its main file?

I'm not sure I understand your question.  Are you asking:

Q1: What tests should I be writing?

or

Q2: Once I know what I want to test, how do I implement those tests?

I'm guessing Q1, so that's what I'm going to base the rest of this post 
on.  Before you cat write a test, you have to understand what your code 
is supposed to do.  So, for example, let's say the specification for 
your program runs something like this:

When you run the program, it will print, I have chosen a number from 
1-10, and then it will print, Guess a number: .  It will then wait 
for input.  When you type an integer, it will print either, That's too 
high., That's too low., or That's right!.

Now, let's look at one of your tests:

def test_guessing_hi_low_4(self):

# Conversation assuming number is 4
child = pe.spawn('python guess.py')
child.expect(self.intro,timeout=5)
child.expect(self.request,timeout=5)
child.sendline('5')
child.expect(self.responseHigh,timeout=5)
child.sendline('3')
child.expect(self.responseLow,timeout=5)
child.sendline('4')
child.expect(self.responseCorrect,timeout=5)
child.expect(self.goodbye,timeout=5)

It looks pretty reasonable up to the point where you do:

child.sendline('5')
child.expect(self.responseHigh,timeout=5)

The problem is, you don't know what number it picked, so you can't 
predict what response it will have to an input of 5.  This goes back to 
what I was saying earlier.  You need some way to set the game to a known 
state, so you can test its responses, in that state, to various inputs.

If you're going to stick with the pexpect interface, then maybe you need 
a command line argument to override the random number generator and set 
the game to a specific number.  So, you can run:

$ python guess.py --test 4

and now you know the number it has picked is 4.  If you send it 5, it 
should tell you too high.  If you send it 3, it should tell you too low.  
And so on.

This is standard procedure in all kinds of testing.  You need some way 
to set the system being tested to a known state.  Then (and only then) 
can you apply various inputs and observe what outputs you get.  This is 
true of hardware was well.  Integrated circuits often have a test 
mode, where you can set the internal state of the chip to some known 
configuration before you perform a test.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nosetests

2013-09-26 Thread melwin9
The question was more like what tests should I be writing, fine if I remove the 
pexpect test I tried the test_guess  test_guesstoolow and still unable to get 
it to work. So if i Want to ask for a number and typed a number which is at 
random indicated by the top of the code, how do I proceed on with my tests?

On Thursday, September 26, 2013 9:16:32 PM UTC-4, Roy Smith wrote:
 In article ,
 
  wrote:
 
 
 
  Initially I was shown pexpect, leaving that there, Can i make up 5 tests? I 
 
  tried tests two different ways and no luck. What am I supposed to be 
  writing 
 
  up when I do a test and is there a particular way I can/should be 
  referencing 
 
  it back to its main file?
 
 
 
 I'm not sure I understand your question.  Are you asking:
 
 
 
 Q1: What tests should I be writing?
 
 
 
 or
 
 
 
 Q2: Once I know what I want to test, how do I implement those tests?
 
 
 
 I'm guessing Q1, so that's what I'm going to base the rest of this post 
 
 on.  Before you cat write a test, you have to understand what your code 
 
 is supposed to do.  So, for example, let's say the specification for 
 
 your program runs something like this:
 
 
 
 When you run the program, it will print, I have chosen a number from 
 
 1-10, and then it will print, Guess a number: .  It will then wait 
 
 for input.  When you type an integer, it will print either, That's too 
 
 high., That's too low., or That's right!.
 
 
 
 Now, let's look at one of your tests:
 
 
 
 def test_guessing_hi_low_4(self):
 
 
 
 # Conversation assuming number is 4
 
 child = pe.spawn('python guess.py')
 
 child.expect(self.intro,timeout=5)
 
 child.expect(self.request,timeout=5)
 
 child.sendline('5')
 
 child.expect(self.responseHigh,timeout=5)
 
 child.sendline('3')
 
 child.expect(self.responseLow,timeout=5)
 
 child.sendline('4')
 
 child.expect(self.responseCorrect,timeout=5)
 
 child.expect(self.goodbye,timeout=5)
 
 
 
 It looks pretty reasonable up to the point where you do:
 
 
 
 child.sendline('5')
 
 child.expect(self.responseHigh,timeout=5)
 
 
 
 The problem is, you don't know what number it picked, so you can't 
 
 predict what response it will have to an input of 5.  This goes back to 
 
 what I was saying earlier.  You need some way to set the game to a known 
 
 state, so you can test its responses, in that state, to various inputs.
 
 
 
 If you're going to stick with the pexpect interface, then maybe you need 
 
 a command line argument to override the random number generator and set 
 
 the game to a specific number.  So, you can run:
 
 
 
 $ python guess.py --test 4
 
 
 
 and now you know the number it has picked is 4.  If you send it 5, it 
 
 should tell you too high.  If you send it 3, it should tell you too low.  
 
 And so on.
 
 
 
 This is standard procedure in all kinds of testing.  You need some way 
 
 to set the system being tested to a known state.  Then (and only then) 
 
 can you apply various inputs and observe what outputs you get.  This is 
 
 true of hardware was well.  Integrated circuits often have a test 
 
 mode, where you can set the internal state of the chip to some known 
 
 configuration before you perform a test.

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


Re: Nosetests

2013-09-26 Thread melwin9
I modified  the guess.py file but am unable to run it, how do I go about 
writing tests for this.

import random

guessesTaken = 0

number = random.randint(1, 10)

intro = 'I have chosen a number from 1-10'
request = 'Guess a number: '
responseHigh = That's too high.
responseLow  = That's too low.
responseCorrect = That's right!
goodbye = 'Goodbye and thanks for playing!'

print(intro)
def main():
while guessesTaken  5:
print(request)
guess = input()
guess = int(guess)

guessesTaken = guessesTaken + 1

if guess  number:
print(responseLow) 

if guess  number:
print(responseHigh)

if guess == number:
break

if guess == number:
guessesTaken = str(guessesTaken)
print(responseCorrect + '! You guessed my number in ' + 
guessesTaken + ' guesses!')

if guess != number:
number = str(number)
print(goodbye + ' The number I was thinking of was ' + number)

##def main():
#print(intro)
 #   user_input = raw_input(request)
  #  print(responseHigh)
  #  print(request)
  #  user_input = raw_input(request)
  #  print(responseLow)
  #  user_input = raw_input(request)
  #  print(responseCorrect)
  #  print(goodbye)

if __name__ == '__main__':
main()


On Thursday, September 26, 2013 9:37:39 PM UTC-4,  wrote:
 The question was more like what tests should I be writing, fine if I remove 
 the pexpect test I tried the test_guess  test_guesstoolow and still unable 
 to get it to work. So if i Want to ask for a number and typed a number which 
 is at random indicated by the top of the code, how do I proceed on with my 
 tests?
 
 
 
 On Thursday, September 26, 2013 9:16:32 PM UTC-4, Roy Smith wrote:
 
  In article ,
 
  
 
   wrote:
 
  
 
  
 
  
 
   Initially I was shown pexpect, leaving that there, Can i make up 5 tests? 
   I 
 
  
 
   tried tests two different ways and no luck. What am I supposed to be 
   writing 
 
  
 
   up when I do a test and is there a particular way I can/should be 
   referencing 
 
  
 
   it back to its main file?
 
  
 
  
 
  
 
  I'm not sure I understand your question.  Are you asking:
 
  
 
  
 
  
 
  Q1: What tests should I be writing?
 
  
 
  
 
  
 
  or
 
  
 
  
 
  
 
  Q2: Once I know what I want to test, how do I implement those tests?
 
  
 
  
 
  
 
  I'm guessing Q1, so that's what I'm going to base the rest of this post 
 
  
 
  on.  Before you cat write a test, you have to understand what your code 
 
  
 
  is supposed to do.  So, for example, let's say the specification for 
 
  
 
  your program runs something like this:
 
  
 
  
 
  
 
  When you run the program, it will print, I have chosen a number from 
 
  
 
  1-10, and then it will print, Guess a number: .  It will then wait 
 
  
 
  for input.  When you type an integer, it will print either, That's too 
 
  
 
  high., That's too low., or That's right!.
 
  
 
  
 
  
 
  Now, let's look at one of your tests:
 
  
 
  
 
  
 
  def test_guessing_hi_low_4(self):
 
  
 
  
 
  
 
  # Conversation assuming number is 4
 
  
 
  child = pe.spawn('python guess.py')
 
  
 
  child.expect(self.intro,timeout=5)
 
  
 
  child.expect(self.request,timeout=5)
 
  
 
  child.sendline('5')
 
  
 
  child.expect(self.responseHigh,timeout=5)
 
  
 
  child.sendline('3')
 
  
 
  child.expect(self.responseLow,timeout=5)
 
  
 
  child.sendline('4')
 
  
 
  child.expect(self.responseCorrect,timeout=5)
 
  
 
  child.expect(self.goodbye,timeout=5)
 
  
 
  
 
  
 
  It looks pretty reasonable up to the point where you do:
 
  
 
  
 
  
 
  child.sendline('5')
 
  
 
  child.expect(self.responseHigh,timeout=5)
 
  
 
  
 
  
 
  The problem is, you don't know what number it picked, so you can't 
 
  
 
  predict what response it will have to an input of 5.  This goes back to 
 
  
 
  what I was saying earlier.  You need some way to set the game to a known 
 
  
 
  state, so you can test its responses, in that state, to various inputs.
 
  
 
  
 
  
 
  If you're going to stick with the pexpect interface, then maybe you need 
 
  
 
  a command line argument to override the random number generator and set 
 
  
 
  the game to a specific number.  So, you can run:
 
  
 
  
 
  
 
  $ python guess.py --test 4
 
  
 
  
 
  
 
  and now you know the number it has picked is 4.  If you send it 5, it 
 
  
 
  should tell you too high.  If you send it 3, it should tell you too low.  
 
  
 
  And so on.
 
  
 
  
 
  
 
  This is standard procedure in all kinds of testing.  You need some way 
 
  
 
  to set the system being tested to a known state.  Then (and only then) 
 
  
 
  can you apply various inputs and observe what outputs you get.  This is 
 
  
 
  true of hardware was well.  Integrated circuits often have a test 
 
  
 
  mode, where you can set the internal 

Re: Nosetests

2013-09-26 Thread Steven D'Aprano
On Thu, 26 Sep 2013 21:20:52 -0700, melwin9 wrote:

 I modified  the guess.py file but am unable to run it, 

What does that mean?

How do you try to run it?

What happens when you do?



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


[issue19092] ExitStack.__exit__ incorrectly suppresses exceptions in __exit__ callbacks of inner context managers

2013-09-26 Thread Hrvoje Nikšić

Hrvoje Nikšić added the comment:

Nick, thanks for the review. Do you need me to write the patch for the test 
suite along with the original patch?

--

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



[issue19092] ExitStack.__exit__ incorrectly suppresses exceptions in __exit__ callbacks of inner context managers

2013-09-26 Thread Nick Coghlan

Nick Coghlan added the comment:

That would be very helpful!

--

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



[issue19087] bytearray front-slicing not optimized

2013-09-26 Thread STINNER Victor

STINNER Victor added the comment:

 Mercurial has another implementation strategy for a similar thing:
 http://selenic.com/repo/hg/file/50d721553198/mercurial/util.py#l935

I found an interesting comment in the following issue:

I think the trouble we get into is chunkbuffer() creates new large strings by 
concatenation and causes memory fragmentation. Keeping a list of chunks might 
be more efficient.

http://bz.selenic.com/show_bug.cgi?id=1842#c17

@Antoine: Do you know if your patch may reduce the memory fragmentation on 
bytearray front-slicing?

--

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



[issue19087] bytearray front-slicing not optimized

2013-09-26 Thread Antoine Pitrou

Antoine Pitrou added the comment:

 @Antoine: Do you know if your patch may reduce the memory
 fragmentation on bytearray front-slicing?

It reduces the number of allocations so, yes, it can reduce memory
fragmentation.
We cannot really use a list of chunks for bytearray since it is
supposed to be usable as a contiguous buffer (using the buffer API).

--

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



[issue19094] urljoin should raise a TypeError if URL is not a string

2013-09-26 Thread Berker Peksag

Changes by Berker Peksag berker.pek...@gmail.com:


--
nosy: +berker.peksag

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



[issue19095] Document SSLSocket.getpeercert always returns None without do_handshake

2013-09-26 Thread Antoine Pitrou

Antoine Pitrou added the comment:

To be honest I'd rather have it raise an exception in that case. None isn't 
helpful as it could mean other things.

--
nosy: +christian.heimes, giampaolo.rodola, janssen, pitrou

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



[issue19083] IDNA prefix should be case insensitive

2013-09-26 Thread Antoine Pitrou

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


--
nosy: +haypo, loewis

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



[issue19096] multiprocessing.Pool._terminate_pool restarts workers during shutdown

2013-09-26 Thread Edward Catmur

New submission from Edward Catmur:

There is a race condition in multiprocessing.Pool._terminate_pool that can 
result in workers being restarted during shutdown (process shutdown or 
pool.terminate()).

worker_handler._state = TERMINATE#  race from here
task_handler._state = TERMINATE

debug('helping task handler/workers to finish')
cls._help_stuff_finish(inqueue, task_handler, len(pool))

assert result_handler.is_alive() or len(cache) == 0

result_handler._state = TERMINATE
outqueue.put(None)  # sentinel

# We must wait for the worker handler to exit before terminating
# workers because we don't want workers to be restarted behind our back.
debug('joining worker handler')
worker_handler.join()# ~ race to here

At any point between setting worker_handler._state = TERMINATE and joining the 
worker handler, if the intervening code causes a worker to exit then it is 
possible for the worker handler to fail to notice that it has been shutdown and 
so attempt to restart the worker:

@staticmethod
def _handle_workers(pool):
thread = threading.current_thread()

# Keep maintaining workers until the cache gets drained, unless the pool
# is terminated.
while thread._state == RUN or (pool._cache and thread._state != 
TERMINATE):
# ~~ race here
pool._maintain_pool()
time.sleep(0.1)
# send sentinel to stop workers
pool._taskqueue.put(None)
util.debug('worker handler exiting')

We noticed this initially because in the absence of the fix to #14881 a 
ThreadPool trying to restart a worker fails and hangs the process.  In the 
presence of the fix to #14881 there is no immediate issue, but trying to 
restart a worker process/thread on pool shutdown is clearly unwanted and could 
result in bad things happening e.g. at process shutdown.

To trigger the race with ThreadPool, it is enough just to pause the 
_handle_workers thread after checking its state and before calling 
_maintain_pool:

import multiprocessing.pool
import time
class ThreadPool(multiprocessing.pool.ThreadPool):
def _maintain_pool(self):
time.sleep(1)
super(ThreadPool, self)._maintain_pool()
def _repopulate_pool(self):
assert self._state == multiprocessing.pool.RUN
super(ThreadPool, self)._repopulate_pool()
pool = ThreadPool(4)
pool.map(lambda x: x, range(5))
pool.terminate()
pool.join()

Exception in thread Thread-5:
Traceback (most recent call last):
  File .../cpython/Lib/threading.py, line 657, in _bootstrap_inner
self.run()
  File .../cpython/Lib/threading.py, line 605, in run
self._target(*self._args, **self._kwargs)
  File .../cpython/Lib/multiprocessing/pool.py, line 358, in _handle_workers
pool._maintain_pool()
  File .../bug.py, line 6, in _maintain_pool
super(ThreadPool, self)._maintain_pool()
  File .../cpython/Lib/multiprocessing/pool.py, line 232, in _maintain_pool
self._repopulate_pool()
  File .../bug.py, line 8, in _repopulate_pool
assert self._state == multiprocessing.pool.RUN
AssertionError

In this case, the race occurs when ThreadPool._help_stuff_finish puts sentinels 
on inqueue to make the workers finish.

It is also possible to trigger the bug with multiprocessing.pool.Pool:

import multiprocessing.pool
import time
class Pool(multiprocessing.pool.Pool):
def _maintain_pool(self):
time.sleep(2)
super(Pool, self)._maintain_pool()
def _repopulate_pool(self):
assert self._state == multiprocessing.pool.RUN
super(Pool, self)._repopulate_pool()
@staticmethod
def _handle_tasks(taskqueue, put, outqueue, pool):
time.sleep(1)
_real_handle_tasks(taskqueue, put, outqueue, pool)
_real_handle_tasks = multiprocessing.pool.Pool._handle_tasks
multiprocessing.pool.Pool._handle_tasks = Pool._handle_tasks
pool = Pool(4)
pool.map(str, range(10))
pool.map_async(str, range(10))
pool.terminate()
pool.join()

In this case, the race occurs when _handle_tasks checks thread._state, breaks 
out of its first loop, and sends sentinels to the workers.

The terminate/join can be omitted, in which case the bug will occur at gc or 
process shutdown when the pool's atexit handler runs.  The bug is avoided if 
terminate is replaced with close, and we are using this workaround.

--
components: Library (Lib)
messages: 198432
nosy: ecatmur
priority: normal
severity: normal
status: open
title: multiprocessing.Pool._terminate_pool restarts workers during shutdown
type: behavior
versions: Python 2.7, Python 3.5

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue19096
___
___
Python-bugs-list mailing list
Unsubscribe: 

[issue19096] multiprocessing.Pool._terminate_pool restarts workers during shutdown

2013-09-26 Thread Antoine Pitrou

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


--
nosy: +sbt

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



[issue19016] autospecced namedtuples should be truthy by default

2013-09-26 Thread Shawn Krisman

Shawn Krisman added the comment:

This fix is actually backwards compatible. This is a more powerful patch too 
because not only does it provide a better default for truthiness, but it also 
provides a better default for length. I also fixed a spelling mistake involving 
the word calculate.

--
Added file: http://bugs.python.org/file31875/namedtuple_truthiness_2.patch

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



[issue19096] multiprocessing.Pool._terminate_pool restarts workers during shutdown

2013-09-26 Thread Edward Catmur

Edward Catmur added the comment:

Suggested patch: 
https://bitbucket.org/ecatmur/cpython/compare/19096-multiprocessing-race..#diff

Move the worker_handler.join() to immediately after setting the worker handler 
thread state to TERMINATE.  This is a safe change as nothing in the moved-over 
code affects the worker handler thread, except by terminating workers which is 
precisely what we don't want to happen.  In addition, this is near-equivalent 
behaviour to current close() + join(), which is well-tested.

Also: write tests; and modify Pool.__init__ to refer to its static methods 
using self rather than class name, to make them overridable for testing 
purposes.

--
hgrepos: +211

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



[issue19094] urljoin should raise a TypeError if URL is not a string

2013-09-26 Thread Vajrasky Kok

Vajrasky Kok added the comment:

This is the preliminary patch to raise TypeError in that situation.

--
keywords: +patch
nosy: +vajrasky
Added file: http://bugs.python.org/file31876/urljoin_throws_type_error.patch

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



[issue19095] Document SSLSocket.getpeercert always returns None without do_handshake

2013-09-26 Thread Dariusz Suchojad

Dariusz Suchojad added the comment:

 None isn't helpful as it could mean other things.

This is another story but yes, it's true. API-wise, None should be returned in 
one situation only - we're on server side, ca_certs is non-CERT_NONE, 
do_handshake has been called yet there is no client certificate. And no other 
checks should be applied.

But the current behavior of returning None is documented and people depend on 
it so straightening it out would break backward compatibility - it's up to you 
to decide. I wouldn't mind it personally. 

But as far as this ticket goes - I'm on 2.7 and it's set in stone so for 2.7 - 
can you please change copy only? If you decide that for 3.x an exception will 
be raised then such a caveat would be included in 2.7 docs as well.

Thanks again.

--

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



[issue19083] IDNA prefix should be case insensitive

2013-09-26 Thread Martin v . Löwis

Martin v. Löwis added the comment:

Pepijn: Can you please submit the contributor form?

http://www.python.org/psf/contrib/contrib-form/

--

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



[issue19083] IDNA prefix should be case insensitive

2013-09-26 Thread STINNER Victor

STINNER Victor added the comment:

Could you also add a test using a IDNA domain not starting with XN--, but with 
XN-- in the middle? Example:

self.assertEqual(str(bbugs.XN--pythn-mua.org., idna), bugs.pyth\xf6n.org.)

And maybe also with mixed cases: Xn-- and xN-- prefixes.

--

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



[issue1185124] pydoc doesn't find all module doc strings

2013-09-26 Thread Sunny

Sunny added the comment:

Added patch for 2.7. Please review.

--
Added file: http://bugs.python.org/file31877/pydoc_2.7.patch

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



[issue19087] bytearray front-slicing not optimized

2013-09-26 Thread STINNER Victor

STINNER Victor added the comment:

Could you please add unit tests for check that ob_start is used instead of 
memmove()?

I didn't find a function for that in _testcapi. I tried to test it using 
sys.getsizeof(), but the size is not reliable (the bytearray buffer is not 
always shrinked, it depends on the new size).

The best is probably to add a new function in _testcapi to get private 
attributes: ob_exports, ob_alloc, ob_start, ob_bytes. Using these attributes, 
it becomes easy to check that fast-path are correctly optimized (eg. increases 
ob_start instead of getting a new ob_bytes buffer).

--

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



[issue19095] Document SSLSocket.getpeercert always returns None without do_handshake

2013-09-26 Thread Antoine Pitrou

Antoine Pitrou added the comment:

 But the current behavior of returning None is documented and people
 depend on it so straightening it out would break backward
 compatibility - it's up to you to decide. I wouldn't mind it
 personally.

I'm not sure people depend on getpeercert() returning None before the
handshake is done, or perhaps by accident?

 But as far as this ticket goes - I'm on 2.7 and it's set in stone so
 for 2.7 - can you please change copy only? If you decide that for
 3.x an exception will be raised then such a caveat would be included
 in 2.7 docs as well.

Yes, if we change behaviour it will only be in 3.4.

--

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



[issue19087] bytearray front-slicing not optimized

2013-09-26 Thread Antoine Pitrou

Antoine Pitrou added the comment:

 Could you please add unit tests for check that ob_start is used
 instead of memmove()?

How would I do that? Most of the time we don't unit-test performance
improvements (i.e. there are no tests that list.append() is O(1), for
example).

--

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



  1   2   >