Re: [Tutor] Difference between 'yield' and 'print'

2007-01-17 Thread Luke Paireepinart
raghu raghu wrote:
> Is there any difference between yield and print in python script?i 
> have written a script based on fibonacci series where in i used yield 
> and print in two different scripts:
> the script is given below:
> def fib(n):
> a,b = 0,1
> while a<=n:
>print a
>a,b = b,a+b
>   
> for x in fib(4):
> print  x. When i executed this script i am getting this error: 
> Typeerror:"nonetype" is not iterable
> But the same script if 'print' is replaced with 'yield' inside while 
> loop it is executing properly without any type errors
> could any one reply why print cant be used instead of yield and why 
> error is generated?


the version of the function you created, fib(n) with the print, takes an 
argument 'n' and outputs the fibonacci sequence.
It does not return anything.

'for x in fib(4):'
is equivalent to saying:
'for x in none:'
which doesn't work because you can't iterate over nothing.



Refer to http://docs.python.org/ref/yield.html for information about yield.
Disclaimer: The following information I'm going to present you I came up 
with after reading that short explanation of 'yield' so it may not be 
exactly correct.
Also, I didn't check any code samples to see if they executed correctly.  :)


The reason the script works with 'yield' is because the fib(n) function 
then becomes a generator function.
The way I understand it (I don't know that much about generators)

By saying

def some_function(n):
a = 0
while a <= n:
   yield a
   a += 1

and looping over (what you'd think would be ) the returned value of the 
function call
for x in some_function(5):
print x

what's really happening is that, the first time the loop is run,
the body of some_function is executed, until it gets to the first 
'yield' statement.
this value is then yielded ('returned' in a sense) but the function 
object is not destroyed.
The yielded value is assigned to x.  after the body of the for loop 
executes,
the function object is resumed at the first 'yield'.
It's the same for every case, where it resumes execution at the last 
'yield' until there are no more values to yield.


So the first time through the 'for' loop,
a is set to 0
the inner while loop starts, a is < 5.
the value 0 is returned and bound to x
we print x

now the for loop starts over, some_function is resumed at where the 
'yield' was,
a is incremented, it's now 1
1 < 5, while loop starts again, the value of a (1) is returned.
etc, etc, etc.

until:
the for loop starts over, some_function is resumed where the 'yield' was,
a is incremented, it's now 6.
while loop repeats, a is not < n, the function ends, not returning a value.

so the end result is
012345

The basic idea, i think, is calling 'some_function(5)' creates a 
generator object (an instance of some 'generator' class?)
which has a method called next().

So you could think of the loop like this:
gen = some_function(5) #create the generator
while 1:
try:
   print gen.next()
except: #we ran out of things to generate (the generator stopped 
yielding results -- supposedly it raises an error here?)
   print "End of generator."
   break

Anyway, I've never used them but that sounds like a reasonable way for 
them to work.
If you'd like a better explanation you might poke around Alan Gauld's 
tutorial, he probably has a section on generators.

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


[Tutor] possible import locations

2007-01-17 Thread Andre Engels

Is it possible to see from a python program where it searches for possible
imports? Is it possible to import from another location than those? (Of
course with an "if so, how" attached).

The issue is that the company I work for is switching providers. With the
old provider (as well as in my local setting), Python automatically searched
for imports in the current directory (the one where the program was run).
With the new one, this does not seem to be the case, so I am getting
ImportErrors all over the place.

--
Andre Engels, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Perfect Python web stack?

2007-01-17 Thread OkaMthembo

Ok pythonistas!

Please help me to decide. I might have asked some of you before, so please
bear with me.

I want to build a database driven python web app and i need to decide, so
please vote on the best components (im developing on XP SP2):

1) MySQL vs PostGRES
2) Lighttpd + FastCGI vs Apache + mod_python
3) Pylons + Cheetah + Kid/Mochikit/Dojo vs Django/Turbogears

This is not to light fires, its for my sole information :-)

Thanks in advance, gurus.

"Shortash"

--
"The Stupidry Foundry"
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difference between 'yield' and 'print'

2007-01-17 Thread Kent Johnson
Luke Paireepinart wrote:

> Refer to http://docs.python.org/ref/yield.html for information about yield.
> Disclaimer: The following information I'm going to present you I came up 
> with after reading that short explanation of 'yield' so it may not be 
> exactly correct.

There is a longer explanation here:
http://www.python.org/doc/2.2.3/whatsnew/node5.html

> So you could think of the loop like this:
> gen = some_function(5) #create the generator
> while 1:
> try:
>print gen.next()
> except: #we ran out of things to generate (the generator stopped 
> yielding results -- supposedly it raises an error here?)
>print "End of generator."
>break
> 
> Anyway, I've never used them but that sounds like a reasonable way for 
> them to work.

Yes, that is pretty much what is going on. The generatory will raise 
StopIteration when there are no more values.

Kent


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


Re: [Tutor] import and unittest

2007-01-17 Thread thomas coopman
On Tue, 16 Jan 2007 10:06:37 -
"Alan Gauld" <[EMAIL PROTECTED]> wrote:

> 
> "Thomas Coopman" <[EMAIL PROTECTED]> wrote
> .
> > I wondered if it was possible to do something like this:
> >
> > src/
> >-a_module/
> >-sub_module/
> > test/
> >-a_module/
> >-sub_module/
> >
> 
> I don;t see any reason why not although its slightly more work.
> Personally I tend to keep the tests with the code, but thats
> mainly because tools such as editors tend to remember the last
> folder opened and its a pain navigating between the two folders.
> 
> The other system I have used(in C++ not Python) is to have
> a test folder inside each src folder like:
> 
> src/
>mod1/
>f1.py
>test/
>   testf1.py
> mod2/
>f2.py
>f3.py
>test/
>testf1.py
>testf2.py
> 
> etc.
> 
> This minimises navigation and keeps the tests separate.
> Its also relatively easy to filter out the tests when it comes
> time to package upp the code for distribution (assuming
> you want to lose them!)
> 
> > I have something like this but I don't know how to organize the 
> > imports in
> > the tests and I don't know if this is a good idea.  What do you 
> > think?
> 
> I think in Python you should create a package structure for
> your code so that import can find the modules more easily.
> But I've never tried this in Python, my Python projects are rarely
> big enough to warrant it.
> 
> 

well, I still have problems with import

suppose I have something like this:

M/
  __init__.py
  A/
__init__.py
One.py
  B/
__init__.py
Two.py

One.py
#!/usr/bin/python

from M.B import Two

Two.py
#!/usr/bin/python

from M.A import One


when I try to run One.py or Two.py I get import errors and I don't know
how to fix them.  I tried to set the __all__ variable in the __init__
files but nothing worked so far.

I would like to run the files from anywhere for example.
What am I doing wrong?
I read the information about modules on python.org
(http://www.python.org/doc/2.0.1/tut/node8.html) but I don't find a
solution

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


Re: [Tutor] possible import locations

2007-01-17 Thread Kent Johnson
Andre Engels wrote:
> Is it possible to see from a python program where it searches for 
> possible imports? Is it possible to import from another location than 
> those? (Of course with an "if so, how" attached).
> 
> The issue is that the company I work for is switching providers. With 
> the old provider (as well as in my local setting), Python automatically 
> searched for imports in the current directory (the one where the program 
> was run). With the new one, this does not seem to be the case, so I am 
> getting ImportErrors all over the place.

sys.path is a list of locations that will be searched for imports. You 
can add new entries to it with the usual list operations.

If you want to add '.' to sys.path always you could do this in a 
sitecustomize.py module.
- Create a directory Lib\site-packages if it doesn't already exist
- Create a file site-packages\sitecustomize.py
- Put your custom startup stuff in sitecustomize.py

Is your new provider using a different OS? I remember a discussion long 
ago about the current directory being in sys.path or not and it seemed 
to vary depending on OS.

Kent

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


Re: [Tutor] import and unittest

2007-01-17 Thread Kent Johnson
thomas coopman wrote:
> On Tue, 16 Jan 2007 10:06:37 -
> "Alan Gauld" <[EMAIL PROTECTED]> wrote:
> 
>> "Thomas Coopman" <[EMAIL PROTECTED]> wrote
>> .
>>> I wondered if it was possible to do something like this:
>>>
>>> src/
>>>-a_module/
>>>-sub_module/
>>> test/
>>>-a_module/
>>>-sub_module/
>>>
>> I don;t see any reason why not although its slightly more work.
>> Personally I tend to keep the tests with the code, but thats
>> mainly because tools such as editors tend to remember the last
>> folder opened and its a pain navigating between the two folders.
>>
>> The other system I have used(in C++ not Python) is to have
>> a test folder inside each src folder like:
>>
>> src/
>>mod1/
>>f1.py
>>test/
>>   testf1.py
>> mod2/
>>f2.py
>>f3.py
>>test/
>>testf1.py
>>testf2.py
>>
>> etc.
>>
>> This minimises navigation and keeps the tests separate.
>> Its also relatively easy to filter out the tests when it comes
>> time to package upp the code for distribution (assuming
>> you want to lose them!)
>>
>>> I have something like this but I don't know how to organize the 
>>> imports in
>>> the tests and I don't know if this is a good idea.  What do you 
>>> think?
>> I think in Python you should create a package structure for
>> your code so that import can find the modules more easily.
>> But I've never tried this in Python, my Python projects are rarely
>> big enough to warrant it.
>>
>>
> 
> well, I still have problems with import
> 
> suppose I have something like this:
> 
> M/
>   __init__.py
>   A/
> __init__.py
> One.py
>   B/
> __init__.py
> Two.py
> 
> One.py
> #!/usr/bin/python
> 
> from M.B import Two
> 
> Two.py
> #!/usr/bin/python
> 
> from M.A import One
> 
> 
> when I try to run One.py or Two.py I get import errors and I don't know
> how to fix them.  I tried to set the __all__ variable in the __init__
> files but nothing worked so far.

What error do you get?

It's better to avoid this kind of circular import if you can. Rather 
than having One depend on Two and Two depend on One, look for some piece 
you can factor out into a new module that both One and Two use (or it 
may only be needed by One or Two).

There are several problems with circular imports, but the technical 
problem you are having is probably like this:

module One starts to execute
One imports Two
Two starts to execute
Two imports One
-- Note that at this point, the body of One has not executed, so 
anything defined in One after the import of Two is not yet defined.
Two tries to use something in One. Since One is not fully defined, it 
gets an AttributeError.

Anyway, try to split up your modules or post the exact error you get.

Kent

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


Re: [Tutor] import and unittest

2007-01-17 Thread Thomas Coopman

On 1/17/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


thomas coopman wrote:
> On Tue, 16 Jan 2007 10:06:37 -
> "Alan Gauld" <[EMAIL PROTECTED]> wrote:
>
>> "Thomas Coopman" <[EMAIL PROTECTED]> wrote
>> .
>>> I wondered if it was possible to do something like this:
>>>
>>> src/
>>>-a_module/
>>>-sub_module/
>>> test/
>>>-a_module/
>>>-sub_module/
>>>
>> I don;t see any reason why not although its slightly more work.
>> Personally I tend to keep the tests with the code, but thats
>> mainly because tools such as editors tend to remember the last
>> folder opened and its a pain navigating between the two folders.
>>
>> The other system I have used(in C++ not Python) is to have
>> a test folder inside each src folder like:
>>
>> src/
>>mod1/
>>f1.py
>>test/
>>   testf1.py
>> mod2/
>>f2.py
>>f3.py
>>test/
>>testf1.py
>>testf2.py
>>
>> etc.
>>
>> This minimises navigation and keeps the tests separate.
>> Its also relatively easy to filter out the tests when it comes
>> time to package upp the code for distribution (assuming
>> you want to lose them!)
>>
>>> I have something like this but I don't know how to organize the
>>> imports in
>>> the tests and I don't know if this is a good idea.  What do you
>>> think?
>> I think in Python you should create a package structure for
>> your code so that import can find the modules more easily.
>> But I've never tried this in Python, my Python projects are rarely
>> big enough to warrant it.
>>
>>
>
> well, I still have problems with import
>
> suppose I have something like this:
>
> M/
>   __init__.py
>   A/
> __init__.py
> One.py
>   B/
> __init__.py
> Two.py
>
> One.py
> #!/usr/bin/python
>
> from M.B import Two
>
> Two.py
> #!/usr/bin/python
>
> from M.A import One
>
>
> when I try to run One.py or Two.py I get import errors and I don't know
> how to fix them.  I tried to set the __all__ variable in the __init__
> files but nothing worked so far.

What error do you get?

It's better to avoid this kind of circular import if you can. Rather
than having One depend on Two and Two depend on One, look for some piece
you can factor out into a new module that both One and Two use (or it
may only be needed by One or Two).

There are several problems with circular imports, but the technical
problem you are having is probably like this:

module One starts to execute
One imports Two
Two starts to execute
Two imports One
-- Note that at this point, the body of One has not executed, so
anything defined in One after the import of Two is not yet defined.
Two tries to use something in One. Since One is not fully defined, it
gets an AttributeError.

Anyway, try to split up your modules or post the exact error you get.

Kent



Well I don't really
need the circular imports but I would like to know how to do the
imports correct.
Suppose that in the example that I showed only One needs Two.

So then we have this:

M/
__init__.py
A/
__init__.py
One.py
B/
__init__.py
Two.py

One.py
#!/usr/bin/python

from M.B import Two


when I run this I get this error:
ImportError: No Module named M.B


--
Thomas Coopman
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] import and unittest

2007-01-17 Thread Kent Johnson
Thomas Coopman wrote:
> Well I don't really 
> need the circular imports but I would like to know how to do the imports 
> correct. 
> 
> Suppose that in the example that I showed only One needs Two.
> 
> So then we have this:
> 
> M/
> __init__.py
> A/
> __init__.py
> One.py
> B/
> __init__.py
> Two.py
> 
> One.py
> #!/usr/bin/python
> 
> from M.B import Two
> 
> 
> when I run this I get this error:
> ImportError: No Module named M.B

Is the directory containing M in sys.path?

Kent

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


[Tutor] Query about getattr used as a dispatcher

2007-01-17 Thread raghu raghu

i am following 'dive into python' for learning. i come across a term
getattr() which gives reference about a function.Its written it can be used
as a dispatcher. Below example given for that builtin function:
import statsout
 def output(data,format='text'):
   output_function = getattr(statsout,"output_%s"  % format)
   return output_function(data)
Actually i installed python 2.5 i ran this script and its showing error it
could not import statsout.why is it so?could any one explain this script
briefly. i dont know whether i am following the right book. as i am a
beginner is it right  to follow this book?or is there any other book which
is best for beginners?

--

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


Re: [Tutor] import and unittest

2007-01-17 Thread Thomas Coopman

On 1/17/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


Thomas Coopman wrote:
> Well I don't really
> need the circular imports but I would like to know how to do the imports
correct.
>
> Suppose that in the example that I showed only One needs Two.
>
> So then we have this:
>
> M/
> __init__.py
> A/
> __init__.py
> One.py
> B/
> __init__.py
> Two.py
>
> One.py
> #!/usr/bin/python
>
> from M.B import Two
>
>
> when I run this I get this error:
> ImportError: No Module named M.B

Is the directory containing M in sys.path?

Kent



When I run One.py from in the directory A, doesn't look python in it's
parent directory when it can't find the module?
And if that's not the case where and what should I add to the sys.pathvariable?


--
Thomas Coopman
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] import and unittest

2007-01-17 Thread Kent Johnson
Thomas Coopman wrote:
> When I run One.py from in the directory A, doesn't look python in it's 
> parent directory when it can't find the module?
> And if that's not the case where and what should I add to the sys.path 
> variable?

No, Python won't look in parent directories for imports. Try running 
One.py from the parent dir of M by typing
 > python M/A/One.py

or add the parent dir of M to sys.path.

Kent

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


Re: [Tutor] import and unittest

2007-01-17 Thread Thomas Coopman

On 1/17/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


Thomas Coopman wrote:
> When I run One.py from in the directory A, doesn't look python in it's
> parent directory when it can't find the module?
> And if that's not the case where and what should I add to the sys.path
> variable?

No, Python won't look in parent directories for imports. Try running
One.py from the parent dir of M by typing
> python M/A/One.py

or add the parent dir of M to sys.path.

Kent

when I try to run it from the parent dir of M, I still get the error,

where and when should I add the the parent dir to sys.path?

when I try from M.A import One in a shell executed in the parent dir of M
than it works.

The thing is that I would like to run One.py from anywhere and not just from
that one directory.
--
Thomas Coopman
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] help with telnet error

2007-01-17 Thread Chris Hallman

I'm working on a program that telnets to multiple devices to test their
backup ISDN BRI connections. I'm trying to build in error recovery with
try/except logic, but I'm having trouble getting it to work. This first
example uses a host name that isn't in our DNS (yes, this does happen):


import telnetlib
host = "s3793ap01"
telnetlib.Telnet(host)


Traceback (most recent call last):
 File "", line 1, in 
   telnetlib.Telnet(host)
 File "c:\python25\lib\telnetlib.py", line 208, in __init__
   self.open(host, port)
 File "c:\python25\lib\telnetlib.py", line 225, in open
   for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
gaierror: (11001, 'getaddrinfo failed')





try:

   telnetlib.Telnet(host)
except gaierror, e:
   print "error found", e




Traceback (most recent call last):
 File "", line 3, in 
   except gaierror, e:
NameError: name 'gaierror' is not defined




try:

   telnetlib.Telnet(host)
except IOError, e:
   print "error found", e




Traceback (most recent call last):
 File "", line 2, in 
   telnetlib.Telnet(host)
 File "c:\python25\lib\telnetlib.py", line 208, in __init__
   self.open(host, port)
 File "c:\python25\lib\telnetlib.py", line 225, in open
   for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
gaierror: (11001, 'getaddrinfo failed')


As you can see, I'm not sure how to handle the error. Any ideas?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Perfect Python web stack?

2007-01-17 Thread Mike Hansen
 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of OkaMthembo
> Sent: Wednesday, January 17, 2007 3:59 AM
> To: tutor
> Subject: [Tutor] Perfect Python web stack?
> 
> Ok pythonistas!
> 
> Please help me to decide. I might have asked some of you 
> before, so please bear with me.
> 
> I want to build a database driven python web app and i need 
> to decide, so please vote on the best components (im 
> developing on XP SP2): 
> 
> 1) MySQL vs PostGRES
> 2) Lighttpd + FastCGI vs Apache + mod_python
> 3) Pylons + Cheetah + Kid/Mochikit/Dojo vs Django/Turbogears
> 
> This is not to light fires, its for my sole information :-)
> 
> Thanks in advance, gurus.
> 
> "Shortash"

I think I recently read the you need to purchase MySQL to get a binary
for Windows, but don't quote me. 

Postgre + Apache + Cheetah + Dojo is working for me. Sometime soon, I
want to experiment with Django.

I'm not familiar with Lighttpd, FastCGI, mod_python, Pylons, or Kid.
When I was looking for a JavaScript toolkit, I took a look at Mochikit
and Dojo. I was able to understand Dojo better although I hear that
Mochikit is more Python-like. 

Mike 
-

  NOTICE:  This e-mail transmission and any documents or files attached to
  it contain information for the sole use of the above-identified individual or 
entity.

  Its contents may be privileged, confidential, and exempt from disclosure 
under the law.
  Any dissemination, distribution, or copying of this communication is strictly 
prohibited.

  Please notify the sender immediately if you are not the intended recipient.

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


Re: [Tutor] help with telnet error

2007-01-17 Thread Kent Johnson
Chris Hallman wrote:
> 
> I'm working on a program that telnets to multiple devices to test their 
> backup ISDN BRI connections. I'm trying to build in error recovery with 
> try/except logic, but I'm having trouble getting it to work. This first 
> example uses a host name that isn't in our DNS (yes, this does happen):
> 
>  >>> import telnetlib
>  >>> host = "s3793ap01"
>  >>> telnetlib.Telnet(host)
> 
> Traceback (most recent call last):
>   File "", line 1, in 
> telnetlib.Telnet(host)
>   File "c:\python25\lib\telnetlib.py", line 208, in __init__
> self.open(host, port)
>   File "c:\python25\lib\telnetlib.py", line 225, in open
> for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
> gaierror: (11001, 'getaddrinfo failed')
>  >>>
>  >>>
>  >>>
>  >>>
>  >>> try:
> telnetlib.Telnet(host)
> except gaierror, e:
> print "error found", e
> 
> 
>
> 
> Traceback (most recent call last):
>   File "", line 3, in 
> except gaierror, e:
> NameError: name 'gaierror' is not defined

gaierror is defined in the socket module. Add
   import socket

then use
   except socket.gaierror, e:

Kent

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


Re: [Tutor] Query about getattr used as a dispatcher

2007-01-17 Thread Chris Calloway
raghu raghu wrote:
> Actually i installed python 2.5 i ran this script and its showing error 
> it could not import statsout. why is it so?

statsout is a *hypothetical* module used for an example only. The 
statsout module does not actually exist. When Dive Into Python wants you 
to type in an example, it will shown either as lines at the Python 
interpreter prompt (>>>), or it will be an example in a Python source 
file included with the Dive Into Python examples bundle. If you look at 
the example using the imaginary statsout module in section 4.12, which 
is not included in any Python source file in the Dive Into Python 
examples bundle, you will see the example is not referenced in a file, 
nor is it shown as being typed at the Python interpreter prompt.

If, however, you had an actual statsout module in your sys.path, you 
could import it. And it that module had top level functions functions 
that took one argument and had function names like "output_text" and 
"output_pdf" and "output_html," then the example would work if you typed 
it in. The example is just showing a hypothetical case of a very simple 
dispatcher. The example is asking you to imagine *if* you had a statsout 
module, and *if* that module had functions by those names in it.

Dive Into Python uses the getattr function in the apihelper.py example 
you are currently reading about. In the next chapter, a more complicated 
example is shown where getattr is used in a dispatcher which finds, not 
just a function by name, but a class by name and dispatches that class 
object to create a new object of that class. So the statsout *imaginary* 
example is just preparing you for a more complicated *real life* 
dispatcher example in the next chapter. Hypothetical examples are often 
shown in programming books to prepare you for more complicated real life 
examples.

-- 
Sincerely,

Chris Calloway
http://www.seacoos.org
office: 332 Chapman Hall   phone: (919) 962-4323
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599

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


Re: [Tutor] Perfect Python web stack?

2007-01-17 Thread OkaMthembo

Thanks, gentlemen. Input appreciated and noted.

"Shortash"

On 1/17/07, Mike Hansen <[EMAIL PROTECTED]> wrote:




> -Original Message-
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of OkaMthembo
> Sent: Wednesday, January 17, 2007 3:59 AM
> To: tutor
> Subject: [Tutor] Perfect Python web stack?
>
> Ok pythonistas!
>
> Please help me to decide. I might have asked some of you
> before, so please bear with me.
>
> I want to build a database driven python web app and i need
> to decide, so please vote on the best components (im
> developing on XP SP2):
>
> 1) MySQL vs PostGRES
> 2) Lighttpd + FastCGI vs Apache + mod_python
> 3) Pylons + Cheetah + Kid/Mochikit/Dojo vs Django/Turbogears
>
> This is not to light fires, its for my sole information :-)
>
> Thanks in advance, gurus.
>
> "Shortash"

I think I recently read the you need to purchase MySQL to get a binary
for Windows, but don't quote me.

Postgre + Apache + Cheetah + Dojo is working for me. Sometime soon, I
want to experiment with Django.

I'm not familiar with Lighttpd, FastCGI, mod_python, Pylons, or Kid.
When I was looking for a JavaScript toolkit, I took a look at Mochikit
and Dojo. I was able to understand Dojo better although I hear that
Mochikit is more Python-like. 

Mike


-

  NOTICE:  This e-mail transmission and any documents or files attached to
  it contain information for the sole use of the above-identified
individual or entity.

  Its contents may be privileged, confidential, and exempt from disclosure
under the law.
  Any dissemination, distribution, or copying of this communication is
strictly prohibited.

  Please notify the sender immediately if you are not the intended
recipient.

FGNS


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






--
"The Stupidry Foundry"
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query about getattr used as a dispatcher

2007-01-17 Thread Kent Johnson
raghu raghu wrote:
> i am following 'dive into python' for learning.  i dont know whether i am 
> following the right book. as i 
> am a beginner is it right  to follow this book?or is there any other 
> book which is best for beginners?

I don't think Dive Into Python is a great book for beginners. In my 
opinion the book is too focused on advanced features.

You might want to try one of these tutorials:
http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

or one of these books:
http://effbot.org/pyfaq/tutor-what-are-some-good-books-on-python.htm

Kent

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


Re: [Tutor] Perfect Python web stack?

2007-01-17 Thread Kent Johnson
OkaMthembo wrote:
> Ok pythonistas!
> 
> Please help me to decide. I might have asked some of you before, so 
> please bear with me.
> 
> I want to build a database driven python web app and i need to decide, 
> so please vote on the best components (im developing on XP SP2):
> 
> 1) MySQL vs PostGRES
> 2) Lighttpd + FastCGI vs Apache + mod_python
> 3) Pylons + Cheetah + Kid/Mochikit/Dojo vs Django/Turbogears

All of these have their proponents. If you search comp.lang.python you 
will find many discussions. Start here:
http://groups.google.com/group/comp.lang.python/

Kent

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


Re: [Tutor] Perfect Python web stack?

2007-01-17 Thread Python
On Wed, 2007-01-17 at 16:46 +0200, OkaMthembo wrote:
> > 1) MySQL vs PostGRES

PostGRES is a more sophisticated SQL server.  It should probably be the
default choice.

However, I'm primarily using MySQL.  The reasons:
easy administration - I think supporting dozens of remote
databases is easier with MySQL.

replication - the loosely coupled MySQL approach to replication
has been a better fit for my needs.

good performance in the common simple cases

You probably need to work through your requirements and match those
against the different available products.  If you provide a list of
requirements, you might get more meaningful feedback.

-- 
Lloyd Kvam
Venix Corp

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


[Tutor] problem with telnetlib, threading class and try/except

2007-01-17 Thread Chris Hallman

I'm writing a multithreaded program that initiates a dial backup connection
to check the B channels on ISDN BRI connections. I recently added logic to
check for offline devices (Operation timed out) and DNS errors (getaddrinfo
failed). When it encounters the exception, it doesn't appear to be exiting
(by way of the return command) the thread, hence the error.

The input file contains:

rtr3926
s0877ap01

Here is part of the program:
import os, os.path, random, re, socket, string, sys, telnetlib, threading,
time
from time import strftime
from threading import Thread

class ISDNTest(threading.Thread):
   def __init__(self, host):
   Thread.__init__(self)
   self.host = host
   try:
   self.tn = telnetlib.Telnet(self.host)
   except socket.error, err:
   if "Operation timed out" in err:
   print "1st if", self.host, err #progress checking
   dialTestResult[self.host] = ["", "connection timed out"]
   return
   elif "getaddrinfo failed" in err:
   print "2nd if", self.host, err #progress checking
   dialTestResult[self.host] = ["", "DNS resolution failed"]
   return
   self.status = []
   print self.tn #progress checking

   def run(self):
   connect_status = self.Logon()
...(much more program not pasted here)

for entry in inFile:
   if entry == '\n':
   continue
   elif "#" in entry:
   continue
   entry = entry.replace("\n", "")
   while count_active() >= Max_Threads:
   time.sleep(1)
   threads = ISDNTest(entry)
   tlist.append(threads)
   threads.start()

for thread in tlist:
   thread.join()


Here is there error:

pythonw -u "new.isdn.test.py"


log me on rtr3926 (progress checking message, this isn't an error)
1st if s0877ap01 (10060, 'Operation timed out') (progress checking message;
this indicates an exception occurred)
log me on s0877ap01 (progress checking message, this isn't an error)
Exception in thread Thread-2:
Traceback (most recent call last):
 File "c:\python25\lib\threading.py", line 460, in __bootstrap
   self.run()
 File "new.isdn.test.py", line 35, in run
   connect_status = self.Logon()
 File "new.isdn.test.py", line 59, in Logon
   self.tn.read_until("Username:", 7)
AttributeError: 'ISDNTest' object has no attribute 'tn'


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


[Tutor] What is a mixin class?

2007-01-17 Thread Don Taylor
I have a vague idea what a mixin class is, I would like to get a better 
handle on it.

It is a term that is used quite often in Python circles, but I can't 
find a definition.

I guess that because of multiple inheritance Python does not need a 
formal way of specifying mixin classes so I presume that there is some 
conventional interpretation/coding that is followed for mixin classes.

So, what constitutes a mixin class and what are the conventional ways to 
denote them in code?

Don.

I notice that Ruby has single inheritance plus mixin classes so maybe 
somebody who knows Ruby could throw some light on in what way a mixin is 
different from normal superclass.

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


Re: [Tutor] possible import locations

2007-01-17 Thread Dave Kuhlman
On Wed, Jan 17, 2007 at 06:07:19AM -0500, Kent Johnson wrote:
> Andre Engels wrote:
> > Is it possible to see from a python program where it searches for 
> > possible imports? Is it possible to import from another location than 
> > those? (Of course with an "if so, how" attached).
> > 
> > The issue is that the company I work for is switching providers. With 
> > the old provider (as well as in my local setting), Python automatically 
> > searched for imports in the current directory (the one where the program 
> > was run). With the new one, this does not seem to be the case, so I am 
> > getting ImportErrors all over the place.
> 
> sys.path is a list of locations that will be searched for imports. You 
> can add new entries to it with the usual list operations.
> 
> If you want to add '.' to sys.path always you could do this in a 
> sitecustomize.py module.
> - Create a directory Lib\site-packages if it doesn't already exist
> - Create a file site-packages\sitecustomize.py
> - Put your custom startup stuff in sitecustomize.py

You will also want to read about .pth files.  See this:

http://docs.python.org/lib/module-site.html

And, of course, you can also read the source.  See site.py, which
on my machine is at:

/usr/local/lib/python2.5/site.py

> 
> Is your new provider using a different OS? I remember a discussion long 
> ago about the current directory being in sys.path or not and it seemed 
> to vary depending on OS.

Right.  I'm on Linux (Debian GNU/Linux).  '.' is not on my
sys.path, but I can import from the current directory.

However, the empty string ('') is the first item in the list
sys.path.  Does the empty string mean the same thing as the
directory '.'?

Does anyone have a reference to a document that specifies
recommended policy for the use of the .pth files and the
sitecustomize.py file.  For example, in addition to Kent's
recommendation, you could also put sitecustomize.py in your current
directory, where it would only affect programs run from that
directory.  Which is recommended and when?

Also, the .pth file thing seems to be a way to give a package, when
it is installed, to hijack the import path by stuffing paths above
(earlier than) existing paths.  Is there a recommended policy on
this?  If so, where can I read up on it?

Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem with telnetlib, threading class and try/except

2007-01-17 Thread Kent Johnson
Chris Hallman wrote:
> 
> I'm writing a multithreaded program that initiates a dial backup 
> connection to check the B channels on ISDN BRI connections. I recently 
> added logic to check for offline devices (Operation timed out) and DNS 
> errors (getaddrinfo failed). When it encounters the exception, it 
> doesn't appear to be exiting (by way of the return command) the thread, 
> hence the error.
> 
> The input file contains:
> 
> rtr3926
> s0877ap01
> 
> Here is part of the program:
> import os, os.path, random, re, socket, string, sys, telnetlib, 
> threading, time
> from time import strftime
> from threading import Thread
> 
> class ISDNTest(threading.Thread):
> def __init__(self, host):
> Thread.__init__(self)
> self.host = host
> try:
>  self.tn  = telnetlib.Telnet(self.host)
> except socket.error, err:
> if "Operation timed out" in err:
> print "1st if", self.host, err #progress checking
> dialTestResult[self.host] = ["", "connection timed out"]
> return
> elif "getaddrinfo failed" in err:
> print "2nd if", self.host, err #progress checking
> dialTestResult[self.host] = ["", "DNS resolution failed"]
> return

You need an else: clause here; what if the error doesn't match either of 
your tests?

Kent

> self.status = []
> print self.tn  #progress checking
> 
> def run(self):
> connect_status = self.Logon()
> ...(much more program not pasted here)
> 
> for entry in inFile:
> if entry == '\n':
> continue
> elif "#" in entry:
> continue
> entry = entry.replace("\n", "")
> while count_active() >= Max_Threads:
> time.sleep(1)
> threads = ISDNTest(entry)
> tlist.append(threads)
> threads.start()
> 
> for thread in tlist:
> thread.join()
> 
> 
> Here is there error:
>  >pythonw -u "new.isdn.test.py "
> 
> log me on rtr3926 (progress checking message, this isn't an error)
> 1st if s0877ap01 (10060, 'Operation timed out') (progress checking 
> message; this indicates an exception occurred)
> log me on s0877ap01 (progress checking message, this isn't an error)
> Exception in thread Thread-2:
> Traceback (most recent call last):
>   File "c:\python25\lib\threading.py", line 460, in __bootstrap
> self.run()
>   File " new.isdn.test.py ", line 35, in run
> connect_status = self.Logon()
>   File "new.isdn.test.py ", line 59, in Logon
> self.tn.read_until("Username:", 7)
> AttributeError: 'ISDNTest' object has no attribute 'tn'
> 
>  >Exit code: 0
> 
> 
> 
> 
> ___
> 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] Perfect Python web stack?

2007-01-17 Thread Dave Kuhlman
On Wed, Jan 17, 2007 at 10:09:02AM -0500, Kent Johnson wrote:
> OkaMthembo wrote:
> > Ok pythonistas!
> > 
> > Please help me to decide. I might have asked some of you before, so 
> > please bear with me.
> > 
> > I want to build a database driven python web app and i need to decide, 
> > so please vote on the best components (im developing on XP SP2):
> > 
> > 1) MySQL vs PostGRES
> > 2) Lighttpd + FastCGI vs Apache + mod_python
> > 3) Pylons + Cheetah + Kid/Mochikit/Dojo vs Django/Turbogears
> 
> All of these have their proponents. If you search comp.lang.python you 
> will find many discussions. Start here:
> http://groups.google.com/group/comp.lang.python/
> 

For the Web part of the stack, here is another good starting point:

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

I'm currently learning Pylons.  The tutorials for Pylons discuss
two object-relational mappers for Python: SQLObject and SQLAlchemy. 
If you want to view your database from a higher, more abstract
level, you might want to look at them, too.  Here are some database
references:

http://wiki.python.org/moin/DatabaseInterfaces
http://www.python.org/doc/topics/database/
http://www.python.org/doc/topics/database/modules/
http://www.sqlalchemy.org/
http://www.sqlobject.org/

Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Graphics with Python: wxPython vs. tkinter vs. PyCairo vs. PyX vs...

2007-01-17 Thread Vijay Pattisapu
Hey friends--

I've been looking through the archives and haven't found any
comparative evaluation of Python graphics libraries...

Which is the best (or your favorite), and for what tasks?

Thanks a lot!
Vijay

-- 
3506 Speedway
Austin, TX 78705
Cell: (469)877-9166
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Perfect Python web stack?

2007-01-17 Thread Vijay Pattisapu
I personally like Zope's object-oriented database.

-Vijay

On 17/01/07, OkaMthembo <[EMAIL PROTECTED]> wrote:
> Ok pythonistas!
>
> Please help me to decide. I might have asked some of you before, so please
> bear with me.
>
> I want to build a database driven python web app and i need to decide, so
> please vote on the best components (im developing on XP SP2):
>
> 1) MySQL vs PostGRES
> 2) Lighttpd + FastCGI vs Apache + mod_python
> 3) Pylons + Cheetah + Kid/Mochikit/Dojo vs Django/Turbogears
>
> This is not to light fires, its for my sole information :-)
>
> Thanks in advance, gurus.
>
> "Shortash"
>
> --
> "The Stupidry Foundry"
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


-- 
3506 Speedway
Austin, TX 78705
Cell: (469)877-9166
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a mixin class?

2007-01-17 Thread Chris Lasher
On 1/17/07, Don Taylor <[EMAIL PROTECTED]> wrote:
> So, what constitutes a mixin class and what are the conventional ways to
> denote them in code?

A mixin is a specific type of superclass, just called a mixin because
of the concept it represents. A common type of mixin would be a class
that defines some sort of modified functionality intended to be given
to multiple, not necessarily related classes.

Say you wanted all your classes to have a similar looking format when
"print"ed. You could define a mixin class that defined a special
__repr__ method. A detailed  example of this is demonstrated in
/Learning Python/ by Lutz & Ascher.

There's no need to explicitly state that they are a mixin class, but
if you felt the need to, you could either put it in a comment or,
probably even better, in the mixin's docstring.

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


Re: [Tutor] What is a mixin class?

2007-01-17 Thread Danny Yoo


On Wed, 17 Jan 2007, Don Taylor wrote:

> I have a vague idea what a mixin class is, I would like to get a better 
> handle on it.

Hi Don,

This post might help:

 http://mail.python.org/pipermail/tutor/2006-October/050448.html


The core idea is that, since classes themselves are first-class objects, 
we can pass classes around to functions.  More importantly, we can make 
classes on-the-fly.

If we have some class C on hand, we can easily build a subclass.  For 
example:


def do_nothing_mixin(C):
 class S(C):
 pass
 return S


Here, do_nothing_mixing takes in a class C and returns a class S, where S 
is a child subclass of C.  Of course, this example is a bit useless. 
*grin*


But here's a slightly more interesting one:

###
def observer_mixin(C):
 class S(C):
 def __init__(self, *args, **kwargs):
 C.__init__(self, *args, **kwargs)
 self.__listeners = []
 def add_listener(self, l):
 self.__listeners.append(l)
 def notify(self, obj):
 for l in self.__listeners:
 l.notified(obj)
 return S
###


This captures most of the "Observer" design pattern (without 
thread-safety, but it wouldn't be hard to add that feature in).

We can now add observer capabilities to an arbitrary class:

###
class Person:
 def __init__(self, name):
 self.name = name
 def notified(self, obj):
 print "Oh, I %s have been notified about %r" % (self.name, obj)

Announcer = observer_mixin(Person)

ann = Announcer("bart")
others = [Person("Lisa"), Person("Marge"), Person("Homer")]
for o in others:
 ann.add_listener(o)

ann.notify("donut")
###

Note here that we have a pre-existing Person class, and we create a new 
kind of Person called an Announcer.  This Announcer is a person, but can 
also act as an observer.


> It is a term that is used quite often in Python circles, but I can't
> find a definition.

One definition (not the only possible one) could be:

 mixin: a function that takes in an input class and produces
 an output class that's guaranteed to be a subclass of the input.

There are other ways to get mixin-like functionality, which is why it's 
necessary to come to terms.



> I notice that Ruby has single inheritance plus mixin classes so maybe 
> somebody who knows Ruby could throw some light on in what way a mixin is 
> different from normal superclass.

'modules' in Ruby are injectable into the definitions of other classes. 
The mechanism, then, is slightly different than what's presented here, but 
the end effect is pretty similar.


If you have more questions, please feel free to ask.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a mixin class?

2007-01-17 Thread Kent Johnson
(resending to the whole list)

Don Taylor wrote:
> I have a vague idea what a mixin class is, I would like to get a better 
> handle on it.
> 
> It is a term that is used quite often in Python circles, but I can't 
> find a definition.
> 
> I guess that because of multiple inheritance Python does not need a 
> formal way of specifying mixin classes so I presume that there is some 
> conventional interpretation/coding that is followed for mixin classes.
> 
> So, what constitutes a mixin class and what are the conventional ways to 
> denote them in code?
> 
> Don.
> 
> I notice that Ruby has single inheritance plus mixin classes so maybe 
> somebody who knows Ruby could throw some light on in what way a mixin is 
> different from normal superclass.

A mixin is a class that is not intended to stand on its own, rather it
adds some behaviour to the class that it is mixed in to. Some discussion
here:
http://en.wikipedia.org/wiki/Mixin
http://c2.com/cgi/wiki?Mixin

This page
http://c2.com/cgi/wiki?MixinsForPython
points to SocketServer.ForkingMixin and SocketServer.ThreadingMixin as
examples from the Python standard library.

HTH,
Kent


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


Re: [Tutor] Graphics with Python: wxPython vs. tkinter vs. PyCairo vs. PyX vs...

2007-01-17 Thread John Fouhy
On 18/01/07, Vijay Pattisapu <[EMAIL PROTECTED]> wrote:
> Hey friends--
>
> I've been looking through the archives and haven't found any
> comparative evaluation of Python graphics libraries...
>
> Which is the best (or your favorite), and for what tasks?

I only have experience with Tkinter and wxPython.

Tkinter is easy, and feels much more pythonic than wxPython.  However,
Tkinter doesn't have a wide range of widgets available (although Pmw
does a good job of faking some more).  Also, Tkinter doesn't use
native widgets, so it will be slower and Tkinter apps will look
foreign and ugly on most systems.

wxPython uses native widgets, so it looks nice.  The biggest problem
with wxPython is that it has evolved from a bare wrapper around C++ to
something more pythonic, but it hasn't left much behind.  There are
several programming styles (explicit IDs? implicit IDs? no IDs?
Keyword arguments or positional arguments?) and several ways of
positioning widgets (sizers, which are like pack/grid, or explicit
positioning, or one or two other methods).

The upshot is that you can write nice pythonic code, but a lot of the
examples you will find will be in a different style, and many of the
experienced coders are used to an older style of wx coding.

But wxPython is better than Tkinter for apps you're going to spend
time on, or show to other people.

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


Re: [Tutor] What is a mixin class?

2007-01-17 Thread Don Taylor
Don Taylor wrote:
> I have a vague idea what a mixin class is, I would like to get a better 
> handle on it.
> 

Thanks for the information and the links, I have a much better idea 
about mix-ins now.  I also found the following informative:

http://www.linuxjournal.com/article/4540
and
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498124

Don.

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


[Tutor] Beginners

2007-01-17 Thread raghu raghu

can i follow 'Learning Python' by oreily. Is it good for beginners? can any
one suggest me i am planning to buy  a  hard copy (text book)

--

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


Re: [Tutor] Beginners

2007-01-17 Thread wesley chun
On 1/17/07, raghu raghu <[EMAIL PROTECTED]> wrote:
> can i follow 'Learning Python' by oreily. Is it good for beginners? can any
> one suggest me i am planning to buy  a  hard copy (text book)


what is your programming background, what languages do you know
already?  or are you completely new to programming?  the books we
recommend are based on where you are coming from -- that will help you
find the right book for your needs.

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginners

2007-01-17 Thread raghu raghu

I dont have any programming background, i know only c language.

On 1/18/07, wesley chun <[EMAIL PROTECTED]> wrote:


On 1/17/07, raghu raghu <[EMAIL PROTECTED]> wrote:
> can i follow 'Learning Python' by oreily. Is it good for beginners? can
any
> one suggest me i am planning to buy  a  hard copy (text book)


what is your programming background, what languages do you know
already?  or are you completely new to programming?  the books we
recommend are based on where you are coming from -- that will help you
find the right book for your needs.

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com





--

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