Tab-completion in tutorial

2012-01-21 Thread Steven D'Aprano
I'm reading the part of the tutorial that talks about tab-completion, and 
I think the docs are wrong.

http://docs.python.org/tutorial/interactive.html#key-bindings

The "more capable startup file" example given claims:

# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).

but I have tried it, and it doesn't seem to actually bind autocomplete to 
anything.

Is this a documentation bug, or am I doing something wrong?



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


Re: Is a with on open always necessary?

2012-01-21 Thread Lie Ryan

On 01/21/2012 02:44 AM, Andrea Crotti wrote:

I normally didn't bother too much when reading from files, and for example
I always did a

content = open(filename).readlines()

But now I have the doubt that it's not a good idea, does the file
handler stays
open until the interpreter quits?


It is not necessary most of the time, and most likely is not necessary 
for short-lived programs. The file handler stays open until the file 
object is garbage collected, in CPython which uses reference counting 
the file handler is closed when the last reference to the file object is 
deleted or goes out of context; in python implementations that uses 
garbage collection method, this is indeterministic.


It is only strictly necessary for programs that opens thousands of files 
in a short while, since the operating system may limit of the number of 
active file handlers you can have.


However, it is considered best practice to close file handlers; making 
it a habit will avoid problems when you least expect it.


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


Re: Tab-completion in tutorial

2012-01-21 Thread Peter Otten
Steven D'Aprano wrote:

> I'm reading the part of the tutorial that talks about tab-completion, and
> I think the docs are wrong.
> 
> http://docs.python.org/tutorial/interactive.html#key-bindings
> 
> The "more capable startup file" example given claims:
> 
> # Add auto-completion and a stored history file of commands to your Python
> # interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
> # bound to the Esc key by default (you can change it - see readline docs).
> 
> but I have tried it, and it doesn't seem to actually bind autocomplete to
> anything.
> 
> Is this a documentation bug, or am I doing something wrong?

I've just tried it on Kubuntu's konsole. I see strange reactions:
After typing "imp" I have to hit ESC three times before "ort" is added, and 
afterwards a character is swallowed. However, I can get the expected 
behaviour after binding the key explicitly with

>>> import readline
>>> readline.parse_and_bind("esc: complete")

I'm not sure whether Python is to blame or Ubuntu; it may be an interference 
with konsole's key bindings.

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


while True or while 1

2012-01-21 Thread Andrea Crotti

I see sometimes in other people code "while 1" instead of "while True".
I think using True is more pythonic, but I wanted to check if there is
any difference in practice.

So I tried to do the following, and the result is surprising.  For what
I can see it looks like the interpreter can optimize away the 1 boolean
conversion while it doesn't with the True, the opposite of what I
supposed.

Anyone can explain me why is that, or maybe is my conclusion wrong?

  def f1():
  while 1:
  pass

  def f2():
  while True:
  pass

  In [10]: dis.dis(f)
  2   0 SETUP_LOOP   3 (to 6)

  3 >>3 JUMP_ABSOLUTE3
>>6 LOAD_CONST   0 (None)
  9 RETURN_VALUE

  In [9]: dis.dis(f1)
  2   0 SETUP_LOOP  10 (to 13)
>>3 LOAD_GLOBAL  0 (True)
  6 POP_JUMP_IF_FALSE   12

  3   9 JUMP_ABSOLUTE3
>>   12 POP_BLOCK
>>   13 LOAD_CONST   0 (None)
 16 RETURN_VALUE

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


Re: while True or while 1

2012-01-21 Thread Andrea Crotti

Actually there was the same question here (sorry should have looked before)
http://stackoverflow.com/questions/3815359/while-1-vs-for-whiletrue-why-is-there-a-difference

And I think the main reason is that 1 is a constant while True is not
such and can be reassigned.
--
http://mail.python.org/mailman/listinfo/python-list


Re: while True or while 1

2012-01-21 Thread Chris Angelico
On Sun, Jan 22, 2012 at 12:47 AM, Andrea Crotti
 wrote:
> So I tried to do the following, and the result is surprising.  For what
> I can see it looks like the interpreter can optimize away the 1 boolean
> conversion while it doesn't with the True, the opposite of what I
> supposed.
>
> Anyone can explain me why is that, or maybe is my conclusion wrong?

In Python 3, they compile to the same code, because 'True' is a
keyword. In Python 2, you can reassign True to be 0.

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


What's the very simplest way to run some Python from a button on a web page?

2012-01-21 Thread tinnews
I want to run a server side python script when a button on a web page
is clicked.  This is on a LAMP server - apache2 on xubuntu 11.10.

I know I *could* run it as a CGI script but I don't want to change the
web page at all when the button is clicked (I'll see the effect
elsewhere on the screen anyway) so normal CGI isn't ideal.

It's easy to show a button:-

;

Can I get away with something clever for 'something' that will somehow
hook through to a server side script?

Alternatively, seeing as both client and server are on the same
system, this *could* be done on the client side by breaking out of the
browser sandbox - is there any easy way to do this?


I'm just looking for the crudest, simplest possible way of doing this,
it's only for my own convenience to fire up a utility I want to use
when viewing certain of my local HTML pages.  These pages aren't
visible from the outside world so security isn't a big issue.

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


Re: while True or while 1

2012-01-21 Thread Matteo Landi
Probably because of the fact it is possible to set True equal to False and
consequently then invalidate loop logic as presented below:

True = False
while True:
...

On the other hand `1' will always be evaluated as a constant.

Don't know, just guessing.


Matteo

On Jan/21, Andrea Crotti wrote:
> I see sometimes in other people code "while 1" instead of "while True".
> I think using True is more pythonic, but I wanted to check if there is
> any difference in practice.
> 
> So I tried to do the following, and the result is surprising.  For what
> I can see it looks like the interpreter can optimize away the 1 boolean
> conversion while it doesn't with the True, the opposite of what I
> supposed.
> 
> Anyone can explain me why is that, or maybe is my conclusion wrong?
> 
>   def f1():
>   while 1:
>   pass
> 
>   def f2():
>   while True:
>   pass
> 
>   In [10]: dis.dis(f)
>   2   0 SETUP_LOOP   3 (to 6)
> 
>   3 >>3 JUMP_ABSOLUTE3
> >>6 LOAD_CONST   0 (None)
>   9 RETURN_VALUE
> 
>   In [9]: dis.dis(f1)
>   2   0 SETUP_LOOP  10 (to 13)
> >>3 LOAD_GLOBAL  0 (True)
>   6 POP_JUMP_IF_FALSE   12
> 
>   3   9 JUMP_ABSOLUTE3
> >>   12 POP_BLOCK
> >>   13 LOAD_CONST   0 (None)
>  16 RETURN_VALUE
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 

-- 
http://www.matteolandi.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: etree/lxml/XSLT and dynamic stylesheet variables

2012-01-21 Thread Adam Tauno Williams
On Sat, 2012-01-21 at 05:56 +0100, Stefan Behnel wrote:
> Adam Tauno Williams, 20.01.2012 21:38:
> > I'm using etree to perform XSLT transforms, such as -
> > from lxml import etree
> > source = etree.parse(self.rfile)
> > xslt = etree.fromstring(self._xslt)
> > transform = etree.XSLT(xslt)
> > result = transform(source)
> > according to the docs at
> >  I can pass a
> > dictionary of parameters to transform, such as -
> > result = transform(doc_root, **{'non-python-identifier': '5'})
> > Can I pass a dictionary-like object?  That doesn't seem to be working.
> Yes it does, Python copies it into a plain dict at call time.

Ah, I wondered if that was happening.  In which case is supresses all
the magic of my dict subclass.

> > I need to perform dynamic lookup of variables for the stylesheet.
> Different story.
> > I've subclassed dictionary and overloaded [], get, has_key, and in to
> > perform the required lookups; these work in testing. But passing the
> > object to transform doesn't work
> You should make the lookup explicit in your XSLT code using an XPath
> function. See here:
> http://lxml.de/extensions.html

Perfect thanks;  this provides everything I need.  

A stupid test case / example for anyone interested:

from lxml import etree

class MyExt:
def __init__(self, languages):
self._languages = languages


def languagelookup(self, _, arg):
language = self._languages.get(arg)
if not language:
return 'undefined'
return language

extensions = etree.Extension( MyExt(languages={ 'ES': 'Spanish',
'EL': 'Greek', 
'DE': 'German',
'EN': 'English' } ),
  ( 'languagelookup', ), 
  ns='847fe241-df88-45c6-b4a7' )

text = '''
  
  109
  OP
  Revolt Of The Masses
  Jose Ortega y Gasset
  1930
  ES
  
  
  108
  P
  Meditations
  Marcus Aurelius
  EL
  1930
  
  
  425
  OP
  The Communist Manifesto
  Karl Marx
  Friedrich Engels
  DE
  1914
  
  
  507
  POT
  The Cathedral & The Bazaar
  Eric S. Raymond
  199
  
'''

source = etree.fromstring(text)

style ='''http://www.w3.org/1999/XSL/Transform";
   xmlns:ext="847fe241-df88-45c6-b4a7">
   
   
 
 ,"
 
 ","
 
 "
   
'''

xslt = etree.XSLT(etree.XML(style), extensions=extensions)
print xslt(source)

-- 
Adam Tauno Williams 
System Administrator, OpenGroupware Developer, LPI / CNA
Fingerprint 8C08 209A FBE3 C41A DD2F A270 2D17 8FA4 D95E D383


signature.asc
Description: This is a digitally signed message part
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's the very simplest way to run some Python from a button on a web page?

2012-01-21 Thread Ian Kelly
On Sat, Jan 21, 2012 at 7:58 AM,  wrote:

> I want to run a server side python script when a button on a web page
> is clicked.  This is on a LAMP server - apache2 on xubuntu 11.10.
>
> I know I *could* run it as a CGI script but I don't want to change the
> web page at all when the button is clicked (I'll see the effect
> elsewhere on the screen anyway) so normal CGI isn't ideal.
>
> It's easy to show a button:-
>
>;
>
> Can I get away with something clever for 'something' that will somehow
> hook through to a server side script?
>

Yes, use AJAX to make an asynchronous request.  There are several AJAX
toolkits out there that will make this simple.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while True or while 1

2012-01-21 Thread Erik Max Francis

Chris Angelico wrote:

On Sun, Jan 22, 2012 at 12:47 AM, Andrea Crotti
 wrote:

So I tried to do the following, and the result is surprising.  For what
I can see it looks like the interpreter can optimize away the 1 boolean
conversion while it doesn't with the True, the opposite of what I
supposed.

Anyone can explain me why is that, or maybe is my conclusion wrong?


In Python 3, they compile to the same code, because 'True' is a
keyword. In Python 2, you can reassign True to be 0.


Why this should concern anyone, I don't know; someone who's rebound 
`True` or `False` to evaluate to something other than true and false, 
respectively, is only doing so to be difficult (or very foolish).  One 
of the principles of Python programming is that We're All Adults Here, 
so this kind of defensive programming is really superfluous.  In other 
words, yes, it's quite reasonable to assume that (even in Python 2) 
`True` is bound to something which is, in fact, true.


The real reason people still use the `while 1` construct, I would 
imagine, is just inertia or habit, rather than a conscious, defensive 
decision.  If it's the latter, it's a case of being _way_ too defensive.


--
Erik Max Francis && [email protected] && http://www.alcyone.com/max/
 San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Jabber erikmaxfrancis
  Ambition can creep as well as soar.
   -- Edmund Burke
--
http://mail.python.org/mailman/listinfo/python-list


Re: while True or while 1

2012-01-21 Thread Erik Max Francis

Andrea Crotti wrote:

I see sometimes in other people code "while 1" instead of "while True".
I think using True is more pythonic, but I wanted to check if there is
any difference in practice.


No (with the exception of `True` and `False` being rebinable in Python 
2).  The idiomatic `while 1` notation comes from back in the pre-Boolean 
days.  In any reasonably modern implementation, `while True` is more 
self-documenting.  I would imagine the primary reason people still do 
it, any after-the-fact rationalizations aside, is simply habit.


--
Erik Max Francis && [email protected] && http://www.alcyone.com/max/
 San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Jabber erikmaxfrancis
  Ambition can creep as well as soar.
   -- Edmund Burke
--
http://mail.python.org/mailman/listinfo/python-list


Re: while True or while 1

2012-01-21 Thread Chris Angelico
On Sun, Jan 22, 2012 at 8:13 AM, Erik Max Francis  wrote:
> Why this should concern anyone, I don't know; someone who's rebound `True`
> or `False` to evaluate to something other than true and false, respectively,
> is only doing so to be difficult (or very foolish).  One of the principles
> of Python programming is that We're All Adults Here, so this kind of
> defensive programming is really superfluous.  In other words, yes, it's
> quite reasonable to assume that (even in Python 2) `True` is bound to
> something which is, in fact, true.

Yes, but there's no special code in the compiler to handle True - it's
just a name like any other. It finds a token that looks like a name,
so it puts a name lookup into the bytecode.

> The real reason people still use the `while 1` construct, I would imagine,
> is just inertia or habit, rather than a conscious, defensive decision.  If
> it's the latter, it's a case of being _way_ too defensive.

Ehh, 'while 1' is shorter too. I reckon some people are just lazy :)
Or have come from C where 'while (1)' is the normal thing to do.
According to the Eliza Effect, supporting 'while 1' is a Good Thing.

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


Re: Masking a dist package with a copy in my own package

2012-01-21 Thread Sam Simmons
I just installed 2.7... should have done this a while ago. pip finally works!

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


access address from object and vice versa

2012-01-21 Thread Tamer Higazi
Hi people!
I have asked myself the following thing.

How do I access the address of an object and later get the object from
that address ?!

I am heavily interisted.


thank you



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


Re: access address from object and vice versa

2012-01-21 Thread Chris Rebert
On Sat, Jan 21, 2012 at 7:04 PM, Tamer Higazi  wrote:
> Hi people!
> I have asked myself the following thing.
>
> How do I access the address of an object

id(obj) happens to do that in CPython, but it's a mere implementation detail.

> and later get the object from
> that address ?!

Not possible.

> I am heavily interisted.

Why?

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


Re: What's the very simplest way to run some Python from a button on a web page?

2012-01-21 Thread Tim Roberts
[email protected] wrote:
>
>I want to run a server side python script when a button on a web page
>is clicked.  This is on a LAMP server - apache2 on xubuntu 11.10.
>
>I know I *could* run it as a CGI script but I don't want to change the
>web page at all when the button is clicked (I'll see the effect
>elsewhere on the screen anyway) so normal CGI isn't ideal.

It seems what you're after is AJAX.  If you are using a Javascript
framework like jQuery, it's easy to fire off an asynchronous request back
to your server that leaves the existing page alone.  If you aren't, then I
think the easiest method is to use an invisible .  From Javascript,
you can set the "src" property of the  to fire off a request while
leaving the rest of the page alone.

You could spend the rest of your career reading all of the good web
material on AJAX.
-- 
Tim Roberts, [email protected]
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's the very simplest way to run some Python from a button on a web page?

2012-01-21 Thread Chris Angelico
On Sun, Jan 22, 2012 at 3:36 PM, Tim Roberts  wrote:
> It seems what you're after is AJAX.  If you are using a Javascript
> framework like jQuery, it's easy to fire off an asynchronous request back
> to your server that leaves the existing page alone.

If you aren't using a framework, look up the XMLHttpRequest object -
that's what does the work. As Tim says, there's no lack of good
material on the subject.

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


Re: access address from object and vice versa

2012-01-21 Thread Chris Angelico
On Sun, Jan 22, 2012 at 2:04 PM, Tamer Higazi  wrote:
> Hi people!
> I have asked myself the following thing.
>
> How do I access the address of an object and later get the object from
> that address ?!

The problem with that sort of idea is that it mucks up garbage
collection. CPython, for example, maintains a reference count for
every object; your address is, in a sense, another reference, but one
that the GC doesn't know about - so it might release the object and
reuse the memory.

What you can do, though, is simply have another name bound to the same
object. You can then manipulate the object through that name, and
it'll function just like a pointer would in C. The original name and
the new name will function exactly the same.

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


Re: while True or while 1

2012-01-21 Thread Steven D'Aprano
On Sun, 22 Jan 2012 09:13:23 +1100, Chris Angelico wrote:

> On Sun, Jan 22, 2012 at 8:13 AM, Erik Max Francis 
> wrote:
>> Why this should concern anyone, I don't know; someone who's rebound
>> `True` or `False` to evaluate to something other than true and false,
>> respectively, is only doing so to be difficult (or very foolish).  One
>> of the principles of Python programming is that We're All Adults Here,
>> so this kind of defensive programming is really superfluous.  In other
>> words, yes, it's quite reasonable to assume that (even in Python 2)
>> `True` is bound to something which is, in fact, true.
> 
> Yes, but there's no special code in the compiler to handle True - it's
> just a name like any other. It finds a token that looks like a name, so
> it puts a name lookup into the bytecode.
> 
>> The real reason people still use the `while 1` construct, I would
>> imagine, is just inertia or habit, rather than a conscious, defensive
>> decision.  If it's the latter, it's a case of being _way_ too
>> defensive.
> 
> Ehh, 'while 1' is shorter too. I reckon some people are just lazy :) 


Or they've been writing Python code since before version 2.2 when True 
and False were introduced, and so they are used to the "while 1" idiom 
and never lost the habit.

In Python 2, "while 1" is a micro-optimization over "while True", because 
there is no need to look-up the name True. For extremely tight loops, 
that may make a difference.

In Python 3, there is no longer any real difference:


py> dis(compile('while 1: pass', '', 'exec'))
  1   0 SETUP_LOOP   3 (to 6)
>>3 JUMP_ABSOLUTE3
>>6 LOAD_CONST   0 (None)
  9 RETURN_VALUE
py> dis(compile('while True: pass', '', 'exec'))
  1   0 SETUP_LOOP   3 (to 6)
>>3 JUMP_ABSOLUTE3
>>6 LOAD_CONST   0 (None)
  9 RETURN_VALUE


Or perhaps they just like the look of "while 1".



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


Re: access address from object and vice versa

2012-01-21 Thread Steven D'Aprano
On Sun, 22 Jan 2012 04:04:08 +0100, Tamer Higazi wrote:

> Hi people!
> I have asked myself the following thing.
> 
> How do I access the address of an object and later get the object from
> that address ?!

Use another language.

By design, Python does not provide pointers. This is a good thing, 
because it makes a whole class of bugs and security vulnerabilities 
impossible in Python.


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


bufsize in subprocess

2012-01-21 Thread yves

Is this the expected behaviour?
When I run this script, it reads only once, but I expected once per line with 
bufsize=1.


What I am trying to do is display the output of a slow process in a tkinter 
window as it runs. Right now, the process runs to completion, then display the 
result.


import subprocess

com = ['/bin/ls', '-l', '/usr/bin']
with subprocess.Popen(com, bufsize=1, stdout=subprocess.PIPE, 
stderr=subprocess.STDOUT) as proc:

print('out: ' + str(proc.stdout.read(), 'utf8'))


Thanks.

--
Yves.  http://www.SollerS.ca/
   http://ipv6.SollerS.ca
   http://blog.zioup.org/
--
http://mail.python.org/mailman/listinfo/python-list


Re: access address from object and vice versa

2012-01-21 Thread Steven D'Aprano
On Sat, 21 Jan 2012 19:36:32 -0800, Chris Rebert wrote:

> On Sat, Jan 21, 2012 at 7:04 PM, Tamer Higazi 
> wrote:
>> Hi people!
>> I have asked myself the following thing.
>>
>> How do I access the address of an object
> 
> id(obj) happens to do that in CPython, but it's a mere implementation
> detail.

I really wish that CPython didn't expose the fact that id happens to use 
address. That simply gives people the wrong idea.

Jython has the right approach, in my opinion. Objects are given IDs on 
request, starting with 1, and no id is ever re-used:

steve@runes:~$ jython
Jython 2.5.1+ (Release_2_5_1, Aug 4 2010, 07:18:19)
[OpenJDK Client VM (Sun Microsystems Inc.)] on java1.6.0_18
Type "help", "copyright", "credits" or "license" for more information.
>>> id(None)
1
>>> id(True)
2

On the other hand, presumably this means that Jython objects need an 
extra field to store the ID, so the CPython approach is a space 
optimization.



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


Re: bufsize in subprocess

2012-01-21 Thread Chris Rebert
On Sat, Jan 21, 2012 at 9:45 PM,   wrote:
> Is this the expected behavior?

Yes. `.read()` [with no argument] on a file-like object reads until
EOF. See http://docs.python.org/library/stdtypes.html#file.read

> When I run this script, it reads only once, but I expected once per line
> with bufsize=1.

You want proc.stdout.readline().
http://docs.python.org/library/stdtypes.html#file.readline

> What I am trying to do is display the output of a slow process in a tkinter
> window as it runs. Right now, the process runs to completion, then display
> the result.
>
>    import subprocess
>
>    com = ['/bin/ls', '-l', '/usr/bin']
>    with subprocess.Popen(com, bufsize=1, stdout=subprocess.PIPE,
> stderr=subprocess.STDOUT) as proc:
>        print('out: ' + str(proc.stdout.read(), 'utf8'))

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