rst2pdf version 0.12.1 released

2009-10-14 Thread Roberto Alsina
I just uploaded rst2pdf 0.12.1 to http://rst2pdf.googlecode.com

This release has no new features, just some bugs fixed. If you had problems 
with previous releases, this could be a good one to try ;-)

For more details, see the changelog: 
http://code.google.com/p/rst2pdf/source/browse/branches/0.12/CHANGES.txt

Rst2pdf is a tool to generate PDF files directly from restructured text 
sources via reportlab.

Rst2pdf aims to support the full restructured text feature set, and is very 
close to that goal, while also including some of the more experimental 
features, like a source code directive with syntax highlighting and math 
notation support with LaTeX-like syntax.

It supports embedding arbitrary fonts, both True Type and PS Type 1, both 
raster and vector images (including SVG and PDF), page transition effects, 
multiple, flexible page layouts, cascading styles, and much, much more.

Best regards,

-- 
 (\''/).__..-''`-. . Roberto Alsina
 `9_ 9  )   `-. ().`-._.`)  KDE Developer (MFCH)
 (_Y_.)' ._   ) `._`.   -.-'  http://lateral.netmanagers.com.ar 
  _..`-'_..-_/ /-'_.' The 6,855th most popular site of Slovenia   
(l)-'' ((i).' ((!.'   according to alexa.com (27/5/2007) 
-- 
http://mail.python.org/mailman/listinfo/python-announce-list

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


pyTexas: Regional Conference - Oct 24/25

2009-10-14 Thread Jeff Rush
pyTexas, the 3rd annual regional Python conference, is coming up in only
ten days. It is being held Oct 24-25 Sat/Sun in Ft. Worth at the
University of North Texas Health Science Center.

  http://pycamp.python.org/Texas

The format is scheduled talks on Saturday morning followed by open space
talks driven by the attendees in the afternoon. On Sunday there will be
sprints on various projects and, in parallel, a Python Lab that tests
the attendees with interesting programming puzzles.

  http://pycamp.python.org/Texas/Schedule2009
  http://pycamp.python.org/Texas/OpenSpaceIdeas
  http://pycamp.python.org/Texas/Sprints2009
  http://pycamp.python.org/Texas/PythonLab

There is no cost to attend but we would appreciate you adding your name
to the registration wiki to give us a better idea of attendance.

  http://pycamp.python.org/Texas/Registration2009

We expect the conference to be a small, interactive gathering of the
Texas community, not a big stuffy conference. It will be a lot of fun.
We hope to see you there!

Jeff Rush, an organizer

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

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


Re: RabbitMQ vs ApacheQpid (AMQP)

2009-10-14 Thread Hendrik van Rooyen
On Tuesday, 13 October 2009 11:42:03 jacopo wrote:

 Background:
 I have a main machine dispatching heavy calculations to different
 machines, collecting the results, performing some calculation on the
 merged results and starting all over again with fresher data. I
 implemented a first solution with Twisted PB, then I decided it was
 more flexible to rely on an AMQP system and I started looking at
 RabbitMQ with txAMQP . Now I am getting really frustrated with the
 complexity of Twisted and the reactor, I am realizing that probably I
 don’t need to handle asynchronicity. Some degree of asynchronicity is
 already handled by the Messaging system and I don’t need to take care
 of it in my components (they keep waiting for a message, when they get
 it they are fully engaged in processing and they cannot do much more).

Have you looked at Pyro?

- Hendrik

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


Re: efficient running median

2009-10-14 Thread Hendrik van Rooyen
On Tuesday, 13 October 2009 17:22:55 Janto Dreijer wrote:
 I'm looking for code that will calculate the running median of a
 sequence, efficiently. (I'm trying to subtract the running median from
 a signal to correct for gradual drift).

 My naive attempt (taking the median of a sliding window) is
 unfortunately too slow as my sliding windows are quite large (~1k) and
 so are my sequences (~50k). On my PC it takes about 18 seconds per
 sequence. 17 of those seconds is spent in sorting the sliding windows.

 I've googled around and it looks like there are some recent journal
 articles on it, but no code. Any suggestions?

Can you not use the mean instead?  It is easier to calculate, as you do not 
have to work through all the values.

- Hendrik


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


the usage of 'yield' keyword

2009-10-14 Thread Peng Yu
http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt

The explanation of yield is not clear to me, as I don't know what a
generator is. I see the following example using 'yield'. Could
somebody explain how 'yield' works in this example? Thank you!

def brange(limit):
  i = 0
  while i  limit:
  yield i
  i += 1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: efficient running median

2009-10-14 Thread sturlamolden
On 14 Okt, 00:03, Steven D'Aprano
ste...@remove.this.cybersource.com.au wrote:

 Obviously to run in O(log n) you must have already built the tree.

You don't need a tree. Quickselect is a partial quicksort.

But my memory served me badly, quickselect is O(n).



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


Re: The rap against while True: loops

2009-10-14 Thread TerryP
Mensanator, thank goodness that was generated :-P
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: efficient running median

2009-10-14 Thread Janto Dreijer
On Oct 13, 6:33 pm, Paul Rubin http://phr...@nospam.invalid wrote:
 Janto Dreijer jan...@gmail.com writes:
  I'm looking for code that will calculate the running median of a
  sequence, efficiently. (I'm trying to subtract the running median from
  a signal to correct for gradual drift).

 Is that a known and valid technique of correcting for drift?  Maybe
 what you really want is a high-pass filter, to remove very low
 frequency components (drift) from the signal.  You can do that with
 standard digital filters.  Using something depending on ordering
 within the samples (i.e. the median) seems weird to me, but I'm no
 expert.

Well, I don't have a lot of theoretical reasoning behind wanting to
use a median filter. I have some experience applying it to images with
very good results, so that was the first thing I tried. It felt right
at the time and the results looked good. Also, most of the elements in
the sequence are supposed to be near zero, so the median is supposed
to give the amount of drift.

That said, you're probably right. I should have first tried to apply a
HPF.

 The obvious way to compute a running median involves a tree structure
 so you can quickly insert and delete elements, and find the median.
 That would be asymtotically O(n log n) but messy to implement.

Messy. Yes. I tried and failed :P

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


Re: efficient running median

2009-10-14 Thread Janto Dreijer
On Oct 13, 7:37 pm, Ethan Furman et...@stoneleaf.us wrote:
 Janto Dreijer wrote:
  I'm looking for code that will calculate the running median of a
  sequence, efficiently. (I'm trying to subtract the running median from
  a signal to correct for gradual drift).

  My naive attempt (taking the median of a sliding window) is
  unfortunately too slow as my sliding windows are quite large (~1k) and
  so are my sequences (~50k). On my PC it takes about 18 seconds per
  sequence. 17 of those seconds is spent in sorting the sliding windows.

  I've googled around and it looks like there are some recent journal
  articles on it, but no code. Any suggestions?

  Thanks
  Janto

 You might look athttp://pypi.python.org/pypi/blist/0.9.4

 ~Ethan~

Very nice! I assume you mean I can use it to quickly insert items into
the sliding window?

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


Re: efficient running median

2009-10-14 Thread Janto Dreijer
On Oct 14, 12:13 am, Paul Rubin http://phr...@nospam.invalid wrote:
 Janto Dreijer jan...@gmail.com writes:
  Well, I don't have a lot of theoretical reasoning behind wanting to
  use a median filter. I have some experience applying it to images with
  very good results, so that was the first thing I tried. It felt right
  at the time and the results looked good.

 If this is image data, which typically would have fairly low
 resolution per pixel (say 8-12 bits), then maybe you could just use
 bins to count how many times each value occurs in a window.  That
 would let you find the median of a window fairly quickly, and then
 update it with each new sample by remembering the number of samples
 above and below the current median, etc.

Thanks, unfortunately it's not image data. It's electrocardiogram
sequences. So it's sequences of floats.

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


Re: the usage of 'yield' keyword

2009-10-14 Thread Javier Collado
Hello,

I think that the best information available on the subject is the following:
http://www.dabeaz.com/generators/
http://www.dabeaz.com/coroutines/

Best regards,
Javier

2009/10/14 Peng Yu pengyu...@gmail.com:
 http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt

 The explanation of yield is not clear to me, as I don't know what a
 generator is. I see the following example using 'yield'. Could
 somebody explain how 'yield' works in this example? Thank you!

 def brange(limit):
  i = 0
  while i  limit:
      yield i
      i += 1
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: MUD Game Programmming - Python Modules in C++

2009-10-14 Thread Ulrich Eckhardt
Irmen de Jong wrote:
 [...] is there any reason why you would go the route of embedding python
 in C++ ? Why not just stick to (pure) Python? Embedding C or C++ stuff
 as extension modules in Python (if you really need to do this) is easier
 than the other way around, in my experience.

If you want to offer scriptability to your application, you obviously need
to embed a scripting language into your application, not the other way
around.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Re: MUD Game Programmming - Python Modules in C++

2009-10-14 Thread Ulrich Eckhardt
Christopher Lloyd wrote:
 I'm a relatively inexperienced programmer, and have been learning some
 basic C++ and working through the demos in Ron Penton's MUD Game
 Programming book. In it, Python modules are run from inside a C++
 program.
[...]
 If I try to compile this in MS Visual C++ 2008 (debug mode), I get the
 following error:
 
 LINK : fatal error LNK1104: cannot open file 'python26_d.lib'

My installation of Python 2.6 doesn't have the debug-compiled Python
libaries, I guess yours doesn't either. Simple solution: don't link to
them, link to the release version instead, even in your debug build.
Unfortunately it requires this the hack with #undefing/#defining _DEBUG.

If you want, you can get the sourcecode for Python, compile it and then use
the generated debug libraries, but I would say it isn't worth the hassle
for now.

 So, what happens if I try compiling the above C++ in release mode?
 Actually, nothing - It compiles just fine. However, upon running the
 resulting program, my command line box displays the following:
 
   Starting Python Demo Test
 
 That's all. The program has hung halfway through and the test isn't
 completed.

If I understand the program correctly, it is waiting for you to enter
something. Each line is then given to Python to interpret, unless the line
is just the simple string end. IOW, this isn't hanging or in any way
broken.

That said, it would hang if you feed it a file via input redirection, unless
the file ends with end. I'd write the loop like this:

  Py_Initialize();
  while(getline(std::cin, line)) {
  if(line==end)
  break;
  Py_RunSimpleString(line.c_str()); // use line
  }
  Py_Finalize();

...and possibly add some error handling to the Py_* calls.


 I've correctly set up all my library files and link (at least, lets assume
 its not that, since I've already spent several hours checking and
 re-checking that).

Note about the linker setup: You don't have to specify the library to link,
that is done in pyconfig.h with 'pragma comment(lib,python26.lib)'. This
is MSVC-specific though.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Re: MUD Game Programmming - Python Modules in C++

2009-10-14 Thread Ulrich Eckhardt
Gabriel Genellina wrote:
 #ifdef _DEBUG
 #undef _DEBUG
 #include Python.h
 #define _DEBUG
 #else
 #include Python.h
 #endif
[...to keep Python from linking against non-existant debug libraries.]
 
 No, don't do that. Just compile your application in release mode.

Why not, does it break anything?

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


win32com.client import problem

2009-10-14 Thread Threader Slash
Hi Everybody,

I have 2 imports:

import pythoncom
from win32com.client import Dispatch

if I run it on my Python 2.6 Console, it works nicely. However, when I go to
Eclipse IDE, open a project, open a main.py file, and try run, it gives the
error:

import pythoncom
ImportError: No module named pythoncom

All other imports are working ok on Eclipse IDE -- e.g. import MySQLdb.

Any suggestion about what is missing?

All comments and suggestion are welcome.

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


Re: The rap against while True: loops

2009-10-14 Thread Marco Mariani

Dennis Lee Bieber wrote:


One thing to note is that break ONLY exits the innermost loop --
Ada adds the confusion that one could define a label on the loops, and
have the innermost use
exit outer_label [when condition]


THAT I find scary... Since you have to match the label name to
something that occurs somewhere prior to the exit, and THEN have to
find the end of that loop.


But we have exceptions. And I know somebody, in other languages, thinks 
it's a Best Practice to avoid using exceptions for flow control.


Thankfully, python programmers are less dogmatic, and use whatever makes 
sense to use. I hope.



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


Re: for loop: range() result has too many items

2009-10-14 Thread Mark Dickinson
On Oct 13, 10:39 pm, Steven D'Aprano
ste...@remove.this.cybersource.com.au wrote:
 On Tue, 13 Oct 2009 16:17:58 -0500, Peng Yu wrote:
  Hi,

  The following code does not run because range() does not accept a big
  number.

 Incorrect.

  range(sys.maxint+2, sys.maxint+5)

 [2147483649L, 2147483650L, 2147483651L]

For what it's worth, there *is* a Python oddity lurking
under the surface here, at least for 64-bit.  Here's
Python 2.6 on a 64-bit machine:

 range(2**31-1)
Traceback (most recent call last):
  File stdin, line 1, in module
MemoryError
 range(2**31)
Traceback (most recent call last):
  File stdin, line 1, in module
OverflowError: range() result has too many items

The first call tries to allocate a list containing
2**31-1 integers.  At 32 bytes per entry (8 for the
list pointer, 24 for the integer itself), that's not
surprising on a machine with  64 Gb of memory.

The second call, however, doesn't even try to
allocate the memory, but decides that the range can't
be represented.  That's not right:  this is an LP64
machine, so the size of the list can be represented,
and the start, stop and step values are representable
as C longs (which is what CPython uses internally for
this purpose).  On a machine with 64 Gb of memory,
this call should succeed.  On an LP64 machine with
64 Gb of memory, it should produce MemoryError, not
OverflowError.

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


Re: python along or bash combined with python (for manipulating files)

2009-10-14 Thread Jean-Michel Pichavant

Peng Yu wrote:
Bash is easy to use 

+JOTW

:)

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


Invitation to connect on LinkedIn

2009-10-14 Thread Frank Pan
LinkedIn


Frank Pan requested to add you as a connection on LinkedIn:
--

Jaime,

I'd like to add you to my professional network on LinkedIn.

- Frank

Accept invitation from Frank Pan
http://www.linkedin.com/e/I2LlXdLlWUhFABKmxVOlgGLlWUhFAfhMPPF/blk/I379833192_3/6lColZJrmZznQNdhjRQnOpBtn9QfmhBt71BoSd1p65Lr6lOfPdvczANcPcUejsPiiZcoQx7e7tluiYUd3kUcP8SejsLrCBxbOYWrSlI/EML_comm_afe/

View invitation from Frank Pan
http://www.linkedin.com/e/I2LlXdLlWUhFABKmxVOlgGLlWUhFAfhMPPF/blk/I379833192_3/0PnP8VcjcPe3ATcQALqnpPbOYWrSlI/svi/

-- 
DID YOU KNOW your LinkedIn profile helps you control your public image when 
people search for you? Setting your profile as public means your LinkedIn 
profile will come up when people enter your name in leading search engines. 
Take control of your image! 
http://www.linkedin.com/e/ewp/inv-22/

 
--
(c) 2009, LinkedIn Corporation

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


Re: the usage of 'yield' keyword

2009-10-14 Thread Stephen Fairchild
Peng Yu wrote:


http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt
 
 The explanation of yield is not clear to me, as I don't know what a
 generator is. I see the following example using 'yield'. Could
 somebody explain how 'yield' works in this example? Thank you!
 
 def brange(limit):
   i = 0
   while i  limit:
   yield i
   i += 1

Let's make this as simple as possible.

 def g(x):
...yield x
...
 p = g(4)
 type(p)
type 'generator'
 type(type(p))
type 'type'

So there you have it. Generator is an instance of 'type', hence a new-style
class, even though it is returned by a simple looking function.

 p.next()
4

What were you expecting?

 type(g)
type 'function'

g is a function that returns a generator. The yield statement does two
things. It states what is to be returned by the generator's next() method
and it also defines g as a function that returns a generator.
-- 
Stephen Fairchild
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Poll on Eval in Python

2009-10-14 Thread Kazimir Majorinc

   THE RESULTS OF THE POLLS
   

.  Lisp   Python   Ruby

Eval is evil,
harmful or at
least unnecessary   2 (4.9%) 7 (21.9%) 0 (0.0%)
--
Eval is useful
but overused   11 (26.8%)6 (18.8%) 9 (29.0%)
--
Eval has just the
right place16 (39.0%)   10 (31.3%)19 (61.3%)
--
Eval is useful
but neglected   3 (7.3%) 4 (12.5%) 1 (3.2%)
--
Eval is a single
most important
feature 5 (12.2%)0 (0.0%)  0 (0.0%)
--
I do not care
for eval4 (9.8%) 5 (15.6%) 2 (6.5%)


--
Kazimir Majorinc
blog: http://kazimirmajorinc.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Writing to function arguments during execution

2009-10-14 Thread Dave Angel

John O'Hagan wrote:

snip
Thanks, sockets are the way to go for this and surprisingly easy to use once 
you get your head around them. I tried Rhodri's suggested approach but for now 
I used the original terminal for both starting the program and entering new 
options (still via raw_input) and a new terminal listening on a socket 
connection to display the results. 

A secondary question: right now I'm starting the listening terminal by 
executing a script ('display.py') as a subprocess:


port = 50007
here = os.path.abspath('')
terminal = os.environ['TERM']
subprocess.Popen([terminal, '-e', here + '/display.py', str(port)])

but to me it feels kind of clunky to have a separate script just for this; is 
there a nicer way to launch another terminal, say by passing a locally defined 
function to it?


Regards,

John

  
You could pass it the same script you're running, but with a 
command-line argument that causes it to execute a different part of the 
script.  Perhaps the port parameter above is enough, as your main 
process won't be getting that argument.  I'd recommend something 
explicit, however.


DaveA

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


id( ) function question

2009-10-14 Thread raffaele ponzini
Dear all,
I have a question concerning the output of the id() function.
In particular since is should:

Return the identity of an object.  This is guaranteed to be unique among
simultaneously existing objects.  (Hint: it's the object's memory address.)

i expect that for two differnt objects it returns two differnt adress in memory.

Let's seee a correct case:
 a=10
 b=20
 a is b
False
 id(a)
9986060
 id(b)
9985940
 c=a
 c is a
True
 id(c)
9986060
 id(a)
9986060

And now a strange (for me) output:
 d=10 #here i'm assingning a integer value to a fresh new variable d without 
 any kind of  link to the variable a
 d is a
True
 d==a
True
 id(a)
9986060
 id(b)
9985940
 id(d)
9986060
 a=1e10
 d=1e10
 d is a
False
 id(a)
11388984
 id(d)
11388920


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


shelve, segfault and bsddb.db.DBPageNotFoundError: (-30987, 'DB_PAGE_NOTFOUND: Requested page not found')

2009-10-14 Thread Gabriel Rossetti
Hello everyone, I get the following error with the shelve module (python 
2.5.2) sometimes :


 s = shelve.open(save.dat, writeback=True)
 s
Traceback (most recent call last):
 File stdin, line 1, in module
 File /usr/lib/python2.5/UserDict.py, line 167, in __repr__
   return repr(dict(self.iteritems()))
 File /usr/lib/python2.5/UserDict.py, line 105, in iteritems
   for k in self:
 File /usr/lib/python2.5/UserDict.py, line 92, in __iter__
   for k in self.keys():
 File /usr/lib/python2.5/shelve.py, line 92, in keys
   return self.dict.keys()
 File /usr/lib/python2.5/bsddb/__init__.py, line 252, in keys
   return _DeadlockWrap(self.db.keys)
 File /usr/lib/python2.5/bsddb/dbutils.py, line 62, in DeadlockWrap
   return function(*_args, **_kwargs)
bsddb.db.DBPageNotFoundError: (-30987, 'DB_PAGE_NOTFOUND: Requested page 
not found')


This must mean that my file is currupt, correct? If I do the following I 
get a segfault :


 len(s)
Segmentation fault

Is this a bug? So as a workaround I must do something like this :

s = shelve.open(save.dat, writeback=True)
try:
   s.keys()
except bsddb.db.DBPageNotFoundError, e:
   os.remove(save.dat)
   s = shelve.open(save.dat, writeback=True)

Any other ideas/comments? Does anyone else have this problem?

Thanks,
Gabriel

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


id( ) function question

2009-10-14 Thread raffaele ponzini
Dear all,
I have a question concerning the output of the id() function.
In particular since is should:

Return the identity of an object.  This is guaranteed to be unique among
simultaneously existing objects.  (Hint: it's the object's memory address.)

i expect that for two differnt objects it returns two differnt adress in memory.

Let's seee a correct case:
 a=10
 b=20
 a is b
False
 id(a)
9986060
 id(b)
9985940
 c=a
 c is a
True
 id(c)
9986060
 id(a)
9986060

And now a strange (for me) output:
 d=10
#here i'm assingning a integer value to a fresh new variable d without
any kind of
#link to the variable a

 d is a
True
 d==a
True
 id(a)
9986060
 id(d)
9986060

But if I chose as a value another number (a big one, let say 1e10) I
get what I will expect also in the case of the chose of the integer 10
showed above:
 a=1e10
 d=1e10
 d is a
False
 id(a)
11388984
 id(d)
11388920


can you please explain me the reasion of this strange behaviour.
Thanks,
--
-- 
lele
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the usage of 'yield' keyword

2009-10-14 Thread Dave Angel

Peng Yu wrote:

http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt

The explanation of yield is not clear to me, as I don't know what a
generator is. I see the following example using 'yield'. Could
somebody explain how 'yield' works in this example? Thank you!

def brange(limit):
  i = 0
  while i  limit:
  yield i
  i += 1

  
Informally, a generator is a convenient syntax for writing an iterator.  
What the above does is match xrange() behavior for single argument, but 
without any limitation on the size or type of the limit parameter.


A little too much detail:  When a function has the keyword yield 
within it, the function works much differently than normal functions.  
When it's called as a function, it returns a special object called an 
iterator.  That iterator has a method called next(), which when called 
executes a piece of this function.  It executes the function until it 
encounters a 'yield' statement.  Then the yield value is returned from 
the next() function, but the function is still out-there, suspended.  
All local variables still exist, and it's just waiting for a chance to 
run some more.  Next time next() method is called, the function resumes 
right after the yield, and runs until it gets to another yield (in this 
case the same yield, but with a new value).  That new value is returned 
from the next().  Eventually, the function may 'return' instead of 
'yield', as this one does when the limit value is reached.  At that 
point, it generates a stop iteration exception (or some name like 
that).  Now this sounds way too complex.


But, if you study the definition of the for loop, you'll find a 
complementary description.  When you write

   for  item in  iterable:

The iterable is called (I'm blurring the details of when you need 
parens, and when you don't), and next() is called repeatedly, with the 
results of next() being assigned to 'item' until an exception occurs.  
If the exception is 'stop iteration' then the loop terminates normally.


Bottom line is it is easy to write very complicated generators which are 
easy to use in simple for loops.  And although you should also try to 
create an iterator object manually, for the experience, most of the time 
the generator function is much easier/quicker to write, and much easier 
to maintain and debug.




Note that yield can do even more.  I'm trying to describe the common 
usage, which is very convenient.  And it can frequently  be used to take 
a complicated generator and wrap it to make an easier-to-use generator 
that's more specific to a particular purpose.  For example, if os.walk 
is a pain to use, and all you need is a sequence of all the files in a 
directory tree,


def find(root):
  for pdf in os.walk(root, topdown=False):
  for file in pdf[2]:
  yield os.path.join(pdf[0],file)



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


Re: win32com.client import problem

2009-10-14 Thread Dave Angel

Threader Slash wrote:

Hi Everybody,

I have 2 imports:

import pythoncom
from win32com.client import Dispatch

if I run it on my Python 2.6 Console, it works nicely. However, when I go to
Eclipse IDE, open a project, open a main.py file, and try run, it gives the
error:

import pythoncom
ImportError: No module named pythoncom

All other imports are working ok on Eclipse IDE -- e.g. import MySQLdb.

Any suggestion about what is missing?

All comments and suggestion are welcome.

ThreaderSlash

  
Two things to check, python version (sys.version), and sys.path.   Add 
prints for the two of them at the beginning of your script, and try the 
script in both environments.  If there are any differences, figure out 
how to reconfigure Eclipse to match what you've got at the console.



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


Re: python performance on Solaris

2009-10-14 Thread Antoine Pitrou
inaf cem.ezberci at gmail.com writes:
 
 Good point. I failed to compare the CPU power on these machines.. 32
 bit linux box I have is 2666 Mhz vs the Solaris zone is 1415 Mhz.. I
 guess that explains :) Thank you for the tip..

You have to compare not only CPU frequencies but the CPU models.
Recently Sun has been selling CPUs optimized for multi-threading (e.g. the
UltraSPARC T2 or Niagara CPUs) which have, by design, very poor
single-threaded performance. If your Solaris zone uses such a CPU then a 6-8x
difference in single-threaded performance compared to a modern Intel or AMD CPU
is totally expected.

Regards

Antoine.


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


Re: id( ) function question

2009-10-14 Thread Christian Heimes
raffaele ponzini schrieb:
 Dear all,
 I have a question concerning the output of the id() function.
 In particular since is should:
 
 Return the identity of an object.  This is guaranteed to be unique among
 simultaneously existing objects.  (Hint: it's the object's memory address.)
 
 i expect that for two differnt objects it returns two differnt adress in 
 memory.

[snip]

Some Python implementations may choose to cache certain objects or use
an internal free list to cache objects. For example CPython caches small
integers, strings with one character and some other objects. It uses a
free list for tuples, lists and dicts. This leads to effects like:

 {} is {}
False
 id({}) == id({})
True

Christian

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


Re: id( ) function question

2009-10-14 Thread Andre Engels
What is going on is that a few objects that are often used, in
particular the small (how small is small depends on the
implementation) integers, are 'preloaded'. When one of these is then
referred to, a new object is not created, but the pre-defined object
is used. 10 is apparently a preloaded constant in your implementation,
1e10 is not.

As far as I know, only None is _guaranteed_ to be such a preloaded
object, so one should not rely on it in implementations.

On Wed, Oct 14, 2009 at 12:46 PM, raffaele ponzini
raffaele.ponz...@gmail.com wrote:
 Dear all,
 I have a question concerning the output of the id() function.
 In particular since is should:
 
 Return the identity of an object.  This is guaranteed to be unique among
 simultaneously existing objects.  (Hint: it's the object's memory address.)
 
 i expect that for two differnt objects it returns two differnt adress in 
 memory.

 Let's seee a correct case:
 a=10
 b=20
 a is b
 False
 id(a)
 9986060
 id(b)
 9985940
 c=a
 c is a
 True
 id(c)
 9986060
 id(a)
 9986060

 And now a strange (for me) output:
 d=10 #here i'm assingning a integer value to a fresh new variable d 
 without any kind of  link to the variable a
 d is a
 True
 d==a
 True
 id(a)
 9986060
 id(b)
 9985940
 id(d)
 9986060
 a=1e10
 d=1e10
 d is a
 False
 id(a)
 11388984
 id(d)
 11388920


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





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


Re: id( ) function question

2009-10-14 Thread Tim Chase

But if I chose as a value another number (a big one, let say 1e10) I
get what I will expect also in the case of the chose of the integer 10
showed above:

a=1e10
d=1e10
d is a

False

id(a)

11388984

id(d)

11388920


CPython has the option to cache frequently used items, and does 
so for a small range of ints.  It's not guaranteed behavior (or a 
guaranteed range) so you shouldn't rely on it, but it's an 
efficiency thing.  In my current version, it looks like it's ints 
from -5 to 256. YMMV


In general, if you're using is (and not comparing with None) or 
id(), you're doing it wrong unless you already know the 
peculiarities of Python's identity implementations.


-tkc


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


Re: id( ) function question

2009-10-14 Thread Christian Heimes
Andre Engels schrieb:
 What is going on is that a few objects that are often used, in
 particular the small (how small is small depends on the
 implementation) integers, are 'preloaded'. When one of these is then
 referred to, a new object is not created, but the pre-defined object
 is used. 10 is apparently a preloaded constant in your implementation,
 1e10 is not.
 
 As far as I know, only None is _guaranteed_ to be such a preloaded
 object, so one should not rely on it in implementations.

None, True, False, NotImplemented are guaranteed to be singletons, all
builtin types and exceptions can be considered as singletons, too.

Christian

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


Unexpected exit of Python script

2009-10-14 Thread vicky
Hello All,

I am a new Python user and don't know much about it.

I just want to know that, due to any reason if a script exits, is
their some way to release all the resources acquired by the script
during execution ?

Actually In my system I want to execute some piece of code at the time
of script exit (expected or unexpected) to ensure the release of all
the resources. I don't know how to do that :(

Can anyone please help me ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Clear interface for mail class

2009-10-14 Thread Benedict Verheyen
Hi,


I'm trying to come up with a decent interface for my email class.
Basically, i have one email function but it has many (too many?) variables:

send_mail(self, send_from, send_to, send_cc, subject, text, separate_emails 
= False, files=[], inline_files=[], server=localhost,
charset=iso-8859-1)

I'm thinking on how i could simplify it and make it look nicer and more easy to 
use.
I can reduce the number of arguments by adding functions for the less common 
tasks
like adding attachement, inline files and so on.
Then i would end up with functions like this:

def __init__(self):


self._files=[]
self._inline_files=[]
...

def add_files(self, files=[]):
assert type(files)==list
self._files=files
...

When sending an email, i could check if files where specified and if so, send 
them too.

But it doesn't feel right to just have a bunch of small functions to set 
variables.
Calling the function would change from:
 (where m is the mail class)
 m.send_mail( from...@work,
  t...@work,
  [],
  Test emailmodule ,
  MSG,
  separate_emails = True,
  files=[attached_pic.png],
  inline_files=[inline_pic.png],
  server=mysmtpserver)

to:

m.add_files([attached_pic.png])
m.add_inline_files([inline_pic.png])
m.smtp_server(mysmtpserver)
m.send_mail( from...@work,
 t...@work,
 [],
 Test emailmodule ,
 MSG)

It looks already better and i could set the smtp server as a class variable,
but i'm not sure this will present me with the most natural interface to use.

Or should i make 1 general function that sets vars according to a type?
For instance: add_header(To,[list_of_recipients])

This kind of problems seems to happen sometimes: i need to fix something 
quickly,
build a script for it, and then i realise that the code i've written is not the 
best in
terms of reusability and has some not so great functions or classes.
Speedy development eh.

Any ideas are welcome.

Thanks,
Benedict

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


Re: Clear interface for mail class

2009-10-14 Thread Marco Mariani

Benedict Verheyen wrote:


Any ideas are welcome.


easy_install turbomail

:)

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


Re: Unexpected exit of Python script

2009-10-14 Thread Diez B. Roggisch
vicky wrote:

 Hello All,
 
 I am a new Python user and don't know much about it.
 
 I just want to know that, due to any reason if a script exits, is
 their some way to release all the resources acquired by the script
 during execution ?
 
 Actually In my system I want to execute some piece of code at the time
 of script exit (expected or unexpected) to ensure the release of all
 the resources. I don't know how to do that :(
 
 Can anyone please help me ?

What resources? Usually, the garbage-collection and the OS will do that for
you anyway. So are there concrete problems you observe? 

Also, take a look at the atexit-module. 

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


Re: Multi-arrays python

2009-10-14 Thread Piet van Oostrum
 bbarb...@inescporto.pt (b) wrote:

b Hi again!
b After testing the whole day, I have got my goals from the last email,  but
b as always, another issues came up! and now that Ive been able to  save a
b list of list (or multi-arrays) as below :

b ['100.mp3\n' '10008.mp3\n' '10005.mp3\n' '10001.mp3\n' '10006.mp3\n']
b ['10001.mp3\n' '10005.mp3\n' '100.mp3\n' '10008.mp3\n' '10006.mp3\n']
b ['10005.mp3\n' '10001.mp3\n' '100.mp3\n' '10008.mp3\n' '10006.mp3\n']
b ['10006.mp3\n' '10005.mp3\n' '10001.mp3\n' '100.mp3\n' '10008.mp3\n']
b ['10008.mp3\n' '100.mp3\n' '10001.mp3\n' '10005.mp3\n' '10006.mp3\n']

b I am not able to manipulate it again! I read it with:
b Myfile.read() and all what I get is a str type data, what make my aim  very
b difficult to reach!  What I want, is just to read one line(one  specific
b line, so  I wouldnt have to read the whole file) and to get  the numbers of
b the songs from that line. Maybe I should save the  information in another
b way... But I just get those lines as lists, and  write them in a file. Is
b there a better way? I am very receptive to  suggestions! Thanks again for
b your help!

You are unclear about the syntax of your file. What is a line? Is this a
line?
['100.mp3\n' '10008.mp3\n' '10005.mp3\n' '10001.mp3\n' '10006.mp3\n']

If so what do the \n after the filenames mean? Is \n a newline, or is it
a backslash followed by 'n'? In the first case then every filename is on
a separate line. So then you are mixing two concepts of line.

Anyhow you would make your life easier by getting rid of the \n's.

If all your filenames are numeric with the extension mp3 then the
following gives you a list of the filenames from a line:

In [31]: re.findall('[0-9]+\.mp3', line)
Out[31]: ['100.mp3', '10008.mp3', '10005.mp3', '10001.mp3', '10006.mp3']

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


Python and USB

2009-10-14 Thread Ronn Ross
I'm new to Python and would like to capture mouse movements. I envision
writing a script that will just write out the mouse movements in the term.
Is this possible? Can someone point me in the right direction?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected exit of Python script

2009-10-14 Thread Donn
On Wednesday 14 October 2009 14:23:11 vicky wrote:
 I just want to know that, due to any reason if a script exits, is
 their some way to release all the resources acquired by the script
 during execution ?
Python cleans-up after itself so I would not worry about that until you are an 
expert and need to split hairs. Just get on with learning and writing code.

Look at exceptions (try, except) for this kind of thing -- but it's a basic 
part of learning Python anyway.

About the only thing I ever found I could not catch was a segfault (under 
Linux) and to get around that I open my main app from a smaller one using the 
subprocess module. When the main app crashes (which it does sometime) that 
process dies suddenly, but it falls-back to the original script which then 
detects the error return code and can continue whatever it needs to do.

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


Re: Unexpected exit of Python script

2009-10-14 Thread Mick Krippendorf
vicky schrieb:
 Actually In my system I want to execute some piece of code at the time
 of script exit (expected or unexpected) to ensure the release of all
 the resources. I don't know how to do that :(

You maybe want to use a context manager. Look for 'with statement' and
'contextlib' in your docs.


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


Re: cpython compilation parameter

2009-10-14 Thread Jorgen Grahn
On Thu, 2009-10-08, Diez B. Roggisch wrote:
 cEd wrote:

 Hello,
 
 I'm wondering how to compile python to get good performance.
 Because when I compare this ugly code which find prime number:

...

  between :
  - the python distributed with my ubuntu
 #time python prime_number.py  /dev/null
 real0m12.237s
 user0m12.129s
 sys0m0.024s
 
  - and the one compiled by my self
 time my_python_compiled prime_number.py  /dev/null
 real0m42.193s
 user0m41.891s
 sys0m0.044s
 
 so which option should I give or which flag ???

 I doubt that there is such a flag. There must be a different reason for
 this. Can you give us the python versions for each, and architecture (32/64
 bit)?

He could start by compiling it exactly like Ubuntu does. Just get the
Ubuntu source packet -- it's all in there, Ubuntu doesn't keep it a
secret.

/Jorgen

-- 
  // Jorgen Grahn grahn@  Oo  o.   .  .
\X/ snipabacken.se   O  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Clear interface for mail class

2009-10-14 Thread Benedict Verheyen
Marco Mariani wrote:
 Benedict Verheyen wrote:
 
 Any ideas are welcome.
 
 easy_install turbomail
 
 :)
 

Looks good but i'm still interested in how one would
make a clean class interface for this type of problem.

Having said that, turbomail looks quite good :)

Thanks

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


Re: Unexpected exit of Python script

2009-10-14 Thread vicky
On Oct 14, 5:52 pm, Diez B. Roggisch de...@nospam.web.de wrote:
 vicky wrote:
  Hello All,

  I am a new Python user and don't know much about it.

  I just want to know that, due to any reason if a script exits, is
  their some way to release all the resources acquired by the script
  during execution ?

  Actually In my system I want to execute some piece of code at the time
  of script exit (expected or unexpected) to ensure the release of all
  the resources. I don't know how to do that :(

  Can anyone please help me ?

 What resources? Usually, the garbage-collection and the OS will do that for
 you anyway. So are there concrete problems you observe?

 Also, take a look at the atexit-module.

 Diez

According to the documentation of atexit module:

The atexit module defines a single function to register cleanup
functions. Functions thus registered are automatically executed upon
normal interpreter termination.

Note: the functions registered via this module are not called when the
program is killed by a signal, when a Python fatal internal error is
detected, or when os._exit() is called.

Actually I have Python ported on vx-works. During initialization I am
initializing some python interpreters, and each interpreter is
associated with a specific buffer to send or receive the messages
using underlying protocol (embedded in C library). Using same library
I am using the subscription functionality. So when I am using Python
to made subscriptions it register an entry. But if some user forgets
to clear the subscription or script exited accidently due to some
error, subscription still remain active. And when next time the same
interpreter is used to execute some script, the older subscription
create a trouble for the user. I want some implementation which
guarantees the clearance of all the subscriptions at script exit
(expected or unexpected) automatically.

Thanks for your reply.
/vicky
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python performance on Solaris

2009-10-14 Thread Jorgen Grahn
On Wed, 2009-10-14, Antoine Pitrou wrote:
 inaf cem.ezberci at gmail.com writes:
 
 Good point. I failed to compare the CPU power on these machines.. 32
 bit linux box I have is 2666 Mhz vs the Solaris zone is 1415 Mhz.. I
 guess that explains :) Thank you for the tip..

 You have to compare not only CPU frequencies but the CPU models.

Yes, at least that.  Megahertz figures have been useless for decades,
except in advertising.

 Recently Sun has been selling CPUs optimized for multi-threading (e.g. the
 UltraSPARC T2 or Niagara CPUs) which have, by design, very poor
 single-threaded performance. If your Solaris zone uses such a CPU then a 6-8x
 difference in single-threaded performance compared to a modern Intel
 or AMD CPU
 is totally expected.

(Had to Google it. A Solaris Zone is apparently some kind of
virtualization thing, with low CPU overhead.)

s/multi-threading/multi-programming/ I suppose. I certainly hope you
can still get performance while running many separate true processes in
parallel.

/Jorgen

-- 
  // Jorgen Grahn grahn@  Oo  o.   .  .
\X/ snipabacken.se   O  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected exit of Python script

2009-10-14 Thread Diez B. Roggisch
vicky wrote:

 On Oct 14, 5:52 pm, Diez B. Roggisch de...@nospam.web.de wrote:
 vicky wrote:
  Hello All,

  I am a new Python user and don't know much about it.

  I just want to know that, due to any reason if a script exits, is
  their some way to release all the resources acquired by the script
  during execution ?

  Actually In my system I want to execute some piece of code at the time
  of script exit (expected or unexpected) to ensure the release of all
  the resources. I don't know how to do that :(

  Can anyone please help me ?

 What resources? Usually, the garbage-collection and the OS will do that
 for you anyway. So are there concrete problems you observe?

 Also, take a look at the atexit-module.

 Diez
 
 According to the documentation of atexit module:
 
 The atexit module defines a single function to register cleanup
 functions. Functions thus registered are automatically executed upon
 normal interpreter termination.
 
 Note: the functions registered via this module are not called when the
 program is killed by a signal, when a Python fatal internal error is
 detected, or when os._exit() is called.
 
 Actually I have Python ported on vx-works. During initialization I am
 initializing some python interpreters, and each interpreter is
 associated with a specific buffer to send or receive the messages
 using underlying protocol (embedded in C library). Using same library
 I am using the subscription functionality. So when I am using Python
 to made subscriptions it register an entry. But if some user forgets
 to clear the subscription or script exited accidently due to some
 error, subscription still remain active. And when next time the same
 interpreter is used to execute some script, the older subscription
 create a trouble for the user. I want some implementation which
 guarantees the clearance of all the subscriptions at script exit
 (expected or unexpected) automatically.

Ok, the embedded part is crucial here - because the atexit is AFAIK
implemented in terms of the real atexit - so it will only be called when
the process dies.

In your case, I guess you have two options: 

 - make the interpreter execute scripts that always have try/finally with
some cleanup-code in the finally

 - clean up after the interpreter in C++

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


Re: Clear interface for mail class

2009-10-14 Thread Joe Riopel
On Wed, Oct 14, 2009 at 8:39 AM, Benedict Verheyen
benedict.verhe...@gmail.com wrote:
 This kind of problems seems to happen sometimes: i need to fix something 
 quickly,
 build a script for it, and then i realise that the code i've written is not 
 the best in
 terms of reusability and has some not so great functions or classes.
 Speedy development eh.

This happens to me a lot too, especially when working on a quick
fix. I don't worry about the re-usability of my code until I actually
have another use for it. Once I find another/similar problem I am
trying to solve, I will spend the time on making the original code
more re-usable. Trying to guess at what will be a useful API, sometime
down the line, is hard for me to do.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MUD Game Programmming - Python Modules in C++

2009-10-14 Thread Gabriel Genellina

En Wed, 14 Oct 2009 05:19:06 -0300, Ulrich Eckhardt
eckha...@satorlaser.com escribió:


Gabriel Genellina wrote:

#ifdef _DEBUG
#undef _DEBUG
#include Python.h
#define _DEBUG
#else
#include Python.h
#endif

[...to keep Python from linking against non-existant debug libraries.]


No, don't do that. Just compile your application in release mode.


Why not, does it break anything?


You have to ensure the same block is used everywhere Python.h is
included, or remember to always use mypython.h, but there is still the
risk when adding some other external library.
The object layout is different in a debug build, and there are other
incompatible differences. Mixing code compiled in both modes hopefully
would generate a linker error, but if not, it may crash the application.
The debug flag for Python *should* be decoupled from the debug flag for
the application embedding it, but unfortunately it isn't. There is a
feature request at http://bugs.python.org (the site doesn't work for me
now)

--
Gabriel Genellina

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


Re: What is Islam?-1

2009-10-14 Thread Aahz
In article mailman.1327.1255481486.2807.python-l...@python.org,
J  dreadpiratej...@gmail.com wrote:
On Tue, Oct 13, 2009 at 20:05, Aahz a...@pythoncraft.com wrote:
Mensanator:

There's no point in trying to reason with a Muslim.

 That's not funny, and if you were being serious, that was incredibly
 rude.

Not as much as posting in comp.lang.python.

 What exactly are you claiming is rude?

This entire thread is rude and really has no place on a list like this.

You have a point, but posting derogatory remarks about religions is even
less appropriate and I will call people who post them to task.  The
correct response to spam is to just ignore it in the first place (or
report it to the person's ISP).

There are plenty of Muslims in the Python community and they don't
deserve to get treated to insults from Mensanator.

Side note: you're not much of one to talk about rudeness when you remove
attributions from quotes.
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

To me vi is Zen.  To use vi is to practice zen.  Every command is a
koan.  Profound to the user, unintelligible to the uninitiated.  You
discover truth everytime you use it.  --re...@lion.austin.ibm.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Pyusb

2009-10-14 Thread Ronn Ross
I'm running Python 2.6 on windows. I'm install Pyusb and I'm having trouble
including the library in my script. Can someone point me in the right
direction to get this working or know of a good tutorial to help me out.
Best regards
-- 
http://mail.python.org/mailman/listinfo/python-list


Help with regex and optional substring in search string

2009-10-14 Thread Timur Tabi
I'm having trouble creating a regex pattern that matches a string that
has an optional substring in it.  What I'm looking for is a pattern
that matches both of these strings:

Subject: [PATCH 08/18] This is the patch name
Subject: This is the patch name

What I want is to extract the This is the patch name.  I tried this:

m = re.search('Subject:\s*(\[[\w\s]*\])*(.*)', x)

Unfortunately, the second group appears to be too greedy, and returns
this:

 print m.group(1)
None
 print m.group(2)
[PATCH 08/18] Subject line


Can anyone help me?  I'd hate to have to use two regex patterns, one
with the [...] and one without.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: efficient running median

2009-10-14 Thread Peter Otten
Janto Dreijer wrote:

 On Oct 13, 6:12 pm, Peter Otten __pete...@web.de wrote:
 Janto Dreijer wrote:
  I'm looking for code that will calculate the running median of a
  sequence, efficiently. (I'm trying to subtract the running median from
  a signal to correct for gradual drift).

  My naive attempt (taking the median of a sliding window) is
  unfortunately too slow as my sliding windows are quite large (~1k) and
  so are my sequences (~50k). On my PC it takes about 18 seconds per
  sequence. 17 of those seconds is spent in sorting the sliding windows.

  I've googled around and it looks like there are some recent journal
  articles on it, but no code. Any suggestions?

 If you aren't using numpy, try that. Showing your code might also be a
 good idea...

 Peter
 
 I placed the test code and its output here:
 http://bitbucket.org/janto/snippets/src/tip/running_median.py

That gives me something to tinker ;)
 
 I also have a version that uses numpy. On random data it seems to be
 about twice as fast as the pure python one. It spends half the time
 sorting and the other half converting the windows from python lists to
 numpy arrays.
 If the data is already sorted, the version using python's builtin sort
 outperforms the numpy convert-and-sort by about 5 times. Strangely
 satisfying :)

I was thinking of using as many of numpy's bulk operations as possible:

def running_median_numpy(seq):
data = array(seq, dtype=float)
result = []
for i in xrange(1, window_size):
window = data[:i]
result.append(median(window))
for i in xrange(len(data)-window_size+1):
window = data[i:i+window_size]
result.append(median(window))
return result

But it didn't help as much as I had hoped.
The fastest I came up with tries hard to keep the data sorted:

def running_median_insort(seq):
seq = iter(seq)
d = deque()
s = []
result = []
for item in islice(seq, window_size):
d.append(item)
insort(s, item)
result.append(s[len(d)//2])
m = window_size // 2
for item in seq:
old = d.popleft()
d.append(item)
del s[bisect_left(s, old)]
insort(s, item)
result.append(s[m])
return result

Some numbers:

10.197 seconds for running_median_scipy_medfilt
25.043 seconds for running_median_python
13.040 seconds for running_median_python_msort
14.280 seconds for running_median_python_scipy_median
4.024 seconds for running_median_numpy
0.221 seconds for running_median_insort

What would be an acceptable performance, by the way?

Peter

PS: code not tested for correctness


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


Re: Clear interface for mail class

2009-10-14 Thread Francesco Bochicchio
On Oct 14, 2:39 pm, Benedict Verheyen benedict.verhe...@gmail.com
wrote:
 Hi,

 I'm trying to come up with a decent interface for my email class.
 Basically, i have one email function but it has many (too many?) variables:

     send_mail(self, send_from, send_to, send_cc, subject, text, 
 separate_emails = False, files=[], inline_files=[], server=localhost,
 charset=iso-8859-1)

 I'm thinking on how i could simplify it and make it look nicer and more easy 
 to use.
 I can reduce the number of arguments by adding functions for the less common 
 tasks
 like adding attachement, inline files and so on.
 Then i would end up with functions like this:

 def __init__(self):
     
     
     self._files=[]
     self._inline_files=[]
 ...

 def add_files(self, files=[]):
     assert type(files)==list
     self._files=files
 ...

 When sending an email, i could check if files where specified and if so, send 
 them too.

 But it doesn't feel right to just have a bunch of small functions to set 
 variables.
 Calling the function would change from:
  (where m is the mail class)
  m.send_mail( from...@work,
               t...@work,
               [],
               Test emailmodule ,
               MSG,
               separate_emails = True,
               files=[attached_pic.png],
               inline_files=[inline_pic.png],
               server=mysmtpserver)

 to:

 m.add_files([attached_pic.png])
 m.add_inline_files([inline_pic.png])
 m.smtp_server(mysmtpserver)
 m.send_mail( from...@work,
              t...@work,
              [],
              Test emailmodule ,
              MSG)

 It looks already better and i could set the smtp server as a class variable,
 but i'm not sure this will present me with the most natural interface to use.

 Or should i make 1 general function that sets vars according to a type?
 For instance: add_header(To,[list_of_recipients])

 This kind of problems seems to happen sometimes: i need to fix something 
 quickly,
 build a script for it, and then i realise that the code i've written is not 
 the best in
 terms of reusability and has some not so great functions or classes.
 Speedy development eh.

 Any ideas are welcome.

 Thanks,
 Benedict

I would add a server class, maybe subclassing something in standard
library, and add to it the 'send' method, so that sending a mail would
be
something like:

myserver = MyMailServer(mysmtpserver, localhost, ) # this only
needs to be done once, not for each mail

m = MyMail( subject, text, separate_emails = False, files=[],
inline_files=[] ) # mail creation

myserver.send( m, from= from...@work, # mail sending
   to = t...@work,
   cc_to= None  )

Note that I put sender and destination senders in the send method, not
as attributes  of the mail object. It makes more sense to me, and yopu
can reuse
the same object if you want to send the same mail to many addresses
( and/or via different servers ).

IN general, un case like yours I use a lot default parameters, as you
did already. Having separate methods to setting specific part of an
object only makes
sens (to me) if you need  first to create an object and later change
some of the attributes. Also, if you are just going to change the
attributes, you do not
need a method: just use the object.attribute = value syntax. If you
are going to need later to do more complex thing, you can always
transform your
attribute in a property.




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


Re: Help with regex and optional substring in search string

2009-10-14 Thread Timur Tabi
On Oct 14, 9:51 am, Timur Tabi timur.t...@gmail.com wrote:
 I'm having trouble creating a regex pattern that matches a string that
 has an optional substring in it.  What I'm looking for is a pattern
 that matches both of these strings:

 Subject: [PATCH 08/18] This is the patch name
 Subject: This is the patch name

 What I want is to extract the This is the patch name.  I tried this:

 m = re.search('Subject:\s*(\[[\w\s]*\])*(.*)', x)

Never mind ... I figured it out.  The middle block should have been [\w
\s/]*

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


Re: Python and USB

2009-10-14 Thread Gabriel Genellina
En Wed, 14 Oct 2009 09:55:15 -0300, Ronn Ross ronn.r...@gmail.com  
escribió:



I'm new to Python and would like to capture mouse movements. I envision
writing a script that will just write out the mouse movements in the  
term.

Is this possible? Can someone point me in the right direction?


Capture mouse movements in your application, or globally?
All GUI frameworks that I know of (including the one that comes with  
Python, Tkinter, a wrapper around Tk and Tcl) provide some sort of  
notifications when the mouse is over your program window.
If you need to track the mouse everywhere (not only inside your app), that  
depends on the OS and desktop you're using. On Windows I think there's a  
library named pyhook or similar.


--
Gabriel Genellina

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


Re: Help with regex and optional substring in search string

2009-10-14 Thread Zero Piraeus
:

2009/10/14 Timur Tabi timur.t...@gmail.com:
 I'm having trouble creating a regex pattern that matches a string that
 has an optional substring in it.  What I'm looking for is a pattern
 that matches both of these strings:

 Subject: [PATCH 08/18] This is the patch name
 Subject: This is the patch name

 What I want is to extract the This is the patch name.  I tried this:

 m = re.search('Subject:\s*(\[[\w\s]*\])*(.*)', x)

 Unfortunately, the second group appears to be too greedy, and returns
 this:

 print m.group(1)
 None
 print m.group(2)
 [PATCH 08/18] Subject line

It's not that the second group is too greedy. The first group isn't
matching what you want it to, because neither \w nor \s match the /
inside your brackets. This works for your example input:

 import re
 pattern = re.compile(Subject:\s*(?:\[[^\]]*\])?\s*(.*))
 for s in (
... Subject: [PATCH 08/18] This is the patch name,
... Subject: This is the patch name,
... ):
... re.search(pattern, s).group(1)
...
'This is the patch name'
'This is the patch name'

Going through the changes from your original regex in order:

'(?:etc)' instead of '(etc)' are non-grouping parentheses (since you
apparently don't care about that bit).

'[^\]]' instead of '[\w\s]' matches everything except a closing bracket.

The '\s*' before the second set of parentheses takes out the leading
whitespace that would otherwise be returned as part of the match.

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help with regex and optional substring in search string

2009-10-14 Thread Zero Piraeus
:

2009/10/14 Timur Tabi timur.t...@gmail.com:
 Never mind ... I figured it out.  The middle block should have been [\w
 \s/]*

This is fragile - you'll have to keep adding extra characters to match
if the input turns out to contain them.

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against while True: loops

2009-10-14 Thread Tim Rowe
2009/10/14 Marco Mariani ma...@sferacarta.com:
 Dennis Lee Bieber wrote:

        One thing to note is that break ONLY exits the innermost loop --
 Ada adds the confusion that one could define a label on the loops, and
 have the innermost use
        exit outer_label [when condition]


        THAT I find scary... Since you have to match the label name to
 something that occurs somewhere prior to the exit, and THEN have to
 find the end of that loop.

 But we have exceptions.

So has Ada.

 And I know somebody, in other languages, thinks it's
 a Best Practice to avoid using exceptions for flow control.

 Thankfully, python programmers are less dogmatic, and use whatever makes
 sense to use. I hope.

Absolutely. And it doesn't make sense to use exceptions for flow control :-)

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


Re: ANN: Testoob 1.15 released

2009-10-14 Thread Jorgen Grahn
On Thu, 2009-10-08, oripel wrote:
 Testoob is the advanced Python test runner and testing framework that
 spices up any existing unittest test suite.

 Home: http://code.google.com/p/testoob

But this sentence on the home page

The documentation is sadly outdated, but may be
a starting point:

made me stop looking.  As far as I can tell, you cannot even find out
what's so advanced about it (or why advanced is a good thing)
without starting to use it.  A brief comparison with module unittest
(which I am rather unhappy with) would have been nice, too.

/Jorgen

-- 
  // Jorgen Grahn grahn@  Oo  o.   .  .
\X/ snipabacken.se   O  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are there any modules for IRC, that work with Python 3.1?

2009-10-14 Thread Jorgen Grahn
On Sat, 2009-10-10, TerryP wrote:
 Does anyone know of any modules for dealing with the IRC protocol,
 that will work with Python 3.1? It doens't have to be super great,
 just less time consuming then playing with sockets directly (and obv.
 stable). The only module in my systems package manager is irclib for
 Python 2.6. I can live with writing code for Python 2.4+ easily but,
 ahem, I think it would be wise to write new code around Python 3.1
 instead...

Even though it is not widely used yet, and the module you want to use
doesn't support it?  I assume you have installed Python 3.x manually
too (my Debian 'stable' is only at Python 2.5 at the moment -- it
probably takes lots of work to bring in Python 3 without losing
important packages).

Or you can ask the irclib maintainers if they have something. If not,
you can do the work for them, after you have convinced yourself it's
good enough (by starting to use it with Python 2.x).

I don't have any more substantial advice, sorry.

 # circumstances

 Having recently been put into search for a new IRC client, and
 everything I've thrown in the cauldron having become a
 disappointment... let's just say, I've come to a conclusion -- either
 I'm going to install ircII and live with whatever it has to offer(!),
 or hash out something quickly in Python that fits my needs. If I'm
 considering writing an IRC client, it makes sense to check for modules
 implementing the protocol before I have to roll something myself, but
 nothing seems to fit the bill.


 (For those that don't know it, ircII is a really freaking old Internet
 Rely Chat client ;)

I would have thought (given the number of hackers who use it a lot)
there were lots of good IRC clients, but I don't use it myself, so ...

/Jorgen

-- 
  // Jorgen Grahn grahn@  Oo  o.   .  .
\X/ snipabacken.se   O  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against while True: loops

2009-10-14 Thread TerryP
When all is said and done, is not all looping *basically* equivalent
to something like this?

  begin_loop:
unless TEST
  goto end_loop
;; loop body here
if TEST
  goto begin_loop
  end_loop:

Which could likely be used to implement something like:

  while TEST:
# loop body here

in any highly expressive language; which in of it self displays
something about Computer Science.


or am I just talking out my ass?



I've watched this thread with some interest, but really it sounds to
me like the metrics are getting rather lax and this will probably end
up on par with a for (i=0; i  count; i++) versus for (i=0; i  count;
i--) discussion. By that, I mean:


  Fruitful conversation but there is no one spoon for every bowl.

--
  TerryP.
Just Another Programmer.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against while True: loops

2009-10-14 Thread Mensanator
On Oct 14, 2:19�am, Dennis Lee Bieber wlfr...@ix.netcom.com wrote:
 On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator
 mensana...@aol.com declaimed the following in
 gmane.comp.python.general:

  You're not getting away that easy.

  What's YOUR opinion of whilr True?

 � � � � Uhm... that it isn't valid in any language having English influence
 upon it's keywords...

Duh. You DO know that 'r' is next to 'e' on the
keyboard?


 � � � � If anything -- I'd suggest a proposal to add a plain � �loop � �as a
 keyword in Python, whose effect is equivalent to a while True, but a
 break � �must be used to exit said loop (well, we'll ignore raising an
 exception G)

And what will that accomplish? The problem isn't
using while True, it's the fact that you are
escaping the loop. Best Practice is to EXIT the
loop properly, not escape from it.

 --
 � � � � Wulfraed � � � � Dennis Lee Bieber � � � � � � � KD6MOG
 � � � � wlfr...@ix.netcom.com � � HTTP://wlfraed.home.netcom.com/

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


Pyusb

2009-10-14 Thread Ronn Ross
Does anyone know where I can download a copy of PyUSB 1.0? I can only find
0.x versions on sourceforge. I'm following a tutorial that requires 1.0.
Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Why is python so sad?

2009-10-14 Thread Zac Burns
There are 10741 occurences of ): or :( in our source code and only 2
occurrences of :) or (:. Not what you would expect from a language
named after a comedian.

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against while True: loops

2009-10-14 Thread MRAB

Dennis Lee Bieber wrote:

On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator
mensana...@aol.com declaimed the following in
gmane.comp.python.general:


You're not getting away that easy.

What's YOUR opinion of whilr True?


Uhm... that it isn't valid in any language having English influence
upon it's keywords...

If anything -- I'd suggest a proposal to add a plainloopas a
keyword in Python, whose effect is equivalent to a while True, but a
breakmust be used to exit said loop (well, we'll ignore raising an
exception G)


bikesheddingI'd prefer it to be called repeat./bikeshedding
--
http://mail.python.org/mailman/listinfo/python-list


Re: XML-RPC(using SimpleXMLRPCServer) slow on the first call

2009-10-14 Thread Mahi Haile
-- Forwarded message --
 From: Gabriel Genellina gagsl-...@yahoo.com.ar
 To: python-list@python.org
 Date: Wed, 14 Oct 2009 00:52:13 -0300
 Subject: Re: XML-RPC(using SimpleXMLRPCServer) slow on the first call
 En Mon, 12 Oct 2009 18:58:45 -0300, Mahi Haile begin.middle@gmail.com
 escribió:

  Hello all,I have an xml-rpc server running on a machine in the same LAN as
 the client. Both the server and the client are in Python.

 When I have a series of xmlrepc calls from the client to the server, the
 first call usually takes much longer than it should - orders of magnitude.
 The latency is usually sub-10ms on the other calls, but the first call
 takes
 up to 10 seconds or so. This are very simple functions, with almost no
 computation.

 Do you have any ideas?


 I doubt this is a Python problem. I'd look into the network: DNS
 resolution, IPv6 (Windows XP has some timeout issues with IPv6 enabled).

 --
 Gabriel Genellina

 That seems to be correct. The machine is behind a NAT, so that is probably
why. Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why is python so sad?

2009-10-14 Thread Chris Withers

Zac Burns wrote:

There are 10741 occurences of ): or :( in our source code and only 2
occurrences of :) or (:. Not what you would expect from a language
named after a comedian.


def ...(...):
  ...

class ...(...):
  ...

etc

;-)

Chris

--
Simplistix - Content Management, Batch Processing  Python Consulting
   - http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pyusb

2009-10-14 Thread Chris Withers

Ronn Ross wrote:
Does anyone know where I can download a copy of PyUSB 1.0? I can only 
find 0.x versions on sourceforge. I'm following a tutorial that requires 
1.0. Thanks


Googling pyusb gives me loads of hits and the newer versions appear to 
be on about the 3rd link down...


Chris

--
Simplistix - Content Management, Batch Processing  Python Consulting
   - http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list


Re: RotatingFileHandler issue

2009-10-14 Thread Max Lynch
I never got a response back from this, but I'm noticing even more odd
behavior, see inline:

On Wed, Sep 30, 2009 at 4:38 PM, Max Lynch ihas...@gmail.com wrote:

 Hi.
 I have a RotatingFileHandler for my logging system.  I have it set to
 rotate once the file becomes 5MB in size.  Here is the conf line I have in
 my logging config file:

 [handler_fileHandlerDebugNoRequest]
 class=handlers.RotatingFileHandler
 formatter=formatterNoRequest
 args=('/web/logs/gobuzz_debug.log', 'a', 5242880, 8)

 However, my logging folder contains these files:
 -rw-r--r-- 1 www-data www-data 566K Sep 30 16:35 gobuzz_debug.log
 -rw-r--r-- 1 www-data www-data 4.2M Sep 30 16:35 gobuzz_debug.log.1
 -rw-r--r-- 1 www-data www-data 572K Sep 30 16:36 gobuzz_debug.log.2
 -rw-r--r-- 1 www-data www-data 558K Sep 30 16:35 gobuzz_debug.log.3
 -rw-r--r-- 1 www-data www-data 3.7K Sep 29 20:52 gobuzz_debug.log.4
 -rw-r--r-- 1 www-data www-data 3.7K Sep 29 20:52 gobuzz_debug.log.5
 -rw-r--r-- 1 www-data www-data 566K Sep 30 16:36 gobuzz_debug.log.6
 -rw-r--r-- 1 www-data www-data 1.6M Sep 30 16:36 gobuzz_debug.log.7
 -rw-r--r-- 1 www-data www-data  45K Sep 29 20:50 gobuzz_debug.log.8
 -rwxrwxrwx 1 www-data www-data 691K Sep 28 09:39 gobuzz_error.log

 Clearly, the files are rotating far before they hit 5MB.  The consequence
 of such being that I'm losing a lot of log data.  What gives?  Am I doing
 something wrong?


For some reason, my Apache2/mod_wsgi/django system is writing to all of the
separate rotated files at once.  I can't detect a pattern, but some times,
for example, logging data goes into gobuzz_debug.log.8 and some times they
go into gobuzz_debug.log.5, rather than only going to gobuzz_debug.log and
rotating after 5MB.

Does anyone have any ideas? Here are my formatter sections if it matters:
[formatter_formatterNoRequest]
format=%(asctime)s - %(mymodule)s:%(mylineno)d - %(levelname)s - %(message)s
datefmt=%a, %d %b %Y %I:%M:%S %p




 Thanks,
 Max




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


Re: The rap against while True: loops

2009-10-14 Thread Tim Rowe
2009/10/14 Dennis Lee Bieber wlfr...@ix.netcom.com:

        If anything -- I'd suggest a proposal to add a plain    loop    as a
 keyword in Python, whose effect is equivalent to a while True, but a
 break    must be used to exit said loop (well, we'll ignore raising an
 exception G)

And with enough static analysis to guarantee that the break will be
reached? I think it would be a bit much to expect Python to solve the
halting problem!

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


Re: python along or bash combined with python (for manipulating files)

2009-10-14 Thread edexter
On Oct 14, 3:42 am, Jean-Michel Pichavant jeanmic...@sequans.com
wrote:
 Peng Yu wrote:
  Bash is easy to use

 +JOTW

 :)

 JM

why choose..  http://shython.sourceforge.net/
I don't think this is the most recent I would also try the package
index
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python along or bash combined with python (for manipulating files)

2009-10-14 Thread Falcolas
On Oct 13, 10:18 pm, TerryP bigboss1...@gmail.com wrote:
 On Oct 14, 2:13 am, Peng Yu pengyu...@gmail.com wrote:

  Bash is easy to use on manipulating files and directories (like change
  name or create links, etc) and on calling external programs. For
  simple functions, bash along is enough. However, bash does not support
  the complex functions. Python has a richer library that could provide
  support for complex functions (such compute the relative path between
  two paths).

  I'm wondering for a task that can not be done with bash along whether
  it would be better to do in pure python or with a mix of both python
  and bash. What I care is mostly coding speed and a little bit
  maintainability (but not much). Can somebody provide some experience
  on when to combine python and bash and when to use pure python?

 bash can **not** manipulate files and directories beyond things like
 the '' and '' I/O redirections, and some minor loading/saving of
 state data from/to files (command history, directory stack, etc). Most
 of what you refer to are **separate operating system specific
 programs** and have absolutely nothing to do with the shell.

 Very sophisticated scripts are possible using bash and ksh, there is
 even a form of ksh that has tk capabilities! (tksh). The Python and
 Bourne-derived languages are however fundamentally different
 creatures, and use very different data models. You should **not**
 write Python (or Perl) scripts as if they were shell scripts -- doing
 so is very bad practice. When you want a shell script, write a shell
 script. When you write a Python script, write a Python script. It
 really is that simple.

 As a rule of thumb, when you have need of data structures beyond what
 scalar strings and very simple word lists can provide -- you should
 use Python. bash and ksh provide support for arrays, and ksh even has
 dictionaries! (Hashes in Perl speak.) That makes programming in bash/
 ksh more robust then pure sh, but also less portable. The best time to
 use bash is when you require bash specific features, other wise don't
 use bash. The same can be said for ksh.

 When the words array, dictionary, class, object, and/or using multiple
 source files comes to mind when implementing a program - you probably
 want to use Python, Perl, Ruby, or some other general programming
 language, not a shell scripting language like bash.

 You should be cautious to avoid mixing bash and Python code in one
 file.

 If maintainability is not a factor in what you are writing, then you
 should probably not be writing code in any language unless it is the
 language of Mathematics (and even then, maintainability is a wise
 consideration).

 --
   TerryP.
 Just Another Programmer.

With all of Terry's admonitions in mind, Python scripts do integrate
very well as a individual tool within a shell toolchain. With the
multiple command line parsers and the ease of reading stdin and
writing to stdout, it's fairly trivial to make a script which
integrates cleanly into a bash script (or oneliner). It's trivial to
implement a script which will either work with files, or work with
stdin/stdout.

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


Re: Parsing email attachments: get_payload() produces unsaveable data

2009-10-14 Thread dpapathanasiou
On Oct 4, 10:27 am, dpapathanasiou denis.papathanas...@gmail.com
wrote:
 I'm using python to access an email account via POP, then for each
 incoming message, save any attachments.

 This is the function which scans the message for attachments:

 def save_attachments (local_folder, msg_text):
     Scan the email message text and save the attachments (if any)
 in the local_folder
     if msg_text:
         for part in email.message_from_string(msg_text).walk():
             if part.is_multipart() or part.get_content_maintype() ==
 'text':
                 continue
             filename = part.get_filename(None)
             if filename:
                 filedata = part.get_payload(decode=True)
                 if filedata:
                     write_file(local_folder, filename, filedata)

 All the way up to write_file(), it's working correctly.

 The filename variable matches the name of the attached file, and the
 filedata variable contains binary data corresponding to the file's
 contents.

 When I try to write the filedata to a file system folder, though, I
 get an AttributeError in the stack trace.

 Here is my write_file() function:

 def write_file (folder, filename, f, chunk_size=4096):
     Write the the file data f to the folder and filename
 combination
     result = False
     if confirm_folder(folder):
         try:
             file_obj = open(os.path.join(folder, file_base_name
 (filename)), 'wb', chunk_size)
             for file_chunk in read_buffer(f, chunk_size):
                 file_obj.write(file_chunk)
             file_obj.close()
             result = True
         except (IOError):
             print file_utils.write_file: could not write '%s' to
 '%s' % (file_base_name(filename), folder)
     return result

 I also tried applying this regex:

 filedata = re.sub(r'\r(?!=\n)', '\r\n', filedata) # Bare \r becomes \r
 \n

 after reading this post (http://stackoverflow.com/questions/787739/
 python-email-getpayload-decode-fails-when-hitting-equal-sign), but it
 hasn't resolved the problem.

 Is there any way of correcting the output of get_payload() so I can
 save it to a file?

An update for the record (and in case anyone else also has this
problem):

The regex suggested in the StackOverflow post (i.e., filedata = re.sub
(r'\r(?!=\n)', '\r\n', filedata) # Bare \r becomes \r\n) is necessary
but not sufficient.

It turns out that because get_payload() returns a binary stream, the
right way to save those bytes to a file is to use a function like
this:

def write_binary_file (folder, filename, filedata):
Write the binary file data to the folder and filename
combination
result = False
if confirm_folder(folder):
try:
file_obj = open(os.path.join(folder, file_base_name
(filename)), 'wb')
file_obj.write(filedata)
file_obj.close()
result = True
except (IOError):
print file_utils.write_file: could not write '%s' to
'%s' % (file_base_name(filename), folder)
return result

I.e., filedata, the output of get_payload(), can be written all at
once, w/o reading and writing in 4k chunks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against while True: loops

2009-10-14 Thread Ethan Furman

Mensanator wrote:

On Oct 14, 2:19�am, Dennis Lee Bieber wlfr...@ix.netcom.com wrote:


On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator
mensana...@aol.com declaimed the following in
gmane.comp.python.general:



You're not getting away that easy.



What's YOUR opinion of whilr True?


� � � � Uhm... that it isn't valid in any language having English influence
upon it's keywords...



Duh. You DO know that 'r' is next to 'e' on the
keyboard?


Not on mine -- it's next to 'o' and 'u'.  :-)  Go Dvorak!


� � � � If anything -- I'd suggest a proposal to add a plain � �loop � �as a
keyword in Python, whose effect is equivalent to a while True, but a
break � �must be used to exit said loop (well, we'll ignore raising an
exception G)



And what will that accomplish? The problem isn't
using while True, it's the fact that you are
escaping the loop. Best Practice is to EXIT the
loop properly, not escape from it.


I don't think anyone's arguing the opposite.  What I *am* seeing argued 
is if it's the only correct way to do it, and that anyone who does it 
any other way is a scoundrel and a knave.  ;-)


For what it's worth, most of my loops run to completion, with no sign of 
a break anywhere.  Some have a break, and use it.  Some, even, (dare I 
say it?) use break *and* else!  And it's awesome!  Go Python!  :-D


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


win32com.client import problem : solved

2009-10-14 Thread Threader Slash
-- Forwarded message --
From: Dave Angel da...@ieee.org
To: Threader Slash threadersl...@gmail.com
Date: Wed, 14 Oct 2009 07:04:21 -0400
Subject: Re: win32com.client import problem
Threader Slash wrote:

 Hi Everybody,

 I have 2 imports:

 import pythoncom
 from win32com.client import Dispatch

 if I run it on my Python 2.6 Console, it works nicely. However, when I go
 to
 Eclipse IDE, open a project, open a main.py file, and try run, it gives the
 error:

 import pythoncom
 ImportError: No module named pythoncom

 All other imports are working ok on Eclipse IDE -- e.g. import MySQLdb.

 Any suggestion about what is missing?

 All comments and suggestion are welcome.

 ThreaderSlash



Two things to check, python version (sys.version), and sys.path.   Add
prints for the two of them at the beginning of your script, and try the
script in both environments.  If there are any differences, figure out how
to reconfigure Eclipse to match what you've got at the console.


DaveA


--  --  --  --

-- hope this can help and save time for others too

Here is what did and works:
* copy the file mfc71.dll on windows\system32
* copy the same also on directories
* copy the same to your directories Python26\DLLs and
Python26\lib\site-packages\win32
* go to preferences : pydev : interpreter python : remove all the
interpreter you have there. apply, ok.
then add python 2.6 again, apply, ok.

It will do the trick...|:0), ThreaderSlash

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


Re: Why is python so sad?

2009-10-14 Thread Dotan Cohen
 ;-)


Be careful, that looks dangerously close to PHP:
function();


-- 
Dotan Cohen

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


Re: Why is python so sad?

2009-10-14 Thread James Matthews
This posting just made my day!

On Wed, Oct 14, 2009 at 2:59 PM, Dotan Cohen dotanco...@gmail.com wrote:

  ;-)
 

 Be careful, that looks dangerously close to PHP:
 function();


 --
 Dotan Cohen

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




-- 
http://www.goldwatches.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python performance on Solaris

2009-10-14 Thread James Matthews
I use python in almost the same environment. I use it on Joyent and on the
Rackspace cloud. Joyent is faster for a few reasons (cpu bursting and faster
disks) but these aren't real benchmarks until they are on the same machines.


James

On Wed, Oct 14, 2009 at 9:59 AM, Jorgen Grahn
grahn+n...@snipabacken.segrahn%2bn...@snipabacken.se
 wrote:

 On Wed, 2009-10-14, Antoine Pitrou wrote:
  inaf cem.ezberci at gmail.com writes:
 
  Good point. I failed to compare the CPU power on these machines.. 32
  bit linux box I have is 2666 Mhz vs the Solaris zone is 1415 Mhz.. I
  guess that explains :) Thank you for the tip..
 
  You have to compare not only CPU frequencies but the CPU models.

 Yes, at least that.  Megahertz figures have been useless for decades,
 except in advertising.

  Recently Sun has been selling CPUs optimized for multi-threading (e.g.
 the
  UltraSPARC T2 or Niagara CPUs) which have, by design, very poor
  single-threaded performance. If your Solaris zone uses such a CPU then a
 6-8x
  difference in single-threaded performance compared to a modern Intel
  or AMD CPU
  is totally expected.

 (Had to Google it. A Solaris Zone is apparently some kind of
 virtualization thing, with low CPU overhead.)

 s/multi-threading/multi-programming/ I suppose. I certainly hope you
 can still get performance while running many separate true processes in
 parallel.

 /Jorgen

 --
  // Jorgen Grahn grahn@  Oo  o.   .  .
 \X/ snipabacken.se   O  o   .
 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
http://www.goldwatches.com

http://www.jewelerslounge.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against while True: loops

2009-10-14 Thread Jorgen Grahn
On Mon, 2009-10-12, RDrewD wrote:
...
 I was a bit surprised that nobody in this discussion so far bantered
 around the phrase loop invariant, but then I looked in
 http://en.wikipedia.org/wiki/Loop_invariant and found it was draped in
 so much formalism that it's sure to put off all but the most dedicated
 of Computer Science fans.

Haven't read it. But much of the CS parts of the Wikipedia sucks, and
whoever writes there doesn't own the trademark on loop invariants
anyway.

IME, a loop invariant is a simple and useful tool for thinking about
the correctness of code. Class invariants (or whatever they are called)
are even better.

 I haven't been in college in 35 years, so
 I'll admit to being rusty on this, but as I remember it, any time we
 wrote a loop, we were expected to be able to say what the loop
 invariant is.

Yes, it's as simple as that.

 my_prissy_little_indicator_variable = true
 while (my_prissy_little_indicator_variable){
 body
 }
 isn't satisfying because it doesn't guard the body with any
 assurance that the loop invariant will be true before you enter into
 that block of code.

Why not? To me, it obviously does.

It would also help if you didn't use intentionally meaningless and
annoying variable names in your examples. In reality you would have a
meaningful expression like not inputqueue.empty() or
time()  deadline or something.

/Jorgen

-- 
  // Jorgen Grahn grahn@  Oo  o.   .  .
\X/ snipabacken.se   O  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against while True: loops

2009-10-14 Thread Jorgen Grahn
On Mon, 2009-10-12, Grant Edwards wrote:
 On 2009-10-12, Gabriel Genellina gagsl-...@yahoo.com.ar wrote:

 my_prissy_little_indicator_variable = true
 while (my_prissy_little_indicator_variable){
 body
 }
 isn't satisfying because it doesn't guard the body with any
 assurance that the loop invariant will be true before you enter into
 that block of code.

 I think you meant the other way; the above is the simplest loop case, with  
 the test at the start.

 Except the test at the start is meaningless when it comes to
 reading the code and troubleshooting.  What counts are
 assignments to my_prissy_little_indicator_variable inside the
 loop.  And those aren't really any easier to spot that break
 statements.

It's a red herring.  A good loop tends to *not* have a boolean
variable as the while ... expression.  That smells like flag
programming, and if I cannot come up with anything better that that, I
often prefer a while 1 with breaks in it.

For a real-life loop, see for example

  http://en.wikipedia.org/wiki/Binary_search#Iterative

(except it confuses me because it's a repeat ... until and it's in
Pascal with that quaint 1-based indexing)

/Jorgen

-- 
  // Jorgen Grahn grahn@  Oo  o.   .  .
\X/ snipabacken.se   O  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


where's self.assertMatch (for Django)?

2009-10-14 Thread Phlip

Hypo Nt:

Been a while, now I'm back, and the first helpless question is...

When I google (including codesearch) for assertMatch, I get a mishmash of 
opinions. Am I missing the One True assertMatch(), or isn't there one and I 
gotta write it? or copy and use one of the pretenders?


Distractingly-yrs,

--
  Phlip
  http://zeekland.zeroplayer.com/Pigleg_Too/1
--
http://mail.python.org/mailman/listinfo/python-list


how to handle broken pipes

2009-10-14 Thread Igor Mikushkin
Hello all!

Could anyone please say me what is the right way to handle broken
pipes in Python?
I can wrap all my print statements with try/except blocks but it looks
like overkill for me.

I'm using my Python script this way: my_script | less
The script produces a lot of data.
So usually when I find what I'm looking for and press 'q' script is
still writing something.

Thanks in advance,
Igor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against while True: loops

2009-10-14 Thread Jorgen Grahn
On Wed, 2009-10-14, Marco Mariani wrote:
 Dennis Lee Bieber wrote:

  One thing to note is that break ONLY exits the innermost loop --
 Ada adds the confusion that one could define a label on the loops, and
 have the innermost use
  exit outer_label [when condition]
 
 
  THAT I find scary... Since you have to match the label name to
 something that occurs somewhere prior to the exit, and THEN have to
 find the end of that loop.

 But we have exceptions. And I know somebody, in other languages, thinks 
 it's a Best Practice to avoid using exceptions for flow control.

A lot of C++ programmers think so, and Stroustrup himself says
exceptions are for exceptional things or something to that effect.
Is that what you're thinking of?

Thankfully, Stroustrup doesn't use the dreaded phrase Best Practice,
which as far as I can tell is designed to shut down rational thought
in the audience.

 Thankfully, python programmers are less dogmatic, and use whatever makes 
 sense to use. I hope.

Calling it dogmatic is unfair.  C++ is very different from Python,
and has a different implementation of exceptions. You also tend to use
the language to solve a different set of problems.

That said, I still don't fully understand the rationale behind that
advice or rule ... so I'm willing to break it, and sometimes I do.

/Jorgen

-- 
  // Jorgen Grahn grahn@  Oo  o.   .  .
\X/ snipabacken.se   O  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against while True: loops

2009-10-14 Thread Raymond Hettinger
 And I know somebody, in other languages, thinks
 it's a Best Practice to avoid using exceptions for flow control.

Ah, now we have two code prions in just one thread.
I hope no newbie or supervisor reads this thread and latches
on to those two counter-productive ideas.

ISTM, both ideas are dangerous contagious because they are
true in some limited contexts but useless (and harmful)
when applied to programming in general.

IIRC, the C++ admonition against using exceptions for flow control
was rooted in performance concerns specific to that language and
its compilers.  It was not stylistic advice and did not deny that
flow control exceptions could provide elegant solutions to some
programming challenges.

Python's IndexError and KeyError are all about flow control.
The notion is deeply embedded in the language and it would
be a disservice to advise people to not use the language as
designed.

Likewise, the use of while True tends to be more important
in Python than in other languages because we can't combine
assignment with a conditional as we can in C.  So instead,
we have this idiom:

while True:
s = f.read(blocksize)
if not s:
break
...

Suggested further reading for those who are interested:

The Little MLer -- a chunk of this book is devoted to showing
how exceptions can simplify code that would otherwise be
somewhat awkward to express (the remainder of the book is devoted
to thinking about types and how to compose program components).

Structured Programming with go to Statements by Donald Knuth
has an in-depth comparative analysis of many different looping
constructs.


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


Re: how to handle broken pipes

2009-10-14 Thread Jorgen Grahn
On Wed, 2009-10-14, Igor Mikushkin wrote:
 Hello all!

 Could anyone please say me what is the right way to handle broken
 pipes in Python?
 I can wrap all my print statements with try/except blocks but it looks
 like overkill for me.

 I'm using my Python script this way: my_script | less
 The script produces a lot of data.
 So usually when I find what I'm looking for and press 'q' script is
 still writing something.

You mean like this on Unix?

  python -c 'while 1: print hello, world'|less

which produces

  Traceback (most recent call last):
File string, line 1, in module
  IOError: [Errno 32] Broken pipe
  
Well, you can catch IOError, examine the errno, and do a sys.exit()
if it's EPIPE. Don't know if it should be sys.exit(0) or sys.exit(1)
though.

Oh, and *now* I see what you wrote at the top:

 I can wrap all my print statements with try/except blocks but it looks
 like overkill for me.

It's overkill if you have to do it for each print. You should always
(IMHO) wrap all your logic inside an object or a function, let's say
foo(). Then you only have to wrap the single call to foo().

There should be an even cleaner way. Mine is kind of ugly (catch,
examine, exit or re-raise) and it also incorrectly catches broken pipes
which aren't related to sys.stdout/stderr.

There is a similar problem with Ctrl-C, by the way -- the user gets a
KeyboardInterrupt exception thrown in his face where other languages
would have exited silently by default.

/Jorgen

-- 
  // Jorgen Grahn grahn@  Oo  o.   .  .
\X/ snipabacken.se   O  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against while True: loops

2009-10-14 Thread Paul Rubin
Raymond Hettinger pyt...@rcn.com writes:
 IIRC, the C++ admonition against using exceptions for flow control
 was rooted in performance concerns specific to that language and
 its compilers.  It was not stylistic advice and did not deny that
 flow control exceptions could provide elegant solutions to some
 programming challenges.

I've been wondering about that.  I know that Java exceptions are
ridiculously expensive but I didn't realize it was so bad with C++.

 Python's IndexError and KeyError are all about flow control.
 The notion is deeply embedded in the language and it would
 be a disservice to advise people to not use the language as
 designed.

Well, usually they're wrapped in iterators etc.

 So instead, we have this idiom:
 
 while True:
 s = f.read(blocksize)
 if not s:
 break
 ...

Well,

   while iter(lambda: f.read(blocksize), ''): 

evolved because of the awkwardness of that idiom...

 The Little MLer -- a chunk of this book is devoted to showing
 how exceptions can simplify code that would otherwise be
 somewhat awkward to express (the remainder of the book is devoted
 to thinking about types and how to compose program components).

Interesting--I've been wanting to look at that book.  I wonder whether
its uses of exceptions could mostly be better handled with coroutines.

 Structured Programming with go to Statements by Donald Knuth
 has an in-depth comparative analysis of many different looping
 constructs.

Found some pdf's: 

 http://scholar.google.com/scholar?cluster=17368311454828547380

Keep in mind that the article is 35 years old though, and is purely
imperative.  Lots of stuff done with cockamamie looping constructs is
more cleanly done with Python generators, itertools, higher-order
functions, etc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Looking for programmer experienced in Python/Django/Git/Unix

2009-10-14 Thread TMChris
Hi everyone,

We're looking for an expert in Python, Django, Git and Unix.  We have
multiple projects needed paid on hourly basis.
Do you have a strong confidence in all of these?
If so, please provide a resume and a little bit about yourself in
regards to these.


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


Re: python performance on Solaris

2009-10-14 Thread inaf
On Oct 14, 7:15 am, Antoine Pitrou solip...@pitrou.net wrote:
 inaf cem.ezberci at gmail.com writes:



  Good point. I failed to compare the CPU power on these machines.. 32
  bit linux box I have is 2666 Mhz vs the Solaris zone is 1415 Mhz.. I
  guess that explains :) Thank you for the tip..

 You have to compare not only CPU frequencies but the CPU models.
 Recently Sun has been selling CPUs optimized for multi-threading (e.g. the
 UltraSPARC T2 or Niagara CPUs) which have, by design, very poor
 single-threaded performance. If your Solaris zone uses such a CPU then a 6-8x
 difference in single-threaded performance compared to a modern Intel or AMD 
 CPU
 is totally expected.

 Regards

 Antoine.

Antonie -- yes, you are right. Even the architecture of the two types
make a difference. I was under the impression that RISC based CPUs did
not need to have a very high clock speed and that they can perform
similarly compared to an x86 processor with higher clock speed. That
is why I was a bit surprised. I guess there could be other factors at
play. That's why I was asking if there are specific things to be done
while compiling Python on Solaris. I found some tips online which led
me to compile it with a different threading lib resulting in slightly
better performance after my original post.

In terms of the processors I have, please see below for details:

Status of virtual processor 40 as of: 10/14/2009 17:13:51
  on-line since 07/23/2009 18:48:21.
  The sparcv9 processor operates at 1415 MHz,
and has a sparcv9 floating point processor.

So I guess this is not one of those you are talking about..

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


Re: windows side-by-side configuration woes on windows HPC

2009-10-14 Thread Nick Touran
I've made enough progress to get my setup working! I attempted to apply the
patch and recompile the packages that complained but got in too deep while
compiling matplotlib on windows (I don't have admin privileges and can't
install the prereqs) so I dug deeper.

I found this: http://blog.kalmbachnet.de/?postid=80 which suggests removing
the publicKeyToken attribute from the manifests to force local DLLs. This
would possibly give the same effect as not embedding the manifests in the
first place using the patch. So I went in, using VS2008, and removed this
attribute from the embedded manifests in python26.dll, python.exe, and the
manifest file in C:\python26. I also copied msvcm90.dll, msvcp90.dll, and
msvcr90.dll from the 9.0.21022.8 folder in c:\Windows\WinSxS into
c:\Python26 W.ith that, matplotlib started working, but pymssql still did
not.

I then renamed _mssql.pyd to _mssql.dll so that VS2008 could recognize the
manifest, removed the publicKeyToken attribute, and renamed it back to
_mssql.pyd, but it still complained. Finally, using depends.exe from the
internet, I noticed msvcr71.dll was required on my local machine, so I tried
copying it over to the remote machine, into the site-packages folder. Bam.
It worked. So while I don't really consider this a solution, it definitely
worked for what I needed. Any other 3rd party modules that complain in the
future in my weird xcopy-deployment will undergo manifest-stripping via
VS2008 and other dependency checking via depends.exe. Yay.

-nick

On Mon, Oct 12, 2009 at 2:41 PM, M.-A. Lemburg m...@egenix.com wrote:

 Nick Touran wrote:
  It is indeed a pain. I would really like a work-around. Matplotlib is
  supposed to be immune to this nowadays but it's not. Nor are some other
  third-party modules. Did they break with the new release? (2.6.3?)

 The main problem appears to be that the the MS VC9 compiler defaults
 to embedding a dependency on the MS VC90 CRT DLL into extension modules:

  dependency
dependentAssembly
  assemblyIdentity type=win32 name=Microsoft.VC90.CRT
 version=9.0.21022.8
 processorArchitecture=x86
 publicKeyToken=1fc8b3b9a1e18e3b/assemblyIdentity
/dependentAssembly
  /dependency

 Unless you have installed the CRT runtime DLLs installed system-wide,
 this will require the DLLs to be installed next to the extension
 module DLL or PYD file... and that even though the Python process
 itself will already have loaded the DLL from the Python directory.

 A work-around is attached to the ticket as patch.

 Even though a fix for distutils is planned in 2.6.4, this type of
 problem will pop up for all kinds of software using VC90-based DLLs
 as plugins, so it's probably better to just install the CRT runtime
 DLLs in the WinSxS directory using the CRT installers:

 x86:

 http://www.microsoft.com/downloads/details.aspx?familyid=A5C84275-3B97-4AB7-A40D-3802B2AF5FC2displaylang=en

 x86_64:

 http://www.microsoft.com/downloads/details.aspx?familyid=BA9257CA-337F-4B40-8C14-157CFDFFEE4Edisplaylang=en

  On Thu, Oct 8, 2009 at 1:12 PM, M.-A. Lemburg m...@egenix.com wrote:
 
  Nick Touran wrote:
  Copying my local copy of Python 2.6 to a Windows HPC 2008 system is
  giving
  dll side-by-side configuration errors for some third-party packages
  (matplotlib, pyMSSQL, in particular). I understand that there is a
  tradition
  of Python supporting XCOPY deployment, and would really like to be able
  to
  just copy my C:\python26 folder to the network drive and have it run on
  the
  server.
 
  I got around a related issue (http://bugs.python.org/issue4566) just
 by
  upgrading to 2.6.3 and was able to import socket and mpi4py and
  everything,
  except matplotlib and pyMSSQL, that is.
 
  I also understand that if I were to install the MS Visual Studio 2008
  redistribution package on the server that everything would be fine
  because
  the modules just can't find the proper C run-time DLL. The problem with
  that
  is two-fold: I don't have admin rights on the machine and there are
 over
  1000 machines on the cluster and I don't think the admin is going to
  install
  that on all of them.
 
  So is there any way to set an environmental variable or something to
 get
  these packages to know where to find the proper msvcr90.dll, akin to
  setting
  LD_LIBRARY_PATH in Linux? Is there another solution?
 
  I assume this is related to this new problem:
 
 http://bugs.python.org/issue4120
 
  Manifests were meant to solve some of the DLL mess... apparently they
  cause even more grief.
 
  --
  Marc-Andre Lemburg
  eGenix.com
 
  Professional Python Services directly from the Source  (#1, Oct 08 2009)
  Python/Zope Consulting and Support ...http://www.egenix.com/
  mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
  mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/
  
 
  ::: Try our new mxODBC.Connect Python Database 

Re: Poll on Eval in Python

2009-10-14 Thread Kazimir Majorinc

On 14.10.2009 17:55, TerryP wrote:


And what about exec?


(note: exec in python is more in spirit of eval then C-style exec
functions)


I thought about that, but decided not to ask about it
in poll, because I wanted to compare opinions on eval
specifically, not on all similar features. Do you think
it would be better if I asked that? That result would
be significantly different?


--
Kazimir Majorinc
blog: http://kazimirmajorinc.blogspot.com

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


Re: efficient running median

2009-10-14 Thread Janto Dreijer
On Oct 14, 4:53 pm, Peter Otten __pete...@web.de wrote:
 Some numbers:

 10.197 seconds for running_median_scipy_medfilt
 25.043 seconds for running_median_python
 13.040 seconds for running_median_python_msort
 14.280 seconds for running_median_python_scipy_median
 4.024 seconds for running_median_numpy
 0.221 seconds for running_median_insort

 What would be an acceptable performance, by the way?


That's great!
Well, the faster it works, the better. It means I can process more
data before getting frustrated. So if you have a faster version I'd
like to see it :)

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


Re: Help with regex and optional substring in search string

2009-10-14 Thread Timur Tabi
On Wed, Oct 14, 2009 at 10:30 AM, Zero Piraeus sche...@gmail.com wrote:

 '(?:etc)' instead of '(etc)' are non-grouping parentheses (since you
 apparently don't care about that bit).

Ah yes, thanks.

 '[^\]]' instead of '[\w\s]' matches everything except a closing bracket.

I originally had just '[^\]', and I couldn't figure out why it
wouldn't work.  Maybe I need new glasses.

 The '\s*' before the second set of parentheses takes out the leading
 whitespace that would otherwise be returned as part of the match.

And I want that.  The next line of my code is:

description = m.group(2).strip() + \n\n

-- 
Timur Tabi
Linux kernel developer at Freescale
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Poll on Eval in Python

2009-10-14 Thread TerryP
On Oct 14, 9:48 pm, Kazimir Majorinc em...@false.false wrote:
 Do you think
 it would be better if I asked that? That result would
 be significantly different?


Not really. The eval, exec, and compile builtins are more or less
related and serve similar purposes, but don't seem to be highly used
in Python. There's just not a great need of it in many typical
applications. That being said though, a language without the ability
to eval is like dancing with cement shoes ;).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against while True: loops

2009-10-14 Thread Jack Norton

kj wrote:


I'm coaching a group of biologists on basic Python scripting.  One
of my charges mentioned that he had come across the advice never
to use loops beginning with while True.  Of course, that's one
way to start an infinite loop, but this seems hardly a sufficient
reason to avoid the construct altogether, as long as one includes
an exit that is always reached.  (Actually, come to think of it,
there are many situations in which a bona fide infinite loops
(typically within a try: block) is the required construct, e.g.
when implementing an event loop.)

I use while True-loops often, and intend to continue doing this
while True, but I'm curious to know: how widespread is the
injunction against such loops?  Has it reached the status of best
practice?

TIA!

kynn
  
This thread has gotten a lot of posts concerning programming practices 
and dogma alike.  I'd like to add a personal use of `while True:` that 
has nothing to do with either best practices or dogma. 
I use python a *lot* to do day-to-day tasks in an engineering lab.  I 
use it to control, log, or otherwise converse with rs232 based gear, as 
well as use it to back up or organize documents, etc... (lo and behold, 
I use this scripting language to write little scripts here and there).  
Don't get me wrong, I also write full blown control/logging apps with 
python, but that is only 10% of my usage. 
Whenever I need to quickly log something (serial output of a device) 
quickly, I find myself writing this in the python REPL:

import serial
comport = serial.Serial('COMx', timeout=1)
while True:
   get = comport.readline()
   f.open(blah, 'a')
   f.write(get)
   f.close()

It is short enough that I don't see the need to write my own module.  
Sometimes I even add a little regexp based filtering -- which adds 2 
lines total.  When I am done logging I just give 'er a CTRL-C and be 
done with it.  It is also a hell of a lot less buggy and error prone 
than hyperterminal, which my boss uses to do the same thing. 
I think this is a perfect example of `while True:` that works damn well, 
and there isn't anything that can replace its simplicity.  Programming 
practices be damned, it is invaluable, and I would recommend doing it in 
my situation to any person, regardless of programming experience. 
Food for thought.


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


Re: The rap against while True: loops

2009-10-14 Thread Tim Rowe
2009/10/12 RDrewD drewl...@gmail.com:
 I was a bit surprised that nobody in this discussion so far bantered
 around the phrase loop invariant, but then I looked in
 http://en.wikipedia.org/wiki/Loop_invariant and found it was draped in
 so much formalism that it's sure to put off all but the most dedicated
 of Computer Science fans.

I think in this case the loop variant is more use than the loop variant.

Basically, the loop variant is what is true on every pass of the loop.
If you're being formal, you have to show that it's true on entry to
the loop and remains true on every pass of the loop. That means that
on exit from the loop you can guarantee the loop invariant and the
exit condition.

The loop variant is a finite natural number (positive or zero integer)
that is guaranteed to decrease on every pass of the loop. Because a
finite natural number cannot decrease indefinitely, if you can define
a loop variant then you gurantee that the loop will terminate.

Even if you are not being formal, just considering what the loop
variants and invariants can save no end of trouble with tricky loops.

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


Re: efficient running median

2009-10-14 Thread Ethan Furman

Janto Dreijer wrote:

On Oct 13, 7:37 pm, Ethan Furman et...@stoneleaf.us wrote:


Janto Dreijer wrote:


I'm looking for code that will calculate the running median of a
sequence, efficiently. (I'm trying to subtract the running median from
a signal to correct for gradual drift).



My naive attempt (taking the median of a sliding window) is
unfortunately too slow as my sliding windows are quite large (~1k) and
so are my sequences (~50k). On my PC it takes about 18 seconds per
sequence. 17 of those seconds is spent in sorting the sliding windows.



I've googled around and it looks like there are some recent journal
articles on it, but no code. Any suggestions?



Thanks
Janto


You might look athttp://pypi.python.org/pypi/blist/0.9.4

~Ethan~



Very nice! I assume you mean I can use it to quickly insert items into
the sliding window?

Thanks
Janto


I'm afraid I can't help any further.  Going from your post, I thought a 
quicker list implementation might be useful, but beyond that I have no 
knowledge to share.


Who said ignorance is bliss?  *hangs head*

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


Re: The rap against while True: loops

2009-10-14 Thread Rhodri James

On Wed, 14 Oct 2009 02:26:17 +0100, Mensanator mensana...@aol.com wrote:


On Oct 13, 5:38�pm, Rhodri James rho...@wildebst.demon.co.uk
wrote:
On Tue, 13 Oct 2009 22:59:04 +0100, Mensanator mensana...@aol.com  
wrote:

 And I'm not saying John nor the OP should stop
 using what works for them. But there are certainly
 valid reasons for don't use while True to be
 on the Best Practices list.

Unfortunately, some of them seem to be reasons from
my point of view to put *do* use while True on the
Best Practices list. �


Really? Which ones?


Some of the constructs you
seem to like ring big alarm bells with me, because
I've found entirely too many bugs hidden by them.


For example?


Well, this one's always popular:

done = False
while not done:
  do_stuff()
  done = worry_about_stuff()
  do_more_stuff_at_great_length()
  done = worry_about_more_stuff()
  and_so_on()

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


Re: id( ) function question

2009-10-14 Thread Laszlo Nagy



Andre Engels schrieb:
  

What is going on is that a few objects that are often used, in
particular the small (how small is small depends on the
implementation) integers, are 'preloaded'. When one of these is then
referred to, a new object is not created, but the pre-defined object
is used. 10 is apparently a preloaded constant in your implementation,
1e10 is not.

As far as I know, only None is _guaranteed_ to be such a preloaded
object, so one should not rely on it in implementations.



None, True, False, NotImplemented are guaranteed to be singletons, all
builtin types and exceptions can be considered as singletons, too.
  
I thought that different mutable objects always have different ids. If 
this is not true, then what the id() function is used for? What useful 
thing can we do with it?


Thanks,

L

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


Re: The rap against while True: loops

2009-10-14 Thread Steven D'Aprano
On Wed, 14 Oct 2009 09:34:28 -0700, Mensanator wrote:

 On Oct 14, 2:19�am, Dennis Lee Bieber wlfr...@ix.netcom.com wrote:
 On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator
 mensana...@aol.com declaimed the following in
 gmane.comp.python.general:

  You're not getting away that easy.

  What's YOUR opinion of whilr True?

Uhm... that it isn't valid in any language having English
 influence upon it's keywords...
 
 Duh. You DO know that 'r' is next to 'e' on the keyboard?

Only on QWERTY keyboards. Not on Dvorak keyboards.

Do you know how to proof-read your writing before hitting send? If not, 
please learn. A spell checker may help. If you do know how, if you care 
so little for what you write that you can't be bothered, why should 
anyone care enough to read what you write? Either way, there's no call 
for you to be snarky when people call you on stupid typos. Your mistake 
could happen to anyone, but it was still *your* mistake.


[...]
 And what will that accomplish? The problem isn't using while True, it's
 the fact that you are escaping the loop. 

That's not a problem.


 Best Practice is to EXIT the loop properly, not escape from it.

A break does exit the loop properly. That's what it is for, it would be a 
pretty stupid language that had break exit the loop improperly.

Nobody is forcing you to use break. You can write Pascal in any language 
you like.



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


Re: The rap against while True: loops

2009-10-14 Thread Steven D'Aprano
On Wed, 14 Oct 2009 18:30:06 +0100, Tim Rowe wrote:

 2009/10/14 Dennis Lee Bieber wlfr...@ix.netcom.com:
 
        If anything -- I'd suggest a proposal to add a plain    loop
           as a
 keyword in Python, whose effect is equivalent to a while True, but a
 break    must be used to exit said loop (well, we'll ignore raising an
 exception G)
 
 And with enough static analysis to guarantee that the break will be
 reached? I think it would be a bit much to expect Python to solve the
 halting problem!

That's a stupid objection. Python doesn't guarantee that any of the 
following will halt:

for x in iterator:
pass

while flag:
pass

for x in [1, 10, 20, 10**100]:
time.sleep(x)

(Technically, that last one will eventually halt, if you're prepared to 
wait long enough... about a billion trillion trillion trillion trillion 
trillion trillion trillion years.)


Why should Python make that guarantee about this hypothetical loop 
forever construct?





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


Re: The rap against while True: loops

2009-10-14 Thread Steven D'Aprano
On Wed, 14 Oct 2009 20:17:40 +, Jorgen Grahn wrote:

 But we have exceptions. And I know somebody, in other languages, thinks
 it's a Best Practice to avoid using exceptions for flow control.
 
 A lot of C++ programmers think so, and Stroustrup himself says
 exceptions are for exceptional things or something to that effect. Is
 that what you're thinking of?
 
 Thankfully, Stroustrup doesn't use the dreaded phrase Best Practice,
 which as far as I can tell is designed to shut down rational thought in
 the audience.
 
 Thankfully, python programmers are less dogmatic, and use whatever
 makes sense to use. I hope.
 
 Calling it dogmatic is unfair.  C++ is very different from Python, and
 has a different implementation of exceptions. You also tend to use the
 language to solve a different set of problems.
 
 That said, I still don't fully understand the rationale behind that
 advice or rule ... so I'm willing to break it, and sometimes I do.

Setting up a try...except block is cheap in Python. According to my 
tests, the overhead is little more than that of a single pass statement.

But actually raising and catching the exception is not cheap. If you use 
a lot of exceptions for flow control, performance will probably suffer.

In C++, exceptions are expensive, whether you catch one or not.

Also, using exceptions this way is a structured form of GOTO -- it's easy 
to abuse and turn it into spaghetti code. Actually, not that easy to 
abuse, because you can't jump back into the try block. It's more like a 
multi-level break outside of a loop than a general GOTO.




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


  1   2   3   >