Re: Isn't there a better way?

2006-07-23 Thread Steve Holden
Lawrence D'Oliveiro wrote:
> In message <[EMAIL PROTECTED]>, Bruno Desthuilliers
> wrote:
> 
> 
>>Lawrence D'Oliveiro a écrit :
>>
>>
>>>If you're calling a number of different routines in
>>>the Processor class, all accessing the same data, then it makes perfect
>>>sense to only pass it once.
>>
>>Actually they are not "passed".
> 
> 
> I think I'm going to plonk you.

And the rest of comp.lang.python has to know about this because ... ?

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb   http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

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

Re: How to generate geometric random numbers?

2006-07-23 Thread Robert Kern
Paul Rubin wrote:
> Robert Kern <[EMAIL PROTECTED]> writes:
>>G = int(ceil(log(U) / log(1.0 - p)))
> 
> I usually owuld write that as int(ceil(log(U, 1.0 - p))).

Knock yourself out. I was cribbing from my C implementation in numpy.

-- 
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: easy questions from python newbie

2006-07-23 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, walterbyrd
wrote:

> This is the first real python program I have ever worked on. What I
> want to do is:
> 1) count identical records in a cvs file
> 2) create a new file with quantities instead duplicate records
> 3) open the new file in ms-excel
> 
> For example, I will start with a file like:
> 
> 1001
> 1012
> 1008
> 1012
> 1001
> 1001
> 
> and finish with a file like:
> 
> 1001,3
> 1008,1
> 1012,2
> 
> What I need to know:
> 1) is there a function in python that will sort a file into another
> file. Something like:
> sort file1.txt > file2.txt from the DOS command line. I know there is
> also a similar "sort" funtion in Unix.

Lists have a sort method.  No need to do this with temporary files.  Just
read in the first file into a list and sort it.

> 3) I will probably be working with 50 items, or less, would it be best
> for me to do this with a multi-diminsional array? For example: sort the
> file, read a rec into the array, if the next rec is the same then incr
> the count, otherwise add a new rec with a count of 1. Then write the
> array to a file?

I would read the file into a list of list, that's what comes closest to a
multidimensional array, via the `csv` module.  Sort that (outer) list and
then use `itertools.groupby()` to group the identical lists.  You can
write the rows with the `csv` module again.  Short example:

import csv
from itertools import groupby

in_file = open('test.csv', 'rb')
data = list(csv.reader(in_file))
in_file.close()

data.sort()

out_file = open('test2.csv', 'wb')
writer = csv.writer(out_file)
for row, identical_rows in groupby(data):
row.append(len(list(identical_rows)))
writer.writerow(row)
out_file.close()

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: find_longest_match in SequenceMatcher

2006-07-23 Thread John Machin

koara wrote:
> Hello, it might be too late or too hot, but i cannot work out this
> behaviour of find_longest_match() in difflib.SequenceMatcher:
>
> string1:
[snipped 500-byte string]
>
> string2:
>
[snipped 500-byte string]
>
> find_longest_match(0,500,0,500)=(24,43,10)="version01t"
>
> What? O_o Clearly there is a longer match, right at the beginning!
> And then, after removal of the last character from each string (i found
> the limit of 500 by trial and error -- and it looks suspiciously
> rounded):

What limit? (a) My results [see below] (b) my inspection of the Python
version 2.4 source for the difflib module (c) what I know of the author
-- all tend to indicate that there is no hidden undocumented length
limit.

>
> find_longest_match(0,499,0,499)=(0,0,32)="releasenotesforwildmagicversion0"
>
>
> Is this the expected behaviour? What's going on?

My code: (koara.py)
8<---
strg1 =
r"""releasenotesforwildmagicversion01thiscdromcontainstheinitialreleaseofthesourcecodethataccompaniesthebook"3dgameenginedesign:apracticalapproachtorealtimecomputergraphics"thereareanumberofknownissuesaboutthecodeastheseissuesareaddressedtheupdatedcodewillbeavailableatthewebsitehttp://wwwmagicsoftwarecom/[EMAIL
 PROTECTED]"""
strg2 =
r"""releasenotesforwildmagicversion02updatefromversion01toversion02ifyourcopyofthebookhasversion01andifyoudownloadedversion02fromthewebsitethenapplythefollowingdirectionsforinstallingtheupdateforalinuxinstallationseethesectionattheendofthisdocumentupdatedirectionsassumingthatthetopleveldirectoryiscalledmagicreplacebyyourtoplevelnameyoushouldhavetheversion01contentsinthislocation1deletethecontentsofmagic\include2deletethesubdirectorymagic\source\mgcapplication3deletetheobsoletefiles:amagic\source\mgc"""
import sys
print sys.version
from difflib import SequenceMatcher as SM
smo = SM(None, strg1, strg2)
print len(strg1), len(strg2)
print smo.find_longest_match(0, 500, 0, 500)
print smo.find_longest_match(0, 499, 0, 499)
print smo.find_longest_match(0, 100, 0, 100)
print smo.find_longest_match(1, 101, 1, 101)
print smo.find_longest_match(2, 102, 2, 102)
8<---

The results on 4 python versions:

C:\junk>c:\python24\python koara.py
2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
500 500
(24, 43, 10)
(24, 43, 10)
(24, 43, 10)
(24, 43, 10)
(24, 43, 10)

C:\junk>c:\python23\python koara.py
2.3.5 (#62, Feb  8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)]
500 500
(24, 43, 10)
(24, 43, 10)
(24, 43, 10)
(24, 43, 10)
(24, 43, 10)

C:\junk>c:\python22\python koara.py
2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)]
500 500
(0, 0, 32)
(0, 0, 32)
(0, 0, 32)
(1, 1, 31)
(2, 2, 30)

C:\junk>c:\python21\python koara.py
2.1.3 (#35, Apr  8 2002, 17:47:50) [MSC 32 bit (Intel)]
500 500
(0, 0, 32)
(0, 0, 32)
(0, 0, 32)
(1, 1, 31)
(2, 2, 30)

Looks to me like the problem has nothing at all to do with the length
of the searched strings, but a bug appeared in 2.3.  What version(s)
were you using? Can you reproduce your results (500 & 499 giving
different answers) with the same version?

Anyway, as they say in the classics, "Take a number; the timbot will be
with you shortly."

Cheers,
John

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


Re: How to generate geometric random numbers?

2006-07-23 Thread Paul Rubin
Robert Kern <[EMAIL PROTECTED]> writes:
>G = int(ceil(log(U) / log(1.0 - p)))

I usually owuld write that as int(ceil(log(U, 1.0 - p))).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to generate geometric random numbers?

2006-07-23 Thread Robert Kern
Paul Rubin wrote:
> Paul Rubin  writes:
>>n = log(c, 1-p) - 1
> 
> I meantn = log(c/p, 1-p) - 1
> sorry.

import random
from math import ceil, log

def geometric(p):
   """ Geometric distribution per Devroye, Luc. _Non-Uniform Random Variate
   Generation_, 1986, p 500. http://cg.scs.carleton.ca/~luc/rnbookindex.html
   """

   # p should be in (0.0, 1.0].
   if p <= 0.0 or p > 1.0:
 raise ValueError("p must be in the interval (0.0, 1.0]")
   elif p == 1.0:
 # If p is exactly 1.0, then the only possible generated value is 1.
 # Recognizing this case early means that we can avoid a log(0.0) later.
 # The exact floating point comparison should be fine. log(eps) works just
 # dandy.
 return 1

   # random() returns a number in [0, 1). The log() function does not
   # like 0.
   U = 1.0 - random.random()

   # Find the corresponding geometric variate by inverting the uniform variate.
   G = int(ceil(log(U) / log(1.0 - p)))
   return G

-- 
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: How to generate geometric random numbers?

2006-07-23 Thread Paul Rubin
Paul Rubin  writes:
>n = log(c, 1-p) - 1

I meantn = log(c/p, 1-p) - 1
sorry.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to generate geometric random numbers?

2006-07-23 Thread Paul Rubin
[EMAIL PROTECTED] writes:
> But I am still surprised because the default Random package in Python
> can generate so few discrete random distritbuions, while it can
> generate quite a few continuous distribution, including some not very
> common one.

It looks pretty simple to transform the uniform distribution to the
geometric distribution.  The formula for its cdf is pretty simple:

  cdf(p,n) = (1-p)**(n-1)*p

For fixed p, if the cdf is c, we get (unless I made an error),

   n = log(c, 1-p) - 1

So choose a uniform point c in the unit interval, run it through that
formula, and round up to the nearest integer.

See http://en.wikipedia.org/wiki/Geometric_distribution
for more about the distribution.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: easy questions from python newbie

2006-07-23 Thread James Stroud
John Machin wrote:
> James Stroud wrote:
> 
>>walterbyrd wrote:
>>
>>>This is the first real python program I have ever worked on. What I
>>>want to do is:
>>>1) count identical records in a cvs file
>>>2) create a new file with quantities instead duplicate records
>>>3) open the new file in ms-excel
>>>
>>>For example, I will start with a file like:
>>>
>>>1001
>>>1012
>>>1008
>>>1012
>>>1001
>>>1001
>>>
>>>and finish with a file like:
>>>
>>>1001,3
>>>1008,1
>>>1012,2
>>>
>>>What I need to know:
>>>1) is there a function in python that will sort a file into another
>>>file. Something like:
>>>sort file1.txt > file2.txt from the DOS command line. I know there is
>>>also a similar "sort" funtion in Unix.
>>
>>import os
>>os.system('sort file1.txt > file2.txt')
>>
>>
>>>2) is there a function in python that will correctly load a csv file
>>>into an excel spreadsheet,
>>>and open that spreadsheet.
>>
>>What's with the excel files? You must be in industry. Excel opens csv
>>files, no problem. In Mac OS X, you can do this:
>>
>>   os.system('open -a /Applications/Excel %s' % 'my_file.csv')
>>
>>It probably requires a nauseating journey through a bunch of hoops to do
>>the equivalent in window$. But you haven't specified your operating
>>system, so I optimistically assume a best-case (OS X) and not a
>>worst-case (window$). If worst-case, I'm truly sorry for your misfortune.
> 
> 
> Hey d00d, what's with the attit00d? All those $ signs? Do you get Excel
> for free on Mac OS X or does your department pay for it?  Us 'doze
> dummies w/o a big endowment like UCLA wouldn't/couldn't afford that.
> Out here in the boonies we have to download the free stuff.


I use FC4 and open office. I had to fire up a window$ machine to test 
the CSV thing, d00d.

> C:\junk>python
> Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
> on win32
> Type "help", "copyright", "credits" or "license" for more information.
> 
import os
os.system('"C:\Program Files\OpenOffice.org 2.0\program\scalc" foo.csv')
> 
> 0
> 
> 
> Look, d00d, no hoops.

Glad for you.

>>>3) I will probably be working with 50 items, or less, would it be best
>>>for me to do this with a
>>>multi-diminsional array? For example: sort the file, read a rec into
>>>the array, if the next rec is the same then incr the count, otherwise
>>>add a new rec with a count of 1. Then write the array to a file?
>>>
>>
>>Ah, a real question. Use a dict:
>>
>>if adict.has_key(some_key):
> 
> 
> Hey, d00d, ask the department sysadmin to update your Python for you,
> then you'll be able to use this:
> 
> if some_key in adict:
> 
> 
>>   adict[some_key] += 1
>>else:
>>   adict[some_key] = 1
>>

Last I checked, both worked.

James


-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

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


Re: easy questions from python newbie

2006-07-23 Thread John Machin

James Stroud wrote:
> walterbyrd wrote:
> > This is the first real python program I have ever worked on. What I
> > want to do is:
> > 1) count identical records in a cvs file
> > 2) create a new file with quantities instead duplicate records
> > 3) open the new file in ms-excel
> >
> > For example, I will start with a file like:
> >
> > 1001
> > 1012
> > 1008
> > 1012
> > 1001
> > 1001
> >
> > and finish with a file like:
> >
> > 1001,3
> > 1008,1
> > 1012,2
> >
> > What I need to know:
> > 1) is there a function in python that will sort a file into another
> > file. Something like:
> > sort file1.txt > file2.txt from the DOS command line. I know there is
> > also a similar "sort" funtion in Unix.
>
> import os
> os.system('sort file1.txt > file2.txt')
>
> > 2) is there a function in python that will correctly load a csv file
> > into an excel spreadsheet,
> > and open that spreadsheet.
>
> What's with the excel files? You must be in industry. Excel opens csv
> files, no problem. In Mac OS X, you can do this:
>
>os.system('open -a /Applications/Excel %s' % 'my_file.csv')
>
> It probably requires a nauseating journey through a bunch of hoops to do
> the equivalent in window$. But you haven't specified your operating
> system, so I optimistically assume a best-case (OS X) and not a
> worst-case (window$). If worst-case, I'm truly sorry for your misfortune.

Hey d00d, what's with the attit00d? All those $ signs? Do you get Excel
for free on Mac OS X or does your department pay for it?  Us 'doze
dummies w/o a big endowment like UCLA wouldn't/couldn't afford that.
Out here in the boonies we have to download the free stuff.

C:\junk>python
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system('"C:\Program Files\OpenOffice.org 2.0\program\scalc" foo.csv')
0
>>>

Look, d00d, no hoops.

>
> > 3) I will probably be working with 50 items, or less, would it be best
> > for me to do this with a
> > multi-diminsional array? For example: sort the file, read a rec into
> > the array, if the next rec is the same then incr the count, otherwise
> > add a new rec with a count of 1. Then write the array to a file?
> >
>
> Ah, a real question. Use a dict:
>
> if adict.has_key(some_key):

Hey, d00d, ask the department sysadmin to update your Python for you,
then you'll be able to use this:

if some_key in adict:

>adict[some_key] += 1
> else:
>adict[some_key] = 1
>

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


Re: How to generate geometric random numbers?

2006-07-23 Thread MyInfoStation
Robert Kern wrote:
> Gerhard Fiedler wrote:
> > On 2006-07-23 17:12:20, [EMAIL PROTECTED] wrote:
> >
> >> I am a newbie to Python and would like to genereate some numbers
> >> according to geometric distribution. However, the Python Random package
> >> seems do not have implemented functionality. I am wondering is there
> >> exist any other libraries that can do this job?
> >
> > The usual way is to generate standard random numbers (linear distribution)
> > and then apply whatever transformation you need to generate the desired
> > distribution.
>
> That only works if there is such a transformation.
>
> The geometric distribution and many others have been implemented in numpy:
>
>http://www.scipy.org/NumPy
>
> In [1]: from numpy import random
>
> In [2]: random.geometric(0.5, size=100)
> Out[2]:
> array([1, 5, 2, 3, 1, 1, 2, 3, 1, 1, 2, 1, 1, 1, 2, 2, 3, 3, 1, 1, 1, 1, 1,
> 2, 2, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 3, 1,
> 4, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 1, 1, 1, 3, 1, 1, 2, 6, 1, 1, 3, 2,
> 1, 1, 2, 1, 1, 7, 2, 1, 1, 2, 1, 1, 2, 4, 1, 2, 1, 4, 2, 1, 1, 2, 1,
> 4, 2, 1, 1, 3, 1, 3, 1])
>
> --
> 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

Thanks a lot. I will try it out.

But I am still surprised because the default Random package in Python
can generate so few discrete random distritbuions, while it can
generate quite a few continuous distribution, including some not very
common one.

Da

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


Re: Understanding Unicode & encodings

2006-07-23 Thread John Machin

Jim wrote:
> John Machin wrote:
> > Jim wrote:
> > > No, I'll bet that you'd like to run something like
> > >   self.dcCursor.execute("INSERT INTO track (name, nbr, idartist,
> > > idalbum,path) VALUES (%(track)s, %(nbr)s,
> > > %(idartist)s,%(idalbum)s,'%(path)s')",
> > > {'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path})
> > > (only without my typos).  That's an improvment for a number of reasons,
> > > one of which is that the system will quote for you, for instance in
> > > idartist="John's Beer" changing the single quote to two single quotes
> > > to suit SQL.
> > I see no improvement here.
> >
> > The OP's code is effectively::
> >
> >   sql = "INSERT INTO track (name, ..., path) VALUES ('%s', ..., '%s')"
> >   value_tuple = (track, , path)
> >   self.dcCursor.execute(sql % value_tuple)
> >
> > Your suggested replacement is effectively:
> >
> >   sql = "INSERT INTO track (name, ...,path) VALUES (%(track)s,
> > ...,'%(path)s')"
> >   str_fmt_dict = {'track':track, ...,'path':path}
> >   self.dcCursor.execute(sql, str_fmt_dict)
> >
> > Well, that won't run at all. Let's correct the presumed typo:
> >
> >self.dcCursor.execute(sql % str_fmt_dict)
> I'm sorry, that wasn't a typo.  I was using what the dBapi 2.0 document
> calls 'pyformat' (see the text under "paramstyle" in that document).

Oh yeah. My mistake. Noticed 'pyformat' years ago, thought "What a good
idea", found out that ODBC supports only qmark, SQLite supports only
qmark, working on database conversions where the SQL was
programatically generated anyway: forgot all about it.

>
> > Now, the only practical difference is that you have REMOVED the OP's
> > explicit quoting of the first column value. Changing the string
> > formatting from the %s style to the %(column_name) style achieves
> > nothing useful. You are presenting the "system" with a constant SQL
> > string -- it is not going to get any chance to fiddle with the quoting.
> > However the verbosity index has gone off the scale: each column name is
> > mentioned 4 times (previously 1).

> Gee, I like the dictionary; it has a lot of advantages.

Like tersemess? Like wide availibility?

>
> Anyway, the point is that to get quote escaping right, to prevent SQL
> injection, etc., paramstyles are better than direct string %-ing.

And possible performance gains (the engine may avoid parsing the SQL
each time).

*NOW* we're on the same page of the same hymnbook, Brother Jim :-)

Cheers,
John

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


Re: Re-evaluating a string?

2006-07-23 Thread Tim Chase
> Thanks Tim, and John for your quick responses!

This is one of the better lists for getting quick (and usually 
helpful) responses.

> Tim, I tested your function and it works! Though I don't completely
> understand how. Could you possibly explain this?

 >>> def makeString(a):
... return ''.join([type(x) != types.IntType and
... str(x) or chr(x) for x in a])
...

The "boolean_expression and value1 or value2" is a common python 
idiom for something akin to C/C++/Java's ternary 
"boolean_expression? value1: value2" expression.  There are some 
gotchas (and workarounds for those gotchas) if value1 can be a 
"false" value (an empty string, a zero or empty list are good 
examples of this).  It pretty much boils down to joining all the 
elements of the list that is composed from "for every item in the 
list 'a', if it's not an int, just return the str() of it; and if 
it is an int, return the chr() of it".  It then smashes them all 
together with the join() and returns the resulting string.

> John, I test your "MEDUIM_WAY"and it works as well. How is it that
> putting the string together this way translates into a hex value to be
> transmitted to the serial port? When I manually tried to put a string
> together I couldn't get this to happen. I was trying:
> 
> controlString = '!SC' + '\\' + ch.__str__() + '\\' + rate.__str__()

The string-formatting "%c" expects a byte and prints the ascii 
character relating to the byte.  Also a good way to do things. 
Come to think of it, John had a lot of good ideas in his post. 
In your above code, the ch.__str__() creates a string 
representation of the number I presume is stored in ch.  The 
string representation of a number is simply a string containing 
that number:

 >>> x = 42
 >>> x.__str__()
'42'

Not very exciting.  And generally stylistically better to use 
str() in favor of the __str__() method call.

> also I noticed you added another line to the code which appears to
> split the low and high bytes for me? If so thanks! Could you also offer
> an explanation on how this works.

the divmod(x,y) function divides x by y, and returns a tuple. 
The first value of the tuple is the integer result of the 
division, and the second value of the tuple is the remainder. 
It's a one-step way of doing

hi,lo = divmod(x,y)

works like a condensing of

hi = x / y
lo = x % y

> I tried a google search and couldn't
> get a decent explanation. 

Googling the python docs for "divmod" should do the trick.

http://www.google.com/search?q=site%3Adocs.python.org+divmod

returns several helpful hits wherein you can learn more than any 
sane person should care to know.  But the above primer should be 
enough to get you on your way.

I think, in the case of your example string, using John's 
suggestion of the %c formatting is the cleanest approach.  As 
such, I'd rework the move() function I suggested to simply be 
something like

def move(rate,lo,hi,chan=1):
return "!SC%c%c%c%c\r" % (chan, rate, lo, hi)

where you possibly even just pass in the "position" parameter, 
and let the function do the splitting with something like

def move(rate,position,chan=1)
hi,lo = divmod(position & 0x, 256)
return "!SC%c%c%c%c\r" % (chan, rate, lo, hi)

or optionally use the struct module to unpack them.

Just a few more thoughts,

-tkc




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


Re: Re-evaluating a string?

2006-07-23 Thread John Machin
bugnthecode wrote:
> Thanks Tim, and John for your quick responses!
>
> Tim, I tested your function and it works!
> Though I don't completely
> understand how. Could you possibly explain this?
>
> John, I test your "MEDUIM_WAY"and it works as well.

Now try the struct module approach.

Are you 100% certain that the servo position can't be negative? What
would you do if it could be negative?

> How is it that
> putting the string together this way translates into a hex value to be
> transmitted to the serial port?

Like I tried to tell you before, there is no such thing as transmitting
a hex value. You are transmitting 8-bit bytes, one bit at a time. The
number 6 might be transmitted as 0110 or 0110 (the latter, IIRC
-- it's been a while). The number 6 can be represented in a computer
program as 6 or 0x6. As your write() function expects a string, you
need to represent it in your program as a string e.g. '\x06' or chr(6)
or "%c" % 6.

Read about chr() in the  manual:
http://docs.python.org/lib/built-in-funcs.html

Read about string formatting in the manual:
http://docs.python.org/lib/typesseq-strings.html

> When I manually tried to put a string
> together I couldn't get this to happen. I was trying:
>
> controlString = '!SC' + '\\' + ch.__str__() + '\\' + rate.__str__()
> ...etc

Ugh. FWIW, use str(foo) instead of foo.__str__()

>
> also I noticed you added another line to the code which appears to
> split the low and high bytes for me? If so thanks! Could you also offer
> an explanation on how this works. I tried a google search and couldn't
> get a decent explanation.

I presume you mean
vhi, vlo = divmod(value, 256)
What did you search for?
If you are on Windows:
do(click on Start, hover on All Programs, hover on Python 2.4,
click on Python Manuals, type in divmod, press Enter key twice)
else:
deduce that divmod can only be a built-in function (I didn't
declare it, did I?)
read web docs on built-in functions
(http://docs.python.org/lib/built-in-funcs.html)

> I implemented this a little differently as
> you can see in my Position class. Could you possibly offer any info on
> the benefits/drawbacks of the different methods?

No great difference. Better than both is to use the struct module.

>
> Thanks again to both of you for your quick responses and help.
>
> Will
>
> import serial
> import types
>
> class Position:

It is extreme overkill, IMHO, to use a class for this trivium.


> def __init__(self, num):
> """Takes the position as an int, and splits the low and high
> bytes
>
> into instance members."""
> self.lowByte = num & 0x00FF
> self.highByte = (num & 0xFF00) >> 8

If you are sure that 0 <= num <= 0x, then you don't need the 0xFF00
mask. If you are unsure, then don't just silently transmit what may
well be rubbish; check, or use an assertion:
assert 0 <= num <= 0x

>
> def __str__(self):
> """Mainly for debugging purposes. Allows meaningful output when
> printed"""

FFS. Use the repr() built-in, like I suggested.

> return 'Low: ' + self.lowByte.__str__() + '\nHigh: ' +
> self.highByte.__str__()
>
> def makeString(a):
> """Takes in  a list, and intelligentlly smashes everything
> together.
>
> Outputs everything as a hex string.

No it doesn't.

> Posted by: Tim Chase on comp.lang.python"""
> return ''.join([type(x) != types.IntType and
> str(x) or chr(x) for x in a])
>
> def getVer(localpsc):
> """Gets the version from the PSC. Mainly just to verify a
> connection"""
>
> localpsc.write('!SCVER?\r')
> localpsc.read(8) #Discard the echo!
> s = localpsc.read(3)
> print s
>
> def moveServo(localpsc, ch, rate, position):
> """Takes in a serial object, the desired channel, the ramp rate,
> and
> the desired position of ther servo. Moves the servo to the desired
> postion."""
>
> #localpsc.write('!SC', ch, rate, position.low, position.high, '\r')
> #controlString = makeString(['!SC', ch, rate, position.lowByte,
> position.highByte, '\r'])
> #localpsc.write(controlString)
> #localpsc.flushInput() #discard the echo!
>
> """Following line from John Machin"""
> controlString = "!SC%c%c%c%c\r" % (ch, rate, position.lowByte,
> position.highByte)
> localpsc.write(controlString)
>
> psc = serial.Serial(1, 2400)
>
> mypos = Position(2500)
>
> moveServo(psc, 0, 5, mypos)
> psc.close()

import struct
DEBUG = True

def moveServo(localpsc, ch, rate, position):
"""Takes in a serial object, the desired channel, the ramp rate,
and
the desired position of the servo. Moves the servo to the
desired
position."""
assert 0 <= ch <= 255 # or tighter bounds from manuf's spec
assert 0 <= rate <= 255
assert 0 <= position <= 65535 # aka 0x
cmd = "!SC" + struct.pack("http://mail.python.org/mailman/listinfo/python-list


Re: easy questions from python newbie

2006-07-23 Thread James Stroud
walterbyrd wrote:
> This is the first real python program I have ever worked on. What I
> want to do is:
> 1) count identical records in a cvs file
> 2) create a new file with quantities instead duplicate records
> 3) open the new file in ms-excel
> 
> For example, I will start with a file like:
> 
> 1001
> 1012
> 1008
> 1012
> 1001
> 1001
> 
> and finish with a file like:
> 
> 1001,3
> 1008,1
> 1012,2
> 
> What I need to know:
> 1) is there a function in python that will sort a file into another
> file. Something like:
> sort file1.txt > file2.txt from the DOS command line. I know there is
> also a similar "sort" funtion in Unix.

import os
os.system('sort file1.txt > file2.txt')

> 2) is there a function in python that will correctly load a csv file
> into an excel spreadsheet,
> and open that spreadsheet.

What's with the excel files? You must be in industry. Excel opens csv 
files, no problem. In Mac OS X, you can do this:

   os.system('open -a /Applications/Excel %s' % 'my_file.csv')

It probably requires a nauseating journey through a bunch of hoops to do 
the equivalent in window$. But you haven't specified your operating 
system, so I optimistically assume a best-case (OS X) and not a 
worst-case (window$). If worst-case, I'm truly sorry for your misfortune.

> 3) I will probably be working with 50 items, or less, would it be best
> for me to do this with a
> multi-diminsional array? For example: sort the file, read a rec into
> the array, if the next rec is the same then incr the count, otherwise
> add a new rec with a count of 1. Then write the array to a file?
> 

Ah, a real question. Use a dict:

if adict.has_key(some_key):
   adict[some_key] += 1
else:
   adict[some_key] = 1

James



-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

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


Re: Understanding Unicode & encodings

2006-07-23 Thread Jim
John Machin wrote:
> Jim wrote:
> > No, I'll bet that you'd like to run something like
> >   self.dcCursor.execute("INSERT INTO track (name, nbr, idartist,
> > idalbum,path) VALUES (%(track)s, %(nbr)s,
> > %(idartist)s,%(idalbum)s,'%(path)s')",
> > {'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path})
> > (only without my typos).  That's an improvment for a number of reasons,
> > one of which is that the system will quote for you, for instance in
> > idartist="John's Beer" changing the single quote to two single quotes
> > to suit SQL.
> I see no improvement here.
>
> The OP's code is effectively::
>
>   sql = "INSERT INTO track (name, ..., path) VALUES ('%s', ..., '%s')"
>   value_tuple = (track, , path)
>   self.dcCursor.execute(sql % value_tuple)
>
> Your suggested replacement is effectively:
>
>   sql = "INSERT INTO track (name, ...,path) VALUES (%(track)s,
> ...,'%(path)s')"
>   str_fmt_dict = {'track':track, ...,'path':path}
>   self.dcCursor.execute(sql, str_fmt_dict)
>
> Well, that won't run at all. Let's correct the presumed typo:
>
>self.dcCursor.execute(sql % str_fmt_dict)
I'm sorry, that wasn't a typo.  I was using what the dBapi 2.0 document
calls 'pyformat' (see the text under "paramstyle" in that document).

> Now, the only practical difference is that you have REMOVED the OP's
> explicit quoting of the first column value. Changing the string
> formatting from the %s style to the %(column_name) style achieves
> nothing useful. You are presenting the "system" with a constant SQL
> string -- it is not going to get any chance to fiddle with the quoting.
> However the verbosity index has gone off the scale: each column name is
> mentioned 4 times (previously 1).
Gee, I like the dictionary; it has a lot of advantages.

> I would suggest the standard default approach:
>
>   sql = "INSERT INTO track (name, ..., path) VALUES (?, ..., ?)"
>   value_tuple = (track, , path)
>   self.dcCursor.execute(sql, value_tuple)
>
> The benefits of doing this include that the DBAPI layer gets to
> determine the type of each incoming value and the type of the
> corresponding DB column, and makes the appropriate adjustments,
> including quoting each value properly, if quoting is necessary.
I'll note that footnote [2] of the dBapi format indicates some
preference for pyformat over the format above, called there 'qmark'.
But it all depends on what the OP is using to connect to the dB; their
database module may well force them to choose a paramstyle, AIUI.

Anyway, the point is that to get quote escaping right, to prevent SQL
injection, etc., paramstyles are better than direct string %-ing.

Jim

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


Re: Isn't there a better way?

2006-07-23 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Bruno Desthuilliers
wrote:

> Lawrence D'Oliveiro a écrit :
>
>> If you're calling a number of different routines in
>> the Processor class, all accessing the same data, then it makes perfect
>> sense to only pass it once.
> 
> Actually they are not "passed".

I think I'm going to plonk you.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: url encode latin-1 characters

2006-07-23 Thread John Machin

Lurker wrote:
> I want send latin-1 string to web server by url parameter
>
> urllib.quote return just symbol code with preceeding percent for every
> non-ascii character:
> #>ustr = 'Ü'
> #>urllib.quote(ustr)
> '%9A'
>

The latin1 encoding for U-with-umlaut is 0xDC.
The cp850 encoding for the same is 0x9A.
I deduce that you typed the above in a DOS command window on a Windows
box.
Fire up IDLE and try the same thing again.

> but this seems to be wrong because server response contains my
> parameter and it differ from original (for example 'Ü' became '[')

You told it (maybe) that you were sending in latin1, then gave it 0x9A
which in latin1 is a control character (SCI, single character
introducer) and it gave you back a '[' ... garbage in, garbage out,
perhaps?

How about showing us (a) a snippet of the code you actually executed
(b) the repr() of what was returned -- *NOT* a copy/paste from the
screen; as shown above, a picture of U with two dots above it is not
reliable information.

Cheers,
John

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


Re: Re-evaluating a string?

2006-07-23 Thread bugnthecode
Thanks Tim, and John for your quick responses!

Tim, I tested your function and it works! Though I don't completely
understand how. Could you possibly explain this?

John, I test your "MEDUIM_WAY"and it works as well. How is it that
putting the string together this way translates into a hex value to be
transmitted to the serial port? When I manually tried to put a string
together I couldn't get this to happen. I was trying:

controlString = '!SC' + '\\' + ch.__str__() + '\\' + rate.__str__()
...etc

also I noticed you added another line to the code which appears to
split the low and high bytes for me? If so thanks! Could you also offer
an explanation on how this works. I tried a google search and couldn't
get a decent explanation. I implemented this a little differently as
you can see in my Position class. Could you possibly offer any info on
the benefits/drawbacks of the different methods?

Thanks again to both of you for your quick responses and help.

Will

import serial
import types

class Position:
def __init__(self, num):
"""Takes the position as an int, and splits the low and high
bytes

into instance members."""
self.lowByte = num & 0x00FF
self.highByte = (num & 0xFF00) >> 8

def __str__(self):
"""Mainly for debugging purposes. Allows meaningful output when
printed"""
return 'Low: ' + self.lowByte.__str__() + '\nHigh: ' +
self.highByte.__str__()

def makeString(a):
"""Takes in  a list, and intelligentlly smashes everything
together.

Outputs everything as a hex string.
Posted by: Tim Chase on comp.lang.python"""
return ''.join([type(x) != types.IntType and
str(x) or chr(x) for x in a])

def getVer(localpsc):
"""Gets the version from the PSC. Mainly just to verify a
connection"""

localpsc.write('!SCVER?\r')
localpsc.read(8) #Discard the echo!
s = localpsc.read(3)
print s

def moveServo(localpsc, ch, rate, position):
"""Takes in a serial object, the desired channel, the ramp rate,
and
the desired position of ther servo. Moves the servo to the desired
postion."""

#localpsc.write('!SC', ch, rate, position.low, position.high, '\r')
#controlString = makeString(['!SC', ch, rate, position.lowByte,
position.highByte, '\r'])
#localpsc.write(controlString)
#localpsc.flushInput() #discard the echo!

"""Following line from John Machin"""
controlString = "!SC%c%c%c%c\r" % (ch, rate, position.lowByte,
position.highByte)
localpsc.write(controlString)

psc = serial.Serial(1, 2400)

mypos = Position(2500)

moveServo(psc, 0, 5, mypos)
psc.close()

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


Re: Track keyboard and mouse usage

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

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

HTH,
~Simon

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


Re: Which Pyton Book For Newbies?

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


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

HTH,
~Simon

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


easy questions from python newbie

2006-07-23 Thread walterbyrd
This is the first real python program I have ever worked on. What I
want to do is:
1) count identical records in a cvs file
2) create a new file with quantities instead duplicate records
3) open the new file in ms-excel

For example, I will start with a file like:

1001
1012
1008
1012
1001
1001

and finish with a file like:

1001,3
1008,1
1012,2

What I need to know:
1) is there a function in python that will sort a file into another
file. Something like:
sort file1.txt > file2.txt from the DOS command line. I know there is
also a similar "sort" funtion in Unix.
2) is there a function in python that will correctly load a csv file
into an excel spreadsheet,
and open that spreadsheet.
3) I will probably be working with 50 items, or less, would it be best
for me to do this with a
multi-diminsional array? For example: sort the file, read a rec into
the array, if the next rec is the same then incr the count, otherwise
add a new rec with a count of 1. Then write the array to a file?

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


Re: Re-evaluating a string?

2006-07-23 Thread John Machin
bugnthecode wrote:
> I'm writing a program to send data over the serial port. I'm using
> pyserial, and I'm on WindowsXP. When I use literals I can get the data
> accross how I want it for example:
>
>1 2  3  4 5  6
> serialport.write('!SC'+'\x01'+'\x05'+'\xFA'+'\x00'+'\r')
>
> 1=Get devices attention
> 2=Select channel on device
> 3=Rate for movement
> 4=Low byte of 16 bits
> 5=High bytes of 16 bits
> 6=Carriage return signaling command is over
>
> This command works as desired. Sends the first 3 ASCII characters, then
> some numbers in hex followed by a carriage return.
>
> My problem is that the "write()" function only takes a string, and I
> want to substitute variables for the hex literals.
>
> I know that I can use the "hex()" function and it will return a string
> with the appropriate hex value, and I could combine this with some
> other literals like "\\" to create my desired hex literal, but then I
> would need something to re-parse my string to change my ASCII text into
> the appropriate hex values.

No need. That would be like travelling from Brooklyn to the Bronx via
Walla Walla, WA. And "the appropriate hex values" exist only as ASCII
text, anyway.

The following code (a) is untested (b) assumes each of the 3 numbers
(channel, rate, value) is UNsigned.

channel = 1
rate = 5
value = 0xFA # or value = 250
if HARD_WAY:
vhi, vlo = divmod(value, 256)
command = "!SC" + chr(channel) + chr(rate) + chr(vlo) + chr(vhi) +
"\r"
elif MEDIUM_WAY:
vhi, vlo = divmod(value, 256)
command = "!SC%c%c%c%c\r" % (channel, rate, vlo, vhi)
else:
import struct
command = "!SC" + struct.pack("http://mail.python.org/mailman/listinfo/python-list


Re: Re-evaluating a string?

2006-07-23 Thread Tim Chase
> serialport.write('!SC'+'\x01'+'\x05'+'\xFA'+'\x00'+'\r')
[cut]
> My problem is that the "write()" function only takes a string, and I
> want to substitute variables for the hex literals.

Well, you can try something like

 >>> import types
 >>> def makeString(a):
... return ''.join([type(x) != types.IntType and
... str(x) or chr(x) for x in a])
...
 >>> data = ['!SC', 1, 5, 0xFA, 0, '\r']
 >>> makeString(data)
'!SC\x01\x05\xfa\x00\r'


Thus, you can mix and match your desired data in a list, and then 
let Python intelligently smash together the string you want, so 
you can later pass that to your write() call.

It does hiccup (read "throw an exception") if you have an empty 
string in your list.  It also falls down if you try and put in an 
integer constant that isn't in range(256).  My advice regarding 
these would be "don't do that". :)  Alternatively, you can 
concoct some cousin-function to chr() that takes any old garbage 
along with a default, and returns either the chr() of it, unless 
that throws an expception, in which case you just return 
something like '\x00' (whatever you specified as the default).

This allows you to use your favorite notation.  If you like hex 
notation, you can use it (as in the "0xFA" in the above data). 
If you prefer integers, you can toss them in the mix.

Alternatively, you can create a suite of API wrapper functions, 
such as


def move(rate, low, high, channel=1):
serialport.write(''.join([type(x) != types.IntType and
... str(x) or chr(x) for x in
... ['!SC', channel, rate, low, high, '\r']
... ]))

(that could be uncompacted a bit for readibility's sake...)

You could then just call move(5, 0xFA, 0) and the function does 
the heavy work for you.  Might also be more readable later for 
other folks coming to the project (if there are others).

Just a couple ideas you might want to try.

-tkc






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


MULTI-DIMENTIONAL CHESS DOWNLOAD FROM DEEP SPACE

2006-07-23 Thread switzerland qunatium computer
MULTI-DIMENTIONAL CHESS DOWNLOAD FROM DEEP SPACE
HTTP://WWW.BEYOND-SCIENCE.COM

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


url encode latin-1 characters

2006-07-23 Thread Lurker
I want send latin-1 string to web server by url parameter

urllib.quote return just symbol code with preceeding percent for every
non-ascii character:
#>ustr = 'Ü'
#>urllib.quote(ustr)
'%9A'

but this seems to be wrong because server response contains my
parameter and it differ from original (for example 'Ü' became '[')

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


Re-evaluating a string?

2006-07-23 Thread bugnthecode
I'm writing a program to send data over the serial port. I'm using
pyserial, and I'm on WindowsXP. When I use literals I can get the data
accross how I want it for example:

   1 2  3  4 5  6
serialport.write('!SC'+'\x01'+'\x05'+'\xFA'+'\x00'+'\r')

1=Get devices attention
2=Select channel on device
3=Rate for movement
4=Low byte of 16 bits
5=High bytes of 16 bits
6=Carriage return signaling command is over

This command works as desired. Sends the first 3 ASCII characters, then
some numbers in hex followed by a carriage return.

My problem is that the "write()" function only takes a string, and I
want to substitute variables for the hex literals.

I know that I can use the "hex()" function and it will return a string
with the appropriate hex value, and I could combine this with some
other literals like "\\" to create my desired hex literal, but then I
would need something to re-parse my string to change my ASCII text into
the appropriate hex values.

Any ideas on how I may do this? Any help is greatly appreciated.

Thanks,
Will

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


Re: Understanding Unicode & encodings

2006-07-23 Thread John Machin
[EMAIL PROTECTED] wrote:
> [EMAIL PROTECTED] wrote:
> > I tried to encode the different variables in many different encodings
> > (latin1), but I always get an exception. Where does this ascii codec
> > error comes from? How can I simply build this query string?
>
> Raphael,
>
> The 'ascii' encoding is set in the python library file site.py
> (/usr/lib/python2.4/site.py on my gentoo machine) as the system default
> encoding for python. The solution I used to the problem you're
> describing was to create a sitecustomize.py file and redefine the
> encoding as 'utf-8'.

Here is the word from on high (effbot, April 2006):
"""
(you're not supposed to change the default encoding. don't
do that; it'll only cause problems in the long run).
"""

That exception is a wake-up call -- it means "you don't have a clue how
your 8-bit strings are encoded". You are intended to obtain a clue
(case by case), and specify the encoding explicitly (case by case).
Sure the current app might dump utf_8 on you. What happens if the next
app dumps latin1 or cp1251 or big5 on you?

> This gets you halfway there. Beyond that you need to "stringify" the
> (potentially Unicode) strings during concatenation, e.g.:
>
> self.dbCursor.execute("""INSERT INTO track (name, nbr, idartist,
> idalbum, path)
>  VALUES ('%s', %s, %s, %s, '%s')""" % \
>  (str(track), nbr, idartist, idalbum, path))
>
> (Assuming that track is the offending string.) I'm not exactly sure why
> this explicit conversion is necessary, as it is supposed to happen
> automatically, but I get the same UnicodeDecodeError error without it.

Perhaps if you were to supply info like which DBMS, type of the
offending column in the DB, Python type of the value that *appears* to
need stringification, ... we could help you too.

Cheers,
John

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


Re: Track keyboard and mouse usage

2006-07-23 Thread dfaber
Hi Francois,
 Thank you for providing me the evdev link! That was exactly what I was
looking for. Instead of sudo'ing the script, I changed /dev/input/
directory to be world readable.
After that, I had to change the way a file was accessed in evdev.py to:
Line No: 91 #self.fd = os.open(filename, os.O_RDWR |
os.O_NONBLOCK)
self.fd = os.open(filename, os.O_RDONLY | os.O_NONBLOCK)

Runs great.

Thanks again.

[EMAIL PROTECTED] wrote:
> Hello dfaber,
>
> I had the same problem not long ago. I tried to use the Xlib since its
> obvious the X server  has all the events but I couldn't have the mouse
> events if my app was out of focus. If you have a way to do that I'm
> really interested.
>
> Anyway I found this to be a good introduction to Xlib:
> http://users.actcom.co.il/~choo/lupg/tutorials/xlib-programming/xlib-programming.html#preface
>
> Since I didn't find a way to do it I went down to the source of the
> events which are provided by the evdev drivers:
> http://en.wikipedia.org/wiki/Evdev
>
> Fortunately you can use Python to access it:
> http://svn.navi.cx/misc/trunk/python/evdev/
>
> First you need to know your input devices, search the eventX in
> relation to your device here:
> cat /proc/bus/input/devices
>
> Then you can do:
> sudo python evdev.py /dev/input/eventX  # where X is the event number
> in relation to your device (kb is usually zero)
>
> It works well but there is two problems with this solution:
> - the root access is annoying (but I'll have to try Diez suggestion)
> - The X event number of the mouse can change from a PC to another one
> (you need to check the PC first with that cat command and search for
> your "mouse"
>
> francois
>
>
>
>
>
> dfaber wrote:
> > Hi all,
> >  I have been searching for a keyboard and mouse tracker on linux. I've
> > read solutions (watch at sourceforge) which look at /proc/interrupts to
> > check keyboard or mouse activity. I also read one post where "watch"
> > seems to have difficulty tracking usb keyboards and mice. So, I'm out
> > of ideas here.
> >
> > My goal are:
> > 1. Check keyboard activity. I'm not interested in logging which keys
> > are pressed or record them.
> > 2. Monitor mouse activity. I want to know the pointer position,
> > left-clicks, right-clicks, middle-mouse button usage.
> >
> > I know that these things can be done in a GUI environment. I am looking
> > for some approach that helps me do this system-wide.
> >
> > Any suggestions would be welcome. Again, I am looking for trackers on
> > Linux based machines.

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


Re: Grail not downloading

2006-07-23 Thread Martin v. Löwis
Dustan wrote:
> Does anybody know anything about Grail?  I've been  unable to get at
> it, and I've tried on both Windows and Macintosh machines.
> 
> http://grail.sourceforge.net/

The files just don't exist, physically, on the server (if you have
an SF account, you can check this yourself). However, it appears
that the source code is still available through CVS:

http://grail.cvs.sourceforge.net/grail/grail/

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


Re: Generating all possible combination of elements in a list

2006-07-23 Thread Martin v. Löwis
Mir Nazim wrote:
> Example Problem:
> 
> Generate all possible permutations for
> [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
> 
> [1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2] (notice an extra 2 )
> 
> eliminate some combinations based on some conditions and combine the
> rest of combinations. And now generate all possible combinations for
> resulting data set.
> Hope you get the idea.

Unfortunately, I don't. Why do you have two lists for which to generate
permutations? Why is it important that the second list has an extra 2
(actually, not extra, but it replaces a 1)?
What are "some conditions" by which to eliminate permutations?
How to "combine" the remaining permutations?
What is the "resulting data set", and what is a "possible combination"
of it?

If the task is to produce all distinct permutations of 6 occurrences
of 1 and 6 occurrences of 2, I suggest the program below. It needs
produces much fewer than 12! results (namely, 924).

Regards,
Martin

numbers = [1,2]
remaining = [None, 6, 6]
result = [None]*12

def permutations(index=0):
if index == 12:
yield result
else:
for n in numbers:
if not remaining[n]:
continue
result[index] = n
remaining[n] -= 1
for k in permutations(index+1):
yield k
remaining[n] += 1

for p in permutations():
print p
-- 
http://mail.python.org/mailman/listinfo/python-list


Grail not downloading

2006-07-23 Thread Dustan
Does anybody know anything about Grail?  I've been  unable to get at
it, and I've tried on both Windows and Macintosh machines.

http://grail.sourceforge.net/

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


Re: Understanding Unicode & encodings

2006-07-23 Thread John Machin
Jim wrote:
> [EMAIL PROTECTED] wrote:
> > Hello,
> >
> > For my application, I would like to execute an SQL query like this:
> > self.dbCursor.execute("INSERT INTO track (name, nbr, idartist, idalbum,
> > path) VALUES ('%s', %s, %s, %s, '%s')" % (track, nbr, idartist,
> > idalbum, path))
> No, I'll bet that you'd like to run something like
>   self.dcCursor.execute("INSERT INTO track (name, nbr, idartist,
> idalbum,path) VALUES (%(track)s, %(nbr)s,
> %(idartist)s,%(idalbum)s,'%(path)s')",
> {'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path})
> (only without my typos).  That's an improvment for a number of reasons,
> one of which is that the system will quote for you, for instance in
> idartist="John's Beer" changing the single quote to two single quotes
> to suit SQL.

>   self.dcCursor.execute("INSERT INTO track (name, nbr, idartist,
> idalbum,path) VALUES (%(track)s, %(nbr)s,
> %(idartist)s,%(idalbum)s,'%(path)s')",
> {'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path})

I see no improvement here.

The OP's code is effectively::

  sql = "INSERT INTO track (name, ..., path) VALUES ('%s', ..., '%s')"
  value_tuple = (track, , path)
  self.dcCursor.execute(sql % value_tuple)

Your suggested replacement is effectively:

  sql = "INSERT INTO track (name, ...,path) VALUES (%(track)s,
...,'%(path)s')"
  str_fmt_dict = {'track':track, ...,'path':path}
  self.dcCursor.execute(sql, str_fmt_dict)

Well, that won't run at all. Let's correct the presumed typo:

   self.dcCursor.execute(sql % str_fmt_dict)

Now, the only practical difference is that you have REMOVED the OP's
explicit quoting of the first column value. Changing the string
formatting from the %s style to the %(column_name) style achieves
nothing useful. You are presenting the "system" with a constant SQL
string -- it is not going to get any chance to fiddle with the quoting.
However the verbosity index has gone off the scale: each column name is
mentioned 4 times (previously 1).

I would suggest the standard default approach:

  sql = "INSERT INTO track (name, ..., path) VALUES (?, ..., ?)"
  value_tuple = (track, , path)
  self.dcCursor.execute(sql, value_tuple)

The benefits of doing this include that the DBAPI layer gets to
determine the type of each incoming value and the type of the
corresponding DB column, and makes the appropriate adjustments,
including quoting each value properly, if quoting is necessary.

> > Every time I execute this, I get an exception like
> > this:
> >
> > UnicodeDecodeError: 'ascii' codec can't decode byte 0xa1 in position
> > 64: ordinal not in range(128)
> >
> > I tried to encode the different variables in many different encodings
> > (latin1), but I always get an exception. Where does this ascii codec
> > error comes from? How can I simply build this query string?

> Some more information may help: is the error returned before or during
> the execute call?  If before, then the execute() call is a distraction.
>  If during, then what is your dB, what is it's encoding (is the dB
> using latin1, or does the dB only accept ascii?), and what are you
> using to connect to it?

These are very sensible questions. Some more q's for the OP:

(1) What is the schema for the 'track' table?

(2) "I tried to encode the different variables in many different
encodings (latin1)" -- you say "many different encodings" but mention
only one ... please explain and/or show a sample of the actual code of
the "many different" attempts.

(3) You said that your input values (produced by some libblahblah) were
in Unicode -- are you sure? The exception that you got means that it
was trying to convert *from* an 8-bit string *to* Unicode, but used the
default ASCII codec (which couldn't hack it). Try doing this before the
execute() call:

  print 'track', type(track), repr(track)
  ...
  print 'path', type(path), repr(path)

and change the execute() call to three statements along the above
lines, so we can see (as Jim asked) where the exception is being
raised.

HTH,
John

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


find_longest_match in SequenceMatcher

2006-07-23 Thread koara
Hello, it might be too late or too hot, but i cannot work out this
behaviour of find_longest_match() in difflib.SequenceMatcher:

string1:
releasenotesforwildmagicversion01thiscdromcontainstheinitialreleaseofthesourcecodethataccompaniesthebook"3dgameenginedesign:apracticalapproachtorealtimecomputergraphics"thereareanumberofknownissuesaboutthecodeastheseissuesareaddressedtheupdatedcodewillbeavailableatthewebsitehttp://wwwmagicsoftwarecom/[EMAIL
 PROTECTED]

string2:
releasenotesforwildmagicversion02updatefromversion01toversion02ifyourcopyofthebookhasversion01andifyoudownloadedversion02fromthewebsitethenapplythefollowingdirectionsforinstallingtheupdateforalinuxinstallationseethesectionattheendofthisdocumentupdatedirectionsassumingthatthetopleveldirectoryiscalledmagicreplacebyyourtoplevelnameyoushouldhavetheversion01contentsinthislocation1deletethecontentsofmagic\include2deletethesubdirectorymagic\source\mgcapplication3deletetheobsoletefiles:amagic\source\mgc

find_longest_match(0,500,0,500)=(24,43,10)="version01t"

What? O_o Clearly there is a longer match, right at the beginning!
And then, after removal of the last character from each string (i found
the limit of 500 by trial and error -- and it looks suspiciously
rounded):

find_longest_match(0,499,0,499)=(0,0,32)="releasenotesforwildmagicversion0"


Is this the expected behaviour? What's going on?
Thank you for any ideas

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


Re: building lists of dictionaries

2006-07-23 Thread Tim Chase
>>parinfo = [{'value':0., 'fixed':0, 'limited':[0,0],
>>'limits':[0.,0.]}.copy() for i in xrange(0,6)]
>>
>>However, this will still reference internal lists that have
>>been referenced multiple times, such that
>>
>> >>> parinfo[5]['limited']
>>[0, 0]
>> >>> parinfo[4]['limited'][0] = 2
>> >>> parinfo[5]['limited']
>>[2, 0]
> 
> Interesting. Cut-and-paste to my python prompt and I get
> 
parinfo[5]['limited']
> 
> [0, 0]

Hmm...same behavior on my 2.3.5 here.  Looks like the problem lay 
within the for-loop version I tried first:

parinfo = [{'value':0., 'fixed':0, 'limited':[0,0],
'limits':[0.,0.]}]
for i in xrange(1,6): parinfo.append(parinfo[0].copy())

which copied the elements, but when the elements were references 
to other lists, only the references were copied.  I just tried it 
with the second method I suggested (the list-comprehension one 
you reference here) and it doesn't have the same problem as the 
crazy for-loop, and as shown by others, doesn't need the 
superflous copy() call either.

That'll teach me to test both proposed ideas, rather than making 
assumptions.

-tkc






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


Re: How to force a thread to stop

2006-07-23 Thread M�ta-MCI
Hi!

>>>  "threadicide" method

I like this word...

Michel Claveau 


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


Re: building lists of dictionaries

2006-07-23 Thread Ernesto García García
>>parinfo = [{'value':0., 'fixed':0, 'limited':[0,0],
>>'limits':[0.,0.]}.copy() for i in xrange(0,6)]
>>
>>However, this will still reference internal lists that have
>>been referenced multiple times, such that
>>
>> >>> parinfo[5]['limited']
>>[0, 0]
>> >>> parinfo[4]['limited'][0] = 2
>> >>> parinfo[5]['limited']
>>[2, 0]
> 
> Interesting. Cut-and-paste to my python prompt and I get
> 
parinfo[5]['limited']
> 
> [0, 0]
> 
> Tried both Python 2.4.1 and 2.5 beta, Linux, GCC 4.0.2

Of course. The expression within the list comprehension is evaluated for 
each iteration, so that the objects are recreated each time. The copy() 
for the dictionary is also not needed:

 >>> parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 
'limits':[0.,0.]} for i in xrange(0,6)]
 >>> parinfo
[{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}]
 >>> parinfo[0]['limited']
[0, 0]
 >>> parinfo[0]['limited'][0]=1
 >>> parinfo
[{'limited': [1, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}]

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


Re: How to generate geometric random numbers?

2006-07-23 Thread Robert Kern
Gerhard Fiedler wrote:
> On 2006-07-23 17:12:20, [EMAIL PROTECTED] wrote:
> 
>> I am a newbie to Python and would like to genereate some numbers
>> according to geometric distribution. However, the Python Random package
>> seems do not have implemented functionality. I am wondering is there
>> exist any other libraries that can do this job? 
> 
> The usual way is to generate standard random numbers (linear distribution)
> and then apply whatever transformation you need to generate the desired
> distribution.

That only works if there is such a transformation.

The geometric distribution and many others have been implemented in numpy:

   http://www.scipy.org/NumPy

In [1]: from numpy import random

In [2]: random.geometric(0.5, size=100)
Out[2]:
array([1, 5, 2, 3, 1, 1, 2, 3, 1, 1, 2, 1, 1, 1, 2, 2, 3, 3, 1, 1, 1, 1, 1,
2, 2, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 3, 1,
4, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 1, 1, 1, 3, 1, 1, 2, 6, 1, 1, 3, 2,
1, 1, 2, 1, 1, 7, 2, 1, 1, 2, 1, 1, 2, 4, 1, 2, 1, 4, 2, 1, 1, 2, 1,
4, 2, 1, 1, 3, 1, 3, 1])

-- 
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: How to generate geometric random numbers?

2006-07-23 Thread Gerhard Fiedler
On 2006-07-23 17:12:20, [EMAIL PROTECTED] wrote:

> I am a newbie to Python and would like to genereate some numbers
> according to geometric distribution. However, the Python Random package
> seems do not have implemented functionality. I am wondering is there
> exist any other libraries that can do this job? 

The usual way is to generate standard random numbers (linear distribution)
and then apply whatever transformation you need to generate the desired
distribution.

Gerhard

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


Re: [Newbie] List from a generator function

2006-07-23 Thread Ernesto García García
Thank you guys.

So the answer is to keep with the original form, perhaps with xrange.

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


How to generate geometric random numbers?

2006-07-23 Thread MyInfoStation
Hi all,

I am a newbie to Python and would like to genereate some numbers
according to geometric distribution. However, the Python Random package
seems do not have implemented functionality. I am wondering is there
exist any other libraries that can do this job? 

Thanks a lot,

Da

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


Generating all possible combination of elements in a list

2006-07-23 Thread Mir Nazim
Hello,

I need to write scripts in which I need to generate all posible unique
combinations of an integer list. Lists are a minimum 12 elements in
size with very large number of possible combination(12!)

I hacked a few lines of code and tried a few things from Python
CookBook (http://aspn.activestate.com/ASPN/Cookbook/), but they are
hell slow.

Does any body know of an algorithm/library/module for python that can
help me in generation of these combinations faster

"""ONLY REQUIREMENT IS SPEED"""

Example Problem:

Generate all possible permutations for
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]

[1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2] (notice an extra 2 )

eliminate some combinations based on some conditions and combine the
rest of combinations. And now generate all possible combinations for
resulting data set.
Hope you get the idea.



Thanks 

PS: Tried matlab/scilab. They are slower than python.

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


Re: Python for Lazarus (not Delphi)

2006-07-23 Thread Ravi Teja

Uwe Grauer wrote:
> Does anyone know if something similar to Python for Delphi
> does exist for lazarus?
>
> Thanks for any pointers,
>   Uwe

Python for Delphi does support Lazarus since Version 3.29
http://mmm-experts.com/VersionHistory.aspx?ProductId=3

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


Re: Nested function scope problem

2006-07-23 Thread Gerhard Fiedler
On 2006-07-23 14:53:33, danielx wrote:

> I can't figure out why Josiah's breakLine function won't work either. I
> know Josiah has had his problem resolved, but I'd still like to know
> why his func won't work. I'd like to redirect this discussion in that
> direction, if I may.

I think what happens is this (and this may not be expressed in the proper
terms for Python): It is possible to read variables from the outer function
in the inner function. But when trying to write to them, this causes that
same name to be re-defined in the inner function's scope, making it a
different variable. Now, in the OP's code, that caused that new variable
(with scope of the inner function) to be accessed before anything was
assigned to it. 

One obvious way is to not write to the variables from the outer scope, but
rather return a value from the inner function and assign it in the outer
function. But it seems there should be a way to be able to write in the
inner function to variables that are defined in the outer function.


>> First point: the nested function only have access to names that exists
>> in the enclosing namespace at the time it's defined.
> 
> Coming from lisp, that doesn't make very much sense, and I'm not sure
> that's true. If you move the def for addTok bellow the lines that
> initialize the locals of breakLines, you still get the same problem.

The problem there is only secondarily a scope problem. At first it is
reading a variable (the inner function scope variable tok) before anything
has been assigned to it. Of course, the real problem is the secondary one:
that this variable tok is a variable of scope addTok and not of scope
breakLine.

>> Second point: a nested function cannot rebind names from the enclosing
>> namespace. Note that in Python, rebinding a name and modifying the
>> object bound to a name are very distinct operations.
> 
> I'm not sure that's the problem, because when I ran the debugger, the
> problem is with the line that says if len(tok), not the one bellow it
> which says tok = "". Regardless, my above issue seems to be overriding
> this one.

Yes, but it is the line "tok = ''" that seems to cause tok to be now a
variable of the inner function's scope (rather than the variable tok of
breakLine). 


> After some experimentation, I am completely baffeled as to why
> breakLine won't work. Here is an example of one of the things I did,
> which I believe exactly mimics what breakLine does:
> 
 def outer():
> ...   def inner():
> ...   if outerLocal:
> ...   return "I hear you, 'hello world'."
> ...   else:
> ...   return "Come again?"
> ...   outerLocal = "hello world"
> ...   return inner()
> ...
 outer()
> "I hear you, 'hello world'."
> 
> As I said, I believe the line which sets tok should break (quietly),
> but not the line which tests tok. My experiment seems to confirm
> this...

The line that sets tok causes tok to be a different tok from the outer tok
-- in the whole scope of the assignment.

Gerhard

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


Re: Referring to Interpreter (Jython)

2006-07-23 Thread wojciech
Hi everyone,

Ah, after some more searching and programming, I got it:

PythonInterpreter py = new PythonInterpreter();
py.set("interpreter", py);

Thanks,
Wojciech

[EMAIL PROTECTED] wrote:
> Hi everyone,
>
> I apologize if this is a bit off-topic. I am currently working on a
> Java-based application that is used Jython as a scripting language
> within the actual program. However, for one of my GUI elements, I need
> to pass the actual PythonInterpeter to the constructor.
>
> I know that in Java there is the "this" keyword which allows an object
> to refer to itself. Does anyone know if there is a way to use a similar
> type of keyword to have the PythonInterpreter refer to itself?
>
> Thanks,
> Wojciech
>
> P.S.
>
> If there is a more appropriate newsgroup to which I should post this,
> please let me know.

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


Re: Understanding Unicode & encodings

2006-07-23 Thread clarkcb
[EMAIL PROTECTED] wrote:
> I tried to encode the different variables in many different encodings
> (latin1), but I always get an exception. Where does this ascii codec
> error comes from? How can I simply build this query string?

Raphael,

The 'ascii' encoding is set in the python library file site.py
(/usr/lib/python2.4/site.py on my gentoo machine) as the system default
encoding for python. The solution I used to the problem you're
describing was to create a sitecustomize.py file and redefine the
encoding as 'utf-8'. The entire file contents look like this:


'''
Site customization: change default encoding to UTF-8
'''
import sys
sys.setdefaultencoding('utf-8')


For more info on creating a sitecustomize.py file, read the comments in
the site.py file.

I use UTF-8 because I do a lot of multilingual text manipulation, but
if all you're concerned about is Western European, you could also use
'latin1'.

This gets you halfway there. Beyond that you need to "stringify" the
(potentially Unicode) strings during concatenation, e.g.:

self.dbCursor.execute("""INSERT INTO track (name, nbr, idartist,
idalbum, path)
 VALUES ('%s', %s, %s, %s, '%s')""" % \
 (str(track), nbr, idartist, idalbum, path))

(Assuming that track is the offending string.) I'm not exactly sure why
this explicit conversion is necessary, as it is supposed to happen
automatically, but I get the same UnicodeDecodeError error without it.

Hope this helps,
Cary

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


Referring to Interpreter (Jython)

2006-07-23 Thread wojciech
Hi everyone,

I apologize if this is a bit off-topic. I am currently working on a
Java-based application that is used Jython as a scripting language
within the actual program. However, for one of my GUI elements, I need
to pass the actual PythonInterpeter to the constructor.

I know that in Java there is the "this" keyword which allows an object
to refer to itself. Does anyone know if there is a way to use a similar
type of keyword to have the PythonInterpreter refer to itself?

Thanks,
Wojciech

P.S.

If there is a more appropriate newsgroup to which I should post this,
please let me know.

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


Re: getaddrinfo not found on SCO OpenServer 5.0.5

2006-07-23 Thread edcdave

Martin v. Löwis wrote:
> [EMAIL PROTECTED] wrote:
> > 1) I've seen mention of native vs. Python getaddrinfo implementations.
> > If that's true, how can I force the program to use the Python one?
> >
> > 2) Is there an option to not use the BSD Library function?
> >
> > 3) Finally, is there a trick to searching for shared libaries?
>
> There is an autoconf test to determine whether getaddrinfo is
> available on the system. You should study that test to find out
> why it thinks the function is available when it is actually not.
> If you can't do that, you can manually edit pyconfig.h to
> change the outcome of configure.
>
> Regards,
> Martin

Thank you for the suggestion. I will try it July 24 when I am back in
the office.

I've dug into it further and, on the surface, it appears that IPv6 was
identified as available when the binary was built for OSR 5. IPv6 is
not available for OSR5, so the binary was built on OSR 6 and
mislabeled, or the autoconf test failed.

Thanks again, Martin, for the relevant reply,
Dave Harris

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


Re: Nested function scope problem

2006-07-23 Thread danielx
Bruno Desthuilliers wrote:
> Josiah Manson a écrit :
> > I found that I was repeating the same couple of lines over and over in
> > a function and decided to split those lines into a nested function
> > after copying one too many minor changes all over. The only problem is
> > that my little helper function doesn't work! It claims that a variable
> > doesn't exist. If I move the variable declaration, it finds the
> > variable, but can't change it. Declaring the variable global in the
> > nested function doesn't work either.
> >
> > But, changing the variable in the containing scope is the whole purpose
> > of this helper function.
> >
> > I'm new to python, so there is probably some solution I haven't
> > encountered yet. Could you please suggest a nice clean solution? The
> > offending code is below. Thanks.
> >
> > def breakLine(s):
> > """Break a string into a list of words and symbols.
> > """
> > def addTok():
> > if len(tok) > 0:
>
>  if tok:
>
> An empty sequence evals to False in a boolean context.
>
> > ls.append(tok)
> > tok = ''
> >

I can't figure out why Josiah's breakLine function won't work either. I
know Josiah has had his problem resolved, but I'd still like to know
why his func won't work. I'd like to redirect this discussion in that
direction, if I may.

>
> First point: the nested function only have access to names that exists
> in the enclosing namespace at the time it's defined.

Coming from lisp, that doesn't make very much sense, and I'm not sure
that's true. If you move the def for addTok bellow the lines that
initialize the locals of breakLines, you still get the same problem.

>
> Second point: a nested function cannot rebind names from the enclosing
> namespace. Note that in Python, rebinding a name and modifying the
> object bound to a name are very distinct operations.

I'm not sure that's the problem, because when I ran the debugger, the
problem is with the line that says if len(tok), not the one bellow it
which says tok = "". Regardless, my above issue seems to be overriding
this one.

>
> Third point : functions modifying their environment this way are usually
> considered bad form.

Again, this is coming from lisp, but I don't see anything wrong with
that :P.

***

After some experimentation, I am completely baffeled as to why
breakLine won't work. Here is an example of one of the things I did,
which I believe exactly mimics what breakLine does:

>>> def outer():
... def inner():
... if outerLocal:
... return "I hear you, 'hello world'."
... else:
... return "Come again?"
... outerLocal = "hello world"
... return inner()
...
>>> outer()
"I hear you, 'hello world'."

As I said, I believe the line which sets tok should break (quietly),
but not the line which tests tok. My experiment seems to confirm
this...

One thing I can understand is why the line tok = "" in addTok won't
work. This is because when Python sees that line, it should create a
new local variable in the scope of addTok. Once addTok returns, that
variable is lost. That's pretty deep, now that I've thought about it...

>
> Here's a possible solution - but note that there are probably much
> better ways to get the same result...
>
> def breakline(line):
>"""Break a string into a list of words and symbols."""
>class TokenList(list):
>  def append(self, token):
>if token:
>  list.append(self, token)
>return ''
>
>tokens = TokenList()
>token = ''
>splitters = '?()&|:~,'
>whitespace = ' \t\n\r'
>specials = splitters + whitespace
>
>for char in line:
>  if char in specials:
>   token = tokens.append(token)
>  if char in splitters:
>tokens.append(char)
>  else:
>token += char
> 
>tokens.append(token)
>return list(tokens)
> 
> (snip)

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


Re: cherrypy tracing back all the time

2006-07-23 Thread Hari Sekhon
On 23/07/06, Hari Sekhon <[EMAIL PROTECTED]> wrote:
I've got a very simple script with cherrypy but for some reason the cherrypy server is constantly tracing back but it stays up, kind of, the performance etc though shows that something is wrong.import cherrypy
import threading
def someFunc():    while 1:    print "working"threading._start_new_thread( someFunc , () )class someClass():    def index(self):    test="this is a test"
    return test    index.exposed=Truecherrypy.root=someClass()cherrypy.config.update( file="cherrypyconfig.txt" )cherrypy.server.start()The tracebacks I get are:Traceback (most recent call last):
  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 242, in run    request.parse_request()  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 134, in parse_request
    method,path,version = request_line.strip().split(" ", 2)ValueError: need more than 1 value to unpackTraceback (most recent call last):  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 242, in run
    request.parse_request()  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 134, in parse_request    method,path,version = request_line.strip().split(" ", 2)ValueError: need more than 1 value to unpack
Traceback (most recent call last):  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 242, in run    request.parse_request()  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 134, in parse_request
    method,path,version = request_line.strip().split(" ", 2)ValueError: need more than 1 value to unpackTraceback (most recent call last):  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 242, in run
    request.parse_request()  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 134, in parse_request    method,path,version = request_line.strip().split(" ", 2)ValueError: need more than 1 value to unpack
Traceback (most recent call last):  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 242, in run    request.parse_request()  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 134, in parse_request
    method,path,version = request_line.strip().split(" ", 2)ValueError: need more than 1 value to unpackTraceback (most recent call last):  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 242, in run
    request.parse_request()  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 134, in parse_request    method,path,version = request_line.strip().split(" ", 2)ValueError: need more than 1 value to unpack
2006/07/23 17:58:46  INFO Traceback (most recent call last):  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgi.py", line 110, in wsgiApp    environ['wsgi.url_scheme'],  File "/usr/lib/python2.4/site-packages/cherrypy/_cpserver.py", line 227, in request
    requestLine, headers, rfile, scheme)  File "/usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py", line 181, in __init__    self.parseFirstLine()  File "/usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py", line 223, in parseFirstLine
    request.method, path, request.protocol = request.requestLine.split()ValueError: too many values to unpackI don't understand, all I've asked the stupid cherrypy thing to do is to print one line...


Ok, when the somefunc() that is executed by threading runs, it doesn't cause the tracing back I showed above, it occurs only when I use one of the other two functions I have in it's place in the script via threading. The thing is, those other two functions both use try excepts inside to make sure there are no exceptions generated beyond the internal function, and neither of those functions can touch anything to do with the class used as the index of the cherrypy document root.
What is going on?
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Which Pyton Book For Newbies?

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

I'm sure you will hear this many times, but that's a great choice ;). I
really think you'll like Learning Python from O'Reilly Press. The
authors claim you can read the book even with no prior programming
experience, which seems plausible having read it. Of course, you
already have some programming experience, so it should go much more
smoothly with you. Good luck finding the right book!

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


cherrypy tracing back all the time

2006-07-23 Thread Hari Sekhon
I've got a very simple script with cherrypy but for some reason the cherrypy server is constantly tracing back but it stays up, kind of, the performance etc though shows that something is wrong.import cherrypyimport threading
def someFunc():    while 1:    print "working"threading._start_new_thread( someFunc , () )class someClass():    def index(self):    test="this is a test"
    return test    index.exposed=Truecherrypy.root=someClass()cherrypy.config.update( file="cherrypyconfig.txt" )cherrypy.server.start()The tracebacks I get are:Traceback (most recent call last):
  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 242, in run    request.parse_request()  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 134, in parse_request
    method,path,version = request_line.strip().split(" ", 2)ValueError: need more than 1 value to unpackTraceback (most recent call last):  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 242, in run
    request.parse_request()  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 134, in parse_request    method,path,version = request_line.strip().split(" ", 2)ValueError: need more than 1 value to unpack
Traceback (most recent call last):  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 242, in run    request.parse_request()  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 134, in parse_request
    method,path,version = request_line.strip().split(" ", 2)ValueError: need more than 1 value to unpackTraceback (most recent call last):  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 242, in run
    request.parse_request()  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 134, in parse_request    method,path,version = request_line.strip().split(" ", 2)ValueError: need more than 1 value to unpack
Traceback (most recent call last):  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 242, in run    request.parse_request()  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 134, in parse_request
    method,path,version = request_line.strip().split(" ", 2)ValueError: need more than 1 value to unpackTraceback (most recent call last):  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 242, in run
    request.parse_request()  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py", line 134, in parse_request    method,path,version = request_line.strip().split(" ", 2)ValueError: need more than 1 value to unpack
2006/07/23 17:58:46  INFO Traceback (most recent call last):  File "/usr/lib/python2.4/site-packages/cherrypy/_cpwsgi.py", line 110, in wsgiApp    environ['wsgi.url_scheme'],  File "/usr/lib/python2.4/site-packages/cherrypy/_cpserver.py", line 227, in request
    requestLine, headers, rfile, scheme)  File "/usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py", line 181, in __init__    self.parseFirstLine()  File "/usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py", line 223, in parseFirstLine
    request.method, path, request.protocol = request.requestLine.split()ValueError: too many values to unpackI don't understand, all I've asked the stupid cherrypy thing to do is to print one line...
-- 
http://mail.python.org/mailman/listinfo/python-list

Machine intelligence- can this program be written ??

2006-07-23 Thread switzerland qunatium computer
Machine intelligence- can this program be written ??

Machine intelligence- can this program be written ??







In this game of chess it is a set of 7 chess boards

The PAWN the pawn can move just like the regular game but up and down
no levels

The rook on the end moves all the way up and left to right back and
forth

The knight moves in a L shape forward and backwards but only one
level step on the board back and forth

The bishop moves in a x just like in regular game but can move to all
levels back and forth

The Queen moves just like in the regular game but can move on all
levels
back and forth

The king can move one square at a time and to move one level back and
forth

NOW EVERY TIME THE GAME HAS ENDED THE 7 LEVEL BOARD IS PLACED IN A CUBE
WITH BOARD ARE ADD ON EACH SIDE TO KEEP IN A CUBE AND GROWS INFINITE
UNTIL THE MACHINE INTELLIGENCE ASK FOR MULTI-DIMENSIONS

THE OBJECT IS:

PROTECTING ALL PIECES WITH ONE OR MORE PIECES.

ONLY LOSE A PIECE BY HIM TAKING ONE, AND THEN YOU TAKING ANOTHER.

TRY TO MAKE IT WHERE HE CANNOT MOVE

TAKE ALL CHESS PIECES

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


Don't you hate it when your bed squeaks?

2006-07-23 Thread switzerland qunatium computer
Don't you hate it when your bed squeaks?

http://www.beyond-science.com

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


Re: building lists of dictionaries

2006-07-23 Thread Juho Schultz

Tim Chase wrote:

> parinfo = [{'value':0., 'fixed':0, 'limited':[0,0],
> 'limits':[0.,0.]}.copy() for i in xrange(0,6)]
>
> However, this will still reference internal lists that have
> been referenced multiple times, such that
>
>  >>> parinfo[5]['limited']
> [0, 0]
>  >>> parinfo[4]['limited'][0] = 2
>  >>> parinfo[5]['limited']
> [2, 0]
>

Interesting. Cut-and-paste to my python prompt and I get
>>> parinfo[5]['limited']
[0, 0]

Tried both Python 2.4.1 and 2.5 beta, Linux, GCC 4.0.2

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


Re: Which Pyton Book For Newbies?

2006-07-23 Thread Cameron Laird
In article <[EMAIL PROTECTED]>,
 <[EMAIL PROTECTED]> wrote:
>
>W. D. Allen wrote:
>> I want to write a retirement financial estimating program. Python was
>> suggested as the easiest language to use on Linux. I have some experience
>> programming in Basic but not in Python.
>>
>> I have two questions:
>>  1. What do I need to be able to make user GUIs for the program, and
>>  2. Which book would be easiest to use to learn Python programming?
>
>I am a fairly experienced programmer and I have been reading Dive Into
>Python. If you have prior experience, you may find it very
>satisfactory. (But see a recent thread I started which points out a few
>small mistakes...nothing too bad over all.) If you have less
>programming experience, you may wish to look at Byte of Python. The
>great thing about Python is that there is a ton of online material to
>peruse...
>

As fond as I am of Python (enough so to have been recognized for my
advocacy by the community), and as often as I've lauded Python for
its "easy entry", I feel compelled to observe that it *is* possible
to program in Basic under Linux; perhaps such an approach would
particularly suit you.  Have you considered, for example, http://directory.google.com/Top/Computers/Programming/Languages/BASIC/ >?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: building lists of dictionaries

2006-07-23 Thread Juho Schultz
Jean_Francois Moulin wrote:
> Hi all,
>
> I tried this piece of code (FWIW, it was taken as is from a help section of 
> mpfit, a mathematical routine for least square fitting):
>
> parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6

> The first line builds a list of six dictionaries with initialised keys.

> This is not so!
> I end up with all dictionaries being identical and having their 'fixed' key 
> set to 1, and limited[0]==1 and limits[0]==50.
>
> I do not understand this behaviour...
>
> Thanks for helping a newbie.
>
> JF


xvec = [{'value':0}]*6
xids = [id(x) for x in xvec]
print xids

Should print a list of six integers, all equal.
So all elements in your list are the same.

Another way to construct the desired list:

yvec = [{'value':0} for i in range(6)]
yids = [id(y) for y in yvec]
print yids

The elements in this list should be all different.

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


Re: building lists of dictionaries

2006-07-23 Thread Tim Chase
 > parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 
'limits':[0.,0.]}]*6
 > parinfo[0]['fixed'] = 1
 > parinfo[4]['limited'][0] = 1
 > parinfo[4]['limits'][0]  = 50.
 >
 > The first line builds a list of six dictionaries with
 > initialised keys.  I expected that the last three lines
 > would only affect the corresponding keys of the
 > corresponding dictionnary and that I would end up with a
 > fully initialised list where only the 'fixed' key of the
 > first dict would be 1, and the first values of limited and
 > limits for dict number 4 would be 1 and 50.
 > respectively
 >
 > This is not so! I end up with all dictionaries being
 > identical and having their 'fixed' key set to 1, and
 > limited[0]==1 and limits[0]==50.

 > I do not understand this behaviour...

The *6 creates multiple references to the same dictionary.
Thus, when you update the dictionary through one
reference/name (parinfo[0]), the things that the other
entries (parinfo[1:5]) reference that changed dictionary.

You're likely looking for something like

parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 
'limits':[0.,0.]}]
for i in xrange(1,6): parinfo.append(parinfo[0].copy())

or something like

parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 
'limits':[0.,0.]}.copy() for i in xrange(0,6)]

However, this will still reference internal lists that have
been referenced multiple times, such that

 >>> parinfo[5]['limited']
[0, 0]
 >>> parinfo[4]['limited'][0] = 2
 >>> parinfo[5]['limited']
[2, 0]

Thus, you'd also want to change it to be something like

parinfo = [
 {'value':0.,
 'fixed':0,
 'limited':[0, 0][:],
 'limits':[0., 0.][:]
  }.copy() for i in xrange(0, 6)]

where the slice operator is used to build a copy of the list
for each element as well (rather than keeping a reference to
the same list for each dictionary).

Hopefully this makes some sense, and helps get you on your
way.

-tkc






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


Re: building lists of dictionaries

2006-07-23 Thread Rob Williscroft
Jean_Francois Moulin wrote in
news:[EMAIL PROTECTED] in
comp.lang.python: 

> Hi all,
> 
> I tried this piece of code (FWIW, it was taken as is from a help
> section of mpfit, a mathematical routine for least square fitting): 
> 
> parinfo = [{'value':0., 'fixed':0, 'limited':[0,0],
> 'limits':[0.,0.]}]*6 parinfo[0]['fixed'] = 1
> parinfo[4]['limited'][0] = 1
> parinfo[4]['limits'][0]  = 50.
> 
> The first line builds a list of six dictionaries with initialised
> keys. I expected that the last three lines would only affect the
> corresponding keys of the corresponding dictionnary and that I would
> end up with a fully initialised list where only the 'fixed' key of the
> first dict would be 1, and the first values of limited and limits for
> dict number 4 would be 1 and 50. respectively 
> 
> This is not so!
> I end up with all dictionaries being identical and having their
> 'fixed' key set to 1, and limited[0]==1 and limits[0]==50. 
> 
> I do not understand this behaviour...

This should help:

http://www.python.org/doc/faq/programming/#how-do-i-create-a-
multidimensional-list>

As a TinyUrl: http://tinyurl.com/s8akj

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: building lists of dictionaries

2006-07-23 Thread Ernesto García García
> parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6

With this, you are creating a list with 6 references to the same list. 
Note that the left operand of '*' is evaluated only once before 
"multiplying" it six times.

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


Re: Coding style

2006-07-23 Thread Gerhard Fiedler
On 2006-07-23 06:24:09, Antoon Pardon wrote:

>> In general, I'd say that in this case the example was not well-chosen.
>> After such a "shudder removal", a poster should IMO review what caused
>> the shudder, and rephrase the original problem without the shudder.
> 
> The shudder is not with the poster. The poster can't predict how the
> readers will react. 

Correct. But if someone with the potential to help shudders, I might just 
rephrase my "question" (the code) to suit that person's preferences. Why 
not?

>> That might take a few iterations, but in general, either one or more of
>> the "shudder removals" actually address the OP's issue, maybe without
>> any individual helper understanding all of the problem (due to
>> incomplete example code), or the iterations lead to a code that's
>> "shudder free" and still shows the original problem -- now usually in a
>> clearer form, because free of other issues that are not relevant to the
>> OP's point.
> 
> I doubt that. 

I'm not sure, but I've seen in the short time I've been here that a few of 
the regulars both have their specific "shudder" issues, and post helpful 
alternative solutions and corrections. So I think it definitely can help to 
address their "shudder" issues and get this out of the way. 

> My experience is that you have more chance of being helped by people who
> try to understand where the OP is trying to go form than from those who
> react to style issues. 

No contest to that. But why not talk to the others, who in a way insist on 
their idiom but otherwise contribute to helpful solutions, in their idiom?

> I have never seem a bug go away because someone suggested to write "if
> Number:" instead of "if Number != 0:"

Of course. I didn't mean to imply this. But if the only thing that's 
required from me is to rephrase my code idiom and replace the "number != 0" 
comparisons with "number", that's a small price to pay. 

You are right, IMO, that this stuff comes over as petty sometimes. And that 
it often is far away from the original question. But just as often 
corrections that at first seem petty lead to different ideas about 
implementation that the OP didn't consider. 

My view is: I ask for help on a public forum. I get what I get... and if I 
consider that someone who responded with something that's not immediately 
helpful has a potential to help me better, I try to get in a dialog and 
address what has been suggested, carving out my real problem in the idiom 
of that person. This is my responsibility -- after all, I'm the one who 
wants to learn how that person would solve my problem. Later then I can try 
to separate what I want to use from that solution from what I consider 
personal preference of the helping person.


>> E.g. if someone makes a suggestion that is valid with the example code I
>> posted but not with the real code I have, I need to post an updated
>> example code that introduces a restriction similar to the one I
>> actually have, which then correctly invalidates the formerly valid
>> suggestion -- and helps getting more helpful responses.
> 
> Not necessarily. A valid suggestion doesn't imply a relevant suggestion.
> 
> If someone come with code like:
> 
>   if Number != 0:
> 
> And someone suggests that the python idiom is
> 
>   if Number:
> 
> Then that suggestion is valid, but it also is irrelevant. 

Correct. But if it helps the person who suggested it to see the original 
problem, now not hidden (for that person) under offending idiom, then the 
change was relevant -- not for the problem, but for the communication about 
it. 

It's like if you want good stock advice from your uncle, who's experienced 
in the matter but very formal, ask him without using the f*** word. You can 
use it when asking your other uncle, who's also experienced but doesn't 
really care about formalities. Tune in if you want a good channel...


>> This is a process that in the past has taught me a lot (not Python, but
>> that's just because I'm too new here :). Once you get good at this
>> process, it often helps you find the problem even before posting,
>> because boiling code down to what the /real/ problem is can show you a
>> lot you otherwise miss.
> 
> Sure, but if you do that, you sometimes have code that deviates from the
> python idiom because there was originnaly a good reason for, but that
> isn't obvious any more because you are temporarily working with a boiled
> down piece of code. People making suggestions on how to make that boiled
> down code look more pythonic by using the python idiom are IMO not very
> helpfull because essentially those are just aesthetic.

I agree that mere aesthetic changes usually don't address the problem in 
itself. But it's just like posting here in a distant Chinese dialect won't 
do you any good (and neither in a distant, say, Scottish dialect :). If you 
want to get up a helpful communication, you need to use the right language. 
Here it is English and Python :)  and if making my examp

building lists of dictionaries

2006-07-23 Thread Jean_Francois Moulin
Hi all,

I tried this piece of code (FWIW, it was taken as is from a help section of 
mpfit, a mathematical routine for least square fitting):

parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6
parinfo[0]['fixed'] = 1
parinfo[4]['limited'][0] = 1
parinfo[4]['limits'][0]  = 50.

The first line builds a list of six dictionaries with initialised keys.
I expected that the last three lines would only affect the corresponding keys 
of the corresponding dictionnary and that I would end up with a fully 
initialised list where only the 'fixed' key of the first dict would be 1, and 
the first values of limited and limits for dict number 4 would be 1 and 50. 
respectively

This is not so!
I end up with all dictionaries being identical and having their 'fixed' key set 
to 1, and limited[0]==1 and limits[0]==50.

I do not understand this behaviour...

Thanks for helping a newbie.

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


Re: function v. method

2006-07-23 Thread Gerhard Fiedler
On 2006-07-22 16:32:38, danielx wrote:

 ...and source code...
>>>
>>> *shudders* What happened to all the goodness of abstraction?
>>
>> Abstraction as you seem to use it requires complete docs of the interface.
>> Which is what you said you don't have... So the original abstractor broke
>> the abstraction when publishing insufficient docs, not the one who looks
>> into the sources to find out what actually happens.
> 
> Absolutely. I didn't mean the user was breaking abstraction (let's not
> blame the victim). I was saying that we should really have more
> sympathy for him.

I have all the sympathy in the world for him... after all, he's me :)

But one reason why I try to write (and insist on as much as possible from
people writing for or with me) self-documenting code is that wherever you
have documentation and code separated (which is the case of API docs), you
can bet (without losing) that sooner or later code and doc will diverge.
This has a probability that approaches 1 :)

So, I agree with you that good API docs are a good thing, as they tell me
everything I need to know without having to wade through tons of
implementation details that may be interesting but don't serve my immediate
need (of having to use the API). But reality seems to be (and mine so far
definitely is) that these docs, even the good ones, are not completely in
alignment with the reality of the code. (We all know that code has bugs...
and the API always describes, at best, how the code /should/ work. It never
describes how it actually works, including the bugs  (this
notwithstanding the bugs that have been elevated to features and henceforth
been described in the API docs).

So... the final authority /is/ the code. I don't see an alternative. For
me, good abstraction doesn't mean I don't have to read the sources; good
abstraction means (among other things) that I can read the sources easily.

Gerhard

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


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

2006-07-23 Thread Paul Boddie
Antoon Pardon wrote:
>
> Except that if you write your own class from scratch, you can't use
> it as a slice.

Correct, but we were actually discussing subclassing built-in classes
for use as a replacement for range/xrange. :-)

It may be "hard work" writing all those methods in a totally new
range/xrange class, but passing objects of that class around should
prove satisfactory for the use of most programs. I personally doubt
that it is that much hard work, especially if you stick to a reasonable
selection of list capabilities, for example, rather than attempting to
emulate support for every dodgy trick available to the programmer in
the modern CPython arsenal.

> For a language that is supposed to be about duck typing
> I find it strange that if I make my own class with a start, stop and
> step attribute, that python barfs on it when I want to use it as a
> slice.

Yes, my post showed this and gave a reference to where in the CPython
source code the tests for specific types are performed.

Paul

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


Python for Lazarus (not Delphi)

2006-07-23 Thread Uwe Grauer
Does anyone know if something similar to Python for Delphi
does exist for lazarus?

Thanks for any pointers,
Uwe
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Understanding Unicode & encodings

2006-07-23 Thread Jim

[EMAIL PROTECTED] wrote:
> Hello,
>
> For my application, I would like to execute an SQL query like this:
> self.dbCursor.execute("INSERT INTO track (name, nbr, idartist, idalbum,
> path) VALUES ('%s', %s, %s, %s, '%s')" % (track, nbr, idartist,
> idalbum, path))
No, I'll bet that you'd like to run something like
  self.dcCursor.execute("INSERT INTO track (name, nbr, idartist,
idalbum,path) VALUES (%(track)s, %(nbr)s,
%(idartist)s,%(idalbum)s,'%(path)s')",
{'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path})
(only without my typos).  That's an improvment for a number of reasons,
one of which is that the system will quote for you, for instance in
idartist="John's Beer" changing the single quote to two single quotes
to suit SQL.
> Every time I execute this, I get an exception like
> this:
>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xa1 in position
> 64: ordinal not in range(128)
>
> I tried to encode the different variables in many different encodings
> (latin1), but I always get an exception. Where does this ascii codec
> error comes from? How can I simply build this query string?
Some more information may help: is the error returned before or during
the execute call?  If before, then the execute() call is a distraction.
 If during, then what is your dB, what is it's encoding (is the dB
using latin1, or does the dB only accept ascii?), and what are you
using to connect to it?

Jim

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


Re: httplib, threading, wx app freezing after 4 hours

2006-07-23 Thread Mark rainess
[EMAIL PROTECTED] wrote:
> Mark rainess wrote:
> [...]
>> It runs perfectly for about 4 hours, then freezes.
>> I'm stuck. How do I debug this?
> [...]
>> Can anyone suggest techniques to help me learn what is going on.
> 
> By inspection: "errcode" is undefined; I expect you stripped the
> example
> a bit too far. If it is set to something other 200, it looks like you
> loop out.
> 
> You are calling wx.CallAfter() from a different thread than runs the
> GUI.
> Is that documented to be safe? I've read that wxPostEvent() is is the
> call to
> use for this.
> 
> Next thing to try is adding enough logging to tell exactly what
> statement
> hangs.
> 
> 

Thanks guys, I found the problem.

I had screen-saver set to None and power set to blank monitor after 30 
minutes. The problem occurred after the monitor blanked. I remembered I 
re-flashed my bios a few weeks ago. I didn't check the bios 
power-management settings. I'm not going to reboot now to check because 
I have too much stuff open.

I set power to never blank monitor. Now there is no problem.

I added code to monitor for activity and to kill and restart the thread 
if activity stops. Now if power-management kills it, it wakes-up when 
the screen returns.

I think using wx.CallAfter() the way I have is correct. I will check 
that. It does work properly though.

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


Re: httplib, threading, wx app freezing after 4 hours

2006-07-23 Thread bryanjugglercryptographer

Mark rainess wrote:
[...]
> It runs perfectly for about 4 hours, then freezes.
> I'm stuck. How do I debug this?
[...]
> Can anyone suggest techniques to help me learn what is going on.

By inspection: "errcode" is undefined; I expect you stripped the
example
a bit too far. If it is set to something other 200, it looks like you
loop out.

You are calling wx.CallAfter() from a different thread than runs the
GUI.
Is that documented to be safe? I've read that wxPostEvent() is is the
call to
use for this.

Next thing to try is adding enough logging to tell exactly what
statement
hangs.


-- 
--Bryan

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


Understanding Unicode & encodings

2006-07-23 Thread Raphael . Benedet
Hello,

For my application, I would like to execute an SQL query like this:
self.dbCursor.execute("INSERT INTO track (name, nbr, idartist, idalbum,
path) VALUES ('%s', %s, %s, %s, '%s')" % (track, nbr, idartist,
idalbum, path))
where the different variables are returned by the libtagedit python
bindings as Unicode. Every time I execute this, I get an exception like
this:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xa1 in position
64: ordinal not in range(128)

I tried to encode the different variables in many different encodings
(latin1), but I always get an exception. Where does this ascii codec
error comes from? How can I simply build this query string?

Thanks in advance.
Best Regards,
Raphael

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


Re: Coding style

2006-07-23 Thread Antoon Pardon
On 2006-07-21, Gerhard Fiedler <[EMAIL PROTECTED]> wrote:
> On 2006-07-21 09:00:43, Antoon Pardon wrote:
>
>> So we have code with certain shudder characteristics. And instead
>> of trying to help the OP with his problem, some people react
>> to the shudder and come with all sort of comments that might be
>> true if the code as shown was production code, but which totally
>> miss the point the code was illustrating and thus aren't much
>> helpfull.
>
> In general, I'd say that in this case the example was not well-chosen.
> After such a "shudder removal", a poster should IMO review what caused the
> shudder, and rephrase the original problem without the shudder.

The shudder is not with the poster. The poster can't predict how
the readers will react.

> That might
> take a few iterations, but in general, either one or more of the "shudder
> removals" actually address the OP's issue, maybe without any individual
> helper understanding all of the problem (due to incomplete example code),
> or the iterations lead to a code that's "shudder free" and still shows the
> original problem -- now usually in a clearer form, because free of other
> issues that are not relevant to the OP's point.

I doubt that. My experience is that you have more chance of being helped
by people who try to understand where the OP is trying to go form than
from those who react to style issues. I have never seem a bug go away
because someone suggested to write "if Number:" instead of "if Number != 0:"

> E.g. if someone makes a suggestion that is valid with the example code I
> posted but not with the real code I have, I need to post an updated example
> code that introduces a restriction similar to the one I actually have,
> which then correctly invalidates the formerly valid suggestion -- and helps
> getting more helpful responses.

Not necessarily. A valid suggestion doesn't imply a relevant suggestion.

If someone come with code like:

  if Number != 0:

And someone suggests that the python idiom is

  if Number:

Then that suggestion is valid, but it also is irrelevant. A bug doesn't
disappear because the latter is the python idiom for the former.

> This is a process that in the past has
> taught me a lot (not Python, but that's just because I'm too new here :).
> Once you get good at this process, it often helps you find the problem even
> before posting, because boiling code down to what the /real/ problem is can
> show you a lot you otherwise miss.

Sure, but if you do that, you sometimes have code that deviates from
the python idiom because there was originnaly a good reason for, but
that isn't obvious any more because you are temporarily working with
a boiled down piece of code. People making suggestions on how to
make that boiled down code look more pythonic by using the python
idiom are IMO not very helpfull because essentially those are just
aesthetic.

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


Re: [Newbie] List from a generator function

2006-07-23 Thread Peter Otten
Ernesto García García wrote:

> I'm sure there is a better way to do this:
> 
> [random.choice(possible_notes) for x in range(length)]

Note that "generator" has a fixed meaning in Python:
http://www.python.org/dev/peps/pep-0255/

For generators you can use

list(itertools.islice(gen()), length)

What you need then would be a way to turn an ordinary function into a
generator:

>>> def make_gen(fun, *args, **kw):
... def gen():
... while 1:
... yield fun(*args, **kw)
... return gen()
...
>>> from random import choice
>>> from itertools import islice
>>> length = 7
>>> sample = "abcde"
>>> list(islice(make_gen(choice, sample), length))
['e', 'b', 'b', 'a', 'b', 'b', 'a']

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


Re: [Newbie] List from a generator function

2006-07-23 Thread Paul Rubin
Ernesto García García <[EMAIL PROTECTED]> writes:
> >>[random.choice(possible_notes) for x in range(length)]
> 
> > There is at least a better way to ask the question.  The subject has
> > nothing to do with the body of your post.  Or am I missing something?
> 
> Sorry, I should have explained better. I just want to build a fix
> length list made up of elements generated by a function, in this case
> random.choice(). I don't like my list comprehension, because I'm using
> that dumb variable x and the range() list.

Use xrange instead of range.  If you want to do it with no variables,
hmmm:

  from itertools import islice, starmap, repeat
  import random

  possible_notes = range(12)
  length = 9

  print list(islice(starmap(random.choice, repeat((possible_notes,))), length))


>>> ## working on region in file /usr/tmp/python-21885hGZ...
[10, 0, 6, 7, 8, 1, 9, 6, 11]

Maybe you're sorry you asked ;)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Coding style

2006-07-23 Thread Antoon Pardon
On 2006-07-21, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On 21 Jul 2006 12:00:43 GMT, Antoon Pardon <[EMAIL PROTECTED]>
> declaimed the following in comp.lang.python:
>
>> So we have code with certain shudder characteristics. And instead
>> of trying to help the OP with his problem, some people react
>> to the shudder and come with all sort of comments that might be
>> true if the code as shown was production code, but which totally
>> miss the point the code was illustrating and thus aren't much
>> helpfull.
>
>   You miss one factor -- the specification of what the code sample is
> supposed to handle. Showing a sample of code which may reproduce an
> apparent problem, without stating /all/ the requirements of the
> data/process, leaves the reviewer in the state of assuming all the
> conditions are explicit in the code.

No that leaves the reviewer in the state of assuming all *relevant*
conditions are explicit in the code.

> And in the case of your sample --
> that just isn't true. The try/except block is too general, and seems to
> imply that the exception might be raised in either (or both) foo() or
> bar()

Which in this case is totally irrelevant. The try/except block
was not part of the code that would have been posetd to the
newsgroup.

> -- it is not clear that the try/except is being used to detect an
> unstated exit condition requirement.

Again irrelevant. This was an example where someone had trouble
with his code (the try/except statement) which he would then
reduce to a minimal piece of code that would still show the 
trouble some behaviour (Just the then part of an if statement)
which he would then post to the list.

All trouble with the so called original code is totally irrelevant
to the point I was trying to make and the more you try to critisize
it the more you are an illustration of the point I am trying to make.

This remark was just under that example code in my originle
article:

  This code makes the distinction between the three possibilities,
  whether it is a good way or not I won't discuss, this is just
  meant as an illustration. 

What didn't you understand about the code just meant to be an
illustration.

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


Re: Python proficiency test

2006-07-23 Thread Satya Kiran
I managed to take the test,though not it's entirety.
I am still far being proficient in Python,but I really liked the
thought-provoking questions in there.
thanks so much.


Kiran Satya


On 7/23/06, Ian Parker <[EMAIL PROTECTED]> wrote:
> In message <[EMAIL PROTECTED]>, Richard
> Jones <[EMAIL PROTECTED]> writes
> >Kent Johnson wrote:
> >> I recently helped create an on-line Python proficiency test. The
> >> publisher of the test is looking for beta testers to try the test and
> >> give feedback. If you are interested, here is an announcement from the
> >> publisher:
> >
> >Had a look. In between my browser blocking a popup on every page and the
> >registration asking far more details that I felt necessary, I stopped
> >before looking at the actual test.
> >
> >
> >Richard
> >
>
> Likewise, I wanted to see a sample test (for any subject) but couldn't
> do that without a long-winded registration procedure.
>
> Regards
>
> Ian
> --
> Ian Parker
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Newbie] List from a generator function

2006-07-23 Thread Ernesto García García
>>I'm sure there is a better way to do this:
>>
>>[random.choice(possible_notes) for x in range(length)]

> There is at least a better way to ask the question.  The subject has
> nothing to do with the body of your post.  Or am I missing something?

Sorry, I should have explained better. I just want to build a fix length 
list made up of elements generated by a function, in this case 
random.choice(). I don't like my list comprehension, because I'm using 
that dumb variable x and the range() list.

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


Re: function v. method

2006-07-23 Thread Antoon Pardon
On 2006-07-21, fuzzylollipop <[EMAIL PROTECTED]> wrote:
>
> Antoon Pardon wrote:
>
>> Suppose I am writing my own module, I use an underscore, to
>> mark variables which are an implementation detail for my
>> module.
>>
>> Now I need to import an other module in my module and need access
>> to an implementation variable from that module. So now I have
>> variables with an underscore which have two different meanings:
>
> you don't understand what "implementation detail" means, it means it is
> NOT part of the public API and no client code should ever use it.
>
> If you reference _vara in your code and it is in someone elses module
> you don't understand YOU ARE NOT SUPPOSED TO DO THAT!

Why do you assume that in my example the other module is
not understood?

>>   1) This is an implemantation detail of this module, It is the
>>  users of my module who have to be extra carefull using it.
>
> Users of your module should NEVER KNOW any of the _ or __ stuff exists
> to begin with.
>
>>   2) This is an implemantation detail of the other module,
>>  I should be extra carefull using it.
>
> You should NEVER use it.

Well that may be your view, but AFAICS it is not the view of
the python community. Because each time some mechanism is
proposed for real private variable, people oppose it, they
want people to have access to what are supposed to be
private variables.

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


Re: function v. method

2006-07-23 Thread Antoon Pardon
On 2006-07-21, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote:
> Antoon Pardon wrote:
>> On 2006-07-21, fuzzylollipop <[EMAIL PROTECTED]> wrote:
>> 
>>>danielx wrote:
>>>
> (snip)
>>>
>>>
>>>if you prefix with a single underscore, that tells the user, DON'T MESS
>>>WITH ME FROM OUTSIDE! I AM AN IMPLEMENTATION DETAIL!
>> 
>> 
>> Personnaly I don't like this convention. 
>
> To bad for you.

I'll survive.

>> It isn't clear enough.
>
> Oh yes ?
>
>> Suppose I am writing my own module, I use an underscore, to
>> mark variables which are an implementation detail for my
>> module.
>> 
>> Now I need to import an other module in my module and need access
>> to an implementation variable from that module.
>>
>> So now I have
>> variables with an underscore which have two different meanings:
>> 
>>   1) This is an implemantation detail of this module, It is the
>>  users of my module who have to be extra carefull using it.
>> 
>>   2) This is an implemantation detail of the other module,
>>  I should be extra carefull using it.
>
> Either you imported with the "from othermodule import *" form (which you
> shouldn't do), and you *don't* have the implementation of othermodule,
> or your used the "import othermodule" form, in which case it's pretty
> obvious which names belongs to othermodule.

As far as I understand the _name convention is often defended with the
argument that it stands out. Now if you have to go and look at the
import statements to make a disticntion, then it seems that the
way _names standout isn't that usefull.

>> And I find variable starting or ending with an underscore ugly. :-)
>
> Too bad for you. Choose another language then... PHP, Perl, Ruby ?-)

Not a very strong argument. Whether or not someone has a legitimat point
of criticism against Python or some of the conventions used. You can
always reply: Too bad, better choose another language then.

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


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

2006-07-23 Thread Antoon Pardon
On 2006-07-21, Paul Boddie <[EMAIL PROTECTED]> wrote:
> Regardless of whether myslice inherits from object or not, there's no
> persuading the interpreter that it is a genuine slice, and remember
> that we can't subclass slice (for some reason unknown). So, it would
> appear that the interpreter really wants instances from some specific
> set of types (presumably discoverable by looking at list_subscript in
> listobject.c) rather than some objects conforming to some interface or
> protocol, and perhaps it is implemented this way for performance
> reasons.
>
> In any case, in the core of Python some types/classes are more equal
> than others, and for whatever reason the duck typing breaks down - a
> case of "malbik endar" [1] that you just have to be aware of, I
> suppose.

Is there any chance this will iever change?

Is there any chance the start:stop:step notation will ever
be considered an atom?

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


Re: Type signature

2006-07-23 Thread Paddy
Yacao Wang wrote:
> Hi, I'm a newbie to Python. I've recently read some books about this
> language and none of them have answered my question.
> As a dynamically-typed language Python doesn't need any form of type
> signature which makes the syntax very clean and concise. However, type
> signatures are not only a kind of information provided for the compiler, but
> also for the programmer, or more important, for the programmer. Without it,
> we have to "infer" the return type or required agument types of a function,
> and this can't be done without seeing the implementation of it, and
> sometimes it is still difficult to extract the above information even if the
> implementation is available. Haskell can also determine type information
> dynamically, but it still supports and recommends the programming style with
> type signatures, which makes the code very readable and maitainable. As I
> understand, Python relies too much on run-time type-checking, that is,
> whenever you give the wrong type, you just end up with an exception, which
> is logically correct, but not that useful as type signatures.
> Any ideas on this issue?
>
> --
> Alex
>
Hi Yacao/Alex,
Try these:
  "How to duck type? - the psychology of static typing in Ruby"
   http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/100511
  "3-31-04 I'm Over It"

http://66.102.9.104/search?q=cache:6XW473VSflcJ:www.mindview.net/WebLog/log-0053+%2B%22duck+typing%22+%2B+%22static+typing%22+%2Bpython&hl=en&gl=uk&ct=clnk&cd=3&client=firefox-a

It seems that the latent or duck typing, used in dynamic languages is
counter-intuitve to  those from a static typing background.
Nevertheless, it does work, and work well.
- Paddy.

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


Re: [Newbie] List from a generator function

2006-07-23 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Ernesto García García wrote:

> I'm sure there is a better way to do this:
> 
> [random.choice(possible_notes) for x in range(length)]

There is at least a better way to ask the question.  The subject has
nothing to do with the body of your post.  Or am I missing something?

What is `length`?  Do you want unique elements from `possible_notes`? 
Then you could use `random.sample()`.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

[Newbie] List from a generator function

2006-07-23 Thread Ernesto García García
Hi all,

I'm sure there is a better way to do this:

[random.choice(possible_notes) for x in range(length)]

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


Re: Type signature

2006-07-23 Thread [EMAIL PROTECTED]
Yacao Wang wrote:
> Hi, I'm a newbie to Python. I've recently read some books about this
> language and none of them have answered my question.
> As a dynamically-typed language Python doesn't need any form of type
> signature which makes the syntax very clean and concise. However, type
> signatures are not only a kind of information provided for the compiler, but
> also for the programmer, or more important, for the programmer. Without it,
> we have to "infer" the return type or required agument types of a function,

Reset your brain.  Functions don't have required argument types,
anything implementing the interface they use will work.

e.g.:
class a(object):
def getVal(self):
return "a_val"

class b(object):
def getVal(self):
return "b_val"

def someFunc(valObj):
return valObj.getVal().upper()


someFunc can take objects of class a or class b or any other class that
has a getVal method returning something with an upper method (getVal
doesn't even have to return a string as long as what it returns has an
upper method).

a and b don't share an inheritance hierarchy, either.

There is no "type signature", there's just which methods/attributes are
used by the function.

Limiting it to only working on specified types is unnecessarily
restrictive.

> Haskell can also determine type information
> dynamically

Haskell does static type inference at compile time, not dynamic typing.
 It's a completely different programming model (shared with ML, among
others)

> As I
> understand, Python relies too much on run-time type-checking, that is,
> whenever you give the wrong type, you just end up with an exception, which
> is logically correct, but not that useful as type signatures.

Dynamic typing is different from static typing, you're right, but it's
not worse.

You probably want to google for "duck typing" think about the
implications on polymorphism.

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


Re: Python proficiency test

2006-07-23 Thread Ian Parker
In message <[EMAIL PROTECTED]>, Richard 
Jones <[EMAIL PROTECTED]> writes
>Kent Johnson wrote:
>> I recently helped create an on-line Python proficiency test. The
>> publisher of the test is looking for beta testers to try the test and
>> give feedback. If you are interested, here is an announcement from the
>> publisher:
>
>Had a look. In between my browser blocking a popup on every page and the
>registration asking far more details that I felt necessary, I stopped
>before looking at the actual test.
>
>
>Richard
>

Likewise, I wanted to see a sample test (for any subject) but couldn't 
do that without a long-winded registration procedure.

Regards

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