Re: Calculate an age

2007-12-11 Thread Gabriel Genellina
En Sat, 08 Dec 2007 14:37:13 -0300, Pierre Quentel  
<[EMAIL PROTECTED]> escribió:

> I understand that there is no possible conversion from a number of
> days to a (X,Y,Z) tuple of (years,months,days), and the reverse. But
> the difference between 2 dates can be unambiguously expressed as
> (X,Y,Z), and given a start date and an interval (X,Y,Z) you can also
> find the end date unambiguously, provided the arguments are valid (for
> instance, 1 month after the 30th of January is not valid)

As a side note, the "legal date reckoning" in Argentina considers that  
case too: 1 month after the 30th of January is Feb 28 (or 29 on leap  
years); 1 month after March 31 is April 30.

-- 
Gabriel Genellina

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


Re: GUI development with 3D view

2007-12-11 Thread Glenn Hutchings
On Dec 11, 1:58 am, gsal <[EMAIL PROTECTED]> wrote:
> If all you need to do is display a bunch of arrows, as you mention,
> the easiest thing to do might be to use Visual Python.  It is
> extremely easy to use.

Another option, if your app is data-driven, is to check out the
Visualization Toolkit (VTK) at http://www.vtk.org.  It also has 3D
graphics and a Python interface.

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


Re: Are Python deques linked lists?

2007-12-11 Thread Peter Otten
Duncan Booth wrote:

> Peter Otten <[EMAIL PROTECTED]> wrote:

>> However, you will get into trouble if you try to run two simultaneous
>> iterations over the same LinkedList, so there is room for another 
>> exercise ;)

That was a bit vague; I saw the shared _cursor attribute but didn't dig
deeper because I understood that your

> point wasn't to give a bullet-proof implementation of a linked
> list, just to demonstrate that there is no bar to having one which lets
> you use a for loop and delete elements.

>> However, you will get into trouble if you try to run two simultaneous
>> iterations over the same LinkedList

> Only if you try to modify the list from both of them. 

One deletion is enough to trigger the assertion:

>>> from linkedlist import *
>>> items = LinkedList(range(10))
>>> a = iter(items)
>>> b = iter(items)
>>> a.next()

>>> x = a.next()
>>> b.next()

>>> items.delete(x)
Traceback (most recent call last):
  File "", line 1, in 
  File "linkedlist.py", line 17, in delete
assert self._cursor.next is element
AssertionError

> Non-modifying
> iterations don't interfere. Changing the code to handle modifications
> from simultaneous iterations is fairly straightforward but probably
> tedious to cover all possible cases: probably the simplest thing is to
> catch such cases and throw an exception.

Or you just rule that delete(x) must occur "immediately" after x = next().

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


Re: dictionary of dictionaries

2007-12-11 Thread Marc 'BlackJack' Rintsch
On Mon, 10 Dec 2007 23:51:00 -0800, kettle wrote:

> On Dec 10, 6:58 pm, Peter Otten <[EMAIL PROTECTED]> wrote:
>> Well, there's also dict.setdefault()
>>
>> >>> pairs = ["ab", "ab", "ac", "bc"]
>> >>> outer = {}
>> >>> for a, b in pairs:
>>
>> ... inner = outer.setdefault(a, {})
>> ... inner[b] = inner.get(b, 0) + 1
>> ...>>> outer
>>
>> {'a': {'c': 1, 'b': 2}, 'b': {'c': 1}}
>>
>> and it's not hard to write your own defaultdict
>>
>> >>> class Dict(dict):
>>
>> ... def __getitem__(self, key):
>> ... return self.get(key, 0)
>> ...>>> d = Dict()
>> >>> for c in "abbbcdeafgh": d[c] += 1
>> ...
>> >>> d
>>
>> {'a': 2, 'c': 1, 'b': 3, 'e': 1, 'd': 1, 'g': 1, 'f': 1, 'h': 1}
>>
>> Peter
> 
> One last question.  I've heard the 'Explicit vs. Implicit' argument
> but this seems to boil down to a question of general usage case
> scenarios and what most people 'expect' for default behavior.  The
> above defaultdict implementation defining the __getitem__ method seems
> like it is more generally useful than the real default.  What is the
> reasoning behind NOT using this as the default implementation for a
> dict in python?

How's that more useful in the general case?  Maybe if you come from a
language where some default value pops up if the key is not present you
are used to write code in a way that exploits this fact.  But in the
general case!?  I need `defaultdict` not very often but want to know if a
key is not present in a dictionary.  Because most of the time that's a
special condition or error that has to be handled or signaled up the call
chain.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dumb newbie back in shell

2007-12-11 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
(Martin, please, don't top post - fixed)
> 
> Peter Otten wrote:
>> MartinRinehart wrote:
>>
>>> However, here's the little tester I wrote:
>>>
>>> # t.py - testing
>>>
>>> global g
>>> g = 'global var, here'
>>>
>>> def f():
>>> print g
>>>
>>> f()
>>>
>>> It prints 'global var, here,' not an error message. Wassup?
>> Try it again with a modified f():
>>
>> def f():
>> print g
>> g = 42
>>
>> In Python variables that are assigned to in a function are
>> function-local by default.
>>
 >
 > question is, why did the first one work?

The answer is just above.

 > In my real code I've got
 > module-level vars and an error msg trying to use them in a function.
 > In my test example I've got them accessed from within a function w/o
 > error message.
 >
 > I am confused.


In the fist you're not rebinding the g. And FWIW, this is a FAQ.

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


Re: Is a "real" C-Python possible?

2007-12-11 Thread Duncan Booth
sturlamolden <[EMAIL PROTECTED]> wrote:

> On 9 Des, 23:34, Christian Heimes <[EMAIL PROTECTED]> wrote:
> 
>> >http://antoniocangiano.com/2007/11/28/holy-shmoly-ruby-19-smokes-pyth
>> >... 
>>
>> The Ruby developers are allowed to be proud. They were able to
>> optimize some aspects of the implementation to get one algorithm
>> about 14 times faster. That's good work. But why was it so slow in
>> the first place? 
> 
> The thing to notice here is that Congiano spent 31.5 seconds computing
> 36 Fibonacci numbers in Python and 11.9 seconds doing the same in
> Ruby. Those numbers are ridiculous! The only thing they prove is that
> Congiano should not be programming computers. Anyone getting such
> results should take a serious look at their algoritm instead of
> blaming the language. I don't care if it takes 31.5 seconds to compute
> 36 Fibonacci numbers in Python 2.5.1 with the dumbest possible
> algorithm.
> 
Quite so.

Take something like 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498110
and then modify the Python code from the "Ruby smokes Python" article by 
the addition of @memoize(3) to decorate the otherwise unchanged fib 
function: the Python runtime drops down to 0.002 seconds.

That is just slightly better than Ruby's 11.9 seconds although I'm sure the 
Ruby code would also gain as much from a memoize decorator.


from memoize import memoize

@memoize(3)
def fib(n):
   if n == 0 or n == 1:
  return n
   else:
  return fib(n-1) + fib(n-2)

from time import clock
start = clock()
for i in range(36):
print "n=%d => %d" % (i, fib(i))
print clock()-start


When I run this (with output directed to a file: I'm not trying to time 
windows console speed), the output is:

n=0 => 0
n=1 => 1
n=2 => 1
n=3 => 2
n=4 => 3
n=5 => 5
n=6 => 8
n=7 => 13
n=8 => 21
n=9 => 34
n=10 => 55
n=11 => 89
n=12 => 144
n=13 => 233
n=14 => 377
n=15 => 610
n=16 => 987
n=17 => 1597
n=18 => 2584
n=19 => 4181
n=20 => 6765
n=21 => 10946
n=22 => 17711
n=23 => 28657
n=24 => 46368
n=25 => 75025
n=26 => 121393
n=27 => 196418
n=28 => 317811
n=29 => 514229
n=30 => 832040
n=31 => 1346269
n=32 => 2178309
n=33 => 3524578
n=34 => 5702887
n=35 => 9227465
0.00226425425578

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


Re: dictionary of dictionaries

2007-12-11 Thread Kay Schluehr
On Dec 9, 9:35 am, kettle <[EMAIL PROTECTED]> wrote:
> Hi,
>  I'm wondering what the best practice is for creating an extensible
> dictionary-of-dictionaries in python?
>
>  In perl I would just do something like:
>
> my %hash_of_hashes;
> for(my $i=0;$i<10;$i++){
> for(my $j=0;$j<10;$j++){
> ${$hash_of_hashes{$i}}{$j} = int(rand(10));
> }
>
> }
>
> but it seems to be more hassle to replicate this in python.  I've
> found a couple of references around the web but they seem cumbersome.
> I'd like something compact.
> -joe

You might produce the behaviour of the hash_of_hashes type directly:

class dict_of_dicts(dict):
def __getitem__(self, key):
d = dict.get(self, key, {})
self[key] = d
return d

def __setitem__(self, key, val):
assert isinstance(val, dict), "Value of type dict expected. %s
found instead."%(type(val))
dict.__setitem__(self, key, val)


>>> d = dict_of_dicts()
>>> d[0][1] = "A"
>>> d[1][1] = "B"
>>> d[1][2] = "C"
>>> d
{0: {1: 'A'}, 1: {1: 'B', 2: 'C'}}
>>> d[0] = 0  # expects values of type dict
Traceback
...
AssertionError: Value of type dict expected.  found
instead.
-- 
http://mail.python.org/mailman/listinfo/python-list


dpap implementation in python

2007-12-11 Thread Andy Cheesman
Hi people

Is there a dpap implementation for python? All I can seem to find is 
perl based? (dpap is used to share photos in iphoto)

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


Re: Are Python deques linked lists?

2007-12-11 Thread Duncan Booth
Peter Otten <[EMAIL PROTECTED]> wrote:


>> Only if you try to modify the list from both of them. 
> 
> One deletion is enough to trigger the assertion:

Yes, but the assertion isn't intended to be the complete code.

> 
> Or you just rule that delete(x) must occur "immediately" after x =
> next(). 

My original code went something like this:

def delete(self, element):
if self._cursor.next is element:
self._cursor.next = self._cursor.next.next
else:
el = self._list
while el.next:
if el.next is element:
el.next = el.next.next
break
el = el.next

i.e. do the most expected case fast and fall back to the slow method for 
other situations. I deleted the 'else' branch because the code I posted 
never takes that branch so I have no way to test whether I got it right or 
not.

There are lots of ways to handle this. You could save a separate pointer 
for each iterator. In fact I would expect that to handle all the possible 
variations of inserting and deleting correctly you do need to keep all the 
pointers somewhere they can be updated. Or as has been suggested you move 
the delete method onto the iterator itself. Actually I suspect that while 
it adds some overhead to a loop where you are going to modify the list it 
probably results in the cleanest code overall: if you have the iterator you 
can safely insert or remove objects without too many worries (just some 
basic sanity checking).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Any way to program custom syntax to python prompt? >> I want to edit matrices.

2007-12-11 Thread Ramsey Nasser
On 11 Dec 2007 04:14:09 GMT, Stargaming <[EMAIL PROTECTED]> wrote:
> On Mon, 10 Dec 2007 16:54:08 -0800, mosi wrote:
>
> > Python matrices are usually defined with numpy scipy array or similar.
> > e.g.
>  matrix1 = [[1, 2], [3, 4], [5, 6]]
> > I would like to have easier way of defining matrices, for example:
>  matrix = [1, 2; 3, 4; 5, 6]
> >
>  matrix =
> > [ 1, 2;
> >   3, 4;
> >   5, 6;]
> >
> > Any ideas how could this be done? The ";" sign is reserved, the "[ ]" is
> > used for lists.

I think mosi was looking to dynamically modify the syntax of Python
itself, as opposed to working around the existing syntax. This
includes things like messing with reserved symbols and keywords.

The interpreter is compiled with these definitions built in, so to
change them would conceivably require a recompile. If this is the
case, then mosi may be out of luck.  Then again, people have done some
interesting dynamic modifications to the interpreter during runtime
without recompiling. Psyco is the best example of this that comes to
mind. Maybe someone more versed in Psyco-esque interpreter voodoo
would be able to help.

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


Re: Securely distributing python source code as an application?

2007-12-11 Thread Ben Finney
xkenneth <[EMAIL PROTECTED]> writes:

> I'll shortly be distributing a number of python applications that
> use proprietary.

That's unfortunate. Hopefully it's not too late to avoid restricting
yourself and your users in this way.

> The software is part of a much larger system and it will need to be
> distributed securely. How can i achieve this?

That depends partly on what "distributed securely" means for you. Can
you elaborate? In particular, what threat model are you seeking
security from?

-- 
 \  "If I ever get real rich, I hope I'm not real mean to poor |
  `\   people, like I am now."  -- Jack Handey |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Job Offer: Python Ninja or Pirate!

2007-12-11 Thread Ben Finney
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:

> Just a few months ago, we found a few amazingly talented ninjas and
> even a few swarthy pirates. The team is doing wonderfully, and we're
> hoping to add a few more adventure-loving souls to our ranks.

For job postings, please don't use the mailing lsits. Instead, make
use of the Python Job Board http://www.python.org/Jobs>.

-- 
 \  "Remember:  every member of your 'target audience' also owns a |
  `\broadcasting station. These 'targets' can shoot back."  -- |
_o__)   Michael Rathbun to advertisers, news.admin.net-abuse.email |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: module level subclassing

2007-12-11 Thread Gabriel Genellina
En Mon, 10 Dec 2007 16:43:15 -0300, km <[EMAIL PROTECTED]>  
escribió:

> Is there a way to access the classes defined in the __init__.py into the
> modules files in the directory?
> for example say i have a module (dir) M with contents __init__.py  and  
> a.pyand
> b.py
> a.py  and b.py would like to subclass/instantiate  a class defined  in
> __init__.py
> how's that possible ?

Move the relevant parts of __init__.py into c.py, and make a.py and b.py  
import c.
You can import the class into __init__.py too, if you want to maintain the  
package's external interfase the same.

-- 
Gabriel Genellina

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


Python 2.4.4 crashes inside a turbogears project

2007-12-11 Thread bulgaro
Hi everybody.
I hope not to be OT but this is a python problem, not a turbogears
one.
I have a problem when starting a turbogears project.
In my model.py i import an api to work with a db which imports a pyd
library.
It gives me this output:

C:\Turbogears>python start-traffic.py dev.cfg
2007-12-11 09:52:52,701 traffic.controllers DEBUG New session

2007-12-11 09:52:52,701 traffic.controllers DEBUG New session

sys:1: DeprecationWarning: Non-ASCII character '\x90' in file C:
\Python24\python.exe on line 1, but no encoding declared; see
http://www.python.org/peps/pep-0263.html for details
  File "C:\Python24\python.exe", line 1
MZÉ$,000$0 0☺ö~
  ^
SyntaxError: invalid syntax
2007-12-11 09:52:54,224 cherrypy.msg INFO ENGINE: SystemExit raised:
shutting down autoreloader
2007-12-11 09:52:54,224 cherrypy.msg INFO HTTP: HTTP Server shut down
2007-12-11 09:52:54,234 turbogears.identity INFO Identity shutting
down
2007-12-11 09:52:54,234 cherrypy.msg INFO ENGINE: CherryPy shut down

It looks like it reads the binary file python.exe
I know this is probably a python error but i can't find any solution.
I use python 2.4.4.
Any clue?
Thank you in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list

simplejson setup problem

2007-12-11 Thread Robin Becker
I'm getting this error whilst building simplejson-1.7.3 with "setup.py install" 
on a win32 platform.

...
creating build\bdist.win32\egg\EGG-INFO
removing simplejson.egg-info\native_libs.txt
copying simplejson.egg-info\PKG-INFO -> build\bdist.win32\egg\EGG-INFO
copying simplejson.egg-info\SOURCES.txt -> build\bdist.win32\egg\EGG-INFO
copying simplejson.egg-info\dependency_links.txt -> 
build\bdist.win32\egg\EGG-INFO
copying simplejson.egg-info\entry_points.txt -> build\bdist.win32\egg\EGG-INFO
error: can't copy 'simplejson.egg-info\native_libs.txt': doesn't exist or not a 
regular file
"error: can't copy 'simplejson.egg-info\native_libs.txt': doesn't exist or not 
a 
regular file"

I'm not sure whether this error is significant, but presumably it is because 
instead of just installing, the setup seems to be creating an egg and that bit 
is failing.

Is there some easy way to avoid this broken egg syndrome and just use ordinary 
distutils?
-- 
Robin Becker

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


pyTTS com_error

2007-12-11 Thread Dick
I am trying to get the pyTTS module working. I have Python 2.4, the
Microsodt SAPI and pyTTS-3.0.win32-py2.4.exe installed.

When I run this script:

import pyTTS
tts = pyTTS.Create()

#set the speech rate
tts.Rate = 4

#set the speech volume percentage (0-100%)
tts.Volume = 40

#get a list of all the available voice actors
print tts.GetVoiceNames()

#explicitly set a voice
tts.SetVoiceByName('MSMary')

#speak the text
tts.Speak('This is the sound of my voice.')

I get this result

>>>
[u'MSMary', u'MSMike', u'MS-Anna-1033-20-DSK']

Traceback (most recent call last):
  File "C:/Python24/spelling/testspeech", line 17, in -toplevel-
tts.Speak('This is the sound of my voice.')
  File "C:\Python24\Lib\site-packages\pyTTS\sapi.py", line 213, in
Speak
self.speech.Speak(text, flagsum)
  File "C:\Python24\lib\site-packages\win32com\gen_py
\C866CA3A-32F7-11D2-9602-00C04F8EE628x0x5x3.py", line 2831, in Speak
, Flags)
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None,
0, -2147221164), None)
>>>

I am a brand-newby, and any help will be greatly appreciated.

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


Re: xpath and current python xml libraries

2007-12-11 Thread Paul Boddie
On Dec 11, 2:03 am, [EMAIL PROTECTED] wrote:
> PyXML seems to be long gone. Is lxml the way to go if i want to have
> xpath supported?

The libxml2dom package (which I maintain) also supports XPath and is
also based on libxml2. If you want to migrate code from using PyXML
without too much effort, it might be a solution. See here for details:

http://www.python.org/pypi/libxml2dom

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


GetPath to variable

2007-12-11 Thread Connolly
Hey there, new Python user here.

Currently, I'm working on a project in Python for my college work, I chose 
to use Python since I heard of its great advantages, now for designing GUI's 
I'm using the wxPython package.
What I'm trying to do at the moment is the following;
A file directory browser is created using self.dialong.ShowModal, when the 
user chooses the folder they want to use for this program, then 
self.dialong.GetPath is created, after this it (would) goes into a variable 
and is stored in a config file I'm creating using the ConfigObj module.
My problem here is GetPath does not want to be stored in some kind of 
variable, is there a specific method to put this string into a variable to 
store in the config file.

Thanks,

- Connolly 


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


Re: xpath and current python xml libraries

2007-12-11 Thread anand nalya
Is there a package that supports XSLT 2?

On 11/12/2007, Paul Boddie <[EMAIL PROTECTED]> wrote:
>
> On Dec 11, 2:03 am, [EMAIL PROTECTED] wrote:
> > PyXML seems to be long gone. Is lxml the way to go if i want to have
> > xpath supported?
>
> The libxml2dom package (which I maintain) also supports XPath and is
> also based on libxml2. If you want to migrate code from using PyXML
> without too much effort, it might be a solution. See here for details:
>
> http://www.python.org/pypi/libxml2dom
>
> Paul
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: GetPath to variable

2007-12-11 Thread Bruno Desthuilliers
Connolly a écrit :
> Hey there, new Python user here.
> 
> Currently, I'm working on a project in Python for my college work, I chose 
> to use Python since I heard of its great advantages, now for designing GUI's 
> I'm using the wxPython package.
> What I'm trying to do at the moment is the following;
> A file directory browser is created using self.dialong.ShowModal, when the 
> user chooses the folder they want to use for this program, then 
> self.dialong.GetPath is created, after this it (would) goes into a variable 
> and is stored in a config file I'm creating using the ConfigObj module.
> My problem here is GetPath does not want to be stored in some kind of 
> variable, is there a specific method to put this string into a variable to 
> store in the config file.

Could you please post the minimal working code reproducing your problem? 
And the full traceback if any ? Else we can only try to guess...

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


Error accessing a java web service

2007-12-11 Thread anand nalya
Hi,

I'm trying to access a web service written in java from python, but it is
returning a NullPointerException. The parameter that I'm passing is somehow
not reaching the server.

Here is the code I'm using for invoking the service

import SOAPpy
from SOAPpy import SOAPProxy
import fpconst
import xml

namespace = 'http://wev.onyomo.com/'
url = 'http://localhost:8176/XSLTTransformerService/XSLTTransformer'
server = SOAPProxy(url,namespace)

server.config.dumpSOAPOut = 1
server.config.dumpSOAPIn = 1

server.convert('');

*** Outgoing SOAP **

http://schemas.xmlsoap.org/soap/encoding/";
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/";
  xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance";
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/";
  xmlns:xsd="http://www.w3.org/1999/XMLSchema";
>

http://wev.onyomo.com/"; SOAP-ENC:root="1">





*** Incoming SOAP **
http://schemas.xmlsoap.org/soap/envelope/"; xmlns:xsd="
http://www.w3.org/2001/XMLSchema";
xmlns:ns1="http://wev.onyomo.com/";>http://schemas.xmlsoap.org/soap/envelope/
">soapenv:Server
java.lang.NullPointerException


Traceback (most recent call last):
  File "", line 1, in ?
  File "/home/anand/Desktop/SOAPpy-0.12.0/SOAPpy/Client.py", line 470, in
__call__
  File "/home/anand/Desktop/SOAPpy-0.12.0/SOAPpy/Client.py", line 492, in
__r_call
  File "/home/anand/Desktop/SOAPpy-0.12.0/SOAPpy/Client.py", line 406, in
__call
SOAPpy.Types.faultType: 


When I access the same web service through a java client, it works fine.

Am I missing something here??

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

Re: Job Offer: Python Ninja or Pirate!

2007-12-11 Thread Bruno Desthuilliers
kromakey a écrit :
> On 10 Dec, 19:11, Stargaming <[EMAIL PROTECTED]> wrote:
>> On Mon, 10 Dec 2007 16:10:16 +0200, Nikos Vergas wrote:
>>
>> [snip]
>>
 Problem: In the dynamic language of your choice, write a short program
 that will:
  1. define a list of the following user ids 42346, 77290, 729 (you can
 hardcode these, but it should
 still work with more or less ids)
  2. retrieve an xml document related to each user at this url "http://
 api.etsy.com/feeds/xml_user_details.php?id="
  3. retrieve the data contained in the city element from each xml
 document
  4. keep a running total of how many users are found in each city 5.
  display the total count of users living in each city
>> [snip]
>>
(snip)
> 
> A simpleton's version:
> 
> #!/usr/local/bin/python
> 
> import urllib
> from elementtree import ElementTree as et
> 
> userids = [71234,729,42346,77290,729,729]
> url = 'http://api.etsy.com/feeds/xml_user_details.php?id='
> 
> if __name__ == "__main__":
> 
>   city = {}
>   for userid in userids:
> feed = urllib.urlopen(url+str(userid))
> tree = et.parse(feed)
> for elem in tree.getiterator('city'):
>   if not city.has_key(elem.text):city[elem.text] = 1
>   else: city[elem.text] += 1
>   for k,v in city.items():
> if not k == None:print k,':\t',v


def count_users_by_city(url, userids):
urlopen = urllib.urlopen
parse = et.parse
cities = {}
for userid in map(str, userids):
   feed = urlopen(url+userid)
   tree = parse(feed)
   for elem in tree.getiterator('city'):
   key = elem.text
   if key in cities:
   cities[key] += 1
   else:
   cities[key] = 1
return cities

if __name__ == '__main__':
 cities = count_users_by_city(url, userids)
 print "\n".join("%s:%s" % item for item in cities.items())


Not tested !-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: logging.py: mutiple system users writing to same file getting permission errors.

2007-12-11 Thread Vinay Sajip
On Dec 10, 8:34 pm, evenrik <[EMAIL PROTECTED]> wrote:
> On Dec 7, 12:46 pm, Vinay Sajip <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Dec 6, 6:35 pm, evenrik <[EMAIL PROTECTED]> wrote:
>
> > > An a redhat box I have root, apache and other normal users run code
> > > that uses theloggingmodule to write to the same log file.  Since
> > > umasks are set to 2 or 022 this gets permission errors.
>
> > > I have fixed my issue by patching theloggingcode everywhere there is
> > > an open for write with:
> > > try:
> > > old_umask = os.umask(0)
> > > # open for write here
> > > finally:
> > > os.umask(old_umask)
>
> > > Is there a better way to solve this issue?
> > > Are there any security problems with this solution other than the log
> > > file not being protected?
>
> > Multiple processes writing to the same log file may step on each
> > other's toes:loggingcontains thread synchronisation code but no
> > protection against multiple processes accessing the same resource. The
> > best solution would be to log from all processes to a SocketHandler,
> > and then have a socket receiver process write the logs to file. This
> > effectively serialises access to the log file. An example is given in
> > theloggingdocs, see
>
> >http://docs.python.org/lib/network-logging.html
>
> > Of course, you can have the receiver process run under a uid of your
> > choosing which has the appropriate permissions to write to the log
> > file.
>
> > Regards,
>
> > Vinay Sajip
>
> Thank you for the warning about multiple processes.  We decided to try
> creating a DBHandler to write the logs to PostgeSQL.

Okay. In case you're interested - the original distribution of the
logging package (before it became part of Python) is at
http://www.red-dove.com/python_logging.html and some of the test
scripts, which are in the tarball available from that page, contain an
example database handler (in test script log_test14.py).

Best regards,

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


Re: Are Python deques linked lists?

2007-12-11 Thread Neil Cerutti
On 2007-12-11, Duncan Booth <[EMAIL PROTECTED]> wrote:
> There are lots of ways to handle this. You could save a
> separate pointer for each iterator. In fact I would expect that
> to handle all the possible variations of inserting and deleting
> correctly you do need to keep all the pointers somewhere they
> can be updated. Or as has been suggested you move the delete
> method onto the iterator itself. Actually I suspect that while
> it adds some overhead to a loop where you are going to modify
> the list it probably results in the cleanest code overall: if
> you have the iterator you can safely insert or remove objects
> without too many worries (just some basic sanity checking).

If you put an instrumented iterator through, say, reversed or
sorted, you'd lose the ability to use it to modify the list--so
it's good for the method to live in the iterator, because you
won't have access to the method anymore, either. In the other
scheme, you could accidentally use an iterator to make
modifications that's ignorant of the necessary bookkeeping (I
suppose you could raise an exception if there's a central
registry of valid iterators, though I like making the method
completely invisible).

I do have one last question about a doubly-linked list. Would you
have to perform any tricks (del statements) to get the garbage
collector to collect every node, or will it just work?

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


Re: abusing exceptions for continuations

2007-12-11 Thread Lautaro Pecile
On Dec 10, 5:39 am, gangesmaster <[EMAIL PROTECTED]> wrote:
> i've had this strange idea of using the exception's traceback (which
> holds the stack frame) to enable functional continuations, meaning,
> raise some special exception which will be caught by a reactor/
> scheduler/framework, which could later revive it by restoring the
> frame.
>
> i'm thinking of using the generator's implementation (some minimal
> support on the c-side)
>
> has this been tried before? what were the results?
>
> thanks,
> -tomer

That idea has been tried here 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474088

It fails with algorithms which not use tail call recursion, and is
veeery slow. Try with Stackless.

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


How can you make pylint/pychecker "see" setattr

2007-12-11 Thread Emin.shopper Martinian.shopper
Dear Experts,

Does anyone know how you can either make pylint "see" setattr or give it
explicit information when you do a "compile time" call to setattr?

For example, imagine that I have the following block of code

class foo:
def __init__(self):
for i in [1,2,5]:
   setattr(self,'y%i'i,i*i)

def baz(self):
print self.y2


With this code, pylint will complain that y2 does not seem to be a member of
self. Obviously, if the arguments for setattr are not known until run-time,
their is nothing pylint can do. But in the case, the arguments are known at
"compile time" so it would be nice if there was some way to communicate this
to pylint. For example, if I could execute something like
pylint.remember_set(...) that pylint would see, that would be great. I
suspect this probably requires more parsing than pylint does, however.

On a related note, does anyone have a suggestion for a way to create a bunch
of similar properties (e.g., y1, y2, y5, etc.) in a "safe" way that either
pychecker or pylint can check (or at least not complain about)? Obviously I
would use better names than y1, y2, etc., but in the project I'm working on
I often need to many large sets of similar variables. Defining them all "by
hand" is tedious and not very readable. I could define them in a dict or use
setattr, but then I can't get any of the benefits of things like pychecker
or pylint. I suspect that is just a fundamental trade-off but it would be
great if someone has a better idiom.

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

Re: Dumb newbie back in shell

2007-12-11 Thread MartinRinehart
I'm less confused. If someone can explain the wisdom of this design,
I'd be grateful.

If someone can explain why the following compiles successfully, I'd be
even more grateful:

def get_toks( text ):
global line_ptr, last_line
while line_ptr < last_line:
while char_ptr < len(text[line_ptr]):
if matches_EOI():
tokens.append( Token(EOI) )
elif matches_EOL():
tokens.append( Token(EOL) )
line_ptr += 1
char_ptr = 0

Shouldn't "char_ptr" be flagged as an error, appearing in line 4
before being a lhs in the last line?

Martin

[EMAIL PROTECTED] wrote:
> Peter,
>
> question is, why did the first one work? In my real code I've got
> module-level vars and an error msg trying to use them in a function.
> In my test example I've got them accessed from within a function w/o
> error message.
>
> I am confused.
>
> Martin
>
> Peter Otten wrote:
> > MartinRinehart wrote:
> >
> > > However, here's the little tester I wrote:
> > >
> > > # t.py - testing
> > >
> > > global g
> > > g = 'global var, here'
> > >
> > > def f():
> > > print g
> > >
> > > f()
> > >
> > > It prints 'global var, here,' not an error message. Wassup?
> >
> > Try it again with a modified f():
> >
> > def f():
> > print g
> > g = 42
> >
> > In Python variables that are assigned to in a function are
> > function-local by default.
> >
> > Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


TCP reset caused by socket.py

2007-12-11 Thread Object01
I've been working with the source code for Trac (http://
trac.edgewall.org/) lately and have run across a bizarre problem.  It
seems that all POST requests to Trac's standalone server (tracd) have
a random chance of causing the server to issue a TCP RST packet that
resets the connection.

Running Trac 10.3.1 on Win2K3 using Python 2.4, watching traffic with
Wireshark 0.99.5.

I've been stepping through the code using Winpdb 1.3.2 and have
isolated the problem to the socket.py that's included in Python 2.4.
Line 157, in _socketobject.close():

  self.send = self.recv = self.sendto = self.recvfrom =
self._sock._dummy

is what's causing the TCP RST to be issued.  If I set a breakpoint on
that line and step over it on a POST request, there's about an 80%
chance the server will issue a TCP RST.  When debugging, the entire
response makes it onto the wire before TCP RST is issued.  If I'm -
not- debugging, it's anybody's guess as to how much of the response
makes it out. The interruption, when it occurs, always seems to be
between calls to _fileobject.write().  This indicates a timing issue:
perhaps buffered data isn't being waited on properly prior to
the .close() method doing its work.

I can't tell if this is a problem with the way Trac was coded (i.e.
are they violating the rules of sockets?) or whether it indicates a
problem in Python's socket implementation.  Either way, isn't this a
strange statement (an assignment) for a TCP RST to occur?  I can only
figure that the garbage collector is unpredictably disposing of a
socket at this opportunity.  And why only for POST requests?

I'm looking for any insight anyone can provide about this!

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


"do" as a keyword

2007-12-11 Thread cokofreedom
First off let me state that I really enjoy using Python. I am a 3rd
year student and have been using python for 3 months, (thanks to
trac!). I do not consider myself an experienced or clever programmer,
but I am able to get by.

Something I love about Python is that almost everything you do can be
written in pseudo code then carried across into Python without a great-
deal of difference.

But reading through the warts and reading about a lack of "do while
statements" I also started to ponder about the "'do something' if
'true' else 'do this'", and pondered if perhaps this statement could
do with the including of the keyword do.

It would clear up the usage of such a statement because it would read
happily. Or am I making a mountain out of an ant hill?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xpath and current python xml libraries

2007-12-11 Thread Dmitri Fedoruk
On Dec 11, 4:03 am, [EMAIL PROTECTED] wrote:
> PyXML seems to be long gone. Is lxml the way to go if i want to have
> xpath supported?

Of course. Lxml proved itself as a very convenient xml & xslt
processing tool.

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


Counter-spam: Change the subject

2007-12-11 Thread Paul McGuire
On Dec 11, 5:59 am, dfg <[EMAIL PROTECTED]> wrote:
> Breakthrough - How To Turn Your Dull Website into Money Making Website


I was surprised at how effectively the spam posting the other day,
"Jesus in the Quran" was masked and defused by a reply titled "J in
the Q".  It seems this might be a simple tool for cleaning up some of
the spam posting clutter, just by "changing the subject" (double
entendre not intentional, it just happened).  Instead of these
breathless, shouting subjects full of '#' signs and capital letters,
we just acronymify them to "H T T Y D W into M M W" by posting a reply
and editing the subject.

Could this work?  It seems too simple.

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


Re: Dumb newbie back in shell

2007-12-11 Thread J. Clifford Dyer
The code you just posted doesn't compile successfully.

However, in your code, you probably have char_ptr defined at the module level, 
and you're confused because you didn't declare it as global.  Am I right?  My 
crystal ball has a smudge on it, but I think I can still see okay.

You can still reference module level variables that aren't declared as global, 
but you can't assign to them.  Or rather, when you try to, you create a new 
local variable that shadows the global one.

So the first time through, your char_ptr in the while expression is a module 
level variable, but the second time through, it references a local variable, 
because it has now been defined.

Cheers,
Cliff

On Tue, Dec 11, 2007 at 05:18:00AM -0800, [EMAIL PROTECTED] wrote regarding Re: 
Dumb newbie back in shell:
> 
> I'm less confused. If someone can explain the wisdom of this design,
> I'd be grateful.
> 
> If someone can explain why the following compiles successfully, I'd be
> even more grateful:
> 
> def get_toks( text ):
> global line_ptr, last_line
> while line_ptr < last_line:
> while char_ptr < len(text[line_ptr]):
> if matches_EOI():
> tokens.append( Token(EOI) )
> elif matches_EOL():
> tokens.append( Token(EOL) )
> line_ptr += 1
> char_ptr = 0
> 
> Shouldn't "char_ptr" be flagged as an error, appearing in line 4
> before being a lhs in the last line?
> 
> Martin
> 
> [EMAIL PROTECTED] wrote:
> > Peter,
> >
> > question is, why did the first one work? In my real code I've got
> > module-level vars and an error msg trying to use them in a function.
> > In my test example I've got them accessed from within a function w/o
> > error message.
> >
> > I am confused.
> >
> > Martin
> >
> > Peter Otten wrote:
> > > MartinRinehart wrote:
> > >
> > > > However, here's the little tester I wrote:
> > > >
> > > > # t.py - testing
> > > >
> > > > global g
> > > > g = 'global var, here'
> > > >
> > > > def f():
> > > > print g
> > > >
> > > > f()
> > > >
> > > > It prints 'global var, here,' not an error message. Wassup?
> > >
> > > Try it again with a modified f():
> > >
> > > def f():
> > > print g
> > > g = 42
> > >
> > > In Python variables that are assigned to in a function are
> > > function-local by default.
> > >
> > > Peter
> -- 
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dumb newbie back in shell

2007-12-11 Thread Chris Mellon
On Dec 11, 2007 8:23 AM, J. Clifford Dyer <[EMAIL PROTECTED]> wrote:
> The code you just posted doesn't compile successfully.
>

It *compiles* fine, but it'll raise an error when run.

> However, in your code, you probably have char_ptr defined at the module 
> level, and you're confused because you didn't declare it as global.  Am I 
> right?  My crystal ball has a smudge on it, but I think I can still see okay.
>

I assume that's what he think he's seeing also.

> You can still reference module level variables that aren't declared as 
> global, but you can't assign to them.  Or rather, when you try to, you create 
> a new local variable that shadows the global one.
>

No, the determination of what names are local and which are global
happens at compile time. The code as posted will not run correctly. It
could run if it weren't in a function and were executed in global
scope.

What's probably happening is that line_ptr < last_line is not true and
the body of the function isn't executed at all. The unbound local
exception is a runtime error that occurs when the local is accessed,
not when the function is compiled.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dumb newbie back in shell

2007-12-11 Thread Bruno Desthuilliers
J. Clifford Dyer a écrit :
> The code you just posted doesn't compile successfully.
> 
> However, in your code, you probably have char_ptr defined at the
> module level, and you're confused because you didn't declare it as
> global.  Am I right?  My crystal ball has a smudge on it, but I think
> I can still see okay.
> 
> You can still reference module level variables that aren't declared
> as global, but you can't assign to them.  Or rather, when you try to,
> you create a new local variable that shadows the global one.
 >
> So the first time through, your char_ptr in the while expression is a
> module level variable, but the second time through, it references a
> local variable, because it has now been defined.

IIRC, local assignments are detected during  compilation - so this is 
not how it works...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dumb newbie back in shell

2007-12-11 Thread J. Clifford Dyer
On Tue, Dec 11, 2007 at 08:36:54AM -0600, Chris Mellon wrote regarding Re: Dumb 
newbie back in shell:
> Delivered-To: [EMAIL PROTECTED]
> Date: Tue, 11 Dec 2007 08:36:54 -0600
> From: "Chris Mellon" <[EMAIL PROTECTED]>
> To: "[email protected]" 
> Subject: Re: Dumb newbie back in shell
> In-Reply-To: <[EMAIL PROTECTED]>
> Precedence: list
> List-Id: General discussion list for the Python programming language
>   
> List-Unsubscribe: ,
>   
> List-Archive: 
> List-Post: 
> List-Help: 
> List-Subscribe: ,
>   
> Errors-To: [EMAIL PROTECTED]
> 
> On Dec 11, 2007 8:23 AM, J. Clifford Dyer <[EMAIL PROTECTED]> wrote:
> > The code you just posted doesn't compile successfully.
> >
> 
> It *compiles* fine, but it'll raise an error when run.
> 
> > However, in your code, you probably have char_ptr defined at the module 
> > level, and you're confused because you didn't declare it as global.  Am I 
> > right?  My crystal ball has a smudge on it, but I think I can still see 
> > okay.
> >
> 
> I assume that's what he think he's seeing also.
> 
> > You can still reference module level variables that aren't declared as 
> > global, but you can't assign to them.  Or rather, when you try to, you 
> > create a new local variable that shadows the global one.
> >
> 
> No, the determination of what names are local and which are global
> happens at compile time. The code as posted will not run correctly. It
> could run if it weren't in a function and were executed in global
> scope.
> 
> What's probably happening is that line_ptr < last_line is not true and
> the body of the function isn't executed at all. The unbound local
> exception is a runtime error that occurs when the local is accessed,
> not when the function is compiled.

Drat!  You're right.  I tried it in the interactive interpeter, and the 
function compiled just fine.  I tried calling the function and got an 
"UnboundLocalError."  I think I need to go sit in the corner and review the 
documentation.

Sorry all.  Disregard my previous post.

Chris, many thanks for your corrections.

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


Re: Dumb newbie back in shell

2007-12-11 Thread Bruno Desthuilliers
Chris Mellon a écrit :
(snip)
> What's probably happening is that line_ptr < last_line is not true

Indeed.

> and the body of the function isn't executed at all. The unbound local
>  exception is a runtime error that occurs when the local is accessed,
>  not when the function is compiled.

Now since the compiler already detected the name as local, why wait 
execution to signal this error ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dumb newbie back in shell

2007-12-11 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :

Martin, would you _please_ learn to quote properly ? top-posting and 
keeping the whole text of the previous posts are two really annoying 
practices. TIA


> I'm less confused. If someone can explain the wisdom of this design,
> I'd be grateful.

Since there's no distinct declarations of names, all names are by 
default created in the local namespace when bound in this namespace. 
Else you couldn't shadow global names in a local namespace... OTHO, free 
variables are looked up in all enclosing namespaces, so in a a 
non-nested function, names used but not bound in the local namespace are 
looked up in the global one.

> If someone can explain why the following compiles successfully, I'd be
> even more grateful:
> 
> def get_toks( text ):
> global line_ptr, last_line
> while line_ptr < last_line:
> while char_ptr < len(text[line_ptr]):
> if matches_EOI():
> tokens.append( Token(EOI) )
> elif matches_EOL():
> tokens.append( Token(EOL) )
> line_ptr += 1
> char_ptr = 0

FWIW, this code is incomplete. Please post the minimal *working* code 
reproducing your problem. Here, I had to define all the missing symbols 
to test your script.

> Shouldn't "char_ptr" be flagged as an error, appearing in line 4
> before being a lhs in the last line?

Note that such errors are raised at runtime, not during compilation 
(FWIW, I wonder why, since the use of the name is detected at 
compilation IIRC).

The error appears as long as line_ptr < last_line before calling the 
function. Now when line_ptr >= last_line, there is no error. So I 
conclude that UnboundLocalErrors are not raised until the offending line 
is actually executed.

Now while I don't know if it's a bug or a well-defined behaviour (I 
almost never rebind globals in functions..), I agree that this is at 
least weird since the compilation phase already tags the name as local 
wherever it's bound in the function.

Here's a minimal code reproducing the problem:

cnt = 1
test = 1 # set this to 0 to have an UnboundLocalError in wontdo()
retest = 1
def wontdo():
 global test
 global restest

 while test < retest:
 while cnt:
 cnt = cnt - 1
 test += 1


Some guru on this ?


NB : tested with
Python 2.5.1 (r251:54863, May  2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2



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


Re: "do" as a keyword

2007-12-11 Thread Diez B. Roggisch
[EMAIL PROTECTED] wrote:

> First off let me state that I really enjoy using Python. I am a 3rd
> year student and have been using python for 3 months, (thanks to
> trac!). I do not consider myself an experienced or clever programmer,
> but I am able to get by.
> 
> Something I love about Python is that almost everything you do can be
> written in pseudo code then carried across into Python without a great-
> deal of difference.
> 
> But reading through the warts and reading about a lack of "do while
> statements" I also started to ponder about the "'do something' if
> 'true' else 'do this'", and pondered if perhaps this statement could
> do with the including of the keyword do.
> 
> It would clear up the usage of such a statement because it would read
> happily. Or am I making a mountain out of an ant hill?

I doubt this will gain any traction. Similar proposals have been made
before - e.g. http://www.python.org/dev/peps/pep-0315/

But they didn't 

There are a few tricks you can use to emulate the desired behavior without
being to disruptive to the eye. E.g.


while True:
do_something()
if condition: break

Granted,

do
  do_something()(
while condition

is a bit more pleasant - but not enough to change the language I'd say.

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


Re: "do" as a keyword

2007-12-11 Thread Neil Cerutti
On 2007-12-11, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> First off let me state that I really enjoy using Python. I am a
> 3rd year student and have been using python for 3 months,
> (thanks to trac!). I do not consider myself an experienced or
> clever programmer, but I am able to get by.
>
> Something I love about Python is that almost everything you do
> can be written in pseudo code then carried across into Python
> without a great- deal of difference.
>
> But reading through the warts and reading about a lack of "do
> while statements" I also started to ponder about the "'do
> something' if 'true' else 'do this'", and pondered if perhaps
> this statement could do with the including of the keyword do.
>
> It would clear up the usage of such a statement because it
> would read happily. Or am I making a mountain out of an ant
> hill?

When I use languages that supply do-while or do-until looping
constructs I rarely need them.

I personally miss 

  for init; condition; next: 
  
more than do-while, but the structured for loop doesn't fit in
Python since init and next would (probably) have to be statements
(or statement suites) rather than expressions. Hence, the cool
innovations of iterators and generators, which otherwise might
not have found a home in Python. I wonder what programming Python
was like before iterators sometimes.

However, did you have an specific need for a do-while construct?
Perhaps we could show you the alternatives.

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


Python-URL! - weekly Python news and links (Dec 11)

2007-12-11 Thread Gabriel Genellina
QOTW:  "I wrote 20 short programs in Python yesterday.  It was wonderful. 
Perl, I'm leaving you." Randall Munroe
title attribute embedded in source of http://xkcd.com/353/

"[M]ost undergraduate degrees in computer science these days are basically
Java vocational training." - Alan Kay


How Ruby "feels" to a Python programmer:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/fe62b9cf1a99a163/

How (not) to check parameter types "the Python way":

http://groups.google.com/group/comp.lang.python/browse_thread/thread/3e624ffe0b9b8d0c/6393a7f32bc5e903?#6393a7f32bc5e903

Merging two sorted lists:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/9cc0bf2eaf6795d/

A simple question: how old are you? actually isn't so simple:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/48aac4ec13c65244/

Using the Python API from C#/.NET:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/b9959656a75d8daa/

Distinguishing attributes and methods:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/7943ab9f93854eb6/

Python rising in TIOBE index:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/6404e4c5ff42581f/

Perhaps it's the antigravity importability:  xkcd featuring Python!
http://xkcd.com/353/



Everything Python-related you want is probably one or two clicks away in
these pages:

Python.org's Python Language Website is the traditional
center of Pythonia
http://www.python.org
Notice especially the master FAQ
http://www.python.org/doc/FAQ.html

PythonWare complements the digest you're reading with the
marvelous daily python url
 http://www.pythonware.com/daily
Mygale is a news-gathering webcrawler that specializes in (new)
World-Wide Web articles related to Python.
 http://www.awaretek.com/nowak/mygale.html
While cosmetically similar, Mygale and the Daily Python-URL
are utterly different in their technologies and generally in
their results.

Just beginning with Python?  This page is a great place to start:
http://wiki.python.org/moin/BeginnersGuide/Programmers

The Python Papers aims to publish "the efforts of Python enthusiats":
http://pythonpapers.org/
The Python Magazine is a technical monthly devoted to Python:
http://pythonmagazine.com

Readers have recommended the "Planet" sites:
http://planetpython.org
http://planet.python.org

comp.lang.python.announce announces new Python software.  Be
sure to scan this newsgroup weekly.

http://groups.google.com/groups?oi=djq&as_ugroup=comp.lang.python.announce

Python411 indexes "podcasts ... to help people learn Python ..."
Updates appear more-than-weekly:
http://www.awaretek.com/python/index.html

Steve Bethard continues the marvelous tradition early borne by
Andrew Kuchling, Michael Hudson, Brett Cannon, Tony Meyer, and Tim
Lesher of intelligently summarizing action on the python-dev mailing
list once every other week.
http://www.python.org/dev/summary/

The Python Package Index catalogues packages.
http://www.python.org/pypi/

The somewhat older Vaults of Parnassus ambitiously collects references
to all sorts of Python resources.
http://www.vex.net/~x/parnassus/

Much of Python's real work takes place on Special-Interest Group
mailing lists
http://www.python.org/sigs/

Python Success Stories--from air-traffic control to on-line
match-making--can inspire you or decision-makers to whom you're
subject with a vision of what the language makes practical.
http://www.pythonology.com/success

The Python Software Foundation (PSF) has replaced the Python
Consortium as an independent nexus of activity.  It has official
responsibility for Python's development and maintenance.
http://www.python.org/psf/
Among the ways you can support PSF is with a donation.
http://www.python.org/psf/donate.html

Kurt B. Kaiser publishes a weekly report on faults and patches.
http://www.google.com/groups?as_usubject=weekly%20python%20patch

Although unmaintained since 2002, the Cetus collection of Python
hyperlinks retains a few gems.
http://www.cetus-links.org/oo_python.html

Python FAQTS
http://python.faqts.com/

The Cookbook is a collaborative effort to capture useful and
interesting recipes.
http://aspn.activestate.com/ASPN/Cookbook/Python

Many Python conferences around the world are in preparation.
Watch this space for links to them.

Among several Python-oriented RSS/RDF feeds available are
http://www.python.org/channews.rd

Re: need the unsigned value from dl.call()

2007-12-11 Thread Larry Bates
eliss wrote:
> I'm using dl.call() to call a C function in an external library. It's
> working great so far except for one function, which returns an
> unsigned int in the C version. However, in python it returns a signed
> value to me. How can I get the unsigned value from this? I haven't
> brushed up on my two's complement in a while, so I was hoping someone
> could give me a hand.
> 
> Thanks
> 
> eliss
It is returning 32 bits.  If the sign bit (bit 32) is on it appears as a 
negative number.  Test for negative and multiply the absolute value * 2.
That should get you the unsigned value you want in a long.

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


Re: Source formatting fixer?

2007-12-11 Thread Jesse Jaggars
Bret wrote:
> Does anyone know of a package that can be used to "fix" bad formatting
> in Python code?  I don't mean actual errors, just instances where
> someone did things that violate the style guide and render the code
> harder to read.
>
> If nothing exists, I'll start working on some sed scripts or something
> to add spaces back in but I'm hoping someone out there has already
> done something like this.
>
> Thanks!
>
>
> Bret Wortman
>   
This may not be exactly what you want, but if you use Gedit there is a 
handy reindent plugin.


http://live.gnome.org/Gedit/Plugins/Reindent
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-11 Thread MonkeeSage
On Dec 11, 3:10 am, Duncan Booth <[EMAIL PROTECTED]> wrote:
> sturlamolden <[EMAIL PROTECTED]> wrote:
> > On 9 Des, 23:34, Christian Heimes <[EMAIL PROTECTED]> wrote:
>
> >> >http://antoniocangiano.com/2007/11/28/holy-shmoly-ruby-19-smokes-pyth
> >> >...
>
> >> The Ruby developers are allowed to be proud. They were able to
> >> optimize some aspects of the implementation to get one algorithm
> >> about 14 times faster. That's good work. But why was it so slow in
> >> the first place?
>
> > The thing to notice here is that Congiano spent 31.5 seconds computing
> > 36 Fibonacci numbers in Python and 11.9 seconds doing the same in
> > Ruby. Those numbers are ridiculous! The only thing they prove is that
> > Congiano should not be programming computers. Anyone getting such
> > results should take a serious look at their algoritm instead of
> > blaming the language. I don't care if it takes 31.5 seconds to compute
> > 36 Fibonacci numbers in Python 2.5.1 with the dumbest possible
> > algorithm.
>
> Quite so.
>
> Take something 
> likehttp://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498110
> and then modify the Python code from the "Ruby smokes Python" article by
> the addition of @memoize(3) to decorate the otherwise unchanged fib
> function: the Python runtime drops down to 0.002 seconds.
>
> That is just slightly better than Ruby's 11.9 seconds although I'm sure the
> Ruby code would also gain as much from a memoize decorator.
>
> from memoize import memoize
>
> @memoize(3)
> def fib(n):
>if n == 0 or n == 1:
>   return n
>else:
>   return fib(n-1) + fib(n-2)
>
> from time import clock
> start = clock()
> for i in range(36):
> print "n=%d => %d" % (i, fib(i))
> print clock()-start
>
> When I run this (with output directed to a file: I'm not trying to time
> windows console speed), the output is:
>
> n=0 => 0
> n=1 => 1
> n=2 => 1
> n=3 => 2
> n=4 => 3
> n=5 => 5
> n=6 => 8
> n=7 => 13
> n=8 => 21
> n=9 => 34
> n=10 => 55
> n=11 => 89
> n=12 => 144
> n=13 => 233
> n=14 => 377
> n=15 => 610
> n=16 => 987
> n=17 => 1597
> n=18 => 2584
> n=19 => 4181
> n=20 => 6765
> n=21 => 10946
> n=22 => 17711
> n=23 => 28657
> n=24 => 46368
> n=25 => 75025
> n=26 => 121393
> n=27 => 196418
> n=28 => 317811
> n=29 => 514229
> n=30 => 832040
> n=31 => 1346269
> n=32 => 2178309
> n=33 => 3524578
> n=34 => 5702887
> n=35 => 9227465
> 0.00226425425578

Another point is, the reason the ruby code shows such a performance
increase is because of the way it wraps native (C) types for integers
in the the new byte compiler; i.e., it's a directed optimization,
which the example code exploits to its full extent. But with
dictionary access, for example, python still creams ruby (by a 2/1
factor in my tests). Speaking as someone who uses both python and
ruby, I can say that ruby 1.9 is approaching python's speed, which is
very cool, but is still not quite as fast as python in general (the
whole "smokes python" bit is just propaganda that utilizes a specific
feature vector, and is generally unhelpful).

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


Re: Dumb newbie back in shell

2007-12-11 Thread Chris Mellon
On Dec 11, 2007 8:51 AM, Bruno Desthuilliers
<[EMAIL PROTECTED]> wrote:
> Chris Mellon a écrit :
> (snip)
> > What's probably happening is that line_ptr < last_line is not true
>
> Indeed.
>
> > and the body of the function isn't executed at all. The unbound local
> >  exception is a runtime error that occurs when the local is accessed,
> >  not when the function is compiled.
>
> Now since the compiler already detected the name as local, why wait
> execution to signal this error ?
>

Because detection assignment to a name in the first AST scan and then
using LOAD_FAST and STORE_FAST for access to and assigning to that
name is a simple, easy thing to implement, while doing execution
analysis to detect that it can't possibly be set before being
referenced is really hard.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are Python deques linked lists?

2007-12-11 Thread Duncan Booth
Neil Cerutti <[EMAIL PROTECTED]> wrote:

> If you put an instrumented iterator through, say, reversed or
> sorted, you'd lose the ability to use it to modify the list

I think that is kind of irrelevant. reversed doesn't take an iterator, it 
requires a sequence and returns an iterator. sorted will take an iterator 
but it always returns a new list.


> I do have one last question about a doubly-linked list. Would you
> have to perform any tricks (del statements) to get the garbage
> collector to collect every node, or will it just work?

It should just work.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need the unsigned value from dl.call()

2007-12-11 Thread Diez B. Roggisch
Larry Bates wrote:

> eliss wrote:
>> I'm using dl.call() to call a C function in an external library. It's
>> working great so far except for one function, which returns an
>> unsigned int in the C version. However, in python it returns a signed
>> value to me. How can I get the unsigned value from this? I haven't
>> brushed up on my two's complement in a while, so I was hoping someone
>> could give me a hand.
>> 
>> Thanks
>> 
>> eliss
> It is returning 32 bits.  If the sign bit (bit 32) is on it appears as a
> negative number.  Test for negative and multiply the absolute value * 2.
> That should get you the unsigned value you want in a long.

Erm... Nope.

All bits set is -1 - so according to your recipe, that would be abs(-1) * 2
= 2

I'd suggest this formula:

if value < 0:
value = 2^32 + value + 1

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


Re: Are Python deques linked lists?

2007-12-11 Thread Neil Cerutti
On 2007-12-11, Duncan Booth <[EMAIL PROTECTED]> wrote:
> Neil Cerutti <[EMAIL PROTECTED]> wrote:
>> If you put an instrumented iterator through, say, reversed or
>> sorted, you'd lose the ability to use it to modify the list
>
> I think that is kind of irrelevant. reversed doesn't take an
> iterator, it requires a sequence and returns an iterator.
> sorted will take an iterator but it always returns a new list.

Thank you! Strangely enough I didn't know either of those things.
I've been using sorted as if it were a generator, and I guess
I've never used reversed on an iterator before.

>> I do have one last question about a doubly-linked list. Would you
>> have to perform any tricks (del statements) to get the garbage
>> collector to collect every node, or will it just work?
>
> It should just work.

Cool.

-- 
Neil Cerutti
I am free of all prejudices. I hate everyone equally. --W. C. Fields
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dumb newbie back in shell

2007-12-11 Thread Bruno Desthuilliers
Chris Mellon a écrit :
> On Dec 11, 2007 8:51 AM, Bruno Desthuilliers
> <[EMAIL PROTECTED]> wrote:
>> Chris Mellon a écrit :
>> (snip)
>>> What's probably happening is that line_ptr < last_line is not true
>> Indeed.
>>
>>> and the body of the function isn't executed at all. The unbound local
>>>  exception is a runtime error that occurs when the local is accessed,
>>>  not when the function is compiled.
>> Now since the compiler already detected the name as local, why wait
>> execution to signal this error ?
>>
> 
> Because detection assignment to a name in the first AST scan and then
> using LOAD_FAST and STORE_FAST for access to and assigning to that
> name is a simple, easy thing to implement, while doing execution
> analysis to detect that it can't possibly be set before being
> referenced is really hard.

Ok, makes sens.

Now nobody should ever rebind a global name anyway !-)
-- 
http://mail.python.org/mailman/listinfo/python-list


PyErr_NoMemory and multiple sub-interpreters

2007-12-11 Thread Manlio Perillo
Hi.

Is it safe to use PyErr_NoMemory in a multi sub-interpreters environment?

I have some doubts since PyErr_NoMemory uses a global variable: 
PyExc_MemoryErrorInst



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


Leo 4.4.5 final released

2007-12-11 Thread Edward K Ream
Leo 4.4.5 final is available at:
http://sourceforge.net/project/showfiles.php?group_id=3458&package_id=29106

Leo 4.4.5 fixes several long-delayed bug fixes and adds several new
features.

Leo is a text editor, data organizer, project manager and much more. See:
http://webpages.charter.net/edreamleo/intro.html

The highlights of Leo 4.4.5:

- Leo now recognizes directives in headlines.
- Adds 3 new sort-lines commands.
- Adds commands to insert and delete icons from headlines.
- Adds all the Tango 16x16 icons to Leo's icon library.
- Adds support for @rst-preformat nodes to the rst3 plugin.

Links:
--
Leo:  http://webpages.charter.net/edreamleo/front.html
Home: http://sourceforge.net/projects/leo/
Download: http://sourceforge.net/project/showfiles.php?group_id=3458
CVS:  http://leo.tigris.org/source/browse/leo/
Quotes:   http://webpages.charter.net/edreamleo/testimonials.html

Edward K. Ream   email:  [EMAIL PROTECTED]
Leo: http://webpages.charter.net/edreamleo/front.html




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


Re: Is a "real" C-Python possible?

2007-12-11 Thread sturlamolden
On 11 Des, 10:10, Duncan Booth <[EMAIL PROTECTED]> wrote:

> @memoize(3)
> def fib(n):
>if n == 0 or n == 1:
>   return n
>else:
>   return fib(n-1) + fib(n-2)


The thing I would do is:

def fibo(n):
while 1:
try:
return fibo.seq[n]
except AttributeError:
fibo.seq = [0, 1, 1]
except IndexError:
fibo.seq.append( fibo.seq[-2] + fibo.seq[-1] )


Here are some timings I got on my laptop (1.7 GHz Pentium M, Windows
XP, Python 2.5.1), calculating 36 Fibonacci numbers:

First run, initalizing cache: 243 µs
Second run, exploiting cache: 28 µs
Third run, exploting cache: 27 µs

This is 6 orders of magnitude faster than Congiano's benchmark. That
is a speed up by a factor of a million.





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


finding dir of main .py file

2007-12-11 Thread ron.longo

Is there any way that I can find the path of the main .py file of my
application?

For example, I have an application with some resources which are in a
subdirectory:


 myPythonApp.py
 /resources
 image1
 image2
 etc.

If i just do a call to os.getcwd() I get back the directory I was in when I
typed 'python myPythonApp.py' which could be any directory.  What I want is
the directory that contains the file myPythonApp.py.  Then I can use this
directory to construct the path to the resources directory.

(Actually, the app I'm writing has several subdirectories with stuff that it
needs such as plugins, configuration files, workspaces, etc.

Thanks for the help.

Ron
-- 
View this message in context: 
http://www.nabble.com/finding-dir-of-main-.py-file-tp14277145p14277145.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Matching XML Tag Contents with Regex

2007-12-11 Thread Chris
I'm trying to find the contents of an XML tag. Nothing fancy. I don't
care about parsing child tags or anything. I just want to get the raw
text. Here's my script:

import re

data = """



here's some text!


here's some text!


here's some text!


"""

tagName = 'div'
pattern = re.compile('<%(tagName)s\s[^>]*>[.\n\r\w\s\d\D\S\W]*[^(%
(tagName)s)]*' % dict(tagName=tagName))

matches = pattern.finditer(data)
for m in matches:
contents = data[m.start():m.end()]
print repr(contents)
assert tagName not in contents

The problem I'm running into is that the [^%(tagName)s]* portion of my
regex is being ignored, so only one match is being returned, starting
at the first  and ending at the end of the text, when it should
end at the first . For this example, it should return three
matches, one for each div.

Is what I'm trying to do possible with Python's Regex library? Is
there an error in my Regex?

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


Re: Help needed with python unicode cgi-bin script

2007-12-11 Thread weheh
import sys

if sys.platform == "win32":
import os, msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)


"Jack" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Just want to make sure, how exactly are you doing that?
>
>> Thanks for the reply, Jack. I tried setting mode to binary but it had no
>> affect.
>
> 


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


Re: "do" as a keyword

2007-12-11 Thread Tor Erik Sønvisen
> I also started to ponder about the "'do
> something' if 'true' else 'do this'", and pondered if perhaps
> this statement could do with the including of the keyword do.

Python has support for this in versions >= 2.5:

>>> a = range(0, 5)
>>> b = range(5, 8)
>>> min(a) if sum(a) < sum(b) else min(b)
0

In prior versions, you can use the and/or trick:

>>> (sum(a) < sum(b) and [min(a)] or [min(b)])[0]
0

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


Re: Matching XML Tag Contents with Regex

2007-12-11 Thread garage
> Is what I'm trying to do possible with Python's Regex library? Is
> there an error in my Regex?

Search for '*?' on http://docs.python.org/lib/re-syntax.html.

To get around the greedy single match, you can add a question mark
after the asterisk in the 'content' portion the the markup.  This
causes it to take the shortest match, instead of the longest. eg

<%(tagName)s\s[^>]*>[.\n\r\w\s\d\D\S\W]*?[^(%(tagName)s)]*

There's still some funkiness in the regex and logic, but this gives
you the three matches
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Counter-spam: Change the subject

2007-12-11 Thread Aahz
In article <[EMAIL PROTECTED]>,
Paul McGuire  <[EMAIL PROTECTED]> wrote:
>On Dec 11, 5:59 am, dfg <[EMAIL PROTECTED]> wrote:
>>
>> Breakthrough - How To Turn Your Dull Website into Money Making Website
>
>I was surprised at how effectively the spam posting the other day,
>"Jesus in the Quran" was masked and defused by a reply titled "J in
>the Q".  It seems this might be a simple tool for cleaning up some of
>the spam posting clutter, just by "changing the subject" (double
>entendre not intentional, it just happened).  Instead of these
>breathless, shouting subjects full of '#' signs and capital letters,
>we just acronymify them to "H T T Y D W into M M W" by posting a reply
>and editing the subject.
>
>Could this work?  It seems too simple.

LL YR VWL R BLNG T S
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Job Offer: Python Ninja or Pirate!

2007-12-11 Thread George Sakkis
On Dec 10, 11:07 pm, Stargaming <[EMAIL PROTECTED]> wrote:
> On Mon, 10 Dec 2007 19:27:43 -0800, George Sakkis wrote:
> > On Dec 10, 2:11 pm, Stargaming <[EMAIL PROTECTED]> wrote:
> >> On Mon, 10 Dec 2007 16:10:16 +0200, Nikos Vergas wrote:
>
> >> [snip]
>
> >> >> Problem: In the dynamic language of your choice, write a short
> >> >> program that will:
> >> >>  1. define a list of the following user ids 42346, 77290, 729 (you
> >> >>  can
> >> >> hardcode these, but it should
> >> >> still work with more or less ids)
> >> >>  2. retrieve an xml document related to each user at this url
> >> >>  "http://
> >> >> api.etsy.com/feeds/xml_user_details.php?id="
> >> >>  3. retrieve the data contained in the city element from each xml
> >> >> document
> >> >>  4. keep a running total of how many users are found in each city 5.
> >> >>  display the total count of users living in each city
> >> [snip]
>
> >> > i wanted to make it a one liner, but i had to import modules :(
>
> >> > import sys, xml, urllib
>
> >> > dummy = [sys.stdout.write(city + ': ' + str(num) + '\n') for city,
> >> > num in set([[(a, o.count(a)) for a in p] for o, p in [2*tuple([[city
> >> > for city in
> >> > ((xml.dom.minidom.parseString(urllib.urlopen('http://api.etsy.com/
> feeds/
>
> >> xml_user_details.php?id='
>
> >> > + str(id)).read()).getElementsByTagName('city')[0].childNodes +
> >> > [(lambda t: (setattr(t, 'data', 'no city'),
> >> > t))(xml.dom.minidom.Text())[1]])[0].data.lower().replace('  ', ' ')
> >> > for id in [71234, 71234, 71234, 71234, 71234, 71234,
> >> > 42792])]])]][0])]
>
> >> I suggest `__import__` in such cases.
>
> >> Even though I do not qualify for the job, I came up with this ()
> >> code (modified list values for demonstration, mixed together from
> >> previous post and original task):
>
> >> print '\n'.join('%s: %d'%(x,len(list(y))) for x,y in __import__
> >> ('itertools').groupby(sorted(__import__('xml').dom.minidom.parse
> >> (__import__('urllib').urlopen('http://api.etsy.com/feeds/
> >> xml_user_details.php?id=%d'%i)).getElementsByTagName('city')
> >> [0].lastChild.data.title() for i in (71234, 729, 42346, 77290, 729,
> >> 729
>
> >> I still find this rather readable, though, and there is no bad side-
> >> effect magic! :-)
>
> >> Output should be:
>
> >> | Chicago: 3
> >> | Fort Lauderdale: 1
> >> | Jersey City And South Florida: 1
> >> | New York: 1
>
> > Alas, it's not:
>
> > AttributeError: 'module' object has no attribute 'dom'
>
> > Here's a working version, optimized for char length (one line, 241
> > chars):
>
> > import urllib as U,elementtree.ElementTree as
> > E;c=[E.parse(U.urlopen('http://api.etsy.com/feeds/xml_user_details.php?
> > id=%d'%u)).findtext('//city')for u in
> > 71234,729,42346,77290,729,729];print'\n'.join('%s: %s'%
> > (i,c.count(i))for i in set(c))
>
> > George
>
> Heh, yes. I did the same error as the participant before me -- test it in
> a premodified environment. A fix is easy, __import__ 'xml.dom.minidom'
> instead of 'xml'. :-)

Closer, but still wrong; for some weird reason, __import__ for modules
in packages returns the top level package by default; you have to use
the 'fromlist' argument:

>>> __import__('xml.dom.minidom') is __import__('xml')
True

>>> __import__('xml.dom.minidom', fromlist=True)



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


Re: finding dir of main .py file

2007-12-11 Thread Rick Dooling
On Dec 11, 10:08 am, "ron.longo" <[EMAIL PROTECTED]> wrote:
> Is there any way that I can find the path of the main .py file of my
> application?
>
> For example, I have an application with some resources which are in a
> subdirectory:
>
>  myPythonApp.py
>  /resources
>  image1
>  image2
>  etc.

I just put the reference in my module. Don't hard code an absolute
path, use the environment tools.

app_path = os.getenv('HOME') + "/your_sub_dir"

resources_path = os.getenv('HOME') + "/your_sub_dir/resources"

If there's another way, someone else will jump in.

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


problem parsing lines in a file

2007-12-11 Thread barronmo
I'm having difficulty getting the following code to work.  All I want
to do is remove the '0:00:00' from the end of each line.  Here is part
of the original file:

3,3,"Dyspepsia NOS",9/12/2003 0:00:00
4,3,"OA of lower leg",9/12/2003 0:00:00
5,4,"Cholera NOS",9/12/2003 0:00:00
6,4,"Open wound of ear NEC*",9/12/2003 0:00:00
7,4,"Migraine with aura",9/12/2003 0:00:00
8,6,"HTN [Hypertension]",10/15/2003 0:00:00
10,3,"Imerslund syndrome",10/27/2003 0:00:00
12,4,"Juvenile neurosyphilis",11/4/2003 0:00:00
13,4,"Benign paroxysmal positional nystagmus",11/4/2003 0:00:00
14,3,"Salmonella infection, unspecified",11/7/2003 0:00:00
20,3,"Bubonic plague",11/11/2003 0:00:00

output = open('my/path/ProblemListFixed.txt', 'w')
for line in open('my/path/ProblemList.txt', 'r'):
 newline = line.rstrip('0:00:00')
 output.write(newline)
output.close()


This result is a copy of "ProblemList" without any changes made.  What
am I doing wrong?  Thanks for any help.

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


Re: Matching XML Tag Contents with Regex

2007-12-11 Thread harvey . thomas
On Dec 11, 4:05 pm, Chris <[EMAIL PROTECTED]> wrote:
> I'm trying to find the contents of an XML tag. Nothing fancy. I don't
> care about parsing child tags or anything. I just want to get the raw
> text. Here's my script:
>
> import re
>
> data = """
> 
> 
> 
> here's some text!
> 
> 
> here's some text!
> 
> 
> here's some text!
> 
> 
> """
>
> tagName = 'div'
> pattern = re.compile('<%(tagName)s\s[^>]*>[.\n\r\w\s\d\D\S\W]*[^(%
> (tagName)s)]*' % dict(tagName=tagName))
>
> matches = pattern.finditer(data)
> for m in matches:
> contents = data[m.start():m.end()]
> print repr(contents)
> assert tagName not in contents
>
> The problem I'm running into is that the [^%(tagName)s]* portion of my
> regex is being ignored, so only one match is being returned, starting
> at the first  and ending at the end of the text, when it should
> end at the first . For this example, it should return three
> matches, one for each div.
>
> Is what I'm trying to do possible with Python's Regex library? Is
> there an error in my Regex?
>
> Thanks,
> Chris

print re.findall(r'<%s(?=[\s/>])[^>]*>' % 'div', r)

["", "", ""]

HTH

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


Re: problem parsing lines in a file

2007-12-11 Thread Diez B. Roggisch
barronmo wrote:

> I'm having difficulty getting the following code to work.  All I want
> to do is remove the '0:00:00' from the end of each line.  Here is part
> of the original file:
> 
> 3,3,"Dyspepsia NOS",9/12/2003 0:00:00
> 4,3,"OA of lower leg",9/12/2003 0:00:00
> 5,4,"Cholera NOS",9/12/2003 0:00:00
> 6,4,"Open wound of ear NEC*",9/12/2003 0:00:00
> 7,4,"Migraine with aura",9/12/2003 0:00:00
> 8,6,"HTN [Hypertension]",10/15/2003 0:00:00
> 10,3,"Imerslund syndrome",10/27/2003 0:00:00
> 12,4,"Juvenile neurosyphilis",11/4/2003 0:00:00
> 13,4,"Benign paroxysmal positional nystagmus",11/4/2003 0:00:00
> 14,3,"Salmonella infection, unspecified",11/7/2003 0:00:00
> 20,3,"Bubonic plague",11/11/2003 0:00:00
> 
> output = open('my/path/ProblemListFixed.txt', 'w')
> for line in open('my/path/ProblemList.txt', 'r'):
>  newline = line.rstrip('0:00:00')
>  output.write(newline)
> output.close()
> 
> 
> This result is a copy of "ProblemList" without any changes made.  What
> am I doing wrong?  Thanks for any help.

It works kind of for me - but it's actually a bit more than you want. Take a 
close look on what rstrip _really_ does. Small hint:

print "foobar".rstrip("rab")

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


Re: finding dir of main .py file

2007-12-11 Thread Shane Geiger
Some usage of __file__ will always get what you want in various situations:

print __file__

print modulename.__file__

print os.getcwd() + "/" + __file__   





Rick Dooling wrote:
> On Dec 11, 10:08 am, "ron.longo" <[EMAIL PROTECTED]> wrote:
>   
>> Is there any way that I can find the path of the main .py file of my
>> application?
>>
>> For example, I have an application with some resources which are in a
>> subdirectory:
>>
>>  myPythonApp.py
>>  /resources
>>  image1
>>  image2
>>  etc.
>> 
>
> I just put the reference in my module. Don't hard code an absolute
> path, use the environment tools.
>
> app_path = os.getenv('HOME') + "/your_sub_dir"
>
> resources_path = os.getenv('HOME') + "/your_sub_dir/resources"
>
> If there's another way, someone else will jump in.
>
> rd
>   


-- 
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED]  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

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


Re: Help needed with python unicode cgi-bin script

2007-12-11 Thread weheh
Hi John:
Thanks for responding.

>Look at your file using
 >   print repr(open('c:/test/spanish.txt','rb').read())

>If you see 'a\xf1o' then use charset="windows-1252"
I did this ... no change ... still see 'a\xf1o'

>else if you see 'a\xc3\xb1o' then use charset="utf-8" else 

>Based on your responses to Martin, it appears that your file is
>actually windows-1252 but you are telling browsers that it is utf-8.

>Another check: if the file is utf-8, then doing
 >   open('c:/test/spanish.txt','rb').read().decode('utf8')
>should be OK; if it's not valid utf8, it will complain.
No. this causes decode error:

UnicodeDecodeError: 'utf8' codec can't decode bytes in position 1-4: invalid 
data
  args = ('utf8', 'a\, 1, 5, 'invalid data')
  encoding = 'utf8'
  end = 5
  object = 'a\xf1o'
  reason = 'invalid data'
  start = 1


>Yet another check: open the file with Notepad. Do File/SaveAs, and
>look at the Encoding box -- ANSI or UTF-8?
Notepad says it's ANSI

Thanks. What now? Also, this is a general problem for me, whether I read 
from a file or read from an html text field, or read from an html text area. 
So I'm looking for a general solution. If it helps to debug by reading from 
textarea or text field, let me know. 


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


Re: finding dir of main .py file

2007-12-11 Thread ron.longo

Nope, maybe I'm not explaining myself well.

When I do os.getenv('HOME') I get back None.

According to the docs, 'HOME' is the user's home directory on some
platforms.  Which is not what I want.

What I want is the directory in which an application's main .py file
resides.  That is, when I type: python MyApp.py, I want to know in which
directory does MyApp.py reside?


Thanks,
Ron



Rick Dooling-2 wrote:
> 
> On Dec 11, 10:08 am, "ron.longo" <[EMAIL PROTECTED]> wrote:
>> Is there any way that I can find the path of the main .py file of my
>> application?
>>
>> For example, I have an application with some resources which are in a
>> subdirectory:
>>
>>  myPythonApp.py
>>  /resources
>>  image1
>>  image2
>>  etc.
> 
> I just put the reference in my module. Don't hard code an absolute
> path, use the environment tools.
> 
> app_path = os.getenv('HOME') + "/your_sub_dir"
> 
> resources_path = os.getenv('HOME') + "/your_sub_dir/resources"
> 
> If there's another way, someone else will jump in.
> 
> rd
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

-- 
View this message in context: 
http://www.nabble.com/finding-dir-of-main-.py-file-tp14277145p14279627.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


new desktops

2007-12-11 Thread lawry_jim
http://cheap-computers-new.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Matching XML Tag Contents with Regex

2007-12-11 Thread Chris
On Dec 11, 11:41 am, garage <[EMAIL PROTECTED]> wrote:
> > Is what I'm trying to do possible with Python's Regex library? Is
> > there an error in my Regex?
>
> Search for '*?' onhttp://docs.python.org/lib/re-syntax.html.
>
> To get around the greedy single match, you can add a question mark
> after the asterisk in the 'content' portion the the markup.  This
> causes it to take the shortest match, instead of the longest. eg
>
> <%(tagName)s\s[^>]*>[.\n\r\w\s\d\D\S\W]*?[^(%(tagName)s)]*
>
> There's still some funkiness in the regex and logic, but this gives
> you the three matches

Thanks, that's pretty close to what I was looking for. How would I
filter out tags that don't have certain text in the contents? I'm
running into the same issue again. For instance, if I use the regex:

<%(tagName)s\s[^>]*>[.\n\r\w\s\d\D\S\W]*?(targettext)+[^(%
(tagName)s)]*

each match will include "targettext". However, some matches will still
include , presumably from the tags which didn't contain
targettext.

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


Why does producer delay halt shell pipe?

2007-12-11 Thread dwhall
I have 2 python scripts: examples of a producer and a filter,
respectively:

#! /usr/bin/env python
import sys, time
if __name__ == "__main__":
while True:
sys.stdout.write("hello.\r\n")
time.sleep(0.01)

#! /usr/bin/env python
import sys
if __name__ == "__main__":
line = sys.stdin.readline()
while line:
sys.stdout.write(line.upper())
line = sys.stdin.readline()

I wish to use these programs in Bash, like so:

$ ./producer.py | ./filter.py

However, the producer's time delay makes this not work.  If I remove
or reduce the delay, it works.  In reality the producer has an
unavoidable one-second delay.  I do NOT want to use popen or its
cousins because I want flexibility from the command line; I have many
filters.  Is there any way to write the filter to make this work?

thanks,

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


Block comments

2007-12-11 Thread MartinRinehart
Tomorrow is block comment day. I want them to nest. I think the reason
that they don't routinely nest is that it's a lot of trouble to code.
Two questions:

1) Given a start and end location (line position and char index) in an
array of lines of text, how do you Pythonly extract the whole block
comment? (Goal: not to have Bruno accusing me - correctly - of writing
C in Python.)

2) My tokenizer has a bunch of module-level constants including ones
that define block comment starts/ends. Suppose I comment that code
out. This is the situation:

/* start of block comment
...
BLOCK_COMMENT_END_CHARS = '*/'
...
end of block comment */

Is this the reason for """?

(If this is a good test of tokenizer smarts, cpp and javac flunked.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem parsing lines in a file

2007-12-11 Thread Chris
On Dec 11, 7:25 pm, barronmo <[EMAIL PROTECTED]> wrote:
> I'm having difficulty getting the following code to work.  All I want
> to do is remove the '0:00:00' from the end of each line.  Here is part
> of the original file:
>
> 3,3,"Dyspepsia NOS",9/12/2003 0:00:00
> 4,3,"OA of lower leg",9/12/2003 0:00:00
> 5,4,"Cholera NOS",9/12/2003 0:00:00
> 6,4,"Open wound of ear NEC*",9/12/2003 0:00:00
> 7,4,"Migraine with aura",9/12/2003 0:00:00
> 8,6,"HTN [Hypertension]",10/15/2003 0:00:00
> 10,3,"Imerslund syndrome",10/27/2003 0:00:00
> 12,4,"Juvenile neurosyphilis",11/4/2003 0:00:00
> 13,4,"Benign paroxysmal positional nystagmus",11/4/2003 0:00:00
> 14,3,"Salmonella infection, unspecified",11/7/2003 0:00:00
> 20,3,"Bubonic plague",11/11/2003 0:00:00
>
> output = open('my/path/ProblemListFixed.txt', 'w')
> for line in open('my/path/ProblemList.txt', 'r'):
>  newline = line.rstrip('0:00:00')
>  output.write(newline)
> output.close()
>
> This result is a copy of "ProblemList" without any changes made.  What
> am I doing wrong?  Thanks for any help.
>
> Mike

rstrip() won't do what you think it should do.
you could either use .replace('0:00:00','') directly on the input
string or if it might occur in one of the other elements as well then
just split the line on delimeters.
In the first case you can do.

for line in input_file:
output_file.write( line.replace('0:00:00','') )

in the latter rather.

for line in input_file:
tmp = line.split( ',' )
tmp[3] = tmp[3].replace('0:00:00')
output_file.write( ','.join( tmp ) )

Hope that helps,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dumb newbie back in shell

2007-12-11 Thread MartinRinehart
re top posting

Thanks for explaining. google groups hides the quoted text, so I
didn't see it.

Bruno Desthuilliers wrote:
> [EMAIL PROTECTED] a �crit :
> 
> Martin, would you _please_ learn to quote properly ? top-posting and
> keeping the whole text of the previous posts are two really annoying
> practices. TIA
> (snip)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: problem parsing lines in a file

2007-12-11 Thread Matimus

> This result is a copy of "ProblemList" without any changes made.  What
> am I doing wrong?  Thanks for any help.

rstrip doesn't work the way you think it does

>>> help(str.rstrip)
Help on method_descriptor:

rstrip(...)
S.rstrip([chars]) -> string or unicode

Return a copy of the string S with trailing whitespace removed.
If chars is given and not None, remove characters in chars
instead.
If chars is unicode, S will be converted to unicode before
stripping

>>> 'abcdef'.rstrip('e')
'abcdef'
>>> 'abcdef'.rstrip('ef')
'abcd'
>>> 'abcdef'.rstrip('efc')
'abcd'
>>> 'abcdef'.rstrip('efcd')
'ab'
>>> '20,3,"Bubonic plague",11/11/2003 0:00:00\n'.rstrip('0:00:00')
'20,3,"Bubonic plague",11/11/2003 0:00:00\n'
>>> '20,3,"Bubonic plague",11/11/2003 0:00:00\n'.rstrip('0:00:00\n')
'20,3,"Bubonic plague",11/11/2003 '
>>> '20,3,"Bubonic plague",11/11/2003 0:00:00\n'.rstrip('0:\n')
'20,3,"Bubonic plague",11/11/2003 '

You probably just want to use slicing though:

>>> '20,3,"Bubonic plague",11/11/2003 0:00:00\n'[:-9]
'20,3,"Bubonic plague",11/11/2003'

But don't forget to re-attach a newline before writing out. That goes
for the first method also.

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


Re: Python 2.4.4 crashes inside a turbogears project

2007-12-11 Thread Terry Reedy

"bulgaro" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

| I hope not to be OT but this is a python problem, not a turbogears one.

What you have shown is not a crash but a planned shutdown in response to an 
error.  There is a difference ;-).  It is almost certainly not a problem 
with the CPython interpreter but with your program or something else.

| I have a problem when starting a turbogears project.
| In my model.py i import an api to work with a db which imports a pyd
| library.

If it 'imports' (in the Python sense) something that is neither Python code 
or a compiled version thereof, that would explain the message below.

| It gives me this output:
|
| C:\Turbogears>python start-traffic.py dev.cfg
| 2007-12-11 09:52:52,701 traffic.controllers DEBUG New session
| 
| 2007-12-11 09:52:52,701 traffic.controllers DEBUG New session
| 
| sys:1: DeprecationWarning: Non-ASCII character '\x90' in file C:
| \Python24\python.exe on line 1, but no encoding declared; see
| http://www.python.org/peps/pep-0263.html for details
|  File "C:\Python24\python.exe", line 1
|MZÉ$,000$0 0?ö~
|  ^
| SyntaxError: invalid syntax

Sometime is trying to read python.exe as Python code.   It is not.

| 2007-12-11 09:52:54,224 cherrypy.msg INFO ENGINE: SystemExit raised:
| shutting down autoreloader
| 2007-12-11 09:52:54,224 cherrypy.msg INFO HTTP: HTTP Server shut down
| 2007-12-11 09:52:54,234 turbogears.identity INFO Identity shutting
| down
| 2007-12-11 09:52:54,234 cherrypy.msg INFO ENGINE: CherryPy shut down
|
| It looks like it reads the binary file python.exe
| I know this is probably a python error but i can't find any solution.

Don't do that.

tjr





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

Re: Matching XML Tag Contents with Regex

2007-12-11 Thread Diez B. Roggisch
Chris wrote:

> On Dec 11, 11:41 am, garage <[EMAIL PROTECTED]> wrote:
>> > Is what I'm trying to do possible with Python's Regex library? Is
>> > there an error in my Regex?
>>
>> Search for '*?' onhttp://docs.python.org/lib/re-syntax.html.
>>
>> To get around the greedy single match, you can add a question mark
>> after the asterisk in the 'content' portion the the markup.  This
>> causes it to take the shortest match, instead of the longest. eg
>>
>> <%(tagName)s\s[^>]*>[.\n\r\w\s\d\D\S\W]*?[^(%(tagName)s)]*
>>
>> There's still some funkiness in the regex and logic, but this gives
>> you the three matches
> 
> Thanks, that's pretty close to what I was looking for. How would I
> filter out tags that don't have certain text in the contents? I'm
> running into the same issue again. For instance, if I use the regex:
> 
> <%(tagName)s\s[^>]*>[.\n\r\w\s\d\D\S\W]*?(targettext)+[^(%
> (tagName)s)]*
> 
> each match will include "targettext". However, some matches will still
> include , presumably from the tags which didn't contain
> targettext.

Stop using the wrong tool for the job. Use lxml or BeautifulSoup to parse &
access HTML. 

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


Re: a Python person's experience with Ruby

2007-12-11 Thread Lou Pecora
In article <[EMAIL PROTECTED]>,
 Bruno Desthuilliers <[EMAIL PROTECTED]> wrote:

> Lou Pecora a écrit :
> > In article <[EMAIL PROTECTED]>,
> >  Bruno Desthuilliers <[EMAIL PROTECTED]> wrote:
> > 
> > 
> >>>Thus: close;
> >>>could replace close();
> 
> *Please* give proper attribution. I'd *never* suggest such a thing.

I apologize.

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

Re: Newbie edit/compile/run cycle question

2007-12-11 Thread Bruno Desthuilliers
Jeremy C B Nicoll a écrit :
> Bruno Desthuilliers <[EMAIL PROTECTED]> wrote:
> 
> 
>>Jeremy C B Nicoll a écrit :
>>
>>>Figuring out how IDLE works is a bit beyond me at this stage.
>>
>>Did you try out, or is it just an a priori ?
> 
> 
> Sort of, no and yes...  
> 
> A few weeks ago I started trying to use Python & IDLE and found a bug (which
> I reported on idle-dev).  In the course of discussing that I found out that
> the whole IDLE source was present in \idlelib which I hadn't realised,

FWIW, Python is free software, and you have access to all sources - C 
and Python - of the python exe and standard lib.

> and
> did look at it.  But for a Python newbie (if not a programming newbie) it's
> too much to understand. 

Mmm... Possibly, yes. OTHO, I know from experience that reading other 
peoples code is a good way to learn. Working mostly with free softwares 
myself, I often use the source as documentation...


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


Re: Why does producer delay halt shell pipe?

2007-12-11 Thread Nanjundi
On Dec 11, 1:05 pm, dwhall <[EMAIL PROTECTED]> wrote:
> filters.  Is there any way to write the filter to make this work?
>
> thanks,
>
> !!Dean

turn off python buffering & it should work.
export PYTHONUNBUFFERED=t

n'joy
-N
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Block comments

2007-12-11 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> Tomorrow is block comment day. I want them to nest. I think the reason
> that they don't routinely nest is that it's a lot of trouble to code.

Indeed.

> Two questions:
> 
> 1) Given a start and end location (line position and char index) in an
> array of lines of text, how do you Pythonly extract the whole block
> comment? (Goal: not to have Bruno accusing me - correctly - of writing
> C in Python.)

Is the array of lines the appropriate data structure here ?

> 2) My tokenizer has a bunch of module-level constants including ones
> that define block comment starts/ends. Suppose I comment that code
> out. This is the situation:
> 
> /* start of block comment
> ...
> BLOCK_COMMENT_END_CHARS = '*/'
> ...
> end of block comment */
> 
> Is this the reason for """?

Triple-quoted strings are not comments, they are a way to build 
multilines string litterals. The fact is that they are commonly used for 
doctrings - for obvious reasons - but then it's the position of this 
string litteral that makes it a docstring, not the fact that it's 
triple-quoted.

wrt/ your above example, making it a legal construct imply that you 
should not consider the block start/end markers as comment markers if 
they are enclosed in string-litteral markers.

Now this doesn't solve the problem of nested block comments. Here, I 
guess the solution would be to only allow fully nested block comments - 
that is, the nested block *must* be opened *and* closed within the 
parent block. In which case it should not be harder to parse than any 
other nested construct.

While we're at it, you may not know but there are already a couple 
Python packages for building tokenizers/parsers - could it be the case 
that you're guilty of ReinventingTheSquaredWheel(tm) ?-)

My 2 cents...

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


Building python 2.5.1 from source using MSVC 2005

2007-12-11 Thread black_13
How do i build python 2.5.1 from source using MSVC 2005?
Are there instructions on doing this.
thanks
black_13
-- 
http://mail.python.org/mailman/listinfo/python-list


Help: Trouble with imp.load_module

2007-12-11 Thread David Hirschfield
I'm not entirely sure what's going on here, but I suspect it's related 
to my general lack of knowledge of the python import internals.

Here's the setup:

module: tester.py:
-
import imp
def loader(mname, mpath):
fp, pathname, description = imp.find_module(mname,[mpath])
try:
m = imp.load_module(mname, fp, pathname, description)
finally:
if fp:
fp.close()
return m

module = loader("testA","/path/to/testA/)
print module.test_func("/path/to/something")

module = loader("test.B","/path/to/test.B/")
print module.test_func("/path/to/something")
--


module: testA.py:
---
def test_func(v):
import os
return os.path.exists(v)
---

module: test.B.py:
---
def test_func(v):
import os
return os.path.exists(v)
---


Okay, so modules "testA.py" and "test.B.py" are functionally identical, 
except for the name of the module files themselves, and this is the 
important part. The tester.py module is a really simple rig to run 
"imp.load_module" on those two files.

You should get no problem running the first test of module "testA.py" 
but you should get a traceback when attempting to run the second module 
"test.B.py":

Traceback (most recent call last):
  File "tester.py", line 15, in ?
print module.test_func("/path/to/something")
  File "./test.B.py", line 2, in test_func
import os
  File "/usr/lib/python2.4/os.py", line 131, in ?
from os.path import curdir, pardir, sep, pathsep, defpath, extsep, 
altsep
ImportError: No module named path


So this must have something to do with the "." in the name of module 
"test.B.py" but what is the problem, exactly? And how do I solve it? I 
will sometimes need to run load_module on filenames which happen to have 
"." in the name somewhere other than the ".py" extension. Is the 
find_module somehow thinking this is a package?

Any help would be appreciated,
-Dave

-- 
Presenting:
mediocre nebula.


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


DNS servers in Python - which ones are a good base for work?

2007-12-11 Thread John Nagle
   I need to do a non-standard DNS server in Python.  This
is for a spam blacklist type DNS server, not for IP lookup.
"dnspython" seems to be client side only.  Oak DNS is
deprecated.  Suggestions?

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


Is anyone happy with csv module?

2007-12-11 Thread massimo s.
Hi,

I'm struggling to use the python in-built csv module, and I must say
I'm less than satisfied. Apart from being rather poorly documented, I
find it especially cumbersome to use, and also rather limited. What I
dislike more is that it seems working by *rows* instead than by
*columns*.

So I have some questions:
1) Is there a good tutorial, example collection etc. on the csv module
that I'm missing?
2) Is there an alternative csv read/write module?
3) In case anyone else is as unhappy as me, and no tutorial etc.
enlighten us, and no alternative is present, anyone is interested in
an alternative csv module? I'd like to write one if it is the case.


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


Re: Is anyone happy with csv module?

2007-12-11 Thread Guilherme Polo
2007/12/11, massimo s. <[EMAIL PROTECTED]>:
> Hi,
>
> I'm struggling to use the python in-built csv module, and I must say
> I'm less than satisfied. Apart from being rather poorly documented, I
> find it especially cumbersome to use, and also rather limited. What I
> dislike more is that it seems working by *rows* instead than by
> *columns*.
>
> So I have some questions:
> 1) Is there a good tutorial, example collection etc. on the csv module
> that I'm missing?
> 2) Is there an alternative csv read/write module?
> 3) In case anyone else is as unhappy as me, and no tutorial etc.
> enlighten us, and no alternative is present, anyone is interested in
> an alternative csv module? I'd like to write one if it is the case.
>
>
> Thanks,
> Massimo
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Hello,

Post your actual problem so you can get more accurate help.
For the questions you placed: google for them, look at python pep 305


-- 
-- Guilherme H. Polo Goncalves
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-11 Thread John Nagle
sturlamolden wrote:
> On 10 Des, 23:49, [EMAIL PROTECTED] (Aahz) wrote:
> 
>> "Premature optimization is the root of all evil in programming."
>> --C.A.R. Hoare (often misattributed to Knuth, who was himself quoting
>> Hoare)

We're ten years into Python, and it's still a naive interpreter.
It's time for a serious optimizing compiler.  Shed Skin is going
in the right direction.  But for some reason, people seem to dislike the
Shed Skin effort. Its author writes "Am I the only one seeing the potential
of an implicitly statically typed Python-like-language that runs at
practically the same speed as C++?"

"For a set of 27 non-trivial test programs (at about 7,000 lines in total; 
... measurements show a typical speedup of 2-40 times over Psyco, about 10 on 
average, and 2-220 times over CPython, about 35 on average."  So that's
what's possible.

I'm surprised that Google management isn't pushing Guido towards
doing something about the performance problem.

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


Re: Is a "real" C-Python possible?

2007-12-11 Thread Paul Rudin
sturlamolden <[EMAIL PROTECTED]> writes:

> On 10 Des, 23:49, [EMAIL PROTECTED] (Aahz) wrote:
>
>> "Premature optimization is the root of all evil in programming."
>> --C.A.R. Hoare (often misattributed to Knuth, who was himself quoting
>> Hoare)
>
> Oh, I was Hoare? Thanks. Anyway, it doesn't change the argument that
> optimizing in wrong places is  a waste of effort.

Although sometimes people seem to think that it goes "optimisation is
the root...". The "premature" bit is significant.


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


Tuples !?!?

2007-12-11 Thread aaragao
Hi,

Is the tuple comparison brooked in python ?!?!?

Thanks.

Try this and you will see funny things:

# -*- coding: cp1252 -*-
import random
import csv
import struct
import array

def gera_string(res):

# acampo3
acampo3=((0,5,'muito reduzidos'),(6,20,'reduzidos'),
(21,32,'satisfatórios'),(33,40,'bons'),(41,45,'excelentes'))
# acampo4
acampo4=((0,2,'reduzidos'),(3,4,'medianos'),(5,5,'elevadas'))
# acampo5
acampo5=((0,2,'reduzidos'),(3,4,'médias'),(5,5,'elevados'))
# acampo6
acampo6=((0,2,'fracos'),(3,4,'satisfatórios'),(5,5,'elevados'))
# acampo7
acampo7=((0,2,'pouco'),(3,4,'bastante'),(5,5,'quase sempre'))
# acampo8
acampo8=((0,4,'mal'),(5,9,'satisfatoriamente'),(10,10,'de forma
exemplar'))
# acampo9
acampo9=((0,2,'muito reduzidos'),(3,4,'reduzidos'),
(5,8,'satisfatórios'),(9,10,'elevados'))
# acampo10
acampo10=((0,2,'pouco'),(3,4,'bastante'),(5,5,'muito'))
# acampo11
acampo11=((0,2,'muito pouco'),(3,4,'pouco'),(5,8,'bastante'),
(9,10,'grande'))

campo1=res[0]
campo2=res[1]
campo3=res[2]
campo4=res[3]
campo5=res[4]
campo6=res[5]
campo7=res[6]
campo8=res[7]
campo9=res[8]
campo10=res[9]
campo11=res[10]

for a in acampo3:
x=a[0]
y=res[2]
z=a[1]
print x
print y
print z
print x < y
print y < z
print z < y
if a[0] <= res[2] <= a[1]:
campo3=a[2]

for a in acampo4:
if (res[3]>=a[0] and res[3]<=a[1]):
campo4=a[2]

for a in acampo5:
if (res[4]>=a[0] and res[4]<=a[1]):
campo5=a[2]

for a in acampo6:
if (res[5]>=a[0] and res[5]<=a[1]):
campo6=a[2]

for a in acampo7:
if (res[6]>=a[0] and res[6]<=a[1]):
campo7=a[2]

for a in acampo8:
if (res[7]>=a[0] and res[7]<=a[1]):
campo8=a[2]

for a in acampo9:
if (res[8]>=a[0] and res[8]<=a[1]):
campo9=a[2]

for a in acampo10:
if (res[9]>=a[0] and res[9]<=a[1]):
campo10=a[2]

for a in acampo11:
if (res[10]>=a[0] and res[10]<=a[1]):
campo11=a[10]

...

return frase

# processar

f=open('leituras.csv','rb')
csv.register_dialect('dialecto', delimiter=';',
quoting=csv.QUOTE_NONE)
leitor =csv.reader(f,'dialecto')

for res in leitor:
print res
print gera_string(res)

f.close()

quit()

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


Re: DNS servers in Python - which ones are a good base for work?

2007-12-11 Thread Jean-Paul Calderone
On Tue, 11 Dec 2007 11:10:52 -0800, John Nagle <[EMAIL PROTECTED]> wrote:
>   I need to do a non-standard DNS server in Python.  This
>is for a spam blacklist type DNS server, not for IP lookup.
>"dnspython" seems to be client side only.  Oak DNS is
>deprecated.  Suggestions?
>

There's Twisted Names.  I wrote a custom DNS server with it just last week.
The documentation is a bit sparse, but it's not particularly complicated.

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


Re: newbie

2007-12-11 Thread Steven D'Aprano
On Mon, 10 Dec 2007 19:25:53 -0800, farsheed wrote:

> On Dec 11, 5:03 am, "Whizzer" <[EMAIL PROTECTED]> wrote:
>> Is OReilly's Learning Python a good place to start learning to program?
>> I've been told Python is a good first language.
>>
>> Thanks for the advice.
> 
> I learn it from python own tutorials comes with it's installation. It is
> so important that you do not study bad books. there are lots a of bad
> books about python that you can't imagine. I rate OReilly's Learning
> Python 3 stars.

Is that 3 stars out of 3, or 3 stars out of 10?


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


Re: Is a "real" C-Python possible?

2007-12-11 Thread Chris Mellon
On Dec 11, 2007 1:25 PM, John Nagle <[EMAIL PROTECTED]> wrote:
> sturlamolden wrote:
> > On 10 Des, 23:49, [EMAIL PROTECTED] (Aahz) wrote:
> >
> >> "Premature optimization is the root of all evil in programming."
> >> --C.A.R. Hoare (often misattributed to Knuth, who was himself quoting
> >> Hoare)
>
> We're ten years into Python, and it's still a naive interpreter.

This is an absurd misrepresentation of the state of the Python VM.

> It's time for a serious optimizing compiler.  Shed Skin is going
> in the right direction.  But for some reason, people seem to dislike the
> Shed Skin effort. Its author writes "Am I the only one seeing the potential
> of an implicitly statically typed Python-likea-lnguage that runs at
> practically the same speed as C++?"
>
> "For a set of 27 non-trivial test programs (at about 7,000 lines in total;
> ... measurements show a typical speedup of 2-40 times over Psyco, about 10 on
> average, and 2-220 times over CPython, about 35 on average."  So that's
> what's possible.
>

... with roughly a hundredth of the python standard library, and a
bunch of standard python features not even possible. I like
generators, thanks.

If shedskin can actually match Pythons feature set and provide the
performance it aspires to, thats great, and I may even start using it
then. But in the meantime, hardly anything I write is CPU bound and
when it is I can easily optimize using other mechanisms. Shedskin
doesn't give me anything that's worth my time to improve on it, or the
restrictions it places on my code. I think JIT is the future of
optimization anyway.

> I'm surprised that Google management isn't pushing Guido towards
> doing something about the performance problem.
>

Assuming your conclusion (ie, that there's a performance problem to do
something about) doesn't prove your case.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding dir of main .py file

2007-12-11 Thread Matt Nordhoff
ron.longo wrote:
> Nope, maybe I'm not explaining myself well.
> 
> When I do os.getenv('HOME') I get back None.
> 
> According to the docs, 'HOME' is the user's home directory on some
> platforms.  Which is not what I want.
> 
> What I want is the directory in which an application's main .py file
> resides.  That is, when I type: python MyApp.py, I want to know in which
> directory does MyApp.py reside?

Shane is right.

>>> print __file__

>>> print modulename.__file__

Just call os.path.dirname() on __file__ to get the directory.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help needed with python unicode cgi-bin script

2007-12-11 Thread John Machin
On Dec 12, 4:46 am, "weheh" <[EMAIL PROTECTED]> wrote:
> Hi John:
> Thanks for responding.
>
> >Look at your file using
>
>  >   print repr(open('c:/test/spanish.txt','rb').read())
>
> >If you see 'a\xf1o' then use charset="windows-1252"
>
> I did this ... no change ... still see 'a\xf1o'

So it's not utf-8, it's windows-1252, so stop lying to browsers: like
I said, use charset="windows-1252"

>
> >else if you see 'a\xc3\xb1o' then use charset="utf-8" else 
> >Based on your responses to Martin, it appears that your file is
> >actually windows-1252 but you are telling browsers that it is utf-8.
> >Another check: if the file is utf-8, then doing
>
>  >   open('c:/test/spanish.txt','rb').read().decode('utf8')>should be OK; if 
> it's not valid utf8, it will complain.
>
> No. this causes decode error:
>
> UnicodeDecodeError: 'utf8' codec can't decode bytes in position 1-4: invalid
> data

No what? YES, the "decode error" is complaining that the data supplied
is NOT valid utf-8 data. So it's not utf-8, it's windows-1252, so stop
lying to browsers: like I said, use charset="windows-1252"

>   args = ('utf8', 'a\, 1, 5, 'invalid data')
>   encoding = 'utf8'
>   end = 5
>   object = 'a\xf1o'
>   reason = 'invalid data'
>   start = 1
>
> >Yet another check: open the file with Notepad. Do File/SaveAs, and
> >look at the Encoding box -- ANSI or UTF-8?
>
> Notepad says it's ANSI

That's correct (in Microsoft jargon) -- it's NOT utf-8. It's
windows-1252, so stop lying to browsers: like I said, use
charset="windows-1252"

>
> Thanks. What now?

Listen to the Bellman: "What I tell you three times is true".
Your file is encoded using windows-1252, NOT utf-8.
You need to use charset="windows-1252".


> Also, this is a general problem for me, whether I read
> from a file or read from an html text field, or read from an html text area.
> So I'm looking for a general solution. If it helps to debug by reading from
> textarea or text field, let me know.

If you are creating a file, you should know what its encoding is. As I
said earlier, *every* file is encoded -- so-called "Unicode" files on
Windows are encoded using utf16le. If you don't explicitly specify the
encoding, it will typically be the default encoding for your locale
(e.g. cp1252 in Western Europe etc).

If you are reading a file created by others and its encoding is not
known, you will have inspect the file and/or guess (using whatever
knowledge you have about the language/locale of the creator).

"whether I ... read from an html text field, or read from an html text
area": isn't that what "charset" is for?

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


cadastros de e-mails por estados

2007-12-11 Thread pesquisa
listas de email marketing livre de spam Maladireta e-mails para mala-
direta. mala direta, emails segmentados, e-mail marketing.

Visite agora:
http://www.divulgaemails.com

Listas de e-mails, lista de emails, compra de emails, maladireta, mala
direta segmentada, , mala direta por e-mail. Mala Direta, Fazer Mala
Direta, Cadastro de Mala Direta, Lista de Mala Direta, Endereço para
Mala
Direta, Maladireta,  Telemarketing, Enviar Mala Direta, Envio de Mala
Direta.

http://www.divulgaemails.com

mala direta,modelo, gratis,e-mail para, por email, virtual, trabalho
em
casa,fazer, software, mail


Visite agora:
http://www.divulgaemails.com

divulga email, mala direta, lista de emails, e-mails, email, lista de
e-mails,  lista de email, lista de e-mail, mailing list, emails,
divulgação por email, divulgação,  email para mala direta, divulga,
email
marketing,  spam, fazer spam, divulgue,  milhões de emails, milhões de
email.


Os melhores Modelos de Contratos e Cartas Comerciais. Como escrever
uma
proposta, ducumento ou carta comercial:

Visite também:

http://www.modelosdecartascomerciais.com



cadastros de e-mails por estados
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tuples !?!?

2007-12-11 Thread Grant Edwards
On 2007-12-11, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Hi,
>
> Is the tuple comparison brooked in python ?!?!?

No.

> Thanks.

You're welcome.

> Try this and you will see funny things:

No thanks.

Maybe you could to post a smaller, easier to read example of
what you think is broken?

-- 
Grant Edwards   grante Yow! I demand IMPUNITY!
  at   
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Zipfile content reading via an iterator?

2007-12-11 Thread Tim Chase
I'm dealing with several large items that have been zipped up to 
get quite impressive compression.  However, uncompressed, they're 
large enough to thrash my memory to swap and in general do bad 
performance-related things.  I'm trying to figure out how to 
produce a file-like iterator out of the contents of such an item.

 >>> z = zipfile.zipFile("test.zip")
 >>> info = z.getinfo("data.txt")
 >>> info.compress_size
132987864
 >>> info.file_size
1344250972
 >>> len(z.namelist())
20

I need to be able to access multiple files within it, but I can 
iterate over each one, only seeing small slices of the file. 
Using the read() method triggers the volumnous read.  Thus what I 
have to do currently:

 >>> content = z.read("data.txt") # ouch!
 >>> len(content)
1344250972
 >>> for row in content.splitlines(): process(row) # pain!

What I'm trying to figure out how to do is something like the 
mythical:

 >>> for row in z.file_iter("data.txt"): process(row) # aah

to more efficiently handle the huge stream of data.

Am I missing something obvious?  It seems like iterating over zip 
contents would be a common thing to do (especially when compared 
to reading the whole contents...I mean, they're zipped because 
they're big! :)

Thanks for any pointers,

-tkc



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


Re: I'm missing something here with range vs. xrange

2007-12-11 Thread Hrvoje Niksic
"Joe Goldthwaite" <[EMAIL PROTECTED]> writes:

> To do a range(100)
>
>   1. Allocate a big chunk of memory
>   2. Fill the memory with the range of integers
>   3. Setup an index into the memory array
>   4. Every time the program asks for the next item, bump
>  the memory index by one, get the value and return it.
>
> To do an xrange(1000)
>
>   1. Set up a counter
>   2. Every time the program asks for the next item, increment
>  the counter and return it.
>
> I guess I thought that between these two methods, the second would be
> dramatically faster. An order of magnitude faster.

You're forgetting that your test iterates over all the elements in
both cases.  The accumulated cost of each iteration simply obscures
the added cost of ahead-of-time allocation and list setup, especially
as the latter is implemented in fairly optimized C.

In other words, the complexity of the task is the same is both cases,
O(n), and your timing reflects that.  When you think about it that
way, there is no reason for the entire loop to be an order of
magnitude slower -- in fact, if it were, it would be a bug in "range".
"range" will only become an order (or more) of magnitude slower when
you start allocating extremely large lists and it starts having to
allocate virtual memory off the system's swap.

The operation where xrange excels is the list setup step:

$ python -m timeit 'xrange(100)'
100 loops, best of 3: 0.463 usec per loop
$ python -m timeit 'range(100)'
10 loops, best of 3: 35.8 msec per loop

xrange finishes in sub-microsecond time, while range takes tens of
milliseconsd, being is ~77000 times slower.  This result follows your
reasoning: because range needs to allocate and set up the list in
advance, it is orders of magnitude slower.

> Hence, the title, "I'm missing something".

I hope this clears it up for you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are Python deques linked lists?

2007-12-11 Thread Hrvoje Niksic
Neil Cerutti <[EMAIL PROTECTED]> writes:

> Anyhow, implementing linked lists in Python is not challenging, but
> they don't work well with Python iterators, which aren't suitable
> for a linked list's purposes--so you have to give up the happy-joy
> for loop syntax in favor of Python's frowny-sad while loops.

With the help of generators, it is trivial to turn a frowny loop into
a happy one:

class Node:
def __init__(self):
self.next = None
# attach data to the node as needed, and use "next" to refer
# to the next node.

def __iter__(self):
node = self
while 1:
yield node
node = node.next
if node is None:
break

def linkify(it):
"""Turn a Python iterable into a linked list."""
head = prev = None
for elem in it:
node = Node()
node.data = elem
if prev is None:
head = node
else:
prev.next = node
prev = node
return head

# iterate over a linked list using 'for':

>>> linkedlist = linkify([1, 2, 3])
>>> for n in linkedlist:
...   print n.data
...
1
2
3

Correctly handling empty lists is left as an excercise for the reader.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-11 Thread Adam Funk
On 2007-12-10, sturlamolden wrote:

> We have seen several examples that 'dynamic' and 'interpreted'
> languages can be quite efficient: There is an implementation of Common
> Lisp - CMUCL - that can compete with Fortran in efficiency for
> numerical computing. There are also versions of Lisp than can compete
> with the latest versions of JIT-compiled Java, e.g. SBCL and Allegro.
> As it happens, SBCL and CMUCL is mostly implemented in Lisp. The issue
> of speed for a language like Python has a lot to do with the quality
> of the implementation. What really makes CMUCL shine is the compiler
> that emits efficient native code on the fly. If it is possible to make
> a very fast Lisp, it should be possible to make a very fast Python as
> well. I remember people complaining 10 years ago that 'Lisp is so
> slow'. A huge effort has been put into making Lisp efficient enough
> for AI. I hope Python some day will gain a little from that effort as
> well.

I've been told that Torbjörn Lager's implementation of the Brill
tagger in Prolog is remarkably fast, but that it uses some
counter-intuitive arrangements of the predicate and argument
structures in order to take advantage of the way Prolog databases are
indexed.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-11 Thread Hrvoje Niksic
sturlamolden <[EMAIL PROTECTED]> writes:

> On 10 Des, 23:54, Bruno Desthuilliers
> <[EMAIL PROTECTED]> wrote:
>
>> Or a lack of time and money. Lisp is one of the older programming
>> languages around, and at a time had BigBucks(tm) invested on it to try
>> and make it practically usable.
>
> Yes. But strangely enough, the two Lisp implementations that really
> kick ass are both free and not particularly old.

Not two, but one -- SBL is simply a fork of CMU CL.  As for their age,
the CMU CL states that it has been "continually developed since the
early 1980s".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are Python deques linked lists?

2007-12-11 Thread Hrvoje Niksic
Duncan Booth <[EMAIL PROTECTED]> writes:

>> I do have one last question about a doubly-linked list. Would you
>> have to perform any tricks (del statements) to get the garbage
>> collector to collect every node, or will it just work?
>
> It should just work.

Fortunately that's trivial to verify:

class Node(object):
def __init__(self, prev):
self.prev = prev
if prev is not None:
prev.next = self
self.next = None

Now, simply create several nodes in a loop:

while 1:
 head = Node(None)
 node = Node(head)
 node = Node(node)

If the Python process grows without bounds, the GC failed to do its
job.

GC in recent Python releases handles cyclic references flawlessly, at
least in the types shipped.  For comparison, the equivalent Perl code
leaks like a hose.
-- 
http://mail.python.org/mailman/listinfo/python-list


Standard Python 2.4 module like "crypt" for Windows?

2007-12-11 Thread dananrg
Is there a module available in the standard library, for Python 2.4
running on Windows, like "crypt" for Python 2.4 running on *nix
machines?

I need to store database passwords in a Python 2.4 script, but
obviously don't want them in clear text.

I'm looking for a reasonable level of security. What are some of my
options?

Also wondering if, for PythonWin, there is a module that will display
asterisks  rather than echo entered passwords in clear text. I see I
have access to the getpass module, but I can't get it to work--it
still echos typed-in passwords to the screen as clear text.
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   >