Re: xml in python

2009-05-10 Thread Piet van Oostrum
> Shawn Milochik  (SM) wrote:

>SM> On Fri, May 8, 2009 at 3:46 AM, Rustom Mody  wrote:
>>> Can someone give me a heads up on xml parsing in python?
>>> The context is that I want to write a simple docbook to text converter.
>>> DOM is alright -- dont want to break my head with SAX just for performance
>>> when my documents are not likely to be large.
>>> 
>>> My problem is that there seems to be so many nearly equivalent ways (Pyxml?
>>> Amara?) some of which have moved from 3rd party to builtin status that I am
>>> not clear what is the current method of choice.
>>> 
>>> Thanks
>>> --

>SM> I've been using minidom for simple XML processing with success. The
>SM> XML module seems to be the way to go, considering how many of its
>SM> child modules are listed on http://docs.python.org/library/.

>SM> import xml.dom.minidom as minidom

These days ElementTree is considered the most pythonic way.
http://docs.python.org/library/xml.etree.elementtree.html

There is also a reimplementation of the ElementTree API based on libxml2
and libxslt, which has more features but requires a separate install. It
is largely compatible with ElementTree, however.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with datetime.datetime.strptime

2009-05-10 Thread Nick Craig-Wood
Tuomas Vesterinen  wrote:
>  I hoped that I could get rid of my special module _strptime2 when 
>  porting to Python 3.0. But testing is a disappointment.
[snip]
>  C :
> strftime('%a %b %e %H:%M:%S %Y')='Sat May  9 11:26:12 2009'
> strptime('Sat May  9 11:26:12 2009','%a %b %e %H:%M:%S %Y')=
>   'e' is a bad directive in format '%a %b %e %H:%M:%S %Y'
[snip]
>  What to do.

I'd start by checking out the code for python 3 trunk and seeing if I
could fix it. If I could I'd update the unit tests and the
documentation then submit the patch to the python bugtracker.

If I couldn't fix it then I'd report it as a bug.

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode bit me

2009-05-10 Thread Nick Craig-Wood
anuraguni...@yahoo.com  wrote:
>  First of all thanks everybody for putting time with my confusing post
>  and I apologize for not being clear after so many efforts.
> 
>  here is my last try (you are free to ignore my request for free
>  advice)
> 
>  # -*- coding: utf-8 -*-
> 
>  class A(object):
> 
>  def __unicode__(self):
>  return u"©au"
> 
>  def __repr__(self):
>  return unicode(self).encode("utf-8")
> 
>  __str__ = __repr__
> 
>  a = A()
>  u1 = unicode(a)
>  u2 = unicode([a])
> 
>  now I am not using print so that doesn't matter stdout can print
>  unicode or not
>  my naive question is line u2 = unicode([a]) throws
>  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
>  1: ordinal not in range(128)
> 
>  shouldn't list class call unicode on its elements? 

You mean when you call unicode(a_list) it should unicode() on each of
the elements to build the resultq?

Yes that does seem sensible, however list doesn't have a __unicode__
method at all so I guess it is falling back to using __str__ on each
element, and which explains your problem exactly.

If you try your example on python 3 then you don't need the
__unicode__ method at all (all strings are unicode) and you won't have
the problem I predict. (I haven't got a python 3 in front of me at the
moment to test.)

So I doubt you'll find the momentum to fix this since unicode and str
integration was the main focus of python 3, but you could report a
bug.  If you attach a patch to fix it - so much the better!

Here is my demonstration of the problem with python 2.5.2

>> class A(object):
... def __unicode__(self):
... return u"\N{COPYRIGHT SIGN}au"
... def __repr__(self):
... return unicode(self).encode("utf-8")
... __str__ = __repr__
...
>>> a = A()
>>> str(a)
'\xc2\xa9au'
>>> repr(a)
'\xc2\xa9au'
>>> unicode(a)
u'\xa9au'
>>> L=[a]
>>> str(L)
'[\xc2\xa9au]'
>>> repr(L)
'[\xc2\xa9au]'
>>> unicode(L)
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
1: ordinal not in range(128)
>>> unicode('[\xc2\xa9au]')
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
1: ordinal not in range(128)
>>> L.__unicode__
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'list' object has no attribute '__unicode__'
>>> unicode(str(L),"utf-8")
u'[\xa9au]'

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Help needed with filenames

2009-05-10 Thread pdenize
I have a program that reads files using glob and puts them into an XML
file in UTF-8 using
  unicode(file, sys.getfilesystemencoding()).encode("UTF-8")
This all works fine including all the odd characters like accents etc.

However I also print what it is doing and someone pointed out that
many characters are not printing correctly in the Windows command
window.

I have tried to figure this out but simply get lost in the translation
stuff.
if I just use print filename it has characters that dont match the
ones in the filename (I sorta expected that).
So I tried print unicode(file, sys.getfilesystemencoding()) expecting
the correct result, but no.
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2013'

I did notice that when a windows command window does a directory
listing of these files the characters seem to be translated into close
approximations (long dash to minus, special double quotes to simple
double quotes, but still retains many of the accent chars).  I looked
at translate to do this but did not know how to determine which
characters to map.

Can anyone tell me what I should be doing here?
--
http://mail.python.org/mailman/listinfo/python-list


Re: import overwrites __name__

2009-05-10 Thread Peter Otten
MRAB wrote:

> Marco wrote:
>> Hi,
>> 
>> There happened something that I do not understand. Actually I don't even
>> know how it can be possible.
>> 
>> I import a module and then the name space of the importing module seems
>> do be overwritten.
>> 
>> my_name = __name__
>> print my_name
>> print len(dir())
>> from x import y as z
>> print __name__
>> print len(dir())
>> print my_name
>> 
>> ->
>> __main__
>> 119
>> x
>> 117
>> unhandled NameError "name 'my_name' is not defined"
>> 
>> The module x is from me, and I am not aware of doing anything cruel
>> there. What evil thing can be done in its sub module that can result in
>> that strange thing?
>> 
> You'd have to show us the module. Try to make the smallest module that
> has the same effect.

$ cat x.py
import sys
globals().update(zip(*(range(110),)*2))
y = 42
print __name__
if __name__ == "__main__":
a = b = 42
print len(dir())
from x import y as z
try:
print my_name
except NameError, e:
print 'unhandled NameError: "%s"' % e
sys.exit()

$ python x.py
__main__
119
x
117
unhandled NameError: "name 'my_name' is not defined"

;)


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


Re: xml in python

2009-05-10 Thread uuid

On 2009-05-10 09:24:36 +0200, Piet van Oostrum  said:




These days ElementTree is considered the most pythonic way.
http://docs.python.org/library/xml.etree.elementtree.html

There is also a reimplementation of the ElementTree API based on libxml2
and libxslt, which has more features but requires a separate install. It
is largely compatible with ElementTree, however.


Indeed - elementtree and its blazing C implementation celementtree are 
included in the standard install.


However, I would also recommend checking out the latter option - lxml. 
It's very fast, has an elementtree compatible API and sports some 
tricks like XPath and XSLT. There's comprehensive documentation as well 
als tutorials, hints and tips here: http://codespeak.net/lxml/


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


Re: Help needed with filenames

2009-05-10 Thread Martin v. Löwis
> Can anyone tell me what I should be doing here?

The console uses the "OEM code page". The Windows conversion
routine from Unicode to the OEM code page provides the lossy
conversion that you observe in listing.

Unfortunately, the OEM code page conversion is not available
from Python. What you can do is to use

  u.encode(sys.stdout.encoding, "replace")

This will replace unprintable characters with question marks.

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


Re: Help needed with filenames

2009-05-10 Thread Yinon Ehrlich

> I did notice that when a windows command window does a directory
> listing of these files the characters seem to be translated into close
> approximations (long dash to minus, special double quotes to simple
> double quotes, but still retains many of the accent chars).  I looked
> at translate to do this but did not know how to determine which
> characters to map.
>
> Can anyone tell me what I should be doing here?

Hi,

Seems like your problem is just with the correct representation of
those characters in Windows command-line.
I have seen two solutions for that:
* Right-click on the top bar of the the command-line window, go to
"properties--> Font", select a font that shows all characters
correctly.
* Even better: use http://sourceforge.net/projects/console - much
better that Windows' default console.

Good luck,
  Yinon
--
http://mail.python.org/mailman/listinfo/python-list


Re: Nimrod programming language

2009-05-10 Thread Paul Rubin
Andreas Rumpf  writes:
> I invented a new programming language called "Nimrod" that combines
> Python's readability with C's performance. Please check it out:
> http://force7.de/nimrod/ Any feedback is appreciated.

Looks nice in many ways.  You also know about PyPy and Felix
(felix.sf.net)?

It seems a little bit hackish that the character type is a byte.
Python did something like that but it ended up causing a lot of
hassles when dealing with real Unicode.  I think you have to bite
the bullet and have separate byte and char types, where char=unicode.

It scares me a little about how there are references that can have
naked pointers, escape the gc, etc.  It would be good if the type
system were powerful enough to guarantee the absence of these effects
in some piece of data, unless the data is somehow marked with an
"unsafe" type.

I'd like to see ML-like algebraic types with pattern matching, not
just tuple unpacking.  

The generic system should be able to analyze the AST of type
parameters, e.g. iterator would be able to automatically generate
an iterator over a list, a tree, or some other type of structure, by
actually figuring out at compile time how T is constructed.

It's sort of surprising that the compiler is written in a Pascal
subset.  Why not implement in Nimrod directly, and bootstrap through C?

The language and library are missing arbitrary precision integer
arithmetic, using GMP or something like that.

It would be good to also be able to have value serialization and
deserialization to both human readable (nested lists, etc) and binary
(for IPC) formats.

It's not obvious from the blurbs whether simple functional programming
is supported, e.g. how would you implement a function like "map" or
"filter"?  What would the types be of those two functions?

The roadmap says you might implement threading with transactional
memory.  How will the transactional memory interact with mutable data?
Are you going to use OS threads, lightweight threads, or both?
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode bit me

2009-05-10 Thread anuraguni...@yahoo.com
ok that explains it,
so
unicode(obj) calls __unicode__ on that object and if it isn't there
__repr__ is used
__repr__ of list by default return a str even if __repr__ of element
is unicode


so my only solution looks like to use my own list class everywhere i
use list
class mylist(list):
def __unicode__(self):
return u"["+u''.join(map(unicode,self))+u"]"
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode bit me

2009-05-10 Thread Diez B. Roggisch

anuraguni...@yahoo.com schrieb:

ok that explains it,
so
unicode(obj) calls __unicode__ on that object and if it isn't there
__repr__ is used
__repr__ of list by default return a str even if __repr__ of element
is unicode


so my only solution looks like to use my own list class everywhere i
use list
class mylist(list):
def __unicode__(self):
return u"["+u''.join(map(unicode,self))+u"]"


Or you use a custom unicode_list-function whenever you care to print out 
 a list.


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


Re: "Beginning Python Visualization" by Shai Vaingast

2009-05-10 Thread Shawn Milochik
Thanks for the review and the podcast. I ordered the book on Friday. I
look forward to playing with it. Also (assuming you're Ron Stephens),
thanks for the Python 411 podcast. It's a great resource, and I
recommend it to all list members.

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


Re: Wrapping comments

2009-05-10 Thread Steven D'Aprano
On Sun, 10 May 2009 13:41:15 +1200, Lawrence D'Oliveiro wrote:

> If I had to print out all the code and documentation I have to look at,
> I'd need to add another room to my house.


You are allowed to throw it away when you're done with it :)



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


Java-style futures in Python - only better

2009-05-10 Thread Brian Quinlan

Hey all,

I've been working on an Java-style futures implementation in Python. 
Futures are a way of representing asynchronous operations e.g. 
operations that are run in another thread or process. The are are a easy 
but powerful way of parallelizing sequential operations. The also 
provide a consistent interface across implementations e.g. they can 
provide the same interface to threading and to multiprocessing.


For example:

def is_prime(n):
"Return True iff n is prime"
...

def check_primes(numbers):
return map(is_prime, numbers)

Could be parallelized as:

def check_primes(numbers):
# ProcessPoolExecutor will create one worker process
# per CPU if called without arguments. Using threads
# is valueless because of the GIL.
with futures.ProcessPoolExecutor() as executor:
return executor.map(is_prime, numbers)

A more complex example:

def load_url(url, timeout):
return urllib.request.urlopen(url, timeout=timeout).read()

### Download the content of some URLs - ignore failures.
def download_urls(urls, timeout=60):
url_to_content = {}
for url in urls:
try:
url_to_content[url] = load_url(url, timeout=timeout)
except:
pass
return url_to_content

Could be parallelized as:

# Making this a global variable used any many functions ensures that
# the global thread count is kept under control.
executor = futures.ThreadPoolExecutor(50)
def download_urls(urls, timeout=60):
url_to_content = {}
# Run load_url for every url and get the result as a FuturesList.
fs = executor.run_to_futures(
(functools.partial(load_url, url, timeout) for url in urls),
timeout=timeout)
for url, future in zip(urls, fs.successful_futures()):
url_to_content[url] = future.result()
return url_to_content


The Python 3.0+ code is here:
http://code.google.com/p/pythonfutures/source

Any feedback on the API would be very much appreciated!

Cheers,
Brian


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


Missing c.l.py posts (was Re: php to python code converter)

2009-05-10 Thread Aahz
In article ,
MRAB   wrote:
>J. Cliff Dyer wrote:
>> On Fri, 2009-05-08 at 17:19 +0200, Pascal Chambon wrote:
>>>
>>> PS : Am I the only one having most of answers rejected by the
>>> antispam 
>>> system of python-list ? That's humiliating :p
>> 
>> I've had several messages not make it through.
>> 
>> :(
> 
>Same here ("Message has a suspicious header").

I've been trying to investigate several instances of posts to
comp.lang.python.announce getting approved and making it out to the
mailing list but not the newsgroup.  In each case, reposting as plain
text fixed the problem.  I suggest that anyone having similar problems on
c.l.py do the same thing.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
--
http://mail.python.org/mailman/listinfo/python-list


Learning C++ for Python Development

2009-05-10 Thread joshua.pea...@gmail.com
I am a recovering C# web developer who has recently picked up Django
and I'm loving it.

I would eventually like to get a job as a Django/Python developer. It
seems that many Python jobs require that you also be a C++ developer.
While I want to remain primarily a web developer, I don't want be
stuck doing CRUD applications, so I would like to learn C++ for Python
development. I have taken two basic programming courses in straight up
C++, no STL, Boost or anything like that, but I do have a very basic
knowledge of the language.

Can you folks suggest some Python packages which use C++ and are
relevant for web development, so I can dig through the source and
contribute in some way?

Or, just give me some general advice on learning C++ for Python?

P.S. I want to develop on Linux not Windows.

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


Re: OT (humor): 'import antigravity' in action!

2009-05-10 Thread Aahz
In article ,
Shawn Milochik   wrote:
>
>I know you've probably all seen this 50 times, but just in case:
>http://xkcd.com/353/
>
>And here's the result:
>http://icanhascheezburger.com/2009/05/06/funny-pictures-behavior-20/

Thanks!
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I test the integrity of a Python installation in Debian and Ubuntu

2009-05-10 Thread Aahz
In article ,
Geoff Gardiner   wrote:
>
>geg...@gegard:~$ python
>Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
>[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
 from test import regrtest
 regrtest.main()
>test_grammar
>test_grammar skipped -- No module named test_grammar
>...  more of the same...
>9 tests skipped:
>test_builtin test_doctest test_doctest2 test_exceptions
>test_grammar test_opcodes test_operations test_types test_unittest
>Traceback (most recent call last):
>  File "", line 1, in 
>  File "/usr/lib/python2.5/test/regrtest.py", line 416, in main
>e = _ExpectedSkips()
>  File "/usr/lib/python2.5/test/regrtest.py", line 1321, in __init__
>from test import test_socket_ssl
>ImportError: cannot import name test_socket_ssl

What directory are you running this from?  What happens if you switch to
running "python Lib/test/regrtest.py"?  Taking a closer look, this looks
more like a plain import error.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
--
http://mail.python.org/mailman/listinfo/python-list


Q's on my first python script

2009-05-10 Thread kj

Below is my very firs python script.

This was just a learning exercise; the script doesn't do anything
terribly exciting: for an argument of the form YYMMDD (year, month,
day) it prints out the corresponding string YYMMDDW, where W is a
one-letter abbreviation for the day of the week.  E.g.

% wd 090511
090511M

The script runs OK, but I can still see a few areas of improvement,
for which I could use your advice.

1. The name of the BadArgument exception class defined in the script
   does not seem to me sufficiently specific.  If one were to import
   the script in order to reuse its wkday_abbrev function, I'd like
   this exception's name to be more unequivocally tied to this
   script.  What I'm looking for is something like a "namespace"
   for this script.  What's the pythonic way to construct a namespace?

2. In some python modules I've seen the idiom

   if __name__ == "__main__":
  # run some tests here

   I'd like to set up tests for this script, mostly to ensure that
   it handles the error cases properly, but I'm alread using the
   idiom above to actually run the script under normal operation.
   What's the typical python idiom for running tests on a *script*
   (as opposed to a module that is normally not supposed to be run
   directly)?

3. Still on the subject of testing, how does one capture in a
   variable the output that would normally have gone to stdout or
   stderr?

4. What's the python way to emit warnings?  (The script below should
   warn the user that arguments after the first one are ignored.)

5. The variable wd is meant to be "global" to the script.  In other
   languages I've programmed in I've seen some typographic convention
   used for the name of such variables (e.g. all caps) to signal
   this widened scope.  Does python have such a convention?

Any comments/suggestions on these questions, or anything else about
the script, would be much appreciated.

TIA!

kynn



from optparse import OptionParser
import re
import datetime
import sys

class BadArgument(Exception): pass

wd = ("M", "T", "W", "H", "F", "S", "U")

def wkday_abbrev(str):
  try:
mm = re.match("(\d{2})(\d{2})(\d{2})\Z", str)

y, m, d = map(lambda x: int(mm.group(x)), (1,2,3))

if y < 38: y = y + 1900
else: y = y + 2000

return wd[datetime.datetime(y, m, d).weekday()]
  except (AttributeError, ValueError):
raise BadArgument()


def main():

  usage = '''Usage: %prog [options] YYMMDD
   %prog -h|--help
'''

  parser = OptionParser(usage=usage)
  parser.add_option("-n", "--no-newline", dest="nonl",
action="store_true", help="omit newline in output")

  (options, args) = parser.parse_args();

  try:
sys.stdout.write("%s%s" % (args[0], wkday_abbrev(args[0])))
if not options.nonl: print

  except (IndexError, BadArgument):
print usage
sys.exit(1)
  except: raise

if __name__ == "__main__": main()

-- 
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list


Missing c.l.py posts (was Re: php to python code converter)

2009-05-10 Thread skip

 PS : Am I the only one having most of answers rejected by the
 antispam system of python-list ? That's humiliating :p
>>> 
>>> I've had several messages not make it through.
>>> 
>>> :(
>> 
>> Same here ("Message has a suspicious header").

Aahz> I've been trying to investigate several instances of posts to
Aahz> comp.lang.python.announce getting approved and making it out to
Aahz> the mailing list but not the newsgroup.  In each case, reposting
Aahz> as plain text fixed the problem.  I suggest that anyone having
Aahz> similar problems on c.l.py do the same thing.

The problem lies with moi.  The ham/spam database had grown rather huge.  I
tossed out a bunch of older ham and spam messages from the database and
retrained.  That caused a large fraction of new posts to be held as
"unsure".  Unfortunately, I didn't notice that for a couple days.  I added a
couple hundred of the recently held hams and spams to the database and
retrained last night.  This morning nothing new was held.  Hopefully that
will solve the problem.

-- 
Skip Montanaro - s...@pobox.com - http://www.smontanaro.net/
America's vaunted "free press" notwithstanding, story ideas that expose
the unseemly side of actual or potential advertisers tend to fall by the
wayside.  Not quite sure why.  -- Jim Thornton
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I test the integrity of a Python installation in Debian and Ubuntu

2009-05-10 Thread Geoff Gardiner
Aahz wrote:
> 
> What directory are you running this from?  What happens if you switch to
> running "python Lib/test/regrtest.py"?  Taking a closer look, this looks
> more like a plain import error.
>   

Thank you for your suggestion.

I couldn't do quite that because there's no Lib, but instead (in Ubuntu
Hardy, this time):

geg...@gegard:~$ cd /usr/lib/python2.5/
geg...@gegard:/usr/lib/python2.5$ python test/regrtest.py
test_grammar
test_grammar skipped -- No module named test_grammar
test_opcodes
test_opcodes skipped -- No module named test_opcodes
test_operations
test_operations skipped -- No module named test_operations
test_builtin
test_builtin skipped -- No module named test_builtin
test_exceptions
test_exceptions skipped -- No module named test_exceptions
test_types
test_types skipped -- No module named test_types
test_unittest
test_unittest skipped -- No module named test_unittest
test_doctest
test_doctest skipped -- No module named test_doctest
test_doctest2
test_doctest2 skipped -- No module named test_doctest2
9 tests skipped:
test_builtin test_doctest test_doctest2 test_exceptions
test_grammar test_opcodes test_operations test_types test_unittest
Traceback (most recent call last):
  File "test/regrtest.py", line 1384, in 
main()
  File "test/regrtest.py", line 416, in main
e = _ExpectedSkips()
  File "test/regrtest.py", line 1321, in __init__
from test import test_socket_ssl
ImportError: cannot import name test_socket_ssl
geg...@gegard:/usr/lib/python2.5$

Also
geg...@gegard:~$ locate */test_socket_ssl.*
geg...@gegard:~$ #returns nothing

And
geg...@gegard:~$ locate /usr/lib/python2.5/test/test_*.*
/usr/lib/python2.5/test/test_support.py
/usr/lib/python2.5/test/test_support.pyc
geg...@gegard:~$

All the best,
Geoff


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


Re: Get multiprocessing.Queue to do priorities

2009-05-10 Thread Jesse Noller
On Sat, May 9, 2009 at 6:11 PM, uuid  wrote:
> The Queue module, apparently, is thread safe, but *not* process safe. If you
> try to use an ordinary Queue, it appears inaccessible to the worker process.
> (Which, after all, is quite logical, since methods for moving items between
> the threads of the same process are quite different from inter-process
> communication.) It appears that creating a manager that holds a shared queue
> might be an option
> (http://stackoverflow.com/questions/342556/python-2-6-multiprocessing-queue-compatible-with-threads).

Using a manager, or submitting a patch which adds priority queue to
the multiprocessing.queue module is the correct solution for this.

You can file an enhancement in the tracker, and assign/add me to it,
but without a patch it may take me a bit (wicked busy right now).

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


Re: Problems with datetime.datetime.strptime

2009-05-10 Thread Tuomas Vesterinen

Nick Craig-Wood wrote:

Tuomas Vesterinen  wrote:
 I hoped that I could get rid of my special module _strptime2 when 
 porting to Python 3.0. But testing is a disappointment.

[snip]

 C :
strftime('%a %b %e %H:%M:%S %Y')='Sat May  9 11:26:12 2009'
strptime('Sat May  9 11:26:12 2009','%a %b %e %H:%M:%S %Y')=
  'e' is a bad directive in format '%a %b %e %H:%M:%S %Y'

[snip]

 What to do.


I'd start by checking out the code for python 3 trunk and seeing if I
could fix it. If I could I'd update the unit tests and the
documentation then submit the patch to the python bugtracker.

If I couldn't fix it then I'd report it as a bug.



Thanks. I have a Python implementation here:

  http://kotisivu.dnainternet.net/tuovest1/booker/booker-0.1.2.tar.gz

See folder booker/csvcnt modules _strptime2.py and time_forms.py.

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


Re: Nimrod programming language

2009-05-10 Thread Paul Boddie
On 10 Mai, 10:40, Paul Rubin  wrote:
>
> Looks nice in many ways.  You also know about PyPy and Felix
> (felix.sf.net)?

Try http://felix-lang.org/ for the latter, I believe.

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


Re: Get multiprocessing.Queue to do priorities

2009-05-10 Thread uuid

Dear Jesse,
thanks for the hint.

I see you are already assigned to the FIFO bug 
(http://bugs.python.org/issue4999), so I won't burden you even more. 
Clearly, a reliable FIFO behavior of multiprocessing.Queue helps more 
than a priority queue, since it can be used to build one, so that 
should really be the first thing to fix.


In the meantime, I think I'll whip up a hack that uses sort of a 
bucket- strategy: fill up a prioritized heapq, and then, in regular 
intervals, unload its contents into a size-limited multiprocessing 
queue.


I'll post this as soon as it works.

-u

On 2009-05-10 15:35:03 +0200, Jesse Noller  said:


Using a manager, or submitting a patch which adds priority queue to
the multiprocessing.queue module is the correct solution for this.

You can file an enhancement in the tracker, and assign/add me to it,
but without a patch it may take me a bit (wicked busy right now).

jesse



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


Re: import overwrites __name__

2009-05-10 Thread Piet van Oostrum
> Peter Otten <__pete...@web.de> (PO) wrote:

>PO> $ cat x.py
>PO> import sys
>PO> globals().update(zip(*(range(110),)*2))
>PO> y = 42
>PO> print __name__
>PO> if __name__ == "__main__":
>PO> a = b = 42
>PO> print len(dir())
>PO> from x import y as z
>PO> try:
>PO> print my_name
>PO> except NameError, e:
>PO> print 'unhandled NameError: "%s"' % e
>PO> sys.exit()

>PO> $ python x.py
>PO> __main__
>PO> 119
>PO> x
>PO> 117
>PO> unhandled NameError: "name 'my_name' is not defined"

This is perfectly normal. python x.py command runs normally (although
the globals().update is a very weird thing to do), until the from x
import y command. Then x.py is loaded again but now as the module 'x'
instead of '__main__'. Python doesn't know it's the same file, and
actually it doesn't want to know. It only knows it when you do import
twice on the same file. Not when you run it as a main script first and
then imports it. **This is a thing you shouldn't do**.
There are now two namespaces: one for __main__ and one for x. These are
distinct and have no relationship. 

The reason that the first number is 119 and the second is 117 is that
while importing x the variables a and b are not created.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
--
http://mail.python.org/mailman/listinfo/python-list


strip part of string

2009-05-10 Thread Francesco Pietra
Hi:
I would like to delete everything from column 54 on for each line
beginning with "ATOM". In the example in the attachment (sorry for the
attachment; I found no way with Google mail to have plain text mail)
the new line would become:

ATOM 49  NH1 ARG84  84.628  41.570  44.395

without any blank space to the right. . Everything else should remain
at its original column. I have been looking for  statement, unable
(my big fault ) to find a right one. I tried with slices % but it
becomes unnecessarily complex.


Thanks
francesco
# Sample line
#   1 2 3 4 5 6 7 8
# 012345678901234567890123456789012345678901234567890123456789012345678901234567890
# ATOM 49  NH1 ARG84  84.628  41.570  44.395  0.00  0.00   N

data = open('rec.crg', 'r')
outp = open('rec.strip.crg', 'w')

for L in data:
   if L[3] == 'M':
 L = L[:55] ???
   outp.write(L) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: strip part of string

2009-05-10 Thread MRAB

Francesco Pietra wrote:

Hi:
I would like to delete everything from column 54 on for each line
beginning with "ATOM". In the example in the attachment (sorry for the
attachment; I found no way with Google mail to have plain text mail)
the new line would become:

ATOM 49  NH1 ARG84  84.628  41.570  44.395

without any blank space to the right. . Everything else should remain
at its original column. I have been looking for  statement, unable
(my big fault ) to find a right one. I tried with slices % but it
becomes unnecessarily complex.


data = open('rec.crg', 'r')
outp = open('rec.strip.crg', 'w')

for L in data:
   if L[3] == 'M':
 L = L[:55].rstrip() + '\n'
   outp.write(L)

data.close()
outp.close()


Note that .rstrip() will strip all whitespace from the right-hand end of
the string, including the '\n' at the end of the string/line, so a new
'\n' has to be added.
--
http://mail.python.org/mailman/listinfo/python-list


Re: strip part of string

2009-05-10 Thread Steven D'Aprano
On Sun, 10 May 2009 17:18:51 +0200, Francesco Pietra wrote:

> Hi:
> I would like to delete everything from column 54 on for each line
> beginning with "ATOM". In the example in the attachment (sorry for the
> attachment; I found no way with Google mail to have plain text mail) the
> new line would become:
> 
> ATOM 49  NH1 ARG84  84.628  41.570  44.395

if line.startswith("ATOM"):
line = line[0:54].rstrip()

should do what you want.



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


Re: strip part of string

2009-05-10 Thread John Machin
On May 11, 1:18 am, Francesco Pietra  wrote:
> Hi:
> I would like to delete everything from column 54 on for each line
> beginning with "ATOM". In the example in the attachment (sorry for the
> attachment; I found no way with Google mail to have plain text mail)

Many of your audience won't have seen your quite unnecessary
attachment; you could have in-lined the text just like this:
[except that I've deleted empty lines, and chopped some irrelevant
stuff off the right hand end of some lines so that line-wrapping won't
confuse anybody any more than they are already :-)
]

8<---
# Sample line
#   1 2 3 4 5
# 012345678901234567890123456789012345678901234567890123456789
# ATOM 49  NH1 ARG84  84.628  41.570  44.395  0.00
data = open('rec.crg', 'r')
outp = open('rec.strip.crg', 'w')
for L in data:
   if L[3] == 'M':
 L = L[:55] ???
   outp.write(L)
8<---

> the new line would become:
>
> ATOM     49  NH1 ARG    84      84.628  41.570  44.395
>
> without any blank space to the right. . Everything else should remain
> at its original column. I have been looking for  statement, unable
> (my big fault ) to find a right one. I tried with slices % but it
> becomes unnecessarily complex.

You wish to retain 54 characters, but are retaining 55. You need a
newline on the end of the line. So the statement
L = L[:55]
should be
L = L[:54] + '\n'
You may wish to precede that with
assert len(L) >= 54
You may wish to change
if L[3] == 'M':
to
if L.startswith('ATOM'):
to accord with your requirements statement.

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


Re: ANN: Python process utility (psutil) 0.1.2 released

2009-05-10 Thread Giampaolo Rodola'
On 9 Mag, 11:30, Nick Craig-Wood  wrote:
> Giampaolo Rodola'  wrote:
> >  psutil is a module providing an interface for retrieving information
> >  on running processes and system utilization (CPU, memory) in a
> >  portable way by using Python, implementing many functionalities
> >  offered by tools like ps, top and Windows task manager.
> >  It currently supports Linux, OS X, FreeBSD and Windows.
>
> Very nice!
>
> Maybe you should make
>
>   Process()
>
> mean
>
>   Process(os.getpid())
>
> as I often want ask how much memory/cpu is my process using but rarely
> want to ask about other processes?
>
> It would be nice if "pydoc psutil" returned something useful also!
> You could do this by replacing your current __init__.py (which just
> contains "from _psutil import *") with _psutil.py
>
> --
> Nick Craig-Wood  --http://www.craig-wood.com/nick

Thanks for the hints. I'll do that.


--- Giampaolo Rodola'
http://code.google.com/p/pyftpdlib
http://code.google.com/p/psutil/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Q's on my first python script

2009-05-10 Thread Steven D'Aprano
On Sun, 10 May 2009 12:52:21 +, kj wrote:

> 1. The name of the BadArgument exception class defined in the script
>does not seem to me sufficiently specific.  If one were to import the
>script in order to reuse its wkday_abbrev function, I'd like this
>exception's name to be more unequivocally tied to this script.  What
>I'm looking for is something like a "namespace" for this script. 
>What's the pythonic way to construct a namespace?

You already have one. The module you have created is a namespace. If your 
script is called "myscript.py", then to use it elsewhere you would do:

import myscript
raise myscript.BadArgument


> 2. In some python modules I've seen the idiom
> 
>if __name__ == "__main__":
>   # run some tests here
> 
>I'd like to set up tests for this script, mostly to ensure that it
>handles the error cases properly, but I'm alread using the idiom
>above to actually run the script under normal operation. What's the
>typical python idiom for running tests on a *script* (as opposed to a
>module that is normally not supposed to be run directly)?

I sometimes give my scripts an option -t or --self-test, and then run 
tests if that option is passed on the command line.

Alternatively, put your tests in another module, say, myscript_tests.py, 
and then just run that when you want to test myscript.

 
> 3. Still on the subject of testing, how does one capture in a
>variable the output that would normally have gone to stdout or
>stderr?

Normally you would write the function to *return* the result, rather than 
*print* the result. If all output goes through the function return 
mechanism, then it's easy to capture: x = func().

However, for cases where the function does print directly, you can 
redefine stdout and strerr to be any file-like object, so you can do 
something like this:


# untested
import sys
import cStringIO
save_stdout, save_stderr = sys.stdout, sys.stderr
c1 = cStringIO.StringIO()
c2 = cStringIO.StringIO()
try:
sys.stdout = c1
sys.stderr = c2
result = func(*args, **kwargs)  # normally prints some stuff
finally:
# restore standard files
sys.stdout = save_stdout
sys.stderr = save_stderr
captured_from_stdout = c1.getvalue()
captured_from_stderr = c2.getvalue()


 
> 4. What's the python way to emit warnings?  (The script below should
>warn the user that arguments after the first one are ignored.)

import warnings
warnings.warn("The end of the world is coming!")


> 5. The variable wd is meant to be "global" to the script.  In other
>languages I've programmed in I've seen some typographic convention
>used for the name of such variables (e.g. all caps) to signal this
>widened scope.  Does python have such a convention?

As a general rule, it's best to avoid globals variables as much as 
possible.

One convention I occasionally use is to prefix global variables with a 
lowercase g. And then ruthlessly refactor my code until any variable 
starting with a lowercase g is removed :)




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


Re: strip part of string

2009-05-10 Thread John Machin
On May 11, 1:34 am, MRAB  wrote:
> Francesco Pietra wrote:
> > Hi:
> > I would like to delete everything from column 54 on for each line
> > beginning with "ATOM". In the example in the attachment (sorry for the
> > attachment; I found no way with Google mail to have plain text mail)
> > the new line would become:
>
> > ATOM     49  NH1 ARG    84      84.628  41.570  44.395
>
> > without any blank space to the right. . Everything else should remain
> > at its original column. I have been looking for  statement, unable
> > (my big fault ) to find a right one. I tried with slices % but it
> > becomes unnecessarily complex.
>
> data = open('rec.crg', 'r')
> outp = open('rec.strip.crg', 'w')
>
> for L in data:
>     if L[3] == 'M':
>       L = L[:55].rstrip() + '\n'
>     outp.write(L)
>
> data.close()
> outp.close()
>
> Note that .rstrip() will strip all whitespace from the right-hand end of
> the string, including the '\n' at the end of the string/line, so a new
> '\n' has to be added.

His line-terminal '\n' has already disappeared before the rstrip gets
a guernsey.
His "column 54" is counting from 0, not from 1 -- his [:55] is getting
him an extra space which he doesn't want.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Nimrod programming language

2009-05-10 Thread Florian Wollenschein

Andreas Rumpf wrote:

Dear Python-users,

I invented a new programming language called "Nimrod" that combines Python's 
readability with C's performance. Please check it out: http://force7.de/nimrod/
Any feedback is appreciated.

Regards,
Andreas Rumpf

__
GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://movieflat.web.de



Looks really interesting. I think I'll give it a try later on.
Thanks for your announcement.

Reagrds,
Listick
http://www.listick.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode bit me

2009-05-10 Thread anuraguni...@yahoo.com
yes but my list sometimes have list of lists

On May 10, 2:59 pm, "Diez B. Roggisch"  wrote:
> anuraguni...@yahoo.com schrieb:
>
> > ok that explains it,
> > so
> > unicode(obj) calls __unicode__ on that object and if it isn't there
> > __repr__ is used
> > __repr__ of list by default return a str even if __repr__ of element
> > is unicode
>
> > so my only solution looks like to use my own list class everywhere i
> > use list
> > class mylist(list):
> >     def __unicode__(self):
> >         return u"["+u''.join(map(unicode,self))+u"]"
>
> Or you use a custom unicode_list-function whenever you care to print out
>   a list.
>
> Diez

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


Re: [Python-Dev] .pth files are evil

2009-05-10 Thread Zooko Wilcox-O'Hearn

On May 9, 2009, at 9:39 AM, P.J. Eby wrote:

It would be really straightforward, though, for someone to  
implement an easy_install variant that does this.  Just invoke  
"easy_install -Zmaxd /some/tmpdir packagelist" to get a full set of  
unpacked .egg directories in /some/tmpdir, and then move the  
contents of the resulting .egg subdirs to the target location,  
renaming EGG-INFO subdirs to projectname-version.egg-info subdirs.


Except for the renaming part, this is exactly what GNU stow does.

(Of course, this ignores the issue of uninstalling previous  
versions, or overwriting of conflicting files in the target -- does  
pip handle these?)


GNU stow does handle these issues.

Regards,

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


help with recursive whitespace filter in

2009-05-10 Thread Rustom Mody
I am trying to write a recursive filter to remove whitespace-only
nodes for minidom.
The code is below.

Strangely it deletes some whitespace nodes and leaves some.
If I keep calling it -- like so: fws(fws(fws(doc)))  then at some
stage all the ws nodes disappear

Does anybody have a clue?


from xml.dom.minidom import parse

#The input to fws is the output of parse("something.xml")


def fws(ele):
""" filter white space (recursive)"""

   for c in ele.childNodes:
if isWsNode(c):
ele.removeChild(c)
#c.unlink() Makes no diff whether this is there or not
elif c.nodeType == ele.ELEMENT_NODE:
fws(c)


def isWsNode(ele):
return (ele.nodeType == ele.TEXT_NODE and not ele.data.strip())
--
http://mail.python.org/mailman/listinfo/python-list


Re: Java-style futures in Python - only better

2009-05-10 Thread Brian Quinlan

Colin J. Williams wrote:

Brian,

Since the word "future" is part of the Python lingo:

A future statement is a directive to the compiler that a particular 
module should be compiled using syntax or semantics that will be 
available in a specified future release of Python. The future statement 
is intended to ease migration to future versions of Python that 
introduce incompatible changes to the language. It allows use of the new 
features on a per-module basis before the release in which the feature 
becomes standard.


Have you given thought to the use of another word?


I named the module "futures" (plural) to try to reduce the potential 
confusion with the "__futures__" module.


The concept of a future is fairly well known in CS 
[http://en.wikipedia.org/wiki/Future_(programming)] so giving it a 
completely different name would be a bit annoying.


Cheers,
Brian


Colin W.

Brian Quinlan wrote:

Hey all,

I've been working on an Java-style futures implementation in Python. 
Futures are a way of representing asynchronous operations e.g. 
operations that are run in another thread or process. The are are a 
easy but powerful way of parallelizing sequential operations. The also 
provide a consistent interface across implementations e.g. they can 
provide the same interface to threading and to multiprocessing.


For example:

def is_prime(n):
"Return True iff n is prime"
...

def check_primes(numbers):
return map(is_prime, numbers)

Could be parallelized as:

def check_primes(numbers):
# ProcessPoolExecutor will create one worker process
# per CPU if called without arguments. Using threads
# is valueless because of the GIL.
with futures.ProcessPoolExecutor() as executor:
return executor.map(is_prime, numbers)

A more complex example:

def load_url(url, timeout):
return urllib.request.urlopen(url, timeout=timeout).read()

### Download the content of some URLs - ignore failures.
def download_urls(urls, timeout=60):
url_to_content = {}
for url in urls:
try:
url_to_content[url] = load_url(url, timeout=timeout)
except:
pass
return url_to_content

Could be parallelized as:

# Making this a global variable used any many functions ensures that
# the global thread count is kept under control.
executor = futures.ThreadPoolExecutor(50)
def download_urls(urls, timeout=60):
url_to_content = {}
# Run load_url for every url and get the result as a FuturesList.
fs = executor.run_to_futures(
(functools.partial(load_url, url, timeout) for url in urls),
timeout=timeout)
for url, future in zip(urls, fs.successful_futures()):
url_to_content[url] = future.result()
return url_to_content


The Python 3.0+ code is here:
http://code.google.com/p/pythonfutures/source

Any feedback on the API would be very much appreciated!

Cheers,
Brian




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


Re: help with recursive whitespace filter in

2009-05-10 Thread Steve Howell
On May 10, 9:10 am, Rustom Mody  wrote:
> I am trying to write a recursive filter to remove whitespace-only
> nodes for minidom.
> The code is below.
>
> Strangely it deletes some whitespace nodes and leaves some.
> If I keep calling it -- like so: fws(fws(fws(doc)))  then at some
> stage all the ws nodes disappear
>
> Does anybody have a clue?
>
> from xml.dom.minidom import parse
>
> #The input to fws is the output of parse("something.xml")
>
> def fws(ele):
>     """ filter white space (recursive)"""
>
>    for c in ele.childNodes:
>         if isWsNode(c):
>             ele.removeChild(c)
>             #c.unlink() Makes no diff whether this is there or not
>         elif c.nodeType == ele.ELEMENT_NODE:
>             fws(c)
>
> def isWsNode(ele):
>     return (ele.nodeType == ele.TEXT_NODE and not ele.data.strip())

I would avoid doing things like delete/remove in a loop.  Instead
build a list of things to delete.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Q's on my first python script

2009-05-10 Thread Andre Engels
On Sun, May 10, 2009 at 5:56 PM, Steven D'Aprano
 wrote:

>> 5. The variable wd is meant to be "global" to the script.  In other
>>    languages I've programmed in I've seen some typographic convention
>>    used for the name of such variables (e.g. all caps) to signal this
>>    widened scope.  Does python have such a convention?
>
> As a general rule, it's best to avoid globals variables as much as
> possible.

However, this is not really a global variable, this is a constant,
which is not something to be avoided, nor something that there is a
convention for (as far as I know).


-- 
André Engels, andreeng...@gmail.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Downloading most recently modified files

2009-05-10 Thread AllenLars

Thanks Shawn.  I went through the ftplib info and I was able to generate a
list.  I am needing to figure out parsing the list. 

AllenLars

Shawn Milochik wrote:
> 
> On Thu, May 7, 2009 at 2:19 PM, AllenLars  wrote:
>>
>> I am trying to code a script that will allow me to go to ftp site and
>> download files based on most recently modified file (date, time).  I am
>> brand new to programming.  Any and all help is appreciated.
>> --
> 
> 
> I've actually written code to do this, and it's fairly easy. Just use
> the FTP module to run the "ls" (directory listing) command and parse
> the results to get the filenames and timestamps. Then you will be able
> to easily do download requests for the file(s) you want.
> 
> Perhaps someone else on the list can chime in with a more elegant
> solution.
> 
> Here's enough to get you started:
> 
> from ftplib import FTP   #(http://docs.python.org/library/ftplib.html)
> 
> ShawnMilo
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Downloading-most-recently-modified-files-tp23432457p23471969.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: import overwrites __name__

2009-05-10 Thread Scott David Daniels

Piet van Oostrum wrote:

Peter Otten <__pete...@web.de> (PO) wrote:



PO> $ cat x.py
PO> import sys
PO> globals().update(zip(*(range(110),)*2))
PO> y = 42
PO> print __name__
PO> if __name__ == "__main__":
PO> a = b = 42
PO> print len(dir())
PO> from x import y as z
PO> try:
PO> print my_name
PO> except NameError, e:
PO> print 'unhandled NameError: "%s"' % e
PO> sys.exit()



PO> $ python x.py
PO> __main__
PO> 119
PO> x
PO> 117
PO> unhandled NameError: "name 'my_name' is not defined"


This is perfectly normal. python x.py command runs normally (although
the globals().update is a very weird thing to do), until the from x
import y command. Then x.py is loaded again but now as the module 'x'
instead of '__main__'. Python doesn't know it's the same file, and
actually it doesn't want to know. It only knows it when you do import
twice on the same file. Not when you run it as a main script first and
then imports it. **This is a thing you shouldn't do**.
There are now two namespaces: one for __main__ and one for x. These are
distinct and have no relationship. 


The reason that the first number is 119 and the second is 117 is that
while importing x the variables a and b are not created.

After a bit more boiling down:

x.py:

import sys
y = 42
if __name__ == "__main__":
a = b = 42
print __name__, 'size', len(dir())
from x import y as z
print __name__, 'size', len(dir()), 'completed import of', z
try:
print my_name
except NameError, e:
print '%s found unhandled NameError: "%s"' % (__name__, e)
sys.exit()

produces:
__main__
__main__ size 8
x
x size 6
x size 7 completed import of 42
x found unhandled NameError: "name 'my_name' is not defined"

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Wrapping comments

2009-05-10 Thread Scott David Daniels

Steven D'Aprano wrote:

On Sun, 10 May 2009 13:41:15 +1200, Lawrence D'Oliveiro wrote:

If I had to print out all the code and documentation I have to look at,
I'd need to add another room to my house.


You are allowed to throw it away when you're done with it :)


Except here in Portland, where you'd better recycle it. ;)

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Wrapping comments

2009-05-10 Thread David Robinow
On Sun, May 10, 2009 at 3:32 AM, Tobias Weber  wrote:
> In article ,
>  Arnaud Delobelle  wrote:
>
>> A simple Alt-Q will reformat everything nicely.
>
> Now that's something. Thanks!
>
> (still not gonna use software that doesn't let me type # because it's
> alt+3 on a UK layout; having to re-learn or configure that is just sick)
>
 Put the following in your .emacs file:

(define-key key-translation-map [?\M-3] "#")


or, if you prefer, just be sick.
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] .pth files are evil

2009-05-10 Thread Martin v. Löwis
> GNU stow does handle these issues.

If GNU stow solves all your problems, why do you want to
use easy_install in the first place?

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


Re: help with recursive whitespace filter in

2009-05-10 Thread rustom
On May 10, 9:49 pm, Steve Howell  wrote:
> On May 10, 9:10 am, Rustom Mody  wrote:
>
>
>
> > I am trying to write a recursive filter to remove whitespace-only
> > nodes for minidom.
> > The code is below.
>
> > Strangely it deletes some whitespace nodes and leaves some.
> > If I keep calling it -- like so: fws(fws(fws(doc)))  then at some
> > stage all the ws nodes disappear
>
> > Does anybody have a clue?
>
> > from xml.dom.minidom import parse
>
> > #The input to fws is the output of parse("something.xml")
>
> > def fws(ele):
> >     """ filter white space (recursive)"""
>
> >    for c in ele.childNodes:
> >         if isWsNode(c):
> >             ele.removeChild(c)
> >             #c.unlink() Makes no diff whether this is there or not
> >         elif c.nodeType == ele.ELEMENT_NODE:
> >             fws(c)
>
> > def isWsNode(ele):
> >     return (ele.nodeType == ele.TEXT_NODE and not ele.data.strip())
>
> I would avoid doing things like delete/remove in a loop.  Instead
> build a list of things to delete.

Yeah I know. I would write the whole damn thing functionally if I knew
how.  But cant figure out the API.
I actually started out to write a (haskell-style) copy out the whole
tree minus the unwanted nodes but could not figure it out
--
http://mail.python.org/mailman/listinfo/python-list


Re: Q's on my first python script

2009-05-10 Thread Scott David Daniels

Steven D'Aprano wrote:

On Sun, 10 May 2009 12:52:21 +, kj wrote:


5. The variable wd is meant to be "global" to the script.  In other
   languages I've programmed in I've seen some typographic convention
   used for the name of such variables (e.g. all caps) to signal this
   widened scope.  Does python have such a convention?


As a general rule, it's best to avoid globals variables as much as 
possible.


One convention I occasionally use is to prefix global variables with a 
lowercase g. And then ruthlessly refactor my code until any variable 
starting with a lowercase g is removed :)


You (the OP) don't seem to know about PEP 8, which advises 4-space
indents, all-caps constants, and many more style specifics.
http://www.python.org/dev/peps/pep-0008/

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: help with recursive whitespace filter in

2009-05-10 Thread MRAB

rustom wrote:

On May 10, 9:49 pm, Steve Howell  wrote:

On May 10, 9:10 am, Rustom Mody  wrote:




I am trying to write a recursive filter to remove whitespace-only
nodes for minidom.
The code is below.
Strangely it deletes some whitespace nodes and leaves some.
If I keep calling it -- like so: fws(fws(fws(doc)))  then at some
stage all the ws nodes disappear
Does anybody have a clue?
from xml.dom.minidom import parse
#The input to fws is the output of parse("something.xml")
def fws(ele):
""" filter white space (recursive)"""
   for c in ele.childNodes:
if isWsNode(c):
ele.removeChild(c)
#c.unlink() Makes no diff whether this is there or not
elif c.nodeType == ele.ELEMENT_NODE:
fws(c)
def isWsNode(ele):
return (ele.nodeType == ele.TEXT_NODE and not ele.data.strip())

I would avoid doing things like delete/remove in a loop.  Instead
build a list of things to delete.


Yeah I know. I would write the whole damn thing functionally if I knew
how.  But cant figure out the API.
I actually started out to write a (haskell-style) copy out the whole
tree minus the unwanted nodes but could not figure it out


def fws(ele):
""" filter white space (recursive)"""
empty_nodes = []
for c in ele.childNodes:
if isWsNode(c):
empty_nodes.append(c)
elif c.nodeType == ele.ELEMENT_NODE:
fws(c)
for c in empty_nodes:
ele.removeChild(c)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Q's on my first python script

2009-05-10 Thread MRAB

Andre Engels wrote:

On Sun, May 10, 2009 at 5:56 PM, Steven D'Aprano
 wrote:


5. The variable wd is meant to be "global" to the script.  In other
   languages I've programmed in I've seen some typographic convention
   used for the name of such variables (e.g. all caps) to signal this
   widened scope.  Does python have such a convention?

As a general rule, it's best to avoid globals variables as much as
possible.


However, this is not really a global variable, this is a constant,
which is not something to be avoided, nor something that there is a
convention for (as far as I know).


In Python the convention for constants is CAPS_WITH_UNDERSCORES.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Nimrod programming language

2009-05-10 Thread Tomasz Rola
On Fri, 8 May 2009, Andreas Rumpf wrote:

> Dear Python-users,
> 
> I invented a new programming language called "Nimrod" that combines 
> Python's readability with C's performance. Please check it out: 
> http://force7.de/nimrod/ 
> Any feedback is appreciated.
> 
> Regards,
> Andreas Rumpf

Interesting.

One question I ask myself upon seeing a new language is if it is possible 
to program amb (amb=ambiguous) operator in it. This page gives a very 
nice, "code first" explanation of amb and how it is supposed to work:

http://www.randomhacks.net/articles/2005/10/11/amb-operator

I am not sure if this kind of extension is doable in your Nimrod. Perhaps 
it can be somewhat extrapolated from docs, but at the moment I have no 
time to do this (and could not google anything interesting).

As can be seen on the page mentioned, in Ruby this seems to be quite light 
and effortless. From what I know about Python, it is either hard or 
impractical (to the point of being impossible) to write amb in it.

Two additional notes regarding amb:

1. Even if Nimrod cannot support amb in an elegant way, it is still nice 
idea. Especially if you can make it to be kind of Python superset, so that 
Python programmer can cross the boundary between the two without much 
hassle (and maybe in both directions). Of course, not everything can be 
copied.

2. The amb itself is not really important so much, and I may never feel 
any need to actually use it. But it stroke me how nice it was looking in 
Ruby, even if I finally decided not to learn Ruby (not in this year, at 
least).

In Scheme, it is a bit more hackish, but still looks nice, at least to me:

http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-16.html#node_chap_14
http://planet.plt-scheme.org/package-source/murphy/amb.plt/1/0/planet-docs/amb/index.html

Anyway, I think amb is quite a test of a language. If you can do it, 
please show the code.

Regards,
Tomasz Rola

--
** A C programmer asked whether computer had Buddha's nature.  **
** As the answer, master did "rm -rif" on the programmer's home**
** directory. And then the C programmer became enlightened...  **
** **
** Tomasz Rola  mailto:tomasz_r...@bigfoot.com **
--
http://mail.python.org/mailman/listinfo/python-list


Re: help with recursive whitespace filter in

2009-05-10 Thread Scott David Daniels

Rustom Mody wrote:

I am trying to write a recursive filter to remove whitespace-only
nodes for minidom.
The code is below.

Strangely it deletes some whitespace nodes and leaves some.
If I keep calling it -- like so: fws(fws(fws(doc)))  then at some
stage all the ws nodes disappear

Does anybody have a clue?


Yup, don't stand on the limb you are sawing off.


def fws(ele):

...

   for c in ele.childNodes:
if isWsNode(c):
ele.removeChild(c)

...
At the very least:
 for c in reversed(ele.childNodes):
  if isWsNode(c):
  ele.removeChild(c)

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: import overwrites __name__

2009-05-10 Thread Peter Otten
Piet van Oostrum wrote:

>> Peter Otten <__pete...@web.de> (PO) wrote:
> 
>>PO> $ cat x.py
>>PO> import sys
>>PO> globals().update(zip(*(range(110),)*2))
>>PO> y = 42
>>PO> print __name__
>>PO> if __name__ == "__main__":
>>PO> a = b = 42
>>PO> print len(dir())
>>PO> from x import y as z
>>PO> try:
>>PO> print my_name
>>PO> except NameError, e:
>>PO> print 'unhandled NameError: "%s"' % e
>>PO> sys.exit()
> 
>>PO> $ python x.py
>>PO> __main__
>>PO> 119
>>PO> x
>>PO> 117
>>PO> unhandled NameError: "name 'my_name' is not defined"
> 
> This is perfectly normal. 

I'm not 100% sure of that.

Just in case you didn't notice: I'm not the OP. The above piece of junk code 
was my attempt to keep as close to the code he posted while producing the 
same output that he got.

> python x.py command runs normally (although
> the globals().update is a very weird thing to do), until the from x
> import y command. Then x.py is loaded again but now as the module 'x'
> instead of '__main__'. Python doesn't know it's the same file, and
> actually it doesn't want to know. It only knows it when you do import
> twice on the same file. Not when you run it as a main script first and
> then imports it. **This is a thing you shouldn't do**.
> There are now two namespaces: one for __main__ and one for x. These are
> distinct and have no relationship.
> 
> The reason that the first number is 119 and the second is 117 is that
> while importing x the variables a and b are not created.

Your explanation is of course between 119 and 117% correct while I was only 
99% serious...

Cheers,
Peter

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


how GNU stow is complementary rather than alternative to distutils

2009-05-10 Thread Zooko Wilcox-O'Hearn

On May 10, 2009, at 11:18 AM, Martin v. Löwis wrote:

If GNU stow solves all your problems, why do you want to use  
easy_install in the first place?


That's a good question.  The answer is that there are two separate  
jobs: building executables and putting them in a directory structure  
of the appropriate shape for your system is one job, and installing  
or uninstalling that tree into your system is another.  GNU stow does  
only the latter.


The input to GNU stow is a set of executables, library files, etc.,  
in a directory tree that is of the right shape for your system.  For  
example, if you are on a Linux system, then your scripts all need to  
be in $prefix/bin/, your shared libs should be in $prefix/lib, your  
Python packages ought to be in $prefix/lib/python$x.$y/site- 
packages/, etc.  GNU stow is blissfully ignorant about all issues of  
building binaries, and choosing where to place files, etc. -- that's  
the job of the build system of the package, e.g. the "./configure -- 
prefix=foo && make && make install" for most C packages, or the  
"python ./setup.py install --prefix=foo" for Python packages using  
distutils (footnote 1).


Once GNU stow has the well-shaped directory which is the output of  
the build process, then it follows a very dumb, completely reversible  
(uninstallable) process of symlinking those files into the system  
directory structure.


It is a beautiful, elegant hack because it is sooo dumb.  It is also  
very nice to use the same tool to manage packages written in any  
programming language, provided only that they can build a directory  
tree of the right shape and content.


However, there are lots of things that it doesn't do, such as  
automatically acquiring and building dependencies, or producing  
executables for the target platform for each of your console  
scripts.  Not to mention creating a directory named "$prefx/lib/python 
$x.$y/site-packages" and cp'ing your Python files into it.  That's  
why you still need a build system even if you use GNU stow for an  
install-and-uninstall system.


The thing that prevents this from working with setuptools is that  
setuptools creates a file named easy_install.pth during the "python ./ 
setup.py install --prefix=foo" if you build two different Python  
packages this way, they will each create an easy_install.pth file,  
and then when you ask GNU stow to link the two resulting packages  
into your system, it will say "You are asking me to install two  
different packages which both claim that they need to write a file  
named '/usr/local/lib/python2.5/site-packages/easy_install.pth'.  I'm  
too dumb to deal with this conflict, so I give up.".  If I understand  
correctly, your (MvL's) suggestion that easy_install create a .pth  
file named "easy_install-$PACKAGE-$VERSION.pth" instead of  
"easy_install.pth" would indeed make it work with GNU stow.


Regards,

Zooko

footnote 1: Aside from the .pth file issue, the other reason that  
setuptools doesn't work for this use while distutils does is that  
setuptools tries to hard to save you from making a mistake: maybe you  
don't know what you are doing if you ask it to install into a  
previously non-existent prefix dir "foo".  This one is easier to fix:  
http://bugs.python.org/setuptools/issue54 # "be more like distutils  
with regard to --prefix=" .

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


Re: php to python code converter

2009-05-10 Thread kay
On 8 Mai, 17:19, Pascal Chambon  wrote:
> Hello
>
> That's funny, I was precisely thinking about a php to python converter,
> some weeks ago.
> Such a tool, allowing for example to convert some CMS like Drupal to
> python, would be a killer app, when we consider the amount of php code
> available.
>
> But of course, there are lots of issues that'd have to be fixed :
> - translating the php syntax to python syntax
> - forcing scope limitations where php doesn't have any
> - handling differences in semantics (for example, the booleanness of "0"
> or)
> - handling the automatic variable creation and coertion that php features
> - handling the php types like arrays (which are neither python lists nor
> python dicts)
> - providing a whole mirror of the php stdlib (string and file functions,
> access to environment vars...)

Some thoughts.

1) Syntax. Not a big deal.

2) Semantics. My favourite approach was to create a Python framework
that represents PHP in Python and enables round-trips. So one could
translate forth and back. Python code that is compliant to the
conventions of the framework can also be translated to PHP and for
each PHP program P following equation holds:

   py2php(php2py(P)) = P

This makes readable code mandatory.

3) PHP stdlib via C bindings ( ctypes? )

4) Corner cases of bindings: cut them off. Not everything has to be
translated. But produce stubs that raise NotImplementedError
exceptions.

Same arguments apply to Javascript. Not sure about Ruby but I do think
a parser is feasible despite context sensitivities. Ruby is not my
concern though.

Personally I'd be interested in Wordpress which I like and use.

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


Re: Re: import overwrites __name__

2009-05-10 Thread Dave Angel

Scott David Daniels wrote:
Piet van 
Oostrum wrote:

Peter Otten <__pete...@web.de> (PO) wrote:



PO> $ cat x.py
PO> import sys
PO> globals().update(zip(*(range(110),)*2))
PO> y = 42
PO> print __name__
PO> if __name__ == "__main__":
PO> a = b = 42
PO> print len(dir())
PO> from x import y as z
PO> try:
PO> print my_name
PO> except NameError, e:
PO> print 'unhandled NameError: "%s"' % e
PO> sys.exit()



PO> $ python x.py
PO> __main__
PO> 119
PO> x
PO> 117
PO> unhandled NameError: "name 'my_name' is not defined"


This is perfectly normal. python x.py command runs normally (although
the globals().update is a very weird thing to do), until the from x
import y command. Then x.py is loaded again but now as the module 'x'
instead of '__main__'. Python doesn't know it's the same file, and
actually it doesn't want to know. It only knows it when you do import
twice on the same file. Not when you run it as a main script first and
then imports it. **This is a thing you shouldn't do**.
There are now two namespaces: one for __main__ and one for x. These are
distinct and have no relationship.
The reason that the first number is 119 and the second is 117 is that
while importing x the variables a and b are not created.

After a bit more boiling down:

x.py:

import sys
y = 42
if __name__ == "__main__":
a = b = 42
print __name__, 'size', len(dir())
from x import y as z
print __name__, 'size', len(dir()), 'completed import of', z
try:
print my_name
except NameError, e:
print '%s found unhandled NameError: "%s"' % (__name__, e)
sys.exit()

produces:
__main__
__main__ size 8
x
x size 6
x size 7 completed import of 42
x found unhandled NameError: "name 'my_name' is not defined"

--Scott David Daniels
scott.dani...@acm.org



I already said this before - don't ever import the file you're using as 
a script.  Now that we have real names for these files, I can say it 
more strongly.


If you use "import x"or its variant "from x import..."   then *DO 
NOT* run x.py as your main script.


You must have a separate script file that you run, and that nobody tries 
to import.


Now, once you get past that problem, you'll probably have others.  Why 
on earth would you try to import a module from itself? The line:

  from x import y as z

is probably equivalent to  z=y


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


Re: how GNU stow is complementary rather than alternative to distutils

2009-05-10 Thread Martin v. Löwis
Zooko Wilcox-O'Hearn wrote:
> On May 10, 2009, at 11:18 AM, Martin v. Löwis wrote:
> 
>> If GNU stow solves all your problems, why do you want to use
>> easy_install in the first place?
> 
> That's a good question.  The answer is that there are two separate jobs:
> building executables and putting them in a directory structure of the
> appropriate shape for your system is one job, and installing or
> uninstalling that tree into your system is another.  GNU stow does only
> the latter.

And so does easy_install - it's job is *not* to build the executables
and to put them in a directory structure. Instead, it's
distutils/setuptools which has this job.

The primary purpose of easy_install is to download the files from PyPI
(IIUC).

> The thing that prevents this from working with setuptools is that
> setuptools creates a file named easy_install.pth

It will stop doing that if you ask nicely. That's why I recommended
earlier that you do ask it not to edit .pth files.

> If I understand correctly,
> your (MvL's) suggestion that easy_install create a .pth file named
> "easy_install-$PACKAGE-$VERSION.pth" instead of "easy_install.pth" would
> indeed make it work with GNU stow.

My recommendation is that you use the already existing flag to
setup.py install that stops it from editing .pth files.

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


Re: [Python-Dev] how GNU stow is complementary rather than alternative to distutils

2009-05-10 Thread Zooko O'Whielacronx
following-up to my own post to mention one very important reason why
anyone cares:

On Sun, May 10, 2009 at 12:04 PM, Zooko Wilcox-O'Hearn  wrote:

> It is a beautiful, elegant hack because it is sooo dumb.  It is also very
> nice to use the same tool to manage packages written in any programming
> language, provided only that they can build a directory tree of the right
> shape and content.

And, you are not relying on the author of the package that you are
installing to avoid accidentally or maliciously screwing up your
system.  You're not even relying on the authors of the *build system*
(e.g. the authors of distutils or easy_install).  You are relying
*only* on GNU stow to avoid accidentally or maliciously screwing up
your system, and GNU stow is very dumb, so it is easy to understand
what it is going to do and why that isn't going to irreversibly screw
up your system.

That is: you don't run the "build yourself and install into $prefix"
step as root.  This is an important consideration for a lot of people,
who absolutely refuse on principle to ever run "sudo python
./setup.py" on a system that they care about unless they wrote the
"setup.py" script themselves.  (Likewise they refuse to run "sudo make
install" on packages written in C.)

Regards,

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


Re: how GNU stow is complementary rather than alternative to distutils

2009-05-10 Thread P.J. Eby

At 12:04 PM 5/10/2009 -0600, Zooko Wilcox-O'Hearn wrote:

The thing that prevents this from working with setuptools is that
setuptools creates a file named easy_install.pth during the "python 
./ setup.py install --prefix=foo" if you build two different Python

packages this way, they will each create an easy_install.pth file,
and then when you ask GNU stow to link the two resulting packages
into your system, it will say "You are asking me to install two
different packages which both claim that they need to write a file
named '/usr/local/lib/python2.5/site-packages/easy_install.pth'.


Adding --record and --single-version-externally-managed to that 
command line will prevent the .pth file from being used or needed, 
although I believe you already know this.


(What that mode won't do is install dependencies automatically.) 


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


Re: import overwrites __name__

2009-05-10 Thread Piet van Oostrum
> Peter Otten <__pete...@web.de> (PO) wrote:

>PO> Piet van Oostrum wrote:
>>> 
>>> This is perfectly normal. 

>PO> I'm not 100% sure of that.

Why not?

>PO> Just in case you didn't notice: I'm not the OP. The above piece of junk 
>code 
>PO> was my attempt to keep as close to the code he posted while producing the 
>PO> same output that he got.

Sorry about that. I was confused because your code was a response tp

> You'd have to show us the module. Try to make the smallest module that
> has the same effect.

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: I'm intrigued that Python has some functional constructions in the language.

2009-05-10 Thread namekuseijin

Carl Banks wrote:

On May 9, 10:57 am, namekuseijin 
wrote:

Carl Banks wrote:

On May 8, 7:19 pm, namekuseijin  wrote:

On May 8, 10:13 pm, Carl Banks  wrote:
In Haskell, Lisp and other functional programming languages, any extra
syntax gets converted into the core lambda constructs.

So?  The user still uses that syntax, so how can you claim it doesn't
matter?

 In Lisp
languages, that syntax is merely user-defined macros, but in Haskell
it's builtin the compiler for convenience.

I don't even know what you're saying here

I'm saying syntax is nothing special.  They are user-defined, as
functions.  And it all gets converted into functions.  Functions matter,
syntax is irrelevant because you can do away with it.


Nope, sorry, you're ignoring half the problem here.  Syntax is only
irrelevant if you actually do do away with it.  As long as syntax is
there and people use it, then it matters, regardless of whether it all
reduces to function calls.


It's handy, yes.  It's just nothing special, specially when composing 
programs out of function composition.



For a very benign example, consider the ways that Python and Haskell
write listcomps:

[ x for x in ss ]
[ x | x <- ss ]

One of these might be more readable than the other (I make no
implication which);


Haskell uses math notation, which may not be to everyone's tastes as far 
as IT has largely left its compsci and math roots and every Joe Sixpack 
might assembly an usable app with a scripting language, some 
template-writing IDE and a few frameworks.



however, readability has nothing to do with
whether the compiler internally reduces it to a function call or not.
Readibility counts, therefore syntax matters.


Function calls are perfectly readable.


Now, maybe readability concerns don't matter to you personally, but it
does matter to the OP, who is trying to advocate functional
programming but is having difficulty because most purely functional
languages have hideous minimalist syntax that turns people off.


BTW, rereading it again, the OP was originally questioning about 
compiler optimizations for purely functional constructs within Python, 
but somehow syntax got in the middle.


I don't see how "syntax would be a lot easier to understand" if it would 
"be possible to more clearly separate the pure code (without side 
effects) from the impure code (that deals with state changes, I/O, 
etc.), so that the pure code could be compiled and have aggressive 
functional transformations applied to it for efficiency."


Syntax would remain the same, I guess.


In functional programming languages, predefined syntax is mostly
irrelevant.  In Python and other imperative languages, it's absolutely
necessary.  That's my point.


I think you are overstating this by a lot.


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


Re: Q's on my first python script

2009-05-10 Thread Dave Angel



kj wrote:

Below is my very firs python script.

This was just a learning exercise; the script doesn't do anything
terribly exciting: for an argument of the form YYMMDD (year, month,
day) it prints out the corresponding string YYMMDDW, where W is a
one-letter abbreviation for the day of the week.  E.g.

% wd 090511
090511M

The script runs OK, but I can still see a few areas of improvement,
for which I could use your advice.

1. The name of the BadArgument exception class defined in the script
   does not seem to me sufficiently specific.  If one were to import
   the script in order to reuse its wkday_abbrev function, I'd like
   this exception's name to be more unequivocally tied to this
   script.  What I'm looking for is something like a "namespace"
   for this script.  What's the pythonic way to construct a namespace?

2. In some python modules I've seen the idiom

   if __name__ == "__main__":
  # run some tests here

   I'd like to set up tests for this script, mostly to ensure that
   it handles the error cases properly, but I'm alread using the
   idiom above to actually run the script under normal operation.
   What's the typical python idiom for running tests on a *script*
   (as opposed to a module that is normally not supposed to be run
   directly)?

3. Still on the subject of testing, how does one capture in a
   variable the output that would normally have gone to stdout or
   stderr?

4. What's the python way to emit warnings?  (The script below should
   warn the user that arguments after the first one are ignored.)

5. The variable wd is meant to be "global" to the script.  In other
   languages I've programmed in I've seen some typographic convention
   used for the name of such variables (e.g. all caps) to signal
   this widened scope.  Does python have such a convention?

Any comments/suggestions on these questions, or anything else about
the script, would be much appreciated.

TIA!

kynn



from optparse import OptionParser
import re
import datetime
import sys

class BadArgument(Exception): pass

wd = ("M", "T", "W", "H", "F", "S", "U")

def wkday_abbrev(str):
  try:
mm = re.match("(\d{2})(\d{2})(\d{2})\Z", str)

y, m, d = map(lambda x: int(mm.group(x)), (1,2,3))

if y < 38: y = y + 1900
else: y = y + 2000

return wd[datetime.datetime(y, m, d).weekday()]
  except (AttributeError, ValueError):
raise BadArgument()


def main():

  usage = '''Usage: %prog [options] YYMMDD
   %prog -h|--help
'''

  parser = OptionParser(usage=usage)
  parser.add_option("-n", "--no-newline", dest="nonl",
action="store_true", help="omit newline in output")

  (options, args) = parser.parse_args();

  try:
sys.stdout.write("%s%s" % (args[0], wkday_abbrev(args[0])))
if not options.nonl: print

  except (IndexError, BadArgument):
print usage
sys.exit(1)
  except: raise

if __name__ == "__main__": main()

  


Other replies cover most of your questions nicely.  But for the testing 
question:


Rename this script to some other name, and call it a module
Write a new script with just three lines in it:

import  othermodule

if __name__ == "__main__":
  othermodule.main()



Now your testing code can go in "othermodule.py"

Note that I would put the argument parsing logic in the new file, so 
parts of main() would actually move.  Normally I'd call main() with two 
arguments - options, args


Your use of optparse could be more streamlined.  For example, it'll 
build the help string for you, based on the various calls to 
add_option(), and it includes its own -h and --help implicitly.



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


Re: FLV download script works, but I want to enhance it

2009-05-10 Thread Aahz
In article ,
The Music Guy   wrote:
>On Thu, May 7, 2009 at 9:29 AM, Aahz  wrote:
>>
>> Here's my download script to get you started figuring this out, it does
>> the wget in the background so that several downloads can run in parallel
>> from a single terminal window:
>>
>> #!/bin/bash
>>
>> echo "Downloading $1"
>> wget "$1" > /dev/null 2>&1 &
>
>Thanks for the reply, but unfortunately that script is going in the
>complete wrong direction.

Not really; my point was that you could use something similar to process
files after downloading.

>Firstly, downloading multiple files in tandem does not speed up the
>process as it merely cuts up the already limited bandwidth into even
>smaller pieces and delays every download in progress. It is much
>better to queue downloads to occur one-by-one.
>
>Secondly, that approach is based on bash rather than Python. I know I
>could use the `&` operator on a command line to background processes,
>but I would like to be able to have more control through Python and
>the use of the subprocess or threading modules.

Threading probably won't get you anywhere; I bet processing the files is
CPU-intensive.  I suggest looking into the multiprocessing module, which
would then run the file processing.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
--
http://mail.python.org/mailman/listinfo/python-list


Re: import overwrites __name__

2009-05-10 Thread Peter Otten
Piet van Oostrum wrote:

>> Peter Otten <__pete...@web.de> (PO) wrote:
> 
>>PO> Piet van Oostrum wrote:
 
 This is perfectly normal.
> 
>>PO> I'm not 100% sure of that.
> 
> Why not?

What you quoted was not code I would /normally/ write. I was playing with 
the context and meaning of your sentence.

Sorry, this is just a joke that went awry.

Peter

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


Re: Q's on my first python script

2009-05-10 Thread kj
In <0216ec41$0$20647$c3e8...@news.astraweb.com> Steven D'Aprano 
 writes:

>On Sun, 10 May 2009 12:52:21 +, kj wrote:

>> 1. The name of the BadArgument exception class defined in the script
>>does not seem to me sufficiently specific.  If one were to import the
>>script in order to reuse its wkday_abbrev function, I'd like this
>>exception's name to be more unequivocally tied to this script.  What
>>I'm looking for is something like a "namespace" for this script. 
>>What's the pythonic way to construct a namespace?

>You already have one. The module you have created is a namespace. If your 
>script is called "myscript.py", then to use it elsewhere you would do:

>import myscript
>raise myscript.BadArgument


>> 2. In some python modules I've seen the idiom
>> 
>>if __name__ == "__main__":
>>   # run some tests here
>> 
>>I'd like to set up tests for this script, mostly to ensure that it
>>handles the error cases properly, but I'm alread using the idiom
>>above to actually run the script under normal operation. What's the
>>typical python idiom for running tests on a *script* (as opposed to a
>>module that is normally not supposed to be run directly)?

>I sometimes give my scripts an option -t or --self-test, and then run 
>tests if that option is passed on the command line.

>Alternatively, put your tests in another module, say, myscript_tests.py, 
>and then just run that when you want to test myscript.

> 
>> 3. Still on the subject of testing, how does one capture in a
>>variable the output that would normally have gone to stdout or
>>stderr?

>Normally you would write the function to *return* the result, rather than 
>*print* the result. If all output goes through the function return 
>mechanism, then it's easy to capture: x = func().

>However, for cases where the function does print directly, you can 
>redefine stdout and strerr to be any file-like object, so you can do 
>something like this:


># untested
>import sys
>import cStringIO
>save_stdout, save_stderr = sys.stdout, sys.stderr
>c1 = cStringIO.StringIO()
>c2 = cStringIO.StringIO()
>try:
>sys.stdout = c1
>sys.stderr = c2
>result = func(*args, **kwargs)  # normally prints some stuff
>finally:
># restore standard files
>sys.stdout = save_stdout
>sys.stderr = save_stderr
>captured_from_stdout = c1.getvalue()
>captured_from_stderr = c2.getvalue()


> 
>> 4. What's the python way to emit warnings?  (The script below should
>>warn the user that arguments after the first one are ignored.)

>import warnings
>warnings.warn("The end of the world is coming!")


>> 5. The variable wd is meant to be "global" to the script.  In other
>>languages I've programmed in I've seen some typographic convention
>>used for the name of such variables (e.g. all caps) to signal this
>>widened scope.  Does python have such a convention?

>As a general rule, it's best to avoid globals variables as much as 
>possible.

>One convention I occasionally use is to prefix global variables with a 
>lowercase g. And then ruthlessly refactor my code until any variable 
>starting with a lowercase g is removed :)


Thanks!  That was very helpful!

Kynn
-- 
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Q's on my first python script

2009-05-10 Thread kj


Thank you all very much!  I really appreciate it.

kynn
-- 
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list


What's the use of the else in try/except/else?

2009-05-10 Thread kj


I know about the construct:

try:
# do something 
except ...:
# handle exception
else:
# do something else

...but I can't come with an example in which the same couldn't be
accomplished with

try:
# do something
# do something else
except ...:
# handle exception

The only significant difference I can come up with is that in the
second form, the except clause may be masking some unexpected
exceptions from the "do something else" part.  Is this the rationale
behind this else clause?  Or is there something more to it?

TIA!

kynn

-- 
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Downloading most recently modified files

2009-05-10 Thread Shawn Milochik
On Sun, May 10, 2009 at 1:04 PM, AllenLars  wrote:
>
> Thanks Shawn.  I went through the ftplib info and I was able to generate a
> list.  I am needing to figure out parsing the list.
>
> AllenLars


Well, start by separating out the date and the file name. You'll want
the date for sorting, and the file name for when you decide to
download it. If you want help with regular expressions, let me know.
If so, post a sample of the code you have so far for parsing, sample
input (the raw listing), and sample output showing exactly how you
want it to look. As long as you write some code (even if it's not
working the way you want), I don't mind helping you fix it up.
--
http://mail.python.org/mailman/listinfo/python-list


Re: I'm intrigued that Python has some functional constructions in the language.

2009-05-10 Thread Carl Banks
On May 10, 12:40 pm, namekuseijin 
wrote:
> Carl Banks wrote:
> > Now, maybe readability concerns don't matter to you personally, but it
> > does matter to the OP, who is trying to advocate functional
> > programming but is having difficulty because most purely functional
> > languages have hideous minimalist syntax that turns people off.
>
> BTW, rereading it again, the OP was originally questioning about
> compiler optimizations for purely functional constructs within Python,
> but somehow syntax got in the middle.

Syntax--the thing you claim doesn't matter--got in the middle because
it was the main factor that drove the OP to look for alternatives to
Haskell.

That's what makes your entire argument ridiculous; you are making this
theoretical argument like, "everything reduces to a function so it
doesn't matter what syntax you use," yet people in the real world are
out there trying to find alternatives because functional languages'
syntax sucks so bad in general.


> I don't see how "syntax would be a lot easier to understand" if it would
> "be possible to more clearly separate the pure code (without side
> effects) from the impure code (that deals with state changes, I/O,
> etc.), so that the pure code could be compiled and have aggressive
> functional transformations applied to it for efficiency."
>
> Syntax would remain the same, I guess.

You totally missed the point.

The thing that would make the syntax a lot easier to understand is
that it's in Python and not Haskell.

The reason the OP was asking about separating pure code from impure
was to see if some subset of Python could be used as a pure functional
language, that way they could employ Python and its already-much-
better-than-Haskell's syntax as a pedagogical replacement for Haskell.

I am sure there are many people who think that even "f a b" is cryptic
compared to "f(a,b)", but if that's the only issue it wouldn't be that
big of a deal.  It's not the only issue.  When a language requires you
to read and write stuff like "map :: (x -> y) -> f x -> f y" or "f s@
(x:xs) = x:s" then it's going to turn a lot of people off.


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


Re: What's the use of the else in try/except/else?

2009-05-10 Thread Scott David Daniels

kj wrote:

...  I can't come with an example in which the same couldn't be
accomplished with

try:
# do something
# do something else
except ...:
# handle exception

The only significant difference I can come up with is that in the
second form, the except clause may be masking some unexpected
exceptions from the "do something else" part.  Is this the rationale
behind this else clause?  Or is there something more to it?


Yes, in a way.  The idea of catching particular exceptions is to only
handle exceptions you expect (let the others go out to more general
reporters).  So, not only should you choose the tightest exception to
catch that you can, but you should look for it in a very narrow window:
exactly where you expect it.

try:
v = mumble.field
except AttributeError:
pass
else:
sys.warning('field was actually there?')

as opposed to:

try:
v = mumble.field
sys.warning('field was actually there?')
except AttributeError:
pass

The idea is to make it clear what you expect might go
wrong that you are prepared to handle.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


There may be a much better way to manage artillery.

2009-05-10 Thread Tobiah
I'm writing a video game with armed space ships.
I decided to make a class to manage all of the bullets
that may be on the screen at a given time:

class Bullets():

def __init__(self):
self.bullets = []

def update(self):
temp = []
for bullet in self.bullets:
bullet.update()
bullet.time_to_live -= 1
if bullet.time_to_live:
temp.append(bullet)
self.bullets = temp

def add(self, new_bullet):
self.bullets.append(new_bullet)

When the main loop calls .update() on the bullets
object, I want it to decrement a counter on each
bullet, and destroy the bullets who's time has come.

I wanted the bullets to be responsible for destroying
themselves, but a little Googling brought me to points
about dangling references and how an object is not allowed
(nor does it seem to have the means) to destroy itself. 
That's why I made this wrapper class for all of the bullets.

The idea is to copy bullets with time left into a temp list
and then overwrite the man bullets array with the good bullets.
I believe that the other bullets will be garbage collected.
I could not delete the items in place within the loop, of course.
Was there a better container than a list for my purposes?

Above is what I settled on, but I wonder
whether my approach is a good one.

Thanks very much,

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


Re: ISO exemplary Python scripts

2009-05-10 Thread Rhodri James

On Sat, 09 May 2009 17:09:38 +0100, kj  wrote:


I know that in the python world the distinction between a script
and a (library) module is not so clear-cut, and it is common for
library modules to have "top-level" stuff (typically test code)
that gets run if the module is invoked directly from the command
line.  But this is not *really* a script as I understand it, because,
even though it "runs" directly from the command-line, it lacks the
typical CLI amenities, such as command-line flags, help messages,
diagnostic messages that are aimed to the "naive user" (i.e. as
opposed to the developer), etc.  The coding of these "CLI amenities"
is one of aspects of these "exemplary Python scripts" I'm most
interested in learning about.


While I don't entirely agree with your definition of a script,
for a lot of those CLI amenities "optparse" is your friend.
Possibly also your enemy if you don't want to do things its way,
but it does at least make parameter handling in a user-friendly way
easy.

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


Re: mod_python and xml.dom.minidom

2009-05-10 Thread Graham Dumpleton
On May 10, 3:40 am, Paul Boddie  wrote:
> On 9 Mai, 01:36, dpapathanasiou  wrote:
>
>
>
> > Apache's configure utility (I'm using httpd version 2.2.11) doesn't
> > explicitly describe an expat library option.
>
> > Also, if libexpat is version 1.95.2, wouldn't I have to get version
> > 2.0 to be compatible with pyexpat?
>
> The aim would be to persuade Apache to configure itself against the
> same Expat library that pyexpat is using, which would involve the
> headers and libraries referenced during the pyexpat configuration
> process, although I seem to recall something about pyexpat bundling
> its own version of Expat - that would complicate matters somewhat.
>
> > If anyone has any advice or suggestions, I'd appreciate hearing them.
>
> Expat might be getting brought into Apache via mod_dav:
>
> http://www.webdav.org/mod_dav/install.html
>
> Perhaps disabling mod_dav when configuring Apache might drop Expat
> from Apache's library dependencies.

The OP was using Python 2.5, so shouldn't be an issue because pyexpat
properly name space prefixes its version of expat. See:

  http://code.google.com/p/modwsgi/wiki/IssuesWithExpatLibrary

where explicitly says that only applies to Python prior to Python 2.5.

His problem is therefore likely to be something completely different.

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


Re: There may be a much better way to manage artillery.

2009-05-10 Thread Rhodri James

On Mon, 11 May 2009 00:06:34 +0100, Tobiah  wrote:

[Snippety snip]


I wanted the bullets to be responsible for destroying
themselves, but a little Googling brought me to points
about dangling references and how an object is not allowed
(nor does it seem to have the means) to destroy itself.
That's why I made this wrapper class for all of the bullets.


An object can, however, erase its representation from the
screen and tell whatever control system is keeping track
of objects to forget about it.  Once the last reference to
the object is deleted, the object will be garbage collected.

What framework are you writing your game in?  Pygame has
facilities for handling this sort of thing fairly
straightforwardly.

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


Re: Wrapping comments

2009-05-10 Thread Rhodri James

On Sun, 10 May 2009 08:32:23 +0100, Tobias Weber  wrote:


In article ,
 Arnaud Delobelle  wrote:


A simple Alt-Q will reformat everything nicely.


Now that's something. Thanks!

(still not gonna use software that doesn't let me type # because it's
alt+3 on a UK layout; having to re-learn or configure that is just sick)


What on earth are you talking about?  '#' has its own key on a UK layout
(shared with '~', but you know what I mean), just to the left of the
RETURN key.  Emacs is my editor of choice, and I've never once come
across anything like this.

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


Re: SimpleXMLRPCServer and creating a new object on for each new client request.

2009-05-10 Thread Martin P. Hellwig

Piet van Oostrum wrote:

goo...@smetj.net (g) wrote:



g> Well, I think Martin's example will suit my needs.



g> Thanks for the explanation!


His client code is unnecessarily complicated with 3 session variables.
The following code does the same:

SESSION = xmlrpclib.ServerProxy(URL_PORT)
print(SESSION.show_random())
print(SESSION.show_random())
SESSION.shutdown_service()

I was actually aware of that, but I thought it would be more educational 
if I rewrote Jelle's example by using code conformity and a way to test 
server and client in a single script instead of just rewriting it to the 
given intent. With the knowledge provided, Jelle should be able to 
redesign his program to what he actually wants to do with it and cutting 
out the croft himself.


P.S. Piet ben je toevallig lid van NLUUG?

--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: help with recursive whitespace filter in

2009-05-10 Thread Steve Howell
On May 10, 10:23 am, rustom  wrote:
> On May 10, 9:49 pm, Steve Howell  wrote:
>
>
>
> > On May 10, 9:10 am, Rustom Mody  wrote:
>
> > > I am trying to write a recursive filter to remove whitespace-only
> > > nodes for minidom.
> > > The code is below.
>
> > > Strangely it deletes some whitespace nodes and leaves some.
> > > If I keep calling it -- like so: fws(fws(fws(doc)))  then at some
> > > stage all the ws nodes disappear
>
> > > Does anybody have a clue?
>
> > > from xml.dom.minidom import parse
>
> > > #The input to fws is the output of parse("something.xml")
>
> > > def fws(ele):
> > >     """ filter white space (recursive)"""
>
> > >    for c in ele.childNodes:
> > >         if isWsNode(c):
> > >             ele.removeChild(c)
> > >             #c.unlink() Makes no diff whether this is there or not
> > >         elif c.nodeType == ele.ELEMENT_NODE:
> > >             fws(c)
>
> > > def isWsNode(ele):
> > >     return (ele.nodeType == ele.TEXT_NODE and not ele.data.strip())
>
> > I would avoid doing things like delete/remove in a loop.  Instead
> > build a list of things to delete.
>
> Yeah I know. I would write the whole damn thing functionally if I knew
> how.  But cant figure out the API.
> I actually started out to write a (haskell-style) copy out the whole
> tree minus the unwanted nodes but could not figure it out

You can use list comprehensions for a more functional style.

Instead of deleting the unwanted nodes in place, try to create new
lists of just the wanted results.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pythonic way to normalize vertical whitespace

2009-05-10 Thread Stephen Hansen
On Fri, May 8, 2009 at 8:53 AM,  wrote:

>  I'm looking for suggestions on technique (not necessarily code) about the
> most pythonic way to normalize vertical whitespace in blocks of text so that
> there is never more than 1 blank line between paragraphs. Our source text
> has newlines normalized to single newlines (\n vs. combinations of \r and
> \n), but there may be leading and trailing whitespace around each newline.
>

I'm not sure what's more Pythonic in this case, but the approach I'd use is
a generator. Like so:

--- code to follow --

def cleaner(body):
empty = False
for line in body.splitlines():
line = line.rstrip()
if line:
empty = False
yield line
else:
if empty:
continue
else:
empty = True
yield ''

Text = """
This is my first paragraph.

This is my second.


This is my third.
This is my fourth.

This is my fifth.



This is my sixth.





This is my seventh.

Okay?
"""

print '\n'.join(cleaner(Text))

--- end code ---

You maybe want to tweak the logic a bit depending on how you want to handle
initial blank lines or such, and I'm two paragraphs w/o a blank line between
them (you talk some of going from \n\n\n to \n\n, so I'm not sure if you
want to allow blank lines separating the paragraphs or require it).

I guess this basically is your first approach, just specifically going with
"Use a generator to do it" instead of regular list building cuz that should
be faster and cleaner int his case and I don't think the third approach
is really all that needed. Python can do this pretty quickly.

The second approach seems distinctly unPythonic. I use regular expressions
in several places, mind, but if there's a straight-forward clean way to do
anything without them that always seems like the Pythonic thing to me. :)

But that's just me :)

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


Re: How can I dynamically insert a base class in a given class

2009-05-10 Thread Gabriel Genellina
En Sun, 10 May 2009 00:04:17 -0300, Дамјан Георгиевски   
escribió:



How can I dynamically insert a base class in a given class? Yeah, I'm
writing a class decorator that needs to manipulate the class by
inserting another base class in it.


In this case, as you want to modify the base classes, I think a metaclass  
is more apropiate (the decorator acts too late, *after* the class has  
already been created).

Based on your code:

class ReallyBase(object):
   def someimportantmethod(self):
 return 'really really'

def dynamically_determine_metaclass(args):

class MyMetaclass(type):
   def __new__(meta, name, bases, namespace):
 bases = (ReallyBase,) + bases
 namespace['some_value'] = args
 return type.__new__(meta, name, bases, namespace)

return MyMetaclass

class Unsuspecting(object):
   __metaclass__ = dynamically_determine_metaclass(123)
   def stuff(self):
 return "ok"

u = Unsuspecting()
print u.stuff(), u.someimportantmethod(), u.some_value,  
Unsuspecting.some_value


--
Gabriel Genellina

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


Re: There may be a much better way to manage artillery.

2009-05-10 Thread draeath
Tobiah wrote:

> I'm writing a video game with armed space ships.
> I decided to make a class to manage all of the bullets
> that may be on the screen at a given time:
> 
> class Bullets():
> 
> def __init__(self):
> self.bullets = []
> 
> def update(self):
> temp = []
> for bullet in self.bullets:
> bullet.update()
> bullet.time_to_live -= 1
> if bullet.time_to_live:
> temp.append(bullet)
> self.bullets = temp
> 
> def add(self, new_bullet):
> self.bullets.append(new_bullet)
> 
> When the main loop calls .update() on the bullets
> object, I want it to decrement a counter on each
> bullet, and destroy the bullets who's time has come.
> 
> I wanted the bullets to be responsible for destroying
> themselves, but a little Googling brought me to points
> about dangling references and how an object is not allowed
> (nor does it seem to have the means) to destroy itself.
> That's why I made this wrapper class for all of the bullets.
> 
> The idea is to copy bullets with time left into a temp list
> and then overwrite the man bullets array with the good bullets.
> I believe that the other bullets will be garbage collected.
> I could not delete the items in place within the loop, of course.
> Was there a better container than a list for my purposes?
> 
> Above is what I settled on, but I wonder
> whether my approach is a good one.
> 
> Thanks very much,
> 
> Toby

This also gives you the means to destroy the oldest bullet when creating a 
new one, once some arbitrary limit was reached. Think back to how old games 
like Raptor worked.

If you had the bullets take care of their lifetime themselves, you would 
have to have more code touching all the bullets to do this. Now, you can add 
a little bit while you are already working through them all, saving yourself 
some cycles I would wager.


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


Re: I'm intrigued that Python has some functional constructions in the language.

2009-05-10 Thread Paul Rubin
Carl Banks  writes:

> Syntax--the thing you claim doesn't matter--got in the middle because
> it was the main factor that drove the OP to look for alternatives to
> Haskell.

I don't think so.  The OP said that "... the syntax would be a lot
easier to understand, than most functional languages, like Haskell".
He didn't say that Haskell syntax caused him to look for alternatives
and didn't say that Haskell wouldn't still be difficult even if its
syntax were simpler.  Hint: Liskell (Haskell with S-expression syntax)
is still difficult.

> yet people in the real world are out there trying to find
> alternatives because functional languages' syntax sucks so bad in general.

Again, I don't think so.  Functional languages' syntax turns some
people off before they even look at other aspects closely, just like
Python's indentation-based syntax turns some people off.  But, once
you get past the syntax, functional languages STILL present a lot of
obstacles that lots of users never get past.

> The reason the OP was asking about separating pure code from impure
> was to see if some subset of Python could be used as a pure functional
> language, that way they could employ Python and its already-much-
> better-than-Haskell's syntax as a pedagogical replacement for Haskell.

That wouldn't make sense at all.  60% of what makes Haskell different
(and harder) than Python is probably the complicated (but powerful)
static type system; 35% is the nonstrict evaluation strategy, and 5%
the syntax.  If you somehow added a method in Python to separate pure
from impure code, but it was still a dynamic language with strict
evaluation, you'd only be 5% of the way to replacing Haskell.
--
http://mail.python.org/mailman/listinfo/python-list


stand alone exec

2009-05-10 Thread prakash jp
Hi all,

I want to run dos commands through python stand alone execs. The created
Python stand alone executable (py2exe)  works fine

in my machine but on transferring the "dist" folder to other systems the
executable fails to run.

I tried to copy the MSVCP90.dll in the "dist" folder. Also tried to exclude
the same dll in the options of the setup.py file

The error reads as follows :

"The application has failed to start because the application configuration
is incorrect. Reinstalling the application may fix this problem".

Details of the installed setup files may be useful :

1- python-2.6.1.msi
2- py2exe-0.6.9.win32-py2.6.exe
3- pywin32-212.win32-py2.6.exe

Thanks in advance

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


Re: Web Based Front End?

2009-05-10 Thread Vinicius Assef
On Thu, May 7, 2009 at 10:52 PM,   wrote:
[snip]
>
> So, my question is what is the best method to be able to have a user
> enter specific data on a web page, have that data be passed to the
> Python script and then have the output returned to the web page?

Django is the buzzword, among python community here, in Brazil.

In fact it is really powerful. Give it a try. ;-)

--
[ ]s
Vinicius Assef.
http://aprenda-python.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list



Importing from a module which contains more than one Class...

2009-05-10 Thread GKalman

OS= MS Vista
File structure:
.../Module  (i.e a Folder with 2 sub-folders)
.../Module_Class(sub-folder #1)
   /MyClass.py
.../Module_Class_Testing  (sub_folder #2)
 /module_class_driver.py

Here is the code for the two (simplified) Python files:


#this is module_class_driver.py
#==
import sys

dir1='C:\\Programming\\Python\\Prototypes\\Core_Python_Prototypes\\Module\\Module_Class'
sys.path.append(dir1)


from MyClass import *
from MyOtherClass import * # error msg: no such module!

c=MyClass(2.5)
print c.double_it()

cc=MyOtherClass(5.0)  #error
print cc.triple_it()   #error


#this is MyClass.py
#
class MyClass:
def __init__(self,x):
self.x=x
def double_it(self):
return 2*self.x

class MyOtherClass:
def __init__(self,y):
self.y=y
def triple_it(self):
return 3*self.y

Question:
***
As I mentioned above,  the code for MyClass & MyOtherClass is in the same
file . This program only works with a single Class in a file. That is when
the File name is the SAME as the Class name.

How to import from a File which contains more than one (unrelated) Classes?



-- 
View this message in context: 
http://www.nabble.com/Importing-from-a-module-which-contains-more-than-one-Class...-tp23476815p23476815.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: Importing from a module which contains more than one Class...

2009-05-10 Thread alex23
GKalman  wrote:
> from MyClass import *
> from MyOtherClass import *     # error msg: no such module!
>
> As I mentioned above,  the code for MyClass & MyOtherClass is in the same
> file . This program only works with a single Class in a file. That is when
> the File name is the SAME as the Class name.
>
> How to import from a File which contains more than one (unrelated) Classes?

You seem to have misunderstood how 'import' works, I strongly
recommend re-reading the tutorial section:
http://docs.python.org/tutorial/modules.html

Basically, the import statement has two forms:

   1. from  import 
   2. import 

So your example of 'from  import *' just doesn't make a lot
of sense within Python.

If MyClass.py contains two classes, MyClass and MyOtherClass, you
should be able to import them with:

   from MyClass import MyClass, MyOtherClass

However, you _should_ be already getting _everything_ MyClass.py
contains as you're using the grab-all asterisk. Could you open a
python shell in your Module_Class folder, type 'from MyClass import
MyClass, MyOtherClass', and past whatever traceback you get here?
--
http://mail.python.org/mailman/listinfo/python-list


Re: ISO exemplary Python scripts

2009-05-10 Thread Tim Roberts
kj  wrote:
>
>Thanks, but the last bit of your post ("...most of which have the
>ability to run by themselves") makes me wonder whether we mean the
>same thing when we talk of "scripts."  Can you give me an example
>of a script that *does not* have the ability to run by itself?
>When I use the word "script" I mean, *by definition*, a piece of
>code that has the ability to run by itself.
>
>I know that in the python world the distinction between a script
>and a (library) module is not so clear-cut,

Exactly.  This is true for many of the languages that are traditionally
interpreted.  Things start off as standalone applications and morph into
general purpose modules, or vice versa.  As a result, I lazily call any
Python source file a "script".

>But this is not *really* a script as I understand it, because,
>even though it "runs" directly from the command-line, it lacks the
>typical CLI amenities, such as command-line flags, help messages,
>diagnostic messages that are aimed to the "naive user" (i.e. as
>opposed to the developer), etc.  The coding of these "CLI amenities"
>is one of aspects of these "exemplary Python scripts" I'm most
>interested in learning about.

In my vocabulary, I'd probably call this an "application".

I guess my basic point is that the standard library contains good examples
of Python programming, and the "Python way of thinking".  Command-line
argument processing is not a particularly unique task, so the same
techniques that work for parsing things from files, or for handling
arguments in a list, work equally well for handling arguments, especially
with the help of getopt and optparse.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode bit me

2009-05-10 Thread Terry Reedy

anuraguni...@yahoo.com wrote:


so unicode(obj) calls __unicode__ on that object


It will look for the existence of type(ob).__unicode__ ...

> and if it isn't there __repr__ is used

According to the below, type(ob).__str__ is tried first.


__repr__ of list by default return a str even if __repr__ of element
is unicode


From the fine library manual, built-in functions section:
(I reccommend using it, along with interactive experiments.)

"repr( object)
Return a string ..."

"str( [object])
Return a string ..."

"unicode( [object[, encoding [, errors]]])

Return the Unicode string version of object using one of the following 
modes:


If encoding and/or errors are given, ...

If no optional parameters are given, unicode() will mimic the behaviour 
of str() except that it returns Unicode strings instead of 8-bit 
strings. More precisely, if object is a Unicode string or subclass it 
will return that Unicode string without any additional decoding applied.


For objects which provide a __unicode__() method, it will call this 
method without arguments to create a Unicode string. For all other 
objects, the 8-bit string version or representation is requested and 
then converted to a Unicode string using the codec for the default 
encoding in 'strict' mode.

"

'unicode(somelist)' has no optional parameters, so skip to third 
paragraph.  Somelist is not a unicode instance, so skip to the last 
paragraph.  If you do dir(list) I presume you will *not* see 
'__unicode__' listed.  So skip to the last sentence.

unicode(somelist) == str(somelist).decode(default,'strict').

I do not believe str() and repr() are specifically documented for 
builtin classes other than the general description, but you can figure 
that str(collection) or repr(collection) will call str or repr on the 
members of the collection in order to return a str, as the doc says. 
(Details are available by experiment.)  Str(uni_string) encodes with the 
default encoding, which seems to be 'ascii' in 2.x.  I am sure it uses 
'strict' errors.


I would agree that str(some_unicode) could be better documented, like 
unicode(some_str) is.



so my only solution looks like to use my own list class everywhere i
use list
class mylist(list):
def __unicode__(self):
return u"["+u''.join(map(unicode,self))+u"]"


Or write a function and use that instead, or, if and when you can, 
switch to 3.x where str and repr accept and produce unicode.


tjr

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


Re: Learning C++ for Python Development

2009-05-10 Thread Roger Binns
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

joshua.pea...@gmail.com wrote:
> Or, just give me some general advice on learning C++ for Python?

You may want to start with Cython first.  It lets you intersperse C and
C level information with Python code to produce extensions.  That will
give you a gentler introduction to using a C like language with Python
and for many real world uses is actually sufficient to get C levels of
performance and interfacing with Python.

After you get that working then you can try coding your own extensions
(the Python docs include all the necessary information) and also trying
other tools such as SWIG which automate some of the code for wrapping
C++, or the wrapping functionality present in Boost.

I find it most helpful to explore doing the same thing in multiple
different ways as it teaches you what is important and what isn't.
Later on it will help you a lot better in choosing the right tool for
the job (no one tool or approach is perfect) and help evaluate anything
that comes up later.

My suggestion would be to pick a particular task and code it in pure
Python.  Benchmark it (cpu and memory) and then try the other approaches
(Cython, Boost, hand coded, SWIG) and see how they compare.  Something
like a brute force Suduko solver would let you have a few
classes/objects but not be too much code.

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

iEYEARECAAYFAkoHy/QACgkQmOOfHg372QTxywCgrJPcHLQ5BzD8O29nSAW+I5xo
juYAnjMP0xqn/TzC5mrTCqBT3ZnIQo24
=KeIk
-END PGP SIGNATURE-

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