Re: Failing on string exceptions in 2.4

2007-06-14 Thread Fredrik Lundh
Stephen R Laniel wrote:

> End goal being that string exceptions would cause
> compilation to fail. A few times now, I've found myself
> doing
> 
> class SomeClass:
> """docstring"""
> pass
> 
> raise SomeClass, "Some description"
> 
> and I've gotten a weird compiler error about the constructor
> for SomeClass.

weird as in

TypeError: this constructor takes no arguments

?

this has nothing to do with string exceptions (using string objects as 
exception objects), and all to do with the fact that

 raise SomeClass, "Some description"

is equivalent to

 raise SomeClass("Some description")

which of course doesn't work if your class constructor isn't accepting 
any arguments.  this is no different from doing:

 class SomeClass:
 pass

 obj = SomeClass("an argument") # fails with a TypeError

(note that it's the runtime that's complaining, not the compiler)

to fix this, just make sure that your exception class takes an argument 
(the exception value), either by adding an __init__ method, or by 
inheriting from a suitable built-in exception class.



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


Re: python i2c ioctl

2007-06-14 Thread Tim Roberts
luca <[EMAIL PROTECTED]> wrote:
>
> I was trying to make to work directly l i2c with python with the
>calls ioctl. But I have of the problems and I do not succeed to go
>ahead.
>
>[EMAIL PROTECTED] /usr/local/wrap]129# python pcf8591_ioctl.py
>Reading from 4 ch 8 bit A/D converter PCF8591
>Traceback (most recent call last):
>  File "pcf8591_ioctl.py", line 269, in ?
>main()
>  File "pcf8591_ioctl.py", line 229, in main
>if i2c_open () < 0:
>  File "pcf8591_ioctl.py", line 168, in i2c_open
>fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETGET_OUTPUT), 0,
>iomask)
>TypeError: ioctl requires a file or file descriptor, an integer and
>optionally a integer or buffer argument

The error message pretty much tells you the problem.  One of the parameters
is not correct.  Notice this:

>def IO_CFG_WRITE_MODE_VALUE(msb, data_mask, clk_mask):
>   ( (((msb)&1) << 16) | (((data_mask) &0xFF) << 8) | ((clk_mask) &
>0xFF) )
>def IO_CFG_WRITE_MODE_VALUE(msb, data_mask, clk_mask):
>   ( (((msb)&1) << 16) | (((data_mask) &0xFF) << 8) | ((clk_mask) &
>0xFF) )

Key question: what do these functions return?

Answer: nothing.  You compute a value and then throw it away.  Thus:
print IO_CFG_WRITE_MODE_VALUE( 15, 0xfff, 0x0f0 )
This will print "None".  Add a "return" before the expression.

The same thing is true of all of these functions.  You are thinking that a
Python "def" is like a C "#define".  It's not.  It's like a C function.
Nothing is returned unless you return it.

>def _IOC(dir,type,nr,size):
>   (((dir)  << _IOC_DIRSHIFT) |
>   ((type) << _IOC_TYPESHIFT) |
>   ((nr)   << _IOC_NRSHIFT) |
>   ((size) << _IOC_SIZESHIFT))
>
>def _IO(type,nr):
>   _IOC(_IOC_NONE,(type),(nr),0)
>
>def _IOR(type,nr,size):
>   _IOC(_IOC_READ,(type),(nr),sizeof(size))
>
>def _IOW(type,nr,size):
>   _IOC(_IOC_WRITE,(type),(nr),sizeof(size))
>
>def _IOWR(type,nr,size):
>   _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))
>
>def _IOC_DIR(nr):
>   (((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
>def _IOC_TYPE(nr):
>   (((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
>def _IOC_NR(nr):
>   (((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
>def _IOC_SIZE(nr):
>   (((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
>def IOC_IN():
>   (_IOC_WRITE << _IOC_DIRSHIFT)
>
>def IOC_OUT():
>   (_IOC_READ << _IOC_DIRSHIFT)
>def IOC_INOUT():
>   ((_IOC_WRITE|_IOC_READ) << _IOC_DIRSHIFT)
>def IOCSIZE_MASK():
>   (_IOC_SIZEMASK << _IOC_SIZESHIFT)
>def IOCSIZE_SHIFT():
>   (_IOC_SIZESHIFT)

However, there are more problems with this script.  Example:

>#Get the SDA line state
>def i2c_getbit():
>   value=fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_READBITS))
>   if ((value&(I2C_DATA_LINE))==0):
>   return 0
>   else:
>   return 1

This will produce a runtime error, because the name "i2c_fd" is not
defined.  Notice this:

>#Open the GPIOB dev
>def i2c_open():
>   i2c_fd = os.open("/dev/gpiog", os.O_RDWR)
>   iomask = I2C_CLOCK_LINE
>   fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETGET_OUTPUT), iomask)
>   iomask = I2C_DATA_LINE
>   fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETGET_INPUT), iomask)
>   fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETBITS),
>I2C_DATA_LINE)
>   i2c_dir_in()
>   i2c_clk(1)
>   return i2c_fd

The result of the open is bound in a LOCAL variable called "i2c_fd".  As
soon as the function returns, that variable goes away.  You do RETURN the
value, but you don't store it anywhere:

>#PCF8591 address scheme
>#|  1 |  0 |  0 |  1 | A2 | A1 | A0 | R/W |
>#| i2c_fixed | i2c_addr | 1/0 |
>def main ():
>i2c_fixed = 0x09
>i2c_addr  = 0x01
>
>print "Reading from 4 ch 8 bit A/D converter PCF8591"
>
>if i2c_open () < 0:
>print "i2c open error"
>return 1

You should probably make this:

i2c_fd = i2c_open()
if i2c_fd < 0:
print "i2c open error"
return 1

But, of course, that's not enough either; i2c_fd is a local variable in
THIS function as well.  You should change ALL of the functions so that they
accept an fd as the first parameter, and then pass i2c_fd into the
functions here.

Alternatively, you could create a wrapper class to hold the fd and make the
function methods on that class.

>while "azz":
>i2c_start ()

I'm curious.  What do you think "while" statement is doing?  The answer is
that it is an infinite loop; the string "azz" will always be true, so the
while statement will always loop.  I can't guess what you really wanted
here.
-- 
Tim Roberts, [EMAIL PROTECTED]
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert to C/C++?

2007-06-14 Thread Tim Roberts
SpreadTooThin <[EMAIL PROTECTED]> wrote:
>
>I am wondering if someone who knows the implemention of python's time
>could help converting this to c/c++

If you are running on Windows, you can replace this entire thing with one
call to CoCreateGuid, possibly with a call to StringFromGUID to make it
printable.
-- 
Tim Roberts, [EMAIL PROTECTED]
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using Mac OS X CoreGraphics via ctypes

2007-06-14 Thread Diez B. Roggisch
Daniel schrieb:
> I'm trying to implement a routine that converts a PDF document to
> image files using ctypes and the Apple CoreGraphics library as per the
> 'Splitting a PDF File' example on Apple's web site [0]. Unfortunately
> I cannot use the CoreGraphics module used in that example because I'm
> using Python 2.5 (AFAIK that module is only available in the system
> default Python 2.3.5). There are three questions in the code snippet
> below. Each problem area has been commented out in the example so it
> runs through to the end. The code is obviously not complete, but it's
> enough do demonstrate my problems so far.
> 
> BTW, this is the only way I have found to convert a PDF (generated by
> ReportLab) to an image that can be fed into a PyQt (v3) QPrinter
> object. If anyone knows another way to gain access to the system print
> services on Mac OS X using Python please do tell. Oh yes, I'd rather
> not include PyObjC because I'm already packaging PyQt, and that would
> make the resulting app a lot bigger.

> # BEGIN CODE
> from ctypes import cdll, c_void_p
> from ctypes.util import find_library
> 
> cglib = cdll.LoadLibrary(find_library('ApplicationServices'))
> 
> # the next line causes a segfault - what's the right way to do this?
> #GCS_RGB = cglib.kCGColorSpaceGenericRGB()

Usually, things in the OSX lib that start with k* are a constant - not a 
function. As is this.


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


Re: FTP Date Format Function

2007-06-14 Thread billiejoex
On 14 Giu, 19:25, samuraisam <[EMAIL PROTECTED]> wrote:
> FTP LST/LIST/NLST date field formatting function for all those seekers
> out there...
>
> import time
> import datetime
>
> def ftpdateformat(value):
> """Formats dates from most FTP servers"""
> if ":" in value: # within 6 months
> return datetime.datetime(
> *time.strptime( # have to guess this calculation
> "%s %s" % (value, datetime.datetime.now().year),
> "%b %d %H:%M %Y"
> )[0:5]
> ).strftime("%B %d, %Y %H:%M")
> else: # before six months
> return datetime.datetime(
> *time.strptime(value, "%b %d %Y")[0:5]
> ).strftime("%B %d, %Y")
>
> I'm not sure if there is a proper algorithm for deciding on a proper
> year within the last 6 months as it isn't given by most FTP servers.
> I'd love to amend the function with the correct solution. :)

I didn't well understand your question, anyway...

- FTP got no LST command. What's that?
- NLST should return filenames only: returned output doesn't contain
file sizes, last modification time values or whatever.
- RFC959 gives no specifications about *how* LIST command output
should be formatted. Depending on the type of server you're talking to
you could find unix/"ls -l"-like format outputs, DOS-like ones or
something completely different and your code does not cover all of
them. Take a look at: http://effbot.org/downloads/#ftpparse

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


Re: FTP Date Format Function

2007-06-14 Thread MC
Thank.







-- 
@-salutations

Michel Claveau


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


Re: Python rocks

2007-06-14 Thread Carl Banks
On Jun 3, 11:56 am, [EMAIL PROTECTED] (Alex Martelli) wrote:
> Allowing a trailing ! in method names has no such cost, because in no
> language I know is ! used as a "postfix unary operator";

Some math oriented languages use it as the factorial function.  E.g.,
Mathematica:

In[1] := 10!

Out[1]= 3628800


(The more you know)

Carl Banks

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


Re: Convert to C/C++?

2007-06-14 Thread Douglas Wells
In article <[EMAIL PROTECTED]>,  SpreadTooThin <[EMAIL PROTECTED]> writes:
> I am wondering if someone who knows the implemention of python's time
> could help converting this to c/c++

First the obligatory sermon:

If you are truly intending to utilize a UUID generator on a UNIX
platform, I urge you to seek out and use a native implementation.
Both Linux and FreeBSD distributions include such a capability,
and there are also functions available in freely available libraries.
Such a native function would offer several advantages over the
code you are attempting to copy:

   1) it could utilize the full capability of the clock rather
  than be limited to the microseconds precision in gettimeofday;
   2) it is probably implemented utilizing a system locking function
  to provide a truly unique-per-system UID;
   3) it would almost certainly incorporate a per-machine unique
  identifier, as called for by the spec, and which is missing
  from your prototype code;
   4) it would have easier access to better pseudo-random number
  generators;
   4) it would probably be coded better than the code you provide,
  which squanders quite a bit of precision (I haven't counted
  exactly, but I would guess about 10 bits worth);
   
Also, it sounds as though you are not comfortable coding in C (as
most of the information that you seek is straight-forwardly available
from the Python and UNIX documentation), and there are numerous
subtleties in the code that you must develop:
   1) the management of endianness will require careful attention
  to your use of this function;
   2) conversion to and from 64-bit numbers requires attention to
  the suffixes.

With that said;

Python doesn't define the time epoch except on UNIX platforms, but
it's almost certainly the same as on UNIX.  So,

time.time() is equivalent to:
double time_time (void) { struct timeval tv;
gettimeofday (&tv, (void *) NULL);
return tv.tv_sec + tv.tv_usec / 1e6); }

random.randrange(value) is approximately equivalent to:
long random_randrange (unsigned long stop) {
return rand () % stop; }
 - with the caveats that:
1) you should choose a better pseudo-random number generator
   than rand();
2) you should be sure to initialize the random function, e.g.,
   via srand();

>   nanoseconds = int(time.time() * 1e9)
>   # 0x01b21dd213814000 is the number of 100-ns intervals between the
>   # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
>   self.timestamp = int(nanoseconds/100) + 0x01b21dd213814000L
>   self.clock_seq = random.randrange(1<<14L) # instead of stable storage
>   self.time_low = self.timestamp & 0xL
>   self.time_mid = (self.timestamp >> 32L) & 0xL
>   self.time_hi_version = (self.timestamp >> 48L) & 0x0fffL
>   self.clock_seq_low = self.clock_seq & 0xffL
>   self.clock_seq_hi_variant = (self.clock_seq >> 8L) & 0x3fL

You should look up the definition of the various fields (e.g.,
clock_seq, time_low, time_mid) in a UUID specification, for example
IETF RFC 4122.  They have precise width requirements.

>   #print 'timestamp ', self.timestamp, self.time_low, self.time_mid, 
> self.time_hi_version
>   #print 'clock_seq ', self.clock_seq, self.clock_seq_low, 
> self.clock_seq_hi_variant
> 
> vs unix gettimeofday
> 
> int gettimeofday(struct timeval *tp, struct timezone *tzp);

By the way, the UNIX gettimeofday function does not include timezone:
the prototype of the second argument is "void *" and must be passed
as NULL.

Good luck, - dmw

-- 
.   Douglas Wells .  Connection Technologies  .
.   Internet:  -sp9804- -at - contek.com- .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where can I suggest an enchantment for Python Zip lib?

2007-06-14 Thread Tim Roberts
durumdara <[EMAIL PROTECTED]> wrote:
>
>The problem, that:
> - I want to process 100-200 MB zip files.
> - I want to abort in process
> - I want to know the actual position
> - I want to slow the operation sometimes!
>
>Why I want to slow?
>
>The big archiving is slow operation. When it is slow, I want to
>working with other apps while it is processing.
>
>So I want to slow the zipping with time.sleep, then the background
>thread is not use the full CPU...
>I can work with other apps.

Surely a more intelligent (and much easier) solution would be to reduce the
priority of your archiving process.  Your  operating system is far better
equipped to share the CPU than you are.
-- 
Tim Roberts, [EMAIL PROTECTED]
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inverting a dictionary of lists

2007-06-14 Thread Raymond Hettinger
On Jun 14, 8:20 pm, [EMAIL PROTECTED] wrote:
> I have a very large dictionary of lists:
> d = {a:[1,2], b:[2,3], c:[3]}
> and i want to reverse the associativity of the integers thusly:
> inverse(d)   makes   {1:[a], 2:[a,b], 3:[b,c]}



Try using setdefault:

>>> d = {'a':[1,2], 'b':[2,3], 'c':[3]}
>>> r = {}
>>> for k in d:
for e in d[k]:
r.setdefault(e, []).append(k)

>>> r
{1: ['a'], 2: ['a', 'b'], 3: ['c', 'b']}


Raymond

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


Re: Python rocks

2007-06-14 Thread Aahz
In article <[EMAIL PROTECTED]>,
George Sakkis  <[EMAIL PROTECTED]> wrote:
>On Jun 2, 4:58 pm, [EMAIL PROTECTED] (Aahz) wrote:
>> In article <[EMAIL PROTECTED]>,
>> George Sakkis  <[EMAIL PROTECTED]> wrote:
>>>
>>>- Strings being iterable; unfortunately this will stay in Py3K.
>>
>> I'll repeat the comment I made on python-3000:
>>
>> "...string iteration isn't about treating strings as sequences of
>> strings, it's about treating strings as sequences of characters.  The
>> fact that characters are also strings is the reason we have problems,
>> but characters are strings for other good reasons."
>
>No, the reason we have problems is that far more often than not
>strings are treated as atomic values, not sequences of smaller strings
>or characters. A classic example is flatten(), where most people are
>surprised if flatten([1, (3.14, 'hello')]) returns [1, 3.14, 'h', 'e',
>'l', 'l', 'o'].

Enh.  That's not my experience -- the ability to slice, dice, and
concatenate strings is intrinsic to their usefulness.

>> Thing is, the fact that you can e.g. slice strings just like other
>> sequence types creates the consequence that you can also iterate over
>> strings -- moreover, some of us actually do iterate over strings (though
>> of course we could if necessary create lists/tuples of characters).  In
>> the grand scheme of things, I rarely see people running into problems
>> with iterating over strings.
>
>One class of problems is functions such as flatten() that expect "a
>collection or an atom", with strings being typically considered
>atomic. A second common pitfall are functions that expect file-like
>objects but are given file names instead:
>
>def process_file(input_file):
>for line in input_file:
>do_stuff(line)
>
>process_file('/home/george/.bashrc')  # oops

That's a problem, yes, and it has oddly enough gotten worse since
iterators were introduced into Python.  Nevertheless, I have yet to see
anyone suggest a mechanism for fixing this particular gotcha without
creating more problems.  Strings are just too useful as sequences.

Moreover, my experience is that these kinds of problems don't show up
all that frequently in practice.
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inverting a dictionary of lists

2007-06-14 Thread Gabriel Genellina
En Fri, 15 Jun 2007 00:20:33 -0300, <[EMAIL PROTECTED]> escribió:

> I searched for an hour and don't see a solution to this (i assume
> somewhat common) problem.
>
> I have a very large dictionary of lists:
> d = {a:[1,2], b:[2,3], c:[3]}
> and i want to reverse the associativity of the integers thusly:
> inverse(d)   makes   {1:[a], 2:[a,b], 3:[b,c]}
>
> my solution expands the original dict into two lists of keys and list
> elements:
> list1:  [a,a,b,b,c]
> list2:  [1,2,2,3,3]
> then recombines them with the reverse operation.
>
> but this takes too much time and a lot of memory.
> I wonder if anyone can point me to a more efficient solution?

py> d = dict(a=[1,2], b=[2,3], c=[3])
py> result = {}
py> for k,v in d.iteritems():
...   for item in v:
... result.setdefault(item, []).append(k)
...
py> result
{1: ['a'], 2: ['a', 'b'], 3: ['c', 'b']}
py>

You may use collections.defaultdict too - search some recent posts.

-- 
Gabriel Genellina

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


inverting a dictionary of lists

2007-06-14 Thread bpowah
I searched for an hour and don't see a solution to this (i assume
somewhat common) problem.

I have a very large dictionary of lists:
d = {a:[1,2], b:[2,3], c:[3]}
and i want to reverse the associativity of the integers thusly:
inverse(d)   makes   {1:[a], 2:[a,b], 3:[b,c]}

my solution expands the original dict into two lists of keys and list
elements:
list1:  [a,a,b,b,c]
list2:  [1,2,2,3,3]
then recombines them with the reverse operation.

but this takes too much time and a lot of memory.
I wonder if anyone can point me to a more efficient solution?

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


using Mac OS X CoreGraphics via ctypes

2007-06-14 Thread Daniel
I'm trying to implement a routine that converts a PDF document to
image files using ctypes and the Apple CoreGraphics library as per the
'Splitting a PDF File' example on Apple's web site [0]. Unfortunately
I cannot use the CoreGraphics module used in that example because I'm
using Python 2.5 (AFAIK that module is only available in the system
default Python 2.3.5). There are three questions in the code snippet
below. Each problem area has been commented out in the example so it
runs through to the end. The code is obviously not complete, but it's
enough do demonstrate my problems so far.

BTW, this is the only way I have found to convert a PDF (generated by
ReportLab) to an image that can be fed into a PyQt (v3) QPrinter
object. If anyone knows another way to gain access to the system print
services on Mac OS X using Python please do tell. Oh yes, I'd rather
not include PyObjC because I'm already packaging PyQt, and that would
make the resulting app a lot bigger.

# BEGIN CODE
from ctypes import cdll, c_void_p
from ctypes.util import find_library

cglib = cdll.LoadLibrary(find_library('ApplicationServices'))

# the next line causes a segfault - what's the right way to do this?
#GCS_RGB = cglib.kCGColorSpaceGenericRGB()
#cs = cglib.CGColorSpaceCreateWithName(GCS_RGB)

# the next line causes the following error:
#CGPDFDocumentRef = cglib.CGPDFDocumentRef
# AttributeError: dlsym(0x1018c0, CGPDFDocumentRef): symbol not found

CGPDFDocumentCreateWithProvider =
cglib.CGPDFDocumentCreateWithProvider
CGPDFDocumentCreateWithProvider.restype = c_void_p #CGPDFDocumentRef

provider = cglib.CGDataProviderCreateWithFilename("sample.pdf")
pdf = CGPDFDocumentCreateWithProvider(provider)

#for page in xrange(1, pdf.getNumberOfPages() + 1):
#print page
# pdf.getNumberOfPages caues the following error:
# AttributeError: 'int' object has no attribute 'getNumberOfPages'
# I presume the 'pdf' object is a pointer. How do I get a real
# CGPDFDocuemnt instance that has a getNumberOfPages method?

cglib.CGPDFDocumentRelease(pdf)
# END CODE

[0] Splitting a PDF File 

Thanks in advance for any hints you can provide.

~ Daniel

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


Re: dynamically generated runtime methods & reflection

2007-06-14 Thread Josiah Carlson
Josiah Carlson wrote:
> Well, the particular operation is typically called 'currying a 
> function', and unless you know what to look for, it isn't very easy to 
> make happen.

Replace "make happen" to "discover in the standard library".

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


Re: save class

2007-06-14 Thread Josiah Carlson
nik wrote:
> of having to keep track of a separate dictionary file. I am new to
> this, but I thought that this would be a regular thing to do in
> python, because people must make classes in the interactive console
> and then export them somehow for later use.

Create a file.  Put your code in it.  Run your code.  Occasionally 
copy/paste your code into the console for testing and/or learning about 
how Python works.  If you write something you want to keep in the 
console, copy it out of the console and paste it into your source file(s).

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


Re: SimplePrograms challenge

2007-06-14 Thread Steve Howell

--- Joe Riopel <[EMAIL PROTECTED]> wrote:

> How about this one for recursion and control flow:
> 
> >>> def hcd(m,n):
> ... r = m % n
> ... if( r > 0 ):
> ... hcd(n, r)
> ... else:
> ... print "hcd = %d" % (n,)
> ...
> >>> hcd(119, 544)
> hcd = 17
> >>>
> 
> It calculates the highest common denominator for m
> and n. Plus it's E1
> in TAoCP by Knuth.
> 

I'm a little wary of having the page contain too many
mathematical examples, although I admit that I've
already included Fibonacci and Eratosthenes.  (And
your example shares in common with those two that it's
a classic example.)

Feel free to post your example on the page, but please
use slightly more descriptive identifiers (e.g. change
hcd to highest_common_denominator or
largest_common_denominator), and also have the program
print out the results.

Thanks.

-- Steve




   

Yahoo! oneSearch: Finally, mobile search 
that gives answers, not web links. 
http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SimplePrograms challenge

2007-06-14 Thread Joe Riopel
How about this one for recursion and control flow:

>>> def hcd(m,n):
... r = m % n
... if( r > 0 ):
... hcd(n, r)
... else:
... print "hcd = %d" % (n,)
...
>>> hcd(119, 544)
hcd = 17
>>>

It calculates the highest common denominator for m and n. Plus it's E1
in TAoCP by Knuth.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with regular expressions

2007-06-14 Thread Jay Loden


Carlos Luis Pérez Alonso wrote:
> I have the next piece of code:
>  
> 
>  if re.search('^(taskid|bugid):\\d+',logMessage):
> return 0 
> else:
> sys.stderr.write("El comentario tiene que contener el taskid: o 
> el bugid:")
> return 1
> -
>  
> The regular exprexión is "^(taskid|bugid):\\d+"
>  
> Mi problem is that if logMessage == "taskid:234" the regular expression 
> matched teorically, but the result is always "None" and the function returns 
> 1.
>  
> ¿Does anybody has an idea of what is happening here?
>  
> Thanks

I'm pretty sure it's the escaping on your regular expression. You should use 
the raw string operator:

-

#!/usr/bin/python

import re,sys

def checkLogMsg(logMessage):
  if re.search(r'^(taskid|bugid):\d+', logMessage):
return 0
  else:
sys.stderr.write("El comentario tiene que contener el taskid: o el 
bugid:")
return 1


print checkLogMsg('taskid:234')
print checkLogMsg('doesnotmatch')

-

Outputs:
$ python test_reg.py 
0
El comentario tiene que contener el taskid: o el bugid:1


HTH, 

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


Re: poking around a running program

2007-06-14 Thread Gabriel Genellina
En Thu, 14 Jun 2007 21:42:50 -0300, Paul Rubin  
<"http://phr.cx"@NOSPAM.invalid> escribió:

> I have a long-running program that has lots of net connections open on
> separate threads.  I'm twiddling my thumbs waiting for it to finish
> (each run takes maybe an hour) and although I'm logging various info
> that I can monitor as the run progresses, it would be cool to be able
> to actually poke around the program's data while it's running.
>
> I'm thinking of adding a "console" thread that would basically be a
> read-eval-print loop that I could use to inspect data interactively.
> Maybe it would just eval stuff or maybe it would use pdb.  Of course
> it would be a bit dangerous since I could mutate things with it but I
> guess it would be enough to just be careful not to do that.
>
> Is this kind of thing common?  Has it been done before and are there
> some recipes?  I did a quick search and didn't find anything obvious.
> One thing I'm wondering is how to examine the internal state of
> iterators, especially the C implementations from itertools.

I don't know how common it is, but for instance, Zope has a remote console  
like this.
The code module may be useful.

> More advanced might be some kind of hot-patching scheme (to be able to
> change the code without stopping it) but for now I think I'm ok just
> restarting the program every so often, modifying the code between runs
> based on what happens in a given run.

That may be hard to do. For global functions, reload() may help, but if  
you have many created instances, modifying and reloading the module that  
contains the class definition won't automatically alter the existing  
instances.

-- 
Gabriel Genellina

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


Re: Problems with regular expressions

2007-06-14 Thread Gabriel Genellina
En Thu, 14 Jun 2007 21:42:02 -0300, Carlos Luis Pérez Alonso  
<[EMAIL PROTECTED]> escribió:

> I have the next piece of code:
> 
>  if re.search('^(taskid|bugid):\\d+',logMessage):
> return 0
> else:
> sys.stderr.write("El comentario tiene que contener el  
> taskid: o el bugid:")
> return 1
> -
> The regular exprexión is "^(taskid|bugid):\\d+"
> Mi problem is that if logMessage == "taskid:234" the regular expression  
> matched teorically, but the result is always "None" and the function  
> returns 1.

For "taskid:234" it matches ok:

py> re.search('^(taskid|bugid):\\d+',"taskid:1234")
<_sre.SRE_Match object at 0x00ACB1E0>
py> re.search('^(taskid|bugid):\\d+',"asdfa fa taskid:1234")
py> re.search('^(taskid|bugid):\\d+'," taskid:1234")
py> re.search('^(taskid|bugid):\\d+',"taskid:123adfa asfa lkjljlj")
<_sre.SRE_Match object at 0x00ACB8A0>

Because of the ^, it *only* matches at the start of the line. If you want  
a match anywhere (as implied by your error message), just remove the ^.

-- 
Gabriel Genellina

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


Re: SimplePrograms challenge

2007-06-14 Thread Steve Howell

--- rzed <[EMAIL PROTECTED]> wrote:
> Hm
> 
> ... and so on.
> 
> In much of Python's documentation, and in this case,
> an occasional 
> working example of use would go FAR in aiding
> understanding of the 
> underlying concept.
> 

I agree with your sentiment 100%.  Feel free to change
the SimplePrograms page to add any clarity, but as the
other Steve pointed out, I did in fact add a driver.

To your larger point...I have argued in another thread
that the Python documentation needs to have a more
flexible structure that allows for better posting of
example code.  The current modules documentation is
optimized for the case that a module provides a sort
of integrated solution to a particular poblem, so
there is just one examples page per module, but some
modules (e.g. itertools) are really just a grab-bag of
functions, so they could be documented better by
one-or-more example per function.

Sorry for not putting this post in better context, but
I think we are on to the same idea...maybe you can
elaborate on your own point better than I did just
now...

-- Steve




 

Food fight? Enjoy some healthy debate 
in the Yahoo! Answers Food & Drink Q&A.
http://answers.yahoo.com/dir/?link=list&sid=396545367
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Failing on string exceptions in 2.4

2007-06-14 Thread Gabriel Genellina
En Thu, 14 Jun 2007 18:45:08 -0300, Stephen R Laniel <[EMAIL PROTECTED]>  
escribió:

> Reading the Python docs, it looks like string exceptions
> will be a DeprecationWarning in Python 2.5. Is there any way
> to make them so in 2.4? Now how about if I want to turn all
> DeprecationWarnings into compile-time errors? Is there some
> way to do this?

Yes, using the warnings module.
First thing is to start Python using the -Wd argument so you can see ALL  
the warnings, even the supressed ones:

C:\temp>python24 -Wd

Then see what happens when you raise a string exception:

py> raise "Error"
__main__:1: PendingDeprecationWarning: raising a string exception is  
deprecated
Traceback (most recent call last):
   File "", line 1, in ?
Error

Now we know it is a PendingDeprecationWarning, and the message says  
"raising a string exception is deprecated"
With this information we can build a filter. Do this at startup of your  
program:

 from warnings import filterwarnings
filterwarnings(action="error", message="raising a string exception",  
category=PendingDeprecationWarning)

Or maybe, place it in sitecustomize.py.
Or, start Python using -W:

C:\temp>python24 -W "error:raising a string  
exception:PendingDeprecationWarning"
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on  
win32
Type "help", "copyright", "credits" or "license" for more information.
py> raise "Error"
Traceback (most recent call last):
   File "", line 1, in ?
   File "c:\apps\python\lib\warnings.py", line 61, in warn
 warn_explicit(message, category, filename, lineno, module, registry,
   File "c:\apps\python\lib\warnings.py", line 96, in warn_explicit

PendingDeprecationWarning: raising a string exception is deprecated

(btw, the right format for the -W option isn't documented, or at least  
I've been unable to find it; each time I want to use -W I have to reread  
the warnings module source...)

> End goal being that string exceptions would cause
> compilation to fail. A few times now, I've found myself
> doing
>
> class SomeClass:
> """docstring"""
> pass
>
> raise SomeClass, "Some description"
>
> and I've gotten a weird compiler error about the constructor
> for SomeClass. I'd prefer it just to fail there and not let
> me raise an exception that isn't subclassed beneath
> Exception.

The compiler won't complain here - you will have to inspect the code (or  
use a tool like pylint or pychecker). The easy fix is just inherit from  
Exception.

-- 
Gabriel Genellina

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


Re: SimplePrograms challenge

2007-06-14 Thread Steve Howell

--- Steven Bethard <[EMAIL PROTECTED]> wrote:

> > How about including a driver?
> 
> Yes, absolutely a good idea. Fortunately, the other
> Steve borrowed the 
> time machine already and added this to the end::
> 
>  for p in iter_primes():
>  if p > 1000: break
>  print p
> 

I think rzed (sorry for bad quoting) was
(partly)making the point that we could have more
explicitly showed how .next() worked.  I'm fine with
the current example, because I think the examples
should solve problems at face value, and here it's
very natural to use a loop with an iterator/generator
(never mind the underlying magic), but I do think some
future example should show how one can have more
control over getting values from an iterator.





   

Looking for a deal? Find great prices on flights and hotels with Yahoo! 
FareChase.
http://farechase.yahoo.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Questions about mathematical and statistical functionality in Python

2007-06-14 Thread Josh Gilbert
On Thursday 14 June 2007 5:54 pm, Tim Churches wrote:
> Michael Hoffman wrote:
> > Talbot Katz wrote:
> >> I hope you'll indulge an ignorant outsider.  I work at a financial
> >> software firm, and the tool I currently use for my research is R, a
> >> software environment for statistical computing and graphics.  R is
> >> designed with matrix manipulation in mind, and it's very easy to do
> >> regression and time series modeling, and to plot the results and test
> >> hypotheses.  The kinds of functionality we rely on the most are standard
> >> and robust versions of regression and principal component / factor
> >> analysis, bayesian methods such as Gibbs sampling and shrinkage, and
> >> optimization by linear, quadratic, newtonian / nonlinear, and genetic
> >> programming; frequently used graphics include QQ plots and histograms.
> >> In R, these procedures are all available as functions (some of them are
> >> in auxiliary libraries that don't come with the standard distribution,
> >> but are easily downloaded from a central repository).
> >
> > I use both R and Python for my work. I think R is probably better for
> > most of the stuff you are mentioning. I do any sort of heavy
> > lifting--database queries/tabulation/aggregation in Python and load the
> > resulting data frames into R for analysis and graphics.
>
> I would second that. It is not either/or. Use Python, including Numpy
> and matplotlib and packages from SciPy, for some things, and R for
> others. And you can even embed R in Python using RPy - see
> http://rpy.sourceforge.net/
>
> We use the combination of Python, Numpy (actually, the older Numeric
> Python package, but soon to be converted to Numpy), RPy and R in our
> NetEpi Analysis project - exploratory epidemiological analysis of large
> data sets - see http://sourceforge.net/projects/netepi - and it is a
> good combination - Python for the Web interface, data manipulation and
> data heavy-lifting, and for some of the more elementary statistics, and
> R for more involved statistical analysis and graphics (with teh option
> of using matplotlib or other Python-based graphics packages for some
> tasks if we wish). The main thing to remember, though, is that indexing
> is zero-based in Python and 1-based in R...
>
> Tim C

Thirded. I use R, Python, Matlab along with other languages (I hate pipeline 
pilot) in my work and from what I've seen nothing can compare with R when it 
comes to stats. I love R, from its brilliant CRAN system (PyPI needs serious 
work to be considered in the same class as CPAN et al) to its delicious Emacs 
integration. 

I just wish there was a way to distribute R packages without requiring the 
user to separately install R. 

In a similar vein, I wish there was a reasonable Free Software equivalent to 
Spotfire. The closest I've found (and they're nowhere near as good) are 
Orange (http://www.ailab.si/orange) and WEKA 
(http://www.cs.waikato.ac.nz/ml/weka/). Orange is written in Python, but its 
tied to QT 2.x as the 3.x series was not available on Windows under the GPL. 


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


Problems with regular expressions

2007-06-14 Thread Carlos Luis Pérez Alonso
I have the next piece of code:
 

 if re.search('^(taskid|bugid):\\d+',logMessage):
return 0 
else:
sys.stderr.write("El comentario tiene que contener el taskid: o el 
bugid:")
return 1
-
 
The regular exprexión is "^(taskid|bugid):\\d+"
 
Mi problem is that if logMessage == "taskid:234" the regular expression matched 
teorically, but the result is always "None" and the function returns 1.
 
¿Does anybody has an idea of what is happening here?
 
Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OS X install confusion

2007-06-14 Thread 7stud
On Jun 14, 11:21 am, [EMAIL PROTECTED] (John Fisher) wrote:
> Hi Groupies,
>
> I have an Intel Macbook running OS X 10.4.
>
> It came installed with Python 2.3.5. I have since installed MacPython
> with version 2.4.4, cool.
>
> When I open a bash terminal session and type python, it brings up
> version 2.3.5. If I type IDLE it brings up version 2.4.4.
>
> My question: what do I have to do to get it to bring up 2.4.4 with the
> "python" command?
>
> Thanks for bringing light to my ignorance.
>
> JF

Strange.  I installed macpython 2.4.4 on an imac this year, and when I
type python on the command line of a bash shell, python 2.4.4 starts
up:

$ python
Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>


I looked in:

/System/Library/Frameworks/Python.framework/Versions/Current/bin/

and inside that directory are the programs:

idle
pydoc
python
python2.3

So I tried typing python2.3 on the command line, and lo and behold
python 2.3.5 started up:

$ python2.3
Python 2.3.5 (#1, Jul 25 2006, 00:38:48)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

So it looks like the macpython install changed the name of the pre-
installed python program from python to python2.3.  What do you see in
that directory?


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


poking around a running program

2007-06-14 Thread Paul Rubin
I have a long-running program that has lots of net connections open on
separate threads.  I'm twiddling my thumbs waiting for it to finish
(each run takes maybe an hour) and although I'm logging various info
that I can monitor as the run progresses, it would be cool to be able
to actually poke around the program's data while it's running.  

I'm thinking of adding a "console" thread that would basically be a
read-eval-print loop that I could use to inspect data interactively.
Maybe it would just eval stuff or maybe it would use pdb.  Of course
it would be a bit dangerous since I could mutate things with it but I
guess it would be enough to just be careful not to do that.

Is this kind of thing common?  Has it been done before and are there
some recipes?  I did a quick search and didn't find anything obvious.
One thing I'm wondering is how to examine the internal state of
iterators, especially the C implementations from itertools.

More advanced might be some kind of hot-patching scheme (to be able to
change the code without stopping it) but for now I think I'm ok just
restarting the program every so often, modifying the code between runs
based on what happens in a given run.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OS X install confusion

2007-06-14 Thread Paul McNett
John Fisher wrote:
> Ted <[EMAIL PROTECTED]> wrote:
> 
>> On Jun 14, 1:31 pm, Kevin Walzer <[EMAIL PROTECTED]> wrote:
>>> John Fisher wrote:
 Hi Groupies,
 I have an Intel Macbook running OS X 10.4.
 It came installed with Python 2.3.5. I have since installed MacPython
 with version 2.4.4, cool.
 When I open a bash terminal session and type python, it brings up
 version 2.3.5. If I type IDLE it brings up version 2.4.4.
 My question: what do I have to do to get it to bring up 2.4.4 with the
 "python" command?
 Thanks for bringing light to my ignorance.
 JF
>>> Sounds like a path problem. Apple's system Python is installed in
>>> /usr/bin. Your installation is probably in /usr/local/bin. Edit your
>>> profile or use the full path.
>>>
>>> --
>>> Kevin Walzer
>>> Code by Kevinhttp://www.codebykevin.com
>> The default python on tiger (2.3.5) is sym-linked to /usr/bin/python
>> and /usr/bin/pythonw.
>>
>> I found it easier to relink to the new installation path. This also
>> leaves /usr/bin/python23 and /usr/bin/pythonw23 still linked to the
>> original version if you want to quickly check something.
>>
>> Cheers,
>> Ted
> 
> OK, please give a little more information how I can accomplish this
> "re-link".

Your Python 2.5 is likely installed here:

/Library/Frameworks/Python.framework/Versions/Current/bin

But OS X comes with a "system Python", version 2.3.5, likely installed here:

/usr/bin

If you look at /usr/bin, you'll see:

lrwxr-xr-x   1 root  wheel  9 Jan 31 17:24 python -> python2.3
lrwxr-xr-x   1 root  wheel 72 Jan 31 17:24 python2.3 -> 
../../System/Library/Frameworks/Python.framework/Versions/2.3/bin/python
lrwxr-xr-x   1 root  wheel 10 Jan 31 17:24 pythonw -> pythonw2.3
-rwxr-xr-x   1 root  wheel  29704 Aug 19  2006 pythonw2.3

So, python is linked to python2.3, and python2.3 is in turn linked to 
/System/Library/Frameworks/Python.framework/Versions/2.3/bin/python

You need to (warning: watch for line wrap):

sudo -s

cd /usr/bin
ln -s /Library/Frameworks/Python.framework/Versions/Current/bin/python 
python_current
ln -s /Library/Frameworks/Python.framework/Versions/Current/bin/pythonw 
pythonw_current
rm python
rm pythonw
ln -s python python_current
ln -s pythonw pythonw_current

However, that isn't what I did. I like the system being able to find and 
use the system-installed python, but I like my scripts to use the python 
version I installed (2.5). To get that, skip the above symlinking and 
instead edit your .bash_profile file (hidden file inside your home 
directory) and put these lines at the top:

PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}
export PATH

-- 
pkm ~ http://paulmcnett.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OS X install confusion

2007-06-14 Thread John Fisher
Ted <[EMAIL PROTECTED]> wrote:

> On Jun 14, 1:31 pm, Kevin Walzer <[EMAIL PROTECTED]> wrote:
> > John Fisher wrote:
> > > Hi Groupies,
> >
> > > I have an Intel Macbook running OS X 10.4.
> >
> > > It came installed with Python 2.3.5. I have since installed MacPython
> > > with version 2.4.4, cool.
> >
> > > When I open a bash terminal session and type python, it brings up
> > > version 2.3.5. If I type IDLE it brings up version 2.4.4.
> >
> > > My question: what do I have to do to get it to bring up 2.4.4 with the
> > > "python" command?
> >
> > > Thanks for bringing light to my ignorance.
> >
> > > JF
> >
> > Sounds like a path problem. Apple's system Python is installed in
> > /usr/bin. Your installation is probably in /usr/local/bin. Edit your
> > profile or use the full path.
> >
> > --
> > Kevin Walzer
> > Code by Kevinhttp://www.codebykevin.com
> 
> The default python on tiger (2.3.5) is sym-linked to /usr/bin/python
> and /usr/bin/pythonw.
> 
> I found it easier to relink to the new installation path. This also
> leaves /usr/bin/python23 and /usr/bin/pythonw23 still linked to the
> original version if you want to quickly check something.
> 
> Cheers,
> Ted

OK, please give a little more information how I can accomplish this
"re-link".

Thanks,

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


Re: save class

2007-06-14 Thread Gabriel Genellina
En Thu, 14 Jun 2007 16:05:14 -0300, nik <[EMAIL PROTECTED]> escribió:

> On Jun 13, 10:04 pm, Josiah Carlson <[EMAIL PROTECTED]>
> wrote:
>> Gabriel Genellina wrote:
>> > En Wed, 13 Jun 2007 23:11:22 -0300, nik <[EMAIL PROTECTED]> escribió:

>> >> It would seem that I want to actually save the source code for the
>> >> class. I know that I could of course open up an editor and just make
>> >> it, but my ideal would be to have the base class, Map, be able to  
>> make
>> >> the sub-classes. I don't want the class definition. What I want is an
>> >> actual class that I could later import and use somewhere else. I am
>> >> planning to have each one of these map objects contain a different
>> >> dictionary and then be able to import the map into the application,
>> >> but have certain methods defined in the Map super-class to draw data
>> >> out of the specific map's specific dictionary. I hope that makes
>> >> sense.
>> > And are you sure you actually need different subclasses? Will you
>> > construct them several instances of each subclass? From the above
>> > description I feel you want just different Map *instances*, each with
>> > its own dict, not different *subclasses*.
>>
>> What you said, and that his solution sounds like a Java approach to the
>> problem (subclass an abstract base class that calls specific methods on
>> the subclass to "do the right thing").
>>
>> To offer the OP source he can use...
>>
>> class Map:
>>  def __init__(self):
>>  self.dict = {}
>>  def DoSomething(self):
>>  #do something with self.dict
>>
>> Every instance gets a new dictionary.  Now, if he actually wants to
>> change the behavior of the DoSomething method, of course then it would
>> make sense to subclass Map.
>>
>>   - Josiah
>
> I am hoping to change the self.dict for each subclass. I realize that
> I could save self.dict to file and then load in different dicts each
> time I get a new instance of class. But I want to be able to make
> subclasses of map that each have different self.dict. Then when I need
> to use them, just import the module and use the specific dict, instead
> of having to keep track of a separate dictionary file. I am new to

As Josiah said, I still don't see why do you want a *subclass*. If the  
only difference between your "subclasses" is their dict, they're not  
subclasses but just Map *instances*.
Let's say, have a class Person, with attributes "name" and "email". If I  
want to represent two different persons, I would create two Person  
*instances*: Person(name="Gabriel", email="[EMAIL PROTECTED]") and  
Person(name="nik", email="[EMAIL PROTECTED]")
Classes try to capture behavior and structure; instances contain state  
(very roughly said). One *could* use two subclasses here, and in certain  
circumstances it may be useful, but it's not the most common case.

> this, but I thought that this would be a regular thing to do in
> python, because people must make classes in the interactive console
> and then export them somehow for later use.

I've never done that. I only use the interactive interpreter for testing  
purposes, I never "develop" code inside the interpreter.

-- 
Gabriel Genellina

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


Re: Moving items from list to list

2007-06-14 Thread Gabriel Genellina
En Thu, 14 Jun 2007 14:23:14 -0300, Evan Klitzke <[EMAIL PROTECTED]> escribió:

> On 6/14/07, HMS Surprise <[EMAIL PROTECTED]> wrote:
>>
>> Just wondered if there was some python idiom for moving a few items
>> from one list to another. I often need to delete 2 or 3 items from one
>> list and put them in another. Delete doesn't seem to have a return
>> value. I don't care which items I get so now I just use a couple of
>> pops or a for loop for more than two.
>
> I'm not sure if this is what you're asking, but if the elements in the
> list are contiguous you can just use list slicing/addition, like this:
>
> a = [1, 2, 3, 4, 5]
> b = [6, 7, 8, 9, 10]
>
> b = a[2:] + b
> a = a[:2]
>
> Now the contents of a and b respectively are a = [1, 2] and b = [3, 4,
> 5, 6, 7, 8, 9, 10].

When lists become large this is less convenient because it has to build  
three intermediate lists. At least on my box, pop+append is 5 orders of  
magnitude faster (but as shown on another posts, you should test on your  
box to see what happens):

c:\temp>call python -m timeit -s "a=range(100);b=range(1000)"  
"b.append(a.po
p())"
100 loops, best of 3: 1.35 usec per loop

c:\temp>call python -m timeit -s "a=range(100);b=range(1000)"  
"b+=a[-1:];a=a
[:-1]"
10 loops, best of 3: 107 msec per loop

c:\temp>call python -m timeit -s "a=range(100);b=range(1000)"  
"b.append(a[-1
]);a=a[:-1]"
10 loops, best of 3: 107 msec per loop

c:\temp>call python -m timeit -s "a=range(100);b=range(1000)"  
"b.append(a[0]
);a=a[1:]"
10 loops, best of 3: 110 msec per loop

-- 
Gabriel Genellina

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


Failing on string exceptions in 2.4

2007-06-14 Thread Stephen R Laniel
Reading the Python docs, it looks like string exceptions
will be a DeprecationWarning in Python 2.5. Is there any way
to make them so in 2.4? Now how about if I want to turn all
DeprecationWarnings into compile-time errors? Is there some
way to do this?

End goal being that string exceptions would cause
compilation to fail. A few times now, I've found myself
doing

class SomeClass:
"""docstring"""
pass

raise SomeClass, "Some description"

and I've gotten a weird compiler error about the constructor
for SomeClass. I'd prefer it just to fail there and not let
me raise an exception that isn't subclassed beneath
Exception.

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-14 Thread Gabriel Genellina
En Thu, 14 Jun 2007 05:54:25 -0300, Francesco Guerrieri  
<[EMAIL PROTECTED]> escribió:

> On 6/14/07, Peter Otten <[EMAIL PROTECTED]> wrote:
>> Gabriel Genellina wrote:
>> > ...
>> > py> print timeit.Timer("f2()", "from __main__ import  
>> f2").repeat(number=1)
>> > [0.42673663831576358, 0.42807591467630662, 0.44401481193838876]
>> > py> print timeit.Timer("f1()", "from __main__ import  
>> f1").repeat(number=1)
>> >
>> > ...after a few minutes I aborted the process...
>>
>> I can't confirm this.
>
> [...]
>
>> $ python2.5 -m timeit -s 'from join import f1' 'f1()'
>> 10 loops, best of 3: 212 msec per loop
>> $ python2.5 -m timeit -s 'from join import f2' 'f2()'
>> 10 loops, best of 3: 259 msec per loop
>> $ python2.5 -m timeit -s 'from join import f3' 'f3()'
>> 10 loops, best of 3: 236 msec per loop
>
> On my machine (using python 2.5 under win xp) the results are:
 print timeit.Timer("f2()", "from __main__ import f2").repeat(number =  
 1)
> [0.19726834822823575, 0.19324697456408974, 0.19474492594212861]
 print timeit.Timer("f1()", "from __main__ import f1").repeat(number =  
 1)
> [21.982707133304167, 21.905312587963252, 22.843430035622767]
>
> so it seems that there is a rather sensible difference.
> what's the reason of the apparent inconsistency with Peter's test?

I left the test running and went to sleep. Now, the results:

C:\TEMP>python -m timeit -s "from join import f1" "f1()"
10 loops, best of 3: 47.7 sec per loop

C:\TEMP>python -m timeit -s "from join import f2" "f2()"
10 loops, best of 3: 317 msec per loop

C:\TEMP>python -m timeit -s "from join import f3" "f3()"
10 loops, best of 3: 297 msec per loop

Yes, 47.7 *seconds* to build the string using the += operator.
I don't know what's the difference: python version (this is not 2.5.1  
final), hardware, OS... but certainly in this PC it is *very* important.
Memory usage was around 40MB (for a 10MB string) and CPU usage went to 99%  
(!).

-- 
Gabriel Genellina

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


Re: Efficient way of generating original alphabetic strings like unix file "split"

2007-06-14 Thread [EMAIL PROTECTED]
On Jun 14, 4:39 pm, py_genetic <[EMAIL PROTECTED]> wrote:
> > You didn't try hard enough. :)
>
> >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465
>
> > --
> > HTH,
> > Rob
>
> Thanks Rob, "permutation" was the keyword I shcould have used!

See my other post to see if that is indeed what you mean.

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


Re: Efficient way of generating original alphabetic strings like unix file "split"

2007-06-14 Thread [EMAIL PROTECTED]
On Jun 14, 3:08 pm, Rob Wolfe <[EMAIL PROTECTED]> wrote:
> py_genetic <[EMAIL PROTECTED]> writes:
> > Hi,
>
> > I'm looking to generate x alphabetic strings in a list size x.  This
> > is exactly the same output that the unix command "split" generates as
> > default file name output when splitting large files.
>
> > Example:
>
> > produce x original, but not random strings from english alphabet, all
> > lowercase.  The length of each string and possible combinations is
> > dependent on x.  You don't want any repeats.
>
> > [aaa, aab, aac, aad,  aax, .. bbc, bbd,  bcd]
>
> > I'm assumming there is a slick, pythonic way of doing this, besides
> > writing out a beast of a looping function.  I've looked around on
> > activestate cookbook, but have come up empty handed.  Any suggestions?
>
> You didn't try hard enough. :)
>
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465


Unfortunately, that's a very poor example. The terminaology is
all wrong.

"xpermutations takes all elements from the sequence, order matters."
This ought to be the Cartesian Product, but it's not (no replacement).

"xcombinations takes n distinct elements from the sequence, order
matters."
If order matters, it's a PERMUTATION, period.

"xuniqueCombinations takes n distinct elements from the sequence,
order is irrelevant."
No such thing, a Combination is unique by definition.

"xselections takes n elements (not necessarily distinct) from the
sequence, order matters."
Ah, this allows a size operator, so if size = length, we get full
Cartesian Product.

The proper terminology for the Cartesian Product and
its subsets is:

Permutations with replacement
Combinations with replacement
Permutations without replacement
Combinations without replacement

And if the functions were properly labeled, you would get:

permutation without replacement - size 4

Permutations of 'love'
love loev lvoe lveo leov levo olve olev ovle ovel oelv oevl vloe vleo
vole voel velo veol elov elvo eolv eovl evlo evol


permutation without replacement - size 2

Combinations of 2 letters from 'love'
lo lv le ol ov oe vl vo ve el eo ev


combination without replacement - size 2

Unique Combinations of 2 letters from 'love'
lo lv le ov oe ve


permutation with replacement - size 2

Selections of 2 letters from 'love'
ll lo lv le ol oo ov oe vl vo vv ve el eo ev ee


full Cartesian Product, permutations with replacement - size 4

Selections of 4 letters from 'love'
 lllo lllv llle llol lloo llov lloe llvl llvo llvv llve llel lleo
llev llee loll lolo lolv lole lool looo loov looe lovl lovo lovv love
loel loeo loev loee lvll lvlo lvlv lvle lvol lvoo lvov lvoe lvvl lvvo
lvvv lvve lvel lveo lvev lvee lell lelo lelv lele leol leoo leov leoe
levl levo levv leve leel leeo leev leee olll ollo ollv olle olol oloo
olov oloe olvl olvo olvv olve olel oleo olev olee ooll oolo oolv oole
oool  ooov oooe oovl oovo oovv oove ooel ooeo ooev ooee ovll ovlo
ovlv ovle ovol ovoo ovov ovoe ovvl ovvo ovvv ovve ovel oveo ovev ovee
oell oelo oelv oele oeol oeoo oeov oeoe oevl oevo oevv oeve oeel oeeo
oeev oeee vlll vllo vllv vlle vlol vloo vlov vloe vlvl vlvo vlvv vlve
vlel vleo vlev vlee voll volo volv vole vool vooo voov vooe vovl vovo
vovv vove voel voeo voev voee vvll vvlo vvlv vvle vvol vvoo vvov vvoe
vvvl vvvo  vvve vvel vveo vvev vvee vell velo velv vele veol veoo
veov veoe vevl vevo vevv veve veel veeo veev veee elll ello ellv elle
elol eloo elov eloe elvl elvo elvv elve elel eleo elev elee eoll eolo
eolv eole eool eooo eoov eooe eovl eovo eovv eove eoel eoeo eoev eoee
evll evlo evlv evle evol evoo evov evoe evvl evvo evvv evve evel eveo
evev evee eell eelo eelv eele eeol eeoo eeov eeoe eevl eevo eevv eeve
eeel eeeo eeev 


And Combinations with replacement seems to be missing.


>
> --
> HTH,
> Rob- Hide quoted text -
>
> - Show quoted text -


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


Re: Questions about mathematical and statistical functionality in Python

2007-06-14 Thread Tim Churches
Michael Hoffman wrote:
> Talbot Katz wrote:
> 
>> I hope you'll indulge an ignorant outsider.  I work at a financial 
>> software firm, and the tool I currently use for my research is R, a 
>> software environment for statistical computing and graphics.  R is 
>> designed with matrix manipulation in mind, and it's very easy to do 
>> regression and time series modeling, and to plot the results and test 
>> hypotheses.  The kinds of functionality we rely on the most are standard 
>> and robust versions of regression and principal component / factor 
>> analysis, bayesian methods such as Gibbs sampling and shrinkage, and 
>> optimization by linear, quadratic, newtonian / nonlinear, and genetic 
>> programming; frequently used graphics include QQ plots and histograms.  
>> In R, these procedures are all available as functions (some of them are 
>> in auxiliary libraries that don't come with the standard distribution, 
>> but are easily downloaded from a central repository).
> 
> I use both R and Python for my work. I think R is probably better for 
> most of the stuff you are mentioning. I do any sort of heavy 
> lifting--database queries/tabulation/aggregation in Python and load the 
> resulting data frames into R for analysis and graphics.

I would second that. It is not either/or. Use Python, including Numpy
and matplotlib and packages from SciPy, for some things, and R for
others. And you can even embed R in Python using RPy - see
http://rpy.sourceforge.net/

We use the combination of Python, Numpy (actually, the older Numeric
Python package, but soon to be converted to Numpy), RPy and R in our
NetEpi Analysis project - exploratory epidemiological analysis of large
data sets - see http://sourceforge.net/projects/netepi - and it is a
good combination - Python for the Web interface, data manipulation and
data heavy-lifting, and for some of the more elementary statistics, and
R for more involved statistical analysis and graphics (with teh option
of using matplotlib or other Python-based graphics packages for some
tasks if we wish). The main thing to remember, though, is that indexing
is zero-based in Python and 1-based in R...

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


RE: Questions about mathematical and statistical functionality in Python

2007-06-14 Thread John Krukoff
On Jun 14, 4:02 pm, "Talbot Katz" <[EMAIL PROTECTED]> wrote: 
> Greetings Pythoners!
> 
> I hope you'll indulge an ignorant outsider.  I work at a financial
> software
> firm, and the tool I currently use for my research is R, a software
> environment for statistical computing and graphics.  R is designed with
> matrix manipulation in mind, and it's very easy to do regression and time
> series modeling, and to plot the results and test hypotheses.  The kinds
> of
> functionality we rely on the most are standard and robust versions of
> regression and principal component / factor analysis, bayesian methods
> such
> as Gibbs sampling and shrinkage, and optimization by linear, quadratic,
> newtonian / nonlinear, and genetic programming; frequently used graphics
> include QQ plots and histograms.  In R, these procedures are all available
> as functions (some of them are in auxiliary libraries that don't come with
> the standard distribution, but are easily downloaded from a central
> repository).
> 
> For a variety of reasons, the research group is considering adopting
> Python.
>   Naturally, I am curious about the mathematical, statistical, and
> graphical
> functionality available in Python.  Do any of you out there use Python in
> financial research, or other intense mathematical/statistical computation?
> Can you compare working in Python with working in a package like R or S-
> Plus
> or Matlab, etc.?  Which of the procedures I mentioned above are available
> in
> Python?  I appreciate any insight you can provide.  Thanks!
> 
> --  TMK  --
> 212-460-5430  home
> 917-656-5351  cell
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list

It is worth noting that there's a bridge available to allow python to
integrate cleanly with R, the Rpy project:
http://rpy.sourceforge.net/

Which should allow you to use python for whatever it is you need without
abandoning R for your mathematical/statistical work.

-
John Krukoff
[EMAIL PROTECTED]

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


Re: Efficient way of generating original alphabetic strings like unix file "split"

2007-06-14 Thread py_genetic
>
> You didn't try hard enough. :)
>
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465
>
> --
> HTH,
> Rob

Thanks Rob, "permutation" was the keyword I shcould have used!

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


Re: Questions about mathematical and statistical functionality in Python

2007-06-14 Thread Michael Hoffman
Talbot Katz wrote:

> I hope you'll indulge an ignorant outsider.  I work at a financial 
> software firm, and the tool I currently use for my research is R, a 
> software environment for statistical computing and graphics.  R is 
> designed with matrix manipulation in mind, and it's very easy to do 
> regression and time series modeling, and to plot the results and test 
> hypotheses.  The kinds of functionality we rely on the most are standard 
> and robust versions of regression and principal component / factor 
> analysis, bayesian methods such as Gibbs sampling and shrinkage, and 
> optimization by linear, quadratic, newtonian / nonlinear, and genetic 
> programming; frequently used graphics include QQ plots and histograms.  
> In R, these procedures are all available as functions (some of them are 
> in auxiliary libraries that don't come with the standard distribution, 
> but are easily downloaded from a central repository).

I use both R and Python for my work. I think R is probably better for 
most of the stuff you are mentioning. I do any sort of heavy 
lifting--database queries/tabulation/aggregation in Python and load the 
resulting data frames into R for analysis and graphics.
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MS Word parser

2007-06-14 Thread Ben C
On 2007-06-13, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> On Jun 13, 1:28 am, Tim Golden <[EMAIL PROTECTED]> wrote:
>> [EMAIL PROTECTED] wrote:
>> > Hi all,
>> > I'm currently using antiword to extract content from MS Word files.
>> > Is there another way to do this without relying on any command prompt
>> > application?
>>
>> Well you haven't given your environment, but is there
>> anything to stop you from controlling Word itself via
>> COM? I'm no Word expert, but looking around, this
>> seems to work:
>>
>> 
>> import win32com.client
>> word = win32com.client.Dispatch ("Word.Application")
>> doc = word.Documents.Open ("c:/temp/temp.doc")
>> text = doc.Range ().Text
>>
>> open ("c:/temp/temp.txt", "w").write (text.encode ("UTF-8"))
>> 
>>
>> TJG
>
> Tim,
> I'm on Linux (RedHat) so using Word is not an option for me.  Any
> other suggestions?

There is OpenOffice which has a Python API to it (called UNO). But
piping through antiword is probably easier.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a_list.count(a_callable) ?

2007-06-14 Thread Carsten Haese
On Thu, 2007-06-14 at 12:53 -0700, Ping wrote:
> Hi,
> 
> I'm wondering if it is useful to extend the count() method of a list
> to accept a callable object?  What it does should be quite intuitive:
> count the number of items that the callable returns True or anything
> logically equivalent (non-empty sequence, non-zero number, etc).
> 
> This would return the same result as len(filter(a_callable, a_list)),
> but without constructing an intermediate list which is thrown away
> after len() is done.
> 
> This would also be equivalent to
> n = 0
> for i in a_list:
> if a_callable(i):  n += 1
> but with much shorter and easier-to-read code.  It would also run
> faster.

As an alternative to the generator-sum approach I posted in reply to
Dustan's suggestion, you could (ab)use the fact that count() uses
equality testing and do something like this:

>>> class Oddness(object):
...def __eq__(self, other): return other%2==1
... 
>>> [1,2,3,4,5].count(Oddness())
3

I don't know which approach is faster. Feel free to compare the two.

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net


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


Re: Questions about mathematical and statistical functionality in Python

2007-06-14 Thread kyosohma
On Jun 14, 4:02 pm, "Talbot Katz" <[EMAIL PROTECTED]> wrote:
> Greetings Pythoners!
>
> I hope you'll indulge an ignorant outsider.  I work at a financial software
> firm, and the tool I currently use for my research is R, a software
> environment for statistical computing and graphics.  R is designed with
> matrix manipulation in mind, and it's very easy to do regression and time
> series modeling, and to plot the results and test hypotheses.  The kinds of
> functionality we rely on the most are standard and robust versions of
> regression and principal component / factor analysis, bayesian methods such
> as Gibbs sampling and shrinkage, and optimization by linear, quadratic,
> newtonian / nonlinear, and genetic programming; frequently used graphics
> include QQ plots and histograms.  In R, these procedures are all available
> as functions (some of them are in auxiliary libraries that don't come with
> the standard distribution, but are easily downloaded from a central
> repository).
>
> For a variety of reasons, the research group is considering adopting Python.
>   Naturally, I am curious about the mathematical, statistical, and graphical
> functionality available in Python.  Do any of you out there use Python in
> financial research, or other intense mathematical/statistical computation?
> Can you compare working in Python with working in a package like R or S-Plus
> or Matlab, etc.?  Which of the procedures I mentioned above are available in
> Python?  I appreciate any insight you can provide.  Thanks!
>
> --  TMK  --
> 212-460-5430home
> 917-656-5351cell

I'd look at following modules:

matplotlib - http://matplotlib.sourceforge.net/
numpy - http://numpy.scipy.org/

Finally, this website lists other resources: 
http://www.astro.cornell.edu/staff/loredo/statpy/

Mike

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


Re: a_list.count(a_callable) ?

2007-06-14 Thread Carsten Haese
On Thu, 2007-06-14 at 21:06 +, Dustan wrote:
> On Jun 14, 3:37 pm, Dustan <[EMAIL PROTECTED]> wrote:
> > Which can then be converted into a generator expression (round
> > brackets instead of square brackets) to avoid the intermediate list:
> > len((i for i in a_list if a_callable(i)))
> 
> Sorry for the excess of posts everybody.
> 
> I just realized that the generator expression would not work. I'm not
> sure how else could be implemented efficiently and without using up
> memory besides by accumulating the count as your earlier example shows.

sum(1 for i in a_list if a_callable(i))

-- 
Carsten Haese
http://informixdb.sourceforge.net


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


Re: a_list.count(a_callable) ?

2007-06-14 Thread Dustan
On Jun 14, 3:37 pm, Dustan <[EMAIL PROTECTED]> wrote:
> Which can then be converted into a generator expression (round
> brackets instead of square brackets) to avoid the intermediate list:
> len((i for i in a_list if a_callable(i)))

Sorry for the excess of posts everybody.

I just realized that the generator expression would not work. I'm not
sure how else could be implemented efficiently and without using up
memory besides by accumulating the count as your earlier example shows.

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


Re: os.path.normpath bug?

2007-06-14 Thread billiejoex
On 14 Giu, 22:35, Michael Hoffman <[EMAIL PROTECTED]> wrote:

> Intentional.
>
> http://en.wikipedia.org/wiki/Path_(computing)#Universal_Naming_Conven...
> --
> Michael Hoffman

Got it.
Thank you.


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


Re: Serialization across languages?

2007-06-14 Thread Martin v. Löwis
> First, what do I need to be aware of when sending arbitrary
> data by a POST, and Second, is there a universally supported
> version of what python can do with pickle?  I mostly need
> python and PHP, but perl would be nice too.

You might want to use WDDX. There are WDDX libraries for Python,
PHP, and Perl.

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


Questions about mathematical and statistical functionality in Python

2007-06-14 Thread Talbot Katz
Greetings Pythoners!

I hope you'll indulge an ignorant outsider.  I work at a financial software 
firm, and the tool I currently use for my research is R, a software 
environment for statistical computing and graphics.  R is designed with 
matrix manipulation in mind, and it's very easy to do regression and time 
series modeling, and to plot the results and test hypotheses.  The kinds of 
functionality we rely on the most are standard and robust versions of 
regression and principal component / factor analysis, bayesian methods such 
as Gibbs sampling and shrinkage, and optimization by linear, quadratic, 
newtonian / nonlinear, and genetic programming; frequently used graphics 
include QQ plots and histograms.  In R, these procedures are all available 
as functions (some of them are in auxiliary libraries that don't come with 
the standard distribution, but are easily downloaded from a central 
repository).

For a variety of reasons, the research group is considering adopting Python. 
  Naturally, I am curious about the mathematical, statistical, and graphical 
functionality available in Python.  Do any of you out there use Python in 
financial research, or other intense mathematical/statistical computation?  
Can you compare working in Python with working in a package like R or S-Plus 
or Matlab, etc.?  Which of the procedures I mentioned above are available in 
Python?  I appreciate any insight you can provide.  Thanks!

--  TMK  --
212-460-5430home
917-656-5351cell


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


Re: a_list.count(a_callable) ?

2007-06-14 Thread Dustan
On Jun 14, 3:37 pm, Dustan <[EMAIL PROTECTED]> wrote:
> map and filter are basically obsolete after the introduction of list
> comprehensions

It is probably worth noting that list comprehensions do not require
that you write a new function; they take any expression where
appropriate. For more information on list comprehensions, see:

http://docs.python.org/tut/node7.html#SECTION00714

Generator expressions are the same, except syntactically they have
round brackets instead of square, and they return a generator instead
of a list, which allows for lazy evaluation.

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


Serialization across languages?

2007-06-14 Thread Tobiah
I want to do SOAP like calls from a device who's libraries
don't include SOAP.  I'm thinking of using simple HTTP posts,
but I'm going to want to send arrays and hashes.

First, what do I need to be aware of when sending arbitrary
data by a POST, and Second, is there a universally supported
version of what python can do with pickle?  I mostly need
python and PHP, but perl would be nice too.

Thanks,

Toby

-- 
Posted via a free Usenet account from http://www.teranews.com

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


Re: a_list.count(a_callable) ?

2007-06-14 Thread Dustan
On Jun 14, 2:53 pm, Ping <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm wondering if it is useful to extend the count() method of a list
> to accept a callable object?  What it does should be quite intuitive:
> count the number of items that the callable returns True or anything
> logically equivalent (non-empty sequence, non-zero number, etc).
>
> This would return the same result as len(filter(a_callable, a_list)),

map and filter are basically obsolete after the introduction of list
comprehensions; your expression is equivalent to:
len([i for i in a_list if a_callable(i)])

Which can then be converted into a generator expression (round
brackets instead of square brackets) to avoid the intermediate list:
len((i for i in a_list if a_callable(i)))

Or syntactically equivalent (avoiding lispy brackets):
len(i for i in a_list if a_callable(i))

> but without constructing an intermediate list which is thrown away
> after len() is done.
>
> This would also be equivalent to
> n = 0
> for i in a_list:
> if a_callable(i):  n += 1
> but with much shorter and easier-to-read code.  It would also run
> faster.
>
> This is my first post and please bear with me if I'm not posting it in
> the right way.
>
> Regards,
> Ping


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


Re: os.path.normpath bug?

2007-06-14 Thread Michael Hoffman
billiejoex wrote:
> Hi there,
> I've noticed that os.path.normpath does not collapse redundant
> separators if they're located at the beginning of the string:
> 
 print os.path.normpath('/a//b//c')
> \a\b\c
 print os.path.normpath('//a//b//c')
> \\a\b\c
> 
> Is it intentional or is it a bug?

Intentional.

http://en.wikipedia.org/wiki/Path_(computing)#Universal_Naming_Convention
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


os.path.normpath bug?

2007-06-14 Thread billiejoex
Hi there,
I've noticed that os.path.normpath does not collapse redundant
separators if they're located at the beginning of the string:

>>> print os.path.normpath('/a//b//c')
\a\b\c
>>> print os.path.normpath('//a//b//c')
\\a\b\c

Is it intentional or is it a bug?

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


python i2c ioctl

2007-06-14 Thread luca

Hi,
 I was trying to make to work directly l i2c with python with the
calls ioctl. But I have of the problems and I do not succeed to go
ahead.

this and l error

[EMAIL PROTECTED] /usr/local/wrap]129# python pcf8591_ioctl.py
Reading from 4 ch 8 bit A/D converter PCF8591
Traceback (most recent call last):
  File "pcf8591_ioctl.py", line 269, in ?
main()
  File "pcf8591_ioctl.py", line 229, in main
if i2c_open () < 0:
  File "pcf8591_ioctl.py", line 168, in i2c_open
fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETGET_OUTPUT), 0,
iomask)
TypeError: ioctl requires a file or file descriptor, an integer and
optionally a integer or buffer argument

the code

import fcntl
import os
###
#etrax.h
ETRAXGPIO_IOCTYPE = 43
GPIO_MINOR_A = 0
GPIO_MINOR_B = 1
GPIO_MINOR_LEDS = 2
GPIO_MINOR_G = 3
GPIO_MINOR_LAST = 3
IO_READBITS = 0x1
IO_SETBITS =  0x2
IO_CLRBITS =  0x3
IO_HIGHALARM = 0x4
IO_LOWALARM = 0x5
IO_CLRALARM = 0x6
IO_LEDACTIVE_SET = 0x7
IO_READDIR  =  0x8
IO_SETINPUT =  0x9
IO_SETOUTPUT = 0xA
IO_LED_SETBIT = 0xB
IO_LED_CLRBIT = 0xC
IO_SHUTDOWN  = 0xD
IO_GET_PWR_BT = 0xE
IO_CFG_WRITE_MODE = 0xF

def IO_CFG_WRITE_MODE_VALUE(msb, data_mask, clk_mask):
( (((msb)&1) << 16) | (((data_mask) &0xFF) << 8) | ((clk_mask) &
0xFF) )
def IO_CFG_WRITE_MODE_VALUE(msb, data_mask, clk_mask):
( (((msb)&1) << 16) | (((data_mask) &0xFF) << 8) | ((clk_mask) &
0xFF) )

IO_READ_INBITS =  0x10
IO_READ_OUTBITS = 0x11
IO_SETGET_INPUT = 0x12
IO_SETGET_OUTPUT = 0x13
###
###
#ioctl.h

_IOC_NRBITS = 8
_IOC_TYPEBITS=  8
_IOC_SIZEBITS   =14
_IOC_DIRBITS = 2
_IOC_NRMASK = ((1 << _IOC_NRBITS)-1)
_IOC_TYPEMASK = ((1 << _IOC_TYPEBITS)-1)
_IOC_SIZEMASK   =((1 << _IOC_SIZEBITS)-1)
_IOC_DIRMASK=((1 << _IOC_DIRBITS)-1)
_IOC_NRSHIFT=0
_IOC_TYPESHIFT  =(_IOC_NRSHIFT+_IOC_NRBITS)
_IOC_SIZESHIFT  =(_IOC_TYPESHIFT+_IOC_TYPEBITS)
_IOC_DIRSHIFT   =(_IOC_SIZESHIFT+_IOC_SIZEBITS)
_IOC_NONE=0
_IOC_WRITE=1
_IOC_READ=2
def _IOC(dir,type,nr,size):
(((dir)  << _IOC_DIRSHIFT) |
((type) << _IOC_TYPESHIFT) |
((nr)   << _IOC_NRSHIFT) |
((size) << _IOC_SIZESHIFT))

def _IO(type,nr):
_IOC(_IOC_NONE,(type),(nr),0)

def _IOR(type,nr,size):
_IOC(_IOC_READ,(type),(nr),sizeof(size))

def _IOW(type,nr,size):
_IOC(_IOC_WRITE,(type),(nr),sizeof(size))

def _IOWR(type,nr,size):
_IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))

def _IOC_DIR(nr):
(((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
def _IOC_TYPE(nr):
(((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
def _IOC_NR(nr):
(((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
def _IOC_SIZE(nr):
(((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
def IOC_IN():
(_IOC_WRITE << _IOC_DIRSHIFT)

def IOC_OUT():
(_IOC_READ << _IOC_DIRSHIFT)
def IOC_INOUT():
((_IOC_WRITE|_IOC_READ) << _IOC_DIRSHIFT)
def IOCSIZE_MASK():
(_IOC_SIZEMASK << _IOC_SIZESHIFT)
def IOCSIZE_SHIFT():
(_IOC_SIZESHIFT)
##
##
##



I2C_DATA_LINE  = 1<<24
I2C_CLOCK_LINE = 1<<25


#Get the SDA line state
def i2c_getbit():
value=fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_READBITS))
if ((value&(I2C_DATA_LINE))==0):
return 0
else:
return 1

#Set the SDA line as output
def i2c_dir_out():
iomask = I2C_DATA_LINE
fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETGET_OUTPUT), iomask)

#Set the SDA line as input
def i2c_dir_in():
iomask = I2C_DATA_LINE
fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETGET_INPUT), iomask)

#Set the SDA line state
def i2c_data(state):
if (state==1):
i2c_dir_in()
else:
i2c_dir_out()
fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_CLRBITS),
I2C_DATA_LINE)
#Set the SCL line state
def i2c_clk(state):
if (state==1):
ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETBITS), 
I2C_CLOCK_LINE)
else:
ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_CLRBITS), 
I2C_CLOCK_LINE)

#Read a byte from I2C bus and send the ack sequence
#Put islast = 1 is this is the last byte to receive from the slave

def i2c_inbyte(islast):
value = 0

#Read data byte

i2c_clk(0)
i2c_dir_in()
for i in range (0,5):
i2c_clk(1)
bitvalue = i2c_getbit()
value |= bitvalue
if (i<7):
value = 1
i2c_clk(0)

if (islast==0):
#Send Ack if is not the last byte to read
i2c_dir_out()
i2c_data(0)
i2c_clk(1)
i2c_clk(0)
i2c_dir_in()
else:
#Doesn't send Ack if is the last byte to read
i2c_dir_in()
   

Proper licensing and copyright attribution for extracted Python code

2007-06-14 Thread Douglas Alan
Hi.  I extracted getpath.c out of Python and modified it to make a
generally useful facility for C and C++ programming.  These comments
are at the top of my .c file, and I would like to know if they pass
muster for meeting licensing, copyright, and aesthetics requirements:

// -*- Mode: C; fill-column: 79 -*-

//=
// Description:
//
//  pathToExecutable.c is a module that allows a Unix program to find the
//  location of its executable.  This capability is extremely useful for
//  writing programs that don't have to recompiled in order to be relocated
//  within the filesystem.  Any auxiliary files (dynamically loaded
//  libraries, help files, configuration files, etc.) can just be placed in
//  the same directory as the executable, and the function
//  pathToExecutable() can be used by the program at runtime to locate its
//  executable file and from there the program can locate any auxiliary
//  files it needs in order to operate.
//
//  pathToExecutable() is smart enough to follow a symlink (or even a chain
//  of symlinks) in order to find the true location of the executable.  In
//  this manner, for instance, you might install all of the files used by a
//  program (let's say it's called "my-program"), including the executable,
//  into the directory /usr/local/lib/my-program, and then put a symlink
//  into /usr/local/bin that points to the executable
//  /usr/local/lib/my-program/my-program.  Initially pathToExecutable()
//  will identify /usr/local/bin/my-program as the executable, but it will
//  then notice that this "file" is really a symbolic link.
//  pathToExecutable() will then follow the symbolic link and return
//  "/usr/local/lib/my-program/my-pogram" instead.
//
//  Before a program can call pathToExecutable(), setArgv() must be called
//  (canonically in main()) so that pathToExecutable() can fetch the value
//  of argv[0] and use it to help figure out where the executable is
//  located.
//
// Copyright and licensing information:
//
//  This software is a heavily modified version of getpath.c from the
//  Python 2.5.1 release.  Both this software and the original software
//  from which it is derived are freely distributable under the terms of
//  the permissive freeware license, Python Software Foundation License
//  Version 2.  You can read more about this license here:
//
//   http://www.python.org/psf/license
//
//  The original software from which this software is derived carries the
//  following copyright:
//
// Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Python
// Software Foundation.
//
//  The modifications to the original software, which are contained herein,
//  are
//
// Copyright (c) 2007 Douglas Alan 
//
//=

Thanks,
|>oug
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Moving items from list to list

2007-06-14 Thread George Sakkis
On Jun 14, 12:30 pm, HMS Surprise <[EMAIL PROTECTED]> wrote:

> Just wondered if there was some python idiom for moving a few items
> from one list to another. I often need to delete 2 or 3 items from one
> list and put them in another. Delete doesn't seem to have a return
> value. I don't care which items I get so now I just use a couple of
> pops or a for loop for more than two.
>
> Thanks
>
> jh

>>> x = range(10)
>>> y = []

>>> y.append(x.pop(4))
>>> print x, y
[0, 1, 2, 3, 5, 6, 7, 8, 9] [4]

>>> y.append(x.pop(7))
>>> print x, y
[0, 1, 2, 3, 5, 6, 7, 9] [4, 8]


HTH,
George

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


Convert to C/C++?

2007-06-14 Thread SpreadTooThin
I am wondering if someone who knows the implemention of python's time
could help converting this to c/c++

nanoseconds = int(time.time() * 1e9)
# 0x01b21dd213814000 is the number of 100-ns intervals between 
the
# UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01
00:00:00.
self.timestamp = int(nanoseconds/100) + 0x01b21dd213814000L
self.clock_seq = random.randrange(1<<14L) # instead of stable
storage
self.time_low = self.timestamp & 0xL
self.time_mid = (self.timestamp >> 32L) & 0xL
self.time_hi_version = (self.timestamp >> 48L) & 0x0fffL
self.clock_seq_low = self.clock_seq & 0xffL
self.clock_seq_hi_variant = (self.clock_seq >> 8L) & 0x3fL
#print 'timestamp ', self.timestamp, self.time_low, 
self.time_mid,
self.time_hi_version
#print 'clock_seq ', self.clock_seq, self.clock_seq_low,
self.clock_seq_hi_variant

vs unix gettimeofday

int gettimeofday(struct timeval *tp, struct timezone *tzp);

struct timeval {
   long tv_sec; /* seconds since Jan. 1, 1970 */
   long tv_usec;/* and microseconds */
};

struct timezone {
   int tz_minuteswest; /* of Greenwich */
   int tz_dsttime; /* type of dst correction to apply */
};

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


Re: Efficient way of generating original alphabetic strings like unix file "split"

2007-06-14 Thread Rob Wolfe
py_genetic <[EMAIL PROTECTED]> writes:

> Hi,
>
> I'm looking to generate x alphabetic strings in a list size x.  This
> is exactly the same output that the unix command "split" generates as
> default file name output when splitting large files.
>
> Example:
>
> produce x original, but not random strings from english alphabet, all
> lowercase.  The length of each string and possible combinations is
> dependent on x.  You don't want any repeats.
>
> [aaa, aab, aac, aad,  aax, .. bbc, bbd,  bcd]
>
> I'm assumming there is a slick, pythonic way of doing this, besides
> writing out a beast of a looping function.  I've looked around on
> activestate cookbook, but have come up empty handed.  Any suggestions?

You didn't try hard enough. :)

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465

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


Re: Efficient way of generating original alphabetic strings like unix file "split"

2007-06-14 Thread [EMAIL PROTECTED]
On Jun 14, 1:41 pm, py_genetic <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm looking to generate x alphabetic strings in a list size x.  This
> is exactly the same output that the unix command "split" generates as
> default file name output when splitting large files.
>
> Example:
>
> produce x original, but not random strings from english alphabet, all
> lowercase.  The length of each string and possible combinations is
> dependent on x.  You don't want any repeats.
>
> [aaa, aab, aac, aad,  aax, .. bbc, bbd,  bcd]
>
> I'm assumming there is a slick, pythonic way of doing this, besides
> writing out a beast of a looping function.  I've looked around on
> activestate cookbook, but have come up empty handed.  Any suggestions?

If you allow numbers also, you can use Base 36:

>>> import gmpy
>>> int('aaa',36)
13330
>>> for n in xrange(13330,13330+1000):
print gmpy.digits(n,36),

aaa aab aac aad aae aaf aag aah aai aaj aak aal
aam aan aao aap aaq aar aas aat aau aav aaw aax
aay aaz ab0 ab1 ab2 ab3 ab4 ab5 ab6 ab7 ab8 ab9
aba abb abc abd abe abf abg abh abi abj abk abl
abm abn abo abp abq abr abs abt abu abv abw abx
aby abz ac0 ac1 ac2 ac3 ac4 ac5 ac6 ac7 ac8 ac9
aca acb acc acd ace acf acg ach aci acj ack acl
acm acn aco acp acq acr acs act acu acv acw acx
acy acz ad0 ad1 ad2 ad3 ad4 ad5 ad6 ad7 ad8 ad9
ada adb adc add ade adf adg adh adi adj adk adl
adm adn ado adp adq adr ads adt adu adv adw adx
ady adz ae0 ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 ae9
aea aeb aec aed aee aef aeg aeh aei aej aek ael
aem aen aeo aep ...


>
> Thanks,
> Conor


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


a_list.count(a_callable) ?

2007-06-14 Thread Ping
Hi,

I'm wondering if it is useful to extend the count() method of a list
to accept a callable object?  What it does should be quite intuitive:
count the number of items that the callable returns True or anything
logically equivalent (non-empty sequence, non-zero number, etc).

This would return the same result as len(filter(a_callable, a_list)),
but without constructing an intermediate list which is thrown away
after len() is done.

This would also be equivalent to
n = 0
for i in a_list:
if a_callable(i):  n += 1
but with much shorter and easier-to-read code.  It would also run
faster.

This is my first post and please bear with me if I'm not posting it in
the right way.

Regards,
Ping

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


Re: Method much slower than function?

2007-06-14 Thread Josiah Carlson
Francesco Guerrieri wrote:
> On 6/14/07, Peter Otten <[EMAIL PROTECTED]> wrote:
>> Gabriel Genellina wrote:
>> > ...
>> > py> print timeit.Timer("f2()", "from __main__ import 
>> f2").repeat(number=1)
>> > [0.42673663831576358, 0.42807591467630662, 0.44401481193838876]
>> > py> print timeit.Timer("f1()", "from __main__ import 
>> f1").repeat(number=1)
>> >
>> > ...after a few minutes I aborted the process...
>>
>> I can't confirm this.
> 
> [...]
> 
>> $ python2.5 -m timeit -s 'from join import f1' 'f1()'
>> 10 loops, best of 3: 212 msec per loop
>> $ python2.5 -m timeit -s 'from join import f2' 'f2()'
>> 10 loops, best of 3: 259 msec per loop
>> $ python2.5 -m timeit -s 'from join import f3' 'f3()'
>> 10 loops, best of 3: 236 msec per loop
> 
> On my machine (using python 2.5 under win xp) the results are:
 print timeit.Timer("f2()", "from __main__ import f2").repeat(number 
 = 1)
> [0.19726834822823575, 0.19324697456408974, 0.19474492594212861]
 print timeit.Timer("f1()", "from __main__ import f1").repeat(number 
 = 1)
> [21.982707133304167, 21.905312587963252, 22.843430035622767]
> 
> so it seems that there is a rather sensible difference.
> what's the reason of the apparent inconsistency with Peter's test?

It sounds like a platform memory resize difference.


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


Re: Moving items from list to list

2007-06-14 Thread HMS Surprise
Thanks.

That will work. The 2nd, smaller lst starts out empty but this is
easily adapted.

jh

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


Re: Method much slower than function?

2007-06-14 Thread Josiah Carlson
[EMAIL PROTECTED] wrote:
> On Jun 14, 1:10 am, Paul Rubin  wrote:
>> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
>>> take virtually the same amount of time on my machine (2.5), and the
>>> non-join version is clearer, IMO.  I'd still use join in case I wind
>>> up running under an older Python, but it's probably not a big issue here.
>> You should not rely on using 2.5
> 
> I use generator expressions and passed-in values to generators and
> other features of 2.5.

For reference, generator expressions are a 2.4 feature.

>> or even on that optimization staying in CPython.
> 
> You also shouldn't count on dicts being O(1) on lookup, or "i in
> myDict" being faster than "i in myList".

Python dictionaries (and most decent hash table implementations) may not 
be O(1) technically, but they are expected O(1) and perform O(1) in 
practice (at least for the Python implementations).  If you have 
particular inputs that force Python dictionaries to perform poorly (or 
as slow as 'i in lst' for large dictionaries and lists), then you should 
post a bug report in the sourceforge tracker.


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


Re: dynamically generated runtime methods & reflection

2007-06-14 Thread Josiah Carlson
Jay Loden wrote:
> Josiah Carlson wrote:
>> Ahh, so you want to pass the method name to the method that you are 
>> returning to be called.  No problem.
>>
>>  >>> import functools
>>  >>>
>>  >>> class foo:
>> ... def __getattr__(self, name):
>> ... return functools.partial(self.ActualMethod, name)
>> ...
>> ... def ActualMethod(self, name, *args, **kwargs):
>> ... #handle *args and **kwargs based on name!
>> ... print name, args, kwargs
>> ...
>>  >>> foo().bar('hello', world=1)
>> bar ('hello',) {'world': 1}
>>  >>>
> 
> Thanks, this is exactly what I was looking for! For some reason functools 
> didn't even show up at all during Google searches...must have just had the 
> wrong search terms.

Well, the particular operation is typically called 'currying a 
function', and unless you know what to look for, it isn't very easy to 
make happen.

On the other hand, it is also relatively easy to implement by hand if 
necessary.

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


Re: save class

2007-06-14 Thread nik
On Jun 13, 10:04 pm, Josiah Carlson <[EMAIL PROTECTED]>
wrote:
> Gabriel Genellina wrote:
> > En Wed, 13 Jun 2007 23:11:22 -0300, nik <[EMAIL PROTECTED]> escribió:
> >> It would seem that I want to actually save the source code for the
> >> class. I know that I could of course open up an editor and just make
> >> it, but my ideal would be to have the base class, Map, be able to make
> >> the sub-classes. I don't want the class definition. What I want is an
> >> actual class that I could later import and use somewhere else. I am
> >> planning to have each one of these map objects contain a different
> >> dictionary and then be able to import the map into the application,
> >> but have certain methods defined in the Map super-class to draw data
> >> out of the specific map's specific dictionary. I hope that makes
> >> sense.
>
> >> Something like,
> >> class Map:
> >>  dict = {}
> >>  def DoSomething(self):
> >>  pass
>
> >>  def MakeNewMapSubClass(self, newclassname):
> >>  """ make a new file, newclassname.py that contains a new
> >> class
> >>  newclassname(Map) that inherits from base-class Map.
>
> > And are you sure you actually need different subclasses? Will you
> > construct them several instances of each subclass? From the above
> > description I feel you want just different Map *instances*, each with
> > its own dict, not different *subclasses*.
>
> What you said, and that his solution sounds like a Java approach to the
> problem (subclass an abstract base class that calls specific methods on
> the subclass to "do the right thing").
>
> To offer the OP source he can use...
>
> class Map:
>  def __init__(self):
>  self.dict = {}
>  def DoSomething(self):
>  #do something with self.dict
>
> Every instance gets a new dictionary.  Now, if he actually wants to
> change the behavior of the DoSomething method, of course then it would
> make sense to subclass Map.
>
>   - Josiah

I am hoping to change the self.dict for each subclass. I realize that
I could save self.dict to file and then load in different dicts each
time I get a new instance of class. But I want to be able to make
subclasses of map that each have different self.dict. Then when I need
to use them, just import the module and use the specific dict, instead
of having to keep track of a separate dictionary file. I am new to
this, but I thought that this would be a regular thing to do in
python, because people must make classes in the interactive console
and then export them somehow for later use.

Thank you for your responses.

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

Re: How can I capture all exceptions especially when os.system() fail? Thanks

2007-06-14 Thread mike
On Jun 14, 2:55 am, Michael Hoffman <[EMAIL PROTECTED]> wrote:
> Gabriel Genellina wrote:
> > En Wed, 13 Jun 2007 21:47:16 -0300, mike <[EMAIL PROTECTED]> escribió:
>
> >> Following piece of code can capture IOError when the file doesn't
> >> exist, also, other unknown exceptions can be captured when I press
> >> Ctrl-C while the program is sleeping(time.sleep). Now the question is:
> >> when I run the non-exist command, the exception cannot be captured.
>
> >> So far so good, then I changed the code to run a non-exist command
> >> "wrong_command_test"(commented the open and sleep lines), then the
> >> script printed:
> >> sh: wrong_command_test: command not found
> >> well Done
>
> > That's because it is not an exception, it is an error message coming
> > from your shell, not from Python.
>
> Of course if you use subprocess.check_call() instead of os.system(), it
> will become an exception (CalledProcessError).
> --
> Michael Hoffman

Really helps. Thanks Michael

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

Re: Method much slower than function?

2007-06-14 Thread [EMAIL PROTECTED]
On Jun 14, 1:10 am, Paul Rubin  wrote:
> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> > take virtually the same amount of time on my machine (2.5), and the
> > non-join version is clearer, IMO.  I'd still use join in case I wind
> > up running under an older Python, but it's probably not a big issue here.
>
> You should not rely on using 2.5

I use generator expressions and passed-in values to generators and
other features of 2.5.  Whether or not to rely on a new version is
really a judgement call based on how much time/effort/money the new
features save you vs. the cost of losing portability to older
versions.

> or even on that optimization staying in CPython.

You also shouldn't count on dicts being O(1) on lookup, or "i in
myDict" being faster than "i in myList".  A lot of quality of
implementation issues outside of the language specification have to be
considered when you're worried about running time.

Unlike fast dictionary lookup at least the += optimization in CPython
is specified in the docs (as well as noting that "".join is greatly
preferred if you're working across different versions and
implementations).

> Best is to use StringIO or something comparable.

Yes, or the join() variant.

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


Efficient way of generating original alphabetic strings like unix file "split"

2007-06-14 Thread py_genetic
Hi,

I'm looking to generate x alphabetic strings in a list size x.  This
is exactly the same output that the unix command "split" generates as
default file name output when splitting large files.

Example:

produce x original, but not random strings from english alphabet, all
lowercase.  The length of each string and possible combinations is
dependent on x.  You don't want any repeats.

[aaa, aab, aac, aad,  aax, .. bbc, bbd,  bcd]

I'm assumming there is a slick, pythonic way of doing this, besides
writing out a beast of a looping function.  I've looked around on
activestate cookbook, but have come up empty handed.  Any suggestions?

Thanks,
Conor

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


Re: SimplePrograms challenge

2007-06-14 Thread Steven Bethard
rzed wrote:
> Steven Bethard <[EMAIL PROTECTED]> wrote in
>> def iter_primes():
>>  # an iterator of all numbers between 2 and +infinity
>>  numbers = itertools.count(2)
>>
>>  # generate primes forever
>>  while True
>>
>>  # generate the first number from the iterator,
>>  # which should always be a prime
>>  prime = numbers.next()
>>  yield prime
>>
>>  # lazily remove all numbers from the iterator that
>>  # are divisible by prime we just selected
>>  numbers = itertools.ifilter(prime.__rmod__, numbers)
>>
>> I think that's 17-ish, though you could shrink it down by
>> removing some of the spaces.
> 
> How about including a driver?

Yes, absolutely a good idea. Fortunately, the other Steve borrowed the 
time machine already and added this to the end::

 for p in iter_primes():
 if p > 1000: break
 print p

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

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


Re: Method much slower than function?

2007-06-14 Thread [EMAIL PROTECTED]
On Jun 14, 1:12 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Thu, 14 Jun 2007 01:39:29 -0300, [EMAIL PROTECTED]
> <[EMAIL PROTECTED]> escribió:
>
>
>
> > Gabriel Genellina wrote:
> >> In addition, += is rather inefficient for strings; the usual idiom is
> >> using ''.join(items)
>
> > Ehh.  Python 2.5 (and probably some earlier versions) optimize += on
> > strings pretty well.
>
> > a=""
> > for i in xrange(10):
> > a+="a"
>
> > and:
>
> > a=[]
> > for i in xrange(10):
> > a.append("a")
> > a="".join(a)
>
> > take virtually the same amount of time on my machine (2.5), and the
> > non-join version is clearer, IMO.  I'd still use join in case I wind
> > up running under an older Python, but it's probably not a big issue
> > here.
>
> Yes, for concatenating a lot of a's, sure... Try again using strings
> around the size of your expected lines - and make sure they are all
> different too.
>
> py> import timeit
> py>
> py> def f1():
> ...   a=""
> ...   for i in xrange(10):
> ...   a+=str(i)*20
> ...
> py> def f2():
> ...   a=[]
> ...   for i in xrange(10):
> ...   a.append(str(i)*20)
> ...   a="".join(a)
> ...
> py> print timeit.Timer("f2()", "from __main__ import f2").repeat(number=1)
> [0.42673663831576358, 0.42807591467630662, 0.44401481193838876]
> py> print timeit.Timer("f1()", "from __main__ import f1").repeat(number=1)
>
> ...after a few minutes I aborted the process...

Are you using an old version of python?  I get a fairly small
difference between the 2:

Python 2.5 (r25:51908, Jan 23 2007, 18:42:39)
[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on ELIDED
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> a=""
>>> def f1():
...   a=""
...   for i in xrange(10):
...  a+=str(i)*20
...
>>> def f2():
...   a=[]
...   for i in xrange(10):
... a.append(str(i)*20)
...   a="".join(a)
...
>>> print timeit.Timer("f2()", "from __main__ import f2").repeat(number=1)
[0.91355299949645996, 0.86561012268066406, 0.84371185302734375]
>>> print timeit.Timer("f1()", "from __main__ import f1").repeat(number=1)
[0.94637894630432129, 0.89946198463439941, 1.170320987701416]

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

Re: qt4 setFlags

2007-06-14 Thread Diez B. Roggisch
luca72 schrieb:
> Hello
> 
> I make this code:
> row = self.tableWidget.rowCount()
> for a in range(row):
> self.tableWidget.item(row, 0).setFlags(Qt.IsSelectable)
> 
> i have this erroror :
> 
> global name Qt is not definied

import Qt might help. And reading the python tutorial.

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


lagrange multipliers in python

2007-06-14 Thread [EMAIL PROTECTED]
Hi all,

Sorry for the cross-posting.

I'm trying to find the minimum of a multivariate function F(x1, x2, ...,
xn) subject to multiple constraints G1(x1, x2, ..., xn) = 0, G2(...) =
0, ..., Gm(...) = 0.

The conventional way is to construct a dummy function Q,

$$Q(X, \Lambda) = F(X) + \lambda_1 G1(X) + \lambda_2 G2(X) + ... + \lambda_m 
Gm(X)$$

and then calculate the value of X and \Lambda when the gradient of function Q 
equals 0.

I think this is a routine work, so I want to know if there are available
functions in python(mainly scipy) to do this? Or maybe there is already
a better way in python?

I have googled but haven't found helpful pages.

Thanks a lot.

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


Re: OS X install confusion

2007-06-14 Thread Ted
On Jun 14, 1:31 pm, Kevin Walzer <[EMAIL PROTECTED]> wrote:
> John Fisher wrote:
> > Hi Groupies,
>
> > I have an Intel Macbook running OS X 10.4.
>
> > It came installed with Python 2.3.5. I have since installed MacPython
> > with version 2.4.4, cool.
>
> > When I open a bash terminal session and type python, it brings up
> > version 2.3.5. If I type IDLE it brings up version 2.4.4.
>
> > My question: what do I have to do to get it to bring up 2.4.4 with the
> > "python" command?
>
> > Thanks for bringing light to my ignorance.
>
> > JF
>
> Sounds like a path problem. Apple's system Python is installed in
> /usr/bin. Your installation is probably in /usr/local/bin. Edit your
> profile or use the full path.
>
> --
> Kevin Walzer
> Code by Kevinhttp://www.codebykevin.com

The default python on tiger (2.3.5) is sym-linked to /usr/bin/python
and /usr/bin/pythonw.

I found it easier to relink to the new installation path. This also
leaves /usr/bin/python23 and /usr/bin/pythonw23 still linked to the
original version if you want to quickly check something.

Cheers,
Ted

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


Re: Method much slower than function?

2007-06-14 Thread Peter Otten
Chris Mellon wrote:

> On 6/14/07, Peter Otten <[EMAIL PROTECTED]> wrote:
>> Peter Otten wrote:
>>
>> > Leo Kislov wrote:
>> >
>> >> On Jun 13, 5:40 pm, [EMAIL PROTECTED] wrote:
>> >>> Hi all,
>> >>>
>> >>> I am running Python 2.5 on Feisty Ubuntu. I came across some code
>> >>> that is substantially slower when in a method than in a function.
>> >>>
>> >>> >>> cProfile.run("bar.readgenome(open('cb_foo'))")
>> >>>
>> >>>  20004 function calls in 10.214 CPU seconds
>> >>
>> >>> >>> cProfile.run("z=r.readgenome(open('cb_foo'))")
>> >>>
>> >>>  20004 function calls in 0.041 CPU seconds
>> >>>
>> >>
>> >> I suspect open files are cached so the second reader
>> >> picks up where the first one left: at the of the file.
>> >> The second call doesn't do any text processing at all.
>> >>
>> >>   -- Leo
>> >
>> > Indeed, the effect of attribute access is much smaller than what the OP
>> > is seeing:
>>
>> I have to take that back
>>
> 
> Your tests (which I have snipped) show attribute access being about 3x
> slower than local access, which is consistent with my own tests. The
> OP is seeing a speed difference of 2 orders of magnitude. That's far
> outside the range that attribute access should account for.

Not if it conspires to defeat an optimization for string concatenation

$ cat iadd.py
class A(object):
def add_attr(self):
self.x = ""
for i in xrange(1):
self.x += " yadda"
def add_local(self):
x = ""
for i in xrange(1):
x += " yadda"

add_local = A().add_local
add_attr = A().add_attr
$ python2.5 -m timeit -s'from iadd import add_local' 'add_local()'
100 loops, best of 3: 3.15 msec per loop
$ python2.5 -m timeit -s'from iadd import add_attr' 'add_attr()'
10 loops, best of 3: 83.3 msec per loop

As the length of self.x grows performance will continue to degrade.
The original test is worthless as I tried to explain in the section you
snipped.

Peter

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


Re: OS X install confusion

2007-06-14 Thread Kevin Walzer
John Fisher wrote:
> Hi Groupies,
> 
> I have an Intel Macbook running OS X 10.4.
> 
> It came installed with Python 2.3.5. I have since installed MacPython
> with version 2.4.4, cool.
> 
> When I open a bash terminal session and type python, it brings up
> version 2.3.5. If I type IDLE it brings up version 2.4.4. 
> 
> My question: what do I have to do to get it to bring up 2.4.4 with the
> "python" command?
> 
> Thanks for bringing light to my ignorance.
> 
> JF

Sounds like a path problem. Apple's system Python is installed in 
/usr/bin. Your installation is probably in /usr/local/bin. Edit your 
profile or use the full path.

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-14 Thread Steven D'Aprano
On Thu, 14 Jun 2007 00:40:12 +, idoerg wrote:

> Hi all,
> 
> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
> is substantially slower when in a method than in a function.


After further testing, I think I have found the cause of the speed
difference -- and it isn't that the code is a method.

Here's my test code:


def readgenome(filehandle):
s = ""
for line in filehandle.xreadlines():
s += line.strip()

class SlowClass:
def readgenome(self, filehandle):
self.s = ""
for line in filehandle.xreadlines():
self.s += line.strip()

class FastClass:
def readgenome(self, filehandle):
s = ""
for line in filehandle.xreadlines():
s += line.strip()
self.s = s


Now I test them. For brevity, I am leaving out the verbose profiling
output, and just showing the total function calls and CPU time.


>>> import cProfile
>>> cProfile.run("readgenome(open('cb_foo'))")
 20005 function calls in 0.071 CPU seconds

>>> cProfile.run("SlowClass().readgenome(open('cb_foo'))")
 20005 function calls in 4.030 CPU seconds

>>> cProfile.run("FastClass().readgenome(open('cb_foo'))")
 20005 function calls in 0.077 CPU seconds


So you can see that the slow-down for calling a method (compared to a
function) is very small.

I think what we're seeing in the SlowClass case is the "normal" speed of
repeated string concatenations. That's REALLY slow. In the function and
FastClass cases, the compiler optimization is able to optimize that slow
behaviour away.

So, nothing to do with methods vs. functions, and everything to do with
the O(N**2) behaviour of repeated string concatenation.


-- 
Steven.

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


FTP Date Format Function

2007-06-14 Thread samuraisam
FTP LST/LIST/NLST date field formatting function for all those seekers
out there...

import time
import datetime

def ftpdateformat(value):
"""Formats dates from most FTP servers"""
if ":" in value: # within 6 months
return datetime.datetime(
*time.strptime( # have to guess this calculation
"%s %s" % (value, datetime.datetime.now().year),
"%b %d %H:%M %Y"
)[0:5]
).strftime("%B %d, %Y %H:%M")
else: # before six months
return datetime.datetime(
*time.strptime(value, "%b %d %Y")[0:5]
).strftime("%B %d, %Y")

I'm not sure if there is a proper algorithm for deciding on a proper
year within the last 6 months as it isn't given by most FTP servers.
I'd love to amend the function with the correct solution. :)

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


Re: Is there any way to catch expections when call python method in C++

2007-06-14 Thread Gabriel Genellina
En Wed, 13 Jun 2007 12:03:00 -0300, Robert Bauck Hamar  
<[EMAIL PROTECTED]> escribió:

> Allen wrote:
>
>> I use try catch, but cannot catch the execeptions of execution python
>> method.
>
> No. CPython is written in C, not C++, and C has no concept of exceptions.
> Exceptions in Python is usually indicated by return value in the
> interpreter, and has no mapping to the C++ exception model. You should
> never let C++ exceptions propagate into the python functions either.
> PyImport_ImportModule will return NULL if an exception occured, and so  
> will
> also PyObject_GetAttrString and PyEval_CallObject do.

For an example on how to do this, see "Extending and Embedding the Python  
Interpreter"  specially section 1.2

-- 
Gabriel Genellina

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


OS X install confusion

2007-06-14 Thread John Fisher
Hi Groupies,

I have an Intel Macbook running OS X 10.4.

It came installed with Python 2.3.5. I have since installed MacPython
with version 2.4.4, cool.

When I open a bash terminal session and type python, it brings up
version 2.3.5. If I type IDLE it brings up version 2.4.4. 

My question: what do I have to do to get it to bring up 2.4.4 with the
"python" command?

Thanks for bringing light to my ignorance.

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


Re: Moving items from list to list

2007-06-14 Thread Evan Klitzke
On 6/14/07, HMS Surprise <[EMAIL PROTECTED]> wrote:
>
> Just wondered if there was some python idiom for moving a few items
> from one list to another. I often need to delete 2 or 3 items from one
> list and put them in another. Delete doesn't seem to have a return
> value. I don't care which items I get so now I just use a couple of
> pops or a for loop for more than two.

I'm not sure if this is what you're asking, but if the elements in the
list are contiguous you can just use list slicing/addition, like this:

a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]

b = a[2:] + b
a = a[:2]

Now the contents of a and b respectively are a = [1, 2] and b = [3, 4,
5, 6, 7, 8, 9, 10].

-- 
Evan Klitzke <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-14 Thread Grant Edwards
On 2007-06-14, Steven D'Aprano <[EMAIL PROTECTED]> wrote:

> However, having said that, the speed difference does seem to be real: even
> when I correct the above issue, I get a large time difference using
> either cProfile.run() or profile.run(), and timeit agrees:
>
 f = bar().readgenome
 timeit.Timer("f(open('cb_foo'))", "from __main__ import f").timeit(5)
> 18.515995025634766
 timeit.Timer("readgenome(open('cb_foo'))", "from __main__ import 
 readgenome").timeit(5)
> 0.1940619945526123
>
> That's a difference of two orders of magnitude, and I can't see why.

Is it independent of the test order?

What happens when you reverse the order?

What happens if you run the same test twice in a row?

-- 
Grant Edwards   grante Yow! Thank god!! ... It's
  at   HENNY YOUNGMAN!!
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-14 Thread Steven D'Aprano
On Thu, 14 Jun 2007 00:40:12 +, idoerg wrote:


 cProfile.run("bar.readgenome(open('cb_foo'))")
>  20004 function calls in 10.214 CPU seconds

This calls the method on the CLASS, instead of an instance. When I try it,
I get this:

TypeError: unbound method readgenome() must be called with bar instance as
first argument (got file instance instead)

So you're running something subtly different than what you think you're
running. Maybe you assigned bar = bar() at some point?

However, having said that, the speed difference does seem to be real: even
when I correct the above issue, I get a large time difference using
either cProfile.run() or profile.run(), and timeit agrees:

>>> f = bar().readgenome
>>> timeit.Timer("f(open('cb_foo'))", "from __main__ import f").timeit(5)
18.515995025634766
>>> timeit.Timer("readgenome(open('cb_foo'))", "from __main__ import 
>>> readgenome").timeit(5)
0.1940619945526123

That's a difference of two orders of magnitude, and I can't see why.


-- 
Steven.

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


Re: In C extension .pyd, sizeof INT64 = 4?

2007-06-14 Thread Laurent Pointal
Allen wrote:

> On 6 13 ,   11 55 , "Martin v. Löwis" <[EMAIL PROTECTED]> wrote:
>> > I used INT64 and initialize its value from PyArg_ParseTuple.
>> > The code is PyArg_ParseTuple(args, "l", &nValue).
>> > It should be PyArg_ParseTuple(args, "L", &nValue).
>>
>> That's still incorrect. For the L format flag, use PY_LONG_LONG,
>> not your own INT64 type. More generally: always use the type
>> documented in
>>
>> http://docs.python.org/api/arg-parsing.html
>>
>> Regards,
>> Martin
> 
> PY_LONG_LONG is decleared as __int64 on windows. There is no
> difference.

IMHO. Make your code platform independant and use your compiler capacities. 

If for an 'L' argument PyArg_ParseTuple requires a PY_LONG_LONG, then give
it a PY_LONG_LONG and assign the parsed value into an INT64 after parsing.
 
You dont know if PY_LONG_LONG or INT64 wil not be defined differently in the
future, so use them where they are specified, do the assignment, and leave
the compiler warn you if anything become invalid in the futur.

My 2 cents.

A+

Laurent.

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

Re: Method much slower than function?

2007-06-14 Thread Chris Mellon
On 6/14/07, Peter Otten <[EMAIL PROTECTED]> wrote:
> Peter Otten wrote:
>
> > Leo Kislov wrote:
> >
> >> On Jun 13, 5:40 pm, [EMAIL PROTECTED] wrote:
> >>> Hi all,
> >>>
> >>> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
> >>> is substantially slower when in a method than in a function.
> >>>
> >>> >>> cProfile.run("bar.readgenome(open('cb_foo'))")
> >>>
> >>>  20004 function calls in 10.214 CPU seconds
> >>
> >>> >>> cProfile.run("z=r.readgenome(open('cb_foo'))")
> >>>
> >>>  20004 function calls in 0.041 CPU seconds
> >>>
> >>
> >> I suspect open files are cached so the second reader
> >> picks up where the first one left: at the of the file.
> >> The second call doesn't do any text processing at all.
> >>
> >>   -- Leo
> >
> > Indeed, the effect of attribute access is much smaller than what the OP is
> > seeing:
>
> I have to take that back
>

Your tests (which I have snipped) show attribute access being about 3x
slower than local access, which is consistent with my own tests. The
OP is seeing a speed difference of 2 orders of magnitude. That's far
outside the range that attribute access should account for.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-14 Thread Peter Otten
Peter Otten wrote:

> Leo Kislov wrote:
> 
>> On Jun 13, 5:40 pm, [EMAIL PROTECTED] wrote:
>>> Hi all,
>>>
>>> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
>>> is substantially slower when in a method than in a function.
>>>
>>> >>> cProfile.run("bar.readgenome(open('cb_foo'))")
>>>
>>>  20004 function calls in 10.214 CPU seconds
>> 
>>> >>> cProfile.run("z=r.readgenome(open('cb_foo'))")
>>>
>>>  20004 function calls in 0.041 CPU seconds
>>>
>> 
>> I suspect open files are cached so the second reader
>> picks up where the first one left: at the of the file.
>> The second call doesn't do any text processing at all.
>> 
>>   -- Leo
> 
> Indeed, the effect of attribute access is much smaller than what the OP is
> seeing:

I have to take that back
 
> $ cat iadd.py
> class A(object):
> def add_attr(self):
> self.x = 0
> for i in xrange(10):
> self.x += 1
> def add_local(self):
> x = 0
> for i in xrange(10):
> x += 1
> 
> add_local = A().add_local
> add_attr = A().add_attr
> $ python2.5 -m timeit -s 'from iadd import add_local' 'add_local()'
> 10 loops, best of 3: 21.6 msec per loop
> $ python2.5 -m timeit -s 'from iadd import add_attr' 'add_attr()'
> 10 loops, best of 3: 52.2 msec per loop

Iddo, adding integers is not a good model for the effect you are seeing.
Caching, while happening on the OS-level isn't, either.

As already mentioned in this thread there is a special optimization for

some_string += another_string

in the Python C-source. This optimization works by mutating on the C-level
the string that is immutable on the Python-level, and is limited to cases
where there are no other names referencing some_string. The statement

self.s += t

is executed internally as

tmp = self.s [1]
tmp += t [2]
self.s = tmp [3]

where tmp is not visible from the Python level. Unfortunately after [1]
there are two references to the string in question (tmp and self.s) so the
optimization cannot kick in.

Peter

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


qt4 setFlags

2007-06-14 Thread luca72
Hello

I make this code:
row = self.tableWidget.rowCount()
for a in range(row):
self.tableWidget.item(row, 0).setFlags(Qt.IsSelectable)

i have this erroror :

global name Qt is not definied

Regards

Luca

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


Moving items from list to list

2007-06-14 Thread HMS Surprise

Just wondered if there was some python idiom for moving a few items
from one list to another. I often need to delete 2 or 3 items from one
list and put them in another. Delete doesn't seem to have a return
value. I don't care which items I get so now I just use a couple of
pops or a for loop for more than two.

Thanks

jh

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


Re: dynamically generated runtime methods & reflection

2007-06-14 Thread Jay Loden


Josiah Carlson wrote:
>
> Ahh, so you want to pass the method name to the method that you are 
> returning to be called.  No problem.
> 
>  >>> import functools
>  >>>
>  >>> class foo:
> ... def __getattr__(self, name):
> ... return functools.partial(self.ActualMethod, name)
> ...
> ... def ActualMethod(self, name, *args, **kwargs):
> ... #handle *args and **kwargs based on name!
> ... print name, args, kwargs
> ...
>  >>> foo().bar('hello', world=1)
> bar ('hello',) {'world': 1}
>  >>>

Thanks, this is exactly what I was looking for! For some reason functools 
didn't even show up at all during Google searches...must have just had the 
wrong search terms.

-Jay

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


Re: for web application development

2007-06-14 Thread Evan Klitzke
On 6/14/07, james_027 <[EMAIL PROTECTED]> wrote:
> My problem is looking for a web framework for python, that could
> provide a natural way of developing web app. I am avoiding to learn
> template language as much as possible.

You should definitely reconsider avoiding templates -- it's hard to
imagine building a reasonably large webapp that didn't use them at
all. Cheetah templates are a really, really good Python framework for
writing templates, and the templates themselves that can use
Python-like constructs, which make them pretty powerful.

The standard way to do web programming in Python would of course be
using something that ties into mod_python, but WSGI[1] has been
brought into the language as of Python 2.5. Between Cheetah templates
and the WSGI modules, you can put together a simple lightweight web
framework in a couple hundred lines (or less) of Python. For my own
personal website I threw together a really simple Python "framework"
that runs as a daemon and uses WSGI and Cheetah templates, and it all
ended up being only 150 lines of code, so I think this is a fairly
good solution if you don't envision a huge/complex site. If you want
to build a a much bigger site you should obviously look into the other
frameworks that are available, particularly Django.

[1] http://www.python.org/dev/peps/pep-0333/

-- 
Evan Klitzke <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-14 Thread Diez B. Roggisch
Grant Edwards schrieb:
> On 2007-06-14, Leo Kislov <[EMAIL PROTECTED]> wrote:
>> On Jun 13, 5:40 pm, [EMAIL PROTECTED] wrote:
>>> Hi all,
>>>
>>> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
>>> is substantially slower when in a method than in a function.
>>>
>> cProfile.run("bar.readgenome(open('cb_foo'))")
>>>  20004 function calls in 10.214 CPU seconds
>> cProfile.run("z=r.readgenome(open('cb_foo'))")
>>>  20004 function calls in 0.041 CPU seconds
>>>
>> I suspect open files are cached
> 
> They shouldn't be.
> 
>> so the second reader picks up where the first one left: at the
>> of the file.
> 
> That sounds like a bug.  Opening a file a second time should
> produce a "new" file object with the file-pointer at the
> beginning of the file.

It's a OS thing.

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


Re: Method much slower than function?

2007-06-14 Thread Matimus
> The first time you read the file, it has to read it from disk.
> The second time, it's probably just reading from the buffer
> cache in RAM.

I can verify this type of behavior when reading large files. Opening
the file doesn't take long, but the first read will take a while
(multiple seconds depending on the size of the file). When the file is
opened a second time, the initial read takes significantly less time.

Matt

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


Re: Python-URL! - weekly Python news and links (Jun 11)

2007-06-14 Thread Alex Martelli
Gabriel Genellina <[EMAIL PROTECTED]> wrote:

> QOTW:  "That's the Martellibot for you.  Never use a word where a paragraph
> with explanatory footnotes will do.
> 
> Sigh.  I miss him on c.l.py." - Simon Brunning

Funny -- didn't Simon write this in 2005 referring to an essay of mine
that I had posted in 2002,
 ?

Longest "week" on record, unless I'm missing something!-)


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


Re: one-time initialization of class members

2007-06-14 Thread James Turk
On Jun 13, 11:42 pm, Steven Bethard <[EMAIL PROTECTED]> wrote:
> James Turk wrote:
> > It actually occured to me that I could use a @classmethod to do the
> > loading and take that out of the BaseClass constructor.  What I have
> > makes more sense and eliminates the unecessary constructors.
>
> > ie.
>
> > class BaseClass:
> > @classmethod
> > def loadData(params):
> > #expensive load here
>
> > class ChildClass1(BaseClass):
> >  dataset = BaseClass.loadData(params)
>
> > This is pretty much along the lines of what you suggested, thank you
> > for the hint in the right direction.
>
> > I realized that this still doesn't meet my needs exactly as I only
> > want the expensive dataset to be loaded if/when a class is actually
> > used (there are potentially many of these and only a few will be
> > used).
>
> Seems like you want a lazy class attribute. How about something like::
>
>  >>> class LazyClassAttribute(object):
> ... def __init__(self, func):
> ... self.func = func
> ... def __get__(self, obj, cls=None):
> ... value = self.func(cls)
> ... setattr(cls, self.func.__name__, value)
> ... return value
> ...
>  >>> class Base(object):
> ... @LazyClassAttribute
> ... def dataset(cls):
> ... print 'calculating dataset'
> ... return 'dataset(%s)' % cls.params
> ...
>  >>> class Child1(Base):
> ... params = 'foo'
> ...
>  >>> class Child2(Base):
> ... params = 'bar'
> ...
>  >>> Child1.dataset
> calculating dataset
> 'dataset(foo)'
>  >>> Child1.dataset
> 'dataset(foo)'
>  >>> Child2.dataset
> calculating dataset
> 'dataset(bar)'
>  >>> Child2.dataset
> 'dataset(bar)'
>
> The idea is basically similar to the @classmethod approach except that
> instead of @classmethod, we use a custom descriptor that calls the
> method the first time it's accessed and then stores that value
> afterwards. This means that instead of explicitly calling the
> @classmethod, the method will be called whenever the attribute is first
> accessed.
>
> STeVe

This is a pretty interesting idea, I hadn't thought of using a
decorator to get this behavior.  I'm evaluating it and will see if it
fits in with the rest of the system well, but it certainly is a unique
solution to this problem.

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


Re: Method much slower than function?

2007-06-14 Thread Grant Edwards
On 2007-06-14, Leo Kislov <[EMAIL PROTECTED]> wrote:
> On Jun 13, 5:40 pm, [EMAIL PROTECTED] wrote:
>> Hi all,
>>
>> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
>> is substantially slower when in a method than in a function.
>>
>> >>> cProfile.run("bar.readgenome(open('cb_foo'))")
>>
>>  20004 function calls in 10.214 CPU seconds
>
>> >>> cProfile.run("z=r.readgenome(open('cb_foo'))")
>>
>>  20004 function calls in 0.041 CPU seconds
>>
>
> I suspect open files are cached

They shouldn't be.

> so the second reader picks up where the first one left: at the
> of the file.

That sounds like a bug.  Opening a file a second time should
produce a "new" file object with the file-pointer at the
beginning of the file.

> The second call doesn't do any text processing at all.

-- 
Grant Edwards   grante Yow! I'm using my X-RAY
  at   VISION to obtain a rare
   visi.comglimpse of the INNER
   WORKINGS of this POTATO!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Build EXE on Mac OsX 10.4

2007-06-14 Thread Paul McNett
Sherm Pendley wrote:
> "Gabriel Genellina" <[EMAIL PROTECTED]> writes:
> 
>> En Wed, 13 Jun 2007 17:35:19 -0300, Paul McNett <[EMAIL PROTECTED]> escribió:
>>
>>> Tempo wrote:
 Has anyone sucesfully built a *.exe file on a mac operating system
 before from a *.py file? I have been trying to do this with
 pyinstaller, but I keep getting errors and I don't know how to
 install  [...]
>>> You need to build Mac Apps on Mac, Windows EXE's on Windows, and Linux
>>> ELF's on Linux. You can't build a windows.exe from Mac, just as you
>>> can't build a mac.app from Windows.
>> That's not entirely true. gcc on linux can generate a Windows EXE, and
>> using: python setup.py bdist_wininst, you can generate a complete
>> binary  installer for Windows. I'm not sure if this can be done on a
>> Mac too.
> 
> In principle, certainly - there's even a MacPort package for a complete
> cygwin installation. I've built a number of packages with it - SDL and
> several related libraries, for instance. There are also ELF cross-compiler
> MacPort packages, presumably for building Linux binaries.
> 
> On the other hand, I *haven't* tried any of those compilers with setup.py,
> and I have no idea if it can support those targets in practice. :-(

There's the rub. In practice, it is hard enough getting my setup.py 
correct for building a nice windows.exe from Windows. Plus, the first 
thing I want to do after building an exe is *test* it on the target 
platform...

-- 
pkm ~ http://paulmcnett.com
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to save python codes in files?

2007-06-14 Thread BartlebyScrivener
On Jun 13, 12:04 am, why? <[EMAIL PROTECTED]> wrote:
> Im working with Python 2.2 on my red hat linux system. Is there any
> way to write python codes in separate files and save them so that i
> can view/edit them in the future? Actually I've just started with
> python and would be grateful for a response. Thanx!

In addition to the help you've already received, you need:

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

It doesn't seem to be loading at the moment, but it will soon, I
suspect.

Otherwise, go to

http://tinyurl.com/w7wgp

Skip the installation instructions (for Windows) and go to the four
references and links under the image of the PythonWin interpreter.

HTH

rd

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


Re: xmlrpclib hangs execution

2007-06-14 Thread itkovian
On Jun 14, 3:10 pm, [EMAIL PROTECTED] wrote:
> Hi,
>
> > 2. The Python implementation ofxmlrpcis not very robust. It just waits for
> > the connection to close. A well-written client (like your Java client)
> > would detect the presence of a Content-Length header and use that.
>
> I'm facing a similar ordeal here. I think the right thing to do would
> be to adjust the xmlrpc library to parse the header and check for the
> Content-Length. Right now, the socket seems to read 1024 bytes, so
> naturally, when the connection closes, the socket throws an error,
> because it thinks that more bytes are coming. Which sounds rather
> weird to me, given the fact that in rare cases the reply will be a
> multiple of 1024 bytes :-)
>
> -- Andy Georges

For now, these changes helped me out. It probably (read: certainly)
needs some cleanup, and I'm probably not taking everything into
account as I should:

--- xmlrpclib.py2007-06-14 15:42:36.0 +0200
+++ /sw/lib/python2.5/xmlrpclib.py  2006-11-29 02:46:38.0
+0100
@@ -1184,11 +1184,6 @@

 errcode, errmsg, headers = h.getreply()

-expected_payload_length = 1024
-if headers.has_key('content-length'):
-  expected_payload_length = int(headers['content-length'])
-
-
 if errcode != 200:
 raise ProtocolError(
 host + handler,
@@ -1203,7 +1198,7 @@
 except AttributeError:
 sock = None

-return self._parse_response(h.getfile(), sock,
expected_payload_length)
+return self._parse_response(h.getfile(), sock)

 ##
 # Create parser.
@@ -1323,23 +1318,21 @@
 #could not be accessed).
 # @return Response tuple and target method.

-def _parse_response(self, file, sock, size=1024):
+def _parse_response(self, file, sock):
 # read response from input file/socket, and parse it

 p, u = self.getparser()

 while 1:
 if sock:
-response = sock.recv(size)
+response = sock.recv(1024)
 else:
-response = file.read(size)
+response = file.read(1024)
 if not response:
 break
 if self.verbose:
 print "body:", repr(response)
 p.feed(response)
-if len(response) == size:
-break

 file.close()
 p.close()


-- Andy Georges

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


Re: xmlrpclib hangs execution

2007-06-14 Thread itkovian
Hi,

> 2. The Python implementation ofxmlrpcis not very robust. It just waits for
> the connection to close. A well-written client (like your Java client)
> would detect the presence of a Content-Length header and use that.

I'm facing a similar ordeal here. I think the right thing to do would
be to adjust the xmlrpc library to parse the header and check for the
Content-Length. Right now, the socket seems to read 1024 bytes, so
naturally, when the connection closes, the socket throws an error,
because it thinks that more bytes are coming. Which sounds rather
weird to me, given the fact that in rare cases the reply will be a
multiple of 1024 bytes :-)

-- Andy Georges

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


Re: Convert String to Int and Arithmetic

2007-06-14 Thread Facundo Batista
tereglow wrote:


> cpuSpeed = 'Speed: 10'
>
> What I would like to do is extract the '10' from the string,
> and divide that by 1000 twice to get the speed of a processor in MHz.

>>> cpuSpeed = 'Speed: 10'
>>> p = cpuSpeed.split(":")
>>> p
['Speed', ' 10']
>>> p[1]
' 10'
>>> v = int(p[1])
>>> v
10
>>> v / 100
1000
>>> 

-- 
.   Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/


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


Re: Method much slower than function?

2007-06-14 Thread Neil Cerutti
On 2007-06-14, Bruno Desthuilliers
<[EMAIL PROTECTED]> wrote:
> Neil Cerutti a écrit :
> (snip)
>> class bar:
>> def readgenome(self, filehandle):
>> self.s = ''.join(line.strip() for line in filehandle)
>
>=>
>self.s = ''.join(line.strip() for line in filehandle if not 
> '>' in line)

Thanks for that correction.

-- 
Neil Cerutti
I don't know what to expect right now, but we as players have to do what we've
got to do to make sure that the pot is spread equally. --Jim Jackson
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >