Re: Encapsulation unpythonic?

2013-08-31 Thread Gary Herron

On 08/30/2013 11:07 PM, Fabrice Pombet wrote:

... long discussion elided ...
well, look at that:

a=(1,2)
a=2+3 ->a is an object and I have changed its type and value from outside. As 
far as I am concerned this is one hell of an encapsulation violation... Could you 
do this -strictly speaking- in Java or C++?


Yes, in fact you can do that in C++ and java:

Obj1 a = ...some object...;
{ // new scope...
   Obj2 a = ...another object...;
}

On one line, the name 'a' is bound to one object, and later it is bound 
to another object.   Your Python code is similar, binding the name 'a' 
to object (1,2) on one line and the object 5 on the next line.  Granted, 
Python seems a little freer because, with it's dynamic typing,  one 
doesn't need to create a new scope to rebind a name, but all languages 
with variable names allow some control over binding/rebinding names.


But this has *nothing* at all to do with objects and encapsulation.

Please don't confuse:

   the binding of names to objects and

   the existence of objects and their encapsulated behavior

They are very different things.

--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

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


Re: UnicodeDecodeError issue

2013-08-31 Thread Ferrous Cranus

Στις 31/8/2013 9:53 πμ, ο/η Chris Angelico έγραψε:

On Sat, Aug 31, 2013 at 4:41 PM, Ferrous Cranus  wrote:

Suddenly my webiste superhost.gr running my main python script presents me
with this error:

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


Does anyone know what this means?


Yes. It means that 0xB6 is an invalid start byte in UTF-8. If that
seems unhelpful, it's because your question was; there is not enough
information for us to be able to give any further information.



Hello Chris,

I provided you with only that info because that only info is displayed 
at http://superhost.gr at the top left.


If you please tell me what to try to provide you guys with more info 
about this problem.



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


Re: UnicodeDecodeError issue

2013-08-31 Thread Ferrous Cranus

Στις 31/8/2013 10:02 πμ, ο/η Ferrous Cranus έγραψε:

Στις 31/8/2013 9:53 πμ, ο/η Chris Angelico έγραψε:

On Sat, Aug 31, 2013 at 4:41 PM, Ferrous Cranus 
wrote:

Suddenly my webiste superhost.gr running my main python script
presents me
with this error:

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


Does anyone know what this means?


Yes. It means that 0xB6 is an invalid start byte in UTF-8. If that
seems unhelpful, it's because your question was; there is not enough
information for us to be able to give any further information.



Hello Chris,

I provided you with only that info because that only info is displayed
at http://superhost.gr at the top left.

If you please tell me what to try to provide you guys with more info
about this problem.


And the wird thign is that few the last days this error was not there, 
just only today it started dissaperaing.


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


Re: UnicodeDecodeError issue

2013-08-31 Thread Peter Otten
Ferrous Cranus wrote:

> Suddenly my webiste superhost.gr running my main python script presents
> me with this error:
 
> Code:
> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
> \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
> 'invalid start byte')
 
> Does anyone know what this means?

>>> b'\xb6\xe3\xed\xf9\xf3\xf4\xef\xfc\xed\xef\xec\xe1\xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2'.decode("iso-8859-7")
'Unknown hostname'


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


Re: Encapsulation unpythonic?

2013-08-31 Thread Fabrice Pombet
On Saturday, August 31, 2013 9:03:58 AM UTC+2, Gary Herron wrote:
> On 08/30/2013 11:07 PM, Fabrice Pombet
>   wrote:
> 
> 
> ... long discussion elided ...
> 
>   well, look at that:
> 
> a=(1,2)
> a=2+3 ->a is an object and I have changed its type and value from outside. As 
> far as I am concerned this is one hell of an encapsulation violation... Could 
> you do this -strictly speaking- in Java or C++?
> 
> 
> 
> 
> Yes, in fact you can do that in C++ and java:
> 
> 
> 
> Obj1 a = ...some object...;
> 
> { // new scope...
> 
>    Obj2 a = ...another object...;
> 
> }
> 
> 
> 
> On one line, the name 'a' is bound to one object, and later it is
> bound to another object.   Your Python code is similar, binding the
> name 'a' to object (1,2) on one line and the object 5 on the next
> line.  Granted, Python seems a little freer because, with it's
> dynamic typing,  one doesn't need to create a new scope to rebind a
> name, but all languages with variable names allow some control over
> binding/rebinding names.
> 
> 
> 
> But this has *nothing* at all to do with objects and encapsulation.
> 
> 
> 
> Please don't confuse:
> 
> the binding of names to objects and
> 
>   
> 
>   the existence of objects and their encapsulated behavior
> 
> 
> They are very different things.
> 
> 
> 
> -- 
> Dr. Gary Herron
> Department of Computer Science
> DigiPen Institute of Technology
> (425) 895-4418

That's interesting, can you do this in C++ or java:

class X():
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semicolon at end of python's statements

2013-08-31 Thread Jussi Piitulainen
Steven D'Aprano writes:

> We really are spoiled for choice here. We can write any of these:
> 
> # Option 1
> for spam in sequence:
> if predicate(spam):
> process(spam)

# Option 1.5
for spam in sequence:
if not predicate(spam): continue
process(spam)

This saves an indent level.

> # Option 2
> for spam in filter(predicate, sequence):
> process(spam)
> 
> # Option 3
> for spam in (spam for spam in sequence if predicate(spam)):
> process(spam)
> 
> 
> Adding a fourth option:
> 
> for spam in sequence if predicate(spam):
> process(spam)
> 
> saves absolutely nothing except a line and an indent level, neither
> of which are in short supply, and gains nothing in readability over
> Option 1.


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


Re: UnicodeDecodeError issue

2013-08-31 Thread Ferrous Cranus

Στις 31/8/2013 10:25 πμ, ο/η Peter Otten έγραψε:

Ferrous Cranus wrote:


Suddenly my webiste superhost.gr running my main python script presents
me with this error:



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



Does anyone know what this means?



b'\xb6\xe3\xed\xf9\xf3\xf4\xef\xfc\xed\xef\xec\xe1\xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2'.decode("iso-8859-7")

'Unknown hostname'



Thanks you decoded in greek-iso, coll thinking!

But llok:

Why can the host varibale be unset since iam using this:


try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
	city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or 
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
	host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or 
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or 'Αγνωστη 
Προέλευση'

except Exception as e:
err = repr(e)

==
it gets set to string 'Αγνωστη Προέλευση' if in case visitor's remote ip 
address cannot be resolved.


So that error should arise at all.

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


Re: Encapsulation unpythonic?

2013-08-31 Thread Fabrice Pombet
On Saturday, August 31, 2013 9:42:55 AM UTC+2, Fabrice Pombet wrote:
> On Saturday, August 31, 2013 9:03:58 AM UTC+2, Gary Herron wrote:
> 
> > On 08/30/2013 11:07 PM, Fabrice Pombet
> 
> >   wrote:
> 
> > 
> 
> > 
> 
> > ... long discussion elided ...
> 
> > 
> 
> >   well, look at that:
> 
> > 
> 
> > a=(1,2)
> 
> > a=2+3 ->a is an object and I have changed its type and value from outside. 
> > As far as I am concerned this is one hell of an encapsulation violation... 
> > Could you do this -strictly speaking- in Java or C++?
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > Yes, in fact you can do that in C++ and java:
> 
> > 
> 
> > 
> 
> > 
> 
> > Obj1 a = ...some object...;
> 
> > 
> 
> > { // new scope...
> 
> > 
> 
> >    Obj2 a = ...another object...;
> 
> > 
> 
> > }
> 
> > 
> 
> > 
> 
> > 
> 
> > On one line, the name 'a' is bound to one object, and later it is
> 
> > bound to another object.   Your Python code is similar, binding the
> 
> > name 'a' to object (1,2) on one line and the object 5 on the next
> 
> > line.  Granted, Python seems a little freer because, with it's
> 
> > dynamic typing,  one doesn't need to create a new scope to rebind a
> 
> > name, but all languages with variable names allow some control over
> 
> > binding/rebinding names.
> 
> > 
> 
> > 
> 
> > 
> 
> > But this has *nothing* at all to do with objects and encapsulation.
> 
> > 
> 
> > 
> 
> > 
> 
> > Please don't confuse:
> 
> > 
> 
> > the binding of names to objects and
> 
> > 
> 
> >   
> 
> > 
> 
> >   the existence of objects and their encapsulated behavior
> 
> > 
> 
> > 
> 
> > They are very different things.
> 
> > 
> 
> > 
> 
> > 
> 
> > -- 
> 
> > Dr. Gary Herron
> 
> > Department of Computer Science
> 
> > DigiPen Institute of Technology
> 
> > (425) 895-4418
> 
> 
> 
> That's interesting, can you do this in C++ or java:
> 
> 
> 
> class X():
def __init__(self, *arg):
for x in arg:
self.x=x

and then:

a=X("x","y","z")
and then:
a.w="w" 
?

I guess my point was dynamic typing and encapsulation go a little in opposite 
directions in terms of philosophy, and it is therefore clear that Python 
privileges "dynamic typing" kind of thinking over encapsulation as a 
philosophical stance. Am I the only one thinking like this?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semicolon at end of python's statements

2013-08-31 Thread Paul Rudin
Jussi Piitulainen  writes:


> # Option 1.5
> for spam in sequence:
> if not predicate(spam): continue
> process(spam)
>
> This saves an indent level.

Just out of interest: is saving an indent level a useful thing?

I wouldn't lay out my code like that just because if you're coming back
to it later and reading through quickly it's (to my mind at least)
easier to miss what's going on.
-- 
http://mail.python.org/mailman/listinfo/python-list


print function and unwanted trailing space

2013-08-31 Thread candide


What is the equivalent in Python 3 to the following Python 2 code:

# -
for i in range(5):
print i,
# -

?

Be careful that the above code doesn't add a trailing space after the 
last number in the list, hence the following Python 3 code isn't 
strictly equivalent:



# -
for i in range(5):
print(i, end=' ')   # <- The last ' ' is unwanted
print()
# -
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading from stdin first, then use curses

2013-08-31 Thread Timo Schmiade
Hi again,

sorry for replying to my own mail, but is there really no solution? Can
curses really not be used in this situation?

Thanks again,

Timo

On Sun, Aug 11, 2013 at 02:05:11PM +0200, Timo Schmiade wrote:
> Hi all,
> 
> I wrote a replacement for urlview to properly extract URLs from emails.
> You can find the first draft here:
> 
>   https://github.com/the-isz/pyurlview
> 
> When I call it with an email file passed to the '-f' argument, it does
> pretty much what I want already. However, I intend to use it in mutt,
> which pipes the message to the program like so:
> 
> macro pager \cu 'pyurlview.py' 'Follow links with 
> pyurlview'
> 
> The problem is rather obvious but - unfortunately - not so easy to solve:
> 
> * The program reads the mail from stdin
> * The terminal in which it runs is a pseudo-terminal (pipe)
> * curses is not able to accept user input from the pseudo-terminal
> 
> The question is:
> 
> How do I read from stdin first and afterwards allow curses to read user
> input?
> 
> Thanks in advance and kind regards,
> 
> Timo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError issue

2013-08-31 Thread Ferrous Cranus


Here is the code inside files.py:

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

except Exception as e:
print( repr(e), file=open( '/tmp/err.out', 'w' ) )


that produces this:


[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] ValueError: 
underlying buffer has been detached, referer: http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] , referer: 
http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] Original 
exception was:, referer: http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] Traceback 
(most recent call last):, referer: http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116]   File 
"/home/nikos/public_html/cgi-bin/files.py", line 135, in , 
referer: http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] 
cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES 
(%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), referer: 
http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] NameError: 
name 'host' is not defined, referer: http://superhost.gr/



But 'host' defaults to an ip address if it cannot resolve the hostname.
Why the errro says its undefined?
--
http://mail.python.org/mailman/listinfo/python-list


Re: print function and unwanted trailing space

2013-08-31 Thread Andreas Perstinger

On 31.08.2013 10:17, candide wrote:


What is the equivalent in Python 3 to the following Python 2 code:

# -
for i in range(5):
  print i,
# -

?


How about

>>> print(" ".join(str(i) for i in range(5)))
0 1 2 3 4

Bye, Andreas



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


Re: print function and unwanted trailing space

2013-08-31 Thread candide

Le 31/08/2013 10:43, Andreas Perstinger a écrit :

> How about
>
>  >>> print(" ".join(str(i) for i in range(5)))
> 0 1 2 3 4
>


Thanks for your answer. The output is stricly the same but the code 
doesn't suit my needs :


1) I'm porting to Python 3 a Python 2 full beginner course : the 
learners are not aware of the join method nor the str type nor 
generators stuff;
2) Your code introduce a (sometimes) useless conversion to str (consider 
a string instead of range(5)).



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


Re: semicolon at end of python's statements

2013-08-31 Thread Jussi Piitulainen
Paul Rudin writes:
> Jussi Piitulainen writes:
> 
> > # Option 1.5
> > for spam in sequence:
> > if not predicate(spam): continue
> > process(spam)
> >
> > This saves an indent level.
> 
> Just out of interest: is saving an indent level a useful thing?

It might be if process(spam) is a more complex statement.

> I wouldn't lay out my code like that just because if you're coming
> back to it later and reading through quickly it's (to my mind at
> least) easier to miss what's going on.

I agree with the general principle but I have actually done the above
(once or twice) precisely because I found it to be the clearer choice
in a particular situation.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: print function and unwanted trailing space

2013-08-31 Thread Peter Otten
candide wrote:

> Le 31/08/2013 10:43, Andreas Perstinger a écrit :
> 
>  > How about
>  >
>  >  >>> print(" ".join(str(i) for i in range(5)))
>  > 0 1 2 3 4
>  >
> 
> 
> Thanks for your answer. The output is stricly the same but the code
> doesn't suit my needs :
> 
> 1) I'm porting to Python 3 a Python 2 full beginner course : the
> learners are not aware of the join method nor the str type nor
> generators stuff;
> 2) Your code introduce a (sometimes) useless conversion to str (consider
> a string instead of range(5)).

You are out of luck, the softspace mechanism, roughly

softspace = False
for i in range(5):
if softspace:
print(end=" ")
print(i, end="")
softspace = True
print()

with `softspace` saved as a file attribute, is gone in Python3. But I don't 
think that someone who doesn't know it existed will miss the feature.

Maybe you can allow for /some/ magic and introduce your students to

print(*range(5))

early-on.

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


Re: print function and unwanted trailing space

2013-08-31 Thread Steven D'Aprano
On Sat, 31 Aug 2013 10:17:23 +0200, candide wrote:

> What is the equivalent in Python 3 to the following Python 2 code:
> 
> # -
> for i in range(5):
>  print i,
> # -
> 
> ?
> 
> Be careful that the above code doesn't add a trailing space after the
> last number in the list, 

Of course it does. Have you actually tried it? The interactive 
interpreter is tricky, because you cannot directly follow a for-loop with 
another statement. If you try, the interactive interpreter gives you an 
indentation error. But we can work around it by sticking everything 
inside an if block, like so:

py> if True:
... for i in range(5):
... print i,
... # could be pages of code here
... print "FINISHED"
...
0 1 2 3 4 FINISHED


Or you could stick the code inside an exec, which doesn't have the same 
limitation as the interactive interpreter. This mimics the behaviour of 
code in a file:

py> exec """for i in range(5):
... print i,
... print "FINISHED"
... """
0 1 2 3 4 FINISHED


The same results occur with any other Python 2.x, and indeed all the way 
back to Python 1.5 and older.


> hence the following Python 3 code isn't strictly equivalent:
> 
> 
> # -
> for i in range(5):
>  print(i, end=' ')   # <- The last ' ' is unwanted
> print()


The last space is exactly the same as you get in Python 2. But really, 
who cares about an extra invisible space? In non-interactive mode, the 
two are exactly the same (ignoring the extra print() call outside the 
loop), but even at the interactive interpreter, I'd like to see the code 
where an extra space makes a real difference.



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


Re: print function and unwanted trailing space

2013-08-31 Thread Ned Batchelder

On 8/31/13 4:17 AM, candide wrote:


What is the equivalent in Python 3 to the following Python 2 code:

# -
for i in range(5):
print i,
# -

?

Be careful that the above code doesn't add a trailing space after the 
last number in the list, hence the following Python 3 code isn't 
strictly equivalent:



# -
for i in range(5):
print(i, end=' ')   # <- The last ' ' is unwanted
print()
# -


For a beginner course, the trailing space is fine, use this code. 
They'll never notice the trailing space (I wouldn't have!) and to 
explain why the comma leaves off the last one takes a really advanced 
understanding of obscure details anyway.


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


Re: UnicodeDecodeError issue

2013-08-31 Thread Steven D'Aprano
On Sat, 31 Aug 2013 11:31:13 +0300, Ferrous Cranus wrote:

> Here is the code inside files.py:

The code you show is not the ENTIRE code inside of files.py, is it? You 
are only showing us a small piece, correct?

The code you show:

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


does not contain a call to cur.execute. And here is your error:


> [error] [client 108.162.229.116] Traceback (most recent call last):,
> referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client
> 108.162.229.116]   File "/home/nikos/public_html/cgi-bin/files.py", line
> 135, in , referer: http://superhost.gr/
> [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116]
> cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES
> (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), referer:
> http://superhost.gr/
> [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] NameError:
> name 'host' is not defined, referer: http://superhost.gr/


Extracting out the critical part:

line 135
cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES
(%s, %s, %s, %s)''', (filename, host, city, lastvisit) ),

Where is the call to cur.execute in the code snippet you show above?



> But 'host' defaults to an ip address if it cannot resolve the hostname.
> Why the errro says its undefined?

Because it is is undefined. Python is not lying to you. If Python tells 
you there is an error, BELIEVE IT. Resolving the hostname is irrelevant.

print(host)  ### ERROR OCCURS HERE BECAUSE HOST IS UNDEFINED ###
host = socket.gethostbyaddr(addr)

The second line is too late, the error has already occurred.


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


Re: Encapsulation unpythonic?

2013-08-31 Thread Steven D'Aprano
On Fri, 30 Aug 2013 23:07:47 -0700, Fabrice Pombet wrote:

> well, look at that:
> 
> a=(1,2)
> a=2+3 ->a is an object and I have changed its type and value from
> outside. 

Incorrect. You have not changed the type or value of any object. "a" is 
not an object, it is a *name*, and while you can change the object bound 
to the name, the objects remain unchanged.

When you do this:

x = 23
x = 42

the *object* 23 does not change, only the name binding changes. To do 
otherwise would cause all sorts of surprises:

# THIS DOES NOT HAPPEN IN PYTHON
# or any other language, as far as I am aware
x = 23
y = x  # y now has the value 23
x = 42  # change the value of the object  ### NOT SO! ###
print y
=> prints 42

Name binding (assignment) does not change objects. It changes the link 
between a name and the object, but the object remains untouched (unless 
it is unbound, and garbage collected). Assignment is not mutation. 
Assigning to a name does not modify the object that was previously bound.


But even if you were right about changing the type and value of objects 
in place -- Python allows you to mutate lists and dicts in place, and 
even mutate the type of some objects, although not built-ins -- your 
understanding is still confused. Re-assignment (re-binding) of local 
variables is hardly a violation of encapsulation. But if it was, then 
Java and C++ have no encapsulation either, because you can re-assign to 
local variables inside a function too.

If you want to see a language without encapsulation, you need to look at 
something like 1970s-style BASIC, a language where all variables are 
global, where there is no support for grouping related code into separate 
modules or files, where the closest thing to a function call is GOTO or 
GOSUB.

Of course, languages can have *more* or *less* encapsulation than other 
languages. C lets you encapsulate related functions into a file, but it 
has no way of grouping data and functions together except loosely, in a 
file. C++ adds classes, which has more encapsulation since you can group 
functions and their data together.



> As far as I am concerned this is one hell of an encapsulation
> violation... Could you do this -strictly speaking- in Java or C++?

Of course you could. All you need is a way to tell the compiler not to 
type-check the variable "a". Or more practically, some way to declare 
variable "a" as a reference to an untyped or generic value. C# has the 
dynamic keyword for this functionality. I don't know about Java or C++, 
but the JVM is certainly capable of implementing dynamic typing as there 
are various dynamically-typed languages built on top of the JVM, such as 
OpenXION and Cobra.



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


Re: print function and unwanted trailing space

2013-08-31 Thread Peter Otten
Steven D'Aprano wrote:

> On Sat, 31 Aug 2013 10:17:23 +0200, candide wrote:
> 
>> What is the equivalent in Python 3 to the following Python 2 code:
>> 
>> # -
>> for i in range(5):
>>  print i,
>> # -
>> 
>> ?
>> 
>> Be careful that the above code doesn't add a trailing space after the
>> last number in the list,
> 
> Of course it does. Have you actually tried it? The interactive
> interpreter is tricky, because you cannot directly follow a for-loop with
> another statement. If you try, the interactive interpreter gives you an
> indentation error. But we can work around it by sticking everything
> inside an if block, like so:
> 
> py> if True:
> ... for i in range(5):
> ... print i,
> ... # could be pages of code here
> ... print "FINISHED"
> ...
> 0 1 2 3 4 FINISHED
> 
> 
> Or you could stick the code inside an exec, which doesn't have the same
> limitation as the interactive interpreter. This mimics the behaviour of
> code in a file:
> 
> py> exec """for i in range(5):
> ... print i,
> ... print "FINISHED"
> ... """
> 0 1 2 3 4 FINISHED
> 
> 
> The same results occur with any other Python 2.x, and indeed all the way
> back to Python 1.5 and older.

Your test is flawed. The softspace mechanism ensures that there is a space 
*between* all printed items, but not *after* the last printed item.

print "FINISHED"

will add a space while

print

will not. Compare:

>>> with open("tmp.txt", "w") as f:
... for i in range(3): print >> f, i,
... print >> f
... 
>>> open("tmp.txt").read()
'0 1 2\n'
>>> with open("tmp.txt", "w") as f:
... for i in range(3): print >> f, i,
... print >> f, "FINISHED"
... 
>>> open("tmp.txt").read()
'0 1 2 FINISHED\n'




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


Re: print function and unwanted trailing space

2013-08-31 Thread Oscar Benjamin
On 31 August 2013 12:16, Steven D'Aprano
 wrote:
> On Sat, 31 Aug 2013 10:17:23 +0200, candide wrote:
>
>> What is the equivalent in Python 3 to the following Python 2 code:
>>
>> # -
>> for i in range(5):
>>  print i,
>> # -
>>
>> ?
>>
>> Be careful that the above code doesn't add a trailing space after the
>> last number in the list,
>
> Of course it does. Have you actually tried it? The interactive
> interpreter is tricky, because you cannot directly follow a for-loop with
> another statement. If you try, the interactive interpreter gives you an
> indentation error. But we can work around it by sticking everything
> inside an if block, like so:
>
> py> if True:
> ... for i in range(5):
> ... print i,
> ... # could be pages of code here
> ... print "FINISHED"
> ...
> 0 1 2 3 4 FINISHED

The space is added by the final print statement, not the last one in
the loop. Here's a demo that shows this:

$ cat print.py
for i in range(5):
print i,
print
$ cat print3.py
for i in range(5):
print(i, end=' ')
print()
$ py -2.7 print.py | cat -A
0 1 2 3 4^M$
$ py -3.3 print3.py | cat -A
0 1 2 3 4 ^M$

(Notice the space between the 4 and ^M (which is cat's way of saying '\r').

[snip]
>
>> hence the following Python 3 code isn't strictly equivalent:
>>
>>
>> # -
>> for i in range(5):
>>  print(i, end=' ')   # <- The last ' ' is unwanted
>> print()
>
> The last space is exactly the same as you get in Python 2. But really,
> who cares about an extra invisible space? In non-interactive mode, the
> two are exactly the same (ignoring the extra print() call outside the
> loop), but even at the interactive interpreter, I'd like to see the code
> where an extra space makes a real difference.

I seem to remember it breaking some unit test or doc test something
when I first tried to port something using 2to3 (this is the
replacement that 2to3 uses for print with a trailing comma). It's not
so important for interactive terminal output but when you do 'python
script.py > output.dat' the unwanted space shouldn't be there. The
soft-space feature is useful but stateful as Peter says and defies the
normal concept of what happens when calling a function so it was
removed when print became a function.


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


Re: Encapsulation unpythonic?

2013-08-31 Thread Ned Batchelder

On 8/31/13 7:46 AM, Steven D'Aprano wrote:

On Fri, 30 Aug 2013 23:07:47 -0700, Fabrice Pombet wrote:


well, look at that:

a=(1,2)
a=2+3 ->a is an object and I have changed its type and value from
outside.

Incorrect. You have not changed the type or value of any object. "a" is
not an object, it is a *name*, and while you can change the object bound
to the name, the objects remain unchanged.

When you do this:

x = 23
x = 42

the *object* 23 does not change, only the name binding changes. To do
otherwise would cause all sorts of surprises:

# THIS DOES NOT HAPPEN IN PYTHON
# or any other language, as far as I am aware
x = 23
y = x  # y now has the value 23
x = 42  # change the value of the object  ### NOT SO! ###
print y
=> prints 42

Name binding (assignment) does not change objects. It changes the link
between a name and the object, but the object remains untouched (unless
it is unbound, and garbage collected). Assignment is not mutation.
Assigning to a name does not modify the object that was previously bound.



I wrote a piece about names and values that might help clarify these 
points: Facts and Myths about Names and Values in Python: 
http://nedbatchelder.com/text/names.html


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


Re: Encapsulation unpythonic?

2013-08-31 Thread Fabrice Pombet

> 
> http://nedbatchelder.com/text/names.html
> 
> 
> 
> --Ned.

This is an excellent explanation, thank you. It is mostly of theoretical 
interest though, and in practice, I still contend that the consequences towards 
the syntax are (or seem, if you prefer) analogous to those of the lack of 
encapsulation. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-08-31 Thread Fabrice Pombet
On Saturday, August 31, 2013 1:46:52 PM UTC+2, Steven D'Aprano wrote:
> On Fri, 30 Aug 2013 23:07:47 -0700, Fabrice Pombet wrote:
> 
> 
> 
> > well, look at that:
> 
> > 
> 
> > a=(1,2)
> 
> > a=2+3 ->a is an object and I have changed its type and value from
> 
> > outside. 
> 
> 
> 
> Incorrect. You have not changed the type or value of any object. "a" is 
> 
> not an object, it is a *name*, and while you can change the object bound 
> 
> to the name, the objects remain unchanged.
> 
> 
> 
> When you do this:
> 
> 
> 
> x = 23
> 
> x = 42
> 
> 
> 
> the *object* 23 does not change, only the name binding changes. To do 
> 
> otherwise would cause all sorts of surprises:
> 
> 
> 
> # THIS DOES NOT HAPPEN IN PYTHON
> 
> # or any other language, as far as I am aware
> 
> x = 23
> 
> y = x  # y now has the value 23
> 
> x = 42  # change the value of the object  ### NOT SO! ###
> 
> print y
> 
> => prints 42
> 
> 
> 
> Name binding (assignment) does not change objects. It changes the link 
> 
> between a name and the object, but the object remains untouched (unless 
> 
> it is unbound, and garbage collected). Assignment is not mutation. 
> 
> Assigning to a name does not modify the object that was previously bound.
> 
> 
> 
> 
> 
> But even if you were right about changing the type and value of objects 
> 
> in place -- Python allows you to mutate lists and dicts in place, and 
> 
> even mutate the type of some objects, although not built-ins -- your 
> 
> understanding is still confused. Re-assignment (re-binding) of local 
> 
> variables is hardly a violation of encapsulation. But if it was, then 
> 
> Java and C++ have no encapsulation either, because you can re-assign to 
> 
> local variables inside a function too.
> 
> 
> 
> If you want to see a language without encapsulation, you need to look at 
> 
> something like 1970s-style BASIC, a language where all variables are 
> 
> global, where there is no support for grouping related code into separate 
> 
> modules or files, where the closest thing to a function call is GOTO or 
> 
> GOSUB.
> 
> 
> 
> Of course, languages can have *more* or *less* encapsulation than other 
> 
> languages. C lets you encapsulate related functions into a file, but it 
> 
> has no way of grouping data and functions together except loosely, in a 
> 
> file. C++ adds classes, which has more encapsulation since you can group 
> 
> functions and their data together.
> 
> 
> 
> 
> 
> 
> 
> > As far as I am concerned this is one hell of an encapsulation
> 
> > violation... Could you do this -strictly speaking- in Java or C++?
> 
> 
> 
> Of course you could. All you need is a way to tell the compiler not to 
> 
> type-check the variable "a". Or more practically, some way to declare 
> 
> variable "a" as a reference to an untyped or generic value. C# has the 
> 
> dynamic keyword for this functionality. I don't know about Java or C++, 
> 
> but the JVM is certainly capable of implementing dynamic typing as there 
> 
> are various dynamically-typed languages built on top of the JVM, such as 
> 
> OpenXION and Cobra.
> -- 
> 
> Steven

Steve, I think that your definition of encapsulation is too wide to give a 
reasonable answer to the question at hand. If I understand you correctly, you 
are taking encapsulation as a language characteristic,  rather than a 
principle. 
Plus, you seem to forget that encapsulation is an OOP principle, and, forgive 
me if I am wrong, does not apply normally to functions or languages like C. 
Please read Steve Holden's (in chaz') definition, and tell us whether you think 
that Python enforces strongly this principle, I think that would be a good 
basis for an agreement. My answer is no, it doesn't, but it allows you to abide 
by it if you want to. Unlike Java or C++ who would tend to do exactly the 
contrary (enforces it strictly, and (possibly?) allow you to discard it at 
times with a few jiffies (or not? I don't know))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError issue

2013-08-31 Thread Ferrous Cranus

Στις 31/8/2013 2:28 μμ, ο/η Steven D'Aprano έγραψε:

On Sat, 31 Aug 2013 11:31:13 +0300, Ferrous Cranus wrote:


Here is the code inside files.py:


The code you show is not the ENTIRE code inside of files.py, is it? You
are only showing us a small piece, correct?

The code you show:


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



does not contain a call to cur.execute. And here is your error:



[error] [client 108.162.229.116] Traceback (most recent call last):,
referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client
108.162.229.116]   File "/home/nikos/public_html/cgi-bin/files.py", line
135, in , referer: http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116]
cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES
(%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), referer:
http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] NameError:
name 'host' is not defined, referer: http://superhost.gr/



Extracting out the critical part:

line 135
cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES
(%s, %s, %s, %s)''', (filename, host, city, lastvisit) ),

Where is the call to cur.execute in the code snippet you show above?




But 'host' defaults to an ip address if it cannot resolve the hostname.
Why the errro says its undefined?


Because it is is undefined. Python is not lying to you. If Python tells
you there is an error, BELIEVE IT. Resolving the hostname is irrelevant.

print(host)  ### ERROR OCCURS HERE BECAUSE HOST IS UNDEFINED ###
host = socket.gethostbyaddr(addr)

I'm not saying Python is lying of course it does not.

But how is this possible since:


host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or 
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or 
os.environ['REMOTE_ADDR']



it must have a value by defaulting to something.

The cur.execute fails because it make use of 'host' which is undefined.

# Try to insert the file into the database
		cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES 
(%s, %s, %s, %s)''', (filename, host, city, lastvisit) )



And the question remain as to why 'host' is undefined.
--
Webhost 
--
http://mail.python.org/mailman/listinfo/python-list


Re: print function and unwanted trailing space

2013-08-31 Thread candide

Le 31/08/2013 13:16, Steven D'Aprano a écrit :



Of course it does. Have you actually tried it?



Of course I did, redirecting the output to a file in order to spot an 
eventually trailing space. I did the same for the Python 3 code.

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


Re: UnicodeDecodeError issue

2013-08-31 Thread Ferrous Cranus

Στις 31/8/2013 3:58 μμ, ο/η Ferrous Cranus έγραψε:

Στις 31/8/2013 2:28 μμ, ο/η Steven D'Aprano έγραψε:

On Sat, 31 Aug 2013 11:31:13 +0300, Ferrous Cranus wrote:


Here is the code inside files.py:


The code you show is not the ENTIRE code inside of files.py, is it? You
are only showing us a small piece, correct?

The code you show:


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



does not contain a call to cur.execute. And here is your error:



[error] [client 108.162.229.116] Traceback (most recent call last):,
referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client
108.162.229.116]   File "/home/nikos/public_html/cgi-bin/files.py", line
135, in , referer: http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116]
cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES
(%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), referer:
http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] NameError:
name 'host' is not defined, referer: http://superhost.gr/



Extracting out the critical part:

line 135
cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES
(%s, %s, %s, %s)''', (filename, host, city, lastvisit) ),

Where is the call to cur.execute in the code snippet you show above?




But 'host' defaults to an ip address if it cannot resolve the hostname.
Why the errro says its undefined?


Because it is is undefined. Python is not lying to you. If Python tells
you there is an error, BELIEVE IT. Resolving the hostname is irrelevant.

print(host)  ### ERROR OCCURS HERE BECAUSE HOST IS UNDEFINED ###
host = socket.gethostbyaddr(addr)

I'm not saying Python is lying of course it does not.

But how is this possible since:


host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
os.environ['REMOTE_ADDR']


it must have a value by defaulting to something.

The cur.execute fails because it make use of 'host' which is undefined.

 # Try to insert the file into the database
 cur.execute('''INSERT INTO files (url, host, city, lastvisit)
VALUES (%s, %s, %s, %s)''', (filename, host, city, lastvisit) )


And the question remain as to why 'host' is undefined.

Thsi fails too:

host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or 
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or 
"Unkknown host"


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


Re: print function and unwanted trailing space

2013-08-31 Thread candide

Le 31/08/2013 12:31, Peter Otten a écrit :
> softspace = False
> for i in range(5):
>  if softspace:
>  print(end=" ")
>  print(i, end="")
>  softspace = True
> print()


The if instruction imposes useless testing (we know in advance the 
problem to occur at the very end of the loop) and useless writing 
(writing '').


The following is clearer

# -
n=5
for i in range(n-1):
print(i, end=' ')
print(n-1)
# -


but doesn't solve all the cases (imagine a string or an iterator).
--
http://mail.python.org/mailman/listinfo/python-list


Re: print function and unwanted trailing space

2013-08-31 Thread candide

Le 31/08/2013 13:24, Ned Batchelder a écrit :


For a beginner course, the trailing space is fine, use this code.


I was really expecting there was a trick but I'll follow your advice, 
after all the trailing space is invisible!


Nevertheless, this can be quite annoying. For instance, some automated 
program testing (cf. http://www.spoj.com/, http://acm.timus.ru/, etc) 
expect the exact output to get accepted, no byte more or a byte less.






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


Re: print function and unwanted trailing space

2013-08-31 Thread Peter Otten
candide wrote:

> Le 31/08/2013 12:31, Peter Otten a écrit :
>  > softspace = False
>  > for i in range(5):
>  >  if softspace:
>  >  print(end=" ")
>  >  print(i, end="")
>  >  softspace = True
>  > print()
> 
> 
> The if instruction imposes useless testing (we know in advance the
> problem to occur at the very end of the loop) and useless writing
> (writing '').

To make it crystal clear, the above was to illustrate the algorithm used in 
Python 2, not a suggestion. Python 2 uses that "useless testing" -- which is 
cheap compared to actual I/O.

> The following is clearer
> 
> # -
> n=5
> for i in range(n-1):
>  print(i, end=' ')
> print(n-1)
> # -
> 
> 
> but doesn't solve all the cases (imagine a string or an iterator).

I still think you should live with a trailing space or go with my actual 
suggestion

print(*range(5))

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


HMAC encription issue

2013-08-31 Thread sergio7654
Hi, I need to create a HMAC in Python just the way the following Javascript 
code does:

var credentials = "admin:amin";
key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035"
var shaObj = new jsSHA(credentials, "ASCII");
var hash = shaObj.getHMAC(key, "HEX", "HEX"); 



in this case the hash is: 8347ce7126c1068aa183fbfdafe2c17a9cc86734

This javascript library can be tested here: http://caligatio.github.io/jsSHA/


So I'm trying to do the same in Python by:

key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A303$

credentials = "admin:admin"

hash = hmac.new(key, credentials, hashlib.sha1).hexdigest()

-

which gives me hash= 2c7e849a0eaa8cd0cc1120f6f156913755b674b6

Something's is wrong obviously. Maybe it has probably something to do with the 
encoding of the key or credentials but I'm lost on this.

I tried to convert key to hex using the following function

def toHex(s):
lst = []
for ch in s:
hv = hex(ord(ch)).replace('0x', '')
if len(hv) == 1:
hv = '0'+hv
lst.append(hv)

return reduce(lambda x,y:x+y, lst)

But this didn't work either.


Any help would be appreciated!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HMAC encription issue

2013-08-31 Thread Roy Smith
In article <3479e08e-d435-492b-b2a0-a1c18678f...@googlegroups.com>,
 sergio7...@gmail.com wrote:

> key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035"
> key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A303$

To start with, your keys are not the same (the JS one ends in "35", the Python 
one ends in "3$",
and I don't know what to make of the fact that your Python key isn't terminated 
with a quote.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HMAC encription issue

2013-08-31 Thread Sergio Sanchez
El sábado, 31 de agosto de 2013 16:39:08 UTC+2, Roy Smith  escribió:
> In article <3479e08e-d435-492b-b2a0-a1c18678f...@googlegroups.com>,
> 
>  sergio7...@gmail.com wrote:
> 
> 
> 
> > key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035"
> 
> > key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A303$
> 
> 
> 
> To start with, your keys are not the same (the JS one ends in "35", the 
> Python one ends in "3$",
> 
> and I don't know what to make of the fact that your Python key isn't 
> terminated with a quote.


Sorry, that was a problem with the cut & paste. The two sentences 
key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035"
 are just the same in Python and Javascript.
-- 
http://mail.python.org/mailman/listinfo/python-list


MySQLdb Problem

2013-08-31 Thread John Smith
Hi;
Since there is no list for MySQLdb, I'm hoping you can help me. I have 
installed, de-installed and reinstalled this s/w and MySQL itself on my Win8 
box. However, when I go to use it from a script, I get the following error:

"C:\Python27\lib\site-packages\MySQLdb\__init.py__", line 27 in 
import _mysql

ImportError: DLL load failed: %1 is not valid Win32 application. 

Well, I'm running Win8 which is 64-bit, but I'm pretty sure it runs 32 as well, 
right? Python 27, MySQL 5.5.
TIA,
John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HMAC encription issue

2013-08-31 Thread Roy Smith
In article ,
 Sergio Sanchez  wrote:

> > To start with, your keys are not the same
>
> Sorry, that was a problem with the cut & paste.

And your credentials are different too:

var credentials = "admin:amin";
credentials = "admin:admin"

Is this also a cut & paste error?

Please, post EXACTlY the code you are actually running.  Otherwise, it 
really is impossible for anybody to figure out what you're doing wrong.  
This is double ultra especially true with crypto, since small 
differences in the inputs result (by design) in huge differences in the 
output.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySQLdb Problem

2013-08-31 Thread Chris Angelico
On Sat, Aug 31, 2013 at 6:38 PM, John Smith
 wrote:
> Hi;
> Since there is no list for MySQLdb, I'm hoping you can help me. I have 
> installed, de-installed and reinstalled this s/w and MySQL itself on my Win8 
> box. However, when I go to use it from a script, I get the following error:
>
> "C:\Python27\lib\site-packages\MySQLdb\__init.py__", line 27 in 
> import _mysql
>
> ImportError: DLL load failed: %1 is not valid Win32 application.
>
> Well, I'm running Win8 which is 64-bit, but I'm pretty sure it runs 32 as 
> well, right? Python 27, MySQL 5.5.

Do your Python and your MySQLdb match? I haven't confirmed, but I'm
pretty sure you'll have problems if you have a 32-bit Python with a
64-bit mysqldb DLL, or vice versa.

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


Re: HMAC encription issue

2013-08-31 Thread Sergio Sanchez
El sábado, 31 de agosto de 2013 16:48:53 UTC+2, Sergio Sanchez  escribió:
> El sábado, 31 de agosto de 2013 16:39:08 UTC+2, Roy Smith  escribió:
> 
> > In article <3479e08e-d435-492b-b2a0-a1c18678f...@googlegroups.com>,
> 
> > 
> 
> >  sergio7...@gmail.com wrote:
> 
> > 
> 
> > 
> 
> > 
> 
> > > key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035"
> 
> > 
> 
> > > key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A303$
> 
> > 
> 
> > 
> 
> > 
> 
> > To start with, your keys are not the same (the JS one ends in "35", the 
> > Python one ends in "3$",
> 
> > 
> 
> > and I don't know what to make of the fact that your Python key isn't 
> > terminated with a quote.
> 
> 
> 
> 
> 
> Sorry, that was a problem with the cut & paste. The two sentences 
> key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035"
>  are just the same in Python and Javascript.



Solved:

key must be converted to binary with the function unhexlify(hexstr)

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


Re: print function and unwanted trailing space

2013-08-31 Thread Steven D'Aprano
On Sat, 31 Aug 2013 14:59:17 +0200, candide wrote:

> Le 31/08/2013 13:16, Steven D'Aprano a écrit :
> 
> 
>> Of course it does. Have you actually tried it?
> 
> 
> Of course I did, redirecting the output to a file in order to spot an
> eventually trailing space. I did the same for the Python 3 code.


Fair enough. Seems like the print statement implementation in Python 2 is 
uglier than I imagined, keeping hidden state between invocations.



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


Re: print function and unwanted trailing space

2013-08-31 Thread Chris Angelico
On Sat, Aug 31, 2013 at 11:33 PM, candide  wrote:
> The if instruction imposes useless testing (we know in advance the problem
> to occur at the very end of the loop) and useless writing (writing '').
>
> The following is clearer
>
> # -
> n=5
> for i in range(n-1):
> print(i, end=' ')
> print(n-1)
> # -
>
>
> but doesn't solve all the cases (imagine a string or an iterator).

Similar but maybe simpler, and copes with more arbitrary iterables:

it=iter(range(5))
print(next(it), end='')
for i in it:
print('',i, end='')

Also guarantees to use 'sep' between the elements, fwiw.

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


Re: print function and unwanted trailing space

2013-08-31 Thread Oscar Benjamin
On 31 August 2013 16:30, Chris Angelico  wrote:
>>
>> but doesn't solve all the cases (imagine a string or an iterator).
>
> Similar but maybe simpler, and copes with more arbitrary iterables:
>
> it=iter(range(5))
> print(next(it), end='')
> for i in it:
> print('',i, end='')

If you want to work with arbitrary iterables then you'll want

it = iter(iterable)
try:
val = next(it)
except StopIteration:
pass  # Or raise or something?
else:
print(val, end='')
for i in it:
print('', i, end='')


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


Re: UnicodeDecodeError issue

2013-08-31 Thread Steven D'Aprano
On Sat, 31 Aug 2013 15:58:11 +0300, Ferrous Cranus wrote:

Failure is here, line 135:

>> cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES
>> (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ),

[...]
> But how is this possible since:
> 
> 
> host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or
> socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
> os.environ['REMOTE_ADDR']

What's the line number of that line of code? My guess is that it is AFTER 
line 135, which is where the error occurs. Or possibly it is inside a 
function that hasn't been called. Here is the same failure:

print(host)
host = "this is too late"


Here is the same failure again:

if 0:
host = "this never gets called"
print(host)


Here is the same failure again:

data = []
for x in data:
host = "This never happens"
print(host)


And again, a trickier one this time:

host = "something"
del host  # and now it is gone
print(host)


And again:

def func():
global host
print(host)
func()
host = "Too late, the error has already occurred"


One last one:

def set_host():
host = "This is a local variable"
set_host()
print(host)


Study all these examples. Now read your own code. host has not been 
defined at the time the cur.execute line is reached. You have to work out 
which of my examples best matches your code.


> it must have a value by defaulting to something.

No, there is no default value for variables. How long have you been 
programming in Python? Six months? A year?


> The cur.execute fails because it make use of 'host' which is undefined.

Correct.


>   # Try to insert the file into the database cur.execute
('''INSERT INTO
>   files (url, host, city, lastvisit) VALUES
> (%s, %s, %s, %s)''', (filename, host, city, lastvisit) )
> 
> 
> And the question remain as to why 'host' is undefined.

Because you haven't defined it BEFORE you try to use it. There is no 
point defining it AFTER you use it, the error has already occurred.

You have to make the coffee before you drink it:

# This fails too
drink(coffee)
coffee = make_coffee()


# This doesn't help at all
drink(coffee)
coffee = make_coffee() or prepare_coffee()


# This is also useless
drink(coffee)
coffee = make_coffee() or prepare_coffee() or "coffee"


# But this works
coffee = make_coffee()
drink(coffee)



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


Re: print function and unwanted trailing space

2013-08-31 Thread candide

Le 31/08/2013 12:31, Peter Otten a écrit :


with `softspace` saved as a file attribute, is gone in Python3.



After reading

http://docs.python.org/3.0/whatsnew/3.0.html#print-is-a-function


I understand what you meant by "softspace". Thanks.

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


Re: print function and unwanted trailing space

2013-08-31 Thread candide

Le 31/08/2013 15:59, Peter Otten a écrit :




To make it crystal clear, the above was to illustrate the algorithm used in
Python 2, not a suggestion.



Ok sorry, I misinterpreted.




> I still think you should live with a trailing space


Are you sure ? The following code

#--
import io

output = io.StringIO()
n=5
for i in range(n-1):
print(i, end=' ', file=output)
print(n-1, file=output)
print(output.getvalue().count(' '))
#--


outputs 4, the correct number of space character.



> or go with my actual
> suggestion
>
> print(*range(5))
>


It's a very good suggestion (the best one in fact) but rather 
complicated to explain to pure novices in programming.


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


argparse - specify order of argument parsing?

2013-08-31 Thread Eduardo Alvarez
When using argparse, is there a way to specify in what order arguments
get parsed? I am writing a script whose parameters can be modified in
the following order:

Defaults -> config file -> command-line switches.

However, I want to give the option of specifying a config file using a
command line switch as well, so logically, that file should be parsed
before any other arguments are applied. However, it seems that
parse_args() parses arguments in the order they're given, so if the
config file switch is not given first, the config file will overwrite
whatever was in the command-line switches, which should have higher
priority.

Thank you in advance,
-- 
Eduardo Alvarez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-08-31 Thread Tim Delaney
On 1 September 2013 03:31, Dennis Lee Bieber  wrote:

> On Fri, 30 Aug 2013 23:07:47 -0700 (PDT), Fabrice Pombet  >
> declaimed the following:
>
> >well, look at that:
> >
> >a=(1,2)
> >a=2+3 ->a is an object and I have changed its type and value from
> outside. As far as I am concerned this is one hell of an encapsulation
> violation... Could you do this -strictly speaking- in Java or C++?
>
> There is where your major misunderstanding is...
>
> "a" is a NAME attached (bound) to an object. In the first statement, the
> object is the tuple (1,2). That object was not changed when you execute the
> second statement -- which is taking two integer objects and creating a new
> integer object having a value of '5', and then attaches the NAME "a" to the
> new object. If no other names are bound to the (1,2) object, it will be
> garbage collected.
>

I'll try another way to explain it, using Java terminology(since Fabrice
appears to be familiar with Java).

Object a = Arrays.asList(1, 2);  // a is a reference to the List
returned by Arrays.asList
a = Integer.valueOf(2 + 3);  // a is now a reference to the Integer
returned by Integer.valueOf

You have not changed the type of 'a' in any way - you have simply changed
what the name 'a' refers to. This is functionally identical to your Python
code above,except that in Python you do not have to downcast the Object
reference 'a' or use reflection to call methods on it or access it's
members (think of it as Python does reflection automatically for you).

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


Re: argparse - specify order of argument parsing?

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

While I haven't come up with a good solution using argparse/optparse
alone, I've found that it's easier (for processing) to specify the
config file as an environment variable.  So rather than doing

  my_prog.py -c /path/to/config.ini arg1 arg2 ...

I just do

  MY_PROG_CONF=/path/to/config.ini my_prog.py arg1 arg2 ...

...at least on *nix systems; on Win32, it's

  c:\temp> set MY_PROG_CONF=c:\path\to\config.ini
  c:\temp> python my_prog.py arg1 arg2 ...

Then you just intercept the config-file name from os.environ:

  config_file = os.environ.get(
"MY_PROG_CONF",
default_ini_location)

-tkc



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


Re: argparse - specify order of argument parsing?

2013-08-31 Thread Terry Reedy

On 8/31/2013 1:11 PM, Eduardo Alvarez wrote:

When using argparse, is there a way to specify in what order arguments
get parsed?


I expect argparse to forward iterate the sequence of arguments that it 
receives.



I am writing a script whose parameters can be modified in
the following order:

Defaults -> config file -> command-line switches.

However, I want to give the option of specifying a config file using a
command line switch as well, so logically, that file should be parsed
before any other arguments are applied. However, it seems that
parse_args() parses arguments in the order they're given,


Right.

> so if the

config file switch is not given first, the config file will overwrite
whatever was in the command-line switches, which should have higher
priority.


So just document that a config file has to be given first to work as 
expected. Order dependence among arguments is common.


--
Terry Jan Reedy

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


Re: argparse - specify order of argument parsing?

2013-08-31 Thread Terry Reedy

On 8/31/2013 2:13 PM, Terry Reedy wrote:

On 8/31/2013 1:11 PM, Eduardo Alvarez wrote:

When using argparse, is there a way to specify in what order arguments
get parsed?


I expect argparse to forward iterate the sequence of arguments that it
receives.


Aside from the environment variable solution, you could search sys.argv 
for 'config=filename' and remove it and process it *before* you invoke 
argparse.


--
Terry Jan Reedy

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


Re: print function and unwanted trailing space

2013-08-31 Thread Chris Angelico
On Sun, Sep 1, 2013 at 1:43 AM, Oscar Benjamin
 wrote:
> On 31 August 2013 16:30, Chris Angelico  wrote:
>>>
>>> but doesn't solve all the cases (imagine a string or an iterator).
>>
>> Similar but maybe simpler, and copes with more arbitrary iterables:
>>
>> it=iter(range(5))
>> print(next(it), end='')
>> for i in it:
>> print('',i, end='')
>
> If you want to work with arbitrary iterables then you'll want
>
> it = iter(iterable)
> try:
> val = next(it)
> except StopIteration:
> pass  # Or raise or something?
> else:
> print(val, end='')
> for i in it:
> print('', i, end='')

I went with this version:

except StopIteration:
raise

In other words, if it's going to bomb, let it bomb :)

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


Re: MySQLdb Problem

2013-08-31 Thread Chris Angelico
On Sun, Sep 1, 2013 at 8:17 AM, John Smith  wrote:
>
> 
> On Sat, 31/8/13, Chris Angelico  wrote:
>
>  Subject: Re: MySQLdb Problem
>  To: python-list@python.org
>  Date: Saturday, 31 August, 2013, 4:18 PM
>
>> Do your Python and your MySQLdb match? I haven't confirmed,
>> but I'm
>> pretty sure you'll have problems if you have a 32-bit Python
>> with a
>> 64-bit mysqldb DLL, or vice versa.
>
> Yeah, that turned out to be the problem. What's strange is that it worked 
> before.

Possibly you upgraded to a new version of Python (even from 2.7.3 to
2.7.5 or something, which wouldn't otherwise break stuff) and
downloaded the other form of executable? Anyway, glad it's sorted.

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


Re: print function and unwanted trailing space

2013-08-31 Thread Joshua Landau
On 31 August 2013 23:08, Chris Angelico  wrote:
> On Sun, Sep 1, 2013 at 1:43 AM, Oscar Benjamin
>  wrote:
>> On 31 August 2013 16:30, Chris Angelico  wrote:

 but doesn't solve all the cases (imagine a string or an iterator).
>>>
>>> Similar but maybe simpler, and copes with more arbitrary iterables:
>>>
>>> it=iter(range(5))
>>> print(next(it), end='')
>>> for i in it:
>>> print('',i, end='')
>>
>> If you want to work with arbitrary iterables then you'll want
>>
>> it = iter(iterable)
>> try:
>> val = next(it)
>> except StopIteration:
>> pass  # Or raise or something?
>> else:
>> print(val, end='')
>> for i in it:
>> print('', i, end='')
>
> I went with this version:
>
> except StopIteration:
> raise
>
> In other words, if it's going to bomb, let it bomb :)

I think the point is that StopIteration is an unsafe error to raise.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interface and duck typing woes

2013-08-31 Thread Joshua Landau
On 31 August 2013 01:13, Steven D'Aprano
 wrote:
> On Fri, 30 Aug 2013 06:35:47 -0400, Roy Smith wrote:
>
>> In article <52200699$0$6599$c3e8da3$54964...@news.astraweb.com>,
>>  Steven D'Aprano  wrote:
>>
>>> These days, it would be relatively simple to implement pre- and post-
>>> condition checking using decorators, and indeed one of the motivating
>>> use- cases for function annotations in Python 3 is to allow such
>>> things.
>>>
>>> http://www.python.org/dev/peps/pep-3107/
>>>
>>> (Function annotations are perhaps the best Python feature that nobody
>>> uses.)
>>
>> This is awesome.
>
> Heh, everybody has one of two reactions:
>
> "This is awesome!"
>
> "You'll add type checking to my Python code over my dead body!!!"
>
> But I'm still to see a practical use for annotations in real world code.
> Or indeed to think of a use for them other than type checking.

I occasionally use them for documentation. I think that there some are
cases where the return type (encoded as a string) is as good an indicator
of functionality as a short docstring, so use both.
-- 
http://mail.python.org/mailman/listinfo/python-list


How do I process this using Python?

2013-08-31 Thread Anthony Papillion
Hello Everyone,

I'm writing a processor for Bitmessage messages and I am needing to
parse the following returned JSON string:

{u'inboxMessages': [{u'fromAddress':
u'BM-2DBYkhiBZCyrBa8J7gFRGrFRSGqtHgPtMvwQ', u'toAddress':
u'BM-2DC7SCTj2gzgrGgMvUCARdrfrsgLyz3iMyN3', u'read': 0, u'msgid':
u'36659a4453e12a085d8fbfeefc58da8fb23f38bfb0984c2983e0ddc31c776038',
u'receivedTime': u'1377986524', u'message':
u'dGVzdGluZyAxIDIgMw0KDQotLQ0KSm9obiBQZXJyeQ0KDQo=\n', u'encodingType':
2, u'subject': u'bWVzc2FnZSAx\n'}, {u'fromAddress':
u'BM-2DBYkhiBZCyrBa8J7gNBrngtgttHgPtMvwQ', u'toAddress':
u'BM-2DC7SCTj2gzgrGgMvUCARdCrfthyz3iMyN3', u'read': 0, u'msgid':
u'2ebe10c788ed47c6c122e3b43ae6642cb15077536c7056ed5088ab2d339c4630',
u'receivedTime': u'1377986557', u'message':
u'VGhpcyBpcyB0aGUgbmV4dCB0ZXN0DQoNCi0tDQpKb2huIFBlcnJ5DQoNCg==\n',
u'encodingType': 2, u'subject': u'dGVzdGluZyAzIDQgNQ==\n'},
{u'fromAddress': u'BM-2DBYkhithgyhyrBa8J7gNBrnSGqtHgPtMvwQ',
u'toAddress': u'BM-2DC7SCTj2gzgrtgtgMvUCARdCogLyz3iMyN3', u'read': 0,
u'msgid':
u'91dffd421c898aab0ffc43a363869a580abec6fa851aa6cf7cefe98263f96c81',
u'receivedTime': u'1377986599', u'message':
u'VGhpcyBpcyB0aGUgM3JkIHRlc3QNCg0hjj0NCkpvaG4gUGVycnkNCg0K\n',
u'encodingType': 2, u'subject': u'dGhpcyBpcyB0aGUgM3Jk\n'}]}

I tried using the following code:

data = json.loads(api.getAllInboxMessages) # This is the API call

for messageSender in data['inboxMessages']['fromAddress']
print messageSender

For some reason (probably obvious reasons) isn't working. I'm trying to
loop through the JSON and return all of the fromAddress fields.

Can anyone offer suggestions?

Thanks,
Anthony
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I process this using Python?

2013-08-31 Thread Chris Angelico
On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion  wrote:
> I'm writing a processor for Bitmessage messages and I am needing to
> parse the following returned JSON string:
>
> {u'inboxMessages':

Does the JSON string really have those u prefixes and apostrophes?
That's not valid JSON. You may be able to use ast.literal_eval() on it
- I was able to with the example data - but not a JSON parser. Can you
sort out your transmission end?

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


Re: MySQLdb Problem

2013-08-31 Thread John Smith


On Sat, 31/8/13, Chris Angelico  wrote:

 Subject: Re: MySQLdb Problem
 To: python-list@python.org
 Date: Saturday, 31 August, 2013, 4:18 PM
 
> Do your Python and your MySQLdb match? I haven't confirmed,
> but I'm
> pretty sure you'll have problems if you have a 32-bit Python
> with a
> 64-bit mysqldb DLL, or vice versa.

Yeah, that turned out to be the problem. What's strange is that it worked 
before.
Thanks,
John

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


Re: print function and unwanted trailing space

2013-08-31 Thread Terry Reedy

On 8/31/2013 7:15 PM, Joshua Landau wrote:

On 31 August 2013 23:08, Chris Angelico  wrote:

On Sun, Sep 1, 2013 at 1:43 AM, Oscar Benjamin
 wrote:

On 31 August 2013 16:30, Chris Angelico  wrote:


but doesn't solve all the cases (imagine a string or an iterator).


Similar but maybe simpler, and copes with more arbitrary iterables:

it=iter(range(5))
print(next(it), end='')
for i in it:
 print('',i, end='')


If you want to work with arbitrary iterables then you'll want

it = iter(iterable)
try:
 val = next(it)
except StopIteration:
 pass  # Or raise or something?
else:
 print(val, end='')
for i in it:
 print('', i, end='')


I went with this version:

except StopIteration:
 raise

In other words, if it's going to bomb, let it bomb :)


I think the point is that StopIteration is an unsafe error to raise.


It should only be raised by iterator.__next__ method and caught by 
iterator user and not re-raised. Raise something like "ValueError('empty 
iterable') from None" instead.



--
Terry Jan Reedy

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


gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-08-31 Thread anntzer . lee
Hi,

At startup, IPython (qtconsole) calls 
"socket.gethostbyname_ex(socket.gethostname())[2]" to find a list of IP 
addresses that point to the machine. On a Linux server that I manage this call 
is extremely slow (>20s)... which I have trouble understanding as "ip addr 
show" seems to give the same information nearly instantaneously. Is there 
anything I can do to make this faster? Can this be a network configuration 
issue (I am behind a router)?

This issue is independent of IPython:

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

Thanks.

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


Re: argparse - specify order of argument parsing?

2013-08-31 Thread Cameron Simpson
On 31Aug2013 14:17, Terry Reedy  wrote:
| On 8/31/2013 2:13 PM, Terry Reedy wrote:
| >On 8/31/2013 1:11 PM, Eduardo Alvarez wrote:
| >>When using argparse, is there a way to specify in what order arguments
| >>get parsed?
| >
| >I expect argparse to forward iterate the sequence of arguments that it
| >receives.
| 
| Aside from the environment variable solution, you could search
| sys.argv for 'config=filename' and remove it and process it *before*
| you invoke argparse.

Although, speaking for myself, I like options to have effect as encountered:

  some-cmd -a --no-foo

Would turn on "all" options then turn off "foo". Hard to do if you look for
"more important" options and then less important options.

I'm personally against out-of-order option handling; it confuses things for the 
user.
If you (as the user) need the config file to be known first, put it first.

Cheers,
-- 
Cameron Simpson 

You wouldn't... ...but you KNOW you could.  - Original V65 Commercial
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I process this using Python?

2013-08-31 Thread Anthony Papillion
On 08/31/2013 06:48 PM, Chris Angelico wrote:
> On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion  wrote:
>> I'm writing a processor for Bitmessage messages and I am needing to
>> parse the following returned JSON string:
>>
>> {u'inboxMessages':
> 
> Does the JSON string really have those u prefixes and apostrophes?
> That's not valid JSON. You may be able to use ast.literal_eval() on it
> - I was able to with the example data - but not a JSON parser. Can you
> sort out your transmission end?
> 
> ChrisA

I think I remembered what the 'u' prefix is. It indicates that the data
following is a unicode string. So could that be valid JSON data wrapped
up in unicode?

Anthony


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


Re: How do I process this using Python?

2013-08-31 Thread Chris Angelico
On Sun, Sep 1, 2013 at 10:19 AM, Anthony Papillion  wrote:
> On 08/31/2013 06:48 PM, Chris Angelico wrote:
>> On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion  
>> wrote:
>>> I'm writing a processor for Bitmessage messages and I am needing to
>>> parse the following returned JSON string:
>>>
>>> {u'inboxMessages':
>>
>> Does the JSON string really have those u prefixes and apostrophes?
>> That's not valid JSON. You may be able to use ast.literal_eval() on it
>> - I was able to with the example data - but not a JSON parser. Can you
>> sort out your transmission end?
>>
>> ChrisA
>
> I think I remembered what the 'u' prefix is. It indicates that the data
> following is a unicode string. So could that be valid JSON data wrapped
> up in unicode?

No; JSON already supports Unicode. What you may have is an incorrect
JSON encoder that uses Python's repr() to shortcut its work - but it's
wrong JSON.

But bitmessage seems to be written in Python. Can you simply access
the objects it's giving you, rather than going via text strings?

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


Re: How do I process this using Python?

2013-08-31 Thread Cameron Simpson
On 31Aug2013 19:19, Anthony Papillion  wrote:
| On 08/31/2013 06:48 PM, Chris Angelico wrote:
| > On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion  
wrote:
| >> I'm writing a processor for Bitmessage messages and I am needing to
| >> parse the following returned JSON string:
| >>
| >> {u'inboxMessages':
| > 
| > Does the JSON string really have those u prefixes and apostrophes?
| > That's not valid JSON. You may be able to use ast.literal_eval() on it
| > - I was able to with the example data - but not a JSON parser. Can you
| > sort out your transmission end?
| 
| I think I remembered what the 'u' prefix is. It indicates that the data
| following is a unicode string. So could that be valid JSON data wrapped
| up in unicode?

How sure are you that it is JSON? It looks to me like a message
that might once have been JSON, but has already been passed through
json.loads() for you.

What does type(the_json) say? What does repr(the_json) say?
Print both.

I would guess it has already been parsed.

Cheers,
-- 
Cameron Simpson 

Sattinger's Second Law: Even though you've plugged it in, it still won't work
until you switch it on.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I process this using Python?

2013-08-31 Thread Anthony Papillion
On 08/31/2013 07:32 PM, Cameron Simpson wrote:
> On 31Aug2013 19:19, Anthony Papillion  wrote:
> | On 08/31/2013 06:48 PM, Chris Angelico wrote:
> | > On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion  
> wrote:
> | >> I'm writing a processor for Bitmessage messages and I am needing to
> | >> parse the following returned JSON string:
> | >>
> | >> {u'inboxMessages':
> | > 
> | > Does the JSON string really have those u prefixes and apostrophes?
> | > That's not valid JSON. You may be able to use ast.literal_eval() on it
> | > - I was able to with the example data - but not a JSON parser. Can you
> | > sort out your transmission end?
> | 
> | I think I remembered what the 'u' prefix is. It indicates that the data
> | following is a unicode string. So could that be valid JSON data wrapped
> | up in unicode?
> 
> How sure are you that it is JSON? It looks to me like a message
> that might once have been JSON, but has already been passed through
> json.loads() for you.
> 
> What does type(the_json) say? What does repr(the_json) say?
> Print both.
> 
> I would guess it has already been parsed.
> 
> Cheers,

And you would be right! It's actually a dictionary!

Anthony


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


Re: Interface and duck typing woes

2013-08-31 Thread Roy Smith
In article <5221352b$0$6599$c3e8da3$54964...@news.astraweb.com>,
 Steven D'Aprano  wrote:

> Heh, everybody has one of two reactions: 
> 
> "This is awesome!" [[i.e. what I said]]
> 
> "You'll add type checking to my Python code over my dead body!!!"

Duck typing is a funny thing.  Sure, I don't have to give you a Duck, I 
just have to give you something that quacks like a Duck.  But, at some 
point, you and I need to agree on what that means.  If you're expecting 
a https://en.wikipedia.org/wiki/Duck and I give you a 
https://en.wikipedia.org/wiki/DUKW, we've had a failure to communicate.

To take a quasi-realistic example, let's say I've got this musical 
masterpiece in my database:

{
   "_id" : ObjectId("4ccb7052e5f37551d479add6"),
   "album" : "My World",
   "album_comp_id" : NumberLong(34732133),
   "artist" : "Justin Bieber",
   "avail_date_aac" : ISODate("1970-01-01T00:00:00Z"),
   "avail_date_mp3" : ISODate("1970-01-01T00:00:00Z"),
   "duration" : 192,
   "genre" : "Pop",
   "mn_comp_id" : NumberLong(34732147),
   "seq_num" : 1297,
   "song_id" : 6544798,
   "title" : "Love Me",
   "track" : -1
}

If I want to ask you, "Please return to me a url from which I can stream 
this song as an mp3", I could look at your Song class and find:

@staticmethod
def get_stream_url(song):
   [some code goes here]

but that would leave me wondering what you mean by "song".  You could 
legitimately mean 6544798, "4ccb7052e5f37551d479add6", 
ObjectId("4ccb7052e5f37551d479add6"), an instance of the Song class 
itself, or possibly even 34732147.  Depending on the context, any of 
those might very well be the right answer.

And, we haven't even gotten to describing what I should expect to get 
back.

So, to clear things up, you had to go and write something in the doc 
string:

@staticmethod
def get_stream_url(song):
   """Song is an instance of class Song.  This will return
   an absolute URL as a string."""

But, why not just embed that information in some way which is both 
compact and machine readable?

Of course, when I say, "Song is an instance of class Song", what I 
really (in full duck typing glory) mean is, "Song is something which has 
a "mn_comp_id" attribute whose value is something which I can pass to 
str() and get back a properly formatted decimal integer string.  So, 
this would work, wouldn't it?

class Platypus:
def __getattr__(self, name):
return 34732147

duck = Platypus()
Song.get_stream_url(duck)

Hey, it met the description you gave me, didn't it?  And, sure enough, 
if you do with duck what I expect you will, we will soon hear Justin 
Bieber's, "Love Me" coming out the speaker.  But, in reality, I suspect 
we would quickly get into an argument about just what exactly did you 
mean when you said "Duck".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I process this using Python? (SOLVED)

2013-08-31 Thread Anthony Papillion
On 08/31/2013 07:32 PM, Chris Angelico wrote:
> On Sun, Sep 1, 2013 at 10:19 AM, Anthony Papillion  
> wrote:
>> On 08/31/2013 06:48 PM, Chris Angelico wrote:
>>> On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion  
>>> wrote:
 I'm writing a processor for Bitmessage messages and I am needing to
 parse the following returned JSON string:

 {u'inboxMessages':
>>>
>>> Does the JSON string really have those u prefixes and apostrophes?
>>> That's not valid JSON. You may be able to use ast.literal_eval() on it
>>> - I was able to with the example data - but not a JSON parser. Can you
>>> sort out your transmission end?
>>>
>>> ChrisA
>>
>> I think I remembered what the 'u' prefix is. It indicates that the data
>> following is a unicode string. So could that be valid JSON data wrapped
>> up in unicode?
> 
> No; JSON already supports Unicode. What you may have is an incorrect
> JSON encoder that uses Python's repr() to shortcut its work - but it's
> wrong JSON.
> 
> But bitmessage seems to be written in Python. Can you simply access
> the objects it's giving you, rather than going via text strings?
> 
> ChrisA

Once I looked at the data in a better structured way and saw it in the
proper type it became clear. Here is my solution:

data = json.loads(api.getAllInboxMessages())
for message in data['inboxmessages']:
print message['fromAddress']

Yep, it was that simple. Thanks for the help guys! I appreciate how fast
everyone responded.

Anthony


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


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

2013-08-31 Thread Roy Smith
In article ,
 anntzer@gmail.com wrote:

> Hi,
> 
> At startup, IPython (qtconsole) calls 
> "socket.gethostbyname_ex(socket.gethostname())[2]" to find a list of IP 
> addresses that point to the machine. On a Linux server that I manage this 
> call is extremely slow (>20s)... which I have trouble understanding as "ip 
> addr show" seems to give the same information nearly instantaneously. Is 
> there anything I can do to make this faster? Can this be a network 
> configuration issue (I am behind a router)?

It's almost certainly some sort of configuration issue which is causing 
some DNS query to timeout.  The first step, is to figure out which part 
is taking so long.  Open up a python shell and run:

>>> name = socket.gethostname()

see how long that takes and what it returns.  Then, assuming it returns 
a string containing your hostname (massive handwave about what that 
actually means), try

>>> socket.gethostbyname_ex(name)

and see how long that takes and what it returns.  At least at that point 
you'll have cut the problem in half.

If I had to guess, you've got a missing PTR record, because that's what 
most people screw up.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I process this using Python?

2013-08-31 Thread Steven D'Aprano
On Sat, 31 Aug 2013 18:44:04 -0500, Anthony Papillion wrote:

> Hello Everyone,
> 
> I'm writing a processor for Bitmessage messages and I am needing to
> parse the following returned JSON string:
[...]
> For some reason (probably obvious reasons) isn't working. I'm trying to
> loop through the JSON and return all of the fromAddress fields.
> 
> Can anyone offer suggestions?


Yes. Don't assume that "isn't working" is obvious. What do you mean 
"isn't working"? What part isn't working? What does it do? Does it print 
the wrong results, or does it raise an exception? If so, please copy and 
paste the entire traceback, do not simplify it, summarise it, or retype 
it from memory.


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


Not getting my head around pandas

2013-08-31 Thread Roy Smith
I've got a pandas DataFrame that looks like:



Int64Index: 12960 entries, 0 to 12959
Data columns (total 2 columns):
date12960  non-null values
ms  12960  non-null values
dtypes: datetime64[ns](1), float64(1)


 date ms
12955 2013-08-30 23:20:00  96.868491
12956 2013-08-30 23:30:00  96.857826
12957 2013-08-30 23:40:00  92.624406
12958 2013-08-30 23:50:00  85.402094
12959 2013-08-31 00:00:00  93.870912

This is samples taken every 10 minutes going back several months.  I 
want to find the mean and variance for all the points that happened at 
the same time each day.  In other words, I want to group by date.time().

I can certainly do this by pulling all the data out of the DataFrame and 
working on it as normal Python data.  But, I suspect there's some easy 
way to do this in pandas and I'm just not seeing it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best practice for generalizing and documenting each method's behaviour

2013-08-31 Thread Fábio Santos
On 30 Aug 2013 19:07,  wrote:
>
> I'm starting a small project coding in Python as I learn the ropes. As
the project grows bigger, there are more and more overlapping and even
redundant methods. For example, several classes have a
checkAndClean_obj_state() method. If just one or two such classes, it is
easy to analyze the behaviour of them and design the optimal interaction
for all objects. However, when there are many of such classes, exactly at
what point to invoke check and clean behaviour becomes a little blurred.
There is a desperate need for generalizing and documenting the behaviour of
each such class, preferably in a flowchart. I'm currently doing the
flowchart manually but the job becomes a bit overwhelming.
>
> I wonder what Python pros are using for analyzing and documenting
classes/functions behaviours and interactions? Is UML the only way?
Personally I found UML is a bit overkill for a one person project, but I'm
not sure if it is the right direction. I'd appreciate any insight. Many
thanks.

I can't say I have ever used any tool for documenting python automatically.
Human written documentation is always best.

As for graphs and all, YMMV, but I've found that paper is best for
expressing your code structure. I have looked into tools which generate
graphs of your imports, but I found none of them satisfactory. There are a
lot of problems parsing python due to its dynamic nature. In the end I
decided to sit and read my codebase as I drew.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best practice for generalizing and documenting each method's behaviour

2013-08-31 Thread Steven D'Aprano
On Fri, 30 Aug 2013 11:04:28 -0700, niubao56 wrote:

> I'm starting a small project coding in Python as I learn the ropes. As
> the project grows bigger, there are more and more overlapping and even
> redundant methods. For example, several classes have a
> checkAndClean_obj_state() method. If just one or two such classes, it is
> easy to analyze the behaviour of them and design the optimal interaction
> for all objects. However, when there are many of such classes, exactly
> at what point to invoke check and clean behaviour becomes a little
> blurred. There is a desperate need for generalizing and documenting the
> behaviour of each such class, preferably in a flowchart. I'm currently
> doing the flowchart manually but the job becomes a bit overwhelming.

If you are suffering from UML burnout in a *small* project, as you say 
you are working on, then I suspect your program design may be suffering 
from excessive use of classes and Java-itis. Ravioli code is just as bad 
as spaghetti code.

What do you consider "small"?

As far as I am concerned, if it is not *absolutely obvious* at what point 
to invoke check and clean behaviour, then the chances are very high that 
this checkAndClean_obj_state method either does too much, or what it does 
is poorly thought out. For example:

* Why do you not check and clean the object at creation time?

* Why does one method do both "check" and "clean"?

* Once it is checked, how is it possible that it becomes unchecked?

* And similarly for cleaned?

* Is your code controlling a nuclear reactor? If not, then maybe you 
don't actually have to call checkAndClean_obj_state before *every* 
operation, just call it once at creation time and let the chips fall 
where they may. The Python philosophy is that we're all adults here, and 
you can't stop somebody from shooting themselves in the foot.


As for your desperate need to generalise the behaviour of each class, 
don't fall prey to the temptation to over-generalization. Every time you 
think of generalizing something, say to yourself "YAGNI" (You Ain't Gonna 
Need It). You must be your own harshest critic, and demand real benefit 
before generalizing a class, not just "it might be useful some day".

You can always generalize and refactor next month.

Regarding documentation, I never find writing documentation a problem, 
because I nearly always write the documentation *before* the code. How 
else do I know what the code is supposed to do if I haven't written it 
down first? I cannot tell you how many times I've discarded what seemed 
like a good API in my head, because *writing it down* showed me that it 
actually sucked.


> I wonder what Python pros are using for analyzing and documenting
> classes/functions behaviours and interactions? Is UML the only way?
> Personally I found UML is a bit overkill for a one person project, but
> I'm not sure if it is the right direction. I'd appreciate any insight.
> Many thanks.

http://drpaulcarter.blogspot.com.au/2009/03/uml-considered-harmful.html

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

And the perspective of a TDD/XP zealot:

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


Personally, I like English. I prefer to document my code in English, not 
jargon. UML is a language, but it is a jargon language, and while I'm 
sure that some people like it, I do not. I can never remember what all 
the dots and circles and arrows mean. If I was working on a giant project 
with 80 other people and millions of lines of code, perhaps it would be 
worth learning UML for parts of the design. But for a one-man project 
with only thousands of lines of code? I doubt it.


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


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

2013-08-31 Thread anntzer . lee
It is the call to gethostbyname_ex that is very slow.  The call to gethostname 
is quick (and returns the same string as /usr/bin/hostname).

On Saturday, August 31, 2013 6:01:00 PM UTC-7, Roy Smith wrote:
> In article ,
> 
>  anntzer@gmail.com wrote:
> 
> 
> 
> > Hi,
> 
> > 
> 
> > At startup, IPython (qtconsole) calls 
> 
> > "socket.gethostbyname_ex(socket.gethostname())[2]" to find a list of IP 
> 
> > addresses that point to the machine. On a Linux server that I manage this 
> 
> > call is extremely slow (>20s)... which I have trouble understanding as "ip 
> 
> > addr show" seems to give the same information nearly instantaneously. Is 
> 
> > there anything I can do to make this faster? Can this be a network 
> 
> > configuration issue (I am behind a router)?
> 
> 
> 
> It's almost certainly some sort of configuration issue which is causing 
> 
> some DNS query to timeout.  The first step, is to figure out which part 
> 
> is taking so long.  Open up a python shell and run:
> 
> 
> 
> >>> name = socket.gethostname()
> 
> 
> 
> see how long that takes and what it returns.  Then, assuming it returns 
> 
> a string containing your hostname (massive handwave about what that 
> 
> actually means), try
> 
> 
> 
> >>> socket.gethostbyname_ex(name)
> 
> 
> 
> and see how long that takes and what it returns.  At least at that point 
> 
> you'll have cut the problem in half.
> 
> 
> 
> If I had to guess, you've got a missing PTR record, because that's what 
> 
> most people screw up.

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


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

2013-08-31 Thread Michael Torrie
On 08/31/2013 10:51 PM, anntzer@gmail.com wrote:
> It is the call to gethostbyname_ex that is very slow.  The call to
> gethostname is quick (and returns the same string as
> /usr/bin/hostname).

What gethostbyname_ex and /usr/bin/hostname do are very different
things.  gethostbyname_ex does a DNS lookup against a server.
/usr/bin/hostname just checks a local computer setting.  I don't see why
you are comparing the two.  /usr/bin/hostname is not going to help you
find a list of IP addresses that point to a machine.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: argparse - specify order of argument parsing?

2013-08-31 Thread Peter Otten
Eduardo Alvarez wrote:

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

If you use 

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

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

$ cat load_options.py
import argparse

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

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

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

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

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

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


Re: UnicodeDecodeError issue

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

Good morning Steven,

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

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


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

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