[Tutor] Debugging multithreaded programs in python

2006-07-31 Thread Noufal Ibrahim
Greetings all,
A friend here is trying to debug a rather badly written python program
which spawns off lots of threads here and there.  Are there any
frameworks that I can reccommend that would ease his pain?

On a more general note, it seems rather strange that I can't quickly
google and find a debugger for multithreaded python programs. Python's
"batteries included" philosophy has always been very useful to me.
I've always managed to find what I need without any hassles. This is
the first exception. Any insights? Are there any decent ways to debug
a multi threaded program which make a debugger redundant?

Peace.
-- 
-NI

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


Re: [Tutor] question about headers and smtplib

2006-07-31 Thread Justin Ezequiel
When I first started with Python, I used MimeWriter to create E-mails.

Then some mail servers rejected my E-mails.

Some research (google) indicated (to me) that I needed a MIME-Version header.
(Can't recall now if I also needed a Content-Type header.)

Anyway, more research (Python docs) indicated that I should use the
email package instead.
I have been doing so since and have not had reports of anymore rejected E-mails.

Hope this helps.

>>> import email
>>> from email.MIMENonMultipart import MIMENonMultipart
>>> from email.Utils import formataddr
>>> format_addresses = lambda pairs: ', '.join([formataddr(pair) for
pair in pairs])
>>> msg = MIMENonMultipart('text', 'plain', charset='us-ascii')
>>> msg.set_payload('Foo Bar')
>>> msg.add_header('From', formataddr(('Justin', '[EMAIL PROTECTED]')))
>>> msg.add_header('To', format_addresses([('Justin', '[EMAIL PROTECTED]'),
('You', '[EMAIL PROTECTED]')]))
>>> print msg.as_string()
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
From: Justin <[EMAIL PROTECTED]>
To: Justin <[EMAIL PROTECTED]>, You <[EMAIL PROTECTED]>

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


Re: [Tutor] syntax error

2006-07-31 Thread Carroll, Barry
Hello Chris,

> -Original Message-
> Date: Mon, 31 Jul 2006 15:29:13 -0700 (PDT)
> From: Christopher Spears <[EMAIL PROTECTED]>
> Subject: [Tutor] syntax error
> To: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=iso-8859-1
> 
> My brain has gone squishy.  I am combining a linked
> list with a priority queue.  This is the last exercise
> out of How To Think Like A Computer Scientist (Chapter
> 19).
> 
<>
> 
>   def insert(self, cargo):
>   node = Node(cargo)
>   node.next = None
>   if self.length == 0:
>   # if list is empty, the new node is head and
last
>   self.head = self.last = node
>   elif node.cargo > self.head.cargo:
>   node.next = self.head
>   self.head = node
>   else node.cargo <= self.head.cargo:
>   self.last.next = node
>   self.last = node
>   self.length = self.length + 1
> 
<>
> 
> Here is the error message:
> >>> from linkedPriorityQueue import *
> Traceback (most recent call last):
>   File "", line 1, in ?
>   File "linkedPriorityQueue.py", line 27
> else node.cargo <= self.head.cargo:
> ^
> SyntaxError: invalid syntax
> 
> I am not sure what this means.  Everything is spelled
> correctly.
> 
> -Chris
> 

The syntax error occurs just after the "else".  Unlike "if" and "elif",
"else" does not take an expression. It covers any cases left over after
the "if" clause and all of the "elif" clauses have been tried and
failed.  The expression "node.cargo <= self.head.cargo" is therefore
logically unnecessary. Python (and all other programming languages I
know of) enforces this by making it illegal to put an expression in an
"else" clause.   

I don't have access to "How To Think Like A Computer Scientist", so I
can't point you to the section that discusses this, but I'm sure it's in
there somewhere.  Look in the index for "else clause", "if-elif-else
statement" or some entry like that.  

HTH.

Regards,
 
Barry
[EMAIL PROTECTED]
541-302-1107

We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed


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


Re: [Tutor] syntax error

2006-07-31 Thread Alan Gauld

> My brain has gone squishy.  

:-)  I know the feeling...

> Here is the error message:
 from linkedPriorityQueue import *
> Traceback (most recent call last):
>  File "", line 1, in ?
>  File "linkedPriorityQueue.py", line 27
>else node.cargo <= self.head.cargo:
>^
> SyntaxError: invalid syntax

else:

or 

elif condition:

but not 

else condition:

HTH,

Alan G.

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


Re: [Tutor] syntax error

2006-07-31 Thread John Fouhy
On 01/08/06, Christopher Spears <[EMAIL PROTECTED]> wrote:
> Traceback (most recent call last):
>  File "", line 1, in ?
>  File "linkedPriorityQueue.py", line 27
>else node.cargo <= self.head.cargo:
>^
> SyntaxError: invalid syntax
>
> I am not sure what this means.  Everything is spelled
> correctly.

An 'else' clause doesn't take a condition.  The general shape of an if
statement is:

if [condition]:
do something
elif [another condition]:
do something else
else:
do other things

The 'else' clause is the catchall for anything that doesn't match one
of the preceding conditions, so it doesn't have its own condition.

What I often like to do is to use a comment to indicate what is true
in the 'else' branch.  eg:

   else:  # node.cargo <= self.head.cargo
   self.last.next = node
   self.last = node

HTH!

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


[Tutor] syntax error

2006-07-31 Thread Christopher Spears
My brain has gone squishy.  I am combining a linked
list with a priority queue.  This is the last exercise
out of How To Think Like A Computer Scientist (Chapter
19).


class Node:
def __init__(self, cargo=None, next=None):
self.cargo = cargo
self.next = next

def __str__(self):
return str(self.cargo)

class ImprovedQueue:
def __init__(self):
self.length = 0
self.head   = None
self.last   = None

def isEmpty(self):
return (self.length == 0) 

def insert(self, cargo):
node = Node(cargo)
node.next = None
if self.length == 0:
# if list is empty, the new node is head and last
self.head = self.last = node
elif node.cargo > self.head.cargo:
node.next = self.head
self.head = node
else node.cargo <= self.head.cargo:
self.last.next = node
self.last = node
self.length = self.length + 1


def remove(self):
cargo = self.head.cargo
self.head = self.head.next
self.length = self.length - 1
if self.length == 0:
self.last = None
return cargo

Here is the error message:
>>> from linkedPriorityQueue import *
Traceback (most recent call last):
  File "", line 1, in ?
  File "linkedPriorityQueue.py", line 27
else node.cargo <= self.head.cargo:
^
SyntaxError: invalid syntax

I am not sure what this means.  Everything is spelled
correctly.

-Chris
 

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


Re: [Tutor] The Self

2006-07-31 Thread Alan Gauld

"Luke Paireepinart" <[EMAIL PROTECTED]> wrote
>> I would love to hear some of your 'layman's definitions' on the 
>> self.

> However, when you have a 'Class' object, it has its own collection
> of variables that it can modify, and its own set of functions that 
> work
> on its variables.  Because of this, you might want one class to do
> a certain thing and another class to do something else.

Thus is all true and good.

> Class Address(object):
>def __init__(street,city,zip):
>   print "Street: %s, City: %s, Zip: %s" % (street,city,zip)

> That's cool, but what if we want to store the values of these 
> attributes?
> Well, if we're going to store the values, we'll want to make a 
> separate
> copy of the class  that we can modify,

But this isn't.
Its very wrong to think of creating instamnces as being copies of the 
class.
classes are objects in the python sense in their own right and its 
possible
to instantiate them and also possible to copy them, and the two are
different!

One way to copy a class is to create a blank class that inherits from
the one we want to copy:

>>> class Thing:
...  def __init__(self,a=42):
...   self.a = a
...  def speak(self):
...   print "I am a thing with value", self.a
...
>>> class Different(Thing): pass
...
>>> t = Thing()
>>> t2 = Thing(27)
>>> d = Different(666)
>>> for it in [t,t2,d]: it.speak()
...
I am a thing with value 42
I am a thing with value 27
I am a thing with value 666
>>>

Notice that d thinks it is a Thing too...

But we have two instances ofThing and one instance of Different.
Which brings us back to the OPs point about the use of self.
Now if we read Luke's response but use instance instead of
copy then the reply is correct. :-)


> This is where self comes in.
> #-- start code
> Class Address(object):
>def __init__(self,street,city,zip):
>   self.street = street
>   self.city = city
>   self.zip = zip
>def output(self):
>   print "Street: %s, City: %s, Zip: %s" %
> (self.street,self.city,self.zip)
> #-- end code
> Now we can make a separate copy of the Address class, called
> an 'instance' of the class, like this:

As Luke says we create instances, but instances are not copies.
I am of class Man, but I am jot a copy of a man I am an instance,
just as my father is and many others. Classes are common nouns
instances are proper nouns in grammatical terms.

> >>> address1 = Address('1232 lexington drive','Annapolis','32423')
> when we call the output method of address1 we see:
> >>> address1.output()
> Street: 1232 lexington drive, City: Annapolis, Zip: 32423
> Now say we want to change the city name, cause we meant to put
> 'Indianapolis.'  All we have to do is this:
> address1.city = 'Indianapolis'
> Basically, when you call any function inside of a class, using a 
> class
> instance,
> (calling address1.anything is calling a function in an instance of a 
> class,
> calling Address.anything is calling the function from the original 
> copy
> of the class.)
> the first argument passed will be 'self', which is just the entire
> contents of the class.
> Hope that helps, I'd go more in-depth but I gotta run.

Likewise :-)
But you can read more on my web site OOP topic under the
heading "What is self".

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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


Re: [Tutor] question about headers and smtplib

2006-07-31 Thread Dustin J. Mitchell
shawn bright wrote:
> the only difference in one from the other is that in the headers of the
> other app (not my python script)
> there exist the following lines:
> 
> MIME-Version: 1.0
> X-Mailer: OstroSoft SMTP Control (4.0.20)
> Content-Type: text/plain; charset="us-ascii"
> Content-Transfer-Encoding: 7-bit
> 
> i do not understand how to make mine work and include (or configure to)
> the above example.
> 
> anyone point me in a right direction ?


It's hard to tell what the problem is without seeing the error messages
-- do you get a "bounce"?  Is there anything in your logfile?  Have you
tried set_debuglevel and looking at the output?  If you have information
there, but don't know how to interpret it, post it here and we'll take a
look.

You could try adding the Content-Type header to your own messages.
People configure mailservers in a lot of weird ways, and it's possible
that some mailservers reject emails without a Content-Type header..

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


[Tutor] question about headers and smtplib

2006-07-31 Thread shawn bright
Hey there,
me again with another question about headers..

if i use my python script to send an email, it gets rejected by some providers.
but another app that i use can send the same email and it gets thru.
i have sent myself test messages from both apps and looked at the headers.

the only difference in one from the other is that in the headers of the other app (not my python script)
there exist the following lines:

MIME-Version: 1.0
X-Mailer: OstroSoft SMTP Control (4.0.20)
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7-bit

i do not understand how to make mine work and include (or configure to) the above example.

anyone point me in a right direction ?

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


Re: [Tutor] The Self

2006-07-31 Thread shawn bright
Im not the OP, but this clears up some stuff for ma about "self". 
I thank you for your time to post this.
-shawnOn 7/31/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote:
Sebastian Smith wrote:> I am new to Python, real new. I am loving the language and learning> fast but I have hit a wall with the 'self'. I have Googled and> searched and read a few definitions but it still doesn't make sense to
> me.>> I would love to hear some of your 'layman's definitions' on the self.>> Thank you all,>> Ben.> ___> Tutor maillist  -  
Tutor@python.org> http://mail.python.org/mailman/listinfo/tutor>>Consider this:#--start code
def f():print 'hello' >>> a = f >>> a >>> a()hello >>> f >>> f()hello
#-- end codeSee how whenever you define a function, it creates a 'function' object?This means you can do anything with the function, assign a variable to it,put it in a list or a dictionary, etc.we assigned the variable 'a' to the function 'f', so we could call
function 'f' using either 'a()' or 'f()'.The reason we're allowed to have as many variables pointing to the samefunction as we want is because the function always does the same thingwhen passed the same parameters (more or less.)
However, when you have a 'Class' object, it has its own collectionof variables that it can modify, and its own set of functions that workon its variables.  Because of this, you might want one class to doa certain thing and another class to do something else.
Consider the following class:#-- start codeClass Address(object):def __init__(street,city,zip):   print "Street: %s, City: %s, Zip: %s" % (street,city,zip)#-- end codeWe can do whatever we want with these variables inside of our __init__
function.If we call the function, using Address.__init__('1232 west highwayboulevard','indianapolis','1')it will print the string 'Street: 1232 west highway boulevard, City:indianapolis, Zip: 1'
That's cool, but what if we want to store the values of these attributes?Well, if we're going to store the values, we'll want to make a separatecopy of the classthat we can modify, so that we don't have to change the original.
This is where self comes in.#-- start codeClass Address(object):def __init__(self,street,city,zip):   self.street = street   self.city = city   self.zip = zipdef output(self):
   print "Street: %s, City: %s, Zip: %s" %(self.street,self.city,self.zip)#-- end codeNow we can make a separate copy of the Address class, calledan 'instance' of the class, like this:
 >>> address1 = Address('1232 lexington drive','Annapolis','32423')when we call the output method of address1 we see: >>> address1.output()Street: 1232 lexington drive, City: Annapolis, Zip: 32423
Now say we want to change the city name, cause we meant to put'Indianapolis.'  All we have to do is this:address1.city = 'Indianapolis'Basically, when you call any function inside of a class, using a class
instance,(calling address1.anything is calling a function in an instance of a class,calling Address.anything is calling the function from the original copyof the class.)the first argument passed will be 'self', which is just the entire
contents of the class.Hope that helps, I'd go more in-depth but I gotta run.Luke___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] The Self

2006-07-31 Thread Luke Paireepinart
Sebastian Smith wrote:
> I am new to Python, real new. I am loving the language and learning
> fast but I have hit a wall with the 'self'. I have Googled and
> searched and read a few definitions but it still doesn't make sense to
> me.
>
> I would love to hear some of your 'layman's definitions' on the self.
>
> Thank you all,
>
> Ben.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   
Consider this:
#--start code
def f():
print 'hello'
 >>> a = f
 >>> a

 >>> a()
hello
 >>> f

 >>> f()
hello
#-- end code

See how whenever you define a function, it creates a 'function' object? 
This means you can do anything with the function, assign a variable to it,
put it in a list or a dictionary, etc.
we assigned the variable 'a' to the function 'f', so we could call
function 'f' using either 'a()' or 'f()'.
The reason we're allowed to have as many variables pointing to the same
function as we want is because the function always does the same thing
when passed the same parameters (more or less.)
However, when you have a 'Class' object, it has its own collection
of variables that it can modify, and its own set of functions that work
on its variables.  Because of this, you might want one class to do
a certain thing and another class to do something else.
Consider the following class:
#-- start code
Class Address(object):
def __init__(street,city,zip):
   print "Street: %s, City: %s, Zip: %s" % (street,city,zip)
#-- end code
We can do whatever we want with these variables inside of our __init__ 
function.
If we call the function, using Address.__init__('1232 west highway 
boulevard','indianapolis','1')
it will print the string 'Street: 1232 west highway boulevard, City: 
indianapolis, Zip: 1'
That's cool, but what if we want to store the values of these attributes?
Well, if we're going to store the values, we'll want to make a separate 
copy of the class
that we can modify, so that we don't have to change the original.
This is where self comes in.
#-- start code
Class Address(object):
def __init__(self,street,city,zip):
   self.street = street
   self.city = city
   self.zip = zip
def output(self):
   print "Street: %s, City: %s, Zip: %s" % 
(self.street,self.city,self.zip)
#-- end code
Now we can make a separate copy of the Address class, called
an 'instance' of the class, like this:
 >>> address1 = Address('1232 lexington drive','Annapolis','32423')
when we call the output method of address1 we see:
 >>> address1.output()
Street: 1232 lexington drive, City: Annapolis, Zip: 32423
Now say we want to change the city name, cause we meant to put
'Indianapolis.'  All we have to do is this:
address1.city = 'Indianapolis'
Basically, when you call any function inside of a class, using a class 
instance,
(calling address1.anything is calling a function in an instance of a class,
calling Address.anything is calling the function from the original copy 
of the class.)
the first argument passed will be 'self', which is just the entire 
contents of the class.
Hope that helps, I'd go more in-depth but I gotta run.
Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] The Self

2006-07-31 Thread Sebastian Smith
I am new to Python, real new. I am loving the language and learning
fast but I have hit a wall with the 'self'. I have Googled and
searched and read a few definitions but it still doesn't make sense to
me.

I would love to hear some of your 'layman's definitions' on the self.

Thank you all,

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