reifying indent and dedent into braces

2009-10-13 Thread Rustom Mody
At http://www.secnetix.de/olli/Python/block_indentation.hawk
I find that the python code

>>> if foo:
... if bar:
... x = 42
... else:
...   print foo
...

has its indentation structure made explicit as

  <:>[0]
   <:>   [0, 4]
  <=> <42> [0, 4, 8]
   <:>  [0]
  [0, 2]
  [0]

Is there any api in python for going from 1st to 2nd and back?

Context: I am trying to generate some python code and its
indentation=structure is giving me a headache!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reifying indent and dedent into braces

2009-10-13 Thread Stefan Behnel
Rustom Mody wrote:
> I am trying to generate some python code and its
> indentation=structure is giving me a headache!

Have you considered searching the web for "Python code generator"?

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


Still wrong architecture on Snow Leopard

2009-10-13 Thread Xiao Yu

Hi everyone,

I have Snow Leopard and followed 4.6.1 snapshot 20091010's reference  
3.4 instructions to configure both SIP and PyQt in i386. Everything  
installed fine but when I open Python and tried to import ImageQt, I  
get the following error:

Traceback (most recent call last):
  File "", line 1, in 
  File "/Library/Python/2.6/site-packages/PIL/ImageQt.py", line 20,  
in 

from PyQt4.QtGui import QImage, qRgb
ImportError: dlopen(/Library/Python/2.6/site-packages/PyQt4/QtGui.so,  
2): no suitable image found.  Did find:
	/Library/Python/2.6/site-packages/PyQt4/QtGui.so: mach-o, but wrong  
architecture


What am I still doing wrong? Is there a specific way I have to run  
Python etc?


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


Re: reifying indent and dedent into braces

2009-10-13 Thread Steven D'Aprano
On Tue, 13 Oct 2009 12:35:13 +0530, Rustom Mody wrote:

> At http://www.secnetix.de/olli/Python/block_indentation.hawk I find that
> the python code
> 
 if foo:
> ... if bar:
> ... x = 42
> ... else:
> ...   print foo
> ...
> 
> has its indentation structure made explicit as
> 
>   <:>[0] 
><:>   [0, 4]
>   <=> <42> [0, 4, 8]
><:>  [0] 
>   [0, 2]
>   [0]


Have you looked at the tokenize module?

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




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


Re: Still wrong architecture on Snow Leopard

2009-10-13 Thread Ned Deily
In article <[email protected]>,
 Xiao Yu  wrote:
> I have Snow Leopard and followed 4.6.1 snapshot 20091010's reference  
> 3.4 instructions to configure both SIP and PyQt in i386. Everything  
> installed fine but when I open Python and tried to import ImageQt, I  
> get the following error:
> Traceback (most recent call last):
>File "", line 1, in 
>File "/Library/Python/2.6/site-packages/PIL/ImageQt.py", line 20,  
> in 
>  from PyQt4.QtGui import QImage, qRgb
> ImportError: dlopen(/Library/Python/2.6/site-packages/PyQt4/QtGui.so,  
> 2): no suitable image found.  Did find:
>   /Library/Python/2.6/site-packages/PyQt4/QtGui.so: mach-o, but wrong  
> architecture
> 
> What am I still doing wrong? Is there a specific way I have to run  
> Python etc?

The Apple-supplied Python 2.6 in Snow Leopard runs by default in 64-bit 
mode.  If the PyQt4 was only built for i386, i.e. 32-bit mode, try 
forcing Apple python to run in 32-mode by either setting an environment 
variable:

# one-time
VERSIONER_PYTHON_PREFER_32_BIT=yes /usr/bin/python

# always in this session (or add to shell profile)
export VERSIONER_PYTHON_PREFER_32_BIT=yes
/usr/bin/python

or you can use the defaults command to permanently prefer 32-bit.  See 
Apple's python man page for details.

-- 
 Ned Deily,
 [email protected]

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


Where to find pexpect

2009-10-13 Thread Antoon Pardon
I have been looking for pexpect. The links I find like
http://pexpect.sourceforge.net all end up at
http://www.noah.org/wiki/Pexpect which produces a 404 not
found problem.

Does someone know the current location?

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


Re: The rap against "while True:" loops

2009-10-13 Thread John Reid



Mensanator wrote:

Nothing wrong with a having a break IMHO.


My opinion is that there is everything wrong with
having a break. I don't think I have ever used one,
I write code that doesn't depend on that crutch.
I guess its crutch-iness is in the eye of the beholder. You seem to have 
a dogmatic view about this.





while not done:

seems very dangerous to me as you'd have to

del done

before writing the same construct again. That's the sort of thing that
leads to errors.


Duh. I won't write silly code like that either.
If I need more than one loop structure then I'll
do something like

while not done_with_this


while not done_with_that

This is neither clean or well scoped.



Besides, since I _always_ initialize the flag
before entering a loop, the flag can be reused
and doesn't have to be deleted (as long as the
loops aren't nested). And since I don't use goto,
there's no chance the initialization can be avoided.
Initialising the flag is just another line of code that has to be 
interpreted later. I didn't notice the initialisation in your original post.




The best way to avoid the pitfalls of spaghetti
code is to not write it in the first place.

I agree. With 'break' it is obvious what the code does and there are 
fewer lines to write in the first place and comprehend in the second.


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


twill unicode related

2009-10-13 Thread james27

hello
im learning twill,but have some problem with unicode.
whenever i use twill's follow function which emulate webbrowser link click
function,it can work well with 
english link,but can't work with unicode.
does anybody some know about this probelm? 
thank in advance
follow is sample

# -*- coding: cp949 -*-
import twill.commands as tc

tc.go("http://searchc.naver.com/pw/";)
tc.info()
tc.follow("unicode")
-- 
View this message in context: 
http://www.nabble.com/twill-unicode-related-tp25869251p25869251.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


RE: How to run python script in emacs

2009-10-13 Thread Sells, Fred
Here is the .emacs file I place at c:\ on xp.  I don't understand it and
cannot explain it.  It was developed by a few guys I worked with 20
years ago and still does the job.  Probably quite obsolete by now, but
if it ain't broke...
In response to your "what do you mean"
With the cursor in a python buffer (and the mode must say python).  Hold
down the control key and hit the "c" key twice.  
--
(setq initial-major-mode 'c-mode)
(setq text-mode-hook 'turn-on-auto-fill)
(setq-default indent-tabs-mode nil)
(global-set-key "\C-z" 'narten-suspend-emacs)
(global-set-key "\C-_" 'help-command)
(setq help-char 31)
(define-key global-map "\C-h" 'backward-delete-char-untabify)
(global-set-key "\C-x\C-e" 'compile)
(global-set-key "\C-x1" 'my-delete-other-windows)
(setq manual-program "man")
(setq manual-formatted-dir-prefix (list "/usr/man/cat"
"/usr/local/X11R4/man"))
(setq manual-formatted-dirlist (list
"/usr/man/cat1" "/usr/man/cat2" "/usr/man/cat3" "/usr/man/cat4"
"/usr/man/cat5" "/usr/man/cat6" "/usr/man/cat7" "/usr/man/cat8"
"/usr/man/catl" "/usr/man/catn" "/usr/local/X11R4/man/catn"
"/usr/local/X11R4/man/cat3" ))
(global-set-key "\em" 'manual-entry)
(global-set-key "\eg" 'goto-line)
(global-set-key "\C-xb" 'my-switch-to-buffer)
(global-set-key "\C-m" 'newline-and-indent)
(global-set-key "\C-q" 'electric-buffer-list)
(global-set-key "\C-x\C-b" 'buffer-menu)
(global-set-key "\C-x\C-y" 'cd)
(global-set-key "\es" 'shell)
(global-set-key "\C-xd" 'display-filename)
(global-set-key "\C-i" 'narten-do-a-tab)
(global-set-key "\C-x\C-b" 'buffer-menu)
(global-set-key "\C-_\C-_" 'help-for-help)
(global-set-key "\C-_\C-a" 'apropos)
(global-unset-key "\C-_\C-c")
(global-unset-key "\C-_\C-d")
(global-unset-key "\C-_\C-n")
(global-unset-key "\C-_\C-w")
(defun my-delete-other-windows ()
  (interactive)
  (delete-other-windows)
  (recenter))
(defun narten-suspend-emacs ()
  (interactive)
  (save-all-buffers)
  (suspend-emacs))
(defun narten-do-a-tab ()
  (interactive)
  (cond ((looking-at "^") 
 (progn (delete-horizontal-space)
(indent-relative)))
((looking-at "[ \t]*")
 (tab-to-tab-stop)))
  (let ((beg (point)))
(re-search-backward "[^ \t]")
(tabify (point) beg))
  (re-search-forward "[ \t]+"))
(defun display-filename ()
  (interactive)
  (message buffer-file-name))
(defun save-all-buffers ()
  (interactive)
  (save-some-buffers 1)
  (message "done!"))
(defun my-switch-to-buffer () 
  "switch to buffer, using completion to prevent bogus buffer names from
being given"
  (interactive)
  (switch-to-buffer (read-buffer "Switch to buffer: " (other-buffer)
"t")))
;;;
;;; GNUS stuff
;;;
(setq gnus-nntp-server "astro")
(setq gnus-your-domain "sunrise.com")
(setq gnus-your-organization "Sunrise Software International")

(setq display-time-day-and-date t)
(setq display-time-no-load t)
(setq display-newmail-beep t)
(display-time)
;;(setq-default tab-width 4 )fred

(put 'narrow-to-region 'disabled nil)


(put 'narrow-to-page 'disabled nil)

(put 'insert-file 'disabled nil)

(autoload 'python-mode "python-mode" "" t)
(setq auto-mode-alist
  (cons '("\\.py$" . python-mode) auto-mode-alist))
;;(my-delete-other-windows)
;;(electric-buffer-list)

;;(cond (window-system
;;   (setq hilit-mode-enable-list  '(not text-mode)
;; hilit-background-mode   'light
;; hilit-inhibit-hooks nil
;; hilit-inhibit-rebinding nil)
;;
;;   (require 'hilit19)
;;   ))

;;
;; Hilit stuff
;;
;;(cond (window-system
;;   (setq hilit-mode-enable-list  '(not text-mode)
;; hilit-background-mode   'light
;; hilit-inhibit-hooks nil
;; hilit-inhibit-rebinding nil)
;;
;;   (require 'hilit19)
;;   ))
;;(require 'paren)

(setq-default transient-mark-mode t)

;;(electric-buffer-menu-mode)
(my-delete-other-windows)


(put 'erase-buffer 'disabled nil)

(put 'upcase-region 'disabled nil)

--
[**CONFIDENTIALITY NOTICE**]: The information contained in this message may be 
privileged and / or confidential and protected from disclosure. If the reader 
of this message is not the intended recipient, you are hereby notified that any 
dissemination, distribution or copying of this communication is strictly 
prohibited. If you have received this communication in error, please notify the 
sender immediately by replying to this message and deleting the material from 
any computer.
-- 
http://mail.python.org/mailman/listinfo/python-list


Form Value Won't Post/Submit

2009-10-13 Thread SuperMetroid
The html code of the form, and my code are below. I can't get the
value to post/submit.. instead I get an error. Can anyone help?

HTML Code of Form:






Grant badge: 


My Code:
opener = urllib.request.build_opener()
cj = http.cookiejar.MozillaCookieJar()
cj.load('C:/Users/Alison/Documents/moz_cookies.txt')
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor
(cj))

params = urllib.parse.urlencode({'grant-userid' : 'Guest_xLolKittyx'})
form = urllib.request.OpenerDirector.open('http://www.imvu.com/catalog/
web_manage_badges.php?action=grant-revoke&creator_badge_index=1',
params)
data = form.read()
form.close()
print(data)

Error Message:
Traceback (most recent call last):
  File "C:\Python31\htmlparser.py", line 34, in 
form = urllib.request.OpenerDirector.open('http://www.imvu.com/
catalog/web_manage_badges.php?action=grant-
revoke&creator_badge_index=1', params)
  File "C:\Python31\lib\urllib\request.py", line 332, in open
req = Request(fullurl, data)
  File "C:\Python31\lib\urllib\request.py", line 174, in __init__
self._parse()
  File "C:\Python31\lib\urllib\request.py", line 179, in _parse
raise ValueError("unknown url type: %s" % self.full_url)
ValueError: unknown url type: grant-userid=Guest_xLolKittyx
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: It Doesn't Add Up!

2009-10-13 Thread Stephen Hansen
On Mon, Oct 12, 2009 at 11:33 AM, Victor Subervi wrote:

> for row in data:
>   i += 1
>   total = 0
>   quantity = form.getfirst('order_' + str(i), '')
>   if quantity != '':
> sql = 'select * from products p join %s c on p.ID=c.ID where
> c.ID=%s;' % (client, str(i))
> cursor.execute(sql)
> stuff = cursor.fetchone()
> price = str(int(stuff[5]*100))
> price = price[0:len(price)-2] + '.' + price[-2:]
> item, price, description, discount = stuff[2], price, stuff[3],
> stuff[8]
> order += 'Item #: ' + item + '\tQuantity: ' + quantity + '\tPrice:
> ' + price + '\tDiscount: ' + str(discount) + '\tDescription: ' +
> description[:20] + '...\n'
> print 'Total 1: %s' % total
> total += float(price) * int(quantity) * (100 - discount)/100
> print 'Total 2: %s' % total
>   order += 'TOTAL: $' + str(total)
>
>
First, consider using the "decimal" module and not floats for money, so you
don't get weird rounding. Floats are inexact. People usually prefer money to
be exact.

Second, you initialize "total" to 0, and before "Total 1" is printed out--
you're never changing it unless I'm blind. You change it the first time
below "Total 1".

Finally, consider being explicit with the "SELECT  FROM "
and not using *. It lets you semi-future-proof your code verses future table
changes, and is easier to maintain as you see in the code what order things
come out and don't have to remember the order in the table itself.

HTH,

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


Re: Work around metaclass programming

2009-10-13 Thread Diez B. Roggisch

Zac Burns schrieb:

I have a class called Signal which is a descriptor. It is a descriptor
so that it can create BoundSignals, much like the way methods work.
What I would like to do is to have the class be a descriptor when
instantiated in what will be the locals of the class, but not a
descriptor everywhere else.

Can I do this without requiring the class to use a special metaclass
that does the transformation?

One way I can think of which would work if there is such a thing would
be a hook that type will call on the value of locals if that value is
defined. That way I could just return the descriptor from that hook.
The advantage of this over using a metaclass is that these are
combinable.

Am I making sense?


Not to me. What do you mean by "locals of the class", and how is a class 
to be instantiated in them? Can you describe what your *usecase* for 
this is?


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


Re: Where to find pexpect

2009-10-13 Thread Javier Collado
Hello,

Google is your friend:
http://sourceforge.net/projects/pexpect/
http://sourceforge.net/projects/pexpect/files/pexpect/Release%202.3/pexpect-2.3.tar.gz/download

Best regards,
Javier

2009/10/13 Antoon Pardon :
> I have been looking for pexpect. The links I find like
> http://pexpect.sourceforge.net all end up at
> http://www.noah.org/wiki/Pexpect which produces a 404 not
> found problem.
>
> Does someone know the current location?
>
> --
> Antoon Pardon
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: An assessment of Tkinter and IDLE

2009-10-13 Thread Ben Finney
TerryP  writes:

> One thing you should also learn about me, is I do not deal in
> absolutes.

What, *never*? That's a pretty uncompromising position to h—

I'll get my coat.

-- 
 \ “Smoking cures weight problems. Eventually.” —Steven Wright |
  `\   |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against "while True:" loops

2009-10-13 Thread Grant Edwards
On 2009-10-12, Gabriel Genellina  wrote:

>> my_prissy_little_indicator_variable = true
>> while (my_prissy_little_indicator_variable){
>> 
>> }
>> isn't satisfying because it doesn't guard the  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.

-- 
Grant Edwards   grante Yow! I want to read my new
  at   poem about pork brains and
   visi.comouter space ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What do I do now?

2009-10-13 Thread n00m

I'm going to develop further my py. script for
text detection and localization in raster images:
http://funkybee.narod.ru/

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


Re: In python CGI, how to pass "hello" back to a javascript function as an argument at client side?

2009-10-13 Thread Bruno Desthuilliers

zxo102 a écrit :

Hi everyone,
How can I pass a string generated from python cgi at server side
to a
javascript function as an argument at client side?



This is common HTTP / javascriot stuff - nothing related to Python. 
First learn about the HTTP protocol - something you obviously need if 
doing web development -, then eventually google for XMLHttpRequest (or 
'ajax').


To make a long story short: the client side doesn't give a damn about 
how the server-side works - it requests an url (-> read the part about 
"HTTP Request" in the rfc), waits for the response (-> read the part 
about "HTTP Response" in the rfc), and whatever it wants with the 
response. FWIW, HTTP requests can have a "query string" containing 
arguments.


From the server-side POV, your CGI script doesn't "return" anything - 
it generates a HTTP response, which will be sent back to the client by 
Apache.

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


Re: Form Value Won't Post/Submit

2009-10-13 Thread Piet van Oostrum
> SuperMetroid  (S) wrote:

>S> The html code of the form, and my code are below. I can't get the
>S> value to post/submit.. instead I get an error. Can anyone help?

>S> HTML Code of Form:
>S> 
>S> 
>S> 

>S> S> value='92dcd92a8bc16f73f330d118ae1ed891' />
>S> 
>S> Grant badge: S> type='text' id='grant-userid' name='grant-userid' value='userid /
>S> avatar name' />
>S> 

>S> My Code:
>S> opener = urllib.request.build_opener()
>S> cj = http.cookiejar.MozillaCookieJar()
>S> cj.load('C:/Users/Alison/Documents/moz_cookies.txt')
>S> opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor
>S> (cj))

>S> params = urllib.parse.urlencode({'grant-userid' : 'Guest_xLolKittyx'})
>S> form = urllib.request.OpenerDirector.open('http://www.imvu.com/catalog/
>S> web_manage_badges.php?action=grant-revoke&creator_badge_index=1',
>S> params)

You are mixing GET-type (indicated by ? in the URL) and POST-type
parameters. Put the action and creator_badge_index parameters also in
the dictionary. And probably you need to provide the other hidden fields
from the form also.

Something like (untested):

paramdict = {
  'action': 'grant-revoke',
  'creator_badge_index': '1',
  'token': '92dcd92a8bc16f73f330d118ae1ed891',
  'do-grant': '1',
  'grant-userid' : 'Guest_xLolKittyx',
  }
params = urllib.parse.urlencode(paramdict)
url = 'http://www.imvu.com/catalog/web_manage_badges.php'
form = urllib.request.OpenerDirector.open(url, params)
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: In python CGI, how to pass "hello" back to a javascript function as an argument at client side?

2009-10-13 Thread Piet van Oostrum
> zxo102  (z) wrote:

>z> Hi everyone,
>z> How can I pass a string generated from python cgi at server side
>z> to a
>z> javascript function as an argument at client side?

>z>  I want test.py to "return"  a "hello" back so the javascript function
>z> load takes "hello" as argument like  load("hello").

>z> 2. server side: test.py
>z> ...
>z> #!c:\python24\python.exe

>z> def main():
>z>message = 'hello'
>z>#return message

>z> main()
>z> ...

>z> Any ideas?

CGI scripts return stuff by printing it, or more generally writing to
stdout.

So print message should do. The rest is not a Python question but a
Javascript question.
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


RabbitMQ vs ApacheQpid (AMQP)

2009-10-13 Thread jacopo
Short question:
I am considering two solutions for a distributed system:  either
RabbitMQ with py-amqplib or ApacheQpid with its own set of API. I
wander is any of you would be able to compare the two. In particular,
I would expect that since Apache comes with its own documented Python
API this would be sufficient to put it as a first choice.

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).


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


does anyone know how to use libjeg from within memory in python

2009-10-13 Thread Chris Colbert
Say I use python to talk to a wireless webcamera that delivers images
via http requests.

I request an image and read it into a buffer, but the image is in jpeg format.

I would like to convert this to a simple RGB format buffer to pass to
numpy. Has anyone managed this using libjpeg or any other lib?

Cheers!

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


Re: does anyone know how to use libjeg from within memory in python

2009-10-13 Thread Stefan Behnel
Chris Colbert wrote:
> Say I use python to talk to a wireless webcamera that delivers images
> via http requests.
> 
> I request an image and read it into a buffer, but the image is in jpeg format.
> 
> I would like to convert this to a simple RGB format buffer to pass to
> numpy. Has anyone managed this using libjpeg or any other lib?

According to the docs, the stdlib jpeg module has been removed in Py3:

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

But since you are processing images anyway, what about using an image
processing library like PIL or ImageMagick?

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


Re: does anyone know how to use libjeg from within memory in python

2009-10-13 Thread Chris Colbert
In answering my own question, this can be done trivially with PIL.
Here is a self contained example:


In [1]: import httplib

In [2]: from PIL import ImageFile

In [3]: import numpy as np

In [4]: conn = httplib.HTTPConnection('www.therealstevencolbert.com')

In [5]: conn.request('GET', '/dump/IMG_0408_rs.JPG')

In [6]: r1 = conn.getresponse()

In [7]: r1.status
Out[7]: 200

In [8]: data = r1.read()

In [9]: parser = ImageFile.Parser()

In [10]: parser.feed(data)

In [11]: img = parser.close()

In [12]: img.show()

In [13]: numpyimg = np.asarray(img)

In [14]: numpyimg.shape
Out[14]: (768, 1024, 3)

In [15]: numpyimg.dtype
Out[15]: dtype('uint8')



On Tue, Oct 13, 2009 at 11:51 AM, Chris Colbert  wrote:
> Say I use python to talk to a wireless webcamera that delivers images
> via http requests.
>
> I request an image and read it into a buffer, but the image is in jpeg format.
>
> I would like to convert this to a simple RGB format buffer to pass to
> numpy. Has anyone managed this using libjpeg or any other lib?
>
> Cheers!
>
> Chris
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: does anyone know how to use libjeg from within memory in python

2009-10-13 Thread Chris Colbert
Heh, for whatever reason, your post is dated earlier than my response,
but wasn't here when I sent mine. But yeah, PIL worked.

On Tue, Oct 13, 2009 at 12:04 PM, Stefan Behnel  wrote:
> Chris Colbert wrote:
>> Say I use python to talk to a wireless webcamera that delivers images
>> via http requests.
>>
>> I request an image and read it into a buffer, but the image is in jpeg 
>> format.
>>
>> I would like to convert this to a simple RGB format buffer to pass to
>> numpy. Has anyone managed this using libjpeg or any other lib?
>
> According to the docs, the stdlib jpeg module has been removed in Py3:
>
> http://docs.python.org/library/jpeg.html
>
> But since you are processing images anyway, what about using an image
> processing library like PIL or ImageMagick?
>
> Stefan
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: does anyone know how to use libjeg from within memory in python

2009-10-13 Thread Dave Angel

Chris Colbert wrote:

Say I use python to talk to a wireless webcamera that delivers images
via http requests.

I request an image and read it into a buffer, but the image is in jpeg format.

I would like to convert this to a simple RGB format buffer to pass to
numpy. Has anyone managed this using libjpeg or any other lib?

Cheers!

Chris

  

You probably want to use PIL

http://www.pythonware.com/products/pil/


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


Re: does anyone know how to use libjeg from within memory in python

2009-10-13 Thread Chris Colbert
My emails must not be making it to list... I posted a solutions about
10 minutes after I posted the questions.

Thanks!

On Tue, Oct 13, 2009 at 12:35 PM, Dave Angel  wrote:
> Chris Colbert wrote:
>>
>> Say I use python to talk to a wireless webcamera that delivers images
>> via http requests.
>>
>> I request an image and read it into a buffer, but the image is in jpeg
>> format.
>>
>> I would like to convert this to a simple RGB format buffer to pass to
>> numpy. Has anyone managed this using libjpeg or any other lib?
>>
>> Cheers!
>>
>> Chris
>>
>>
>
> You probably want to use PIL
>
>    http://www.pythonware.com/products/pil/
>
>
> DaveA
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reifying indent and dedent into braces

2009-10-13 Thread rustom
On Oct 13, 12:45 pm, Steven D'Aprano
 wrote:
>
> Have you looked at the tokenize module?
>
> http://docs.python.org/library/tokenize.html

Thanks Steven -- that was what I was looking for.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to run python script in emacs

2009-10-13 Thread rustom
On Sep 26, 8:54 pm, devilkin  wrote:
> I'm just starting learning python, and coding in emacs. I usually
> split emacs window into two, coding in one, and run script in the
> other, which is not very convenient. anyone can help me with it? is
> there any tricks like emacs short cut?

>
> also please recommand some emacs plug-ins for python programming, i'm
> also beginner in emacs.currently i'm only using python.el.
python.el comes with emacs
python-mode.el comes from python  https://launchpad.net/python-mode/
Because of some emacs politics the first ships with emacs although
most uses prefer the second.
Note 1. The key bindings are different
Note 2. Does not work with python3. See my post
http://groups.google.com/group/comp.lang.python/browse_thread/thread/d65139d6d1822ad4?pli=1

> Are any plugins supply code folding and autocomplete?
See rope http://rope.sourceforge.net/ropemacs.html if you want
but its an installation headache (requires pymacs bleeding edge
version etc)
I suggest you just get used to python-mode first (C-c ! and C-c C-c)
and then explore these questions a bit later.

>
> BTW, I'm not a english native speaker, any grammer mistakes, please
> correct them. :)

grammer is spelt grammar :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: postprocessing in os.walk

2009-10-13 Thread kj
In  Dave Angel 
 writes:

>kj wrote:
>> Perl's directory tree traversal facility is provided by the function
>> find of the File::Find module.  This function accepts an optional
>> callback, called postprocess, that gets invoked "just before leaving
>> the currently processed directory."  The documentation goes on to
>> say "This hook is handy for summarizing a directory, such as
>> calculating its disk usage", which is exactly what I use it for in
>> a maintenance script.
>>
>> This maintenance script is getting long in the tooth, and I've been
>> meaning to add a few enhancements to it for a while, so I thought
>> that in the process I'd port it to Python, using the os.walk
>> function, but I see that os.walk does not have anything like this
>> File::Find::find's postprocess hook.  Is there a good way to simulate
>> it (without having to roll my own File::Find::find in Python)?
>>
>> TIA!
>>
>> kynn
>>
>>   
>Why would you need a special hook when the os.walk() generator yields 
>exactly once per directory?  So whatever work you do on the list of 
>files you get, you can then put the summary logic immediately after.

>Or if you really feel you need a special hook, then write a wrapper for 
>os.walk(), which takes a hook function as a parameter, and after 
>yielding each file in a directory, calls the hook.  Looks like about 5 
>lines.

I think you're missing the point.  The hook in question has to be
called *immediately after* all the subtrees that are rooted in
subdirectories contained in the current directory have been visited
by os.walk.

I'd love to see your "5 lines" for *that*.

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


"undefined symbol: TLSv1_method" error when import psycopg2

2009-10-13 Thread 一首诗
I installed psycopg2 by "easy_install psycopg2" on CentOS 5.3, no
error was reported, but when I tried "import psycopg2", I got :

exceptions.ImportError: /usr/lib/python2.4/site-packages/
psycopg2-2.0.9-py2.4-linux-i686.egg/psycopg2/_psycopg.so: undefined
symbol: TLSv1_method

What might cause the problem?
-- 
http://mail.python.org/mailman/listinfo/python-list


Cannot upload a good egg on pypi

2009-10-13 Thread Luca Fabbri
Hi all.

I've problems while using setuptools for getting an egg and upload it
on pypi. The egg (in bdist_egg or sdist format) are always "corrupted"
as far as a lot of files are not included in the zip, tar.gz or egg
results.
Looking at google it seems that this problem has been fixed (related
to the SVN version used).

But I continue to have problems. I'm using setuptools 0.6c9 and SVN
1.6.2 on my MacOS.

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


Re: postprocessing in os.walk

2009-10-13 Thread Peter Otten
kj wrote:

> In  Dave Angel
>  writes:
> 
>>kj wrote:
>>> Perl's directory tree traversal facility is provided by the function
>>> find of the File::Find module.  This function accepts an optional
>>> callback, called postprocess, that gets invoked "just before leaving
>>> the currently processed directory."  The documentation goes on to
>>> say "This hook is handy for summarizing a directory, such as
>>> calculating its disk usage", which is exactly what I use it for in
>>> a maintenance script.
>>>
>>> This maintenance script is getting long in the tooth, and I've been
>>> meaning to add a few enhancements to it for a while, so I thought
>>> that in the process I'd port it to Python, using the os.walk
>>> function, but I see that os.walk does not have anything like this
>>> File::Find::find's postprocess hook.  Is there a good way to simulate
>>> it (without having to roll my own File::Find::find in Python)?
>>>
>>> TIA!
>>>
>>> kynn
>>>
>>>   
>>Why would you need a special hook when the os.walk() generator yields
>>exactly once per directory?  So whatever work you do on the list of
>>files you get, you can then put the summary logic immediately after.
> 
>>Or if you really feel you need a special hook, then write a wrapper for
>>os.walk(), which takes a hook function as a parameter, and after
>>yielding each file in a directory, calls the hook.  Looks like about 5
>>lines.
> 
> I think you're missing the point.  The hook in question has to be
> called *immediately after* all the subtrees that are rooted in
> subdirectories contained in the current directory have been visited
> by os.walk.
> 
> I'd love to see your "5 lines" for *that*.

import os

def find(root, process):
for pdf in os.walk(root, topdown=False):
process(*pdf)

def process(path, dirs, files):
print path

find(".", process)

Peter

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


speed up linecache.getline()

2009-10-13 Thread bbarbero

Hi Everyone!!


I am using linecache.getline, to access to a line in a long file. It s  
really fast, appx 4seconds, but I was just wandering if any of you,  
know either another way, or there is something that I can do to speed  
it up...  thank you very much for your help!!


Regards,
Bea











Quoting Chris Rebert :


On Wed, Oct 7, 2009 at 10:21 AM,   wrote:

Hi again!

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

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

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


Have you considered using the `json` module
(http://docs.python.org/library/json.html) to serialize and
deserialize the lists to/from a file in JSON format?
The `pickle` module is another option:
http://docs.python.org/library/pickle.html

Cheers,
Chris
--
http://blog.rebertia.com






This message was sent using IMP, the Internet Messaging Program.
--
http://mail.python.org/mailman/listinfo/python-list


Re: postprocessing in os.walk

2009-10-13 Thread Paul Rubin
kj  writes:
> I think you're missing the point.  The hook in question has to be
> called *immediately after* all the subtrees that are rooted in
> subdirectories contained in the current directory have been visited
> by os.walk.
> 
> I'd love to see your "5 lines" for *that*.

I'm having trouble understanding the specification.  To find the disk
usage (in bytes) of a directory:

import os,stat
def find_disk_usage(dirname):
  return sum(sum(os.stat(dirpath+'/'+filename)[stat.ST_SIZE]
  for filename in fn_list)
  for dirpath, dirlist, fn_list in os.walk(dirname))

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


Is somebody used Python in LabVIEW?

2009-10-13 Thread Nadav Chernin
Hi, I use Python as scripting tool in LabVIEW. I use lvpython.dll that
include only built-in modules of python. 

 

But I need to use for example also urllib that is not included in
lvpython.dll. 

 

How can I use it also?

 

Thanks, Nadav

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


Re: Where to find pexpect

2009-10-13 Thread Jean-Michel Pichavant

Antoon Pardon wrote:

I have been looking for pexpect. The links I find like
http://pexpect.sourceforge.net all end up at
http://www.noah.org/wiki/Pexpect which produces a 404 not
found problem.

Does someone know the current location?

  

maybe they removed the distribution so you may use "subprocess" :o)

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


Re: Cannot upload a good egg on pypi

2009-10-13 Thread Chris Withers

Luca Fabbri wrote:

I've problems while using setuptools for getting an egg and upload it
on pypi. The egg (in bdist_egg or sdist format) are always "corrupted"
as far as a lot of files are not included in the zip, tar.gz or egg
results.
Looking at google it seems that this problem has been fixed (related
to the SVN version used).

But I continue to have problems. I'm using setuptools 0.6c9 and SVN
1.6.2 on my MacOS.


Yeah, that will be broken if you use find_packages() and friends.

You need to wait for setuptools 0.6c10 (due in a week or two) or switch 
to using Distribute...


Chris

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


Re: a=[1,2,3,4].reverse() - why "a" is None?

2009-10-13 Thread Simon Forman
On Mon, Oct 12, 2009 at 4:44 AM, Nadav Chernin  wrote:
>
>        Chris Withers wrote:
>
>        ...becauase you were looking for:
>
>        reversed([1,2,3,4])
>
> OK, but my question is generic. Why when I use object's function that
> changed values of the object, I can't to get value of it on the fly
> without writing additional code?
>
 a=[1,3,2,4]
 a.sort()
 a.reverse()
 a
> [4, 3, 2, 1]
>
> Why I can't a==[1,3,2,4].sort().reverse() ?

That's just the way it is with list objects.

Note that string objects do work that way:

In [1]: "FOO".lower().replace('o', 'a')
Out[1]: 'faa'

But this is because string objects are immutable in python, so their
methods must return new strings.

Bottom line: read the documentation.  (In the python interpreter you
can use the built-in function help() on any object or function or
method to get information on it.)

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


Input redirection with IDLE

2009-10-13 Thread candide
Hi

Does  IDLE (the default GUI avalaible with the standard distribution)
support redirection from the standard input ? To be more precise, inside
a Windows or Linux shell I may redirect input data from a text file to
be executed by a python file. For instance suppose I have this python file


# foo.py
print int(raw_input())+int(raw_input())


and this data file

- data.txt BOF 
42
77
- data.txt EOF 

I may process the data in this way :

~$ python foo.py < data.txt

and the result is the following printing  :

119


Is IDLE able to provide the same service ?

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


os.chmod problem

2009-10-13 Thread Victor Subervi
Hi;
I have the following code:

  if 13 < x < 20:
y += 1
w += 1
try:
  getpic = "getpic" + str(w) + ".py"
  try:
os.remove(getpic)
  except:
pass
  code = """#! /usr/bin/python
import cgitb; cgitb.enable()
import MySQLdb
import cgi
import sys,os
sys.path.append(os.getcwd())
from login import login
def pic():
  user, passwd, db, host = login()
  form = cgi.FieldStorage()
  db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
  cursor= db.cursor()
  sql = "select pic%s from productsX where id=%s;"
  cursor.execute(sql)
  content = cursor.fetchone()[0]
  cursor.close()
  print content
print 'Content-type: image/jpeg'
print
pic()
print ''""" % (str(w), str(w))
  script = open(getpic, "w")
  script.write(code)
  print '\n' % str(x)
  print '\n' % (getpic)
  os.chmod(getpic, 755)
  count2 += 1
except:
  pass

Forgive the empty except :) The problem is that the os.chmod command doesn't
work. It's obviously in the right directory, because the script correctly
writes there. What do?

On another note, I find it interesting that the above revised code does in
fact get that darned image to finally post correctly. It appears that this
python server insists on my calling a function...that I can't simply code it
out without defining a function. Never run into that before.
TIA,
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.chmod problem

2009-10-13 Thread Victor Subervi
Never mind. I just discovered that os.chmod requires 0755, not just 755.
V

On Tue, Oct 13, 2009 at 9:26 AM, Victor Subervi wrote:

> Hi;
> I have the following code:
>
>   if 13 < x < 20:
> y += 1
> w += 1
> try:
>   getpic = "getpic" + str(w) + ".py"
>   try:
> os.remove(getpic)
>   except:
> pass
>   code = """#! /usr/bin/python
> import cgitb; cgitb.enable()
> import MySQLdb
> import cgi
> import sys,os
> sys.path.append(os.getcwd())
> from login import login
> def pic():
>   user, passwd, db, host = login()
>   form = cgi.FieldStorage()
>   db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
>   cursor= db.cursor()
>   sql = "select pic%s from productsX where id=%s;"
>   cursor.execute(sql)
>   content = cursor.fetchone()[0]
>   cursor.close()
>   print content
> print 'Content-type: image/jpeg'
> print
> pic()
> print ''""" % (str(w), str(w))
>   script = open(getpic, "w")
>   script.write(code)
>   print '\n' % str(x)
>   print '\n' % (getpic)
>   os.chmod(getpic, 755)
>   count2 += 1
> except:
>   pass
>
> Forgive the empty except :) The problem is that the os.chmod command
> doesn't work. It's obviously in the right directory, because the script
> correctly writes there. What do?
>
> On another note, I find it interesting that the above revised code does in
> fact get that darned image to finally post correctly. It appears that this
> python server insists on my calling a function...that I can't simply code it
> out without defining a function. Never run into that before.
> TIA,
> Victor
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pickle's backward compatibility

2009-10-13 Thread M.-A. Lemburg
[email protected] wrote:
> On 03:17 pm, [email protected] wrote:
>> Hi,
>>
>> If I define my own class and use pickle to serialize the objects in
>> this class, will the serialized object be successfully read in later
>> version of python.
>>
>> What if I serialize (using pickle) an object of a class defined in
>> python library, will it be successfully read in later version of
>> python?
> 
> Sometimes.  Sometimes not.  Python doesn't really offer any guarantees
> regarding this.

I think this needs to be corrected: the pickle protocol versions are
compatible between Python releases, however, there are two things to
consider:

 * The default pickle version sometimes changes between minor
   releases.

   This is easy to handle, though, since you can provide the pickle
   protocol version as parameter.

 * The pickle protocol has changed a bit between 2.x and 3.x.

   This is mostly due to the fact that Python's native string
   format changed to Unicode in 3.x.

http://docs.python.org/library/pickle.html#data-stream-for

Finally, you can register your own pickle functions using the
copy_reg module:

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

OTOH, marshal, the more simplistic serialization format used
for basic types and PYC files, does change often and is not
guaranteed to be backwards compatible.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Oct 13 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 Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
-- 
http://mail.python.org/mailman/listinfo/python-list


When to derive from object?

2009-10-13 Thread Igor Mikushkin
Hello all!

I'm a newbie to Python.
Could you please say me when it is better to derive from "object" and
when not?

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


Re: When to derive from object?

2009-10-13 Thread Bruno Desthuilliers

Igor Mikushkin a écrit :

Hello all!

I'm a newbie to Python.


Welcome onboard


Could you please say me when it is better to derive from "object" and
when not?


- When not : when using Python >= 3.0, or when already subclassing 
another class.


- When : any other case !-)

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


Re: When to derive from object?

2009-10-13 Thread Matimus
On Oct 13, 7:45 am, Igor Mikushkin  wrote:
> Hello all!
>
> I'm a newbie to Python.
> Could you please say me when it is better to derive from "object" and
> when not?
>
> Thanks,
> Igor

The only reason to derive from 'object' is if there is some sort of
weird side effect of using new style classes. I _have_ run into some
interesting scenarios when creating com objects in python using the
win32com module. Other than that, I always use new style classes
(derived from object).

Here are some articles that point out the distinctions between old and
new style classes: http://python.org/doc/newstyle/

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


Re: pickle's backward compatibility

2009-10-13 Thread exarkun

On 02:48 pm, [email protected] wrote:

[email protected] wrote:

On 03:17 pm, [email protected] wrote:

Hi,

If I define my own class and use pickle to serialize the objects in
this class, will the serialized object be successfully read in later
version of python.

What if I serialize (using pickle) an object of a class defined in
python library, will it be successfully read in later version of
python?


Sometimes.  Sometimes not.  Python doesn't really offer any guarantees
regarding this.


I think this needs to be corrected: the pickle protocol versions are
compatible between Python releases, however, there are two things to
consider:

* The default pickle version sometimes changes between minor
  releases.

  This is easy to handle, though, since you can provide the pickle
  protocol version as parameter.

* The pickle protocol has changed a bit between 2.x and 3.x.

  This is mostly due to the fact that Python's native string
  format changed to Unicode in 3.x.


The pickle protocol isn't the only thing that determines whether an 
existing pickle can be loaded.  Consider this very simple example of a 
class which might exist in Python 2.x:


   class Foo:
   def __init__(self):
   self._bar = None

   def bar(self):
   return self._bar

Nothing particularly fancy or interesting going on there.  Say you write 
a pickle that includes an instance of this class.


Now consider this modified version of Foo from Python 2.(x+1):

   class Foo(object): # The class is new-style now, because someone felt 
like

  # making it new style

   def __init__(self, baz):  # The class requires an argument to 
__init__

 # now to specify some new piece of info

self.barValue = None  # _bar was renamed barValue because 
someone
  # thought it would make sense to 
expose the

  # info publically

self._baz = baz

   def bar(self):
return self.barValue  # Method was updated to use the new 
name of

  # the attribute

Three fairly straightforward changes.  Arguably making Foo new style and 
adding a required __init__ argument are not backwards compatible changes 
to the Foo class itself, but these are changes that often happen between 
Python releases.  I think that most people would not bother to argue 
that renaming "_bar" to "barValue" is an incompatibility, though.


But what happens when you try to load your Python 2.x pickle in Python 
2.(x+1)?


First, you get an exception like this:

 Traceback (most recent call last):
   File "", line 1, in 
   File "/usr/lib/python2.5/pickle.py", line 1374, in loads
 return Unpickler(file).load()
   File "/usr/lib/python2.5/pickle.py", line 858, in load
 dispatch[key](self)
   File "/usr/lib/python2.5/pickle.py", line 1070, in load_inst
 self._instantiate(klass, self.marker())
   File "/usr/lib/python2.5/pickle.py", line 1060, in _instantiate
 value = klass(*args)
 TypeError: in constructor for Foo: __init__() takes exactly 2 arguments 
(1 given)


But let's say the class didn't get changed to new-style after all... 
Then you can load the pickle, but what happens when you try to call the 
bar method?  You get this exception:


 Traceback (most recent call last):
   File "", line 1, in 
   File "", line 6, in bar
 AttributeError: Foo instance has no attribute 'barValue'

So these are the kinds of things I am talking about when I say that 
there aren't really any guarantees.


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


Re: pickle's backward compatibility

2009-10-13 Thread M.-A. Lemburg
[email protected] wrote:
> On 02:48 pm, [email protected] wrote:
>> [email protected] wrote:
>>> On 03:17 pm, [email protected] wrote:
 Hi,

 If I define my own class and use pickle to serialize the objects in
 this class, will the serialized object be successfully read in later
 version of python.

 What if I serialize (using pickle) an object of a class defined in
 python library, will it be successfully read in later version of
 python?
>>>
>>> Sometimes.  Sometimes not.  Python doesn't really offer any guarantees
>>> regarding this.
>>
>> I think this needs to be corrected: the pickle protocol versions are
>> compatible between Python releases, however, there are two things to
>> consider:
>>
>> * The default pickle version sometimes changes between minor
>>   releases.
>>
>>   This is easy to handle, though, since you can provide the pickle
>>   protocol version as parameter.
>>
>> * The pickle protocol has changed a bit between 2.x and 3.x.
>>
>>   This is mostly due to the fact that Python's native string
>>   format changed to Unicode in 3.x.
> 
> The pickle protocol isn't the only thing that determines whether an
> existing pickle can be loaded.  Consider this very simple example of a
> class which might exist in Python 2.x:
>
> [...changes to baseclasses and __init__ arguments...]
>
> So these are the kinds of things I am talking about when I say that
> there aren't really any guarantees.

True, there's no guarantee that pickle can work out your code changes.
I think that's a bit much to expect from a serialization protocol :-)

I was only talking about the data storage format itself, ie. whether
it's possible to pickle the data in Python 2.n and load it again
in 2.n+m without code changes.

Even with code changes, you can still work-around many issues by
implementing a proper .__setstate__() method in your class and
using a class .version attribute to detect older pickles.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Oct 13 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 Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against "while True:" loops

2009-10-13 Thread Mensanator
On Oct 13, 3:44�am, John Reid  wrote:
> Mensanator wrote:
> >> Nothing wrong with a having a break IMHO.
>
> > My opinion is that there is everything wrong with
> > having a break. I don't think I have ever used one,
> > I write code that doesn't depend on that crutch.
>
> I guess its crutch-iness is in the eye of the beholder. You seem to have
> a dogmatic view about this.

No, it's just that the OP was asking whether
avoiding "while True" is considered Best Practice.
How can you answer such a question without sounding
dogmatic?

>
>
>
>
>
>
>
> >> while not done:
>
> >> seems very dangerous to me as you'd have to
>
> >> del done
>
> >> before writing the same construct again. That's the sort of thing that
> >> leads to errors.
>
> > Duh. I won't write silly code like that either.
> > If I need more than one loop structure then I'll
> > do something like
>
> > � � while not done_with_this
>
> > � � while not done_with_that
>
> This is neither clean or well scoped.
>
>
>
> > Besides, since I _always_ initialize the flag
> > before entering a loop, the flag can be reused
> > and doesn't have to be deleted (as long as the
> > loops aren't nested). And since I don't use goto,
> > there's no chance the initialization can be avoided.
>
> Initialising the flag is just another line of code that has to be
> interpreted later. I didn't notice the initialisation in your original post.

"Just another line that has to be interpreted later"
is a strange comment in the context of "del done".

>
>
>
> > The best way to avoid the pitfalls of spaghetti
> > code is to not write it in the first place.
>
> I agree. With 'break' it is obvious what the code does and there are
> fewer lines to write in the first place and comprehend in the second.

Do you consider Perl golf to be Best Practice?

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


efficient running median

2009-10-13 Thread Janto Dreijer
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When to derive from object?

2009-10-13 Thread Matimus
On Oct 13, 8:02 am, Matimus  wrote:
> On Oct 13, 7:45 am, Igor Mikushkin  wrote:
>
> > Hello all!
>
> > I'm a newbie to Python.
> > Could you please say me when it is better to derive from "object" and
> > when not?
>
> > Thanks,
> > Igor
>
> The only reason to derive from 'object' is ...

erm... that should say "the only reason _not_ to derive from object"
oops.

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


Re: Is pythonic version of scanf() or sscanf() planned?

2009-10-13 Thread Aahz
In article ,
ryniek90   wrote:
>
>But I remember that lambda function also was unwelcome in Python, but
>finally it is and is doing well. So maybe someone, someday decide to
>put in Python an alternative, really great implementation of scanf() ?

How long have you been using Python?  lambda has been there almost from
the beginning.
-- 
Aahz ([email protected])   <*> 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."  [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


setting variables in the local namespace

2009-10-13 Thread Chris Withers

Hi All,

Say I have a piece of code like this:

mname = model.__name__
fname = mname+'_order'
value = request.GET.get('order')
if value:
request.session[fname]=value
else:
value = request.session.get(
fname,
model.default_name
)

Now, if I want to do *exactly* the same thing with a variable named 
'sort', I have to copy and paste the above code or do something hacky 
like have a dict called "vars" and manipulate that, or factor the above 
into a function and take the hit on the extra function call...


What I'd be looking for is something like:

locals()[name]=value

...or, say:

setattr(,name,value)

Now, I got horribly flamed for daring to be so heretical as to suggest 
this might be a desirable thing in #python, so I thought I'd ask here 
before trying to take this to python-dev or writing a PEP:


- what is so wrong with wanting to set a variable in the local namespace 
based on a name stored in a variable?


- have I missed something that lets me do this already?

cheers,

Chris

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


Re: The rap against "while True:" loops

2009-10-13 Thread John Reid



Mensanator wrote:

No, it's just that the OP was asking whether
avoiding "while True" is considered Best Practice.
How can you answer such a question without sounding
dogmatic?

I was just pointing out your style of programming seems inflexible.



"Just another line that has to be interpreted later"
is a strange comment in the context of "del done".
Interpreted in the sense that the maintainer of the code interprets it, 
not the machine.


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


Re: efficient running median

2009-10-13 Thread Peter Otten
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

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


Re: [OT] organizing your scripts, with plenty of re-use

2009-10-13 Thread Gabriel Genellina
En Tue, 13 Oct 2009 03:48:00 -0300, Dennis Lee Bieber  
 escribió:

On Mon, 12 Oct 2009 16:36:58 -0700, Ethan Furman 
declaimed the following in gmane.comp.python.general:


coffe table, you look in your car, etc, etc, and so forth.  If you move
a file in a package to somewhere else, and you don't tell the package
where it's at, it's not going to start looking all over the hard-drive
for it.  If that were the case you would have to be extra careful to
have every module's name be distinct, and then what's the point of
having packages?


Heh... Digging up some ancient history but... TRS-80 TRSDOS6 (and
some earlier incarnations too) WOULD search all active drives if no
drive letter was specified (no subdirectories, of course -- and I never
had a hard drive on mine [$5000 for a 5MB drive?], just the two
floppies). And for creating files, again if no drive were specified, it
would create the file on the first drive that was not write-protected.


In the old MSDOS era, there was the APPEND command. It was used to set a  
list of directories or subdirectories that were searched for data files  
(in a way similar as PATH works for executable files). For example, after  
APPEND c:\pirulo\data;doc
you could edit a file like c:\foo\doc\readme.txt directly from the c:\foo  
directory:


c:\foo> edit readme.txt

and because of "doc" being in the APPEND path, edit would find it. I think  
Stef would enjoy using it - just listing each subdirectory would make the  
disk tree completely flat as seen by the application.


I think this functionality was removed by the time Windows 95 came out  
because it was very dangerous. It was extremely easy to open (or even  
remove!) the wrong file.


--
Gabriel Genellina

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


Re: organizing your scripts, with plenty of re-use

2009-10-13 Thread Buck
On Oct 12, 4:30 pm, Carl Banks  wrote:
> On Oct 12, 11:24 am, Buck  wrote:
>
> > On Oct 10, 9:44 am, "Gabriel Genellina" 
> > wrote:
>
> > > The good thing is that, if the backend package is properly installed  
> > > somewhere in the Python path ... it still works with no modifications.
>
> > I'd like to get to zero-installation if possible. It's easy with
> > simple python scripts, why not packages too? I know the technical
> > reasons, but I haven't heard any practical reasons.
>
> No it's purely technical.  Well mostly technical (there's a minor
> issue of how a script would figure out its "root").  No language is
> perfect, not even Python, and sometimes you just have to deal with
> things the way they are.

Python is the closest I've seen. I'd like to deal with this wart if we
can.

> We're trying to help you with workarounds, but it seems like you just
> want to vent more than you want an actual solution.

Steven had the nicest workaround (with the location = __import__
('__main__').__file__ trick), but none of them solve the problem of
the OP: organization of runnable scripts. So far it's been required to
place all runnable scripts directly above any used packages. The
workaround that Gabriel has been touting requires this too.

Maybe it seems like I'm venting when I shoot down these workarounds,
but my real aim is to find some sort of consensus, either that there
is a solution, or an unsolved problem. I'd be delighted with a
solution, but none have been acceptable so far (as I explained in
aggravating detail earlier).

If I can find consensus that this is a real problem, not just my
personal nit-pick, then I'd be willing to donate my time to design,
write and push through a PEP for this purpose. I believe it can be
done neatly with just three new standard functions, but it's premature
to discuss that.

> > If the reasons are purely technical, it smells like a PEP to me.
>
> Good luck with that.  I'd wholeheartedly support a good alternative,
Thanks.

> I just want to warn you that it's not a simple issue to fix, it would be
> involve spectacular and highly backwards-incompatible changes.
> --Carl Banks

I don't believe that's true, but I think that's a separate discussion.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: organizing your scripts, with plenty of re-use

2009-10-13 Thread Buck
On Oct 12, 3:34 pm, "Gabriel Genellina" 
wrote:
> En Mon, 12 Oct 2009 15:24:34 -0300, Buck  escribió:
>
> > On Oct 10, 9:44 am, "Gabriel Genellina" 
> > wrote:
> >> The good thing is that, if the backend package is properly installed  
> >> somewhere in the Python path ... it still works with no modifications.
>
> > I'd like to get to zero-installation if possible. It's easy with
> > simple python scripts, why not packages too? I know the technical
> > reasons, but I haven't heard any practical reasons.
>
> > If the reasons are purely technical, it smells like a PEP to me.
>
> That's what I meant to say. It IS a zero-installation schema, and it also  
> works if you properly install the package. Quoting Steven D'Aprano  
> (changing names slightly):
>
> """You would benefit greatly from separating the interface from
> the backend. You should arrange matters so that the users see something
> like this:
>
> project/
> +-- animal
> +-- mammal
> +-- reptile
> +-- somepackagename/
>      +-- __init__.py
>      +-- animals.py
>      +-- mammals/
>          +-- __init__.py
>          +-- horse.py
>          +-- otter.py
>      +-- reptiles/
>          +-- __init__.py
>          +-- gator.py
>          +-- newt.py
>      +-- misc/
>          +-- __init__.py
>          +-- lungs.py
>          +-- swimming.py
>
> where the front end is made up of three scripts "animal", "mammal" and
> "reptile", and the entire backend is in a package.""" [ignore the rest]
>
> By example, the `animal` script would contain:
>
>  from somepackagename import animals
> animals.main()
>
> or perhaps something more elaborate, but in any case, the script imports  
> whatever it needs from the `somepackagename` package.
>
> The above script can be run:
>
> a) directly from the `project` directory; this could be a checked out copy  
>  from svn, or a tar file extracted in /tmp, or whatever. No need to install  
> anything, it just works.
>
> b) alternatively, you may install somepackagename into site-packages (or  
> the user site directory, or any other location along the Python path), and  
> copy the scripts into /usr/bin (or any other location along the system  
> PATH), and it still works.
>
> The key is to put all the core functionality into a package, and place the  
> package where Python can find it. Also, it's a good idea to use relative  
> imports from inside the package. There is no need to juggle with sys.path  
> nor even set PYTHONPATH nor import __main__ nor play any strange games; it  
> Just Works (tm).
>
> --
> Gabriel Genellina

Hi Gabriel. This is very thoughtful. Thanks.

As in the OP, when I have 50 different runnable scripts, it become
necessary to arrange them in directories. How would you do that in
your scheme? Currently it looks like they're required to live directly
above the package containing their code.

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


Re: postprocessing in os.walk

2009-10-13 Thread Dave Angel

Peter Otten wrote:

kj wrote:

  

In  Dave Angel
 writes:



kj wrote:
  

Perl's directory tree traversal facility is provided by the function
find of the File::Find module.  This function accepts an optional
callback, called postprocess, that gets invoked "just before leaving
the currently processed directory."  The documentation goes on to
say "This hook is handy for summarizing a directory, such as
calculating its disk usage", which is exactly what I use it for in
a maintenance script.

This maintenance script is getting long in the tooth, and I've been
meaning to add a few enhancements to it for a while, so I thought
that in the process I'd port it to Python, using the os.walk
function, but I see that os.walk does not have anything like this
File::Find::find's postprocess hook.  Is there a good way to simulate
it (without having to roll my own File::Find::find in Python)?

TIA!

kynn

  


Why would you need a special hook when the os.walk() generator yields
exactly once per directory?  So whatever work you do on the list of
files you get, you can then put the summary logic immediately after.
  
Or if you really feel you need a special hook, then write a wrapper for

os.walk(), which takes a hook function as a parameter, and after
yielding each file in a directory, calls the hook.  Looks like about 5
lines.
  

I think you're missing the point.  The hook in question has to be
called *immediately after* all the subtrees that are rooted in
subdirectories contained in the current directory have been visited
by os.walk.

I'd love to see your "5 lines" for *that*.



import os

def find(root, process):
for pdf in os.walk(root, topdown=False):
process(*pdf)

def process(path, dirs, files):
print path

find(".", process)

Peter



  

Thanks Peter,

To expand it to five lines, and make it the generator I mentioned,

import os

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

def process(path, dirs, files):
   print "hooked --", path

for fullpath in find("..", process):
   print fullpath


This is a generator which yields each file in a directory tree, and 
after all the files below a particular directory are processed, 
"immediately" calls the hook


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


Re: setting variables in the local namespace

2009-10-13 Thread Gabriel Genellina
En Tue, 13 Oct 2009 13:05:03 -0300, Chris Withers   
escribió:



What I'd be looking for is something like:

locals()[name]=value

...or, say:

setattr(,name,value)

Now, I got horribly flamed for daring to be so heretical as to suggest  
this might be a desirable thing in #python, so I thought I'd ask here  
before trying to take this to python-dev or writing a PEP:


- what is so wrong with wanting to set a variable in the local namespace  
based on a name stored in a variable?


In principle, nothing. But the local namespace is highly optimized: it is  
not a dictionary but a fixed-size C array of pointers. It's not indexed by  
name but by position. This can be done because the compiler knows (just by  
static inspection) which names are local and which aren't (locals are the  
names assigned to).
If you could add arbitrary names to the local namespace, the optimization  
is no more feasible. That's why "from foo import *" issues a warning in  
2.x and is forbidden in 3.x, and locals() isn't writable in practice.



- have I missed something that lets me do this already?


Use your own namespace. You may choose dict-like access, or attribute-like  
access:


class namespace:pass
ns = namespace()

ns.order = .
setattr(ns, variablename, ..)
ns.area = ns.height * ns.width
---
ns = {}
ns['order'] = ...
ns[variablename] = ...
ns['area'] = ns['height'] * ns['width']

Or, see some recent posts about "dictionary with attribute-style access".

--
Gabriel Genellina

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


Re: organizing your scripts, with plenty of re-use

2009-10-13 Thread Ethan Furman

Buck wrote:

[snip]


Steven had the nicest workaround (with the location = __import__
('__main__').__file__ trick), but none of them solve the problem of
the OP: organization of runnable scripts. So far it's been required to
place all runnable scripts directly above any used packages. The
workaround that Gabriel has been touting requires this too.


[snip]

Wha?  "Place all runnable scripts directly above any used packages?"  I 
must have missed something major in this thread.  The only thing 
necessary is to have the package being imported to be somewhere in 
PYTHONPATH.


Previous post:
>I'd like to get to zero-installation if possible. It's easy with
>simple python scripts, why not packages too? I know the technical
>reasons, but I haven't heard any practical reasons.

I don't think we mean the same thing by "zero-installation"... seems to 
me that if you have to copy it, check it out, or anything to get the 
code from point A to point 'usable on your computer', then you have done 
some sort of installation.


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


Re: speed up linecache.getline()

2009-10-13 Thread Gabriel Genellina

En Tue, 13 Oct 2009 10:21:31 -0300,  escribió:

I am using linecache.getline, to access to a line in a long file. It s  
really fast, appx 4seconds, but I was just wandering if any of you, know  
either another way, or there is something that I can do to speed it  
up...  thank you very much for your help!!


If all the lines in the file have exactly the same length, you may seek to  
the specific line and read it at once.
Otherwise I'm afraid you have to read all the previous n-1 lines to get to  
line n, and that's what makes it slow.


--
Gabriel Genellina

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


Re: setting variables in the local namespace

2009-10-13 Thread Mick Krippendorf
Hello.

Chris Withers schrieb:
> mname = model.__name__
> fname = mname+'_order'
> value = request.GET.get('order')
> if value:
> request.session[fname]=value
> else:
> value = request.session.get(
> fname,
> model.default_name
> )
> 
> Now, if I want to do *exactly* the same thing with a variable named
> 'sort', 

I don't really understand what you mean by "*exactly* the same".
request.GET.get('sort') ?
or
request.session[fname] = locals()['sort']
maybe? or sth else?

> I have to copy and paste the above code or do something hacky
> like have a dict called "vars" and manipulate that, or factor the above
> into a function and take the hit on the extra function call...

>From the "request.session" stuff I conclude you're writing a webapp.
Compared to all the other time consuming things involved, I think,
another function call is negligible.

> What I'd be looking for is something like:
> 
> locals()[name]=value

That will probably do what you want, and if that is a good idea depends
not on the pythonicity of it per se, but on the context in which you use
it, which I, as mentioned earlier, don't quite understand.

> - what is so wrong with wanting to set a variable in the local namespace
> based on a name stored in a variable?

What's your use case, I ask?

> - have I missed something that lets me do this already?

Yes, and, uh, yes. "locals()['foo'] = bar" works in that it does the
same thing as "foo = bar". So why don't you write that instead?

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


Re: postprocessing in os.walk

2009-10-13 Thread Ethan Furman

kj wrote:

In  Dave Angel 
 writes:




[snippetty snip]

Why would you need a special hook when the os.walk() generator yields 
exactly once per directory?  So whatever work you do on the list of 
files you get, you can then put the summary logic immediately after.



I think you're missing the point.  The hook in question has to be
called *immediately after* all the subtrees that are rooted in
subdirectories contained in the current directory have been visited
by os.walk.

I'd love to see your "5 lines" for *that*.

kj


So now that you've seen a couple examples, perhaps you noticed the flag 
"topdown=False"?  With that (un)set, I repeat the question -- why do you 
need a hook?


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


Re: setting variables in the local namespace

2009-10-13 Thread Duncan Booth
Chris Withers  wrote:

> Now, if I want to do *exactly* the same thing with a variable named 
> 'sort', I have to copy and paste the above code or do something hacky 
> like have a dict called "vars" and manipulate that, or factor the above 
> into a function and take the hit on the extra function call...
> 
The function call every time. You aren't going to notice the extra function 
call.

Once you've got everything working, and you've profiled it, and you've 
determined that that function call is a major overhead, then might be the 
time to consider optimising it, but not before then.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against "while True:" loops

2009-10-13 Thread Ethan Furman

Mensanator wrote:

On Oct 13, 3:44�am, John Reid  wrote:


while not done:

seems very dangerous to me as you'd have to

del done

before writing the same construct again. That's the sort of thing that
leads to errors.


Duh. I won't write silly code like that either.
If I need more than one loop structure then I'll
do something like

� � while not done_with_this

� � while not done_with_that

Besides, since I _always_ initialize the flag
before entering a loop, the flag can be reused
and doesn't have to be deleted (as long as the
loops aren't nested). And since I don't use goto,
there's no chance the initialization can be avoided.


Initialising the flag is just another line of code that has to be
interpreted later. I didn't notice the initialisation in your original post.



"Just another line that has to be interpreted later"
is a strange comment in the context of "del done".


I don't believe John is advocating using it, just pointing out that an 
extra line is needed -- whether the extra line is "del loop_control_var" 
or "loop_control_var = False", it's still an extra line.


Mind you, I'm not saying you should change the way you program as it 
seems to work for you, just that there are other ways to write good 
clean programs.


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


Re: efficient running median

2009-10-13 Thread Dave Angel

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

  
Since the previous window is sorted, it should just be a "case of delete 
one, add one" in the sorted list.  And if that isn't fast enough, 
consider some form of tree.


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


Re: When to derive from object?

2009-10-13 Thread Anson Mackeracher
Can someone point me to some reason on why not to derive from Object
when using Python >= 3.0? I am a Python novice, I need some
background.

On Oct 13, 10:49 am, Bruno Desthuilliers  wrote:
> Igor Mikushkin a écrit :
>
> > Hello all!
>
> > I'm a newbie to Python.
>
> Welcome onboard
>
> > Could you please say me when it is better to derive from "object" and
> > when not?
>
> - When not : when using Python >= 3.0, or when already subclassing
> another class.
>
> - When : any other case !-)

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


Re: efficient running median

2009-10-13 Thread Ethan Furman

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 at http://pypi.python.org/pypi/blist/0.9.4

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


Re: When to derive from object?

2009-10-13 Thread Benjamin Kaplan
On Tue, Oct 13, 2009 at 1:34 PM, Anson Mackeracher  wrote:
> Can someone point me to some reason on why not to derive from Object
> when using Python >= 3.0? I am a Python novice, I need some
> background.
>

It's redundant. Python 3 cleaned up a lot of the warts that appeared
in Python over the years. Old-style classes (classes that didn't
inherit from object) were one of them. Every class in Python 3 is
derived from object whether you specify it or not.

> On Oct 13, 10:49 am, Bruno Desthuilliers  [email protected]> wrote:
>> Igor Mikushkin a écrit :
>>
>> > Hello all!
>>
>> > I'm a newbie to Python.
>>
>> Welcome onboard
>>
>> > Could you please say me when it is better to derive from "object" and
>> > when not?
>>
>> - When not : when using Python >= 3.0, or when already subclassing
>> another class.
>>
>> - When : any other case !-)
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: setting variables in the local namespace

2009-10-13 Thread Carl Banks
On Oct 13, 9:05 am, Chris Withers  wrote:
> Hi All,
>
> Say I have a piece of code like this:
>
>          mname = model.__name__
>          fname = mname+'_order'
>          value = request.GET.get('order')
>          if value:
>              request.session[fname]=value
>          else:
>              value = request.session.get(
>                  fname,
>                  model.default_name
>                  )
>
> Now, if I want to do *exactly* the same thing with a variable named
> 'sort', I have to copy and paste the above code or do something hacky
> like have a dict called "vars" and manipulate that, or factor the above
> into a function and take the hit on the extra function call...

Just a bit of perspective:

Once you are at the point of worrying about taking a hit on an extra
function call, a lot of the other things that are normally bad ideas
(like cutting and pasting a lot of code) aren't considered "bad"
anymore.  At least, not if you have good reason to worry about the
overhead of a function call.

The right way to this kind of thing, almost always, is to factor this
code into a function.


> What I'd be looking for is something like:
>
> locals()[name]=value
>
> ...or, say:
>
> setattr(,name,value)
>
> Now, I got horribly flamed for daring to be so heretical as to suggest
> this might be a desirable thing in #python, so I thought I'd ask here
> before trying to take this to python-dev or writing a PEP:
>
> - what is so wrong with wanting to set a variable in the local namespace
> based on a name stored in a variable?

My opinion: It's not necessarily a bad thing to do per se, but
whenever people think they want to do this, very often they are
approaching the problem the wrong way, especially when done with
locals.  (There are more valid reasons to do it a global level.)

One thing I've noticed is that often people only really need the
convenient local names for testing.  Example: I once set up an object
that had dynamically-assigned attributes that it read from a file.
(So you'd write "x = load_values(filename)", and it would return an
object with attributes determined from the data in the file.)  I did
it that way because I got irritated typing x["attr"] all the time, and
thought it'd be better to type x.attr.  But, when I got around to
actually using x, I found that I was always accessing the attributes
dynamically with getattr and setattr.  Whoops, wasn't that useful
after all.

There are some cases when dynamically settable locals really might be
helpful.  I'm not saying it's heretical to want it.  But on the whole
I'd rather not see it allowed.  I'd expect it to be overused, and when
it is used readability can take a severe hit.


> - have I missed something that lets me do this already?

No.

However, one thing that might work in your case would be to then pass
a dictionary as keyword arguments into a function that defines the
locals you need.

u['value'] = 1
u['sort'] = 2
u['key'] = 3

def something(value,sort,key):
return value + sort + key  # look they're locals now

something(**u)


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


Re: setting variables in the local namespace

2009-10-13 Thread Mel
Chris Withers wrote:

> - what is so wrong with wanting to set a variable in the local namespace
> based on a name stored in a variable?

What's wrong is that no other statement using the local name space can know 
what that name might be.  It's a documented fact that changing the locals() 
dictionary doesn't feed back to the users of the namespace:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...   foo=0
...   globals()['foo'] = 'bar'
...   return foo
... 
>>> f()
0

For the reasons Gabriel gave outside the thread.

You can actually assign into a local namespace using exec:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def g(s):
...   exec (s)
...   return a
... 
>>> g('a=4')
4

But look at the dependency you'll create between the function's code and the 
incoming data stream:

>>> g('b=5')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in g
NameError: name 'a' is not defined


Once you start naming local variables at run-time, you pretty much commit to 
writing the entire function at run-time.  Better to use a dictionary.

Mel.

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


Is pythonic version of scanf() or sscanf() planned?

2009-10-13 Thread ryniek90




In article ,
ryniek90   wrote:
  

But I remember that lambda function also was unwelcome in Python, but
finally it is and is doing well. So maybe someone, someday decide to
put in Python an alternative, really great implementation of scanf() ?



How long have you been using Python?  lambda has been there almost from
the beginning.
  


Otherwise - community wanted to kick of lambda, but ultimately left it.
Yes, i made mistake - lambda is almost from the beginning.
--
http://mail.python.org/mailman/listinfo/python-list


Re: setting variables in the local namespace

2009-10-13 Thread Carl Banks
On Oct 13, 9:39 am, Mick Krippendorf  wrote:
> Yes, and, uh, yes. "locals()['foo'] = bar" works in that it does the
> same thing as "foo = bar". So why don't you write that instead?

Lemme guess.

You tried this at the interactive prompt and concluded it worked in
general, right?

Even though when we speak of local variables, we usually are referring
to local variables inside a function, you didn't actually test whether
locals() works in a function, did you?

Because if you had you would have seen it doesn't work.


One of these days we're going to have a thread like this where no one
makes this mistake.  Don't know when, but one day it will happen.


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


Load a list subset with pickle?

2009-10-13 Thread Peng Yu
I use pickle to dump a long list. But when I load it, I only want to
load the first a few elements in the list. I am wondering if there is
a easy way to do so? Thank you!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When to derive from object?

2009-10-13 Thread Paul Rudin
Benjamin Kaplan  writes:

>
> It's redundant. Python 3 cleaned up a lot of the warts that appeared
> in Python over the years. Old-style classes (classes that didn't
> inherit from object) were one of them. Every class in Python 3 is
> derived from object whether you specify it or not.

... it could be argued that having two ways to specify the same thing
(derivation from object explictly or implicitly) is a wart in itself :/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: organizing your scripts, with plenty of re-use

2009-10-13 Thread Gabriel Genellina

En Tue, 13 Oct 2009 13:28:05 -0300, Buck  escribió:

On Oct 12, 3:34 pm, "Gabriel Genellina" 
wrote:

Quoting Steven D'Aprano  
(changing names slightly):

"""You would benefit greatly from separating the interface from
the backend. You should arrange matters so that the users see something
like this:

project/
+-- animal
+-- mammal
+-- reptile
+-- somepackagename/
     +-- __init__.py
     +-- animals.py
     +-- mammals/
         +-- __init__.py
         +-- horse.py
         +-- otter.py
     +-- reptiles/
         +-- __init__.py
         +-- gator.py
         +-- newt.py
     +-- misc/
         +-- __init__.py
         +-- lungs.py
         +-- swimming.py

where the front end is made up of three scripts "animal", "mammal" and
"reptile", and the entire backend is in a package.""" [ignore the rest]

By example, the `animal` script would contain:

 from somepackagename import animals
animals.main()

or perhaps something more elaborate, but in any case, the script  
imports  

whatever it needs from the `somepackagename` package.

The above script can be run:

a) directly from the `project` directory; this could be a checked out  
copy  
 from svn, or a tar file extracted in /tmp, or whatever. No need to  
install  

anything, it just works.

b) alternatively, you may install somepackagename into site-packages  
(or  
the user site directory, or any other location along the Python path),  
and  

copy the scripts into /usr/bin (or any other location along the system  
PATH), and it still works.

The key is to put all the core functionality into a package, and place  
the  
package where Python can find it. Also, it's a good idea to use  
relative  
imports from inside the package. There is no need to juggle with  
sys.path  
nor even set PYTHONPATH nor import __main__ nor play any strange games;  
it  

Just Works (tm).


As in the OP, when I have 50 different runnable scripts, it become
necessary to arrange them in directories. How would you do that in
your scheme? Currently it looks like they're required to live directly
above the package containing their code.


I'd say that, if your application contains 50 different runnable scripts  
in several directories, it deserves an install script. Being able to  
execute something from a checked-out copy is fine for a small application  
but in your case I'd say we are off limits...


Python must be able to import the package. In case a) above, this happens  
because the directory containing the script (and the package) is in  
sys.path by default. In case b), because you copy the package into some  
place that is already in sys.path. There is another option: add the  
directory containing the package to sys.path, by setting the environment  
variable PYTHONPATH. Robert Kern already explained how to do that:  



--
Gabriel Genellina

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


Re: Load a list subset with pickle?

2009-10-13 Thread Robert Kern

On 2009-10-13 13:00 PM, Peng Yu wrote:

I use pickle to dump a long list. But when I load it, I only want to
load the first a few elements in the list. I am wondering if there is
a easy way to do so? Thank you!


Not by pickling the list. However, you can concatenate pickles, so you could 
just pickle each item from the list in order to the same file and only unpickle 
the first few.


In [1]: import cPickle

In [2]: from cStringIO import StringIO

In [3]: very_long_list = range(10)

In [4]: f = StringIO()

In [5]: for item in very_long_list:
   ...: cPickle.dump(item, f)
   ...:
   ...:

In [6]: f.seek(0,0)

In [7]: cPickle.load(f)
Out[7]: 0

In [8]: cPickle.load(f)
Out[8]: 1

In [9]: cPickle.load(f)
Out[9]: 2

In [10]: cPickle.load(f)
Out[10]: 3


--
Robert Kern

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

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


Re: efficient running median

2009-10-13 Thread Dale Dalrymple
On Oct 13, 8:22 am, 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).
> ...

>  Any suggestions?

For a reference try:

 Comparison of algorithms for standard median filtering
Juhola, M.   Katajainen, J.   Raita, T.
Signal Processing, IEEE Transactions on
Jan. 1991, Volume: 39 , Issue: 1, page(s): 204 - 208
Abstract

An algorithm I have used comes from:

 On computation of the running median
Astola, J.T.   Campbell, T.G.
Acoustics, Speech and Signal Processing, IEEE Transactions on
Apr 1989, Volume: 37,  Issue: 4, page(s): 572-574

This is a dual heap approach. No code though. There are some obvious
(yeah, right) errors in their pseudo-code.

The point of the dual heap algorithm (loosely put) is to reduce the
computation to slide the window 1 element to be proportional to 2
bubble sorts of log window size instead of a window size sort.

Good luck.

Dale B. Dalrymple


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


Re: setting variables in the local namespace

2009-10-13 Thread Dave Angel

Mick Krippendorf wrote:


  
Yes, and, uh, yes. "locals()['foo'] = bar" works in that it does the

same thing as "foo = bar". So why don't you write that instead?

Mick.

  
I wouldn't expect it to do the same thing at all, and it doesn't, at 
least not in Python 2.6.2.  It may store the "bar" somewhere, but not in 
anything resembling a local variable.



bar = 42

def  mytestfunc():
   stuff = 9
   locals()['stuff'] = bar
   print locals()['stuff']
   print stuff

mytestfunc()

prints 9, twice.  No sign of the 42 value.


DaveA

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


Python XMLRPC question

2009-10-13 Thread prasanna
In using Python's XMLRPC, there is a statement that gets printed on
stdout of the form:
 localhost - - [12/Oct/2009 23:36:12] "POST /RPC2 HTTP/
1.0" 200 -

Where does this message originate? Can I turn it off, or at least
redirect it into a logging file? I am planning to run the server code
automatically at start up and wouldn't have a terminal window open to
get this message. I guess I could redirect/pipe it to a file, but it
would be more useful I could send it to the same log file that I have
the server writing other messages to.

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


Re: setting variables in the local namespace

2009-10-13 Thread Peter Pearson
On Tue, 13 Oct 2009 17:05:03 +0100, Chris Withers wrote:
[snip]
> - what is so wrong with wanting to set a variable in the local namespace 
> based on a name stored in a variable?

I'm not sure it's "so wrong" that one should never, ever do
it, but it *does* blur the boundary between the program and
the data on which the program operates.  Also, variable
names are customarily for the convenience of programmers, and
have often (e.g., compiled languages) disappeared by
run-time; so run-time references to variable names seem to
puncture the "what's for programmers" / "what's for the hardware"
distinction.

It strikes me much as the question, "What's so wrong with having
a soda straw going from the driver's seat to the gas tank?"  Curious
question.  Go ahead!  Odd that it should seem desirable, though.
I hope the smart guys on this group can do a more precise job of
expressing this.

-- 
To email me, substitute nowhere->spamcop, invalid->net.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested Menus

2009-10-13 Thread Victor Subervi
This is probably more appropriate for the MySQL list, but since this is
Dennis' pseudo-code...

Dennis wrote the following:

  cursor.execute("""create table if not exists Relationship
(ID integer auto_increment primary key,
Parent  integer not null,
foreign key (Categories.ID),
Child   integer not null,
foreign key (Categories.ID),
check (Parent <> Child) );""")

This code throws an error in MySQL around "Categories.ID". I have never
worked with foreign keys. It seems there should be a "references" statement
after that...but what does it reference? Also, why is it repeated twice?
TIA,
V

On Sat, Oct 10, 2009 at 1:58 AM, Dennis Lee Bieber wrote:

> On Fri, 9 Oct 2009 00:04:02 -0700 (PDT), r  declaimed
> the following in comp.lang.python:
>
> > This is not a personal attack on you but your writing style is so
> > horrendous i must at least notify you of it. Go to this link and view
> > your post in GG's and let me know what you think of it.
> >
> This is the first time, in over a decade of Usenet posting, I've
> been accused of having a horrendous style... Occasional over-detailed
> technical harangues, perhaps -- often declared as such by me as a
> preface to the post itself.
>
> >
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/6301372f8e581a74?hl=en#
>
> What the web-based Google Groups does to a post is beyond my
> control. It is not a proper Usenet client. Though if it means anything,
> I did look at the post.
>
> >
> > As you can see the post is virtually impossible to read from the large
> > and seemingly random indenting and tabbing going on.(notwitstanding
>
> Really? ~90% of my response was pseudo-Python code. If you hadn't
> noticed, Python uses indentation for structural elements -- so of course
> I'd use indentation. No randomness there. Did you want me to put long
> SQL statements as single line strings, exceeding common Usenet practice
> of ~76 characters per line, and letting the lines be wrapped by clients
> at truly indeterminate points?
>
>Other than Google using a very large tab width on the indentation,
> the post looked perfectly fine to me; no confusing line wraps breaking
> otherwise syntactically valid lines -- one could cut&paste to a Python
> file and probably get very few syntax errors (since I wasn't trying to
> fully working code, just the conceptual pieces).
>
> > the bombastic verbosity and unbridled quoting). I have seen many posts
>
> Out of ~176 lines (as reported by Agent's thread list) I count only
> 35 lines of quoted material. That is less than 20% of the total line
> count. That's a great improvement over the many influenced by M$
> Outlook, which promotes the unhealthy habits of top-posting and quoting
> of the entire message chain with no trimming of irrelevant text.
>
> --
>Wulfraed Dennis Lee Bieber   KD6MOG
>[email protected]   HTTP://wlfraed.home.netcom.com/
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is Islam?-1

2009-10-13 Thread Aahz
In article <[email protected]>,
Mensanator   wrote:
>
>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.
-- 
Aahz ([email protected])   <*> 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."  [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


PyCon 2010 US - Call For Tutorials Ending Soon

2009-10-13 Thread Greg Lindstrom
The period to submit proposals to teach a tutorial at PyCon 2010 US ends on
Sunday, October 18th.  There is still time for you to get a proposal on your
favorite Python topic and teach a 3-hour class (with breaks and
refreshments) to your colleagues on the Wednesday or Thursday before the
conference ("Tutorial Days").

An example proposal (and a blank template) can be found at
http://us.pycon.org/2010/tutorials/proposals/ .

Thanks!

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


Re: efficient running median

2009-10-13 Thread Daniel Stutzbach
On Tue, Oct 13, 2009 at 10:22 AM, 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).
>

In the past, I've used the following algorithm which approximates the
running median.  I got it from an article in a peer-reviewed journal, but I
can't locate the reference at the moment.

my_median = some_reasonable_default
step_size = some_value_depending_on_your_application

def adjust_median(value):
global my_median
if my_median < value:
my_median += step_size
else:
my_median -= step_size

Let me know if you have any questions about it.

--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When to derive from object?

2009-10-13 Thread Terry Reedy

Paul Rudin wrote:

Benjamin Kaplan  writes:


It's redundant. Python 3 cleaned up a lot of the warts that appeared
in Python over the years. Old-style classes (classes that didn't
inherit from object) were one of them. Every class in Python 3 is
derived from object whether you specify it or not.


... it could be argued that having two ways to specify the same thing
(derivation from object explictly or implicitly) is a wart in itself :/


Every function with default arguments can be called two or more ways.
Every function that returns None can be written two or more ways.
;-)

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


Re: setting variables in the local namespace

2009-10-13 Thread Mick Krippendorf
Carl Banks schrieb:
> Lemme guess.
> 
> You tried this at the interactive prompt and concluded it worked in
> general, right?

Yes. Thank you for enlighten me.

> One of these days we're going to have a thread like this where no one
> makes this mistake.  Don't know when, but one day it will happen.

But at least it will not be my mistake anymore.

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


problem with running os.path.walk()

2009-10-13 Thread MalC0de
heya there,
where's the problem with the following code ? I couldn't see any
result while running as a script :


#!/usr/bin/python
import time
import os
def walker2(arg,dirname,filenames):
cutoff = time.time() - (arg * 24 * 60 * 60)
for filename in filenames :
stats = os.stat(dirname + os.sep + filename)
modified = stats[8]
if modified >= cutoff :
print dirname + os.sep + filename

os.path.walk('C:\\windows\\system',walker2,30)



if I have problem with it then let me know and if is possible please
fix it up .
thnx .
-- 
http://mail.python.org/mailman/listinfo/python-list


python-apache configuration

2009-10-13 Thread Bhanu Mangipudi
>
> Hi,
>
> I am new to python I have few questions regarding configuring apache with
> python.


I have a hello_world.py file in /var/www/html/testing/ with permissions
755, but when I try to access http://localhost/testing/helloworl.py in mu
web browser. The web browser is showing the out put in this way


#!/usr/bin/python print "Content-Type: text/html" print print """\ Hello
World! """



 Please help me to solve this issue

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


Re: organizing your scripts, with plenty of re-use

2009-10-13 Thread Stef Mientki




[snip]

The key is to put all the core functionality into a package, and 
place the package where Python can find it. Also, it's a good idea 
to use relative imports from inside the package. There is no need to 
juggle with sys.path nor even set PYTHONPATH nor import __main__ nor 
play any strange games; it Just Works (tm).



please don't get angry,
I'm not a programmer, I'm just a human ;-)

Hierarchical choices are done on todays knowledge, tomorrow we might 
have different views and want/need to arrange things in another way.

An otter may become a reptile ;-)
So from the human viewpoint the following should be possible (and is 
for example possible in Delphi)


- I can move the complete project anywhere I like and it should still 
work without any modifications (when I move my desk I can still do my 
work)


Move a complete package anywhere along the PYTHONPATH and it will 
still work.  Check.


- I can move any file in he project to any other place in the project 
and again everything should work without any modifications ( when I 
rearrange my books, I can still find a specific book)


Move any file in any directory to any other spot in that same 
directory and it will still work.  Check.  ;-)

sub ;-)


Humans are a lot smarter than computers.  Even 'just humans'.  ;-)  If 
you move your book, then can't find it on the last shelf it used to be 
on, you look on other shelves, you look on your desk, you look on the 
coffe table, you look in your car, etc, etc, and so forth.  If you 
move a file in a package to somewhere else, and you don't tell the 
package where it's at, it's not going to start looking all over the 
hard-drive for it.

I didn't say "whole the world";-)
  If that were the case you would have to be extra careful to have 
every module's name be distinct, and then what's the point of having 
packages?

Yes, I still wonder !

cheers,
Stef


~Ethan~

In my humble opinion if these actions are not possible, there must be 
redundant information in the collection. The only valid reason for 
redundant information is to perform self healing (or call it error 
correction), and here we have a catch-22.


cheers,
Stef Mientki




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


Re: Python XMLRPC question

2009-10-13 Thread Falcolas
On Oct 13, 12:47 pm, prasanna  wrote:
> In using Python's XMLRPC, there is a statement that gets printed on
> stdout of the form:
>                  localhost - - [12/Oct/2009 23:36:12] "POST /RPC2 HTTP/
> 1.0" 200 -
>
> Where does this message originate? Can I turn it off, or at least
> redirect it into a logging file? I am planning to run the server code
> automatically at start up and wouldn't have a terminal window open to
> get this message. I guess I could redirect/pipe it to a file, but it
> would be more useful I could send it to the same log file that I have
> the server writing other messages to.
>
> Thanks for any help.

Looking back through the SimpleXMLRPCServer code, it appears that this
happens if the logRequests parameter in the SimpleXMLRPCServer class
initialization is set to True, which it is by default. The underlying
implementation of the logging is in BaseHTTPServer.py, which uses
sys.stderr.

Looks like the simplest way to change that would be to inherit from
the SimpleXMLRPCRequestHandler class and implement your own
log_request method. You could then pass that to the SimpleXMLRPCServer
constructor.

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


Re: efficient running median

2009-10-13 Thread Paul Rubin
sturlamolden  writes:
> > 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.
> 
> QuickSelect will find the median in O(log n) time.

That makes no sense, you can't even examine the input in O(log n) time.

Anyway, the problem isn't to find the median of n elements.  It's to
find n different medians of n different sets.  You might be able to
get close to linear time though, depending on how the data acts.
-- 
http://mail.python.org/mailman/listinfo/python-list


Reply Delays

2009-10-13 Thread Ned Deily
In article 
<[email protected]>,
 Chris Colbert  wrote:

> Heh, for whatever reason, your post is dated earlier than my response,
> but wasn't here when I sent mine. [...]

It's not always obvious but this "forum" is multiplexed in several 
places.  It's available as a Usenet newsgroup (comp.lang.python), as a 
mailing list from python.org 
(http://mail.python.org/mailman/listinfo/python-list), as a Google 
group, and in various formats from consolidators like gmane.org 
(http://dir.gmane.org/gmane.comp.python.general).  All of those 
"channels" are bi-directional so there are often latency delays while a 
reply from one channel pushes its way upstream and back out to the 
various other channels.  Sometimes those latencies are really long when 
one channel gets a little slow.  So, if a fast moving thread, replies 
will often cross in the pipeline, adding another level of confusion 
("Didn't they read first before posting??").  It's sort of amazing that 
it all works as well as it does.

-- 
 Ned Deily,
 [email protected]

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


Re: Python XMLRPC question

2009-10-13 Thread Gabriel Genellina

En Tue, 13 Oct 2009 16:55:09 -0300, Falcolas  escribió:


On Oct 13, 12:47 pm, prasanna  wrote:

In using Python's XMLRPC, there is a statement that gets printed on
stdout of the form:
                 localhost - - [12/Oct/2009 23:36:12] "POST /RPC2 HTTP/
1.0" 200 -

Where does this message originate? Can I turn it off, or at least
redirect it into a logging file? I am planning to run the server code
automatically at start up and wouldn't have a terminal window open to
get this message. I guess I could redirect/pipe it to a file, but it
would be more useful I could send it to the same log file that I have
the server writing other messages to.

Thanks for any help.


Looking back through the SimpleXMLRPCServer code, it appears that this
happens if the logRequests parameter in the SimpleXMLRPCServer class
initialization is set to True, which it is by default. The underlying
implementation of the logging is in BaseHTTPServer.py, which uses
sys.stderr.

Looks like the simplest way to change that would be to inherit from
the SimpleXMLRPCRequestHandler class and implement your own
log_request method. You could then pass that to the SimpleXMLRPCServer
constructor.


I think it's easier to pass logRequests=False when creating the server.

--
Gabriel Genellina

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


Re: organizing your scripts, with plenty of re-use

2009-10-13 Thread Stef Mientki

Stephen Hansen wrote:



On Mon, Oct 12, 2009 at 4:15 PM, Stef Mientki > wrote:


Hierarchical choices are done on todays knowledge, tomorrow we
might have different views and want/need to arrange things in
another way.
An otter may become a reptile ;-)
So from the human viewpoint the following should be possible (and
is for example possible in Delphi)
- I can move the complete project anywhere I like and it should
still work without any modifications (when I move my desk I can
still do my work)


This is readily doable in Python; in fact it should just work. You 
only need to ensure where you move it ends up in the PYTHONPATH-- but 
that'll take at most just one change, once. You can use batch scripts 
to alter it if you really need to, but that's all you would need to do.
 


- I can move any file in he project to any other place in the
project and again everything should work without any modifications
( when I rearrange my books, I can still find a specific book)

In my humble opinion if these actions are not possible, there must
be redundant information in the collection. The only valid reason
for redundant information is to perform self healing (or call it
error correction), and here we have a catch-22.


This is the problem; this is just incorrect thinking and if one's 
programming in Python needs to be excised. Things don't work like 
that, and trying to make it work like that will result in you going 
against the grain of /how/ Python works, and life will become difficult.

I agree it's difficult at the start, but after that,
you can create a new script / module everywhere you like ( within the 
projects), and every module is available,
and you can use (almost) any file as a self running program or as a 
library module.


Packages are meaningful in Python. They aren't just directories which 
you're organizing for your human-programmer's convenience and 
understanding. They are a fundamental structure of objects and must be 
treated as such.

Well I missed that, I can't see the "higher" meaning of packages,
so if someone has a good link that explains the "higher" meaning of 
packages,

I'ld be much obliged.


Python doesn't think in terms of files. You can't just move files 
around within a project because that's a semantically significant change. 

It's just like you can't move a method on a class to another class, 
and expect everything to just work.

No, that's like tearing a page from a book and gluing it in another book
and the story get's
So the difference in my reasoning might be a mis-concept of the largest 
atomic part, which in my view is a class / function.


A package is just like a class, its a significant, structural 
component in the code itself. It contains files, but those files 
aren't what's significant to Python-- what's significant is that they 
are modules, which are also just like a class. They're objects which 
contain other objects. 

Every package creates a namespace of objects it contains; those 
objects are modules (and other things that may be defined in 
__init__.py). Every module creates a namespace of objects it contains. 
Classes create a namespace of objects they contain.


These are all objects. A package isn't a directory you're organizing 
-- its an object which contains other objects.
Well I'm completely lost now, but that's probably because I'm not a 
programmer,

and I don't know the exact meaning of these words.
So I don't see a class as an object, just as a definition of 
"functionality".
A module, a file, a package, a directory (with it's subdirectories) is 
just a collector for classes.
I can freely move a class around within any of these containers without 
affecting the functionality of that class or anything that really uses 
that class.




If you try to change this so that there's no object organization of an 
object hierarchy, then you're going to create significant issues for 
maintenance and have to jump through all kinds of hoops to try to 
accomplish it. It sounds like you want a system where each file is a 
unique module, and its particular location isn't important-- the 
filename creates a unique identity, a top-level namespace which can be 
accessed from anywhere.

yes that's about the view I've now.


Python's object model just doesn't work like that. There's nothing 
wrong with how Python works in this regard, its simply different, and 
if you try to program Delphi in Python, you'll run into lots of 
problems. Just like if you try to program Java or C in Python. They're 
different languages with different object models, and you have to 
adapt to the object model of the language if you want to use the 
language effectively.


I would love to hear one (and preferable more ;-) feature that a package 
adds to a collection of classes.


cheers,
Stef


HTH,

--S

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


Re: problem with running os.path.walk()

2009-10-13 Thread Gabriel Genellina
En Tue, 13 Oct 2009 16:32:07 -0300, MalC0de   
escribió:



where's the problem with the following code ? I couldn't see any
result while running as a script :


#!/usr/bin/python
import time
import os
def walker2(arg,dirname,filenames):
cutoff = time.time() - (arg * 24 * 60 * 60)
for filename in filenames :
stats = os.stat(dirname + os.sep + filename)
modified = stats[8]
if modified >= cutoff :
print dirname + os.sep + filename

os.path.walk('C:\\windows\\system',walker2,30)

if I have problem with it then let me know and if is possible please
fix it up .


Because all files in your c:\windows\system directory are older than one  
month?
os.walk is simpler to use; stats.st_mtime is less "magical" than stats[8];  
and I'd use os.path.join(dirname, filename) instead of +os.sep+


--
Gabriel Genellina

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


Re: organizing your scripts, with plenty of re-use

2009-10-13 Thread Buck
On Oct 13, 9:37 am, Ethan Furman  wrote:
> Buck wrote:
>  >I'd like to get to zero-installation if possible. It's easy with
>  >simple python scripts, why not packages too? I know the technical
>  >reasons, but I haven't heard any practical reasons.
>
> I don't think we mean the same thing by "zero-installation"... seems to
> me that if you have to copy it, check it out, or anything to get the
> code from point A to point 'usable on your computer', then you have done
> some sort of installation.

I think most people would agree that installation is whatever you need
to do between downloading the software and being able to use it. For
GNU packages, it's './configure && make && make install'. For Python
packages, it's usually './setup.py install'.

> > Steven had the nicest workaround (with the location = __import__
> > ('__main__').__file__ trick), but none of them solve the problem of
> > the OP: organization of runnable scripts. So far it's been required to
> > place all runnable scripts directly above any used packages. The
> > workaround that Gabriel has been touting requires this too.
>
> Wha?  "Place all runnable scripts directly above any used packages?"  I
> must have missed something major in this thread.  The only thing
> necessary is to have the package being imported to be somewhere in
> PYTHONPATH.

The only way to get your packages on the PYTHONPATH currently is to:
   * install the packages to site-packages  (I don't have access)
   * edit the PYTHONPATH all users' environment  (again, no access)
   * create some boilerplate that edits sys.path at runtime (various
problems in previous post)
   * put your scripts directly above the package (this seems best so
far, but forces a flat hierarchy of scripts)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: RabbitMQ vs ApacheQpid (AMQP)

2009-10-13 Thread Roger Binns
jacopo wrote:
> I am considering two solutions for a distributed system:  either
> RabbitMQ with py-amqplib or ApacheQpid with its own set of API. 

Have you considered the multiprocessing module?

http://docs.python.org/library/multiprocessing.html#using-a-remote-manager

Roger

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


Re: setting variables in the local namespace

2009-10-13 Thread Carl Banks
On Oct 13, 12:23 pm, Mick Krippendorf  wrote:
> Carl Banks schrieb:
>
> > Lemme guess.
>
> > You tried this at the interactive prompt and concluded it worked in
> > general, right?
>
> Yes. Thank you for enlighten me.
>
> > One of these days we're going to have a thread like this where no one
> > makes this mistake.  Don't know when, but one day it will happen.
>
> But at least it will not be my mistake anymore.

One by one, baby.  It just amazes me so many people don't think to
check inside a function, especially when we normally think of local
variables as being local to a function.  If I wasn't familiar with
locals(), it probably wouldn't even occur to me to try using locals()
at the top level.  I'd expect it to throw an exception like "No locals
at global level".


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


MUD Game Programmming - Python Modules in C++

2009-10-13 Thread Christopher Lloyd
Hello all,

I'm new to Python and new to this list, although I've done some digging in the 
archives and already read up on the problem I'm about to describe.

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.

The problem that I'm having is making the Python part work. Take the following 
code:

// This program shows you how to integrate Python in a very basic manner

#include 
#include 
#include "Python.h"

int main()
{
std::cout << "Starting Python Demo Test" << std::endl;

Py_Initialize();// initialize python

std::string str;
std::getline( std::cin, str );
while( str != "end" )
{
PyRun_SimpleString( const_cast( str.c_str() ) );
std::getline( std::cin, str );
}

Py_Finalize();  // shut down python

std::cout << "Demo Complete!" << std::endl;

return 0;
}

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'

>From my reading, it looks like there's a problem with compiling in release 
>mode or debug mode either in C++ or in Python. At the moment, I'm using the 
>Python Windows .exe download. I'm not using version 2.6 for any particular 
>reason - I'm also trying 2.2 and 2.3.

So I don't have the Python source code downloaded (and I'm not entirely sure 
what to do with it if I do download it, since the instructions for the .tar 
file are for Linux, not windows). The Windows executables for 2.2 and 2.3 came 
on a CD with the book, so it seems clear that the author thought that the 
source code wasn't required anyway.

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.

It's been suggested that I replace the first part of my C++ code with the 
following, and then try to compile in release mode:

#ifdef _DEBUG
#undef _DEBUG
#include 
#define _DEBUG
#else
#include 
#endif

I've tried this, and it compiles successfully but when run, the program is the 
same - It doesn't work.

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).

I'd be very grateful for any help or asvice people might have on this.

Thanks,

Chris Lloyd

-- 


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


  1   2   >