Re: testing if a list contains a sublist

2011-08-20 Thread Simon Forman
On Mon, Aug 15, 2011 at 4:26 PM, Johannes  wrote:
> hi list,
> what is the best way to check if a given list (lets call it l1) is
> totally contained in a second list (l2)?
>
> for example:
> l1 = [1,2], l2 = [1,2,3,4,5] -> l1 is contained in l2
> l1 = [1,2,2,], l2 = [1,2,3,4,5] -> l1 is not contained in l2
> l1 = [1,2,3], l2 = [1,3,5,7] -> l1 is not contained in l2
>
> my problem is the second example, which makes it impossible to work with
> sets insteads of lists. But something like set.issubset for lists would
> be nice.
>
> greatz Johannes
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Probably not the most efficient way, but I wanted to mention it:


from difflib import SequenceMatcher


def list_in(a, b):
'''Is a completely contained in b?'''
matcher = SequenceMatcher(a=a, b=b)
m = matcher.find_longest_match(0, len(a), 0, len(b))
return m.size == len(a)


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


Re: A curious bit of code...

2014-02-14 Thread Simon Forman
On Friday, February 14, 2014 1:01:48 PM UTC-8, Mark Lawrence wrote:
[snip]
> 
> Pleased to have you on board, as I'm know that Terry Reedy et al can do 
> with a helping hand.
> 
> But please note you appear to be using google groups, hence the double 
> line spacing above and trying to reply to paragraphs that run as a 
> single line across the screen.  Therefore would you please read and 
> action this https://wiki.python.org/moin/GoogleGroupsPython, thanks.


Ah!  Thanks for the tip. ;-)

(Just last night I was trying to explain to a friend about Usenet
and how it's not Google Groups.)


I really hope I can be of use with IDLE.  I've been using it for years now. :)


Warm regards,
~Simon



-- 
http://phoenixbureau.org/
http://phoenixbureau.org/blog.html
http://twitter.com/SimonForman



"The history of mankind for the last four centuries is rather like that of
an imprisoned sleeper, stirring clumsily and uneasily while the prison that
restrains and shelters him catches fire, not waking but incorporating the
crackling and warmth of the fire with ancient and incongruous dreams, than
like that of a man consciously awake to danger and opportunity."
--H. P. Wells, "A Short History of the World"

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


A curious bit of code...

2014-02-14 Thread Simon Forman

(Apologies if this results in a double-post.)

On Friday, February 14, 2014 1:01:48 PM UTC-8, Mark Lawrence wrote:
[snip]
> 
> Pleased to have you on board, as I'm know that Terry Reedy et al can do 
> with a helping hand.
> 
> But please note you appear to be using google groups, hence the double 
> line spacing above and trying to reply to paragraphs that run as a 
> single line across the screen.  Therefore would you please read and 
> action this https://wiki.python.org/moin/GoogleGroupsPython, thanks.



Ah!  Thanks for the tip. ;-)

(Just last night I was trying to explain to a friend about Usenet
and how it's not Google Groups.)


I really hope I can be of use with IDLE.  I've been using it for years now. :)


Warm regards,
~Simon

-- 
http://phoenixbureau.org/
http://phoenixbureau.org/blog.html
http://twitter.com/SimonForman



"The history of mankind for the last four centuries is rather like that of
an imprisoned sleeper, stirring clumsily and uneasily while the prison that
restrains and shelters him catches fire, not waking but incorporating the
crackling and warmth of the fire with ancient and incongruous dreams, than
like that of a man consciously awake to danger and opportunity."
--H. P. Wells, "A Short History of the World"

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


Re: Relying on the behaviour of empty container in conditional statements

2006-07-11 Thread Simon Forman
horizon5 wrote:
> Hi,
>
> my collegues and I recently held a coding style review.
> All of the code we produced is used in house on a commerical project.
> One of the minor issues I raised was the common idiom of specifing:
>
> 
> if len(x) > 0:
> do_something()
> 
> Instead of using the language-defined bahviour, as stated by PEP8:
>
> 
> - For sequences, (strings, lists, tuples), use the fact that empty
>   sequences are false.
>
>   Yes: if not seq:
>if seq:
>
>   No: if len(seq)
>   if not len(seq)
> 
>
> Without wishing to start a flame war, what are other's opinions on
> this, is using "len" safer? If so why?
>
> Arguments that have been presented for using len(x) > 0 to
> test emptiness of a container include:
>   - It's safer
>   - Not relying on weird behaviour of the language
>   - Explicit is better than implicit (as stated by 'this' module, Zen
> of Python)
>
> My own feeling is that I am willing to work with the behaviours defined
> by Python, and treat the use of len in these cases as excessive
> duplication (this is however, quite a minor point i agree).
>
> Note that I have much more experience with the language (6-7 years),
> whilst the majority of my collegues have about 1-2 years experience.

I've been programming in python for years and I've always used this
form

if not seq:
if seq:

rather than the other and never had any problems.

Anyone presenting arguments in favor of the len() form is IMHO, not
"getting it".  AFAIK, python is not "smart" enough to optimize away the
(totally unnecessary) call to len(), so the programmer should do it for
herself.

The "pythonic" form is safe, not weird, and just as explicit.

There's no more point to using the len() form than there is to saying
"seq[len(seq)-1]" rather than just "seq[-1]"  to get the last item of a
sequence.

My $0.02

Peace,
~Simon

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


Re: Abuse of the object-nature of functions?

2006-07-11 Thread Simon Forman
Carl J. Van Arsdall wrote:
> Hrmms, well, here's an interesting situation.  So say we wanna catch
> most exceptions but we don't necessarily know what they are going to
> be.  For example, I have a framework that executes modules (python
> functions), the framework wraps each function execution in a try/except
> block in order to compensate for what *might* happen.  Upon coding the
> framework I really have no idea what types of problems these modules
> might have but I want to catch these errors so that I can clean up and
> exit gracefully, not only that but I want to dump the exception to log
> files so that we can attempt to fix it.  So, I have the option of
> catching all standard exceptions and not list the ones I know I don't
> want to catch.  But what about user defined exceptions?  Do I then have
> to enforce policies on the system stating what types of exceptions can
> be raised?
>
> Is there a way in python to say, "hey, catch everything but these two"?
>
>
>
> --
>
> Carl J. Van Arsdall
> [EMAIL PROTECTED]
> Build and Release
> MontaVista Software


try:
# Do some stuff
except Exception, err:
if err not in (DontCatchMe1, DontCatchMe2):
# Handle err

HTH,
~Simon

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


Re: Abuse of the object-nature of functions?

2006-07-11 Thread Simon Forman

Simon Forman wrote:
> Carl J. Van Arsdall wrote:
> > Hrmms, well, here's an interesting situation.  So say we wanna catch
> > most exceptions but we don't necessarily know what they are going to
> > be.  For example, I have a framework that executes modules (python
> > functions), the framework wraps each function execution in a try/except
> > block in order to compensate for what *might* happen.  Upon coding the
> > framework I really have no idea what types of problems these modules
> > might have but I want to catch these errors so that I can clean up and
> > exit gracefully, not only that but I want to dump the exception to log
> > files so that we can attempt to fix it.  So, I have the option of
> > catching all standard exceptions and not list the ones I know I don't
> > want to catch.  But what about user defined exceptions?  Do I then have
> > to enforce policies on the system stating what types of exceptions can
> > be raised?
> >
> > Is there a way in python to say, "hey, catch everything but these two"?
> >
> >
> >
> > --
> >
> > Carl J. Van Arsdall
> > [EMAIL PROTECTED]
> > Build and Release
> > MontaVista Software
>
>
> try:
> # Do some stuff
> except Exception, err:
> if err not in (DontCatchMe1, DontCatchMe2):
> # Handle err
>
> HTH,
> ~Simon

Dang!  not only did somebody else beat me to it, but my code is wrong
and theirs correct.

Sorry,
~Simon

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


Re: String handling and the percent operator

2006-07-13 Thread Simon Forman
Tom Plunket wrote:
> I have some code to autogenerate some boilerplate code so that I don't
> need to do the tedious setup stuff when I want to create a new module.
>
> So, my script prompts the user for the module name, then opens two
> files and those files each get the contents of one of these functions:
>
> def GetPyContents(module):
>   boilerplate = \
> """
> class %s:
>   pass
>
> if __name__ == '__main__':
>   import unittest
>   unittest.main('%s_t')
> """
>
>   return boilerplate % ((module,) * 2)
>
> def GetTestContents(module):
>   boilerplate = \
> """from %s import *
> import unittest
>
> class Test%s(unittest.TestCase):
>   def testConstruction(self):
>   self.failUnless(%s())
>
>   def testWriteMoreTests(self):
>   self.fail('This test should fail.')
>
> if __name__ == '__main__':
>   unittest.main()
> """
>
>   return boilerplate % ((module,) * 3)
>
> My question is, I don't like hardcoding the number of times that the
> module name should be repeated in the two return functions.  Is there
> an straight forward (inline-appropriate) way to count the number of
> '%s'es in the 'boilerplate' strings?  ...or maybe a different and more
> Pythonic way to do this?  (Maybe I could somehow use generators?)
>
> thx.
> -tom!


strings have a count() method.

Since you know that you won't have things like '%%s' in your
boilerplate, it's perfectly reasonable to use:

return boilerplate % ((module,) * boilerplate.count('%s'))

in your code.

Peace,
~Simon

return boilerplate % ((module,) * 3)

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


Re: String handling and the percent operator

2006-07-13 Thread Simon Forman
Tom Plunket wrote:
> Simon Forman wrote:
>
> > strings have a count() method.
>
> thanks!
>
> For enrichment purposes, is there a way to do this sort of thing with
> a generator?  E.g. something like:
>
> def SentenceGenerator():
>words = ['I', 'have', 'been', 'to', 'the', 'fair']
>for w in words:
>   yield w
>
> message = "%s %s %s %s"
>
> print message % SentenceGenerator()
>
> (I ask because the above doesn't work)?
> -tom!

If you're asking if there's some way that the generator can know how
many formatting fields are in message then the straightforward answer
is no.   At least not without passing the message to the generator for
it to call count() itself, and then you're better off just doing it
without the generator.

(Actually, IIRC, this was discussed quite recently on this list, and
(again IIRC) I think there is some deep voodoo that can do this, but
you're much better off without it.)

FWIW, in your shoes I would use the trick Justin Azoff posted, i.e.:

boiler = 'foo %(modname)s bar %(modname)s baz'

message = boiler % {'modname': modname}


Peace,
~Simon

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


Re: testing array of logicals

2006-07-13 Thread Simon Forman

John Henry wrote:
> Hi list,
>
> Is there a more elagant way of doing this?
>
> # logflags is an array of logicals
> test=True
> for x in logflags:
>test = test and x
> print test
>
> --
> Thanks,

So many ways  *drool*
How about:

False not in logflags


(Anybody gonna run all these through timeit?  ;P )

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


Re: testing array of logicals

2006-07-13 Thread Simon Forman
>
> False not in logflags
>

Or, if your values aren't already bools

False not in (bool(n) for n in logflags)



Peace,
~Simon

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


Re: searching for strings (in a tuple) in a string

2006-07-14 Thread Simon Forman
Simon Forman wrote:
...
> I usually use this with assert statements when I need to check a
> sequence. Rather than:
>
> for something in something_else: assert expression
>
> I say
>
> assert False not in (expression for something in something_else)
>
> This way the whole assert statement will be removed if you use the '-O'
> switch to the python interpreter.  (It just occurred to me that that's
> just an assumption on my part.  I don't know for sure that the
> interpreter isn't smart enough to remove the first form as well.  I
> should check that. ;P )

FWIW I did just check that and it seems valid, the second form gets
"optimized" away.

File delme.py:
import dis

N = (True, True, False)

def a():
for n in N:
assert n

def b():
assert False not in (n for n in N)

dis.dis(a)
print '==='
dis.dis(b)


Results of running it without '-O':
$ python delme.py
  8   0 SETUP_LOOP  28 (to 31)
  3 LOAD_GLOBAL  0 (N)
  6 GET_ITER
>>7 FOR_ITER20 (to 30)
 10 STORE_FAST   0 (n)

  9  13 LOAD_FAST0 (n)
 16 JUMP_IF_TRUE 7 (to 26)
 19 POP_TOP
 20 LOAD_GLOBAL  2 (AssertionError)
 23 RAISE_VARARGS1
>>   26 POP_TOP
 27 JUMP_ABSOLUTE7
>>   30 POP_BLOCK
>>   31 LOAD_CONST   0 (None)
 34 RETURN_VALUE
===
 13   0 LOAD_GLOBAL  0 (False)
  3 LOAD_CONST   1 ( at 0xb7d89ca0, file "delme.py", line 13>)
  6 MAKE_FUNCTION0
  9 LOAD_GLOBAL  1 (N)
 12 GET_ITER
 13 CALL_FUNCTION1
 16 COMPARE_OP   7 (not in)
 19 JUMP_IF_TRUE 7 (to 29)
 22 POP_TOP
 23 LOAD_GLOBAL  2 (AssertionError)
 26 RAISE_VARARGS1
>>   29 POP_TOP
 30 LOAD_CONST   0 (None)
 33 RETURN_VALUE


Results of running it with '-O':
$ python -O delme.py
  8   0 SETUP_LOOP  14 (to 17)
  3 LOAD_GLOBAL  0 (N)
  6 GET_ITER
>>7 FOR_ITER 6 (to 16)
 10 STORE_FAST   0 (n)

  9  13 JUMP_ABSOLUTE7
>>   16 POP_BLOCK
>>   17 LOAD_CONST   0 (None)
 20 RETURN_VALUE
===
 13   0 LOAD_CONST   0 (None)
  3 RETURN_VALUE

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


Re: instances

2006-07-14 Thread Simon Forman
Quenton Bonds wrote:
> Hello
> I am trying to understand the abilities and limitation of creating an
> instance.  First I will give you my understanding then please steer me
> in the right direction.
>

Wow, you've got it nearly completely comprehensively backwards.

> Abiities
> 1.  The two ways to create an instance is def method(self) &
> __int__(self, other, instances,...)

There's really just basically one way to create an instance, and that's
by writing a class and then "calling" it.  (example below)

if you use the def statement by itself, then you are creating a
FUNCTION object, that you later call with arguments to do some work.

When you create a class, it looks like this:

class foo:
def __init__(self, arg1, arg2):
# do some work to set up the instance of the class.

and then you "call" the class like so:

bar = foo(arg1, arg2)

to create an INSTANCE of the CLASS.  the name 'bar' now references an
instance of class 'foo'.

(also note that when the def statement is used within a class, like the
__init__ above, then it creates a METHOD, which is almost the same
thing as a FUNCTION.)

> 2.  By creating an instance of a method; the functions of that method
> can be used through out the
>program in a fashion such as self.methodofprogram(parameters)

Ok, I think the easiest thing to do here would be to rewrite your
sentence using the proper terminology:

By creating an instance of a CLASS, the METHODS of that CLASS can be
used through out the program in a fashion such as
INSTANCE.methodofCLASS(parameters)

Example of creating a class with a method:

class foo:
def __init__(self, arg1):
# do some work to set up the instance of the class.
self.value = arg1
def printme(self):
print self.value

Example of creating an instance of that class:

bar = foo('Hi there!')


Example of using a method of an instance of that class:

bar.printme()

# Prints "Hi there!"



> Limitations
> 3.  One cannot create an instance of a class.

:)  One can ONLY create instances of classes.


> 4.  An instance can only perform functions that are provided from the
> method it was instanced from.

Yes, *IF* you replace "method" in that sentence with "class",  and
"functions" with "methods".


> 5.  Is there any other key information I am missing.

I hope this helps,
~Simon

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


Re: reading specific lines of a file

2006-07-15 Thread Simon Forman
Yi Xing wrote:
> Hi All,
>
> I want to read specific lines of a huge txt file (I know the line #).
> Each line might have different sizes. Is there a convenient and fast
> way of doing this in Python? Thanks.
>
> Yi Xing

I once had to do a lot of random access of lines in a multi gigabyte
log file.  I found that a very fast way to do this was to build an
index file containing the int offset in bytes of each line in the log
file.

I could post the code if you're interested.

Peace,
~Simon

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


Re: instantiate all subclasses of a class

2006-07-16 Thread Simon Forman
Daniel Nogradi wrote:
> What is the simplest way to instantiate all classes that are
> subclasses of a given class in a module?
>
> More precisely I have a module m with some content:
>
> # m.py
> class A:
> pass
> class x( A ):
> pass
> class y( A ):
> pass
> # all kinds of other objects follow
> # end of m.py
>
> and then in another module I have currently:
>
> # n.py
> import m
> x = m.x( )
> y = m.y( )
> # end of n.py
>
> and would like to automate this in a way that results in having
> instances of classes from m in n whose names are the same as the
> classes themselves. But I only would like to do this with classes that
> are subclasses of A.
>
> Any ideas?

It's pretty easy


import m
from inspect import getmembers, isclass, getmro

t = '%s = m.%s()'

for name, class_ in getmembers(m, isclass):
if class_ is m.A:
continue
if m.A in getmro(class_):
exec t % (name, name)


Peace,
~Simon

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


Re: instantiate all subclasses of a class

2006-07-16 Thread Simon Forman
Daniel Nogradi wrote:
> > > What is the simplest way to instantiate all classes that are
> > > subclasses of a given class in a module?
> > >
> > > More precisely I have a module m with some content:
> > >
> > > # m.py
> > > class A:
> > > pass
> > > class x( A ):
> > > pass
> > > class y( A ):
> > > pass
> > > # all kinds of other objects follow
> > > # end of m.py
> > >
> > > and then in another module I have currently:
> > >
> > > # n.py
> > > import m
> > > x = m.x( )
> > > y = m.y( )
> > > # end of n.py
> > >
> > > and would like to automate this in a way that results in having
> > > instances of classes from m in n whose names are the same as the
> > > classes themselves. But I only would like to do this with classes that
> > > are subclasses of A.
> > >
> > > Any ideas?
> >
> > It's pretty easy
> >
> >
> > import m
> > from inspect import getmembers, isclass, getmro
> >
> > t = '%s = m.%s()'
> >
> > for name, class_ in getmembers(m, isclass):
> > if class_ is m.A:
> > continue
> > if m.A in getmro(class_):
> > exec t % (name, name)
> >
>
> Actually, this variant also suffers from the broken isclass implementation.
>
> (Simon, sorry for the double post.)


Not a problem,  I haven't used inspect much so I've not been bitten by
this bug before.  It's good to know!

(I would have assumed that isclass() would have been implemented as
isinstance(obj, (types.ClassType, type))  anyway.   I'm surprised it's
not,  and that it's so broken..)

Thanks.

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


Re: execute a shell script from a python script

2006-07-17 Thread Simon Forman
spec wrote:
> Thanks, actually there are no args, is there something even simpler?
>
> Thanks
> Frank

you could try os.system()

>From the docs:

system(command)
Execute the command (a string) in a subshell. This is implemented
by calling the Standard C function system(), and has the same
limitations. Changes to posix.environ, sys.stdin, etc. are not
reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded
in the format specified for wait(). Note that POSIX does not specify
the meaning of the return value of the C system() function, so the
return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell
after running command, given by the Windows environment variable
COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always
0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status
of the command run; on systems using a non-native shell, consult your
shell documentation.

Availability: Macintosh, Unix, Windows.

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


Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman

Dan Bishop wrote:
> [EMAIL PROTECTED] wrote:
> > it seems that range() can be really slow:
> ...
> > if i in range (0, 1):
>
> This creates a 10,000-element list and sequentially searches it.  Of
> course that's gonna be slow.

And you're doing it 3 times.

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


Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
Nick Craig-Wood wrote:
>
> Sets are pretty fast too, and have the advantage of flexibility in
> that you can put any numbers in you like
>

I know this is self-evident to most of the people reading this, but I
thought it worth pointing out that this is a great way to test
membership in range(lo, hi, step) without doing "the necessary
algebra".

i.e.  n in set(xrange(0, 1, 23)) ...


Peace,
~Simon

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


Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
Diez B. Roggisch wrote:
> Simon Forman wrote:
>
> > Nick Craig-Wood wrote:
> >>
> >> Sets are pretty fast too, and have the advantage of flexibility in
> >> that you can put any numbers in you like
> >>
> >
> > I know this is self-evident to most of the people reading this, but I
> > thought it worth pointing out that this is a great way to test
> > membership in range(lo, hi, step) without doing "the necessary
> > algebra".
> >
> > i.e.  n in set(xrange(0, 1, 23)) ...
>
> No, its not. It works, but it works by no means faster than
>
> n in range(0, 1, 23)
>
> Both need O(n), which is a bit slow compared to
>
> (((n - 15) % 23) == 0 and n >= 15 and n < 1)
>
> that will run in O(1)
>
> Diez

You're right, of course.  I should have said that if you're testing
such a membership, say, 3 times AND you (like me) are slightly
rusty on the algebra and prefer to let the computer do it, then you
could use something like:

test_set = set(xrange(0, 1, 23))

if n in test_set:
...

;-)
~Simon

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


Re: Piping external commands

2006-07-18 Thread Simon Forman
[EMAIL PROTECTED] wrote:
> What is the Python translation for this Bash statement:
>
>   tar cf - "[EMAIL PROTECTED]" | bzip2 > "$file".tar.bz2
>
> (Ignoring the fact that "tar cjf" also exists...)
>
> In other words, how does one pipe together arbitrary commands?

For piping subcommands check out the subprocess module, especially
http://docs.python.org/lib/node242.html , for bzip2 check out the bz2
module http://docs.python.org/lib/module-bz2.html , but note, there's
also a tarfile module http://docs.python.org/lib/module-tarfile.html
which can handle bzip2 as well.

HTH,
~Simon

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


Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
K.S.Sreeram wrote:
> Simon Forman wrote:
> > Nick Craig-Wood wrote:
> >> Sets are pretty fast too, and have the advantage of flexibility in
> >> that you can put any numbers in you like
> >>
> >
> > I know this is self-evident to most of the people reading this, but I
> > thought it worth pointing out that this is a great way to test
> > membership in range(lo, hi, step) without doing "the necessary
> > algebra".
> >
> > i.e.  n in set(xrange(0, 1, 23)) ...
>
> This is very very misleading... here are some timings :

Yes it is. I'm sorry about that.

> python -mtimeit "n=5000" "n in set(xrange(0,1))"
> 1000 loops, best of 3: 1.32 msec per loop
>
> python -mtimeit "n=5000" "n in xrange(0,1)"
> 1000 loops, best of 3: 455 usec per loop
>
> python -mtimeit "n=5000" "0 <= n < 1"
> 100 loops, best of 3: 0.217 usec per loop
>
> sets are fast only if you create them *once* and use them again and
> again. even in that case, the sets use up O(n) memory.

That's what I meant.  But I didn't state it clearly.

One of the things I like most about python is that it allows you to
specify the problem that you want to solve without a great deal of
difficulty as to *how* to specify it.  To me, and perhaps others, "T =
set(xrange(0, 1, 23))" and "n in T"  are somewhat easier to read
and write than "not n % 23 and 0 <= n < 1", YMMV.

In the given case a set of ~(1 / 23) ints would not usually be too
burdensome on ram, and the code runs close to the same speed as
compared to the direct calculation:

from timeit import Timer

times = 10
Max = 1
n = 5000
T = set(xrange(0, Max, 23))

s1 = 'n in T'
s2 = 'not n %% 23 and 0 <= n < %s' % Max

setup = 'from __main__ import n, T'


S1 = Timer(s1, setup).repeat(number=times)
S2 = Timer(s2, setup).repeat(number=times)


print "%.3f usec/pass" % (100 * min(S1) / times)
print "%.3f usec/pass" % (100 * min(S2) / times)

On my machine this printed:
0.476 usec/pass
0.552 usec/pass


>
> with comparison operators, you don't need extra memory *and* there is no
> pre-computation required.

When I set Max = 1 in the above test code there was serious
disk thrashing...  ;-)

>
> [sreeram;]
>


FWIW, in production code I would certainly use the comparison
operators.  A kilobyte saved is a kilobyte earned.

Peace,
~Simon

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


Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
tac-tics wrote:
> Simon Forman wrote:
> > To me, and perhaps others, "T =
> > set(xrange(0, 1, 23))" and "n in T"  are somewhat easier to read
> > and write than "not n % 23 and 0 <= n < 1", YMMV.
>
> Eh? How is the first easier to read than the second?? You have a nested
> function call in the first!

I find the first form more immediately comprehensible than the latter.
I know what xrange() does, and I know what set() does, and "nested
function calls" give me no trouble,  whereas the latter form with a
modulus, negation, and comparisons would take me a bit longer both to
compose and/or understand.

If this is not the case for you then by all means please disregard my
posting.  YMMV.

>
> Regardless, testing if a member is part of a ranged set is always going
> to be slower.

Yes.  Roughly 0.001 seconds slower on my five year old computer.
I'm not worried.

> It's the nature of what you're doing. Building a set and
> then searching it takes much longer than a single modulus and
> subtraction (which is all an integer comparison is).

Building the set, yes, but searching the set is very close to the same
speed, even for rather large sets.  If you were performing the search
3 times (like in the OP) it would only take about three thousandths
of a second longer, and that's on my old slow computer.

If I were doing this a thousand times more often, or on a range of a
million or more, or in production code, or with ranges that changed
often, then I would certainly take the time to write out the latter
form.


Peace,
~Simon

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


Re: Text Summarization

2006-07-19 Thread Simon Forman
[EMAIL PROTECTED] wrote:
> Jim Jones wrote:
> > Is there  a Python library that would allow me to take a paragraph of text,
> > and generate a one or two sentence summary of that paragraph?
>
> There is a OTS wrapper.

http://libots.sourceforge.net/

as for the wrapper, this was all I could find (in about 15 minutes on
google):
http://cheeseshop.python.org/pypi?name=ots&version=0.4.2.1&:action=display

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


Re: Authentication

2006-07-19 Thread Simon Forman
bigodines wrote:
> Hello guys,
>
> I'm trying to learn python by making some small programs that could be
> useful for some bigger propouses. In fact, i've made a small "check
> latest-modified" for webpages and it's working great.
>
> The next step I would like to do is to check if I have new e-mails (I
> don't wanna read it from my program, i just wanna it to tells me "yes,
> you have new mail(s)" or "no".
>
> My question is, how do I log into my google account using urllib2 to
> check this? Does anyone have a sample script that I can use or
> something to help me?
>
> thanks in advice,
> Matheus

Gmail supports pop and smtp access.  See
http://docs.python.org/lib/module-poplib.html and
http://docs.python.org/lib/module-smtplib.html,  as well as
http://mail.google.com/support/bin/answer.py?answer=13273 and
http://mail.google.com/support/bin/answer.py?answer=13287


Here's a simple pop script:
import poplib

me = '[EMAIL PROTECTED]'
p = '***' # put your real password of course..

host ='pop.gmail.com'

pop = poplib.POP3_SSL(host)

try:
pop.user(me)
pop.pass_(p)
message_count, mailbox_size = pop.stat()
# Do some more stuff here...
finally:
pop.quit()

print message_count, mailbox_size


And a simple smtp script:
import smtplib

me = '[EMAIL PROTECTED]'
p = '***' # put your real password of course..

server = smtplib.SMTP('smtp.gmail.com', 587)

try:
server.ehlo()
server.starttls()
server.ehlo()
server.login(me, p)
# Do some more stuff here...
finally:
server.close()


HTH,
~Simon

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


Re: Simple file writing techiques ...

2006-07-19 Thread Simon Forman
cdecarlo wrote:
> Hello,
>
> I've often found that I am writing little scripts at the interpretor to
> read a text file, perform some conversion, and then write the converted
> data back out to a file. I normally accomplish the above task by



>
> Any suggestions,
> 
> Colin

You should check out the fileinput module.

HTH,
~Simon

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


Re: Depricated String Functions in Python

2006-07-20 Thread Simon Forman
Anoop wrote:
> Thanks Stefen
>
> let me be more specific how would i have to write the following
> function in the deprecated format
>
> map(string.lower,list)
>
> Thanks Anoop

Ah.  This is easy enough:

lower_list = [s.lower() for s in str_list]

Or, if you really like map() (or really don't like list comprehensions
;P ) you could use this:

lower_list = map(lambda s : s.lower(), str_list)


Hope this helps,
~Simon

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


Re: regular expression - matches

2006-07-21 Thread Simon Forman
abcd wrote:
> how can i determine if a given character sequence matches my regex,
> completely?
>
> in java for example I can do,
> Pattern.compile(regex).matcher(input).matches()
>
> this returns True/False whether or not input matches the regex
> completely.
>
> is there a matches in python?

Yes.  It's called match and it's in the re module
(http://docs.python.org/lib/module-re.html)

Python's re.match() matches from the start of the string, so if you
want to ensure that the whole string matches completely you'll probably
want to end your re pattern with the "$" character (depending on what
the rest of your pattern matches.)

HTH,
~Simon

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


Re: regular expression - matches

2006-07-21 Thread Simon Forman
John Salerno wrote:
> Simon Forman wrote:
>
> > Python's re.match() matches from the start of the string, so if you
> > want to ensure that the whole string matches completely you'll probably
> > want to end your re pattern with the "$" character (depending on what
> > the rest of your pattern matches.)
>
> Is that necessary? I was thinking that match() was used to match the
> full RE and string, and if they weren't the same, they wouldn't match
> (meaning a begin/end of string character wasn't necessary). That's wrong?

My understanding, from the docs and from dim memories of using
re.match() long ago, is that it will match on less than the full input
string if the re pattern allows it (for instance, if the pattern
*doesn't* end in '.*' or something similar.)

I'd test this, though, before trusting it.

What the heck, I'll do that now:

>>> import re
>>> re.match('ab', 'abcde')
<_sre.SRE_Match object at 0xb6ff8790>
>>> m = _
>>> m.group()
'ab'
>>> print re.match('ab$', 'abcde')
None


Yup!  That's the case.

Peace,
~Simon

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


Re: An optparse question

2006-07-21 Thread Simon Forman
T wrote:
> fuzzylollipop wrote:
> >
> > you can make the usage line anything you want.
> >
> > ...
> > usage = 'This is a line before the usage line\nusage %prog [options]
> > input_file'
> > parser = OptionsParser(usage=usage)
> > parser.print_help()
> > ...
> >
>
> No, that affects the string printed only *after* the "usage = " string.
>  What I would like to do is insert some string *before* the "usage = "
> string, which is right after the command I type at the command prompt.
> So I would like to make it look like this:
>
> % myprog.py -h
>  THIS IS NEWLY INSERTED STRING 
> usage: myprog.py [options] input_file
>
>
> options:
>   -h, --help show this help message and exit
>   -v, --verboseprint program's version number and exit
>   -o FILE   Output file

It's possible, but it ain't easy:

from optparse import OptionParser, _, IndentedHelpFormatter

class MyFormatter(IndentedHelpFormatter):
pre_usage = "Hi there!\n"
def format_usage(self, usage):
return _("%susage: %s\n") % (self.pre_usage, usage)

parser = OptionParser(formatter=MyFormatter())


The above filthy hack will print "Hi there!" before the usual usage
message.

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


Re: An optparse question

2006-07-21 Thread Simon Forman
[EMAIL PROTECTED] wrote:
> > No, that affects the string printed only *after* the "usage = " string.
> >  What I would like to do is insert some string *before* the "usage = "
> > string, which is right after the command I type at the command prompt.
> > So I would like to make it look like this:
>
> The example was fine (except for a typo) as far as demonstrating the
> concept.  Try this corrected version:
>
> from optparse import OptionParser
>
> usage = ' THIS IS NEWLY INSERTED STRING
> \nusage: %prog [options] input_file'
> parser = OptionParser(usage=usage)
> parser.print_help()

Nope.  That only *nearly* does what T wants.  The usage message will
still be printed immediately *after* the 'usage: ' string.

>>> parser = OptionParser(usage=usage)
>>> parser.print_help()
usage:  THIS IS NEWLY INSERTED STRING
usage: lopts.py [options] input_file

options:
  -h, --help  show this help message and exit


I had the same problem, and in order to get something printed before
the usage message, I found one easy-ish way was to subclass the
Formatter passed in to the Parser.

IMHO, optparse does a tricky task well, but it's implemented in a hard
to follow, inflexible manner.  My "favorite" pet peeve is that the
options "dictionary" it returns isn't a dict.  I wound up doing this to
it to get something [I considered] useful:

o, a = parser.parse_args()
o = o.__dict__.copy()


Peace,
~Simon

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


Re: regular expression - matches

2006-07-21 Thread Simon Forman

John Salerno wrote:
> Thanks guys!

A pleasure.  : )

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


Re: Nested function scope problem

2006-07-21 Thread Simon Forman
Gerhard Fiedler wrote:
> On 2006-07-21 21:05:22, Josiah Manson wrote:
>
> > I found that I was repeating the same couple of lines over and over in
> > a function and decided to split those lines into a nested function
> > after copying one too many minor changes all over. The only problem is
> > that my little helper function doesn't work! It claims that a variable
> > doesn't exist. If I move the variable declaration, it finds the
> > variable, but can't change it. Declaring the variable global in the
> > nested function doesn't work either.
> >
> > But, changing the variable in the containing scope is the whole purpose
> > of this helper function.
> >
> > I'm new to python, so there is probably some solution I haven't
> > encountered yet. Could you please suggest a nice clean solution? The
> > offending code is below. Thanks.
>
> I'm no Python specialist, so here's just some guesses... I don't know how
> to make variables known inside the inner function. It seems just using the
> names there overrides the outside names. It also seems that local variables
> are in some kind of dictionary; so maybe you can access them through that
> somehow.
>
> One other solution (a bit ugly) would be to make this a class with two
> static methods (breakLine and _addTok) and two static attributes (_ls and
> _tok).
>
> Still another (also ugly) would be to pass both tok and ls to addTok() and
> pass tok back out again. (I think ls doesn't have to be passed back,
> because it is a list and so its data gets modified. tok's data doesn't get
> modified, so local changes don't propagate to the outside.)
>
> Gerhard

That third option seems to work fine.

def breakLine(s):
"""Break a string into a list of words and symbols.
"""

def addTok(tok, ls):
if len(tok) > 0:
ls.append(tok)
tok = ''
return tok

ls = []
tok = ''
splitters = '?()&|:~,'
whitespace = ' \t\n\r'

for c in s:
if c in splitters:
tok = addTok(tok, ls)
ls.append(c)
elif c in whitespace:
tok = addTok(tok, ls)
else:
tok = tok + c

tok = addTok(tok, ls)

return ls

#some tests to make sure it works
print breakLine('carolina(Prada):cat(X,Y)')
print breakLine('trouble :bird (X ) &cat ( Y )')
print breakLine('?trouble')

# Prints:
['carolina', '(', 'Prada', ')', ':', 'cat', '(', 'X', ',', 'Y', ')']
['trouble', ':', 'bird', '(', 'X', ')', '&', 'cat', '(', 'Y', ')']
['?', 'trouble']

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


Re: Which Pyton Book For Newbies?

2006-07-23 Thread Simon Forman
W. D. Allen wrote:
> I want to write a retirement financial estimating program. Python was
> suggested as the easiest language to use on Linux. I have some experience
> programming in Basic but not in Python.
>
> I have two questions:
>  1. What do I need to be able to make user GUIs for the program, and
>  2. Which book would be easiest to use to learn Python programming?
>
> Thanks,
>
> WDA
> [EMAIL PROTECTED]


Tkinter is an easy to use GUI that comes with python.  There's a good
online manual, "Tkinter reference: a GUI for Python", at
http://infohost.nmt.edu/tcc/help/lang/python/tkinter.html

HTH,
~Simon

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


Re: Track keyboard and mouse usage

2006-07-23 Thread Simon Forman
Dennis Lee Bieber wrote:
> On 17 Jul 2006 21:00:09 -0700, "dfaber" <[EMAIL PROTECTED]> declaimed
> the following in comp.lang.python:
>
> > Is there no clean method of accessing the keyboard device or the mouse
> > on linux?
> > It seems that looking at /proc/interrupts might prove to be useful for
> > keyboard monitoring. What about checking if the left mouse button is
> > clicked or finding the position of the cursor on the screen?
>
...
>
>   I don't think anyone has ported raw X-protocol access to Python.
>

Actually someone did.  http://python-xlib.sourceforge.net/  It's old
but it works fine.  Speaks X protocol in pure python.

HTH,
~Simon

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


Re: Search within running python scripts

2006-07-24 Thread Simon Forman
gmax2006 wrote:
> Hi,
>
> Is it possible that a python script finds out whether another instance
> of it is currently running or not?
>
> Thank you,
> Max

Yes, there are several ways.  What OS are you using?

~Simon

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


Re: Search within running python scripts

2006-07-24 Thread Simon Forman
gmax2006 wrote:
> Simon Forman wrote:
> > gmax2006 wrote:
> > > Hi,
> > >
> > > Is it possible that a python script finds out whether another instance
> > > of it is currently running or not?
> > >
> > > Thank you,
> > > Max
> >
> > Yes, there are several ways.  What OS are you using?
> >
> > ~Simon
>
> I have to use an os-independent approach.
>
> At this point I use a file as running-flag. It doesn't work so good.
> Because if the python application breaks or get terminated, it won't
> run again unless somebody deletes the flag file.
>
> Alan

Hmm,  I'm very far from being an expert on this, so hopefully someone
who knows better will reply.
You might have to check the OS your script is running on and do, say,
what faulkner proposed for linux (and Mac?), and something like
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474070 for
windows.

HTH,
~Simon

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


Re: Search within running python scripts

2006-07-24 Thread Simon Forman
Cameron Laird wrote:
...
> Particularly when I hear "os-independent", I think first of
> binding to a socket.  While http://wiki.tcl.tk/1558 >
> is written for a Tcl-based crowd, the commentary there ap-
> plies quite well to Python.

I was going to suggest something like this, as I have noticed that IDLE
seems to do exactly this, and on windows and linux, but I was afraid to
look the fool if it was indeed foolish. (and also, I didn't know
details of it.)

Thanks Cameron.

Peace,
~Simon

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


Re: dicts vs classes

2006-07-25 Thread Simon Forman
Simon Hibbs wrote:
> I'm wondering about whether to use objects in this way or dictionaries
> for a program I'm writing at the moment. It seems to me that unless you
> need some of the functionality supplied with dictionaries (len(a),
> has_key, etc) then simple objects are a syntacticaly cleaner and more
> natural way to express yourself.
>
> Any objctions to this, or pitfalls?
>
> Simon Hibbs

I'm not sure, but I think this should be the other way round: unless
you need special behavior that dicts don't supply (methods) or you
really want/need obj.attr notation, you're better off just using dicts,
but Marco Wahl is right, if it really matters measure it.

Peace,
~Simon

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


Re: Help in string.digits functions

2006-07-25 Thread Simon Forman
John McMonagle wrote:
> On Mon, 2006-07-24 at 22:19 -0700, Anoop wrote:
> > Hi All
> >
> > I am getting two different outputs when i do an operation using
> > string.digits and test.isdigit(). Is there any difference between the
> > two. I have given the sample program and the output
> >
> > Thanks for ur inputs
> >
> > Anoop
> >
> > #1:
> > ~~
> > import string
> >
> > test='121206'
> >
> > if test not in string.digits:
> > print "I am Not Digit"
> > else:
> > print "I am Digit"
> >
> > #2:
> > ~~
> > import string
> >
> > test='121206'
> >
> > if not test.isdigit():
> > print "I am Not Digit"
> > else:
> > print "I am Digit"
> >
> > Output
> > ~
> > #1:I am Not Digit
> > #2:I am Digit
> >
> > Thnks and Rgds
> >
> > Anoop
> >
>
>
> string.digits is the string constant '0123456789'
>
> So your test, "if test not in string.digits:" will evaluate True because
> '121206' is not in '0123456789'.
>
> Whereas test.isdigit() returns true if all the characters in test are
> digits.
>
> So yes, there is a big difference between the two.
>
> Regards,
>
> John
>
>
>


Your first test could be rewritten to do what I think you're thinking
it should do like so:

import string

test='121206'

for ch in test:
if ch not in string.digits:
print "I am not all Digits"
break
else:
print "I am all Digits"

But isdigit() would be the better way.


Peace,
~Simon

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


Re: print function question

2006-07-25 Thread Simon Forman
Bertrand-Xavier M. wrote:
> On Tuesday 25 July 2006 05:52, Eric Bishop wrote:
> > Why does this work:
> >
> > # start
> > a = 5
> >
> > print a, 'is the number'
> >
> > #end, prints out "5 is the number"
> >
> > But not this:
> >
> > # start
> >
> > a = 5
> >
> > print a 'is the number'
> >
> > #end, errors out
> >
> > The difference here is the comma seperating the variable and the string
> > literal. Is the comma some sort of concatenation operator or is the comma
> > necessary in some form of a requirement in the print function, i.e is the
> > variable a an argument to print as well as 'is th number' another argument
> > to print?
>
> Yes.
> It allows to concat several variables, and also adds a space.
> These do work as well:
>
> a = 5
> print "value is", a
> print "value %s" %(a)
> print "value is", a, '...'
>
> Regards,
> Rob

Also, a comma at the end of a print statement surpresses the usual
trailing newline (it will cause a space to appear instead if you print
something else, but NOT if you write directly to stdout.)

print "Hello",
print "world!"

# prints Hello world! on one line with a space between them, but

import sys
print "Hello",
sys.stdout.write("world!")

# prints Helloworld!

Peace,
~Simon

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


Re: cStringIO.StringIO has no write method?

2006-07-25 Thread Simon Forman

Laszlo Nagy wrote:
> >>
> > Nope. StringI is an input-only object, StringO is an output object. You
> > got a StringI because you gave a string argument to the creator.
> >
> >
> >  >>> f1 = cStringIO.StringIO()
> >  >>> f1
> > 
> >  >>> dir(f1)
> > ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
> > '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__',
> > '__repr__', '__setattr__', '__str__', 'close', 'closed', 'flush',
> > 'getvalue', 'isatty', 'next', 'read', 'readline', 'readlines', 'reset',
> > 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines']
> >  >>> f2 = cStringIO.StringIO("This is the fixed content of the StringIO")
> >  >>> f2
> > 
> >  >>> dir(f2)
> > ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
> > '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__',
> > '__repr__', '__setattr__', '__str__', 'close', 'closed', 'flush',
> > 'getvalue', 'isatty', 'next', 'read', 'readline', 'readlines', 'reset',
> > 'seek', 'tell', 'truncate']
> >  >>>
> >
> Is it possible to have a direct access in-memory file that can be
> written and read too?
>
>Laszlo

I think that's what you'd get if you changed

f = cStringIO.StringIO('') 

to

f = cStringIO.StringIO() 

?

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


Re: building an index for large text files for fast access

2006-07-25 Thread Simon Forman
Yi Xing wrote:
> Hi,
>
> I need to read specific lines of huge text files. Each time, I know
> exactly which line(s) I want to read. readlines() or readline() in a
> loop is just too slow. Since different lines have different size, I
> cannot use seek(). So I am thinking of building an index for the file
> for fast access. Can anybody give me some tips on how to do this in
> Python? Thanks.
>
> Yi

I had to do this for some large log files.  I wrote one simple script
to generate the index file and another that used the index file to read
lines from the log file.  Here are (slightly cleaned up for clarity)
the two scripts.  (Note that they'll only work with files less than
4,294,967,296 bytes long..  If your files are larger than that
substitute 'Q' for 'L' in the struct formats.)

First, genoffsets.py
#!/usr/bin/env python
'''
Write the byte offset of each line.
'''
import fileinput
import struct
import sys

def f(n): return struct.pack('L', n)

def main():
total = 0

# Main processing..
for n, line in enumerate(fileinput.input()):

sys.stdout.write(f(total))

total += len(line)

# Status output.
if not n % 1000:
print >> sys.stderr, '%i lines processed' % n

print >> sys.stderr, '%i lines processed' % (n + 1)


if __name__ == '__main__':
main()


You use it (on linux) like so:
cat large_file | ./genoffsets.py > index.dat

And here's the getline.py script:
#!/usr/bin/env python
'''
Usage: "getline.py   "

Prints line num from datafile using indexfile.
'''
import struct
import sys

fmt = 'L'
fmt_size = struct.calcsize(fmt)


def F(n, fn):
'''
Return the byte offset of line n from index file fn.
'''
f = open(fn)

try:
f.seek(n * fmt_size)
data = f.read(fmt_size)
finally:
f.close()

return struct.unpack(fmt, data)[0]


def getline(n, data_file, index_file):
'''
Return line n from data file using index file.
'''
n = F(n, index_file)
f = open(data_file)

try:
f.seek(n)
data = f.readline()
finally:
f.close()

return data


if __name__ == '__main__':
dfn, ifn, lineno = sys.argv[-3:]
n = int(lineno)
print getline(n, dfn, ifn)



Hope this helps,
~Simon

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


Re: list problem

2006-07-25 Thread Simon Forman
placid wrote:
> Hi all,
>
> I have two lists that contain strings in the form string + number for
> example
>
> >>> list1 = [ ' XXX1', 'XXX2', 'XXX3', 'XXX5']
>
> the second list contains strings that are identical to the first list,
> so lets say the second list contains the following
>
> >>> list1 = [ ' XXX1', 'XXX2', 'XXX3', 'XXX6']
>
> and now what ive been trying to do is find the first string that is
> available,
> i.e a string that is in neither of the two lists so the following code
> should only print XXX4 then return.
>
> for i in xrange(1,10):
> numpart = str(1) + str("%04i" %i)
> str = "XXX" + numpart
>
>   for list1_elm in list1:
>   if list1_elm == str:
>break
>   else:
>for list2_elm in list2:
>if list2_elm == str:
>   break
>else:
>   print str
>   return
>
> Cheer

Well first off, don't use 'str' for a variable name.

Second, "%04i" % i creates a string, don't call str() on it.

Third, str(1) will always be "1" so just add that to your format string
already "1%04i" % i

(And if the "XXX" part is also constant then add that too: "XXX1%04i" %
i)

Finally, you can say:

for i in xrange(1,10):
s = "XXX1%04i" % i
if s not in list1 and s not in list2:
print s

HTH,
~Simon

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


Re: list problem

2006-07-25 Thread Simon Forman
Simon Forman wrote:
> Finally, you can say:
>
> for i in xrange(1,10):
> s = "XXX1%04i" % i
> if s not in list1 and s not in list2:
> print s
>
> HTH,
> ~Simon

D'oh!  Forgot to break.

for i in xrange(1,10):
s = "XXX1%04i" % i
if s not in list1 and s not in list2:
print s
break

Peace,
~Simon

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


Re: list problem

2006-07-25 Thread Simon Forman
placid wrote:
> Simon Forman wrote:
> > placid wrote:
> > > Hi all,
> > >
> > > I have two lists that contain strings in the form string + number for
> > > example
> > >
> > > >>> list1 = [ ' XXX1', 'XXX2', 'XXX3', 'XXX5']
> > >
> > > the second list contains strings that are identical to the first list,
> > > so lets say the second list contains the following
> > >
> > > >>> list1 = [ ' XXX1', 'XXX2', 'XXX3', 'XXX6']
> > >
> > > and now what ive been trying to do is find the first string that is
> > > available,
> > > i.e a string that is in neither of the two lists so the following code
> > > should only print XXX4 then return.
> > >
> > > for i in xrange(1,10):
> > > numpart = str(1) + str("%04i" %i)
> > > str = "XXX" + numpart
> > >
> > >   for list1_elm in list1:
> > >   if list1_elm == str:
> > >break
> > >   else:
> > >for list2_elm in list2:
> > >if list2_elm == str:
> > >   break
> > >else:
> > >   print str
> > >   return
> > >
> > > Cheer
> >
> > Well first off, don't use 'str' for a variable name.
> >
> > Second, "%04i" % i creates a string, don't call str() on it.
> >
> > Third, str(1) will always be "1" so just add that to your format string
> > already "1%04i" % i
> >
>
> thanks for the tips
>
> > (And if the "XXX" part is also constant then add that too: "XXX1%04i" %
> > i)
> >
> > Finally, you can say:
> >
> > for i in xrange(1,10):
> > s = "XXX1%04i" % i
> > if s not in list1 and s not in list2:
> > print s
> >
>
> But there may be other characters before XXX (which XXX is constant). A
> better example would be, that string s is like a file name and the
> characters before it are the absolute path, where the strings in the
> first list can have a different absolute path then the second list
> entries. But the filenames are always exact. So you need to split the
> entries bases on "\\" (windows machine) and match on this ?
>
>
> Cheers

hmm, a slightly different problem than your OP.

Yeah, I would build a new list (or set) from the contents of BOTH lists
with the prefixes stripped off and test your target string against
that.  You might also be able to do something with the endswith()
method of strings.

test = set(n[3:] for n in list1) + set(n[3:] for n in list2)

if s not in test: print s


It's late though, so I may be being stupid.  ;-)

Peace,
~Simon

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


Re: list problem

2006-07-26 Thread Simon Forman
placid wrote:
>
> But there may be other characters before XXX (which XXX is constant). A
> better example would be, that string s is like a file name and the
> characters before it are the absolute path, where the strings in the
> first list can have a different absolute path then the second list
> entries. But the filenames are always exact. So you need to split the
> entries bases on "\\" (windows machine) and match on this ?
>
>
> Cheers

If you're actually working with filenames and paths then you should use
os.path.basename() to get just the filename parts of the paths.

test = set(map(os.path.basename, list1))
test |= set(map(os.path.basename, list2))

(Note: I *was* being stupid last night, the + operator doesn't work for
sets. You want to use | )

Peace,
~Simon

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


Re: import from containing folder

2006-07-26 Thread Simon Forman
David Isaac wrote:
> Suppose I have inherited the structure
>
> PackageFolder/
>   __init__.py
>   mod1.py
>   mod2.py
>   SubPackageFolder/
>   __init__.py
>   mod3.py
>
> and mod3.py should really use a function in mod2.py.
> *Prior* to Python 2.5, what is the best way to access that?
> (Please assume that "move it" is not the answer.)
>
> Thanks,
> Alan Isaac

To play with this I created a directory 'delmepy' in
/usr/lib/python2.4/site-packages/ and gave it the following structure:

$ ls -R /usr/lib/python2.4/site-packages/delmepy
/usr/lib/python2.4/site-packages/delmepy:
__init__.py  mod2.py  subp
/usr/lib/python2.4/site-packages/delmepy/subp:
__init__.py  mod3.py

Both __init__.py's are empty, mod2.py looks like this:

def banana(n):
print n

and mod3.py looks like this:

import delmepy.mod2
delmepy.mod2.banana(23)

The following (at the interactive prompt) worked fine:
>>> import delmepy.subp.mod3
23

I would assume (but I haven't checked) that this should work as long as
delmepy (in your case PackageFolder) was somewhere on sys.path.

HTH,
~Simon

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


Re: Tkinter pack Problem

2006-07-26 Thread Simon Forman
I find the "Tkinter reference: a GUI for Python" under "Local links" on
this page http://infohost.nmt.edu/tcc/help/lang/python/tkinter.html to
be very helpful.  It has a decent discussion of the grid layout
manager.

HTH,
~Simon

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


Re: extender method

2006-07-26 Thread Simon Forman
[EMAIL PROTECTED] wrote:
> 'Learning Python' by Lutz and Ascher (excellent book by the way)
> explains that a subclass can call its superclass constructor as
> follows:
>
> class Super:
>def method(self):
># do stuff
>
> class Extender(Super):
>def method(self):
>Super.method(self)   # call the method in super
># do more stuff - additional stuff here
>
>
>
> I'm trying to use this for a superclass called 'component' in the
> constructor. I have different types of component (let's say for
> arguments sake resistor, capacitor etc). When I instantiate a new
> resistor, say, I want the constructor to call the constructor within
> the component superclass, and then add some resistor-specific stuff.
>
> Now, this is fine using the above code. Where I'm struggling is with
> argument passing. The following, for example, doesn't seem to work:
>
> class Super:
>def __init__(self, **kargs):
>self.data = kargs
>
> class Extender(Super):
>def __init__(self, **kargs):
>Super.__init__(self, kargs)   # call the constructor method in Super
># do additional extender-specific stuff here
>
> What am I doing wrong? I get:
> TypeError: __init__() takes exactly 1 argument (2 given)
> WARNING: Failure executing file: 
>
> Dave

Try this:

class Extender(Super):
   def __init__(self, **kargs):
   Super.__init__(self, **kargs)   # call the constructor method in
Super

(add two asterisks to the call.)

Observe, the following script:

def a(*a, **b):
return a, b

print a(**{'arg':2})
print a(arg=2)
print a({'arg':2})

# Prints:

((), {'arg': 2})
((), {'arg': 2})
(({'arg': 2},), {})


HTH,
~Simon

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


Re: Changing a value for each folder while traversing a file system

2006-07-26 Thread Simon Forman
PipedreamerGrey wrote:
> I'm using the script below (originally from http://effbot.org, given to
> me here) to open all of the text files in a directory and its
> subdirectories and combine them into one Rich text
> file (index.rtf).  Now I'm adapting the script to convert all the text
> files into individual html files.  What I can't figure out is how to
> trigger a change in the background value for each folder change (not
> each file), so that text is color-coded by folder.
>
> I have tried to integrate the following snippet into the directory
> walker, so that I could later write it to the file as text: color =
> random.choice(["#99", "#CC", "#99"])
> That method didn't work, does anyone else have a suggestion?
>
>
> #! /usr/bin/python
> import glob
> import fileinput
> import os
> import string
> import sys
>
> index = open("index.rtf", 'w')
>
> class DirectoryWalker:
> # a forward iterator that traverses a directory tree, and
> # returns the filename
>
> def __init__(self, directory):
> self.stack = [directory]
> self.files = []
> self.index = 0
>
> def __getitem__(self, index):
> while 1:
> try:
> file = self.files[self.index]
> self.index = self.index + 1
> except IndexError:
> # pop next directory from stack
> self.directory = self.stack.pop()
> self.files = os.listdir(self.directory)
> self.index = 0
> else:
> # get a filename, eliminate directories from list
> fullname = os.path.join(self.directory, file)
> if os.path.isdir(fullname) and not
> os.path.islink(fullname):
> self.stack.append(fullname)
> else:
> return fullname
>
> for file in DirectoryWalker("."):
> # divide files names into path and extention
> path, ext = os.path.splitext(file)
> # choose the extention you would like to see in the list
> if ext == ".txt":
> print file
> file = open(file)
> fileContent = file.readlines()
># just for example, let's say I want to print the color here as
> if in an html tag...
> index.write(color)
> for line in fileContent:
> if not line.startswith("\n"):
> index.write(line)
> index.write("\n")
>
> index.close()

Add a color attribute to the DirectoryWalker class, change it when you
change directories, return it with the fullname.  Change your for loop
like so:

for color, file in DirectoryWalker("."):
# ...

I think that should work.

HTH,
~Simon

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


Re: subprocess problem on WinXP

2006-07-26 Thread Simon Forman
Wolfgang wrote:
> Hi,
>
> I want to compress all files (also in subfolder). The code is working
> more or less, but I get a black popup window (command line window) for
> every file to compress. How do I cave to change my system call that
> nothing pops up?
>
> Wolfgang
>
> import os
> import subprocess
>
> dir="g:\\messtech"
>
> for root, dirs, files in os.walk(dir):
>  for file in files:
>  f=os.path.join(root,file)
>  subprocess.Popen([r"bzip2",'', f],shell=False).wait()

How about forgetting the system call and just using the bz2 standard
library module?

http://docs.python.org/lib/module-bz2.html

Peace,
~Simon

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


Re: import from containing folder

2006-07-26 Thread Simon Forman

David Isaac wrote:
> Alan wrote:
> > I do not want to make any assumptions about
> > this particular package being on sys.path.
> > (I want a relative import, but cannot assume 2.5.)
>
>
> I should mention that to get around this I have
> been using
> sys.path.append(os.path.split(sys.argv[0])[0])
> in the script I care most about.  It works,
> but seems like a hack.
>
> Thanks,
> Alan Isaac

Hack or not, that's what I would do.  In fact I have done that.  I hope
if there's a better way that someone will post it here.  ;-)

Peace,
~Simon

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


Re: subprocess problem on WinXP

2006-07-26 Thread Simon Forman
Wolfgang wrote:
> Hi Simon,
>
> I did not know that library! I'm still new to python and I still have
> problems to find the right commands.

Welcome. : )  Python comes with "batteries included".  I'm always
finding cool new modules myself, and I've been using it for years. In
fact, I didn't notice the bz2 module until about a week ago.

Browse the standard library docs for fun:
http://docs.python.org/lib/lib.html  there's all kinds of cool stuff in
there.  Whenever you say to yourself, "Hmm, somebody must have had this
problem before," reach for the standard library.  The solution's likely
already in there.

>
> But I suppose this library is mainly for partially
> compressing/decompressing of files. How can I use that library to
> compress/decompress full files without reading them into memory? And
> what about performance?

Read the docs.  There seems to be api for (de)compressing both
"streams" of data and whole files.

I don't know about performance, as I've never tried to use the module
before, but I would bet that it's good.  It almost certainly uses the
same bzip2 library as the bzip2 program itself and it avoids the
overhead of creating a new process for each file.

But if you're in doubt (and performance really matters for this
application) test and measure it.

I think your script could be rewritten as follows with good speed and
memory performance, but I haven't tested it (and the output filepaths
may not be what you want...):

import os
import bz2

dir_ = r"g:\messtech"


for root, dirs, files in os.walk(dir_):
for file_ in files:
f = os.path.join(root, file_)
bzf = os.path.join(f, '.bz2')

F = open(f)
BZF = BZ2File(bzf, 'w')

try:
for line in F: BZF.write(line)
finally:
F.close()
BZF.close()


Also, note that I changed 'dir' and 'file' to 'dir_' and 'file_'.  Both
dir and file are python built-ins, so you shouldn't reuse those names
for your variables.


Peace,
~Simon

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


Re: Splitting a float into bytes:

2006-07-26 Thread Simon Forman
Michael Yanowitz wrote:
> Hello:
>
>   For some reason I can't figure out how to split
> a 4-byte (for instance) float number (such as 3.14159265359)
> into its 4-bytes so I can send it via a socket to another
> computer.
>   For integers, it is easy, I can get the 4 bytes by anding like:
> byte1 = int_val & 0x00FF
> byte2 = int_val & 0xFF00
> byte3 = int_val & 0x00FF
> byte4 = int_val & 0xFF00
>   But if I try to do that with floats I get:
> >>> pi & 0xFF
> Traceback (most recent call last):
>   File "", line 1, in ?
> TypeError: unsupported operand type(s) for &: 'float' and 'int'
>
>   Is there some easy way to get what the bytes of the float are?
>
> Thanks in advance:
> Michael Yanowitz

The struct module.  (It also works for ints. ;-)  )

http://docs.python.org/lib/module-struct.html


HTH,
~Simon

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


Re: Splitting a float into bytes:

2006-07-26 Thread Simon Forman
Michael Yanowitz wrote:
> -Original Message-
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] Behalf
> Of Simon Forman
> Sent: Wednesday, July 26, 2006 2:56 PM
> To: python-list@python.org
> Subject: Re: Splitting a float into bytes:
>
>
> Michael Yanowitz wrote:
> > Hello:
> >
> >   For some reason I can't figure out how to split
> > a 4-byte (for instance) float number (such as 3.14159265359)
> > into its 4-bytes so I can send it via a socket to another
> > computer.
> >   For integers, it is easy, I can get the 4 bytes by anding like:
> > byte1 = int_val & 0x00FF
> > byte2 = int_val & 0xFF00
> > byte3 = int_val & 0x00FF
> > byte4 = int_val & 0xFF00
> >   But if I try to do that with floats I get:
> > >>> pi & 0xFF
> > Traceback (most recent call last):
> >   File "", line 1, in ?
> > TypeError: unsupported operand type(s) for &: 'float' and 'int'
> >
> >   Is there some easy way to get what the bytes of the float are?
> >
> > Thanks in advance:
> > Michael Yanowitz
>
> The struct module.  (It also works for ints. ;-)  )
>
> http://docs.python.org/lib/module-struct.html
>
>
> HTH,
> ~Simon
>
>Thanks, but maybe I am missing something.
> If I use pack, doesn't it have to be unpacked at the other end
> to make sense? The data will be picked up on some other computer
> by some other application probably written in C or C++. Would
> it have to be rewritten to unpack the data?
>
> Thanks in advance:
> Michael Yanowitz

It says in the docs "This module performs conversions between Python
values and C structs represented as Python strings."  This means, to
me, that the packed data will be in the format that C (and C++ I would
assume) uses.

You might have to mind the endian-ness if you're transferring data
between different architectures, but struct includes format specifiers
for this.

Have fun,
~Simon

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


Re: splitting words with brackets

2006-07-26 Thread Simon Forman
Qiangning Hong wrote:
> faulkner wrote:
> > re.findall('\([^\)]*\)|\[[^\]]*|\S+', s)
>
> sorry i forgot to give a limitation: if a letter is next to a bracket,
> they should be considered as one word. i.e.:
> "a(b c) d" becomes ["a(b c)", "d"]
> because there is no blank between "a" and "(".

This variation seems to do it:

import re

s = "a (b c) d [e f g] h i(j k) l [m n o]p q"

def splitup(s):
return re.findall('''
\S*\( [^\)]* \)\S*  |
\S*\[ [^\]]* \]\S*  |
\S+
''', s, re.VERBOSE)

print splitup(s)

# Prints

['a', '(b c)', 'd', '[e f g]', 'h', 'i(j k)', 'l', '[m n o]p', 'q']


Peace,
~Simon

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


Re: splitting words with brackets

2006-07-26 Thread Simon Forman

Qiangning Hong wrote:
> Tim Chase wrote:
> >  >>> import re
> >  >>> s ='a (b c) d [e f g] h ia abcd(b c)xyz d [e f g] h i'
> >  >>> r = re.compile(r'(?:\S*(?:\([^\)]*\)|\[[^\]]*\])\S*)|\S+')
> >  >>> r.findall(s)
> > ['a', '(b c)', 'd', '[e f g]', 'h', 'ia', 'abcd(b c)xyz', 'd',
> > '[e f g]', 'h', 'i']
> >
> [...]
> > However, the above monstrosity passes the tests I threw at
> > it.
>
> but it can't pass this one: "(a c)b(c d) e"
> the above regex gives out ['(a c)b(c', 'd)', 'e'], but the correct one
> should be ['(a c)b(c d)', 'e']

What are the desired results in cases like this:

"(a b)[c d]" or "(a b)(c d)" ?

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


Re: removing duplicates, or, converting Set() to string

2006-07-26 Thread Simon Forman

[EMAIL PROTECTED] wrote:
> Hello,
>
> I have some lists for which I need to remove duplicates. I found the
> sets.Sets() module which does exactly this, but how do I get the set
> back out again?
>
> # existing input: A,B,B,C,D
> # desired result: A,B,C,D
>
> import sets
> dupes = ['A','B','B','C','D']
> clean = sets.Set(dupes)
>
> out = open('clean-list.txt','w')
> out.write(clean)
> out.close
>
> ---
> out.write(clean) fails with "TypeError: argument 1 must be string or
> read-only character buffer, not Set" and out.write( str(clean) )
> creates "Set(['A', 'C', 'B', 'D'])" instead of just A,B,C,D.
>
> thanks in advance for your time,
>
> -matt

Do ','.join(clean) to make a single string with commas between the
items in the set.  (If the items aren't all strings, you'll need to
convert them to strings first.)

Peace,
~Simon

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


Re: Functions and code objects

2006-07-27 Thread Simon Forman
Fuzzyman wrote:
> Fuzzyman wrote:
> > Fuzzyman wrote:
> > > Hello all,
> > >
> > > I'm trying to extract the code object from a function, and exec it
> > > without explicitly passing parameters.
> > >
> > > The code object 'knows' it expects to receive paramaters. It's
> > > 'arg_count' attribute is readonly.
> > >
> > > How can I set the arg_count to 0, or pass parameters to the code object
> > > when I exec it ?
> > >
> >
> > Ok, so now I'm getting somewhere, without really knowing what I'm
> > doing. Using the CodeType I can create a new code object with identical
> > attributes, except an 'argcount' of 0.
> >
> > It doesn't quite work, so I probably need to set some of the attributes
> > *differently*.
> >
> > The code I use is :
> >
> > >>> def f(x):
> >  ... print x
> >  ...
> > >>> c = f.func_code
> > >>> CodeType = type(c)
> > >>> a = CodeType(0, c.co_nlocals, c.co_stacksize, c.co_flags, c.co_code,
> >  ... c.co_consts, c.co_names, c.co_varnames, c.co_filename,
> > c.co_name,
> >  ... c.co_firstlineno, c.co_lnotab, c.co_freevars, c.co_cellvars)
> > >>> a
> > ", line 1>
> > >>> exec a
> > Traceback (most recent call last):
> >   File "", line 1, in ?
> >   File "", line 2, in f
> > UnboundLocalError: local variable 'x' referenced before assignment
> >
> > (the first argument, 0, becomes the 'arg_count' attribute)
> >
> > So the code object is still creating a local scope, and knows that x is
> > a local variable. ('co_nlocals' and 'co_varnames' ?).
> >
> > I'd like to construct the code object so that it takes the parameters
> > from the enclosing scope (the context I pass into exec I guess),
> > without clobbering any local variables that may be defined in the code
> > object.
> >
> > Anyone got any clues ?
> >
>
> *Damn* I've extracted the code object and told it that it has no
> arguments. Executing the code object results in the function object !
>
> The code object is obviously the code object for the function
> definition. *sigh*
>
> I was hoping I could get to the code object for the *body* of the
> function. Looks like that won't be possible without dis-assembling the
> bytecode or other tricks even more hackish than what I've already done.
>
> For the record, the code  I was using was :
>
> x = 3
> def f(x):
> print x
>
> CodeType = type(f.func_code)
>
> def convert_function(f):
> code = f.func_code
> nlocals = max(code.co_nlocals - code.co_argcount, 0)
> newCode = CodeType(0, nlocals, code.co_stacksize, code.co_flags,
>code.co_code, code.co_consts, code.co_names,
>code.co_varnames, code.co_filename,
> code.co_name,
>code.co_firstlineno, code.co_lnotab,
> code.co_freevars,
>code.co_cellvars)
> return newCode
>
> print convert_function(f)
> exec convert_function(f)
>
> Fuzzyman
> http://www.voidspace.org.uk/python/index.shtml

Out of curiosity, why are you doing this?

Peace,
~Simon

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


Re: Thread Question

2006-07-27 Thread Simon Forman
Ritesh Raj Sarraf wrote:
> Duncan,
>
> I couldn't make out much from the code.

Please, try again to understand Duncan's code.  It's much better than
what you did.

> Instead this is what I did.
>
> threads = []
> nloops = range(len(lRawData))
> for i in nloops:
> (sUrl, sFile, download_size, checksum) =
> stripper(lRawData[i])
> t = threading.Thread(target=download_from_web, args=(sUrl,
> sFile, sSourceDir, None))
> # = pypt_thread(download_from_web, i,
> stripper(lRawData[i]))
> threads.append(t)
>
> i = 0
> join_i = 0
> while i < nloops:
> counter = 0
> while counter < 3:
> threads[i].start()
> counter += 1
> i += 1
> counter = 0
> join_i = i - 3
> while counter < 3:
> threads[join_i].join()
> counter += 1
> join_i += 1
>
> Is this correct ? Comments!!

This is just painful.  It's not exactly "incorrect", but I think (I
*think*, it's hard to tell even after reading it 3 times) that it's
going to only download three requests at a time and if one (or more) of
the requests takes a long time it will hold up all the others.  Also,
you're creating one thread per item in lRawData even though you only
need/use three at a time.

if you set numthreads = 3 in Duncan's code, you would only be
downloading 3 things at a time, but you'd only be using three threads
and long running requests would not affect the others.

If you need help understanding it please ask questions.  I, for one,
would be happy to comment it for you to explain how it works.  It's so
nice and elegant that I've already cut-and-pasted it into my own
"notebook" of cool useful python "patterns" to use in the future.

Reread it slowly, think about what it's doing, if questions arise write
them down and ask them.  Duncan's code is beautiful.  It's well worth
your time to understand it.

Peace,
~Simon

>
> Ritesh
>
>
> Duncan Booth wrote:
> > Ritesh Raj Sarraf wrote:
> >
> > > I'm planning to implement threads in my application so that multiple
> > > items can be downloaded concurrently. I want the thread option to be
> > > user-defined.
> > >
> > > Looking at the documentation of threads (Core Python Programming), I've
> > > noticed that all threads are executed a once. Depending upon what they
> > > are doing, some finish early and some later.
> > >
> > > But I want to implement something like:
> > >
> > > for item in list_items:
> > > for num in thread_args:
> > >thread[num].start()
> > >thread[num].start()
> > >
> > > Is this the correct way of threading applications ?
> > > This is the first time I'd be doing threading. So was looking for
> > > comments and suggestions.
> > >
> >
> > What you want is to use a pool of threads so that you can configure how
> > many requests are issued at a time (you don't want to try to issue 100
> > requests all in parallel). You can communicate with the threads through a
> > Queue.
> >
> > So if the code for a thread looks like:
> >
> >def run(request, response):
> >while 1:
> >item = request.get()
> >if item is None:
> > break
> >response.put(download_from_web(item))
> >
> > # your main loop can be something like:
> >
> > requestQueue = Queue()
> > responseQueue = Queue()
> > thread_pool = [
> > Thread(target=run, args=(requestQueue, responseQueue)
> > for i in range(numthreads)]
> > for t in thread_pool: t.start()
> >
> > for item in list_items:
> >  requestQueue.put(item)
> >
> > for i in range(len(list_items)):
> > response = responseQueue.get()
> > handle_response(response)
> >
> > # and then to shut down the threads when you've finished:
> > for t in thread_pool:
> > requestQueue.put(None)
> > for t in thread_pool:
> >  t.join()

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


Re: locked file

2006-07-27 Thread Simon Forman
Kirt wrote:
> i have a code that backsup file from src to dest.
>  Now if some of the files are locked , i need to skip those files..
> I was trying to use fctl module but it can be used only in unix i
> suppose.
>
> is there anyother way? i am using windows os.

What does locked mean in this case?  No read permissions? In use by
another program?

I haven't used windows in a long time, and I wasn't aware that one
could lock files in it.

Peace,
~Simon

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


Re: Thread Question

2006-07-27 Thread Simon Forman
Duncan Booth wrote:
> Simon Forman wrote:
>
> > If you need help understanding it please ask questions.  I, for one,
> > would be happy to comment it for you to explain how it works.  It's so
> > nice and elegant that I've already cut-and-pasted it into my own
> > "notebook" of cool useful python "patterns" to use in the future.
> >
> > Reread it slowly, think about what it's doing, if questions arise write
> > them down and ask them.  Duncan's code is beautiful.  It's well worth
> > your time to understand it.
>
> If you convert what I wrote into an example which actually runs then do
> repost it. It might also be easier to understand since I'm sure I'll have
> made some mistakes or omitted some critical bits (such as the import
> lines).

A pleasure.

There was one missing close-parenthesis in the Thread() call, but other
than that (and the implied imports and variables) everything was good.

I added some dummy code to simulate long-running requests, and I made
the run() function return its Thread's name, but other than that it's
basically the code you posted.  It's neat to increase NUMTHREADS and
watch the script run faster. :)

When the time comes to use this in something I'll probably wrap it up
in a class, and add some error handling to the run() function (or
method).  But in the meantime it's quite elegant and useful as is.
Definitely not not Scottish ;-)

Thanks Duncan.

Peace,
~Simon

from Queue import Queue
from threading import Thread, currentThread

# Imports for the dummy testing func.
from time import sleep
from random import random

NUMTHREADS = 3


def dummy_func(secs):
'''
Sleep for secs seconds then return secs.
(Dummy function to simulate requests.)
'''
sleep(secs)
return secs

# Some fake requests to pass to dummy_func().
dummy_requests = [
5 * random() for notused in xrange(100)
]

# Dummy handle_response() for demo purposes.
def handle_response(resp): print resp


def run(request, response, func=dummy_func):
'''
Get items from the request Queue, process them
with func(), put the results along with the
Thread's name into the response Queue.

Stop running once an item is None.
'''
name = currentThread().getName()
while 1:
item = request.get()
if item is None:
break
response.put((name, func(item)))


# Create two Queues for the requests and responses
requestQueue = Queue()
responseQueue = Queue()


# Pool of NUMTHREADS Threads that run run().
thread_pool = [
Thread(
target=run,
args=(requestQueue, responseQueue),
name="Thread %i" % i
)
for i in range(NUMTHREADS)
]


# Start the threads.
for t in thread_pool: t.start()


# Queue up the requests.
for item in dummy_requests: requestQueue.put(item)


# Shut down the threads after all requests end.
# (Put one None "sentinel" for each thread.)
for t in thread_pool: requestQueue.put(None)


# Get and handle each response.
for notused in xrange(len(dummy_requests)):
response = responseQueue.get()
handle_response(response)


# Don't end the program prematurely.
#
# (Note that because Queue.get() is blocking by
# default this isn't strictly necessary.  But if
# you were, say, handling responses in another
# thread, you'd want something like this in your
# main thread.)
for t in thread_pool: t.join()



###
Example output from a run of the above script:

('Thread 0', 0.10915462751068916)
('Thread 0', 0.29428189134629135)
('Thread 1', 1.6234285192453246)
('Thread 0', 3.195799156145096)
('Thread 1', 2.7641123440885367)
('Thread 2', 4.7810243032096862)
('Thread 2', 1.1752965020601662)
('Thread 1', 2.0727863018148924)
('Thread 0', 4.8127195859913252)
('Thread 1', 2.4780495377626242)
.
.
.
etc...

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


Re: iter(callable, sentinel)

2006-07-27 Thread Simon Forman

Will McGugan wrote:
> Hi,
>
> I've been using Python for years, but I recently encountered something
> in the docs I wasnt familar with. That is, using two arguements for
> iter(). Could someone elaborate on the docs and maybe show a typical use
> case for it?
>
>
> Thanks,
>
> Will McGugan
>
> --
> work: http://www.kelpiesoft.com
> blog: http://www.willmcgugan.com

I just found out about that recently too.

help(iter) shows this:
Help on built-in function iter in module __builtin__:

iter(...)
iter(collection) -> iterator
iter(callable, sentinel) -> iterator

Get an iterator from an object.  In the first form, the argument
must
supply its own iterator, or be a sequence.
In the second form, the callable is called until it returns the
sentinel.

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


Re: iter(callable, sentinel)

2006-07-27 Thread Simon Forman
Will McGugan wrote:
> Hi,
>
> I've been using Python for years, but I recently encountered something
> in the docs I wasnt familar with. That is, using two arguements for
> iter(). Could someone elaborate on the docs and maybe show a typical use
> case for it?
>
>
> Thanks,
>
> Will McGugan
>
> --
> work: http://www.kelpiesoft.com
> blog: http://www.willmcgugan.com

D'oh!  You said *elaborate*...  Sorry.  Fredrik Lundh gives a great
example of it's use in this thread:
http://groups.google.ca/group/comp.lang.python/browse_frm/thread/b3ab8141c492bb21/e3f71597917afaa3

Peace,
~Simon

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


Re: iter(callable, sentinel)

2006-07-27 Thread Simon Forman
Will McGugan wrote:
> Hi,
>
> I've been using Python for years, but I recently encountered something
> in the docs I wasnt familar with. That is, using two arguements for
> iter(). Could someone elaborate on the docs and maybe show a typical use
> case for it?
>
>
> Thanks,
>
> Will McGugan
>
> --
> work: http://www.kelpiesoft.com
> blog: http://www.willmcgugan.com

Also, FWIW, GvR said this (parenthetically) about a week ago on the
Python-3000 list: "I also think that the two-argument form
iter(function, sentinel) is not very successful or useful and might be
dropped, but that's a separate issue."

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


Re: help: output arrays into file as column

2006-07-27 Thread Simon Forman
bei wrote:
> Hi,
>
> I am trying to write several arrays into one file, with one arrays in
> one column. Each array (column) is seperated by space.
> ie. a=[1,2,3, 4] b=[5,6,7,8] c=[9,10,11,12]
> 1  5  9
> 2  6  10
> 3  7  11
> 4  8  12
>
> Now I use the function file.writelines(a), file.writelines(b),
> file.writelines(c). And the output is a sequence of strings without
> newlines between a, b ,c . Also each array stays in row other than
> column.
>
> I am a new comer to python.Any idea about this is appreciated!
>
> Bei

Hi Bei,

file.writelines() works with lists of strings, not lists of numbers, so
I'm going to assume that a, b, and c are already lists of strings..

You can use the zip() function to change the lists in the way you want
to:

|>> a=['1','2','3','4']; b=['5','6','7','8']; c=['9','10','11','12']
|>> zip(a, b, c)
[('1', '5', '9'), ('2', '6', '10'), ('3', '7', '11'), ('4', '8', '12')]

now that you have the data for each line, you can combine them with the
string join() method:

|>> data = zip(a, b, c)
|>> for datum in data:
... print ' '.join(datum)
...
1 5 9
2 6 10
3 7 11
4 8 12

You can print to an open file object, so the above loop will do what
you need:

|>> f = open('output.txt', 'w')
|>> for datum in data:
... print >> f, ' '.join(datum)
...
|>> f.close()
|>> print open('output.txt').read()
1 5 9
2 6 10
3 7 11
4 8 12


I hope that helps!

Peace,
~Simon

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


Re: Fastest Way To Loop Through Every Pixel

2006-07-27 Thread Simon Forman
Chaos wrote:
> As my first attempt to loop through every pixel of an image, I used
>
> for thisY in range(0, thisHeight):
> for thisX in range(0, thisWidth):
>   #Actions here for Pixel thisX, thisY
>
> But it takes 450-1000 milliseconds
>
> I want speeds less than 10 milliseconds
>
> I have tried using SWIG, and pypy but they all are unsuccessfull in
> compiling my files.

You could try the PIL package.

>From the docs at
http://www.pythonware.com/library/pil/handbook/image.htm

Image.eval(function, image) => image

Applies the function (which should take one argument) to each pixel in
the given image. If the image has more than one band, the same function
is applied to each band. Note that the function is evaluated once for
each possible pixel value, so you cannot use random components or other
generators.

HTH,
~Simon

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


Re: Fastest Way To Loop Through Every Pixel

2006-07-28 Thread Simon Forman
Chaos wrote:
> Simon Forman wrote:
> > Chaos wrote:
> > > As my first attempt to loop through every pixel of an image, I used
> > >
> > > for thisY in range(0, thisHeight):
> > > for thisX in range(0, thisWidth):
> > >   #Actions here for Pixel thisX, thisY
> > >
> > > But it takes 450-1000 milliseconds
> > >
> > > I want speeds less than 10 milliseconds
> > >
> > > I have tried using SWIG, and pypy but they all are unsuccessfull in
> > > compiling my files.
> >
> > You could try the PIL package.
> >
> > >From the docs at
> > http://www.pythonware.com/library/pil/handbook/image.htm
> >
> > Image.eval(function, image) => image
> >
> > Applies the function (which should take one argument) to each pixel in
> > the given image. If the image has more than one band, the same function
> > is applied to each band. Note that the function is evaluated once for
> > each possible pixel value, so you cannot use random components or other
> > generators.
> >
> > HTH,
> > ~Simon
>
> I have tried PIL. Not only that, but the Image.eval function had no
> success either. I did some tests and I found out that Image.eval only
> called the function a certain number of times either 250, or 255.

It says "the function is evaluated once for each possible pixel value",
so if it's only calling the function 250 or 255 times it's because your
image only has 250 or 255 colors.

Obviously Image.eval() caches the results of your function for each
color/pixel-value and reuses them rather than recomputing them.

I take it that whatever you're doing to those pixels involves more
information than just the pixel values?  What are you doing to the
pixels? That's probably wher you should look to improve the speed of
the loop.

Peace,
~Simon

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


Re: non-blocking PIPE read on Windows

2006-07-28 Thread Simon Forman
placid wrote:
> Hi all,
>
> I have been looking into non-blocking read (readline) operations on
> PIPES on windows XP and there seems to be no way of doing this. Ive
> read that you could use a Thread to read from the pipe, but if you
> still use readline() wouldnt the Thread block too?

Yes it will, but that's ok.  In this case that's what it's for.  While
the thread waits for the readline(), the rest of your program continues
to carry on.

>
> What i need to do is, create a process using subprocess.Popen, where
> the subprocess outputs information on one line (but the info
> continuesly changes and its always on the same line) and read this
> information without blocking, so i can retrieve other data from the
> line i read in then put this in a GUI interface.
>
>
> readline() blocks until the newline character is read, but when i use
> read(X) where X is a number of bytes then it doesnt block(expected
> functionality) but i dont know how many bytes the line will be and its
> not constant so i cant use this too.
> 
> Any ideas of solving this problem?
> 
> 
> Cheers

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


Re: SocketServer and timers

2006-07-28 Thread Simon Forman
alf wrote:
> Hi,
>
> I have one thread app using SocketServer and use server_forever() as a
> main loop. All works fine, but now I need certain timer checking let's
> say every 1 second something and stopping the main loop. So questions are:
>   -how to stop serve_forever
>   -how to implement timers
>
> thx, alf

Do you really need a timer, or do you just want to check something
every second or so?

If the latter, then something like this would do it:

from time import time

INTERVAL = 1.0

RUN = True

while RUN:

# Get a time in the future.
T = time() + INTERVAL

# Serve requests until then.
while time() < T:
server.handle_request()

# Check whatever.
if check():
# Do something, for example, stop running.
RUN = False

HTH,
~Simon

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


Re: locked file

2006-07-28 Thread Simon Forman

Kirt wrote:
> By locked files i mean Outlook PST file while Outlook has it open
>
> Simon Forman wrote:
> > Kirt wrote:
> > > i have a code that backsup file from src to dest.
> > >  Now if some of the files are locked , i need to skip those files..
> > > I was trying to use fctl module but it can be used only in unix i
> > > suppose.
> > >
> > > is there anyother way? i am using windows os.
> >
> > What does locked mean in this case?  No read permissions? In use by
> > another program?
> >
> > I haven't used windows in a long time, and I wasn't aware that one
> > could lock files in it.
> >
> > Peace,
> > ~Simon

So what happens when you try to back that up while outlook is messing
with it?  What are you doing to back it up? Copying?

If all you want to do is skip it...  can't you try to copy it and then
when that doesn't work just skip it?

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


Re: Fastest Way To Loop Through Every Pixel

2006-07-28 Thread Simon Forman
Simon Forman wrote:
> Chaos wrote:
> > Simon Forman wrote:
> > > Chaos wrote:
> > > > As my first attempt to loop through every pixel of an image, I used
> > > >
> > > > for thisY in range(0, thisHeight):
> > > > for thisX in range(0, thisWidth):
> > > >   #Actions here for Pixel thisX, thisY
> > > >
> > > > But it takes 450-1000 milliseconds
> > > >
> > > > I want speeds less than 10 milliseconds
> > > >
> > > > I have tried using SWIG, and pypy but they all are unsuccessfull in
> > > > compiling my files.
> > >
> > > You could try the PIL package.
> > >
> > > >From the docs at
> > > http://www.pythonware.com/library/pil/handbook/image.htm
> > >
> > > Image.eval(function, image) => image
> > >
> > > Applies the function (which should take one argument) to each pixel in
> > > the given image. If the image has more than one band, the same function
> > > is applied to each band. Note that the function is evaluated once for
> > > each possible pixel value, so you cannot use random components or other
> > > generators.
> > >
> > > HTH,
> > > ~Simon
> >
> > I have tried PIL. Not only that, but the Image.eval function had no
> > success either. I did some tests and I found out that Image.eval only
> > called the function a certain number of times either 250, or 255.
>
> It says "the function is evaluated once for each possible pixel value",
> so if it's only calling the function 250 or 255 times it's because your
> image only has 250 or 255 colors.
>
> Obviously Image.eval() caches the results of your function for each
> color/pixel-value and reuses them rather than recomputing them.
>
> I take it that whatever you're doing to those pixels involves more
> information than just the pixel values?  What are you doing to the
> pixels? That's probably wher you should look to improve the speed of
> the loop.
>
> Peace,
> ~Simon

Oops, sorry.  I didn't see where you had already posted the actions
you're taking on the pixels..

He is the code #Actions here

myCol = (0.3 * image.GetRed(thisX, thisY)) + (0.59 *
image.GetGreen(thisX, thisY)) + (0.11 * image.GetBlue(thisX, thisY))
if myCol < darkestCol:
   darkestCol = myCol
   possX = thisX
   possY = thisY

Hmm,  if you're just trying to find the darkest color and one of it's
pixel coordinates after applying your weighting calculation, and you
don't mind if the coordinates are of the first encountered pixel of
that color, then I think you might be able to use Image.eval() after
all.

What format are the images in?


Peace,
~Simon

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


Re: replacing single line of text

2006-07-28 Thread Simon Forman
[EMAIL PROTECTED] wrote:
> I want to be able to replace a single line in a large text file
> (several hundred MB). Using the cookbook's method (below) works but I
> think the replace fxn chokes on such a large chunk of text. For now, I
> simply want to replace the 1st line (CSV header) in the file but I'd
> also like to know a more general solution for any line in the file.
> There's got a be quick and dirty (and cheap) way to do this... any
> help?
>
> Cookbook's method:
> output_file.write(input_file.read().replace(stext, rtext))
>
> Thanks,
> Pythonner

The read() method of a file will "read all data until EOF is reached"
if you don't pass it a size argument.  If your file is "several hundred
MB" and your RAM is not, you may have some trouble with this.

I don't know how well the replace() method works on tenths-of-a-GB
strings.

The file object supports line-by-line reads through both the readline()
and readlines() methods, but be aware that the readlines() method will
also try to read ALL the data into memory at once unless you pass it a
size argument.

You can also iterate through the lines in an open file object like so:

for line in input_file:
# Do something with the line here.

so, if you know that you want to replace a whole actual line, you could
do this like so:

for line in input_file:
if line == stext:
output_file.write(rtext)
else:
output_file.write(line)


Check out the docs on the file object for more info:
http://docs.python.org/lib/bltin-file-objects.html


HTH,
~Simon

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


Re: SocketServer and timers

2006-07-28 Thread Simon Forman
[EMAIL PROTECTED] wrote:
...
>
> That alone does not work. If server.handle_request() blocks,
> you don't get to the check(). You need some kind of timeout
> in handle_request().
>
>
> --
> --Bryan

Ach! You're right.  I didn't consider that handle_request() might
block..

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


Re: trouble understanding super()

2006-07-31 Thread Simon Forman
John Salerno wrote:
> Here's some code from Python in a Nutshell. The comments are lines from
> a previous example that the calls to super replace in the new example:
>
> class A(object):
>  def met(self):
>  print 'A.met'
>
> class B(A):
>  def met(self):
>  print 'B.met'
>  # A.met(self)
>  super(B, self).met()
>
> class C(A):
>  def met(self):
>  print 'C.met'
>  # A.met(self)
>  super(C, self).met()
>
> class D(B, C):
>  def met(self):
>  print 'D.met'
>  # B.met()
>  # C.met()
>  super(D, self).met()
>
> Then you call D().met()
>
> Now, I understand that the commented code would cause A.met to be called
> twice. But why does the second version (with super) not also do this? I
> guess my problem lies in not understanding exactly what the super
> function returns.
>
> super(D, self).met() seems like it would return something that has to do
> with both B and C, which each in turn return a superobject having to do
> with A, so why isn't A.met called twice still?
>
> Thanks!


Basically super(class_, self).method  looks in self's mro (it's list of
base classes) for class class_, and then starts searching *after* class
class_ for the next class that implements the method.

In this case the object's (instance of D) mro will be (D, B, C, A,
object), so as super gets called in each class, it looks in that list
(tuple, whatever) for the class following it (actually the next class
following it that implements the method).

Since no class appears in that list more than once, each class's
implementation of the method will only be called once.


HTH,
~Simon


Also, if you haven't already, read:
http://www.python.org/download/releases/2.2.3/descrintro/#cooperation

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


Re: FTP (ftplib) output capture

2006-07-31 Thread Simon Forman
ChaosKCW wrote:
> Hi
>
> Has anyone caputerd the output from the std ftp lib? It seems a bit
> annoying that everything is printed to stdout. It means incorporating
> this into any real program is a problem. It would have been much better
> if they used the std logging module and hooked up a console logger for
> the feault ftp application. Alas it isnt that way.
>
> Capturing stdout like this??? :
>
> import sys
>
> sys.stdout = open('bb', 'w)
>
> But I want to re-route it to the logging module not a file , so do I
> need to write a steam object?
>
> Thanks,

ftplib pre-dates the standard logging system by a bit.

I think ftplib only prints stuff if you set its (the FTP class
instances') debug level to greater than 0.

If you really want to replace sys.stdout with something that passes the
data to a logger, then something like the following (untested) class
should do it:

class FileLog:
def __init__(self, log):
self.log = log
def write(self, data):
self.log.debug(data)

The best solution might be to subclass or rewrite FTP to really do what
you want.  I believe one of the goals of Python-3000 is better
integration of the standard library with the new-ish standard logging
system, so if you do a good job send it in.  ;-)

Peace,
~Simon

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


Re: import error

2006-07-31 Thread Simon Forman
cheeky wrote:
> Hi, all.
>
> I now really like to program with Python, even though I'm a newbie. I
> have difficulty in solving the following problem.
>
> $ python
> Traceback (most recent call last):
>   File "x.py", line 6, in ?
> import calendar, time
>   File "time.py", line 5, in ?
> now = time.time()
> TypeError: 'module' object is not callable
>
> I now see the above error message. The source code is the following.
>
> $ cat -n x.py
>  1  #!/usr/bin/python
>  2
>  3  import traceback
>  4
>  5  from datetime import date
>  6  import calendar, time
>
> I don't have the line now = time.time() which encountered an error in
> the previous step. I removed that line but I still see the error.
>
> Someone says I should reload time module. However, I can't even import
> time module. That's why I can't even try to reload time module name.
>
> What's wrong with me? I'm studying on Ubuntu Linux. On Window IDLE
> doesn't seem to have this problem. I saw someone saying it could be
> happening when running independent script. I don't know what is right.
>
> I really would like to know what is going on my system and how to clear
> the past information. Thank you.

It looks like you might have a file named 'time.py' in the same
directory as your 'x.py'.  If so, move it away (or rename it) and try
again.

HTH,
~Simon

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


Re: running an app as user "foo"

2006-07-31 Thread Simon Forman
bruce wrote:
> hi.
>
> within python, what's the best way to automatically spawn an app as a given
> user/group.
>
> i'm testing an app, and i'm going to need to assign the app to a given
> user/group, as well as assign it certain access rights/modes (rwx) i then
> want to copy the test app to a given dir, and then spawn a process to run
> the app..
>
> thanks

Look in the os module docs at the set*id() functions
http://docs.python.org/lib/os-procinfo.html

Then when you spawn the app using subprocess.Popen() you can pass a
function as the preexec_fn argument that changes the user and group ids
of the child "just before the child is executed"
http://docs.python.org/lib/node235.html

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


Re: Pickle vs XML for file I/O

2006-07-31 Thread Simon Forman
crystalattice wrote:
> I'm creating an RPG for experience and practice.  I've finished a
> character creation module and I'm trying to figure out how to get the
> file I/O to work.
>
> I've read through the python newsgroup and it appears that shelve
> probably isn't the best option for various reasons.  This lead me to
> try messing w/ pickle, but I can't figure out how to use it with
> classes.  I've found many examples of using pickle w/ non-OOP code but
> nothing that shows how to use it w/ classes, subclasses, etc.  I've
> read the documentation but it doesn't explain it well enough for me.
>
> Then I started thinking perhaps I'm going about it wrong.  My current
> thought is to save an instance of each character so all the info (name,
> skills, hit points, etc.) is stored in one place.  But looking at
> OpenRPG made me think that perhaps using XML to store the information
> would be better.  Each character could have a separate XML file, though
> I don't know how it would work if many NPC's are required.
>
> I guess my question is, what are the benifits of getting pickle to work
> w/ my classes vs. converting all the character data into XML and just
> writing that to a file?  Since most of the data would need to be
> modified often, e.g. hit points, is one storage format better than the
> other?  Can you even modify data if it's been pickled w/o having to
> unpickle it, change the data, then repickle it?

Um, there's nothing tricky to using pickle with classes:

|>> import pickle
|>> class foo: pass
|>> f = foo()
|>> pstr = pickle.dumps(f)
|>> pstr
'(i__main__\nfoo\np0\n(dp1\nb.'
|>> newf = pickle.loads(pstr)
|>> newf
<__main__.foo instance at 0xb664690c>

Pickle is simple and should work "out-of-the-box".  I wouldn't mess
with XML until I was sure I needed it for something.

What kind of trouble were you having with pickle?

Peace,
~Simon

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


Re: BCD List to HEX List

2006-07-31 Thread Simon Forman
Philippe, please!  The suspense is killing me.  What's the cpu!?

For the love of God,  what's the CPU?

I-can't-take-it-anymore-it's-such-a-simple-question-ingly yours,
~Simon

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


Re: Help with arrays of strings

2006-07-31 Thread Simon Forman
Jon Smirl wrote:
> I only have a passing acquaintance with Python and I need to modify some
> existing code. This code is going to get called with 10GB of data so it
> needs to be fairly fast.
>
> http://cvs2svn.tigris.org/ is code for converting a CVS repository to
> Subversion. I'm working on changing it to convert from CVS to git.
>
> The existing Python RCS parser provides me with the CVS deltas as
> strings.I need to get these deltas into an array of lines so that I can
> apply the diff commands that add/delete lines (like 10 d20, etc). What is
> the most most efficient way to do this? The data structure needs to be
> able to apply the diffs efficently too.
>
> The strings have embedded @'s doubled as an escape sequence, is there an
> efficient way to convert these back to single @'s?
>
> After each diff is applied I need to convert the array of lines back into
> a string, generate a sha-1 over it and then compress it with zlib and
> finally write it to disk.
>
> The 10GB of data is Mozilla CVS when fully expanded.
>
> Thanks for any tips on how to do this.
>
> Jon Smirl
> [EMAIL PROTECTED]

Splitting a string into a list (array) of lines is easy enough,  if you
want to discard the line endings,

lines = s.splitlines()

or, if you want to keep them,

lines = s.splitlines(True)

replacing substrings in a string is also easy,

s = s.replace('@@', '@')

For efficiency, you'll probably want to do the replacement first, then
split:

lines = s.replace('@@', '@').splitlines()


Once you've got your list of lines, python's awesome list manipulation
should makes applying diffs very easy.  For instance, to replace lines
3 to 7 (starting at zero) you could assign a list (containing the
replacement lines) to a "slice" of the list of lines:

lines[3:8] = replacement_lines

Where replacement_lines is a list containing the replacement lines.
There's a lot more to this, read up on python's lists.


To convert the list back into one string use the join() method; if you
kept the line endings,

s = "".join(lines)

or if you threw them away,

s = "\n".join(lines)

Python has standard modules for sha-1 digest, sha, and zlib
compression, zlib.  See http://docs.python.org/lib/lib.html

HTH, enjoy,
~Simon

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


Re: cleaner way to write this try/except statement?

2006-08-01 Thread Simon Forman
John Salerno wrote:
> John Salerno wrote:
> > The code to look at is the try statement in the NumbersValidator class,
> > just a few lines down. Is this a clean way to write it? i.e. is it okay
> > to have all those return statements? Is this a good use of try? Etc.
>
> I cleaned it up a little and did this, but of course this doesn't work.
> Is this the wrong way to create a function (not method) within a class?
>
>
>
> def Validate(self, parent):
>  text_ctrl = self.GetWindow()
>  text = text_ctrl.GetValue()
>
>  try:
>  if not text or int(text) <= 0:
>  error_message()
>  return False
>  else:
>  return True
>  except ValueError:
>  error_message()
>  return False
>
>  @staticmethod
>  def error_message():
>  wx.MessageBox('Enter a valid time.', 'Invalid time entered',
>wx.OK | wx.ICON_ERROR)

Your indentation looks off.  Your error_message() function should be at
the same indentation as the rest of the body of the Validate() method
(i.e. same as the try statement, "text_ctrl = ..." statements.)

If that's not an artifact of posting it here then you'll need to
correct that.

Also, there's no need to declare the function a staticmethod, since it
isn't.

Other than that it looks ok to me, but I might have missed something.

Peace,
~Simon

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


Re: cleaner way to write this try/except statement?

2006-08-01 Thread Simon Forman
Simon Forman wrote:
> John Salerno wrote:
> > John Salerno wrote:
> > > The code to look at is the try statement in the NumbersValidator class,
> > > just a few lines down. Is this a clean way to write it? i.e. is it okay
> > > to have all those return statements? Is this a good use of try? Etc.
> >
> > I cleaned it up a little and did this, but of course this doesn't work.
> > Is this the wrong way to create a function (not method) within a class?
> >
> >
> >
> > def Validate(self, parent):
> >  text_ctrl = self.GetWindow()
> >  text = text_ctrl.GetValue()
> >
> >  try:
> >  if not text or int(text) <= 0:
> >  error_message()
> >  return False
> >  else:
> >  return True
> >  except ValueError:
> >  error_message()
> >  return False
> >
> >  @staticmethod
> >  def error_message():
> >  wx.MessageBox('Enter a valid time.', 'Invalid time entered',
> >wx.OK | wx.ICON_ERROR)
>
> Your indentation looks off.  Your error_message() function should be at
> the same indentation as the rest of the body of the Validate() method
> (i.e. same as the try statement, "text_ctrl = ..." statements.)
>
> If that's not an artifact of posting it here then you'll need to
> correct that.
>
> Also, there's no need to declare the function a staticmethod, since it
> isn't.
>
> Other than that it looks ok to me, but I might have missed something.


Maybe I did miss something:  Are you actually trying to make a static
method?  Say, to be able to call it elsewhere than just the Validate()
method?

In that case, your indentation is still (apparently) off, but the
error_message() method (a staticmethod is still a method I think, but I
could be wrong) should be at the same indentation level as any other
method, and you have to prepend the instance object ('self.' if you're
calling it from inside another method) to it when you call it.

class A(object):
def meth(self, arg):
self.func(arg)

@staticmethod
def func(arg):
print arg

a = A()

a.func("hi")
a.meth("world.")


# prints
hi
world.

HTH,
~Simon

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


Re: cleaner way to write this try/except statement?

2006-08-01 Thread Simon Forman
John Salerno wrote:
> John Salerno wrote:
> > The code to look at is the try statement in the NumbersValidator class,
> > just a few lines down. Is this a clean way to write it? i.e. is it okay
> > to have all those return statements? Is this a good use of try? Etc.
>
> I cleaned it up a little and did this, but of course this doesn't work.
> Is this the wrong way to create a function (not method) within a class?
>
>
>
> def Validate(self, parent):
>  text_ctrl = self.GetWindow()
>  text = text_ctrl.GetValue()
>
>  try:
>  if not text or int(text) <= 0:
>  error_message()
>  return False
>  else:
>  return True
>  except ValueError:
>  error_message()
>  return False
>
>  @staticmethod
>  def error_message():
>  wx.MessageBox('Enter a valid time.', 'Invalid time entered',
>wx.OK | wx.ICON_ERROR)

If you're not really interested in methods, static or otherwise, and
actually just want a cleaner Validate() method, I would code it as
follows (note that calling int() on an empty string will raise
ValueError):


def Validate(self, parent):
text = self.GetWindow().GetValue()

try:
T = int(text)
except ValueError:
result = False
else:
result = T > 0

if not result:
wx.MessageBox('Enter a valid time.', 'Invalid time entered',
   wx.OK | wx.ICON_ERROR)

return result

Peace,
~Simon

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


Re: cleaner way to write this try/except statement?

2006-08-01 Thread Simon Forman
Boris Borcic wrote:
> John Salerno wrote:
> > The code to look at is the try statement in the NumbersValidator class,
> > just a few lines down. Is this a clean way to write it? i.e. is it okay
> > to have all those return statements? Is this a good use of try? Etc.
> >
> > Thanks.
> >
> > 
> >
> > import wx
> >
> >
> > class NumbersValidator(wx.PyValidator):
> >
> > def __init__(self):
> > wx.PyValidator.__init__(self)
> >
> > def Clone(self):
> > return NumbersValidator()
> >
> > def Validate(self, parent):
> > text_ctrl = self.GetWindow()
> > text = text_ctrl.GetValue()
> >
> > try:
> > if not text or int(text) <= 0:
> > wx.MessageBox('Enter a valid time.', 'Invalid time
> >   entered', wx.OK | wx.ICON_ERROR)
> > return False
> > else:
> > return True
> > except ValueError, error:
> > wx.MessageBox('Enter a valid time.', 'Invalid time entered',
> >   wx.OK | wx.ICON_ERROR)
> > return False
>
> well, assuming you are unsatisfied with the above, you could try to assert the
> validation condition and catch together all failures, eg :
>
> def Validate(self, parent):
>  text_ctrl = self.GetWindow()
>  text = text_ctrl.GetValue()
>  try :
>  assert int(text)>0
>  return True
>  except (ValueError,AssertionError) :
>  wx.MessageBox('Enter a valid time.', 'Invalid time entered',
>wx.OK | wx.ICON_ERROR)
>  return False
>
> hth, BB

Assertion statements "go away" when you run python with the '-O' or
'-OO' options.  They're only meant for debugging and shouldn't be used
as part of your actual program logic.

You run the risk of introducing hard-to-find bugs if you use them like
this and somebody, somewhere, sometime runs your code in "optimized"
mode.

Peace,
~Simon

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


Re: Get age of a file/dir

2006-08-01 Thread Simon Forman
url81-1 wrote:
> Actually this has  nothing to do with datetime.datetime -- he's asking
> how to find the created time of the directory.
>
> Python has a builtin module called "stat" (afer sys/stat.h) which
> includes ST_ATIME, ST_MTIME, ST_CTIME members which are times accessed,
> modified, and created, respectively.
>
> Best,
> Earle Ady
>
> Jim wrote:
> > Carl J. Van Arsdall wrote:
> > > I've been looking around the OS module and I haven't found anything
> > > useful yet.  Does anyone know how to get the age of a file or directory
> > > in days?  I'm using unix and don't seem to find anything that will help
> > > me.  The only function that comes close so far is
> > >
> > > os.path.getctime(path)
> > >
> > >
> > > However this only gets creation time on Windows, on Unix it gets the the
> > > time of the last change.  Any ideas?
> > >
> > > Thanks!
> > >
> > > -carl
> > >
> > > --
> > >
> > > Carl J. Van Arsdall
> > > [EMAIL PROTECTED]
> > > Build and Release
> > > MontaVista Software
> >
> > Hi,
> > You should check out the datetime module.  And convert dates to an
> > ordinal number.
> >  today = datetime.date.today().toordinal()
> >  age = today - datetime.date(year, month, day).toordinal()
> > Jim

No, the st_ctime member isn't the creation time on *nix, from the os
module docs: "st_ctime (platform dependent; time of most recent
metadata change on Unix, or the time of creation on Windows)"

I hope somebody does post a solution to this, as I'd like to know how
to get the creation time of a file on linux, et. al.

It may be impossible:
http://www.faqs.org/faqs/unix-faq/faq/part3/section-1.html

Peace,
~Simon

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


Re: Zipping files/zipfile module

2006-08-02 Thread Simon Forman
Brian Beck wrote:
> OriginalBrownster wrote:
> > I want to zip all the files within a directory called "temp"
> > and have the zip archive saved in a directory with temp called ziptemp
> >
> > I was trying to read up on how to use the zipfile module python
> > provides, but I cannot seem to find adequate documentation on function
> > itself.
> >
> > Perhaps someone could help me in this task?
>
> Hello,
>
> This isn't completely tested, but perhaps it will help you get started:
>
> from os import listdir, mkdir
> from os.path import join, basename, isfile
> from zipfile import ZipFile
>
> def zip_dir(path, output_path, include_hidden=True):
> files = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
> try:
> mkdir(output_path)
> except OSError, e:
> if e.errno == 17: # Path exists
> pass
> zip_file = ZipFile(join(output_path, 'temp.zip'), 'w')
> for f in files:
> if basename(f).startswith('.') and not include_hidden:
> continue
> print "Adding %s to archive..." % (f,)
> zip_file.write(f)
> zip_file.close()
>
> Use like:
> zip_dir('temp', 'temp/ziptemp')
>
> Note that if you want to add the entire contents of a directory
> (subdirectories, recursively), you should consider using os.walk or
> something similar. This will only add the file contents of the directory.
> I'm not sure if the zipfile module provides any nice ways to write
> directories to the archive, but I'm assuming it just involves writing an
> arcname with a '/' in it (see help(zipfile.ZipFile)).
>
> --
> Brian Beck
> Adventurer of the First Order

To avoid calling os.path.join() twice for each filename when you build
the list of files you could write the list comprehension like so:

[n for n in (join(path, f) for f in listdir(path)) if isfile(n)]

Also, you should use the "symbolic" errors from the errno module rather
than hard-coding a constant:

from errno import EEXIST
...
if e.errno == EEXIST: # Path exists

Finally, if your using a single arg with a string interpolation and you
know it'll never be a tuple you needn't wrap it in a tuple:

print "Adding %s to archive..." % f

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


Re: Is there an obvious way to do this in python?

2006-08-02 Thread Simon Forman
H J van Rooyen wrote:
> Hi,
>
> I want to write a small system that is transaction based.
>
> I want to split the GUI front end data entry away from the file handling and
> record keeping.
>
> Now it seems almost trivially easy using the sockets module to communicate
> between machines on the same LAN, so that I want to do the record keeping on 
> one
> machine.
>
> I want to keep the "server" machine as simple as possible - just doing record
> keeping on a stimulus response basis - I would prefer it to do one thing at a
> time to completion because this style of operation, though limited in
> performance, keeps a lot of hassles out of life - a transaction has either
> completed, or it has not - recovery scenarios are relatively easy...
>
> Up to this point, I don't have a problem - my toy system can create a dummy
> transaction, and I can echo it from the "server" machine, with more than one
> "user" machine running - so I think it is feasible to have several tens of 
> "data
> entry terminal" systems running, served by one not very strong machine.
>
> Now what I would really like to do is to differentiate between the 'User"
> machines, so that some can do a full range of transactions, and others a 
> limited
> range.
>
> And I would like to make this flexible, so that it becomes easy to introduce 
> new
> transactions, without having to run around updating the code in all the user
> machines, with the concomitant version number hassles.
>
> And I would like to do the whole thing in python - so my question is this - is
> it possible to do the equivalent of dynamic linking? - i.e. if I keep a list 
> of
> what a user is allowed to do - can I somehow send him just the bits he needs 
> to
> do the job, without having to change the static code on his machine? - it 
> seems
> to me that the eval() thingy could possibly do this for me, by sending it data
> that makes it do import statements followed by calls to whatever... - will 
> this
> work, or is there a better way?
>
> Or has all this been done already? - and no I don't want a web server and php
> and browsers and Java and html or xml... - I want to write something that 
> works
> simply and reliably - its just short message accounting type data...
>
> - Hendrik

Don't reinvent the wheel.  Use a database...

You probably don't want to hear this, but what you just described is a
GUI client front-end with a database backend.  The time it takes to
download, install, and learn to use, say, postgres will be similar to
the time you'd spend implementing what you've described above, but with
at least 10 to 100 times the payoff.


As for updating the client on the fly, one strategy would be to keep
the "dynamic" code in it's own module and have the clients reload()
that module when you upload a new version of it to the client machines.

Peace,
~Simon

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


Re: Thread Question

2006-08-02 Thread Simon Forman
Ritesh Raj Sarraf wrote:
> Hi,
>
> I have this following situation:
>
> #INFO: Thread Support
> # Will require more design thoughts
> from Queue import Queue
> from threading import Thread, currentThread
>
> NUMTHREADS = variables.options.num_of_threads
>
> def run(request, response, func=download_from_web):
> '''Get items from the request Queue, process them
> with func(), put the results along with the
> Thread's name into the response Queue.
>
> Stop running once an item is None.'''
>
> name = currentThread().getName()
> while 1:
> item = request.get()
> (sUrl, sFile, download_size, checksum) = stripper(item)
> if item is None:
> break
> response.put((name, func(sUrl, sFile, sSourceDir, None)))
>

One thing about this code: you should check whether item is None
*before* passing it to stripper().  Even if stripper() can handle None
as input there's no reason to make it do so.

> My download_from_web() returns True or False depending upon whether the 
> download
> was successful or failed. How can I check that in the above code ?

Well, you'd probably want to check that *outside* the above run()
function.  The True/False return values will queue up in the
responseQueue, where you can access them with responseQueue.get().
Since these can be in a different order than your requests you're going
to want to "tag" them with the sUrl and sFile.

response.put((name, sUrl, sFile, func(sUrl, sFile, sSourceDir, None)))

That way your response handing code can tell which downloads succeeded
and which failed.

Of course, you could have the run() function check the return value
itself and retry the download ane or a few times.

>
> One other question I had,
> If my user passes the --zip option, download_from_web() internally (when the
> download is successful) zips the downloaded data to a zip file. Since in case
> of threading there'll be multiple threads, and say if one of the thread
> completes 2 seconds before others and is doing the zipping work:
> What will the other thread, at that moment do, if it completes while the
> previous thread is doing the zipping work ?

The other threads will just take the next request from the Queue and
process it.  They won't "care" what the one thread is doing,
downloading, zipping, whatever.


Peace,
~Simon

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


Re: What is the best way to print the usage string ?

2006-08-03 Thread Simon Forman
Leonel Gayard wrote:
> Hi all,
>
> I had to write a small script, and I did it in python instead of
> shell-script. My script takes some arguments from the command line,
> like this.
>
> import sys
> args = sys.argv[1:]
> if args == []:
>   print """Concat: concatenates the arguments with a colon (:) between 
> them
> Usage: concat arg1 [arg2...]
> Example: concat a b c prints \"a.jar:b.jar:c/\
>   sys.exit(1)
> print reduce(lambda x, y: x + ':' + y, sys.argv[1:])
>
> Notice that the string messes the indentation in my script. The
> indentation is correct, and if the script is invoked without
> arguments, the usage string is printed correctly.
>
> Now, how can I achieve the same result while keeping a clean
> indentation ? How is this done in python world ? In C, I would do
> this:
>
> ;; This buffer is for notes you don't want to save, and for Lisp evaluation.
> ;; If you want to create a file, visit that file with C-x C-f,
> ;; then enter the text in that file's own buffer.
>
> if (argc < N) {
> printf("Usage: blah blah blah\n"
> "Some more lines in the usage text\n"
> "Some more lines here too\n");
> exit(1);
> }
>
> The whitespace at the beginning of the string helps me keep the
> indentation clean, and the construct "a" "b" is syntactic sugar that
> allows me to create a large string without concatenating them at
> runtime.
>
> How can I get this in Python ?
>
> []'s
> Leonel

Python also concatenates adjacent strings, but the "real" newlines
between your strings will need to be escaped (otherwise, because the
newlines are statement separators, you will have one print statement
followed by string literals with the wrong indentation.)

print "Usage: blah blah blah\n" \
  "Some more lines in the usage text\n" \
  "Some more lines here too."

(Note that the final string literal newline is not needed since print
will add one of it's own.)

HTH,
~Simon

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


Re: Hiding Console Output

2006-08-03 Thread Simon Forman
Kkaa wrote:
> This seems like the right thing to do, but it runs the program in the
> background, and I need my program to wait until the x.exe has finished.
>  I tried using this code:
>
> p =
> subprocess.Popen("x.exe",shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE,
> stderr=subprocess.PIPE)
> sts = os.waitpid(p.pid, 0)
>
> But I get the error: "No child process".  Any ideas?

Read the docs.

Here's a link to the docs for the subprocess.Popen object.  Note the
second method?  http://docs.python.org/lib/node239.html

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


Re: OS independent files

2006-08-03 Thread Simon Forman
crystalattice wrote:
> I'm sure this has been addressed before but it's difficult to search
> through several thousand postings for exactly what I need, so I
> apologize if this a redundant question.

Google groups has a very good search.

> I've figured out how to use os.path.join to make a file or directory
> location prior to pickling something to it.  But I have a question
> about it.  In Windows I can make a file with this:
>
> os.path.join("C:", "myfiles", "myfile.dat")
>
> If I want to make sure the file/directory is made in a user's home
> directory (e.g. /home/users/path/to/file) but also compatible w/
> Windows, how would I rewrite this (if required)?

Try os.path.expanduser('~') (in
http://docs.python.org/lib/module-os.path.html) or you could just look
up the HOME environment variable in os.environ, but I don't know if
windows sets this correctly in all cases.

(os.environ is documented in
http://docs.python.org/lib/os-procinfo.html)

Peace,
~Simon

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


Re: help - iter & dict

2006-08-03 Thread Simon Forman
[EMAIL PROTECTED] wrote:
> Dear Python people,
>
> im a newbie to python and here...so hello!

Hi Ali, and welcome.

> Im trying to iterate through values in a dictionary so i can find the
> closest value and then extract the key for that valuewhat ive done so far:
>
> def pcloop(dictionary, exvalue):
> z = dictionary.itervalues()
> y = z - exvalue
> v = (y*y)**1/2
> if v < 0.001:
> u = dictionary.get[z]
> return u
>
>
> ive been working off a couple of books and this is the best i can get it in
> short time. I was trying to define a function (its my first!) so that i
> could apply to several 'dictionary's and 'exvalue's. The best ive been able
> to come up with is iterating over the dictionary values, subtracting the
> exvalue, squaring then root squaring to render positive and applying an
> error (0.001) which is not the ideal solution i want. Is there any easy way
> to iterate through dictionary values and return the key for the minimum. Or
> can someone tell me where im going wrong with this def & loop.
>
> regards all
>
> Ali

You're doing many interesting things wrong here.  :-)  I'm going to
take them slightly out of order.

First, very ingenious way to get the absolute value of a number, but
there are a few issues with this that you should know about.

For instance, just as multiplication and division take precedence over
addition and subtraction, the "power" operator ** take precedence over
division,  so what you're really doing above is

((y*y)**1)/2

rather than

(y*y)**(1/2)

However, the above still wouldn't work correctly because of the way
python handles integer division.  1/2 == 0 in python, try it at the
interactive prompt and you'll see.

So, you'd have to change at least one of the division's operands to
float to get the proper result you desire.

(y*y)**(1./2)

Except that you should actually use the (built in) abs() function,

v = abs(y)

:-)

Next, the itervalues() method of dicts does not return a number, but
rather a "dictionary-valueiterator" object, as you can see from this:

|>> d = {}
|>> z = d.itervalues()
|>> z


In order to get values out of it you need to iterate over it like so:

for z in d.itervalues():
# Do something with z's here...

You could also get both the keys and values at the same time by
iterating like so:

for k, z in d.iteritems():
# Do something with k's and z's here...

(I'll come back to that.)

Now, a little further down in your function, you seem to want to use
the z value with the get() method of the dict to retrieve the key.
This won't work.

First, you're not *calling* the get method (that uses ()'s) you're
*subscripting* it (that uses []'s.)  If your code got this far, it
would break here.

|>> d = {}
|>> d.get

|>> d.get[23]
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: unsubscriptable object

So you want to use ()'s, not []'s.

Further, get() works with *keys*, not *values*.  If you call it with a
value, it won't work, instead it will return None.  Unless, of course,
you *have* a key with the same value as your value, in which case it
will return the value associated with the key, but NOT the key
associated with the value (that you passed to get().)

In fact, there is no easy way to get the key from a dict given a value.
Dicts work the other way around.  (You basically have to iterate
through the values *and* keys, one pair at a time, testing each value
as you go.  This is very slow, compared to getting the value given a
key.)

value = d[key]

This is extremely fast.  It's pretty much the whole point of a
dictionary that this is very fast.

So, when you build the dict that you pass in to your function, you
might want to build it the other way round, i.e. make the keys the
values and the values keys.  However, you might not and here's why:

Dicts can only have one key of any given value of key.  Look:

|>> d = {1: 'a', 1: 'b'}
|>> d
{1: 'b'}
|>> d = {'a': 1, 'b': 1}
|>> d
{'a': 1, 'b': 1}

So, if you're running a mathematical function on a range of input (x's)
and storing the results (y's) as values in a dict, then you *do* want
the x's to be the keys and the y's to be the values.

I'm guessing that's what you're doing, and if so, you're doing it
correctly.

Let me address one last problem in your code above and then I'll show
you a neat way to return the key whose associated value is closest to
some value exvalue.


In this part of your code,

> if v < 0.001:
> u = dictionary.get[z]
> return u

the return statement should be at the same indentation level as the
assignment statement:

if v < 0.001:
u = dictionary.get[z]  # <- note, this won't work
return u

or even just

if v < 0.001:
return dictionary.get[z]  # <- note, this won't work

The way you have it now, the return statement would execute immediately
after the if statement, regardless of whether v < 0.001 or not.  Not
too bad if the very first value was close enough 

Re: current recursion level

2006-08-03 Thread Simon Forman
David Bear wrote:
> Is there an easy way to get the current level of recursion? I don't mean
> sys.getrecursionlimit.  I want to know during a live run of a script how
> many times the functions has recursed -- curses, I don't know how to say it
> better.
>
> --
> David Bear
> -- let me buy your intellectual property, I want to own your thoughts --

This might help,

import sys

def getStackDepth():
'''Return the current call stack depth.'''
n = 1
while True:
try:
sys._getframe(n)
except ValueError:
return n - 1
n += 1


def f(n=3):
'''Demo getStackDepth()'''
print 'hi!', n
if n:
return f(n - 1)
else:
return getStackDepth()

This is an evil hack. Never use it.

If you really need to know how many times your function has recursed
pass a counter down the "stack" and increment it every time you
recursively call your function.  This will also give you an accurate
count when calling a different function or functions that then
recursively call back into your function, but you'll have to pass your
counter around or *shudder* use a global variable.

def g(n=0):
'''n is the number of recursions...'''
if n < 4:
return g(n + 1)
else:
return n


Peace,
~Simon

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


Re: Problem reading/writing files

2006-08-03 Thread Simon Forman
[EMAIL PROTECTED] wrote:
> This is a bit of a peculiar problem. First off, this relates to Python
> Challenge #12, so if you are attempting those and have yet to finish
> #12, as there are potential spoilers here.
>
> I have five different image files shuffled up in one big binary file.
> In order to view them I have to "unshuffle" the data, which means
> moving bytes around. Currently my approach is to read the data from the
> original, unshuffle as necessary, and then write to 5 different files
> (2 .jpgs, 2 .pngs and 1 .gif).
>
> The problem is with the read() method. If I read a byte valued as 0x00
> (in hexadecimal), the read method returns a character with the value
> 0x20.

No.  It doesn't.

Ok, maybe it does, but I doubt this so severely that, without even
checking, I'll bet you a [virtual] beer it doesn't.  :-)

Are you opening the file in binary mode?


Ok, I did check, it doesn't.

|>> s = '\0'
|>> len(s)
1
|>> print s
\x00
|>> f = open('noway', 'wb')
|>> f.write(s)
|>> f.close()

Checking that the file is a length 1 null byte:

$ hexdump noway
000 
001
$ ls -l noway
-rw-r--r-- 1 sforman sforman 1 2006-08-03 23:40 noway

Now let's read it and see...

|>> f = open('noway', 'rb')
|>> s = f.read()
|>> f.close()
|>> len(s)
1
|>> print s
\x00

The problem is not with the read() method.  Or, if it is, something
very very weird is going on.

If you can do the above and not get the same results I'd be interested
to know what file data you have, what OS you're using.

Peace,
~Simon

(Think about this:  More people than you have tried the challenge, if
this happened to them they'd have mentioned it too, and it would have
fixed or at least addressed by now. Maybe.)

(Hmm, or maybe this is *part* of the challenge?)

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


Re: Need help building boost python on mac os x.

2006-08-04 Thread Simon Forman

KraftDiner wrote:
> Could someone point me to step by step instructions on building boost
> python on mac os x?
> I have bjam running.. I have the boost source... but the tests are
> failing..
> Probably something to do with environement variables...
> Anyone with time?

You might also ask on the boost python list:
http://www.boost.org/more/mailing_lists.htm

HTH,
~Simon

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


Re: Backup GMAIL Messages with Python

2006-08-05 Thread Simon Forman
Gregory PiƱero wrote:
> I was wondering what methods you experts would reccomend for this task?
>
> Here are the options I have come up with so far:
>
> 1. Build something with the poblib library
> (http://docs.python.org/lib/module-poplib.html)
> --Any pointers on doing this?  How to I get poplib to save messages in
> a standard format I can
> later import into Thunderbird, Outlook, etc?  (mbox?)

I don't do much with email, but there's an example of accessing gmail
via poplib here:
http://groups.google.ca/group/comp.lang.python/msg/a12263a870f5f236

And of course there's the 'email' standard library module:
http://docs.python.org/lib/module-email.html

HTH,
~Simon

Out of curiosity, why do you want to _backup_ a gmail account?  (I use
my gmail account to backup files and documents I never want to lose.)
I could think of some reasons, but I'm wondering what yours are. : )

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


Re: testing array of logicals

2006-08-05 Thread Simon Forman
Janto Dreijer wrote:
> Janto Dreijer wrote:
> > John Henry wrote:
> > > Simon Forman wrote:
> > > > >
> > > > > False not in logflags
> > > > >
> > > >
> > > > Or, if your values aren't already bools
> > > >
> > > > False not in (bool(n) for n in logflags)
> > >
> > > Very intriguing use of "not in"...
> >
> > Is there a reason why you didn't write
> >  True in (bool(n) for n in logflags)
> 
>  doh! Never mind.

*grin*

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


Re: Thread Question

2006-08-05 Thread Simon Forman
Ritesh Raj Sarraf wrote:
> Bryan Olson on Saturday 05 Aug 2006 23:56 wrote:
>
> > You don't want "ziplock = threading.Lock()" in the body of
> > the function. It creates a new and different lock on every
> > execution. Your threads are all acquiring different locks.
> > To coordinate your threads, they need to be using the same
> > lock.
> >
> > Try moving "ziplock = threading.Lock()" out of the function, so
> > your code might read, in part:
> >
> >
> > ziplock = threading.Lock()
> >
> > def run(request, response, func=copy_first_match):
> > # And so on...
>
> Thanks. That did it. :-)
>
> Ritesh

Another thing you might want to consider would be to split your
download and zipping code into separate functions then create one more
thread to do all the zipping.  That way your downloading threads would
never be waiting around for each other to zip.


Just a thought.  :)
~Simon

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


Re: subprocesses and deadlocks

2006-08-06 Thread Simon Forman
[EMAIL PROTECTED] wrote:
> Hi,
>
> there are many ways of solving the problem of finite buffer sizes when
> talking to a subprocess. I'd usually suggest using select() but today I
> was looking for a more readable/understandable way of doing this. Back
> in 1997 Guido himself posted a very nice solution, write your input to
> a temporary file and then read that from your new process. His posting
> can be found here:
> http://groups.google.com/group/comp.lang.python/tree/browse_frm/thread/2b31d990a8613d93/17d3dea9089aad00?rnum=1&q=subprocess+deadlock&_done=%2Fgroup%2Fcomp.lang.python%2Fbrowse_frm%2Fthread%2F2b31d990a8613d93%2F63b0a786d87ba23b%3Flnk%3Dgst%26q%3Dsubprocess+deadlock%26rnum%3D6%26#doc_63b0a786d87ba23b
>
> Being a bit puzzled over this usage of tempfile I read its
> documentation and as expected it says:
>
> [...] The file is created using mkstemp. It will be destroyed as soon
> as it is closed (including an implicit close when the object is garbage
> collected). [...] your code should not rely on a temporary file created
> using this function having or not having a visible name in the file
> system.
>
> so how was Guido planning to get the contents of the file after closing
> it? Should we do a tf.flush() instead of the close to ensure everything
> is written, then read from it, using subprocess.Popen(,stdin=tf,..)
> and only close it afterwards?
>
> Is it correct to assume that a named temporary file will be (sometimes)
> accesible while it has not been closed yet?
>
> cheers,
> tim

When GvR wrote that around a decade ago, tempfile.mktemp() had not yet
been deprecated.  It returns "an absolute pathname of a file that did
not exist at the time the call is made".  It does not create a file,
you have to do that yourself.

You're quoting the docs for tempfile.TemporaryFile(). It returns a
"file (or file-like) object", and I'd assume that you would have to
pass this object around without closing it in order to use it in the
manner described in GvR's post.


http://docs.python.org/lib/module-tempfile.html

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


  1   2   3   4   5   >