Re: exporting symbols with ctypes?

2009-04-06 Thread Diez B. Roggisch

Brian schrieb:

I'd like to load a library that expects executables which link against
it to provide a particular symbol.  Is there a way to do the inverse
of the in_dll() operation?  I'd prefer to avoid creating a brand new
library on the fly just to satisfy this one dependency.


Maybe elmer can help here:

http://elmer.sourceforge.net/

It appears to be a bit dated, but maybe it shows a way how to go about this.

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


Re: Killing threads

2009-04-06 Thread bieffe62
On Apr 5, 9:48 pm, Dennis Lee Bieber  wrote:
> On Sun, 05 Apr 2009 12:54:45 +0200, Francesco Bochicchio
>  declaimed the following in
> gmane.comp.python.general:
>
> > If yor threads are not set as 'deamons' using Thread.setDaemon method,
> > then your main program at its termination should call Thread.join for
> > each of the thread spawned, otherwise the whole process will not quit.
>
>         .join() alone won't do anything but wait for the thread itself to
> quit -- which means one still has to signal the threads to commit
> suicide.
>

Yes. Mine was an 'additional suggestion' to the ones that the OP
already received.
I guests that was not clear enough ...

.. follows a nice explanation on methods to stop threads that I was
too lazy to write ...

>
>         If the thread has the capability to become blocked on some operation
> (say a socket read without timeout), none of these solutions will work.
> That just leaves setting the threads daemonic at the start -- which
> indicates the runtime may brutally kill them when the main program
> exits.
>

You know, this bugger me a little. I know that killing threads is hard
in any language
(I'm facing now the issue in a C++ program I'm writing at work),
expecially doing in
a platform-independent way, but Java managed to do it. Now python is
in many ways an
higher level language than Java, but when it comes to threading I feel
it lacks something.
I know that often it is not too hard to avoid blocking reads, and you
can always use subprocesses that
with the new multiprocessing module are almost as easy as threads, but
still ...

Ciao
-
FB

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


Monitoring Internet Explorer

2009-04-06 Thread a b
Hi,
I'm trying to write a program that monitor Internet Explorer events - creating/deletion of the process, loading pages, creating tabs etc. I managed to monitor creation/deletion by using WMI, but I couldn't find a way to monitor the rest of the events. Is there a way to do this ?

Thanks.
 



Walla! Mail - Get your free unlimited mail today--
http://mail.python.org/mailman/listinfo/python-list


Re: Killing threads

2009-04-06 Thread bieffe62
On 6 Apr, 05:25, ericwoodwo...@gmail.com wrote:
> On Apr 5, 11:07 pm, Dennis Lee Bieber  wrote:
>
>
>
>
>
> > On Sun, 5 Apr 2009 17:27:15 -0700 (PDT), imageguy
> >  declaimed the following in
> > gmane.comp.python.general:
>
> > > In threading.Event python 2.5 docs say;
> > > "This is one of the simplest mechanisms for communication between
> > > threads: one thread signals an event and other threads wait for it. "
>
> > > Again, I have limited experience, however, in my reading of the
> > > threading manual and review examples, Events were specifically design
> > > to be a thread safe way to communicate a 'state' to running threads ?
> > > In the OP's example 'do stuff' was open to wide interpretation,
> > > however, if within the thread's main 'while' loop the tread checks to
> > > see if the 'keepgoing' Event.isSet(), in what scenario would this
> > > create deadlock ?
>
> >         If you are going to perform a CPU intensive polling loop, there is
> > no sense in using the Event system in the first place... Just create a
> > globally accessible flag and set it to true when you want to signal the
> > threads (or false if you don't want to use the negation "while not
> > flagged: do next processing step")
>
> >         Event is optimized for the case wherein threads can WAIT (block) on
> > the Event object.
> > --
> >         Wulfraed        Dennis Lee Bieber               KD6MOG
> >         wlfr...@ix.netcom.com             wulfr...@bestiaria.com
> >                 HTTP://wlfraed.home.netcom.com/
> >         (Bestiaria Support Staff:               web-a...@bestiaria.com)
> >                 HTTP://www.bestiaria.com/
>
> Well it turns out my problem was with queues not with threads.  I had
> a self.die prop in my thread object that defaults to FALSE and that I
> set to true when i wanted the thread to die.  then my loop would be
> while not die:  It seemed pretty simple so I didn't know why it was
> failing.  What I didn't know, because I'm quite new to python, is that
> queue.get was blocking.  So my producer thread why dying immediately
> but my worker threads were all blocking on their queue.gets.  So they
> were never falling off the loop.  I changed it to queue.get_nowait()
> and added a queue.empty exception and everything worked as expected.
>
> So I thought I knew what was going on and that I was having a really
> esoteric problem when i was actually having a pretty boring problem I
> didn't recognize.
>
> Thanks everybody for the help!>

I've gone through that also, when I started with python threads :-)
Be aware that using get_nowait may lead to your thread using too much
CPU in checking a queue often empty. I tend to use  Queue.get with a
timeout, smaller enough to keep the thread responsive but large enough
not
to waste CPU in too-frequent checks.

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


Re: py2exe problem

2009-04-06 Thread Wolfgang Forstmeier

On 03.04.2009 15:58, Dave Angel wrote:



Wolfgang Forstmeier wrote:






Ok, but do you really use idlelib for something? Or it's just some
random code you found somewhere and drop into your application?


Ah yes, I really use this. I create some message boxes for a little
GUI application that controls some other program with COM.
Running my app without py2exe, just with python, there is no warning
at all. This comes in with py2exe first.

Here some piece of code that I use for tkMessageBox.

from idlelib.OutputWindow import tkMessageBox

...
# Define about message box
def about(self):
tkMessageBox.showinfo("About", "My little about text box.")
# --
...

There is some more GUI programming arround that def in my class, but
that uses only Tkinter, should not be interesting for that error.



So why not use tkMessageBox directly, and skip Idle's namespace wrapping?

import tkMessageBox

tkMessageBox.showinfo("About", "My little about text box.")



Hey Dave, thanks a lot, that did the job, because I really does not use 
Idle in any other place.


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


decorators don't play nice with nose?

2009-04-06 Thread hyperboreean
Hi, I am trying to test the business part of a web service. For this I 
am using unittest & nose.
I wrote a decorator that should handle the xml test file retrieval, but 
it seems I can't get it working with nose.

Here's the code:


* MyApp.py -- base test class *

import os
import unittest

from MyApp.Core import XmlParser


__all__ = ['MyAppTest', 'setup']


PATH = os.path.dirname(__file__) or ''


class setup(object):
   """Decorator to ease the use of xml files in MyApp tests.

   The way it works it that it decorates a test method which has a first
   default parameter called 'parser' and it overwrites this parameter value
   with a XmlParser instance.

   The xml file should be located under:
   data/testedBusinessRequest/testMethodName.xml
   """
   def __init__(self, testedBusinessRequest = ''):
   self.testedBusinessRequest =\
   testedBusinessRequest.lower()


   def _getXmlParser(self, xml):
   documentElement = XmlParser.parseXmlStream(xml)
   parser = XmlParser.getParser(documentElement)
   return parser


   def __call__(self, method):

   # TODO: error handling here
   methodName = method.func_code.co_name
   methodName = methodName.split('_')[1]

   xmlFolder = self.testedBusinessRequest
   xmlFile = '%s.xml' % methodName

   path = os.path.join(PATH, 'data',
   xmlFolder, xmlFile)

   f = open(path)
   xml = f.read()
   f.close()
   method.func_defaults = (self._getXmlParser(xml),)
   return method


class MyAppTest(unittest.TestCase):

   def setUp(self):
   self.database = Database()

   def tearDown(self):
   pass


* test_Login.py - test a business request *
from MyAppTest import MyAppTest, setup

from MyApp import Login


class TestLogin(MyAppTest):
   testedBusinessRequest = 'Login'

   @setup(testedBusinessRequest)
   def test_validParameters(self, parser = None):
   response = Login(self.database, parser).run()
   return True



Ok, so the decorator setup should fill the parser parameter with a 
XmlParser object. This works well if I add a __main__ and use unittest 
to run the tests. But if I use nose, I get the following error:


*TypeError: unbound method __call__() must be called with setup instance 
as first argument (got module instance instead)*


Any advices?
Thanks!

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


Re: Need advise about an application

2009-04-06 Thread Hendrik van Rooyen
"azrael"  wrote:


> I guess that this is not an option because of the case that the
> calculation of the needed statistics takes not always the same time
> nad I am afraid tht using sleep() would after a couple of time periods
> skip a meassurement.

If I understand correctly what you are attempting,
I would use three threads:

one to do the serial stuff - it is responsible for getting
a slug of data in, checking for errors, doing protocol
stuff like re tries, etc.

One to store the slug in the database, making sure it
is written.

One to display the results.

Glue them together with queues.

If ever you need more performance, make the
threads processes, and replace the queues by pipes.

- Hendrik


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


Re: Python2.4 and HTTPS

2009-04-06 Thread Jeremiah Dodds
On Sun, Apr 5, 2009 at 8:17 AM, Good Z  wrote:

> Dear all,
> I am using Python 2.4.3 for my project. We need to use HTTPS with
> python2.4.3 unfortunately it seems httplib is not working fine for me. Below
> is small code that works well with Python2.6.1 but not with Python2.4.3.
> Unfortunately its not possible for me to move away from Python 2.4.3. Would
> request if anyone has any idea how to make this programs work in
> Python2.4.3.
>
> import httplib
> conn1 = httplib.HTTPSConnection(WEBSITE_ADDRESS)
> conn1.putrequest('GET', RELATIVE_ADDR)
> conn1.putheader('Connection', 'close')
> conn1.endheaders()
>
> r1 = conn1.getresponse()
>
> data1 = r1.read()
> print data1
> conn1.close()
>
> One executing the above program, it crash while reading the response and
> gives following error:
> Traceback (most recent call last):
>   File "", line 1, in 
> r1 = conn1.getresponse()
>   File "C:\Python25\lib\httplib.py", line 928, in getresponse
> response.begin()
>   File "C:\Python25\lib\httplib.py", line 385, in begin
> version, status, reason = self._read_status()
>   File "C:\Python25\lib\httplib.py", line 349, in _read_status
> raise BadStatusLine(line)
> BadStatusLine
>
> Any help would be appreciated.
>
> Best Regards,
> Mike.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

You may be running into http://bugs.python.org/issue5007  - if you could
post the url of the site you're trying to open, I can test to see if it
works in 2.5/2.6 (It won't if you're running into that bug).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Example for readline module usage?

2009-04-06 Thread Peter Otten
Grant Edwards wrote:

> [I swear I've asked this question before, but Google can't find
> it.]

My Google is better than yours then:

http://mail.python.org/pipermail/python-list/2008-July/669582.html

Peter


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


Re: logging - string formating problems

2009-04-06 Thread Werner F. Bruhin

Werner F. Bruhin wrote:

I see the following exception with a string formating problem.

TypeError: not all arguments converted during string formatting
Traceback (most recent call last):
 File "/usr/lib/python2.5/logging/__init__.py", line 744, in emit
   msg = self.format(record)
 File "/usr/lib/python2.5/logging/__init__.py", line 630, in format
   return fmt.format(record)
 File "/usr/lib/python2.5/logging/__init__.py", line 418, in format
   record.message = record.getMessage()
 File "/usr/lib/python2.5/logging/__init__.py", line 288, in getMessage
   msg = msg % self.args

The exception does not give any information on where the problem is 
coming from.


I am using Python 2.5.4 but I see that in 2.6 the code is still the same.

Any chance that getMessage could catch this exception and provide 
better debugging information (i.e. content of msg and self.args).

I understand that my problem is that the arguments don't match the
format.  But currently the traceback is not of any help in figuring out
where this in my code this is.

So, I suggest that line 288 in getMessage is changed from:

   msg = msg % self.args

To something along these lines:
   if self.args:
   try:
   msg = msg % self.args
   except:
   print 'msg: %s' % msg
   print 'args: %s' % self.args
   traceback.print_exception(ei[0], ei[1], ei[2])
   return msg

Werner


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


set python default encoding

2009-04-06 Thread reetesh nigam
Hi All,
I am unable to set the python default encoding.
i used the following proccess to set the python encoding

import sys
reload(sys)
sys.setdefaultencoding('latin-1')

but it is giving me the same error :

  args = ('utf8', "MEDICINE '\xc4 ", 10, 12, 'invalid data',
>)
  encoding = 'utf8'
  end = 12
  message = ''
  object = "MEDICINE '\xc4 "
  reason = 'invalid data'
  start = 10


Please tell me how to solve this problem.

Thanks and Regards

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


Re: How to go about. On read/write locks

2009-04-06 Thread Emanuele D'Arrigo
On Apr 6, 7:49 am, "Diez B. Roggisch"  wrote:
> The CPython-specific answer is that the GIL takes care of that for you
> right now anyway. So unless you plan for a distant future where some
> kind of swallows fly around that don't have a GIL, you are safe to
> simply read and write in threads without any locking whatsoever.

Diez, thanks for your reply. I didn't know what the GIL is. I did some
research finding an interesting article that did clarify many multi-
threading related concepts and issues:

http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/

Python's approach with the GIL is both reasonable and disappointing.
Reasonable because I understand how it can make things easier for its
internals. Disappointing because it means that standard python cannot
take advantage of the parallelism that can more and more often be
afforded by today's computers. I.e. I found only recently, almost by
chance, that my wife's laptop has not one but two processors, even
though it isn't a particularly high-end computer. I now understand
that OS-level threading does use them both, but I understand that the
GIL effectively prevents parallel operations. (Am I understanding
correctly?)

I do not completely understand your statement in the context of my
original example though, the shared dictionary. As the GIL is released
every X bytecode operations surely it can happen that as the
dictionary is iterated through, i.e. in a for/in loop, a different
thread might change it, with potentially catastrophic consequences.
The GIL wouldn't be able to prevent this, wouldn't it?

Manu







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


Re: How to go about. On read/write locks

2009-04-06 Thread Piet van Oostrum
> "Emanuele D'Arrigo"  (ED) wrote:

>ED> Hi everybody,
>ED> I'm having a threading-related design issue and I suspect it has a
>ED> name that I just don't know.  Here's a description.

>ED> Let's assume a resource (i.e. a dictionary) that needs to be accessed
>ED> by multiple threads. A simple lock will do the job but in some
>ED> circumstances it will create an unnecessary bottleneck. I.e. let's
>ED> assume that most threads only need to have a -read- access to the
>ED> resource, while only few threads actually change the dictionary.
>ED> Ideally, the reading threads should not block each other. However, as
>ED> soon as a threads intends to change the dictionary it should let all
>ED> the reading threads finish, lock the access to the resource, change
>ED> it, and then release the lock.

>ED> I don't think it'd be difficult to implement but I'm wondering if
>ED> something in this direction has been done already, if it has a name or
>ED> if it's even well known design pattern.

>ED> Anybody can shed some light?

This is a classical synchronization problem with a classical solution: 
You treat the readers as a group, and the writers individually. So you
have a write lock that each writer has to acquire and release, but it is
acquired only by the first reader and released by the last one.
Therefore you need a counter of the number of readers, and manipulations
of this counter must be protected by another lock.

#
from threading import Lock

mutex = Lock()
writelock = Lock()
numreaders = 0
#
# Reader code:

mutex.acquire()
numreaders += 1
if numreaders == 1:
writelock.acquire()
mutex.release()

## critical section for reader

mutex.acquire()
numreaders -= 1
if numreaders == 0:
writelock.release()
mutex.release()
#
# Writer code:

writelock.acquire()

## critical section for writer

writer.release
#

Notes:
1. From Python 2.6 on you can use the with statement, which makes it
   more robust against exceptions:
with mutex:
numreaders += 1
if numreaders == 1:
writelock.acquire()
etc.
In Python 2.5 you can also use this if you use:
from __future__ import with_statement

2. The code above can cause starvation for writers if there are many
   readers (or if new readers come in before all other readers have
   finished. You need at least one more lock and probably a writer
   counter to solve this.

3. See also http://code.activestate.com/recipes/465156/

4. The code has not been tested, not even for syntax errors.

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


Re: logging - string formating problems

2009-04-06 Thread Steven D'Aprano
On Mon, 06 Apr 2009 11:11:37 +0200, Werner F. Bruhin wrote:

> Werner F. Bruhin wrote:
>> I see the following exception with a string formating problem.
>>
>> TypeError: not all arguments converted during string formatting
>> Traceback (most recent call last):
>>  File "/usr/lib/python2.5/logging/__init__.py", line 744, in emit
>>msg = self.format(record)
>>  File "/usr/lib/python2.5/logging/__init__.py", line 630, in format
>>return fmt.format(record)
>>  File "/usr/lib/python2.5/logging/__init__.py", line 418, in format
>>record.message = record.getMessage()
>>  File "/usr/lib/python2.5/logging/__init__.py", line 288, in getMessage
>>msg = msg % self.args
>>
>> The exception does not give any information on where the problem is
>> coming from.


I'm pretty sure it does. Are you sure that you are including the *entire* 
traceback? Because judging from the four entries given, it looks like the 
error is originating from logging.__init__ itself, and not from your 
application itself. Weird huh? 

Oh wait... is this error occurring when you exit your application? 
Possibly this is the logging shutdown handler failing? I'm kind of 
whistling in the dark here.

Perhaps this recipe can help you get more information from the tracebacks:

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

Good luck, and let us know what the problem was.



>> I am using Python 2.5.4 but I see that in 2.6 the code is still the
>> same.
>>
>> Any chance that getMessage could catch this exception and provide
>> better debugging information (i.e. content of msg and self.args).

*shrug* 

You could always request a feature enhancement on the Python bug list.


> I understand that my problem is that the arguments don't match the
> format.  But currently the traceback is not of any help in figuring out
> where this in my code this is.
> 
> So, I suggest that line 288 in getMessage is changed from:
> 
> msg = msg % self.args
> 
> To something along these lines:
> if self.args:
> try:
> msg = msg % self.args
> except:
> print 'msg: %s' % msg
> print 'args: %s' % self.args
> traceback.print_exception(ei[0], ei[1], ei[2])
> return msg


Patches will increase the likelihood of the enhancement being accepted, 
but I suspect this is too *specific* to be accepted.



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


Re: How to free /destroy object created by PyTuple_New

2009-04-06 Thread Hrvoje Niksic
grbgooglefan  writes:

> Regarding PyTuple_New, when I pass this tuple with variable values
> set to some evaluation function like PyObject_CallObject, do I need
> to increment reference for this tuple & then decrement again after
> the call returns?

You don't.  It is assumed that you already own the reference to the
tuple, so you can freely pass it to functions.  When you're done with
the tuple (such as when you're about to leave the C scope that holds
the reference to it), you're suppose to decref it.  If the function
you call stores it somewhere, that function is responsible for
incrementing its reference count.

The exception to the above are functions documented to "steal" the
reference count of their arguments, such as PyList_SetItem, but those
are generally quite rare and always clearly documented.

Maybe you should post a question to the capi-sig list explaining what
you're trying to do; perhaps there's a better way to do it.  For
example, maybe you don't need to create a tuple and then call
PyObject_CallObject, but simply use PyObject_CallFunction, which will
create the tuple for you.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Painful?: Using the ast module for metaprogramming

2009-04-06 Thread Kay Schluehr
> -It would be nice if decorators were passed a function's AST instead
> of a function object. As it is I have to use inspect.getsource to
> retrieve the source for the function in question, and then use
> ast.parse, which is a bit inefficient because the cpython parser has
> to already have done this once before.

It doesn't matter that much though because the Python parser is very
efficient and the decorator is applied only once.

The PyGPU project used this approach when I remember it correctly:

http://www.cs.lth.se/home/Calle_Lejdfors/pygpu/

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


group several methods under a attribute

2009-04-06 Thread jelle
Hi,

I'm working on a pretty large class and I'd like to group several
methods under a attribute.
Its not convenient to chop up the class in several smaller classes,
nor would mixins really solve the issue.
So, what is a pythonic way of grouping several methods under a
attribute?

Many thanks in advance,

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


Re: HTTP Authentication

2009-04-06 Thread Gerhard Häring
Lakshman wrote:
> Whats is the python urllib2 equivallent of
> 
> curl -u username:password status="abcd" http://example.com/update.json
> 
> I did this:
> 
> handle = urllib2.Request(url)
> authheader =  "Basic %s" % base64.encodestring('%s:%s' % (username,
> password))
> handle.add_header("Authorization", authheader)
> 
> Is there a better / simpler way?

Better? Yes.
Simpler? No.

Actually, the proper way using the urllib2 API is more code.

When I need it some time ago, I googled and used this recipe:

http://www.voidspace.org.uk/python/articles/urllib2.shtml#id6


# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()

# Add the username and password.
# If we knew the realm, we could use it instead of ``None``.
top_level_url = "http://example.com/foo/";
password_mgr.add_password(None, top_level_url, username, password)

handler = urllib2.HTTPBasicAuthHandler(password_mgr)

# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(handler)

# use the opener to fetch a URL
opener.open(a_url)

# Install the opener.
# Now all calls to urllib2.urlopen use our opener.
urllib2.install_opener(opener)

-- Gerhard

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


HTTP Authentication

2009-04-06 Thread Lakshman
Whats is the python urllib2 equivallent of

curl -u username:password status="abcd" http://example.com/update.json

I did this:

handle = urllib2.Request(url)
authheader =  "Basic %s" % base64.encodestring('%s:%s' % (username,
password))
handle.add_header("Authorization", authheader)

Is there a better / simpler way?
--
http://mail.python.org/mailman/listinfo/python-list


C API String Parsing/Returning

2009-04-06 Thread k3xji
Hi all,

This might be a newbie question. I am trying to implement a simple
string decoder/encoder algorithm. Just suppose I am substrcating some
values from the string passed as a parameter to the function and I
want the function to return encoded/decoded version of the string.

Here is the call:
ss= esauth.penc('s')
st = esauth.pdec(ss)

static PyObject *
pdec(PyObject *self, PyObject *args)
{
unsigned char *s= NULL;

unsigned int v,len,i = 0;

if (!PyArg_ParseTuple(args, "s", &s))
return NULL;
if (!s)
return NULL;

len = strlen(s);

for(i=0;i 10)
s[i] = s[i] - 10;
}

return Py_BuildValue("s",s);
}


This is returning the original string. I mean the parameter is changed
but the Py_BuildValue is returning the original string passed in as
param.

 have dealt with another nmore complex extension and because of the
same string handling problems, I just stop implementing it. Can
somebody please briefly explain the gotchas in Python's string
handling and returning values, cause I am having real trouble with
them.

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


Re: logging - string formating problems

2009-04-06 Thread Peter Otten
Werner F. Bruhin wrote:

> Werner F. Bruhin wrote:
>> I see the following exception with a string formating problem.
>>
>> TypeError: not all arguments converted during string formatting
>> Traceback (most recent call last):
>>  File "/usr/lib/python2.5/logging/__init__.py", line 744, in emit
>>msg = self.format(record)
>>  File "/usr/lib/python2.5/logging/__init__.py", line 630, in format
>>return fmt.format(record)
>>  File "/usr/lib/python2.5/logging/__init__.py", line 418, in format
>>record.message = record.getMessage()
>>  File "/usr/lib/python2.5/logging/__init__.py", line 288, in getMessage
>>msg = msg % self.args
>>
>> The exception does not give any information on where the problem is
>> coming from.
>>
>> I am using Python 2.5.4 but I see that in 2.6 the code is still the same.
>>
>> Any chance that getMessage could catch this exception and provide
>> better debugging information (i.e. content of msg and self.args).
> I understand that my problem is that the arguments don't match the
> format.  But currently the traceback is not of any help in figuring out
> where this in my code this is.
> 
> So, I suggest that line 288 in getMessage is changed from:
> 
> msg = msg % self.args
> 
> To something along these lines:
> if self.args:
> try:
> msg = msg % self.args
> except:
> print 'msg: %s' % msg
> print 'args: %s' % self.args
> traceback.print_exception(ei[0], ei[1], ei[2])
> return msg

I would be more interested in the origin of the problem in my code. For that
you need the complete traceback. You might be able to find it by
monkey-patching the Handler.handlError() method, e. g.:

import logging

def handleError(self, record):
raise

logging.Handler.handleError = handleError

logging.critical("", 42)

Of course you'd only do that while you are debugging the app.

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


Re: C API String Parsing/Returning

2009-04-06 Thread k3xji
Sorry, Here is the correct output:
>>> ss= esauth.penc('s')
>>> print ss
╣
>>> esauth.pdec(ss)
'\xb9'
>>> print ss
s --> Works fine!!!
>>> ss= esauth.penc('s')
>>> print ss
s
>>> ss = esauth.pdec(ss)
>>> print ss
╣  --> how did this happen if the param and return values are same? I
cannot understand this. Something has todo with ref counts but I don't
understand the problem.
>>>



On Apr 6, 3:13 pm, k3xji  wrote:
> Hi all,
>
> This might be a newbie question. I am trying to implement a simple
> string decoder/encoder algorithm. Just suppose I am substrcating some
> values from the string passed as a parameter to the function and I
> want the function to return encoded/decoded version of the string.
>
> Here is the call:
> ss= esauth.penc('s')
> st = esauth.pdec(ss)
>
> static PyObject *
> pdec(PyObject *self, PyObject *args)
> {
>         unsigned char *s= NULL;
>
>         unsigned int v,len,i = 0;
>
>         if (!PyArg_ParseTuple(args, "s", &s))
>         return NULL;
>         if (!s)
>                 return NULL;
>
>         len = strlen(s);
>
>         for(i=0;i                 if (s[i] > 10)
>                     s[i] = s[i] - 10;
>         }
>
>         return Py_BuildValue("s",s);
>
> }
>
> This is returning the original string. I mean the parameter is changed
> but the Py_BuildValue is returning the original string passed in as
> param.
>
>  have dealt with another nmore complex extension and because of the
> same string handling problems, I just stop implementing it. Can
> somebody please briefly explain the gotchas in Python's string
> handling and returning values, cause I am having real trouble with
> them.
>
> Thanks,

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


Re: Killing threads

2009-04-06 Thread ericwoodworth
On Apr 6, 3:45 am, bieff...@gmail.com wrote:
> On 6 Apr, 05:25, ericwoodwo...@gmail.com wrote:
>
>
>
> > On Apr 5, 11:07 pm, Dennis Lee Bieber  wrote:
>
> > > On Sun, 5 Apr 2009 17:27:15 -0700 (PDT), imageguy
> > >  declaimed the following in
> > > gmane.comp.python.general:
>
> > > > In threading.Event python 2.5 docs say;
> > > > "This is one of the simplest mechanisms for communication between
> > > > threads: one thread signals an event and other threads wait for it. "
>
> > > > Again, I have limited experience, however, in my reading of the
> > > > threading manual and review examples, Events were specifically design
> > > > to be a thread safe way to communicate a 'state' to running threads ?
> > > > In the OP's example 'do stuff' was open to wide interpretation,
> > > > however, if within the thread's main 'while' loop the tread checks to
> > > > see if the 'keepgoing' Event.isSet(), in what scenario would this
> > > > create deadlock ?
>
> > >         If you are going to perform a CPU intensive polling loop, there is
> > > no sense in using the Event system in the first place... Just create a
> > > globally accessible flag and set it to true when you want to signal the
> > > threads (or false if you don't want to use the negation "while not
> > > flagged: do next processing step")
>
> > >         Event is optimized for the case wherein threads can WAIT (block) 
> > > on
> > > the Event object.
> > > --
> > >         Wulfraed        Dennis Lee Bieber               KD6MOG
> > >         wlfr...@ix.netcom.com             wulfr...@bestiaria.com
> > >                 HTTP://wlfraed.home.netcom.com/
> > >         (Bestiaria Support Staff:               web-a...@bestiaria.com)
> > >                 HTTP://www.bestiaria.com/
>
> > Well it turns out my problem was with queues not with threads.  I had
> > a self.die prop in my thread object that defaults to FALSE and that I
> > set to true when i wanted the thread to die.  then my loop would be
> > while not die:  It seemed pretty simple so I didn't know why it was
> > failing.  What I didn't know, because I'm quite new to python, is that
> > queue.get was blocking.  So my producer thread why dying immediately
> > but my worker threads were all blocking on their queue.gets.  So they
> > were never falling off the loop.  I changed it to queue.get_nowait()
> > and added a queue.empty exception and everything worked as expected.
>
> > So I thought I knew what was going on and that I was having a really
> > esoteric problem when i was actually having a pretty boring problem I
> > didn't recognize.
>
> > Thanks everybody for the help!>
>
> I've gone through that also, when I started with python threads :-)
> Be aware that using get_nowait may lead to your thread using too much
> CPU in checking a queue often empty. I tend to use  Queue.get with a
> timeout, smaller enough to keep the thread responsive but large enough
> not
> to waste CPU in too-frequent checks.
>
> Ciao
> -
> FB

Ok thanks - good to now.  I'm trying to throttle it with a 1/2 sec
sleep statement in the loop but I might just have the "main" loop toss
some stuff on that queue as another solution.  I'm still kicking it
around
--
http://mail.python.org/mailman/listinfo/python-list


Re: group several methods under a attribute

2009-04-06 Thread Gerhard Häring
jelle wrote:
> Hi,
> 
> I'm working on a pretty large class 

Can you describe what its purpose is?

> and I'd like to group several methods under a attribute.

That doesn't work in Python without bending the Python.

> Its not convenient to chop up the class in several smaller classes,

But that's almost certainly the right thing to do.

> nor would mixins really solve the issue.
> So, what is a pythonic way of grouping several methods under a
> attribute?

Whatever it is, you should find a better way instead of cramming
everything into a single class. That smells of the God Object
antipattern (http://en.wikipedia.org/wiki/God_object).

-- Gerhard

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


Re: C API String Parsing/Returning

2009-04-06 Thread Gerhard Häring
k3xji wrote:
> Hi all,
> 
> This might be a newbie question. I am trying to implement a simple
> string decoder/encoder algorithm. Just suppose I am substrcating some
> values from the string passed as a parameter to the function and I
> want the function to return encoded/decoded version of the string.
> 
> Here is the call:
> ss= esauth.penc('s')
> st = esauth.pdec(ss)
> 
> static PyObject *
> pdec(PyObject *self, PyObject *args)
> {
>   unsigned char *s= NULL;
> 
>   unsigned int v,len,i = 0;
> 
>   if (!PyArg_ParseTuple(args, "s", &s))
> return NULL;

>   if (!s)
>   return NULL;

These two lines are superfluous. s now points to the contents of the
Python string (which must not contain any 0 characters, else a TypeError
is raised instead). Python strings are immutable, so you should *not
modify this C string*.

>   len = strlen(s);
> 
>   for(i=0;i if (s[i] > 10)
>   s[i] = s[i] - 10;
>   }
> 
>   return Py_BuildValue("s",s);
> }
> 
> 
> This is returning the original string. I mean the parameter is changed
> but the Py_BuildValue is returning the original string passed in as
> param. [...]

Yes, that's because you're returning a Python string from the string
passed in ;-)

You should do something else instead:

char* buf = strdup(s);
if (!buf) {
PyErr_SetString(PyExc_MemoryError, "Out of memory: strdup failed");
return NULL;
}

/* TODO: your string manipulation */

return PyString_FromString(buf); /* return Py_BuildValue("s", buf); */

If you want to cope with Python strings that may contain 0 bytes, parse
them with "s#" instead. This should normally be better because you avoid
the strlen() this way.

HTH

-- Gerhard

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


Re: logging - string formating problems

2009-04-06 Thread Werner F. Bruhin
I am fully aware that the problem is in my code, however as getMessage 
in logging.__init__.py does not catch the exception it is pretty 
difficult to find the problem without manually inspecting any 
logging.something statements.


My hack of logging.py is really a hack and I know that this can not be 
used a patch but I hope that someone with more know how then me knows 
how to improve the situation.


BTW, using the below hack was enough for me to mind the bad code in my 
stuff, but it shouldn't have been this difficult.


Traceback (most recent call last):
  File "C:\Python25\lib\logging\__init__.py", line 750, in emit
msg = self.format(record)
  File "C:\Python25\lib\logging\__init__.py", line 636, in format
return fmt.format(record)
  File "C:\Python25\lib\logging\__init__.py", line 424, in format
record.message = record.getMessage()
  File "C:\Python25\lib\logging\__init__.py", line 288, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting


Then I hack logging.py and change line 288 in getMessage from:
msg = msg % self.args

to:
try:
msg = msg % self.args
except:
print 'msg: %s' % msg
print 'args: %s' % self.args
print traceback.format_exc()

I get the following which helps a lot more in finding were in my code I 
have the problem:


msg: ping_min: 1
args: replace
Traceback (most recent call last):
  File "C:\Python25\lib\logging\__init__.py", line 290, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting


As mentioned above, I know the above code is not good enough at all, but 
I hope that maybe Vinay Sajip or someone else with more know how then I 
have can come up with a patch which will make this type of situation easier.


Werner

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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-06 Thread Chris Withers



Martin v. Löwis wrote:

Chris Withers wrote:

Martin v. Löwis wrote:

I propose the following PEP for inclusion to Python 3.1.
Please comment.

Would this support the following case:

I have a package called mortar, which defines useful stuff:

from mortar import content, ...

I now want to distribute large optional chunks separately, but ideally
so that the following will will work:

from mortar.rbd import ...
from mortar.zodb import ...
from mortar.wsgi import ...

Does the PEP support this? 


That's the primary purpose of the PEP. 


Are you sure?

Does the pep really allow for:

from mortar import content
from mortar.rdb import something

...where 'content' is a function defined in mortar/__init__.py and 
'something' is a function defined in mortar/rdb/__init__.py *and* the 
following are separate distributions on PyPI:


- mortar
- mortar.rdb

...where 'mortar' does not contain 'mortar.rdb'.

> You can do this today already

(see the zope package,


No, they have nothing but a (functionally) empty __init__.py in the zope 
package.


cheers,

Chris

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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-06 Thread Jesse Noller
On Thu, Apr 2, 2009 at 4:33 PM, M.-A. Lemburg  wrote:
> On 2009-04-02 17:32, Martin v. Löwis wrote:
>> I propose the following PEP for inclusion to Python 3.1.
>
> Thanks for picking this up.
>
> I'd like to extend the proposal to Python 2.7 and later.
>

-1 to adding it to the 2.x series. There was much discussion around
adding features to 2.x *and* 3.0, and the consensus seemed to *not*
add new features to 2.x and use those new features as carrots to help
lead people into 3.0.

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


Re: How to go about. On read/write locks

2009-04-06 Thread Emanuele D'Arrigo
On Apr 6, 12:44 pm, Piet van Oostrum  wrote:
> 3. See also http://code.activestate.com/recipes/465156/

Thank you for the useful suggestions Piet. In particular I just had a
look at the SharedLock class provided through the link above and it
seems to fit the bill quite nicely. I'll give it a go!

Thank you again!

Manu


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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-06 Thread Barry Warsaw

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On Apr 6, 2009, at 9:21 AM, Jesse Noller wrote:


On Thu, Apr 2, 2009 at 4:33 PM, M.-A. Lemburg  wrote:

On 2009-04-02 17:32, Martin v. Löwis wrote:

I propose the following PEP for inclusion to Python 3.1.


Thanks for picking this up.

I'd like to extend the proposal to Python 2.7 and later.



-1 to adding it to the 2.x series. There was much discussion around
adding features to 2.x *and* 3.0, and the consensus seemed to *not*
add new features to 2.x and use those new features as carrots to help
lead people into 3.0.


Actually, isn't the policy just that nothing can go into 2.7 that  
isn't backported from 3.1?  Whether the actual backport happens or not  
is up to the developer though.  OTOH, we talked about a lot of things  
and my recollection is probably fuzzy.


Barry

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (Darwin)

iQCVAwUBSdoDAXEjvBPtnXfVAQIrPgQAse7BXQfPYHJJ/g3HNEtc0UmZZ9MCNtGc
sIoZ2EHRVz+pylZT9fmSmorJdIdFvAj7E43tKsV2bQpo/am9XlL10SMn3k0KLxnF
vNCi39nB1B7Uktbnrlpnfo4u93suuEqYexEwrkDhJuTMeye0Cxg0os5aysryuPza
mKr5jsqkV5c=
=Y9iP
-END PGP SIGNATURE-
--
http://mail.python.org/mailman/listinfo/python-list


Re: logging - string formating problems

2009-04-06 Thread MRAB

Werner F. Bruhin wrote:
I am fully aware that the problem is in my code, however as getMessage 
in logging.__init__.py does not catch the exception it is pretty 
difficult to find the problem without manually inspecting any 
logging.something statements.


My hack of logging.py is really a hack and I know that this can not be 
used a patch but I hope that someone with more know how then me knows 
how to improve the situation.


BTW, using the below hack was enough for me to mind the bad code in my 
stuff, but it shouldn't have been this difficult.


Traceback (most recent call last):
  File "C:\Python25\lib\logging\__init__.py", line 750, in emit
msg = self.format(record)
  File "C:\Python25\lib\logging\__init__.py", line 636, in format
return fmt.format(record)
  File "C:\Python25\lib\logging\__init__.py", line 424, in format
record.message = record.getMessage()
  File "C:\Python25\lib\logging\__init__.py", line 288, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting


Then I hack logging.py and change line 288 in getMessage from:
msg = msg % self.args

to:
try:
msg = msg % self.args
except:
print 'msg: %s' % msg
print 'args: %s' % self.args
print traceback.format_exc()

I get the following which helps a lot more in finding were in my code I 
have the problem:


msg: ping_min: 1
args: replace
Traceback (most recent call last):
  File "C:\Python25\lib\logging\__init__.py", line 290, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting


As mentioned above, I know the above code is not good enough at all, but 
I hope that maybe Vinay Sajip or someone else with more know how then I 
have can come up with a patch which will make this type of situation 
easier.



It looks like what you need is for the module to record the exception in
the log you're generating as a "logging error" or some such. BTW, you 
should catch only TypeError and not use an empty "except":


try:
msg = msg % self.args
except TypeError:
msg = 'Logging error: msg is %s, args is %s' % (repr(msg), 
repr(self.args))

msg += traceback.format_exc()

(needs tidying up, of course!)
--
http://mail.python.org/mailman/listinfo/python-list


Newbie: Development plaform

2009-04-06 Thread Hermann Wehrmeyer
Hi, I am looking for an old school friend of mine, Demos Economacos. Are
you perhaps the Demos who completed schooling 1979 at Kroonstad SA.

 

Groete/Greetings

 

Hermann Wehrmeyer

 

Tel: 012 342 3710

Fax: 012 342 3775

 

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


Re: C API String Parsing/Returning

2009-04-06 Thread Christian Heimes
Gerhard Häring wrote:
> char* buf = strdup(s);
> if (!buf) {
> PyErr_SetString(PyExc_MemoryError, "Out of memory: strdup failed");
> return NULL;
> }
> 
> /* TODO: your string manipulation */

Don't forget to free(buf). ;)

Christian

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


speed of string chunks file parsing

2009-04-06 Thread Hyunchul Kim

Hi, all

I have a simple script.
Can you improve algorithm of following 10 line script, with a view point 
of speed ?

Following script do exactly what I want but I want to improve the speed.

This parse a file and accumulate lines till a line match a given regular 
expression.
Then, when a line match a given regular expression, this function yield 
lines before the matched lines.



import re
resultlist = []
cp_regularexpression = re.compile('^a complex regular expression here$)
for line in file(inputfile):
   if cp_regularexpression.match(line):
   if resultlist != []:
   yield resultlist
   resultlist = []
   resultlist.append(line)
yield resultlist


Thank you in advance,

Hyunchul


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


Re: speed of string chunks file parsing

2009-04-06 Thread MRAB

Hyunchul Kim wrote:

Hi, all

I have a simple script.
Can you improve algorithm of following 10 line script, with a view point 
of speed ?

Following script do exactly what I want but I want to improve the speed.

This parse a file and accumulate lines till a line match a given regular 
expression.
Then, when a line match a given regular expression, this function yield 
lines before the matched lines.



import re
resultlist = []
cp_regularexpression = re.compile('^a complex regular expression here$)
for line in file(inputfile):
   if cp_regularexpression.match(line):
   if resultlist != []:
   yield resultlist
   resultlist = []
   resultlist.append(line)
yield resultlist


Thank you in advance,


It looks OK to me.

Of course, it could be the regular expression that's the slowest part.
Have you tried timing it?
--
http://mail.python.org/mailman/listinfo/python-list


Re: decorators don't play nice with nose?

2009-04-06 Thread J Kenneth King
hyperboreean  writes:

> From: hyperboreean 
> Subject: decorators don't play nice with nose?
> Newsgroups: comp.lang.python
> To: python-list@python.org
> Date: Mon, 06 Apr 2009 11:01:04 +0300
>
> Hi, I am trying to test the business part of a web service. For this I
> am using unittest & nose.
> I wrote a decorator that should handle the xml test file retrieval,
> but it seems I can't get it working with nose.
> Here's the code:
>
>
> * MyApp.py -- base test class *
>
> import os
> import unittest
>
> from MyApp.Core import XmlParser
>
>
> __all__ = ['MyAppTest', 'setup']
>
>
> PATH = os.path.dirname(__file__) or ''
>
>
> class setup(object):
>"""Decorator to ease the use of xml files in MyApp tests.
>
>The way it works it that it decorates a test method which has a first
>default parameter called 'parser' and it overwrites this parameter value
>with a XmlParser instance.
>
>The xml file should be located under:
>data/testedBusinessRequest/testMethodName.xml
>"""
>def __init__(self, testedBusinessRequest = ''):
>self.testedBusinessRequest =\
>testedBusinessRequest.lower()
>
>
>def _getXmlParser(self, xml):
>documentElement = XmlParser.parseXmlStream(xml)
>parser = XmlParser.getParser(documentElement)
>return parser
>
>
>def __call__(self, method):
>
># TODO: error handling here
>methodName = method.func_code.co_name
>methodName = methodName.split('_')[1]
>
>xmlFolder = self.testedBusinessRequest
>xmlFile = '%s.xml' % methodName
>
>path = os.path.join(PATH, 'data',
>xmlFolder, xmlFile)
>
>f = open(path)
>xml = f.read()
>f.close()
>method.func_defaults = (self._getXmlParser(xml),)
>return method
>
>
> class MyAppTest(unittest.TestCase):
>
>def setUp(self):
>self.database = Database()
>
>def tearDown(self):
>pass
>
>
> * test_Login.py - test a business request *
> from MyAppTest import MyAppTest, setup
>
> from MyApp import Login
>
>
> class TestLogin(MyAppTest):
>testedBusinessRequest = 'Login'
>
>@setup(testedBusinessRequest)
>def test_validParameters(self, parser = None):

FWIW, nose and unittest both provide methods for setting up and
tearing down tests. You should probably look at those first before
rolling your own. At the very least it will give you an idea of how
yours should work.

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


Re: Killing threads

2009-04-06 Thread Aahz
In article ,
  wrote:
>
>I know that killing threads is hard in any language (I'm facing now
>the issue in a C++ program I'm writing at work), expecially doing in a
>platform-independent way, but Java managed to do it.

That's not my understanding:

http://www.roseindia.net/javatutorials/shutting_down_threads_cleanly.shtml
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"...string iteration isn't about treating strings as sequences of strings, 
it's about treating strings as sequences of characters.  The fact that
characters are also strings is the reason we have problems, but characters 
are strings for other good reasons."  --Aahz
--
http://mail.python.org/mailman/listinfo/python-list


Re: Killing threads

2009-04-06 Thread Aahz
In article <685a59cd-9f02-483f-bc59-b55091a18...@u9g2000pre.googlegroups.com>,
imageguy   wrote:
>Aahz:
>>
>>For more info, see the slides from my thread tutorial:
>>http://pythoncraft.com/OSCON2001/
>
>Aahz, thanks for this reference and link to your presentation.  At the
>risk of highjacking the OP's question, I am bit confused as to how
>using an Event would cause a deadlock.  You presentation outlines
>Events, but doesn't elaborate on the specifics or usage.

>In threading.Event python 2.5 docs say;
>"This is one of the simplest mechanisms for communication between
>threads: one thread signals an event and other threads wait for it. "
>
>Again, I have limited experience, however, in my reading of the
>threading manual and review examples, Events were specifically design
>to be a thread safe way to communicate a 'state' to running threads ?
>In the OP's example 'do stuff' was open to wide interpretation,
>however, if within the thread's main 'while' loop the tread checks to
>see if the 'keepgoing' Event.isSet(), in what scenario would this
>create deadlock ?

Consider two threads trying to pass information to each other.  You need
two Event objects; unless you are very careful, they can both block on
waiting for each other.  It's easier if Queue is the only construct you
use, it can do everything that the lower-level constructs can do, without
needing to figure out the little gotchas -- and you'll need a Queue
eventually, so just learn that one.  It's all about simplifying.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"...string iteration isn't about treating strings as sequences of strings, 
it's about treating strings as sequences of characters.  The fact that
characters are also strings is the reason we have problems, but characters 
are strings for other good reasons."  --Aahz
--
http://mail.python.org/mailman/listinfo/python-list


Q: "Best" book for teaching

2009-04-06 Thread grkuntzmd
I am considering teaching an "introduction to programming" course for
continuing education adults at a local  community college. These would
people with no programming experience, but I will require a reasonable
facility with computers.

What would be a good book to use as the text for the course?

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


Re: Example for readline module usage?

2009-04-06 Thread Grant Edwards
On 2009-04-06, Peter Otten <__pete...@web.de> wrote:
> Grant Edwards wrote:
>
>> [I swear I've asked this question before, but Google can't find
>> it.]
>
> My Google is better than yours then:
>
> http://mail.python.org/pipermail/python-list/2008-July/669582.html

It certainly is.  All I could come up with were tons of hits on
the file-object's readline.  What I don't understand is
how/where the readline module is told what file descriptor it's
supposed to be reading from.  Is that just implicit in the
"import" operation?

-- 
Grant Edwards   grante Yow! I'm having BEAUTIFUL
  at   THOUGHTS about the INSIPID
   visi.comWIVES of smug and wealthy
   CORPORATE LAWYERS ...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Q: "Best" book for teaching

2009-04-06 Thread Reckoner
On Apr 6, 7:37 am, grkunt...@gmail.com wrote:
> I am considering teaching an "introduction to programming" course for
> continuing education adults at a local  community college. These would
> people with no programming experience, but I will require a reasonable
> facility with computers.
>
> What would be a good book to use as the text for the course?
>
> Thanks.

QuickPython is pretty good, but might be somewhat above the level
you're looking for. Depends on the class. You might want to use it to
guide a selection of topics.

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


object knows which object called it?

2009-04-06 Thread Reckoner
hi,

I have the following problem: I have two objects, say, A and B, which
are both legitimate stand-alone objects with lives of their own.

A contains B as a property, so I often do

A.B.foo()

the problem is that some functions inside of B actually need A
(remember I said they were both standalone objects), so I have to
often do:

A.B.foo_func(A)

Which is kind of awkward.

Is there some way that B.foo_func() could somehow know that it was
called as a property of A in this way?

Note that I'm looking for the calling object and NOT the calling
function.

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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-06 Thread Eric Smith
> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA1
>
> On Apr 6, 2009, at 9:21 AM, Jesse Noller wrote:
>
>> On Thu, Apr 2, 2009 at 4:33 PM, M.-A. Lemburg  wrote:
>>> On 2009-04-02 17:32, Martin v. Löwis wrote:
 I propose the following PEP for inclusion to Python 3.1.
>>>
>>> Thanks for picking this up.
>>>
>>> I'd like to extend the proposal to Python 2.7 and later.
>>>
>>
>> -1 to adding it to the 2.x series. There was much discussion around
>> adding features to 2.x *and* 3.0, and the consensus seemed to *not*
>> add new features to 2.x and use those new features as carrots to help
>> lead people into 3.0.
>
> Actually, isn't the policy just that nothing can go into 2.7 that
> isn't backported from 3.1?  Whether the actual backport happens or not
> is up to the developer though.  OTOH, we talked about a lot of things
> and my recollection is probably fuzzy.

I believe Barry is correct. The official policy is "no features in 2.7
that aren't also in 3.1". I personally think I'm not going to put anything
else in 2.7, specifically the ',' formatter stuff from PEP 378. 3.1 has
diverged too far from 2.7 in this regard to make the backport easy to do.
But this decision is left up to the individual committer.

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


Re: group several methods under a attribute

2009-04-06 Thread jelle
> Whatever it is, you should find a better way instead of cramming
> everything into a single class. That smells of the God Object
> antipattern (http://en.wikipedia.org/wiki/God_object).

Thanks Gerard, I'll take your advice.

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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-06 Thread P.J. Eby

At 02:00 PM 4/6/2009 +0100, Chris Withers wrote:

Martin v. Löwis wrote:

Chris Withers wrote:

Would this support the following case:

I have a package called mortar, which defines useful stuff:

from mortar import content, ...

I now want to distribute large optional chunks separately, but ideally
so that the following will will work:

from mortar.rbd import ...
from mortar.zodb import ...
from mortar.wsgi import ...

Does the PEP support this?

That's the primary purpose of the PEP.


Are you sure?

Does the pep really allow for:

from mortar import content
from mortar.rdb import something

...where 'content' is a function defined in mortar/__init__.py and 
'something' is a function defined in mortar/rdb/__init__.py *and* 
the following are separate distributions on PyPI:


- mortar
- mortar.rdb

...where 'mortar' does not contain 'mortar.rdb'.


See the third paragraph of http://www.python.org/dev/peps/pep-0382/#discussion

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


Re: speed of string chunks file parsing

2009-04-06 Thread bearophileHUGS
Hyunchul Kim:

> Following script do exactly what I want but I want to improve the speed.

This may be a bit faster, especially if sequences are long (code
untested):

import re
from collections import deque

def scanner1(deque=deque):
result_seq = deque()
cp_regular_expression = re.compile("^a complex regular expression
here$")
for line in file(inputfile):
if cp_regular_expression.match(line) and result_seq:
yield result_list
result_seq = deque()
result_seq.append(line)
yield result_seq

If the sequences are processed on the fly then you don't need to
create new ones and you can clear old ones, this may be a bit faster:

def scanner2(deque=deque):
result_seq = deque()
cp_regular_expression = re.compile("^a complex regular expression
here$")
for line in file(inputfile):
if cp_regular_expression.match(line) and result_seq:
yield result_list
result_seq.clear()
result_seq.append(line)
yield result_seq

Note that most of the time may be used by the regular expression,
often there are ways to speed it up using string methods, even as a
first faster approximate match too.

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


Re: speed of string chunks file parsing

2009-04-06 Thread bearophileHUGS
bearophile:
>     cp_regular_expression = re.compile("^a complex regular expression
> here$")
>     for line in file(inputfile):
>         if cp_regular_expression.match(line) and result_seq:

Sorry, you can replace that with:

cp_regular_expression = re.compile("^a complex regular expression
here$").match
for line in file(inputfile):
if cp_regular_expression(line) and result_seq:

That is a bit faster.

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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-06 Thread Chris Withers

P.J. Eby wrote:

See the third paragraph of 
http://www.python.org/dev/peps/pep-0382/#discussion


Indeed, I guess the PEP could be made more explanatory then 'cos, as a 
packager, I don't see what I'd put in the various setup.py and 
__init__.py to make this work...


That said, I'm delighted to hear it's going to be possible and 
wholeheartedly support the PEP and it's backporting to 2.7 as a result...


cheers,

Chris

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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-06 Thread Jesse Noller
On Mon, Apr 6, 2009 at 9:26 AM, Barry Warsaw  wrote:
> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA1
>
> On Apr 6, 2009, at 9:21 AM, Jesse Noller wrote:
>
>> On Thu, Apr 2, 2009 at 4:33 PM, M.-A. Lemburg  wrote:
>>>
>>> On 2009-04-02 17:32, Martin v. Löwis wrote:

 I propose the following PEP for inclusion to Python 3.1.
>>>
>>> Thanks for picking this up.
>>>
>>> I'd like to extend the proposal to Python 2.7 and later.
>>>
>>
>> -1 to adding it to the 2.x series. There was much discussion around
>> adding features to 2.x *and* 3.0, and the consensus seemed to *not*
>> add new features to 2.x and use those new features as carrots to help
>> lead people into 3.0.
>
> Actually, isn't the policy just that nothing can go into 2.7 that isn't
> backported from 3.1?  Whether the actual backport happens or not is up to
> the developer though.  OTOH, we talked about a lot of things and my
> recollection is probably fuzzy.
>
> Barry

That *is* the official policy, but there was discussions around no
further backporting of features from 3.1 into 2.x, therefore providing
more of an upgrade incentive
--
http://mail.python.org/mailman/listinfo/python-list


Re: speed of string chunks file parsing

2009-04-06 Thread andrew cooke

[disclaimer - this is just guessing from general knowledge of regular
expressions; i don't know any details of python's regexp engine]

if your regular expression is the bottleneck rewrite it to avoid lazy
matching, references, groups, lookbacks, and perhaps even counted repeats.
 with a little thought you can do almost everything using just choices
'(a|b)' and repeat 'a*'.  even if the expression is longer, it will
probably be faster.

character ranges - either explicit '[a-z]' or predefined '\w' (even '.') -
should be fine, but try to avoid having multiple occurrences of ".*".

see the timeit package for testing the speed of small chunks of code.

andrew



Hyunchul Kim wrote:
> Hi, all
>
> I have a simple script.
> Can you improve algorithm of following 10 line script, with a view point
> of speed ?
> Following script do exactly what I want but I want to improve the speed.
>
> This parse a file and accumulate lines till a line match a given regular
> expression.
> Then, when a line match a given regular expression, this function yield
> lines before the matched lines.
>
> 
> import re
> resultlist = []
> cp_regularexpression = re.compile('^a complex regular expression here$)
> for line in file(inputfile):
> if cp_regularexpression.match(line):
> if resultlist != []:
> yield resultlist
> resultlist = []
> resultlist.append(line)
> yield resultlist
> 
>
> Thank you in advance,
>
> Hyunchul
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


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


Re: Example for readline module usage?

2009-04-06 Thread Peter Otten
Grant Edwards wrote:

> On 2009-04-06, Peter Otten <__pete...@web.de> wrote:
>> Grant Edwards wrote:
>>
>>> [I swear I've asked this question before, but Google can't find
>>> it.]
>>
>> My Google is better than yours then:
>>
>> http://mail.python.org/pipermail/python-list/2008-July/669582.html
> 
> It certainly is.  All I could come up with were tons of hits on
> the file-object's readline.  What I don't understand is
> how/where the readline module is told what file descriptor it's
> supposed to be reading from.  Is that just implicit in the
> "import" operation?

What I've gleaned from the source: On import it sets the
PyOS_ReadlineFunctionPointer in Parser/myreadline.c. If both stdin and
stdout are a tty (GNU) readline is invoked via this pointer.

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


object knows which object called it?

2009-04-06 Thread R. David Murray
Reckoner  wrote:
> hi,
> 
> I have the following problem: I have two objects, say, A and B, which
> are both legitimate stand-alone objects with lives of their own.
> 
> A contains B as a property, so I often do
> 
> A.B.foo()
> 
> the problem is that some functions inside of B actually need A
> (remember I said they were both standalone objects), so I have to
> often do:
> 
> A.B.foo_func(A)
> 
> Which is kind of awkward.
> 
> Is there some way that B.foo_func() could somehow know that it was
> called as a property of A in this way?
> 
> Note that I'm looking for the calling object and NOT the calling
> function.

You could probably do this by creating a custom __getattr__ method (or
maybe even just a property) on A that would recognize B as an object and
return it wrapped in a class that would pick up the __getattr__ call on B
and translate it into a real call on B passing A as the first argument.

But that kind of magic is not considered good Python practice ("explicit
is better than implicit").  And it would be quite inefficient :)

I think the OO way to do this is to provide a method on A that does the
right thing:

def Bfoo_func(self):
self.B.foo_func(self)

Or maybe you could look at generic methods, which provide a way
to do multiple dispatch.

--
R. David Murray http://www.bitdance.com

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


Re: group several methods under a attribute

2009-04-06 Thread Aaron Brady
On Apr 6, 5:40 am, jelle  wrote:
> Hi,
>
> I'm working on a pretty large class and I'd like to group several
> methods under a attribute.
> Its not convenient to chop up the class in several smaller classes,
> nor would mixins really solve the issue.
> So, what is a pythonic way of grouping several methods under a
> attribute?
>
> Many thanks in advance,
>
> -jelle

Hi jelle,

I disagree with Gerhard that you 'should find a better way' and
another way 'almost certainly the right thing', unless his reason is
merely that it's an advanced technique that will get you into problems
down the road.  There's nothing 'un-object-oriented' about it.  The
link to the God pattern alleges that it fails to 'divide and conquer',
but you are dividing.  I think Python opens some beautiful doors in
this regard.

Here is some code which I understand accomplishes what you sought.

class ClsA( object ):
def __init__( self ):
self.inst= None
def __get__( self, instance, owner ):
self.inst= instance
return self
def submethA( self, arg ):
print( 'submethA %r, instance %r'% ( arg, self.inst ) )

class ClsB( object ):
A= ClsA( )
def methA( self, arg ):
print( 'methA %r'% arg )

b= ClsB( )
b.methA( 'this' )
b.A.submethA( 'that' )

#Output:
'''
methA 'this'
submethA 'that', instance <__main__.ClsB object...>
'''

In Python 3, you don't need 'ClsA' and 'ClsB' to inherit from 'object'
explicitly.  If you call 'b.A' from a class, 'ClsB.A', you will get a
'ClsA' object with a None 'inst' attribute, which is a big hole
waiting for you to step in.  Perhaps you want a wrapper for your
methods of 'ClsA' to check that 'inst' is set, or merely throw an
exception in '__get__' if 'instance' is 'None'.  Python after all
requires that a method's first argument be an instance of an improper
subclass of the class it was defined for.

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


Re: object knows which object called it?

2009-04-06 Thread Aaron Brady
On Apr 6, 9:53 am, Reckoner  wrote:
> hi,
>
> I have the following problem: I have two objects, say, A and B, which
> are both legitimate stand-alone objects with lives of their own.
>
> A contains B as a property, so I often do
>
> A.B.foo()
>
> the problem is that some functions inside of B actually need A
> (remember I said they were both standalone objects), so I have to
> often do:
>
> A.B.foo_func(A)
>
> Which is kind of awkward.
>
> Is there some way that B.foo_func() could somehow know that it was
> called as a property of A in this way?
>
> Note that I'm looking for the calling object and NOT the calling
> function.
>
> Thanks in advance.

Hi Reckoner,

I believe this does what you want.  It's an advanced technique and not
available in all OO languages.

class ClsA( object ):
def __init__( self ):
self.inst= None
def __get__( self, instance, owner ):
self.inst= instance
return self
def submethA( self, arg ):
print( 'submethA %r, instance %r'% ( arg, self.inst ) )

class ClsB( object ):
A= ClsA( )
def methA( self, arg ):
print( 'methA %r'% arg )

b= ClsB( )
b.methA( 'this' )
b.A.submethA( 'that' )

#Output:
'''
methA 'this'
submethA 'that', instance <__main__.ClsB object...>
'''

There's a small discussion in another today's thread, 'group several
methods under an attribute'.
--
http://mail.python.org/mailman/listinfo/python-list


Re: object knows which object called it?

2009-04-06 Thread Duncan Booth
Reckoner  wrote:

> hi,
> 
> I have the following problem: I have two objects, say, A and B, which
> are both legitimate stand-alone objects with lives of their own.
> 
> A contains B as a property, so I often do
> 
> A.B.foo()
> 
> the problem is that some functions inside of B actually need A
> (remember I said they were both standalone objects), so I have to
> often do:
> 
> A.B.foo_func(A)
> 
> Which is kind of awkward.
> 
> Is there some way that B.foo_func() could somehow know that it was
> called as a property of A in this way?
> 
> Note that I'm looking for the calling object and NOT the calling
> function.
> 
You can do something like this if you really want (Zope does), but it is 
complex and confusing (Zope 3 has dropped implicit acquisition as being a 
bad idea). Have a look at 
http://www.zope.org/Documentation/Books/ZDG/current/Acquisition.stx
which has the following example:

import ExtensionClass, Acquisition

class C(ExtensionClass.Base):
  color='red'

class A(Acquisition.Implicit):

  def report(self):
print self.color

a=A()
c=C()
c.a=A()

c.a.report() # prints 'red'

d=C()
d.color='green'
d.a=a

d.a.report() # prints 'green'

a.report() # raises an attribute error

and what you actually asked for: in the example above 'c.a.aq_parent is c'

If you want to confuse yourself you can install Zope's ExtensionClass and 
Acquisition modules from PyPi: 
http://pypi.python.org/pypi/Acquisition/2.12.0a1


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


set python default encoding

2009-04-06 Thread R. David Murray
reetesh nigam  wrote:
> Hi All,
> I am unable to set the python default encoding.
> i used the following proccess to set the python encoding
> 
> import sys
> reload(sys)
> sys.setdefaultencoding('latin-1')
> 
> but it is giving me the same error :
> 
>   args = ('utf8', "MEDICINE '\xc4 ", 10, 12, 'invalid data',
>  0x036FFE90>>)
>   encoding = 'utf8'
>   end = 12
>   message = ''
>   object = "MEDICINE '\xc4 "
>   reason = 'invalid data'
>   start = 10
> 
> 
> Please tell me how to solve this problem.

That doesn't look anything like a python traceback.  I'm guessing
you are using some sort of web framework?  Perhaps you should
try asking in the forum for the framework.

--
R. David Murray http://www.bitdance.com

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


Injecting python function in an embedded python engine

2009-04-06 Thread Roberto Fichera
Hi All in the list,

I've embedded python v2.6.x engine into my application without any problem.
Now I would like to inject some additional functions after importing a
python module.
So, basically I'm importing a python module via PyImport_ImportModule()
function.
The python module is a simple set of functions and I would check if some
functions
does exist or not, than if doesn't I would add it from my C application.
Checking if
a function does exist or not isn't an issue, it works without any
problem, my problem
is only how to inject a new function in an already imported module.

Thanks in advance,
Roberto Fichera.
--
http://mail.python.org/mailman/listinfo/python-list


Randomized Incremental Decision Tree

2009-04-06 Thread razafindrazaka faniry harijaona
Hello,



How to model this problem as a python code:



Starting with a general condition A, we enter a statement 'p' , if p
satisfy A which is always the case, then split A to three sub-conditions 
A1,A2,A3. And we enter
again a statement p1: if p1 satisfy A:

 if p1 satisfy A1: split A1 to A1_1,A1_2,A1_3

 if p1 satisfy A2: split A2 to A2_1,A2_2,A2_3

 if p1 satisfy A3: split A3 to A3_1,A3_2,A3_3

we enter a third statement p2 and test from A-->A_i-->Ai_j and
split the corresponding satisfied condition and so one Since we add 
incrementally
the statement, the condition will be generated according to the truth
of the statement.



Regards


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


Executing a C program from Python

2009-04-06 Thread vishakha vaibhav
Hi,
I am very new to python. I have my cgi script written in Python. My CGI script 
should call a C program and collect the value returned by this program and pass 
it to the browser.
 
Can anyone help me out in this. How can I execute the c program and collect the 
return value.
 
I tried,
import os
a=os.system("my-app")//my-app is the C program executable
print a
 
But the variable a prints 0 everytime. It should return a non-zero value as per 
my C program.  Is there any method to do what I need.
 
Regards,
vish


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


Re: group several methods under a attribute

2009-04-06 Thread Aaron Brady
On Apr 6, 12:02 pm, Aaron Brady  wrote:
> On Apr 6, 5:40 am, jelle  wrote:
>
> > Hi,
>
> > I'm working on a pretty large class and I'd like to group several
> > methods under a attribute.
> > Its not convenient to chop up the class in several smaller classes,
> > nor would mixins really solve the issue.
> > So, what is a pythonic way of grouping several methods under a
> > attribute?
>
> > Many thanks in advance,
>
> > -jelle
>
> Hi jelle,
>
> I disagree with Gerhard that you 'should find a better way' and
> another way 'almost certainly the right thing', unless his reason is
> merely that it's an advanced technique that will get you into problems
> down the road.
snip
> class ClsA( object ):
>     def __init__( self ):
>         self.inst= None
>     def __get__( self, instance, owner ):
>         self.inst= instance
>         return self
snip

There's a potential conflict in the above, which may have contributed
to Gerhard's advice.

If you have:

bA= ClsB( )
bB= ClsB( )
bA.A.submethA( 'what' )

then the ClsA instance will think it's being accessed from 'bA' until
it's accessed again!  This may cause problems such as if you store a
reference to it, because bB could access it, and it won't think it's
being accessed from bA anymore.  To get around this, you probably want
'__get__' to create a new object that is specifically dedicated to the
instance of ClsB that's calling it.  bA.A and bB.A return new objects,
separate from each other.  However, then two calls to bA.A won't
return the same object; they'll return new every time.

Another detail is that ClsA can't store instance information.  It
should be stored in ClsB instances, and the methods merely go
somewhere else.  Otherwise, the right place for 'A= ClsA( )' is in the
constructor:

class clsB:
  def __init__( self ):
self.A= ClsA( )

Then, every ClsB instance has its own instance of ClsA, and it can
store instance information, and doesn't create new instances when you
call it.  The creation of 'ClsA' merely goes in the initializer, not
the class statement.
--
http://mail.python.org/mailman/listinfo/python-list


Re: An executable operational semantics for Python

2009-04-06 Thread Piet van Oostrum
> bearophileh...@lycos.com (b) wrote:

>b> gideon:
>>> I've recently finished my Master's thesis on the semantics of Python.
>>> In my thesis I define the semantics of Python by rewriting an abstract
>>> machine. The sources that are used to produce my thesis can also be
>>> compiled into a working interpreter. Hence I call it an 'executable'
>>> semantics.

>b> Can it be used for some useful purpose?

I wanted to play with Python's MRO algorithm in the last weeks. I just
took the MRO description from Gideon's thesis, put it in a small Haskell
program and now I can easily try it out. 
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Cannot find text in *.py files with Windows Explorer?

2009-04-06 Thread Ant
On Apr 4, 7:09 am, Tim Golden  wrote:
> ... Now I think about it, try searching
> for "xplorer2" ...

I'll second that. It's one of the few non-open source bits of software
that I'll willingly pay a license for. Have used it for around 5 or 6
years now. It's by a little 1 man company called Zabkat.

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


Re: object knows which object called it?

2009-04-06 Thread Anthra Norell

Reckoner wrote:

hi,

I have the following problem: I have two objects, say, A and B, which
are both legitimate stand-alone objects with lives of their own.

A contains B as a property, so I often do

A.B.foo()

the problem is that some functions inside of B actually need A
(remember I said they were both standalone objects), so I have to
often do:

A.B.foo_func(A)

Which is kind of awkward.

Is there some way that B.foo_func() could somehow know that it was
called as a property of A in this way?

Note that I'm looking for the calling object and NOT the calling
function.

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

  


B has to get into A somehow. Providing an installation method is no 
inconvenience if one remembers later not to bypass it.



class A:
   something_for_contained_to_show = 99
   def install_contained (self, B):
   B.container = self   # Provides B with a reference to A
   self.contained = B

class B:
   def method_with_container_access (self):
  print self.container   # Show container object
  print self.container.something_for_contained_to_show


>>> a = A (); b = B (); a.install_contained (b)
>>> b.method_with_container_access ()

<__main__.A instance at 0x019D5788>
99


Does this look like a good idea?


Frederic


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


Re: Help with wxPython program :.: return 1?

2009-04-06 Thread Trent Mick

Dennis Lee Bieber wrote:

I don't know what Komodo is coded in, but if it is using wx, you may
be failing from having two "mainloop" processes... (same problem as
trying to run a Tkinter application from inside IDLE, and probably
trying to run a win32gui application from PythonWin)


No, Komodo is not a wx app.

Trent

--
Trent Mick
trentm at activestate.com
--
http://mail.python.org/mailman/listinfo/python-list


Delicious API and urllib2

2009-04-06 Thread Bill
The delicious api requires http authorization (actually https). A
generic delicious api post url is "https://
username:passw...@api.api.del.icio.us/v1/posts/add?url=http://
example.com/&description=interesting&tags=whatever".

This works fine when entered in the Firefox address bar. However
urllib2.urlopen(delicious_post_url) chokes and returns
"httplib.InvalidURL: nonnumeric port: 'passw...@api.del.icio.us".

Delicious really wants that authorization stuff embedded in the url as
it also rejected my attempts at using urllib2.HTTPBasicAuthHandler(),
etc.
Anybody have any hints?
--
http://mail.python.org/mailman/listinfo/python-list


pycap (popcap gaming lib w python 2.5) mouse question

2009-04-06 Thread eric_dex...@msn.com
anyone use pycap based on popcap gaming lib.. http://www.farbs.org/pycap.html??
(not to be confused with the other pycap)  I was trying to figure out
why the mouse works in the example I didn't see any python code for it
but It seem to have an effect in the example..

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


Re: logging - string formating problems

2009-04-06 Thread Vinay Sajip
On Apr 6, 1:58 pm, "Werner F. Bruhin"  wrote:
> I am fully aware that the problem is in my code, however as getMessage
> inlogging.__init__.py does not catch the exception it is pretty
> difficult to find the problem without manually inspecting 
> anylogging.something statements.
>
> My hack oflogging.py is really a hack and I know that this can not be
> used a patch but I hope that someone with more know how then me knows
> how to improve the situation.
>
> BTW, using the below hack was enough for me to mind the bad code in my
> stuff, but it shouldn't have been this difficult.
>
> Traceback (most recent call last):
>    File "C:\Python25\lib\logging\__init__.py", line 750, in emit
>      msg = self.format(record)
>    File "C:\Python25\lib\logging\__init__.py", line 636, in format
>      return fmt.format(record)
>    File "C:\Python25\lib\logging\__init__.py", line 424, in format
>      record.message = record.getMessage()
>    File "C:\Python25\lib\logging\__init__.py", line 288, in getMessage
>      msg = msg % self.args
> TypeError: not all arguments converted during string formatting
>
> Then I hacklogging.py and change line 288 in getMessage from:
>              msg = msg % self.args
>
> to:
>              try:
>                  msg = msg % self.args
>              except:
>                  print 'msg: %s' % msg
>                  print 'args: %s' % self.args
>                  print traceback.format_exc()
>
> I get the following which helps a lot more in finding were in my code I
> have the problem:
>
> msg: ping_min: 1
> args: replace
> Traceback (most recent call last):
>    File "C:\Python25\lib\logging\__init__.py", line 290, in getMessage
>      msg = msg % self.args
> TypeError: not all arguments converted during string formatting
>
> As mentioned above, I know the above code is not good enough at all, but
> I hope that maybe Vinay Sajip or someone else with more know how then I
> have can come up with a patch which will make this type of situation easier.
>
> Werner

Handlers should direct any exceptions raised during emit() to
handleError(), except for SystemExit and KeyboardInterrupt which are
re-raised. So, you should be able to redefine handleError to print the
relevant information if a TypeError is raised, either by subclassing
or by monkey-patching as Peter Otten has suggested. However,
handleError normally swallows the exceptions if raiseExceptions is
false (by default, it's set to true. It's meant to be set to false
only in production environments where exceptions raised in logging
should have no influence on the application's behaviour).

Regards,

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


Re: Accessing mail box tree of Thunderbird/Seamonkey?

2009-04-06 Thread robert

Chris Rebert wrote:

On Sun, Apr 5, 2009 at 1:04 AM, robert  wrote:

Is there a API/possibilty for reading&writing (live) in the mail box tree of
Thunderbird/Seamonkey with Python?



From what I can google, they're already in mbox format, so you can use

mailbox.mbox to read/write to them.
See http://docs.python.org/library/mailbox.html#mailbox.mbox



I was already aware of. those mbox file just grow and grow until 
one does a 'Compress Folder'. There are additional complicated 
index files and caching and all. So it doesn't reflect the current 
state nor allows for practical modifying.
I found only SimpleMAPI for the inbox&send only. the XUL api seems 
to be usable only from inside.


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


Re: Delicious API and urllib2

2009-04-06 Thread Brent Bloxam

Bill wrote:

The delicious api requires http authorization (actually https). A
generic delicious api post url is "https://
username:passw...@api.api.del.icio.us/v1/posts/add?url=http://
example.com/&description=interesting&tags=whatever".

This works fine when entered in the Firefox address bar. However
urllib2.urlopen(delicious_post_url) chokes and returns
"httplib.InvalidURL: nonnumeric port: 'passw...@api.del.icio.us".

Delicious really wants that authorization stuff embedded in the url as
it also rejected my attempts at using urllib2.HTTPBasicAuthHandler(),
etc.
Anybody have any hints?
--
http://mail.python.org/mailman/listinfo/python-list
  
What failure were you experiencing when you were using the 
HTTPBasicAuthHandler?


Did you follow the sample code from the docs?


import urllib2
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='PDQ Application',
  uri='https://mahler:8092/site-updates.py',
  user='klem',
  passwd='kadidd!ehopper')
opener = urllib2.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib2.install_opener(opener)
urllib2.urlopen('http://www.example.com/login.html'


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


Re: Executing a C program from Python

2009-04-06 Thread Brent Bloxam

vishakha vaibhav wrote:

Hi,
I am very new to python. I have my cgi script written in Python. My 
CGI script should call a C program and collect the value returned by 
this program and pass it to the browser.
 
Can anyone help me out in this. How can I execute the c program and 
collect the return value.
 
I tried,

import os
a=os.system("my-app")//my-app is the C program executable
print a
 
But the variable *a* prints 0 everytime. It should return a non-zero 
value as per my C program.  Is there any method to do what I need.
 
Regards,

vish




--
http://mail.python.org/mailman/listinfo/python-list
  
Executing from os.system returns the return code from the application. 
Take a look at the subprocess module 
(http://docs.python.org/library/subprocess.html#module-subprocess), as 
recommended by the os docs when you need to deal with capturing output 
from stdout.
--
http://mail.python.org/mailman/listinfo/python-list


Best way to start

2009-04-06 Thread Avi
Hi,

What is a good way to learn Python?

Do you recommend going by a book (suggestions welcome) or learning
with tutorials? Both?

Thanks in advance,
Avi
--
http://mail.python.org/mailman/listinfo/python-list


Eval Problem

2009-04-06 Thread Victor Subervi
Hi:
I have this code:

x = 1
while x <= bitties:
  file = open(p + str(x) + ".txt")
  for line in file:
print line
  print eval(bits[x - 1])
  x += 1

which throws this error:

[Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler
mod_python.cgihandler: Traceback (most recent call last):
[Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler
mod_python.cgihandler: File
"/usr/lib64/python2.4/site-packages/mod_python/apache.py", line 299, in
HandlerDispatch\n result = object(req)
[Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler
mod_python.cgihandler: File
"/usr/lib64/python2.4/site-packages/mod_python/cgihandler.py", line 96, in
handler\n imp.load_module(module_name, fd, path, desc)
[Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler
mod_python.cgihandler: File "/var/www/vhosts/
articles.13gems.com/httpdocs/index_frame.py", line 89, in ?\n print
eval(bits[1])
[Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler
mod_python.cgihandler: File "", line 1
[Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler
mod_python.cgihandler: tableBottom(348,180)
[Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler
mod_python.cgihandler: ^
[Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221] PythonHandler
mod_python.cgihandler: SyntaxError: invalid syntax

However, if I edit out the offending line and add:

print tableBottom(348,180)

it prints! What´s wrong?
TIA,
Victor
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to start

2009-04-06 Thread Chris Rebert
On Mon, Apr 6, 2009 at 12:08 PM, Avi  wrote:
> Hi,
>
> What is a good way to learn Python?
>
> Do you recommend going by a book (suggestions welcome) or learning
> with tutorials? Both?

The official Python tutorial is pretty darn good:
http://docs.python.org/tutorial/
If you want a book as well, I personally liked "Python in a Nutshell";
it's not in tutorial format, but you can definitely learn Python from
it.

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Weird Tk Canvas coordinate issue

2009-04-06 Thread Tim Shannon
I'm new to python, so keep that in mind.

I have a tk Canvas that I'm trying to draw on, and I want to start my
drawing at an offset (from 0) location.  So I can tweak this as I code, I
set this offset as a class level variable:

def ClassName:
OFFSET = 20

def __init__(self, master)):
self.canvas = Canvas(master)
self.canvas.create_rectangle(self.OFFSET,
 self.OFFSET,
 300 + self.OFFSET,
 300 + self.OFFSET,
 width=2)


The weird thing is, it doesn't offset.  If I set my offset to 10, it
still starts drawing at 0,0.
Here's the really weird part (at least to me), if I put a print line right
about my drawing statements to print the value of the offset, it works like
it should, and offsets the drawing.
If I remove the print line, the offset goes away.

This makes no sense to me.
--
http://mail.python.org/mailman/listinfo/python-list


cgi file limit size?

2009-04-06 Thread davidj411
I am wondering where the limitation of filesize comes from when i
upload a large file.
it uploads when the filesize is less than 20 MB (but not if larger).
the script does not limit the filesize so it is either an HTTP
specification or a webserver limit, right?
maybe my connection to the server is timing out during the upload?
web server is IIS 6.0.
python is 2.5.2.
IIS webmapping does not use "-u" b/c nothing works when that option is
used.

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


Re: group several methods under a attribute

2009-04-06 Thread jelle
Hi Aaron,

Thanks a lot for your suggestions.
I wasnt familiar with the __get__ magic, which seems interesting.

So, finally it seems that the cleanest pattern is:

class ClsA( object ):
def __init__( self, other ):
self.inst= other

def submethA( self, arg ):
print( 'submethA %r, instance %r'% ( arg, self.inst ) )

class ClsB( object ):
def methA( self, arg ):
self.A= ClsA( self )
print( 'methA %r'% arg )

b= ClsB( )
b.methA( 'this' )
b.A.submethA( 'that' )


Many thanks,


-jelle


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


Re: object knows which object called it?

2009-04-06 Thread George Sakkis
On Apr 6, 10:53 am, Reckoner  wrote:
> hi,
>
> I have the following problem: I have two objects, say, A and B, which
> are both legitimate stand-alone objects with lives of their own.
>
> A contains B as a property, so I often do
>
> A.B.foo()
>
> the problem is that some functions inside of B actually need A
> (remember I said they were both standalone objects), so I have to
> often do:
>
> A.B.foo_func(A)
>
> Which is kind of awkward.
>
> Is there some way that B.foo_func() could somehow know that it was
> called as a property of A in this way?
>
> Note that I'm looking for the calling object and NOT the calling
> function.

Read up on descriptors [1], it seems that's what you're looking for.

HTH,
George

[1] http://users.rcn.com/python/download/Descriptor.htm
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to start

2009-04-06 Thread andrew cooke
Avi wrote:
> What is a good way to learn Python?
>
> Do you recommend going by a book (suggestions welcome) or learning
> with tutorials? Both?

how do you like to learn and how much experience do you have programming
in other languages?

andrew


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


Re: Best way to start

2009-04-06 Thread Tim Shannon
I personally learned a lot from www.diveintopython.org

On Mon, Apr 6, 2009 at 2:08 PM, Avi  wrote:

> Hi,
>
> What is a good way to learn Python?
>
> Do you recommend going by a book (suggestions welcome) or learning
> with tutorials? Both?
>
> Thanks in advance,
> Avi
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to start

2009-04-06 Thread namekuseijin
I was able to get a friend into Python over a Google Chat.  I pointed 
him to the downloads page, waited for him to install, then covered the 
basics in quite a few steps (syntax, conditionals, loops, function 
definition and application, classes and methods, lists, dicts and 
comprehensions).


He loved the language and used it to solve some problems in the Euler 
Project. :)


--
a game sig: http://tinyurl.com/d3rxz9
--
http://mail.python.org/mailman/listinfo/python-list


Returning different types based on input parameters

2009-04-06 Thread George Sakkis
That's more of a general API design question but I'd like to get an
idea if and how things are different in Python context. AFAIK it's
generally considered bad form (or worse) for functions/methods to
return values of different "type" depending on the number, type and/or
values of the passed parameters. I'm using "type" loosely in a duck-
typing sense, not necessarily as a concrete class and its descendants,
although I'm not sure if even duck-typing is endorsed for return
values (as opposed to input parameters).

For example, it is common for a function f(x) to expect x to be simply
iterable, without caring of its exact type. Is it ok though for f to
return a list for some types/values of x, a tuple for others and a
generator for everything else (assuming it's documented), or it should
always return the most general (iterator in this example) ?

To take it further, what if f wants to return different types,
differing even in a duck-type sense? That's easier to illustrate in a
API-extension scenario. Say that there is an existing function `solve
(x)` that returns `Result` instances.  Later someone wants to extend f
by allowing an extra optional parameter `foo`, making the signature
`solve(x, foo=None)`. As long as the return value remains backward
compatible, everything's fine. However, what if in the extended case,
solve() has to return some *additional* information apart from
`Result`, say the confidence that the result is correct ? In short,
the extended API would be:

def solve(x, foo=None):
'''
@rtype: `Result` if foo is None; (`Result`, confidence)
otherwise.
'''

Strictly speaking, the extension is backwards compatible; previous
code that used `solve(x)` will still get back `Result`s. The problem
is that in new code you can't tell what `solve(x,y)` returns unless
you know something about `y`. My question is, is this totally
unacceptable and should better be replaced by a new function `solve2
(x, foo=None)` that always returns (`Result`, confidence) tuples, or
it might be a justifiable cost ? Any other API extension approaches
that are applicable to such situations ?

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


Some test fail on my new Python 2.6

2009-04-06 Thread Sorin Schwimmer

Hi All,

I just downloaded and compiled Python 2.6 on a Gentoo Linux, IBM NetVista.

After going through the usual steps (./configure, make), I ran a test (make 
test), and got some unexpected issues, which are detailed here:

# ./python Lib/test/test_tcl.py
Traceback (most recent call last):
  File "Lib/test/test_tcl.py", line 6, in 
from Tkinter import Tcl
  File "/install/Python-2.6.1/Lib/lib-tk/Tkinter.py", line 39, in 
import _tkinter # If this fails your Python may not be configured for Tk

# ./python Lib/test/test_dbm.py
Traceback (most recent call last):
  File "Lib/test/test_dbm.py", line 3, in 
import dbm
ImportError: No module named dbm

# ./python Lib/test/test_multiprocessing.py
Traceback (most recent call last):
  File "Lib/test/test_multiprocessing.py", line 1825, in 
main()
  File "Lib/test/test_multiprocessing.py", line 1822, in main
test_main(unittest.TextTestRunner(verbosity=2).run)
  File "Lib/test/test_multiprocessing.py", line 1788, in test_main
raise TestSkipped("OSError raises on RLock creation, see issue 3111!")
test.test_support.TestSkipped: OSError raises on RLock creation, see issue 3111!

What is issue 3111? Couldn't find any reference to it.

Two more:

# ./python Lib/test/test_httpservers.py
test_command (__main__.BaseHTTPServerTestCase) ... ok
test_handler (__main__.BaseHTTPServerTestCase) ... ok
test_head_keep_alive (__main__.BaseHTTPServerTestCase) ... ok
test_header_close (__main__.BaseHTTPServerTestCase) ... ok
test_internal_key_error (__main__.BaseHTTPServerTestCase) ... ok
test_request_line_trimming (__main__.BaseHTTPServerTestCase) ... ok
test_return_custom_status (__main__.BaseHTTPServerTestCase) ... ok
test_return_header_keep_alive (__main__.BaseHTTPServerTestCase) ... ok
test_send_blank (__main__.BaseHTTPServerTestCase) ... ok
test_version_bogus (__main__.BaseHTTPServerTestCase) ... ok
test_version_digits (__main__.BaseHTTPServerTestCase) ... ok
test_version_invalid (__main__.BaseHTTPServerTestCase) ... ok
test_version_none (__main__.BaseHTTPServerTestCase) ... ok
test_version_none_get (__main__.BaseHTTPServerTestCase) ... ok
test_get (__main__.SimpleHTTPServerTestCase) ... FAIL
test_head (__main__.SimpleHTTPServerTestCase) ... ok
test_invalid_requests (__main__.SimpleHTTPServerTestCase) ... ok
test_authorization (__main__.CGIHTTPServerTestCase) ... FAIL
test_headers_and_content (__main__.CGIHTTPServerTestCase) ... FAIL
test_invaliduri (__main__.CGIHTTPServerTestCase) ... ok
test_post (__main__.CGIHTTPServerTestCase) ... FAIL

==
FAIL: test_get (__main__.SimpleHTTPServerTestCase)
--
Traceback (most recent call last):
  File "Lib/test/test_httpservers.py", line 243, in test_get
self.check_status_and_reason(response, 404)
  File "Lib/test/test_httpservers.py", line 219, in check_status_and_reason
self.assertEquals(response.status, status)
AssertionError: 200 != 404

==
FAIL: test_authorization (__main__.CGIHTTPServerTestCase)
--
Traceback (most recent call last):
  File "Lib/test/test_httpservers.py", line 340, in test_authorization
(res.read(), res.getheader('Content-type'), res.status))
AssertionError: ('Hello World\n', 'text/html', 200) != ('', None, 200)

==
FAIL: test_headers_and_content (__main__.CGIHTTPServerTestCase)
--
Traceback (most recent call last):
  File "Lib/test/test_httpservers.py", line 321, in test_headers_and_content
(res.read(), res.getheader('Content-type'), res.status))
AssertionError: ('Hello World\n', 'text/html', 200) != ('', None, 200)

==
FAIL: test_post (__main__.CGIHTTPServerTestCase)
--
Traceback (most recent call last):
  File "Lib/test/test_httpservers.py", line 328, in test_post
self.assertEquals(res.read(), '1, python, 123456\n')
AssertionError: '' != '1, python, 123456\n'

--
Ran 21 tests in 3.149s

FAILED (failures=4)

# ./python Lib/test/test_socket.py
testCrucialConstants (__main__.GeneralModuleTests) ... ok
testDefaultTimeout (__main__.GeneralModuleTests) ... ok
testGetServBy (__main__.GeneralModuleTests) ... ok
testGetSockOpt (__main__.GeneralModuleTests) ... ok
testHostnameRes (__main__.GeneralModuleTests) ... ok
testIPv4toString (__main__.GeneralModuleTests) ... ok
testIPv6toString (__main__.GeneralModuleTests) ... ok
testInterpreterCrash (__main__.GeneralModuleTests) ... ok
testNewAttributes (__main__.GeneralModuleTests) ... ok
testNtoH (__main__.GeneralModuleTests) ... ok
testN

Re: Best way to start

2009-04-06 Thread Avi
A BIG Thanks to Chris and Andrew for suggestions.

This is an awesome place.

namekuseijin: haha...got a friend hooked to Python on chat? hilarious!
--
http://mail.python.org/mailman/listinfo/python-list


Re: decorators don't play nice with nose?

2009-04-06 Thread Diez B. Roggisch

hyperboreean schrieb:
Hi, I am trying to test the business part of a web service. For this I 
am using unittest & nose.
I wrote a decorator that should handle the xml test file retrieval, but 
it seems I can't get it working with nose.

Here's the code:


* MyApp.py -- base test class *

import os
import unittest

from MyApp.Core import XmlParser


__all__ = ['MyAppTest', 'setup']


PATH = os.path.dirname(__file__) or ''


class setup(object):
   """Decorator to ease the use of xml files in MyApp tests.

   The way it works it that it decorates a test method which has a first
   default parameter called 'parser' and it overwrites this parameter value
   with a XmlParser instance.

   The xml file should be located under:
   data/testedBusinessRequest/testMethodName.xml
   """
   def __init__(self, testedBusinessRequest = ''):
   self.testedBusinessRequest =\
   testedBusinessRequest.lower()


   def _getXmlParser(self, xml):
   documentElement = XmlParser.parseXmlStream(xml)
   parser = XmlParser.getParser(documentElement)
   return parser


   def __call__(self, method):

   # TODO: error handling here
   methodName = method.func_code.co_name
   methodName = methodName.split('_')[1]

   xmlFolder = self.testedBusinessRequest
   xmlFile = '%s.xml' % methodName

   path = os.path.join(PATH, 'data',
   xmlFolder, xmlFile)

   f = open(path)
   xml = f.read()
   f.close()
   method.func_defaults = (self._getXmlParser(xml),)
   return method


class MyAppTest(unittest.TestCase):

   def setUp(self):
   self.database = Database()

   def tearDown(self):
   pass


* test_Login.py - test a business request *
from MyAppTest import MyAppTest, setup

from MyApp import Login


class TestLogin(MyAppTest):
   testedBusinessRequest = 'Login'

   @setup(testedBusinessRequest)
   def test_validParameters(self, parser = None):
   response = Login(self.database, parser).run()
   return True



Ok, so the decorator setup should fill the parser parameter with a 
XmlParser object. This works well if I add a __main__ and use unittest 
to run the tests. But if I use nose, I get the following error:


*TypeError: unbound method __call__() must be called with setup instance 
as first argument (got module instance instead)*


Any advices?


Nose works via the func_name parameter of a method/function.

So when you decorate it, you need to make sure that is set properly. One 
option is to do something like this:



from functools import wraps

def my_decorator(f):
   @wraps(f)
   def _d(*args, **kwargs):
   return f(*args, **kwargs)

   return _d


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


print from a python script.

2009-04-06 Thread Ronn Ross
I'm trying to print a simple string to a network printer. This is what I
have so far:

import os

printer_path = "192.168.200.139"
p = os.popen(printer_path, 'w')
p.write("this is a printer test")
p.close()

I'm trying to call the printer from its IP address. When I run the script I
get:
sh: 192.168.200.139: not found

thanks for you help in advance.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to go about. On read/write locks

2009-04-06 Thread Diez B. Roggisch
This is a classical synchronization problem with a classical solution: 
You treat the readers as a group, and the writers individually. So you

have a write lock that each writer has to acquire and release, but it is
acquired only by the first reader and released by the last one.
Therefore you need a counter of the number of readers, and manipulations
of this counter must be protected by another lock.



I was going to suggest a similar approach but refused to because of a 
problem I see with your code as well - if the readers are reading very 
fast (the OP didn't state what his actual application is, so it might 
not be a consumer-producer scheme which I don't think a dict would be 
the natural choice anyway) they can block the writer from writing 
alltogether.



Or do I miss something here?

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


Re: print from a python script.

2009-04-06 Thread Chris Rebert
On Mon, Apr 6, 2009 at 2:24 PM, Ronn Ross  wrote:
> I'm trying to print a simple string to a network printer. This is what I
> have so far:
>
> import os
>
> printer_path = "192.168.200.139"
> p = os.popen(printer_path, 'w')
> p.write("this is a printer test")
> p.close()
>
> I'm trying to call the printer from its IP address. When I run the script I
> get:
> sh: 192.168.200.139: not found

Note that os.popen() is for running /commands/, not opening files or
IP addresses. I think you want the httplib or urllib modules instead.

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to go about. On read/write locks

2009-04-06 Thread Diez B. Roggisch

Python's approach with the GIL is both reasonable and disappointing.
Reasonable because I understand how it can make things easier for its
internals. Disappointing because it means that standard python cannot
take advantage of the parallelism that can more and more often be
afforded by today's computers. I.e. I found only recently, almost by
chance, that my wife's laptop has not one but two processors, even
though it isn't a particularly high-end computer. I now understand
that OS-level threading does use them both, but I understand that the
GIL effectively prevents parallel operations. (Am I understanding
correctly?)


Not entirely. Yes, if your application is CPU-bound. No if it's 
IO-bound. And a lot of people think that threads are actually the wrong 
approach for concurrency anyway, so with python2.6 there comes the 
multiprocessing-module that lets you use the full capacity of your CPUs.



I do not completely understand your statement in the context of my
original example though, the shared dictionary. As the GIL is released
every X bytecode operations surely it can happen that as the
dictionary is iterated through, i.e. in a for/in loop, a different
thread might change it, with potentially catastrophic consequences.
The GIL wouldn't be able to prevent this, wouldn't it?


You didn't give a concrete usage scenario for your shared dict - but I 
assumed that by reading and writing you meant


mydict[key] = value

value = mydict[key]

which are both atomic through the GIL.

More complex operations - such as iteration - might need more coarse 
grained locking.


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


Re: Best way to start

2009-04-06 Thread namekuseijin

Avi escreveu:

A BIG Thanks to Chris and Andrew for suggestions.

This is an awesome place.

namekuseijin: haha...got a friend hooked to Python on chat? hilarious!


True story.  But he was already a programmer.  Only Pascal Delphi though.

--
a game sig: http://tinyurl.com/d3rxz9
--
http://mail.python.org/mailman/listinfo/python-list


Re: Returning different types based on input parameters

2009-04-06 Thread MRAB

George Sakkis wrote:

That's more of a general API design question but I'd like to get an
idea if and how things are different in Python context. AFAIK it's
generally considered bad form (or worse) for functions/methods to
return values of different "type" depending on the number, type and/or
values of the passed parameters. I'm using "type" loosely in a duck-
typing sense, not necessarily as a concrete class and its descendants,
although I'm not sure if even duck-typing is endorsed for return
values (as opposed to input parameters).

For example, it is common for a function f(x) to expect x to be simply
iterable, without caring of its exact type. Is it ok though for f to
return a list for some types/values of x, a tuple for others and a
generator for everything else (assuming it's documented), or it should
always return the most general (iterator in this example) ?

To take it further, what if f wants to return different types,
differing even in a duck-type sense? That's easier to illustrate in a
API-extension scenario. Say that there is an existing function `solve
(x)` that returns `Result` instances.  Later someone wants to extend f
by allowing an extra optional parameter `foo`, making the signature
`solve(x, foo=None)`. As long as the return value remains backward
compatible, everything's fine. However, what if in the extended case,
solve() has to return some *additional* information apart from
`Result`, say the confidence that the result is correct ? In short,
the extended API would be:

def solve(x, foo=None):
'''
@rtype: `Result` if foo is None; (`Result`, confidence)
otherwise.
'''

Strictly speaking, the extension is backwards compatible; previous
code that used `solve(x)` will still get back `Result`s. The problem
is that in new code you can't tell what `solve(x,y)` returns unless
you know something about `y`. My question is, is this totally
unacceptable and should better be replaced by a new function `solve2
(x, foo=None)` that always returns (`Result`, confidence) tuples, or
it might be a justifiable cost ? Any other API extension approaches
that are applicable to such situations ?


I don't like the sound of this. :-)

In your example I would possibly suggest returning a 'Result' object and
then later subclassing to give 'ConfidenceResult' which has the
additional 'confidence' attribute.

I think the only time when it's OK to return instances of different
classes is when one of them is None, for example the re module where
match() returns either a MatchObject (if successful) or None (if
unsuccessful); apart from that, a function should always return an
instance of the same class (or perhaps a subclass) or, if a collection
then the same type of collection (eg always a list and never sometimes a
list, sometimes a tuple).
--
http://mail.python.org/mailman/listinfo/python-list


Re: print from a python script.

2009-04-06 Thread R. David Murray
Chris Rebert  wrote:
> On Mon, Apr 6, 2009 at 2:24 PM, Ronn Ross  wrote:
> > I'm trying to print a simple string to a network printer. This is what I
> > have so far:
> >
> > import os
> >
> > printer_path = "192.168.200.139"
> > p = os.popen(printer_path, 'w')
> > p.write("this is a printer test")
> > p.close()
> >
> > I'm trying to call the printer from its IP address. When I run the script I
> > get:
> > sh: 192.168.200.139: not found
> 
> Note that os.popen() is for running /commands/, not opening files or
> IP addresses. I think you want the httplib or urllib modules instead.

Actually in this case (_writing_ text to a printer) I think you want the
Linux CUPS package for access to your remote printer, and then popen on
the 'lp' command (but use subprocess.Popen instead, that's better).

By the way, when you get closer to getting the above to work, you're
going to want to add a '\n' to the end of that string you are writing.

--
R. David Murray http://www.bitdance.com

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


Re: Weird Tk Canvas coordinate issue

2009-04-06 Thread John Posner

Tim Shannon wrote:

I'm new to python, so keep that in mind.
 
I have a tk Canvas that I'm trying to draw on, and I want to start my 
drawing at an offset (from 0) location.  So I can tweak this as I 
code, I set this offset as a class level variable:
 
def ClassName:

OFFSET = 20

def __init__(self, master)):
self.canvas = Canvas(master)
self.canvas.create_rectangle(self.OFFSET,
 self.OFFSET,
 300 + self.OFFSET,
 300 + self.OFFSET,
 width=2)
  
 
The weird thing is, it doesn't offset.  If I set my offset to 10, 
it still starts drawing at 0,0. 
Here's the really weird part (at least to me), if I put a print line 
right about my drawing statements to print the value of the offset, it 
works like it should, and offsets the drawing.

If I remove the print line, the offset goes away.
 
This makes no sense to me.

Tim Shannon wrote:

I'm new to python, so keep that in mind.
 
I have a tk Canvas that I'm trying to draw on, and I want to start my 
drawing at an offset (from 0) location.  So I can tweak this as I 
code, I set this offset as a class level variable:
 
def ClassName:

OFFSET = 20

def __init__(self, master)):
self.canvas = Canvas(master)
self.canvas.create_rectangle(self.OFFSET,
 self.OFFSET,
 300 + self.OFFSET,
 300 + self.OFFSET,
 width=2)
  
The above code wouldn't even compile. Please be careful to cut-and-paste 
working code into your email message. (I've made this mistake myself!) 
Changes to make:


1. first line: change "def" to "class"

2. def __init__(self, master)):<--- get rid of extra ")"

3. Make sure to "pack" the canvas into the overall Tk window:

   self.canvas = Canvas(master)
   self.canvas.pack()<--- add this line

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


extract Infobox contents

2009-04-06 Thread Anish Chapagain
Hi,
I was trying to extract wikipedia Infobox contents which is in format
like given below, from the opened URL page in Python.

{{ Infobox Software
| name   = Bash
| logo   = [[Image:bash-org.png|165px]]
| screenshot = [[Image:Bash demo.png|250px]]
| caption= Screenshot of bash and [[Bourne shell|sh]]
sessions demonstrating some features
| developer  = [[Chet Ramey]]
| latest release version = 4.0
| latest release date= {{release date|mf=yes|2009|02|20}}
| programming language   = [[C (programming language)|C]]
| operating system   = [[Cross-platform]]
| platform   = [[GNU]]
| language   = English, multilingual ([[gettext]])
| status = Active
| genre  = [[Unix shell]]
| source model   = [[Free software]]
| license= [[GNU General Public License]]
| website= [http://tiswww.case.edu/php/chet/bash/
bashtop.html Home page]
}} //upto this line

I need to extract all data between {{ Infobox ...to }}

Thank's if anyone can help,
am trying with

s1='{{ Infobox'
s2=len(s1)
pos1=data.find("{{ Infobox")
pos2=data.find("\n",pos2)

pat1=data.find("}}")

but am ending up getting one line at top only.

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


Re: Best way to start

2009-04-06 Thread namekuseijin
Google's automatic chat logging is nice too.  My first online python 
tutorial for someone who never saw it before (sorry for not being in 
english):


14/09/08
00:50 KALEL: I'm on Phyton Shell
00:52 me: cool
  let's go
  type it: 2
  just to get rid of your fears... :)
 KALEL: Hah hah hah hah
 me: really
  1+2
00:53 KALEL: Stupendous
  AND???
 me: type( 1 )
  type( "foobar" )
00:54 KALEL: Good
 me: Python is dinamically typed, you don't declare types: a = 2
  a+3
 KALEL: Ok... how do I refer to the values
00:55 me: a
  ?
 KALEL: I mean, define some constant???
 me: no constants, all variable. of course, there are scope rules
00:56 modules, classes and functions
  let's go by steps
 KALEL: Ok Morpheus
 me: type there: dir() to know what it is in the current scope
 KALEL: ['__builtins__', '__doc__', '__name__', 'a']
 me: exact
  it's a list: []
00:57 KALEL: I see
 me: type dir( [] ) to know the methods of a list object
 KALEL: Cool
 me: show
00:58 KALEL: Waitaminute:
  dir is a list, right?
 me: it's a function returning a list
  ls = dir()
 KALEL: OK.
 me: ls.sort
00:59 KALEL: Traceback (most recent call last):
File "", line 1, in 
ls.sort
NameError: name 'ls' is not defined
 me: you did not define it?
  ls = dir()
01:00 KALEL: 
 me: yes
  ls.sort()
  forgot the parentheses to call
  ;)
  ls.index('a')
01:01 for i in dir(): print i
01:02 KALEL: stopped
01:03 me: one more ENTER
  BTW: blocks in python are idented by tabs
 KALEL: ok
 me: no {} or begin end
  wait
01:04 KALEL: wonderful
01:05 Ok, but when you have a single command, you don't need {} nor 
begin end

01:06 me: true, for that same reason I put print in the same line
  for and if end in :
 KALEL: 0x0
 me: if foo:
  this
  else:
01:07 that
  not forgetting tab indenting
 KALEL: good
 me: if you get another nested if, more indenting
 KALEL: This is very good
01:08 The guy does not get lost
 me: let's define a function
 KALEL: And you don't need to worry about non-practical details
 me: def fact( n ):
  if n < 2: return 1
  else:
  return n+fact(n-1)
01:10 you can, of course, write a program in a text file and import it
01:11 KALEL: good
  Guess I can already solve an Euler
01:12 me: say
  type
  def fact( n ):
"""Factorial of n"""
if n < 2:
return 1
else:
return n*fact(n-1)
  into a text file and save as euler.py
01:13 notice documentation as part of a function
01:14 KALEL: Just did it.
  Opened a python instance
01:15 me: still open?
 KALEL: Yes
 me: so, type this
  euler.fact( 5 )
01:16 KALEL: FAIL
 me: what?
 KALEL: typed on the shell and didnt recongnize
 me: import euler
  now should go
  euler.fact(5)
01:22 KALEL: Now it works!
 me: I forgot the import, no? :P
  hahahaha
 KALEL: yep
 me: sorry
 KALEL: Will get more coffee
 me: huhauhua, javaman
01:24 KALEL: Got coke
 me: good
 KALEL: while is in the same vein???
01:26 me: i = 1
  while i<10:
... print i
... i += 1
  without the dots, sure
01:30 KALEL: any mod function??
01:32 me: %
  3%2
 KALEL: Yes, thanks... I looked up on help
01:33 Any easy way to sum var to itself + new result??? say: sp = sp + p1
01:34 me: sp += 3
 KALEL: sp +=p1
  That?
 me: yep
 KALEL: Awesome.
01:35 me: converting a previous problem to Python? :)
 KALEL: No... Solving the second one
 me: ok
01:39 KALEL: how to end while???
  indenting???
  I mean, how do I close the block?
01:40 Forget
 me: ident all commands inside while
  ok
  ending it is newline, plainly
01:43 KALEL: finally
  Very good Phyton
  GOT IT!!
 me: way to go
  very easy, ain't it?
01:44 nothing like removing away everything non-essential
 KALEL: while sp<=400:
p1=p1+p2
if p1%2 == 0: sp += p1
p2=p2+p1
if p2%2 == 0: sp += p2
 me: hehe
 KALEL: of course, defining p1 = 2
  p1 = 1 and p2 = 2
 me: I was worried you would use = to compare
 KALEL: sp = 2
 me: exactly
 KALEL: a bug
01:45 me: you don't even need a fib function
 KALEL: Then I remembered = was used for attribution
 me: hehe
  common mistake among Pascal programmers
 KALEL: COOL
 me: not even in need for OO too! :O
  haha
01:46 KALEL: too much...
  Used OOPS!!
 me: OOPS?
 KALEL: OOPS
01:47 you try, if it fails: OOPS!
01:50 KALEL: Thank you.
  I'll continue studying... you wait for me... now the force if with me 
too.

 me: we're right here
  huhauhuha
01:51 nice, young Padawan
01:52 KALEL: Thanks, Jedi Master
01:55 me: BTW, my solution will make you go nuts:
  def p2():
print "Find the sum of all the even-valued terms in the Fibonacci 
sequence which do not exceed 4 million."

sum = 0
fib = 2
pfib = 1
while (fib<400):
if fib%2==0: sum+=fib
fib, pfib = fib+pfib,fib
return sum
01:56 check out the excellent variables swap near the last line... :)
01:57 KALEL: AWESOME!
 me: yeah, no need for intermediary temp variables
01:58 I see you have a strange gleaming in the eyes, young disciple
  :)
01:59 KALEL: huah huah huah
02:01 me: you may also reload a module after each change
  reload( euler )

--
a game sig: http://tinyurl.com/d3rxz9
--
http://mail.python.org/mailman/list

Some test fail on my new Python 2.6

2009-04-06 Thread R. David Murray
Sorin Schwimmer  wrote:
> I just downloaded and compiled Python 2.6 on a Gentoo Linux, IBM NetVista.
> 
> After going through the usual steps (./configure, make), I ran a test (make 
> test), and got some unexpected issues, which are detailed here:
> 
> # ./python Lib/test/test_tcl.py
> Traceback (most recent call last):
>   File "Lib/test/test_tcl.py", line 6, in 
> from Tkinter import Tcl
>   File "/install/Python-2.6.1/Lib/lib-tk/Tkinter.py", line 39, in 
> import _tkinter # If this fails your Python may not be configured for Tk
[...] 
> Anybody has a quick fix?

Yep.  Run 'make test' instead of running them one by one.  make test
will run them under regrtest, which has mechanisms for detecting tests
that are expected to or may reasonably fail on a given platform.  Then if
you still have errors, report back :)

--
R. David Murray

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


How can I change size of GUI?

2009-04-06 Thread Muddy Coder
Hi Folks,

I copied code from book:

class ScrolledText(Frame):
def __init__(self, parent=None, text='', file=None):
Frame.__init__(self, parent)
self.pack(expand=YES, fill=BOTH)
self.makeWidgets()
self.settext(text, file)
def makeWidgets(self):
sbar = Scrollbar(self)
text = Text(self, relief=SUNKEN, width=120)
sbar.config(command=text.yview)
text.config(yscrollcommand=sbar.set)
sbar.pack(side=RIGHT, fill=Y)
text.pack(side=LEFT, expand=YES, fill=BOTH)
self.text = text

It works, of course. But, the GUI is small, and I want to enlarge it.
I tried to add in options of width=120 for Text(), but it did not
work. Can somebody drop me a couple of lines for help? Thanks!

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


New online docs broken?

2009-04-06 Thread Jim Garrison

I notice the online docs (at docs.python.org/3.0/index.html) were
updated today.  It seems some of the top-level pages, like
Tutorial, "Using Python", "Language Reference" are truncated
after the first few paragraphs.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Eval Problem

2009-04-06 Thread J. Clifford Dyer
On Mon, 2009-04-06 at 15:11 -0400, Victor Subervi wrote:
> Hi:
> I have this code:
> 
> x = 1
> while x <= bitties:
>   file = open(p + str(x) + ".txt")
>   for line in file:
> print line
>   print eval(bits[x - 1])
>   x += 1
> 
> which throws this error:
> 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: Traceback (most recent call
> last): 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: File
> "/usr/lib64/python2.4/site-packages/mod_python/apache.py", line 299,
> in HandlerDispatch\n result = object(req) 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: File
> "/usr/lib64/python2.4/site-packages/mod_python/cgihandler.py", line
> 96, in handler\n imp.load_module(module_name, fd, path, desc) 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: File
> "/var/www/vhosts/articles.13gems.com/httpdocs/index_frame.py", line
> 89, in ?\n print eval(bits[1]) 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: File "", line 1 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: tableBottom(348,180) 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: ^ 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: SyntaxError: invalid syntax
> 

Hmm.  That's not the problem I get.  For me, your code raises:
"NameError: name 'bitties' is not defined"

It's easier for us to help you if you present a self-contained piece of
code that exhibits the problem you've encountered.  Creating that code
will often reveal the problem with the original code, and you will have
solved your own problem.

The other problem with your code is that you are using eval.  Eval leads
to difficult-to-debug errors, and is usually unnecessary, given the
dynamic nature of python.  If you need to access a class method using a
string, you can use getattr.  If you need to dynamically select a
function based on some condition known in another variable, you can use
a dictionary to hash into the function.  

It's hard to say what might be appropriate for your situation, because
your code fragment is so... fragmentary.

Cheers,
Cliff


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


Re: extract Infobox contents

2009-04-06 Thread Rhodri James
On Mon, 06 Apr 2009 23:12:14 +0100, Anish Chapagain  
 wrote:



Hi,
I was trying to extract wikipedia Infobox contents which is in format
like given below, from the opened URL page in Python.

{{ Infobox Software
| name   = Bash
| logo   = [[Image:bash-org.png|165px]]
| screenshot = [[Image:Bash demo.png|250px]]
| caption= Screenshot of bash and [[Bourne shell|sh]]
sessions demonstrating some features
| developer  = [[Chet Ramey]]
| latest release version = 4.0
| latest release date= {{release date|mf=yes|2009|02|20}}
| programming language   = [[C (programming language)|C]]
| operating system   = [[Cross-platform]]
| platform   = [[GNU]]
| language   = English, multilingual ([[gettext]])
| status = Active
| genre  = [[Unix shell]]
| source model   = [[Free software]]
| license= [[GNU General Public License]]
| website= [http://tiswww.case.edu/php/chet/bash/
bashtop.html Home page]
}} //upto this line

I need to extract all data between {{ Infobox ...to }}

Thank's if anyone can help,
am trying with

s1='{{ Infobox'
s2=len(s1)
pos1=data.find("{{ Infobox")
pos2=data.find("\n",pos2)

pat1=data.find("}}")

but am ending up getting one line at top only.


How are you getting your data?  Assuming that you can arrange to get
it one line at a time, here's a quick and dirty way to extract the
infoboxes on a page.

infoboxes = []
infobox = []
reading_infobox = False

for line in feed_me_lines_somehow():
if line.startswith("{{ Infobox"):
reading_infobox = True
if reading_infobox:
infobox.append(line)
if line.startswith("}}"):
reading_infobox = False
infoboxes.append(infobox)
infobox = []

You end up with 'infoboxes' containing a list of all the infoboxes
on the page, each held as a list of the lines of their content.
For safety's sake you really should be using regular expressions
rather than 'startswith', but I leave that as an exercise for the
reader :-)

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


Web validation

2009-04-06 Thread r-w

If no internet connection:
if have files:
run anyway, with warning
else:
ERROR
else:
if error getting hash/files:
if have files:
run anyway, with warning
else:
ERROR
else:
run
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >