Re: def X(l=[]): weirdness. Python bug ?

2008-08-28 Thread Andrew Lee

Bart van Deenen wrote:

Hi all.

I've stumbled onto a python behavior that I don't understand at all.

Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) 

# function 
def X(l=[]):

   l.append(1)
   print l

# first call of X
X()
[1]

#second call of X
X()
[1, 1]

Where does the list parameter 'l' live between the two successive calls of X(). 
Why is it not recreated with an empty list?

Is this correct behavior or is it a Python bug?
Does anyone have any pointers to the language documentation where this behavior 
is described?

Thanks all

Bart van Deenen



I happen to be reading about decorators at the moment:

from copy import deepcopy
def nodefault(myfunc):
myfunc_defaults = myfunc.func_defaults
def fresh(*args, **kwargs):
myfunc.func_defaults = deepcopy(myfunc_defaults)
return myfunc(*args, **kwargs)
return fresh

@nodefault
def X(l=[]):
l.append(1)
print l

 for i in range(1,6):
... X()
...
[1]
[1]
[1]
[1]
[1]


Which is just a very fancy way of doing:
def X(l=[]):
if l is None:
l = []
l.append(1)
print l

* sound of two pennies *
--
http://mail.python.org/mailman/listinfo/python-list


Re: Write bits in file

2008-05-23 Thread Andrew Lee

Tim Roberts wrote:

Monica Leko [EMAIL PROTECTED] wrote:

I have a specific format and I need binary representation.  Does
Python have some built-in function which will, for instance, represent
number 15 in exactly 10 bits?


For the record, I'd like to point out that even C cannot do this.  You need
to use shifting and masking to produce a stream of 8-bit bytes, or to
extract your values from a stream of 8-bit bytes.



H, bitfields are exactly non-aligned bits in a structure and are 
part of ANSI C.  Although C gives no guarantee of the ordering of fields 
within machine words ... so bitfieds are of limited use in portable 
programs unless they are 0-initialized.


IIRC, Huffman code uses arbitrary length bit strings and is the basis of 
many compression algorithms.





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


Re: can python do some kernel stuff?

2008-05-23 Thread Andrew Lee

Jimmy wrote:

Hi to all

python now has grown to a versatile language that can
accomplish tasks for many different purposes. However,
AFAIK, little is known about its ability of kernel coding.

So I am wondering if python can do some kernel coding that
used to be the private garden of C/C++. For example, can python
intercept the input of keyboard on a system level? someone told me
it's a kernel thing, isn't it?



http://wiki.python.org/moin/elmer

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


Re: [Regexes] Stripping puctuation from a text

2008-05-23 Thread Andrew Lee

shabda raaj wrote:

I want to strip punctuation from text.

So I am trying,


p = re.compile('[a-zA-Z0-9]+')
p.sub('', 'I love tomatoes!! hell yeah! ... Why?')

'  !!  ! ... ?'

Which gave me all the chars which I want to replace.

So Next I tried by negating the regex,


p = re.compile('^[a-zA-Z0-9]+')
p.sub('', 'I love tomatoes!! hell yeah! ... Why?')

' love tomatoes!! hell yeah! ... Why?'

But this removed the first char instead of the puctuation. So I guess
^ is matching start of line, instead of negation. How can I take
negation of the regex here?



p = re.compile('[^a-zA-Z0-9]+')
--
http://mail.python.org/mailman/listinfo/python-list


Re: php vs python

2008-05-23 Thread Andrew Lee

notbob wrote:

I'm not posting this just to initiate some religious flame war, though it's
the perfect subject to do so.  No, I actaully want some serious advice about
these two languages and since I think usenet is the best arena to find it,
here ya' go.

So, here's my delimna: I want to start a blog.  Yeah, who doesn't.  Yet, I
want learn the guts of it instead of just booting up some wordwank or
whatever.  I started to learn python, but heard php was easier or faster or
more like shell scripting or... fill in the blank.  Anyway, so I change over
to learning php.  Then I run across that blog, Coding Horror, and start
reading articles like this:

http://www.codinghorror.com/blog/archives/001119.html

Now what?  Go back to python.  Soldier on with php?  What do I know?  Not
much.  I can setup mysql and apache,, but don't know how to use 'em, really.
I use emacs and run slackware and can fumble my way through bash scripts,
but I can't really write them or do lisp.  I've taken basic basic and basic
C, but am barely literate in html.  Sometimes it seems overwhelming, but I
persevere because it's more fun/challenging than video games, which bore me
to tears.  


Well, that's my actual question, then.  Is php really so bad I'm just
wasting my time?  Or is it really the quickest way to blog functionality?
Would I be better served in the long run learning python, which claims to be
easy as pie to learn/program (still looks hard to me).  I admit I'm no code
geek.  But, I'm not completely brain dead, either, and I need something to
keep my geezer brain sparking.  What say ye?

nb


Personally, I believe PHP would get you more productive more quickly for 
a blog, but it is a potentially brain damaging language in terms of 
really getting your juices flowing with programming.  It is not a 
general purpose language and suffers from all the limitations of of a 
tool designed for one job.  If you are interested in programming and the 
blog is your path to that, stick with Python!  In particular, immerse 
yourself in mod_python or look at a framework like Django or Pylons -- 
the learning curve is steep for any of these technologies but they are a 
full meal compared to saltine cracker and water of PHP.

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


Re: can python do some kernel stuff?

2008-05-23 Thread Andrew Lee

Jimmy wrote:

On May 23, 3:05 pm, Andrew Lee [EMAIL PROTECTED] wrote:

Jimmy wrote:

Hi to all
python now has grown to a versatile language that can
accomplish tasks for many different purposes. However,
AFAIK, little is known about its ability of kernel coding.
So I am wondering if python can do some kernel coding that
used to be the private garden of C/C++. For example, can python
intercept the input of keyboard on a system level? someone told me
it's a kernel thing, isn't it?

http://wiki.python.org/moin/elmer


well, straightly speaking, how can I know a key is pressed on a system-
level if
using python?


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

Unless you are using an ancient piece of hardware -- a terminal is a 
pseudo terminal and a key stroke isn't a kernel event at all.


If you were looking for examples of kernel level functions you might 
want to consider driver interfaces -- how to program device interfaces 
-- that, too, can be done in Python -- but, again, you are always 
calling some underlying C function exposed via SWIG or another cross 
compilation tool.  Python doesn't reinvent system calls!  :-)

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


Re: can python do some kernel stuff?

2008-05-23 Thread Andrew Lee

Diez B. Roggisch wrote:

Jimmy schrieb:

On May 23, 3:05 pm, Andrew Lee [EMAIL PROTECTED] wrote:

Jimmy wrote:

Hi to all
python now has grown to a versatile language that can
accomplish tasks for many different purposes. However,
AFAIK, little is known about its ability of kernel coding.
So I am wondering if python can do some kernel coding that
used to be the private garden of C/C++. For example, can python
intercept the input of keyboard on a system level? someone told me
it's a kernel thing, isn't it?

http://wiki.python.org/moin/elmer


well, straightly speaking, how can I know a key is pressed on a system-
level if
using python?


What has that todo with kernel programming? You can use e.g. pygame to 
get keystrokes. Or under linux, read (if you are root) the keyboard 
input file - I've done that to support several keyboards attached to a 
machine.


And the original question: no, python can't be used as kernel 
programming language. Amongst other reasons, performance  the GIL 
prevent that.


Diez


http://www.kernel-panic.it/programming/py-pf/

Of course you can code kernel routines in Python -- you are just calling 
the underlying C interface.  The GIL means you have to manage 
threadsafety on your own -- it doesn't imply kernel programming can not 
be done.


I'm not talking about writing an OS in Python (though a simple DOS-like 
OS is very possible).  Nor would I suggest writing device drivers in 
Python -- but of course you can call kernel routines and manage some 
kernel resources effectively.  Python's IPC libraries expose kernel 
functionality.

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


Re: can python do some kernel stuff?

2008-05-23 Thread Andrew Lee

Diez B. Roggisch wrote:

Andrew Lee schrieb:

Diez B. Roggisch wrote:

Jimmy schrieb:

On May 23, 3:05 pm, Andrew Lee [EMAIL PROTECTED] wrote:

Jimmy wrote:

Hi to all
python now has grown to a versatile language that can
accomplish tasks for many different purposes. However,
AFAIK, little is known about its ability of kernel coding.
So I am wondering if python can do some kernel coding that
used to be the private garden of C/C++. For example, can python
intercept the input of keyboard on a system level? someone told me
it's a kernel thing, isn't it?

http://wiki.python.org/moin/elmer


well, straightly speaking, how can I know a key is pressed on a system-
level if
using python?


What has that todo with kernel programming? You can use e.g. pygame 
to get keystrokes. Or under linux, read (if you are root) the 
keyboard input file - I've done that to support several keyboards 
attached to a machine.


And the original question: no, python can't be used as kernel 
programming language. Amongst other reasons, performance  the GIL 
prevent that.


Diez


http://www.kernel-panic.it/programming/py-pf/

Of course you can code kernel routines in Python -- you are just 
calling the underlying C interface.  The GIL means you have to manage 
threadsafety on your own -- it doesn't imply kernel programming can 
not be done.


I understood the OP's question as can one program kernelspace routines 
in python. Which I don't think is possible. And I don't see how py-pf 
does that either.


Diez



OP: I am wondering if python can do some kernel coding that
used to be the private garden of C/C++.

The answer is yes.  IPC and py-pf are examples.  If you don't think of 
packet filtering as kernel coding, I can understand.  But clearly the 
Python interfaces to fork(), waitpid(), signal(), alarm() and so forth 
are forays into the once private garden of C.

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


Re: Markov Analysis Help

2008-05-22 Thread Andrew Lee

dave wrote:

Hi Guys,

I've written a Markov analysis program and would like to get your 
comments on the code  As it stands now the final input comes out as a 
tuple, then list, then tuple.  Something like ('the', 'water') ['us'] 
('we', 'took')..etc...


I'm still learning so I don't know any advanced techniques or methods 
that may have made this easier.



here's the code:

def makelist(f): #turn a document into a list
fin = open(f)
results = []
for line in fin:
   line = line.replace('', '')
line = line.strip().split()
for word in line:
results.append(word)
return results




What's you data look like?  Just straight text?




def markov(f, preflen=2):#f is the file to analyze, preflen is 
prefix length

convert_file = makelist(f)
mapdict = {}#dict where the prefixes will map to suffixes
start = 0
end = preflen #start/end set the slice size
for words in convert_file:
prefix = tuple(convert_file[start:end]) #tuple as mapdict key
suffix = convert_file[start + 2 : end + 1]  #word as suffix to key
mapdict[prefix] = mapdict.get(prefix, []) + suffix #append suffixes
start += 1
end += 1
return mapdict




What is convert_file??





def randsent(f, amt=10): #prints a random sentence
   analyze = markov(f)
for i in range(amt):
rkey = random.choice(analyze.keys())
print rkey, analyze[rkey],


The book gave a hint  saying to make the prefixes in the dict using:

def shift(prefix, word):
return prefix[1:] + (word, )


That's not a very helpful hint.

It works if you call it with a tuple and a word --- it shifts off the 
front of the tuple ... so :


shift(('foo','bar') word)
becomes   ('bar', 'word')

Whoopty doo --- I'm not sure what that accomplishes!!

Unless the author means pass a list and a randomly pick a word from the 
list in which case the return statement could be


random.choice(prefix) + (word, )

* shrug *

But -- that's not very Markov ... you'd want a weighted choice of words 
... depending on how you define your Markov chain -- say a Markov chain 
based on part-of-speech or probability of occurrence from a given word-set.


Can you give some more detail??





However I can't seem to wrap my head around incorporating that into the 
code above, if you know a method or could point me in the right 
direction (or think that I don't need to use it) please let me know.


Thanks for all your help,

Dave


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


Re: Python is slow

2008-05-22 Thread Andrew Lee

cm_gui wrote:

Python is slow.Almost all of the web applications written in
Python are slow.   Zope/Plone is slow, sloow, so very slooow.  Even
Google Apps is not faster.   Neither is Youtube.
Facebook and Wikipedia (Mediawiki), written in PHP, are so much faster
than Python.
Okay, they probably use caching or some code compilation -- but Google
Apps and those Zope sites probably also use caching.

I've yet to see a web application written in Python which is really
fast.


So --- don't use Google

* shrug *


Personally I find PHP to be a hideous language -- it has only one use. 
It is Perl for people who can be bothered to learn a general purpose 
programming language and just write web applications.


Python is a general purpose programming language ... it does web nicely 
(Django and pylons!), it supports real OOP and OOD, it's extensible, it 
has a smart set of libraries, intelligent and coherent interfaces, it 
sails across platforms, it has at least one great ORM (SQLAlchemy) and I 
keep discovering new cool things about the language as a recent convert 
that make me wonder why I would ever write another large application in 
Perl or Java.



I find Zope to be a mess and it gives me a headache ... but it Zope is 
an *application framework*, not a *programming language*.  So, I think 
you are barking up the wrong straw man (if I may mix metaphors).


But that's just my $0.02.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why is math.pi slightly wrong?

2008-05-22 Thread Andrew Lee

Mensanator wrote:

On May 22, 11:32 am, Dutton, Sam [EMAIL PROTECTED] wrote:

I've noticed that the value of math.pi -- just entering it at the interactive 
prompt -- is returned as 3.1415926535897931, whereas (as every pi-obsessive 
knows) the value is 3.1415926535897932... (Note the 2 at the end.)

Is this a precision issue, or from the underlying C, or something else? How is 
math.pi calculated?


If you actually need that many digits, use a different library.


import gmpy



print gmpy.pi(64) # 64 bit precision

3.14159265358979323846

print gmpy.pi(128) # 128 bit precision

3.141592653589793238462643383279502884197

print gmpy.pi(16384) # 16384 bit precision

3.14159265358979323846264338327950288419716939937510582097494459
23


Heh!


I wonder who needs that many digits?


Certainly not number theorists (they need a LOT more). Certainly not 
physicists -- they need about 30 digits to be within 1% of any 
measurement from molecules to galaxies.  Certainly not engineers, they 
need half the digits that physicists need.  Cryptographers are making a 
dire mistake if they are using PI in any computations (possible 
exception for elliptic curves -- see number theorists, above) ... so -- 
other than PI-philes, who needs PI to thousands of digits?

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


Re: Why is math.pi slightly wrong?

2008-05-22 Thread Andrew Lee

Dan Upton wrote:

On Thu, May 22, 2008 at 2:53 PM, Mensanator [EMAIL PROTECTED] wrote:

On May 22, 11:32 am, Dutton, Sam [EMAIL PROTECTED] wrote:

I've noticed that the value of math.pi -- just entering it at the interactive 
prompt -- is returned as 3.1415926535897931, whereas (as every pi-obsessive 
knows) the value is 3.1415926535897932... (Note the 2 at the end.)

Is this a precision issue, or from the underlying C, or something else? How is 
math.pi calculated?

If you actually need that many digits, use a different library.


import gmpy
print gmpy.pi(64) # 64 bit precision

3.14159265358979323846

print gmpy.pi(128) # 128 bit precision

3.141592653589793238462643383279502884197

print gmpy.pi(16384) # 16384 bit precision

3.14159265358979323846264338327950288419716939937510582097494459


...



Who wants to verify that that's correct to that many digits? ;)



Ramanujan?

http://numbers.computation.free.fr/Constants/Pi/piramanujan.html

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


Re: MVC

2008-05-22 Thread Andrew Lee

George Maggessy wrote:

Hi Gurus,

I'm a Java developer and I'm trying to shift my mindset to start
programming python. So, my first exercise is to build a website.
However I'm always falling back into MVC pattern. I know it's a
standard, but the implementation language affects the use of design
patter. So, here goes my question. Is that OK if I follow this? Should
I create DAOs, View Objects, Controllers and etc? Is there any sort of
best practice / standard to Python?

Cheers,
George



Look at Pylons.
--
http://mail.python.org/mailman/listinfo/python-list


Re: MatplotLib errors

2008-04-29 Thread Andrew Lee

Thomas Philips wrote:

I have just started using MatPlotLib, and use it to generate graphs
from Python simulations. It often happens that the graph is generated
and a Visual C++ Runtime Library error then pops up: Runtime Error!
Program C:\Pythin25\Pythonw.exe   This application has requested the
Runtime to terminate in an unusual way. Please contact the
application's support team for more information.

I'm running Python 2.5.2 under Windows XP. Any thoughts on what what
may be causing the problem?

Thanks in advance

Thomas Philips


There is a matplotlib-sig mailing list:

[EMAIL PROTECTED]


They would have MUCH more info on your problem that can be offered in a 
general python group.


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


Re: help needed with classes/inheritance

2008-04-23 Thread Andrew Lee

barbaros wrote:

Hello everybody,

I am building a code for surface meshes (triangulations for instance).
I need to implement Body objects (bodies can be points, segments,
triangles and so on), then a Mesh will be a collection of bodies,
together with their neighbourhood relations.
I also need OrientedBody objects, which consist in a body
together with a plus or minus sign to describe its orientation.
So, basically an OrientedBody is just a Body with an
integer label stuck on it.

I implemented it in a very crude manner:
--
class Body:
  [...]
class OrientedBody:
  def __init__ (self,b,orient=1):
# b is an already existing body
assert isinstance(b,Body)
self.base = b
self.orientation = orient
---


class Body(object) :
...

class OrientedBody (Body):
   def __init__(self, orientation = 1) :
Body.__init__(self)
self.orientation = orientation



as noted


But, also.

as a rule of thumb .. if you are using isinstance in a class to 
determine what class a parameter is ... you have broken the OO contract. 
 Remember, every class ought to have a well defined internal state and 
a well defined interface to its state.


If I write --

class foo (object):
   def __init__ :
  pass

   def some_func (self, val) :
  if isinstance (val, bar) :


Then I am either doing something very wrong or very clever (either can 
get me in trouble)


In Python it is preferred that I write two functions some_func_a and 
some_func_b


e.g.

def some_func_a (self, val = None, class = bar) :
assert(isinstance (class, bar), True)


def some_func_b (self, val = None, class = baz) :
assert (isinstance (class, baz), True)

C++ and Java try to enforce the OO contract by making data and methods 
private, protected or public.  Which helps -- but leads to some 
confusion (what is protected inheritance in C++)  Python exposes all 
of its classes internals to everyone -- but that doesn't mean you should 
touch them!!


As Larry Wall once wrote, There is a difference between, 'do not enter 
my living room because I asked you not to' and 'do not enter my living 
room because I have a shotgun'


Python adopts the 'do not touch my private parts because I asked you not 
to' idiom.  (In C++, only friends can touch your privates ... ;-)


So -- be mindful that checking the class of well defined parameters at 
anytime is breaking the contract -- you may need to do it -- but it is 
more likely that you aren't adhering to good OOD.


Does that make any sense?

Seriously -- I have not had any coffee yet and I am still new at Python.


-- Andrew






My question is: can it be done using inheritance ?
I recall that I need three distinct objects:
the basic (non-oriented) body, the same body with positive
orientation and the same body with negative orientation.

Thank you. Cristian Barbarosie
http://cmaf.fc.ul.pt/~barbaros

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


Re: Checking if a text file is blank

2008-04-20 Thread Andrew Lee
[EMAIL PROTECTED] wrote:
 Greetings!
 
 I've just started learning python, so this is probably one of those
 obvious questions newbies ask.
 
 Is there any way in python to check if a text file is blank?
 
 What I've tried to do so far is:
 
 f = file(friends.txt, w)
   if f.read() is True:
   do stuff
   else:
   do other stuff
   f.close()
 
 What I *mean* to do in the second line is to check if the text file is
 not-blank. But apparently that's not the way to do it.
 
 Could someone set me straight please?


Along with the other posts ... consider using the lstat command to get 
information about the file.


import os
print os.lstat(friends.txt)[6]


gives the size in bytes of friends.txt or throws an OSError if 
friends.txt does not exist.

lstat is portable, it defaults to stat on Windows.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking if a text file is blank

2008-04-20 Thread Andrew Lee
David wrote:
  import os
  print os.lstat(friends.txt)[6]

 
 I prefer os.lstat(friends.txt).st_size

MUCH easier to remember

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


Announce : TimeDuration 0.1a released

2008-04-18 Thread Andrew Lee
This is a pure python module for parsing time interval strings,
normalizing, comparing and ordering time intervals.

This module is still in alpha phase.

It handles string like :

1 hour, 15 minutes and 23.2 seconds
01:15:23.2
1h 15min 23.2sec

Output from *nix uptime or time commands :

15 days, 23:04
0m2.496s

It handles comparisons and sorting of TimeDuration objects.

It normalizes time interval strings, e.g. :

5 d, 27 h, 75 m 120 s

will normalize to

6 D 04:17:0.00

TODO:

* Catch garbled input strings that might confuse the parser.
* Create an iterable class to handle slices, min, max, sums, average,
mean and stddev
* More testing



Homepage:

http://statz.com/libs-TimeDuration/


Author :

Andrew Lee (fiacre.patrick  - at - gmail.com)
-- 
http://mail.python.org/mailman/listinfo/python-announce-list

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


Re: Can't do a multiline assignment!

2008-04-17 Thread Andrew Lee
[EMAIL PROTECTED] wrote:
 Yuck!  No way!!  If you *want* to make your code that hard to read, I'm
 sure you can find lots of ways to do so, even in Python, but don't
 expect Python to change to help you toward such a dubious goal.

 
 Well, my actual code doesn't look like that. Trust me, I like clean
 code.
 
 Seriously, examine your motivations for wanting such a syntax.   Does it
 make the code more readable?  (Absolutely not.)  Does it make it more
 maintainable.  (Certainly not -- consider it you needed to change
 CONSTANT2 to a different value some time in the future.)
 

Use a dictionary?

 Yes, it makes it more readable. And yes, it does make it (a lot) more
 maintainable. Mainly because I don't have those four variables, I have
 about thirty. And I think I won't need to one or two of them, but
 maybe all of them at once.
-- 
http://mail.python.org/mailman/listinfo/python-list