Re: [Tutor] unittest and private methods

2006-01-20 Thread lemeia
kent wrote:
> > I currently have a class where I have quite a few private methods.
> > One
> of these methods is a small general utility that extracts characters
> from provided text up until it reaches a delimiter. Something like
> extractTo(self, text, delimiter).
> > 
> > It's not a relevant interface to the class, just a utility for the
> class' public methods, which is why I've made it private. That seems to
> be a logical decision to me.
> 
> Yes, that is good reasoning. When you say it is private, do you mean the 
> name starts with two underscores?
> 
Yes - it is __extractTo(self, text, delimiter)

> 
> > However, the public methods within the class that call it currently
> only do so to extract numeric events from the text. extractTo is a
> little more generalised than that and so I decided that I would use
> unittest to build test cases for the extractTo method and exercise it to
> its fullest in case the class requires its extended functionality in the
> future.
> > 
> > You'll probably see what my problem is. I can't exercise this method
> directly from an external test case as the method is not part of the
> class' public interface. If I call a method in the interface that
> currently utilises this private method I'm not testing the method as
> fully as I would like.
> 
> I'm not sure I understand this part. You mean the private methods have 
> capabilities that can't currently be exercised through the public 
> interface? That smells of "Speculative Generality" - writing code 
> because you think you might need it later. If that is the case the best 
> answer might be to rip it out and wait until you need it.
> > 
> 
I like the term Speculative Generality - never heard that before. If the method 
was already extremely complicated I would agree that you should only alter when 
needed. However, this method is simpler in design and I don't see the harm in 
generalising in this particular case. Once I'd made the decision to generalise 
it I consequently saw several worthwhile refactoring possibilities in my class, 
which I shall probably exercise a little later.

> Now I can build arbitrary interfaces into my class just for testing
> this method but that sounds like a messy way to maintain a program.
> Additionally, I could make this method public, but that also seems like
> an unsatisfactory solution.
> > 
> > The example isn't so important to my question, but I wanted to give
> some context before asking..
> > 
> > Is there a way to specifically target and unit test private methods
> > in
> a class via unittest without testing them through the class' public
> interface?
> 
> Python's notion of privacy is pretty weak. I would have no problem with 
> a unit test that called single-underscore methods in the class under 
> test. Unit test purists object to this, saying a class should only be 
> tested through its public interface, but "practicality beats purity" in 
> my mind.
> 
> Rereading your mail another possibility comes to mind. It sounds like 
> extractTo() is a general-purpose function that is used for a specific 
> purpose by the module you are testing. Why not extract extractTo to a 
> utility module? Then you can test it as much as you like in the unit 
> tests for the utility and you can reuse it in other code if the need 
> arises. Almost all of my projects have 'util' packages containing 
> collections of miscellaneous useful stuff. The utils have their own unit 
> tests and get included as needed in other modules and projects.
> 
> This makes sense to me and I hadn't thought of that in all honesty. I get so 
> involved in object orientated design, I sometimes forget about the 
> convenience of a few general functions to "help out". Would I lose my OO 
> membership card though? 

I think I might stick to the public interface testing wherever possible and I 
have done so with other private methods in my class. This particular one just 
seemed to benefit from direct testing.

I'm really enjoying Python. I don't like the "sentence structure" quite as much 
as Smalltalk (ie I like anObject actOn: a untilYouSee: b), but there's 
something quite friendly about its syntax all the same. I find it much faster 
to do things in Python even over Visual Basic. I really like developing using 
OO and so far, Python has been the easiest to adopt (besides Smalltalk 
perhaps). For some reason I struggled with OO in C++ when I looked into it and 
Java always seemed to be a big red stop sign for me (not sure why).

Patrick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Is this overkill?

2006-01-20 Thread Bradly McConnell
Greetings all:

I'm new to Python, and have come across and exercise that basically
counts to 100.  The idea is to accept user input for an initial
number, and then let the user add additional numbers.  I wanted to
give feedback if a number selected would bring the total above 100, so
the user would select a lower number.  It looks like I have it
working, but it doesn't seem very efficient.  I would like any hints,
help, or comments that you can provide.  Below is what I have so far.

number = input("Please enter a number: ")
while number != 100:
additional_number = input("Please enter an additional number: ")
if additional_number + number > 100:
lower_number = input("please enter a lower number: ")
if lower_number + number > 100:
lower_number = input("Lower!")
else:
number = lower_number + number
elif additional_number + number < 100:
number = additional_number + number
else:
continue

print "Done", number


Brad
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python-based system programming and admin?

2006-01-20 Thread Neal McBurnett
I'm an experienced linux guy, with lots of python interest and some
python experience.

I'm helping a colleague develop software to run on our linux server.
He has python skill, but doesn't know the shell or linux very well at
all.

I'd like to give him a secure, safe, flexible development environment
on the serve, (which does audio streaming and other fun things).

At the moment, he has an account and can connect from his mac via ssh,
and copy files back and forth (ftp-like stuff - I forget which ssh
client).  But he doesn't want to log in to a bash shell and learn a
whole new way to do things.  But he does want to run programs.

Editing python scripts spawned by cron is one of the goals.
But developing them by waiting for cron to fire and send the output
via email is pretty painful

The server currently doesn't need to run a web server, and I'm
reluctant to introduce new services that have much security risk
associated with them, but something like that seems like a
possibility.

One idea that just popped in my brain is to give him a python login
shell on linux - any advice on how to do that?

Other possibilities, I guess:

 - An https-based web server with and mod-python, somehow configured
   so that his jobs run as him.

 - a pure-python server (via twisted?) running as him

 - moinmoin or the like

 - zope or plone (not sounding very simple any more, but I've done a
   bit of this )

What would be the safest, simplest solution that was adequate for
providing a reasonable development environment?

Any ideas I haven't thought of yet?

Cheers,

Neal McBurnett http://bcn.boulder.co.us/~neal/
Signed and/or sealed mail encouraged.  GPG/PGP Keyid: 2C9EBA60
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Doubt with classes

2006-01-20 Thread Kent Johnson
Edgar Antonio Rodríguez Velazco wrote:
> Hi everybody,
> I've been reading the chapter of classes of Byte of Python by Swaroop. 
> There's an example with classes (11.4) that is below:

The example is depending on Person.__del__() being called on swaroop and 
kalam when the intepreter exits. The Python Language Reference 
documentation on __del__() says, "It is not guaranteed that __del__() 
methods are called for objects that still exist when the interpreter 
exits." So this program is relying on behaviour that may change between 
different versions of Python.

Try adding
   del swaroop
   del kalam
to the end of the program, this should force the __del__() method to be 
called (at least in CPython, in Jython it still may not be called).

Kent

>  
> #
> class Person:
> '''Represents a person.'''
> population = 0
> 
> def __init__(self, name):
> '''Initializes the person's data.'''
> self.name = name
> print '(Initializing %s)' % self.name
> 
> # When this person is created, he/she
> # adds to the population
> Person.population += 1
> 
> def __del__(self):
> '''I am dying.'''
> print '%s says bye.' % self.name
> 
> Person.population -= 1
> 
> if Person.population == 0:
> print 'I am the last one.'
> else:
> print 'There are still %d people left.' % Person.population
> 
> def sayHi (self):
> '''Greeting by the person.
> 
> Really, that's all it does.'''
> print 'Hi, my name is %s.' % self.name
> 
> def howMany(self):
> '''Prints the current population.'''
> if Person.population == 1:
> print 'I am the only person here.'
> else:
> print 'We have %d persons here.' % Person.population
> 
> swaroop = Person('Swaroop')
> swaroop.sayHi()
> swaroop.howMany()
> 
> kalam = Person('Abdul Kalam')
> kalam.sayHi()
> kalam.howMany()
> 
> swaroop.sayHi()
> swaroop.howMany()
>  
> #
> EXAMPLE'S OUTPUT (Output shown in the example)
>  
> $ python objvar.py
> (Initializing Swaroop)
> Hi, my name is Swaroop.
> I am the only person here.
> (Initializing Abdul Kalam)
> Hi, my name is Abdul Kalam.
> We have 2 persons here.
> Hi, my name is Swaroop.
> We have 2 persons here.
> Abdul Kalam says bye.
> There are still 1 people left.
> Swaroop says bye.
> I am the last one.
> ##
> MY OUTPUT
>  
> (Initializing Swaroop)
> Hi, my name is Swaroop.
> I am the only person here.
> (Initializing Abdul Kalam)
> Hi, my name is Abdul Kalam.
> We have 2 persons here.
> Hi, my name is Swaroop.
> We have 2 persons here.
> ##
>  
> I have runned the script in both Linux and Windows and got the same 
> result. Could you explain me what's wrong with this???
> 
> -- 
> Edgar A. Rodriguez
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Doubt with classes

2006-01-20 Thread Edgar Antonio Rodríguez Velazco
Hi everybody,
I've been reading the chapter of classes of Byte of Python by Swaroop. There's an example with classes (11.4) that is below:
 
#
class Person:'''Represents a person.'''population = 0
def __init__(self, name):'''Initializes the person's data.'''self.name = name
print '(Initializing %s)' % self.name# When this person is created, he/she
# adds to the populationPerson.population += 1def __del__(self):
'''I am dying.'''print '%s says bye.' % self.namePerson.population -= 1
if Person.population == 0:print 'I am the last one.'
else:print 'There are still %d people left.' % Person.populationdef sayHi
(self):'''Greeting by the person.Really, that's all it does.'''print 'Hi, my name is %s.'
 % self.namedef howMany(self):'''Prints the current population.'''
if Person.population == 1:print 'I am the only person here.'
else:print 'We have %d persons here.' % Person.populationswaroop = Person('Swaroop')swaroop.sayHi()
swaroop.howMany()kalam = Person('Abdul Kalam')kalam.sayHi()kalam.howMany()swaroop.sayHi()swaroop.howMany() 
#
EXAMPLE'S OUTPUT (Output shown in the example)
 
$ python objvar.py(Initializing Swaroop)Hi, my name is Swaroop.I am the only person here.(Initializing Abdul Kalam)Hi, my name is Abdul Kalam.We have 2 persons here.Hi, my name is Swaroop.
We have 2 persons here.Abdul Kalam says bye.There are still 1 people left.Swaroop says bye.I am the last one.
##
MY OUTPUT
 
(Initializing Swaroop)Hi, my name is Swaroop.I am the only person here.(Initializing Abdul Kalam)Hi, my name is Abdul Kalam.We have 2 persons here.Hi, my name is Swaroop.We have 2 persons here.

##
 
I have runned the script in both Linux and Windows and got the same result. Could you explain me what's wrong with this???
-- Edgar A. Rodriguez
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Quoting trouble

2006-01-20 Thread Kent Johnson
Marilyn Davis wrote:
> significant_headers[each] = 
> text[len(each):].strip().replace('"','\\"').replace('\n',' ')

This looks like you are trying to make an escaped string, with literal 
backslashes. You might prefer using the 'string_escape' codec:
  >>> '\n'.encode('string_escape')
'\\n'

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Quoting trouble

2006-01-20 Thread Marilyn Davis
On Fri, 20 Jan 2006, Python wrote:

> On Fri, 2006-01-20 at 13:50 -0800, Marilyn Davis wrote:
> > for each in significant_headers.keys():
> > this = '''self.h_%s = "%s"''' % \
> >(each[:-2].lower().replace('-','_'),
> > repr(significant_headers[each]))
> > exec(this)
> 
> So you are using exec to process an assignment statement.  The setattr
> builtin function will do what you want.
> 
> http://docs.python.org/lib/built-in-funcs.html#l2h-64
> 
> so your code could wind up looking something like:
> 
> setattr( self,
> 'h_' + each[:-2].lower().replace('-','_'),
> significant_headers[each]
> )
> 
> Looking at that code, you can pull the key and value from
> significant_headers in the for statement.  That is:
> 
> for key,val in significant_headers.items():
> setattr( self,
> 'h_' + key[:-2].lower().replace('-','_'),
> val
> )
> 
> Now the line that actually determines the attribute name looks pretty
> ugly.  I would recommend writing a function to replace that operation
> with an understandable function name (perhaps key2name).  That would
> result in:
> setattr(self, key2name(key), val)

Very nice indeed!

> 
> 
> Hope this helps.  The ease with which Python allows you to attach names
> to values is one of the great features of the language.

Yes.  Thank you so much.  I'll fix it and I'm glad to have it in my
bag of tricks.

Marilyn

> 
> 

-- 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Quoting trouble

2006-01-20 Thread Python
On Fri, 2006-01-20 at 13:50 -0800, Marilyn Davis wrote:
> for each in significant_headers.keys():
> this = '''self.h_%s = "%s"''' % \
>(each[:-2].lower().replace('-','_'),
> repr(significant_headers[each]))
> exec(this)

So you are using exec to process an assignment statement.  The setattr
builtin function will do what you want.

http://docs.python.org/lib/built-in-funcs.html#l2h-64

so your code could wind up looking something like:

setattr( self,
'h_' + each[:-2].lower().replace('-','_'),
significant_headers[each]
)

Looking at that code, you can pull the key and value from
significant_headers in the for statement.  That is:

for key,val in significant_headers.items():
setattr( self,
'h_' + key[:-2].lower().replace('-','_'),
val
)

Now the line that actually determines the attribute name looks pretty
ugly.  I would recommend writing a function to replace that operation
with an understandable function name (perhaps key2name).  That would
result in:
setattr(self, key2name(key), val)


Hope this helps.  The ease with which Python allows you to attach names
to values is one of the great features of the language.

-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Quoting trouble

2006-01-20 Thread Marilyn Davis
On Fri, 20 Jan 2006, Kent Johnson wrote:

Thank you so much Kent.  This is *very* helpful.

> Marilyn Davis wrote:
> > Dear Tutors,
> > 
> > I'm having a problem and going around in circles.  I'm on Python 2.4.1.
> > 
> > This odd address line comes in email:  
> > 
> > 
> line = '''"ma >> \"[EMAIL PROTECTED]"" <[EMAIL PROTECTED]>'''
> > 
> > 
> > It makes sense to me, and to the MTA.  I want to use it to create a
> > variable:
> > 
> > 
> exec('h_to = %s' % line)
> > 
> > Traceback (most recent call last):
> >   File "", line 1, in ?
> >   File "", line 1
> > h_to = "ma >> "[EMAIL PROTECTED]"" <[EMAIL PROTECTED]>
> >  ^
> > SyntaxError: invalid syntax
> > 
> > So, how do I keep the \" in there?
> 
> Why are you using exec? What's wrong with
>h_to = line
> ??

Explanation below.

> 
> If you really need the exec then you need to put some quotes in there 
> somewhere, either
>exec('h_to = '''%s''' ' % line)

>>> exec('h_to = '''%s''' % line ')
...

It seems to want more.

> or
>exec('h_to = %s' % repr(line))

This one does!

>>> exec('h_to = %s' % repr(line))
>>> h_to
'"ma >> "[EMAIL PROTECTED]"" <[EMAIL PROTECTED]>'

Why I'm using exec:

I'm using exec so that I have a flexible scheme for collecting
headers.  Maybe you have a better suggestion?

I have a dictionary that starts:

significant_headers = {
'Cc: ':None,
'CC: ':None,
'From: ':None,
'Return-path: ':None,
'Sender: ':None,
'Subject: ':None,
'To: ':None}
and
heads_to_find = significant_headers.keys()

So I collect the header texts into the dictionary items:

for each in heads_to_find:
if not significant_headers[each] \
   and text.startswith(each):
significant_headers[each] = 
text[len(each):].strip().replace('"','\\"').replace('\n',' ')
break

And then later, I make variables that match the variables that exim,
my MTA, uses.  For each header in the message exim provides a $h_to,
$h_from, etc.  I like my variable names to match exim's as much as
possible:

for each in significant_headers.keys():
this = '''self.h_%s = "%s"''' % \
   (each[:-2].lower().replace('-','_'),
repr(significant_headers[each]))
exec(this)

If I decide that I need to collect another header, I can just add it
to my dicitonary.

And now, with your help, I don't get any exceptions ... so far.

But, if there's another way to do this, I'd appreciate any improvement.

Thank you so much.

Marilyn




> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Quoting trouble

2006-01-20 Thread Kent Johnson
Marilyn Davis wrote:
> Dear Tutors,
> 
> I'm having a problem and going around in circles.  I'm on Python 2.4.1.
> 
> This odd address line comes in email:  
> 
> 
line = '''"ma >> \"[EMAIL PROTECTED]"" <[EMAIL PROTECTED]>'''
> 
> 
> It makes sense to me, and to the MTA.  I want to use it to create a
> variable:
> 
> 
exec('h_to = %s' % line)
> 
> Traceback (most recent call last):
>   File "", line 1, in ?
>   File "", line 1
> h_to = "ma >> "[EMAIL PROTECTED]"" <[EMAIL PROTECTED]>
>  ^
> SyntaxError: invalid syntax
> 
> So, how do I keep the \" in there?

Why are you using exec? What's wrong with
   h_to = line
??

If you really need the exec then you need to put some quotes in there 
somewhere, either
   exec('h_to = '''%s''' ' % line)
or
   exec('h_to = %s' % repr(line))

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Starbucks does not use two-phase commit

2006-01-20 Thread Danny Yoo


On Fri, 20 Jan 2006, Bernard Lebel wrote:

> So have written a little test script. The fact is that I want to be able
> to manage the same queue from separate threads. Below is an example of
> what my real program is doing:


Hi Bernard,

One problem is that there's a single outputQueue being presented to get
results back from the Server.

A different approach is to use a lot of outputQueues.  *grin*

The idea is that when we send a job submission, we immediately get back a
"ticket".  We can then use this ticket to claim() our result.  Each ticket
is unique to a job submission, so we shouldn't see any bleeding going on
between clients.


Here's some code that implements this idea.  It's a little complex, so you
may want to read through it slowly:



from threading import Thread
from Queue import Queue


class Ticket:
"""A small token that we can use to claim our result."""
def __init__(self, q):
self.q = q
self.result = None
self.done = False

def claim(self):
if not self.done:
self.result = self.q.get()
self.done = True
return self.result


class Server:
_QUIT_JOB = ['Quit!']

def __init__(self):
"""A queue will contain 2-tuples of (job, outputQueue)
elements."""
self.queue = Queue()


def startServer(self):
"""Brings the server online."""
Thread(target=self._jobLoop).start()


def schedule(self, job):
"""Schedules a job to be done and returns a ticket that the
client can use later to claim the result of the job."""
outputQueue = Queue()
self.queue.put((job, outputQueue))
return Ticket(outputQueue)


def scheduleShutdown(self):
"""Add a 'job' that shuts the system down."""
self.queue.put((Server._QUIT_JOB, None))


def _jobLoop(self):
"""Continue looping through tasks."""
while True:
print "Looping..."
(nextJob, outputQueue) = self.queue.get()
if nextJob is Server._QUIT_JOB:
return
returnValue = self._doJob(nextJob)
outputQueue.put(returnValue)


def _doJob(self, job):
print "I'm doing", job
return job + job ## something to show that we're doing something



def separateCaller(server):
for i in range(1000, 1004 + 1):
print "--Separate caller asks %d" % i
ticket = server.schedule(str(i))
print "--Separate caller got %s" % ticket.claim()


if __name__ == '__main__':
server = Server()
server.startServer()
Thread(target=separateCaller, args=(server,)).start()

result1 = server.schedule("1")
print "result1: %s" % result1.claim()
result2 = server.schedule("2")
print "result2: %s" % result2.claim()
result3 = server.schedule("3")
print "result3: %s" % result3.claim()
server.scheduleShutdown()
#


Play around with this a bit and see if it makes sense to you.  You might
also be interested in the amusing article "Starbucks Does Not Use
Two-Phase Commit":

http://www.eaipatterns.com/ramblings/18_starbucks.html

as it touches on concepts in the code.


If you have more questions, please feel free to ask!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Quoting trouble

2006-01-20 Thread Marilyn Davis
Dear Tutors,

I'm having a problem and going around in circles.  I'm on Python 2.4.1.

This odd address line comes in email:  

>>> line = '''"ma >> \"[EMAIL PROTECTED]"" <[EMAIL PROTECTED]>'''

It makes sense to me, and to the MTA.  I want to use it to create a
variable:

>>> exec('h_to = %s' % line)
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 1
h_to = "ma >> "[EMAIL PROTECTED]"" <[EMAIL PROTECTED]>
 ^
SyntaxError: invalid syntax

So, how do I keep the \" in there?

I've tried a bunch of things but can't find anything that works.

Heeelp.  Please.

Thank you.

Marilyn Davis


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] glibc error while Python script runs - Solved

2006-01-20 Thread Bernard Lebel
Hi Danny,

So have written a little test script.
The fact is that I want to be able to manage the same queue from
separate threads. Below is an example of what my real program is
doing:



from threading import Thread
from Queue import Queue
import time

class SQLServer:

def __init__( self ):
self.queueinput = Queue()
self.queueoutput = Queue()


def query( self, sInput ):

# Puts an item to the input queue
self.queueinput.put( sInput )

# Wait for input queue to output result
while True:

# Get output from output queue
sOutput = self.queueoutput.get()

# Return result to caller
return sOutput



def shutdownOnIdle( self ):
self.queueinput.put( "QUIT!" )



# SERVER
def serveSqlQueue( self ):

while True:

# Get a job from the input queue
sQuery = self.queueinput.get()

# Check if server has to quit
if sQuery == "QUIT!": return

print 'Server does %s' % sQuery

self.queueoutput.put( sQuery )



# START THE SERVER
def startServer( self ):
Thread( target = self.serveSqlQueue ).start()





def separateCaller():
i = 1000
while True:
print '--Separate caller asks %i' % i
oSeparateOutput = server.query( i )
print '--Separate caller got %s' % str(oSeparateOutput)
i += 1
if i == 1004:
break




if __name__ == '__main__':

# Instantiate the server class
server = SQLServer()



# Start the server
server.startServer()

# Start the separate thread
Thread( target = separateCaller ).start()



print 'Main thread asks %s' % 'a'
oMainOutput = server.query( 'a' )
print 'Main thread got %s' % str(oMainOutput)

print 'Main thread asks %s' % 'b'
oMainOutput = server.query( 'b' )
print 'Main thread got %s' % str(oMainOutput)

print 'Main thread asks %s' % 'c'
oMainOutput = server.query( 'c' )
print 'Main thread got %s' % str(oMainOutput)

server.shutdownOnIdle()




When I run this, I get this output:

Main thread asks a
--Separate caller asks 1000
Server does 1000
--Separate caller got 1000
--Separate caller asks 1001
Server does a
--Separate caller got a
--Separate caller asks 1002
Server does 1001
--Separate caller got 1001
--Separate caller asks 1003
Server does 1002
--Separate caller got 1002
Server does 1003
Main thread got 1003
Main thread asks b
Server does b
Main thread got b
Main thread asks c
Server does c
Main thread got c


As you can see, the main thread and the separateCaller function have
mixed results. How can I "synchronize" the queues?



Thanks
Bernard










On 1/19/06, Bernard Lebel <[EMAIL PROTECTED]> wrote:
> Thanks a lot Danny,
>
> That certainly does make sense. I'll look into implementing the Queue
> approach in my program tomorrow. I remember you recommending me this
> module as well not long ago, although in a different discussion (where
> I suspected problem with file access from multiple thread, but I guess
> it's pretty much a similar problem, isn't it? :-)
>
>
> Thanks again
> Bernard
>
>
>
> On 1/19/06, Danny Yoo <[EMAIL PROTECTED]> wrote:
> >
> >
> > On Thu, 19 Jan 2006, Kent Johnson wrote:
> >
> >
> > > In your original desing were you sharing a connection between threads?
> > > That could cause trouble. But if each connection has its own thread and
> > > you are using transactions and isolation levels appropriately, they
> > > shouldn't stomp on each other.
> >
> > Hi Kent and Bernard,
> >
> > It's possible that MySQLdb was linked against the non-thread-safe
> > mysqlclient (rather than mysqlclient_r) library; that would probably cause
> > havoc.
> >
> >
> > > > So the solution was to start some sort of queue server in a separate
> > > > thread. This queue would consist of a list, and each time the program
> > > > would want to perform a MySQL operation, it would add it to the queue.
> >
> > We may want to use the Queue module here instead of a list; the way the
> > program is described now sounds like the server is busy-spinning when it
> > looks for new things to do.  The thing that makes busy-spinning slightly
> > not-nice is that it consumes CPU regardless if the system

Re: [Tutor] unittest and private methods

2006-01-20 Thread Kent Johnson
lemeia wrote:
> I currently have a class where I have quite a few private methods.
> One
of these methods is a small general utility that extracts characters
from provided text up until it reaches a delimiter. Something like
extractTo(self, text, delimiter).
> 
> It's not a relevant interface to the class, just a utility for the
class' public methods, which is why I've made it private. That seems to
be a logical decision to me.

Yes, that is good reasoning. When you say it is private, do you mean the 
name starts with two underscores?
> 
> However, the public methods within the class that call it currently
only do so to extract numeric events from the text. extractTo is a
little more generalised than that and so I decided that I would use
unittest to build test cases for the extractTo method and exercise it to
its fullest in case the class requires its extended functionality in the
future.
> 
> You'll probably see what my problem is. I can't exercise this method
directly from an external test case as the method is not part of the
class' public interface. If I call a method in the interface that
currently utilises this private method I'm not testing the method as
fully as I would like.

I'm not sure I understand this part. You mean the private methods have 
capabilities that can't currently be exercised through the public 
interface? That smells of "Speculative Generality" - writing code 
because you think you might need it later. If that is the case the best 
answer might be to rip it out and wait until you need it.
> 
> Now I can build arbitrary interfaces into my class just for testing
this method but that sounds like a messy way to maintain a program.
Additionally, I could make this method public, but that also seems like
an unsatisfactory solution.
> 
> The example isn't so important to my question, but I wanted to give
some context before asking..
> 
> Is there a way to specifically target and unit test private methods
> in
a class via unittest without testing them through the class' public
interface?

Python's notion of privacy is pretty weak. I would have no problem with 
a unit test that called single-underscore methods in the class under 
test. Unit test purists object to this, saying a class should only be 
tested through its public interface, but "practicality beats purity" in 
my mind.

Rereading your mail another possibility comes to mind. It sounds like 
extractTo() is a general-purpose function that is used for a specific 
purpose by the module you are testing. Why not extract extractTo to a 
utility module? Then you can test it as much as you like in the unit 
tests for the utility and you can reuse it in other code if the need 
arises. Almost all of my projects have 'util' packages containing 
collections of miscellaneous useful stuff. The utils have their own unit 
tests and get included as needed in other modules and projects.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] unittest and private methods

2006-01-20 Thread lemeia
I currently have a class where I have quite a few private methods. One of these 
methods is a small general utility that extracts characters from provided text 
up until it reaches a delimiter. Something like extractTo(self, text, 
delimiter).

It's not a relevant interface to the class, just a utility for the class' 
public methods, which is why I've made it private. That seems to be a logical 
decision to me.

However, the public methods within the class that call it currently only do so 
to extract numeric events from the text. extractTo is a little more generalised 
than that and so I decided that I would use unittest to build test cases for 
the extractTo method and exercise it to its fullest in case the class requires 
its extended functionality in the future.

You'll probably see what my problem is. I can't exercise this method directly 
from an external test case as the method is not part of the class' public 
interface. If I call a method in the interface that currently utilises this 
private method I'm not testing the method as fully as I would like.

Now I can build arbitrary interfaces into my class just for testing this method 
but that sounds like a messy way to maintain a program. Additionally, I could 
make this method public, but that also seems like an unsatisfactory solution.

The example isn't so important to my question, but I wanted to give some 
context before asking..

Is there a way to specifically target and unit test private methods in a class 
via unittest without testing them through the class' public interface?

I hope I've made some sense there.

Patrick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get terminal settings

2006-01-20 Thread Vincent Zee
On Thursday, 19 January 2006 at 23:53:06 -, Alan Gauld wrote:
> Assuming you are on a Unix style OS/terminal you can read the 
> output of stty. Look at the rows value
> 
> Here are a couple of runs of rows at different terminal sizes:
> 
> $ stty -a | grep rows
> speed 38400 baud; rows 41; columns 80; line = 0;
> 
> $ stty -a | grep rows
> speed 38400 baud; rows 26; columns 80; line = 0;
> 
> A call to Popen should get you what you want.
> 
> An alternative technique, and the one useed by the real 
> more/less programmes is to use curses to open a display 
> window.
> 
Hi Alan,

this has helped me very much, thank you.
I'll use the popen() before venturing into curses land.

/\
Vincent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple MS Windows Shell Extension?

2006-01-20 Thread Alan Gauld
> I have a python script that I want my users to execute with a
> "Right-Mouse-Click" on a file under Windows XP.
>
> (If possible without changing the default "Open with ..." behavior from 
> Windows.)

Use Windows explorer to edit the options on that file type.
You can add new context menu items using the

Tools->FolderOptions->FileTypes

dialog tab.

For instance I have several options for a Python sdcript including
Edit  (in IDLE), View (in Scite)
in addition to the usual Open (run it), and Edit with Vim (guess!:-)
that are always there.

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] glibc error while Python script runs - Solved

2006-01-20 Thread Kent Johnson
Bernard Lebel wrote:
> On 1/19/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
> 
>>Hi Bernard,
>>
>>I'm glad you got it working but kind of surprised at what you had to do.
>>You shouldn't have to have a single thread to access the database. In
>>your original desing were you sharing a connection between threads? That
>>could cause trouble. But if each connection has its own thread and you
>>are using transactions and isolation levels appropriately, they
>>shouldn't stomp on each other.
> 
> 
> I'm not sure what you mean by transaction and isolation levels. I get
> some sort of an idea, but could you elaborate on the idea?

Transactions are helpful in two ways. One is to make a sequence of 
operations atomic - they either all succeed or all fail. A classic 
example is a banking transaction where you have to debit one account and 
credit another. You wouldn't want one request to succeed and the other 
fail - that would be disastrous. By wrapping the two operations in a 
transaction, the database ensures that they succeed or fail together.

The other thing is to isolate the intermediate steps from other database 
clients. You might not want a second client to see the database in the 
intermediate step. By setting the proper isolation level on the 
transaction the database will not make the changes visible to other 
clients until the transaction commits.

http://en.wikipedia.org/wiki/Database_transaction
http://en.wikipedia.org/wiki/ACID

> 
> I have not considered having one connection per thread, and have not
> tested the idea. The way it works right now is that there is only one
> connection, as a class instance attribute. All transaction go through
> that connection.

So you *were* sharing one connection between several threads before? 
ouch. I'm not surprised that that broke badly...

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get terminal settings

2006-01-20 Thread Vincent Zee
On Thursday, 19 January 2006 at 16:03:16 -0800, Terry Carroll wrote:
> On Thu, 19 Jan 2006, Vincent Zee wrote:
> 
> > Any pointers to other curses howto's?
> 
> There's http://heather.cs.ucdavis.edu/~matloff/Python/PyCurses.pdf ; but 
> it's mostly a couple of example programs, without a lot of explanation.

Hi Terry,

thank you for the link, I'll have a look.

/\
Vincent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor