Re: stdin: processing characters

2006-05-01 Thread Kevin Simmons
Serge Orlov wrote:
> Cameron Laird wrote:
>> In article <[EMAIL PROTECTED]>,
>> Edward Elliott  <[EMAIL PROTECTED]> wrote:
>>> Kevin Simmons wrote:
 I have a python script that prompts the user for input from stdin via a
 menu. I want to process that input when the user types in two characters
 and not have to have the user press . As a comparison, in the bash
 shell one can use (read -n 2 -p "-->" CHOICE; case $CHOICE in...). Works
 great and is very
>>> I did something like this a couple years ago, curses was the easiest way I
>>> found to do it.  It's pretty painless with the wrapper function, which
>>> restores your terminal on error/exit.
>>>
>> Kevin, the bad news is that curses() is as good as Python gets in
>> this regard.  For better or worse, to the best of my knowledge,
>> unextended Python caNOT implement bash's read.  Here's the closest
>> small approximation I know:
>>
>>   import curses
>>   import os
>>
>>   msvcrt = curses.initscr()
>>   msvcrt.addstr("-->")
>>   first = msvcrt.getch()
>>   second = msvcrt.getch()
>>   os.system("stty echo -nl")
>>   print "\nThe two characters are '%s' and '%s'." % (first, second)
>>
>> I hope someone proves me wrong.
> 
> I'm not sure what "unextended Python" means, but on POSIX platforms
> termios module can disable echo and command line option -u can disable
> buffering. I think there should be a way to disable buffering after
> program started. Probably fcntl module.
> 
Thanks for your input. I found an answer that suits my needs, not curses
:-), but stty settings and sys.stdin.read(n) :

  import os, sys

  while 1:
  os.system("stty -icanon min 1 time 0")
  print """
  Radio computer control program.
  --
  Choose a function:
 po) Power toggle
 fq) Change frequency
 cm) Change mode
 vo) Change volume
 re) Reset
 qu) Quit
  -->""",
  func = sys.stdin.read(2)
  if func == "po":
  ...
  ... rest of menu actions ...
  elif func = "qu":
  os.system("stty cooked")
  sys.exit()

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


using break in exec()

2006-05-01 Thread Weber Matthias
Title: using break in exec()






Hi,

has anybody a suggestion to get this example to work? I have the need to jump out of the loop within the exec statement.

while True:
    exec("break")

Thanks
Matthias




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

Re: self modifying code

2006-05-01 Thread [EMAIL PROTECTED]
First of all, the test can be optimized by adding a boolean flag which
indicates if the data has been initialized or not, and then just
testing a boolean value instead of doing an "is" comparison, at the
cost of an extra global variable. But this is still ugly (actually
uglier IMO).


I think this is a more Pythonic way to do it.
This class implements a function which initializes itself upon the
first call:

class InitializingFunction(object):
def __init__(self, init):
def initializer(*args, **kw):
self.func = init()
return self(*args, **kw)
self.func = initializer
def __call__(self, *args, **kw):
return self.func(*args, **kw)

Now you can write your function almost exactly like you did before:

def init():
data = somethingcomplexandcostly()
def foo(a):
return simple(data, a)
return foo
func = InitializingFunction(init)

What have we gained from this? Two major advantages:
* no ugly 'global' statement
* no reliance on the function's name

And now you can easily create such functions forever using this class
to abstract away the ugly implementation ;)


Notice that since Function Decorators were introduced in Python2.4, you
can use InitializingFunction as a Decorator to achieve the same effect,
this time even without the need for a temporary name for a function:

@InitializingFunction
def func():
data = somethingcomplexandcostly()
def foo(a):
return simple(data, a)
return foo


And finally I must note that no matter which way you turn this around,
it will still be hard to read!

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


Converting floating point to string in non-scientific format

2006-05-01 Thread Madhusudhanan Chandrasekaran
Hi all:

When I try to convert a float variable into string via repr() or str()
function, i get the value as is, i.e '0.1e-7' in IEEE 754 format.
Instead how can force the created string to represent the floating
point in non-scientific fashion (non IEEE 754 format)? i.e
something like 0.1

Thanks in advance for your help.

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


Re: ending a string with a backslash

2006-05-01 Thread Dan Bishop
John Salerno wrote:
> I have this:
>
> subdomain = raw_input('Enter subdomain name: ')
>
> path = r'C:\Documents and Settings\John Salerno\My Documents\My
> Webs\1and1\johnjsalerno.com\' + subdomain
>
> Obviously the single backslash at the end of 'path' will cause a
> problem, and escaping it with a backslash seems to fix this problem, but
> how does escaping work when I already have it as a raw string? When I
> test it out and then print string, I get something like this:
>
> C:\Documents and Settings\John Salerno\My Documents\My
> Webs\1and1\johnjsalerno.com\\test
>
> But I don't see how this is valid, since all the backslashes are single
> (which is correct) except the last one. Somehow this still works when I
> tried to create the new directory -- os.mkdir(path) -- but I wasn't sure
> if this is the right way to go about it, or if there is some other,
> better way to handle the final backslash.

As others have stated, you can use a forward slash.  Alternatively, you
can write:

>>> r'This\string\contains\backslashes' '\\'
'This\\string\\contains\\backslashes\\'

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


Re: Converting floating point to string in non-scientific format

2006-05-01 Thread Dan Bishop
Madhusudhanan Chandrasekaran wrote:
> Hi all:
>
> When I try to convert a float variable into string via repr() or str()
> function, i get the value as is, i.e '0.1e-7' in IEEE 754 format.
> Instead how can force the created string to represent the floating
> point in non-scientific fashion (non IEEE 754 format)? i.e
> something like 0.1

Use the % operator with the f format (e.g., '%.7f' % x).

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


[ANN] markup.py 1.3 - an HTML and XML writer

2006-05-01 Thread Daniel Nogradi
The new 1.3 release of markup.py is available from

http://markup.sourceforge.net/

The markup module attempts to make it easier to output HTML or XML
from a python program without cluttering your code with tags in a
simple and flexible way.

Changes

  * improved documentation
  * function added to optionally escape < and > as &tl; and >
  * several functions added for frequently used HTML tag combinations
to speed up things
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: returning none when it should be returning a list?

2006-05-01 Thread Dan Bishop
Edward Elliott wrote:
[in reponse to some prime-number code]
> 5. you can do better than checking every odd number (next+2) to find the
> next prime, but I'm too tired to look into it right now.  it may require
> more complex machinery.

You only need to check the prime numbers up to sqrt(n).  If you're
calculating primes in sequential order, this is easy.  Otherwise, you
can remove a third of the odd divisors by considering only odd numbers
of the form 6k±1 (because 6k±3 is divisible by 3).

def potential_primes():
   yield 2
   yield 3
   i = 6
   while True:
  yield i - 1
  yield i + 1
  i += 6

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


Re: [ANN] markup.py 1.3 - an HTML and XML writer

2006-05-01 Thread Daniel Nogradi
> The new 1.3 release of markup.py is available from
>
> http://markup.sourceforge.net/
>
> The markup module attempts to make it easier to output HTML or XML
> from a python program without cluttering your code with tags in a
> simple and flexible way.
>
> Changes
>
>   * improved documentation
>   * function added to optionally escape < and > as &tl; and >
>   * several functions added for frequently used HTML tag combinations
> to speed up things

Ooops, I forgot the most important change resulting from a thread on
this list. Now if you want to use python keywords as keyword
arguments, such as class (happens often in HTML) or print then this
should be done as class_ or print_. This convention conforms to PEP-8.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OpenOffice UNO export PDF help needed

2006-05-01 Thread Michele Petrazzo
Sells, Fred wrote:
> I've geen googling for 3 days now, and cannot find out how to do
> this.
> 
> I'm trying to use OpenOffice 2.0 and UNO to generate PDF documents.
> I'm using windows, but will have to make it work under Linux for
> production. I've been able to set the parameters and call the
> exportToPdf method, but the exported file is not PDF but an .odt
> document,

Have you tried the ooextract.py found on:
http://udk.openoffice.org/python/python-bridge.html

Here work well and generate a pdf file.

See also this for more info about generate pdf:
http://mithrandr.moria.org/blog/447.html

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


Re: self modifying code

2006-05-01 Thread Robin Becker
[EMAIL PROTECTED] wrote:

> 
> What have we gained from this? Two major advantages:
> * no ugly 'global' statement
> * no reliance on the function's name

I don't dispute either of the above, however, the actual overhead of 
your approach appears to be much higher (see below) probably because it 
has two function calls instead on one to get the answer.




> 
> And now you can easily create such functions forever using this class
> to abstract away the ugly implementation ;)
... yes indeed

##file dingo.py
class InitializingFunction(object):
def __init__(self, init):
def initializer(*args, **kw):
self.func = init()
return self(*args, **kw)
self.func = initializer
def __call__(self, *args, **kw):
return self.func(*args, **kw)

def init():
data = 42
def foo(arg):
return arg+data
return foo
a = InitializingFunction(init)

def b(arg):
global b
data = 42
def b(arg):
return arg+data
return b(arg)
##

Testing with timeit
C:\Tmp>\Python\lib\timeit.py -s"from dingo import a;a(0)" a(1)
10 loops, best of 3: 2.25 usec per loop

C:\Tmp>\Python\lib\timeit.py -s"from dingo import b;b(0)" b(1)
100 loops, best of 3: 0.52 usec per loop

so since the simple function is fairly trivial the overhead seems to be 
around 4 times that of the weird approach.

The global naming stuff is pretty flaky and relies on the way names are 
looked up; in particular it seems as though references to the original 
global will be held at least throughout a single statement. If the first 
call is "print b(0),b(1)" then b is initialised twice.

This 'masterpiece of obfuscation' ;) gets round that problem, but is 
pretty odd to say the least and still relies on knowing the class name.

class Weird(object):
@staticmethod
def __call__(arg):
data = 42
def func(arg):
return arg+data
Weird.__call__ = staticmethod(func)
return func(arg)
c = Weird()

it is still more expensive than b, but not by much

C:\Tmp>\Python\lib\timeit.py -s"from dingo import c;c(1)" c(1)
100 loops, best of 3: 0.709 usec per loop

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


An Atlas of Graphs with Python

2006-05-01 Thread Giandomenico Sica
 Call for Cooperation
An Atlas of Linguistic Graphs

I'm a researcher in graph theory and networks.
I'm working about a project connected with the theory and the applications 
of
linguistic graphs, which are mathematical structures useful to represent
languages and consequently to manage the organization of data in different
kinds of scientific fields.
At the present I'm developing an application of these graphs to medicine,
specifically related to the ontology of clinical diseases.
And now to the purpose of this message, which is to ask if someone in this 
list
can be interested in collaborating with me about the construction of an open
source software useful to represent, to analyse and to compare linguistic
graphs.
I've developed the project but don't have the necessary programming skills 
to
proceed with the creation of the code.
The software would be distributed in public domain and the collaboration is 
free
and voluntary.
I really hope that someone can be interested.
In the case, please feel free to contact me by using my private e-mail 
address.
I'll be pleased to send the complete documentation related to the project.
Really many thanks.

All the best,
Giandomenico Sica

Faculty of Philosophy
Leiden University
[EMAIL PROTECTED]

Publications
http://www.polimetrica.com/polimetrica/view/people/Sica,_Giandomenico.html

1st World Congress and School on Universal Logic
http://www.uni-log.org 


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


Re: python and xulrunner

2006-05-01 Thread James Graham
[EMAIL PROTECTED] wrote:

> I've been trying to get xulrunner compiled with python (in windows) but
> have been unsuccessful.  I've been following this:
> .

I haven't tried it, but do the instructions at developer.mozilla.org[1] 
help? XULrunner in particular has changed quite a bit recently.

[1] http://developer.mozilla.org/en/docs/Building_PyXPCOM

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


Re: critique this little script, if you like

2006-05-01 Thread Alexis Roda
John Salerno escribió:
> 2. Between the if block or the try block, which is more Pythonic? 

Since the command line argument is optional I don't think it should be 
considered and exceptional condition if it's missing, so the "if" block 
looks better to me. No idea if this is more pythonic.

 > The
 > try block seems nicer because it doesn't have such an ugly-looking check
 > to make.

Ok, so you should do:

try:
 x = 1/i
except ZeroDivisionError :
 do_something_if_zero()

instead of

if (i == 0) :
   do_something_if_zero()




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


Re: ending a string with a backslash

2006-05-01 Thread Roel Schroeven
John Salerno schreef:
> Edward Elliott wrote:
>> John Salerno wrote:
>>> Obviously the single backslash at the end of 'path' will cause a
>>> problem, and escaping it with a backslash seems to fix this problem, but
>>> how does escaping work when I already have it as a raw string? 
>> see the first two items here:
>> http://www.ferg.org/projects/python_gotchas.html
> 
> #2 was very helpful. But if I want to use forward slashes instead, can I 
> just replace the backslashes with them, or must I use the 
> os.path.normcase() function to do it?

You can just replace them: all internal Windows functions accept forward 
slashed instead of backslashes in path names.

I think this is also the right time to mention os.path.join. It takes 
any number of path components and joins them, taking care of placing 
path delimiters between them. That means you could have written your 
code as follows:

path = os.path.join(r'C:\Documents and Settings\John Salerno\My 
Documents\My Webs\1and1\johnjsalerno.com', subdomain)

It also handles the case where there is a trailing backslash:

  >>> os.path.join('foo', 'bar')
'foo\\bar'
  >>> os.path.join('foo\\', 'bar')
'foo\\bar'

Greatly simplifies concatenating path components IMO.

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

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


Re: critique this little script, if you like

2006-05-01 Thread Roel Schroeven
Dave Jones schreef:
> Hi John!
> 
> About the path and the \'s, take a look at the os.path.join function.
> The function is smart enough to add the correct slash when joining
> directories.

But watch out with leading backslashes, as in

subdirs = [r'\cgi-bin', r'\images', r'\styles']

os.path.join will assume they are meant to be absolute paths, and will 
discard all previous components:

  >>> os.path.join('base', 'subdomain', r'\images')
'\\images'

In fact I think it's best to specify components without leading or 
trailing backslashes (unless you really want an absolute path):

  >>> os.path.join('base', 'subdomain', 'images')
'base\\subdomain\\images'

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

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


Re: critique this little script, if you like

2006-05-01 Thread Roel Schroeven
John Salerno schreef:
> John Salerno wrote:
>> Dave Jones wrote:
>>> Hi John!
>>>
>>> About the path and the \'s, take a look at the os.path.join function.
>>> The function is smart enough to add the correct slash when joining
>>> directories.
>>>
>>> Dave
>> Interesting, thanks! I was reading about the normcase() function, but 
>> your suggestion might be better.
> 
> Excellent, works great! But one more question: do I need to import 
> os.path for this? I tried it with and without it, and it worked both 
> ways, so I'm a little uncertain about whether os.path is really a 
> separate module, or if it is still contained within os and doesn't need 
> to be imported separately (it seems like it doesn't, but it is listed 
> separately in the docs).

I always thought I had to import it separately, but just yesterday I 
discovered import os and import os.path do the same thing. If I do one 
of the two, doesn't matter which one, I can access both os.listdir and 
os.path.join.

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

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


noob question: "TypeError" wrong number of args

2006-05-01 Thread Holger
Hi guys

Tried searching for a solution to this, but the error message is so
generic, that I could not get any meaningfull results.

Anyways - errormessage:

TypeError: addFile() takes exactly 1 argument (2 given)


The script is run with two args "arg1" and "arg2":

import sys

class KeyBase:
def addFile(file):
print "initialize the base with lines from this file"

print "These are the args"
print "Number of args %d" % len(sys.argv)
print sys.argv
print sys.version_info
print sys.version

f = sys.argv[1]
print "f = '%s'" % f
b = KeyBase()

b.addFile(f)


The output - including error message
(looks like stdout and stderr are a bit out of sync...):

These are the args
Traceback (most recent call last):

Number of args 3
['C:\\home\\<.. bla bla snip ...>\\bin\\test.py', 'arg1', 'arg2']
(2, 4, 2, 'final', 0)
2.4.2 (#67, Oct 30 2005, 16:11:18) [MSC v.1310 32 bit (Intel)]
f = 'arg1'

  File "C:\Program Files\ActiveState Komodo
3.5\lib\support\dbgp\pythonlib\dbgp\client.py", line 1806, in runMain
self.dbg.runfile(debug_args[0], debug_args)
  File "C:\Program Files\ActiveState Komodo
3.5\lib\support\dbgp\pythonlib\dbgp\client.py", line 1529, in runfile
h_execfile(file, args, module=main, tracer=self)
  File "C:\Program Files\ActiveState Komodo
3.5\lib\support\dbgp\pythonlib\dbgp\client.py", line 590, in __init__
execfile(file, globals, locals)
  File "C:\home\hbille\projects\bc4rom\bin\test.py", line 20, in
__main__
b.addFile(f)
TypeError: addFile() takes exactly 1 argument (2 given)


I'm running this inside ActiveState Komodo on WinXP.

Hope one you wizards can give me pointers to either what I'm doing
wrong or maybe advise me what to modify in my setup.

Thank you!

Regards,
Holger

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


Re: noob question: "TypeError" wrong number of args

2006-05-01 Thread Fredrik Lundh
Holger wrote:

> Tried searching for a solution to this, but the error message is so
> generic, that I could not get any meaningfull results.
>
> Anyways - errormessage:
> 
> TypeError: addFile() takes exactly 1 argument (2 given)
> 
>
> The script is run with two args "arg1" and "arg2":
> 
> import sys
>
> class KeyBase:
> def addFile(file):
> print "initialize the base with lines from this file"

when defining your own classes, you must spell out the "self"
argument in your method definitions:

def addFile(self, file):
print "initialize the base with lines from this file"

see:

http://pyfaq.infogami.com/what-is-self





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


Re: basic python programing

2006-05-01 Thread Steve Holden
Grant Edwards wrote:
> On 2006-04-30, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> 
> 
>>here we  discuss the most basic concepts  about python.
> 
> 
> Yes we do.
> 
> As well as the most basic concepts about Usenet.
> 

Yes, sir. BasicConceptsRUs.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Love me, love my blog  http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

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


Re: noob question: "TypeError" wrong number of args

2006-05-01 Thread Holger
oops, that was kinda embarrassing.
But thx anyway :-)

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


Re: python and xulrunner

2006-05-01 Thread joe . hrbek
Well, it's my understanding (I could be wrong), that pyxpcom is to
enable firefox/mozilla to use python.  My interest is more in the area
of xulrunner.  I've read that page several times and pulled
compiler/linker build steps from it, but never followed it exactly. I
suppose I can try pyxpcom instead.  Maybe if i can compile that i could
coax xulrunner into compiling. Unfortunately, most of the windows
pieces are listed as "todo" and "there are no special build
instructions", which is why i've been taking pieces from several
different sources and trying to make it work. :(

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


Re: SPE / winpdb problem

2006-05-01 Thread Steve Holden
MakaMaka wrote:
> Hi All,
> I'm having a problem with Stani's Python Editor (SPE) that I'm hoping
> somebody has run into before.  If I create a simple hello world
> program:
> 
> print "hello"
> 
> and try and run it in winpdb via the SPE command ALT-F9, the program
> does not launch and the following results (It's at the bottom):
> 
> 
> Spe Warning: Spe was developped on wxPython v2.6.1.0., but v2.6.3.2.
> was found.
> If you experience any problems please install wxPython v2.6.1.0.
> 
> 
> SPE v0.8.2.a (c)2003-2005 www.stani.be
> 
> If spe fails to start:
>  - type "python SPE.py --debug > debug.txt 2>&1" at the command prompt
>(or if you use tcsh: "python SPE.py --debug >& debug.txt")
>  - send debug.txt with some info to spe.stani.be[at]gmail.com
> 
> 
> Spe Warning: Spe was developped on wxPython v2.6.1.0., but v2.6.3.2.
> was found.
> If you experience any problems please install wxPython v2.6.1.0.
> 
> Blender support disabled (run SPE inside Blender to enable).
> 
> Encrypted debugging disabled.
>   If you prefer encrypted debugging, install the "Python Cryptography
> Toolkit"
>   from http://www.amk.ca/python/code/crypto
> 
> Launching application...
> Traceback (most recent call last):
>   File "C:\Python24\lib\site-packages\_spe\plugins\winpdb\winpdb.py",
> line 3313,
>  in ?
> ret = main()
>   File "C:\Python24\lib\site-packages\_spe\plugins\winpdb\winpdb.py",
> line 3303,
>  in main
> return rpdb2.main(StartClient)
>   File "C:\Python24\Lib\site-packages\_spe\plugins\winpdb\rpdb2.py",
> line 7221,
> in main
> StartClient_func(_args, fAttach, fchdir, pwd, fAllowUnencrypted,
> fRemote, ho
> st)
>   File "C:\Python24\lib\site-packages\_spe\plugins\winpdb\winpdb.py",
> line 3293,
>  in StartClient
> app = CWinpdbApp(sm, fchdir, command_line, fAttach,
> fAllowUnencrypted)
>   File "C:\Python24\lib\site-packages\_spe\plugins\winpdb\winpdb.py",
> line 1528,
>  in __init__
> wx.App.__init__(self, redirect = False)
>   File "C:\Python24\Lib\site-packages\wx-2.6-msw-ansi\wx\_core.py",
> line 7700, i
> n __init__
> self._BootstrapApp()
>   File "C:\Python24\Lib\site-packages\wx-2.6-msw-ansi\wx\_core.py",
> line 7352, i
> n _BootstrapApp
> return _core_.PyApp__BootstrapApp(*args, **kwargs)
>   File "C:\Python24\lib\site-packages\_spe\plugins\winpdb\winpdb.py",
> line 1533,
>  in OnInit
> self.m_settings.load_settings()
>   File "C:\Python24\lib\site-packages\_spe\plugins\winpdb\winpdb.py",
> line 638,
> in load_settings
> d = cPickle.load(f)
> EOFError
> 
> 
> 
> Any ideas what is going on?
> 
> Thanks!
> 
I'd try installing wxPython v2.6.1.0 and see if that gets rid of your 
error. Multiple versions can coexist.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Love me, love my blog  http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

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


Re: using break in exec()

2006-05-01 Thread Steve Holden
Weber Matthias wrote:
> Hi,
> 
> has anybody a suggestion to get this example to work? I have the need to 
> jump out of the loop within the exec statement.
> 
> while True:
> exec("break")
> 

You can't do that, just as you can't break out of a loop by executing a 
break within a function that's called inside the loop. Python insists 
that break only occurs inside a loop construct.

You will have use some construct like

while True:
   try:
  exec(your_code)
   except SomeException:
  break

then have your executed code raise the appropriate exception to break 
out of the loop.

If you'll forgive me saying so you seem to be relatively new to 
programming, and newcomers often make inappropriate use of "exec", which 
should really only be a last resort.

If you explain the problem in a little more depth (i.e. say exactly what 
it is you are trying to do) you might get more helpful suggestion.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Love me, love my blog  http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

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


Re: How to prevent this from happening?

2006-05-01 Thread Serge Orlov
[EMAIL PROTECTED] wrote:
> Regarding this expression:  1 << x
>
> I had a bug in my code that made x become Very Large - much larger than
> I had intended. This caused Python, and my PC, to lock up tight as a
> drum, and it appeared that the Python task (Windows XP) was happily and
> rapidly consuming all available virtual memory.
>
> Presumably, Python was trying to create a really really long integer,
> just as I had asked it.
>
> Is there a way to put a limit on Python, much like there is a stack
> limit, so that this sort of thing can't get out of hand?

This is a general problem regardless of programming language and it's
better solved by OS. Windows has API for limiting resource usage but it
lacks user tools. At least I'm not aware of them, maybe *you* can find
them. There is Windows System Resource Manager
http://www.microsoft.com/technet/downloads/winsrvr/wsrm.mspx It won't
run on Windows XP, but you may take a look at its distribution CD
image. If you're lucky maybe there is a command line tool for Windows
XP.

Alternatively you can switch to a better OS ;) Any Unix-like (Max OS X,
Linux, *BSD, etc...), they all have resource usage limiting tools out
of the box.

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


Re: returning none when it should be returning a list?

2006-05-01 Thread Roy Smith
[EMAIL PROTECTED] wrote:

Several people have already given you good answers as to why you're getting 
None, and how to improve the algorithm you're using.  I want to address 
some stylistic questions.

First, is the

> if(next >= lim):
> print(seed)
> return seed
> else:
> count = 0;
> [...]

construct.  You don't need the else part, since the if clause ends with a 
return.  You can just un-indent one stop and put everything that is now 
inside the "else" at the same level as the "if".  This makes your program 
easier to read and understand.  Your program isn't too bad because it's 
only got about a dozen lines of code in the "else", and you only end up 
about 4 indents deep.  In larger programs, you can end up with 100's of 
lines of code and 5, 6, or more indents.  Then it's a nightmare to 
understand.

The other sylistic issue is this:

> if(count == len(seed)):
> seed.append(next)
> findPrime(seed, next+2, lim)
> else:
> findPrime(seed, next+2, lim)

You've repeated the call to findPrime().  You refactor that out like:

 if(count == len(seed)):
 seed.append(next)
 findPrime(seed, next+2, lim)

Three lines of code instead of five, and no repeated code.  Easier to 
understand.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OOP techniques in Python

2006-05-01 Thread Philippe Martin
Thanks,

Did not know that.

Philippe



Dennis Lee Bieber wrote:

> On Thu, 27 Apr 2006 14:32:15 -0500, Philippe Martin
> <[EMAIL PROTECTED]> declaimed the following in comp.lang.python:
> 
>> 
>> What then is the point of the double underscore (if any) ?:
> 
> To prevent masking/shadowing of inherited attributes...
> 
 class A(object):
> ...   def __init__(self):
> ...   self.__WhoMe = "From A"
> ...   print "A : ", dir(self)
> ...   super(A, self).__init__()
> ...
 class B(object):
> ...   def __init__(self):
> ...   self.__WhoMe = 42
> ...   print "B : ", dir(self)
> ...   super(B, self).__init__()
> ...
 class Confusion(A, B):
> ...   def __init__(self):
> ...   self.__WhoMe = "I'm confuzzled"
> ...   print "Confusion: ", dir(self)
> ...   super(Confusion, self).__init__()
> ...
 cab = Confusion()
> Confusion:  ['_Confusion__WhoMe', '__class__', '__delattr__',
> '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__',
> '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
> '__setattr__', '__str__', '__weakref__']
> A :  ['_A__WhoMe', '_Confusion__WhoMe', '__class__', '__delattr__',
> '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__',
> '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
> '__setattr__', '__str__', '__weakref__']
> B :  ['_A__WhoMe', '_B__WhoMe', '_Confusion__WhoMe', '__class__',
> '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__',
> '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
> '__repr__', '__setattr__', '__str__', '__weakref__']
 
> 
> Note that A, B, and Confusion each have "__WhoMe". Also notice how
> each __init__ invokes the parent module __init__; each one adds its
> __WhoMe to the object without masking those defined in others.
> 
> Without the __, you'd have only ONE attribute after all of that; as
> shown next...
> 
 class A(object):
> ...   def __init__(self):
> ...   self._WhoMe = "From A"
> ...   print "A : ", dir(self)
> ...   super(A, self).__init__()
> ...
 class B(object):
> ...   def __init__(self):
> ...   self._WhoMe = 42
> ...   print "B : ", dir(self)
> ...   super(B, self).__init__()
> ...
 class Confusion(A, B):
> ...   def __init__(self):
> ...   self._WhoMe = "I'm confuzzled"
> ...   print "Confusion: ", dir(self)
> ...   super(Confusion, self).__init__()
> ...
 cab2 = Confusion()
> Confusion:  ['_WhoMe', '__class__', '__delattr__', '__dict__',
> '__doc__', '__getattribute__', '__hash__', '__init__', '__module__',
> '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
> '__str__', '__weakref__']
> A :  ['_WhoMe', '__class__', '__delattr__', '__dict__', '__doc__',
> '__getattribute__', '__hash__', '__init__', '__module__', '__new__',
> '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
> '__weakref__']
> B :  ['_WhoMe', '__class__', '__delattr__', '__dict__', '__doc__',
> '__getattribute__', '__hash__', '__init__', '__module__', '__new__',
> '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
> '__weakref__']
 
> --
>  > == <
>  >   [EMAIL PROTECTED]  | Wulfraed  Dennis Lee Bieber  KD6MOG <
>  >  [EMAIL PROTECTED] |   Bestiaria Support Staff   <
>  > == <
>  >   Home Page: <
>  >Overflow Page: <

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


Re: stdin: processing characters

2006-05-01 Thread Serge Orlov
Kevin Simmons wrote:
> Thanks for your input. I found an answer that suits my needs, not curses
> :-), but stty settings and sys.stdin.read(n) :
>
>   import os, sys
>
>   while 1:
>   os.system("stty -icanon min 1 time 0")
>   print """
>   Radio computer control program.
>   --
>   Choose a function:
>  po) Power toggle
>  fq) Change frequency
>  cm) Change mode
>  vo) Change volume
>  re) Reset
>  qu) Quit
>   -->""",
>   func = sys.stdin.read(2)
>   if func == "po":
>   ...
>   ... rest of menu actions ...
>   elif func = "qu":
>   os.system("stty cooked")
>   sys.exit()
>

Looks reasonable if you don't need portability. But you may want to
refactor it a little bit to make sure terminal setting are always
restored:

try:
do_all_the_work()
finally:
os.system("stty cooked")

P.S. Maybe its me, but when I see call sys.exit() I always have a gut
feeling this function never returns. But in fact my I'm wrong and
sys.exit is more reasonable: it raises exception. So you can call
sys.exit() inside do_all_the_work and you can still be sure that
os.system("stty cooked") is always executed at the end.

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


Re: installing pyodbc

2006-05-01 Thread timw.google
No I didn't. Thanks for the reply. I've moved on to mx.ODBC.

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


Python based file format cracking

2006-05-01 Thread Wesley Brooks
Dear Users,Is there another tool that can examine an string for undefined repeating patterns? I'm aware of and have limited experience regular expressions which finds supplied patterns within a string. Yours Faithfully,
Wesley Brooks
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Measure memory usage in Python

2006-05-01 Thread Rune Strand

gene tani wrote:
> Rune Strand wrote:
> > Is there a way to measure how much memory a data structure use? For
> > instance, what is the footprint of a particular list object like
> > [0,1,2,3,4,5,6,7,8,9]?
>
> i have a note to try this, but haven't got around to it, if you want to
> blog/post
>
> http://pysizer.8325.org/

Thank you!  That seems to be what I was looking for.

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


Re: noob question: "TypeError" wrong number of args

2006-05-01 Thread Ben Finney
"Holger" <[EMAIL PROTECTED]> writes:

> 
> TypeError: addFile() takes exactly 1 argument (2 given)
> 
> 
> 
> import sys
> 
> class KeyBase:
> def addFile(file):
> print "initialize the base with lines from this file"

You've misunderstood -- or never followed -- the tutorial, especially
how Python does object methods. Please follow the whole tutorial
through, understanding each example as you work through it. You'll
then have a solid basis of knowledge to go on with.

http://docs.python.org/tut/>

-- 
 \ "We are not gonna be great; we are not gonna be amazing; we are |
  `\gonna be *amazingly* amazing!"  -- Zaphod Beeblebrox, _The |
_o__)Hitch-Hiker's Guide To The Galaxy_, Douglas Adams |
Ben Finney

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


Re: self modifying code

2006-05-01 Thread [EMAIL PROTECTED]
Yes, my implementation was less efficient because of the extra function
call.

> class Weird(object):
> @staticmethod
> def __call__(arg):
> data = 42
> def func(arg):
> return arg+data
> Weird.__call__ = staticmethod(func)
> return func(arg)
> c = Weird()

Ugh... you've used a class just like a function. You can't have two
different objects of this class, since you are overriding a static
method of the class! And you've hard-coded the data into the class
definition. Yes, it works, but I would never, ever trust such code to
someone else to maintain.

And you'll have to manually define such a class for every such
function. That's not very Pythonic.

Here's a reusable function that will define such a class for you, as
well as hide most of the ugliness (in fact, it supports -exactly- the
same interface as my previous implementation):

def InitializingFunction(func):
class temp:
@staticmethod
def __call__(*args, **kw):
temp.__call__ = staticmethod(func())
return temp.__call__(*args, **kw)
return temp()

@InitializingFunction
def func():
data = somethingcomplexandcostly()
def foo(a): 
return simple(data, a) 
return foo

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


Re: Measure memory usage in Python

2006-05-01 Thread gene tani

Rune Strand wrote:
> gene tani wrote:
> > Rune Strand wrote:
> > > Is there a way to measure how much memory a data structure use? For
> > > instance, what is the footprint of a particular list object like
> > > [0,1,2,3,4,5,6,7,8,9]?
> >
> > i have a note to try this, but haven't got around to it, if you want to
> > blog/post
> >
> > http://pysizer.8325.org/
>
> Thank you!  That seems to be what I was looking for.

ok, my notes are disorganized:

http://guppy-pe.sourceforge.net/#Heapy
http://www.softwareverify.com/

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


Re: returning none when it should be returning a list?

2006-05-01 Thread randomtalk

John Machin wrote:
>
> That's because it "falls off the end" of the function, which causes it
> to return None. However that's not your only problem. Major other
> problem is updating "seed" in situ.
>
I'm not sure what "falls off the end" of the function means, i searched
online, it seems to mean that the function has reached the end
prematurely and returned a default identifier to signal success or
not.. Can you please explain what that means?

Other than that, thanks alot for all those who posted! It's been very
educational!

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


regd efficient methods to manipulate *large* files

2006-05-01 Thread Madhusudhanan Chandrasekaran
Hi:

This question is not directed "entirely" at python only. But since
I want to know how to do it in python, I am posting here.


I am constructing a huge matrix (m x n), whose columns n are stored in
smaller files. Once I read m such files, my matrix is complete. I want to
pass this matrix as an input to another script of mine (I just have the
binary.) Currently, the script reads a file (which is nothing but the
matrix) and processes it. Is there any way of doing this in memory,
without writing the matrix onto the disk?

Since I have to repeat my experimentation for multiple iterations, it
becomes expensive to write the matrix onto the disk.

Thanks in advance. Help appreciated.

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


Re: python and xulrunner

2006-05-01 Thread Michel Claveau
Hi!

My answer is (perhaps a little) hard, but...

I read pages and pages, and traversed sites and sites.

Conclusion :
 - XPcom functions, perhaps, on some special configurations.
 - xulrunner is an idea (vapor?)

But, in production, nothing of all that is usable  (for the moment?)

-- 
@-salutations

Michel Claveau


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


Re: ending a string with a backslash

2006-05-01 Thread John Salerno
Roel Schroeven wrote:

> I think this is also the right time to mention os.path.join. 

Yeah, I ended up using this and it looks a lot nicer too. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: critique this little script, if you like

2006-05-01 Thread John Salerno
Roel Schroeven wrote:

> But watch out with leading backslashes, as in
> 
> subdirs = [r'\cgi-bin', r'\images', r'\styles']
> 
> os.path.join will assume they are meant to be absolute paths, and will 
> discard all previous components:
> 
>  >>> os.path.join('base', 'subdomain', r'\images')
> '\\images'
> 
> In fact I think it's best to specify components without leading or 
> trailing backslashes (unless you really want an absolute path):
> 
>  >>> os.path.join('base', 'subdomain', 'images')
> 'base\\subdomain\\images'

Yeah, I made the list of subdirectories just strings, like 'cgi-bin' and 
took out the backslash. Then I used os.path.join to create those 
subdirectories on the end of the main directory. It always amazes me how 
posting here can lead to little tweaks that really clean up my code. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: critique this little script, if you like

2006-05-01 Thread John Salerno
Alexis Roda wrote:
> John Salerno escribió:
>> 2. Between the if block or the try block, which is more Pythonic? 
> 
> Since the command line argument is optional I don't think it should be 
> considered and exceptional condition if it's missing, so the "if" block 
> looks better to me. No idea if this is more pythonic.
> 
>  > The
>  > try block seems nicer because it doesn't have such an ugly-looking check
>  > to make.
> 
> Ok, so you should do:
> 
> try:
> x = 1/i
> except ZeroDivisionError :
> do_something_if_zero()

Why do you think that this try block is preferred over what I have (if I 
were to stick with the try block)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to efficiently read binary files?

2006-05-01 Thread Grant Edwards
On 2006-04-30, Robert Kern <[EMAIL PROTECTED]> wrote:
> Grant Edwards wrote:
>> On 2006-04-30, David Lees <[EMAIL PROTECTED]> wrote:
>> 
>>>I want to process large binary files (>2GB) in Python.  I have played 
>>>around with prototypes in pure Python and profiled the code.  Most of 
>>>the time seems to be spent converting back and forth to and from strings 
>>>using the struct module.  Is there a way to directly read into an array 
>>>of integers in Python?
>> 
>> Perhaps the numarray module?
>
> numpy for new code, please.

So numarray and numpy were both written to replace numeric?

-- 
Grant Edwards   grante Yow!  I want to kill
  at   everyone here with a cute
   visi.comcolorful Hydrogen Bomb!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: self modifying code

2006-05-01 Thread Robin Becker
[EMAIL PROTECTED] wrote:
> Yes, my implementation was less efficient because of the extra function
> call.
> 
>> class Weird(object):
>> @staticmethod
>> def __call__(arg):
>> data = 42
>> def func(arg):
>> return arg+data
>> Weird.__call__ = staticmethod(func)
>> return func(arg)
>> c = Weird()
> 

ugh indeed

> Ugh... you've used a class just like a function. You can't have two
> different objects of this class, since you are overriding a static
> method of the class! And you've hard-coded the data into the class
> definition. Yes, it works, but I would never, ever trust such code to
> someone else to maintain.
> 
> And you'll have to manually define such a class for every such
> function. That's not very Pythonic.

no arguments here


> 
> Here's a reusable function that will define such a class for you, as
> well as hide most of the ugliness (in fact, it supports -exactly- the
> same interface as my previous implementation):
> 
> def InitializingFunction(func):
> class temp:
> @staticmethod
> def __call__(*args, **kw):
> temp.__call__ = staticmethod(func())
> return temp.__call__(*args, **kw)
> return temp()
> 
> @InitializingFunction
> def func():
> data = somethingcomplexandcostly()
> def foo(a): 
> return simple(data, a) 
> return foo
> 

I already tried this kind of factory function, but in fact I think the 
original global test version outperforms even the statically coded Weird 
class.

ie
data = None
def d(arg):
global data
if data is None:
data = 42
return arg+data

@InitializingFunction
def e():
data = 43
def foo(a):
return data+a
return foo

are both better than any except the global function replacement nonsense

C:\Tmp>\Python\lib\timeit.py -s"from dingo import d;d(0)" d(1)
100 loops, best of 3: 0.556 usec per loop

C:\Tmp>\Python\lib\timeit.py -s"from dingo import e;e(0)" e(1)
100 loops, best of 3: 1.09 usec per loop

but the structured approach is still twice as slow as the simplistic one :(
-- 
Robin Becker
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: returning none when it should be returning a list?

2006-05-01 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

> John Machin wrote:
> >
> > That's because it "falls off the end" of the function, which causes it
> > to return None. However that's not your only problem. Major other
> > problem is updating "seed" in situ.
> >
> I'm not sure what "falls off the end" of the function means, i searched
> online, it seems to mean that the function has reached the end
> prematurely and returned a default identifier to signal success or
> not.. Can you please explain what that means?

it means exactly what it says: that execution of the function body reaches
the end of the function without seeing an explicit "return" statement.

Python handles this by inserting an extra "return" statement at the end, to
make sure there's always something to return (a plain "return" returns None
to the caller).





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


Setting a module package to use new-style classes

2006-05-01 Thread Panos Laganakos
Is there a way to have a whole module package use the new-style
classes, without having to specify it per module-file or even worse,
per class definition?

Maybe by declaring the __metaclass__ in the module's __init__.py?

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


list*list

2006-05-01 Thread BBands
There must be a better way to multiply the elements of one list by
another:

a = [1,2,3]
b = [1,2,3]
c = []
for i in range(len(a)):
c.append(a[i]*b[i])
a = c
print a
[1, 4, 9]

Perhaps a list comprehension or is this better addressed by NumPy?

Thanks,

 jab

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


Re: list*list

2006-05-01 Thread Tim Williams
On 1 May 2006 08:28:12 -0700, BBands <[EMAIL PROTECTED]> wrote:
There must be a better way to multiply the elements of one list byanother:a = [1,2,3]b = [1,2,3]c = []for i in range(len(a)):c.append(a[i]*b[i])a = cprint a[1, 4, 9]

Something like:

>>> [ x * y  for x,y in zip(a,b) ]
[1, 4, 9]

:)


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

Re: list*list

2006-05-01 Thread Diez B. Roggisch
> There must be a better way to multiply the elements of one list by
> another:
> 
> a = [1,2,3]
> b = [1,2,3]
> c = []
> for i in range(len(a)):
> c.append(a[i]*b[i])
> a = c
> print a
> [1, 4, 9]
> 
> Perhaps a list comprehension or is this better addressed by NumPy?


First of all: it's considered bad style to use range if all you want is a
enumeration of indices, as it will actually create a list of the size you
specified. Use xrange in such cases. 

You can use a listcomp like this:

c = [a[i] * b[i] for i in xrange(len(a))]

But maybe nicer is zip:

c = [av * bv for av, bv in zip(a, b)]

And if lists get large and perhaps multidemnsional, numpy certainly is the
way to go.

Diez

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


Re: critique this little script, if you like

2006-05-01 Thread Alexis Roda
John Salerno escribió:
> Alexis Roda wrote:
>>
>>  > The
>>  > try block seems nicer because it doesn't have such an ugly-looking 
>> check
>>  > to make.
>>
> 
> Why do you think that this try block is preferred over what I have (if I 
> were to stick with the try block)?

My example has nothing to do with your script, apart from trying to 
illustrate why I think you should use "if" instead of "try" with a kind 
of "reductio ad absurdum" reasoning.

To me i == 0 seems as ugly as len(sys.argv) == 1 (*), so, according to 
your statement about ugly checks, you should avoid code like:

if i == 0 :
 do_something_if_zero()

and write something like:

try:
 x = 1 / i
except ZeroDivisionError :
 do_something_if_zero()

but probably you wouldn't, the "if" block seems more "natural" than the 
convoluted "try" block, so ...




Regards

(*) len(sys.argv) == 1 is exactly the same as len(sys.argv) - 1 == 0
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting floating point to string in non-scientific format

2006-05-01 Thread ernesto . adorio
See also non-exponential floating point representation in
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/358361 by R.
Hettinger.

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


Re: list*list

2006-05-01 Thread Tim Chase
> There must be a better way to multiply the elements of one list by
> another:
> 
> a = [1,2,3]
> b = [1,2,3]
> c = []
> for i in range(len(a)):
>   c.append(a[i]*b[i])
> a = c
> print a
> [1, 4, 9]
> 
> Perhaps a list comprehension or is this better addressed by NumPy?

a = [1,2,3]
b = [1,2,3]
c = [q*r for q,r in zip(a,b)]

seems to do the trick for me.

-tim




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


[ANN] Movable Python 1.0.2

2006-05-01 Thread Fuzzyman
`Movable Python 1.0.2 `_ is
now available.

This release if for the Python 2.4 distribution of **Movable Python**,
and is now available for `download
`_.

Features new to this release include :

* Now built with Python 2.4.3.

* Other updated modules include :

- pywin32 build 208
- ctypes 0.9.9.6
- wxPython 2.6.3.2
- Firedrop 0.2.1
- ConfigObj 4.3.1
- Wax 0.3.33

* Scripts (and ``customize.py``) are now executed in a specific
namespace, no
  more movpy cruft.
* When entering interactive mode (``movpy -``), any *additional*
command line
  arguments are passed to IPython.
* ``imp.find_module`` has been fixed to work with modules contained in
the
  zipfile. This fix doesn't write any temporary files, but
``imp.load_module``
  has been patched to accept a ``StringIO`` instance.
* Built in support for *matplotlib* interactive sessions. (``movpy -
pylab``)
* Verified that ``__future__`` statements are handled correctly.
* New look documentation and website.

To try the new `matplotlib `_
support, you'll need the `matplotlib files
`_ in your ``lib/``
directory. You can then run the following at the command line :

``movpy.exe - -pylab``

This should drop you straight into a IPython session, with pylab
enabled.

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


Re: An Atlas of Graphs with Python

2006-05-01 Thread Cameron Laird
In article <[EMAIL PROTECTED]>,
Giandomenico Sica <[EMAIL PROTECTED]> wrote:
> Call for Cooperation
>An Atlas of Linguistic Graphs
>
>I'm a researcher in graph theory and networks.
>I'm working about a project connected with the theory and the applications 
>of
>linguistic graphs, which are mathematical structures useful to represent
>languages and consequently to manage the organization of data in different
>kinds of scientific fields.
>At the present I'm developing an application of these graphs to medicine,
>specifically related to the ontology of clinical diseases.
>And now to the purpose of this message, which is to ask if someone in this 
>list
>can be interested in collaborating with me about the construction of an open
>source software useful to represent, to analyse and to compare linguistic
>graphs.
>I've developed the project but don't have the necessary programming skills 
>to
>proceed with the creation of the code.
>The software would be distributed in public domain and the collaboration is 
>free
>and voluntary.
.
.
.
Much as I'd love personally to take up this opportunity,
previous commitments preclude it.  I wonder whether it
might attract someone at Google?  They certainly have
an interest in analytic linguistics and familiarity with
high-level languages; while Summer of Code might appear
superficially to be a vehicle of some sort, I believe it's
already closed to new project ideas ...

I'd ask also among the practitioners of ML, Lisp, J, Snobol,
and other high-level languages that I believe are likeliest
to host interest in graph theory.  It's certainly true, 
though, that Python boasts at least a couple.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using a browser as a GUI: which Python package

2006-05-01 Thread Jacob Rael
I am looking for the samething. I was thinking of Karrigell.

http://karrigell.sourceforge.net/

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


Multi-Monitor Support

2006-05-01 Thread Mark rainess
Hello,


Does Python or wxPython include any support for multiple monitors. In my 
application multiple monitors are located at a distance. It is not 
convenient to move a window from the main monitor to one of the others. 
I want to have the option to open an an application on any connected 
monitor. I am using wxPython. Does anyone know how to do it?

Thanks

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


Numeric, vectorization

2006-05-01 Thread RonnyM
Hello.

I want to vectorize this operation, which below is implemented as a
for-loop:

def smoothing_loop( y ): #y is an array with noisy values
ybar = []
ybar.append( y[0] )
#Smoothing with a loop
length = size( y )
for i in range( 1, length -1 ):
ybar.append( .5 * ( y[ i-1 ] + y[ i + 1 ] ) )

e.g. y = [ 1, 2, 3, 4, 5, 6 ,7 ,8, 9 ]

ybar = [ 1, (1 + 3)*.5,(2 + 4)*.5,(3 + 5)*.5,..., (n-1 + n+1)*.5 ], n =
1,...len(y) -1

How do I make a vectorized version of this, I will prefer not to
utilize Map or similar functions, but numeric instead.


Regards,

Ronny Mandal

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


how to do with the multi-frame GIF

2006-05-01 Thread liupei
how to do with the multi-frame GIF, I used the PIL ,but it seems not
support?

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


how to do with the multi-frame GIF

2006-05-01 Thread liupei
how to do with the multi-frame GIF, I used the PIL ,but it seems not
support?

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


how to do with the multi-frame GIF

2006-05-01 Thread liupei
how to do with the multi-frame GIF, I used the PIL ,but it seems not
support?

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


Re: Numeric, vectorization

2006-05-01 Thread David Isaac
"RonnyM" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> e.g. y = [ 1, 2, 3, 4, 5, 6 ,7 ,8, 9 ]

> ybar = [ 1, (1 + 3)*.5,(2 + 4)*.5,(3 + 5)*.5,..., (n-1 + n+1)*.5 ], n =
> 1,...len(y) -1
> How do I make a vectorized version of this, I will prefer not to
> utilize Map or similar functions, but numeric instead.


You treat the first element asymmetrically, so that does not vectorize.
The rest does:
>>> import numpy as N
>>> y=N.arange(1,10)
>>> slice1 = slice(0,-2,1)
>>> slice2 = slice(2,None,1)
>>> ybar = 0.5*(y[slice1]+y[slice2])
>>> ybar
array([ 2.,  3.,  4.,  5.,  6.,  7.,  8.])

hth,
Alan Isaac


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


Re: self modifying code

2006-05-01 Thread [EMAIL PROTECTED]
Personally, I would almost always pay the x2 efficiency price in order
to use a class. But then I don't know what you're writing.

Of course, if you really need it to be efficient, you can write it as a
C extension, or use Pyrex, etc. and get -much- better results.

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


Re: How to efficiently read binary files?

2006-05-01 Thread Robert Kern
Grant Edwards wrote:
> On 2006-04-30, Robert Kern <[EMAIL PROTECTED]> wrote:
> 
>>Grant Edwards wrote:

>>>Perhaps the numarray module?
>>
>>numpy for new code, please.
> 
> So numarray and numpy were both written to replace numeric?

numpy was written to replace both Numeric and numarray. There is a good
explanation in Chapter 1 of _The Guide to NumPy_ included in the sample 
chapters:

  http://numeric.scipy.org/scipybooksample.pdf

There were a number of features in Numeric that could not be replicated in
numarray, among them the ufunc C API and the speed for small arrays. numpy's
code base is closer to Numeric's but it incorporates nearly all (if not all) of
the features of numarray. The numarray developers fully support the migration to
numpy as the one array package.

-- 
Robert Kern

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

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


Re: list*list

2006-05-01 Thread David Isaac
"Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> it's considered bad style to use range if all you want is a
> enumeration of indices, as it will actually create a list of the size you
> specified. Use xrange in such cases.

I'm pretty sure this distinction goes away in 2.5.
Cheers,
Alan Isaac


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


Re: How to efficiently read binary files?

2006-05-01 Thread Grant Edwards
On 2006-05-01, Robert Kern <[EMAIL PROTECTED]> wrote:

Perhaps the numarray module?
>>>
>>>numpy for new code, please.
>> 
>> So numarray and numpy were both written to replace numeric?
>
> numpy was written to replace both Numeric and numarray.

too many batteries...

;)

-- 
Grant Edwards   grante Yow!  ... bleakness...
  at   desolation... plastic
   visi.comforks...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list*list

2006-05-01 Thread Fredrik Lundh
David Isaac wrote:

> > it's considered bad style to use range if all you want is a
> > enumeration of indices, as it will actually create a list of the size you
> > specified. Use xrange in such cases.
>
> I'm pretty sure this distinction goes away in 2.5.

3.0.

and for short sequences, it doesn't really matter much in the 2.X series.
it's definitely not "bad style" to use range instead of xrange for a ten to,
say, 1000 item loop.

(it's not always the most efficient thing to do, though, but that's
another story).





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


Re: list*list

2006-05-01 Thread Diez B. Roggisch
> and for short sequences, it doesn't really matter much in the 2.X series.
> it's definitely not "bad style" to use range instead of xrange for a ten
> to, say, 1000 item loop.

You and me are aware of that - but I figured the OP wasn't. And just the
other day somebody on de.c.l.py wondered about the memory consumption of
his little script that contained a range(900) which he used for a loop.

And as in the case of a "for in" one can't even access the list produced by
range() and thus there is virtually no difference to xrange, I'd consider
it a code smell if it's used there  - why creating an object you don't use
when you can't even get a reference to it?

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


Re: Add file to zip, or replace file in zip

2006-05-01 Thread Scott David Daniels
Edward Elliott wrote:
> Scott David Daniels wrote:
>>...
 > ... You windows kids and your crazy data formats.
There were a few oth OS's than Linux and Windows.  Maybe you
should call me "you crazy Tenex kid."  Knuth says, "the fastest
way to search is to know where to go." -- Zips have locations of
files, and you needn't read in a lot of a huge zip to find and
extract a couple of files.

>> Any error 
>> (or raised exception like Control-C) during this process is likely
>> to leave you with an inconsistent and therefore unreadable zip file.
> 
> Isn't that true of any modifications to the zip archive, e.g. appending a
> new file rather than replacing an existing one?

Nope.  There is enough info in the zip to rebuild the directory
with a forward scan of the zip.  (Each entry has a file descr).
"appending" is really replacing backing up before the zip archive
directory and writing another entry, followed by a new directory.

>> in one pass, copy all non-deleted files to a new zip (which you can then
>> swap for the original zip).  Shortcutting this process puts all data in
>> your zip file at risk.
> 
> Again, isn't this true of any substantive change to any file whatsoever?  
> Errors during write can always leave your data in an inconsistent state,
> unless your data uses a structured append format like journaled
> filesystems.  That seems like an orthogonal issue to replacing a file in
> the archive.

--Scott David Daniels
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to do with the multi-frame GIF

2006-05-01 Thread Daniel Nogradi
> how to do with the multi-frame GIF, I used the PIL ,but it seems not
> support?

The source distribution of the 1.1.4 version comes with a Scripts
directory where you can find player.py, gifmaker.py and explode.py
which all deal with animated gif. I don't know if they work with
version 1.1.5 though.

The 1.1.4 source is here: http://effbot.org/downloads/Imaging-1.1.4.tar.gz
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to do with the multi-frame GIF

2006-05-01 Thread Fredrik Lundh
Daniel Nogradi wrote:

> The source distribution of the 1.1.4 version comes with a Scripts
> directory where you can find player.py, gifmaker.py and explode.py
> which all deal with animated gif. I don't know if they work with
> version 1.1.5 though.

they're still shipped with 1.1.5 (and 1.1.6), and they should work.

if all you're missing is a few files from the script directory, you can get
them here:

http://effbot.python-hosting.com/browser/pil/Scripts/





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


Re: How to efficiently read binary files?

2006-05-01 Thread Robert Kern
Grant Edwards wrote:
> On 2006-05-01, Robert Kern <[EMAIL PROTECTED]> wrote:
> 
>Perhaps the numarray module?

numpy for new code, please.
>>>
>>>So numarray and numpy were both written to replace numeric?
>>
>>numpy was written to replace both Numeric and numarray.
> 
> too many batteries...

Too many cryptic complaints...

-- 
Robert Kern

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

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


Re: How to efficiently read binary files?

2006-05-01 Thread Grant Edwards
On 2006-05-01, Robert Kern <[EMAIL PROTECTED]> wrote:
> Grant Edwards wrote:
>> On 2006-05-01, Robert Kern <[EMAIL PROTECTED]> wrote:
>> 
>>Perhaps the numarray module?
>
>numpy for new code, please.

So numarray and numpy were both written to replace numeric?
>>>
>>>numpy was written to replace both Numeric and numarray.
>> 
>> too many batteries...
>
> Too many cryptic complaints...

Too easy to miss the intended humor on Usenet...

-- 
Grant Edwards   grante Yow!  Where's my SOCIAL
  at   WORKER?
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to efficiently read binary files?

2006-05-01 Thread Robert Kern
Grant Edwards wrote:
> On 2006-05-01, Robert Kern <[EMAIL PROTECTED]> wrote:
> 
>>Grant Edwards wrote:
>>
>>>On 2006-05-01, Robert Kern <[EMAIL PROTECTED]> wrote:
>>>
>>>Perhaps the numarray module?
>>
>>numpy for new code, please.
>
>So numarray and numpy were both written to replace numeric?

numpy was written to replace both Numeric and numarray.
>>>
>>>too many batteries...
>>
>>Too many cryptic complaints...
> 
> Too easy to miss the intended humor on Usenet...

Oh, I saw the smiley. I knew it was meant to be humorous. I just didn't
understand it. The only "batteries" reference I know of in this context is the
"batteries included" philosophy of the stdlib. Of course, none of Numeric,
numarray, or numpy have anything to do with the stdlib, so again I am confused.

-- 
Robert Kern

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

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


Re: list*list

2006-05-01 Thread Klaas
Diez wrote:
> First of all: it's considered bad style to use range if all you want is a
> enumeration of indices, as it will actually create a list of the size you
> specified. Use xrange in such cases.

> But maybe nicer is zip:
> c = [av * bv for av, bv in zip(a, b)]

By your logic, shouldn't it also be "bad style" to create an
unnecessary list with zip instead of using izip?

-Mike

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


Re: returning none when it should be returning a list?

2006-05-01 Thread Dave Hughes
Edward Elliott wrote:

> [EMAIL PROTECTED] wrote:
> > The function basically takes in a list of all the prime number
> > found, it takes the next number to be tested for (next) and the
> > limit it will go up to. It divide next by the list of previous
> > prime numbers if next is not bigger than lim, count up all the
> > times where it's not evenly divdided, if the result is equal the
> > length of prime number, then we have another prime number, lather
> > rinse and repeat :)
> 
> Your basic algorithm as described above sounds workable, but can be
> made more efficient (I didn't look closely at your code for bugs).  I
> won't even go near the complicated number theory algorithms.
> 
> 1. integer mod will be faster than floating point mod (unless you're
> getting into numbers too large to store efficiently as ints, but
> you'll probably run into other limitations before you get near that
> point in this code).
> 
> 2. you can stop as soon as you find a zero remainder, no need to keep
> testing the rest of the primes in the list.
> 
> 3. you can stop testing numbers at the halfway point.  there are no
> primes smaller than 2, so no number x can have a factor larger than
> x/2.
> 
> 4. in fact you can do even better.  a simple proof by contradiction
> shows that if primes 1..y don't divide x and y^2 > x then x must be
> prime.  put another way, once you test up to the square root of x,
> there's no point continuing.  if one factor were bigger than sqrt(x),
> the other would have to be smaller than sqrt(x).  but you've already
> tested the factors smaller than sqrt(x), so there can't be any
> factors.

The Prime Number article[1] on Wikipedia has a nice little Python
snippet implementing this algorithm (more or less). See the Finding
Prime Numbers section.

> 5. you can do better than checking every odd number (next+2) to find
> the next prime, but I'm too tired to look into it right now.  it may
> require more complex machinery.
> 
> Locating primes is an interesting challenge because of the seemingly
> endless grades of refinements over simple brute-force search.  Like I
> said though, if speed and size aren't concerns, your algorithm is
> fine.

Further reading: the Sieve of Eratosthenes[2] is a relatively simple
and fun little algorithm to implement (also has a size issue in that it
requires a list of numbers from 2 up to the largest number you wish to
test for primality, and I don't think it's any faster than the
algorithm above). A modern refinement called the Sieve of Atkin[3] is
also interesting, a bit faster, although rather more complicated.

If you want to look further into the subject, see the Primality Test
article[4] on Wikipedia (mentions things like probabilistic testing,
the Miller-Rabin primality test, and such like).

[1] http://en.wikipedia.org/wiki/Prime_number
[2] http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
[3] http://en.wikipedia.org/wiki/Sieve_of_Atkin
[4] http://en.wikipedia.org/wiki/Primality_test


Dave.
-- 

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


How do I take a directory name from a given dir?

2006-05-01 Thread Merrigan
Hi,

I am trying to write a script to backup all my company's server configs
weekly. The script will run in a cronjob, on whatever I set it.

The issue I am currently having isto "extract" the directory name from
a given directory string. For example: from the string
"/home/testuser/projects/" I need to extract the "projects" part. The
problem is that the directory names that needs to be used will never be
the same lenght, so as far as my (very limited) knowledge stretches,
slicing and indicing is out of the question.

Can anybody help me, or give me an idea of what I should look at,
seeing as I'm seriously stuck. I only started coding at the beginnig of
the year, and was never interested before that, so my knowlege is
basically non-existent.

Thanks in advance for the help :)

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


Re: How do I take a directory name from a given dir?

2006-05-01 Thread Daniel Nogradi
> Hi,
>
> I am trying to write a script to backup all my company's server configs
> weekly. The script will run in a cronjob, on whatever I set it.
>
> The issue I am currently having isto "extract" the directory name from
> a given directory string. For example: from the string
> "/home/testuser/projects/" I need to extract the "projects" part. The
> problem is that the directory names that needs to be used will never be
> the same lenght, so as far as my (very limited) knowledge stretches,
> slicing and indicing is out of the question.
>
> Can anybody help me, or give me an idea of what I should look at,
> seeing as I'm seriously stuck. I only started coding at the beginnig of
> the year, and was never interested before that, so my knowlege is
> basically non-existent.

You can try this:

>>> '/home/testuser/projects/'.strip( '/' ).split( '/' )
['home', 'testuser', 'projects']

strip gets rid of the first and last / and split splits the remaining
part and puts the results into a list.

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


Passing events

2006-05-01 Thread Wesley Brooks
Dear Users,Is it possible to pass events such as keyboard, mouse, and joystick events to any software application and take screen shots? In this example I would like to be able to control a car racing game using python script.
Is it difficult to recieve and create events from devices like the steering wheels and pedals?Thanks again in advance of any help.Yours Sincerely,Wesley Brooks
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How do I take a directory name from a given dir?

2006-05-01 Thread Merrigan Took
Daniel Nogradi wrote:
>> Hi,
>>
>> I am trying to write a script to backup all my company's server configs
>> weekly. The script will run in a cronjob, on whatever I set it.
>>
>> The issue I am currently having isto "extract" the directory name from
>> a given directory string. For example: from the string
>> "/home/testuser/projects/" I need to extract the "projects" part. The
>> problem is that the directory names that needs to be used will never be
>> the same lenght, so as far as my (very limited) knowledge stretches,
>> slicing and indicing is out of the question.
>>
>> Can anybody help me, or give me an idea of what I should look at,
>> seeing as I'm seriously stuck. I only started coding at the beginnig of
>> the year, and was never interested before that, so my knowlege is
>> basically non-existent.
> 
> You can try this:
> 
 '/home/testuser/projects/'.strip( '/' ).split( '/' )
> ['home', 'testuser', 'projects']
> 
> strip gets rid of the first and last / and split splits the remaining
> part and puts the results into a list.
> 
> HTH :)
h, ok, Thank you, I will definitely look into this and play with it!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I take a directory name from a given dir?

2006-05-01 Thread Mike Kent
Python 2.4.2 (#1, Nov 29 2005, 14:04:55)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> d = "/home/testuser/projects"
>>> os.path.basename(d)
'projects'
>>> os.path.dirname(d)
'/home/testuser'
>>>

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


Re: How do I take a directory name from a given dir?

2006-05-01 Thread Fredrik Lundh
"Merrigan" wrote:

> I am trying to write a script to backup all my company's server configs
> weekly. The script will run in a cronjob, on whatever I set it.
>
> The issue I am currently having isto "extract" the directory name from
> a given directory string. For example: from the string
> "/home/testuser/projects/" I need to extract the "projects" part. The
> problem is that the directory names that needs to be used will never be
> the same lenght, so as far as my (very limited) knowledge stretches,
> slicing and indicing is out of the question.

os.path.split(path) splits a path into directory name and "base name"
parts.

>>> import os
>>> os.path.split("/usr/bin/python")
('/usr/bin', 'python')

if you have a trailing slash, split will return an empty string for the base-
name part

>>> os.path.split("/home/testuser/projects/")
('/home/testuser/projects', '')

but you can work around that by doing an extra split:

>>> path = "/home/testuser/projects/"
>>> path, name = os.path.split(path)
>>> if not name:
... path, name = os.path.split(path)
...
>>> path
'/home/testuser'
>>> name
'projects'

hope this helps!





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


Re: How do I take a directory name from a given dir?

2006-05-01 Thread Michael Ekstrand
On Mon, May 01, 2006 at 12:42:58PM -0700, Merrigan wrote:
> The issue I am currently having isto "extract" the directory name from
> a given directory string. For example: from the string
> "/home/testuser/projects/" I need to extract the "projects" part. The
> problem is that the directory names that needs to be used will never be
> the same lenght, so as far as my (very limited) knowledge stretches,
> slicing and indicing is out of the question.

Look at the os.path module, especially the basename and dirname
functions.  basename will extract the "base name", that is, the last
component of a path.

- Michael

-- 
mouse, n: a device for pointing at the xterm in which you want to type.
-- Fortune
Visit me on the Web: http://www.elehack.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to efficiently read binary files?

2006-05-01 Thread Grant Edwards
On 2006-05-01, Robert Kern <[EMAIL PROTECTED]> wrote:

> Oh, I saw the smiley. I knew it was meant to be humorous. I
> just didn't understand it. The only "batteries" reference I
> know of in this context is the "batteries included" philosophy
> of the stdlib. Of course, none of Numeric, numarray, or numpy
> have anything to do with the stdlib, so again I am confused.

Sorry. I forgot about the distinction between the standard
library and the "third-party" packages like scientific
packages.

-- 
Grant Edwards   grante Yow!  I think I'll make
  at   SCRAMBLED EGGS!! They're
   visi.comeach in LITTLE SHELLS...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I take a directory name from a given dir?

2006-05-01 Thread Steve Poe
Daniel,Before writing your script, what is your general approach to backup configuration files? Do you tar/zip them into one file? Do you ftp/ssh each file to another server? One approach I've used in backing up files from another server is:
from ftplib import FTPdef handleDownload(block):    file.write(block)#    print "."os.chdir('/home/backup')ftp = FTP('10.0.0.2')ftp.login('backuserid','backuppasswd')
ftp.cwd('/home/backup')# Start downloadingfilename = 'main_config.tar.bz2'file = open(filename,'wb')print 'Getting ' + filenameftp.retrbinary('RETR ' + filename, handleDownload, 1024)print 'Closing file ' + filename
file.close()#Finish downloadingThe 'Start downloading' and 'Stop downloading' are what I repeat for each file I need. I am sure there are better ways, but this works. I imagine you could do this with wildcards as well.
Good luck.Steve PoeOn 5/1/06, Daniel Nogradi <[EMAIL PROTECTED]> wrote:
> Hi,>> I am trying to write a script to backup all my company's server configs> weekly. The script will run in a cronjob, on whatever I set it.>> The issue I am currently having isto "extract" the directory name from
> a given directory string. For example: from the string> "/home/testuser/projects/" I need to extract the "projects" part. The> problem is that the directory names that needs to be used will never be
> the same lenght, so as far as my (very limited) knowledge stretches,> slicing and indicing is out of the question.>> Can anybody help me, or give me an idea of what I should look at,> seeing as I'm seriously stuck. I only started coding at the beginnig of
> the year, and was never interested before that, so my knowlege is> basically non-existent.You can try this:>>> '/home/testuser/projects/'.strip( '/' ).split( '/' )['home', 'testuser', 'projects']
strip gets rid of the first and last / and split splits the remainingpart and puts the results into a list.HTH :)--http://mail.python.org/mailman/listinfo/python-list

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

using ftplib

2006-05-01 Thread John Salerno
I'm experimenting with this now and I'm a little confused about 
transferring commands. This might be more of an FTP question than 
strictly Python, but it's still related to how to use the ftplib methods.

Anyway, if what I want to do is send a command to change the file 
permission settings of a file (or files), I assume I would use 
transfercmd() and the command would be something like SITE CHMOD 755 
name.py, for example.

What I'm confused about is why the documentation says to send a PORT or 
PASV command as well. Is this just something that must be done before 
each command?

Also, in order to figure out what the FTP commands are to begin with, I 
did a little testing in FileZilla and I noted what was being displayed 
at the top (things like PORT xxx, STOR xxx, SITE CHMOD, etc. depending 
on what I was doing). So I assume these are the 'cmd' parameter. So here 
are a couple of questions:

1. Are PORT/PASV necessary to start a transfer?

2. Is the cmd parameter some kind of list, or is it just a string and 
you have to call a separate transfercmd() for each command?

3. My burning question: also during every transfer I made in FileZilla, 
I see that it sometimes sends a TYPE A or TYPE I command as well. What 
are these, and are they also necessary when I'm using transfercmd()?

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


Re: regd efficient methods to manipulate *large* files

2006-05-01 Thread Dave Hughes
Madhusudhanan Chandrasekaran wrote:

> Hi:
> 
>   This question is not directed "entirely" at python only. But since
> I want to know how to do it in python, I am posting here.
> 
> 
> I am constructing a huge matrix (m x n), whose columns n are stored in
> smaller files. Once I read m such files, my matrix is complete. I
> want to pass this matrix as an input to another script of mine (I
> just have the binary.) Currently, the script reads a file (which is
> nothing but the matrix) and processes it. Is there any way of doing
> this in memory, without writing the matrix onto the disk?
> 
> Since I have to repeat my experimentation for multiple iterations, it
> becomes expensive to write the matrix onto the disk.
> 
> Thanks in advance. Help appreciated.
> 
> -Madhu

Basically, you're asking about Inter Process Communication (IPC), for
which Python provides several interfaces to mechanisms provided by the
operating system (whatever that may be). Here's a couple of commonly
used methods:

Redirected I/O

Have a look at the popen functions in the os module, or better still
the subprocess module (which is a higher level interface to the same
functionality). Specifically, the "Replacing the shell pipe line"
example in the subprocess module's documentation should be interesting:

  output=`dmesg | grep hda`
  ==>
  p1 = Popen(["dmesg"], stdout=PIPE)
  p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
  output = p2.communicate()[0]

Here, the stdout of the "dmesg" process has been redirected to the
stdin of the "grep" process. You could do something similar with your
two scripts: e.g., the first script simply writes the content of the
matrix in some format to stdout (e.g. print, sys.stdout.write), while
the second script reads the content of the matrix from stdin (e.g.
raw_input, sys.stdin.read). Here's some brutally simplistic scripts
that demonstrate the method:

in.py
=
#!/bin/env python
#
# I read integers from stdin until I encounter 0

import sys

while True:
i = int(sys.stdin.readline())
print "Read %d from stdin" % i
if i == 0:
break


out.py
==
#!/bin/env python
#
# I write some numbers to stdout

for i in [1, 2, 3, 4, 5, 0]:
print i


run.py
==
#!/bin/env python
#
# I run out.py and in.py with a pipe between them, capture the
# output of in.py and print it

from subprocess import Popen, PIPE

process1 = Popen(["./out.py"], stdout=PIPE)
process2 = Popen(["./in.py"], stdin=process1.stdout, stdout=PIPE)
output = process2.communicate()[0]

print output


Sockets

Another form of IPC uses sockets to communicate between two processes
(see the socket module or one of the higher level modules like
SocketServer). Hence, the second process would listen on a port
(presumably on the localhost interface, although there's no reason it
couldn't listen on a LAN interface for example), and the first process
connects to that port and sends the matrix data across it to the second
process.


Summary

Given that your second script currently reads a file containing the
complete matrix (if I understand your post correctly), it's probably
easiest for you to use the Redirected I/O method (as it's very similar
to reading a file, although there are some differences, and sometimes
one must be careful about closing pipe ends to avoid deadlocks).
However, the sockets method has the advantage that you can easily move
one of the processes onto a different machine.

There are other methods of IPC (for example, shared memory: see the
mmap module) however the two mentioned above are available on most
platforms whereas others may be specific to a given platform, or have
platform specific subtleties (for example, mmap is only available on
Windows and UNIX, and has a slightly different constructor on each).


HTH,

Dave.

-- 

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


Re: stdin: processing characters

2006-05-01 Thread Russ Salsbury
Serge Orlov wrote:
> Kevin Simmons wrote:


>
> Looks reasonable if you don't need portability. But you may want to
> refactor it a little bit to make sure terminal setting are always
> restored:
>
> try:
> do_all_the_work()
> finally:
> os.system("stty cooked")
>

IIRC You need to do "stty raw" first.  The last time I had to do this
was in the '80's, so take for what it's worth.

Russ

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


Re: How do I take a directory name from a given dir?

2006-05-01 Thread Merrigan
WOW, Thanks guys. I am really awestruck by the speed you guys relied
with. Thanks a lot for the help. I eventually found a way that did the
job for me.

I will definitely visit this Group more often.

Thanks for all the help! :D

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


[ANN] markup.py 1.4 - bugfix

2006-05-01 Thread Daniel Nogradi
I'm terribly sorry but there was a crucial bug left in the 1.3 release
announced today. Thanks to a user now it's fixed, and the new 1.4
release is available on http://markup.sourceforge.net/ which should be
okay.

Sorry for the inconvenience :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Could not find platform independent libraries

2006-05-01 Thread pythonhelpneeded
While building Python 2.4.3 on SCO OpenServer 5.0.5, I'm getting the
error below.  The modules mentioned seem to be available in the Lib
subdirectory.  I've tried setting PYTHONHOME to the build directory
(/tmp/Python-2.4.3), it didn't help.

Any help would be appreciated.

# make
case $MAKEFLAGS in \
*-s*)  CC='cc' LDSHARED='cc -Wl,-G,-Bexport' OPT='-DNDEBUG -O'
./python -E ./setup.py -q build;; \
*)  CC='cc' LDSHARED='cc -Wl,-G,-Bexport' OPT='-DNDEBUG -O'
./python -E ./setup.py build;; \
esac
Could not find platform independent libraries 
Could not find platform dependent libraries 
Consider setting $PYTHONHOME to [:]
'import site' failed; use -v for traceback
Traceback (most recent call last):
  File "./setup.py", line 6, in ?
import sys, os, getopt, imp, re
ImportError: No module named os
*** Error code 1 (bu21)
UX:make: ERROR: fatal error.

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


Re: Add file to zip, or replace file in zip

2006-05-01 Thread Edward Elliott
Scott David Daniels wrote:

> Edward Elliott wrote:
>> Scott David Daniels wrote:
>>>...
>  > ... You windows kids and your crazy data formats.
> There were a few oth OS's than Linux and Windows.  Maybe you
> should call me "you crazy Tenex kid."  

Windows popularized the zip format, but if you insist:
You crazy Tenex kids, with your char-at-a-time raw password checking and
new-fangled virtual memory.  Back in my day, all we had was Multics and we
liked it that way.

Despite having no memory prior to the Apple II, I do know a bit of history.


> Knuth says, "the fastest 
> way to search is to know where to go." -- Zips have locations of
> files, and you needn't read in a lot of a huge zip to find and
> extract a couple of files.

You whippersnappers and your random access, always in such a hurry to get
your data.  You want faster search, just wait a few machine generations.

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


Re: using ftplib

2006-05-01 Thread Dave Hughes
John Salerno wrote:

> I'm experimenting with this now and I'm a little confused about
> transferring commands. This might be more of an FTP question than
> strictly Python, but it's still related to how to use the ftplib
> methods.
> 
> Anyway, if what I want to do is send a command to change the file
> permission settings of a file (or files), I assume I would use
> transfercmd() and the command would be something like SITE CHMOD 755
> name.py, for example.
> 
> What I'm confused about is why the documentation says to send a PORT
> or PASV command as well. Is this just something that must be done
> before each command?
> 
> Also, in order to figure out what the FTP commands are to begin with,
> I did a little testing in FileZilla and I noted what was being
> displayed at the top (things like PORT xxx, STOR xxx, SITE CHMOD,
> etc. depending on what I was doing). So I assume these are the 'cmd'
> parameter. So here are a couple of questions:
> 
> 1. Are PORT/PASV necessary to start a transfer?

Indeed. FTP is a tricky (and _very_ old) protocol that does things in a
very different manner to the later (simpler) protocols like HTTP. Not
sure how familiar you are with FTP, so my apologies if I wind up
repeating stuff you know already:

FTP sessions typically have *two* connections: the "control"
connection, and the "data" connection. The control connection is from
the client to the server (typically port 21 on the server side), and is
used to send commands to the server and obtain responses.

The PORT command tells the server to open a data connection to the
client (yes, that's not a typo: from the server to the client) and the
parameters tell the server the address and port to connect to. This is
the "active" (or default) mode for FTP (although due to the heavy use
of NAT these days, "passive" mode is probably as, or more common in
practice).

The PASV command is the reverse of the PORT command: it requests the
server listen on a new port for a data connection from the client to
the server.

Data is never transferred over the control connection, it always goes
over the separate data connection, hence why PORT or PASV are required
before initiating a transfer.

See RFC 959 for the full spec (gawd, it's been a long time since I read
that ... amazed I still remember the number):
http://www.faqs.org/rfcs/rfc959.html

> 
> 2. Is the cmd parameter some kind of list, or is it just a string and
> you have to call a separate transfercmd() for each command?

Sorry, I haven't used ftplib yet, but a brief glance at the code
suggests to me that it's just a string and you probably do need to call
a separate transfercmd for each command.

> 3. My burning question: also during every transfer I made in
> FileZilla, I see that it sometimes sends a TYPE A or TYPE I command
> as well. What are these, and are they also necessary when I'm using
> transfercmd()?

TYPE I indicates that an "Image" transfer is to take place (though this
is more commonly referred to nowadays as a "binary" transfer). In an
binary transfer the data is transferred verbatim with no
transformations.

TYPE A indicates that an ASCII transfer is to take place. An ASCII
transfer is intended for use with text files and indicates that the
data should be transformed from the native text format on the client
platform to the native text format on the server platform. For example,
transferring a file from a DOS/Windows client to a UNIX/Linux platform
in ASCII mode would convert CRLF line endings (ASCII char 13 + ASCII
char 10) to LF line endings (ASCII char 10).

There are other transfer modes as well, though I forget exactly what
they are (there's probably one for EBCDIC  :-).

Take a look at the storbinary, storlines, retrbinary and retrlines
methods of the FTP object: looks like they perform the appropriate TYPE
command for you, then pass the specified command to transfercmd.


HTH,

Dave.
-- 

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


Difference between threading.local and protecting data with locks

2006-05-01 Thread juxstapose
Hello,

I have been develop a blocking socket application with threading. The
main thread handles connections and inserts them into python's
protected queue as jobs for the thread pool to handle.

There is not much information on threading.local except that it states
that in maintains variable uniqueness among multiple instances of the
same thread.  I am assuming that it implements some sort of locking
functionality under the covers?  Is it possible to run into a race
condition using threading.local?

In a non-blocking threaded socket receive handler is it better to use a
threading.local variable for writes or protect the write with a
lock.acquire/lock.release ?

I know the size of the incoming request so the receive handler finishes
when the max size is reached.  However in case of network problems I
also have timeout functionality. Both of these behaviors require
writing to variables in a possible critical section.
What is the best way to handle writes in a multi-threaded non-blocking
receive handler?

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


Re: python about mobile game?

2006-05-01 Thread cyberco
http://sourceforge.net/projects/pys60

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


Re: list*list

2006-05-01 Thread Diez B. Roggisch
Klaas schrieb:
> Diez wrote:
>> First of all: it's considered bad style to use range if all you want is a
>> enumeration of indices, as it will actually create a list of the size you
>> specified. Use xrange in such cases.
> 
>> But maybe nicer is zip:
>> c = [av * bv for av, bv in zip(a, b)]
> 
> By your logic, shouldn't it also be "bad style" to create an
> unnecessary list with zip instead of using izip?

Yep. I always forget about the itertools. And then of course we'd go for 
an generator expression, won't we?


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


Re: regd efficient methods to manipulate *large* files

2006-05-01 Thread Paddy
I take it that you have a binary file that takes a file name and
proceses the file contents.
Sometimes Unix binaries are written so that a file name of '-', (just a
dash), causes it to take input from stdin so that the piping mentioned
in a previous reply could work.
On some of our unix systems /tmp is set up as a 'virtual disk' It
quacks like a normal disk filesystem but is actually implimented in
RAM/virtual memory, and is faster than normal disk access.
(Unfortunately we are not allowed to save multi-gigabyte files there as
it affects other aspects of the OS).

Maybe you can mount a similar filesystem if you have the RAM.

-- Pad.

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


Re: An Atlas of Graphs with Python

2006-05-01 Thread Paddy
A little off topic I'm afraid Giandomenico,
But I had to smile. Here is someone working in the field of
linguistics, who wants a programming solution, in the language Python.
(It's Larry Wall,  creator of Perl that cites his linguistic
foundations).

-- Pad.

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


Re: noob question: "TypeError" wrong number of args

2006-05-01 Thread Holger
I guess I deserved that. :-(
I *did* read the tutorial, but then I forgot and didn't notice...
My brain is getting is slow - so thx for the friendly slap in the face
;-)

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


Re: using ftplib

2006-05-01 Thread John Salerno
Dave Hughes wrote:

> Indeed. FTP is a tricky (and _very_ old) protocol that does things in a
> very different manner to the later (simpler) protocols like HTTP. Not
> sure how familiar you are with FTP, so my apologies if I wind up
> repeating stuff you know already:

Thanks very much, all that information was great to have.

> TYPE A indicates that an ASCII transfer is to take place. An ASCII
> transfer is intended for use with text files and indicates that the
> data should be transformed from the native text format on the client
> platform to the native text format on the server platform. For example,
> transferring a file from a DOS/Windows client to a UNIX/Linux platform
> in ASCII mode would convert CRLF line endings (ASCII char 13 + ASCII
> char 10) to LF line endings (ASCII char 10).
> 
> There are other transfer modes as well, though I forget exactly what
> they are (there's probably one for EBCDIC  :-).
> 
> Take a look at the storbinary, storlines, retrbinary and retrlines
> methods of the FTP object: looks like they perform the appropriate TYPE
> command for you, then pass the specified command to transfercmd.

So if I already have files on the server and I want to change file 
permissions, do I need to mess with TYPE A/TYPE I commands, or are those 
strictly for when you *transfer* files? Because I just did a quick test 
of changing file permissions through my FTP program, and it still sent a 
TYPE A command in the process. I know that the retr and stor methods 
might do this automatically, but if I don't need to use those, and just 
need to use transfercmd (assuming this *is* the right method to use when 
changing file permissions), do I need to manually send a TYPE A/I 
command as well?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Setting a module package to use new-style classes

2006-05-01 Thread Ben Finney
"Panos Laganakos" <[EMAIL PROTECTED]> writes:

> Is there a way to have a whole module package use the new-style
> classes, without having to specify it per module-file or even worse,
> per class definition?

TTBOMK, you do that with a single statement per module, before any
class definitions:

__metaclass__ = object

In general, code in one module doesn't magically affect other modules.

> Maybe by declaring the __metaclass__ in the module's __init__.py?

Presumably you mean "the package's __init__.py", which is itself a
separate module; so no.

-- 
 \"I hate it when my foot falls asleep during the day, because |
  `\ that means it's gonna be up all night."  -- Steven Wright |
_o__)  |
Ben Finney

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


Re: Add file to zip, or replace file in zip

2006-05-01 Thread Scott David Daniels
Edward Elliott wrote:
> Scott David Daniels wrote:
> 
>> Edward Elliott wrote:
>>> Scott David Daniels wrote:
 ...
>>  > ... You windows kids and your crazy data formats.
>> There were a few oth OS's than Linux and Windows.  Maybe you
>> should call me "you crazy Tenex kid."  
> 
> Windows popularized the zip format, but if you insist:
> You crazy Tenex kids, with your char-at-a-time raw password checking and
> new-fangled virtual memory.  Back in my day, all we had was Multics and we
> liked it that way.

Actually I think it was a combination of CP/M and DOS that popularized
the ZIP format; essentially the floppy-disk set, for whom the zip format
was a godsend.

-- 
-Scott David Daniels
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: noob question: "TypeError" wrong number of args

2006-05-01 Thread Edward Elliott
Holger wrote:
> oops, that was kinda embarrassing.

It's really not.  You got a completely unhelpful error message saying you
passed 2 args when you only passed one explicitly.  The fact the b is also
an argument to b.addfile(f) is totally nonobvious until you know that 1) b
is an object not a module*, and 2) objects pass references to themselves as
the first argument to their methods.  The syntax "b." is completely
different from the syntax of any other type of parameter.

The mismatch between the number of parameters declared in the method
signature and the number of arguments actually passed is nonobvious,
unintuitive, and would trip up anybody who didn't already know what was
going on.  It's ugly and confusing.  It's definitely a wart on the
langauge.

Making people pass 'self' explicitly is stupid because it always has to be
the first argument, leading to these kinds of mistakes.  The compiler
should handle it for you - and no, explicit is not *always* better than
implicit, just often and perhaps usually.  While it's easy to recognize
once you know what's going on, that doesn't make it any less of a wart.

* technically modules may be objects also, but in practice you don't declare
self as a parameter to module functions
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >