Re: How to launch a function at regular time intervals ?

2009-08-14 Thread Frank Millman
On Aug 14, 12:52 am, David  wrote:
>
> Yes, I guess it would be more simple. Here is really what I am trying
> to do. I simplified the functions, but the purpose is to write some
> text in a local file every x seconds (here, I'm just writing the
> timestamp, i.e. a string representing the date & time, every 10
> seconds) and to transfer this file to a distant server via FTP every y
> seconds (20 seconds in the example below). My code is a little bit
> more complicated because each time I transfer the file, I delete the
> local file which is then recreated when data is written, but for
> simplicity I left this out in the code below. So, here is the code
> I've been using to test Frank's code. I've been struggling with using
> or not a While True loop or not, and everything I try seems to run
> into issues.
>
> import threading
> from datetime import datetime
> import ftplib
>
> class CFtpConnection:
> """FTP Connection parameters"""
> def __init__(self, host, port, timeout, user, passwd):
> self.host = ""
> self.port = 21
> self.timeout = 60
> self.user = ""
> self.passwd = ""
>
> class CStoreData(threading.Thread):
> """Write timestamp in a file every 10 seconds in separate
> thread"""
>
> def __init__(self, timestamp):
> threading.Thread.__init__(self)
> self.event = threading.Event()
> self.timestamp = timestamp
>
> def run(self):
> while not self.event.is_set():
> file_handler = open("Test.txt", 'a')
> file_handler.write(self.timestamp.strftime("%y%m%d%H%M%S
> \n"))
> file_handler.close()
> self.event.wait(10)
>
> def stop(self):
> self.event.set()
>
> class CTransferData(threading.Thread):
> """Transfer timestamp file every 20 seconds in separate thread"""
>
> def __init__(self, ftp_connection):
> threading.Thread.__init__(self)
> self.event = threading.Event()
> self.ftp_connection = ftp_connection
>
> def run(self):
> while not self.event.is_set():
> file_handler = open("Test.txt", 'r')
> Ftp_handler = ftplib.FTP('')
> Ftp_handler.connect(self.ftp_connection.host,
> self.ftp_connection.port, self.ftp_connection.timeout)
> Ftp_handler.login(self.ftp_connection.user,
> self.ftp_connection.passwd)
> Ftp_handler.storbinary("STOR Test.txt", file_handler)
> file_handler.close()
> Ftp_handler.close()
> self.event.wait(20)
>
> def stop(self):
> self.event.set()
>
> ftp_connection = CFtpConnection("", 21, 60, "", "")
> ftp_connection.host = '127.0.0.1'
> ftp_connection.user = "admin"
> ftp_connection.passwd = "admin"
>
> while(1):
>   timestamp = datetime.now()
>   func_store_data = CStoreData(timestamp)
>   func_store_data.start()
>
>   func_transfer_data = CTransferData(ftp_connection)
>   func_transfer_data.start()
>
> func_store_data.stop()
> func_store_data.join()
>
> func_transfer_data.stop()
> func_transfer_data.join()

Hi David

I think that the main problem is your use of  while(1) (usually written as 
'while 1' BTW). The effect is that you create multiple copies of CStoreData 
and CTransferData, each running simultaneously. As you bind them to the same 
name each time, the previous one presumably gets garbage collected. I don't 
know if this effectively kills the running thread or not. Either way, this 
is not what you want to happen.

Here are some initial thoughts -

1. You say that you want the main program to continue running in the 
background. Personally I find it easier to think of the main program running 
in the foreground, and each of the functions running in the background.

2. As you speak of launching several functions, you may want to keep a list 
of them to make it easier to stop them at the end.

3. Instead of passing 'timestamp' as an argument to each function, it is 
probably better to call datatime.now() within the function itself.

Based on these thoughts, maybe your program should look something like 
this -

running_functions = []

while 1:
if some_condition:  # start function to store data
func = CStoreData()
func.start()
running_functions.append(func)

   if some_other_condition:  # start function to transfer data
func = CTransferData()
func.start()
running_functions.append(func)

if end_condition:  # terminate program
break  # break out of while loop

time.sleep(0.1)  # to avoid hogging the cpu

# stop all running functions at end
for func in running_functions:
func.stop()
func.join()

I hope this gives you some ideas.

Frank


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


Re: Nice copy in interactive terminal

2009-08-14 Thread casebash
I mainly develop on Linux these days, but if I ever end up doing
anything on windows I'll make sure to look at that.

On Aug 13, 6:56 pm, "Elias Fotinis \(eliasf\)" 
wrote:
> "casebash" wrote:
> > I've been wondering for a while if there exists an interactive
> > terminal which has nice copy feature (ie. I can copy code without
> > getting the >>> in front of every line).
>
> It would help if we knew what platform you're interested in -- your
> User-Agent is G2/1.0, but I don't know what that is.  :o)
>
> On Windows, PythonWin can copy from the interactive window without the
> prompts.

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


socket.send : (11, 'Resource temporarily unavailable')

2009-08-14 Thread Gabriel Rossetti

Hello everyone,

I get a (11, 'Resource temporarily unavailable') error when I try to 
send a file using a socket. Is there s size limit? I tried sending a 
smaller file and ii poses no problem. Am I doing something wrong? Here 
is the code:


def sendMessage(host, port, msg):

   if isinstance(msg, unicode):
   msg = msg.encode("utf-8")
  
   sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

   sock.connect((host, port))
   sock.setblocking(0)
   totalsent = 0
   while totalsent < len(msg):
   sent = sock.send(msg[totalsent:])
   if sent == 0:
   raise RuntimeError, "socket connection broken"
   totalsent = totalsent + sent
   sock.close()

Thank you,
Gabriel
--
http://mail.python.org/mailman/listinfo/python-list


Re: OptionParser How to: prog [options] [arguments]

2009-08-14 Thread Javier Collado
Hello,

I think that this isn't possible with optparse library.

However, it's possible with argparse (http://code.google.com/p/argparse/):
 http://argparse.googlecode.com/svn/trunk/doc/other-methods.html#sub-commands

It's not a standard library, but it's worth to take a look at it.

Best regards,
Javier

2009/8/14 Steven Woody :
> Hi,
> I am using OptionParser, but I've not managed figure out a way to support
> what I wanted command line format "prog  [options] [arguments]".
> E.g., "svn ls -r123 http://hello.world".   Can I do this using OptionParser?
> Thanks.
> --
> Life is the only flaw in an otherwise perfect nonexistence
>    -- Schopenhauer
>
> narke
> public key at http://subkeys.pgp.net:11371 ([email protected])
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Plotting Quadratic Functions, pygame

2009-08-14 Thread Esmail

Hello Senad,

You might find this style guide useful too in your toolbox of
Python skills & tricks.

  http://www.python.org/dev/peps/pep-0008/

This is a great and helpful group of people here, we are lucky to
have access to groups like this.

Best,

Esmail

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


Re: Plotting Quadratic Functions, pygame

2009-08-14 Thread Esmail

Hello Senad,

You might find this style guide useful too in your toolbox of
Python skills & tricks.

  http://www.python.org/dev/peps/pep-0008/

This is a great and helpful group of people here, we are lucky to
have access to groups like this.

Best,

Esmail

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


Re: Format Code Repeat Counts?

2009-08-14 Thread jschwab
Thanks all! That was most helpful and informative.

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


Pygresql, and query meta informations

2009-08-14 Thread durumdara
Hi!

Pygresql, DB-API.

I search for a solution to get meta information about last query,
because I must export these infos to Delphi.

Delphi have TDataSet, and it have meta structure that must be defined
before I create it.
For char/varchar fields I must define their sizes!

Pygresql is not retreive the field sizes.

Ok, it have solution that CHAR fields values have full size; but
varchars are not.
Ok, secondary I can calc all field lengths before I export, and I can
set THIS CALCULATED size to the field, but this is data dependent.

If I have NULL only, the field size = 0. Next query get 71 to the
field len. Next query is 234...

So I wanna ask that
a.) have I some special way in Pygresql to retreive the char/varchar
field's length?
b.) if not, how to I realize this (other ways)?

Thanks for your help:
   dd

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


Re: socket.send : (11, 'Resource temporarily unavailable')

2009-08-14 Thread Gabriel Rossetti

Gabriel Rossetti wrote:

Hello everyone,

I get a (11, 'Resource temporarily unavailable') error when I try to 
send a file using a socket. Is there s size limit? I tried sending a 
smaller file and ii poses no problem. Am I doing something wrong? Here 
is the code:


def sendMessage(host, port, msg):

   if isinstance(msg, unicode):
   msg = msg.encode("utf-8")
 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   sock.connect((host, port))
   sock.setblocking(0)
   totalsent = 0
   while totalsent < len(msg):
   sent = sock.send(msg[totalsent:])
   if sent == 0:
   raise RuntimeError, "socket connection broken"
   totalsent = totalsent + sent
   sock.close()

Thank you,
Gabriel


Actually, the original code didn't have the sock.setblocking(0), the 
problem I am trying to find is that the server does have 
sock.setblocking(0) (I can't change that) and it is getting the same 
error as my client has with the sock.setblocking(0), except it gets it 
on the accept() call. I tried modifying my code to be like this :


def sendMessage(host, port, msg):
  
   if isinstance(msg, unicode):

   msg = msg.encode("utf-8")
  
   burstSize = 4096
  
   sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

   sock.connect((host, port))
   while msg:
   sent = sock.send(msg[:burstSize])
   print "Sending %d bytes..." % sent
   if sent == 0:
   raise RuntimeError, "socket connection broken"
   msg = msg[burstSize:]
   sock.close()

thinking maybe if I send small parts it would work better but this does 
not seem to change anything.
How are large msgs sent w/ a socket in python to a non-blocking server? 
The msg I'm trying to send is 175213 bytes long.


Thank,
Gabriel
--
http://mail.python.org/mailman/listinfo/python-list


Re: socket.send : (11, 'Resource temporarily unavailable')

2009-08-14 Thread Hendrik van Rooyen
On Friday 14 August 2009 09:15:34 Gabriel Rossetti wrote:
> Hello everyone,
>
> I get a (11, 'Resource temporarily unavailable') error when I try to
> send a file using a socket. Is there s size limit? I tried sending a
> smaller file and ii poses no problem. Am I doing something wrong? Here
> is the code:
>
> def sendMessage(host, port, msg):
>
> if isinstance(msg, unicode):
> msg = msg.encode("utf-8")
>
> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> sock.connect((host, port))
> sock.setblocking(0)

This is the problem - if the socket does not block, it will return
the " temporarily unavailable" error when it is busy.

If you want to use it non blocking then you have to handle the
error in a try except, and loop.

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


Re: socket.send : (11, 'Resource temporarily unavailable')

2009-08-14 Thread Hendrik van Rooyen
On Friday 14 August 2009 09:47:50 Gabriel Rossetti wrote:
> Gabriel Rossetti wrote:
8< --
>
> Actually, the original code didn't have the sock.setblocking(0), the
> problem I am trying to find is that the server does have
> sock.setblocking(0) (I can't change that) and it is getting the same
> error as my client has with the sock.setblocking(0), except it gets it
> on the accept() call. I tried modifying my code to be like this :
>
> def sendMessage(host, port, msg):
>
> if isinstance(msg, unicode):
> msg = msg.encode("utf-8")
>
> burstSize = 4096
>
> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> sock.connect((host, port))
> while msg:
> sent = sock.send(msg[:burstSize])
> print "Sending %d bytes..." % sent
> if sent == 0:
> raise RuntimeError, "socket connection broken"
> msg = msg[burstSize:]
> sock.close()
>
> thinking maybe if I send small parts it would work better but this does
> not seem to change anything.
> How are large msgs sent w/ a socket in python to a non-blocking server?
> The msg I'm trying to send is 175213 bytes long.

Google for netstring, and also look at sock.sendall instead of send.

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


Re: Plotting Quadratic Functions, pygame

2009-08-14 Thread Bearophile
Senad Ibraimoski Of Belgrade:
> Hello, I'm a new guy to this group, my professor recommend this group
> to me, because I was boring him with questions.I'm new to python, and
> I have problem plotting Quadratic Functions. Using module pygame.

Python is a tool you use to learn something else, but tools are
important, they also help shape the way you think. So tell your
teacher that questions about tools are important enough.

If your purpose is to learn something then using Pygame to plot
functions is OK. If your purpose is just to plot them, and you don't
have strange needs, then MatPlotLib can be better.

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


Re: trouble with reload

2009-08-14 Thread Steven D'Aprano
On Fri, 14 Aug 2009 13:49:19 +0900, Terry Reedy wrote:

> Dr. Phillip M. Feldman wrote:
>> According to the Python documentation, 'reload' reloads a previously
>> imported module (so that changes made via an external editor will be
>> effective). But, when I try to use this command, I get the following
>> error message:
>> 
>> TypeError: reload() argument must be module
>> 
>> Any suggestions will be appreciated.
> 
> Besides the other answers, do not use reload. It is removed in Py3
> because it cannot be made to work as people reasonably expect.

That's a damn shame, because it is very useful for interactive use once 
you get it's quirks. Is it gone-gone or just removed from built-ins? 

If the former, would the following be a reasonable replacement?

def reload(module):
if type(module) is not type(__builtins__):
raise TypeError("reload() argument must be module")
name = module.__name__
del globals()[name]
del sys.modules[name]
globals()[name] = __import__(name)


It seems to work for me, but I'm not sure if I've missed something.


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


Re: Python "and" behavior

2009-08-14 Thread ryles
On Aug 13, 8:36 pm, goldtech  wrote:
> Could you explain or link me to an explanation of this?

http://docs.python.org/tutorial/datastructures.html#more-on-conditions

Give the whole tutorial a good read.
-- 
http://mail.python.org/mailman/listinfo/python-list


Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-14 Thread Hendrik van Rooyen
In the past, on this  group, I have made statements that said that on Linux, 
the serial port handling somehow does not allow transmitting and receiving at 
the same time, and nobody contradicted me.

I am running into the self same issue again.

What I normally do is to open the port like this:

port = open("/dev/ttyS0","r+b",0)

and then I unblock it with:

def unblock(f):
"""Given file 'f', sets its unblock flag to true."""

fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

Then I can write a loop that uses a try-except to see if there are characters 
available, and that examines a queue to see if there is something to 
transmit, to give the appearance of full duplex functionality.

What I would really like is to have two threads - one that does blocking input 
waiting for a character, and one that examines an output queue and transmits 
the stuff it finds.

When I try to do this, it does not seem to work - as far as I can see, it is 
as if the underlying implementation is somehow single threaded - if it is 
waiting for a received character, it waits until something comes in before it 
will transmit anything.  So if you are talking to a device that does not 
respond, the whole thing freezes up waiting for a character that never comes, 
and nothing is transmitted either, despite the call to 
port.write(somestring).  The write blocks, and everything stops, waiting for 
the receipt to finish.

Is there a way to get full duplex, so that the transmit and receive are 
independent of each other?

Or are we stuck with a disk-like model that forces a sequence on reads and 
writes?

- Hendrik

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


Re: trouble with reload

2009-08-14 Thread Jean-Michel Pichavant

Dr. Phillip M. Feldman wrote:

Actually, I've tried both of these, and I get (different) errors in both
cases:

In [1]: from mymath import *

In [2]: reload(mymath)
NameError: name 'mymath' is not defined

In [3]: reload('mymath')
TypeError: reload() argument must be module

  

Please don't top post :o)

1/ Do not use the 'from  import *' form, unless you don't have 
any other choice, it will set in your namespace an undefined number of 
symbols, you may get some name collisions which usually lead to very 
nasty bugs.


import mymath
reload(mymath)

will do the trick.

If you are of those lazy coders you can write

import mymath as mm
reload(mm)
mm.aFunction()

If you are using a few functions and don't what to prefix them with the 
module name you can also write

import mymath
from mymath import func1, func2, func3

# reload the functions
reload(mymath)
func1 = mymath.func1
func2 = mymath.func2
func3 = mymath.func3

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


Re: trouble with reload

2009-08-14 Thread Gabriel Genellina
En Fri, 14 Aug 2009 05:34:52 -0300, Steven D'Aprano  
 escribió:

On Fri, 14 Aug 2009 13:49:19 +0900, Terry Reedy wrote:



Besides the other answers, do not use reload. It is removed in Py3
because it cannot be made to work as people reasonably expect.


That's a damn shame, because it is very useful for interactive use once
you get it's quirks. Is it gone-gone or just removed from built-ins?


Just relocated:

p3> import imp
p3> imp.reload


--
Gabriel Genellina

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


Re: OptionParser How to: prog [options] [arguments]

2009-08-14 Thread Carl Banks
On Aug 14, 12:18 am, Javier Collado  wrote:
> 2009/8/14 Steven Woody :
>
> > Hi,
> > I am using OptionParser, but I've not managed figure out a way to support
> > what I wanted command line format "prog  [options] [arguments]".
> > E.g., "svn ls -r123http://hello.world";.   Can I do this using OptionParser?
> > Thanks.
>
> Hello,
>
> I think that this isn't possible with optparse library.

It's possible if you remove sys.argv[1] before invoking optparse.
But...


> However, it's possible with argparse (http://code.google.com/p/argparse/):
>  http://argparse.googlecode.com/svn/trunk/doc/other-methods.html#sub-c...
>
> It's not a standard library, but it's worth to take a look at it.

It's more than worth looking at, it's a slam dunk.  It's superior to
optparse in every way I can think of.  Unless you don't want the third-
party dependency there is no reason to use optparse instead of
argparse.


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


Re: OptionParser How to: prog [options] [arguments]

2009-08-14 Thread Gabriel Genellina
En Fri, 14 Aug 2009 03:22:49 -0300, Steven Woody   
escribió:



I am using OptionParser, but I've not managed figure out a way to support
what I wanted command line format "prog  [options] [arguments]".
E.g., "svn ls -r123 http://hello.world";.   Can I do this using  
OptionParser?


Extract the  yourself, and pass the remaining arguments (that is,  
sys.argv[2:]) to parser.parse_args()


--
Gabriel Genellina

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


Re: implementing descriptors

2009-08-14 Thread Jean-Michel Pichavant

Emile van Sebille wrote:

On 8/13/2009 3:17 PM dippim said...

I am new to Python and I have a question about descriptors.  If I have
a class as written below, is there a way to use descriptors to be
certain that the datetime in start is always before the one in end?

class foo(object):
   def __init__(self,a = None,b = None)
  self.start = a
  self.end = b

from datetime import datetime
c = datetime(2009,8,13,6,15,0)
d = datetime(2009,8,14,12,0,0)
afoo = foo(c,d)

For instance, if the following code were run, I would like to instance
of foo to switch the start and end times.

afoo.start = datetime(2010,8,13,6,15,0)

I was thinking of using the __set__ descriptor to catch the assignment
and reverse the values if necessary, 


why not...


class foo(object):
   def __init__(self,a = None,b = None)
  self.start = min(a,b)
  self.end = max(a,b)


Emile


or

class foo(object):
   def __init__(self, start, end)
  self.start = start
  self.end = end


Problem solved by design :o)

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


Re: Python "and" behavior

2009-08-14 Thread Duncan Booth
MRAB  wrote:

> Operation  Result
>|x or y|  x if x else y
>|x and y|  y if x else x
>|not x|  False if x else False
> 
>:-)
> 

That's not a terribly good definition for the 'not' operator. Try:

  |not x| False if x else True



-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-14 Thread Diez B. Roggisch

Hendrik van Rooyen schrieb:
In the past, on this  group, I have made statements that said that on Linux, 
the serial port handling somehow does not allow transmitting and receiving at 
the same time, and nobody contradicted me.


I am running into the self same issue again.

What I normally do is to open the port like this:

port = open("/dev/ttyS0","r+b",0)


How about using pyserial? With that, I never had any problems accessing 
the the serial ports, and AFAIK no duplex-problems as well. And I 
seriously doubt that these are a python-related problem - python only 
has a very thin, direct layer above the posix-calls, and doesn't do 
anything that would explain your observed behavior. The GIL is not the 
issue here either - it won't interfer with blocking IO.


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


Re: get the pause status from amarok 2.1

2009-08-14 Thread Diez B. Roggisch

Sleepy Cabbage schrieb:
As the title says, I'm trying to find a way to get the pause status from 
amarok 2.1.


I'm running kubuntu 9.04 with kde 4.2.2, python 2.6.2.

Thanks in advance.


Not at my linux-system right now, but dcop and the respective 
python-module should help.


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


Re: need help calculating point between two coordinates.

2009-08-14 Thread Steven D'Aprano
On Thu, 13 Aug 2009 14:26:54 -0700, PeteDK wrote:

> Hi there
> 
> I'am working on a route comparison tool for carpools.
> 
> The route comparison is based on 'steps' retrieved from google maps
> GDirection. These steps vary in length and i use the coordinates at the
> beginning of each "step". However, sometimes the distance of these steps
> is too long(ex. driving 30-40 km. on the freeway). Therefore i would
> like to calculate/find the coordinate located in between two given
> coordinates.
> Lets say one step starts at:
> 56.043185,9.922714
> and ends at:
> 56.234287,9.864521
> 
> I would then like to calculate the point right in the middle of these
> coordinates.

Can you assume that the coordinate system is virtually flat between those 
points? That is, are the distances small enough that the curvature of the 
earth isn't relevant?

If so, the following should be close enough:

def midpoint(a, b):
"""Return the two-dimensional point midway between points a and b."""
x = (a[0] + b[0])/2.0
y = (a[1] + b[1])/2.0
return (x, y)

Otherwise, you can probably start here:

http://www.geomidpoint.com/methods.html

http://mathforum.org/library/drmath/results.html?contexts=drmath&levels=college&passed_id=51416&passed_title=College+Physics&search_cats=no&textsearch=spherical&textsearch_bool_type=and&topics=physics


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


Re: Database query execution times in Python?

2009-08-14 Thread pwnedd

> Look up EXPLAIN

Thanks for the suggestion. I don't see any option to have EXPLAIN display
the query time though?

-- 
View this message in context: 
http://www.nabble.com/Database-query-execution-times-in-Python--tp24870050p24969867.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: How to reset document string

2009-08-14 Thread Anand K Rayudu

Dear Carl,

Your ideas are extremely good, and I liked idea 2 especially, based on 
that I am considering following approach.

Eg: let us say I have module named myModule and exposing myModule.myAPI
So I will now rename myModule as _myModule and write a python layer with 
myModule


So my python layer will look like this

file name myModule.py

import _myModule

def myAPI(arg1, arg2):
   """
  @param  arg1 help string
  @param  arg2 help string
  
  _myModule.myAPI(arg1,arg2) # actual API

So I will provide mechanism which reads the help files and generates 
this binding python file and reload the module,

So now my python editor has the new python strings.
Also another advantages of this approach is if customer choses his own 
IDE he will still get the documentation of our APIs, by just ensuring 
these python files are in PYTHON_PATH

I think this is better approach, kindly please let me know your comments



Regards,
Anand


 




On Aug 7, 2:54 am, Anand K Rayudu  wrote:
  

Dear All,

We have extended and embedded python into my our application.
We exposed few APIs to python using

 Py_InitModule("myModuleName", myMethods);
where my methods are

static PyMethodDef VistaDbMethods[] = {
   { (char *)"myAPI",_myAPICFunctionPtr ,METH_VARARGS,"usage: MyHelp)" }

Now problem is ml_doc (Document string). Most of the time the strings
given by development team is not descriptive enough, so support team
want to enhance these docstring on need basis and supply to customer
The idea is we will provide get latest help option from application,
which will contact our webserver or allow user to pick new help
document,  which will re apply the help on fly.
 From then on our script editors will show the new enhanced help.
How do I achieve this.



Sounds very cool.  I have a few ideas.

1. Since you say you are embedding Python in your application, the
most direct way might be to modify the Python interpreter to allow the
__doc__ attribute to be changed.  You'd have to modify the PyCFunction
type (defined in methodobject.h and methodobject.c) to allow
overriding the compiled-in doc field.

2. Instead of replacing the __doc__ attribute of the function, just
replace the whole function with a wrapper.  So, for instance, if your
application decides to update the docstring for myModuleName.myAPI(),
instead of running code like this:

myModuleName.myAPI.__doc__ = 'new docstring'

run code like this:

def create_wrapper(func,docstring):
def wrapper(*args):
return func(*args)
wrapper.__doc__ = doc
return wrapper
myModuleName.myAPI = create_wrapper(
 myModuleName.myAPI,'new docstring')

So now myApi is a Python function with the new docstring that calls
the old function.  (Note: if you are concerned with efficiency, it's
possible to write a wrapper in C that has very little overhead.)

This approach has minor disadvantages (such as if any code write from
myModuleName import myAPI--it won't see the new version) but it may be
the easiest approach.

3. Instead of customizing the __doc__ attribute, store any custom
docstrings in a dictionary keyed by the function.

custom_doc[myModuleName.myApi] = 'new docstring'

Your script editor, when looking for documentation, will first search
in this custom area to see if the docstring has been overridden; if
so, use that; if not, use the docstring.  Something like this:

def documentation_to_use_in_script_editor(func):
try:
return custom_doc[func]
except KeyError:
return func.__doc__

That has the negative of the special docstring not being visible at an
interactive prompt.


I have given you some fairly vague answers, hopefully that'll give you
some idea.  It sounds like you have an ambitious project, suggesting
that you are probably good enough to implement the suggestions.


Carl Banks
  


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


python-ldap and encodings

2009-08-14 Thread Matias

Hi!

I'm using python-ldap to create some entries on my openldap server.

The problem is that some of those entries have accented characters and 
unicode text in general.


I'm wondering if there is any example or documentation on how to add
or modify ldap objects whose values contains non-ascii characters,
such as accents, and so on. As far as I understand, those values
should be encoded using base64, but if I do that, I don't know how to
indicate in the modlist that this is the encoded value and not the
value itself.

Any help will be bery appreciated.


Matias.

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


Re: Format Code Repeat Counts?

2009-08-14 Thread MRAB

Scott David Daniels wrote:

MRAB wrote:

The shortest I can come up with is:
"[" + "][".join(letters) + "]"


Maybe a golf shot:
  "][".join(letters).join("[]")


Even shorter:

"["+"][".join(letters)+"]"

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


Re: python-ldap and encodings

2009-08-14 Thread Matias

Matias wrote:

Hi!

I'm using python-ldap to create some entries on my openldap server.

The problem is that some of those entries have accented characters and 
unicode text in general.


I'm wondering if there is any example or documentation on how to add
or modify ldap objects whose values contains non-ascii characters,
such as accents, and so on. As far as I understand, those values
should be encoded using base64, but if I do that, I don't know how to
indicate in the modlist that this is the encoded value and not the
value itself.

Any help will be bery appreciated.


Matias.




Nevermind I was doing something really stupid.Don't ask please :-)

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


Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-14 Thread greg

Hendrik van Rooyen wrote:


port = open("/dev/ttyS0","r+b",0)

What I would really like is to have two threads - one that does blocking input 
waiting for a character, and one that examines an output queue and transmits 
the stuff it finds.


You can't read and write with the same stdio file object
at the same time. Odd things tend to happen if you try.

You need to open *two* file objects, one for reading
and one for writing:

  fr = open("/dev/ttyS0","rb",0)
  fw = open("/dev/ttyS0","wb",0)

and give fr to the reading thread and fw to the
writing thread.

You could also try avoiding file objects altogether
and use the raw system calls in the os module. Since
you're not using any buffering, there's little reason
to use the stdio layer. If you do that, you should be
able to use the same file descriptor for reading and
writing without any trouble.

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


anyone with genomewide microarray analysis experience ?

2009-08-14 Thread trias

Hi,

 I am trying to analyse some biological data from microarray experiments.
Different experiments have been stored in a SQL database.

 One of the things I would like to do is to fetch all data from a certain
distance from gene ATGs say 100+/- bp and calculate the bp average over all
genes over this region. 

 The microarray data has been normalised over each bp

Does anyone have some scripts I could use for this purpose. I work with
S.cerevisiae

Thank you
-- 
View this message in context: 
http://www.nabble.com/anyone-with-genomewide-microarray-analysis-experience---tp24971224p24971224.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: anyone with genomewide microarray analysis experience ?

2009-08-14 Thread Jochen Schulz
trias:
> 
>  One of the things I would like to do is to fetch all data from a certain
> distance from gene ATGs say 100+/- bp and calculate the bp average over all
> genes over this region. 

I know absolutely nothing about your problem domain, but if your
distance function is metric, you can use this:

http://well-adjusted.de/mspace.py

or that:

http://code.activestate.com/recipes/572156/

The latter (by Baerophile) is fast, the former (by me) has more
features. Baerophile has even faster implementations (non-Python) as
well.

J.
-- 
I am worried that my dreams pale in comparison beside TV docu-soaps.
[Agree]   [Disagree]
 


signature.asc
Description: Digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: trouble with reload

2009-08-14 Thread Colin J. Williams

Terry Reedy wrote:

Dr. Phillip M. Feldman wrote:
According to the Python documentation, 'reload' reloads a previously 
imported
module (so that changes made via an external editor will be 
effective). But, when I try to use this command, I get the following 
error message:


TypeError: reload() argument must be module

Any suggestions will be appreciated.


Besides the other answers, do not use reload. It is removed in Py3 
because it cannot be made to work as people reasonably expect.


tjr


It's typically a user module that needs to be reloaded.

It seems that  del sys.modules['moduleName'] has no effect.

Is there some other way of ensuring that any import goes to moduleName.py, 
instead of moduleName.pyc?


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


Re: csv.DictWriter.write_header()

2009-08-14 Thread Alan G Isaac
> On Aug 13, 1:15 pm, Alan G Isaac  wrote:
>> I do not understand the reason for your silly, sarcastic response.


On 8/13/2009 7:58 AM John Machin apparently wrote:
> Duck typing: ask a silly question, get a silly answer.

Maybe if you learned to be a more generous reader,
fewer questions would look "silly" to you.


On 8/13/2009 7:58 AM John Machin apparently wrote:
> I can imagine that one might (without reading the source) make do with
> the published APIs:

Now you get it.

> On Aug 13, 1:15 pm, Alan G Isaac  wrote:
>> So my question was, would this improve the class from
>> a usability perspective?

On 8/13/2009 7:58 AM John Machin apparently wrote:
> Of course.

Thank you,
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: implementing descriptors

2009-08-14 Thread dippim
On Aug 14, 5:45 am, Jean-Michel Pichavant 
wrote:
> Emile van Sebille wrote:
> > On 8/13/2009 3:17 PM dippim said...
> >> I am new to Python and I have a question about descriptors.  If I have
> >> a class as written below, is there a way to use descriptors to be
> >> certain that the datetime in start is always before the one in end?
>
> >> class foo(object):
> >>    def __init__(self,a = None,b = None)
> >>       self.start = a
> >>       self.end = b
>
> >> from datetime import datetime
> >> c = datetime(2009,8,13,6,15,0)
> >> d = datetime(2009,8,14,12,0,0)
> >> afoo = foo(c,d)
>
> >> For instance, if the following code were run, I would like to instance
> >> of foo to switch the start and end times.
>
> >> afoo.start = datetime(2010,8,13,6,15,0)
>
> >> I was thinking of using the __set__ descriptor to catch the assignment
> >> and reverse the values if necessary,
>
> > why not...
>
> > class foo(object):
> >    def __init__(self,a = None,b = None)
> >       self.start = min(a,b)
> >       self.end = max(a,b)
>
> > Emile
>
> or
>
> class foo(object):
>     def __init__(self, start, end)
>        self.start = start
>        self.end = end
>
> Problem solved by design :o)
>
> JM

Emile and JM,

   Thanks for the response.  However, these solution only work at
instantiation.  If I change the value of start or end after
instantiation, then I can make start or end whatever I like without
regard to order.

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


Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-14 Thread Hendrik van Rooyen
On Friday 14 August 2009 12:54:32 Diez B. Roggisch wrote:

>
> How about using pyserial? With that, I never had any problems accessing
> the the serial ports, and AFAIK no duplex-problems as well. And I
> seriously doubt that these are a python-related problem - python only
> has a very thin, direct layer above the posix-calls, and doesn't do
> anything that would explain your observed behavior. The GIL is not the
> issue here either - it won't interfer with blocking IO.

I will have a look at pyserial - have never used it before.

I agree that it is probably not a Python issue, and that the GIL is 
irelevant  - I was hoping that someone had already travelled the road and 
could give me a signpost.

In the meantime I have had another idea which I have also not tried yet, 
namely to do independent opens for reading and writing, to give me two file 
instances instead of one, and to try with that.  I have no idea if it would 
make any difference, or even work at all.

My normal stuff works, but I do not like it as it is essentially busy looping 
with short sleeps in between. In the eBox, it uses most of the processor just 
to move a few bytes of I/O in and out between the serial port and the TCP/IP,
and struggles to do that better than five times a second, while the message 
time on the 115200 baud port is only about 2 milliseconds.

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


Re: implementing descriptors

2009-08-14 Thread Diez B. Roggisch

dippim schrieb:

On Aug 14, 5:45 am, Jean-Michel Pichavant 
wrote:

Emile van Sebille wrote:

On 8/13/2009 3:17 PM dippim said...

I am new to Python and I have a question about descriptors.  If I have
a class as written below, is there a way to use descriptors to be
certain that the datetime in start is always before the one in end?
class foo(object):
   def __init__(self,a = None,b = None)
  self.start = a
  self.end = b
from datetime import datetime
c = datetime(2009,8,13,6,15,0)
d = datetime(2009,8,14,12,0,0)
afoo = foo(c,d)
For instance, if the following code were run, I would like to instance
of foo to switch the start and end times.
afoo.start = datetime(2010,8,13,6,15,0)
I was thinking of using the __set__ descriptor to catch the assignment
and reverse the values if necessary,

why not...
class foo(object):
   def __init__(self,a = None,b = None)
  self.start = min(a,b)
  self.end = max(a,b)
Emile

or

class foo(object):
def __init__(self, start, end)
   self.start = start
   self.end = end

Problem solved by design :o)

JM


Emile and JM,

   Thanks for the response.  However, these solution only work at
instantiation.  If I change the value of start or end after
instantiation, then I can make start or end whatever I like without
regard to order.



But for that, you don't need descriptors. All you need is a property:

class Foo(object):

   def __init__(self, start, end):
   self._start = start
   self._end = end


   @property
   def start(self):
   return start


   @start.setter
   def start(self, value):
   if value >= self._end:
  raise ValueError, "Tried to set a value greater than end!"
   self._start = value

   # repeat for end, switch relational op


Diez



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


Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-14 Thread Hendrik van Rooyen
On Friday 14 August 2009 14:13:46 greg wrote:

> You can't read and write with the same stdio file object
> at the same time. Odd things tend to happen if you try.
>
> You need to open *two* file objects, one for reading
> and one for writing:
>
>fr = open("/dev/ttyS0","rb",0)
>fw = open("/dev/ttyS0","wb",0)
>
> and give fr to the reading thread and fw to the
> writing thread.

Does this actually work without somehow falling foul of the fact that 
the /dev/ttyS0 is only one thing?  - I know that there is no physical reason 
for not being able to go in and out at the same time - in my embedded stuff I 
do that routinely - but that is all assembler running on bare metal so it is  
under my own control.

>
> You could also try avoiding file objects altogether
> and use the raw system calls in the os module. Since
> you're not using any buffering, there's little reason
> to use the stdio layer. If you do that, you should be
> able to use the same file descriptor for reading and
> writing without any trouble.

Thanks for this - I now have my weekend cut out for me...

- Hendrik

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


Re: implementing descriptors

2009-08-14 Thread dippim
On Aug 14, 2:34 am, Raymond Hettinger  wrote:
> [David]
>
>
>
> > I am new to Python and I have a question about descriptors.  If I have
> > a class as written below, is there a way to use descriptors to be
> > certain that the datetime in start is always before the one in end?
>
> > class foo(object):
> >    def __init__(self,a = None,b = None)
> >       self.start = a
> >       self.end = b
>
> > from datetime import datetime
> > c = datetime(2009,8,13,6,15,0)
> > d = datetime(2009,8,14,12,0,0)
> > afoo = foo(c,d)
>
> > For instance, if the following code were run, I would like to instance
> > of foo to switch the start and end times.
>
> > afoo.start = datetime(2010,8,13,6,15,0)
>
> > I was thinking of using the __set__ descriptor to catch the assignment
> > and reverse the values if necessary, but I can't figure out how to
> > determine which values is being set.
>
> You're on the right track, but it is easier to use property() than to
> write your own custom descriptor with __get__ and __set__.
>
> class foo(object):
>     def __init__(self,a = None,b = None):
>         self._start = a
>         self._end = b
>     def get_start(self):
>         return self._start
>     def set_start(self, value):
>         if self._end is None or value < self._end:
>             self._start = value
>         else:
>             self._end = value
>     start = property(get_start, set_start)
>     def get_end(self):
>         return self._end
>     def set_end(self, value):
>         if self._start is None or value > self._start:
>             self._end = value
>         else:
>             self._start = value
>     end = property(get_end, set_end)
>
> Raymond

Raymond,
   This functionality is exactly what I was looking for. Thanks!  I'll
be using this to solve my problem.

   Now that I'm on the right track, I'm still a bit confused about how
__get__ and __set__ are useful.  Admittedly, I don't need to
understand them to solve this problem, but perhaps they may be useful
in the future.  If I wanted to solve this problem using __get__ and
__set__ could it be done?

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


Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-14 Thread exarkun

On 01:38 pm, [email protected] wrote:

On Friday 14 August 2009 12:54:32 Diez B. Roggisch wrote:


How about using pyserial? With that, I never had any problems 
accessing

the the serial ports, and AFAIK no duplex-problems as well. And I
seriously doubt that these are a python-related problem - python only
has a very thin, direct layer above the posix-calls, and doesn't do
anything that would explain your observed behavior. The GIL is not the
issue here either - it won't interfer with blocking IO.


I will have a look at pyserial - have never used it before.

I agree that it is probably not a Python issue, and that the GIL is
irelevant  - I was hoping that someone had already travelled the road 
and

could give me a signpost.

In the meantime I have had another idea which I have also not tried 
yet,
namely to do independent opens for reading and writing, to give me two 
file
instances instead of one, and to try with that.  I have no idea if it 
would

make any difference, or even work at all.

My normal stuff works, but I do not like it as it is essentially busy 
looping
with short sleeps in between. In the eBox, it uses most of the 
processor just
to move a few bytes of I/O in and out between the serial port and the 
TCP/IP,
and struggles to do that better than five times a second, while the 
message

time on the 115200 baud port is only about 2 milliseconds.


One strategy you might employ to get rid of the busy looping is to use 
Twisted and its serial port support.  This also addresses the full- 
duplex issue you've raised.


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


Re: Python "and" behavior

2009-08-14 Thread Ethan Furman

MRAB wrote:

Gary Herron wrote:


goldtech wrote:


Could you explain or link me to an explanation of this? Been using
Python for a while but not sure I understand what's happening below.
Thanks.


 


ss=1 and "f"
ss



'f'
 


ss=0 and "f"
ss



0
  



Python's Boolean operators don't turn arbitrary values into True and 
False values.  If you use it in any conditional, you'll get the same 
result as if it did, but it is occasionally it's nice to get the 
actual values used in the "and" instead of having the value distilled 
down to a True/False.



 >From the Python manual:
These are the Boolean operations, ordered by ascending priority:

Operation Result Notes
|x or y| if x is false, then y, else x (1)
|x and y| if x is false, then x, else y (1)
|not x| if x is false, then |True|, else |False| (2)


The Pythonic table would be:

Operation Result
|x or y| x if x else y
|x and y| y if x else x
|not x| False if x else False

:-)


That last should be
|not x|   False if x else True

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


Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-14 Thread Diez B. Roggisch

Hendrik van Rooyen schrieb:

On Friday 14 August 2009 14:13:46 greg wrote:


You can't read and write with the same stdio file object
at the same time. Odd things tend to happen if you try.

You need to open *two* file objects, one for reading
and one for writing:

   fr = open("/dev/ttyS0","rb",0)
   fw = open("/dev/ttyS0","wb",0)

and give fr to the reading thread and fw to the
writing thread.


Does this actually work without somehow falling foul of the fact that 
the /dev/ttyS0 is only one thing?  - I know that there is no physical reason 
for not being able to go in and out at the same time - in my embedded stuff I 
do that routinely - but that is all assembler running on bare metal so it is  
under my own control.



You could also try avoiding file objects altogether
and use the raw system calls in the os module. Since
you're not using any buffering, there's little reason
to use the stdio layer. If you do that, you should be
able to use the same file descriptor for reading and
writing without any trouble.


Thanks for this - I now have my weekend cut out for me...


You should *really* just use pyserial. No hassle, instant satisfaction.

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


Re: Unrecognized escape sequences in string literals

2009-08-14 Thread Aahz
In article <6e13754c-1fa6-4d1b-8861-146bffec8...@h30g2000vbr.googlegroups.com>,
Douglas Alan   wrote:
>
>My friend begs to differ with the above. It would be much better for
>debugging if Python generated a parsing error for unrecognized escape
>sequences, rather than leaving them unchanged. g++ outputs a warning
>for such escape sequences, for instance. This is what I would consider
>to be the correct behavior. (Actually, I think it should just generate
>a fatal parsing error, but a warning is okay too.)

Well, then, the usual response applies: create a patch, discuss it on
python-ideas, and see what happens.

(That is, nobody has previously complained so vociferously IIRC, and
adding a warning is certainly within the bounds of what's theoretically
acceptable.)
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"I saw `cout' being shifted "Hello world" times to the left and stopped
right there."  --Steve Gonedes
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: coding for multiple versions of python

2009-08-14 Thread Grant Edwards
On 2009-08-14, Martin v. L?wis  wrote:

>> I'm guessing I need to configure cvs to copy files to both
>> locations whenever I commit. Does that sound right? Is there a
>> better way I'm not thinking of?

Just use one set of source files.

> If the set of files doesn't change too often, you can use symlinks.
> That's how Debian currently installs Python packages for multiple
> versions on a single system.
>
> Specifically, put the source code into /net/source/python/foo/*.py.
> Then, on each system, put symlinks to all .py files into
> lib/site-packages/foo. Then Python will place the .pyc files next
> to the symlinks, not next to the actual .py files.

Why would he need two sets of .py files?

-- 
Grant Edwards   grante Yow! I'm EMOTIONAL
  at   now because I have
   visi.comMERCHANDISING CLOUT!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket.send : (11, 'Resource temporarily unavailable')

2009-08-14 Thread Grant Edwards
On 2009-08-14, Gabriel Rossetti  wrote:

> I get a (11, 'Resource temporarily unavailable') error when I
> try to send a file using a socket. Is there s size limit?

No, there's no size limit.  However, there is a bandwidth
limit.  You can't shove bytes into the pipe faster than they
come out the other end (at least not over the long term).

> I tried sending a smaller file and ii poses no problem. Am I
> doing something wrong?

Yes.  If you want to have the socket in non-blocking mode, then
you have to catch EAGAIN and retry the operation.

> def sendMessage(host, port, msg):
>
> if isinstance(msg, unicode):
> msg = msg.encode("utf-8")
>
> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> sock.connect((host, port))
> sock.setblocking(0)
> totalsent = 0
> while totalsent < len(msg):
> sent = sock.send(msg[totalsent:])
> if sent == 0:
> raise RuntimeError, "socket connection broken"
> totalsent = totalsent + sent
> sock.close()

-- 
Grant Edwards   grante Yow! I didn't order any
  at   WOO-WOO ... Maybe a YUBBA
   visi.com... But no WOO-WOO!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-14 Thread Grant Edwards
On 2009-08-14, Hendrik van Rooyen  wrote:

> In the past, on this group, I have made statements that said
> that on Linux, the serial port handling somehow does not allow
> transmitting and receiving at the same time,

That's not true.  Linux/Unix does and always has supported
full-duplex communications on serial ports.

> and nobody contradicted me.

Um, sorry, I guess.

> What I would really like is to have two threads - one that
> does blocking input waiting for a character, and one that
> examines an output queue and transmits the stuff it finds.

That's the traditional way of doing full-duplex serial IO on
Unix back in the day before the select/poll system calls were
available.

> When I try to do this, it does not seem to work - as far as I
> can see, it is as if the underlying implementation is somehow
> single threaded - if it is waiting for a received character,
> it waits until something comes in before it will transmit
> anything.

Nope.   I'll try to dig up an example, but that approach has
always worked for me.

> So if you are talking to a device that does not respond, the
> whole thing freezes up waiting for a character that never
> comes, and nothing is transmitted either, despite the call to 
> port.write(somestring).  The write blocks, and everything
> stops, waiting for the receipt to finish.

I've never observed that behavior.

> Is there a way to get full duplex, so that the transmit and
> receive are independent of each other?

That's the way serial ports work on Unix.

> Or are we stuck with a disk-like model that forces a sequence
> on reads and writes?

No.

-- 
Grant Edwards   grante Yow! Like I always say
  at   -- nothing can beat
   visi.comthe BRATWURST here in
   DUSSELDORF!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-14 Thread Grant Edwards
On 2009-08-14, Hendrik van Rooyen  wrote:

> In the meantime I have had another idea which I have also not tried yet, 
> namely to do independent opens for reading and writing, to give me two file 
> instances instead of one, and to try with that.  I have no idea if it would 
> make any difference, or even work at all.

That should work (and shouldn't make any difference)

> My normal stuff works, but I do not like it as it is
> essentially busy looping with short sleeps in between. In the
> eBox, it uses most of the processor just to move a few bytes
> of I/O in and out between the serial port and the TCP/IP, and
> struggles to do that better than five times a second, while
> the message time on the 115200 baud port is only about 2
> milliseconds.

What platform are you using?  I suppose it's possible that
there's something broken in the serial driver for that
particular hardware.

-- 
Grant Edwards   grante Yow! I feel ... JUGULAR ...
  at   
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-14 Thread Grant Edwards
On 2009-08-14, [email protected]  wrote:

> One strategy you might employ to get rid of the busy looping
> is to use Twisted and its serial port support.  This also
> addresses the full- duplex issue you've raised.

There are no such full-dulex issues.

-- 
Grant Edwards   grante Yow! The PINK SOCKS were
  at   ORIGINALLY from 1952!!
   visi.comBut they went to MARS
   around 1953!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A Exhibition Of Tech Geekers Incompetence: Emacs whitespace-mode

2009-08-14 Thread despen
"Martin P. Hellwig"  writes:

> Sounds like a bad case of STRIS
> http://blog.dcuktec.com/2009/08/stris.html

I believe the correct technical term for it is potty mouth.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-14 Thread Grant Edwards
On 2009-08-14, greg  wrote:
> Hendrik van Rooyen wrote:
>
>> port = open("/dev/ttyS0","r+b",0)
>> 
>> What I would really like is to have two threads - one that
>> does blocking input waiting for a character, and one that
>> examines an output queue and transmits the stuff it finds.
>
> You can't read and write with the same stdio file object at
> the same time. Odd things tend to happen if you try.
>
> You need to open *two* file objects, one for reading and one
> for writing:
>
>fr = open("/dev/ttyS0","rb",0)
>fw = open("/dev/ttyS0","wb",0)

Doh!  It didn't even occur to me that somebody would use python
"file" objects for serial ports, and I completely overlooked
the fact that the OP was doing that.

In short: don't do that -- it just messes things up.

> and give fr to the reading thread and fw to the writing
> thread.
>
> You could also try avoiding file objects altogether and use
> the raw system calls in the os module.

That's definitely the way you should do serial port I/O.

> Since you're not using any buffering, there's little reason to
> use the stdio layer. If you do that, you should be able to use
> the same file descriptor for reading and writing without any
> trouble.

Do not use Python file objects.  Use the underlying file
descriptors: os.open(), os.read(), os.write().  That will
almost certainly solve your problems.

If you want examples of os.x() usage, below is the
PosixSerial.py module that I use for Linux-only applications.
For cross-platform work, use pyserial (whose Posix support is
based on the code below).


-PosixSerial.py--
# Posix serial port class
# Copyright 2001-2009 Grant B. Edwards  

# You may use this code in any way you like so long as you
# leave this copyright notice.

# Though you are not required to, it would be nice if you send
# me a copy of bufixes or enhancements and allowed me to
# incorporate and distribute them.

import sys
import fcntl
import os
import struct
import termios
import string
import select

if string.split(sys.version)[0] > '2':
TERMIOS = termios
else:
import TERMIOS

# construct dictionaries for baud rate lookups

baudEnumToInt = {}
baudIntToEnum = {}
for rate in 
(0,50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200,230400,460800,50,576000,921600,100,1152000,150,200,250,300,350,400):
try:
i = eval('TERMIOS.B'+str(rate))
baudEnumToInt[i]=rate
baudIntToEnum[rate] = i
except:
pass

# Do not know if these are right for anything except Linux
if sys.platform[:5] == 'linux':
TIOCMGET = 0x5415
TIOCMBIS = 0x5416
TIOCMBIC = 0x5417
TIOCMSET = 0x5418

TIOCM_LE   =  0x001
TIOCM_DTR  =  0x002
TIOCM_RTS  =  0x004
TIOCM_ST   =  0x008
TIOCM_SR   =  0x010
TIOCM_CTS  =  0x020
TIOCM_CAR  =  0x040
TIOCM_RNG  =  0x080
TIOCM_DSR  =  0x100
TIOCM_CD   =  TIOCM_CAR
TIOCM_RI   =  TIOCM_RNG
TIOCM_OUT1 =  0x2000
TIOCM_OUT2 =  0x4000

TIOCM_zero_str = struct.pack('I',0)
TIOCM_one_str = struct.pack('I',1)
TIOCM_RTS_str = struct.pack('I',TIOCM_RTS)
TIOCM_DTR_str = struct.pack('I',TIOCM_DTR)

portNotOpenError = ValueError('port not open')

class Port:
""" An object wrapper for Posix serial ports """
def __init__(self,path=None,noinit=False):
self.fd = None
if path:
self.open(path,noinit)

def __tcsetattr(self):

termios.tcsetattr(self.fd,TERMIOS.TCSANOW,[self.iflag,self.oflag,self.cflag,self.lflag,self.ispeed,self.ospeed,self.cc])

def __tcgetattr(self):

self.iflag,self.oflag,self.cflag,self.lflag,self.ispeed,self.ospeed,self.cc = 
termios.tcgetattr(self.fd)

def open(self,path,noinit):
if self.fd:
self.close()
self.path = path
self.fd = os.open(path,os.O_RDWR)
self.__tcgetattr()
if not noinit:
self.iflag = 0
self.oflag = 0
self.lflag = 0
self.__tcsetattr()

def close(self):
if self.fd:
os.close(self.fd)
self.fd = None

def fileno(self):
return self.fd;

def _write(self,data):
if not self.fd: raise portNotOpenError
return os.write(self.fd,data)

def write(self,data):
if not self.fd: raise portNotOpenError
t = len(data)
d = data
while t>0:
n = os.write(self.fd,d)
d = d[n:]
t = t - n

def read(self,size=1024,timeout=None):
if not self.fd: raise portNotOpenError
if timeout is None:
return os.read(self.fd,size)
else:
r,w,e = select.select([self.fd],[],[self.fd],timeout)
if r:
return os.read(self.fd,size)
else:
raise "timeout"



def baud(self,rate=None):
i

Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-14 Thread exarkun

On 02:19 pm, inva...@invalid wrote:
On 2009-08-14, [email protected]  
wrote:

One strategy you might employ to get rid of the busy looping
is to use Twisted and its serial port support.  This also
addresses the full- duplex issue you've raised.


There are no such full-dulex issues.


There was a perceived issues.  Obviously it's possible to do full-duplex 
with Linux's serial port support (and all the other major platforms too, 
as far as I know), as long as you know how. :)  Twisted makes the how a 
lot simpler.


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


Re: implementing descriptors

2009-08-14 Thread Dave Angel

dippim wrote:

On Aug 14, 2:34 am, Raymond Hettinger  wrote:
  

[David]





I am new to Python and I have a question about descriptors.  If I have
a class as written below, is there a way to use descriptors to be
certain that the datetime in start is always before the one in end?
  
class foo(object):

   def __init__(self,a =one,b = None)
  self.start =
  self.end =
  
from datetime import datetime

c =atetime(2009,8,13,6,15,0)
d =atetime(2009,8,14,12,0,0)
afoo =oo(c,d)
  
For instance, if the following code were run, I would like to instance

of foo to switch the start and end times.
  
afoo.start =atetime(2010,8,13,6,15,0)
  
I was thinking of using the __set__ descriptor to catch the assignment

and reverse the values if necessary, but I can't figure out how to
determine which values is being set.
  

You're on the right track, but it is easier to use property() than to
write your own custom descriptor with __get__ and __set__.

class foo(object):
def __init__(self,a =one,b = None):
self._start =
self._end =
def get_start(self):
return self._start
def set_start(self, value):
if self._end is None or value < self._end:
self._start =alue
else:
self._end =alue
start =roperty(get_start, set_start)
def get_end(self):
return self._end
def set_end(self, value):
if self._start is None or value > self._start:
self._end =alue
else:
self._start =alue
end =roperty(get_end, set_end)

Raymond



Raymond,
   This functionality is exactly what I was looking for. Thanks!  I'll
be using this to solve my problem.

   Now that I'm on the right track, I'm still a bit confused about how
__get__ and __set__ are useful.  Admittedly, I don't need to
understand them to solve this problem, but perhaps they may be useful
in the future.  If I wanted to solve this problem using __get__ and
__set__ could it be done?

Thanks Again!

  

DANGER- WILL ROBINSON!

Don't use this code as-is.  There is a nasty surprise waiting for the 
caller when he sets start and end, and discovers that one of them gets 
thrown out, and an old value still remains.


obj= foo(3, 5)
obj.start = 8
obj.end = 12
print obj.start, obj.end

will print out  3, 12.Not what the caller expected.

Four fixes, in order of preference:
0) Trust your user to read and obey your docstrings.  This was what JM 
was implying, by changing the names of the formal parameters.
1)  make a new method that sets both values, making these two properties 
readonly.  That new method would make sure the two parameters are 
self-consistent.  Making the actual values readonly can be done with a 
descriptor as well, or even a decorator.

2) Raise an exception in the getter methods if they're out of order
3) do the min/max logic on the getter methods, but I don't like that one 
at all.


DaveA

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


Re: coding for multiple versions of python

2009-08-14 Thread Dave Angel

Grant Edwards wrote:

On 2009-08-14, Martin v. L?wis  wrote:

  

I'm guessing I need to configure cvs to copy files to both
locations whenever I commit. Does that sound right? Is there a
better way I'm not thinking of?
  


Just use one set of source files.

  

If the set of files doesn't change too often, you can use symlinks.
That's how Debian currently installs Python packages for multiple
versions on a single system.

Specifically, put the source code into /net/source/python/foo/*.py.
Then, on each system, put symlinks to all .py files into
lib/site-packages/foo. Then Python will place the .pyc files next
to the symlinks, not next to the actual .py files.



Why would he need two sets of .py files?

  
I can't speak for Martin, but his description doesn't have two sets of 
.py files, but two sets of symlinks pointing to the same .py files.  
It's the .pyc files that exist in two forms, since those are version 
dependent.


He's assuming:
   1) an OS that supports symlinks
   2) two versions of Python on same system
   3) one set of pure-python sources that want to stay in synch for 
both versions.



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


Re: Format Code Repeat Counts?

2009-08-14 Thread Scott David Daniels

MRAB wrote:

Scott David Daniels wrote:

MRAB wrote:

The shortest I can come up with is:
"[" + "][".join(letters) + "]"


Maybe a golf shot:
  "][".join(letters).join("[]")


Even shorter:

"["+"][".join(letters)+"]"

:-)

I was going by PEP8 rules. ;-)

--Scott David Daniels
Scott David [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


callable virtual method

2009-08-14 Thread Jean-Michel Pichavant

Hi fellows,

Does anyone know a way to write virtual methods (in one virtual class) 
that will raise an exception only if called without being overridden ?
Currently in the virtual method I'm checking that the class of the 
instance calling the method has defined that method as well.


Example:

class Stream(object):
   """Interface of all stream objects"""
   def resetStats(self):
   """Reset the stream statistics. All values a zeroed except the 
date."""

   _log.info('Reset statistics of %s' % self)
   if self.__class__.resetStats == Stream.resetStats:
   raise NotImplementedError()

It works but it's tedious, I have to add these 2 lines to every virtual 
method, changing the content of the 2 lines.


Maybe there is a nice/builtin way to do so (python 2.4)

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


Re: trouble with reload

2009-08-14 Thread Steven D'Aprano
On Fri, 14 Aug 2009 09:23:17 -0400, Colin J. Williams wrote:

> It's typically a user module that needs to be reloaded.

What's a user module?


> It seems that  del sys.modules['moduleName'] has no effect.

sys.modules is just a dictionary, I find it hard to believe that deleting 
from it has no effect. It works for me:

>>> import sys
>>> import math
>>> 'math' in sys.modules
True
>>> del sys.modules['math']
>>> 'math' in sys.modules
False

What behaviour do you get?


Of course deleting the math module from the cache doesn't do anything to 
the math module in your namespace:

>>> math

>>> del math
>>> math
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'math' is not defined

Of course deleting the module (or reloading it) doesn't have any effect 
on any objects you already have:


>>> import math
>>> func = math.sin
>>> del sys.modules['math']
>>> del math
>>> math.sin(1.2)
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'math' is not defined
>>> func(1.2)
0.93203908596722629




> Is there some other way of ensuring that any import goes to
> moduleName.py, instead of moduleName.pyc?

Delete moduleName.pyc.

Make sure the .pyc file doesn't exist in the first place.

Make sure the last modification date of the .py file is newer than the 
modification date of the .pyc file.



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


Re: Programming by Contract

2009-08-14 Thread Charles Yeomans


On Aug 14, 2009, at 12:09 AM, Scott David Daniels wrote:


Charles Yeomans wrote:

On Aug 11, 2009, at 3:30 PM, Ethan Furman wrote:

Ethan Furman wrote:

Greetings!
I have seen posts about the assert statement and PbC (or maybe it  
was DbC), and I just took a very brief look at pycontract (http://www.wayforward.net/pycontract/ 
) and now I have at least one question:  Is this basically  
another way of thinking about unit testing, or is the idea of PbC  
more along the lines of *always* checking the input/output of  
functions to ensure they are correct?  (*Contstant vigilance!* as  
Prof Moody would say ;)
I know asserts can be turned off, so they obviously won't work  
for the latter case, and having seen the sample of pycontract it  
seems it only does its thing during debugging.
So is Design (Programming) by Contract a fancy way of saying  
"Document your inputs/outputs!" or is there more to it?

~Ethan~


Hmmm...

Well, from the (apparently) complete lack of interest, I shall  
take away the (better?) documentation ideas and unit testing  
ideas, and not worry about the rest.  :)



Design by contract is complementary to unit testing (I notice that  
the author of PEP 316 appears confused about this).  DbC is,  
roughly speaking, about explicit allocation of responsibility.   
Consider this contrived example.

def foo(s):
   require(s is not None)
   //code
   ensure(hasattr(returnValue, '__iter__'))


yo might want two flags, REQUIRE_OFF, and ENSURE_ON that control
testing, and change the code above to:
 require(REQUIRE_OFF or s is not None)
 //code
 ensure(ENSURE_OFF or hasattr(returnValue, '__iter__'))

Python has no good way to turn off argument calculation by
manipulating function definition (at least that I know of).



For this purpose, it had occurred to me to do something like the  
following.


def require(condition):
if condition:
return True
else:
raise PreconditionFailure


def foo(s):
   assert require(s is not None)


Then it occurred to me to actually read the assert documentation,  
where I learned that one can pass a second expression to assert. So  
instead one might write


assert precondition, "PreconditionFailure"

though I think I prefer the former.


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


Re: implementing descriptors

2009-08-14 Thread dippim
On Aug 14, 10:48 am, Dave Angel  wrote:
> dippim wrote:
> > On Aug 14, 2:34 am, Raymond Hettinger  wrote:
>
> >> [David]
>
> >>> I am new to Python and I have a question about descriptors.  If I have
> >>> a class as written below, is there a way to use descriptors to be
> >>> certain that the datetime in start is always before the one in end?
>
> >>> class foo(object):
> >>>    def __init__(self,a =one,b = None)
> >>>       self.start =
> >>>       self.end =
>
> >>> from datetime import datetime
> >>> c =atetime(2009,8,13,6,15,0)
> >>> d =atetime(2009,8,14,12,0,0)
> >>> afoo =oo(c,d)
>
> >>> For instance, if the following code were run, I would like to instance
> >>> of foo to switch the start and end times.
>
> >>> afoo.start =atetime(2010,8,13,6,15,0)
>
> >>> I was thinking of using the __set__ descriptor to catch the assignment
> >>> and reverse the values if necessary, but I can't figure out how to
> >>> determine which values is being set.
>
> >> You're on the right track, but it is easier to use property() than to
> >> write your own custom descriptor with __get__ and __set__.
>
> >> class foo(object):
> >>     def __init__(self,a =one,b = None):
> >>         self._start =
> >>         self._end =
> >>     def get_start(self):
> >>         return self._start
> >>     def set_start(self, value):
> >>         if self._end is None or value < self._end:
> >>             self._start =alue
> >>         else:
> >>             self._end =alue
> >>     start =roperty(get_start, set_start)
> >>     def get_end(self):
> >>         return self._end
> >>     def set_end(self, value):
> >>         if self._start is None or value > self._start:
> >>             self._end =alue
> >>         else:
> >>             self._start =alue
> >>     end =roperty(get_end, set_end)
>
> >> Raymond
>
> > Raymond,
> >    This functionality is exactly what I was looking for. Thanks!  I'll
> > be using this to solve my problem.
>
> >    Now that I'm on the right track, I'm still a bit confused about how
> > __get__ and __set__ are useful.  Admittedly, I don't need to
> > understand them to solve this problem, but perhaps they may be useful
> > in the future.  If I wanted to solve this problem using __get__ and
> > __set__ could it be done?
>
> > Thanks Again!
>
> DANGER- WILL ROBINSON!
>
> Don't use this code as-is.  There is a nasty surprise waiting for the
> caller when he sets start and end, and discovers that one of them gets
> thrown out, and an old value still remains.
>
> obj= foo(3, 5)
> obj.start = 8
> obj.end = 12
> print obj.start, obj.end
>
> will print out  3, 12.    Not what the caller expected.

You're right about this and I appreciate the warning, but I think what
Raymond was going for was directional accuracy without a great deal of
his time wasted on details.  The explanation served the purpose of
moving me forward using property() and I'm thankful for it.

>
> Four fixes, in order of preference:
> 0) Trust your user to read and obey your docstrings.  This was what JM
> was implying, by changing the names of the formal parameters.

Considering I am not the sharpest knife in the drawer, I don't wish to
start a philosophical discussion about the relative aptitude and
capabilities of the people who might use a class I build.  However, I
will say that as this particular requirement is imposed on this class
by the writer, shouldn't it be the writer's responsibility to enforce
it, especially, when the cost of enforcement is so low?

> 1)  make a new method that sets both values, making these two properties
> readonly.  That new method would make sure the two parameters are
> self-consistent.  Making the actual values readonly can be done with a
> descriptor as well, or even a decorator.

> 2) Raise an exception in the getter methods if they're out of order
> 3) do the min/max logic on the getter methods, but I don't like that one
> at all.
>
> DaveA

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


Re: implementing descriptors

2009-08-14 Thread dippim
On Aug 14, 10:48 am, Dave Angel  wrote:
> dippim wrote:
> > On Aug 14, 2:34 am, Raymond Hettinger  wrote:
>
> >> [David]
>
> >>> I am new to Python and I have a question about descriptors.  If I have
> >>> a class as written below, is there a way to use descriptors to be
> >>> certain that the datetime in start is always before the one in end?
>
> >>> class foo(object):
> >>>    def __init__(self,a =one,b = None)
> >>>       self.start =
> >>>       self.end =
>
> >>> from datetime import datetime
> >>> c =atetime(2009,8,13,6,15,0)
> >>> d =atetime(2009,8,14,12,0,0)
> >>> afoo =oo(c,d)
>
> >>> For instance, if the following code were run, I would like to instance
> >>> of foo to switch the start and end times.
>
> >>> afoo.start =atetime(2010,8,13,6,15,0)
>
> >>> I was thinking of using the __set__ descriptor to catch the assignment
> >>> and reverse the values if necessary, but I can't figure out how to
> >>> determine which values is being set.
>
> >> You're on the right track, but it is easier to use property() than to
> >> write your own custom descriptor with __get__ and __set__.
>
> >> class foo(object):
> >>     def __init__(self,a =one,b = None):
> >>         self._start =
> >>         self._end =
> >>     def get_start(self):
> >>         return self._start
> >>     def set_start(self, value):
> >>         if self._end is None or value < self._end:
> >>             self._start =alue
> >>         else:
> >>             self._end =alue
> >>     start =roperty(get_start, set_start)
> >>     def get_end(self):
> >>         return self._end
> >>     def set_end(self, value):
> >>         if self._start is None or value > self._start:
> >>             self._end =alue
> >>         else:
> >>             self._start =alue
> >>     end =roperty(get_end, set_end)
>
> >> Raymond
>
> > Raymond,
> >    This functionality is exactly what I was looking for. Thanks!  I'll
> > be using this to solve my problem.
>
> >    Now that I'm on the right track, I'm still a bit confused about how
> > __get__ and __set__ are useful.  Admittedly, I don't need to
> > understand them to solve this problem, but perhaps they may be useful
> > in the future.  If I wanted to solve this problem using __get__ and
> > __set__ could it be done?
>
> > Thanks Again!
>
> DANGER- WILL ROBINSON!
>
> Don't use this code as-is.  There is a nasty surprise waiting for the
> caller when he sets start and end, and discovers that one of them gets
> thrown out, and an old value still remains.
>
> obj= foo(3, 5)
> obj.start = 8
> obj.end = 12
> print obj.start, obj.end
>
> will print out  3, 12.    Not what the caller expected.

You're right about this and I appreciate the warning, but I think what
Raymond was going for was directional accuracy without a great deal of
his time wasted on details.  The explanation served the purpose of
moving me forward using property() and I'm thankful for it.

>
> Four fixes, in order of preference:
> 0) Trust your user to read and obey your docstrings.  This was what JM
> was implying, by changing the names of the formal parameters.

Considering I am not the sharpest knife in the drawer, I don't wish to
start a philosophical discussion about the relative aptitude and
capabilities of the people who might use a class I build.  However, I
will say that as this particular requirement is imposed on this class
by the writer, shouldn't it be the writer's responsibility to enforce
it, especially, when the cost of enforcement is so low?

> 1)  make a new method that sets both values, making these two properties
> readonly.  That new method would make sure the two parameters are
> self-consistent.  Making the actual values readonly can be done with a
> descriptor as well, or even a decorator.

> 2) Raise an exception in the getter methods if they're out of order
> 3) do the min/max logic on the getter methods, but I don't like that one
> at all.
>
> DaveA

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


Re: callable virtual method

2009-08-14 Thread MRAB

Jean-Michel Pichavant wrote:

Hi fellows,

Does anyone know a way to write virtual methods (in one virtual class) 
that will raise an exception only if called without being overridden ?
Currently in the virtual method I'm checking that the class of the 
instance calling the method has defined that method as well.


Example:

class Stream(object):
   """Interface of all stream objects"""
   def resetStats(self):
   """Reset the stream statistics. All values a zeroed except the 
date."""

   _log.info('Reset statistics of %s' % self)
   if self.__class__.resetStats == Stream.resetStats:
   raise NotImplementedError()

It works but it's tedious, I have to add these 2 lines to every virtual 
method, changing the content of the 2 lines.


Maybe there is a nice/builtin way to do so (python 2.4)


Why are you checking which class it's in? The method in the base class
will be called only if it hasn't been overridden in the subclass.
--
http://mail.python.org/mailman/listinfo/python-list


Re: callable virtual method

2009-08-14 Thread Diez B. Roggisch

Jean-Michel Pichavant schrieb:

Hi fellows,

Does anyone know a way to write virtual methods (in one virtual class) 
that will raise an exception only if called without being overridden ?
Currently in the virtual method I'm checking that the class of the 
instance calling the method has defined that method as well.


Example:

class Stream(object):
   """Interface of all stream objects"""
   def resetStats(self):
   """Reset the stream statistics. All values a zeroed except the 
date."""

   _log.info('Reset statistics of %s' % self)
   if self.__class__.resetStats == Stream.resetStats:
   raise NotImplementedError()

It works but it's tedious, I have to add these 2 lines to every virtual 
method, changing the content of the 2 lines.


Maybe there is a nice/builtin way to do so (python 2.4)


Python has no concept of "virtual" methods. A simple


class Stream(object):


   def resetStats(self):
   raise NotImplemented


is all you need. Once a subclass overrides resetStats, that 
implementatino is used.


Additionally, there are modules such as zope.interface out there, that 
let you define more formally what an interface is, and declare who's 
implementing it. I don't used this myself though, so I can't really 
comment to which extend it e.g. warns you if you subclass *without* 
implementing.


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


retrieve item from nested list given index tuple

2009-08-14 Thread Alan G Isaac
`lst` is a nested list

`tpl` is the indexes for an item in the list

What is the nice way to retrieve the item?
(Speedy access is nice.)

I don't want to use NumPy, but I'd like somehow
to avoid an explicit loop.  I did consider using
eval.  E.g., eval('lst' + '[%d]'*len(tpl)%tpl).
It works but seems rather ugly.  I kind of like
reduce(list.__getitem__, tpl, lst) but the
reliance on reduce remains controversial enough
to see i removed from the Python 3 built-ins ...

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


Re: callable virtual method

2009-08-14 Thread Steven D'Aprano
On Fri, 14 Aug 2009 16:49:47 +0200, Jean-Michel Pichavant wrote:

> Hi fellows,
> 
> Does anyone know a way to write virtual methods (in one virtual class)
> that will raise an exception only if called without being overridden ?
> Currently in the virtual method I'm checking that the class of the
> instance calling the method has defined that method as well.

I'm not entirely sure of the terminology -- is this the same as an 
abstract base class? Googling has not enlightened me. Given your example, 
it seems to be.


> Example:
> 
> class Stream(object):
> """Interface of all stream objects"""
> def resetStats(self):
> """Reset the stream statistics. All values a zeroed except the
> date."""
> _log.info('Reset statistics of %s' % self)
> if self.__class__.resetStats == Stream.resetStats:
> raise NotImplementedError()

The usual idiom I've seen for abstract methods is to simplify the check, 
and to put it *before* any work is done:

class Stream(object):
"""Interface of all stream objects"""
def resetStats(self):
if self.__class__ is Stream:
raise NotImplementedError()
_log.info('Reset statistics of %s' % self)

Even simpler is to just put the check in __init__, so to prevent the 
caller from creating an instance of the class:

class AbstractStream(object):
def __init__(self):
if self.__class__ is Stream:
raise NotImplementedError('abstract class')
def resetStats(self):
# This does not need to be over-ridden.
_log.info('Reset statistics of %s' % self)
def whatever(self):
# This *must* be over-ridden, and *cannot* be called
raise NotImplementedError('abstract method')


If you have a lot of methods, you can probably reduce the boilerplate 
with decorators:


# Untested
from functools import wraps
def abstract(func):
# Abstract methods don't have to be over-ridden, so long as they
# are called from a subclass of the abstract class.
@functools.wraps(func)
def inner(self, *args, **kwargs):
if self.__class__ is Stream:
raise NotImplementedError()
return func(self, *args, **kwargs)
return inner

def virtual(func):
# Virtual methods must be over-ridden, and must not be called by
# inheritance.
@functools.wraps(func)
def inner(self, *args, **kwargs):
raise NotImplementedError()
return inner

class Stream(object):
@abstract
def __init__(self):
pass
def resetStats(self):
_log.info('Reset statistics of %s' % self)
@virtual
def whatever(self):
pass



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


Why does my ftp script quit after couple of hours?

2009-08-14 Thread kk
Hi
This way the first time I did something with ftp stuff. I think that
generally it works but it stops working(quits or disappears) after
couple of hours of running.

This was a personal test-trial script for my own needs which was to
get my dynamic ip and broadcast to a client(I have a client script on
another computer). I sure could use something like DynDns for the same
purpose with easier management but I just wanted to give it a try to
see if i could even make it work .

Basically this script parses my ip from DynDns ip check page and
uploads it to the given ftp site. It works fine initially, it does
upload, it updates the Ip every hour but the problem is that after
couple of hours the Python console window disappears, I assume it
crashes.  I know it does upload at least couple times(works for couple
hours). it might be something to do with ftp connection. I will
investigate that but I just wanted to see if I have any logic or some
kind of contextual problem in the script.


Here is the link to Pastie page
http://pastie.org/584152


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


OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-14 Thread Steven D'Aprano
On Fri, 14 Aug 2009 07:07:31 -0700, Aahz wrote:

> "I saw `cout' being shifted "Hello world" times to the left and stopped
> right there."  --Steve Gonedes

Assuming that's something real, and not invented for humour, I presume 
that's describing something possible in C++. Am I correct? What the hell 
would it actually do???


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


Re: Why does my ftp script quit after couple of hours?

2009-08-14 Thread Diez B. Roggisch

kk schrieb:

Hi
This way the first time I did something with ftp stuff. I think that
generally it works but it stops working(quits or disappears) after
couple of hours of running.

This was a personal test-trial script for my own needs which was to
get my dynamic ip and broadcast to a client(I have a client script on
another computer). I sure could use something like DynDns for the same
purpose with easier management but I just wanted to give it a try to
see if i could even make it work .

Basically this script parses my ip from DynDns ip check page and
uploads it to the given ftp site. It works fine initially, it does
upload, it updates the Ip every hour but the problem is that after
couple of hours the Python console window disappears, I assume it
crashes.  I know it does upload at least couple times(works for couple
hours). it might be something to do with ftp connection. I will
investigate that but I just wanted to see if I have any logic or some
kind of contextual problem in the script.


Here is the link to Pastie page
http://pastie.org/584152



The code isn't very robust if anything happens retrieving the IP-website 
- if for some reason the connection is broken, it won't catch any 
ensuing IO-exception.


Also your way of extracing the IP is rather awkward, a simple re (often 
not the right tool, but here certainl yes) would help:


>>> m = re.search(r"(([0-9]+\.){3}[0-9]+)", body)
>>> m.group(1)
'85.177.92.101'
>>>


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


Re: Why does my ftp script quit after couple of hours?

2009-08-14 Thread kk
Hi Diez

Thanks for your insight. The reason I chose the awkward method to
parse the ip digits is that I was not familiar with the regex module
and the Dyndns Ip page is pretty simple page. I guess it is time to
learn more about the Re module.

As far as robustness, I agree with your assestment. I guess my main
confusion with my result is that the console window just disappears. I
wonder if I can make the window stay even if it crashesor if there are
connection issues? I will createa  seperate log file to see if I can
catch any issues in a log file.

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


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-14 Thread Grant Edwards
On 2009-08-14, Steven D'Aprano  wrote:
> On Fri, 14 Aug 2009 07:07:31 -0700, Aahz wrote:
>
>> "I saw `cout' being shifted "Hello world" times to the left and stopped
>> right there."  --Steve Gonedes
>
> Assuming that's something real, and not invented for humour, I presume 
> that's describing something possible in C++. Am I correct?

Yes.  In C++, the "<<" operator is overloaded.  Judging by the
context in which I've seen it used, it does something like
write strings to a stream.

> What the hell
> would it actually do???

IIRC in C++, 

   cout << "Hello world";

is equivalent to this in C:

   printf("Hellow world");

or this in Python:

   print "hellow world"

-- 
Grant Edwards   grante Yow! Bo Derek ruined
  at   my life!
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why does my ftp script quit after couple of hours?

2009-08-14 Thread Francesco Bochicchio
On 14 Ago, 18:03, kk  wrote:
> Hi
> This way the first time I did something with ftp stuff. I think that
> generally it works but it stops working(quits or disappears) after
> couple of hours of running.
>
> This was a personal test-trial script for my own needs which was to
> get my dynamic ip and broadcast to a client(I have a client script on
> another computer). I sure could use something like DynDns for the same
> purpose with easier management but I just wanted to give it a try to
> see if i could even make it work .
>
> Basically this script parses my ip from DynDns ip check page and
> uploads it to the given ftp site. It works fine initially, it does
> upload, it updates the Ip every hour but the problem is that after
> couple of hours the Python console window disappears, I assume it
> crashes.  I know it does upload at least couple times(works for couple
> hours). it might be something to do with ftp connection. I will
> investigate that but I just wanted to see if I have any logic or some
> kind of contextual problem in the script.
>
> Here is the link to Pastie pagehttp://pastie.org/584152
>
> Thanks

Try catching the exception inside the main loop, to prevent your
program to exit in case of failure:

if __name__=='__main__':
  while True:
try:
writeExtFile(FILE_PATH,FILE_NAME)
uploadFile
(FTP_NAME,FTP_USER_NAME,FTP_PASSWD,FTP_FOLDER,FILE_NAME)
time.sleep(TIME_DELAY)
except:
  err, det, tb = sys.exc_info()
  print "ERROR =>", err, det # gives you a description of the
occurred failure
  traceback.print_tb(tb) # this might be needed only for debug


Ciao

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


Natural Language Processing in Python

2009-08-14 Thread Prateek
Hi,

Can somebody please provide me link to a good online resource or e-
book for doing natural language processing programming in Python.

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


Re: Why does my ftp script quit after couple of hours?

2009-08-14 Thread Diez B. Roggisch

kk schrieb:

Hi Diez

Thanks for your insight. The reason I chose the awkward method to
parse the ip digits is that I was not familiar with the regex module
and the Dyndns Ip page is pretty simple page. I guess it is time to
learn more about the Re module.

As far as robustness, I agree with your assestment. I guess my main
confusion with my result is that the console window just disappears. I
wonder if I can make the window stay even if it crashesor if there are
connection issues? I will createa  seperate log file to see if I can
catch any issues in a log file.


I'm not a Windows-user, but even there it should be possible to *first* 
open the console, and then start the script in it.


That will keep it open & you can see the stacktrace.
Diez
--
http://mail.python.org/mailman/listinfo/python-list


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-14 Thread MRAB

Grant Edwards wrote:

On 2009-08-14, Steven D'Aprano  wrote:

On Fri, 14 Aug 2009 07:07:31 -0700, Aahz wrote:


"I saw `cout' being shifted "Hello world" times to the left and stopped
right there."  --Steve Gonedes
Assuming that's something real, and not invented for humour, I presume 
that's describing something possible in C++. Am I correct?


Yes.  In C++, the "<<" operator is overloaded.  Judging by the
context in which I've seen it used, it does something like
write strings to a stream.


What the hell
would it actually do???


IIRC in C++, 


   cout << "Hello world";


It also returns cout, so you can chain them:

cout << "Hello, " << name << '\n';


is equivalent to this in C:

   printf("Hellow world");

or this in Python:

   print "hellow world"



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


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-14 Thread Douglas Alan
On Aug 14, 12:17 pm, Grant Edwards  wrote:

> On 2009-08-14, Steven D'Aprano  wrote:

> > On Fri, 14 Aug 2009 07:07:31 -0700, Aahz wrote:

> >> "I saw `cout' being shifted "Hello world" times to the left and stopped
> >> right there."  --Steve Gonedes
>
> > Assuming that's something real, and not invented for humour, I presume
> > that's describing something possible in C++. Am I correct?
>
> Yes.  In C++, the "<<" operator is overloaded.  Judging by the
> context in which I've seen it used, it does something like
> write strings to a stream.

There's a persistent rumor that it is *this* very "abuse" of
overloading that caused Java to avoid operator overloading all
together.

But then then Java went and used "+" as the string concatenation
operator. Go figure!

|>ouglas

P.S. Overloading "left shift" to mean "output" does indeed seem a bit
sketchy, but in 15 years of C++ programming, I've never seen it cause
any confusion or bugs.

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


Re: callable virtual method

2009-08-14 Thread Jean-Michel Pichavant

MRAB wrote:

Jean-Michel Pichavant wrote:

Hi fellows,

Does anyone know a way to write virtual methods (in one virtual 
class) that will raise an exception only if called without being 
overridden ?
Currently in the virtual method I'm checking that the class of the 
instance calling the method has defined that method as well.


Example:

class Stream(object):
   """Interface of all stream objects"""
   def resetStats(self):
   """Reset the stream statistics. All values a zeroed except the 
date."""

   _log.info('Reset statistics of %s' % self)
   if self.__class__.resetStats == Stream.resetStats:
   raise NotImplementedError()

It works but it's tedious, I have to add these 2 lines to every 
virtual method, changing the content of the 2 lines.


Maybe there is a nice/builtin way to do so (python 2.4)


Why are you checking which class it's in? The method in the base class
will be called only if it hasn't been overridden in the subclass.


Sorry guys (means guys *and* gals :op ), I realized I've not been able 
to describe precisely what I want to do.
I'd like the base class to be virtual (aka abstract). However it may be 
abstract but it does not mean it cannot do some usefull stuff.



Here is the schema of my abstract methods :

class Interface(object):
   def method(self):
   # -
   # some common stuff executed here
   # -
   print 'hello world'
   # -
   # here shall stand child specific stuff (empty in the interface 
method)

   # -
   if self.__class__.method == Interface.method:
   raise NotImplementedError('You should have read the f** 
manual ! You must override this method.')


class GoodChild(Interface):
   def method(self):
  Interface.method(self) # I want to process the cool stuff done my 
the base Interface

  # 
  # Specific GoodChild stuff here
  # 
  print 'I am a good'
  return 'perfect'

class BadChild(Interface):
   pass #I'm too lazy


good = GoodChild()
bad = BadChild()

good.method()
...hello world
...I am a good

bad.method()
...NotImplementedError: You should have read the f** manual ! You 
must override this method.



The reason I'd like to do so: I'll be providing the Interface, but child 
classes will be overridden by others. Having a reliable error RTFM 
feedback is a time saver, for me and the users.

I hope I clarified my issue.

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


Re: Python Permutations Problem

2009-08-14 Thread MRAB

Asanka Wasala wrote:

Hi
I am developing a spell checker for my language, and I came across
solving an interesing problem. It would be great if you can suggest
me  an optimized solution for the problem described below:

I have certain group of letters like these:

Group #1: c,r,b
Group #2: a,z,k
Group #3: h,t
.
.

Letters within the same group can be substituted/replaced with the
rest of the letters in that group.

(ie. Group #1 means letter 'c' can be replaced with either 'r' or 'b',
and letter r can be replaced with either c or b, and letter b can be
replaced with either r or c)

A group can have upto 3 letters, and letters do not overlap between
groups. There can be upto 25 such groups.
Given a word containing above letters, (e.g. 'cat'.) I want to
generate all possible words by subsituting letters in the groups.

eg. expected output:

cat rat bat
czt rzt bzt
ckt rkt bkt
cah rah bah
czh rzh bzh
ckh rkh bkh

My code is given below. But for complex words like 'cczzk' it
thows the 'maximum recursion depth exceeded' error. Therefore, can you
suggest me an optimized solution to achive the above task? Thanks in
advance.
--

[snip]
This uses a generator in order:

def get_permutations(word, group_dict):
if word:
for first in group_dict[word[0]]:
for rest in get_permutations(word[1 : ], group_dict):
yield first + rest
else:
yield ""

def count_permutations(word, group_dict):
if word:
total = 1
for letter in word:
total *= len(group_dict[letter])
return total
else:
return 0

group_def = u"crb azk ht"

group_dict = {}
for group in group_def.split():
for letter in group:
group_dict[letter] = group

word = u"cat"

print "There are %d permutations." % count_permutations(word, group_dict)
print

for perm in get_permutations(word, group_dict):
print perm
--
http://mail.python.org/mailman/listinfo/python-list


Re: callable virtual method

2009-08-14 Thread Diez B. Roggisch

Jean-Michel Pichavant schrieb:

MRAB wrote:

Jean-Michel Pichavant wrote:

Hi fellows,

Does anyone know a way to write virtual methods (in one virtual 
class) that will raise an exception only if called without being 
overridden ?
Currently in the virtual method I'm checking that the class of the 
instance calling the method has defined that method as well.


Example:

class Stream(object):
   """Interface of all stream objects"""
   def resetStats(self):
   """Reset the stream statistics. All values a zeroed except the 
date."""

   _log.info('Reset statistics of %s' % self)
   if self.__class__.resetStats == Stream.resetStats:
   raise NotImplementedError()

It works but it's tedious, I have to add these 2 lines to every 
virtual method, changing the content of the 2 lines.


Maybe there is a nice/builtin way to do so (python 2.4)


Why are you checking which class it's in? The method in the base class
will be called only if it hasn't been overridden in the subclass.


Sorry guys (means guys *and* gals :op ), I realized I've not been able 
to describe precisely what I want to do.
I'd like the base class to be virtual (aka abstract). However it may be 
abstract but it does not mean it cannot do some usefull stuff.



Here is the schema of my abstract methods :

class Interface(object):
   def method(self):
   # -
   # some common stuff executed here
   # -
   print 'hello world'
   # -
   # here shall stand child specific stuff (empty in the interface 
method)

   # -
   if self.__class__.method == Interface.method:
   raise NotImplementedError('You should have read the f** 
manual ! You must override this method.')


class GoodChild(Interface):
   def method(self):
  Interface.method(self) # I want to process the cool stuff done my 
the base Interface

  # 
  # Specific GoodChild stuff here
  # 
  print 'I am a good'
  return 'perfect'

class BadChild(Interface):
   pass #I'm too lazy


good = GoodChild()
bad = BadChild()

good.method()
...hello world
...I am a good

bad.method()
...NotImplementedError: You should have read the f** manual ! You 
must override this method.



The reason I'd like to do so: I'll be providing the Interface, but child 
classes will be overridden by others. Having a reliable error RTFM 
feedback is a time saver, for me and the users.

I hope I clarified my issue.


First of all, I doubt the above code really yields that output. You are 
missing a super-call there in GoodChild


And the whole problem goes magically away if you start using OO a bit:


class Base(object):


  def method(self):
  self._do_some_work_for_method()
  print "some more work"

  def _do_some_work_for_method(self):
  raise NotImplemented


So your subclasses must implement something else instead of method - and 
voila, without any hassle things work as expected.


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


Python Permutations Problem

2009-08-14 Thread Asanka Wasala
Hi
I am developing a spell checker for my language, and I came across
solving an interesing problem. It would be great if you can suggest
me  an optimized solution for the problem described below:

I have certain group of letters like these:

Group #1: c,r,b
Group #2: a,z,k
Group #3: h,t
.
.

Letters within the same group can be substituted/replaced with the
rest of the letters in that group.

(ie. Group #1 means letter 'c' can be replaced with either 'r' or 'b',
and letter r can be replaced with either c or b, and letter b can be
replaced with either r or c)

A group can have upto 3 letters, and letters do not overlap between
groups. There can be upto 25 such groups.
Given a word containing above letters, (e.g. 'cat'.) I want to
generate all possible words by subsituting letters in the groups.

eg. expected output:

cat rat bat
czt rzt bzt
ckt rkt bkt
cah rah bah
czh rzh bzh
ckh rkh bkh

My code is given below. But for complex words like 'cczzk' it
thows the 'maximum recursion depth exceeded' error. Therefore, can you
suggest me an optimized solution to achive the above task? Thanks in
advance.
--
CODE---
#-*- coding: UTF-8 -*-
import re
def getPermutations(word):
def getSimilarLetters(letter):
pattern=u"|c,r,b|a,z,k|h,t|"
list=[]
ptrn=re.compile("(?P\|(.?,)*?"+letter+"(,.)*?\|)")
k=ptrn.search(pattern)
if k:
letterlist=k.group("grp")[1:-1]
list=letterlist.split(",")
list.remove(letter)
return list
else:
return letter

list=[]

def perm(word):
posi=0
wrd=word
for w in word:
posi=posi+1
lst=getSimilarLetters(w)
if len(lst)>0:
for l in lst:
newwrd=wrd[:posi-1]+l+wrd[posi:]
if not (newwrd in list):
list.append(newwrd)
perm(newwrd)

try:
perm(word)
except RuntimeError:
list=[]
list.append("error")
return list

list=getPermutations(u"cat")

for i in list:
print i.encode('utf-8')
END of
CODE-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] expy: an expressway to extend Python

2009-08-14 Thread Yingjie Lan
--- On Sat, 8/8/09, Stefan Behnel  wrote:

> From: Stefan Behnel 
> Subject: Re: [Python-Dev] expy: an expressway to extend Python
> To: [email protected]
> Date: Saturday, August 8, 2009, 4:55 PM
> > More details at http://expy.sourceforge.net/
> 
> I'm clearly biased, but my main concern here is that expy
> requires C code
> to be written inside of strings. There isn't any good
> editor support for
> that, so I doubt that expy is good for anything but very
> thin wrappers (as
> in the examples you presented).

Thanks a lot for the input -- I sort of recaptured the advantages of expy and 
listed four points in the new introduction at http://expy.sf.net/ homepage. 

Lacking of editor highlight support is quite a problem, but it is possible to 
create a support. For example, you can use this to indicate the start of 
embedded code highlight: 

return """

and then the end mark is of course the enclosing """

> 
> That said, you might want to look at the argument unpacking
> code generated
> by Cython. It's highly optimised through specialisation and
> has been
> benchmarked quite a bit faster than the generic Python
> C-API functions for
> tuple/keyword extracting. Since argument conversion seems
> to be more or
> less all that expy really does, maybe you want to reuse
> that code.
> 
> Stefan

Oh sure, that's nice if that part can be adopted by expy-cxpy. Any help out on 
this would be very welcomed.


Yingjie


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


Re: A Exhibition Of Tech Geekers Incompetence: Emacs whitespace-mode

2009-08-14 Thread vippstar
On Aug 14, 4:36 am, Xah Lee  wrote:
> • A Exhibition Of Tech Geekers Incompetence: Emacs whitespace-mode
>  http://xahlee.org/UnixResource_dir/writ/emacs_whitespace-mode_problem...

Instead of writing a completely useless article you could had asked
for help in an emacs newsgroup, or wait until someone else does that
for you too (creating and publishing such configuration you desire).
Your article, except for the brief description of that mode, is
completely useless. Why would you fill your website with junk? Perhaps
because you like to write but you're too lazy to write something of
value.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: retrieve item from nested list given index tuple

2009-08-14 Thread Steven D'Aprano
On Fri, 14 Aug 2009 15:54:54 +, Alan G Isaac wrote:

> `lst` is a nested list
> 
> `tpl` is the indexes for an item in the list

> What is the nice way to retrieve the item? (Speedy access is nice.)

Assuming you want to do this frequently, write a helper function, then 
use it:

# Untested
def extract(nested, indexes):
for index in indexes:
nested = nested[index]
return nested


> I don't want to use NumPy, but I'd like somehow to avoid an explicit
> loop.  I did consider using eval.  E.g., eval('lst' +
> '[%d]'*len(tpl)%tpl). It works but seems rather ugly.

And slow.


> I kind of like
> reduce(list.__getitem__, tpl, lst) but the reliance on reduce remains
> controversial enough to see i removed from the Python 3 built-ins ...

It's just moved into functools.


>>> lst = ['a', 'b', ['aa', ['aaa', 'bbb', 'ccc'], 'cc']]
>>> from functools import reduce
>>> lst = ['a', 'b', ['aa', ['aaa', 'bbb', 'ccc'], 'cc']]
>>> reduce(list.__getitem__, (2, 1, 0), lst)
'aaa'


However, it doesn't work too well as soon as you mix sequence types:

>>> reduce(list.__getitem__, (2, 1, 0, 0), lst)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: descriptor '__getitem__' requires a 'list' object but received 
a 'str'

Try this instead:

>>> from operator import getitem
>>> reduce(getitem, (2, 1, 0), lst)
'aaa'
>>> reduce(getitem, (2, 1, 0, 0), lst)
'a'

operator.getitem is less ugly too.



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


Re: csv.DictWriter.write_header()

2009-08-14 Thread Chris Withers

Alan G Isaac wrote:

On 8/13/2009 7:58 AM John Machin apparently wrote:

Duck typing: ask a silly question, get a silly answer.


Maybe if you learned to be a more generous reader,
fewer questions would look "silly" to you.


If you take a look at the crap that John very patiently wades through on 
the python-excel group, you might consider that a little light hearted 
sarcasm isn't much to put up with ;-)


FWIW, if you're using csv because you think .xls is too hard to 
generate, try xlwt :-)


cheers,

Chris

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


Re: trouble with reload

2009-08-14 Thread Colin J. Williams

Steven D'Aprano wrote:

On Fri, 14 Aug 2009 09:23:17 -0400, Colin J. Williams wrote:


It's typically a user module that needs to be reloaded.


What's a user module?

A module written by a user, as distinguished from a libary




It seems that  del sys.modules['moduleName'] has no effect.


sys.modules is just a dictionary, I find it hard to believe that deleting 
from it has no effect. It works for me:



import sys
import math
'math' in sys.modules

True

del sys.modules['math']
'math' in sys.modules

False

What behaviour do you get?


Of course deleting the math module from the cache doesn't do anything to 
the math module in your namespace:



math



del math
math

Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'math' is not defined

Of course deleting the module (or reloading it) doesn't have any effect 
on any objects you already have:




import math
func = math.sin
del sys.modules['math']
del math
math.sin(1.2)

Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'math' is not defined

func(1.2)

0.93203908596722629





Is there some other way of ensuring that any import goes to
moduleName.py, instead of moduleName.pyc?


Delete moduleName.pyc.

Make sure the .pyc file doesn't exist in the first place.

Make sure the last modification date of the .py file is newer than the 
modification date of the .pyc file.


That's easier said than done, when one is working with an IDE.  The cached 
.pyc file might be different from that in the file.


Colin W.





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


Re: httplib incredibly slow :-(

2009-08-14 Thread Chris Withers

Aahz wrote:

Sorry, I mostly have been working on our Mac port, so I'm not sure what's
needed to make this work on Windows.  Did you try downloading the PyCurl
binary?  Maybe it statically links libcurl on Windows.


Shame it's not available as a bdist_egg, that's what I'm really after...


What do you need to know for a decent example?


Simple download of a file from a url with some auth headers added would 
do me.
Other than that, nice to haves would be how to build a http post with 
fields in a multi-part body, some of which might be files.


cheers,

Chris

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


Re: A Exhibition Of Tech Geekers Incompetence: Emacs whitespace-mode

2009-08-14 Thread vippstar
On Aug 14, 8:25 pm, fortunatus  wrote:
> On Aug 14, 1:01 pm, vippstar  wrote:
>
> > Why would you fill your website with junk?
>
> The OP made it clear:
>
> >Just wanted to express some frustration with whitespace-mode.

You took my question out of context and answered it. I read the
article, it's not like I missed any of it. Plus, it's not a real
answer. "Because I wanted to". Why did you? Why would you?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: callable virtual method

2009-08-14 Thread Jean-Michel Pichavant

Diez B. Roggisch wrote:

Jean-Michel Pichavant schrieb:

MRAB wrote:

Jean-Michel Pichavant wrote:

Hi fellows,

Does anyone know a way to write virtual methods (in one virtual 
class) that will raise an exception only if called without being 
overridden ?
Currently in the virtual method I'm checking that the class of the 
instance calling the method has defined that method as well.


Example:

class Stream(object):
   """Interface of all stream objects"""
   def resetStats(self):
   """Reset the stream statistics. All values a zeroed except 
the date."""

   _log.info('Reset statistics of %s' % self)
   if self.__class__.resetStats == Stream.resetStats:
   raise NotImplementedError()

It works but it's tedious, I have to add these 2 lines to every 
virtual method, changing the content of the 2 lines.


Maybe there is a nice/builtin way to do so (python 2.4)


Why are you checking which class it's in? The method in the base class
will be called only if it hasn't been overridden in the subclass.


Sorry guys (means guys *and* gals :op ), I realized I've not been 
able to describe precisely what I want to do.
I'd like the base class to be virtual (aka abstract). However it may 
be abstract but it does not mean it cannot do some usefull stuff.



Here is the schema of my abstract methods :

class Interface(object):
   def method(self):
   # -
   # some common stuff executed here
   # -
   print 'hello world'
   # -
   # here shall stand child specific stuff (empty in the 
interface method)

   # -
   if self.__class__.method == Interface.method:
   raise NotImplementedError('You should have read the 
f** manual ! You must override this method.')


class GoodChild(Interface):
   def method(self):
  Interface.method(self) # I want to process the cool stuff done 
my the base Interface

  # 
  # Specific GoodChild stuff here
  # 
  print 'I am a good'
  return 'perfect'

class BadChild(Interface):
   pass #I'm too lazy


good = GoodChild()
bad = BadChild()

good.method()
...hello world
...I am a good

bad.method()
...NotImplementedError: You should have read the f** manual ! You 
must override this method.



The reason I'd like to do so: I'll be providing the Interface, but 
child classes will be overridden by others. Having a reliable error 
RTFM feedback is a time saver, for me and the users.

I hope I clarified my issue.


First of all, I doubt the above code really yields that output. You 
are missing a super-call there in GoodChild


And the whole problem goes magically away if you start using OO a bit:


class Base(object):


  def method(self):
  self._do_some_work_for_method()
  print "some more work"

  def _do_some_work_for_method(self):
  raise NotImplemented


So your subclasses must implement something else instead of method - 
and voila, without any hassle things work as expected.


Diez


It does yield that output, there's an unbound call to Interface.method.

Your solution will work, for sure. The problem is that it will dumb down 
the Base class interface, multiplying the number of methods by 2. This 
would not be an issue in many cases, in mine there's already too much 
meaningful methods in my class for me to add artificial ones.


Thanks for the tip anyway.
JM






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


Re: A Exhibition Of Tech Geekers Incompetence: Emacs whitespace-mode

2009-08-14 Thread Jean-Michel Pichavant

vippstar wrote:

On Aug 14, 8:25 pm, fortunatus  wrote:
  

On Aug 14, 1:01 pm, vippstar  wrote:



Why would you fill your website with junk?
  

The OP made it clear:



Just wanted to express some frustration with whitespace-mode.
  


You took my question out of context and answered it. I read the
article, it's not like I missed any of it. Plus, it's not a real
answer. "Because I wanted to". Why did you? Why would you?
  
FYI, Xah Lee is a well known BTFL (Benevolent Troller For Life), you 
shouldn't argue about one of his post.

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


Re: Unrecognized escape sequences in string literals

2009-08-14 Thread Steven D'Aprano
I think I've spent enough time on this discussion, so I won't be directly 
responding to any of your recent points -- it's clear that I'm not 
persuading you that there's any justification for any behaviour for 
escape sequences other than the way C++ deals with them. That's your 
prerogative, of course, but I've done enough tilting at windmills for 
this week, so I'll just make one final comment and then withdraw from an 
unproductive argument. (I will make an effort to read any final comments 
you wish to make, so feel free to reply. Just don't expect an answer to 
any questions.)

Douglas, you and I clearly have a difference of opinion on this. Neither 
of us have provided even the tiniest amount of objective, replicable, 
reliable data on the error-proneness of the C++ approach versus that of 
Python. The supposed superiority of the C++ approach is entirely 
subjective and based on personal opinion instead of quantitative facts.

I prefer languages that permit anything that isn't explicitly forbidden, 
so I'm happy that Python treats non-special escape sequences as valid, 
and your attempts to convince me that this goes against the Zen have 
entirely failed to convince me. As I've done before, I will admit that 
one consequence of this design is that it makes it hard to introduce new 
escape sequences to Python. Given that it's vanishingly rare to want to 
do so, and that wanting to add backslashes to strings is common, I think 
that's a reasonable tradeoff. Other languages may make different 
tradeoffs, and that's fine by me.



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


Re: retrieve item from nested list given index tuple

2009-08-14 Thread Colin J. Williams

Steven D'Aprano wrote:

On Fri, 14 Aug 2009 15:54:54 +, Alan G Isaac wrote:


`lst` is a nested list

`tpl` is the indexes for an item in the list



What is the nice way to retrieve the item? (Speedy access is nice.)


Assuming you want to do this frequently, write a helper function, then 
use it:


# Untested
def extract(nested, indexes):
for index in indexes:
nested = nested[index]
return nested


This looks OK for the first level of nesting.  We are not told much about tpl 
but suppose that:


lst= [a, [b, [c, d]], [e, f]] and that we wish to retrieve d and f from lst. 
tpl would need to be something like [[1, 1, 1], [2, 1]].


If that is the requirement, then Untested is only a step along the road, 
extract could be made recursive.


Colin W.




I don't want to use NumPy, but I'd like somehow to avoid an explicit
loop.  I did consider using eval.  E.g., eval('lst' +
'[%d]'*len(tpl)%tpl). It works but seems rather ugly.


And slow.



I kind of like
reduce(list.__getitem__, tpl, lst) but the reliance on reduce remains
controversial enough to see i removed from the Python 3 built-ins ...


It's just moved into functools.



lst = ['a', 'b', ['aa', ['aaa', 'bbb', 'ccc'], 'cc']]
from functools import reduce
lst = ['a', 'b', ['aa', ['aaa', 'bbb', 'ccc'], 'cc']]
reduce(list.__getitem__, (2, 1, 0), lst)

'aaa'


However, it doesn't work too well as soon as you mix sequence types:


reduce(list.__getitem__, (2, 1, 0, 0), lst)

Traceback (most recent call last):
  File "", line 1, in 
TypeError: descriptor '__getitem__' requires a 'list' object but received 
a 'str'


Try this instead:


from operator import getitem
reduce(getitem, (2, 1, 0), lst)

'aaa'

reduce(getitem, (2, 1, 0, 0), lst)

'a'

operator.getitem is less ugly too.




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


Re: retrieve item from nested list given index tuple

2009-08-14 Thread Alan G Isaac
On 8/14/2009 1:09 PM Steven D'Aprano apparently wrote:
> Try this instead:
> 
 from operator import getitem
 reduce(getitem, (2, 1, 0), lst)
> 'aaa'
 reduce(getitem, (2, 1, 0, 0), lst)
> 'a'
> 
> operator.getitem is less ugly too.


Yes, that's better.
Thanks,
Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: trouble with reload

2009-08-14 Thread Steven D'Aprano
On Fri, 14 Aug 2009 13:14:16 -0400, Colin J. Williams wrote:

> Steven D'Aprano wrote:
>> On Fri, 14 Aug 2009 09:23:17 -0400, Colin J. Williams wrote:
>> 
>>> It's typically a user module that needs to be reloaded.
>> 
>> What's a user module?
> A module written by a user, as distinguished from a libary

You mean an end-user of the application? Or you mean the application 
programmer? 

I'm not sure there is any difference between "user modules" and "library 
modules" apart from what they do.

Perhaps it would help if you could explain the circumstances of when you 
would reload a module. During application development, for an incremental 
edit/reload/test cycle? Or while the completed application is being 
executed?


>>> Is there some other way of ensuring that any import goes to
>>> moduleName.py, instead of moduleName.pyc?
>> 
>> Delete moduleName.pyc.
>> 
>> Make sure the .pyc file doesn't exist in the first place.
>> 
>> Make sure the last modification date of the .py file is newer than the
>> modification date of the .pyc file.
> 
> That's easier said than done, when one is working with an IDE.  The
> cached .pyc file might be different from that in the file.

If the IDE is getting in your way, then don't use it.

Or if you tell us what IDE you're using, and precisely what it is doing, 
somebody can tell you how to defeat the IDE's cache.

(That won't be me -- I don't use IDEs.)

Or perhaps you're entire approach is wrong, and you shouldn't be using 
reload() at all. Don't know.



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


Re: callable virtual method

2009-08-14 Thread Nigel Rantor

Jean-Michel Pichavant wrote:


Your solution will work, for sure. The problem is that it will dumb down 
the Base class interface, multiplying the number of methods by 2. This 
would not be an issue in many cases, in mine there's already too much 
meaningful methods in my class for me to add artificial ones.


Thanks for the tip anyway.


I suggest you reconsider.

You asked a question and have been given a standard way of achieving the 
desired outcome.


It's common in OO to use a Template pattern like this.

If you're not interested in finding out how loads of people have already 
solved the problem then why ask?


The methods that require overriding can be prefixed with an underscore 
so that people get a hint that they are an implementation detail rather 
than part of the public interface.


I don't see your problem, other than a vague aesthetic unease.

Regards,

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


Komodo(!)

2009-08-14 Thread David C Ullrich
Probably this isn't news to anyone but me, but just in case:

Last I heard Komodo was a very highly regarded IDE that unfortunately
cost money. Yesterday I discovered that they now have an editor
available for free.

Doesn't contain all the features of the IDE, but just having glanced
at it it seems to contain more than I'll ever use. What I really
like about it is that one can write Komodo scripts in Python
(using an extensive API allowing various manipulations of the
current document).


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


Re: callable virtual method

2009-08-14 Thread Steven D'Aprano
On Fri, 14 Aug 2009 18:49:26 +0200, Jean-Michel Pichavant wrote:

> Sorry guys (means guys *and* gals :op ), I realized I've not been able
> to describe precisely what I want to do. I'd like the base class to be
> virtual (aka abstract). However it may be abstract but it does not mean
> it cannot do some usefull stuff.
> 
> 
> Here is the schema of my abstract methods :
> 
> class Interface(object):
> def method(self):
> # -
> # some common stuff executed here
> # -
> print 'hello world'
> # -
> # here shall stand child specific stuff (empty in the interface
> method)
> # -
> if self.__class__.method == Interface.method:
> raise NotImplementedError('You should have read the f**
> manual ! You must override this method.')


Okay, so I want to sub-class your Interface class. As you said, the 
methods in the abstract class are still useful, so in my class, I don't 
need any extra functionality for some methods -- I'm happy with just the 
"common stuff". So I use normal OO techniques and over-ride just the 
methods I need to over-ride:

class GoodChild(Interface):
# no need to over-ride method, because it does everything I want
# but over-ride other methods that don't
def whatever(self):
print "Whatever..."
return Interface.whatever()

But now my class fails, with an insulting error message *wink*, and you 
force me to write a whole lot of crappy stupid boilerplate code:

class VerboseGoodChild(Interface):
# forced to over-ride methods for no good reason
def method(self):
return Interface.method(self)
def another_method(self):
return Interface.another_method(self)
def yet_another_method(self):
return Interface.yet_another_method(self)
def still_more_methods(self):
return Interface.still_more_methods(self)
# Whew! That was a waste of effort. Now at last over-ride the
# methods I need to:
def whatever(self):
print "Whatever..."
return Interface.whatever()

After the fourth such class, I say "Bugger this for a game of soldiers" 
and dump your Interface class for something else.


> The reason I'd like to do so: I'll be providing the Interface, but child
> classes will be overridden by others. Having a reliable error RTFM
> feedback is a time saver, for me and the users. I hope I clarified my
> issue.

The usual way of implementing abstract base classes is to simply prohibit 
instantiation of the class, but allow all other inheritance. Putting 
useful functionality in methods, but then prohibiting subclasses from 
using them without jumping through hoops first, seems rather perverse to 
me.



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


Re: callable virtual method

2009-08-14 Thread Jean-Michel Pichavant

Nigel Rantor wrote:

Jean-Michel Pichavant wrote:


Your solution will work, for sure. The problem is that it will dumb 
down the Base class interface, multiplying the number of methods by 
2. This would not be an issue in many cases, in mine there's already 
too much meaningful methods in my class for me to add artificial ones.


Thanks for the tip anyway.


I suggest you reconsider.

You asked a question and have been given a standard way of achieving 
the desired outcome.


It's common in OO to use a Template pattern like this.

If you're not interested in finding out how loads of people have 
already solved the problem then why ask?


The methods that require overriding can be prefixed with an underscore 
so that people get a hint that they are an implementation detail 
rather than part of the public interface.


I don't see your problem, other than a vague aesthetic unease.

Regards,

  n
I understand how refuting some obvious solution may look just stupid. 
You're right, I shouldn't have asked.


By the way I'd like to know if I am I alone to find that

class Stream:
   def start
   def stop
   def reset

is better than

class Stream:
   def start
   def _start
   def stop
   def _stop
   def reset
   def _reset

(try to figure out with 20+ methods)
What you call aesthetic may sometimes fall into readability.


JM

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


Any way to adjust difflib algorithm?

2009-08-14 Thread Grant Edwards
I'm trying to use difflib to compare two files, and it's not
producing very useful results.  When comparing two lines where
only a few characters have changed, it usually seems to decide
that a line was deleted/inserted/replaced rather than changed.

Here's how I'm using it:

   #!/usr/bin/python
   import sys,difflib
   
   fromlines = [l.rstrip('\n') for l in open(sys.argv[1]).readlines()]
   tolines   = [l.rstrip('\n') for l in open(sys.argv[2]).readlines()]
   
   print difflib.HtmlDiff().make_file(fromlines,tolines)
   
In my particular usage, no lines have ever been
inserted/deleted, so perhaps I should be running diffs on
individual lines instead?  If I do that, I can't figure out how
to generate HTML output.

Is there a way to tell the differ to try harder to match lines?

-- 
Grant Edwards   grante Yow! I hope something GOOD
  at   came in the mail today so
   visi.comI have a REASON to live!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: implementing descriptors

2009-08-14 Thread Dave Angel

dippim wrote:

On Aug 14, 10:48 am, Dave Angel  wrote:
  

dippim wrote:


On Aug 14, 2:34 am, Raymond Hettinger  wrote:
  

[David]


I am new to Python and I have a question about descriptors.  If I have
a class as written below, is there a way to use descriptors to be
certain that the datetime in start is always before the one in end?
  
class foo(object):

   def __init__(self,a =e,b = None)
  self.start > >>>   self.end >
from datetime import datetime
c =etime(2009,8,13,6,15,0)
d =etime(2009,8,14,12,0,0)
afoo =(c,d)
  
For instance, if the following code were run, I would like to instance

of foo to switch the start and end times.
  
afoo.start =etime(2010,8,13,6,15,0)
  
I was thinking of using the __set__ descriptor to catch the assignment

and reverse the values if necessary, but I can't figure out how to
determine which values is being set.
  

You're on the right track, but it is easier to use property() than to
write your own custom descriptor with __get__ and __set__.

class foo(object):

def __init__(self,a =e,b = None):
self._start > >> self._end > >> def get_start(self):
return self._start
def set_start(self, value):
if self._end is None or value < self._end:
self._start =ue
else:
self._end =ue
start =perty(get_start, set_start)
def get_end(self):
return self._end
def set_end(self, value):
if self._start is None or value > self._start:
self._end =ue
else:
self._start =ue
end =perty(get_end, set_end)

Raymond


Raymond,
   This functionality is exactly what I was looking for. Thanks!  I'll
be using this to solve my problem.
  
   Now that I'm on the right track, I'm still a bit confused about how

__get__ and __set__ are useful.  Admittedly, I don't need to
understand them to solve this problem, but perhaps they may be useful
in the future.  If I wanted to solve this problem using __get__ and
__set__ could it be done?
  
Thanks Again!
  

DANGER- WILL ROBINSON!

Don't use this code as-is.  There is a nasty surprise waiting for the
caller when he sets start and end, and discovers that one of them gets
thrown out, and an old value still remains.

obj=oo(3, 5)
obj.start = 8
obj.end = 12
print obj.start, obj.end

will print out  3, 12.Not what the caller expected.



You're right about this and I appreciate the warning, but I think what
Raymond was going for was directional accuracy without a great deal of
his time wasted on details.  The explanation served the purpose of
moving me forward using property() and I'm thankful for it.

  

Four fixes, in order of preference:
0) Trust your user to read and obey your docstrings.  This was what JM
was implying, by changing the names of the formal parameters.



Considering I am not the sharpest knife in the drawer, I don't wish to
start a philosophical discussion about the relative aptitude and
capabilities of the people who might use a class I build.  However, I
will say that as this particular requirement is imposed on this class
by the writer, shouldn't it be the writer's responsibility to enforce
it, especially, when the cost of enforcement is so low?

  

1)  make a new method that sets both values, making these two properties
readonly.  That new method would make sure the two parameters are
self-consistent.  Making the actual values readonly can be done with a
descriptor as well, or even a decorator.



  

2) Raise an exception in the getter methods if they're out of order
3) do the min/max logic on the getter methods, but I don't like that one
at all.

DaveA




  
I'm going to assume by "writer" you mean yourself?  In other words, the 
interface isn't being forced upon you by someone else?  So I'll continue 
to push for a cleaner/clearer interface.


I'm not going to make any judgment about the relative abilities of you 
and the caller of your class.  You make the final judgment call on how 
much you want to bulletproof your interface.


But occasionally the "automatic fix" causes more problems than it 
solves.  Now, this time you use the word "enforce," which is much 
different than the original implied "fix up."  So I'll assume you mean 
it literally, that you want to detect when the caller has broken the 
rules, and raise an exception in that case.  In either case, you need to 
define what "the rules" are.  And normally this is done by specifying 
invariants.


I think you're declaring an invariant that, at all times, the object has 
two datetime attributes, and that obj.start is no greater than obj.end.  
If the attributes are individually writable, and you do the swap at that 
time, you get into the trouble I described above.  On the other hand, if 
you just do a raise, then your caller would still get into trouble for 
the same sequence of calls.


So the cure is to either make a ne

Re: callable virtual method

2009-08-14 Thread Nigel Rantor

Jean-Michel Pichavant wrote:

Nigel Rantor wrote:

Jean-Michel Pichavant wrote:


Your solution will work, for sure. The problem is that it will dumb 
down the Base class interface, multiplying the number of methods by 
2. This would not be an issue in many cases, in mine there's already 
too much meaningful methods in my class for me to add artificial ones.


Thanks for the tip anyway.


I suggest you reconsider.

You asked a question and have been given a standard way of achieving 
the desired outcome.


It's common in OO to use a Template pattern like this.

If you're not interested in finding out how loads of people have 
already solved the problem then why ask?


The methods that require overriding can be prefixed with an underscore 
so that people get a hint that they are an implementation detail 
rather than part of the public interface.


I don't see your problem, other than a vague aesthetic unease.

Regards,

  n
I understand how refuting some obvious solution may look just stupid. 
You're right, I shouldn't have asked.


I never said it seemed stupid. I was merely curious as to why you'd ask 
a question and ignore solutions.



By the way I'd like to know if I am I alone to find that

class Stream:
   def start
   def stop
   def reset

is better than

class Stream:
   def start
   def _start
   def stop
   def _stop
   def reset
   def _reset

(try to figure out with 20+ methods)
What you call aesthetic may sometimes fall into readability.


Depends on what you mean by "better".

Do you mean pleasing to your eye or performs the task you want it to?

Assuming you are taking the aesthetic viewpoint I think that in this 
case it will depend on how you set out your code.


Realise that all of the underscore methods for your class are 
boilerplate, they simply raise an exception.


They can all be at the end of the file, commented as an entire block to 
be left alone.


Editing the main body of code is then fairly easy, and uncluttered...

e.g.

#
# Stream class blah blah blah
#
class Stream:

def start

def stop

def reset

#
# stubs to be over-ridden in sub-classes, add one for each
# method that requires overriding.
#
def _start
def _stop
def _reset

Regards,

  Nigel

p.s. Please take this in the spirit it is offered. I'm trying to stop 
you from ignoring a good suggestion, not make you feel like a fool.

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


Re: Any way to adjust difflib algorithm?

2009-08-14 Thread Chris Rebert
On Fri, Aug 14, 2009 at 2:38 PM, Grant Edwards wrote:
> I'm trying to use difflib to compare two files, and it's not
> producing very useful results.  When comparing two lines where
> only a few characters have changed, it usually seems to decide
> that a line was deleted/inserted/replaced rather than changed.

> Is there a way to tell the differ to try harder to match lines?

You could use a wordwise diff instead: http://www.gnu.org/software/wdiff/
Obviously that's not a pure Python solution though.

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Any way to adjust difflib algorithm?

2009-08-14 Thread Grant Edwards
On 2009-08-14, Grant Edwards  wrote:

> I'm trying to use difflib to compare two files, and it's not
> producing very useful results.  When comparing two lines where
> only a few characters have changed, it usually seems to decide
> that a line was deleted/inserted/replaced rather than changed.

[...]

> In my particular usage, no lines have ever been
> inserted/deleted, so perhaps I should be running diffs on
> individual lines instead?  If I do that, I can't figure out
> how to generate HTML output.

I ended up using the SequenceMatcher on individual pairs of
lines and generating my own HTML based on the results of
get_matching_blocks().

That produced the desired results.

-- 
Grant Edwards   grante Yow! I have a very good
  at   DENTAL PLAN.  Thank you.
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: callable virtual method

2009-08-14 Thread Dave Angel

Jean-Michel Pichavant wrote:
Nigel 
Rantor wrote:

Jean-Michel Pichavant wrote:


Your solution will work, for sure. The problem is that it will dumb 
down the Base class interface, multiplying the number of methods by 
2. This would not be an issue in many cases, in mine there's already 
too much meaningful methods in my class for me to add artificial ones.


Thanks for the tip anyway.


I suggest you reconsider.

You asked a question and have been given a standard way of achieving 
the desired outcome.


It's common in OO to use a Template pattern like this.

If you're not interested in finding out how loads of people have 
already solved the problem then why ask?


The methods that require overriding can be prefixed with an 
underscore so that people get a hint that they are an implementation 
detail rather than part of the public interface.


I don't see your problem, other than a vague aesthetic unease.

Regards,

  n
I understand how refuting some obvious solution may look just stupid. 
You're right, I shouldn't have asked.


By the way I'd like to know if I am I alone to find that

class Stream:
   def start
   def stop
   def reset

is better than

class Stream:
   def start
   def _start
   def stop
   def _stop
   def reset
   def _reset

(try to figure out with 20+ methods)
What you call aesthetic may sometimes fall into readability.


JM




Usually when one defines an abstract base class, one expects many people 
will derive from it, as opposed to only one having to write it.  So if 
the base must be "ugly" according to some definition, so be it.  
Aesthetics are quite subjective.


Nigel's approach has another benefit not stated, which is to keep the 
child class code simpler.  They avoid the need to call any base class 
method to access the common logic.  The downside is it assumes that the 
common logic will always be either at the beginning or always at the end 
of the child classes implementation.  That's because the base class has 
hardcoded where in its implementation to call the child class method.


Anyway, without arguing for or against either approach, I'd point out 
that you could have an extra formal parameter in the base method, which 
is a private signal from the child class that this is the internal 
call.  Missing such a formal parameter would then trigger the "missing 
method in derived class" error message.  You'd check such a parameter 
the same place as you're now checking the object's type.


DaveA


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


  1   2   >