Pyro 3.10 released

2009-12-09 Thread Irmen de Jong

Hi,

Pyro 3.10 has been released!

Pyro is a an advanced and powerful Distributed Object Technology system 
written entirely in Python, that is designed to be very easy to use.

Have a look at http://pyro.sourceforge.net for more information.

Highlights of this release are:
- improvements in the SSL configuration
- uses new-style classes so super() now works in Pyro objects
- various minor bugfixes

As always the detailed changes are in the changes chapter in the manual.
Please read this for more details and info before upgrading.

You can download Pyro 3.10 from sourceforge: 
http://sourceforge.net/projects/pyroor from PyPI 
http://pypi.python.org/pypi/Pyro/



Enjoy,

Irmen de Jong

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

   Support the Python Software Foundation:
   http://www.python.org/psf/donations/


ANN: pyrtm-0.2 released

2009-12-09 Thread Sridhar Ratnakumar
pyrtm is a Python API binding for accessing Remember The Milk. This
releases fixes a few issues related to API spec.

* app.py: Fix for unreliable type of 'taskseries' (#5)
* rtm.py: Fixed incorrect number of arguments to lists (#3)
* rtm.py: Try to import django.utils.simplejson before falling back
* app.py: Made it a command-line app accepting key/secret/token as args

To download pyrtm, visit:

  http://pypi.python.org/pypi/pyrtm

You can also install pyrtm using pip or PyPM:

  $ pip install pyrtm
  $ pypm install pyrtm

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

Support the Python Software Foundation:
http://www.python.org/psf/donations/


Pydev 1.5.2 Released

2009-12-09 Thread Fabio Zadrozny
Hi All,

Pydev 1.5.2 has been released

Details on Pydev: http://pydev.org
Details on its development: http://pydev.blogspot.com

Release Highlights:
---

* Profile to have much lower memory requirements (especially on
code-analysis rebuilds)
* Profile for parsing to be faster
* Compare Editor
  o Syntax highlighting integrated
  o Editions use the pydev editor behavior
  o Code completion works
* Fixed issue where pydev could deadlock
* No longer reporting import redefinitions and unused variables for
the initial parts of imports such as import os.path
* Fixed issue where pydev was removing __classpath__ from the
pythonpath in jython
* Using M1, M2 and M3 for keys instead of hardcoding Ctrl, Shift and
Alt (which should make keybindings right on Mac OS)
* Fixed some menus and popups
* Properly categorizing Pydev views
* Handling binary numbers in the python 2.6 and 3.0 grammar
* from __future__ import print_function works on python 2.6
* Added drag support from the pydev package explorer
* Properly translating slashes on client/server debug
* Other minor fixes


What is PyDev?
---

PyDev is a plugin that enables users to use Eclipse for Python, Jython
and IronPython development -- making Eclipse a first class Python IDE
-- It comes with many goodies such as code completion, syntax
highlighting, syntax analysis, refactor, debug and many others.


Cheers,

-- 
Fabio Zadrozny
--
Software Developer

Aptana
http://aptana.com/python

Pydev - Python Development Environment for Eclipse
http://pydev.org
http://pydev.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations/


ANN: TextTest 3.16.1

2009-12-09 Thread Geoff Bache
Hi all,

TextTest 3.16 was released last week and there is now a bugfix
release also. The main changes are around the HTML batch
report, which will amongst other things now generate you a nice
dashboard page giving the latest status of all your applications.
There is also integration with the Jira bug tracker, and improvements
to basic functionality like run_dependent_text and collate_file.

Regards,
Geoff Bache


TextTest is a tool for automatic text-based functional testing. This
means running a batch-mode executable in lots of different ways from
the command line, and using the text output produced as a means of
controlling the behavior of that application. As well as being usable
standalone, it is an extendable framework for black-box testing
written in Python.

Homepage: http://www.texttest.org
Download: http://sourceforge.net/projects/texttest
Mailing list: https://lists.sourceforge.net/lists/listinfo/texttest-users
Bugs: https://bugs.launchpad.net/texttest
Source: https://code.launchpad.net/texttest
-- 
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations/


ANN: PyUseCase 3.0.1 - GUI testing for Python UIs

2009-12-09 Thread Geoff Bache
A new major release of PyUseCase came out last week with some
big improvements on previous versions, and now there is a bugfix release
tidying it up also.

If you've looked at it before and decided it was too hard to use it might be
time to try again. It no longer requires any instrumentation or any logging
on your part, it generates both for you.

Regards,
Geoff Bache

Summary for those who haven't seen it before:

PyUseCase is an unconventional GUI testing tool for PyGTK, along
with a framework for testing Python GUIs in general.

Instead of recording GUI mechanics directly, it asks the user for
descriptive names and hence builds up a domain language along with a
UI map file that translates this language into actions on the
current GUI widgets. The point is to reduce coupling, allow very
expressive tests, and ensure that GUI changes mean changing the UI map
file but not all the tests.

Instead of an assertion mechanism, it auto-generates a log of the
GUI appearance and changes to it. The point is then to use that as a
baseline for text-based testing, using e.g. TextTest.

It also includes support for instrumenting code so that waits can be
recorded, making it far easier for a tester to record correctly
synchronized tests without having to explicitly plan for this.

Homepage: http://www.texttest.org/index.php?page=ui_testing
Download: http://sourceforge.net/projects/pyusecase
Mailing list: https://lists.sourceforge.net/lists/listinfo/pyusecase-users (new)
Bugs: https://bugs.launchpad.net/pyusecase/
Source: https://code.launchpad.net/pyusecase/
-- 
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations/


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

2009-12-09 Thread Gabriel Genellina
QOTW:  I'm not sure you ever understood what the problem was, or where, but
I'm happy you feel like you've solved it. - Marco Mariani

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


Python 2.7a1, the first alpha release of the 2.7 series, is available now:
http://groups.google.com/group/comp.lang.python/t/743e13e80290fbbf/

A bidirectional dictionary structure analyzed:
http://groups.google.com/group/comp.lang.python/t/785d100681f7d101/

How to define and use a nested class:
http://groups.google.com/group/comp.lang.python/t/6649dfa6eb9797c8/

PyCon 2010 registration opens:

http://pycon.blogspot.com/2009/11/registration-for-pycon-2010-is-open.html

Why not have a local variable scope smaller than a function?
http://groups.google.com/group/comp.lang.python/t/57a8dec0d5d5deda/

How to test whether two floating point values are close enough to be
almost equal:
http://groups.google.com/group/comp.lang.python/t/54ec18c06c46caaf/

A special notation for accessing dynamic attribute names:
http://groups.google.com/group/comp.lang.python/t/2f5674a1f451b12f/

Computed attributes, properties, and the descriptor protocol magic:
http://groups.google.com/group/comp.lang.python/t/c07268689549cf01/

Name resolution, global names, importing modules, and how all of them
interact:
http://groups.google.com/group/comp.lang.python/t/55afecc0d9e3e6c/
http://groups.google.com/group/comp.lang.python/t/de91efba9c5d68ae/

Are there any high-volume Python-powered websites?
http://groups.google.com/group/comp.lang.python/t/7e388f22cccf7c4b/

How decoupled are the Python web frameworks?
http://groups.google.com/group/comp.lang.python/t/f04940f9d4b136bc/

Moving from Python 2 to Python 3: A cheat sheet by Mark Summerfield
http://groups.google.com/group/comp.lang.python/t/53716c4136be473b/
The sheets:
http://www.informit.com/promotions/promotion.aspx?promo=137519

Thoughts about bytecode optimization and measuring overhead of the eval
loop:
http://groups.google.com/group/comp.lang.python/t/115b24c0ee9ee06a/

Python as the starting point for a career on IT:
http://groups.google.com/group/comp.lang.python/t/907419d0cdd68570/

Idea: get around the GIL using XMLRPC
http://groups.google.com/group/comp.lang.python/t/9f84e7aa634de4f5/

Bored and looking for something to do in Python:
http://groups.google.com/group/comp.lang.python/t/bba7c231bb9940a4/



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

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

PythonWare complements the digest you're reading with the
marvelous daily python url
 http://www.pythonware.com/daily

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

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

Readers have recommended the Planet site:
http://planet.python.org

comp.lang.python.announce announces new Python software.  Be
sure to scan this newsgroup weekly.
http://groups.google.com/group/comp.lang.python.announce/topics

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

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

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

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

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

The Summary of Python Tracker Issues is an automatically generated
report summarizing new bugs, closed ones, and patch submissions. 

http://search.gmane.org/?author=status%40bugs.python.orggroup=gmane.comp.python.develsort=date

Although unmaintained 

Re: switch

2009-12-09 Thread Carl Banks
On Dec 8, 9:36 pm, Asun Friere afri...@yahoo.co.uk wrote:
 This code is probably symptomatic of poor design. (Not to mention that
 your condition tests).  For which reason python has no 'case'
 statement and why no decent OO language should.

 It is a principle of OO design that an object should know what to do
 itself.  Rather running an object though a series of tests, it is
 better to send the object a message, relying on polymorphism or duck-
 typing, and deal with any exceptions thrown.

What if the object is a string you just read from a file?

How do you dispatch using polymorphism in that case?


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


Re: switch

2009-12-09 Thread Bruno Desthuilliers

Steven D'Aprano a écrit :

On Tue, 08 Dec 2009 21:36:23 -0800, Asun Friere wrote:

(snip)

It is a principle of OO design that an object should know what to do
itself.  Rather running an object though a series of tests, it is
better to send the object a message, relying on polymorphism or duck-
typing, and deal with any exceptions thrown.


But putting that aside, I find myself wondering how you would deal with 
the following switch-like series of tests.



def print_grades(score):
if not 0 = score = 100:
raise ValueError(score must be between 0 and 100)
if score  50:
print You have failed.
consider_suspension()
elif score == 50:
print You have just passed by the skin of your teeth.
elif score  60:
print You have scored a D. You need to try harder.
elif score  70:
print You have scored a C.
elif score  80:
print You have scored a B. Well done.
elif score  100:
print Congratulations, you have scored an A.
else:
assert score == 100
print You have scored a PERFECT 100% SCORE!!!
if not evidence_of_cheating():
call_newspapers()



Well, obviously such business rules must by no mean be hardcoded. You 
really need a rule engine, configurable by your domain experts thru a 
DSL that we'll design specially for you. The rule engine will generate 
an AbstractScoreFactory that will instanciate appropriate IScore 
implementation objects that knows what to do.


You also need to decouple the output mechanism - what if you need to 
output to a web page, an IPhone app, a RSS stream, an audio stream or 
clay tablets ? To allow for maximum decoupling, the output mechanism 
should be configurable thru a strict, well defined and universally 
understood language - I mean XML, of course.




Obviously that could, with a non-trivial amount of work, be turned into a 
dictionary dispatch, 


Dictionnary dispatch ? C'mon, you must be joking ? An enterprise 
application is not some QD cowboy script, you know ? You do have to 
apply state of the art designs and patterns to do it properly


g


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


problem in freezing python script...

2009-12-09 Thread Jebagnana Das
Hello all,

 I'm using cxfreeze to freeze the python script in my ubuntu
9.04 machine. Now when i tried to execute the binary in mandriva 2008 the
error message is..

File
/usr/local/lib/python3.1/site-packages/cx_Freeze/initscripts/Console3.py,
line 27, in module
  File test.py, line 7, in module
  File ExtensionLoader_PyQt4_QtGui.py, line 12, in module
ImportError: /home/oputils/dist/PyQt4.QtGui.so: *undefined symbol:
_ZTI19QStyledItemDelegate*

 when i searched the python mailing list i found this issue has been
already raised http://www.mail-archive.com/pyt...@py.cz/msg04278.html and is
in french language(i think) which i don't know.. Can any of you restate this
in english?? Your help would be much appreciated..

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


UnboundLocalError: local variable '_[1]' referenced before assignment

2009-12-09 Thread Gabriel Rossetti

Hello everyone,

I get this error on python 2.6.1 on mac os x 10.6 :

UnboundLocalError: local variable '_[1]' referenced before assignment

here's the code that raises this:

params = [ self.__formatData(paramProcFunc, query, p) for p in params ]

what I don't get is that it worked on mac os x 10.5 (python 2.5.x) but 
it no longer works. I tried the following and it works :


r = []
for p in params:
   r.append(self.__formatData(paramProcFunc, query, p))
params = r

Does anyone understand what is going on here?

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


Re: UnboundLocalError: local variable '_[1]' referenced before assignment

2009-12-09 Thread Richard Thomas
On Dec 9, 10:17 am, Gabriel Rossetti gabriel.rosse...@arimaz.com
wrote:
 Hello everyone,

 I get this error on python 2.6.1 on mac os x 10.6 :

 UnboundLocalError: local variable '_[1]' referenced before assignment

 here's the code that raises this:

 params = [ self.__formatData(paramProcFunc, query, p) for p in params ]

 what I don't get is that it worked on mac os x 10.5 (python 2.5.x) but
 it no longer works. I tried the following and it works :

 r = []
 for p in params:
     r.append(self.__formatData(paramProcFunc, query, p))
 params = r

 Does anyone understand what is going on here?

 Thank you,
 Gabriel

That isn't an error that should occur, not least because _[1] isn't a
valid name. Can you post a full traceback?

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


Question about 'remote objects'

2009-12-09 Thread Frank Millman
Hi all

I am writing a multi-user business/accounting application. It is getting 
rather complex and I am looking at how to, not exactly simplify it, but find 
a way to manage the complexity.

I have realised that it is logically made up of a number of services -
database service with connection to database
workflow engine for business processes
services manager to handle automated services, such as web services
client manager to service logins from client workstations
possibly others

I have made a start by splitting some of these off into separate modules and 
running them in their own threads.

I am concerned about scalability if they are all running on the same 
machine, so I am looking into how to enable these services to run on 
separate servers if required.

My first thought was to look into Pyro. It seems quite nice. One concern I 
had was that it creates a separate thread for each object made available by 
the server. My database server creates separate objects for each instance of 
a row read in from the database, and with multiple users running multiple 
applications, with each one opening multiple tables, this could run into 
hundreds, so I was not sure if that would work.

Then I read that the multiprocessing module allows processes to be spread 
across multiple servers. The documentation is not as clear as Pyro's, but it 
looks as if it could do what I want. I assume it would use processes rather 
than threads to make multiple objects available, but I don't know if there 
is a practical limit.

Then I thought that, instead of the database server exposing each object 
remotely, I could create one 'proxy' object on the server through which all 
clients would communicate, and it in turn would communicate with each 
instance locally.

That felt more managable, but then I thought - why bother with remote 
objects at all? Why not just run a SocketServer on the database server, and 
design a mini-protocol to allow clients to make requests and receive 
results. This is a technology I am already comfortable with, as this is how 
I handle client workstation logins. If I did go this route, I could apply 
the same principle to all the services.

I don't have the experience to make an informed decision at this point, so I 
thought I would see if there is any consensus on the best way to go from 
here.

Is there any particular benefit in using remote objects as opposed to 
writing a SocketServer?

Any advice will be much appreciated.

Thanks

Frank Millman



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


ANN: London Financial Python Users Group

2009-12-09 Thread Giles Thomas
Hi all,

If you're based in or visiting London, and have an interest in using
Python for financial software, you might want to come to the next
meeting of the London Financial Python Users Group!

The time: Monday 14 December 2009 at 7pm
The place: MWB Regent Street, Liberty House 222 Regent Street,
London W1B 5TR

We've got a couple of lightning talks lined up, but if you're
interested in giving one yourself then drop Didrik Pinte a line at
dpi...@enthought.com.

There's a (very minimal) Wiki page about the Users Group here: http://
wiki.python.org/moin/LondonFinancialPythonUserGroup


Cheers,

Giles
--
Giles Thomas
giles.tho...@resolversystems.com
+44 (0) 20 7253 6372

17a Clerkenwell Road, London EC1M 5RD, UK
VAT No.: GB 893 5643 79
Registered in England and Wales as company number 5467329.
Registered address: 843 Finchley Road, London NW11 8NA, UK
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: relative imports with the __import__ function

2009-12-09 Thread Chris Colbert
On Tue, Dec 8, 2009 at 5:48 PM, Peter Otten __pete...@web.de wrote:
 Chris Colbert wrote:

 I have package tree that looks like this:

 main.py
 package
 __init__.py
 configuration.ini
 server
 __init__.py
 xmlrpc_server.py
 controller.py
 reco
 irrelevant.py's
 segmentation
 __init__.py
 red_objects.py
 other irrelevant folders


 main.py launches an instance of xmlrpc_server.py which, in turn,
 imports controller.py.
 controller.py reads configuration.ini to determine which
 module/function to import from the segmentation directory and
 subsequently use.

 that config file specifies the module as 'red_objects' and the
 function as 'segment_red'.

 I am trying to dynamically import that module and func using the
 __import__ statement but keep getting empty module errors.


 I'm assuming i'm missing something fundamental on the import resolution...

 After some experimentation it turns out you have to provide some context for
 __import__() to determine the absolute location of the requested module. The
 required bit of information is the current module's __name__ attribute which
 you can provide via the globals parameter:

 def import_segmentation(name):
    return getattr(__import__(segmentation. + name, level=2,
 globals=globals()), name)

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


Many thanks Peter!
Almost like a charm!

It seems the relative import level is dependent on the location of the
main entry module. I thought the whole idea of relative imports was to
make the import independent of the entry point?

here is the import function i'm using

def import_segmentation(self):
   # get the segmentation function defined in configuration.ini
   parent_dir = os.path.split(os.path.dirname(__file__))[0]
   prsr = ConfigParser.ConfigParser()
   prsr.read(os.path.join(parent_dir, 'configuration.ini'))
   seg_mod = prsr.get('segmentation', 'module')
   seg_func = prsr.get('segmentation', 'function')
   print __name__
   smod = __import__('segmentation.%s' % seg_mod, globals=globals(),
 fromlist=[seg_func], level=2)
   sfunc = getattr(smod, seg_func)
   return sfunc

for that import level of 2 to work the tree must look like this:

main.py
package
__init__.py
configuration.ini
server
__init__.py
xmlrpc_server.py
controller.py
reco
irrelevant.py's
segmentation
__init__.py
red_objects.py
other irrelevant folders

but if I rearrange the package structure like this (just moving the
location of main.py):

package
main.py
__init__.py
configuration.ini
server
__init__.py
xmlrpc_server.py
controller.py
reco
irrelevant.py's
segmentation
__init__.py
red_objects.py
other irrelevant folders

I have to change the import to level=1 or I get this error:
ValueError: Attempted relative import beyond toplevel package

I don't understand why the location of my main.py should have ANY
bearing on relative import resolution.

But again, i'm probably just being dense and need someone to explain
it to me in newb speak ;)

Cheers,

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


Pydev 1.5.2 Released

2009-12-09 Thread Fabio Zadrozny
Hi All,

Pydev 1.5.2 has been released

Details on Pydev: http://pydev.org
Details on its development: http://pydev.blogspot.com

Release Highlights:
---

* Profile to have much lower memory requirements (especially on
code-analysis rebuilds)
* Profile for parsing to be faster
* Compare Editor
  o Syntax highlighting integrated
  o Editions use the pydev editor behavior
  o Code completion works
* Fixed issue where pydev could deadlock
* No longer reporting import redefinitions and unused variables for
the initial parts of imports such as import os.path
* Fixed issue where pydev was removing __classpath__ from the
pythonpath in jython
* Using M1, M2 and M3 for keys instead of hardcoding Ctrl, Shift and
Alt (which should make keybindings right on Mac OS)
* Fixed some menus and popups
* Properly categorizing Pydev views
* Handling binary numbers in the python 2.6 and 3.0 grammar
* from __future__ import print_function works on python 2.6
* Added drag support from the pydev package explorer
* Properly translating slashes on client/server debug
* Other minor fixes


What is PyDev?
---

PyDev is a plugin that enables users to use Eclipse for Python, Jython
and IronPython development -- making Eclipse a first class Python IDE
-- It comes with many goodies such as code completion, syntax
highlighting, syntax analysis, refactor, debug and many others.


Cheers,

-- 
Fabio Zadrozny
--
Software Developer

Aptana
http://aptana.com/python

Pydev - Python Development Environment for Eclipse
http://pydev.org
http://pydev.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Request for py program to insert space between two characters and saved as text?

2009-12-09 Thread Dave Angel



r0g wrote:

Dave Angel wrote:
  

r0g wrote:


Dave Angel wrote:
 
  

r0g wrote:
   


Dennis Lee Bieber wrote:
 
 
  

On Tue, 8 Dec 2009 08:26:58 +0530, 74yrs old withblessi...@gmail.com
declaimed the following in gmane.comp.python.general:

  


For Kannada project .txt(not .doc) is used, my requirement is to
have one

  

big snip
 
  
  

That's even worse.  As far as I can tell, the code will never do what he
wants in Python 2.x.   The Kannada text file is full of Unicode
characters in some encoding, and if you ignore the encoding, you'll just
get garbage.





Ah, fair enough. In my defence though I never saw the original post or
this kannada.txt file as my newsserver is not so much with the
reliability. I guess it's naive to assume an english .txt file is going
to be in ASCII these days eh?

I've yet to try python 3 yet either, this whole Unicode thing looks like
it could be a total nightmare! :(

Roger.

  
But it isn't an english  .txt file, it's a Kannada  .txt file.   
Presumably you didn't realize that Kannada is a (non-English) language, 
spoken in parts of India, with several hundred characters.  ASCII wasn't 
even an option.  Anyway, no harm done, someone else referred the OP to a 
Python user-group local to that region.


DaveA

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


Re: In lista infinita?

2009-12-09 Thread Andrea Crotti
On 8 Dic, 18:50, Andreas Waldenburger use...@geekmail.invalid wrote:
 Maybe. But I'm sure it.comp.lang.python might help you better. And from
 the looks of it, you seem to have started a similar thread there
 (called Generatori infiniti).

 Generally, you'll fare better with English (even broken English will be
 fine.) in this group. It's just nicer for everyone if they can
 understand all messages and not feel left out.

 Anyway, good luck with your question. I didn't understand a whole lot
 of it, but I guess you want to test for membership in an infinite
 list/generator. Yes, possible in principle, but obviously eats memory
 for larger generators. Also, consumes the generators, which is also a
 factor.


I'm sorry of course it was a mistake, I wanted to post on it.comp. but
I had this other group open...

Anyway it was not really a question, that code was just something to
check if an element is a member of a generator.
Is always possible to write
a in x()
but if it's not in it doesn't terminate, passing the right function
(in that case it was just increasing sequence) will do the trick..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnboundLocalError: local variable '_[1]' referenced before assignment

2009-12-09 Thread Dave Angel

Gabriel Rossetti wrote:
div class=moz-text-flowed style=font-family: -moz-fixedHello 
everyone,


I get this error on python 2.6.1 on mac os x 10.6 :

UnboundLocalError: local variable '_[1]' referenced before assignment

here's the code that raises this:

params = [ self.__formatData(paramProcFunc, query, p) for p in params ]

what I don't get is that it worked on mac os x 10.5 (python 2.5.x) but 
it no longer works. I tried the following and it works :


r = []
for p in params:
   r.append(self.__formatData(paramProcFunc, query, p))
params = r

Does anyone understand what is going on here?

Thank you,
Gabriel

/div

Clearly you're not supplying enough context.  The UnboundLocalError is 
only raised inside a function (or method), and you only show one line of 
that function.


And in the second example, it's even worse, since you imply it's 
top-level code by carefully unindenting everything.


My *guess* is that you have a global variable params, which you're 
looping on.  And then you assign a local variable by the same name.  If 
you have an assignment anywhere in the function, that name is considered 
a local, and if you reference it before you assign it, it'll generate 
this error.


Three possible fixes, depending on why you're doing this.

1) pass the params in to the function as an argument
2) use a different name for the local thing you're building
3) use a global statement to force the compiler to use the global one 
and not create a local.  (probably a bad idea)


DaveA

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


Re: UnboundLocalError: local variable '_[1]' referenced before assignment

2009-12-09 Thread Bruno Desthuilliers

Richard Thomas a écrit :

On Dec 9, 10:17 am, Gabriel Rossetti gabriel.rosse...@arimaz.com
wrote:


UnboundLocalError: local variable '_[1]' referenced before assignment



That isn't an error that should occur, not least because _[1] isn't a
valid name


It's an internal identifier used in list comps. Implementation detail, 
really, you're not even supposed to know about it...


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


no SPA in poplib?

2009-12-09 Thread Gabriel Rossetti

Hello,

I couldn't find SPA in poplib, does anyone know of an alternative 
implementation?


Thanks,
Gabriel

--
Arimaz SA
Ingénieur en Informatique
Av. du 24 Janvier 11
Ateliers de la Ville de Renens, Atelier 5
1020 Renens, Switzerland
www.arimaz.com
www.mydeskfriend.com
Mob: +41-(0)79-539-0069
Tel: +41-(0)21-566-7343

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


Re: UnboundLocalError: local variable '_[1]' referenced before assignment

2009-12-09 Thread Gabriel Rossetti

Dave Angel wrote:

Gabriel Rossetti wrote:
div class=moz-text-flowed style=font-family: -moz-fixedHello 
everyone,


I get this error on python 2.6.1 on mac os x 10.6 :

UnboundLocalError: local variable '_[1]' referenced before assignment

here's the code that raises this:

params = [ self.__formatData(paramProcFunc, query, p) for p in params ]

what I don't get is that it worked on mac os x 10.5 (python 2.5.x) 
but it no longer works. I tried the following and it works :


r = []
for p in params:
   r.append(self.__formatData(paramProcFunc, query, p))
params = r

Does anyone understand what is going on here?

Thank you,
Gabriel

/div

Clearly you're not supplying enough context.  The UnboundLocalError is 
only raised inside a function (or method), and you only show one line 
of that function.


And in the second example, it's even worse, since you imply it's 
top-level code by carefully unindenting everything.


My *guess* is that you have a global variable params, which you're 
looping on.  And then you assign a local variable by the same name.  
If you have an assignment anywhere in the function, that name is 
considered a local, and if you reference it before you assign it, 
it'll generate this error.


Three possible fixes, depending on why you're doing this.

1) pass the params in to the function as an argument
2) use a different name for the local thing you're building
3) use a global statement to force the compiler to use the global one 
and not create a local.  (probably a bad idea)


DaveA



Hello Dave,

ok, you' re right about not showing enough:

params = [ self.__formatData(paramProcFunc, query, p) for p in params ]


where :

paramProcFunc = percent2Volume

def __formatData(self, func, query, data):
   return getattr(self._unitDataAbs, func)(self._unitCmdAbs, query, 
data)


def percent2Volume(self, absCmds, query, percent):
   return query, int(round(percent / 100.0 * absCmds[volCeil]))


but I still don't see where the problem is and why it works with the 
explicit loop and not the list comp.


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


a huge shared read-only data in parallel accesses -- How? multithreading? multiprocessing?

2009-12-09 Thread Valery
Hi all,

Q: how to organize parallel accesses to a huge common read-only Python
data structure?

Details:

I have a huge data structure that takes 50% of RAM.
My goal is to have many computational threads (or processes) that can
have an efficient read-access to the huge and complex data structure.

Efficient in particular means without serialization and without
unneeded lockings on read-only data

To what I see, there are following strategies:

1. multi-processing
 = a. child-processes get their own *copies* of huge data structure
-- bad and not possible at all in my case;
 = b. child-processes often communicate with the parent process via
some IPC -- bad (serialization);
 = c. child-processes access the huge structure via some shared
memory approach -- feasible without serialization?! (copy-on-write is
not working here well in CPython/Linux!!);

2. multi-threading
 = d. CPython is told to have problems here because of GIL --  any
comments?
 = e. GIL-less implementations have their own issues -- any hot
recommendations?

I am a big fan of parallel map() approach -- either
multiprocessing.Pool.map or even better pprocess.pmap. However this
doesn't work straight-forward anymore, when huge data means 50%
RAM
;-)

Comments and ideas are highly welcome!!

Here is the workbench example of my case:

##
import time
from multiprocessing import Pool
def f(_):
time.sleep(5) # just to emulate the time used by my
computation
res = sum(parent_x) # my sofisticated formula goes here
return res

if __name__ == '__main__':
parent_x = [1./i for i in xrange(1,1000)]# my huge read-
only data :o)
p = Pool(7)
res= list(p.map(f, xrange(10)))
# switch to ps and see how fast your free memory is getting
wasted...
print res
##

Kind regards
Valery
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about 'remote objects'

2009-12-09 Thread J Kenneth King
Frank Millman fr...@chagford.com writes:

 Hi all

 I am writing a multi-user business/accounting application. It is getting 
 rather complex and I am looking at how to, not exactly simplify it, but find 
 a way to manage the complexity.

 I have realised that it is logically made up of a number of services -
 database service with connection to database
 workflow engine for business processes
 services manager to handle automated services, such as web services
 client manager to service logins from client workstations
 possibly others

 I have made a start by splitting some of these off into separate modules and 
 running them in their own threads.

 I am concerned about scalability if they are all running on the same 
 machine, so I am looking into how to enable these services to run on 
 separate servers if required.

Have you finished the application already?

At my job we're still serving just over 1M+ web requests (a month),
processing 15k+ uploads, and searching through over 5M+ database records
a day.  We're still running on 3 boxes.  You can get a lot out of your
machines before you have to think about the complex task of
scaling/distributing.


 My first thought was to look into Pyro. It seems quite nice. One concern I 
 had was that it creates a separate thread for each object made available by 
 the server. My database server creates separate objects for each instance of 
 a row read in from the database, and with multiple users running multiple 
 applications, with each one opening multiple tables, this could run into 
 hundreds, so I was not sure if that would work.

It probably will work.

Pyro is a very nice framework and one that I've built a few applications
on.  It has a lot of flexible design patterns available.  Just look in
the examples included with the distribution.


 Then I read that the multiprocessing module allows processes to be spread 
 across multiple servers. The documentation is not as clear as Pyro's, but it 
 looks as if it could do what I want. I assume it would use processes rather 
 than threads to make multiple objects available, but I don't know if there 
 is a practical limit.

There is a theoretical limit to all of the resources on a machine.
Threads don't live outside of that limit.  They just have a speedier
start-up time and are able to communicate with one another in a single
process.  It doesn't sound like that will buy you a whole lot in your
application.

You can spawn as many processes as you need.


 Then I thought that, instead of the database server exposing each object 
 remotely, I could create one 'proxy' object on the server through which all 
 clients would communicate, and it in turn would communicate with each 
 instance locally.

 That felt more managable, but then I thought - why bother with remote 
 objects at all? Why not just run a SocketServer on the database server, and 
 design a mini-protocol to allow clients to make requests and receive 
 results. This is a technology I am already comfortable with, as this is how 
 I handle client workstation logins. If I did go this route, I could apply 
 the same principle to all the services.

Because unless you wrote your own database or are using some arcane
relic, it should already have its own configurable socket interface?


 I don't have the experience to make an informed decision at this point, so I 
 thought I would see if there is any consensus on the best way to go from 
 here.

Finish building the application.

Do the benchmarks.  Profile.  Optimize.

Find the clear boundaries of each component.

Build an API along those boundaries.

Add a network layer in front of the boundaries.  Pyro is a good choice,
twisted is also good.  Roll your own if you think you can do better or
it would fit your projects' needs.

 Is there any particular benefit in using remote objects as opposed to 
 writing a SocketServer?

Abstraction.  Pyro is just an abstraction over an RPC mechanism.
Nothing special about it.  Twisted has libraries to do the same thing.
Writing your own socket-level code can be messy if you don't do it
right.


 Any advice will be much appreciated.

 Thanks

 Frank Millman

Best of luck.
-- 
http://mail.python.org/mailman/listinfo/python-list


plain text parsing to html (newbie problem)

2009-12-09 Thread João
I apologize for my newbiness but I'm banging my head making this work :
(
What change must I made for the tag enforcement being reflected to the
'mail' file? Am I using the WritableObject class correctly?
(I'm getting a blank 'mail' file after running the .py script)
How can I see the output run in debug mode like in perl?

#!/usr/bin/env python

import sys, os
import subprocess
import re
import datetime

# simple class with write method to redirect print output to a file
class WritableObject:
def __init__(self):
self.content = []
def write(self, string):
self.content.append(string)

x=datetime.date.today()
, dd = x.year, x.day

mail = WritableObject()
print mail, To: %s % (mail1)
print mail, Subject: %s day %s  % (, dd)
print mail, Content-Type: text/html; charset=\us-ascii\

subprocess.call(php $HOME/report.php  temp.txt, shell=True)
body = subprocess.call(cat $HOME/.txt, shell=True)

print mail, 
print mail, html
print mail, body

#copies common header in the Problem html to the OK one
subprocess.call(cp $HOME/mail $HOME/OK.html, shell=True)

input_file = open('mail', 'r+')
lines = input_file.readlines()
input_file.close()
output_file = open(mail, w+)

for line in lines:
if line.startswith('p'):
line = 'p' + line.rstrip('\n') + '/p' + '\n'
output_file.write(line)

if not body:
print mail, '''/p b[No problems detected ].b / All monitored
metrics
pfont size=\4\
font color=\darkgreen\
bOKb
/p
/font
/body
/html'''
else:
print mail, '''
table border=\1\ tr
th Table/th
th Table2/th
th Table3/th
/tr
/table
%s
/body
/html''' % (body)

subprocess.call(sendmail m...@my.mail  mail, shell=True)
output_file.close()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnboundLocalError: local variable '_[1]' referenced before assignment

2009-12-09 Thread Hrvoje Niksic
Richard Thomas chards...@gmail.com writes:

 That isn't an error that should occur, not least because _[1] isn't a
 valid name. Can you post a full traceback?

The name _[n] is used internally when compiling list comprehensions.
The name is chosen precisely because it is not an (otherwise) valid
identifier.  For example, try:

import dis
dis.dis(lambda: [ a for a in l ])

The user should never see _[n]; if he does, it's probably due to a bug
in Python.
-- 
http://mail.python.org/mailman/listinfo/python-list


tkinter photoimage, couldn't recognize image data (PPM)

2009-12-09 Thread Martin P. Hellwig

Hi all,

I've tried to display an image with the source being a string but it 
fails (see below). Is there a way to display PPM without writing it 
first to a file?


Thanks,

Martin

- snippet -
'''
Ubuntu 9.04 64bit, python 3.1
'''
import tkinter

DATA=P3
3 2
255
255   0   0 0 255   0 0   0 255
255 255   0   255 255 255 0   0   0

def display():
tk = tkinter.Tk()
canvas = tkinter.Canvas(tk, width=3, height=2)
canvas._image_reference = tkinter.PhotoImage(format='ppm', data=DATA)
canvas.create_image((0,0), image=canvas._image_reference)
canvas.pack()
tk.after(1500, tk.quit)
tk.mainloop()

if __name__ == '__main__':
display()
-

- traceback -
Traceback (most recent call last):
  File /home/martin/DCUK 
Technologies/workspace/mhellwig/src/test/tkintering.py

, line 24, in module
display()
  File /home/martin/DCUK 
Technologies/workspace/mhellwig/src/test/tkintering.py

, line 17, in display
canvas._image_reference = tkinter.PhotoImage(format='ppm', data=DATA)
  File /usr/local/lib/python3.1/tkinter/__init__.py, line 3269, in 
__init__

Image.__init__(self, 'photo', name, cnf, master, **kw)
  File /usr/local/lib/python3.1/tkinter/__init__.py, line 3225, in 
__init__

self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize image data
-

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


More stuff added to ch 2 of my programming intro

2009-12-09 Thread Alf P. Steinbach

  Format: PDF
  url: http://preview.tinyurl.com/ProgrammingBookP3

The new stuff, section 2.7, is about programs as simulations and handling data, 
focusing on modeling things. It includes some Python GUI programming. The plan 
is to discuss containers like lists and dictionaries in perhaps two more 
subsections of 2.7, but I'm not quite sure about how to approach that or exactly 
how much to cover, since the intent of ch 2 is to introduce mostly general 
concepts and enable the reader to try out (more or less) interesting things.



Cheers,

- Alf

PS: comments welcome!
--
http://mail.python.org/mailman/listinfo/python-list


How do I Block Events in wxPython

2009-12-09 Thread Wanderer
I have a wxPython program which does some calculations and displays
the results. During these calculations if I click the mouse inside the
dialog the program locks up. If I leave the dialog alone the process
completes fine. I have tried running the function from a separate
dialog with Show Modal and I have tried using SetEvtHandlerEnabled all
to no avail. The program is long and occupies several files so I won't
show the whole thing but here is the calculation part. How do I block
events?

def DoEfficiency(self):

  from time import sleep

  self.timer.Stop()



  TotalPower = 0
  Counter = 0
  for ang in range(1,89):
self.tc_angle.SetValue(ang)
height = int(self.eclwidth * 10)
for hgt in range(0,height):
  self.tc_height.SetValue(float(hgt)/10.0)
  self.UpdateValues()
  self.Redraw()
  self.DrawRays()
  sPower = self.tc_power.GetValue()
  Power = sPower.split('%')
  TotalPower +=float(Power[0])
  Counter +=1
  sleep(0.1)

  efficiency = TotalPower/Counter
  self.tc_eff.SetLabel(str(round(efficiency,1)))
  self.timer.Start(10)


# end DoEfficiency

def OnEfficiency(self, event):

  self.tc_aangle.SetEvtHandlerEnabled(False)
  self.tc_angle.SetEvtHandlerEnabled(False)
  self.tc_calc_len.SetEvtHandlerEnabled(False)
  self.tc_cpclength.SetEvtHandlerEnabled(False)
  self.tc_cpcwidth.SetEvtHandlerEnabled(False)
  self.tc_det.SetEvtHandlerEnabled(False)
  self.tc_ecl.SetEvtHandlerEnabled(False)
  self.tc_eff.SetEvtHandlerEnabled(False)
  self.tc_gap1.SetEvtHandlerEnabled(False)
  self.tc_gap2.SetEvtHandlerEnabled(False)
  self.tc_height.SetEvtHandlerEnabled(False)
  self.tc_parab.SetEvtHandlerEnabled(False)
  self.tc_power.SetEvtHandlerEnabled(False)
  self.tc_refresh.SetEvtHandlerEnabled(False)
  self.tc_tlength.SetEvtHandlerEnabled(False)
  self.tc_twidth.SetEvtHandlerEnabled(False)
  self.tc_use_tir.SetEvtHandlerEnabled(False)
  self.SetEvtHandlerEnabled(False)

  dlf = CalcEfficiency(self,CalcEfficiency,wx.DefaultPosition,
(90,60))

  self.tc_aangle.SetEvtHandlerEnabled(True)
  self.tc_angle.SetEvtHandlerEnabled(True)
  self.tc_calc_len.SetEvtHandlerEnabled(True)
  self.tc_cpclength.SetEvtHandlerEnabled(True)
  self.tc_cpcwidth.SetEvtHandlerEnabled(True)
  self.tc_det.SetEvtHandlerEnabled(True)
  self.tc_ecl.SetEvtHandlerEnabled(True)
  self.tc_eff.SetEvtHandlerEnabled(True)
  self.tc_gap1.SetEvtHandlerEnabled(True)
  self.tc_gap2.SetEvtHandlerEnabled(True)
  self.tc_height.SetEvtHandlerEnabled(True)
  self.tc_parab.SetEvtHandlerEnabled(True)
  self.tc_power.SetEvtHandlerEnabled(True)
  self.tc_refresh.SetEvtHandlerEnabled(True)
  self.tc_tlength.SetEvtHandlerEnabled(True)
  self.tc_twidth.SetEvtHandlerEnabled(True)
  self.tc_use_tir.SetEvtHandlerEnabled(True)
  self.SetEvtHandlerEnabled(True)

# end MyInterface


class CalcEfficiency(wx.Dialog):


def __init__(self, parent, title, pos, size):

self.parent = parent


wx.Dialog.__init__(self,parent, -1, title, pos, size)

self.runButton = wx.ToggleButton(self, ID_TOGGLE, Start,
wx.DefaultPosition, (70,30))
self.Bind(wx.EVT_TOGGLEBUTTON, self.OnToggle, id =
ID_TOGGLE)

self.ShowModal()

self.Destroy()

# end init

def OnToggle(self, event):

  if self.runButton.GetValue():
self.runButton.SetLabel(WAIT)
self.SetEvtHandlerEnabled(False)
self.parent.DoEfficiency()
self.SetEvtHandlerEnabled(True)
  else:
self.Close()


# end OnQuit

# end CalcEfficiency


if __name__ == '__main__':
app = MyApp(False)
app.MainLoop()

# end main
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: plain text parsing to html (newbie problem)

2009-12-09 Thread MRAB

João wrote:

I apologize for my newbiness but I'm banging my head making this work :
(
What change must I made for the tag enforcement being reflected to the
'mail' file? Am I using the WritableObject class correctly?
(I'm getting a blank 'mail' file after running the .py script)
How can I see the output run in debug mode like in perl?


[snip]

You're storing the text in the 'mail' object (an instance of
WritableObject), but at no point do you use what you have stored.

I can also see that you're writing to a file call mail via
output_file, but you're not closing the file before passing it to
sendmail.

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


Re: Question about 'remote objects'

2009-12-09 Thread Diez B. Roggisch
 I am writing a multi-user business/accounting application. It is getting
 rather complex and I am looking at how to, not exactly simplify it, but
 find a way to manage the complexity.
 
 I have realised that it is logically made up of a number of services -
 database service with connection to database
 workflow engine for business processes
 services manager to handle automated services, such as web services
 client manager to service logins from client workstations
 possibly others
 
 I have made a start by splitting some of these off into separate modules
 and running them in their own threads.

I wouldn't do that. Creating threads (or processes) with potentially
interacting components ramps up complexity a great deal, with little if any
benefit at your current stage, and only a vague possibility that scaling
issues appear and can be remedied by that.

Instead, use threading or multi-processing to create various instances of
your application that synchronize only over the DB, using locks where it is
needed.

Introducing RPC of whatever kind to your design will make you lose a lot of
the power and flexibility code-wise, because all of a sudden you can only
pass data, not behavior around.

And as J Kenneth already said, deal with performance issues when the crop
up. 

At work, we had a design with a whole bunch of separated XMLRPC-connected
services, all of them restricting their access to only certain sub-schemas
of the DB. This was done so that we could run them on separate servers if
we wanted, with several databases. The creators of that system had the same
fears as you.

Guess what? Most of the time the system spend in serializing and
de-serializing XML for making RPC-calls. We had no referential integrity
between schemas, and no single transactions around HTTP-requests, which
didn't exactly make crap out of our data, but the occasional hickup was in
there. And through the limited RCP-interface, a great deal of code
consisted of passing around dicts, lists and strings - with no rich
OO-interface of whatever kind.

Once we got rid of these premature optimizations, the system improved in
performance, and the code-base was open to a *lot* of cleaning up that is
still under way, but already massively improved the design.

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


Re: Request for py program to insert space between two characters and saved as text?

2009-12-09 Thread r0g
Dave Angel wrote:
 
 
 r0g wrote:
 Dave Angel wrote:
  
 r0g wrote:

 Dave Angel wrote:
  
  
 r0g wrote:
   
 Dennis Lee Bieber wrote:
  
   
 On Tue, 8 Dec 2009 08:26:58 +0530, 74yrs old
 withblessi...@gmail.com
 declaimed the following in gmane.comp.python.general:

  
 For Kannada project .txt(not .doc) is used, my requirement is to
 have one
   
 big snip
  
 
 That's even worse.  As far as I can tell, the code will never do what he
 wants in Python 2.x.   The Kannada text file is full of Unicode
 characters in some encoding, and if you ignore the encoding, you'll just
 get garbage.


 

 Ah, fair enough. In my defence though I never saw the original post or
 this kannada.txt file as my newsserver is not so much with the
 reliability. I guess it's naive to assume an english .txt file is going
 to be in ASCII these days eh?

 I've yet to try python 3 yet either, this whole Unicode thing looks like
 it could be a total nightmare! :(

 Roger.

   
 But it isn't an english  .txt file, it's a Kannada  .txt file.  
 Presumably you didn't realize that Kannada is a (non-English) language,
 spoken in parts of India, with several hundred characters.  ASCII wasn't
 even an option.  Anyway, no harm done, someone else referred the OP to a
 Python user-group local to that region.
 
 DaveA
 

Well this looked like English to me...

example: *F o r  K a n n a d a  p r o j e c t . t x t(n o t .d o c)  i s
 u s e d,  m y  r e q u i r e m e n t   i s  t o  h a v e  o n e  s p a
c e  b e t w e e n  t w o  c h a r a c t e r s  i n  t h e  t e x t.*

...but yes you're right, I had never heard of Kannada let alone knew it
was another language!

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


Re: How do I Block Events in wxPython

2009-12-09 Thread Philip Semanchuk


On Dec 9, 2009, at 10:42 AM, Wanderer wrote:


I have a wxPython program which does some calculations and displays
the results. During these calculations if I click the mouse inside the
dialog the program locks up. If I leave the dialog alone the process
completes fine. I have tried running the function from a separate
dialog with Show Modal and I have tried using SetEvtHandlerEnabled all
to no avail. The program is long and occupies several files so I won't
show the whole thing but here is the calculation part. How do I block
events?



Hi Wanderer,
I don't have a solution for you but I have three suggestions.

First, your program shouldn't be locking up just because you clicked  
on it. IMO that's the real problem, and discarding events is just a  
band-aid to cover it up. Nevertheless, sometimes a band-aid is an  
appropriate solution and you're the best judge of that.


Second, the wxPython mailing list would be a better place for this  
question.


Third, if you can't seem to resolve the problem, try paring it down to  
a minimal example that reproduces the problem. It's difficult to offer  
suggestions when we can't see the whole code or try the sample code  
ourselves.



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


Re: How do I Block Events in wxPython

2009-12-09 Thread zeph
The wxPython wiki actually has a page on dealing with long running
tasks called from event handlers called (surprise surprise):
http://wiki.wxpython.org/LongRunningTasks

Hint: the second to last example on that page has the clearest example
- using a worker thread object to do your DoEfficiency() function.

I also don't think you want to disable so many event handlers, do
you?  Nothing will respond to inputs as long as that process is
running (assuming you aren't running it in another thread.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I Block Events in wxPython

2009-12-09 Thread r0g
Wanderer wrote:
 I have a wxPython program which does some calculations and displays
 the results. During these calculations if I click the mouse inside the
 dialog the program locks up. If I leave the dialog alone the process
 completes fine.

If anything in your GUI app takes a non trivial length of time to
execute you need to run it in either a thread or a separate process.

http://linuxgazette.net/107/pai.html

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


Re: UnboundLocalError: local variable '_[1]' referenced before assignment

2009-12-09 Thread Terry Reedy

Gabriel Rossetti wrote:

Gabriel Rossetti wrote:



I get this error on python 2.6.1 on mac os x 10.6 :

UnboundLocalError: local variable '_[1]' referenced before assignment

here's the code that raises this:

params = [ self.__formatData(paramProcFunc, query, p) for p in params ]

what I don't get is that it worked on mac os x 10.5 (python 2.5.x) 
but it no longer works.


In 3.0, list comps were changed to not 'leak' the iteration variables.
To do this, they are compiled to a function run in a separate namespace.
It appears the new code was backported to 2.6 with an extra fix to 
continue leaking the iteration variable for back compatibility. I 
presume there is or was intended to be a switch to to get 3.x behavior 
for code being ported.


As others have said, the error is from that internal function, and 
should never appear. Hence, you may have found a corner-case bug that 
otherwise passed the current tests.


Suggestions.
1) Run with 3.1 and see if you get the same error. In not, problem is in 
difference between 3.1 and 2.6 listcomp code. Report to tracker.
2) Simplify the code and see if you still get the error. I would try 
removing the '__' from the method name, or using a data attribute 
instead of a method, or a method with no parameters.


Unless someone here otherwise pins the error on you, report on the tracker.

Terry Jan Reedy


I tried the following and it works :


r = []
for p in params:
   r.append(self.__formatData(paramProcFunc, query, p))
params = r


In 2.5, list comps were rewritten like this.



Does anyone understand what is going on here?



params = [ self.__formatData(paramProcFunc, query, p) for p in params ]


where :

paramProcFunc = percent2Volume

def __formatData(self, func, query, data):
   return getattr(self._unitDataAbs, func)(self._unitCmdAbs, query, 
data)


def percent2Volume(self, absCmds, query, percent):
   return query, int(round(percent / 100.0 * absCmds[volCeil]))


but I still don't see where the problem is and why it works with the 
explicit loop and not the list comp.


Thanks,
Gabriel


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


Re: plain text parsing to html (newbie problem)

2009-12-09 Thread Terry Reedy

João wrote:

I apologize for my newbiness but I'm banging my head making this work :
(
What change must I made for the tag enforcement being reflected to the
'mail' file? Am I using the WritableObject class correctly?
(I'm getting a blank 'mail' file after running the .py script)
How can I see the output run in debug mode like in perl?

#!/usr/bin/env python

import sys, os
import subprocess
import re
import datetime

# simple class with write method to redirect print output to a file
class WritableObject:
def __init__(self):
self.content = []
def write(self, string):
self.content.append(string)


Python comes with a stringio class that does this *AND* has a method to 
retrieve the result. StringIO in 2.x, io module in 3.x


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


Re: How do I Block Events in wxPython

2009-12-09 Thread Wanderer
On Dec 9, 11:48 am, r0g aioe@technicalbloke.com wrote:
 Wanderer wrote:
  I have a wxPython program which does some calculations and displays
  the results. During these calculations if I click the mouse inside the
  dialog the program locks up. If I leave the dialog alone the process
  completes fine.

 If anything in your GUI app takes a non trivial length of time to
 execute you need to run it in either a thread or a separate process.

 http://linuxgazette.net/107/pai.html

 Roger

Thanks Everyone. I'll have to look over these wikis about threading. I
decided to go another route and user a timer to perform the loops.
That way the events can be processed normally.

def DoEfficiency(self, event):


  ang = self.tc_angle.GetValue()
  hgt = self.tc_height.GetValue()
  hgt += 0.1
  if hgt  self.eclwidth:
hgt = 0
ang +=1
self.tc_angle.SetValue(ang)
  self.height = hgt
  self.tc_height.SetValue(hgt)
  self.tc_height.SetValue(hgt)
  self.UpdateValues()
  self.Redraw()
  self.DrawRays()
  sPower = self.tc_power.GetValue()
  Power = sPower.split('%')
  self.TotalPower +=float(Power[0])
  self.Counter +=1
  efficiency = self.TotalPower/self.Counter
  self.tc_eff.SetLabel(str(round(efficiency,1)))
  if ang  89:
self.efftimer.Stop()
self.timer.Start(10)



# end DoEfficiency

def OnEfficiency(self, event):

self.TotalPower = 0
self.Counter = 0
self.tc_angle.SetValue(1)
self.tc_height.SetValue(0)
self.timer.Stop()
self.efftimer.Start(100)


# end MyInterface

Found another strange bug (Strange to me, anyway). int(0.8 * 10.0) =
7. Had to change the code to int(0.8 * 10.0 + 0.0001).

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


ANN: WHIFF.0.7 += GAE + jQueryUI + internationalization + testdrive = (last beta?)

2009-12-09 Thread Aaron Watters
ANNOUNCING WHIFF [WSGI HTTP Integrated Filesystem Frames] release 0.7

   WHIFF INDEX PAGE: http://whiff.sourceforge.net

The new release adds many new features, including

- Google app engine support with tutorial:
 http://whiffdoc.appspot.com/docs/W1100_2300.GAEDeploy
- jQueryUI interactive widget support with tutorial:
 http://whiffdoc.appspot.com/docs/W1100_1450.jQueryUI
- Internationalization tutorial:
 http://whiffdoc.appspot.com/docs/W1100_1700.international
- no install test drive:
 http://whiffdoc.appspot.com/docs/W1100_0500.TestDrive

Also the WHIFF documentation is now hosted on Google App
Engine at the http://whiffdoc.appspot.com/ domain.

What is WHIFF?

WHIFF is a collection of support services for
WSGI/Python web applications which allows applications
to be composed by dropping dynamic pages into
container directories. This mode of development will
be familiar to developers who have created PHP applications,
vanilla CGI scripts, Apache/modpy Publisher applications,
JSP pages, or static web content.

The WHIFF implementation significantly generalizes
the drop in paradigm to support WSGI middleware
components and application fragments as well as
stand-alone pages.

WHIFF provides other services in addition to
supporting drop in components, such as managed
application resources.

WHIFF requires Python 2.5 or better to run out-of-the-box.

Unless significant problems appear in the next
month or so I think this release will be the last beta
release and the next release will add a couple
more features to become WHIFF 1.0.

For more fun please have a look at my community
list/tree maker at http://listtree.appspot.com
which is inspired by Guido van Rossum's excellent
faqwiz.  ListTree is implemented on Google App
Engine using WHIFF.

I hope you like.
-- Aaron Watters

===
Talk low.
Talk slow.
And don't say a lot.
  -- John Wayne's advice to Clint Eastwood
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I Block Events in wxPython

2009-12-09 Thread Jeff Peck

Philip Semanchuk wrote:


On Dec 9, 2009, at 10:42 AM, Wanderer wrote:


I have a wxPython program which does some calculations and displays
the results. During these calculations if I click the mouse inside the
dialog the program locks up. If I leave the dialog alone the process
completes fine. I have tried running the function from a separate
dialog with Show Modal and I have tried using SetEvtHandlerEnabled all
to no avail. The program is long and occupies several files so I won't
show the whole thing but here is the calculation part. How do I block
events?



Hi Wanderer,
I don't have a solution for you but I have three suggestions.

First, your program shouldn't be locking up just because you clicked 
on it. IMO that's the real problem, and discarding events is just a 
band-aid to cover it up. Nevertheless, sometimes a band-aid is an 
appropriate solution and you're the best judge of that.


Second, the wxPython mailing list would be a better place for this 
question.


Third, if you can't seem to resolve the problem, try paring it down to 
a minimal example that reproduces the problem. It's difficult to offer 
suggestions when we can't see the whole code or try the sample code 
ourselves.



Good luck
Philip

Wanderer,
  I agree with Philip. You probably want your calculation code in a 
separate thread. I'd advise against this, but if you're just looking for 
a bandaid you could try creating an event handler to catch the mouse 
clicks and simply call event.Skip(). If you do this, you might have to 
introduce a flag that gets set to True only during your calculation, and 
then your event hander could look something like this:


def OnMouseClick(self, event):
   # Only skip mouse click event if calculating
   if self.busy:
   event.Skip()


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


Re: subprocess kill

2009-12-09 Thread Nobody
On Mon, 07 Dec 2009 11:04:06 +0100, Jean-Michel Pichavant wrote:

 When using shell=True, your process is started in a shell, meaning the 
 PID of your subprocess is not self.luca.pid, self.luca.pid is the PID of 
 the shell.

This isn't true for a simple command on Unix (meaning a program name plus
arguments, and redirections, rather than e.g. a pipeline or a command
using subshells, flow-control constructs, etc).

For a simple command, /bin/sh -c 'prog arg arg ...' will exec() the
program *without* fork()ing, so the program will take over the shell's
process and PID.

You can verify this by running e.g.:

import subprocess
p = subprocess.Popen('sleep 10', shell=True)
print p.pid
subprocess.call('ps')
p.wait()


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


Re: xmlrpc idea for getting around the GIL

2009-12-09 Thread Patrick Stinson
On Wed, Dec 2, 2009 at 7:42 AM, sturlamolden sturlamol...@yahoo.no wrote:
 On 2 Des, 02:47, Patrick Stinson patrickstinson.li...@gmail.com
 wrote:

 We don't need extension modules, and all we need to do is run some
 fairly basic scripts that make callbacks and use some sip-wrapped
 types.

 Sure, you use SIP but not extension modules...


 - Python is not suitable for real-time work.

 Not true. We have been running python callback code using
 PyObject_CallObject from within our audio thread for some time without
 a problem, and it's *extremely* fast.

 It seems you are confusing real-time with real-fast. The fact that
 something runs fast does not make it real-time.

 Python is not suitable for real-time applications, nor are the OSes
 commonly used to run Python.


Semantics aside, my point is that it runs well enough in our
environment. If the audio is smooth, it is considered working.



 We
 need just a ltle push to get our code to work at low latencies,
 and the only thing that is standing in our way is that all threads
 9usually around 20 have to block on the Gil, and this causes small
 gaps in the sound at low latencies (around 5ms, or 64 sample period).

 ...almost perfect.

 Python is not programmed with real-time applications in mind: You have
 no guarrantees on maximum time-lag when a thread is blocked on the
 GIL.

We don't need theoretical guarantees, because we've tried it and it
works. That's the bottom line


 Priority requests (i.e. pre-emptive multitasking) was removed from
 Antoine's newgil branch, but that is the kind of mechanism you would
 need. Even with priority requests, Python would not be suitable for
 real-time apps, as extension modules (e.g. C++ wrapped with SIP) can
 hold the GIL forever.

see above.


 You will also need an OS with a real-time scheduler and a real-time C
 library, such as QNX or VxWorks.

 I find thea idea of a true real-time Python very interesting, but it
 would take a completely reworked interpreter.






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

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


Sum of the factorial of the digits of a number - wierd behaviour

2009-12-09 Thread SiWi
Dear python community,
I've got a wierd problem and I hope you can help me out at it.
I wrote the following code to find the Sum of the factorial of the
digits of a number (this is for Project Euler 74):

def fac(n):
x=1
for i in range(2,n+1):
x*=i
return x

t=tuple(fac(n) for n in range(1,10))

def decimals(x):
i=1
d=[]
while x0:
d.append(x%10)
x=x/10
return d

def sumfac(x):
return sum(t[n-1] for n in decimals(x))

The problem is that i get the following results, for which I can't see
any reason:
sumfac(145)-145 (1!+4!+5!=1 + 24 +120 = 145) - ok
sumfac(1454)- 169 - ok
sumfac(45362) - 872 - ok
sumfac(363600) - 727212 - wrong, should be1454

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


Re: Sum of the factorial of the digits of a number - wierd behaviour

2009-12-09 Thread SiWi
On Dec 9, 6:36 pm, SiWi wimmersi...@googlemail.com wrote:
 Dear python community,
 I've got a wierd problem and I hope you can help me out at it.
 I wrote the following code to find the Sum of the factorial of the
 digits of a number (this is for Project Euler 74):

 def fac(n):
     x=1
     for i in range(2,n+1):
         x*=i
     return x

 t=tuple(fac(n) for n in range(1,10))

 def decimals(x):
     i=1
     d=[]
     while x0:
         d.append(x%10)
         x=x/10
     return d

 def sumfac(x):
     return sum(t[n-1] for n in decimals(x))

 The problem is that i get the following results, for which I can't see
 any reason:
 sumfac(145)-145 (1!+4!+5!=1 + 24 +120 = 145) - ok
 sumfac(1454)- 169 - ok
 sumfac(45362) - 872 - ok
 sumfac(363600) - 727212 - wrong, should be1454

 Greetings,
 SiWi.

Oops, found it myself. You can ignote the message above.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I Block Events in wxPython

2009-12-09 Thread Stephen Hansen
On Wed, Dec 9, 2009 at 9:06 AM, Wanderer wande...@dialup4less.com wrote:

 Found another strange bug (Strange to me, anyway). int(0.8 * 10.0) =
 7. Had to change the code to int(0.8 * 10.0 + 0.0001).


http://effbot.org/pyfaq/why-are-floating-point-calculations-so-inaccurate.htm

Floating point math is not precise; if you need precision, use the decimal
module. Alternately, you can just be sure to round() your floats to whatever
precision you need to consider significant after calculations.

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


Re: How decoupled are the Python frameworks?

2009-12-09 Thread mdipierro
Interesting post. I would like to make some comments about design
decisions that went into web2py:

- For each app Model/View/Controllers/Language Files/Static Files/
Modules/Cron Tasks are stored in separated folders
- You can code only the models (no controllers and no view) and you
get a fully functional admin interface
- You can develop only controllers (with or without models but no
views) and you get a fully working application with workflow
- There is a convention for dispatching. You can override it with
routes.
- There is no metadata in the framework. URLs are mapped into an app
(within the web2py instance), into a controller file, and into a
function (action) in that controller file.
- You can have multiple apps within a web2py instance (they can be
installed, uninstalled, packaged, compiled without restarting web2py)
- You can have multiple model files, multiple controllers and multiple
views. You can override the mapping between controllers and default
views.
- You can group files (models/controllers/views/static files)
functionally into plugins. Plugins can be packaged separately from the
app and applied to multiple apps.
- Plugins expose components (i.e. reusable objects that can be
embedded in a page and talk to their own controllers via ajax).
- Plugin components are coded as any other web2py models/controller/
view but the form submission is automatically trapped (transparently
to the user) and executed via ajax so that, if the component contains
a form only the component is reloaded upon submissions of the form.
- web2py supports FORM, SQLFORM, SQLFORM.factory and CRUD for
automtical generation of forms (from a model or other structure). All
web2py forms execute postbacks and modify themselves to report error
messages. A form is comprised of widgets that contain validators.
There is a default layout but it can be customized in the view by
allocating widgets or individual html tags.
- You can put doctests in actions and we provide a web interface for
testing the app online.
- It completely abstracts the database backend (we support 10
different database backends including Google App Engine) thus make the
code very portable.
- It authomatically writes sql for queries, for create table and alter
table.
-Web2py provides a Role Based Access Control mechanism with plugguble
login components so that you can authenticate using multiple mechanism
including Gmail and Twitter for example.

Specifically about you concerns:- better scalability
- easy to test = web2py is very easy to test because of the web based
interface to doctests
- easy to maintain = In web2py Do not repeat yourself trumps
explicit if better than implicit. This means code is very compact.
- easy to re-use code for different applications = using plugins
- easy to migrate/port = because of DAL

Massimo

On Dec 7, 4:38 pm, shocks benmari...@googlemail.com wrote:
 Hi

 I'm getting back into Python after a long break.  I've been developing
 large enterprise apps solely with Adobe Flex (ActionScript) for the
 past couple years.  During that time I've used a number of 'MVC'
 frameworks to glue the bits together - among them Cairngorm, a
 modified implementation of Cairngorm using the Presentation Model
 pattern, PureMVC, Mate (an IOC container but with an MVC
 implementation) and Parsley (IOC but you have to roll-you-own MVC).
 During that time I've been in large teams (30 Flex + 30 Java) to small
 teams (2 Flex + 1 Java).  The motivation of these frameworks is the
 decouple your concerns, allowing your apps to be more scalable, easier
 to test, and  supposedly easier to maintain.  Some do the decoupling
 better job than others, but there is also the question of how
 decoupled is your code from the framework?  It's all well and good
 having something clever working behind the scenes wiring and routing
 everything together, but I wonder where this leaves the code base if
 the framework, which was selected at the beginning of the project, is
 replaced with something else months or years later (i.e. the framework
 just doesn't scale as expected, the community involvement dies and
 it's no longer maintained properly, etc).  I've seen it happen and
 I've been experienced the pain of detangling massive amounts of code
 which is full of framework specific imports, methods and boilerplate
 code.  And then there's updating the unit tests!

 My question is how good are the current crop of Python frameworks?
 I've used Django twice in production and didn't like that much.  The
 implementation is Django specific for starters.  I've picked up Pylons
 and I'm trying that out.  I'm not sure how well it fares?  I do feel a
 bit uneasy about the code generation that some of the Python
 frameworks do.  Pylons creates something like 20 files for a
 'helloworld'.  It does do some great things out of the box, but I
 wonder where that leaves your own code.  After spending 3-6 months on
 your Pylons webapp, how easy is it to move to something else?  Maybe
 

Re: tkinter photoimage, couldn't recognize image data (PPM)

2009-12-09 Thread Terry Reedy

Martin P. Hellwig wrote:

Hi all,

I've tried to display an image with the source being a string but it 
fails (see below). Is there a way to display PPM without writing it 
first to a file?


Thanks,

Martin

- snippet -
'''
Ubuntu 9.04 64bit, python 3.1
'''
import tkinter

DATA=P3
3 2
255
255   0   0 0 255   0 0   0 255
255 255   0   255 255 255 0   0   0


Should the string really have the newlines? Or should this be
DATA=P3\
3 2\
255\
255   0   0 0 255   0 0   0 255\
255 255   0   255 255 255 0   0   0


def display():
tk = tkinter.Tk()
canvas = tkinter.Canvas(tk, width=3, height=2)
canvas._image_reference = tkinter.PhotoImage(format='ppm', data=DATA)
canvas.create_image((0,0), image=canvas._image_reference)
canvas.pack()
tk.after(1500, tk.quit)
tk.mainloop()

if __name__ == '__main__':
display()
-

- traceback -
Traceback (most recent call last):
  File /home/martin/DCUK 
Technologies/workspace/mhellwig/src/test/tkintering.py

, line 24, in module
display()
  File /home/martin/DCUK 
Technologies/workspace/mhellwig/src/test/tkintering.py

, line 17, in display
canvas._image_reference = tkinter.PhotoImage(format='ppm', data=DATA)
  File /usr/local/lib/python3.1/tkinter/__init__.py, line 3269, in 
__init__

Image.__init__(self, 'photo', name, cnf, master, **kw)
  File /usr/local/lib/python3.1/tkinter/__init__.py, line 3225, in 
__init__

self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize image data
-



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


Re: When will Python 3 be fully deployed

2009-12-09 Thread Nobody
On Sun, 06 Dec 2009 22:10:15 +, Edward A. Falk wrote:

I recently read that many libraries, including Numpy have not been
ported to Python 3.

When do you think that Python 3 will be fully deployed?
 
 It will never be fully deployed.  There will always be people out there who
 haven't felt it necessary to upgrade their systems.

Moreover, there will always be people out there who have felt it necessary
not to upgrade their systems.

IMNSHO, Python 2 will still be alive when Python 4 is released. If
python.org doesn't want to maintain it, ActiveState will.

In particular: for Unix scripting, Python 3's Unicode obsession just gets
in the way. Ultimately, argv, environ, filenames, etc really are just byte
strings. Converting to Unicode just means that the first thing that the
script does is to convert back to bytes.

I'm sure that the Unicode approach works great on Windows, where wchar_t
is so pervasive that Microsoft may as well have just redefined char
(even to the point of preferring UTF-16-LE for text files over UTF-8,
ASCII-compatibility be damned).

But on Unix, it's a square-peg-round-hole situation.

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


Re: switch

2009-12-09 Thread Matt McCredie
hong zhang henryzhang62 at yahoo.com writes:

 
 List,
 
 Python does not have switch statement. Any other option does similar work?
 Thanks for help.
 
 --henry
 
   

I see a couple of people have mentioned using a dictionary. If the value that 
you are switching on is a string, or could be made into one, you can use a 
variant of the command dispatch pattern.


class MyCommandDispatcher(object):
def do_a(self):
  # do stuff

def do_b(self):
  # do stuff

def do_5(self):
  # do stuff

def default(self):
  # do stuff

def switch(self, option):
getattr(self, 'do_' + str(option), self.default)()


d = MyCommandDispatcher()
d.switch('a')
d.switch(5)


This isn't _much_ more coding than using the dictionary method, and is pretty 
readable. This is also a common pattern in python.


Matt



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


Re: When will Python 3 be fully deployed

2009-12-09 Thread Rami Chowdhury
On Wed, Dec 9, 2009 at 09:53, Nobody nob...@nowhere.com wrote:

 I'm sure that the Unicode approach works great on Windows, where wchar_t
 is so pervasive that Microsoft may as well have just redefined char
 (even to the point of preferring UTF-16-LE for text files over UTF-8,
 ASCII-compatibility be damned).

 But on Unix, it's a square-peg-round-hole situation.

I dunno, I find it rather useful not to have to faff about with
encoding to/from when working with non-ASCII files (with non-ASCII
filenames) on Linux.


Rami Chowdhury
Never assume malice when stupidity will suffice. -- Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)




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

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


Re: tkinter photoimage, couldn't recognize image data (PPM)

2009-12-09 Thread Martin P. Hellwig

Terry Reedy wrote:
cut

DATA=P3
3 2
255
255   0   0 0 255   0 0   0 255
255 255   0   255 255 255 0   0   0


Should the string really have the newlines? Or should this be
DATA=P3\
3 2\
255\
255   0   0 0 255   0 0   0 255\
255 255   0   255 255 255 0   0   0



I've tried it, still same error, I did had a look at 
http://netpbm.sourceforge.net/doc/ppm.html and the wikipedia page.


cut

self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize image data
-






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


Re: ANN: WHIFF.0.7 += GAE + jQueryUI + internationalization + testdrive = (last beta?)

2009-12-09 Thread Terry Reedy

Aaron Watters wrote:


Also the WHIFF documentation is now hosted on Google App
Engine at the http://whiffdoc.appspot.com/ domain.


When I went there and clicked on the scatter chart is generated by a 
straightforward invocation of the standard WHIFF OpenFlashChart 
middleware: , Firefox *immediately* opened the 'Official' page of a 
well-known ugly cult www.sci-logy.org in a new tab. Something is 
very, very wrong. I really hope this was not intentional on your part.


When I load the doc page again and click the chart box again, the chart 
is reloaded from the local cache instead of your site and no tab is opened.


tjr

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


Re: switch

2009-12-09 Thread Nobody
On Tue, 08 Dec 2009 21:02:44 -0800, Kee Nethery wrote:

 I string together a bunch of elif statements to simulate a switch
 
 if foo == True:
   blah
 elif bar == True:
   blah blah
 elif bar == False:
   blarg
 elif 

This isn't what would normally be considered a switch (i.e. what C
considers a switch). A switch tests the value of an expression against a
set of constants. If you were writing the above in C, you would need to
use a chain of if/else statements; you couldn't use a switch.

Compiled languages' switch statements typically require constant labels as
this enables various optimisations.

The above construct is equivalent to Lisp's cond, or guards in some
functional languages.

While switch-like constructs can be implemented with a dictionary,
cond-like constructs would have to be implemented with a list, as there's
no guarantee that the tests are mutually exclusive, so the order is
significant. E.g.

rules = [((lambda (foo, bar): return foo), (lambda: blah)),
 ((lambda (foo, bar): return bar), (lambda: blah blah)),
 ((lambda (foo, bar): return not bar), (lambda: blarg)),
 ...]

for test, action in rules:
if test(foo, bar):
action()
break

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


Re: Brent's variation of a factorization algorithm

2009-12-09 Thread n00m
Being an absolute dummy in Theory of Number
for me ***c'est fantastique*** that brent() works =)

PS
1.
Values of magic parameters c = 11 and m = 137
almost don't matter. Usually they choose c = 2
(what about to run brent() in parallel with different
values of c waiting for n is cracked?)

2.
Before calling brent() n should be tested for its
primality. If it is a prime brent(n) may freeze for good.

3.
 A better place to publish this code would be the Python Cookbook:

It requires a tedious registration etc.
Gabriel, don't you mind to publish the code there by yourself?
In the long run, it is an invention by Richard Brent (b.1946) =)
I just rewrote it to Python from a pseudo-code once available in
Wiki but which for some vague reason was later on removed from there.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: attributes, properties, and accessors -- philosophy

2009-12-09 Thread Trevor Dorsey
Back to the subject of good tools.  Use an IDE that's intended for python.
 We started using WingIDE because it had an inline debugger and came with
all the nice things like autocomplete etc that things like eclipse or visual
studio have.

On Wed, Nov 25, 2009 at 8:46 AM, Bruno Desthuilliers
bruno.42.desthuilli...@websiteburo.invalid wrote:

 Ethan Furman a écrit :


 Very helpful, thank you.  Hopefully my brain will be up to the descriptor
 protocol this time... the last couple times were, um, less than successful.
  :)


 Well, it's quite simple in fact. Most of the magic happens in
 object.__getattribute__ and object.__setattr__. You'll find a rough
 description of what happens here:


 http://groups.google.com/group/comp.lang.python/browse_frm/thread/a136f7626b2a8b7d/70a672cf7448c68e





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




-- 

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


Re: Implementation suggestions for creating a Hierarchical circuit database

2009-12-09 Thread Ask me about System Design
On Dec 9, 9:57 am, nick freesof...@gmail.com wrote:
 Hi,

 I am writing a personal software that will read circuit design/
 netlist. I will be using the MCNC benchmarks that contain different
 types of designs in SPICE netlist format.

 I need some pointers/papers/suggestions on creating a hierarchical
 netlist database. The netlist database can, at times, be fully
 flattened, partially flattened or fully hierarchical. I should be able
 to answer queries like: are there any capacitors connected to node:
 x1.x2.n1?

 My program is currently only for analyzing designs for connectivity,
 types of elements (resistors/capacitors) and figuring out some simple
 electrical properties.

 I am just starting, so please bear with me if I haven't thought about
 corner cases.

 Regards
 Nick

If you start by considering just the flattened case,
you will find that the underlying database is not much
more than a labeled graph.  Make sure the code (or
specs anyway) to handle that case is rock solid before
trying non-flattened versions.  You don't want to be
fixing those problems when you move to the non-flat
situations.

I used to work at a CAE (computer-aided enigineering)
vendor where commercial software was developed to do
this, plus simulation and layout (and other
considerations).  One issue was name resolution and
linking signals across different levels.  Another issue
was using shared (nested) designs, where one page was
used to specify a component and other pages used several
instances of that component, but I don't know if the
flattened version contained copies of the subcircuit
or different references to (virtual) copies of the
subcircuit.   I advise implementing limited hierarchical
features and debugging them thoroughly before you move
on.  E.g., make sure mutli-page designs work, then
try multi-level, then nested, etc.

If you limit your specs in the beginning, you will be
able to build and test prototype versions quickly.
Your eventual end-design will hinge on answers to
questions like: Am I only doing lookup and simple
local queries, or will I have to provide a flattened
version of the design?  If you only have to do local
queries, then you can build a virtual copy of what
you need in a subcircuit and then throw it away; if
you need a flattened version, then several actual
copies of the subcircuit need to be built and printed
out.  So even before you build a good spec, you should
have a good set of questions whose answers will help
determine the specification and direct the design.

Mentor Graphics is still around; they may have someone
who can give you pointers to aid in your project.  Also
many issues are addressed by CAD software; hopefully
you will ask on those forums.  Try hardware and CAD
forums as well as comp.* forums

Gerhard Ask Me About System Design Paseman, 2009.12.09
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When will Python 3 be fully deployed

2009-12-09 Thread Nobody
On Wed, 09 Dec 2009 10:28:40 -0800, Rami Chowdhury wrote:

 But on Unix, it's a square-peg-round-hole situation.
 
 I dunno, I find it rather useful not to have to faff about with
 encoding to/from when working with non-ASCII files (with non-ASCII
 filenames) on Linux.

For the kind of task I'm referring to, there is no encoding or decoding.
You get byte strings from argv, environ, files, etc, and pass them to
library functions. What those bytes mean as text (if anything) never
enters the equation.

For cases where you *need* text (e.g. GUIs), Python 3 makes the simplest
cases easier. The more complex cases (e.g. where each data source may have
its own encoding, or even multiple encodings) aren't much different
between Python 2 and Python 3.

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


Re: ANN: WHIFF.0.7 += GAE + jQueryUI + internationalization + testdrive = (last beta?)

2009-12-09 Thread Aaron Watters
On Dec 9, 1:48 pm, Terry Reedy tjre...@udel.edu wrote:
 Aaron Watters wrote:
  Also the WHIFF documentation is now hosted on Google App
  Engine at thehttp://whiffdoc.appspot.com/domain.

 When I went there and clicked on the scatter chart is generated by a
 straightforward invocation of the standard WHIFF OpenFlashChart
 middleware: , Firefox *immediately* opened the 'Official' page of a
 well-known ugly cultwww.sci-logy.orgin a new tab. Something is
 very, very wrong. I really hope this was not intentional on your part.

 When I load the doc page again and click the chart box again, the chart
 is reloaded from the local cache instead of your site and no tab is opened.

 tjr

That was a joke in the tree view.  You clicked on the science link
in the tree hierarchy.  I will fix it due to your complaint.

Nothing is very very wrong :), it was just a misguided joke
(the bio link also goes off to someone fisherman's biography...)

Sorry about that.  Thanks for the feedback.
   -- Aaron Watters

===
want a friend?
get a dog.  -- Truman
-- 
http://mail.python.org/mailman/listinfo/python-list


Recommendation for small, fast, Python based web server

2009-12-09 Thread python
I'm looking for a small, simple, fast, Python based web server
for a simple, client side application we're building. We don't
want to distrubute and support a real web server like Apache or
Tomcat or depend on the presence of local web server such as IIS.
The application in question will service AJAX requests from a
browser.

We're not looking for a web framework like Django, Plone, etc.

I've looked at the web servers that come bundled with the Python
standard library[1] and they are too slow. I suspect this is
because they don't maintain a session between the client and
server, thus every GET/POST request repeats the session setup and
break down process. Or they might be based on a polling model?

Here are the other Python based web server implementations I'm
aware of:
- cherrypy
- web.py
- twisted

Any recommendations appreciated (Python 2.6 preferred but open to
Python 3.1 options).

Thanks!
Malcolm

[1]
http://docs.python.org/library/basehttpserver.html (base)
http://docs.python.org/library/simplehttpserver.html
http://docs.python.org/library/cgihttpserver.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When will Python 3 be fully deployed

2009-12-09 Thread Rami Chowdhury
On Wed, Dec 9, 2009 at 11:25, Nobody nob...@nowhere.com wrote:
 On Wed, 09 Dec 2009 10:28:40 -0800, Rami Chowdhury wrote:

 But on Unix, it's a square-peg-round-hole situation.

 I dunno, I find it rather useful not to have to faff about with
 encoding to/from when working with non-ASCII files (with non-ASCII
 filenames) on Linux.

 For the kind of task I'm referring to, there is no encoding or decoding.
 You get byte strings from argv, environ, files, etc, and pass them to
 library functions. What those bytes mean as text (if anything) never
 enters the equation.

Perhaps we're referring to slightly different tasks, then. I'm
thinking of scripts to move log files around, or archive documents,
where some manipulation of file and folder names is necessary --
that's where I personally have been bitten by encodings and the like
(especially since I was moving around between filesystems, as well).
But I take your point that the more complex cases are complex
regardless of Python version.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about 'remote objects'

2009-12-09 Thread Irmen de Jong

On 9-12-2009 13:56, Frank Millman wrote:

My first thought was to look into Pyro. It seems quite nice. One concern I
had was that it creates a separate thread for each object made available by
the server.


It doesn't. Pyro creates a thread for every active proxy connection.
You can register thousands of objects on the server, as long as your 
client programs only access a fraction of those at the same time you 
will have as many threads as there are proxies in your client programs.


This behavior can be tuned a little as well:
- you can tell Pyro to not use threading at all
  (that will hurt concurrency a lot though)
- you can limit the number of proxies that can be connected
  to the daemon at a time.



Then I thought that, instead of the database server exposing each object
remotely, I could create one 'proxy' object on the server through which all
clients would communicate, and it in turn would communicate with each
instance locally.


I think that this is the better design in general: access large amounts 
of remote objects not individually, but as a batch. Lots of small remote 
calls are slow. A few larger calls are more efficient.



Is there any particular benefit in using remote objects as opposed to
writing a SocketServer?


It saves you reinventing the wheel and dealing with all its problems 
again, problems that have been solved already in existing remote object 
libraries such as Pyro. Think about it: do you want to spend time 
implementing a stable, well defined communication protocol, or do you 
want to spend time building your actual application logic?


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


Re: switch

2009-12-09 Thread Tim Chase

Carl Banks wrote:

What if the object is a string you just read from a file?

How do you dispatch using polymorphism in that case?


This is where I most miss a switch/case statement in Python...I 
do lots of text-file processing (cellular provider data), so I 
have lots of code (for each provider's individual format) that 
looks like


  phones = {}
  for row in csv.DictReader(file('data.txt', 'rb')):
phonenumber = row['phonenumber']
if phonenumber not in phones:
  phones[phonenumber] = Phone(phonenumber)
phone = phones[phonenumber]
rectype = rectype
if rectype == '01':
  phone.international += Decimal(row['internationalcost'])
elif rectype == '02':
  phone.text_messaging += (
int(row['textmessages sent']) +
int(row['pages received']) +
int(row['textmessages sent']) +
int(row['pages received'])
elif rectype == ...
   ...
else:
  raise WhatTheHeckIsThis()

which would nicely change into something like

  switch row['recordtype']:
case '01':
  phone.international += Decimal(row['internationalcost'])
  // optionally a break here depending on
  // C/C++/Java/PHP syntax vs. Pascal syntax which
  // doesn't have fall-through
case '02':
  phone.text_messaging += (
int(row['textmessages sent']) +
int(row['pages received']) +
int(row['textmessages sent']) +
int(row['pages received'])
...
default:
  raise WhatTheHeckIsThis()

This doesn't convert well (i.e. compactly) to a 
dictionary-dispatch idiom. :(


-tkc




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


using freeze.py with python3

2009-12-09 Thread Patrick Stinson
Has anyone tried using Python-3.1.1/Tools/freeze/freeze.py with the
encodings package? It appears that encodings is required to intialize
the interpreter, but PyImport_ImportFrozenModule is failing for the
encodings module in marshal.c:r_object(), after trying to demarshal
an object of type 0.

The failing callstack looks like this:

#0  0x00599114 in PyImport_ImportFrozenModule at import.c:2011
#1  0x00598b14 in load_module at import.c:1830
#2  0x0059aef2 in import_submodule at import.c:2631
#3  0x0059a2ed in load_next at import.c:2436
#4  0x00599629 in import_module_level at import.c:2153
#5  0x00599a80 in PyImport_ImportModuleLevel at import.c:2204
#6  0x00565b76 in builtin___import__ at bltinmodule.c:173
#7  0x004e6cfc in PyCFunction_Call at methodobject.c:84
#8  0x0048fbe5 in PyObject_Call at abstract.c:2160
#9  0x0048fd94 in call_function_tail at abstract.c:2192
#10 0x0048fe6e in PyObject_CallFunction at abstract.c:2216
#11 0x0059b7f6 in PyImport_Import at import.c:2811
#12 0x0059945e in PyImport_ImportModule at import.c:2064
#13 0x00599551 in PyImport_ImportModuleNoBlock at import.c:2115
#14 0x0058c99a in _PyCodecRegistry_Init at codecs.c:1042
#15 0x00589a50 in _PyCodec_Lookup at codecs.c:110
#16 0x005a7892 in get_codeset at pythonrun.c:145
#17 0x005a7ea1 in Py_InitializeEx at pythonrun.c:272
#18 0x005a7fbc in Py_Initialize at pythonrun.c:309

As you can see it is calling  _PyCodecRegistry_Init from
get_codeset(). Is there something I can disable here to avoid this
problem altogether? I am using the following script with freeze.py to
freeze the required modules:

print('Hello world...')
import dummy_threading

import encodings
import encodings.aliases
import encodings.ascii
import encodings.big5
import encodings.big5hkscs
import encodings.charmap
import encodings.cp037
import encodings.cp1006
import encodings.cp1026
import encodings.cp1140
import encodings.cp1250
import encodings.cp1251
import encodings.cp1252
import encodings.cp1253
import encodings.cp1254
import encodings.cp1255
import encodings.cp1256
import encodings.cp1257
import encodings.cp1258
import encodings.cp424
import encodings.cp437
import encodings.cp500
import encodings.cp737
import encodings.cp775
import encodings.cp850
import encodings.cp852
import encodings.cp855
import encodings.cp856
import encodings.cp857
import encodings.cp860
import encodings.cp861
import encodings.cp862
import encodings.cp863
import encodings.cp864
import encodings.cp865
import encodings.cp866
import encodings.cp869
import encodings.cp874
import encodings.cp875
import encodings.cp932
import encodings.cp949
import encodings.cp950
import encodings.euc_jis_2004
import encodings.euc_jisx0213
import encodings.euc_jp
import encodings.euc_kr
import encodings.gb18030
import encodings.gb2312
import encodings.gbk
import encodings.hp_roman8
import encodings.hz
import encodings.idna
import encodings.iso2022_jp
import encodings.iso2022_jp_1
import encodings.iso2022_jp_2
import encodings.iso2022_jp_2004
import encodings.iso2022_jp_3
import encodings.iso2022_jp_ext
import encodings.iso2022_kr
import encodings.iso8859_1
import encodings.iso8859_10
import encodings.iso8859_11
import encodings.iso8859_13
import encodings.iso8859_14
import encodings.iso8859_15
import encodings.iso8859_16
import encodings.iso8859_2
import encodings.iso8859_3
import encodings.iso8859_4
import encodings.iso8859_5
import encodings.iso8859_6
import encodings.iso8859_7
import encodings.iso8859_8
import encodings.iso8859_9
import encodings.johab
import encodings.koi8_r
import encodings.koi8_u
import encodings.latin_1
import encodings.mac_arabic
import encodings.mac_centeuro
import encodings.mac_croatian
import encodings.mac_cyrillic
import encodings.mac_farsi
import encodings.mac_greek
import encodings.mac_iceland
import encodings.mac_latin2
import encodings.mac_roman
import encodings.mac_romanian
import encodings.mac_turkish
import encodings.mbcs
import encodings.palmos
import encodings.ptcp154
import encodings.punycode
import encodings.raw_unicode_escape
import encodings.shift_jis
import encodings.shift_jis_2004
import encodings.shift_jisx0213
import encodings.tis_620
import encodings.undefined
import encodings.unicode_escape
import encodings.unicode_internal
import encodings.utf_16
import encodings.utf_16_be
import encodings.utf_16_le
import encodings.utf_32
import encodings.utf_32_be
import encodings.utf_32_le
import encodings.utf_7
import encodings.utf_8
import encodings.utf_8_sig
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: switch

2009-12-09 Thread MRAB

Tim Chase wrote:

Carl Banks wrote:

What if the object is a string you just read from a file?

How do you dispatch using polymorphism in that case?



[snip]


which would nicely change into something like

  switch row['recordtype']:
case '01':
  phone.international += Decimal(row['internationalcost'])
  // optionally a break here depending on
  // C/C++/Java/PHP syntax vs. Pascal syntax which
  // doesn't have fall-through
case '02':
  phone.text_messaging += (
int(row['textmessages sent']) +
int(row['pages received']) +
int(row['textmessages sent']) +
int(row['pages received'])
...
default:
  raise WhatTheHeckIsThis()

This doesn't convert well (i.e. compactly) to a dictionary-dispatch 
idiom. :(



Shouldn't 'case' be indented to the same level as 'switch'? And
'default' could be replaced by 'else' without ambiguity.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Sum of the factorial of the digits of a number - wierd behaviour

2009-12-09 Thread Jon Clements
Even though you've worked it out -- a couple of tips:

On Dec 9, 5:39 pm, SiWi wimmersi...@googlemail.com wrote:
 On Dec 9, 6:36 pm, SiWi wimmersi...@googlemail.com wrote:



  Dear python community,
  I've got a wierd problem and I hope you can help me out at it.
  I wrote the following code to find the Sum of the factorial of the
  digits of a number (this is for Project Euler 74):

  def fac(n):
      x=1
      for i in range(2,n+1):
          x*=i
      return x


numpy/scipy etc... are quite useful for Euler :)

They contain a function to do factorials (and loads more).

This to one of the readable uses of 'reduce':
def fac(n):
reduce(operator.mul, xrange(2, n+1), n)

  t=tuple(fac(n) for n in range(1,10))

  def decimals(x):
      i=1
      d=[]
      while x0:
          d.append(x%10)
          x=x/10
      return d

The builtin str object can take integers and return it as a string.


def decimals(x):
return map(int, str(x))
decimals(145) == [1, 4, 5]


  def sumfac(x):
      return sum(t[n-1] for n in decimals(x))


And join the two:
print sum(fac(n) for n in decimals(145))



  The problem is that i get the following results, for which I can't see
  any reason:
  sumfac(145)-145 (1!+4!+5!=1 + 24 +120 = 145) - ok
  sumfac(1454)- 169 - ok
  sumfac(45362) - 872 - ok
  sumfac(363600) - 727212 - wrong, should be1454

  Greetings,
  SiWi.

 Oops, found it myself. You can ignote the message above.

You might also find it useful to write generators (esp. for primes and
factorials).

Cheers,

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


Perl to Python conversion

2009-12-09 Thread Martin Schöön
First off: I am new here and this is my first post after
lurking for quite some time.

Second off: I don't know much Python---yet.

Problem: I have come across a small open source application
that I find quite useful. It does have one major flaw though.
Its output is in imperial units. Converting isn't a big deal
for occasional use but if I start to use this stuff on a
regular basis...

So I down-loaded the source code and found this thing is written
in Perl.

Should I learn enough Perl to add the conversion? Probably
but this may be a nice excuse to get my Python education
going and if I do I might as well re-do the user interface.

If I do re-write this thing in Python I might need to learn both
Perl and Python...

Hence, are there any Perl to Python converters? So far I
have only found bridgekeeper which really is (was?) consultancy.
Apart from that I only find people recommending a manual re-write.

Any thoughts/recommendations?

TIA,

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


Re: a huge shared read-only data in parallel accesses -- How? multithreading? multiprocessing?

2009-12-09 Thread Emile van Sebille

On 12/9/2009 6:58 AM Valery said...

Hi all,

Q: how to organize parallel accesses to a huge common read-only Python
data structure?


I have such a structure which I buried in a zope process which keeps it 
in memory and is accessed through http requests.  This was done about 8 
years ago, and I think today I'd check out pyro.


Emile

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


Re: Sum of the factorial of the digits of a number - wierd behaviour

2009-12-09 Thread geremy condra
 numpy/scipy etc... are quite useful for Euler :)

I've come to love sympy, personally.

 They contain a function to do factorials (and loads more).

 from math import factorial
 factorial(5)
120

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


Re: switch

2009-12-09 Thread Tim Chase

MRAB wrote:

Tim Chase wrote:

  switch row['recordtype']:
case '01':
  phone.international += Decimal(row['internationalcost'])
  // optionally a break here depending on
  // C/C++/Java/PHP syntax vs. Pascal syntax which
  // doesn't have fall-through
case '02':
  phone.text_messaging += (
int(row['textmessages sent']) +
int(row['pages received']) +
int(row['textmessages sent']) +
int(row['pages received'])
...
default:
  raise WhatTheHeckIsThis()

This doesn't convert well (i.e. compactly) to a dictionary-dispatch 
idiom. :(



Shouldn't 'case' be indented to the same level as 'switch'? And
'default' could be replaced by 'else' without ambiguity.


But I want a GREEN bike-shed! :-)  Yeah, else works nicely and 
makes sense.  Indentation could go either way in my book, but I 
lean towards indented case because the switch can get easily 
lost if the cases aren't indented:


  switch foo:
  case 1:
stuff()
  case 2:
morestuff()
  switch bar:
  case 3:
whatever()
  case 4:
yet_more()
  else:
whip_it()

vs

  switch foo:
case 1:
  stuff()
case 2:
  morestuff()
  switch bar:
case 3:
  whatever()
case 4:
  yet_more()
else:
  whip_it()

Just my ponderings...

-tkc



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


Re: plain text parsing to html (newbie problem)

2009-12-09 Thread akean
On Dec 10, 3:59 am, João joao...@gmail.com wrote:
 I apologize for my newbiness but I'm banging my head making this work :
 (
...
 How can I see the output run in debug mode like in perl?



One method:  install ipython (another python shell, but with some
useful extra features)
and then run the program inside ipython in debug mode:
-
$ ipython


In [1]: run -d filename.py


Breakpoint 1 at /path/to/filename.py:3
NOTE: Enter 'c' at the ipdb  prompt to start your script.
 string(1)module()

ipdb

(You type c to continue)

ipdb c
 /path/to/filename.py(3)module()
  2
1--- 3 import sys, os
  4 import subprocess


and you can see you're now on line 3.
Press h for help to see what commands you can type.
Press n for next line if you want to step.
Press s to step into a function when it's being called.  Etc.

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


Re: Perl to Python conversion

2009-12-09 Thread zeph
Python and Perl often have different design idioms - learning to write
*well* in a language involves understanding those idioms, and being
able to translate between languages involves understanding the source
language well enough to understand the intent of the program's code
(even if its poorly written), and understanding the target language
well enough to translate the intent into a design fitting the
language.

Perl and Python have enough syntactic similarities that you could, if
you wanted, just translate the program verbatim.

Overall, I would recommend adding your imperial-metric functionality
to the Perl code for now, and on your free time work on translating to
Python, so you don't feel rushed to get it finished.  You will
probably come to understand the Perl code better after having worked
with it and been involved in a hands-on way with it.  Likewise, you
will become even more familiar with it and with Python as you learn
how to translate the application.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a huge shared read-only data in parallel accesses -- How? multithreading? multiprocessing?

2009-12-09 Thread Aaron Watters
On Dec 9, 9:58 am, Valery khame...@gmail.com wrote:
 Hi all,

 Q: how to organize parallel accesses to a huge common read-only Python
 data structure?

Use a BTree on disk in a file.  A good file system will keep most of
the
pages you need in RAM whenever the data is warm.  This works
for Python or any other programming language.  Generally you can
always get to any piece of data in about 4 seeks at most anyway,
so if your disk is fast your app will be fast too.  The file can
be accessed concurrently without problems by any number of processes
or threads.

   -- Aaron Watters
  http://listtree.appspot.com
  http://whiffdoc.appspot.com
===
less is more
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Perl to Python conversion

2009-12-09 Thread Intchanter / Daniel Fackrell
On Dec 9, 1:33 pm, martin.sch...@gmail.com (Martin Schöön) wrote:
 First off: I am new here and this is my first post after
 lurking for quite some time.

 Second off: I don't know much Python---yet.

 Problem: I have come across a small open source application
 that I find quite useful. It does have one major flaw though.
 Its output is in imperial units. Converting isn't a big deal
 for occasional use but if I start to use this stuff on a
 regular basis...

 So I down-loaded the source code and found this thing is written
 in Perl.

 Should I learn enough Perl to add the conversion? Probably
 but this may be a nice excuse to get my Python education
 going and if I do I might as well re-do the user interface.

 If I do re-write this thing in Python I might need to learn both
 Perl and Python...

 Hence, are there any Perl to Python converters? So far I
 have only found bridgekeeper which really is (was?) consultancy.
 Apart from that I only find people recommending a manual re-write.

 Any thoughts/recommendations?

 TIA,

 /Martin

Martin,

A full answer will depend a lot on several different factors,
including the length of the Perl code, what style it was written in
(there seem to be uncountably many possibilities), your experience
with languages in general, and with that style in particular.

In general, though, if your primary purpose is to learn Python and
ending up with a useful tool is secondary, I'd recommend rewriting the
tool from scratch, possibly keeping the Perl source handy.  If the
existing tool is command-line based, you might also be able to write a
short script through which you can pipe the output of the original
program to handle the conversion.

Intchanter
Daniel Fackrell
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2009-12-09 Thread Cameron Laird
QOTW:  I'm not sure you ever understood what the problem was, or
where, but
I'm happy you feel like you've solved it. - Marco Mariani

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


Python 2.7a1, the first alpha release of the 2.7 series, is
available now:
http://groups.google.com/group/comp.lang.python/t/743e13e80290fbbf/

A bidirectional dictionary structure analyzed:
http://groups.google.com/group/comp.lang.python/t/785d100681f7d101/

How to define and use a nested class:
http://groups.google.com/group/comp.lang.python/t/6649dfa6eb9797c8/

PyCon 2010 registration opens:

http://pycon.blogspot.com/2009/11/registration-for-pycon-2010-is-open.html

Why not have a local variable scope smaller than a function?
http://groups.google.com/group/comp.lang.python/t/57a8dec0d5d5deda/

How to test whether two floating point values are close enough
to be
almost equal:
http://groups.google.com/group/comp.lang.python/t/54ec18c06c46caaf/

A special notation for accessing dynamic attribute names:
http://groups.google.com/group/comp.lang.python/t/2f5674a1f451b12f/

Computed attributes, properties, and the descriptor protocol
magic:
http://groups.google.com/group/comp.lang.python/t/c07268689549cf01/

Name resolution, global names, importing modules, and how all of
them
interact:
http://groups.google.com/group/comp.lang.python/t/55afecc0d9e3e6c/
http://groups.google.com/group/comp.lang.python/t/de91efba9c5d68ae/

Are there any high-volume Python-powered websites?
http://groups.google.com/group/comp.lang.python/t/7e388f22cccf7c4b/

How decoupled are the Python web frameworks?
http://groups.google.com/group/comp.lang.python/t/f04940f9d4b136bc/

Moving from Python 2 to Python 3: A cheat sheet by Mark
Summerfield
http://groups.google.com/group/comp.lang.python/t/53716c4136be473b/
The sheets:
http://www.informit.com/promotions/promotion.aspx?promo=137519

Thoughts about bytecode optimization and measuring overhead of the
eval
loop:
http://groups.google.com/group/comp.lang.python/t/115b24c0ee9ee06a/

Python as the starting point for a career on IT:
http://groups.google.com/group/comp.lang.python/t/907419d0cdd68570/

Idea: get around the GIL using XMLRPC
http://groups.google.com/group/comp.lang.python/t/9f84e7aa634de4f5/

Bored and looking for something to do in Python:
http://groups.google.com/group/comp.lang.python/t/bba7c231bb9940a4/



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

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

PythonWare complements the digest you're reading with the
marvelous daily python url
 http://www.pythonware.com/daily

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

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

Readers have recommended the Planet site:
http://planet.python.org

comp.lang.python.announce announces new Python software.  Be
sure to scan this newsgroup weekly.
http://groups.google.com/group/comp.lang.python.announce/topics

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

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

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

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

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

The Summary of Python Tracker Issues is an automatically generated
report summarizing new bugs, closed ones, and patch submissions.

http://search.gmane.org/?author=status%40bugs.python.orggroup=gmane.comp.python.develsort=date

Although unmaintained since 2002, the Cetus collection of Python
hyperlinks retains a few 

Re: Recommendation for small, fast, Python based web server

2009-12-09 Thread Tim Chase

pyt...@bdurham.com wrote:

I'm looking for a small, simple, fast, Python based web server
for a simple, client side application we're building. 


I've used WebStack[1] for this in the past.  It allows for 
stand-alone serving as well as plugging nicely into various 
real servers (apache+mod_python, etc) with a small tweak in how 
it's configured.



I've looked at the web servers that come bundled with the Python
standard library[1] and they are too slow. I suspect this is
because they don't maintain a session between the client and
server, thus every GET/POST request repeats the session setup and
break down process. Or they might be based on a polling model?


I'm not sure what caused the slowness you've experienced -- using 
Python in a CGI environment requires starting the Python 
interpreter each time.  However if the interpreter starts just 
once, I've not had notable performance issues for low-volume 
sites (using the BaseHTTP builtin).  For higher-volume sites, you 
might reach for Twisted which WebStack supports as well.  I 
believe both WebStack and Twisted are redistribtable as needed.


-tkc

[1] http://www.boddie.org.uk/python/WebStack.html




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


Re: UnboundLocalError: local variable '_[1]' referenced before assignment

2009-12-09 Thread Dave Angel

Gabriel Rossetti wrote:

Dave Angel wrote:

Gabriel Rossetti wrote:
div class=moz-text-flowed style=font-family: -moz-fixedHello 
everyone,


I get this error on python 2.6.1 on mac os x 10.6 :

UnboundLocalError: local variable '_[1]' referenced before assignment

here's the code that raises this:

params = [ self.__formatData(paramProcFunc, query, p) for p in params ]

what I don't get is that it worked on mac os x 10.5 (python 2.5.x) 
but it no longer works. I tried the following and it works :


r = []
for p in params:
   r.append(self.__formatData(paramProcFunc, query, p))
params = r

Does anyone understand what is going on here?

Thank you,
Gabriel

/div

Clearly you're not supplying enough context.  The UnboundLocalError 
is only raised inside a function (or method), and you only show one 
line of that function.


And in the second example, it's even worse, since you imply it's 
top-level code by carefully unindenting everything.


My *guess* is that you have a global variable params, which you're 
looping on.  And then you assign a local variable by the same name.  
If you have an assignment anywhere in the function, that name is 
considered a local, and if you reference it before you assign it, 
it'll generate this error.


Three possible fixes, depending on why you're doing this.

1) pass the params in to the function as an argument
2) use a different name for the local thing you're building
3) use a global statement to force the compiler to use the global one 
and not create a local.  (probably a bad idea)


DaveA



Hello Dave,

ok, you' re right about not showing enough:

params = [ self.__formatData(paramProcFunc, query, p) for p in params ]


where :

paramProcFunc = percent2Volume

def __formatData(self, func, query, data):
   return getattr(self._unitDataAbs, func)(self._unitCmdAbs, 
query, data)


def percent2Volume(self, absCmds, query, percent):
   return query, int(round(percent / 100.0 * absCmds[volCeil]))


but I still don't see where the problem is and why it works with the 
explicit loop and not the list comp.


Thanks,
Gabriel

I don't see either;  you still don't supply nearly enough context.  You 
don't show enough code for someone to actually try it, and since it's 
not crashing on that line, you're clearly not showing the whole stack trace.


I made a few wild guesses about your code, and have something that 
compiles and runs.  But I'm on Windows XP, with Python 2.6.4, so your 
mileage may vary.


import sys
print sys.version

class  Dummy(object):
   def __init__(self):
   self._unitDataAbs = self
   self._unitCmdAbs =  {volCeil:298}
   def crash(self):
   query = this is a query
   params = [100,20,37,42]
   paramProcFunc = percent2Volume
   params = [ self.__formatData(paramProcFunc, query, p) for p in 
params ]

   return params

   def __formatData(self, func, query, data):
   return getattr(self._unitDataAbs, func)(self._unitCmdAbs, query, 
data)


   def percent2Volume(self, absCmds, query, percent):
  return query, int(round(percent / 100.0 * absCmds[volCeil]))

obj = Dummy()
p = obj.crash()
print p


When I run it, I get the following output:

2.6.4 (r264:75706, Nov  3 2009, 13:23:17) [MSC v.1500 32 bit (Intel)]
[('this is a query', 298), ('this is a query', 60), ('this is a query', 
110), ('this is a query', 125)]


You could try pasting the same code into a file on your system, and if 
it crashes, then copy the full error stacktrace into a message.


If it doesn't, you need to either post your whole code (enough for 
somebody to actually test), or simplify it till it doesn't crash. Then 
the next-to-last change is the one that masks the problem.



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


Re: Request for py program to insert space between two characters and saved as text?

2009-12-09 Thread Dave Angel



r0g wrote:

Dave Angel wrote:
  

r0g wrote:


Dave Angel wrote:
 
  

r0g wrote:
   


Dave Angel wrote:
 
 
  

r0g wrote:
  


Dennis Lee Bieber wrote:
 
  
  

On Tue, 8 Dec 2009 08:26:58 +0530, 74yrs old
withblessi...@gmail.com
declaimed the following in gmane.comp.python.general:

 


For Kannada project .txt(not .doc) is used, my requirement is to
have one
  
  

big snip
 
  

  

That's even worse.  As far as I can tell, the code will never do what he
wants in Python 2.x.   The Kannada text file is full of Unicode
characters in some encoding, and if you ignore the encoding, you'll just
get garbage.





Ah, fair enough. In my defence though I never saw the original post or
this kannada.txt file as my newsserver is not so much with the
reliability. I guess it's naive to assume an english .txt file is going
to be in ASCII these days eh?

I've yet to try python 3 yet either, this whole Unicode thing looks like
it could be a total nightmare! :(

Roger.

  
  
But it isn't an english  .txt file, it's a Kannada  .txt file.  
Presumably you didn't realize that Kannada is a (non-English) language,

spoken in parts of India, with several hundred characters.  ASCII wasn't
even an option.  Anyway, no harm done, someone else referred the OP to a
Python user-group local to that region.

DaveA




Well this looked like English to me...

example: *F o r  K a n n a d a  p r o j e c t . t x t(n o t .d o c)  i s
 u s e d,  m y  r e q u i r e m e n t   i s  t o  h a v e  o n e  s p a
c e  b e t w e e n  t w o  c h a r a c t e r s  i n  t h e  t e x t.*

...but yes you're right, I had never heard of Kannada let alone knew it
was another language!

Roger.

  
There were two examples.  The one you quoted was in English, and 
immediately afterward was the second one, presumably in Kannada.


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


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

2009-12-09 Thread Gabriel Genellina
QOTW:  I'm not sure you ever understood what the problem was, or where, but
I'm happy you feel like you've solved it. - Marco Mariani

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


Python 2.7a1, the first alpha release of the 2.7 series, is available now:
http://groups.google.com/group/comp.lang.python/t/743e13e80290fbbf/

A bidirectional dictionary structure analyzed:
http://groups.google.com/group/comp.lang.python/t/785d100681f7d101/

How to define and use a nested class:
http://groups.google.com/group/comp.lang.python/t/6649dfa6eb9797c8/

PyCon 2010 registration opens:

http://pycon.blogspot.com/2009/11/registration-for-pycon-2010-is-open.html

Why not have a local variable scope smaller than a function?
http://groups.google.com/group/comp.lang.python/t/57a8dec0d5d5deda/

How to test whether two floating point values are close enough to be
almost equal:
http://groups.google.com/group/comp.lang.python/t/54ec18c06c46caaf/

A special notation for accessing dynamic attribute names:
http://groups.google.com/group/comp.lang.python/t/2f5674a1f451b12f/

Computed attributes, properties, and the descriptor protocol magic:
http://groups.google.com/group/comp.lang.python/t/c07268689549cf01/

Name resolution, global names, importing modules, and how all of them
interact:
http://groups.google.com/group/comp.lang.python/t/55afecc0d9e3e6c/
http://groups.google.com/group/comp.lang.python/t/de91efba9c5d68ae/

Are there any high-volume Python-powered websites?
http://groups.google.com/group/comp.lang.python/t/7e388f22cccf7c4b/

How decoupled are the Python web frameworks?
http://groups.google.com/group/comp.lang.python/t/f04940f9d4b136bc/

Moving from Python 2 to Python 3: A cheat sheet by Mark Summerfield
http://groups.google.com/group/comp.lang.python/t/53716c4136be473b/
The sheets:
http://www.informit.com/promotions/promotion.aspx?promo=137519

Thoughts about bytecode optimization and measuring overhead of the eval
loop:
http://groups.google.com/group/comp.lang.python/t/115b24c0ee9ee06a/

Python as the starting point for a career on IT:
http://groups.google.com/group/comp.lang.python/t/907419d0cdd68570/

Idea: get around the GIL using XMLRPC
http://groups.google.com/group/comp.lang.python/t/9f84e7aa634de4f5/

Bored and looking for something to do in Python:
http://groups.google.com/group/comp.lang.python/t/bba7c231bb9940a4/



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

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

PythonWare complements the digest you're reading with the
marvelous daily python url
 http://www.pythonware.com/daily

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

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

Readers have recommended the Planet site:
http://planet.python.org

comp.lang.python.announce announces new Python software.  Be
sure to scan this newsgroup weekly.
http://groups.google.com/group/comp.lang.python.announce/topics

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

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

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

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

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

The Summary of Python Tracker Issues is an automatically generated
report summarizing new bugs, closed ones, and patch submissions. 

http://search.gmane.org/?author=status%40bugs.python.orggroup=gmane.comp.python.develsort=date

Although unmaintained 

Re: How do I Block Events in wxPython

2009-12-09 Thread Dave Angel

Wanderer wrote:

snip

Found another strange bug (Strange to me, anyway). int(0.8 * 10.0) 7. Had to 
change the code to int(0.8 * 10.0 + 0.0001).


  
Floating point is intrinsically imprecise.  The value 0.8 cannot be 
exactly represented in IEEE fp notation (binary).  One answer is to 
round() the result before converting to int.



DaveaA

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


Parsing json where object keys are not quoted?

2009-12-09 Thread Wells
Is there some way to finagle the json module to parse JSON (well,
almost JSON) where the object keys are not in quotes? I know it's not
100% valid JSON, but I'm just curious.

I don't have control over the data, so I can't make it fit the spec :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Graph library for Python

2009-12-09 Thread Rhodri James
On Wed, 09 Dec 2009 03:47:03 -, geremy condra debat...@gmail.com  
wrote:



On Tue, Dec 8, 2009 at 8:42 PM, Rhodri James
rho...@wildebst.demon.co.uk wrote:


g = Graph(
   nodes=[Node(a, colour=red),
  Node(b, colour=white),
  Node(c, colour=blue)],
   edges=[Edge(a, b, ab, weight=2),
  Edge(a, c, ac, is_directed=True),
  Edge(b, c, bc, style=dotted)],
   is_directed=True)

I could see a use for this tracking a database structure using a  
constant

graph, hence all set up in one go for preference.


While I agree with the rationale, I think we need to find another way.
Aesthetics aside, directly instantiating edges by giving only node names
requires that the edge be aware of what graph its in to provide expected
behavior, which creates a bit of a chicken-or-the-egg dilemma.


Oops.  I missed that, sorry.


How about this: the constructor can take any type of iterable, and
assumes that it follows my earlier format unless it specifies a .items()
method, in which case it takes the values as follows:


isinstance(x, collections.Mapping) is perhaps the right test?


g = Graph(
nodes={'a':{'colour':'red'},
   'b':{'colour':'white'},
   'c':{'colour':'blue'}},
edges={('a', 'b'):{'name':'ab', 'weight':2},
   ('a', 'c'):{'name':'ac'},
   ('b', 'c'):{'name':'bc', 'style':'dotted'}}
)


That's OK for nodes, but for consistency with add_edges I would have  
expected the name to be the optional third element of the key tuples.  It  
works either way, but I can't help feel it's beginning to look a bit ugly.


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


Re: Brent's variation of a factorization algorithm

2009-12-09 Thread Irmen de Jong

On 27-11-2009 16:36, n00m wrote:

Maybe someone'll make use of it:


def gcd(x, y):
 if y == 0:
 return x
 return gcd(y, x % y)

def brent(n):

[...]

[D:\Projects]python brentfactor.py
9
== 27 * 37037037

What gives? Isn't this thing supposed to factor numbers into the product 
of two primes?


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


Porting pyftpdlib to Python 3.x: question about tarball naming convention

2009-12-09 Thread Giampaolo Rodola'
Hi,
I've started the (hard) process of porting pyftpdlib [1] to Python 3.
In order to do that I'm working on a separate SVN branch and I plan to
maintain two different releases of my software, one for 2.x and
another one for 3.x.

My doubts are about the naming convention I have to use for the
tarball and how it affects the integration with distutils and
setuptools.
So far I've always used the following naming convention:

pyftpdlib-0.5.2.tar.gz
pyftpdlib-0.5.1.tar.gz
pyftpdlib-0.4.1.tar.gz
...


This way I'm able to download and easy install pyftpdlib by just
doing:

 easy_install pyftpdlib

...which retrieves the last pyftpdlib version (0.5.2, at the moment)
from PYPI and installs it.


Now that I'm going to have two major releases (pyftpdlib-0.5.2 for
Python 2.x and pyftpdlib-0.5.2 for Python 3.x) how am I supposed to
deal with that?
Do I have to use a different name like pyftpdlib-0.5.2-py3k.tar.gz
or pyftpdlib-py3k-0.5.2.tar.gz?
How this affects the interaction with easy install?

And again: in case it is possible to keep the same tarball name for
both versions what am I gonna do with PYPI? Is it possible to host two
programs with the same name on PYPI and just differentiate the
description (e.g. pyftpdlib version for python 2.x / pyftpdlib
version for python 3.x)


Thanks in advance

[1] http://code.google.com/p/pyftpdlib
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Recommendation for small, fast, Python based web server

2009-12-09 Thread Daniel Fetchinson
 I'm looking for a small, simple, fast, Python based web server
 for a simple, client side application we're building. We don't
 want to distrubute and support a real web server like Apache or
 Tomcat or depend on the presence of local web server such as IIS.
 The application in question will service AJAX requests from a
 browser.

 We're not looking for a web framework like Django, Plone, etc.

 I've looked at the web servers that come bundled with the Python
 standard library[1] and they are too slow. I suspect this is
 because they don't maintain a session between the client and
 server, thus every GET/POST request repeats the session setup and
 break down process. Or they might be based on a polling model?

 Here are the other Python based web server implementations I'm
 aware of:
 - cherrypy
 - web.py
 - twisted

 Any recommendations appreciated (Python 2.6 preferred but open to
 Python 3.1 options).

I'm using cherrypy for this purpose, actually together with turbogears 1.

Cheers,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing json where object keys are not quoted?

2009-12-09 Thread Intchanter / Daniel Fackrell
On Dec 9, 3:51 pm, Wells thewellsoli...@gmail.com wrote:
 Is there some way to finagle the json module to parse JSON (well,
 almost JSON) where the object keys are not in quotes? I know it's not
 100% valid JSON, but I'm just curious.

 I don't have control over the data, so I can't make it fit the spec :)

Hopefully this won't be a recurring problem, because maintenance of
any solution could very well be a nightmare if you have to keep it up.

The JSON library that ships with Python doesn't appear to be built for
malformed JSON like what you mention, and making it handle it will
take a bit of work on your part, but here's a start (based on my 2.6.4
installation):

In /path_to_python_standard_library/json/decoder.py (please back this
up before making any changes), comment out the try/except block that
tries to load scanstring from _json and duplicate the last line
(c_scanstring = None), removing its indentation.

You'll then need to modify py_scanstring() to meet your needs, but be
sure you understand what it's doing first.  You'll need to track
whether you found the leading '' for the key and look for the other
one if you did, but just look for the ':' otherwise.

Again, this isn't an advisable solution, and it won't work in all
cases even if you have the best of luck, but it may just work in
enough cases.  It's pretty amazing that the incoming document doesn't
match the spec, though.  The only correct solution would be to fix the
library that generated it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Perl to Python conversion

2009-12-09 Thread Peter Chant
Martin Schöön wrote:

 Hence, are there any Perl to Python converters? So far I
 have only found bridgekeeper which really is (was?) consultancy.
 Apart from that I only find people recommending a manual re-write.
 
 Any thoughts/recommendations?

Voice of almost no experience.  I once ran a fortran programme through a 
fortran to c converter and when I saw the result I ran away screaming - it 
did not look very human friendly.




-- 
http://www.petezilla.co.uk

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


Re: Graph library for Python

2009-12-09 Thread geremy condra
On Wed, Dec 9, 2009 at 6:04 PM, Rhodri James
rho...@wildebst.demon.co.uk wrote:
 On Wed, 09 Dec 2009 03:47:03 -, geremy condra debat...@gmail.com
 wrote:

 On Tue, Dec 8, 2009 at 8:42 PM, Rhodri James
 rho...@wildebst.demon.co.uk wrote:

 g = Graph(
   nodes=[Node(a, colour=red),
          Node(b, colour=white),
          Node(c, colour=blue)],
   edges=[Edge(a, b, ab, weight=2),
          Edge(a, c, ac, is_directed=True),
          Edge(b, c, bc, style=dotted)],
   is_directed=True)

 I could see a use for this tracking a database structure using a constant
 graph, hence all set up in one go for preference.

 While I agree with the rationale, I think we need to find another way.
 Aesthetics aside, directly instantiating edges by giving only node names
 requires that the edge be aware of what graph its in to provide expected
 behavior, which creates a bit of a chicken-or-the-egg dilemma.

 Oops.  I missed that, sorry.

 How about this: the constructor can take any type of iterable, and
 assumes that it follows my earlier format unless it specifies a .items()
 method, in which case it takes the values as follows:

 isinstance(x, collections.Mapping) is perhaps the right test?

The code I kludged together last night just tries __getitem__
and it seems to work, so unless theres something I'm missing
I'll probably just leave it at that.

 g = Graph(
    nodes={'a':{'colour':'red'},
               'b':{'colour':'white'},
               'c':{'colour':'blue'}},
    edges={('a', 'b'):{'name':'ab', 'weight':2},
               ('a', 'c'):{'name':'ac'},
               ('b', 'c'):{'name':'bc', 'style':'dotted'}}
 )

 That's OK for nodes, but for consistency with add_edges I would have
 expected the name to be the optional third element of the key tuples.  It
 works either way, but I can't help feel it's beginning to look a bit ugly.

I have to admit, I prefer it the other way, but patrick (our test guru and
chief bug squasher) likes your proposal better. I'm going to get in touch
with robbie tonight and see what he says. Since this is not a feature I'll
use much, if he agrees with you then I'll go ahead and implement the
change tonight and merge it back into mainline. If not, I'd appreciate
it if you'd take another look at it and figure out if its something you can
live with, or if theres another syntax you'd prefer, etc. Fair enough?

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


Re: Recommendation for small, fast, Python based web server

2009-12-09 Thread python
Tim,

 I've used WebStack[1] for this in the past. It allows for stand-alone serving 
 as well as plugging nicely into various real servers (apache+mod_python, 
 etc) with a small tweak in how it's configured.

Thanks for that recommendation.

 I'm not sure what caused the slowness you've experienced (... with running 
 local versions of Python web servers)

Thanks to a past post by Christoph Zwerschke c...@online.de, I was
able to identify the problem.

Windows (XP, Vista, Windows 7) tries to do a IPV6 connection which times
out after a second followed by an IPV4 connection which is almost
instantaneous. Apparently this is a known problem that is a Windows
issue [1] - not a Python problem.

Two workarounds:

1. Use 127.0.0.1 as your URL vs. localhost 

-OR-

2. Edit your Windows hosts file (c:\windows\system32\drivers\etc\hosts)
and create a virtual domain name, eg. put the following on a line by
itself:

127.0.0.1  mydomain.someext

And then use mydomain.someext vs. localhost

Note: Editing your hosts file requires admin rights under Vista and
Windows 7.

Regards,
Malcolm

[1]
http://schotime.net/blog/index.php/2008/05/27/slow-tcpclient-connection-sockets/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Graph library for Python

2009-12-09 Thread Rhodri James
On Wed, 09 Dec 2009 23:42:13 -, geremy condra debat...@gmail.com  
wrote:



On Wed, Dec 9, 2009 at 6:04 PM, Rhodri James
rho...@wildebst.demon.co.uk wrote:

On Wed, 09 Dec 2009 03:47:03 -, geremy condra debat...@gmail.com
wrote:



g = Graph(
   nodes={'a':{'colour':'red'},
  'b':{'colour':'white'},
  'c':{'colour':'blue'}},
   edges={('a', 'b'):{'name':'ab', 'weight':2},
  ('a', 'c'):{'name':'ac'},
  ('b', 'c'):{'name':'bc', 'style':'dotted'}}
)


That's OK for nodes, but for consistency with add_edges I would have
expected the name to be the optional third element of the key tuples.  
 It
works either way, but I can't help feel it's beginning to look a bit  
ugly.


I have to admit, I prefer it the other way, but patrick (our test guru  
and

chief bug squasher) likes your proposal better. I'm going to get in touch
with robbie tonight and see what he says. Since this is not a feature  
I'll

use much, if he agrees with you then I'll go ahead and implement the
change tonight and merge it back into mainline. If not, I'd appreciate
it if you'd take another look at it and figure out if its something you  
can

live with, or if theres another syntax you'd prefer, etc. Fair enough?


Fair enough.  Don't take my word as having much weight; I'm not likely to  
use graphs much for graph theory purposes (having skipped the topology  
courses in the Maths part of my degree), it just happens to be clearly the  
right datastructure for a project I'm fiddling with at home.


Here's a thought: are

  g.add_edge(a, b, ab)

and

  g.add_edge(a, b, name=ab)

equivalent?  If so, there's no reason not to have both forms of the  
initialiser.  If not, that weighs against having 'name' as a dictionary  
key.


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


Re: Recommendation for small, fast, Python based web server

2009-12-09 Thread python
Daniel,

 I'm using cherrypy for this purpose, actually together with turbogears 1.

My research has constantly pointed back to cherrypy as a tool of choice
for building local web servers. My initial impression was that cherrypy
was too big and complicated for my simple task. However, I'm going to
re-examine this assumption and take another look at cherrypy.

Thanks for your help!

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


Re: Graph library for Python

2009-12-09 Thread M.-A. Lemburg
geremy condra wrote:
 On Mon, Dec 7, 2009 at 6:28 PM, M.-A. Lemburg m...@egenix.com wrote:
  * Graph.__init__ should be able to take a list or set
   of nodes and edges as initializer

 The format of this will need to be thought all the way
 through before being implemented. To date, we haven't
 come up with anything completely satisfactory, but
 AFAIK everybody involved is still open to suggestions
 on this.

 I wasn't thinking of anything clever :-) ...

 g = Graph(
  [Node(a), Node(b), Node(c)],
  [Edge(Node(a), Node(b), ab),
   Edge(Node(a), Node(c), ac),
   Edge(Node(b), Node(c), bc),
  ])

 The main motivation here is to get lists, sets and dicts
 play nice together.
 
 Generally, we've tried to discourage people from instantiating
 nodes and edges directly, in favor of having them controlled
 through the graph. Maybe something along the lines of:
 
 g = Graph(nodes=['a', 'b', 'c'], edges=[('a', 'b'), ('a', 'c'), ('b', 'c')])
 
 ?

That would work as well, but you then miss out on the extra
parameters you can pass to Edge() and Node().

In another email you wrote that Edge() and Node() constructors
should not be used directly, since they have to know the Graph
to which they belong.

Wouldn't it be possible to implement this kind of parent-
referencing in a lazy way, ie. by postponing all the init-
processing until the Graph instance is known ?

  * Graph.__setitem__ could be mapped to .add_node() for
   convenience

 This may be a question of playing around with it. ATM I'm
 not sold on the idea, but I'll implement it and see how it
 turns out in practice.

 Thinking about it some more, I agree, it's not all that useful.

  * Graph.__length__ could be mapped to .size() for
   convenience

 We decided not to do this due to the ambiguity between
 whether .size() or .order() was the intended operation,
 and looking back I'm not sure that was entirely unjustified.
 Do you see there being any confusion on that score?

 There is an ambiguity here, indeed. My thinking was that
 the edges define the graph and can be mapped to a dictionary,
 e.g.

 d = {ab: (a, b),
 ac: (a, c),
 bc: (b, c)}

 len(d) == 3
 len(g) == 3

 A graph without edges is also what you typically call an empty
 graph, ie.

 if not g:
print 'empty'

 http://mathworld.wolfram.com/EmptyGraph.html

 Still, perhaps it's better to just not go down this route.
 
 I'd rather avoid it if possible, since there are many
 potential interpretations of this in different contexts.
 Again, I'm open to to the idea, but not convinced.
 
  * Graph.__iter__ could be mapped to an iterator using
   the fastest traversal method for the graph nodes
   (ie. order does not matter, it's only important that
   all nodes are found as fast as possible)

 Again, it seems ambiguous as to whether nodes or
 edges are the intended target here, and while the
 API can obviously dictate that, it seems a bit like
 a case of in the face of ambiguity, refuse the
 temptation to guess to me.

 Right, but sometimes practicalty beats purity ;-) We had
 the same situation for dictionaries and then decided that
 iteration over keys would be more natural than iterating over
 items (key, value) or values.

 It's also important to note that:

for n in g: print n

 and

n in g

 match up in terms of semantics.

 Since n in g already uses the node in graph approach,
 the for-loop should use the same logic.
 
 Beware, that doesn't just match nodes.
 
 g = Graph()
 g.add_node('a')
 g.add_node('b')
 g.add_edge('a', 'b', 'ab')
 'ab' in g # returns true

I'd avoid such an ambiguity. It could easily hide programming
errors (testing for edges instead of nodes).

OTOH, you could also regard the graph as a set of nodes
and edges (as you apparently already do). In that case,
you'd define

for x in g: print x

as iteration of both nodes and edges in some arbitrary
order and then use the more specific:

for n in g.nodes: print x
for e in g.edges: print x

for iteration over just the nodes or edges.

  * Graph could also benefit from some bulk update methods,
   e.g. to update weights on all edges or nodes by passing
   in a dictionary mapping names to attribute values

 Sounds good. Again, the format will need some careful
 thought, but you're right that this would improve it
 greatly.

 This is only an optimization, but could lead to some great
 performance improvements by avoiding function call overhead.
 
 Agreed.
 
  * Graph could benefit from some bulk query methods,
   such as .get_node_attributes() and .add_edges().

 I'm not sure how the first is different from the existing
 .data, what did you have in mind?

 The second one was a typo. It should have been
 .get_edge_attributes().

 In both cases I was thinking of being able to quickly
 extract a number of node/edge attributes, e.g.

 g.get_edge_attributes('weight', 'color')
 {ab: {weight: 3, color: blue},
  ac: {weight: 2, color: green},
  bc: {weight: 1, color: red}}

 Again, the idea is to 

Re: Graph library for Python

2009-12-09 Thread Patrick Laban
On Wed, Dec 9, 2009 at 4:02 PM, Rhodri James rho...@wildebst.demon.co.ukwrote:


 Here's a thought: are

  g.add_edge(a, b, ab)

 and

  g.add_edge(a, b, name=ab)

 equivalent?  If so, there's no reason not to have both forms of the
 initialiser.  If not, that weighs against having 'name' as a dictionary key.


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



Both methods are equivalent.

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


Re: Immediate Help with python program!

2009-12-09 Thread Daniel
On Dec 9, 6:18 pm, Jon Clements jon...@googlemail.com wrote:
 On Dec 9, 11:55 pm, Daniel dkeepe...@yahoo.com wrote:



  i am making a tic-tac-toe game using python. i am pretty new to it,
  but cant seem to figure this one out.
  Here is my code:

  X = X
  O = O
  empty =  
  tie = Tie
  squares = 9

  def display():
      print Welcome to Tic-Tac-Toe. Player will play against the
  computer.
              \nYou will move by typing in the number to the
  corresponding square below:

                    0 | 1 | 2
                  -
                    3 | 4 | 5
                  -
                    6 | 7 | 8 \n

  def select():
      question = ask(Do you want to go first? (y/n))
      if question == y:
          player = X
          computer = O
      else:
          computer = X
          player = O
      return computer, player

  def newBoard():
      board = []
      for i in range(squares):
          board.append(empty)
      return board

  def displayBoard(board):
      print \n\t, board[0], |, board[1], |, board[2]
      print \t, -
      print \t, board[3], |, board[4], |, board[5]
      print \t, -
      print \t, board[6], |, board[7], |, board[8], \n

  def boardMoves(board):
      moves = []
      for i in range(squares):
          if board[i] == empty:
              moves.append(i)
      return moves

  def findWinner(board):
      win = ((0, 1, 2),
             (3, 4, 5),
             (6, 7, 8),
             (0, 3, 6),
             (1, 4, 7),
             (2, 5, 8),
             (0, 4, 8),
             (2, 4, 6))

      for i in win:
          if board[i[0]] == board[i[1]] == board[i[2]] != empty:
              winner = board[i[0]]
              return winner
          if empty not in board:
              return tie
          return None

  def askMove(question, low, high):
      response = None
      while response not in range(low, high):
          response = int(raw_input(question))
      return response

  def playerMove(board, player):
      legal = boardMoves(board)
      move = None
      while move not in legal:
          move = askMove(Pick a number where you want to move(0-8):,
  0, squares)
          if move not in legal:
              print \nThat move is taken already. Pick another.
      return move

  def compMove(board, computer, player):
      board = board[:]
      strategy = (4, 0, 2, 6, 8, 1, 3, 5, 7)
      print Computer chooses:,

      # if computer can win, take that move
      for move in boardMoves(board):
          board[move] = computer
          if findWinner(board) == computer:
              print move
              return move
          board[move] = empty

      # if human can win, block that move
      for move in boardMoves(board):
          board[move] = player
          if findWinner(board) == player:
              print move
              return move
          board[move] = empty

      # If no one can win pick best open square
      for move in strategy:
          if move in boardMoves(board):
              print move
              return move

  def nextTurn(turn):
      if turn == X:
          return 0
      else:
          return X

  def gameWinner(winner, computer, player):
      if winner == computer:
          print Computer Wins. Better luck next time
      elif winner == player:
          print You win. Good job!
      elif winner == tie:
          print Tie game. Play again.

  def main():
      display()
      computer, player = select()
      turn = X
      board = newBoard()
      displayBoard(board)

      while not findWinner(board):
          if turn == player:
              move = playerMove(board, player)
              board[move] = player
          else:
              move = compMove(board, computer, player)
              board[move] = computer
          displayBoard(board)
          turn = nextTurn(turn)
      winner = findWinner(board)
      gameWinner(winner, computer, player)

  main()

  Here is my problem:
  If you hit 'n' at the beginning prompt, the computer does four moves
  automatically and wins- you can't input anything. If you hit 'y' at
  the beginning prompt, you can play but cannot win. I got three x's in
  a row and it didn't let me win. It just keeps letting
  you input numbers until the computer wins even if you have three in a
  row.

  If anyone can help please do asap.
  Thank you!

 Someone's homework assignment is overdue/due very soon? And, I don't
 believe for a second this is your code. In fact, just searching for
 (the obvious Java based) function names leads me to believe you've
 'butchered' it from Java code (do you not think your teacher/lecturer
 can do the same?).

 Someone might well help out, but I'd be surprised if you got a here's
 how to fix it response, as from my POV you haven't done any work.

 Of course, I'm occasionally wrong,

 Jon.

Just lookin for some help man. It is my code. but im here because i
needed help and i dont know whats wrong with 

Re: Immediate Help with python program!

2009-12-09 Thread Intchanter / Daniel Fackrell
On Dec 9, 5:18 pm, Jon Clements jon...@googlemail.com wrote:

snip original post

 Someone's homework assignment is overdue/due very soon? And, I don't
 believe for a second this is your code. In fact, just searching for
 (the obvious Java based) function names leads me to believe you've
 'butchered' it from Java code (do you not think your teacher/lecturer
 can do the same?).

 Someone might well help out, but I'd be surprised if you got a here's
 how to fix it response, as from my POV you haven't done any work.

 Of course, I'm occasionally wrong,

 Jon.

I also got the impression that it was a homework assignment.  I'll
just add that trying to bring attention to your request for help with
exclamation points and words that indicate urgency is pretty bad form
in a volunteer support community.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Graph library for Python

2009-12-09 Thread geremy condra
On Wed, Dec 9, 2009 at 7:02 PM, Rhodri James
rho...@wildebst.demon.co.uk wrote:
 On Wed, 09 Dec 2009 23:42:13 -, geremy condra debat...@gmail.com
 wrote:

 On Wed, Dec 9, 2009 at 6:04 PM, Rhodri James
 rho...@wildebst.demon.co.uk wrote:

 On Wed, 09 Dec 2009 03:47:03 -, geremy condra debat...@gmail.com
 wrote:

 g = Graph(
   nodes={'a':{'colour':'red'},
              'b':{'colour':'white'},
              'c':{'colour':'blue'}},
   edges={('a', 'b'):{'name':'ab', 'weight':2},
              ('a', 'c'):{'name':'ac'},
              ('b', 'c'):{'name':'bc', 'style':'dotted'}}
 )

 That's OK for nodes, but for consistency with add_edges I would have
 expected the name to be the optional third element of the key tuples.  It
 works either way, but I can't help feel it's beginning to look a bit
 ugly.

 I have to admit, I prefer it the other way, but patrick (our test guru and
 chief bug squasher) likes your proposal better. I'm going to get in touch
 with robbie tonight and see what he says. Since this is not a feature I'll
 use much, if he agrees with you then I'll go ahead and implement the
 change tonight and merge it back into mainline. If not, I'd appreciate
 it if you'd take another look at it and figure out if its something you
 can
 live with, or if theres another syntax you'd prefer, etc. Fair enough?

 Fair enough.  Don't take my word as having much weight; I'm not likely to
 use graphs much for graph theory purposes (having skipped the topology
 courses in the Maths part of my degree), it just happens to be clearly the
 right datastructure for a project I'm fiddling with at home.

 Here's a thought: are

  g.add_edge(a, b, ab)

 and

  g.add_edge(a, b, name=ab)

 equivalent?  If so, there's no reason not to have both forms of the
 initialiser.  If not, that weighs against having 'name' as a dictionary key.

You're right, of course- the obvious way to do this is just to apply
tuple unpacking to each element in the edges argument. I've applied
the change, although testing and documentation will need to be
updated before I can merge it back into mainline, and I doubt I'll
get to that tonight.

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


Re: Recommendation for small, fast, Python based web server

2009-12-09 Thread birdsong
On Dec 9, 4:05 pm, pyt...@bdurham.com wrote:
 Daniel,

  I'm using cherrypy for this purpose, actually together with turbogears 1.

 My research has constantly pointed back to cherrypy as a tool of choice
 for building local web servers. My initial impression was that cherrypy
 was too big and complicated for my simple task. However, I'm going to
 re-examine this assumption and take another look at cherrypy.

 Thanks for your help!

 Regards,
 Malcolm

tornado all the way, it is teh radness: http://www.tornadoweb.org/

epoll based python server.  fun to hack on. def check it out.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Immediate Help with python program!

2009-12-09 Thread Rhodri James
Ahem.  This is a newsgroup/mailing list, not IM.  I happen to have seen  
this within half an hour of you posting it, but that's just luck.   
Expecting an immediate response is unrealistic.


Furthermore, this is comp.lang.python, a group right up there in pedantry  
terms with cam.misc.  Wandering in and demanding immediate help is just  
begging for half a dozen replies that give you detailed and mind-boggling  
versions of exactly what you asked for, especially if it's got nothing to  
do with the answer you actually need.


So...

On Wed, 09 Dec 2009 23:55:01 -, Daniel dkeepe...@yahoo.com wrote:


i am making a tic-tac-toe game using python. i am pretty new to it,
but cant seem to figure this one out.
Here is my code:

X = X
O = O
empty =  
tie = Tie
squares = 9


There's a convention (PEP 8, why not go to www.python.org and read it?)  
that constants should be given names in CAPITAL_LETTERS_AND_UNDERSCORES so  
that you can tell at a glance that they aren't supposed to be changed.   
PEP 8 also has things to say about function names.


[snip rest of program]


Here is my problem:
If you hit 'n' at the beginning prompt, the computer does four moves
automatically and wins- you can't input anything.


Sounds like you have a bug in your end-of-turn function.  To be fair, it  
took me a couple of minutes to be sure I'd spotted this correctly.



If you hit 'y' at
the beginning prompt, you can play but cannot win. I got three x's in
a row and it didn't let me win. It just keeps letting
you input numbers until the computer wins even if you have three in a
row.


Sounds like you have a bug in your check-for-a-win function.  That should  
be all the hint you need.


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


web crawler in python

2009-12-09 Thread my name
I'm currently planning on writing a web crawler in python but have a
question as far as how I should design it. My goal is speed and maximum
efficient use of the hardware\bandwidth I have available.

As of now I have a Dual 2.4ghz xeon box, 4gb ram, 500gb sata and a 20mbps
bandwidth cap (for now) . Running FreeBSD.

What would be the best way to design the crawler? Using the thread module?
Would I be able to max out this connection with the hardware listed above
using python threads?

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


Re: Graph library for Python

2009-12-09 Thread geremy condra
 Generally, we've tried to discourage people from instantiating
 nodes and edges directly, in favor of having them controlled
 through the graph. Maybe something along the lines of:

 g = Graph(nodes=['a', 'b', 'c'], edges=[('a', 'b'), ('a', 'c'), ('b', 'c')])

 ?

 That would work as well, but you then miss out on the extra
 parameters you can pass to Edge() and Node().

Just pushed a change that will allow that.

 In another email you wrote that Edge() and Node() constructors
 should not be used directly, since they have to know the Graph
 to which they belong.

 Wouldn't it be possible to implement this kind of parent-
 referencing in a lazy way, ie. by postponing all the init-
 processing until the Graph instance is known ?

While possible, I'm wary of this. We tried it in an initial
prototype (all nodes and edges descended naturally from
a Universe graph until otherwise stated) and that way lie
madness. Perhaps at some point someone would like to
give another shot at it, though.

 Beware, that doesn't just match nodes.

 g = Graph()
 g.add_node('a')
 g.add_node('b')
 g.add_edge('a', 'b', 'ab')
 'ab' in g # returns true

 I'd avoid such an ambiguity. It could easily hide programming
 errors (testing for edges instead of nodes).

 OTOH, you could also regard the graph as a set of nodes
 and edges (as you apparently already do). In that case,
 you'd define

 for x in g: print x

 as iteration of both nodes and edges in some arbitrary
 order and then use the more specific:

 for n in g.nodes: print x
 for e in g.edges: print x

 for iteration over just the nodes or edges.

Yup, that's exactly what we do.

 g.get_edge_attributes('weight', 'color')
 {ab: {weight: 3, color: blue},
  ac: {weight: 2, color: green},
  bc: {weight: 1, color: red}}

 Again, the idea is to reduce call overhead and later
 on be able to move these lookups to C.

 Entirely doable, but I'm not sure I see the use case.
 Would you mind providing a more realistic example?

 The idea is that you use the Graph representation of the
 data to implement some algorithm e.g. optimizing weights
 for example.

 The algorithm could be implemented in C to be fast enough
 for large data sets.

 The above two methods would then be used to quickly push the
 original data into the graph and extract it again after
 the algorithm has run.

I can see this, but I think the cleaner way is just to iterate
over nodes and edges. Having said that, if somebody wants
to come up with some timing data and it seems to provide a
big advantage, I think robbie for one would make the change.

 Hmm. Sounds like a plausible use case to me, although I'm
 not sure its one that should be encouraged. The bigger
 question in my mind is whether all attribute lookups should
 have to pay the extra lookup cost to support a somewhat
 narrow need. I'll definitely have to talk with the other devs
 about this one, and run a little bit of timing besides.

 This would only be an optional second way of accessing
 the .data dictionary.

 node.data['pass'] = 1
 node.data['from'] = 'Las Vegas'
 node.data['to'] = 'New York'

 would work without such modifications.

Yes, but the change is not reflected on the node. For
example:

 g = Graph(nodes={'a':{'spotted':True}})
 g['a'].data['spotted'] = False
 g['a'].data['spotted']
True

I can see how this would represent a gotcha, though.
Is there a general opinion on whether this is a plus or
a minus?

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


Re: switch

2009-12-09 Thread Asun Friere

On Dec 9, 5:39 pm, Steven D'Aprano
ste...@remove.this.cybersource.com.au wrote:
 On Tue, 08 Dec 2009 21:36:23 -0800, Asun Friere wrote:
  On Dec 9, 4:02 pm, Kee Nethery k...@kagi.com wrote:
  I string together a bunch of elif statements to simulate a switch

  if foo == True:
          blah
  elif bar == True:
          blah blah
  elif bar == False:
          blarg
  elif 

  This code is probably symptomatic of poor design. (Not to mention that
  your condition tests).  For which reason python has no 'case' statement
  and why no decent OO language should.

 That's a provocative statement.


My reply was lost in the aether, so here goes again.

If it's provocative, it is at least hedged.  It is merely symptomatic
and only probably so, because there are numerous instances where case
logic is the only sensible solution.  I'm not advocating some cargo-
cult rule for the elimination of all uses of elif.  If I were I would
rightly be presented with numerous code examples where a switch is a
sensible option, much as happens when someone pronounces against the
humble goto statement.


  It is a principle of OO design that an object should know what to do
  itself.  Rather running an object though a series of tests, it is
  better to send the object a message, relying on polymorphism or duck-
  typing, and deal with any exceptions thrown.

 Perhaps that's true, but you'll note that the example given above doesn't
 run a single object through a series of tests, but runs a series of tests
 on DIFFERENT objects, to find the first which matches.


Well actually two objects with one being tested twice.  But you are
right, I was being sloppy when I wrote running an object especially
in light of the fact that the following clause makes more sense when
run against objects of potentially different class.  Same for dispatch
mechanisms of course.

What I'm saying is that when you find a large if/elif/else in your
code, regard it with suspicion and consider whether better design
might not eliminate it.  And I'm speaking as someone who still has to
maintain some code (in perl not python) which has an if/elif/else
statement spanning 5 A4 pages.  What's worse, I was the one who did
this to myself some 8 years ago.

What I'm also saying is learn about dispatch mechanisms, they are
about the most useful patterns out there (next to the State pattern).
As a matter of practice I have found that more often than not, large
case statements can better be solved using double-dispatch.  Obviously
not all.  Obviously!

 But putting that aside, I find myself wondering how you would deal with
 the following switch-like series of tests.

 def print_grades(score):
     if not 0 = score = 100:
         raise ValueError(score must be between 0 and 100)
     if score  50:
         print You have failed.
         consider_suspension()
     elif score == 50:
         print You have just passed by the skin of your teeth.
     elif score  60:
         print You have scored a D. You need to try harder.
     elif score  70:
         print You have scored a C.
     elif score  80:
         print You have scored a B. Well done.
     elif score  100:
         print Congratulations, you have scored an A.
     else:
         assert score == 100
         print You have scored a PERFECT 100% SCORE!!!
         if not evidence_of_cheating():
             call_newspapers()

 Obviously that could, with a non-trivial amount of work, be turned into a
 dictionary dispatch, but is the benefit worth the extra effort?


Probably not.  Depending on the nature of the app, I'd probably be
calling score.print_grades() and using cutoff values of 85, 75, 60 and
50 (perhaps not even hardcoded into the logic), but sure this is a
fine example of a place where a solution other than a simple switch
would be overkill.  As such this example would be a good counter to
the absolute repudiation of case logic I did not make.  I doubt,
however, that it is of great pedagogic value in alerting programmers
to the design options available to them in overcomming what the
perceive as a lack in the language.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Immediate Help with python program!

2009-12-09 Thread MRAB

Daniel wrote:

i am making a tic-tac-toe game using python. i am pretty new to it,
but cant seem to figure this one out.
Here is my code:


[snip]
You problem is due to your choice of variable names, because '0' looks
too much like 'O'.

You also don't define 'ask'.
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >