RE: Remove whitespaces and line breaks in a XML file

2011-02-08 Thread David Vicente
I´ll try to mix it with my code (xml.etree). 
Thanks ;)


-Mensaje original-
De: python-list-bounces+dvicente=full-on-net@python.org
[mailto:python-list-bounces+dvicente=full-on-net@python.org] En nombre
de Josh English
Enviado el: martes, 08 de febrero de 2011 0:46
Para: python-list@python.org
CC: python-list@python.org
Asunto: Re: Remove whitespaces and line breaks in a XML file

I found the code posted at 

http://infix.se/2007/02/06/gentlemen-indent-your-xml

quite helpful in turning my xml into human-readable structures. It works
best for XML-Data.

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

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


Re: Easy function, please help.

2011-02-08 Thread Paul Rudin
Nanderson  writes:


> loop would be infinite. I get what is happening in the function, and I
> understand why this would work, but for some reason it's confusing me
> as to how it is exiting the loop after a certain number of times. Help
> is appreciated, thanks.

It works because 0 tests false and because integer division yields
integers... eventually you'll get something like 1/10 giving 0.

It's not necessarily a good thing to rely on. For example if you try it
after "from __future__ import division" - or in python 3 - you'll get a
float as the result of the division and it won't test False.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Easy function, please help.

2011-02-08 Thread Nanderson
On Feb 8, 10:29 pm, Michael Hrivnak  wrote:
> Your function only works if n is an integer.  Example:
>
> >>> num_digits(234)
> 3
> >>> num_digits(23.4)
>
> 325
>
> When doing integer division, python will throw away the remainder and
> return an int.  Using your example of n==44, 44/10 == 4 and 4/10 == 0
>
> Before each iteration of the while loop, the given expression (in this
> case just n) is evaluated as a boolean.  Your function would act the
> same if it looked like this:
>
> def num_digits(n):
>   count = 0
>   while bool(n):
>       count = count + 1
>       n = n / 10
>   return count
>
> 0 of course evaluates to False as a boolean, which is why the while loop 
> stops.
>
> Just for kicks, this function would work about as well:
>
> def num_digits(n):
>    return len(str(n))
>
> And if either of these were a real function you planned to use, you'd
> probably want to either cast n as an int ( int(n) ) or at least check
> its type:
>
> if not isinstance(n, int):
>    raise TypeError("WTF you didn't pass me an int")
>
> Michael
>
> On Wed, Feb 9, 2011 at 12:52 AM, Nanderson
>
>
>
>
>
>
>
>  wrote:
> > def num_digits(n):
> >    count = 0
> >    while n:
> >        count = count + 1
> >        n = n / 10
> >    return count
>
> > This is a function that basically says how many digits are in a
> > number. For example,
> print num_digits(44)
> > 2
> print num_digits(7654)
> > 4
>
> > This function counts the number of decimal digits in a positive
> > integer expressed in decimal format. I get this function ALMOST
> > completely. The only thing I don't understand is why it eventually
> > exits the loop, and goes off to the second branch. "while n" is
> > confusing me. What I am thinking is that if someone puts "while n" the
> > loop would be infinite. I get what is happening in the function, and I
> > understand why this would work, but for some reason it's confusing me
> > as to how it is exiting the loop after a certain number of times. Help
> > is appreciated, thanks.
> > --
> >http://mail.python.org/mailman/listinfo/python-list

Ah, thank you very much! I forgot completely about how python does
division, do this confused me quite a bit. Thanks for the help, and
the only reason I wrote the function this way is because I'm learning
to program, and I'm try to do the exercises of the tutorial I'm
reading. It really is for learning purposes. Thanks again!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Easy function, please help.

2011-02-08 Thread Michael Hrivnak
Your function only works if n is an integer.  Example:

>>> num_digits(234)
3
>>> num_digits(23.4)
325

When doing integer division, python will throw away the remainder and
return an int.  Using your example of n==44, 44/10 == 4 and 4/10 == 0

Before each iteration of the while loop, the given expression (in this
case just n) is evaluated as a boolean.  Your function would act the
same if it looked like this:

def num_digits(n):
  count = 0
  while bool(n):
  count = count + 1
  n = n / 10
  return count

0 of course evaluates to False as a boolean, which is why the while loop stops.

Just for kicks, this function would work about as well:

def num_digits(n):
   return len(str(n))

And if either of these were a real function you planned to use, you'd
probably want to either cast n as an int ( int(n) ) or at least check
its type:

if not isinstance(n, int):
   raise TypeError("WTF you didn't pass me an int")

Michael

On Wed, Feb 9, 2011 at 12:52 AM, Nanderson
 wrote:
> def num_digits(n):
>    count = 0
>    while n:
>        count = count + 1
>        n = n / 10
>    return count
>
> This is a function that basically says how many digits are in a
> number. For example,
print num_digits(44)
> 2
print num_digits(7654)
> 4
>
> This function counts the number of decimal digits in a positive
> integer expressed in decimal format. I get this function ALMOST
> completely. The only thing I don't understand is why it eventually
> exits the loop, and goes off to the second branch. "while n" is
> confusing me. What I am thinking is that if someone puts "while n" the
> loop would be infinite. I get what is happening in the function, and I
> understand why this would work, but for some reason it's confusing me
> as to how it is exiting the loop after a certain number of times. Help
> is appreciated, thanks.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Easy function, please help.

2011-02-08 Thread Nanderson
def num_digits(n):
count = 0
while n:
count = count + 1
n = n / 10
return count

This is a function that basically says how many digits are in a
number. For example,
>>>print num_digits(44)
2
>>>print num_digits(7654)
4

This function counts the number of decimal digits in a positive
integer expressed in decimal format. I get this function ALMOST
completely. The only thing I don't understand is why it eventually
exits the loop, and goes off to the second branch. "while n" is
confusing me. What I am thinking is that if someone puts "while n" the
loop would be infinite. I get what is happening in the function, and I
understand why this would work, but for some reason it's confusing me
as to how it is exiting the loop after a certain number of times. Help
is appreciated, thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reassign or discard Popen().stdout from a server process

2011-02-08 Thread Nobody
On Fri, 04 Feb 2011 15:48:55 +, John O'Hagan wrote:

> But I'm still a little curious as to why even unsuccessfully attempting to
> reassign stdout seems to stop the pipe buffer from filling up.

It doesn't. If the server continues to run, then it's ignoring/handling
both SIGPIPE and the EPIPE error. Either that, or another process has the
read end of the pipe open (so no SIGPIPE/EPIPE), and the server is using
non-blocking I/O or select() so that it doesn't block writing its
diagnostic messages.

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


Re: Wing in mod_python vs wsgi?

2011-02-08 Thread Doug Epling
I don't know about your IDE, I am using the default IDLE just because it 
is handy.  But I have made the switch from mod_python.  It was a good 
idea, but mod_wsgi is a better idea.  And as you know, mod_python is no 
longer supported.


I am running Apache with mod_wsgi in a windows 7 environment -- I am 
working on implementing Pylons.


Also, I have another Apache server with mod_wsgi serving a MoinMoin 
wiki.  This one is on a good os -- Fedora.


On 2/8/2011 6:57 PM, Tom Stambaugh wrote:

I'm still using mod_python to deploy my framework for production (CentOS
running Python 2.5.5, Apache 2.2.3, mod_python 3.3.1). I'm acutely aware
of how elderly mod_python is, and I've had some frustrations using Wing
to debug inside it -- at least its possible, which is not true for any
other Python IDE I've tried.

Does Wing do better in mod_wsgi? Is it time for me to migrate from
mod_python to mod_wsgi?

Has anybody tried to do this (mod_wsgi and apache) in a Windoze
environment?

Thx,
Tom S.


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


Re: [python-list] - what do you think ?

2011-02-08 Thread Vivek Shrivastava
I think that will help people like me who just depend on gmail, and I know
there are various ways to do filtering in gmail, but just addition of the
tag in [ ] will help avoiding those.. I kindly second that...

On Tue, Feb 8, 2011 at 6:12 PM, Rhodri James wrote:

> On Wed, 09 Feb 2011 01:59:46 -, David Robinow 
> wrote:
>
>  On Tue, Feb 8, 2011 at 5:03 PM, Rhodri James
>>  wrote:
>>
>>> On Tue, 08 Feb 2011 12:18:50 -,  wrote:
>>>
 On Tue, Feb 08, 2011 at 10:16:42PM +1100, Ben Finney wrote:

> Either your mail client already knows how to filter messages
> appropriately depending on which mailing list they came from; or, you
> should use a better mail client.
>

 mutt is quite good ;-)

>>>
>>> Definitely a step up from Outlook :-)
>>>
>> Not sure what your beef with Outlook is, but it does know how to
>>
>> filter messages appropriately.
>> [I've never used mutt.]
>>
>
> Nor have I, but according to the manual it can handle mailing lists
> perfectly well.  mutt has been around for a while.
>
>
> --
> Rhodri James *-* Wildebeest Herder to the Masses
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python subprocesses experience mysterious delay in receiving stdin EOF

2011-02-08 Thread MRAB

On 09/02/2011 01:59, Yang Zhang wrote:

I reduced a problem I was seeing in my application down into the
following test case. In this code, a parent process concurrently
spawns 2 (you can spawn more) subprocesses that read a big message
from the parent over stdin, sleep for 5 seconds, and write something
back. However, there's unexpected waiting happening somewhere, causing
the code to complete in 10 seconds instead of the expected 5.

If you set `verbose=True`, you can see that the straggling subprocess
is receiving most of the messages, then waiting for the last chunk of
3 chars---it's not detecting that the pipe has been closed.
Furthermore, if I simply don't do anything with the second process
(`doreturn=True`), the first process will *never* see the EOF.

Any ideas what's happening? Further down is some example output.
Thanks in advance.

 from subprocess import *
 from threading import *
 from time import *
 from traceback import *
 import sys
 verbose = False
 doreturn = False
 msg = (20*4096+3)*'a'
 def elapsed(): return '%7.3f' % (time() - start)
 if sys.argv[1:]:
   start = float(sys.argv[2])
   if verbose:
 for chunk in iter(lambda: sys.stdin.read(4096), ''):
   print>>  sys.stderr, '..', time(), sys.argv[1], 'read', len(chunk)
   else:
 sys.stdin.read()
   print>>  sys.stderr, elapsed(), '..', sys.argv[1], 'done reading'
   sleep(5)
   print msg
 else:
   start = time()
   def go(i):
 print elapsed(), i, 'starting'
 p = Popen(['python','stuckproc.py',str(i), str(start)],
stdin=PIPE, stdout=PIPE)
 if doreturn and i == 1: return
 print elapsed(), i, 'writing'
 p.stdin.write(msg)
 print elapsed(), i, 'closing'
 p.stdin.close()
 print elapsed(), i, 'reading'
 p.stdout.read()
 print elapsed(), i, 'done'
   ts = [Thread(target=go, args=(i,)) for i in xrange(2)]
   for t in ts: t.start()
   for t in ts: t.join()

Example output:

   0.001 0 starting
   0.003 1 starting
   0.005 0 writing
   0.016 1 writing
   0.093 0 closing
   0.093 0 reading
   0.094 1 closing
   0.094 1 reading
   0.098 .. 1 done reading
   5.103 1 done
   5.108 .. 0 done reading
  10.113 0 done


I changed 'python' to the path of python.exe and 'stuckproc.py' to its
full path and tried it with Python 2.7 on Windows XP Pro. It worked as
expected.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Socket connection between python and C

2011-02-08 Thread Dan Stromberg
On Tue, Feb 8, 2011 at 5:41 PM, Roy Smith  wrote:
> In article ,
>  "Williamson, Ross X. (Guest)" 
>  wrote:
>
>> Dear All,
>>
>> I'm trying to implement a server/client system where the server is written in
>> python and the client has to be written in c/c++.  I can happily send simple
>> text through the socket. Ideally I would like make say a struct (using python
>> struct library) - and then read that in using C. Is there a better way to
>> package data on the server in python to send down a socket to a C client?
>> XML? Pickle?
>
> Depends on what you are trying to accomplish.
>
> If your goal is for the communication to be as efficient as possible,
> sending raw packed binary with the struct module on the python side, and
> casting the i/o buffer pointer to a struct pointer on the C/C++ side
> probably can't be beat.  The downside is you need to worry about
> low-level things like padding, overflow, and endian-ness yourself.

Yes, this is fast, and yes, this likely won't be a good long-term
option if you envision someday using even slightly exotic (or new)
hardware - even using a different compiler on the same hardware could
lead to troubles with this approach.

However, socket.htons and related functions are a pretty good (and
somewhat similar, though without most of the problems) option.

> If you want to give up a little bit of efficiency in return for a huge
> amount of convenience, I'd go with JSON.  For the kinds of things you
> might be thinking about using the struct module for, it's just peachy.
> Hugely portable (libraries for every language imaginable), easy to use,
> and not grossly inefficient.  There's also BSON, which will be a bit
> more efficient at the cost of some portability.

JSON's cool.

> Based on your statement that you're thinking of using "struct", my guess
> is that XML would be overkill.

Yes, XML's a bit more heavyweight than JSON.

Also, if your data need not ever become hierarchical (which is another
architectural choice that could paint you into a corner a bit), or you
are prepared to add framing to your protocol if/when the time comes,
you can just use ASCII.  This has the benefit of allowing you to test
your server with telnet.

Actually, JSON and XML should preserve telnet-based server testing as
well, at least to some extent - you might have to cut and paste your
tests more though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [python-list] - what do you think ?

2011-02-08 Thread Rhodri James
On Wed, 09 Feb 2011 01:59:46 -, David Robinow   
wrote:



On Tue, Feb 8, 2011 at 5:03 PM, Rhodri James
 wrote:

On Tue, 08 Feb 2011 12:18:50 -,  wrote:

On Tue, Feb 08, 2011 at 10:16:42PM +1100, Ben Finney wrote:

Either your mail client already knows how to filter messages
appropriately depending on which mailing list they came from; or, you
should use a better mail client.


mutt is quite good ;-)


Definitely a step up from Outlook :-)

Not sure what your beef with Outlook is, but it does know how to
filter messages appropriately.
[I've never used mutt.]


Nor have I, but according to the manual it can handle mailing lists
perfectly well.  mutt has been around for a while.

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: [python-list] - what do you think ?

2011-02-08 Thread David Robinow
On Tue, Feb 8, 2011 at 5:03 PM, Rhodri James
 wrote:
> On Tue, 08 Feb 2011 12:18:50 -,  wrote:
>> On Tue, Feb 08, 2011 at 10:16:42PM +1100, Ben Finney wrote:
>>> Either your mail client already knows how to filter messages
>>> appropriately depending on which mailing list they came from; or, you
>>> should use a better mail client.
>>
>> mutt is quite good ;-)
>
> Definitely a step up from Outlook :-)
Not sure what your beef with Outlook is, but it does know how to
filter messages appropriately.
[I've never used mutt.]
-- 
http://mail.python.org/mailman/listinfo/python-list


Python subprocesses experience mysterious delay in receiving stdin EOF

2011-02-08 Thread Yang Zhang
I reduced a problem I was seeing in my application down into the
following test case. In this code, a parent process concurrently
spawns 2 (you can spawn more) subprocesses that read a big message
from the parent over stdin, sleep for 5 seconds, and write something
back. However, there's unexpected waiting happening somewhere, causing
the code to complete in 10 seconds instead of the expected 5.

If you set `verbose=True`, you can see that the straggling subprocess
is receiving most of the messages, then waiting for the last chunk of
3 chars---it's not detecting that the pipe has been closed.
Furthermore, if I simply don't do anything with the second process
(`doreturn=True`), the first process will *never* see the EOF.

Any ideas what's happening? Further down is some example output.
Thanks in advance.

from subprocess import *
from threading import *
from time import *
from traceback import *
import sys
verbose = False
doreturn = False
msg = (20*4096+3)*'a'
def elapsed(): return '%7.3f' % (time() - start)
if sys.argv[1:]:
  start = float(sys.argv[2])
  if verbose:
for chunk in iter(lambda: sys.stdin.read(4096), ''):
  print >> sys.stderr, '..', time(), sys.argv[1], 'read', len(chunk)
  else:
sys.stdin.read()
  print >> sys.stderr, elapsed(), '..', sys.argv[1], 'done reading'
  sleep(5)
  print msg
else:
  start = time()
  def go(i):
print elapsed(), i, 'starting'
p = Popen(['python','stuckproc.py',str(i), str(start)],
stdin=PIPE, stdout=PIPE)
if doreturn and i == 1: return
print elapsed(), i, 'writing'
p.stdin.write(msg)
print elapsed(), i, 'closing'
p.stdin.close()
print elapsed(), i, 'reading'
p.stdout.read()
print elapsed(), i, 'done'
  ts = [Thread(target=go, args=(i,)) for i in xrange(2)]
  for t in ts: t.start()
  for t in ts: t.join()

Example output:

  0.001 0 starting
  0.003 1 starting
  0.005 0 writing
  0.016 1 writing
  0.093 0 closing
  0.093 0 reading
  0.094 1 closing
  0.094 1 reading
  0.098 .. 1 done reading
  5.103 1 done
  5.108 .. 0 done reading
 10.113 0 done


-- 
Yang Zhang
http://yz.mit.edu/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket connection between python and C

2011-02-08 Thread Roy Smith
In article ,
 "Williamson, Ross X. (Guest)"  
 wrote:

> Dear All,
> 
> I'm trying to implement a server/client system where the server is written in 
> python and the client has to be written in c/c++.  I can happily send simple 
> text through the socket. Ideally I would like make say a struct (using python 
> struct library) - and then read that in using C. Is there a better way to 
> package data on the server in python to send down a socket to a C client? 
> XML? Pickle?

Depends on what you are trying to accomplish.

If your goal is for the communication to be as efficient as possible, 
sending raw packed binary with the struct module on the python side, and 
casting the i/o buffer pointer to a struct pointer on the C/C++ side 
probably can't be beat.  The downside is you need to worry about 
low-level things like padding, overflow, and endian-ness yourself.

If you want to give up a little bit of efficiency in return for a huge 
amount of convenience, I'd go with JSON.  For the kinds of things you 
might be thinking about using the struct module for, it's just peachy.  
Hugely portable (libraries for every language imaginable), easy to use, 
and not grossly inefficient.  There's also BSON, which will be a bit 
more efficient at the cost of some portability.

Based on your statement that you're thinking of using "struct", my guess 
is that XML would be overkill.
-- 
http://mail.python.org/mailman/listinfo/python-list


Socket connection between python and C

2011-02-08 Thread Williamson, Ross X. (Guest)
Dear All,

I'm trying to implement a server/client system where the server is written in 
python and the client has to be written in c/c++.  I can happily send simple 
text through the socket. Ideally I would like make say a struct (using python 
struct library) - and then read that in using C. Is there a better way to 
package data on the server in python to send down a socket to a C client? XML? 
Pickle?

Cheers,

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


Re: Idea for removing the GIL...

2011-02-08 Thread Jean-Paul Calderone
On Feb 8, 7:12 pm, Paul Rubin  wrote:
> But the refcount scheme is just an implementation hack
> that gets rationalized way too much.  I hope PyPy abandons it.

Done. :)

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


Re: Idea for removing the GIL...

2011-02-08 Thread Paul Rubin
sturlamolden  writes:
> comes with a cost. The interpreter will sometimes pause to collect
> garbage. The memory use will be larger as well, as garbage remain
> uncollected for a while and is not immediately reclaimed. Many rely on
> CPython because the interpreter does not pause and a Python process
> has a small fingerprint. 

We've had that discussion before: CPython's refcount scheme can also
pause (if the last reference to a large structure is released), CPython
has its own gc for cyclic structure with its own pauses, and Python is
fairly memory hungry compared to plenty of small Lisp systems or even
something like J2ME.  Python has many nice qualities which is why I use
it every day.  But the refcount scheme is just an implementation hack
that gets rationalized way too much.  I hope PyPy abandons it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to Write grep in Emacs Lisp (tutorial)

2011-02-08 Thread Icarus Sparry
On Tue, 08 Feb 2011 14:30:53 -0800, Xah Lee wrote:

> On Feb 8, 9:32 am, Icarus Sparry  wrote:
[snip]
>> The 'modern' way to do this is
>> find . -maxdepth 2 -name '*.html' -exec grep whatever {} +
>>
>> The key thing which makes this 'modern' is the '+' at the end of the
>> command, rather than '\;'. This causes find to execute the grep once
>> per group of files, rather than once per file.
> 
> Nice. When was the + introduced?

Years ago! The posix spec for find lists it in the page which has a 
copyright of 2001-2004.

http://pubs.opengroup.org/onlinepubs/009695399/utilities/find.html

Using google, I have come up with this reference from 2001

https://www.opengroup.org/sophocles/show_mail.tpl?
CALLER=show_archive.tpl&source=L&listname=austin-group-l&id=3067

in which David Korn reports writing the code in 1987.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Idea for removing the GIL...

2011-02-08 Thread sturlamolden
On 8 Feb, 10:39, Vishal  wrote:

> Is it possible that the Python process, creates copies of the
> interpreter for each thread that is launched, and some how the thread
> is bound to its own interpreter ?


In .NET lingo this is called an 'AppDomain'. This is also how tcl
works -- one interpreter per thread. I once had a mock-up of that
using ctypes and Python'c C API. However, the problem with 'app
domains' is that OS handles are global to the process. To make OS
handles private, the easiest solution is to use multiple processes,
which incidentally is what the 'multiprocessing' modules does (or just
os.fork if you are on Unix).

Most people would not consider 'app domains' to be a true GIL-free
Python, but rather think of free threading comparable to .NET, Java
and C++. However, removing the GIL will do no good as long as CPython
uses reference counting. Any access to reference counts must be atomic
(e.g. requiring a mutex or spinlock). Here we can imagine using fine-
grained locking instead of a global interpreter lock. There is a
second problem, which might not be as obvious: In parallel computing
there is something called 'false sharing', which in this case will be
incurred on the reference counts. That is, any updating will dirty the
cache lines everywhere; all processors must stop whatever they are
doing to synchronize cache with RAM. This 'false sharing' will put the
scalability down the drain.

To make a GIL free Python, we must start by removing reference
counting in favour of a generational garbage collector. That also
comes with a cost. The interpreter will sometimes pause to collect
garbage. The memory use will be larger as well, as garbage remain
uncollected for a while and is not immediately reclaimed. Many rely on
CPython because the interpreter does not pause and a Python process
has a small fingerprint. If we change this, we have 'yet another
Java'. There are already IronPython and Jython for those who want
this.


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


Wing in mod_python vs wsgi?

2011-02-08 Thread Tom Stambaugh
I'm still using mod_python to deploy my framework for production (CentOS 
running Python 2.5.5, Apache 2.2.3, mod_python 3.3.1). I'm acutely aware 
of how elderly mod_python is, and I've had some frustrations using Wing 
to debug inside it -- at least its possible, which is not true for any 
other Python IDE I've tried.


Does Wing do better in mod_wsgi? Is it time for me to migrate from 
mod_python to mod_wsgi?


Has anybody tried to do this (mod_wsgi and apache) in a Windoze environment?

Thx,
Tom S.
--
Tom Stambaugh
63 Boston Ave
Somerville, MA 02144
617-776-8934 (land)
617-721-0446 (cell)
--
http://mail.python.org/mailman/listinfo/python-list


Re: SunLisp III: Lisp jobs and beer in Ft Lauderdale

2011-02-08 Thread fortunatus
Are you using your qooxlisp thingy?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to Write grep in Emacs Lisp (tutorial)

2011-02-08 Thread Xah Lee
On Feb 8, 9:32 am, Icarus Sparry  wrote:
> On Tue, 08 Feb 2011 13:51:54 +0100, Petter Gustad wrote:
> > Xah Lee  writes:
>
> >> problem with find xargs is that they spawn grep for each file, which
> >> becomes too slow to be usable.
>
> > find . -maxdepth 2 -name '*.html -print0 | xargs -0 grep whatever
>
> > will call grep with a list of filenames given by find, only a single
> > grep process will run.
>
> > //Petter
>
> This is getting off-topic for the listed newsgroups and into
> comp.unix.shell (although the question was originally posed in a MS
> windows context).
>
> The 'modern' way to do this is
> find . -maxdepth 2 -name '*.html' -exec grep whatever {} +
>
> The key thing which makes this 'modern' is the '+' at the end of the
> command, rather than '\;'. This causes find to execute the grep once per
> group of files, rather than once per file.

Nice. When was the + introduced?

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


Re: frequency of values in a field

2011-02-08 Thread Vlastimil Brom
2011/2/8, Paul Rubin :
> noydb  writes:
>> I am looking for ways to go about capturing the frequency of unique
>> values in one field in a dbf table which contains ~50k records.  The
>> values are numbers with atleast 5 digits to the right of the decimal,
>> but I want the frequency of values to only 2 decimal places.  I do
>> have a method to do this courtesy of a provided tool in ArcGIS.  Was
>> just curious about ways to do it without arcgis sw, using just python.
>...
>
> from decimal import Decimal as D
> from collections import defaultdict
>
> records = ['3.14159','2.71828','3.142857']
>
> td = defaultdict(int)
> for x in records:
> td[D(x).quantize(D('0.01'))] += 1
>
> print td
>
>>...

Another variant of the above code using collections.Counter (in newer
python versions);
The actual frequency counting code is actually the single
instantiation of the Counter  from an iterable. The appropriate
handling of the number values might be tweaked as needed.

>>> from decimal import Decimal as D
>>> from collections import Counter
>>> records = ['3.14159','2.71828','3.142857']
>>> Counter(D(x).quantize(D('0.01')) for x in records)
Counter({Decimal('3.14'): 2, Decimal('2.72'): 1})
>>>

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


Re: [python-list] - what do you think ?

2011-02-08 Thread Grant Edwards
On 2011-02-08, przemol...@poczta.fm  wrote:

> I have just subscribed to this python-list@ and this is my N list.
> Usually many mailing lists use square brackets to identify its name
> when you have e-mails from different forums.
> Would you consider adding [] to this list also ?

Please don't.

 1) It's redundant.  There are already header lines to identify the
fact that the message is from the python mailing list.

 2) A lot of subscribers read the list in its own mailbox or newsgroup
(e.g. via gmane.org), so identifying the list in the subject would
just be a waste of pixels.

 3) If you want to add stuff to the subject line for your own use,
it's trivial with something like procmail.

-- 
Grant Edwards   grant.b.edwardsYow! LOOK!!  Sullen
  at   American teens wearing
  gmail.comMADRAS shorts and "Flock of
   Seagulls" HAIRCUTS!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [python-list] - what do you think ?

2011-02-08 Thread Rhodri James

On Tue, 08 Feb 2011 12:18:50 -,  wrote:


On Tue, Feb 08, 2011 at 10:16:42PM +1100, Ben Finney wrote:

przemol...@poczta.fm writes:

> I have just subscribed to this python-list@ and this is my N list.
> Usually many mailing lists use square brackets to identify its name
> when you have e-mails from different forums.
> Would you consider adding [] to this list also ?

No thank you.

Either your mail client already knows how to filter messages
appropriately depending on which mailing list they came from; or, you
should use a better mail client.


mutt is quite good ;-)


Definitely a step up from Outlook :-)


Either way, please don't ask for the subject lines to be munged.


Any technical reason why not ?


python-list reflects and is reflected by the comp.lang.python newsgroup  
(and gmane, etc).  People on the newsgroup side won't add [tags] for you,  
and won't thank you for breaking the minimal threading that can be  
inferred from titles if the list reflector adds stuff unexpectedly.


--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: [python-list] - what do you think ?

2011-02-08 Thread Ben Finney
przemol...@poczta.fm writes:

> On Tue, Feb 08, 2011 at 10:16:42PM +1100, Ben Finney wrote:
> > Either way, please don't ask for the subject lines to be munged.
>
> Any technical reason why not ?

No technical reason to my knowledge; but then, I haven't looked for one.
The reason of “don't mess with it if it isn't broken” is sufficient.

-- 
 \  “If you ever reach total enlightenment while you're drinking a |
  `\  beer, I bet it makes beer shoot out your nose.” —Jack Handey |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: frequency of values in a field

2011-02-08 Thread Paul Rubin
noydb  writes:
> I am looking for ways to go about capturing the frequency of unique
> values in one field in a dbf table which contains ~50k records.  The
> values are numbers with atleast 5 digits to the right of the decimal,
> but I want the frequency of values to only 2 decimal places.  I do
> have a method to do this courtesy of a provided tool in ArcGIS.  Was
> just curious about ways to do it without arcgis sw, using just python.

The Decimal module is pretty slow but is conceptually probably the right
way to do this.  With just 50k records it shouldn't be too bad.  With
more records you might look for a faster way.

from decimal import Decimal as D
from collections import defaultdict

records = ['3.14159','2.71828','3.142857']

td = defaultdict(int)
for x in records:
td[D(x).quantize(D('0.01'))] += 1

print td

> Saw this http://code.activestate.com/recipes/277600-one-liner-frequency-count/
> using itertools.

That is cute but I cringe a bit at the temporary lists and the n log n 
algorithm.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Idea for removing the GIL...

2011-02-08 Thread Carl Banks
On Feb 8, 11:49 am, John Nagle  wrote:
>     The real reason for the GIL, though, is to support dynamic
> code modification in multi-thread progrems.  It's the ability
> to replace a function while it's being executed in another thread
> that's hard to do without a global lock.  If it were just a data-side
> problem, local object locks, a lock at the allocator, and a
> concurrent garbage collector would work.

I realize that you believe that Python's hyper-dynamicism is the cause
of all evils in the world, but in this case you're not correct.

Concurrent garbage collectors work just fine in IronPython and Jython,
which are just as dynamic as CPython.  I'm not sure why you think an
executing function would be considered inaccessible and subject to
collection.  If you replace a function (code object, actually) in
another thread it only deletes the reference from that namespace,
references on the executing stack still exist.

The real reason they never replaced the GIL is that fine-grained
locking is expensive with reference counting.  The only way the cost
of finer-grained locking would be acceptable, then, is if they got rid
of the reference counting altogether, and that was considered too
drastic a change.


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


Re: Idea for removing the GIL...

2011-02-08 Thread John Nagle

On 2/8/2011 1:39 AM, Vishal wrote:

Hello,

This might sound crazy..and dont know if its even possible, but...

Is it possible that the Python process, creates copies of the
interpreter for each thread that is launched, and some how the thread
is bound to its own interpreter ?

This will increase the python process size...for sure, however data
sharing will remain just like it is in threads.

and it "may" also allow the two threads to run in parallel, assuming
the processors of today can send independent instructions from the
same process to multiple cores?


   Won't work.  You'd have two threads updating the same shared data
structures without locking.  In CPython, there's a reference count
shared across threads, but no locking at the object level.

   The real reason for the GIL, though, is to support dynamic
code modification in multi-thread progrems.  It's the ability
to replace a function while it's being executed in another thread
that's hard to do without a global lock.  If it were just a data-side
problem, local object locks, a lock at the allocator, and a
concurrent garbage collector would work.

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


Re: Idea for removing the GIL...

2011-02-08 Thread Adam Tauno Williams
On Tue, 2011-02-08 at 11:52 -0500, Roy Smith wrote: 
> In article ,
>  Robert Kern  wrote:
> > Unlike a UNIX fork, CreateProcess() does not have the same copy-on-write 
> > semantics for initializing the memory of the new process. If you want to 
> > pass 
> > data to the children, the data must be pickled and sent across the process 
> > boundary. He's not saying that multiprocessing isn't useful at all on 
> > Windows, just less useful for the scenarios he is considering here.
> Amen, brother!  I used to work on a project that had a build system 
> which was very fork() intensive (lots of little perl and shell scripts 

Comparing issues that are simply fork() to using "multiprocessing" is a
bit of a false comparison.  multiprocessing provides a fairly large set
of information sharing techniques.  Just-doing-a-fork isn't really using
multiprocessing - fork'ing scripts isn't at all an equivalent to using
threads.

> As far as we could tell, it was entirely due to how bad Windows was at 
> process creation.

Nope.  If you want performance DO NOT USE cygwin.

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


Re: frequency of values in a field

2011-02-08 Thread Josh English
You could try a collections.defaultdict object with an integer as the startup 
value:

counts = collections.defaultdict(int)
for thing in long_list:
  counts[get_last_two_digits(thing)] += 1

This assumes get_last_two_digits is the function that provides the key you want 
to count by. I'm not sure what you want get_last_two_digits to look like from 
your post, or how you would get the long_list, which is just an iterator 
through your records.

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


frequency of values in a field

2011-02-08 Thread noydb
I am looking for ways to go about capturing the frequency of unique
values in one field in a dbf table which contains ~50k records.  The
values are numbers with atleast 5 digits to the right of the decimal,
but I want the frequency of values to only 2 decimal places.  I do
have a method to do this courtesy of a provided tool in ArcGIS.  Was
just curious about ways to do it without arcgis sw, using just python.

Saw this http://code.activestate.com/recipes/277600-one-liner-frequency-count/
using itertools.

I'd be curious to see how experienced pythoners' (or not too
experienced!) would go about doing this.

Thanks for any snippets provided, this should be interesting and
educating!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where and when does Python say this to you?

2011-02-08 Thread Terry Reedy

On 2/8/2011 8:38 AM, Peter Otten wrote:


import socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)


If you do not like typing 'socket' so many times:

import socket as so # or pick own abbreviation
sock = so.socket(so.AF_UNIX, sot.SOCK_STREAM)


Have you worked through the tutorial

http://docs.python.org/tutorial/index.html


It is worth reading through more than once.

--
Terry Jan Reedy

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


Re: - what do you think ?

2011-02-08 Thread Terry Reedy

On 2/8/2011 7:18 AM, przemol...@poczta.fm wrote:

On Tue, Feb 08, 2011 at 10:16:42PM +1100, Ben Finney wrote:



Either way, please don't ask for the subject lines to be munged.


Any technical reason why not ?


For one reason, python-list exchanges messages with both 
comp.lang.python and gmane.comp.python.general (the latter is how I read 
it), and newsreaders already separate messages by group. I also read 
pydev and a couple of sig lists via gmane, so extra headers would be noise.


--
Terry Jan Reedy

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


Re: How to Write grep in Emacs Lisp (tutorial)

2011-02-08 Thread Petter Gustad
Icarus Sparry  writes:

> The 'modern' way to do this is
> find . -maxdepth 2 -name '*.html' -exec grep whatever {} +

Agree, I've noticed that recent version of find have the + option. I
remember in the old days the exec method was considered bad since it
would fork grep for each process, so I've got used to using xargs. I
always used to quote "{}" as well, but this does not seem to be
required in later versions of find.

In terms of the number of forks the above will be similar to xargs as
they both have to make sure that they don't overflow the command
length.


Petter
-- 
.sig removed by request. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to Write grep in Emacs Lisp (tutorial)

2011-02-08 Thread Icarus Sparry
On Tue, 08 Feb 2011 13:51:54 +0100, Petter Gustad wrote:

> Xah Lee  writes:
> 
>> problem with find xargs is that they spawn grep for each file, which
>> becomes too slow to be usable.
> 
> find . -maxdepth 2 -name '*.html -print0 | xargs -0 grep whatever
> 
> will call grep with a list of filenames given by find, only a single
> grep process will run.
> 
> //Petter

This is getting off-topic for the listed newsgroups and into 
comp.unix.shell (although the question was originally posed in a MS 
windows context).

The 'modern' way to do this is
find . -maxdepth 2 -name '*.html' -exec grep whatever {} +

The key thing which makes this 'modern' is the '+' at the end of the 
command, rather than '\;'. This causes find to execute the grep once per 
group of files, rather than once per file.

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


Re: Idea for removing the GIL...

2011-02-08 Thread Stefan Behnel

Roy Smith, 08.02.2011 17:52:

Robert Kern wrote:


Unlike a UNIX fork, CreateProcess() does not have the same copy-on-write
semantics for initializing the memory of the new process. If you want to pass
data to the children, the data must be pickled and sent across the process
boundary. He's not saying that multiprocessing isn't useful at all on
Windows, just less useful for the scenarios he is considering here.


Amen, brother!  I used to work on a project that had a build system
which was very fork() intensive (lots of little perl and shell scripts
driven by make).  A full system build on a linux box took 30-60 minutes.
Building the same code on windows/cygwin took about 12 hours.  Identical
hardware (8-core, 16 gig Dell server, or something like that).

As far as we could tell, it was entirely due to how bad Windows was at
process creation.


Unlikely. Since you mention cygwin, it was likely due to the heavy lifting 
cygwin does in order to emulate fork() on Windows.


http://www.cygwin.com/faq/faq-nochunks.html#faq.api.fork

Stefan

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


Re: Unicode error in sax parser

2011-02-08 Thread Stefan Behnel

Rickard Lindberg, 08.02.2011 16:57:

Hi,

Here is a bash script to reproduce my error:

 #!/bin/sh

 cat>  å.timeline<
 
   0.13.0devb38ace0a572b+
   
   
   
 
   2011-02-01 00:00:00
   2011-02-03 08:46:00
   asdsd
 
   
   
 
   2011-01-24 16:38:11
   2011-02-23 16:38:11
 
 
 
   
 
 EOF

 python<

Expected behaviour. You cannot parse XML from unicode strings, especially 
not when the XML data explicitly declares itself as being encoded in UTF-8.


Parse from a byte string instead, as you do in your fixed code.

Stefan

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


Re: Idea for removing the GIL...

2011-02-08 Thread Roy Smith
In article ,
 Robert Kern  wrote:

> Unlike a UNIX fork, CreateProcess() does not have the same copy-on-write 
> semantics for initializing the memory of the new process. If you want to pass 
> data to the children, the data must be pickled and sent across the process 
> boundary. He's not saying that multiprocessing isn't useful at all on 
> Windows, just less useful for the scenarios he is considering here.

Amen, brother!  I used to work on a project that had a build system 
which was very fork() intensive (lots of little perl and shell scripts 
driven by make).  A full system build on a linux box took 30-60 minutes.  
Building the same code on windows/cygwin took about 12 hours.  Identical 
hardware (8-core, 16 gig Dell server, or something like that).

As far as we could tell, it was entirely due to how bad Windows was at 
process creation.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [python-list] - what do you think ?

2011-02-08 Thread Ethan Furman

przemol...@poczta.fm wrote:

On Tue, Feb 08, 2011 at 10:16:42PM +1100, Ben Finney wrote:

Would you consider adding [] to this list also ?

>>>

No thank you.

>>

Any technical reason why not ?



Nope.  Just don't care for it.  For those of us who have our e-mails 
automatically sorted into folders, having [python-list] in the subject 
line would be incredibly redundant.  Also, it's a waste of horizontal space.


I believe you could use a mail preprocessor, like Fetchmail or Procmail, 
to modify your e-mails before you receive them, though.


Good luck!

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


Re: Unicode error in sax parser

2011-02-08 Thread Chris Rebert
On Tue, Feb 8, 2011 at 7:57 AM, Rickard Lindberg  wrote:
> Hi,
>
> Here is a bash script to reproduce my error:

Including the error message and traceback is still helpful, for future
reference.

>    #!/bin/sh
>
>    cat > å.timeline <
>    EOF
>
>    python <    # encoding: utf-8
>    from xml.sax import parse
>    from xml.sax.handler import ContentHandler
>    parse(u"å.timeline", ContentHandler())
>    EOF
>
> If I instead do
>
>    parse(u"å.timeline".encode("utf-8"), ContentHandler())
>
> the script runs without errors.
>
> Is this a bug or expected behavior?

Bug; open() figures out the filesystem encoding just fine.
Bug tracker to report the issue to: http://bugs.python.org/

Workaround:
parse(open(u"å.timeline", 'r'), ContentHandler())

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


Re: Idea for removing the GIL...

2011-02-08 Thread Robert Kern

On 2/8/11 10:11 AM, Brian Curtin wrote:

On Tue, Feb 8, 2011 at 06:34, Vishal mailto:vsapr...@gmail.com>> wrote:

Also, multiprocessing has issues on Windows (most probably because of
the way CreateProcess() functions...)

Such as?


Unlike a UNIX fork, CreateProcess() does not have the same copy-on-write 
semantics for initializing the memory of the new process. If you want to pass 
data to the children, the data must be pickled and sent across the process 
boundary. He's not saying that multiprocessing isn't useful at all on Windows, 
just less useful for the scenarios he is considering here.


--
Robert Kern

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

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


Re: Idea for removing the GIL...

2011-02-08 Thread Brian Curtin
On Tue, Feb 8, 2011 at 06:34, Vishal  wrote:

> Also, multiprocessing has issues on Windows (most probably because of
> the way CreateProcess() functions...)


Such as?
-- 
http://mail.python.org/mailman/listinfo/python-list


Unicode error in sax parser

2011-02-08 Thread Rickard Lindberg
Hi,

Here is a bash script to reproduce my error:

#!/bin/sh

cat > å.timeline <

  0.13.0devb38ace0a572b+
  
  
  

  2011-02-01 00:00:00
  2011-02-03 08:46:00
  asdsd

  
  

  2011-01-24 16:38:11
  2011-02-23 16:38:11



  

EOF

python 

Re: Where and when does Python say this to you?

2011-02-08 Thread Grant Edwards
On 2011-02-08, Chris Rebert  wrote:
> On Tue, Feb 8, 2011 at 5:26 AM, gracemia gracemia  wrote:
>> ??File "prueba.py", line 4, in 
>> ?? ??sock = socket(AF_UNIX, SOCK_STREAM)
>> NameError: name 'AF_UNIX' is not defined
>>
>> code:
>>
>> import socket
>> sock = socket(AF_UNIX, SOCK_STREAM)
>
> You need to qualify all those names. Normal `import` doesn't dump all
> the importee's names into the importer's namespace;

Unfortunately, it's somewhat common for example code in various places
to show the above usage (where somebody did a "from  import
*).  IIRC there used to be examples like that even in the official
docs, but I haven't seen any there in a while.

But examples that look like that are still pretty easy to stumble
across using Google.

I'm guessing he copied the code from a "tutorial" example like this:

http://www.evolt.org/node/60276

-- 
Grant Edwards   grant.b.edwardsYow! hubub, hubub, HUBUB,
  at   hubub, hubub, hubub, HUBUB,
  gmail.comhubub, hubub, hubub.
-- 
http://mail.python.org/mailman/listinfo/python-list


SunLisp III: Lisp jobs and beer in Ft Lauderdale

2011-02-08 Thread kenny
Come on down to the Frog & Toad for wild and crazy debates over
parentheses on their own line and bring your resume -- the SunLisp
core is from an up and coming Ft Lauderdale firm that is still looking
for local Lisp (or Scheme or Clojure or...) talent.

The pub: http://www.thefrogandtoadpub.com/

The listing: http://lispjobs.wordpress.com/ (scroll down to 22-
Dec-2010)

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


Re: Where and when does Python say this to you?

2011-02-08 Thread gracemia
On 8 feb, 14:40, Chris Rebert  wrote:
> On Tue, Feb 8, 2011 at 5:26 AM, gracemia gracemia  wrote:
> >  File "prueba.py", line 4, in 
> >    sock = socket(AF_UNIX, SOCK_STREAM)
> > NameError: name 'AF_UNIX' is not defined
>
> > code:
>
> > import socket
> > sock = socket(AF_UNIX, SOCK_STREAM)
>
> You need to qualify all those names. Normal `import` doesn't dump all
> the importee's names into the importer's namespace; it only imports
> the name of the module itself. Thus:
>
> import socket
> sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
>
> I would recommend (re-)reading the relevant part of the 
> tutorial:http://docs.python.org/tutorial/modules.html
>
> Cheers,
> Chris
> --
> I would also recommend bypassing the EggHeadCafe junk and instead
> posting directly to the newsgroup/mailinglist.http://blog.rebertia.com

Sorry about my reply numbers, my browser blocked.

Thank you very much. It works for me.

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


Re: Where and when does Python say this to you?

2011-02-08 Thread Peter Otten
gracemia gracemia wrote:

>  File "prueba.py", line 4, in 
> sock = socket(AF_UNIX, SOCK_STREAM)
> NameError: name 'AF_UNIX' is not defined
> 
> code:
> 
> import socket
> sock = socket(AF_UNIX, SOCK_STREAM)

You need to qualify the variable names with the module name:

import socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

Have you worked through the tutorial 

http://docs.python.org/tutorial/index.html

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


PyCon Australia 2011: 20th & 21st August, Sydney Masonic Center

2011-02-08 Thread Richard Jones
The second PyCon Australia will be held in Sydney on the weekend of the 20th
and 21st of August at the Sydney Masonic Center.

The first PyCon Australia was held in June 2010 and attracted over 200 Python
programming enthusiasts. The second event is expected to
host over 250 attendees.

The weekend will see dozens of presentations introducing;

- Python programming and techniques,
- web programming,
- business applications,
- game development,
- education, science and mathematics,
- social issues,
- testing, databases, documentation and more!

We are hoping to organise sprints on the days following the conference proper.

International guests should note that Kiwi PyCon is to run on the
following weekend, making it a great opportunity to attend a couple of
awesome Down Under conferences and hopefully do some sprinting with
the locals.


Richard Jones
http://pycon-au.org/
PyCon AU Committee
-- 
http://mail.python.org/mailman/listinfo/python-list


Python Game Programming Challenge 12 (April 2011) is coming!

2011-02-08 Thread Richard Jones
The 12th Python Game Programming Challenge (PyWeek) is coming. It'll
run from the 3rd to the 10th of April.

The PyWeek challenge:

- Invites entrants to write a game in one week from scratch either as
an individual or in a team,
- Is intended to be challenging and fun,
- Will hopefully increase the public body of game tools, code and expertise,
- Will let a lot of people actually finish a game, and
- May inspire new projects (with ready made teams!)


Richard
http://pyweek.org/12/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where and when does Python say this to you?

2011-02-08 Thread Chris Rebert
On Tue, Feb 8, 2011 at 5:26 AM, gracemia gracemia  wrote:
>  File "prueba.py", line 4, in 
>    sock = socket(AF_UNIX, SOCK_STREAM)
> NameError: name 'AF_UNIX' is not defined
>
> code:
>
> import socket
> sock = socket(AF_UNIX, SOCK_STREAM)

You need to qualify all those names. Normal `import` doesn't dump all
the importee's names into the importer's namespace; it only imports
the name of the module itself. Thus:

import socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

I would recommend (re-)reading the relevant part of the tutorial:
http://docs.python.org/tutorial/modules.html

Cheers,
Chris
--
I would also recommend bypassing the EggHeadCafe junk and instead
posting directly to the newsgroup/mailinglist.
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where and when does Python say this to you?

2011-02-08 Thread Benjamin Kaplan
On Tue, Feb 8, 2011 at 8:27 AM, gracemia gracemia  wrote:
> This is the simple code:
>
> 
> import socket
>
> sock = socket(AF_UNIX, SOCK_STREAM)
> --
>
> Thank you

I think you're having a bit of trouble with Python's namespaces. doing
"import socket" does not give you all of the socket module's stuff in
your current namespace. For that, you do "from socket import *", but
that's bad form because you can end up clobbering a lot of stuff.
There's two ways to do it:


import socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

from socket import socket, AF_UNIX, SOCK_STREAM
sock = socket(AF_UNIX, SOCK_STREAM)

>
> Submitted via EggHeadCafe
> SQL Server CLR Stored Procedures for External Access
> http://www.eggheadcafe.com/tutorials/aspnet/08c40d08-af4a-41f6-9352-91ac82b90078/sql-server-clr-stored-procedures-for-external-access.aspx
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where and when does Python say this to you?

2011-02-08 Thread gracemia gracemia
this is the simple code:

--
import socket

# Create an unbound and not-connected socket.
sock = socket(AF_UNIX, SOCK_STREAM)

---

Thank you !

Submitted via EggHeadCafe 
SharePoint Tip / Thought of the Day WebPart
http://www.eggheadcafe.com/tutorials/aspnet/14280ff8-3c9f-46bd-8214-9267e613c8ec/sharepoint-tip--thought-of-the-day-webpart.aspx
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where and when does Python say this to you?

2011-02-08 Thread gracemia gracemia
This is the simple code:


import socket

sock = socket(AF_UNIX, SOCK_STREAM)
--

Thank you

Submitted via EggHeadCafe 
SQL Server CLR Stored Procedures for External Access
http://www.eggheadcafe.com/tutorials/aspnet/08c40d08-af4a-41f6-9352-91ac82b90078/sql-server-clr-stored-procedures-for-external-access.aspx
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where and when does Python say this to you?

2011-02-08 Thread gracemia gracemia
 File "prueba.py", line 4, in 
sock = socket(AF_UNIX, SOCK_STREAM)
NameError: name 'AF_UNIX' is not defined

code:

import socket
sock = socket(AF_UNIX, SOCK_STREAM)

Thank you !


Submitted via EggHeadCafe 
Statistics, Probability, Lotteries and Dumb Programmers
http://www.eggheadcafe.com/tutorials/aspnet/041de19a-e704-468f-bd3c-79164fc739f5/statistics-probability-lotteries-and-dumb-programmers.aspx
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to Write grep in Emacs Lisp (tutorial)

2011-02-08 Thread Petter Gustad
Xah Lee  writes:

> problem with find xargs is that they spawn grep for each file, which
> becomes too slow to be usable.

find . -maxdepth 2 -name '*.html -print0 | xargs -0 grep whatever

will call grep with a list of filenames given by find, only a single
grep process will run. 

//Petter
-- 
.sig removed by request. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Idea for removing the GIL...

2011-02-08 Thread Jean-Paul Calderone
On Feb 8, 7:34 am, Vishal  wrote:
> On Feb 8, 3:05 pm, Adam Tauno Williams  wrote:
>
> > On Tue, 2011-02-08 at 01:39 -0800, Vishal wrote:
> > > Is it possible that the Python process, creates copies of the
> > > interpreter for each thread that is launched, and some how the thread
> > > is bound to its own interpreter ?
> > > and it "may" also allow the two threads to run in parallel, assuming
> > > the processors of today can send independent instructions from the
> > > same process to multiple cores?
> > > Comments, suggestions, brush offs  are welcome :))
>
> > Yes, it is possible, and done.  See the multiprocessing module.  It
> > works very well.
> > 
>
> > It isn't exactly the same as threads, but provides many similar
> > constructs.
>
> Hi,
>
> Pardon me for my ignorance here, but 'multiprocessing' creates actual
> processes using fork() or CreateProcess().
> I was talking of a single process, running multiple instances of the
> interpreter. Each thread, bound with its own interpreter.
> so the GIL wont be an issue anymore...each interpreter has only one
> thing to do, and that one thing holds the lock on its own interpreter.
> Since its still the same process, data sharing should happen just like
> in Threads.

CPython does support multiple interpreters in a single process.
However,
you cannot have your cake and eat it too.  If you create multiple
interpreters,
then why do you think you'll be able to share objects between them for
free?

In what sense would you have *multiple* interpreters in that scenario?

You will need some sort of locking between the interpreters.  Then
you're either
back to the GIL or to some more limited form of sharing - such as you
might
get with the multiprocessing module.

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


Re: AF_UNIX socket not supported

2011-02-08 Thread Jean-Paul Calderone
On Feb 8, 7:30 am, gracemia  wrote:
> Hello !
>
> I'm trying to develop with AF_UNIX socket type in python (2.5) but
> python says that AF_UNIX is not supported
> How can I do for work with AF_UNIX sockets?
>
> Thank you

Where and when does Python say this to you?

http://docs.python.org/library/socket.html#socket.AF_UNIX

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


Re: Idea for removing the GIL...

2011-02-08 Thread Vishal
On Feb 8, 3:05 pm, Adam Tauno Williams  wrote:
> On Tue, 2011-02-08 at 01:39 -0800, Vishal wrote:
> > Is it possible that the Python process, creates copies of the
> > interpreter for each thread that is launched, and some how the thread
> > is bound to its own interpreter ?
> > and it "may" also allow the two threads to run in parallel, assuming
> > the processors of today can send independent instructions from the
> > same process to multiple cores?
> > Comments, suggestions, brush offs  are welcome :))
>
> Yes, it is possible, and done.  See the multiprocessing module.  It
> works very well.
> 
>
> It isn't exactly the same as threads, but provides many similar
> constructs.

Hi,

Pardon me for my ignorance here, but 'multiprocessing' creates actual
processes using fork() or CreateProcess().
I was talking of a single process, running multiple instances of the
interpreter. Each thread, bound with its own interpreter.
so the GIL wont be an issue anymore...each interpreter has only one
thing to do, and that one thing holds the lock on its own interpreter.
Since its still the same process, data sharing should happen just like
in Threads.

Also, multiprocessing has issues on Windows (most probably because of
the way CreateProcess() functions...)

Thanks and best regards,
Vishal
-- 
http://mail.python.org/mailman/listinfo/python-list


On Windows, how do I protect arguments to shell scripts launched with subprocess?

2011-02-08 Thread arve.knud...@gmail.com
Hi

Since upgrading to Python 2.7, I've run into the problem that when I
launch shell scripts (.e.g, *.bat) via subprocess.Popen (with False
for the 'shell' option, mind you), the arguments get interpreted by
the shell. For instance, the '|' character, no longer gets passed
verbatim to the script. What is now the correct way to protect
arguments passed as a list to subprocess.Popen? I tried enclosing each
argument in double quotes, but subprocess in turn thwarts my attempt,
by protecting each double quote with a backslash! For example, if I
were to pass ['"|"'] as the argument list to subprocess.Popen, it'd be
transformed like this:
>>> subprocess.list2cmdline(['"|"'])
'\\"|\\"'

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


AF_UNIX socket not supported

2011-02-08 Thread gracemia
Hello !

I'm trying to develop with AF_UNIX socket type in python (2.5) but
python says that AF_UNIX is not supported
How can I do for work with AF_UNIX sockets?

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


Re: [python-list] - what do you think ?

2011-02-08 Thread przemolicc
On Tue, Feb 08, 2011 at 10:16:42PM +1100, Ben Finney wrote:
> przemol...@poczta.fm writes:
> 
> > I have just subscribed to this python-list@ and this is my N list.
> > Usually many mailing lists use square brackets to identify its name
> > when you have e-mails from different forums.
> > Would you consider adding [] to this list also ?
> 
> No thank you.
> 
> Either your mail client already knows how to filter messages
> appropriately depending on which mailing list they came from; or, you
> should use a better mail client.

mutt is quite good ;-)

> Either way, please don't ask for the subject lines to be munged.

Any technical reason why not ?

Regards
Przemek

---
Dramatyczny wypadek Roberta Kubicy - zobacz najswiezsze doniesienia!
Sprawdz >>> http://linkint.pl/f2915

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


Re: PDB how to define a global inspection function?

2011-02-08 Thread Peter Otten
Charles Fox (Sheffield) wrote:

> Hi guys, I'm new to this group and have a question about debugging.
> I'm stepping through my code (using emacs pdbtrack and python-mode.el)
> and would like to isnpect objects as I go.  So I've defined a little
> object print function,
> 
> def p(obj):
> print obj
> print obj.__class__
> d=dir(obj)
> for a in d:
> print a, "=", getattr(obj, a)
> 
> 
> however it only works if the function is imported by whatever module I
> am currently debugging.  I'd like it to be available globally so I can
> stop and inspect anything as I step through various modules (including
> external libraries).  Is there a way to put it in the global scope for
> pdb to use?   Also is there a way to automatically import it whenever
> pdb starts up (like a matlab startup file)? (I'm not using ipython
> as it's not happy with pdbtrack in emacs, so am launching from emacs M-
> x pdb command).

For debugging purposes it's OK to put your function into the __builtin__ 
namespace:

>>> def p(): print 42
...
>>> import __builtin__
>>> __builtin__.p = p

Try it out:

>>> with open("tmp.py", "w") as f:
... f.write("p()\n")
...
>>> import tmp
42

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


Re: [python-list] - what do you think ?

2011-02-08 Thread Peter Otten
przemol...@poczta.fm wrote:

> I have just subscribed to this python-list@ and this is my N list.
> Usually many mailing lists use square brackets to identify its name
> when you have e-mails from different forums.
> Would you consider adding [] to this list also ?

A better approach is to configure your email client to move mails with 

List-Id: 

into a separate folder.
-- 
http://mail.python.org/mailman/listinfo/python-list


PDB how to define a global inspection function?

2011-02-08 Thread Charles Fox (Sheffield)
Hi guys, I'm new to this group and have a question about debugging.
I'm stepping through my code (using emacs pdbtrack and python-mode.el)
and would like to isnpect objects as I go.  So I've defined a little
object print function,

def p(obj):
print obj
print obj.__class__
d=dir(obj)
for a in d:
print a, "=", getattr(obj, a)


however it only works if the function is imported by whatever module I
am currently debugging.  I'd like it to be available globally so I can
stop and inspect anything as I step through various modules (including
external libraries).  Is there a way to put it in the global scope for
pdb to use?   Also is there a way to automatically import it whenever
pdb starts up (like a matlab startup file)? (I'm not using ipython
as it's not happy with pdbtrack in emacs, so am launching from emacs M-
x pdb command).

thanks,
Charles Fox
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [python-list] - what do you think ?

2011-02-08 Thread Ben Finney
przemol...@poczta.fm writes:

> I have just subscribed to this python-list@ and this is my N list.
> Usually many mailing lists use square brackets to identify its name
> when you have e-mails from different forums.
> Would you consider adding [] to this list also ?

No thank you.

Either your mail client already knows how to filter messages
appropriately depending on which mailing list they came from; or, you
should use a better mail client.

Either way, please don't ask for the subject lines to be munged.

-- 
 \   “I disapprove of what you say, but I will defend to the death |
  `\ your right to say it.” —Evelyn Beatrice Hall, _The Friends of |
_o__)  Voltaire_, 1906 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


[python-list] - what do you think ?

2011-02-08 Thread przemolicc
Hello,

I have just subscribed to this python-list@ and this is my N list.
Usually many mailing lists use square brackets to identify its name
when you have e-mails from different forums.
Would you consider adding [] to this list also ?

Please compare both version below:
5350 Feb 07 Richard Holmes(  20) PIL Open Problem
5351 N   Feb 07 Corey Richardson  (  17) ??>
5352 N   Feb 08 Ben Finney(  28) ??>
5376 N   Feb 07 Collins, Kevin [BEELINE]  (  35) Re: [rhelv5-list] ext4 options
5385 N   Feb 07 Bob Friesenhahn   (  22) Re: [zfs-discuss] ZFS and TRIM 
- No need for TRIM
5386 N   Feb 07 Eric D. Mudama(  25) ??>
5387 N   Feb 07 Nevins Duret  ( 215) [Tutor] Converting From 
Unicode to ASCII!!
5388 N   Feb 08 David Hutto   ( 138) ??>
5389 N   Feb 08 David Hutto   (   7)   ??>
5390 N   Feb 08 David Hutto   (   9) ??>
5391 N   Feb 08 Mr. Bean  (   9) Somehing interesting
5392 N   Feb 08 Mr. Bean  (   9) ??>
5393 N   Feb 07 nguytom   (  11) [Veritas-bu]  How to use the 
DataDomain system cleaning process
5394 N   Feb 07 David Stanaway(  29) ??>Re: [Veritas-bu] How to use 
the DataDomain system cleaning process

and

5350 Feb 07 Richard Holmes(  20) [python-list] PIL Open Problem
5351 N   Feb 07 Corey Richardson  (  17) ??>
5352 N   Feb 08 Ben Finney(  28) ??>
5376 N   Feb 07 Collins, Kevin [BEELINE]  (  35) Re: [rhelv5-list] ext4 options
5385 N   Feb 07 Bob Friesenhahn   (  22) Re: [zfs-discuss] ZFS and TRIM 
- No need for TRIM
5386 N   Feb 07 Eric D. Mudama(  25) ??>
5387 N   Feb 07 Nevins Duret  ( 215) [Tutor] Converting From 
Unicode to ASCII!!
5388 N   Feb 08 David Hutto   ( 138) ??>
5389 N   Feb 08 David Hutto   (   7)   ??>
5390 N   Feb 08 David Hutto   (   9) ??>
5391 N   Feb 08 Mr. Bean  (   9) [python-list] Somehing 
interesting
5392 N   Feb 08 Mr. Bean  (   9) ??>
5393 N   Feb 07 nguytom   (  11) [Veritas-bu]  How to use the 
DataDomain system cleaning process
5394 N   Feb 07 David Stanaway(  29) ??>Re: [Veritas-bu] How to use 
the DataDomain system cleaning process


Kind regards
przemol 'Seamie'


























Rozwiaz krzyzowke i wygraj nagrode!
Sprawdz >> http://linkint.pl/f2907

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


Re: using attributes as defaults

2011-02-08 Thread Jean-Michel Pichavant

Westley Martínez wrote:

On Fri, 2011-02-04 at 13:08 -0800, Wanderer wrote:

I want to give the option of changing attributes in a method or using
the current values of the attributes as the default.

class MyClass():
 """  my Class
 """
 def __init__(self):
  """ initialize
  """
  self.a = 3
  self.b = 4

 def MyMethod(self, a = self.a, b = self.b)
  """ My Method
  """
  self.a = a
  self.b = b
  DoSomething(a, b)

The above doesn't work. Is there a way to make it work?

Thanks


This doesn't work because you can't reference keyword arguments in the 
keyword argument array. This will work:

class MyClass:

def __init__(self):
""" initialize

Really? These are the worst docstrings ever.

"""
self.a = 3
self.b = 4

def MyMethod(self, a=None, b=None)
if a is not None:
self.a = a
if b is not None:
self.b = b
DoSomething(a, b) 

There is an alternative to this None thing:

Don't use optional arguments. Optional arguments are fine ,but I found 
myself avoiding using them is often a better choice.

Quoting the zen of  python: "Explicit is better than implicit."
If the reason for using optional arguments is that it'll take you 2 sec 
less to write the method call, then it sounds kind of wrong. Any other 
reason would be valid I guess.


I personnaly use optional arguments only to keep backward compatibility 
when changing a method signature.


JM

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


Re: Idea for removing the GIL...

2011-02-08 Thread Adam Tauno Williams
On Tue, 2011-02-08 at 01:39 -0800, Vishal wrote: 
> Is it possible that the Python process, creates copies of the
> interpreter for each thread that is launched, and some how the thread
> is bound to its own interpreter ?
> and it "may" also allow the two threads to run in parallel, assuming
> the processors of today can send independent instructions from the
> same process to multiple cores?
> Comments, suggestions, brush offs  are welcome :))

Yes, it is possible, and done.  See the multiprocessing module.  It
works very well.


It isn't exactly the same as threads, but provides many similar
constructs.

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


Idea for removing the GIL...

2011-02-08 Thread Vishal
Hello,

This might sound crazy..and dont know if its even possible, but...

Is it possible that the Python process, creates copies of the
interpreter for each thread that is launched, and some how the thread
is bound to its own interpreter ?

This will increase the python process size...for sure, however data
sharing will remain just like it is in threads.

and it "may" also allow the two threads to run in parallel, assuming
the processors of today can send independent instructions from the
same process to multiple cores?

Comments, suggestions, brush offs  are welcome :))

I heard that this has been tried before...any info about that?

Thanks and best regards,
Vishal Sapre
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: exec, import and isinstance

2011-02-08 Thread Peter Otten
Michele Petrazzo wrote:

> Hi all,
> I'm playing with python internals and I'm discovering a strange behavior
> of isinstance. Considering the following test case:
> 
> test/bar.py
> test/b.py
> test/a/__init__.py
> test/a/foo.py
> 
> -- __init__.py -> empty
> 
> --- foo.py:
> class foo: pass
> c = foo
> 
> --- b.py
> def ret():
>d = {}
>#s = "import sys;sys.path.append('a');import foo" ## this cause the
> problem
>s = "import sys;from a import foo"
>exec s in d
>return d["foo"].c
> 
> --- bar.py
> import a.foo
> import b
> 
> c1 = a.foo.foo
> c2 = b.ret()
> 
> print c1, c2
> print isinstance(c2(), c1)
> ---
> 
> Executing bar.py, I receive:
> a.foo.foo a.foo.foo
> True
> 
> But, if I replace the indicated line, python doesn't recognize anymore
> the instance and I receive:
> a.foo.foo foo.foo
> False
> 
> Why? I don't understand how python see the instance. Or
> better, does it see only the path? The classes (and the instances) are
> the same!
> 
> Thanks for all that help me to turn the light on...
> 
> Michele

You are importing the same module twice, once as foo and once as a.foo. 

To avoid importing a module that was already imported Python looks into the 
module cache sys.modules. If the module is already there it uses the cached 
version. 
In your case the cache contains the module, but under the key "a.foo" which 
Python cannot magically recognize as being the same as just "foo". Therefore 
the code in the module is executed twice and you get two distinct versions 
of every object in it.

Here is a simplified demo:

$ mkdir package
$ touch package/__init__.py
$ echo 'print "importing", __name__' > package/module.py
$ python
Python 2.6.4 (r264:75706, Dec  7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append("package")
>>> from package import module
importing package.module
>>> import module
importing module

Conclusion: never allow a path in sys.path that leads into a package.
Also, never import the main script; again you'll get two versions of the 
contents, this time filed in the cache under the script's filename and under 
"__main__".

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


exec, import and isinstance

2011-02-08 Thread Michele Petrazzo

Hi all,
I'm playing with python internals and I'm discovering a strange behavior
of isinstance. Considering the following test case:

test/bar.py
test/b.py
test/a/__init__.py
test/a/foo.py

-- __init__.py -> empty

--- foo.py:
class foo: pass
c = foo

--- b.py
def ret():
  d = {}
  #s = "import sys;sys.path.append('a');import foo" ## this cause the 
problem

  s = "import sys;from a import foo"
  exec s in d
  return d["foo"].c

--- bar.py
import a.foo
import b

c1 = a.foo.foo
c2 = b.ret()

print c1, c2
print isinstance(c2(), c1)
---

Executing bar.py, I receive:
a.foo.foo a.foo.foo
True

But, if I replace the indicated line, python doesn't recognize anymore
the instance and I receive:
a.foo.foo foo.foo
False

Why? I don't understand how python see the instance. Or
better, does it see only the path? The classes (and the instances) are 
the same!


Thanks for all that help me to turn the light on...

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