Re: Split string but ignore quotes

2009-09-30 Thread Processor-Dev1l
On Sep 29, 5:11 pm, Scooter slbent...@gmail.com wrote:
 I'm attempting to reformat an apache log file that was written with a
 custom output format. I'm attempting to get it to w3c format using a
 python script. The problem I'm having is the field-to-field matching.
 In my python code I'm using split with spaces as my delimiter. But it
 fails when it reaches the user agent because that field itself
 contains spaces. But that user agent is enclosed with double quotes.
 So is there a way to split on a certain delimiter but not to split
 within quoted words.

 i.e. a line might look like

 2009-09-29 12:00:00 - GET / Mozilla/4.0 (compatible; MSIE 7.0;
 Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC
 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022)http://somehost.com200
 1923 1360 31715 -

Best option for you is to use shlex module as Björn said.
This is quite a simple question and you would find it on your own for
sure if you search python docs a little bit :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple if-else question

2009-09-30 Thread Steven D'Aprano
On Tue, 29 Sep 2009 22:29:10 -0700, John Yeung wrote:

 On Sep 29, 1:15 pm, Carl Banks pavlovevide...@gmail.com wrote:
 Hmm, I wonder if Python should emit a warning if an else is used on a
 for block with no break inside.  I don't think the else can be invoked
 in any other way.  As a bonus it could catch some cases where people
 mistakenly use it thinking it will execute [only] when there are no
 iterations.
 
 [Edit from Duncan Booth]
 
 I would definitely be in favor of a warning.  Yes, people should read
 the docs more carefully, and yes, it would cost a certain amount of
 annoyance to implement this.  But I don't think it would get in people's
 way if they do know how to use else, 

Of course it would. It would mean that everybody who knows how to use 
for...else correctly would have to deal with a totally useless warning.


 and I think it would cut down on
 the number of questions from mystified beginners, some of whom are much
 more aggressive than this particular OP about claiming that Python is
 broken (when it's actually behaving as designed).

By raising a warning, you are implying that the behaviour is broken, or 
at least suspicious. Warnings mean something needs to be warned against 
-- don't do this. Warnings shouldn't be perfectly legitimate behaviours 
on the off-chance that the user is confused. Warning, are you sure you 
want to put the car into reverse? Perhaps you meant neutral?

What would the warning say?

Warning, you have used a legitimate Python control structure but you 
might be confused by it.

Warning, did you mean if...else instead of for...else?

Then we can add a whole lot of extra warnings to protect newbies who 
don't read docs (and probably won't read the warnings either) from 
themselves:

Warning, did you mean obj(1) instead of obj[1]?

Warning, did you mean def f(object) instead of class f(object)?

Warning, did you mean class f(object) instead of def f(object)?

Warning, did you mean 2*3 instead of 2**3?

Warning, did you mean %s instead of %x?

Warning, we think you're helpless and don't know what you want, perhaps 
you should be programming in PHP?



I'm sympathetic to the desire to educate the n00bs, and in fact I've even 
suggested similar warnings myself. But I've been convinced that this is 
the wrong approach. Certainly the language shouldn't assume the 
programmer is confused. If this sort of warning belongs anywhere (and 
that's a big if), it belongs in an IDE.



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


Re: How to pass a global variable to a module?

2009-09-30 Thread Hendrik van Rooyen
On Tuesday, 29 September 2009 20:24:53 Mars creature wrote:

 From the link Gregor posted, it seems no way to share variable between

 modules.

 I can understand the point that global variables tends to mess up
 programs.

 Assume that I have 10 parameters need to pass to the function. If
 these parameters are fixed, I can use another module to store these 10
 parameters, and import to the module, as suggested by jean-michel. But
 what if these 10 parameters will be changed in the main program?
 Passing the variable to the function as a parameter suggested by Rami
 will certainly do, but I am wondering weather there are other ways.
 What you'd like to code it?

You can put all the 10 things in a file called say my_params.py.

Then where you need it, you do either:

from my_params import *  to make them available where needed,

or:

import my_params as p   and access them as:

print p.my_parm_1,p.my_parm_2,p.my_parm_3,p.my_parm_4

- Hendrik

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


Re: Partial directory search question

2009-09-30 Thread lallous
chad cdal...@gmail.com wrote in message 
news:4e260ef3-8b0e-4613-a4f8-1c267e875...@u16g2000pru.googlegroups.com...

On Sep 29, 7:20 pm, Tim Chase python.l...@tim.thechases.com wrote:

 What's the sanest way to print out all the files in the directory that
 start with the underscore? Ie, I just want to list _1, _2, _3, _4.

I'd use a string's join() method to combine the results of a
list-comprehension or generator that filtered the output of
os.listdir() based on the startswith() method of the strings.

Left intentionally oblique and code-free because this sounds a
bit like a home-work problem.  If you're a python coder, that
should make pretty decent sense and be a one-liner to implement.

-tkc


Okay, sorry for the delay to the response. I got side tracked trying
to stalk, I mean talk to the 59 year old neighbor girl. Anyways, I
couldn't get it to one in one line. Here is what I did...

% more rec.py
#!/usr/local/bin/python

import os
import time

for filename in os.listdir(/usr/bbs/confs/september):
#stat = os.stat(filename)
if filename.startswith(_):
   print filename



L = [filename for filename in os.listdir(/usr/bbs/confs/september) if 
filename.startswith(_)]


Now you have a list with the desired filtered names.


./rec.py
_1
_2
_3
_4
_5
_6
_7
_8

It correctly prints out all the files in the directory that start with
an underscore. 


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


Re: UnboundLocalError with extra code after return

2009-09-30 Thread Duncan Booth
Rich Healey healey.r...@gmail.com wrote:

 It seems that my problem was that I can't assign a new function to the
 name func within the callonce() function. I can however interact with
 the func object (in this case storing information about whether or not
 I'd called it in it's __RECALL item.
 
 Is there a cleaner solution?
 

You want to call something, and have that something remember state between 
each call? If it was me I'd define a class rather than a function.

 class CallableOnlyOnce(object):
def __init__(self, func):
self.func = func
def __call__(self):
f = self.func
if f:
self.func = None
return f()


 def callonce(func):
return CallableOnlyOnce(func)

 @callonce
def t2():
print T2 called


 t2()
T2 called
 t2()
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Most active coroutine library project?

2009-09-30 Thread Hendrik van Rooyen
On Wednesday, 30 September 2009 04:16:45 Grant Edwards wrote:

 Assembler macros are indeed a lost art.  Back in the day, I
 remember seeing some pretty impressive macro libraries layered
 2-3 deep.  I've done assember macros as recently as about 2-3
 years go because it was the easiest way to auto-magically
 generate lookup tables for use by C programs (macro assemblers
 always have a repeat directive, and cpp doesn't).

  The 803x bit handling is, in my arrogant opinion, still the
  best of any processor. - jump if bit set then clear as an
  atomic instruction rocks.

 The bit-addressing mode was (and still is) cool. However, the
 stack implementation hurts pretty badly now that memory is
 cheap.

 I shouldn't criticize the 8051.  I remember switching from the
 8048 to the 8051 (8751 actually, at about $300 each) and
 thinking it was wonderful.  [Anybody who remembers fighting
 with the 8048 page boundaries knows what I mean.]

You were lucky - I started with an 8039 and the 8048 was a step up!

You are right about the stack - there are a lot of implementations now with 
two or more data pointers, which make a big difference.  If only someone 
would build one with a two byte stack pointer that points into movx space, 
the thing would fly faster again.  It would make a stunning difference to the 
multitasking performance if you do not have to store the whole stack. Of 
course, if you are mucking around in assembler, then the 128 bytes at the top 
of the internal memory is often enough.

This is getting a bit far away from python and coroutines, though. :-)

- Hendrik

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


unicode issue

2009-09-30 Thread gentlestone
Why don't work this code on Python 2.6? Or how can I do this job?

_MAP = {
# LATIN
u'À': 'A', u'Á': 'A', u'Â': 'A', u'Ã': 'A', u'Ä': 'A', u'Å': 'A',
u'Æ': 'AE', u'Ç':'C',
u'È': 'E', u'É': 'E', u'Ê': 'E', u'Ë': 'E', u'Ì': 'I', u'Í': 'I',
u'Î': 'I',
u'Ï': 'I', u'Ð': 'D', u'Ñ': 'N', u'Ò': 'O', u'Ó': 'O', u'Ô': 'O',
u'Õ': 'O', u'Ö':'O',
u'Ő': 'O', u'Ø': 'O', u'Ù': 'U', u'Ú': 'U', u'Û': 'U', u'Ü': 'U',
u'Ű': 'U',
u'Ý': 'Y', u'Þ': 'TH', u'ß': 'ss', u'à':'a', u'á':'a', u'â': 'a',
u'ã': 'a', u'ä':'a',
u'å': 'a', u'æ': 'ae', u'ç': 'c', u'è': 'e', u'é': 'e', u'ê': 'e',
u'ë': 'e',
u'ì': 'i', u'í': 'i', u'î': 'i', u'ï': 'i', u'ð': 'd', u'ñ': 'n',
u'ò': 'o', u'ó':'o',
u'ô': 'o', u'õ': 'o', u'ö': 'o', u'ő': 'o', u'ø': 'o', u'ù': 'u',
u'ú': 'u',
u'û': 'u', u'ü': 'u', u'ű': 'u', u'ý': 'y', u'þ': 'th', u'ÿ': 'y',
# LATIN_SYMBOLS
u'©':'(c)',
# GREEK
u'α':'a', u'β':'b', u'γ':'g', u'δ':'d', u'ε':'e', u'ζ':'z',
u'η':'h', u'θ':'8',
u'ι':'i', u'κ':'k', u'λ':'l', u'μ':'m', u'ν':'n', u'ξ':'3',
u'ο':'o', u'π':'p',
u'ρ':'r', u'σ':'s', u'τ':'t', u'υ':'y', u'φ':'f', u'χ':'x',
u'ψ':'ps', u'ω':'w',
u'ά':'a', u'έ':'e', u'ί':'i', u'ό':'o', u'ύ':'y', u'ή':'h',
u'ώ':'w', u'ς':'s',
u'ϊ':'i', u'ΰ':'y', u'ϋ':'y', u'ΐ':'i',
u'Α':'A', u'Β':'B', u'Γ':'G', u'Δ':'D', u'Ε':'E', u'Ζ':'Z',
u'Η':'H', u'Θ':'8',
u'Ι':'I', u'Κ':'K', u'Λ':'L', u'Μ':'M', u'Ν':'N', u'Ξ':'3',
u'Ο':'O', u'Π':'P',
u'Ρ':'R', u'Σ':'S', u'Τ':'T', u'Υ':'Y', u'Φ':'F', u'Χ':'X',
u'Ψ':'PS', u'Ω':'W',
u'Ά':'A', u'Έ':'E', u'Ί':'I', u'Ό':'O', u'Ύ':'Y', u'Ή':'H',
u'Ώ':'W', u'Ϊ':'I', u'Ϋ':'Y',
# TURKISH
u'ş':'s', u'Ş':'S', u'ı':'i', u'İ':'I', u'ç':'c', u'Ç':'C',
u'ü':'u', u'Ü':'U',
u'ö':'o', u'Ö':'O', u'ğ':'g', u'Ğ':'G',
# RUSSIAN
u'а':'a', u'б':'b', u'в':'v', u'г':'g', u'д':'d', u'е':'e',
u'ё':'yo', u'ж':'zh',
u'з':'z', u'и':'i', u'й':'j', u'к':'k', u'л':'l', u'м':'m',
u'н':'n', u'о':'o',
u'п':'p', u'р':'r', u'с':'s', u'т':'t', u'у':'u', u'ф':'f',
u'х':'h', u'ц':'c',
u'ч':'ch', u'ш':'sh', u'щ':'sh', u'ъ':'', u'ы':'y', u'ь':'',
u'э':'e', u'ю':'yu', u'я':'ya',
u'А':'A', u'Б':'B', u'В':'V', u'Г':'G', u'Д':'D', u'Е':'E',
u'Ё':'Yo', u'Ж':'Zh',
u'З':'Z', u'И':'I', u'Й':'J', u'К':'K', u'Л':'L', u'М':'M',
u'Н':'N', u'О':'O',
u'П':'P', u'Р':'R', u'С':'S', u'Т':'T', u'У':'U', u'Ф':'F',
u'Х':'H', u'Ц':'C',
u'Ч':'Ch', u'Ш':'Sh', u'Щ':'Sh', u'Ъ':'', u'Ы':'Y', u'Ь':'',
u'Э':'E', u'Ю':'Yu', u'Я':'Ya',
# UKRAINIAN
u'Є':'Ye', u'І':'I', u'Ї':'Yi', u'Ґ':'G', u'є':'ye', u'і':'i',
u'ї':'yi', u'ґ':'g',
# CZECH
u'č':'c', u'ď':'d', u'ě':'e', u'ň':'n', u'ř':'r', u'š':'s',
u'ť':'t', u'ů':'u',
u'ž':'z', u'Č':'C', u'Ď':'D', u'Ě':'E', u'Ň':'N', u'Ř':'R',
u'Š':'S', u'Ť':'T', u'Ů':'U', u'Ž':'Z',
# POLISH
u'ą':'a', u'ć':'c', u'ę':'e', u'ł':'l', u'ń':'n', u'ó':'o',
u'ś':'s', u'ź':'z',
u'ż':'z', u'Ą':'A', u'Ć':'C', u'Ę':'e', u'Ł':'L', u'Ń':'N',
u'Ó':'o', u'Ś':'S',
u'Ź':'Z', u'Ż':'Z',
# LATVIAN
u'ā':'a', u'č':'c', u'ē':'e', u'ģ':'g', u'ī':'i', u'ķ':'k',
u'ļ':'l', u'ņ':'n',
u'š':'s', u'ū':'u', u'ž':'z', u'Ā':'A', u'Č':'C', u'Ē':'E',
u'Ģ':'G', u'Ī':'i',
u'Ķ':'k', u'Ļ':'L', u'Ņ':'N', u'Š':'S', u'Ū':'u', u'Ž':'Z'
}

def downcode(name):

 downcode(uŽabovitá zmiešaná kaša)
u'Zabovita zmiesana kasa'

for key, value in _MAP.iteritems():
name = name.replace(key, value)
return name
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode issue

2009-09-30 Thread Andre Engels
On Wed, Sep 30, 2009 at 9:34 AM, gentlestone tibor.b...@hotmail.com wrote:
 Why don't work this code on Python 2.6? Or how can I do this job?

Please be more specific than it doesn't work:
* What exactly are you doing
* What were you expecting the result of that to be
* What is the actual result?

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


Re: Most active coroutine library project?

2009-09-30 Thread Paul Rubin
Hendrik van Rooyen hend...@microcorp.co.za writes:
 You were lucky - I started with an 8039 and the 8048 was a step up!
 
 This is getting a bit far away from python and coroutines, though. :-)

Getting away from python in the opposite direction, if you click

   http://cufp.galois.com/2008/schedule.html

the second presentation Controlling Hybrid Vehicles with Haskell
might interest you.  Basically it's about a high level DSL that
generates realtime control code written in C.  From the slides:

* 5K lines of Haskell/atom replaced 120K lines of matlab, simulink,
  and visual basic.
* 2 months to port simulink design to atom.
* Rules with execution periods from 1ms to 10s all scheduled at
  compile time to a 1 ms main loop.
* Atom design clears electronic/sw testing on first pass.
* Currently in vehicle testing with no major issues.

Code is here: http://hackage.haskell.org/package/atom  

Blurb: Atom is a Haskell DSL for designing hard realtime embedded
programs. Based on conditional term rewriting, atom will compile a
collection of atomic state transition rules to a C program with
constant memory use and deterministic execution time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple if-else question

2009-09-30 Thread Terry Reedy

John Yeung wrote:

On Sep 29, 1:15 pm, Carl Banks pavlovevide...@gmail.com wrote:

Hmm, I wonder if Python should emit a warning if an else is
used on a for block with no break inside.  I don't think the
else can be invoked in any other way.  As a bonus it could
catch some cases where people mistakenly use it thinking it
will execute [only] when there are no iterations.


[Edit from Duncan Booth]

I would definitely be in favor of a warning.  Yes, people should read
the docs more carefully, and yes, it would cost a certain amount of
annoyance to implement this.  But I don't think it would get in
people's way if they do know how to use else, and I think it would cut
down on the number of questions from mystified beginners, some of whom
are much more aggressive than this particular OP about claiming that
Python is broken (when it's actually behaving as designed).


This sort of warning is appropriate for code checkers like Pylint, 
Pychecker, but not for the running interpreter.


tjr


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


Re: Can I make these getters/setters?

2009-09-30 Thread Bruno Desthuilliers

Michael George Lerner a écrit :

Hi,

As part of my GUI, I have lots of fields that people can fill in,
defined like this:

self.selection =  Pmw.EntryField(group.interior(),
labelpos='w',
label_text='Selection to use:
',
value='(polymer)',
)

I then use self.selection.get_value() and self.selection.set_value(),
and those two functions are the only ways in which I care about
self.selection. I've never really used properties, getters or setters
before. I tried this, but it didn't work:

def __init__(self):
self._selection =  Pmw.EntryField(group.interior(),
labelpos='w',
label_text='Selection to use:
',
value='(polymer)',
)
self.selection = property(self._selection.get_value
(),self._selection.set_value())


What you're passing here are the results of the calls to .get_value and 
.set_value, not the methods themselves. You'd want:


self.selection = property(
   self._selection.get_value,
   self._selection.set_value
   )

But as Steven already mentioned, property only works as a class 
attribute, not as an instance attribute. What you need here is:


class Parrot(object):
  def __init__(self):
 self._selection =  Pmw.EntryField(...) 



  selection = property(
  lambda self: self._selection.get_value(),
  lambda self, value: self._selection.set_value(value)
  )



Of course, I really have ~40 things that I'd like to do this for, not
just one, so I'd like to find a fairly concise syntax.



the property object is just one possible application of the descriptor 
protocol. You could write your own custom descriptor (warning: untested 
code, may contains typos etc):


class FieldProperty(object):
def __init__(self, fieldname):
self._fieldname = fieldname

def __get__(self, instance, cls):
if instance is None:
   # can't work on the class itself
   return self
return getattr(instance, self._fieldname).get_value()

def __set__(self, instance, value):
getattr(instance, self._fieldname).set_value(value)


class Parrot(object):
  def __init__(self):
 self._selection =  Pmw.EntryField(...) 



  selection = FieldProperty(_selection)



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


Re: unicode issue

2009-09-30 Thread gentlestone
On 30. Sep., 09:41 h., Andre Engels andreeng...@gmail.com wrote:
 On Wed, Sep 30, 2009 at 9:34 AM, gentlestone tibor.b...@hotmail.com wrote:
  Why don't work this code on Python 2.6? Or how can I do this job?

 Please be more specific than it doesn't work:
 * What exactly are you doing
 * What were you expecting the result of that to be
 * What is the actual result?

 --
 André Engels, andreeng...@gmail.com

* What exactly are you doing
replace non-ascii characters - see doctest documentation

* What were you expecting the result of that to be
see doctest documentation

* What is the actual result?
the actual result is unchanged name
-- 
http://mail.python.org/mailman/listinfo/python-list


a questions about thread-safety of boolean variables

2009-09-30 Thread Charlie Dickens
Hi,if I have a class A that contains a boolean variable named x, is it safe
to read and change it from different threads without using locks?
Is it guaranteed that A.x will be always True or False, and not any other
weird value that that causes it to be inconsistent (assuming I only set it
to True or False) ?

I have a = A()
first thread does:
if a.x is True :
pass

2nd thread does:
a.x = False

is it safe?

and what if x was a dict ? especially if the only values that are set in the
dictionary are simple: booleans, integers, floats, strings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Business issues regarding adapting Python

2009-09-30 Thread Vladimir Ignatov
 Python-related programming job. Positions both very rare (comparing
 with Java/C++ - maybe 1/100) and not pays well. And about 99% of them
 are web+Django.

 To who/what are you replying?

Nope. Just a replic.
BTW I agreed - just peek a good programmers and let them learn python.
Literally in a days they will be able to write average production
code. Don't expect hovewer that this code will be a very pythonic.
Especially from guys with strong C++ - like background.  My advise -
show em a generators. IMHO that is a mindblowing feature for
everybody who get up from static-type languages.

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


Re: unicode issue

2009-09-30 Thread Andre Engels
I get the feeling that the problem is with the Python interactive
mode. It does not have full unicode support, so uŽabovitá zmiešaná
kaša is changed to u'\x8eabovit\xe1 zmie\x9aan\xe1 ka\x9aa'. If you
call your code from another program, it might work correctly.


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


Re: a questions about thread-safety of boolean variables

2009-09-30 Thread steve

Hi,

On 09/30/2009 01:53 PM, Charlie Dickens wrote:

Hi,
if I have a class A that contains a boolean variable named x, is it safe
to read and change it from different threads without using locks?
Is it guaranteed that A.x will be always True or False, and not any
other weird value that that causes it to be inconsistent (assuming I
only set it to True or False) ?

The guarantee for A.x being only True or False, is independent of whether you 
use locks or not. It depends entirely on code that assigns to A.x.



I have a = A()
first thread does:
if a.x is True :
 pass

2nd thread does:
a.x = False

is it safe?


what if you have code like this:

Thread 1
if a.x is True:
doSomething()

Thread 2
a.x == False

..and thread 2 executes *after* thread 1's 'if' condition but *before* 
doSomething(). If that behavior is acceptable in your application, you possibly 
don't even need a.x.




and what if x was a dict ? especially if the only values that are set in
the dictionary are simple: booleans, integers, floats, strings


Same logic applies.

hth,
cheers,
- steve

--
random non tech spiel: http://lonetwin.blogspot.com/
tech randomness: http://lonehacks.blogspot.com/
what i'm stumbling into: http://lonetwin.stumbleupon.com/
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode issue

2009-09-30 Thread gentlestone
On 30. Sep., 10:35 h., Andre Engels andreeng...@gmail.com wrote:
 I get the feeling that the problem is with the Python interactive
 mode. It does not have full unicode support, so uŽabovitá zmiešaná
 kaša is changed to u'\x8eabovit\xe1 zmie\x9aan\xe1 ka\x9aa'. If you
 call your code from another program, it might work correctly.

 --
 André Engels, andreeng...@gmail.com

thx a lot

I spent 2 days of my life beacause of this

so doctests are unuseable for non-engish users in python - seems to be
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode issue

2009-09-30 Thread gentlestone
On 30. Sep., 10:43 h., gentlestone tibor.b...@hotmail.com wrote:
 On 30. Sep., 10:35 h., Andre Engels andreeng...@gmail.com wrote:

  I get the feeling that the problem is with the Python interactive
  mode. It does not have full unicode support, so uŽabovitá zmiešaná
  kaša is changed to u'\x8eabovit\xe1 zmie\x9aan\xe1 ka\x9aa'. If you
  call your code from another program, it might work correctly.

  --
  André Engels, andreeng...@gmail.com

 thx a lot

 I spent 2 days of my life beacause of this

 so doctests are unuseable for non-engish users in python - seems to be

yes, you are right, now it works:

def slugify(name):

 slugify(u'\u017dabovit\xe1 zmie\u0161an\xe1 ka\u0161a s.r.o')
u'zabovita-zmiesana-kasa-sro'

for key, value in _MAP.iteritems():
name = name.replace(key, value)
return defaultfilters.slugify(name)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a questions about thread-safety of boolean variables

2009-09-30 Thread Charlie Dickens
What about dictionaries? Reading values, adding new ones and the most
important: changing an existing value - can it corrupt the state of the
dictionary or that it is guaranteed that if I try to read the value of this
key, I can only get the old one or the new one, but not something weird
instead (because a different thread changed the value while this thread was
trying to read it).
About your remark that I don't need to check if a.x is True, I do need, and
indeed it is acceptable that doSomething() will run, because this logic
resides inside a loop, and it is only important for me that it will stop
entering the loop sometime (I don't care if there are n iterations or n+1 or
even n+m)

Thanks

On Wed, Sep 30, 2009 at 10:44 AM, steve st...@lonetwin.net wrote:

 Hi,

 On 09/30/2009 01:53 PM, Charlie Dickens wrote:

 Hi,
 if I have a class A that contains a boolean variable named x, is it safe
 to read and change it from different threads without using locks?
 Is it guaranteed that A.x will be always True or False, and not any
 other weird value that that causes it to be inconsistent (assuming I
 only set it to True or False) ?

  The guarantee for A.x being only True or False, is independent of whether
 you use locks or not. It depends entirely on code that assigns to A.x.

  I have a = A()
 first thread does:
 if a.x is True :
 pass

 2nd thread does:
 a.x = False

 is it safe?


 what if you have code like this:

 Thread 1
 if a.x is True:
doSomething()

 Thread 2
 a.x == False

 ..and thread 2 executes *after* thread 1's 'if' condition but *before*
 doSomething(). If that behavior is acceptable in your application, you
 possibly don't even need a.x.


 and what if x was a dict ? especially if the only values that are set in
 the dictionary are simple: booleans, integers, floats, strings

  Same logic applies.

 hth,
 cheers,
 - steve

 --
 random non tech spiel: http://lonetwin.blogspot.com/
 tech randomness: http://lonehacks.blogspot.com/
 what i'm stumbling into: http://lonetwin.stumbleupon.com/

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


Re: How to pass a global variable to a module?

2009-09-30 Thread Jean-Michel Pichavant

Mars creature wrote:

On Sep 29, 12:49 pm, Rami Chowdhury rami.chowdh...@gmail.com
wrote:
  

On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature jin...@gmail.com wrote:


Dear Python users,
  I just start to use python and love this language. I met this
problem when I try to save my functions in a separate file.
The question is how I can pass a global variable to a function which
is saved in another file. If I save the function I defined in the same
file with the main program, there is no problem after I declare the
global variable. But problem comes out when I save all the function is
a separate file. Help is very much appreciated! Thanks!
Jinbo
  
In Python, as in many other languages, I'd advise that you think about  
whether your variable needs to be global, or whether you could (or should)  
simply pass the variable to the function as a parameter.


HTH,
Rami

--
Rami Chowdhury
Never attribute to malice that which can be attributed to stupidity --  
Hanlon's Razor

408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)



Thank you guys for the prompt and helpful response.
From the link Gregor posted, it seems no way to share variable between
modules.

I can understand the point that global variables tends to mess up
programs.

Assume that I have 10 parameters need to pass to the function. If
these parameters are fixed, I can use another module to store these 10
parameters, and import to the module, as suggested by jean-michel. But
what if these 10 parameters will be changed in the main program?
Passing the variable to the function as a parameter suggested by Rami
will certainly do, but I am wondering weather there are other ways.
What you'd like to code it?
Thank you very much!
Jinbo
  
Why don't you post the function you're trying to code, with the 
parameter names ?
Write the documentation for that function, write what it is supposed to 
do, the parameters, their purpose and the returned value. Just by doing 
this, you may be able to find all by yourself what should be the correct 
function prototype.


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


Re: user authorization (with one time login) in a Python desktop application ?

2009-09-30 Thread Rüdiger Ranft
Stef Mientki schrieb:

 By making use of the one time login on windows,
 I'm not sure, but I guess the user environment variable USER  should
 hold the vald user,
 which has probably a one-to-one relation with the SID
Environment variables are *very* easy to forge. But since you use
windows, you can let windows do all the security stuff for you. Since u
use sqlite, I guess that the database is stored on a file on a disk. You
can use the file permission to give access only to the users permitted
to access the file.

But when you want to separate access at dataset/column/row level, then
sqlite is not the best tool, since every user can open the database file
with an other sqlite tool. Encryption will only take more time for rogue
users, since the key needs to be stored in your application, and can be
read by the user. I would recommend a real database server in this
case, so the permission checks are out of the reach of the users. When u
have a kerberos or active directory environment, some servers even can
use kerberos to get the user name from the client.

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


Re: Simple if-else question

2009-09-30 Thread dksr
On Sep 29, 6:38 pm, Duncan Booth duncan.bo...@invalid.invalid wrote:
 Carl Banks pavlovevide...@gmail.com wrote:
  Hmm, I wonder if Python should emit a warning if an else is used on a
  for block with no break inside.  I don't think the else can be invoked
  in any other way.  As a bonus it could catch some cases where people
  mistakenly use it thinking it will execute when there are no
  iterations.

 It will execute when there are no iterations. Did you mean to say people
 think it will execute *only* when there are no iterations?

Yes thats what I thought. for-else looks similar to if-else and in if-
else, else part is executed only when if part is not executed, but in
for-else it has entirely a different job.
Thats somewhat confusing, though it will be clear after playing around
with it.

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


Re: unicode issue

2009-09-30 Thread Dave Angel

gentlestone wrote:

Why don't work this code on Python 2.6? Or how can I do this job?

_MAP =
# LATIN
u'À': 'A', u'Á': 'A', u'Â': 'A', u'Ã': 'A', u'Ä': 'A', u'Å': 'A',
u'Æ': 'AE', u'Ç':'C',
u'È': 'E', u'É': 'E', u'Ê': 'E', u'Ë': 'E', u'Ì': 'I', u'Í': 'I',
u'Î': 'I',
u'Ï': 'I', u'Ð': 'D', u'Ñ': 'N', u'Ò': 'O', u'Ó': 'O', u'Ô': 'O',
u'Õ': 'O', u'Ö':'O',
u'Ő': 'O', u'Ø': 'O', u'Ù': 'U', u'Ú': 'U', u'Û': 'U', u'Ü': 'U',
u'Ű': 'U',
u'Ý': 'Y', u'Þ': 'TH', u'ß': 'ss', u'à':'a', u'á':'a', u'â': 'a',
u'ã': 'a', u'ä':'a',
u'å': 'a', u'æ': 'ae', u'ç': 'c', u'è': 'e', u'é': 'e', u'ê': 'e',
u'ë': 'e',
u'ì': 'i', u'í': 'i', u'î': 'i', u'ï': 'i', u'ð': 'd', u'ñ': 'n',
u'ò': 'o', u'ó':'o',
u'ô': 'o', u'õ': 'o', u'ö': 'o', u'ő': 'o', u'ø': 'o', u'ù': 'u',
u'ú': 'u',
u'û': 'u', u'ü': 'u', u'ű': 'u', u'ý': 'y', u'þ': 'th', u'ÿ': 'y',
# LATIN_SYMBOLS
u'©':'(c)',
# GREEK
u'α':'a', u'β':'b', u'γ':'g', u'δ':'d', u'ε':'e', u'ζ':'z',
u'η':'h', u'θ':'8',
u'ι':'i', u'κ':'k', u'λ':'l', u'μ':'m', u'ν':'n', u'ξ':'3',
u'ο':'o', u'π':'p',
u'ρ':'r', u'σ':'s', u'τ':'t', u'υ':'y', u'φ':'f', u'χ':'x',
u'ψ':'ps', u'ω':'w',
u'ά':'a', u'έ':'e', u'ί':'i', u'ό':'o', u'ύ':'y', u'ή':'h',
u'ώ':'w', u'ς':'s',
u'ϊ':'i', u'ΰ':'y', u'ϋ':'y', u'ΐ':'i',
u'Α':'A', u'Β':'B', u'Γ':'G', u'Δ':'D', u'Ε':'E', u'Ζ':'Z',
u'Η':'H', u'Θ':'8',
u'Ι':'I', u'Κ':'K', u'Λ':'L', u'Μ':'M', u'Ν':'N', u'Ξ':'3',
u'Ο':'O', u'Π':'P',
u'Ρ':'R', u'Σ':'S', u'Τ':'T', u'Υ':'Y', u'Φ':'F', u'Χ':'X',
u'Ψ':'PS', u'Ω':'W',
u'Ά':'A', u'Έ':'E', u'Ί':'I', u'Ό':'O', u'Ύ':'Y', u'Ή':'H',
u'Ώ':'W', u'Ϊ':'I', u'Ϋ':'Y',
# TURKISH
u'ş':'s', u'Ş':'S', u'ı':'i', u'İ':'I', u'ç':'c', u'Ç':'C',
u'ü':'u', u'Ü':'U',
u'ö':'o', u'Ö':'O', u'ğ':'g', u'Ğ':'G',
# RUSSIAN
u'а':'a', u'б':'b', u'в':'v', u'г':'g', u'д':'d', u'е':'e',
u'ё':'yo', u'ж':'zh',
u'з':'z', u'и':'i', u'й':'j', u'к':'k', u'л':'l', u'м':'m',
u'н':'n', u'о':'o',
u'п':'p', u'р':'r', u'с':'s', u'т':'t', u'у':'u', u'ф':'f',
u'х':'h', u'ц':'c',
u'ч':'ch', u'ш':'sh', u'щ':'sh', u'ъ':'', u'ы':'y', u'ь':'',
u'э':'e', u'ю':'yu', u'я':'ya',
u'А':'A', u'Б':'B', u'В':'V', u'Г':'G', u'Д':'D', u'Е':'E',
u'Ё':'Yo', u'Ж':'Zh',
u'З':'Z', u'И':'I', u'Й':'J', u'К':'K', u'Л':'L', u'М':'M',
u'Н':'N', u'О':'O',
u'П':'P', u'Р':'R', u'С':'S', u'Т':'T', u'У':'U', u'Ф':'F',
u'Х':'H', u'Ц':'C',
u'Ч':'Ch', u'Ш':'Sh', u'Щ':'Sh', u'Ъ':'', u'Ы':'Y', u'Ь':'',
u'Э':'E', u'Ю':'Yu', u'Я':'Ya',
# UKRAINIAN
u'Є':'Ye', u'І':'I', u'Ї':'Yi', u'Ґ':'G', u'є':'ye', u'і':'i',
u'ї':'yi', u'ґ':'g',
# CZECH
u'č':'c', u'ď':'d', u'ě':'e', u'ň':'n', u'ř':'r', u'š':'s',
u'ť':'t', u'ů':'u',
u'ž':'z', u'Č':'C', u'Ď':'D', u'Ě':'E', u'Ň':'N', u'Ř':'R',
u'Š':'S', u'Ť':'T', u'Ů':'U', u'Ž':'Z',
# POLISH
u'ą':'a', u'ć':'c', u'ę':'e', u'ł':'l', u'ń':'n', u'ó':'o',
u'ś':'s', u'ź':'z',
u'ż':'z', u'Ą':'A', u'Ć':'C', u'Ę':'e', u'Ł':'L', u'Ń':'N',
u'Ó':'o', u'Ś':'S',
u'Ź':'Z', u'Ż':'Z',
# LATVIAN
u'ā':'a', u'č':'c', u'ē':'e', u'ģ':'g', u'ī':'i', u'ķ':'k',
u'ļ':'l', u'ņ':'n',
u'š':'s', u'ū':'u', u'ž':'z', u'Ā':'A', u'Č':'C', u'Ē':'E',
u'Ģ':'G', u'Ī':'i',
u'Ķ':'k', u'Ļ':'L', u'Ņ':'N', u'Š':'S', u'Ū':'u', u'Ž':'Z'
}

def downcode(name):

 downcode(uŽabovitá zmiešaná kaša)
u'Zabovita zmiesana kasa'

for key, value in _MAP.iteritems():
name =ame.replace(key, value)
return name

  

Works for me:

rrr = downcode(uŽabovitá zmiešaná kaša)
print repr(rrr)
print rrr

prints out:

u'Zabovita zmiesana kasa'
Zabovita zmiesana kasa

I did have to add an encoding declaration as line 2 of the file:

#-*- coding: latin-1 -*-

and I had to convince my editor (Komodo) to save the file in utf-8.

DaveA

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


Re: Simple if-else question

2009-09-30 Thread Iain King
On Sep 30, 7:12 am, Steven D'Aprano
ste...@remove.this.cybersource.com.au wrote:
 On Tue, 29 Sep 2009 22:29:10 -0700, John Yeung wrote:
  On Sep 29, 1:15 pm, Carl Banks pavlovevide...@gmail.com wrote:
  Hmm, I wonder if Python should emit a warning if an else is used on a
  for block with no break inside.  I don't think the else can be invoked
  in any other way.  As a bonus it could catch some cases where people
  mistakenly use it thinking it will execute [only] when there are no
  iterations.

  [Edit from Duncan Booth]

  I would definitely be in favor of a warning.  Yes, people should read
  the docs more carefully, and yes, it would cost a certain amount of
  annoyance to implement this.  But I don't think it would get in people's
  way if they do know how to use else,

 Of course it would. It would mean that everybody who knows how to use
 for...else correctly would have to deal with a totally useless warning.

  and I think it would cut down on
  the number of questions from mystified beginners, some of whom are much
  more aggressive than this particular OP about claiming that Python is
  broken (when it's actually behaving as designed).

 By raising a warning, you are implying that the behaviour is broken, or
 at least suspicious. Warnings mean something needs to be warned against
 -- don't do this. Warnings shouldn't be perfectly legitimate behaviours
 on the off-chance that the user is confused. Warning, are you sure you
 want to put the car into reverse? Perhaps you meant neutral?

 What would the warning say?

 Warning, you have used a legitimate Python control structure but you
 might be confused by it.

 Warning, did you mean if...else instead of for...else?

 Then we can add a whole lot of extra warnings to protect newbies who
 don't read docs (and probably won't read the warnings either) from
 themselves:

 Warning, did you mean obj(1) instead of obj[1]?

 Warning, did you mean def f(object) instead of class f(object)?

 Warning, did you mean class f(object) instead of def f(object)?

 Warning, did you mean 2*3 instead of 2**3?

 Warning, did you mean %s instead of %x?

 Warning, we think you're helpless and don't know what you want, perhaps
 you should be programming in PHP?

 I'm sympathetic to the desire to educate the n00bs, and in fact I've even
 suggested similar warnings myself. But I've been convinced that this is
 the wrong approach. Certainly the language shouldn't assume the
 programmer is confused. If this sort of warning belongs anywhere (and
 that's a big if), it belongs in an IDE.

 --
 Steven

Read the suggestion again - it's not a warning on the for-else
structure, it's a warning when the for-else doesn't contain a break;
he's theorising that a for-else without a break will always trigger
the else, in which case it's almost certainly an error, and having the
warning is not a bad idea.
However, I assume you can get past the else by raising an exception,
so the idea becomes a little muddled - do you warn when there is no
break and no explicit raise caught outside the loop?  What about an
implicit exception?  I would guess that code intentionally using an
implicit exception to break out of a for loop is in need of a warning
(and the author in need of the application of a lart), but I'm sure
you could construct a plausible situation where it wouldn't be that
bad...

Anyway, I'm ambivalently on the fence.

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


Re: Simple if-else question

2009-09-30 Thread Duncan Booth
Iain King iaink...@gmail.com wrote:

 However, I assume you can get past the else by raising an exception,
 so the idea becomes a little muddled - do you warn when there is no
 break and no explicit raise caught outside the loop?  What about an
 implicit exception?  I would guess that code intentionally using an
 implicit exception to break out of a for loop is in need of a warning
 (and the author in need of the application of a lart), but I'm sure
 you could construct a plausible situation where it wouldn't be that
 bad...
 
 Anyway, I'm ambivalently on the fence.
 
I think that in all cases of a for loop not containing break the following 
are exactly equivalent:

   for expr:
  suite1
   else:
  suite2

and

   for expr:
  suite1
   suite2

If you exit from the loop by an exception or a return statement suite2 is 
not executed in either case. The only case where the else clause makes a 
difference is when you break from the loop.

So a warning for 'else' following a breakless for loop is not entirely 
unreasonable as you can rewrite the code without the else.

However, if Python were going to start generating warnings though I'd much 
rather see it warn or even error for accessing a guaranteed uninitialised 
local variable.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Partial directory search question

2009-09-30 Thread Tim Chase

import os

for filename in os.listdir(/usr/bbs/confs/september):
 #stat = os.stat(filename)
 if filename.startswith(_):
print filename


yes, as lallous mentioned, this can be done as a 
list-comprehension/generator.  If printing is all you want to do, 
it's a nice and concise way:


 print '\n'.join(fname for fname in os.listdir(loc) if 
fname.startswith('_'))


If you're doing more processing than just printing it, your 
for-loop is a better (clearer) way to go.  If you have lots of 
processing code, it might help to do the inverse:


  for filename in os.listdir(location):
if not filename.startswith('_'): continue
lots()
of_processing()
and_your_complex_logic()
goes()
here()

It removes one level of indentation depth and makes it clear that 
you don't intend to do anything with the non-leading-underscore 
versions (rather than looking for a corresponding else: line 
possibly screens later).


-tkc




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


Re: Storing a C pointer in a Python class instance

2009-09-30 Thread lallous

Thanks everyone.

Finally, I used Falcolas suggestion and took into consideration 
sturlamolden's comments.


Regards,
Elias
lallous lall...@lgwm.org wrote in message news:h9sgcn$iv...@aioe.org...

Hello

From my C extension module I want to store a C pointer in a given 
PyObject.


The only way I figure how to do it is to use Py_BuildValues and store the 
poiner casted to Py_ssize_t, thus:


Py_BuildValues(n, (Py_ssize_t)my_ptr)

Can it be done differently?

Regards,
Elias 


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


Python book

2009-09-30 Thread lallous

Hello

Can anyone suggest a good book Python book for advancing from beginner 
level?


(I started with Learning Python 3rd ed)

Regards,
Elias 


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


Re: Python book

2009-09-30 Thread Parikshat Dubey
Learning Python and Python in a nutshell from O'Reilly

Regards
Parikshat Dubey

On Wed, Sep 30, 2009 at 5:28 PM, lallous lall...@lgwm.org wrote:

 Hello

 Can anyone suggest a good book Python book for advancing from beginner
 level?

 (I started with Learning Python 3rd ed)

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




-- 
-Fear not the path of truth, for the lack of people walking on it.


Practice yourself, for heaven's sake, in little things; and thence
proceed to
greater.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a C pointer in a Python class instance

2009-09-30 Thread lallous

Hello

After using the PyCObject, I cannot pickle the class anymore.
Any simple solution to this problem? (or resorting to __reduce__ is the only 
solution?)


Thanks,
Elias

Falcolas garri...@gmail.com wrote in message 
news:9d3790aa-f7d9-4bb5-a81f-5428b2d60...@v25g2000yqk.googlegroups.com...

On Sep 29, 2:27 am, lallous lall...@lgwm.org wrote:

Hello

From my C extension module I want to store a C pointer in a given 
PyObject.


The only way I figure how to do it is to use Py_BuildValues and store the
poiner casted to Py_ssize_t, thus:

Py_BuildValues(n, (Py_ssize_t)my_ptr)

Can it be done differently?

Regards,
Elias


You can use a PyCObject_FromVoidPtr

http://docs.python.org/c-api/cobject.html

PyArg_ParseTuple(args, O, pyVoidPointer);
castPointer = (type *) PyCObject_AsVoidPtr(pyVoidPointer);
return PyCObject_FromVoidPtr((void *) castPointer, NULL); 


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


Re: Python book

2009-09-30 Thread Krishnakant
On Wed, 2009-09-30 at 17:38 +0530, Parikshat Dubey wrote:
 Learning Python and Python in a nutshell from O'Reilly
  
 Regards
 Parikshat Dubey
 

How to think like a computer scientist in python is a good book to go
from beginner to intermediate level.
another good book is dive into python.
 
Mail me off the list if you want the soft book in compressed format.

Happy hacking.
Krishnakant.

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


Python 2.6.3rc1 available

2009-09-30 Thread Barry Warsaw
The first (and hopefully last) release candidate for Python 2.6.3 is  
now available via


http://www.python.org/download/releases/2.6.3/

Source releases and Windows binaries are currently available, and Mac  
OS X binaries should be forthcoming.


Nearly 100 bugs have been fixed since 2.6.2.  Barring any unforeseen  
problems, we will make the final 2.6.3 release this Friday, October  
2nd.  Please give this release candidate a spin and let us know if you  
encounter any show stopping problems.


Enjoy,
-Barry



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


SQLObject 0.10.8

2009-09-30 Thread Oleg Broytman
Hello!

I'm pleased to announce version 0.10.8, a minor bugfix release of 0.10 branch
of SQLObject.


What is SQLObject
=

SQLObject is an object-relational mapper.  Your database tables are described
as classes, and rows are instances of those classes.  SQLObject is meant to be
easy to use and quick to get started with.

SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite,
Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB).


Where is SQLObject
==

Site:
http://sqlobject.org

Development:
http://sqlobject.org/devel/

Mailing list:
https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss

Archives:
http://news.gmane.org/gmane.comp.python.sqlobject

Download:
http://cheeseshop.python.org/pypi/SQLObject/0.10.8

News and changes:
http://sqlobject.org/News.html


What's New
==

News since 0.10.7
-

* Fixed a bug in logging to console - convert unicode to str.

* Fixed an obscure bug in ConnectionHub triggered by an SQLObject class
  whose instances can be coerced to boolean False.

For a more complete list, please see the news:
http://sqlobject.org/News.html

Oleg.
-- 
 Oleg Broytmanhttp://phd.pp.ru/p...@phd.pp.ru
   Programmers don't die, they just GOSUB without RETURN.
-- 
http://mail.python.org/mailman/listinfo/python-list


SQLObject 0.11.2

2009-09-30 Thread Oleg Broytman
Hello!

I'm pleased to announce version 0.11.2, a minor bugfix release of 0.11 branch
of SQLObject.


What is SQLObject
=

SQLObject is an object-relational mapper.  Your database tables are described
as classes, and rows are instances of those classes.  SQLObject is meant to be
easy to use and quick to get started with.

SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite,
Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB).


Where is SQLObject
==

Site:
http://sqlobject.org

Development:
http://sqlobject.org/devel/

Mailing list:
https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss

Archives:
http://news.gmane.org/gmane.comp.python.sqlobject

Download:
http://cheeseshop.python.org/pypi/SQLObject/0.11.2

News and changes:
http://sqlobject.org/News.html


What's New
==

News since 0.11.1
-

* Fixed a bug in logging to console - convert unicode to str.

* Fixed an obscure bug in ConnectionHub triggered by an SQLObject class
  whose instances can be coerced to boolean False.

For a more complete list, please see the news:
http://sqlobject.org/News.html

Oleg.
-- 
 Oleg Broytmanhttp://phd.pp.ru/p...@phd.pp.ru
   Programmers don't die, they just GOSUB without RETURN.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: is this whiff/wsgi claim true?

2009-09-30 Thread Aaron Watters
regarding
  http://aaron.oirt.rutgers.edu/myapp/docs/W1000.concepts

On Sep 27, 11:12 pm, Дамјан Георгиевски gdam...@gmail.com wrote:
 mod_wsgi (the apache module) can be configured to automatically run any
 .wsgi file dropped in a folder just like CGI ... see here
 http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIScr...

Thanks.  I think this observation makes a liar of me :(.
I'll have to reword the claim.  I could split hairs to
assert that this is not the same thing, but it's close
enough

   -- Aaron Watters

===
Speak roughly to your little boy
  and beat him when he sneezes.
He only does it to annoy
  because he knows it teases.
I speak severely to my boy
  and beat him when he sneezes
For he can thoroughly enjoy
  the pepper when he pleases!
 -- Lewis Carroll
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode issue

2009-09-30 Thread gentlestone
On 30. Sep., 11:45 h., Dave Angel da...@dejaviewphoto.com wrote:
 gentlestone wrote:
  Why don't work this code on Python 2.6? Or how can I do this job?

  _MAP =
      # LATIN
      u'À': 'A', u'Á': 'A', u'Â': 'A', u'Ã': 'A', u'Ä': 'A', u'Å': 'A',
  u'Æ': 'AE', u'Ç':'C',
      u'È': 'E', u'É': 'E', u'Ê': 'E', u'Ë': 'E', u'Ì': 'I', u'Í': 'I',
  u'Î': 'I',
      u'Ï': 'I', u'Ð': 'D', u'Ñ': 'N', u'Ò': 'O', u'Ó': 'O', u'Ô': 'O',
  u'Õ': 'O', u'Ö':'O',
      u'Ő': 'O', u'Ø': 'O', u'Ù': 'U', u'Ú': 'U', u'Û': 'U', u'Ü': 'U',
  u'Ű': 'U',
      u'Ý': 'Y', u'Þ': 'TH', u'ß': 'ss', u'à':'a', u'á':'a', u'â': 'a',
  u'ã': 'a', u'ä':'a',
      u'å': 'a', u'æ': 'ae', u'ç': 'c', u'è': 'e', u'é': 'e', u'ê': 'e',
  u'ë': 'e',
      u'ì': 'i', u'í': 'i', u'î': 'i', u'ï': 'i', u'ð': 'd', u'ñ': 'n',
  u'ò': 'o', u'ó':'o',
      u'ô': 'o', u'õ': 'o', u'ö': 'o', u'ő': 'o', u'ø': 'o', u'ù': 'u',
  u'ú': 'u',
      u'û': 'u', u'ü': 'u', u'ű': 'u', u'ý': 'y', u'þ': 'th', u'ÿ': 'y',
      # LATIN_SYMBOLS
      u'©':'(c)',
      # GREEK
      u'α':'a', u'β':'b', u'γ':'g', u'δ':'d', u'ε':'e', u'ζ':'z',
  u'η':'h', u'θ':'8',
      u'ι':'i', u'κ':'k', u'λ':'l', u'μ':'m', u'ν':'n', u'ξ':'3',
  u'ο':'o', u'π':'p',
      u'ρ':'r', u'σ':'s', u'τ':'t', u'υ':'y', u'φ':'f', u'χ':'x',
  u'ψ':'ps', u'ω':'w',
      u'ά':'a', u'έ':'e', u'ί':'i', u'ό':'o', u'ύ':'y', u'ή':'h',
  u'ώ':'w', u'ς':'s',
      u'ϊ':'i', u'ΰ':'y', u'ϋ':'y', u'ΐ':'i',
      u'Α':'A', u'Β':'B', u'Γ':'G', u'Δ':'D', u'Ε':'E', u'Ζ':'Z',
  u'Η':'H', u'Θ':'8',
      u'Ι':'I', u'Κ':'K', u'Λ':'L', u'Μ':'M', u'Ν':'N', u'Ξ':'3',
  u'Ο':'O', u'Π':'P',
      u'Ρ':'R', u'Σ':'S', u'Τ':'T', u'Υ':'Y', u'Φ':'F', u'Χ':'X',
  u'Ψ':'PS', u'Ω':'W',
      u'Ά':'A', u'Έ':'E', u'Ί':'I', u'Ό':'O', u'Ύ':'Y', u'Ή':'H',
  u'Ώ':'W', u'Ϊ':'I', u'Ϋ':'Y',
      # TURKISH
      u'ş':'s', u'Ş':'S', u'ı':'i', u'İ':'I', u'ç':'c', u'Ç':'C',
  u'ü':'u', u'Ü':'U',
      u'ö':'o', u'Ö':'O', u'ğ':'g', u'Ğ':'G',
      # RUSSIAN
      u'а':'a', u'б':'b', u'в':'v', u'г':'g', u'д':'d', u'е':'e',
  u'ё':'yo', u'ж':'zh',
      u'з':'z', u'и':'i', u'й':'j', u'к':'k', u'л':'l', u'м':'m',
  u'н':'n', u'о':'o',
      u'п':'p', u'р':'r', u'с':'s', u'т':'t', u'у':'u', u'ф':'f',
  u'х':'h', u'ц':'c',
      u'ч':'ch', u'ш':'sh', u'щ':'sh', u'ъ':'', u'ы':'y', u'ь':'',
  u'э':'e', u'ю':'yu', u'я':'ya',
      u'А':'A', u'Б':'B', u'В':'V', u'Г':'G', u'Д':'D', u'Е':'E',
  u'Ё':'Yo', u'Ж':'Zh',
      u'З':'Z', u'И':'I', u'Й':'J', u'К':'K', u'Л':'L', u'М':'M',
  u'Н':'N', u'О':'O',
      u'П':'P', u'Р':'R', u'С':'S', u'Т':'T', u'У':'U', u'Ф':'F',
  u'Х':'H', u'Ц':'C',
      u'Ч':'Ch', u'Ш':'Sh', u'Щ':'Sh', u'Ъ':'', u'Ы':'Y', u'Ь':'',
  u'Э':'E', u'Ю':'Yu', u'Я':'Ya',
      # UKRAINIAN
      u'Є':'Ye', u'І':'I', u'Ї':'Yi', u'Ґ':'G', u'є':'ye', u'і':'i',
  u'ї':'yi', u'ґ':'g',
      # CZECH
      u'č':'c', u'ď':'d', u'ě':'e', u'ň':'n', u'ř':'r', u'š':'s',
  u'ť':'t', u'ů':'u',
      u'ž':'z', u'Č':'C', u'Ď':'D', u'Ě':'E', u'Ň':'N', u'Ř':'R',
  u'Š':'S', u'Ť':'T', u'Ů':'U', u'Ž':'Z',
      # POLISH
      u'ą':'a', u'ć':'c', u'ę':'e', u'ł':'l', u'ń':'n', u'ó':'o',
  u'ś':'s', u'ź':'z',
      u'ż':'z', u'Ą':'A', u'Ć':'C', u'Ę':'e', u'Ł':'L', u'Ń':'N',
  u'Ó':'o', u'Ś':'S',
      u'Ź':'Z', u'Ż':'Z',
      # LATVIAN
      u'ā':'a', u'č':'c', u'ē':'e', u'ģ':'g', u'ī':'i', u'ķ':'k',
  u'ļ':'l', u'ņ':'n',
      u'š':'s', u'ū':'u', u'ž':'z', u'Ā':'A', u'Č':'C', u'Ē':'E',
  u'Ģ':'G', u'Ī':'i',
      u'Ķ':'k', u'Ļ':'L', u'Ņ':'N', u'Š':'S', u'Ū':'u', u'Ž':'Z'
  }

  def downcode(name):
      
       downcode(uŽabovitá zmiešaná kaša)
      u'Zabovita zmiesana kasa'
      
      for key, value in _MAP.iteritems():
          name =ame.replace(key, value)
      return name

 Works for me:

 rrr = downcode(uŽabovitá zmiešaná kaša)
 print repr(rrr)
 print rrr

 prints out:

 u'Zabovita zmiesana kasa'
 Zabovita zmiesana kasa

 I did have to add an encoding declaration as line 2 of the file:

 #-*- coding: latin-1 -*-

 and I had to convince my editor (Komodo) to save the file in utf-8.

 DaveA

great, thanks you all, I changed utf-8 to latin-1 in the header and it
works for me too

how mutch time could I save, just ask in this forum
-- 
http://mail.python.org/mailman/listinfo/python-list


Python logging and 1.5.2 compatibility

2009-09-30 Thread Vinay Sajip
I'm planning to officially drop support for Python 1.5.2 in the logging
package.

When the logging package was introduced in Python 2.3, many Linux distros were
shipping 1.5.2 as the system's Python, even though 2.2 had been out for a
while. So it seemed important to support 1.5.2 for those sysadmins who wanted
to use logging with their system Python.

The Linux landscape has changed a bit since then. Most Linux distros ship with
much more recent versions of Python, and so I no longer see 1.5.2 support as
important.

Dropping support for 1.5.2 means that future changes to logging will not be
concerned with 1.5.2 compatibility. For example, boolean values which were 0/1
in the logging package will at some point be replaced by False/True, and newer
language features will start to be used when changes are made. There are no
plans for a specific cleanup exercise at the moment. In fact some changes
made a while ago inadvertently broke 1.5.2 compatibility, but no-one's
complained. So I'm assuming the whole thing is really a non-issue, and this
post is just to keep everyone in the picture.

A 1.5.2-compatible version of the package is still available via
http://www.red-dove.com/python_logging.html if anyone needs it. This version
is not actively maintained, but that shouldn't be an issue.

Regards,

Vinay Sajip

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


Re: print object attributes recursively

2009-09-30 Thread Piet van Oostrum
 lallous lall...@lgwm.org (l) wrote:

l Hello
l Suppose I have this code:

l class X:
ldef __init__(self, n):
lself.L = [x for x in xrange(0, n+1)]

l class Y:
ldef __init__(self, n):
lself.M = [X(x) for x in xrange(0, n)]

l t = Y(5)


l How can I easily print t and all its nested attributes? (Something like
l PHP's print_r())

I don't know what print_r does, but in your example above

print [x.L for x in t.M] would work.

Probably you would split this into two methods in X and Y.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: logging.handlers.SMTPHandler question

2009-09-30 Thread Chris Withers

akonsu wrote:

hello,

SMTPHAndler seems to email every single record separately. is there a
way to collect all log output and then send it in a single email
message? or do i have to do it manually?


You want the SummarisingHandler from this package:

http://www.simplistix.co.uk/software/python/mailinglogger

cheers,

Chris

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


Re: Repeated output when logging exceptions

2009-09-30 Thread Chris Withers

Vinay Sajip wrote:

I'm not sure why you need all the code you've posted. The logging
package allows you to add tracebacks to your logs by using the
exception() method, which logs an ERROR with a traceback and is
specifically intended for use from within exception handlers. 


You can also use the exc_info=True parameter with any logging method...

Chris

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


Re: Repeated output when logging exceptions

2009-09-30 Thread Chris Withers

John Gordon wrote:

If I didn't do all that in a class, where would I do it?


I find the configureLoggers method of ZConfig most convenient for this:

http://pypi.python.org/pypi/ZConfig

cheers,

Chris

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


Re: problems trying to build python 2.6 as a shared library

2009-09-30 Thread Chris Withers

Marco Nawijn wrote:

2. Add path to dynamic linker configuration file. This typically
is in '/etc/ld.so.conf'. See man page for ld for more information.


Yes, this was it.
Don't forget to run ldconfig after you've changed /etc/ld.so.conf

It's frustrating how the contents of this file vary from distro to 
distro, particularly w.r.t. /usr/local/lib :-(


Chris

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


Re: Can I make these getters/setters?

2009-09-30 Thread Michael rotini Lerner
Excellent. I now understand why it was broken, and a slightly tweaked
version of FieldProperty does what I want. Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cannot get POST to work

2009-09-30 Thread Piet van Oostrum
 tedpot...@gmail.com tedpot...@gmail.com (t) wrote:

t Hi,
t I'm trying to post data to a short test script in php I wrote.
t The python code to do the post is
t import httplib

t #server address
t conn = httplib.HTTPConnection(localhost)

headers = {Content-type: application/x-www-form-urlencoded,
Accept: text/plain}

t #file location
t conn.request(POST, /programming/bots/test.php,ted=fred)

conn.request(POST, /programming/bots/test.php, ted=fred, headers)

t r1 = conn.getresponse()
t print r1.status, r1.reason
t data1 = r1.read()
t print data1
t conn.close()
t print new ok

t The PHP script is
t printhello br;
t print $_POST[ted];

t Ted post

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


Re: unicode issue

2009-09-30 Thread saeed.gnu
I recommend to use UTF-8 coding(specially in GNU/Linux) then write
this in the second line:
#-*- coding: latin-1 -*-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode issue

2009-09-30 Thread Mark Tolonen


Dave Angel da...@dejaviewphoto.com wrote in message 
news:4ac328d4.3060...@dejaviewphoto.com...

gentlestone wrote:

Why don't work this code on Python 2.6? Or how can I do this job?

_MAP =
# LATIN
u'À': 'A', u'Á': 'A', u'Â': 'A', u'Ã': 'A', u'Ä': 'A', u'Å': 'A',
u'Æ': 'AE', u'Ç':'C',
u'È': 'E', u'É': 'E', u'Ê': 'E', u'Ë': 'E', u'Ì': 'I', u'Í': 'I',
u'Î': 'I',
u'Ï': 'I', u'Ð': 'D', u'Ñ': 'N', u'Ò': 'O', u'Ó': 'O', u'Ô': 'O',
u'Õ': 'O', u'Ö':'O',
u'Ő': 'O', u'Ø': 'O', u'Ù': 'U', u'Ú': 'U', u'Û': 'U', u'Ü': 'U',
u'Ű': 'U',
u'Ý': 'Y', u'Þ': 'TH', u'ß': 'ss', u'à':'a', u'á':'a', u'â': 'a',
u'ã': 'a', u'ä':'a',
u'å': 'a', u'æ': 'ae', u'ç': 'c', u'è': 'e', u'é': 'e', u'ê': 'e',
u'ë': 'e',
u'ì': 'i', u'í': 'i', u'î': 'i', u'ï': 'i', u'ð': 'd', u'ñ': 'n',
u'ò': 'o', u'ó':'o',
u'ô': 'o', u'õ': 'o', u'ö': 'o', u'ő': 'o', u'ø': 'o', u'ù': 'u',
u'ú': 'u',
u'û': 'u', u'ü': 'u', u'ű': 'u', u'ý': 'y', u'þ': 'th', u'ÿ': 'y',
# LATIN_SYMBOLS
u'©':'(c)',
# GREEK
u'α':'a', u'β':'b', u'γ':'g', u'δ':'d', u'ε':'e', u'ζ':'z',
u'η':'h', u'θ':'8',
u'ι':'i', u'κ':'k', u'λ':'l', u'μ':'m', u'ν':'n', u'ξ':'3',
u'ο':'o', u'π':'p',
u'ρ':'r', u'σ':'s', u'τ':'t', u'υ':'y', u'φ':'f', u'χ':'x',
u'ψ':'ps', u'ω':'w',
u'ά':'a', u'έ':'e', u'ί':'i', u'ό':'o', u'ύ':'y', u'ή':'h',
u'ώ':'w', u'ς':'s',
u'ϊ':'i', u'ΰ':'y', u'ϋ':'y', u'ΐ':'i',
u'Α':'A', u'Β':'B', u'Γ':'G', u'Δ':'D', u'Ε':'E', u'Ζ':'Z',
u'Η':'H', u'Θ':'8',
u'Ι':'I', u'Κ':'K', u'Λ':'L', u'Μ':'M', u'Ν':'N', u'Ξ':'3',
u'Ο':'O', u'Π':'P',
u'Ρ':'R', u'Σ':'S', u'Τ':'T', u'Υ':'Y', u'Φ':'F', u'Χ':'X',
u'Ψ':'PS', u'Ω':'W',
u'Ά':'A', u'Έ':'E', u'Ί':'I', u'Ό':'O', u'Ύ':'Y', u'Ή':'H',
u'Ώ':'W', u'Ϊ':'I', u'Ϋ':'Y',
# TURKISH
u'ş':'s', u'Ş':'S', u'ı':'i', u'İ':'I', u'ç':'c', u'Ç':'C',
u'ü':'u', u'Ü':'U',
u'ö':'o', u'Ö':'O', u'ğ':'g', u'Ğ':'G',
# RUSSIAN
u'а':'a', u'б':'b', u'в':'v', u'г':'g', u'д':'d', u'е':'e',
u'ё':'yo', u'ж':'zh',
u'з':'z', u'и':'i', u'й':'j', u'к':'k', u'л':'l', u'м':'m',
u'н':'n', u'о':'o',
u'п':'p', u'р':'r', u'с':'s', u'т':'t', u'у':'u', u'ф':'f',
u'х':'h', u'ц':'c',
u'ч':'ch', u'ш':'sh', u'щ':'sh', u'ъ':'', u'ы':'y', u'ь':'',
u'э':'e', u'ю':'yu', u'я':'ya',
u'А':'A', u'Б':'B', u'В':'V', u'Г':'G', u'Д':'D', u'Е':'E',
u'Ё':'Yo', u'Ж':'Zh',
u'З':'Z', u'И':'I', u'Й':'J', u'К':'K', u'Л':'L', u'М':'M',
u'Н':'N', u'О':'O',
u'П':'P', u'Р':'R', u'С':'S', u'Т':'T', u'У':'U', u'Ф':'F',
u'Х':'H', u'Ц':'C',
u'Ч':'Ch', u'Ш':'Sh', u'Щ':'Sh', u'Ъ':'', u'Ы':'Y', u'Ь':'',
u'Э':'E', u'Ю':'Yu', u'Я':'Ya',
# UKRAINIAN
u'Є':'Ye', u'І':'I', u'Ї':'Yi', u'Ґ':'G', u'є':'ye', u'і':'i',
u'ї':'yi', u'ґ':'g',
# CZECH
u'č':'c', u'ď':'d', u'ě':'e', u'ň':'n', u'ř':'r', u'š':'s',
u'ť':'t', u'ů':'u',
u'ž':'z', u'Č':'C', u'Ď':'D', u'Ě':'E', u'Ň':'N', u'Ř':'R',
u'Š':'S', u'Ť':'T', u'Ů':'U', u'Ž':'Z',
# POLISH
u'ą':'a', u'ć':'c', u'ę':'e', u'ł':'l', u'ń':'n', u'ó':'o',
u'ś':'s', u'ź':'z',
u'ż':'z', u'Ą':'A', u'Ć':'C', u'Ę':'e', u'Ł':'L', u'Ń':'N',
u'Ó':'o', u'Ś':'S',
u'Ź':'Z', u'Ż':'Z',
# LATVIAN
u'ā':'a', u'č':'c', u'ē':'e', u'ģ':'g', u'ī':'i', u'ķ':'k',
u'ļ':'l', u'ņ':'n',
u'š':'s', u'ū':'u', u'ž':'z', u'Ā':'A', u'Č':'C', u'Ē':'E',
u'Ģ':'G', u'Ī':'i',
u'Ķ':'k', u'Ļ':'L', u'Ņ':'N', u'Š':'S', u'Ū':'u', u'Ž':'Z'
}

def downcode(name):

 downcode(uŽabovitá zmiešaná kaša)
u'Zabovita zmiesana kasa'

for key, value in _MAP.iteritems():
name =ame.replace(key, value)
return name



Works for me:

rrr = downcode(uŽabovitá zmiešaná kaša)
print repr(rrr)
print rrr

prints out:

u'Zabovita zmiesana kasa'
Zabovita zmiesana kasa

I did have to add an encoding declaration as line 2 of the file:

#-*- coding: latin-1 -*-

and I had to convince my editor (Komodo) to save the file in utf-8.


Why decare latin-1 and save in utf-8?  I'm not sure how you got that to work 
because those encodings aren't equivalent.  I get:


Traceback (most recent call last):
 File stdin, line 1, in module
 File testit.py, line 1
SyntaxError: encoding problem: utf-8

In fact, some of the characters in the above code don't map to latin-1.

Traceback (most recent call last):
 File stdin, line 1, in module
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u0150' in 
position

309: ordinal not in range(256)

import unicodedata as ud
ud.name(u'\u0150')


-Mark



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


python memory use

2009-09-30 Thread Gary Robinson
The chart at 
http://shootout.alioth.debian.org/u32q/benchmark.php?test=alllang=javasteadylang2=pythonbox=1
 is very interesting to me because it shows CPython using much less memory than 
Java for most tests.

I'd be interested in knowing whether anybody can share info about how 
representative those test results are. For instance, suppose we're talking 
about a huge dictionary that maps integers to lists of integers (something I 
use in my code). Would something like that really take up much more memory in 
Java (using the closest equivalent Java data structures) than in CPython? I 
find it hard to believe that that would be the case, but I'm quite curious.

(I could test the particular case I mention, but I'm wondering if someone has 
some fundamental knowledge that would lead to a basic understanding.)


-- 

Gary Robinson
CTO
Emergent Music, LLC
personal email: gary...@me.com
work email: grobin...@flyfi.com
Company: http://www.flyfi.com
Blog:http://www.garyrobinson.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode issue

2009-09-30 Thread Piet van Oostrum
 Dave Angel da...@dejaviewphoto.com (DA) wrote:

DA Works for me:

DA rrr = downcode(uŽabovitá zmiešaná kaša)
DA print repr(rrr)
DA print rrr

DA prints out:

DA u'Zabovita zmiesana kasa'
DA Zabovita zmiesana kasa

DA I did have to add an encoding declaration as line 2 of the file:

DA #-*- coding: latin-1 -*-

DA and I had to convince my editor (Komodo) to save the file in utf-8.

*Seems to work*.
If you save in utf-8 the coding declaration also has to be utf-8.
Besides, many of these characters won't be representable in latin-1.
The reason it worked is that these characters were translated into two-
or more-bytes sequences and replace did work with these. But it's
dangerous, as they are then no longer the unicode characters they were
intended to be. 
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple if-else question

2009-09-30 Thread Carl Banks
On Sep 30, 3:40 am, Iain King iaink...@gmail.com wrote:
 Read the suggestion again - it's not a warning on the for-else
 structure, it's a warning when the for-else doesn't contain a break;
 he's theorising that a for-else without a break will always trigger
 the else, in which case it's almost certainly an error, and having the
 warning is not a bad idea.

I actually had minor moment of confusion, somehow I was thinking that
if no break appeared in the for-block then the else-block would be
impossible to execute (the opposite of what actually happens).  Even
then just kind of throwing the idea out there, wasn't really
advocating it.

Now that I am no longer confused I definitely don't think there should
be a warning.


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


Re: Storing a C pointer in a Python class instance

2009-09-30 Thread Carl Banks
On Sep 30, 5:24 am, lallous lall...@lgwm.org wrote:
 Hello

 After using the PyCObject, I cannot pickle the class anymore.
 Any simple solution to this problem? (or resorting to __reduce__ is the only
 solution?)


You can't pickle a CObject, you'd have to create a custom type (one
that implements one of the pickling methods) for that.  Or arrange for
whatever object contains the CObject to pack and unpack it manually.

Out of curiosity, what kind of data you storing in this CObject?
Maybe we can help you choose a better way to handle it at the C level.


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


Re: Partial directory search question

2009-09-30 Thread Rami Chowdhury



Rami Chowdhury
Never attributed to malice that which can be attributed to stupidity. -- 
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

On Tuesday 29 September 2009 19:54:17 chad wrote:
 On Sep 29, 7:52 pm, chad cdal...@gmail.com wrote:
  On Sep 29, 7:20 pm, Tim Chase python.l...@tim.thechases.com wrote:
What's the sanest way to print out all the files in the directory
that start with the underscore? Ie, I just want to list _1, _2, _3,
_4.
  
   I'd use a string's join() method to combine the results of a
   list-comprehension or generator that filtered the output of
   os.listdir() based on the startswith() method of the strings.
  
   Left intentionally oblique and code-free because this sounds a
   bit like a home-work problem.  If you're a python coder, that
   should make pretty decent sense and be a one-liner to implement.
  
   -tkc
 
  Okay, sorry for the delay to the response. I got side tracked trying
  to stalk, I mean talk to the 59 year old neighbor girl. Anyways, I
  couldn't get it to one in one line. Here is what I did...
 
  % more rec.py
  #!/usr/local/bin/python
 
  import os
  import time
 
  for filename in os.listdir(/usr/bbs/confs/september):
   #stat = os.stat(filename)
   if filename.startswith(_):
  print filename
 
  ./rec.py
  _1
  _2
  _3
  _4
  _5
  _6
  _7
  _8
 
  It correctly prints out all the files in the directory that start with
  an underscore.
 
 er *couldn't get it into a one liner*.
 

To get it into one line, I suggest:

http://docs.python.org/tutorial/datastructures.html#list-comprehensions
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python book

2009-09-30 Thread Patrick Sabin

My favorite book is Python Essential Reference from David M. Beazley.
It is not a beginner book. It is about the python language and not about 
 a framework or third-party library. It is much more complete than for 
instance Dive into python, but maybe somewhat more difficult.


- Patrick

lallous wrote:

Hello

Can anyone suggest a good book Python book for advancing from beginner 
level?


(I started with Learning Python 3rd ed)

Regards,
Elias


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


Re: Most active coroutine library project?

2009-09-30 Thread Hendrik van Rooyen
On Wednesday, 30 September 2009 09:46:38 Paul Rubin wrote:

 Getting away from python in the opposite direction, if you click

http://cufp.galois.com/2008/schedule.html

 the second presentation Controlling Hybrid Vehicles with Haskell
 might interest you.  Basically it's about a high level DSL that
 generates realtime control code written in C.  From the slides:

 * 5K lines of Haskell/atom replaced 120K lines of matlab, simulink,
   and visual basic.
 * 2 months to port simulink design to atom.
 * Rules with execution periods from 1ms to 10s all scheduled at
   compile time to a 1 ms main loop.
 * Atom design clears electronic/sw testing on first pass.
 * Currently in vehicle testing with no major issues.

 Code is here: http://hackage.haskell.org/package/atom

 Blurb: Atom is a Haskell DSL for designing hard realtime embedded
 programs. Based on conditional term rewriting, atom will compile a
 collection of atomic state transition rules to a C program with
 constant memory use and deterministic execution time.

Awesome!  Thank you 

- Hendrik


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


SVG PIL decoder

2009-09-30 Thread Patrick Sabin
I would like to open svg files with PIL, but svg doesn't seem to be 
supported. Does anyone know about a svg decoder for the PIL?


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


Re: [Image-SIG] Some issue with easy_install and PIL/Imaging

2009-09-30 Thread Chris Withers

Fredrik Lundh wrote:

On Fri, Sep 11, 2009 at 3:49 PM, Chris Withers ch...@simplistix.co.uk wrote:

Klein Stéphane wrote:

Resume :
1. first question : why PIL package in pypi don't work ?

Because Fred Lundh have his package distributions unfortunate names that
setuptools doesn't like...


It used to support this, but no longer does.  To me, that says more
about the state of setuptools than it does about the state of PIL,
which has been using the same naming convention for 15 years.


Yep, but it is now in the minority, and consistency in package naming is 
always good.


Would there be any problems for you in naming the distribution in a 
setuptools-friendly way from the next point release?


cheers,

Chris

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


Re: SVG PIL decoder

2009-09-30 Thread Donn
On Wednesday 30 September 2009 18:01:50 Patrick Sabin wrote:
 I would like to open svg files with PIL, but svg doesn't seem to be
 supported. Does anyone know about a svg decoder for the PIL?
Have a look at Cairo (python-cairo) in conjunction with librsvg (python-rsvg) 
-- that'll fix you up. You can go from an SVG to a PNG/array and thence into 
PIL if you need to.

\d
-- 
home: http://otherwise.relics.co.za/
2D vector animation : https://savannah.nongnu.org/projects/things/
Font manager : https://savannah.nongnu.org/projects/fontypython/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode issue

2009-09-30 Thread Dave Angel

Piet van Oostrum wrote:

Dave Angel da...@dejaviewphoto.com (DA) wrote:



  

DA Works for me:



  

DA rrr = downcode(uŽabovitá zmiešaná kaša)
DA print repr(rrr)
DA print rrr



  

DA prints out:



  

DA u'Zabovita zmiesana kasa'
DA Zabovita zmiesana kasa



  

DA I did have to add an encoding declaration as line 2 of the file:



  

DA #-*- coding: latin-1 -*-



  

DA and I had to convince my editor (Komodo) to save the file in utf-8.



*Seems to work*.
If you save in utf-8 the coding declaration also has to be utf-8.
Besides, many of these characters won't be representable in latin-1.
The reason it worked is that these characters were translated into two-
or more-bytes sequences and replace did work with these. But it's
dangerous, as they are then no longer the unicode characters they were
intended to be. 
  
Thanks for the correction. What I meant by works for me is that the 
single example in the docstring translated okay. But I do have a lot to 
learn about using Unicode in sources, and I want to learn.


So tell me, how were we supposed to guess what encoding the original 
message used? I originally had the mailing list message (in Thunderbird 
email). When I copied (copy/paste) to Komodo IDE (text editor), it 
wouldn't let me save because the file type was ASCII. So I randomly 
chosen latin-1 for file type, and it seemed to like it.


At that point I expected and got errors from Python because I had no 
coding declaration. I used latin-1, and still had problems, though I 
forget what they were. Only when I changed the file encoding type again, 
to utf-8, did the errors go away. I agree that they should agree, but I 
don't know how to reconcile the copy/paste boundary, the file type 
(without BOM, which is another variable), the coding declaration, and 
the stdout implicit ASCII encoding. I understand a bunch of it, but not 
enough to be able to safely walk through the choices.


Is this all written up in one place, to where an experienced programmer 
can make sense of it? I've nibbled at the edges (even wrote a UTF-8 
encoder/decoder a dozen years ago).


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


Re: Storing a C pointer in a Python class instance

2009-09-30 Thread Carl Banks
On Sep 29, 11:16 am, sturlamolden sturlamol...@yahoo.no wrote:
 On 29 Sep, 19:11, Carl Banks pavlovevide...@gmail.com wrote:

  CObjects can be passed a C function as a deallocator; this should work
  as reliably as a custom class deallocator.

 Except that __del__ prevents cyclic GC.

You are mistaken on two counts.

First of all, a CObject is not a container.  It can't prevent cyclic
GC because it's never a part of a cycle.

Second, CObjects do not have a __del__ method.  They call the supplied
constructor from the type's tp_dealloc slot.  Use of the tp_dealloc
slot does not, by itself, prevent cyclic GC.

Bottom line is, the CObject's deallocator is as reliable as a custom
type's tp_dealloc.


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


Re: Python logging and 1.5.2 compatibility

2009-09-30 Thread TerryP
On Sep 30, 1:26 pm, Vinay Sajip vinay_sa...@yahoo.co.uk wrote:

 A 1.5.2-compatible version of the package is still available 
 viahttp://www.red-dove.com/python_logging.htmlif anyone needs it. This version
 is not actively maintained, but that shouldn't be an issue.

 Regards,

 Vinay Sajip

As long as people can dig up an old version, everything should be
fine.

It's not like you're asking people to port over a quarter million
lines of working code to the latest and untested :-)

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


Re: python memory use

2009-09-30 Thread Mensanator
On Sep 30, 9:38 am, Gary Robinson gary...@me.com wrote:
 The chart 
 athttp://shootout.alioth.debian.org/u32q/benchmark.php?test=alllang=ja...is 
 very interesting to me because it shows CPython using much less memory than 
 Java for most tests.

Which version of Python? If you're talking 3.x for Windows, any memory
usage statistics are meaningless.



 I'd be interested in knowing whether anybody can share info about how 
 representative those test results are. For instance, suppose we're talking 
 about a huge dictionary that maps integers to lists of integers (something I 
 use in my code). Would something like that really take up much more memory in 
 Java (using the closest equivalent Java data structures) than in CPython? I 
 find it hard to believe that that would be the case, but I'm quite curious.

 (I could test the particular case I mention, but I'm wondering if someone has 
 some fundamental knowledge that would lead to a basic understanding.)

 --

 Gary Robinson
 CTO
 Emergent Music, LLC
 personal email: gary...@me.com
 work email: grobin...@flyfi.com
 Company:http://www.flyfi.com
 Blog:    http://www.garyrobinson.net

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


Re: print object attributes recursively

2009-09-30 Thread TerryP
On Sep 30, 1:49 pm, Piet van Oostrum p...@cs.uu.nl wrote:
 I don't know what print_r does, but in your example above

 print [x.L for x in t.M] would work.

 Probably you would split this into two methods in X and Y.
 --
 Piet van Oostrum p...@vanoostrum.org
 WWW:http://pietvanoostrum.com/
 PGP key: [8DAE142BE17999C4]

PHP's print_r() is basically a recursive pretty printer; if you know
Perl, think of it as PHPs idea of Data::Dumper.


Personally, when I want to know whats in something, I invoke dir() on
it and filter the results accordingly. Whether it is smart or stupid,
it works well at an interactive prompt.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python memory use

2009-09-30 Thread Isaac Gouy
On Sep 30, 7:38 am, Gary Robinson gary...@me.com wrote:
 The chart 
 athttp://shootout.alioth.debian.org/u32q/benchmark.php?test=all〈=ja...is very 
 interesting to me because it shows CPython using much less memory than Java 
 for most tests.

 I'd be interested in knowing whether anybody can share info about how 
 representative those test results are. For instance, suppose we're talking 
 about a huge dictionary that maps integers to lists of integers (something I 
 use in my code). Would something like that really take up much more memory in 
 Java (using the closest equivalent Java data structures) than in CPython? I 
 find it hard to believe that that would be the case, but I'm quite curious.

 (I could test the particular case I mention, but I'm wondering if someone has 
 some fundamental knowledge that would lead to a basic understanding.)


1) That URL is approximate averages rather than the straight Java
measurements.

2) Unless the programs are using *a lot* of memory you're just seeing
default JVM memory use.

3) More of the Java programs may have been re-written to use quad
core, which may use extra buffering.


So look for tasks that use a lot of memory and watch for time/space
tradeoffs -

2.1  Java 6 -server #229.32259,868
62   Python #614 min   674,316
167  Python #238 min   221,236

http://shootout.alioth.debian.org/u32/benchmark.php?test=binarytreeslang=all


2.8  Java 6 -server #246.87363,488
34   Python   9 min439,196

http://shootout.alioth.debian.org/u32/benchmark.php?test=knucleotidelang=all


2.5  Java 6 -server #42.87 473,324
6.5  Python #37.67 543,908

http://shootout.alioth.debian.org/u32/benchmark.php?test=revcomplang=all
-- 
http://mail.python.org/mailman/listinfo/python-list


M2Crypto 0.20.1 won't build on Red Hat Linux

2009-09-30 Thread John Nagle

M2Crypto, from

http://pypi.python.org/packages/source/M/M2Crypto/M2Crypto-0.20.1.tar.gz

won't build on Red Hat Linux / 386.  The error is

swig -python -I/usr/local/include/python2.5 -I/usr/include -includeall -o 
SWIG/_m2crypto_wrap.c


SWIG/_m2crypto.i
/usr/include/openssl/opensslconf.h:27: Error: CPP #error This openssl-devel 
package does not work your architecture?.

Use the -cpperraswarn option to continue swig processing.
error: command 'swig' failed with exit status 1

It's some incompatibility between Red Hat include file packaging and M2Crypto.

There was at one time a note on how to deal with this problem, but it's been 
deleted.  It's still in Google's cache, though.

http://74.125.155.132/search?q=cache:Bv79oR0b-msJ:www.heikkitoivonen.net/blog/2009/02/09/m2crypto-build-wrapper-for-fedora-core-based-distributions/+M2Crypto+buildingcd=2hl=enct=clnkgl=us

I think I went through this mess two years ago, building M2Crypto 0.17.  But
I've forgotten the solution.

What's current thinking on this?

(I know, Python 2.6 has new SSL support, but MySQLdb doesn't support
Python 2.6,  so I can't convert yet.)

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


Re: python memory use

2009-09-30 Thread TerryP
Honestly, the only performance data involving Java, that would ever
surprise me: is when a Java program takes less time to startup and get
going, then the computer it is being run from did ;).


When planning-ahead for a project, I look at what performance the
language implementations offer, in the light of Blazingly fast on all
but the extreme cases or Fast enough for the job, and still cycles
leftover to toast bread with like questions; the rest gets more
specific to the problem domain. I have only ever had one main stream
language prove to slow for my needs over the years, and that was
because it was the least optimal use for perl... although I must
admit, I would never want to try software rendering in pure Python (to
what extent that is possible).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python book

2009-09-30 Thread James Matthews
I like core python programming and dive into python.

On Wed, Sep 30, 2009 at 11:57 AM, Patrick Sabin
patrick.just4...@gmail.comwrote:

 My favorite book is Python Essential Reference from David M. Beazley.
 It is not a beginner book. It is about the python language and not about  a
 framework or third-party library. It is much more complete than for instance
 Dive into python, but maybe somewhat more difficult.

 - Patrick


 lallous wrote:

 Hello

 Can anyone suggest a good book Python book for advancing from beginner
 level?

 (I started with Learning Python 3rd ed)

 Regards,
 Elias


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




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


Re: is there some error crashing reporting

2009-09-30 Thread Stef Mientki

Stef Mientki wrote:

like MadExcept for Delphi
http://www.madshi.net/madExceptDescription.htm

which catches any error,
send an email with the error report and complete system analysis to 
the author,

and continues the program (if possible)

thanks,
Stef

apparently there isn't any such tool ;-(
thanks,
Stef
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python book

2009-09-30 Thread geremy condra
On Wed, Sep 30, 2009 at 7:58 AM, lallous lall...@lgwm.org wrote:

 Hello

 Can anyone suggest a good book Python book for advancing from beginner
 level?

 (I started with Learning Python 3rd ed)

 Regards,
 Elias


dive into python and, for me, foundations of python network programming-
narrowly targeted, but practical and full of good examples.

Geremy Condra


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


Problem building Python from source

2009-09-30 Thread Kent Tenney
Trying to do a vanilla cmmi:

~/Python-2.6.3rc1$ ./configure
~/Python-2.6.3rc1$ make
...
Traceback (most recent call last):
...
ImportError: No module named cStringIO
make: *** [sharedmods] Error 1

The fix is to uncomment the line in Modules/Setup
#cStringIO cStringIO.c

Question:

Is there an argument to ./configure or make, or an environment setting
which will make cStringIO available without editing Modules/Setup ?

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


Re: UnboundLocalError with extra code after return

2009-09-30 Thread John Posner

Duncan Booth wrote:


/ class CallableOnlyOnce(object):

/def __init__(self, func):
   self.func = func
   def __call__(self):
   f = self.func
   if f:
   self.func = None
   return f()


/ def callonce(func):

/   return CallableOnlyOnce(func)


/ @callonce

/def t2():
   print T2 called

   

/ t2()

/T2 called

/ t2()

// /


You don't need both the CallableOnlyOnce class and the callonce function. How 
about the following ... just for fun, it provides the extra functionality of 
counting the number of times a function was *called*, even though it was 
*executed* only once:

--
class ExecOnlyOnce(object):
   def __init__(self, f):
   print Making function '%s' into a one-shot % f.__name__
   self.func = f
   self.func_call_count = 0

   def __call__(self):
   print Calling one-shot function:, self.func.__name__
   self.func_call_count += 1
   if self.func_call_count == 1:
   return self.func()

@ExecOnlyOnce
def func1():
   print   inside original function: func1
   return 111

@ExecOnlyOnce
def func2():
   print   inside original function: func2
   return 222

# run the functions
# collect return values just once for each function
r1 = func1()
r2 = func2()
func1()
func1()
func2()
func1()

# summary
print \nSummary:
for fn in (func1, func2):
   print Function '%s' was called %d times % (fn.func.__name__, 
fn.func_call_count)

print r1 =, r1
print r2 =, r2
--

Output:

Making function 'func1' into a one-shot
Making function 'func2' into a one-shot
Calling one-shot function: func1
 inside original function: func1
Calling one-shot function: func2
 inside original function: func2
Calling one-shot function: func1
Calling one-shot function: func1
Calling one-shot function: func2
Calling one-shot function: func1

Summary:
Function 'func1' was called 4 times
Function 'func2' was called 2 times
r1 = 111
r2 = 222

-John


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


Quick compare string to list

2009-09-30 Thread Scooter
I'm reading in a text file, and for each line in the file, I'm looking
for the existence of phrases from a list. The list contains approx.
120 items currently but will most likely grow. This procedure itself
is not the main function of my program and only grew out of the need
to reformat certain phrases I'm finding in a file before re-outputting
it. But as I suspected, this searching of the lists slows the whole
process way way down. Was looking for ideas of a better way to do
this.

I basically have

mylist=[]
...
code that reads in the flat file into string 'flatfileString'
...
for listitem in mylist:
if flatfileString.count(listitem):
...whatever...I found it.

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


Re: Quick compare string to list

2009-09-30 Thread Terry Reedy

Scooter wrote:

I'm reading in a text file, and for each line in the file, I'm looking
for the existence of phrases from a list. The list contains approx.
120 items currently but will most likely grow. This procedure itself
is not the main function of my program and only grew out of the need
to reformat certain phrases I'm finding in a file before re-outputting
it. But as I suspected, this searching of the lists slows the whole
process way way down. Was looking for ideas of a better way to do
this.

I basically have

mylist=[]
...
code that reads in the flat file into string 'flatfileString'
...
for listitem in mylist:
if flatfileString.count(listitem):
...whatever...I found it.


I would try:

turn mylist into my_re and compile
for line in file:
  while search line for first occurence of any phase returns yes:
  process
  reduce line to remainder of line after phrase found
  # assuming no overlaps

tjr

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


Re: Quick compare string to list

2009-09-30 Thread Emile van Sebille

On 9/30/2009 11:36 AM Scooter said...

I'm reading in a text file, and for each line in the file, I'm looking
for the existence of phrases from a list. The list contains approx.
120 items currently but will most likely grow. This procedure itself
is not the main function of my program and only grew out of the need
to reformat certain phrases I'm finding in a file before re-outputting
it. But as I suspected, this searching of the lists slows the whole
process way way down. Was looking for ideas of a better way to do
this.

I basically have

mylist=[]
...
code that reads in the flat file into string 'flatfileString'
...
for listitem in mylist:
if flatfileString.count(listitem):
...whatever...I found it.



Whatever you do next to reformat those certain phrases will require a 
second scan which doubles the time involved, and as you don't save the 
count anyway, if mylist were exchange couplets you could use replace 
directly.  Something like:


mylist = [('Chevy','Chevrolet'),
  ('GM','General Motors'),
  (... etc... )
 ]

for wrong,right in mylist:
flatfileString=flatfileString.replace(wrong,right)


Flavor to taste,

Emile

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


Re: python memory use

2009-09-30 Thread Paul Rubin
Gary Robinson gary...@me.com writes:
 I'd be interested in knowing whether anybody can share info about
 how representative those test results are. For instance, suppose
 we're talking about a huge dictionary that maps integers to lists of
 integers (something I use in my code). Would something like that
 really take up much more memory in Java (using the closest
 equivalent Java data structures) than in CPython? I find it hard to
 believe that that would be the case, but I'm quite curious.

Arrays of Java ints would use less memory than lists of Python's boxed
integers.  If you want unboxed ints in Python, use the array module.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python book

2009-09-30 Thread wesley chun
On Sep 30, 4:58 am, lallous lall...@lgwm.org wrote:

 Can anyone suggest a good book Python book for advancing from beginner level?
 (I started with Learning Python 3rd ed)

 From: James Matthews nyt...ail.com 
 Date: Wed Sep 30 18:47:58 CEST 2009

 I like core python programming and dive into python.


hi Elias, and welcome to Python! thanks for the plug james! this
question is asked somewhat regularly. i addressed it a few years but
most of my reply still applies:
http://www.mail-archive.com/python-list@python.org/msg109993.html

one big difference is that Alan Gauld's tutorial is now located at
http://www.alan-g.me.uk ... another difference is that a good number
of ultra-beginner (new to programming not just Python) books have come
on the market as well. finally, i created a DVD + PowerPoint version
of Core Python called Python Fundamentals if you want to watch video
presentations on topics in addition to reading.

if you're also looking to take an intensive Python training course
that will take you beyond beginner status, i'm offering one in about
6-7 weeks (near San Francisco) where i'll *give* you a copy of Core
Python :-) ... and speaking of which, i'm actually starting to
research what it would take to bring the book to a 3rd edition and
soliciting assistance from the community. more info on both the course
and the next edition here:

http://mail.python.org/pipermail/baypiggies/2009-September/005483.html

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2007,2001
Python Fundamentals, Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-30 Thread Mars creature
On Sep 30, 5:31 am, Jean-Michel Pichavant jeanmic...@sequans.com
wrote:
 Mars creature wrote:
  On Sep 29, 12:49 pm, Rami Chowdhury rami.chowdh...@gmail.com
  wrote:

  On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature jin...@gmail.com wrote:

  Dear Python users,
    I just start to use python and love this language. I met this
  problem when I try to save my functions in a separate file.
  The question is how I can pass a global variable to a function which
  is saved in another file. If I save the function I defined in the same
  file with the main program, there is no problem after I declare the
  global variable. But problem comes out when I save all the function is
  a separate file. Help is very much appreciated! Thanks!
  Jinbo

  In Python, as in many other languages, I'd advise that you think about  
  whether your variable needs to be global, or whether you could (or should) 
   
  simply pass the variable to the function as a parameter.

  HTH,
  Rami

  --
  Rami Chowdhury
  Never attribute to malice that which can be attributed to stupidity --  
  Hanlon's Razor
  408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

  Thank you guys for the prompt and helpful response.
  From the link Gregor posted, it seems no way to share variable between
  modules.

  I can understand the point that global variables tends to mess up
  programs.

  Assume that I have 10 parameters need to pass to the function. If
  these parameters are fixed, I can use another module to store these 10
  parameters, and import to the module, as suggested by jean-michel. But
  what if these 10 parameters will be changed in the main program?
  Passing the variable to the function as a parameter suggested by Rami
  will certainly do, but I am wondering weather there are other ways.
  What you'd like to code it?
  Thank you very much!
  Jinbo

 Why don't you post the function you're trying to code, with the
 parameter names ?
 Write the documentation for that function, write what it is supposed to
 do, the parameters, their purpose and the returned value. Just by doing
 this, you may be able to find all by yourself what should be the correct
 function prototype.

 JM

The function I am trying to code is quite simple and nothing special.
I guess what I wanted to say was how to avoid typing all parameters
everytime I am using the function. I used to use common block in
Fortran to keep the frequently used data. I could've put all
parameters in a file and import it, if they are unchangable. But in my
case the parameters are changing.

Allow me to say, unpacking the list or dictionary is the answer I
wanted, although this is too trivial for some of you.

Based on the discussion (correct me if I'm wrong),
1, try to avoid global,
2, if parameters are constant, put them in a tuple/list/dictionary and
import them
3, if parameters are changeable, pack them into a list/dictionary and
use *params (for list) or **params (for dict) to unpack and pass to
the function.

I want to thank you all! It's quite bit learning for me from your
discussion.
Jinbo
-- 
http://mail.python.org/mailman/listinfo/python-list


How different are a generator's send and next methods

2009-09-30 Thread Andrey Fedorov
As far as I can tell, a generator's .next() is equivalent to .send(None). Is
this true?
If so, aren't they unified in a method with a single argument which defaults
to None?
- Andrey
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python memory use

2009-09-30 Thread Bearophile
Gary Robinson:

(I could test the particular case I mention, but I'm wondering if someone has 
some fundamental knowledge that would lead to a basic understanding.)

Java is one of the languages most avid of memory, often even more than
Python 2.x. Some bad people have said that Java developers were not
that interested in saving RAM because Sun sells hardware, and the more
RAM it uses the more they can sell ;-)

More seriously, Java uses a complex hybrid generational garbage
collectors, while CPython uses a much simpler reference count GC +
cycle detector.

A reference counter usually has a lower performance compared to good
generational garbage collectors, especially if they are hybridized
with several other algorithms, but it's simpler (and most things in
CPython design are designed for simplicity even when they are a little
less efficient, and among other things this simplicity helps this
OpenSource project recruit and keep developers), it's almost
deterministic (so for example in some situations you can forget to
close a file) so it often uses less memory because in any moment you
have only the objects you are using (reference cycles add a little
extra complexity in this). While a generational GC keeps a lot of
extra memory unused, free, etc. There are studies that show that if
you use such kind of good generational GCs and you pay about a 2-5X
memory penalty you can have a language about as fast as ones where you
manually manage memory. Indeed today good Java programs are often no
more than 2X slower than C++ and sometimes are about as fast or even
faster (thanks to other optimizations, like a strong inlining of
virtual methods done by HotSpot).

If you want a language that uses less RAM you can try FreePascal :-)

I think that among the languages designed to work with a GC, the D
language is among the ones that uses less memory (because so far its
GC is not that good, so it saves memory while being slower than the
advanced GC used by Sun Java).

On 64 bit systems Java Sun has added an optimization, certain pointers
are compressed in 32 bits, reducing memory usage. Similar things may
be done by the LLVM too in future:
http://llvm.org/pubs/2005-06-12-MSP-PointerCompSlides.pdf
Maybe someday 64-bit CPython will do the same, or maybe
UnlandenSwallow or PyPy (Jthon in a sense may do it already if you use
it on a 64 bit Java. I don't know).

Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Quick compare string to list

2009-09-30 Thread Bearophile
Scooter:
 I'm reading in a text file, and for each line in the file, I'm looking
 for the existence of phrases from a list. The list contains approx.
 120 items currently but will most likely grow. This procedure itself
 is not the main function of my program and only grew out of the need
 to reformat certain phrases I'm finding in a file before re-outputting
 it. But as I suspected, this searching of the lists slows the whole
 process way way down. Was looking for ideas of a better way to do
 this.

Know your basic computer science :-)
http://en.wikipedia.org/wiki/Aho-Corasick_algorithm

There are probably C implementations that can be used from Python,
like:
http://hkn.eecs.berkeley.edu/~dyoo/python/ahocorasick/

Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


RotatingFileHandler issue

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

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

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

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

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


Re: Storing a C pointer in a Python class instance

2009-09-30 Thread sturlamolden
On 30 Sep, 19:03, Carl Banks pavlovevide...@gmail.com wrote:

 Second, CObjects do not have a __del__ method.  They call the supplied
 constructor from the type's tp_dealloc slot.  Use of the tp_dealloc
 slot does not, by itself, prevent cyclic GC.

 Bottom line is, the CObject's deallocator is as reliable as a custom
 type's tp_dealloc.

You are right. I did not look at the PyCObject_* API close enough.

I thought of wrapping the CObject with a Python class, and calling the
destructor from __del__. That would be less reliable.

S.M.






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


Re: unicode issue

2009-09-30 Thread Piet van Oostrum
 Dave Angel da...@ieee.org (DA) wrote:
[snip]
DA Thanks for the correction. What I meant by works for me is that the
DA single example in the docstring translated okay. But I do have a lot to
DA learn about using Unicode in sources, and I want to learn.

DA So tell me, how were we supposed to guess what encoding the original
DA message used? I originally had the mailing list message (in Thunderbird
DA email). When I copied (copy/paste) to Komodo IDE (text editor), it wouldn't
DA let me save because the file type was ASCII. So I randomly chosen latin-1
DA for file type, and it seemed to like it.

You can see the encoding of the message in its headers. But it is not
important, as the Unicode characters you see is what it is about. You
just copy and paste them in your Python file. The Python file does not
have to use the same encoding as the message from which you pasted. The
editor will do the proper conversion. (If it doesn't throw it away
immediately.) Only for the Python file you must choose an encoding that
can encode all the characters that are in the file. In this case utf-8
is the only reasonable choice, but if there are only latin-1 characters
in the file then of course latin-1 (iso-8859-1) will also be good.

Any decent editor will only allow you to save in an encoding that can
encode all the characters in the file, otherwise you will lose some
characters. 

Because Python must also know which encoding you used and this is not in
itself deductible from the file contents, you need the coding
declaration. And it must be the same as the encoding in which the file
is saved, otherwise Python will see something different than you saw in
your editor. Sooner or later this will give you a big headache.

DA At that point I expected and got errors from Python because I had no coding
DA declaration. I used latin-1, and still had problems, though I forget what
DA they were. Only when I changed the file encoding type again, to utf-8, did
DA the errors go away. I agree that they should agree, but I don't know how to
DA reconcile the copy/paste boundary, the file type (without BOM, which is
DA another variable), the coding declaration, and the stdout implicit ASCII
DA encoding. I understand a bunch of it, but not enough to be able to safely
DA walk through the choices.

DA Is this all written up in one place, to where an experienced programmer can
DA make sense of it? I've nibbled at the edges (even wrote a UTF-8 
DA encoder/decoder a dozen years ago).

I don't know a place. Usually utf-8 is a safe bet but in some cases can
be overkill. And then in you Python input/output (read/write) you may
have to use a different encoding if the programs that you have to
communicate with expect something different.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Quick compare string to list

2009-09-30 Thread Steven D'Aprano
On Wed, 30 Sep 2009 11:36:03 -0700, Scooter wrote:

 I'm reading in a text file, and for each line in the file, I'm looking
 for the existence of phrases from a list. The list contains approx. 120
 items currently but will most likely grow. This procedure itself is not
 the main function of my program and only grew out of the need to
 reformat certain phrases I'm finding in a file before re-outputting it.
 But as I suspected, this searching of the lists slows the whole process
 way way down. Was looking for ideas of a better way to do this.
 
 I basically have
 
 mylist=[]
 ...
 code that reads in the flat file into string 'flatfileString' ...
 for listitem in mylist:
 if flatfileString.count(listitem):
 ...whatever...I found it.


For starters, why are you bothering to count occurrences of the string if 
you only need a There/Not There answer? That's wasteful... it means the 
code has to walk the entire length of the flatfileString every single 
time. Now, string.count() is likely to be fast because it's written in C, 
but it's not instantaneous. Better is:


for listitem in mylist:
if listitem in flatfileString:
process()


That should show a small improvement, but you can probably do better. 
Here's two more simple approaches worth trying, all untested:

# Use a regex.
r = re.compile('|'.join(mylist))  # item 0 or item 1 or ... 
if r.search(flatfileString):
process()


# Use a loop, re-writing it as a list comprehension for speed.
if any([item in flatfileString for item in mylist]):
process()


# As above, but a generator expression instead.
if any(item in flatfileString for item in mylist):
process()



You will probably find that which approach is faster depends on how many 
items are in mylist.

If none of these approaches are fast enough, you may need to look at a 
more complicated approach, such as Bearophile's suggestion.



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


Re: Most active coroutine library project?

2009-09-30 Thread Rhodri James
On Mon, 28 Sep 2009 15:44:48 +0100, Grant Edwards  
inva...@invalid.invalid wrote:



$10 is pretty expensive for a lot of applications.  I bet that
processor also uses a lot of power and takes up a lot of board
space. If you've only got $2-$3 in the money budget, 200uA at
1.8V in the power budget, and 6mm X 6mm of board-space, your
choices are limited.

Besides If you can get by with 256 or 512 bytes of RAM, why pay
4X the price for a 1K part?

Besides which, the 8032 instruction set and development tools
are icky compared to something like an MSP430 or an AVR. ;)

[The 8032 is still head and shoulders above the 8-bit PIC
family.]


I was going to say, you want 256 bytes of RAM, you profligate
so-and-so?  Here, have 32 bytes of data space and stop your
whining :-)

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


Re: iterate over list while changing it

2009-09-30 Thread Aahz
In article mailman.430.1253826262.2807.python-l...@python.org,
Terry Reedy  tjre...@udel.edu wrote:
Torsten Mohr wrote:
 
 a = [1, 2, 3, 4, 5, 6]
 
 for i, x in enumerate(a):

If you change a list while iterating over, start at the tail.

This only applies if you add/remove elements; simply updating elements
does not require starting at the tail.
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

Normal is what cuts off your sixth finger and your tail...  --Siobhan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SVG PIL decoder

2009-09-30 Thread Patrick Sabin

Donn wrote:
Have a look at Cairo (python-cairo) in conjunction with librsvg (python-rsvg) 
-- that'll fix you up. You can go from an SVG to a PNG/array and thence into 
PIL if you need to.


Thanks for the tip. Got it work, although it was a bit tricky, as 
resizing doesn't seem to be supported by python-rsvg and 
cairo.ImageSurface.create_from_png doesn't allow StringIO or 
TemporaryFile for some reason (got Memory Error). So the code, if 
someone else needs it or someone can improve it:


def open_svg_as_image(fn, width, height):
tmpfd, tmppath = tempfile.mkstemp(.png)
tmpfile = os.fdopen(tmpfd,'w')

file = StringIO.StringIO()
svgsurface = cairo.SVGSurface (file, width, height)
svgctx = cairo.Context(svgsurface)
svg = rsvg.Handle(file=fn)
svgwidth = svg.get_property('width')
svgheight = svg.get_property('height')
svgctx.scale(width/float(svgwidth),height/float(svgheight))
svg.render_cairo(svgctx)

svgsurface.write_to_png(tmpfile)
tmpfile.close()
svgsurface.finish()

tmpfile = open(tmppath, 'r')
imgsurface = cairo.ImageSurface.create_from_png(tmpfile)
imgwidth = imgsurface.get_width()
imgheight = imgsurface.get_height()

data = imgsurface.get_data()

im = Image.frombuffer(RGBA,(imgwidth, imgheight),
data ,raw,RGBA,0,1)
os.remove(tmppath)
return im

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


Re: Most active coroutine library project?

2009-09-30 Thread Grant Edwards
On 2009-09-30, Rhodri James rho...@wildebst.demon.co.uk wrote:
 On Mon, 28 Sep 2009 15:44:48 +0100, Grant Edwards  
inva...@invalid.invalid wrote:

 $10 is pretty expensive for a lot of applications.  I bet that
 processor also uses a lot of power and takes up a lot of board
 space. If you've only got $2-$3 in the money budget, 200uA at
 1.8V in the power budget, and 6mm X 6mm of board-space, your
 choices are limited.

 Besides If you can get by with 256 or 512 bytes of RAM, why pay
 4X the price for a 1K part?

 Besides which, the 8032 instruction set and development tools
 are icky compared to something like an MSP430 or an AVR. ;)

 [The 8032 is still head and shoulders above the 8-bit PIC
 family.]

 I was going to say, you want 256 bytes of RAM, you profligate
 so-and-so?  Here, have 32 bytes of data space and stop your
 whining :-)

What?  You had 1's?  All we had were 0's.  And we _liked_ it.

-- 
Grant




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


Re: Python logging and 1.5.2 compatibility

2009-09-30 Thread Aahz
In article mailman.693.1254317221.2807.python-l...@python.org,
Vinay Sajip  vinay_sa...@yahoo.co.uk wrote:

I'm planning to officially drop support for Python 1.5.2 in the logging
package.

Sounds good -- posting publicly about it is definitely appreciated.
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

Normal is what cuts off your sixth finger and your tail...  --Siobhan
-- 
http://mail.python.org/mailman/listinfo/python-list


sympy returns a dictionary sometimes, and sometimes a list of tuples...why?

2009-09-30 Thread Brian Blais

Hello,

I wrote a very simple script using sympy, and things were working  
fine, except for one problem.  So I have:


from sympy import *

x, y = symbols('x','y',real=True)
alpha,beta,gamma=symbols('alpha','beta','gamma',real=True)
alpha_p,beta_p,gamma_p=symbols('alpha_p','beta_p','gamma_p',real=True)

L = symbols('L',real=True)



and then I look at solutions to some equations, like:

solution=solve([beta*y - alpha*(1+y/L) ,
-beta_p*x + alpha_p ], [x, y])

print solution


which prints (correctly):

{x: alpha_p/beta_p, y: L*alpha/(-alpha + L*beta)}

now, if I do:

solution=solve([beta*y - alpha*(1+y/L) - gamma*x*(1+y/L),
-beta_p*x + alpha_p - gamma_p*y], [x, y])

print solution

I get a very long solution, which isn't a problem, but it isn't a  
dictionary: I can't tell what is the solution for x and for y.  What  
I get is:


[(-(alpha*beta_p*abs(gamma)*abs(gamma_p) + alpha_p*gamma*abs(gamma) 
*abs(gamma_p) - L*beta*beta_p*abs(gamma)*abs(gamma_p) -  
L*gamma*gamma_p*abs(gamma)*abs(gamma_p) - gamma*gamma_p* 
(2*alpha*alpha_p*beta_p*gamma - 2*L*alpha_p*beta*beta_p*gamma +  
2*L*alpha*beta_p*gamma*gamma_p + alpha**2*beta_p**2 +  
alpha_p**2*gamma**2 + L**2*beta**2*beta_p**2 +  
L**2*gamma**2*gamma_p**2 - 2*L*alpha*beta*beta_p**2 +  
2*L*alpha_p*gamma_p*gamma**2 + 2*beta*beta_p*gamma*gamma_p*L**2)** 
(1/2))/(2*beta_p*gamma*abs(gamma)*abs(gamma_p)) + alpha_p/beta_p,  
(alpha*beta_p*abs(gamma)*abs(gamma_p) + alpha_p*gamma*abs(gamma)*abs 
(gamma_p) - L*beta*beta_p*abs(gamma)*abs(gamma_p) -  
L*gamma*gamma_p*abs(gamma)*abs(gamma_p) - gamma*gamma_p* 
(2*alpha*alpha_p*beta_p*gamma - 2*L*alpha_p*beta*beta_p*gamma +  
2*L*alpha*beta_p*gamma*gamma_p + alpha**2*beta_p**2 +  
alpha_p**2*gamma**2 + L**2*beta**2*beta_p**2 +  
L**2*gamma**2*gamma_p**2 - 2*L*alpha*beta*beta_p**2 +  
2*L*alpha_p*gamma_p*gamma**2 + 2*beta*beta_p*gamma*gamma_p*L**2)** 
(1/2))/(2*gamma*gamma_p*abs(gamma)*abs(gamma_p))), (-(alpha*beta_p*abs 
(gamma)*abs(gamma_p) + alpha_p*gamma*abs(gamma)*abs(gamma_p) -  
L*beta*beta_p*abs(gamma)*abs(gamma_p) - L*gamma*gamma_p*abs(gamma)*abs 
(gamma_p) + gamma*gamma_p*(2*alpha*alpha_p*beta_p*gamma -  
2*L*alpha_p*beta*beta_p*gamma + 2*L*alpha*beta_p*gamma*gamma_p +  
alpha**2*beta_p**2 + alpha_p**2*gamma**2 + L**2*beta**2*beta_p**2 +  
L**2*gamma**2*gamma_p**2 - 2*L*alpha*beta*beta_p**2 +  
2*L*alpha_p*gamma_p*gamma**2 + 2*beta*beta_p*gamma*gamma_p*L**2)** 
(1/2))/(2*beta_p*gamma*abs(gamma)*abs(gamma_p)) + alpha_p/beta_p,  
(alpha*beta_p*abs(gamma)*abs(gamma_p) + alpha_p*gamma*abs(gamma)*abs 
(gamma_p) - L*beta*beta_p*abs(gamma)*abs(gamma_p) -  
L*gamma*gamma_p*abs(gamma)*abs(gamma_p) + gamma*gamma_p* 
(2*alpha*alpha_p*beta_p*gamma - 2*L*alpha_p*beta*beta_p*gamma +  
2*L*alpha*beta_p*gamma*gamma_p + alpha**2*beta_p**2 +  
alpha_p**2*gamma**2 + L**2*beta**2*beta_p**2 +  
L**2*gamma**2*gamma_p**2 - 2*L*alpha*beta*beta_p**2 +  
2*L*alpha_p*gamma_p*gamma**2 + 2*beta*beta_p*gamma*gamma_p*L**2)** 
(1/2))/(2*gamma*gamma_p*abs(gamma)*abs(gamma_p)))]



which, if you ignore the complexity, is a list of two items, each  
item is a tuple of length 2.  What does this mean?  I can handle the  
mess of the solution, but I'd like to know what is a solution for x,  
and for y.  Why isn't it in a dictionary?  Am I doing something wrong?



thanks,

Brian Blais


--
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais



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


ActivePython 3.1.1.2 vs Python 3.1.1 for OSX?

2009-09-30 Thread Robert Hicks
I am just curious which I should use. I am going to start learning
Python soon. Are they comparable and I just do a eenie meenie minie
moe?

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


Re: SVG PIL decoder

2009-09-30 Thread Donn
On Thursday 01 October 2009 01:08:28 Patrick Sabin wrote:
 Thanks for the tip. Got it work, although it was a bit tricky, as
 resizing doesn't seem to be supported by python-rsvg and
 cairo.ImageSurface.create_from_png doesn't allow StringIO or
My best suggestions are to visit the Cairo website -- inside there somewhere 
is a recipe page with many samples in Python. 

Next would be  http://www.tortall.net/mu/wiki/CairoTutorial. 

Third is a tutorial I made (perhaps less useful) on my site 
http://otherwise.relics.co.za/wiki/Tuts/Python/Cairo/ links at bottom of that 
page

Fourth is to join the ca...@cairographics.org mailing list at 
http://lists.cairographics.org/mailman/listinfo/cairo they are super helpful.

Lastly is my animation API (in sig)which is also Python and may help you with 
the source.

The general idea for scaling is to use matrices (cairo provides all commands) 
and then output the surface to a file-like object.

My animation API brings selected snippets of SVG in from an Inkscape file 
(tagged by id), animates them by tweening and can output each frame to another 
SVG or to a PNG.

HTH,
\d
-- 
home: http://otherwise.relics.co.za/
2D vector animation : https://savannah.nongnu.org/projects/things/
Font manager : https://savannah.nongnu.org/projects/fontypython/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a global variable to a module?

2009-09-30 Thread Mel
Mars creature wrote:

 On Sep 30, 5:31 am, Jean-Michel Pichavant jeanmic...@sequans.com
 wrote:
 Mars creature wrote:
  On Sep 29, 12:49 pm, Rami Chowdhury rami.chowdh...@gmail.com
  wrote:

  On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature jin...@gmail.com
  wrote:

  Dear Python users,
  I just start to use python and love this language. I met this
  problem when I try to save my functions in a separate file.
  The question is how I can pass a global variable to a function which
  is saved in another file. If I save the function I defined in the
  same file with the main program, there is no problem after I declare
  the global variable. But problem comes out when I save all the
  function is a separate file. Help is very much appreciated! Thanks!
  Jinbo

  In Python, as in many other languages, I'd advise that you think about
  whether your variable needs to be global, or whether you could (or
  should) simply pass the variable to the function as a parameter.

  HTH,
  Rami

  --
  Rami Chowdhury
  Never attribute to malice that which can be attributed to stupidity
  -- Hanlon's Razor
  408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

  Thank you guys for the prompt and helpful response.
  From the link Gregor posted, it seems no way to share variable between
  modules.

  I can understand the point that global variables tends to mess up
  programs.

  Assume that I have 10 parameters need to pass to the function. If
  these parameters are fixed, I can use another module to store these 10
  parameters, and import to the module, as suggested by jean-michel. But
  what if these 10 parameters will be changed in the main program?
  Passing the variable to the function as a parameter suggested by Rami
  will certainly do, but I am wondering weather there are other ways.
  What you'd like to code it?
  Thank you very much!
  Jinbo

 Why don't you post the function you're trying to code, with the
 parameter names ?
 Write the documentation for that function, write what it is supposed to
 do, the parameters, their purpose and the returned value. Just by doing
 this, you may be able to find all by yourself what should be the correct
 function prototype.

 JM
 
 The function I am trying to code is quite simple and nothing special.
 I guess what I wanted to say was how to avoid typing all parameters
 everytime I am using the function. I used to use common block in
 Fortran to keep the frequently used data. I could've put all
 parameters in a file and import it, if they are unchangable. But in my
 case the parameters are changing.

Write a function that calls the function you want to call, taking the 
arguments you want to retype, and filling in all the arguments you don't:

def stand_in (great, nifty):
call a_function (bo, great, ri, nifty, ng)


Mel.


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


Python and ASP - failure on 2nd and subsequent page loads

2009-09-30 Thread Jon Southard
I would be grateful for any advice about a problem which is preventing me from 
using Python for my current project.

I am hoping to use Python 2.6.2 on the server side with Microsoft ASP [not 
ASP.NET; version details below].   The behavior I see is:


1.   Load very simple page [text below] into browser [Firefox or IE, 
versions below].  Everything displays, fine.

2.   Exit browser, restart it, and reload the same page; or open a second 
tab and load the same page.

3.   In either case, page fails to load, with error:
Python ActiveX Scripting Engine, ASP 0211 (0x80020009)
A built-in ASP object has been referenced, which is no longer valid.

If I edit the page in any way, or just 'touch' it, and refresh the browser, the 
page will load correctly the first time, and then fail again as described.

The page I am using has no actual Python scripting, just the LANGUAGE 
directive.  Here is the complete page text:

%...@language=python%
HTML
head/head
body
h1Python Test/h1
p
/body
/html

Of course, things don't get any better if I put in % % with some actual 
Python in between.

I found a five-year-old newsgroup posting with what seemed to be a similar (but 
not identical) problem.   This thread said that the problem went away if 
server-side debugging was turned off.  I have it turned off, no effect.I 
also tried adding some Response object settings to disallow cacheing, but these 
did not have any effect.

Version Info:

Python:  2.6.2
PyWin:  pywin32-214 (I ran the client\pyscript.py and server\axsite.py scripts 
for setup)
Client OS:  Windows XP Pro SP2
Browsers:   Firefox 3.0.14, IE 8.0.6  [identical bug]
Server:   IIS 5.1

Note, I also tried using Python 3.1 with corresponding pywin, but I could not 
get that to work with ASP at all;  just got server 500 errors.   I'd prefer to 
use Python 2.6.2 anyway since I am fairly new to Python.

I would be very grateful for any expert pointers on this, since if I can't 
solve it I will probably have to go use PHP, Javascript, or something else.  
Thank you!


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


Re: iterate over list while changing it

2009-09-30 Thread Дамјан Георгиевски

 a = [1, 2, 3, 4, 5, 6]
 
 for i, x in enumerate(a):
 
 If you change a list while iterating over, start at the tail.
 
 ...reversed(enumerate(a))

Python 2.6.2 (r262:71600, Jul 20 2009, 02:19:59) 
 a = [1, 2, 3, 4, 5, 6]
 reversed(enumerate(a))
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: argument to reversed() must be a sequence


 no such list method, which mean you did not run the above before
 posting. Boo!



-- 
дамјан ( http://softver.org.mk/damjan/ )

Teaching a pig to sing wastes your time  annoys the pig.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Partial directory search question

2009-09-30 Thread alex23
Tim Chase python.l...@tim.thechases.com wrote:
 If you're doing more processing than just printing it, your
 for-loop is a better (clearer) way to go.  If you have lots of
 processing code, it might help to do the inverse:

    for filename in os.listdir(location):
      if not filename.startswith('_'): continue
      lots()
      of_processing()
      and_your_complex_logic()
      goes()
      here()

Personally, I'd still go with a generator to drive the for-loop:

underscored_files = (f for f in os.listdir(location) if not
f.startswith('_'))
for filename in underscored_files:
 etc...

What I'm traversing in the for-loop is far more obvious (to me) from
the name of the generator than from having to parse the first few
lines of the loop. It's a lot easier to genericise that behaviour too.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple if-else question

2009-09-30 Thread alex23
dksr dksre...@gmail.com wrote:
 Yes thats what I thought. for-else looks similar to if-else and in if-
 else, else part is executed only when if part is not executed, but in
 for-else it has entirely a different job.

If you think of if-else more in terms of the else-branch occurring
when the if-condition is no longer true (as opposed to 'not
executing'), they're a lot more similar in approach than different.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sympy returns a dictionary sometimes, and sometimes a list of tuples...why?

2009-09-30 Thread Robert Kern

Brian Blais wrote:

Hello,

I wrote a very simple script using sympy, and things were working fine, 
except for one problem.  So I have:


You will probably want to ask on the sympy mailing list:

  http://groups.google.com/group/sympy


from sympy import *

x, y = symbols('x','y',real=True)
alpha,beta,gamma=symbols('alpha','beta','gamma',real=True)
alpha_p,beta_p,gamma_p=symbols('alpha_p','beta_p','gamma_p',real=True)

L = symbols('L',real=True)



and then I look at solutions to some equations, like:

solution=solve([beta*y - alpha*(1+y/L) ,
-beta_p*x + alpha_p ], [x, y])

print solution


which prints (correctly):

{x: alpha_p/beta_p, y: L*alpha/(-alpha + L*beta)}

now, if I do:

solution=solve([beta*y - alpha*(1+y/L) - gamma*x*(1+y/L),
-beta_p*x + alpha_p - gamma_p*y], [x, y])

print solution

I get a very long solution, which isn't a problem, but it isn't a 
dictionary: I can't tell what is the solution for x and for y.  What I 
get is:


[(-(alpha*beta_p*abs(gamma)*abs(gamma_p) + 
alpha_p*gamma*abs(gamma)*abs(gamma_p) - 
L*beta*beta_p*abs(gamma)*abs(gamma_p) - 
L*gamma*gamma_p*abs(gamma)*abs(gamma_p) - 
gamma*gamma_p*(2*alpha*alpha_p*beta_p*gamma - 
2*L*alpha_p*beta*beta_p*gamma + 2*L*alpha*beta_p*gamma*gamma_p + 
alpha**2*beta_p**2 + alpha_p**2*gamma**2 + L**2*beta**2*beta_p**2 + 
L**2*gamma**2*gamma_p**2 - 2*L*alpha*beta*beta_p**2 + 
2*L*alpha_p*gamma_p*gamma**2 + 
2*beta*beta_p*gamma*gamma_p*L**2)**(1/2))/(2*beta_p*gamma*abs(gamma)*abs(gamma_p)) 
+ alpha_p/beta_p, (alpha*beta_p*abs(gamma)*abs(gamma_p) + 
alpha_p*gamma*abs(gamma)*abs(gamma_p) - 
L*beta*beta_p*abs(gamma)*abs(gamma_p) - 
L*gamma*gamma_p*abs(gamma)*abs(gamma_p) - 
gamma*gamma_p*(2*alpha*alpha_p*beta_p*gamma - 
2*L*alpha_p*beta*beta_p*gamma + 2*L*alpha*beta_p*gamma*gamma_p + 
alpha**2*beta_p**2 + alpha_p**2*gamma**2 + L**2*beta**2*beta_p**2 + 
L**2*gamma**2*gamma_p**2 - 2*L*alpha*beta*beta_p**2 + 
2*L*alpha_p*gamma_p*gamma**2 + 
2*beta*beta_p*gamma*gamma_p*L**2)**(1/2))/(2*gamma*gamma_p*abs(gamma)*abs(gamma_p))), 
(-(alpha*beta_p*abs(gamma)*abs(gamma_p) + 
alpha_p*gamma*abs(gamma)*abs(gamma_p) - 
L*beta*beta_p*abs(gamma)*abs(gamma_p) - 
L*gamma*gamma_p*abs(gamma)*abs(gamma_p) + 
gamma*gamma_p*(2*alpha*alpha_p*beta_p*gamma - 
2*L*alpha_p*beta*beta_p*gamma + 2*L*alpha*beta_p*gamma*gamma_p + 
alpha**2*beta_p**2 + alpha_p**2*gamma**2 + L**2*beta**2*beta_p**2 + 
L**2*gamma**2*gamma_p**2 - 2*L*alpha*beta*beta_p**2 + 
2*L*alpha_p*gamma_p*gamma**2 + 
2*beta*beta_p*gamma*gamma_p*L**2)**(1/2))/(2*beta_p*gamma*abs(gamma)*abs(gamma_p)) 
+ alpha_p/beta_p, (alpha*beta_p*abs(gamma)*abs(gamma_p) + 
alpha_p*gamma*abs(gamma)*abs(gamma_p) - 
L*beta*beta_p*abs(gamma)*abs(gamma_p) - 
L*gamma*gamma_p*abs(gamma)*abs(gamma_p) + 
gamma*gamma_p*(2*alpha*alpha_p*beta_p*gamma - 
2*L*alpha_p*beta*beta_p*gamma + 2*L*alpha*beta_p*gamma*gamma_p + 
alpha**2*beta_p**2 + alpha_p**2*gamma**2 + L**2*beta**2*beta_p**2 + 
L**2*gamma**2*gamma_p**2 - 2*L*alpha*beta*beta_p**2 + 
2*L*alpha_p*gamma_p*gamma**2 + 
2*beta*beta_p*gamma*gamma_p*L**2)**(1/2))/(2*gamma*gamma_p*abs(gamma)*abs(gamma_p)))]



which, if you ignore the complexity, is a list of two items, each item 
is a tuple of length 2.  What does this mean?  I can handle the mess of 
the solution, but I'd like to know what is a solution for x, and for y. 
 Why isn't it in a dictionary?  Am I doing something wrong?


I suspect that the latter are two possible solutions given as an (x,y) pair. 
Possibly, they should be given as a list of dicts. Ask on the sympy mailing list.


--
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: ActivePython 3.1.1.2 vs Python 3.1.1 for OSX?

2009-09-30 Thread Jon Clements
On 1 Oct, 00:51, Robert Hicks sigz...@gmail.com wrote:
 I am just curious which I should use. I am going to start learning
 Python soon. Are they comparable and I just do a eenie meenie minie
 moe?

 Bob

First off, a great choice of language to begin trying! Is it your
first language (I'm guessing not), or do you come from another
'background'.

Basically, Active is a possible 'superset' of the main distro. of
Python. So, for Windows, for instance, it will offer com objects
etc...

I normally stick with the Python core, then use additional libraries
where appropriate.

Just my 2p,

Jon.

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


Re: ActivePython 3.1.1.2 vs Python 3.1.1 for OSX?

2009-09-30 Thread Robert H
On Sep 30, 9:07 pm, Jon Clements jon...@googlemail.com wrote:
 On 1 Oct, 00:51, Robert Hicks sigz...@gmail.com wrote:

  I am just curious which I should use. I am going to start learning
  Python soon. Are they comparable and I just do a eenie meenie minie
  moe?

  Bob

 First off, a great choice of language to begin trying! Is it your
 first language (I'm guessing not), or do you come from another
 'background'.

 Basically, Active is a possible 'superset' of the main distro. of
 Python. So, for Windows, for instance, it will offer com objects
 etc...

 I normally stick with the Python core, then use additional libraries
 where appropriate.

 Just my 2p,

 Jon.

Yes, I currently do my SA stuff in Perl. We added a product that uses
Python (and as a side my son wants to learn Blender) so I thought I
might look into it is as well.

Thank you for the reply.

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


Re: ActivePython 3.1.1.2 vs Python 3.1.1 for OSX?

2009-09-30 Thread srid
On Sep 30, 4:51 pm, Robert Hicks sigz...@gmail.com wrote:
 I am just curious which I should use. I am going to start learning
 Python soon. Are they comparable and I just do a eenie meenie minie
 moe?

ActivePython is essentially same as the installers from python.org -
but it also comes with additional documentation and tutorials, such
as:

Python FAQs
A snapshot of the Python Enhancement Proposals (PEPs) (For the most
recent version, refer to the PEPs on python.org .)
Dive Into Python (A tutorial for programmers)
Non-Programmers Tutorial For Python

http://docs.activestate.com/activepython/3.1/whatsincluded.html

Also note that 2.6.x is probably the best bet if you are going to use
some 3rd party libraries (after you learn the basics of Python) ..
because 3.x does not have many of those libraries ported yet.

  http://www.activestate.com/activepython/

Further, early next week - a new release of ActivePython-2.6 will be
made available that will include, for the first time, a new Python
package manager (PyPM) from ActiveState that makes it easier to
install packages from pypi.python.org (without having to compile them
yourself). This is similar to PPM from ActivePerl.

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


Re: ActivePython 3.1.1.2 vs Python 3.1.1 for OSX?

2009-09-30 Thread Robert H
On Sep 30, 9:28 pm, srid sridhar.ra...@gmail.com wrote:
 On Sep 30, 4:51 pm, Robert Hicks sigz...@gmail.com wrote:

  I am just curious which I should use. I am going to start learning
  Python soon. Are they comparable and I just do a eenie meenie minie
  moe?

 ActivePython is essentially same as the installers from python.org -
 but it also comes with additional documentation and tutorials, such
 as:

 Python FAQs
 A snapshot of the Python Enhancement Proposals (PEPs) (For the most
 recent version, refer to the PEPs on python.org .)
 Dive Into Python (A tutorial for programmers)
 Non-Programmers Tutorial For Python

 http://docs.activestate.com/activepython/3.1/whatsincluded.html

 Also note that 2.6.x is probably the best bet if you are going to use
 some 3rd party libraries (after you learn the basics of Python) ..
 because 3.x does not have many of those libraries ported yet.

  http://www.activestate.com/activepython/

 Further, early next week - a new release of ActivePython-2.6 will be
 made available that will include, for the first time, a new Python
 package manager (PyPM) from ActiveState that makes it easier to
 install packages from pypi.python.org (without having to compile them
 yourself). This is similar to PPM from ActivePerl.

 -srid

Thanks!

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


  1   2   >