file.read() doesn't read the whole file

2009-03-19 Thread Sreejith K
Hi,

>>> snapdir = './mango.txt_snaps'
>>> snap_cnt = 1
>>> block = 0
>>> import os
>>> os.chdir('/mnt/gfs_local')
>>> snap = open(snapdir + '/snap%s/%s' % (repr(snap_cnt), repr(block)),'r')
>>> snap.read()
'dfdfdgagdfgdf\ngdgfadgagadg\nagafg\n\nfs\nf\nsadf\n\nsdfsdfsadf\n'
>>> snapdir + '/snap%s/%s' % (repr(snap_cnt), repr(block))
'./mango.txt_snaps/snap1/0'

The above code works fine and it reads the whole file till EOF. But
when this method is used in a different scenario the file is not read
completely. I'll post the code that read only some part of the file...

self.snap = open(self.snapdir + '/snap%d/%d' % (self.snap_cnt,
block),'r') ## opens /mnt/gfs_local/mango.txt_snaps/snap1/0
self.snap.seek(off%4096) ## seeks to 0 in this case
bend = 4096-(off%4096) ## 4096 in this case
if length-bend <= 0:## true in this case as length is 4096
tf.writelines("returned \n")
data = self.snap.read(length)
self.snap.close()
break

the output data is supposed to read the whole fie but it only reads a
part of it. Why is it encountering an early EOF ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python + PostgreSQL

2009-03-19 Thread Martin v. Löwis
> I saw in a different post that psycopg2 does work on Python 3.x as
> long as a patch is applied (by Martin v. Löwis):
> 
[...]
> Do you know where can I find this patch

It's linked in

http://wiki.python.org/moin/Early2to3Migrations

and lives in

http://www.dcl.hpi.uni-potsdam.de/home/loewis/psycopg_3k_v2.diff
(or perhaps a later version should I need to apply more fixes)

> and if it does fully solve
> any incompatibility issues to be able to use Python 3.x without
> problems?.

I think it should resolve all issues for psycopg2. If you find issues,
I would certainly like to know.

Of course, it does not magically resolve *all* problems that you
might have with Python 3.x.

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


Re: Unicode problem in ucs4

2009-03-19 Thread Martin v. Löwis
> Any idea on why this is happening? 

Can you provide a complete example? Your code looks correct, and should
just work.

How do you know the result contains only 't' (i.e. how do you know it
does not contain 'e', 's', 't')?

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


Re: Threads not Improving Performance in Program

2009-03-19 Thread Vijayendra Bapte
On Mar 20, 4:21 am, Tim Rowe  wrote:
> > Thank you for your response. I did not realize that. That seems like a
> > huge limitation for such a great language.
> > I will look into forking off processes instead of using threads.
>
> If that's what you need to do, yes it is. If it isn't, no it's not. No
> language is optimum for all possible applications, each one has
> different compromises. You've just discovered one of Python's.
>
> --
> Tim Rowe

try this: 
http://docs.python.org/library/multiprocessing.html#module-multiprocessing

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


Re: improve this newbie code/nested functions in Python?

2009-03-19 Thread Terry Reedy

Esmail wrote:

Hi,

I'm new to writing Python code. This is a simple client I wrote, it
works, but I feel it doesn't look as clean as it could. Can anyone
make suggestions how to streamline this code?

Also, I am using two nested functions, it seems that nested functions
aren't used that much in Python - is that correct? And if so, how
come?


What you wrote are two nested classes, not functions.  In my opinion, 
neither should be nested.  Nothing is gained and something is lost. 
Neither are used by client; indeed both use client.


Nested classes are rare because they have little use and are almost 
never necessary.


Nested functions are a different story.

I would rename '_parent' (misleading) as 'client' or 'user'.

Terry Jan Reedy



ps: I realize there is minimal error checking/exception handling.

#!/usr/bin/env python


import sys
from socket import *
from threading import Thread


class Client(object):
def __init__(self, host="localhost", port=, name = "esmail"):
self._host = host
self._port = port
self._name = name
self._address=(self._host, self._port)
self._sock=socket(AF_INET, SOCK_STREAM)
self._sock.connect(self._address)
self._parent = self


Makes no sense in the code given.


self._keepGoing = True

def info(self):
return self._host, self._port, self._name



class Listener(Thread):
   def __init__(self, parent, tname="listener"):
   Thread.__init__(self,name = tname)
   self._parent = parent
   print self._parent._host

   def run(self):

   while self._parent._keepGoing:
   m = self._parent._sock.recvfrom(1024)
   print m[0]



class Speaker(Thread):
   def __init__(self, parent, tname = "speaker"):
   Thread.__init__(self,name = tname)
   self._parent = parent
   self._parent._sock.send(self._parent._name + "\n")


   def run(self):

   while(True):
   m = raw_input("-> ")
   if m == "bye":
   self._parent._sock.send(self._parent._name + " is
signing off.\n")
   self._parent._sock.send("bye\n")
   self._parent._keepGoing = False
   break;
   else:
   self._parent._sock.send(m + "\n")


def main():

if len(sys.argv) == 4:  # prog name + 3 args
host = sys.argv[1]
port = int(sys.argv[2])
name = sys.argv[3]
c = Client(host, port, name)
else:
c = Client()

print "Client connecting to - host=%s  port=%d   name=%s" % c.info
()

s = Client.Speaker(c)
s.start()

l = Client.Listener(c)
l.start()


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



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


Re: Object System

2009-03-19 Thread Terry Reedy

John Mendelewski wrote:

I was wondering if anyone had documents or articles what gave an in-
depth view of the object system in Python.

http://effbot.org/zone/python-objects.htm

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


Re: [python-list] Re: Strange crash issue on Windows w/ PyGTK, Cairo...

2009-03-19 Thread CJ Kucera
CJ Kucera wrote:
> Okay, I've got a reproducible testcase of this available up here:
> http://apocalyptech.com/pygtk-zlib/

Hello, two brief notes here:

1) Someone on the PyGTK list mentioned that I should really be using
StringIO instead of my own hacky attempt at one, in there, and of course
he was right.  Replacing my class with StringIO doesn't result in any
changed behavior, at least, so I can definitely rule out any weirdness
that my own file-like class could have introduced.  I've uploaded a new
version of run.py to the above URL which includes that change.

2) It looks like specifying "bufsize" isn't actually the totally magic
bullet for this.  I had put in that fix to my application, so it was
using bufsize, and things seemed to be working all right, but after four
or five data loads, I got that Windows crash again, regardless.  The fix
does make the application stable enough that I'm not that worried about
it, at least, but it doesn't seem to have totally bypassed whatever bug
I'm running into.

-CJ

-- 
WOW: Flemmy|   "Happiness isn't good enough for me!  I
p...@apocalyptech.com   |  demand euphoria!"
24.24.2.3171   |  - Calvin
--
http://mail.python.org/mailman/listinfo/python-list


multiprocessing and Tk GUI program (won't work under Linux)

2009-03-19 Thread akineko
Hello everyone,

I have started using multiprocessing module, which is now available
with Python 2.6.
It definitely opens up new possibilities.

Now, I developed a small GUI package, which is to be used from other
programs.
It uses multiprocessing and Pipes are used to pump image data/command
to the GUI process.
(I used multiprocessing because I got stack size problem if I used
threading)

It works great under Solaris environment, which is my primary
development environment.

When I tried the program under Linux (CentOS5), the program didn't
work (it hung).
My other programs that use multiprocessing work flawlessly under both
Solaris and Linux.

To investigate this problem, I create a much simpler test program. The
test program uses only basic necessary codes, nothing else. But my
simple test program still exhibits the same problem.

My test program display a GUI Button using three possible approaches:

(1) multiprocessing   (Solaris - okay, Linux - hung)
(2) threading(Solaris - okay, Linux - okay)
(3) none (main thread) (Solaris - okay, Linux - okay)

Is this a bug in a multiprocessing package? Or, I overlooked
something?

Any comments on resolving this problem will be greatly appreciated.

The attached is my test program (sorry for posting a long program).

Thank you!
Aki Niimura


#!/usr/bin/env python

import sys, os
import time
import threading
import multiprocessing

from Tkinter import *

###
### class Panel
###

class Panel:

def __init__(self, subp='multip'):
if subp == 'multip':
print 'multiprocessing module to handle'
# GUI process
self.process1 = multiprocessing.Process(target=self.draw)
self.process1.start()
elif subp == 'thread':
print 'threading module to handle'
# GUI thread
self.thread1 = threading.Thread(target=self.draw)
self.thread1.start()
#   self.thread1.setDaemon(1)
else:
print 'main thread to handle'
pass

def draw(self):
self.root = Tk()
w = Button(self.root, text='Exit', command=self.root.quit)
w.pack()
self.root.mainloop()

###
### Main routine
###

def main():
subp = 'multip'
if len(sys.argv) >= 2:
if not sys.argv[1] in ['multip', 'thread', 'none',]:
print 'Invalid option: %s' % sys.argv[1]
print "Valid options are 'multip', 'thread', 'none'"
sys.exit(1)
else:
subp = sys.argv[1]
panel = Panel(subp)
if subp == 'none':
panel.draw()
while 1:
time.sleep(1)
pass


if __name__ == '__main__':
main()
--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a python extension that works with multiprocessing.Queue

2009-03-19 Thread Gabriel Genellina
En Fri, 20 Mar 2009 00:16:29 -0200, Travis Miller   
escribió:



So far the C Api is really cool.  I can do all the
math stuff where I need the speed, and still be able to use python
which is way more expressive.


Sure! One gets the best of both worlds that way.

--
Gabriel Genellina

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


Re: Object System

2009-03-19 Thread Michele Simionato
On Mar 19, 10:18 pm, John Mendelewski 
wrote:
> I was wondering if anyone had documents or articles what gave an in-
> depth view of the object system in Python. Ones concerning dispatch,
> how self really works, and maybe some meta-programming that comes
> along with the new style classes.

The second Google hit for "python object system":

http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html

This is actually a very good book indeed.

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


EasyGui now supports Python 3.0

2009-03-19 Thread Steve Ferg
Just a follow-up:

I've just uploaded a version of Easygui that works with Python 2.x and
3.x.
http://easygui.sourceforge.net/current_version/index.html

I blog a bit about it at
http://pythonconquerstheuniverse.blogspot.com/2009/03/moving-to-python-30-part3.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can I rely on...

2009-03-19 Thread alex23
On Mar 20, 1:42 am, "Emanuele D'Arrigo"  wrote:
> I just had a bit of a shiver for something I'm doing often in my code
> but that might be based on a wrong assumption on my part. Take the
> following code:
>
> pattern = "aPattern"
>
> compiledPatterns = [ ]
> compiledPatterns.append(re.compile(pattern))
>
> if(re.compile(pattern) in compiledPatterns):
>     print("The compiled pattern is stored.")

Others have discussed the problem with relying on the compiled RE
objects being the same, but one option may be to use a dict instead of
a list and matching on the pattern string itself:

compiledPatterns = { }
if pattern not in compiledPatterns:
compiledPatterns[pattern] = re.compile(pattern)
else:
print("The compiled pattern is stored.")
--
http://mail.python.org/mailman/listinfo/python-list


improve this newbie code/nested functions in Python?

2009-03-19 Thread Esmail
Hi,

I'm new to writing Python code. This is a simple client I wrote, it
works, but I feel it doesn't look as clean as it could. Can anyone
make suggestions how to streamline this code?

Also, I am using two nested functions, it seems that nested functions
aren't used that much in Python - is that correct? And if so, how
come?

thanks,

Esmail

ps: I realize there is minimal error checking/exception handling.

#!/usr/bin/env python


import sys
from socket import *
from threading import Thread


class Client(object):
def __init__(self, host="localhost", port=, name = "esmail"):
self._host = host
self._port = port
self._name = name
self._address=(self._host, self._port)
self._sock=socket(AF_INET, SOCK_STREAM)
self._sock.connect(self._address)
self._parent = self
self._keepGoing = True

def info(self):
return self._host, self._port, self._name



class Listener(Thread):
   def __init__(self, parent, tname="listener"):
   Thread.__init__(self,name = tname)
   self._parent = parent
   print self._parent._host

   def run(self):

   while self._parent._keepGoing:
   m = self._parent._sock.recvfrom(1024)
   print m[0]



class Speaker(Thread):
   def __init__(self, parent, tname = "speaker"):
   Thread.__init__(self,name = tname)
   self._parent = parent
   self._parent._sock.send(self._parent._name + "\n")


   def run(self):

   while(True):
   m = raw_input("-> ")
   if m == "bye":
   self._parent._sock.send(self._parent._name + " is
signing off.\n")
   self._parent._sock.send("bye\n")
   self._parent._keepGoing = False
   break;
   else:
   self._parent._sock.send(m + "\n")


def main():

if len(sys.argv) == 4:  # prog name + 3 args
host = sys.argv[1]
port = int(sys.argv[2])
name = sys.argv[3]
c = Client(host, port, name)
else:
c = Client()

print "Client connecting to - host=%s  port=%d   name=%s" % c.info
()

s = Client.Speaker(c)
s.start()

l = Client.Listener(c)
l.start()


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


Re: Creating a python extension that works with multiprocessing.Queue

2009-03-19 Thread Travis Miller
I'm on linux actually.  I found that so long as I implement
__reduce__, then pickling works, and my extension works with Queue
objects correctly.  So far the C Api is really cool.  I can do all the
math stuff where I need the speed, and still be able to use python
which is way more expressive.

travis

On Mar 17, 8:43 pm, "Gabriel Genellina" 
wrote:
> En Sun, 15 Mar 2009 01:51:35 -0200, Travis Miller   
> escribió:
>
> > I am very new to the python C API, and have written a simple type
> > called SU2 that has 4 members that are all doubles.  Everything seems
> > to work fine (can import my module and instantiate the new type and
> > act on it with various methods I have defined), except for when I
> > attempt to use my new type with multiprocessing.Queue (by the way I am
> > working with python 2.6).
>
> > So when I declare my object instance and call the put() method on the
> > Queue instance, the object I get out with the get() method of the same
> > Queue instance is not the same object.   The four members of the
> > object are all coming through as all zeros.  Below is the snippet of
> > code.
>
> Are you on Windows? multiprocessing uses pickles to transfer objects  
> between processes. See if you can dump and load those kind of objects. If  
> not, you may need to implement __getstate__/__setstate__ or __reduce__
> Seehttp://docs.python.org/library/pickle.html
>
> --
> Gabriel Genellina

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


Re: REDIRECT

2009-03-19 Thread gaeasiankom
On Mar 19, 11:39 am, I V  wrote:
> On Wed, 18 Mar 2009 21:30:59 -0700, gaeasiankom wrote:
> > What actually I'm try to do is :
>
> > I'm having a Login page which developed in HTML. When I click on the
> > "Login" button I want the page to validate (at datastore of google app)
> > using python and redirect to other HTML page. As what I understand,
> > Python is the only language that supported by the GoogleApps.
>
> The webapp framework that comes with the Google Apps SDK has a function
> for redirecting:
>
> http://code.google.com/appengine/docs/python/tools/webapp/redirects.html


Thanks again. I went through that page earlier but I'm not clear with
the procedures.

Sample that I did :

class FormHandler(webapp.RequestHandler):
  def post(self):
  if processFormData(self.request):
  self.redirect("/test.html")
  else:
  print 'Hello'

I failed to redirect. Error :

INFO 2009-03-20 00:21:16,573 appengine_rpc.py] Server:
appengine.google.com
WARNING  2009-03-20 00:21:16,671 dev_appserver.py] Could not
initialize images API; you are likely missing the Python "PIL" module.
ImportError: No module named _imaging
INFO 2009-03-20 00:21:16,700 dev_appserver_main.py] Running
application pythonredirect on port 9200: http://localhost:9200
ERROR2009-03-20 00:21:28,891 dev_appserver.py] Exception
encountered handling request
Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 2711, in _HandleRequest
base_env_dict=env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 380, in Dispatch
base_env_dict=base_env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 1998, in Dispatch
self._module_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 1916, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 1812, in ExecuteOrImportScript
exec module_code in script_module.__dict__
  File "E:\J\Python\WorkSpace 01\pythonredirect\src\redirt.py", line
4, in 
self.redirect("/home")
NameError: name 'self' is not defined
INFO 2009-03-20 00:21:28,913 dev_appserver.py] "GET / HTTP/1.1"
500 -
INFO 2009-03-20 00:21:29,276 dev_appserver.py] "GET /favicon.ico
HTTP/1.1" 404 -
INFO 2009-03-20 00:21:29,282 dev_appserver_index.py] Updating E:\J
\Python\WorkSpace 01\pythonredirect\src\index.yaml
ERROR2009-03-20 00:22:04,434 dev_appserver.py] Exception
encountered handling request
Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 2711, in _HandleRequest
base_env_dict=env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 380, in Dispatch
base_env_dict=base_env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 1998, in Dispatch
self._module_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 1916, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 1812, in ExecuteOrImportScript
exec module_code in script_module.__dict__
  File "E:\J\Python\WorkSpace 01\pythonredirect\src\redirt.py", line
4, in 
self.redirect("/test.html")
NameError: name 'self' is not defined
INFO 2009-03-20 00:22:04,436 dev_appserver.py] "GET / HTTP/1.1"
500 -
INFO 2009-03-20 00:24:00,278 dev_appserver.py] "GET / HTTP/1.1"
200 -
INFO 2009-03-20 00:24:00,326 dev_appserver.py] "GET /favicon.ico
HTTP/1.1" 404 -
ERROR2009-03-20 00:24:48,917 dev_appserver.py] Encountered error
loading module "redirt": :
expected an indented block (redirt.py, line 6)
Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 1736, in LoadTargetModule
module_code = import_hook.get_code(module_fullname)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 982, in decorate
return func(self, *args, **kwargs)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_appserver.py", line 1612, in get_code
return compile(source_code, full_path, 'exec')
  File "E:\J\Python\WorkSpace 01\pythonredirect\src\redirt.py", line 6
 # Display the form, possibly with error messages.

^
 IndentationError: expected an indented block
ERROR2009-03-20 00:24:48,917 dev_appserver.py] Parent package
initialization files are present, but must be broken
ERROR2009-03-20 00:24:48,933 dev_appserver.py] Exception
encountered handling request
Traceback (most recent call las

Concrete Factory Pattern syntax?

2009-03-19 Thread R. David Murray
Austin Schutz  wrote:
> 
> I have a fairly simple bit of code, something like:
> 
> # This should be importing the subclasses somehow, so that the factory
> # can make them.
> # import Parser.One
> # import Parser.Two
> # or.. from Parser import *?
> class Parser():
>def parse:
>   'Implemented only in subclass'

Hmm.  You need to go back to the tutorial, I think :)

This should be

def parse(self):
raise NotImplementedError

The raise NotImplementedError is the pythonic way of indicating
unimplemented methods in a superclass.  But also take a look
at ABCs (Abstract Base Classes), which provide some nice sugar
for this kind of thing.

>def make_parser(which_parser):
>if(which_parser = 'one'):
>  return One()
>else:
>   return Two()

Skip this, I'll rewrite it later.

> # import Parser?
> class One(Parser):
>def parse:
>'one implementation'

Again, 'def parse(self):'.  Might want to make the body something
like "print 'one implementation'" so you can tell if you got
the correct parser when you test it.

> class Two(Parser):
>def parse:
>'another implementation'

Same comments as above.

> The problem I have is that I don't understand how to put this into
> actual files in actual directories and have the interpreter do
> something actually useful :-) . What I would like to do is something
> like:
> 
> lib/
>   Parser.py
>   Parser/

Well, there's your first import mistake :).  You can't have a module
and a package both with the same name, nor do you need to.  So drop the
'Parser.py'.  And rename the directory 'parser'; module and package
names are lower case according to the Python style guide.

>   __init__.py (maybe?)

Yes.  Required if you are going to have a package you can import
things from.

>   One.py
>   Two.py

Again, rename these to 'one.py' and 'two.py'.  Then in your
__init__.py file, do:

from one import One
from two import Two

Now, in your code that uses the parser, do:

from parser import One as Parser

or

from parser import Two as Parser

depending on which parser you want.

Unless your parsers are really heavy, you could probably just
keep all this stuff in one file, parser.py...but if they are complex
enough you want to split the source up, and you want a base class
or ABC, then put that in another file (parserbase.py, perhaps),
and at the top of each of the individual parser files do

from parserbase import ParserBase

(or some such name).

Now, if you really need the parser to instantiate to be chosen at run
time via a string, you could add something like this to your __init__.py:

def parserFactory(which_parser):
return globals()[which_parser.capitalize()]()

which will look up the capitalized version of the string (eg: 'One')
in the __init__.py module's global namespace, thus picking up the
class, and then calls it to create an instance, which is then
returned.

Then your code that uses this can do:

from parser import parserFactory

myparser = parserFactory('one')


>From your class heavy patterns I am guessing you are coming from
Java or some similar languageyou don't have to work as hard
to get things done in Python.

--
R. David Murray   http://www.bitdance.com

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


Re: read web page that requires javascript on client

2009-03-19 Thread Greg
On Mar 18, 7:25 pm, Carl  wrote:
> On Mar 18, 1:56 pm, a...@pythoncraft.com (Aahz) wrote:
>
>
>
> > In article ,
> > R. David Murray  wrote:
>
> > >That said, I've heard mention here of something that can apparently be
> > >used for this.  I think it was some incarnation of Webkit.  I remember
> > >someone saying you wanted to use the one with, I think it was GTK
> > >bindings, even though you were dealing with just network IO.  But I don't
> > >remember clearly and did not record the reference.  Perhaps the person
> > >who posted that info will answer you, or you will be able to figure out
> > >from these clues.  Unfortunately I'm not 100% sure it was Webkit.
>
> > By the power of Gooja!
>
> >http://groups.google.com/group/comp.lang.python/msg/aed53725885a9250
> > --
> > Aahz (a...@pythoncraft.com)           <*>        http://www.pythoncraft.com/
>
> > "Programming language design is not a rational science. Most reasoning
> > about it is at best rationalization of gut feelings, and at worst plain
> > wrong."  --GvR, python-ideas, 2009-3-1
>
> Probably the easiest thing is to actually use a browser. There are
> many examples of automating a browser via Python. So, you can
> programmatically launch the browser, point it to the JavaScript
> afflicted page, let the JS run and grab the page source. As an added
> bonus you can later interact with the page by programatically, filling
> form fields, selecting options from lists and clicking buttons.
>
> HTH, Carl

Selenium. It's not pretty for what I want to do but it works ... then
again, what I need to do is not pretty either.
Ciao,
Greg
--
http://mail.python.org/mailman/listinfo/python-list


Re: Concrete Factory Pattern syntax?

2009-03-19 Thread Benjamin Kaplan
On Thu, Mar 19, 2009 at 6:52 PM, Austin Schutz  wrote:

>
> I have a fairly simple bit of code, something like:
>
> # This should be importing the subclasses somehow, so that the factory
> # can make them.
> # import Parser.One
> # import Parser.Two
> # or.. from Parser import *?
> class Parser():
>   def parse:
>'Implemented only in subclass'
>
>   def make_parser(which_parser):
>   if(which_parser = 'one'):
> return One()
>   else:
>  return Two()
>
> # import Parser?
> class One(Parser):
>   def parse:
>   'one implementation'
>
> class Two(Parser):
>   def parse:
>   'another implementation'
>
> The problem I have is that I don't understand how to put this into
> actual files in actual directories and have the interpreter do
> something actually useful :-) . What I would like to do is something
> like:
>
> lib/
>  Parser.py
>  Parser/
>  __init__.py (maybe?)
>  One.py
>  Two.py
>
> But I'm not clear on how to structure the import statements. I'm a bit
> of a newb wrt python, and I get any number of different errors depending
> on how I arrange the import statements, everything from
>
> AttributeError: 'module' object has no attribute 'make_parser'
> to
> ImportError: cannot import name
> to
> TypeError: Error when calling the metaclass bases
>
> depending on how I use import. Nothing seems to be the correct
> combination. Any help would be much appreciated!
>
> Austin
>
>
This seems to be a good use for a metaclass, though it's tricky to work
around the fact that the new class doesn't exist when the metaclass is
called.To get around this, I have the metaclass add the new Parser to a
"to-do" list.  The metaclass will be called whenever a subclass is created,
so every class that subclasses parser will automatically be added.

BTW, don't create a class and a package with the same name. When you import
Parser, python won't know whether you want Parser.py or Parser/__init__.py.

I'm not very good with metaclasses, so there might be a couple mistakes in
here. Note that this won't work if you run this as the main script because
then you end up with two copies of Parser
(Parser.Parser and __main__.Parser).



class ParserMeta(type) : #the metaclass is called when the class is created
def __new__(cls, *args) :
#args = (name, bases, dict). dict has __module__ in it.
#we don't want to add the base class
if args[0] != "Parser"  :
Parser._not_loaded_parsers.append(args)
return type.__new__(cls, *args)


#note that this is for Python 3.
#for python 2.x do:
#class Parser(object) :
#__metaclass__ = ParserMeta
class Parser(metaclass=ParserMeta) :
_not_loaded_parsers = [] #the parsers that aren't loaded yet
parsers = {} #the list of parsers- names mapped to the class.
def parse(self) :
raise NotImplementedError

@classmethod
def load(cls) :
""" This method will add the parsers to the dictionary if they aren't
already in there"""
while cls._not_loaded_parsers :
new_parser = cls._not_loaded_parsers.pop()
mod = __import__(new_parser[2]['__module__'], fromlist=['*'])
cls.parsers[new_parser[0]] = mod.__dict__[new_parser[0]]

@classmethod
def make_parser(cls, which_one) :
"""loads the parsers if necessary and returns the parsers"""
cls.load()
return cls.parsers[which_one]()


import modules-that-contain-other-parsers

Parser.load()



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


Re: Object System

2009-03-19 Thread Benjamin Peterson
John Mendelewski  gmail.com> writes:
> 
> What goes on behind the scenes to make a.do() evaluate the do
> method with a bound to self in that method? Is this implemented
> in C, Python? I think I have a grasp of how to use objects, but
> I was wondering about the implementation I suppose.

This is implemented using descriptors. See
http://users.rcn.com/python/download/Descriptor.htm for a good overview. 




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


Re: Need guidelines to show results of a process

2009-03-19 Thread MRAB

Vizcayno wrote:

Hi:
I wrote a Python program which, during execution, shows me messages on
console indicating at every moment the time and steps being performed
so I can have a 'log online' and guess remaining time for termination,
I used many 'print' instructions to show those messages, i.e.  print
"I am in step 4 at "+giveTime()  print "I am in step 5 at
"+giveTime(), etc.
Now I need to execute the same program but from a GUI application. I
must show the same messages but on a "text field".
As you can guess, it implies the changing of my program or make a copy
and replace the print instructions by   textField += "I am in step 4
at "+giveTime() then textField += "I am in step 5 at "+giveTime(),
etc.
I wanted to do the next:
if output == "GUI":
textField += "I am in step 4 at "+giveTime()
force_output()
else:
print "I am in step 4 at "+giveTime()
But it is not smart, elegant, clean ... isn't it?
Any ideas please?
Regards.


At the very least you could put the reporting into a function:

def report(message):
if output == "GUI":
textField += message
force_output()
else:
print message

...
report("I am in step 4 at " + giveTime())

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


Re: Need guidelines to show results of a process

2009-03-19 Thread Mensanator
On Mar 19, 7:00 pm, Vizcayno  wrote:
> Hi:
> I wrote a Python program which, during execution, shows me messages on
> console indicating at every moment the time and steps being performed
> so I can have a 'log online' and guess remaining time for termination,
> I used many 'print' instructions to show those messages, i.e.  print
> "I am in step 4 at "+giveTime()  print "I am in step 5 at
> "+giveTime(), etc.
> Now I need to execute the same program but from a GUI application. I
> must show the same messages but on a "text field".
> As you can guess, it implies the changing of my program or make a copy
> and replace the print instructions by   textField += "I am in step 4
> at "+giveTime() then textField += "I am in step 5 at "+giveTime(),
> etc.
> I wanted to do the next:
> if output == "GUI":
>     textField += "I am in step 4 at "+giveTime()
>     force_output()
> else:
>     print "I am in step 4 at "+giveTime()
> But it is not smart, elegant, clean ... isn't it?

Oh, then you want one of those elegant progress meters.
You know, where the tenth box accounts for 90% of the elapsed time.

Elegant isn't necessarily smart.

> Any ideas please?

It looks good to me, only I would use elapsed time instead of clock
time.
Something along the lines of "Step 4 completed in",t1-t0,"seconds."

> Regards.

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


Need guidelines to show results of a process

2009-03-19 Thread Vizcayno
Hi:
I wrote a Python program which, during execution, shows me messages on
console indicating at every moment the time and steps being performed
so I can have a 'log online' and guess remaining time for termination,
I used many 'print' instructions to show those messages, i.e.  print
"I am in step 4 at "+giveTime()  print "I am in step 5 at
"+giveTime(), etc.
Now I need to execute the same program but from a GUI application. I
must show the same messages but on a "text field".
As you can guess, it implies the changing of my program or make a copy
and replace the print instructions by   textField += "I am in step 4
at "+giveTime() then textField += "I am in step 5 at "+giveTime(),
etc.
I wanted to do the next:
if output == "GUI":
textField += "I am in step 4 at "+giveTime()
force_output()
else:
print "I am in step 4 at "+giveTime()
But it is not smart, elegant, clean ... isn't it?
Any ideas please?
Regards.

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


Re: Object System

2009-03-19 Thread John Mendelewski
On Mar 19, 6:19 pm, Benjamin Peterson  wrote:
> John Mendelewski  gmail.com> writes:
>
>
>
> > I was wondering if anyone had documents or articles what gave an in-
> > depth view of the object system in Python. Ones concerning dispatch,
> > how self really works, and maybe some meta-programming that comes
> > along with the new style classes.
>
> What do you mean "how self really works"?
>
> It's just an instance of the class.

I meant:

>>> class A(object):
...   def do(self):
... print self.__class__, self.__class__.__bases__
...
>>> a = A()
>>> a.do()
 (,)

What goes on behind the scenes to make a.do() evaluate the do
method with a bound to self in that method? Is this implemented
in C, Python? I think I have a grasp of how to use objects, but
I was wondering about the implementation I suppose.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Threads not Improving Performance in Program

2009-03-19 Thread Tim Rowe
> Thank you for your response. I did not realize that. That seems like a
> huge limitation for such a great language.
> I will look into forking off processes instead of using threads.

If that's what you need to do, yes it is. If it isn't, no it's not. No
language is optimum for all possible applications, each one has
different compromises. You've just discovered one of Python's.

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


Re: Ordered Sets

2009-03-19 Thread Aahz
In article <9a5d59e1-2798-4864-a938-9b39792c5...@s9g2000prg.googlegroups.com>,
Raymond Hettinger   wrote:
>
>Here's a new, fun recipe for you guys:
>
>http://code.activestate.com/recipes/576694/

That is *sick* and perverted.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Programming language design is not a rational science. Most reasoning
about it is at best rationalization of gut feelings, and at worst plain
wrong."  --GvR, python-ideas, 2009-3-1
--
http://mail.python.org/mailman/listinfo/python-list


Re: Neatest way to do a case insensitive "in"?

2009-03-19 Thread Jervis Whitley
>
> I agree that it's an alternative. There are a number of alternatives.
> However the OP was asking for a "neater/easier" alternative. I argue
> that introducing an external module/function to do the exact same thing
> as a built-in type's method doesn't exactly qualify as a "neater/easier"
> alternative.

I argue that it is this functional approach looks visually clean and
hence may appeal
to a readers sense of "neatness" or aesthetics. No one is right or
wrong in this case,
they are both fine solutions.

> In fact, unless you are using a *very* old version of python, if you
> look at the implementation of string.lower(s) it simply returns
> s.lower().
--
http://mail.python.org/mailman/listinfo/python-list


RE: Is python worth learning as a second language?

2009-03-19 Thread Delaney, Timothy (Tim)
Aahz wrote:

> In article <49b58b35$0$3548$426a7...@news.free.fr>,
> Bruno Desthuilliers   wrote:
>> Tomasz Rola a écrit :
>>> 
>>> I may not be objective (tried Java, hated it after 6 years).
>> 
>> Arf - only took me 6 months !-)
> 
> That long?  It only took me six minutes.

I was young and foolish - I actually thought Java was a decent language for a 
few months - until I had to maintain a program written by outside contractors. 
This was back in the Java 1.1 days ...

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


Concrete Factory Pattern syntax?

2009-03-19 Thread Austin Schutz

I have a fairly simple bit of code, something like:

# This should be importing the subclasses somehow, so that the factory
# can make them.
# import Parser.One
# import Parser.Two
# or.. from Parser import *?
class Parser():
   def parse:
'Implemented only in subclass'

   def make_parser(which_parser):
   if(which_parser = 'one'):
 return One()
   else:
  return Two()

# import Parser?
class One(Parser):
   def parse:
   'one implementation'

class Two(Parser):
   def parse:
   'another implementation'

The problem I have is that I don't understand how to put this into
actual files in actual directories and have the interpreter do
something actually useful :-) . What I would like to do is something
like:

lib/
  Parser.py
  Parser/
  __init__.py (maybe?)
  One.py
  Two.py

But I'm not clear on how to structure the import statements. I'm a bit
of a newb wrt python, and I get any number of different errors depending
on how I arrange the import statements, everything from

AttributeError: 'module' object has no attribute 'make_parser'
to
ImportError: cannot import name
to
TypeError: Error when calling the metaclass bases

depending on how I use import. Nothing seems to be the correct
combination. Any help would be much appreciated!

Austin





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


Re: can someone help me (again) stuck on ' hands on python'

2009-03-19 Thread Rhodri James

On Thu, 19 Mar 2009 14:40:55 -, Gary Wood  wrote:


#i adjusted the main function helps me see the code as the program runs


Put it back the way it was (you've broken it rather thoroughly there),
and put the debug "print" calls in printLocations where they stand some
chance of showing you what's going on!

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


Re: Why doesn't this work ? For loop variable scoping ?

2009-03-19 Thread Aahz
In article ,
Linuxguy123   wrote:
>
>I've got a small piece of code that I don't understand.  Basically, a
>variable inside an if statement inside a for loop doesn't seem to be
>updating.  Is this a scope issue ?

Nope, it's a spelling issue.  I suggest you change your code to a more
readable:

newTemp += delta
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Programming language design is not a rational science. Most reasoning
about it is at best rationalization of gut feelings, and at worst plain
wrong."  --GvR, python-ideas, 2009-3-1
--
http://mail.python.org/mailman/listinfo/python-list


Re: Threads not Improving Performance in Program

2009-03-19 Thread Ryan Rosario
On Mar 19, 10:35 am, Jean-Paul Calderone  wrote:
> On Thu, 19 Mar 2009 09:50:51 -0700, Ryan Rosario  
> wrote:
> >I have a parser that needs to process 7 million files. After running
> >for 2 days, it had only processed 1.5 million. I want this script to
> >parse several files at once by using multiple threads: one for each
> >file currently being analyzed.
>
> Threads don't magically make a program faster.  In Python in particular,
> threads won't do much, if anything, to speed up a CPU bound task.  Assuming
> you have more than one CPU, Python still limits you to one thread executing
> Python bytecode at a time.  You could try running multiple processes instead,
> if there really is more hardware that's sitting idle with your single
> threaded version.
>
> Jean-Paul

Thank you for your response. I did not realize that. That seems like a
huge limitation for such a great language.
I will look into forking off processes instead of using threads.

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


Re: Why doesn't this work ? For loop variable scoping ?

2009-03-19 Thread Benjamin Peterson
Linuxguy123  gmail.com> writes:

> 
> 
> Hi people. 
> 
> I've got a small piece of code that I don't understand.  Basically, a
> variable inside an if statement inside a for loop doesn't seem to be
> updating.  Is this a scope issue ?

No, it's because you mispelled the variables.




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


Re: Why doesn't this work ? For loop variable scoping ?

2009-03-19 Thread Ryan Kelly
> newCylinderTempertature = newCylinderTemperature + deltaTemp

Take a careful look at the variable name here: "Tempertature".  Python's
dynamic nature provides a lot of wonderful benefits, but you've just hit
one of the drawbacks - you don't get any protection from typos in
variable names.


   Ryan

-- 
Ryan Kelly
http://www.rfk.id.au  |  This message is digitally signed. Please visit
r...@rfk.id.au|  http://www.rfk.id.au/ramblings/gpg/ for details



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


Re: Lambda forms and scoping

2009-03-19 Thread Benjamin Peterson
Márcio Faustino  gmail.com> writes:

> 
> Hi,
> 
> Executing the example below doesn't produce the expected behavior, but
> using the commented code does. Is this normal, or is it a problem with
> Python? I've tested it with version 2.6.1 on Windows XP.
> 
> Thanks,
> 
> --
> 
> from abc import *
> from types import *
> import re
> 
> class Base (ObjectType):
> __metaclass__ = ABCMeta
> 
> def __init__(self):
> for option in self.get_options().keys():
> method = 'get_%s_option' % re.sub(' ', '_', option.lower
> ())
> setattr(self.__class__, method, lambda self:
> self.get_option(option))

This is because the closure over option is changed when it is reassigned in the
for loop. For example:

>>> def f():
... return [lambda: num for num in xrange(2)] 
... 
>>> f()
[ at 0x83f30>,  at 0x83e70>]
>>> f()[0]
 at 0x83ef0>
>>> g = f()
>>> g[0]()
1
>>> g[1]()
1




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


Why doesn't this work ? For loop variable scoping ?

2009-03-19 Thread Linuxguy123

Hi people. 

I've got a small piece of code that I don't understand.  Basically, a
variable inside an if statement inside a for loop doesn't seem to be
updating.  Is this a scope issue ?

Thanks

Code segment: 



# run through the cycle and calculate the temperature and pressure at
each position, ie every 0.5 crankshaft degrees 
#global newCylinderPressure
#global newCylinderTemperature

for position in range(1,721):
newCylinderPressure = cylinderPressure[position -1] *
(cylinderVolume[position -1] / cylinderVolume[position])**1.4
newCylinderTemperature = (cylinderTemperature[position -1] + 459.67)
* (cylinderVolume[position -1] / cylinderVolume[position])**0.4 - 459.67

# add 1.0 BTUs of heat to the combustion chamber at TDC, which is
position # 360
if position == 360:
#calculate the new temperature
print newCylinderTemperature
deltaTemp = 1.0 / (airMass * Cv)
newCylinderTempertature = newCylinderTemperature + deltaTemp
print deltaTemp
print newCylinderTemperature



The output from this code is:

1357.65862978 <-- newCylinderTemperature when we enter the if statement
2626.04165688 <-- deltaTemp, as it should be
1357.65862978 <-- newCylinderTemperature at the end of the if statement,
which HASN'T CHANGED.  It should be 1357 + 2626 = 3983.   Why doesn't it
change ?  Is there some sort of scope issue here ?



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


Re: Object System

2009-03-19 Thread Benjamin Peterson
John Mendelewski  gmail.com> writes:

> 
> I was wondering if anyone had documents or articles what gave an in-
> depth view of the object system in Python. Ones concerning dispatch,
> how self really works, and maybe some meta-programming that comes
> along with the new style classes.

What do you mean "how self really works"?

It's just an instance of the class.




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


Re: Neatest way to do a case insensitive "in"?

2009-03-19 Thread Albert Hopkins
On Fri, 2009-03-20 at 08:52 +1100, Jervis Whitley wrote:
> On Fri, Mar 20, 2009 at 8:28 AM, Albert Hopkins  
> wrote:
> > On Fri, 2009-03-20 at 07:25 +1100, Jervis Whitley wrote:
> >> >
> >> >if stringA.lower() in stringB.lower():
> >> >bla bla bla
> >> >
> >>
> >> from string import lower
> >>
> >> if lower(stringA) in lower(stringB):
> >>  # was this what you were after?
> >>
> >
> > This is analogous to standing behind a perfectly functioning automobile
> > and pushing it everywhere you want to go.
> 
> It is an alternative solution. That is all.

I agree that it's an alternative. There are a number of alternatives.
However the OP was asking for a "neater/easier" alternative. I argue
that introducing an external module/function to do the exact same thing
as a built-in type's method doesn't exactly qualify as a "neater/easier"
alternative.

In fact, unless you are using a *very* old version of python, if you
look at the implementation of string.lower(s) it simply returns
s.lower().






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


Is there any library for COREL or ILLUSTRATOR?

2009-03-19 Thread alejandro
I need to import cdr files to python and just make some calculations 
(surface size of some objects, perimeters..)
Don't need to show them or manipulate with them... 


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


Lambda forms and scoping

2009-03-19 Thread Márcio Faustino
Hi,

Executing the example below doesn't produce the expected behavior, but
using the commented code does. Is this normal, or is it a problem with
Python? I've tested it with version 2.6.1 on Windows XP.

Thanks,

--

from abc import *
from types import *
import re

class Base (ObjectType):
__metaclass__ = ABCMeta

def __init__(self):
for option in self.get_options().keys():
method = 'get_%s_option' % re.sub(' ', '_', option.lower
())
setattr(self.__class__, method, lambda self:
self.get_option(option))

#def create_method(option):
#method = 'get_%s_option' % re.sub(' ', '_', option.lower
())
#setattr(self.__class__, method, lambda self:
self.get_option(option))
#
#map(create_method, self.get_options().keys())

@abstractmethod
def get_options(self):
raise NotImplementedError()

def get_option(self, option):
return self.get_options()[option]

class Derived (Base):
def get_options(self):
return {
'Message': 'Hello world!',
'Web site': 'http://www.example.com',
}

object = Derived()
print object.get_message_option()
print object.get_web_site_option()
--
http://mail.python.org/mailman/listinfo/python-list


Re: Neatest way to do a case insensitive "in"?

2009-03-19 Thread Jervis Whitley
On Fri, Mar 20, 2009 at 8:28 AM, Albert Hopkins  wrote:
> On Fri, 2009-03-20 at 07:25 +1100, Jervis Whitley wrote:
>> >
>> >    if stringA.lower() in stringB.lower():
>> >        bla bla bla
>> >
>>
>>     from string import lower
>>
>>     if lower(stringA) in lower(stringB):
>>          # was this what you were after?
>>
>
> This is analogous to standing behind a perfectly functioning automobile
> and pushing it everywhere you want to go.

It is an alternative solution. That is all.

Cheers,

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


Ordered Sets

2009-03-19 Thread Raymond Hettinger
Here's a new, fun recipe for you guys:

http://code.activestate.com/recipes/576694/

Enjoy,


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


Re: Neatest way to do a case insensitive "in"?

2009-03-19 Thread Albert Hopkins
On Fri, 2009-03-20 at 07:25 +1100, Jervis Whitley wrote:
> >
> >if stringA.lower() in stringB.lower():
> >bla bla bla
> >
> 
> from string import lower
> 
> if lower(stringA) in lower(stringB):
>  # was this what you were after?
> 

This is analogous to standing behind a perfectly functioning automobile
and pushing it everywhere you want to go.

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


Object System

2009-03-19 Thread John Mendelewski
I was wondering if anyone had documents or articles what gave an in-
depth view of the object system in Python. Ones concerning dispatch,
how self really works, and maybe some meta-programming that comes
along with the new style classes.
--
http://mail.python.org/mailman/listinfo/python-list


Re: cross compile Python to Linux-ARM

2009-03-19 Thread Paul McGuire
On Mar 19, 11:54 am, jefm  wrote:
> Hi,
> We are looking to use Python on an embedded Linux ARM system.
> What I gather from googling the subject is that it is not that
> straight forward (a fair amount of patching & hacking).
> Nobody out there that has done it claims it is easy, which makes me
> worried.
>

I've been able to use Debian to create a gumstix (I'm pretty sure this
is ARM-based) build root with Python, and I am a mere Linux dabbler.
Once built, I had the full standard Python lib, and was able to import
my custom pure Python code without a hitch.

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


Re: How complex is complex?

2009-03-19 Thread Daniel Fetchinson
>> > I understand that my question was foolish, even for a newbie.
>> > I will not ask any more such questions in the future.
>>
>> Gaaah! Your question was just fine, a good question on coding style.
>> I wish more people would ask such questions so that bad habits could
>> be avoided.
>>
>> The newbie posts that are annoying are the ones that:
>> - are answered on page 1 of any tutorial ("how do I get the second
>> character of a string?")
>> - are obvious homework assignments with no actual effort on the
>> poster's part ("how do I write a Python program to find the first 10
>> prime numbers?")
>> - pontificate on what is wrong with Python, based on 2 hours'
>> experience with the language (often titled "What's wrong with Python",
>> with content like "Python sucks because it doesn't have a switch
>> statement/has significant whitespace/doesn't check types of arguments/
>> isn't totally object-oriented like Java/doesn't have interfaces/...")
>> - are so vague as to be just Usenet noise (titled "Help me", with no
>> content, or "i need to write a program and don't know where to start
>> can someone write it for me?")
>>
>> I think Daniel's joke was on the rest of us, who each had to chime in
>> with our favorite dict processing algorithm.
>>
>> It *would* be good for you as a newbie to get an appreciation of the
>> topics that were covered in these responses, though, especially the
>> distinction between updating the dict in-place vs. creating a new
>> dict.
>>
> Daniel, Sorry for misunderstanding your post. I hope I was not being
> passive-aggresive - (also because I found that the second mechanism I
> provided was quite horrible :-), so I was indeed being foolish
> there. )

My point was exactly what somebody already mentioned: with such
subjective matters there is no way of deciding one way or another in a
rational way. What I found is that once you completely discard these
issues and don't waste brain cycles on them at all, but rather you
just go ahead and code, you will be actually sorting these things out
by yourself or put it in another way, these things will be sorted out
by themselves.

After all, GvR said things to the effect that the whole concept of
language design is not rational or objective or scientific, but rather
a big bag of gut feelings and I completely agree. Rationalizing about
these things is pretty dubious to me.

Have fun with python!
Daniel


> Paul/Aahz, I did understand 2 things
> (1) When using map always consider that the function will be called
> everytime, so the hit on the performance is more.
> (2) The second mechanism and the first mechanism provides different
> solutions (new dict/same dict)
> both of which I did not think about at all.
>
> Also, thank you everyone for all the help. I have been following this
> thread for the last 4 months (when I started with python) and I have
> learned a lot. The amount of help provided here is amazing.
>
> p.s. -> English is indeed not my first language :-)



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


Re: cross compile Python to Linux-ARM

2009-03-19 Thread Nick Craig-Wood
jefm  wrote:
>  We are looking to use Python on an embedded Linux ARM system.
>  What I gather from googling the subject is that it is not that
>  straight forward (a fair amount of patching & hacking).
>  Nobody out there that has done it claims it is easy, which makes me
>  worried.
> 
>  I haven't seen a description on porting Python 2.6 or 3.0 yet. Is it
>  much different than for the earlier versions (the latest I have seem
>  is Python 2.5).
> 
>  Does it matter whether Python is cross compiled to Linux 2.4 or Linux
>  2.6 ?
> 
>  Can anyone point to a howto they know works well ?

I gave up trying to cross compile and just used the python from debian
ARM which works very well.

>  What are the chances of an 'officially' supported ARM-Linux Python
>  distribution ?

Depending on your definition of "officially" and "supported" :-

  http://packages.debian.org/lenny/arm/python2.5/download

>  (or is it safer to wait for industrial spec Intel Atom boards to avoid
>  the cross compilation altogether ?

We often compile stuff on our 200 MHz TS-7200 boards.  We NFS mount
the build environment, type make then go out for lunch ;-)

For builds of our application (which embeds python) we normally use
the headers and libraries from the debian ARM packages and cross
compile on something a big beefier.

>  How is the performance and stability of a working Python on an
>  embedded ARM-Linux system ?

Works very well.

>  Does cross compiling Python automatically include the standard
>  Python library, or is that yet another adventure ?

If you use the debian compiled version then you get the lot.

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


Re: Neatest way to do a case insensitive "in"?

2009-03-19 Thread Jervis Whitley
>
>    if stringA.lower() in stringB.lower():
>        bla bla bla
>

from string import lower

if lower(stringA) in lower(stringB):
 # was this what you were after?

Cheers,

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


Re: Concurrent tasklets in Stackless Python

2009-03-19 Thread Aahz
[posted and e-mailed]

In article ,
Minesh Patel   wrote:
>
>Can you suggest any Python libraries for true parallelism or should I
>just stick with Python Threads or asyncore

Python threads are mostly only parallel for I/O (you have to write
special C code to release the GIL).  If you want parallel CPU in pure
Python, you should use e.g. the multiprocessing library.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Programming language design is not a rational science. Most reasoning
about it is at best rationalization of gut feelings, and at worst plain
wrong."  --GvR, python-ideas, 2009-3-1
--
http://mail.python.org/mailman/listinfo/python-list


Re: How complex is complex?

2009-03-19 Thread pruebauno
On Mar 19, 1:25 pm, Paul Hildebrandt 
wrote:
> On Mar 19, 9:41 am, Kottiyath  wrote:
>
>
>
> > On Mar 19, 9:33 pm, Kottiyath  wrote:
>
> > > On Mar 19, 8:42 pm, Paul McGuire  wrote:
>
> > > > On Mar 19, 4:39 am, Kottiyath  wrote:
>
> > > > > I understand that my question was foolish, even for a newbie.
> > > > > I will not ask any more such questions in the future.
>
> > > > Gaaah! Your question was just fine, a good question on coding style.
> > > > I wish more people would ask such questions so that bad habits could
> > > > be avoided.
>
> > > > The newbie posts that are annoying are the ones that:
> > > > - are answered on page 1 of any tutorial ("how do I get the second
> > > > character of a string?")
> > > > - are obvious homework assignments with no actual effort on the
> > > > poster's part ("how do I write a Python program to find the first 10
> > > > prime numbers?")
> > > > - pontificate on what is wrong with Python, based on 2 hours'
> > > > experience with the language (often titled "What's wrong with Python",
> > > > with content like "Python sucks because it doesn't have a switch
> > > > statement/has significant whitespace/doesn't check types of arguments/
> > > > isn't totally object-oriented like Java/doesn't have interfaces/...")
> > > > - are so vague as to be just Usenet noise (titled "Help me", with no
> > > > content, or "i need to write a program and don't know where to start
> > > > can someone write it for me?")
>
> > > > I think Daniel's joke was on the rest of us, who each had to chime in
> > > > with our favorite dict processing algorithm.
>
> > > > It *would* be good for you as a newbie to get an appreciation of the
> > > > topics that were covered in these responses, though, especially the
> > > > distinction between updating the dict in-place vs. creating a new
> > > > dict.
>
> > > > -- Paul
>
> > > Daniel, Sorry for misunderstanding your post. I hope I was not being
> > > passive-aggresive - (also because I found that the second mechanism I
> > > provided was quite horrible :-), so I was indeed being foolish
> > > there. )
>
> > > Paul/Aahz, I did understand 2 things
> > > (1) When using map always consider that the function will be called
> > > everytime, so the hit on the performance is more.
> > > (2) The second mechanism and the first mechanism provides different
> > > solutions (new dict/same dict)
> > > both of which I did not think about at all.
>
> > > Also, thank you everyone for all the help. I have been following this
> > > thread for the last 4 months (when I started with python) and I have
> > > learned a lot. The amount of help provided here is amazing.
>
> > > p.s. -> English is indeed not my first language :-)
>
> > Oops, Forgot to mention the biggest learning.
>
> > Readability is better than brevity -
>
> I rewrote your sentence to be more optimized.
>
> Readability > brevity
>
> ;-)
>
> > Thanks to Rhodri.
>
> > This was a question which was bugging me all the time. When I look at
> > code, I am always envious when I see the same code written in much
> > smaller number of lines. Now, I will force myself to ask the questions
> > Rhodri proposed (esp: does it look uglier part) before deciding
> > whether or not to go ahead with brevity.
>
>

sometimes: brevity==Readability

but as in many things the trick is in finding the right tradeoff. I am
willing to accept some trivial amount of additional complexity if it
means I have to read less lines of code, but I have my limits too. I
aim for a comprehension speed of 2-10 lines per minute for somebody
proficient in the language.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Neatest way to do a case insensitive "in"?

2009-03-19 Thread Steve Holden
aiwarrior wrote:
> On Mar 13, 9:31 pm, Albert Hopkins  wrote:
>> On Fri, 2009-03-13 at 21:04 +, tinn...@isbd.co.uk wrote:
>>> What's the neatest way to do the following in case insensitive fashion:-
>>> if stringA in stringB:
>>> bla bla bla
>>> I know I can just do:-
>>> if stringA.lower() in stringB.lower():
>>> bla bla bla
>>> But I was wondering if there's a neater/easier way?
>> How is "if stringA.lower() in stringB.lower():" complex/messy?
> 
> I do agree with you but what if your really insist and perhaps
> subclass the str and override the __contains__(x) method?
> 
But then objects have to be explicitly created as subclasses of str.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/

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


Re: DictReader and fieldnames

2009-03-19 Thread skip

Ted> Thanks.  Is there any way to make this work before actually reading
Ted> in a row of data?  In version 2.5, I need to first do a rdr.next()
Ted> before rdr.fieldnames gives me anything.

If you know the csv file contains column headers this should work:

f = open("f.csv", "rb")
names = csv.reader(f).next()
rdr = csv.DictReader(f, fieldnames=names)

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


Re: Heuristically processing documents

2009-03-19 Thread MRAB

BJörn Lindqvist wrote:

I have a large set of documents in various text formats. I know that
each document contains its authors name, email and phone number.
Sometimes it also contains the authors home address.

The task is to find out the name, email and phone of as many documents
as possible. Since the documents are not in a specific format, you
have to do a lot of guessing and getting approximate results is fine.

For example, to find the email you can use a simple regexp. If there
is a match you can be certain that that is the authors email. But what
algorithms can you use to figure out the other information?


Tricky! :-)

How would _you_ recognise them? Have a look at the documents and see if
you can see a pattern. For example, names and address often consist of a
sequence of words in title case, eg "Björn Lindqvist", which might help
you narrow down the list of possibilities. What do telephone numbers
look like, etc?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can I rely on...

2009-03-19 Thread R. David Murray
"Emanuele D'Arrigo"  wrote:
> Thank you everybody for the informative replies.
> 
> I'll have to comb my code for all the instances of "item in sequence"
> statement because I suspect some of them are as unsafe as my first
> example. Oh well. One more lesson learned.

You may have far fewer unsafe cases than you think, depending
on how you understood the answers you got, some of which
were a bit confusing.  Just to make sure it is clear
what is going on in your example

>From the documentation of 'in':

x in s   True if an item of s is equal to x, else False

(http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange)

Note the use of 'equal' there.  So for lists and tuples,

if x in s: dosomething

is the same as

for item in s:
if item == x:
do something
break

So:

>>> s = ['sdb*&', 'uuyh', 'foo']
>>> x = 'sdb*&'
>>> x is s[0]
False
>>> x in s
True

(I used a string with special characters in it to avoid Python's
interning of identifier-like strings so that x and s[0] would not be
the same object).

Your problem with the regex example is that re makes no promise that
patterns compiled from the same source string will compare equal to
each other.  Thus their _equality_ is not guaranteed.  Switching to
using an equals comparison won't help you avoid your problem in
the example you showed.

Now, if you have a custom sequence type, 'in' and and an '==' loop
might produce different results, since 'in' is evaluated by the special
method __contains__ if it exists (and list iteration with equality if
it doesn't).  But the _intent_ of __contains__ is that comparison be
by equality, not object identity, so if the two are not the same something
weird is going on and there'd better be a good reason for it :)

In summary, 'in' is the thing to use if you want to know if your
sample object is _equal to_ any of the objects in the container.
As long as equality is meaningful for the objects involved, there's
no reason to switch to a loop.

--
R. David Murray   http://www.bitdance.com

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


Re: What happened to NASA at Python? :(

2009-03-19 Thread Steve Holden
Dotan Cohen wrote:
>> There are about 40 people supporting the Mars Lander mission using
>> Python and aiming for a launch window this September. Wish them luck!
>>
> 
> What, exactly, are they using Python for?
> 
Mostly for testing, I understand, but during their training I was the
Python guy among a bunch of scientists and engineers. I believe there
are aerospace testing rigs that are controlled through a Python API. I
have another client using them for airframe automation testing.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/


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


Heuristically processing documents

2009-03-19 Thread BJörn Lindqvist
I have a large set of documents in various text formats. I know that
each document contains its authors name, email and phone number.
Sometimes it also contains the authors home address.

The task is to find out the name, email and phone of as many documents
as possible. Since the documents are not in a specific format, you
have to do a lot of guessing and getting approximate results is fine.

For example, to find the email you can use a simple regexp. If there
is a match you can be certain that that is the authors email. But what
algorithms can you use to figure out the other information?

-- 
mvh Björn
--
http://mail.python.org/mailman/listinfo/python-list


Re: REDIRECT

2009-03-19 Thread I V
On Wed, 18 Mar 2009 21:30:59 -0700, gaeasiankom wrote:

> What actually I'm try to do is :
> 
> I'm having a Login page which developed in HTML. When I click on the
> "Login" button I want the page to validate (at datastore of google app)
> using python and redirect to other HTML page. As what I understand,
> Python is the only language that supported by the GoogleApps.

The webapp framework that comes with the Google Apps SDK has a function 
for redirecting:

http://code.google.com/appengine/docs/python/tools/webapp/redirects.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: converting pipe delimited file to fixed width

2009-03-19 Thread MRAB

John Posner wrote:

[snip]
 

field_widths = [14, 6, 18, 21, 21, 4, 6]

out = open("/home/chatdi/ouptut.csv", 'w')
for line in open("/home/chatdi/input.csv", "r"):
fields = line.rstrip().split('|')
padded_fields = [field.ljust(width) for field, width in zip(fields, 
field_widths)]
out.write("".join(padded_fields) + "\n")

out.close()


How about a version that uses Python 3.0 string formatting:

  field_widths = [14, 6, 18, 21, 21, 4, 6]

  out = open(r"c:\temp\output.csv", 'w')
  for line in open(r"c:\temp\input.csv", "r"):
  fields = line.rstrip().split('|')
  outline = ""
  for pair in zip(fields, field_widths):
  outline += "{0:{1}}".format(*pair)
  out.write(outline + "\n")
  out.close()

Or, if you hate NLs (and, perhaps, like obfuscation):

  field_widths = [14, 6, 18, 21, 21, 4, 6]

  out = open(r"c:\temp\output.csv", 'w')
  for line in open(r"c:\temp\input.csv", "r"):
  fields = line.rstrip().split('|')
  out.write("".join(["{0:{1}}".format(*pair) for pair in \
zip(fields, field_widths)]) + "\n")
  out.close()


Alternatively:

#!/usr/bin/python

field_widths = [14, 6, 18, 21, 21, 4, 6]

# build the format for the output line
fmt = "".join("%%-%ds" % width for width in field_widths) + "\n"

out = open("/home/chatdi/ouptut.csv", 'w')
for line in open("/home/chatdi/input.csv", "r"):
out.write(fmt % tuple(line.rstrip().split('|')))

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


Re: Parallel processing on shared data structures

2009-03-19 Thread MRAB

psaff...@googlemail.com wrote:

I'm filing 160 million data points into a set of bins based on their
position. At the moment, this takes just over an hour using interval
trees. I would like to parallelise this to take advantage of my quad
core machine. I have some experience of Parallel Python, but PP seems
to only really work for problems where you can do one discrete bit of
processing and recombine these results at the end.

I guess I could thread my code and use mutexes to protect the shared
lists that everybody is filing into. However, my understanding is that
Python is still only using one process so this won't give me multi-
core.

Does anybody have any suggestions for this?


Could you split your data set and run multiple instances of the script
at the same time and then merge the corresponding lists?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Missing values in tuple assignment

2009-03-19 Thread Terry Reedy

Jim Garrison wrote:

Use case: parsing a simple config file line where lines start with a
keyword and have optional arguments.  I want to extract the keyword and
then pass the rest of the line to a function to process it. An obvious
use of split(None,1)

cmd,args= = line.split(None,1);
if cmd in self.switch: self.switch[cmd](self,args)
else: self.errors.append("unrecognized keyword '{0)'".format(cmd))

Here's a test in IDLE:

 >>> a="now is the time"
 >>> x,y=a.split(None,1)
 >>> x
 'now'
 >>> y
 'is the time'

However, if the optional argument string is missing:

 >>> a="now"
 >>> x,y=a.split(None,1)
 Traceback (most recent call last):
   File "", line 1, in 
 x,y=a.split(None,1)
 ValueError: need more than 1 value to unpack

I understand the problem is not with split() but with the assignment
to a tuple.  Is there a way to get the assignment to default the
missing values to None?


In 3.0 (and 2.6?), the following is close:

>>> a,*b = (1,2)
>>> a,b
(1, [2])
>>> a,*b = (1,)
>>> a,b
(1, [])

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


Re: cross compile Python to Linux-ARM

2009-03-19 Thread Tino Wildenhain

jefm wrote:

Hi,
We are looking to use Python on an embedded Linux ARM system.
What I gather from googling the subject is that it is not that
straight forward (a fair amount of patching & hacking).
Nobody out there that has done it claims it is easy, which makes me
worried.


Yes unfortunately its quite difficult. Been there, stopped at some
point. But for arm however you have some chances, see below.


I haven't seen a description on porting Python 2.6 or 3.0 yet. Is it
much different than for the earlier versions (the latest I have seem
is Python 2.5).

Does it matter whether Python is cross compiled to Linux 2.4 or Linux
2.6 ?

Can anyone point to a howto they know works well ?

What are the chances of an 'officially' supported ARM-Linux Python
distribution ?
(or is it safer to wait for industrial spec Intel Atom boards to avoid
the cross compilation altogether ?


Easier for sure.


What would it take for the Linux version of Python to be easily cross
compiled (i.e. would the Linux-Python maintainers be willing to
include and maintain cross-compilation specific functions) ?


Either throw the automake stuff away or correct it accordingly
to support target platforms different from build platform.


Let's say we can get it done.
How is the performance and stability of a working Python on an
embedded ARM-Linux system ?


Its working quite nice on my Psion 5MX installed via debian
packages. So I believe you should be able to work from
debian source packages since they are cross compiles as far
as I know.


Does cross compiling Python automatically include the standard Python
library, or is that yet another adventure ?


The standard lib is in python mostly so no worries. The only problematic
part can be various C extensions like zlib, curses, pthreads ... which
are all optional anyway.

Regards
Tino


smime.p7s
Description: S/MIME Cryptographic Signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: converting pipe delimited file to fixed width

2009-03-19 Thread John Posner

[snip]
 
> field_widths = [14, 6, 18, 21, 21, 4, 6]
> 
> out = open("/home/chatdi/ouptut.csv", 'w')
> for line in open("/home/chatdi/input.csv", "r"):
> fields = line.rstrip().split('|')
> padded_fields = [field.ljust(width) for field, width in zip(fields, 
> field_widths)]
> out.write("".join(padded_fields) + "\n")
> 
> out.close()

How about a version that uses Python 3.0 string formatting:

  field_widths = [14, 6, 18, 21, 21, 4, 6]

  out = open(r"c:\temp\output.csv", 'w')
  for line in open(r"c:\temp\input.csv", "r"):
  fields = line.rstrip().split('|')
  outline = ""
  for pair in zip(fields, field_widths):
  outline += "{0:{1}}".format(*pair)
  out.write(outline + "\n")
  out.close()

Or, if you hate NLs (and, perhaps, like obfuscation):

  field_widths = [14, 6, 18, 21, 21, 4, 6]

  out = open(r"c:\temp\output.csv", 'w')
  for line in open(r"c:\temp\input.csv", "r"):
  fields = line.rstrip().split('|')
  out.write("".join(["{0:{1}}".format(*pair) for pair in \
zip(fields, field_widths)]) + "\n")
  out.close()

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


Parallel processing on shared data structures

2009-03-19 Thread psaff...@googlemail.com
I'm filing 160 million data points into a set of bins based on their
position. At the moment, this takes just over an hour using interval
trees. I would like to parallelise this to take advantage of my quad
core machine. I have some experience of Parallel Python, but PP seems
to only really work for problems where you can do one discrete bit of
processing and recombine these results at the end.

I guess I could thread my code and use mutexes to protect the shared
lists that everybody is filing into. However, my understanding is that
Python is still only using one process so this won't give me multi-
core.

Does anybody have any suggestions for this?

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


Re: converting pipe delimited file to fixed width

2009-03-19 Thread Tim Chase

Caveat: none of the solutions (including mine) deal with the case of
the field being longer than the width. You might want to throw an
exception.


Alternatively, you can just crop the results.  Tweaking MRAB's 
elegant solution:


  field_widths = [14, 6, 18, 21, 21, 4, 6]
  infile = open("input.csv")
  out = open("ouptut.csv", 'w')

  for fields in csv.reader(infile, delimiter='|'):
 padded_fields = [
   # pad, and then crop
   field.ljust(width)[:width]
   for field, width
   in zip(fields, field_widths)
   ]
 out.write("".join(padded_fields) + "\n")

  infile.close()
  out.close()

If you want them right-justified, you can use

  field.rjust(width)[-width:]

-tkc



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


Re: Missing values in tuple assignment

2009-03-19 Thread MRAB

Albert Hopkins wrote:

On Thu, 2009-03-19 at 11:57 -0500, Jim Garrison wrote:

Use case: parsing a simple config file line where lines start with a
keyword and have optional arguments.  I want to extract the keyword and
then pass the rest of the line to a function to process it. An obvious
use of split(None,1)

 cmd,args= = line.split(None,1);
 if cmd in self.switch: self.switch[cmd](self,args)
 else: self.errors.append("unrecognized keyword '{0)'".format(cmd))

Here's a test in IDLE:

  >>> a="now is the time"
  >>> x,y=a.split(None,1)
  >>> x
  'now'
  >>> y
  'is the time'

However, if the optional argument string is missing:

  >>> a="now"
  >>> x,y=a.split(None,1)
  Traceback (most recent call last):
File "", line 1, in 
  x,y=a.split(None,1)
  ValueError: need more than 1 value to unpack

I understand the problem is not with split() but with the assignment
to a tuple.  Is there a way to get the assignment to default the
missing values to None?


why not do this?
>>> a= 'now'
>>> z = a.split(None, 1)
>>> x = z[0]
>>> y = z[1] if len(z) == 2 else None


A 1-line solution (not necessarily recommended) is:

x, y = (a.split(None, 1) + [None])[ : 2]
--
http://mail.python.org/mailman/listinfo/python-list


Re: What happened to NASA at Python? :(

2009-03-19 Thread Dotan Cohen
> Could you perhaps be persuaded to post in ASCII?

Sorry, Aahz, Gmail sends the mail as base64 encoded if there are
non-ascii characters. This is the first problem that I've encountered
with this, what mailer are you using?

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
--
http://mail.python.org/mailman/listinfo/python-list


Re: Threads not Improving Performance in Program

2009-03-19 Thread Jean-Paul Calderone

On Thu, 19 Mar 2009 09:50:51 -0700, Ryan Rosario  wrote:

I have a parser that needs to process 7 million files. After running
for 2 days, it had only processed 1.5 million. I want this script to
parse several files at once by using multiple threads: one for each
file currently being analyzed.


Threads don't magically make a program faster.  In Python in particular,
threads won't do much, if anything, to speed up a CPU bound task.  Assuming
you have more than one CPU, Python still limits you to one thread executing
Python bytecode at a time.  You could try running multiple processes instead,
if there really is more hardware that's sitting idle with your single
threaded version.

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


Re: converting pipe delimited file to fixed width

2009-03-19 Thread MRAB

Terry Reedy wrote:

digz wrote:

Hi,
I am trying to convert a | delimited  file to fixed width by right
padding with spaces, Here is how I have written the program , just get
the feeling this can be done in a much better ( python functional )
way rather than the procedural code i have below . Any help
appreciated

#!/usr/bin/python
def rightFill(fillString, toLength, fillChar):
return fillString+''.join([fillChar for x in range(len
(fillString),toLength)])


Ugh.  That should be the same as
return fillString + (toLength-length(fillString))*fillChar


Ugh. That should be the same as
return fillString.ljust(toLength, fillChar)

(and there's no function length()) ;-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: What happened to NASA at Python? :(

2009-03-19 Thread Aahz
[posted and e-mailed]

In article ,
Dotan Cohen   wrote:
>
>PiBUaGVyZSBhcmUgYWJvdXQgNDAgcGVvcGxlIHN1cHBvcnRpbmcgdGhlIE1hcnMgTGFuZGVyIG1p
>c3Npb24gdXNpbmcKPiBQeXRob24gYW5kIGFpbWluZyBmb3IgYSBsYXVuY2ggd2luZG93IHRoaXMg
>U2VwdGVtYmVyLiBXaXNoIHRoZW0gbHVjayEKPgoKV2hhdCwgZXhhY3RseSwgYXJlIHRoZXkgdXNp
>bmcgUHl0aG9uIGZvcj8KCi0tIApEb3RhbiBDb2hlbgoKaHR0cDovL3doYXQtaXMtd2hhdC5jb20K
>aHR0cDovL2dpYmJlcmlzaC5jby5pbAoK15At15Et15It15Mt15Qt15Ut15Yt15ct15gt15kt15ot
>15st15wt150t154t158t16At16Et16It16Mt16Qt16Ut16Yt16ct16gt16kt16oK2Kct2Kgt2Kot
>2Kst2Kwt2K0t2K4t2K8t2LAt2LEt2LIt2LMt2LQt2LUt2LYt2Lct2Lgt2Lkt2Lot2YEt2YIt2YMt
>2YQt2YUt2YYt2YfigI0t2Ygt2YoK0JAt0JEt0JIt0JMt0JQt0JUt0IEt0JYt0Jct0Jgt0Jkt0Jot
>0Jst0Jwt0J0t0J4t0J8t0KAt0KEt0KIt0KMt0KQt0KUt0KYt0Kct0Kgt0Kkt0Kot0Kst0Kwt0K0t
>0K4t0K8K0LAt0LEt0LIt0LMt0LQt0LUt0ZEt0LYt0Lct0Lgt0Lkt0Lot0Lst0Lwt0L0t0L4t0L8t
>0YAt0YEt0YIt0YMt0YQt0YUt0YYt0Yct0Ygt0Ykt0Yot0Yst0Ywt0Y0t0Y4t0Y8Kw6Qtw7Ytw7wt
>w58tw4Qtw5Ytw5wK

Could you perhaps be persuaded to post in ASCII?
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Programming language design is not a rational science. Most reasoning
about it is at best rationalization of gut feelings, and at worst plain
wrong."  --GvR, python-ideas, 2009-3-1
--
http://mail.python.org/mailman/listinfo/python-list


Re: How complex is complex?

2009-03-19 Thread Paul Hildebrandt
On Mar 19, 9:41 am, Kottiyath  wrote:
> On Mar 19, 9:33 pm, Kottiyath  wrote:
>
>
>
> > On Mar 19, 8:42 pm, Paul McGuire  wrote:
>
> > > On Mar 19, 4:39 am, Kottiyath  wrote:
>
> > > > I understand that my question was foolish, even for a newbie.
> > > > I will not ask any more such questions in the future.
>
> > > Gaaah! Your question was just fine, a good question on coding style.
> > > I wish more people would ask such questions so that bad habits could
> > > be avoided.
>
> > > The newbie posts that are annoying are the ones that:
> > > - are answered on page 1 of any tutorial ("how do I get the second
> > > character of a string?")
> > > - are obvious homework assignments with no actual effort on the
> > > poster's part ("how do I write a Python program to find the first 10
> > > prime numbers?")
> > > - pontificate on what is wrong with Python, based on 2 hours'
> > > experience with the language (often titled "What's wrong with Python",
> > > with content like "Python sucks because it doesn't have a switch
> > > statement/has significant whitespace/doesn't check types of arguments/
> > > isn't totally object-oriented like Java/doesn't have interfaces/...")
> > > - are so vague as to be just Usenet noise (titled "Help me", with no
> > > content, or "i need to write a program and don't know where to start
> > > can someone write it for me?")
>
> > > I think Daniel's joke was on the rest of us, who each had to chime in
> > > with our favorite dict processing algorithm.
>
> > > It *would* be good for you as a newbie to get an appreciation of the
> > > topics that were covered in these responses, though, especially the
> > > distinction between updating the dict in-place vs. creating a new
> > > dict.
>
> > > -- Paul
>
> > Daniel, Sorry for misunderstanding your post. I hope I was not being
> > passive-aggresive - (also because I found that the second mechanism I
> > provided was quite horrible :-), so I was indeed being foolish
> > there. )
>
> > Paul/Aahz, I did understand 2 things
> > (1) When using map always consider that the function will be called
> > everytime, so the hit on the performance is more.
> > (2) The second mechanism and the first mechanism provides different
> > solutions (new dict/same dict)
> > both of which I did not think about at all.
>
> > Also, thank you everyone for all the help. I have been following this
> > thread for the last 4 months (when I started with python) and I have
> > learned a lot. The amount of help provided here is amazing.
>
> > p.s. -> English is indeed not my first language :-)
>
> Oops, Forgot to mention the biggest learning.
>
> Readability is better than brevity -

I rewrote your sentence to be more optimized.

Readability > brevity

;-)


> Thanks to Rhodri.
>
> This was a question which was bugging me all the time. When I look at
> code, I am always envious when I see the same code written in much
> smaller number of lines. Now, I will force myself to ask the questions
> Rhodri proposed (esp: does it look uglier part) before deciding
> whether or not to go ahead with brevity.

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


Re: What happened to NASA at Python? :(

2009-03-19 Thread Dotan Cohen
> There are about 40 people supporting the Mars Lander mission using
> Python and aiming for a launch window this September. Wish them luck!
>

What, exactly, are they using Python for?

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il

א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת
ا-ب-ت-ث-ج-ح-خ-د-ذ-ر-ز-س-ش-ص-ض-ط-ظ-ع-غ-ف-ق-ك-ل-م-ن-ه‍-و-ي
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
ä-ö-ü-ß-Ä-Ö-Ü
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can I rely on...

2009-03-19 Thread Emanuele D'Arrigo
Thank you everybody for the informative replies.

I'll have to comb my code for all the instances of "item in sequence"
statement because I suspect some of them are as unsafe as my first
example. Oh well. One more lesson learned.

Thank you again.

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


Re: What happened to NASA at Python? :(

2009-03-19 Thread Steve Holden
s...@pobox.com wrote:
> >> In fact, graphics were added for several organizations.  I believe
> >> they will be chosen randomly.  NASA is still there.
> 
> MiO> In that case, they must be using the random number generator from
> MiO> Dilbert. You know, the one that said 9, 9, 9, 9,...
> 
> Sorry, randomly chosen whenever the front page is rebuilt by one of the web
> gnomes.  It's not chosen randomly on each page fetch.
> 
Though of course it easily *could* be, and I hope it soon *will* be.

More about that in my PyCon talk ...

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/

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


Re: Memory efficient tuple storage

2009-03-19 Thread psaff...@googlemail.com
In the end, I used a cStringIO object to store the chromosomes -
because there are only 23, I can use one character for each chromosome
and represent the whole lot with a giant string and a dictionary to
say what each character means. Then I used numpy arrays for the data
and coordinates. This squeezed each file into under 100MB.

Thanks again for the help!

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


Re: Strange crash issue on Windows w/ PyGTK, Cairo...

2009-03-19 Thread CJ Kucera
CJ Kucera wrote:
> Anyway, the issue turned out to be zlib.decompress() - for larger sets
> of data, if I wasn't specifying "bufsize," the malloc()s that it was
> doing behind-the-scenes must have been clobbering memory.  As soon as I
> specified bufsize, everything was totally kosher.

Okay, I've got a reproducible testcase of this available up here:

http://apocalyptech.com/pygtk-zlib/

I'm no longer *totally* convinced that it's a zlib issue...  zlib's call
actually returns a valid string, and the error happens later in the app.
I've yet to be able to engineer a crash using anything other than that
cairo.ImageSurface.create_from_png() function, so it's possible that
specifying "bufsize" in zlib.decompress() merely allocates memory in
such a way that a bug in PyCairo doesn't come to light in that case.

So, I'm not really sure if I should submit this to Python or PyGTK's
tracker yet.  Could someone check it out and let me know what you think?
That'd be great.  Thanks!

As I mention on that page, removing "import os" and "import sys" will
"fix" the issue on XP, though you can remove them on win2k and still see
the crash.

Thanks,
CJ

-- 
WOW: Flemmy|   "The ships hung in the sky in much the same
p...@apocalyptech.com   |way that bricks don't." - Douglas Adams,
24.24.2.3171   | _The Hitchhiker's Guide To The Galaxy_
--
http://mail.python.org/mailman/listinfo/python-list


Re: Missing values in tuple assignment

2009-03-19 Thread Albert Hopkins
On Thu, 2009-03-19 at 11:57 -0500, Jim Garrison wrote:
> Use case: parsing a simple config file line where lines start with a
> keyword and have optional arguments.  I want to extract the keyword and
> then pass the rest of the line to a function to process it. An obvious
> use of split(None,1)
> 
>  cmd,args= = line.split(None,1);
>  if cmd in self.switch: self.switch[cmd](self,args)
>  else: self.errors.append("unrecognized keyword '{0)'".format(cmd))
> 
> Here's a test in IDLE:
> 
>   >>> a="now is the time"
>   >>> x,y=a.split(None,1)
>   >>> x
>   'now'
>   >>> y
>   'is the time'
> 
> However, if the optional argument string is missing:
> 
>   >>> a="now"
>   >>> x,y=a.split(None,1)
>   Traceback (most recent call last):
> File "", line 1, in 
>   x,y=a.split(None,1)
>   ValueError: need more than 1 value to unpack
> 
> I understand the problem is not with split() but with the assignment
> to a tuple.  Is there a way to get the assignment to default the
> missing values to None?

why not do this?
>>> a= 'now'
>>> z = a.split(None, 1)
>>> x = z[0]
>>> y = z[1] if len(z) == 2 else None

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


Re: What happened to NASA at Python? :(

2009-03-19 Thread Steve Holden
r wrote:
> On Mar 12, 3:31 am, Michele Simionato 
> wrote:
> 
>> That's pretty much impossible. I am sure NASA uses all programming
>> languages in existence,
>> plus probably many internal ones we never heard of.
> 
> True but...
> 
 all([NASA.does_endorse(lang) for lang in NASA['languages']])
> False
> 
> As the code suggests NASA doesn't sport an endorsement of X languages
> on every X language's webpage, which i feel is very important for
> Python's image, along with Google(Even more important!!), ILM, and
> others.
> 
> I am really not worried if NASA *actually* uses Python or not(or to
> what extent), just as long as they "say" they do is good enough for
> me. *wink-wink*

There are about 40 people supporting the Mars Lander mission using
Python and aiming for a launch window this September. Wish them luck!

That's just one of the NASA groups relying heavily on Python.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/

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


Re: Simple question about yyyy/mm/dd

2009-03-19 Thread Scott David Daniels

mattia wrote:

Hi all, I need to receive in input a date represented by a string in the
form "/mm/dd" (or reversed), then I need to assure that the date is 
= the current date and then split the dates in variables like year, 

month, day. Is there some module to do this quickly?

Look into time.strptime

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


Re: Threads not Improving Performance in Program

2009-03-19 Thread odeits
On Mar 19, 9:50 am, Ryan Rosario  wrote:
> I have a parser that needs to process 7 million files. After running
> for 2 days, it had only processed 1.5 million. I want this script to
> parse several files at once by using multiple threads: one for each
> file currently being analyzed.
>
> My code iterates through all of the directories within a directory,
> and at each directory, iterates through each file in that directory. I
> structured my code something like this. I think I might be
> misunderstanding how to use threads:
>
> mythreads = []
> for directory in dirList:
>  #some processing...
>  for file in fileList:
>     p = Process(currDir,directory,file)    #class that extends 
> thread.Threading
>     mythreads.append(p)
>     p.start()
>
> for thread in mythreads:
>  thread.join()
>  del thread
>
> The actual class that extends threading.thread is below:
>
> class Process(threading.Thread):
>         vlock = threading.Lock()
>         def __init__(self,currDir,directory,file):      #thread constructor
>                 threading.Thread.__init__(self)
>                 self.currDir = currDir
>                 self.directory = directory
>                 self.file = file
>         def run(self):
>                 redirect = re.compile(r'#REDIRECT',re.I)
>                 xmldoc = minidom.parse(os.path.join(self.currDir,self.file))
>                 try:
>                         markup =
> xmldoc.firstChild.childNodes[-2].childNodes[-2].childNodes[-2].childNodes[0­].data
>                 except:
>                         #An error occurred
>                         Process.vlock.acquire()
>                         BAD = open("bad.log","a")
>                         BAD.writelines(self.file + "\n")
>                         BAD.close()
>                         Process.vlock.release()
>                         print "Error."
>                         return
>                 #if successful, do more processing...
>
> I did an experiment with a variety of numbers of threads and there is
> no performance gain. The code is taking the same amount of time to
> process 1000 files as it would if the code did not use threads. Any
> ideas on what I am doing wrong?

Perhabs the bottleneck is the IO. How big are the files you are trying
to parse? Another possible bottleneck is that threads share memory.
Thread construction is also expensive in python, try looking up a
threadPool class. (ThreadPools are collections of threads that do
work, when they finish they go to an idle state untill you give them
more work, that way you aren't constantly creating new threads which
is expensive)
--
http://mail.python.org/mailman/listinfo/python-list


Missing values in tuple assignment

2009-03-19 Thread Jim Garrison

Use case: parsing a simple config file line where lines start with a
keyword and have optional arguments.  I want to extract the keyword and
then pass the rest of the line to a function to process it. An obvious
use of split(None,1)

cmd,args= = line.split(None,1);
if cmd in self.switch: self.switch[cmd](self,args)
else: self.errors.append("unrecognized keyword '{0)'".format(cmd))

Here's a test in IDLE:

 >>> a="now is the time"
 >>> x,y=a.split(None,1)
 >>> x
 'now'
 >>> y
 'is the time'

However, if the optional argument string is missing:

 >>> a="now"
 >>> x,y=a.split(None,1)
 Traceback (most recent call last):
   File "", line 1, in 
 x,y=a.split(None,1)
 ValueError: need more than 1 value to unpack

I understand the problem is not with split() but with the assignment
to a tuple.  Is there a way to get the assignment to default the
missing values to None?


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


cross compile Python to Linux-ARM

2009-03-19 Thread jefm
Hi,
We are looking to use Python on an embedded Linux ARM system.
What I gather from googling the subject is that it is not that
straight forward (a fair amount of patching & hacking).
Nobody out there that has done it claims it is easy, which makes me
worried.

I haven't seen a description on porting Python 2.6 or 3.0 yet. Is it
much different than for the earlier versions (the latest I have seem
is Python 2.5).

Does it matter whether Python is cross compiled to Linux 2.4 or Linux
2.6 ?

Can anyone point to a howto they know works well ?

What are the chances of an 'officially' supported ARM-Linux Python
distribution ?
(or is it safer to wait for industrial spec Intel Atom boards to avoid
the cross compilation altogether ?

What would it take for the Linux version of Python to be easily cross
compiled (i.e. would the Linux-Python maintainers be willing to
include and maintain cross-compilation specific functions) ?

Let's say we can get it done.
How is the performance and stability of a working Python on an
embedded ARM-Linux system ?

Does cross compiling Python automatically include the standard Python
library, or is that yet another adventure ?

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


Re: converting pipe delimited file to fixed width

2009-03-19 Thread odeits
On Mar 19, 8:51 am, digz  wrote:
> Hi,
> I am trying to convert a | delimited  file to fixed width by right
> padding with spaces, Here is how I have written the program , just get
> the feeling this can be done in a much better ( python functional )
> way rather than the procedural code i have below . Any help
> appreciated
>
> #!/usr/bin/python
> def rightFill(fillString, toLength, fillChar):
>     return fillString+''.join([fillChar for x in range(len
> (fillString),toLength)])
>
> fieldWidth=[ 14, 6, 18, 21, 21,4, 6  ];
>
> file = open("/home/chatdi/input.csv", "r");
> lines = file.readlines()
> file.close()
>
> out = open( "/home/chatdi/ouptut.csv", 'w')
> for line in lines:
>     line = line[:-1]
>     index = 0
>     for token in line.split('|'):
>         paddedToken = rightFill(token, fieldWidth[index], ' ' )
>         out.write( paddedToken )
>         index = index + 1
>     out.write("\n")


Caveat: none of the solutions (including mine) deal with the case of
the field being longer than the width. You might want to throw an
exception.

I use the csv module to do the reading of the file, i am going to do
some testing to see if i can set the delimiter to '' so that i can
also use it for the output. For now this should work.


http://docs.python.org/library/csv.html


import csv

fieldWidth=[ 14, 6, 18, 21, 21,4, 6  ];
csvfilepath = 'somefile.csv'

outcsvfilepath = 'someoutfile.csv'
endl = '\n'

f = open(csvfilepath)
outf = open(outcsvfilepath)


csvfile = csv.reader(f,delimiter = '|')

for row in csvfile:
outrow = [ field + fillchar * (width - len(field)) for width, field
in zip(fieldWidth,row)]
outcsv.write( ''.join(outrow))
outcsv.write( endl )

f.close()
outf.close()

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


Re: converting pipe delimited file to fixed width

2009-03-19 Thread Terry Reedy

digz wrote:

Hi,
I am trying to convert a | delimited  file to fixed width by right
padding with spaces, Here is how I have written the program , just get
the feeling this can be done in a much better ( python functional )
way rather than the procedural code i have below . Any help
appreciated

#!/usr/bin/python
def rightFill(fillString, toLength, fillChar):
return fillString+''.join([fillChar for x in range(len
(fillString),toLength)])


Ugh.  That should be the same as
return fillString + (toLength-length(fillString))*fillChar

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


Threads not Improving Performance in Program

2009-03-19 Thread Ryan Rosario
I have a parser that needs to process 7 million files. After running
for 2 days, it had only processed 1.5 million. I want this script to
parse several files at once by using multiple threads: one for each
file currently being analyzed.

My code iterates through all of the directories within a directory,
and at each directory, iterates through each file in that directory. I
structured my code something like this. I think I might be
misunderstanding how to use threads:

mythreads = []
for directory in dirList:
 #some processing...
 for file in fileList:
p = Process(currDir,directory,file)#class that extends thread.Threading
mythreads.append(p)
p.start()

for thread in mythreads:
 thread.join()
 del thread

The actual class that extends threading.thread is below:

class Process(threading.Thread):
vlock = threading.Lock()
def __init__(self,currDir,directory,file):  #thread constructor
threading.Thread.__init__(self)
self.currDir = currDir
self.directory = directory
self.file = file
def run(self):
redirect = re.compile(r'#REDIRECT',re.I)
xmldoc = minidom.parse(os.path.join(self.currDir,self.file))
try:
markup =
xmldoc.firstChild.childNodes[-2].childNodes[-2].childNodes[-2].childNodes[0].data
except:
#An error occurred
Process.vlock.acquire()
BAD = open("bad.log","a")
BAD.writelines(self.file + "\n")
BAD.close()
Process.vlock.release()
print "Error."
return
#if successful, do more processing...


I did an experiment with a variety of numbers of threads and there is
no performance gain. The code is taking the same amount of time to
process 1000 files as it would if the code did not use threads. Any
ideas on what I am doing wrong?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple question about yyyy/mm/dd

2009-03-19 Thread skip
>> Hi all, I need to receive in input a date represented by a string in
>> the form "/mm/dd" (or reversed), then I need to assure that the
>> date is = the current date and then split the dates in variables like
>> year, month, day. Is there some module to do this quickly?

The dateutil package has a parser module which works well here:

>>> import dateutil.parser, datetime
>>> dateutil.parser.parse("2008/03/31")
datetime.datetime(2008, 3, 31, 0, 0)
>>> dateutil.parser.parse("2008/03/31") > datetime.datetime.now()
False

-- 
Skip Montanaro - s...@pobox.com - http://www.smontanaro.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: DictReader and fieldnames

2009-03-19 Thread skip

Ted> Is it possible to grab the fieldnames that the csv DictReader
Ted> module automatically reads from the first line of the input file?

Like this, perhaps?

>>> rdr = csv.DictReader(open("f.csv", "rb"))
>>> rdr.fieldnames
['col1', 'col2', 'color']
>>> rdr.next()
{'color': '3', 'col2': '2', 'col1': '1'}

-- 
Skip Montanaro - s...@pobox.com - http://www.smontanaro.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can I rely on...

2009-03-19 Thread Terry Reedy

Emanuele D'Arrigo wrote:

Hi everybody,

I just had a bit of a shiver for something I'm doing often in my code
but that might be based on a wrong assumption on my part. Take the
following code:

pattern = "aPattern"

compiledPatterns = [ ]
compiledPatterns.append(re.compile(pattern))

if(re.compile(pattern) in compiledPatterns):


Note that for this generally take time proportional to the length of the 
list.  And as MRAB said, drop the parens.



print("The compiled pattern is stored.")

As you can see I'm effectively assuming that every time re.compile()
is called with the same input pattern it will return the exact same
object rather than a second, identical, object. In interactive tests
via python shell this seems to be the case but... can I rely on it -
always- being the case? Or is it one of those implementation-specific
issues?


As MRAB indicated, this only works because the CPython re module itself 
has a cache so you do not have to make one. It is, however, limited to 
100 or so since programs that use patterns repeatedly generally use a 
limited number of patterns.  Caches usually use a dict so that 
cache[input] == output and lookup is O(1).



And what about any other function or class/method? Is there a way to
discriminate between methods and functions that when invoked twice
with the same arguments will return the same object and those that in
the same circumstances will return two identical objects?


In general, a function that calculates and return an object will return 
a new object.  The exceptions are exceptions.




If the answer is no, am I right to state the in the case portrayed
above the only way to be safe is to use the following code instead?

for item in compiledPatterns:
   if(item.pattern == pattern):


Yes.  Unless you are comparing against None (or True or False in Py3) or 
specifically know otherwise, you probably want '==' rather than 'is'.


Terry Jan Reedy

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


Re: How complex is complex?

2009-03-19 Thread Kottiyath
On Mar 19, 9:33 pm, Kottiyath  wrote:
> On Mar 19, 8:42 pm, Paul McGuire  wrote:
>
>
>
> > On Mar 19, 4:39 am, Kottiyath  wrote:
>
> > > I understand that my question was foolish, even for a newbie.
> > > I will not ask any more such questions in the future.
>
> > Gaaah! Your question was just fine, a good question on coding style.
> > I wish more people would ask such questions so that bad habits could
> > be avoided.
>
> > The newbie posts that are annoying are the ones that:
> > - are answered on page 1 of any tutorial ("how do I get the second
> > character of a string?")
> > - are obvious homework assignments with no actual effort on the
> > poster's part ("how do I write a Python program to find the first 10
> > prime numbers?")
> > - pontificate on what is wrong with Python, based on 2 hours'
> > experience with the language (often titled "What's wrong with Python",
> > with content like "Python sucks because it doesn't have a switch
> > statement/has significant whitespace/doesn't check types of arguments/
> > isn't totally object-oriented like Java/doesn't have interfaces/...")
> > - are so vague as to be just Usenet noise (titled "Help me", with no
> > content, or "i need to write a program and don't know where to start
> > can someone write it for me?")
>
> > I think Daniel's joke was on the rest of us, who each had to chime in
> > with our favorite dict processing algorithm.
>
> > It *would* be good for you as a newbie to get an appreciation of the
> > topics that were covered in these responses, though, especially the
> > distinction between updating the dict in-place vs. creating a new
> > dict.
>
> > -- Paul
>
> Daniel, Sorry for misunderstanding your post. I hope I was not being
> passive-aggresive - (also because I found that the second mechanism I
> provided was quite horrible :-), so I was indeed being foolish
> there. )
>
> Paul/Aahz, I did understand 2 things
> (1) When using map always consider that the function will be called
> everytime, so the hit on the performance is more.
> (2) The second mechanism and the first mechanism provides different
> solutions (new dict/same dict)
> both of which I did not think about at all.
>
> Also, thank you everyone for all the help. I have been following this
> thread for the last 4 months (when I started with python) and I have
> learned a lot. The amount of help provided here is amazing.
>
> p.s. -> English is indeed not my first language :-)

Oops, Forgot to mention the biggest learning.

Readability is better than brevity -
Thanks to Rhodri.

This was a question which was bugging me all the time. When I look at
code, I am always envious when I see the same code written in much
smaller number of lines. Now, I will force myself to ask the questions
Rhodri proposed (esp: does it look uglier part) before deciding
whether or not to go ahead with brevity.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How complex is complex?

2009-03-19 Thread Kottiyath
On Mar 19, 8:42 pm, Paul McGuire  wrote:
> On Mar 19, 4:39 am, Kottiyath  wrote:
>
>
>
> > I understand that my question was foolish, even for a newbie.
> > I will not ask any more such questions in the future.
>
> Gaaah! Your question was just fine, a good question on coding style.
> I wish more people would ask such questions so that bad habits could
> be avoided.
>
> The newbie posts that are annoying are the ones that:
> - are answered on page 1 of any tutorial ("how do I get the second
> character of a string?")
> - are obvious homework assignments with no actual effort on the
> poster's part ("how do I write a Python program to find the first 10
> prime numbers?")
> - pontificate on what is wrong with Python, based on 2 hours'
> experience with the language (often titled "What's wrong with Python",
> with content like "Python sucks because it doesn't have a switch
> statement/has significant whitespace/doesn't check types of arguments/
> isn't totally object-oriented like Java/doesn't have interfaces/...")
> - are so vague as to be just Usenet noise (titled "Help me", with no
> content, or "i need to write a program and don't know where to start
> can someone write it for me?")
>
> I think Daniel's joke was on the rest of us, who each had to chime in
> with our favorite dict processing algorithm.
>
> It *would* be good for you as a newbie to get an appreciation of the
> topics that were covered in these responses, though, especially the
> distinction between updating the dict in-place vs. creating a new
> dict.
>
> -- Paul

Daniel, Sorry for misunderstanding your post. I hope I was not being
passive-aggresive - (also because I found that the second mechanism I
provided was quite horrible :-), so I was indeed being foolish
there. )

Paul/Aahz, I did understand 2 things
(1) When using map always consider that the function will be called
everytime, so the hit on the performance is more.
(2) The second mechanism and the first mechanism provides different
solutions (new dict/same dict)
both of which I did not think about at all.

Also, thank you everyone for all the help. I have been following this
thread for the last 4 months (when I started with python) and I have
learned a lot. The amount of help provided here is amazing.

p.s. -> English is indeed not my first language :-)
--
http://mail.python.org/mailman/listinfo/python-list


Simple question about yyyy/mm/dd

2009-03-19 Thread mattia
Hi all, I need to receive in input a date represented by a string in the
form "/mm/dd" (or reversed), then I need to assure that the date is 
>= the current date and then split the dates in variables like year, 
month, day. Is there some module to do this quickly?
--
http://mail.python.org/mailman/listinfo/python-list


Re: File Compare with difflib.context_diff

2009-03-19 Thread JohnV
Here is the latest version of the code:

currentdata_file = r"C:\Users\Owner\Desktop\newdata.txt" # the latest
download from the clock
lastdata_file = r"C:\Users\Owner\Desktop\mydata.txt" # the prior
download from the clock
output_file = r"C:\Users\Owner\Desktop\out.txt" # will hold delta
clock data

newdata = open(currentdata_file).read()[len(open(lastdata_file).read
()):]
newdata2 = newdata.strip()


file = open(output_file, 'w')
file.write(newdata2)
file.close()


Do I need to close currentdata_file and lastdata_file ?

Have not gotten the os.stat example to work yet...
thanks for the help.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Do deep inheritance trees degrade efficiency?

2009-03-19 Thread Terry Reedy

Terry Reedy wrote:

Anthra Norell wrote:
Would anyone who knows the inner workings volunteer to clarify whether 
or not every additional derivation of a class hierarchy adds an 
indirection to the base class's method calls and attribute read-writes. 


More potential search layers rather than pointer indirection.  But I 
doubt this is a bottleneck in very many programs.  So I would more 
concern myself first with quickly writing correct code.


I should add that when object access time matters and the object is 
accessed repeatedly, a common solution is to bind it to a local name 
*once* and then access it via the local (which is the fastest way 
possible for CPython and, I expect, for other implementation).  This 
works for all slower access methods.


tjr

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


Re: converting pipe delimited file to fixed width

2009-03-19 Thread MRAB

digz wrote:

Hi,
I am trying to convert a | delimited  file to fixed width by right
padding with spaces, Here is how I have written the program , just get
the feeling this can be done in a much better ( python functional )
way rather than the procedural code i have below . Any help
appreciated

#!/usr/bin/python
def rightFill(fillString, toLength, fillChar):
return fillString+''.join([fillChar for x in range(len
(fillString),toLength)])

fieldWidth=[ 14, 6, 18, 21, 21,4, 6  ];

file = open("/home/chatdi/input.csv", "r");
lines = file.readlines()
file.close()

out = open( "/home/chatdi/ouptut.csv", 'w')
for line in lines:
line = line[:-1]
index = 0
for token in line.split('|'):
paddedToken = rightFill(token, fieldWidth[index], ' ' )
out.write( paddedToken )
index = index + 1
out.write("\n")


Here's my version:

#!/usr/bin/python

field_widths = [14, 6, 18, 21, 21, 4, 6]

out = open("/home/chatdi/ouptut.csv", 'w')
for line in open("/home/chatdi/input.csv", "r"):
fields = line.rstrip().split('|')
padded_fields = [field.ljust(width) for field, width in zip(fields, 
field_widths)]

out.write("".join(padded_fields) + "\n")

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


Re: Can I rely on...

2009-03-19 Thread Albert Hopkins
On Thu, 2009-03-19 at 08:42 -0700, Emanuele D'Arrigo wrote:
> Hi everybody,
> 
> I just had a bit of a shiver for something I'm doing often in my code
> but that might be based on a wrong assumption on my part. Take the
> following code:
> 
> pattern = "aPattern"
> 
> compiledPatterns = [ ]
> compiledPatterns.append(re.compile(pattern))
> 
> if(re.compile(pattern) in compiledPatterns):
> print("The compiled pattern is stored.")
> 
> As you can see I'm effectively assuming that every time re.compile()
> is called with the same input pattern it will return the exact same
> object rather than a second, identical, object. In interactive tests
> via python shell this seems to be the case but... can I rely on it -
> always- being the case? Or is it one of those implementation-specific
> issues?
> 
> And what about any other function or class/method? Is there a way to
> discriminate between methods and functions that when invoked twice
> with the same arguments will return the same object and those that in
> the same circumstances will return two identical objects?
> 
> If the answer is no, am I right to state the in the case portrayed
> above the only way to be safe is to use the following code instead?
> 
> for item in compiledPatterns:
>if(item.pattern == pattern):

In general, no.  You cannot rely on objects instantiated with the same
parameters to be equal. Eg.:

>>> class N(object):
def __init__(self, foo):
self.foo = foo

>>> a = N('m')
>>> b = N('m')
>>> a == b
False

If, however, the designer of the class implements it as such
(and documents it as well) then you can. E.g:

>>> del N
>>> class N(object):
def __init__(self, foo):
self.foo = foo
def __eq__(self, other):
return self.foo == other.foo

>>> a = N('m')
>>> b = N('m')
>>> a == b
True

For functions/methods it really depends on the implementation.  For
example, do we *really* want the following to always be true? Even
though we passed the same arguments?

>>> import random
>>> random.randint(0, 10) == random.randint(0, 10)

For the re module, unless it's documented that 

>>> re.compile(p) == re.compile(p)

is always true then you should not rely on it, because it's an
implementation detail that may change in the future.


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


Re: Can I rely on...

2009-03-19 Thread MRAB

Emanuele D'Arrigo wrote:
[snip]

If the answer is no, am I right to state the in the case portrayed
above the only way to be safe is to use the following code instead?

for item in compiledPatterns:
   if(item.pattern == pattern):
print("The compiled pattern is stored.")
break


Correction to my last post: this isn't the same as using 'in'.

It should work, but remember that it compares only the pattern and not
any flags you might have used in the original re.compile().

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


Re: Can I rely on...

2009-03-19 Thread MRAB

Emanuele D'Arrigo wrote:

Hi everybody,

I just had a bit of a shiver for something I'm doing often in my code
but that might be based on a wrong assumption on my part. Take the
following code:

pattern = "aPattern"

compiledPatterns = [ ]
compiledPatterns.append(re.compile(pattern))

if(re.compile(pattern) in compiledPatterns):
print("The compiled pattern is stored.")


You don't need parentheses in the 'if', or the 'print' in Python 2.x.


As you can see I'm effectively assuming that every time re.compile()
is called with the same input pattern it will return the exact same
object rather than a second, identical, object. In interactive tests
via python shell this seems to be the case but... can I rely on it -
always- being the case? Or is it one of those implementation-specific
issues?


The re module has a cache of patterns, so if the pattern is already
known then it'll return the existing compiled pattern. However, the
cache has a limited size. In reality, no 2 pattern objects are equal.


And what about any other function or class/method? Is there a way to
discriminate between methods and functions that when invoked twice
with the same arguments will return the same object and those that in
the same circumstances will return two identical objects?

If the answer is no, am I right to state the in the case portrayed
above the only way to be safe is to use the following code instead?

for item in compiledPatterns:
   if(item.pattern == pattern):


This is the same as using 'in'.
--
http://mail.python.org/mailman/listinfo/python-list


converting pipe delimited file to fixed width

2009-03-19 Thread digz
Hi,
I am trying to convert a | delimited  file to fixed width by right
padding with spaces, Here is how I have written the program , just get
the feeling this can be done in a much better ( python functional )
way rather than the procedural code i have below . Any help
appreciated

#!/usr/bin/python
def rightFill(fillString, toLength, fillChar):
return fillString+''.join([fillChar for x in range(len
(fillString),toLength)])

fieldWidth=[ 14, 6, 18, 21, 21,4, 6  ];

file = open("/home/chatdi/input.csv", "r");
lines = file.readlines()
file.close()

out = open( "/home/chatdi/ouptut.csv", 'w')
for line in lines:
line = line[:-1]
index = 0
for token in line.split('|'):
paddedToken = rightFill(token, fieldWidth[index], ' ' )
out.write( paddedToken )
index = index + 1
out.write("\n")
--
http://mail.python.org/mailman/listinfo/python-list


Can I rely on...

2009-03-19 Thread Emanuele D'Arrigo
Sorry for the double-post, the first one was sent by mistake before
completion.

Hi everybody,

I just had a bit of a shiver for something I'm doing often in my code
but that might be based on a wrong assumption on my part. Take the
following code:

pattern = "aPattern"

compiledPatterns = [ ]
compiledPatterns.append(re.compile(pattern))

if(re.compile(pattern) in compiledPatterns):
print("The compiled pattern is stored.")

As you can see I'm effectively assuming that every time re.compile()
is called with the same input pattern it will return the exact same
object rather than a second, identical, object. In interactive tests
via python shell this seems to be the case but... can I rely on it -
always- being the case?

If the answer is no, am I right to state the in the case portrayed
above the only way to be safe is to use the following code instead?

for item in compiledPatterns:
   if(item.pattern == pattern):
print("The compiled pattern is stored.")
break

And what about any other function or class/method? Is there a way to
discriminate between methods and functions that when invoked twice
with the same arguments will return the same object and those that in
the same circumstances will return two identical objects? Or is it one
of those implementation-specific issues?

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


Can I rely on...

2009-03-19 Thread Emanuele D'Arrigo
Hi everybody,

I just had a bit of a shiver for something I'm doing often in my code
but that might be based on a wrong assumption on my part. Take the
following code:

pattern = "aPattern"

compiledPatterns = [ ]
compiledPatterns.append(re.compile(pattern))

if(re.compile(pattern) in compiledPatterns):
print("The compiled pattern is stored.")

As you can see I'm effectively assuming that every time re.compile()
is called with the same input pattern it will return the exact same
object rather than a second, identical, object. In interactive tests
via python shell this seems to be the case but... can I rely on it -
always- being the case? Or is it one of those implementation-specific
issues?

And what about any other function or class/method? Is there a way to
discriminate between methods and functions that when invoked twice
with the same arguments will return the same object and those that in
the same circumstances will return two identical objects?

If the answer is no, am I right to state the in the case portrayed
above the only way to be safe is to use the following code instead?

for item in compiledPatterns:
   if(item.pattern == pattern):
--
http://mail.python.org/mailman/listinfo/python-list


Re: How complex is complex?

2009-03-19 Thread Paul McGuire
On Mar 19, 4:39 am, Kottiyath  wrote:
>
> I understand that my question was foolish, even for a newbie.
> I will not ask any more such questions in the future.
>

Gaaah! Your question was just fine, a good question on coding style.
I wish more people would ask such questions so that bad habits could
be avoided.

The newbie posts that are annoying are the ones that:
- are answered on page 1 of any tutorial ("how do I get the second
character of a string?")
- are obvious homework assignments with no actual effort on the
poster's part ("how do I write a Python program to find the first 10
prime numbers?")
- pontificate on what is wrong with Python, based on 2 hours'
experience with the language (often titled "What's wrong with Python",
with content like "Python sucks because it doesn't have a switch
statement/has significant whitespace/doesn't check types of arguments/
isn't totally object-oriented like Java/doesn't have interfaces/...")
- are so vague as to be just Usenet noise (titled "Help me", with no
content, or "i need to write a program and don't know where to start
can someone write it for me?")

I think Daniel's joke was on the rest of us, who each had to chime in
with our favorite dict processing algorithm.

It *would* be good for you as a newbie to get an appreciation of the
topics that were covered in these responses, though, especially the
distinction between updating the dict in-place vs. creating a new
dict.

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


Re: Preferred syntax for the docstrings

2009-03-19 Thread Scott David Daniels

Luis Zarrabeitia wrote:

What's the preferred style to document code in python? ...
def somefunction(arg1, arg2, out = sys.stdout):
""" This function does blahblablha with the string arg1, using 
the tuple of ints arg2 as the control sequence, and prints the 
result to out (defaults to sys.stdout) """

... [or] ...
def somefunction(arg1, ar2, out = sys.stdout):
""" brief description, possibly involving arg1,  
arg2 and  arg3>

 arg1: string, some description
...
"""
I guess there are several languages for writing the docstring. The question is,
which is the preferred one in python, and where can I learn the syntax? (the one
that python documentation viewers understand better? the one used by the
stdlib?) How should/in what order should I write the docs? (brief description,
argument types, return type, followed perhaps by some doctests).


A fairly common style is to have the first line of the docstring
summarize the function (without repeating the arglist), a blank line,
and then more elaborating text.  Some systems (notably Idle) will
provide the arglist and that first line as a "floating hint" when
using functions/methods interactively.  Note how much of Python's
own library provide help this way.  Exploratory programming becomes
much easier when you follow that rule.

Try it, it is fun.  go into Idle and type:
>>> def f(a, bc, defg):
'''A trap: never use 'def' as an arg name.

In which Doris gets her oats.
'''
return 3

>>> f(
And at this point pause a second, the hint  will appear.  I use
both this and "print(a.b.__doc__)" regularly for reminders of what
I have once read.

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


  1   2   >