Re: [Tutor] Import Modules

2009-04-16 Thread ALAN GAULD


> In general, importing a package does not give access to members of a
> sub-package. You have to explicitly import the subpackage. For
> example,
> 
> In [1]: import xml
> In [2]: xml.dom.Node
> ---
> AttributeErrorTraceback (most recent call last)
> AttributeError: 'module' object has no attribute 'dom'

> There are some exceptions, notably os.path:
> In [5]: import os
> In [6]: os.path
> but that is a special case and requires special coding in the os module.

Interestingly I added the comment about sub packages specifically 
because I remembered os.path and assumed it was the norm! :-)

I really must read up on packages. I will need to for my v3 rewrite 
anyway!

Alan G.

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


[Tutor] importance of Docstring

2009-04-16 Thread mbikinyi brat
Dear ALL,
What is really the importance of Docstring in Python???
Regards,
Henry


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


Re: [Tutor] importance of Docstring

2009-04-16 Thread spir
Le Thu, 16 Apr 2009 02:04:25 -0700 (PDT),
mbikinyi brat  s'exprima ainsi:

> Dear ALL,
> What is really the importance of Docstring in Python???
> Regards,
> Henry

Very very very great, I guess ;-)   

Denis
--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] importance of Docstring

2009-04-16 Thread A.T.Hofkamp

mbikinyi brat wrote:

Dear ALL,
What is really the importance of Docstring in Python???
Regards,
Henry


The same as comments, except at function, class, and module level. In 
addition, Python provides hooks for extracting that information, and eg put it 
in a document such as the standardlib documentation.


Doc-strings are a means to document your design at the 'what' level rather 
than the lower 'how' level of code.



A short exercise:


def get_sum(val_lst):

  total = 0

  for val in val_lst:
total = total + val

  return total


Now what does this function do?
This is a 'what' question, namely, from a design point of view, if I call this 
function, what does it do, and what is its result-value?




You have got the "how", namely you can read the code, and imagine what steps 
are taken to compute the return value.


You have to do several deduction steps from the code to answer the question.
1. "total" is returned, so that variable represents the return-value of the 
function.

2. total is initialized to 0
3. total is iteratively incremented with each value from the val_lst
4. the for-loop as a whole thus computes the sum of val_lst.
5. that value is returned as function return value.
6. val_lst is not modified
7. its data values are not modified either.

So the answer to the question "what does this function do?" is that it 
implements a sum function over a list (or even, an iterable).

Also, 'total' represents the sum of the values summed so far.

Experienced programmers see this in a fraction of a second for functions like 
above. However, if you have a 200-line function with 30 variables (which 
nobody here has of course, you all follow the PEP 8 guide lines :) ) and a 
complex flow of control between statements, the above deduction steps cost 
time and effort. Also, there is the chance that you make a mistake in your 
reasoning, or that the original author of the code made a mistake and your 
reasoning breaks down. Eg



total = 0

def get_sum(val_lst):

  for val in val_lst:
total = total + val

  return total

Python has no problems whatsoever with this function. However, if you just 
read the function, it is not clear what the intention is. You'll have to find 
out how the global var 'total' is used to understand what the function does.



The above reasoning and/or deduction problems originate from the fact that you 
are trying to reason towards the higher "what" level of the function from the 
low "how" level.

Comments and doc-string are helpful to eliminate the reasoning effort:

def get_sum(val_lst):
  """
  Add all values from the 'val_lst' iterable together and return the sum.
  """

  total = 0   # Sum of values added so far.

  # Sum all values of the list.
  for val in val_lst:
total = total + val

  return total

Now I have added some comment stating what the function does, what the meaning 
of the 'total' variable is, and what the purpose of the loop is.
Effectively, I have stated the 'what' reasoning results that we have deduced 
previously from the code.


Adding comments like this saves you the effort to do the same reasoning 
exercise I did before writing the code, and it eliminates some possible 
mistakes in your deduction reasoning.
In addition, it clearly states the intended function (the 'what') of the 
function as a whole, the variable and the parts of the code, making it easier 
for you to verify whether the code does the right thing at each point, thus 
making code-reviewing and bug-finding easier.


Now, tell me, what is easier?
1. Reading the whole code, doing the entire deduction process to discover what 
a function does, or
2. Read a single line in a doc-string or comment telling you exactly what you 
want to know.



Note that unlike popular belief, in my opinion, code should be written in such 
a way that it is easy to read. Code is written only once, and in general read 
about 3 times, I seem to remember (people have been researching this).
Code is much like any other document, the fact that a computer can also 
interpret it, should mostly be considered a lucky coindence.


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


Re: [Tutor] Import Modules

2009-04-16 Thread Kent Johnson
On Thu, Apr 16, 2009 at 3:21 AM, ALAN GAULD  wrote:
>
>
>> In general, importing a package does not give access to members of a
>> sub-package.
>
> Interestingly I added the comment about sub packages specifically
> because I remembered os.path and assumed it was the norm! :-)

Yes, it took me a long time to realize that it is not. Strictly
speaking I don't think os is even a package, it is a module that
imports another module as it's path attribute. There is no os
directory or os/__init__.py or os/path.py.

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


[Tutor] bootstrapping

2009-04-16 Thread spir
Hello,

This question is rather intended to people who have some knowledge on parser 
generation and/or on self-compiling languages.
I have a custom PEG parser generator in python, called 'pijnu', just 
bootstrapped. Meaning it's a kind of specific language which generator 
(compiler) is able to produce itself by parsing its own meta-grammar, written 
in itself. Here is a picture that tries to illustrate how this works:

text.lang[langParser.py]==> textTree   [process.py]==> 
^
|
+---+
|
^
lang.pijnu   [pijnuParser.py]   ==> langTree   [generator.py]  ==> 
^
|
+---+
|   |
v   ^
pijnu.pijnu  [pijnuParser.py]   ==> pijnuTree  [generator.py]  ==> 

* level 1: use of a parser generated by pijnu to parse a user text, then 
process further
* level 2: use of pijnu to generate a parser for a user language (or any other 
format)
* level 3: use of pijnu to generate its own parser


I'm an amateur and it's the first time I do such a thing. I thought a main 
advantage of bootstrapping was the ability to produce a compiler of next 
generation by feeding the present parser/generator with a modified grammar.

Actually, this does not work for me. The reason I guess is that a feature 
addition or modification (semantic level) usually comes with a change in the 
language itself (syntactic, or rather grammatical, level). This latter change 
will prevent the current parser to accept the modified grammar: I need then to 
introduce the change in the current parser itself instead of only in the 
grammar.
Is there anything wrong in my method and/or reflexion?

[I still find it worthful anyway to recursively produce the generator by 
itself, not only for intellectual satisfaction, but also as way to validate the 
change.]

Denis
--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Building VST's with Python

2009-04-16 Thread Logan Thomas
I'm new to python but do have a little programming knowledge with C++I
got into programming so I could build Virtual Instruments because I love the
world of sound and creating instruments that shape it...I have tried to find
a program that would be an ideal vehicle for this task...I've narrowed it
down to Delphi or PythonC++ would take too long for me to learn and
would take to long to build an application...I was wondering if you could
direct me to some tutorials or source code that might involve building
virtual instruments or even gamesThanks for listening, Logan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] code publishing

2009-04-16 Thread spir
Hello,

I have no clue whether there is a common place to publish some (free like the 
air) python source code for review, use, evolution...
(It's about 17 5kb.)

Denis
--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] code publishing

2009-04-16 Thread Sander Sweers
2009/4/16 spir :
> I have no clue whether there is a common place to publish some (free like the 
> air) python source code for review, use, evolution...
> (It's about 17 5kb.)

Maybe http://python.pastebin.com is what you are looking for?

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


Re: [Tutor] code publishing

2009-04-16 Thread Kent Johnson
On Thu, Apr 16, 2009 at 6:59 AM, spir  wrote:
> Hello,
>
> I have no clue whether there is a common place to publish some (free like the 
> air) python source code for review, use, evolution...
> (It's about 17 5kb.)

If you just want a temporary place to put it for comments then
pastebin is good. For open-source project hosting there are many
options; google code, github and bitbucket are currently popular. For
publishing, pypi  is the standard.

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


Re: [Tutor] importance of Docstring

2009-04-16 Thread python
Albert,

That was a great writeup on docstrings. I'm going to share that with my
dev team.

Thank you!
Malcolm
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] print items in a list...

2009-04-16 Thread Spencer Parker
I have a script that is putting elements into a list.  Now instead of adding
a for blah in blah statement...is there an easy way to print out the
elements in a list that is similar to this?  I don't have an absolute length
on the size of the list either since it changes depending on what I am
searching for.  I have it finding the elements through a regular expression
and then putting those into a list.  Separate lists for each item I am
searching for...if it find this put it into this list...else put into this
list.  Then I want to write each list to a text file.  trying to clean up
messy code for the most part.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] PyCon Videos

2009-04-16 Thread Martin Walsh
Hi All,

Not sure if it's common knowledge, particularly for those who didn't
make it to PyCon this year, but all of the talks were recorded and will
be available online in good time, thanks to Carl Karsten and his merry
band of A/V volunteers. I can't even begin to grasp how much work is
required to accomplish such a monumental task.

I noticed this morning that the tutorials, are making their way to the
blip.tv site, including "Internet Programming with Python" presented by
our very own Wesley Chun. And I presume "An Introduction to
Object-Oriented Programming", announced on this list a couple months ago
by Michael Goldwasser, will be available soon as well. I'm really
looking forward to watching both.

Not a replacement for attending a PyCon in person -- which I highly
recommend for anyone interested in python, no matter your skill level --
but certainly the next best thing.

http://pycon.blip.tv/posts?view=archive&nsfw=dc

It seems, many presenters included links to additional material (code
examples, slides, etc) on the scheduled-talks page [1] -- not sure about
the tutorials [2], but it doesn't look like it.

[1] http://us.pycon.org/2009/conference/schedule/
[2] http://us.pycon.org/2009/tutorials/schedule/

HTH,
Marty

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


Re: [Tutor] print items in a list...

2009-04-16 Thread spir
Le Thu, 16 Apr 2009 11:37:35 -0600,
Spencer Parker  s'exprima ainsi:

> trying to clean up
> messy code for the most part

How does it look like?

Denis
--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] print output

2009-04-16 Thread mbikinyi brat
 
Dear ALL,
I have defined variables as below and when I call them using the print 
function, I have something discontinous as in pink colour. How do I call it so 
that my output should be as in blue
>>> counter=101
>>> miles=1000
>>> name="joe"

>>> print counter
101
>>> print miles
1000
>>> print name
joe
>>> 
What I want:(Output)

101
1000
joe

Regards,
Henry


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


[Tutor] Fwd: Import Modules

2009-04-16 Thread Kent Johnson
Forwarding to the list


-- Forwarded message --
From: GG Labs 10 
Date: Thu, Apr 16, 2009 at 10:28 AM
Subject: Re: [Tutor] Import Modules
To: Kent Johnson 


Thankyou,

i think i've understood the problem.

Now, another App Engine related question:

This code:

---
import google

print "Content-Type: text/html"
print ""

print ""
print ""
print ""

print "google",dir(google)

print ""
---

Gives this output:

---
google ['__builtins__', '__doc__', '__file__', '__name__', '__path__',
'appengine', 'net', 'pyglib']
---

appengine, net and pyglib are path (so subpackages of google package),
not modules.

Why when i import the google package all those subpackages are
imported? In the __init__.py file i can't find anything (__all__ for
example), and if i add a directory with a .py file at the same level
of appengine, it doesn't get imported.

Thankyou


2009/4/16 Kent Johnson 
>
> On Thu, Apr 16, 2009 at 3:21 AM, ALAN GAULD  wrote:
> >
> >
> >> In general, importing a package does not give access to members of a
> >> sub-package.
> >
> > Interestingly I added the comment about sub packages specifically
> > because I remembered os.path and assumed it was the norm! :-)
>
> Yes, it took me a long time to realize that it is not. Strictly
> speaking I don't think os is even a package, it is a module that
> imports another module as it's path attribute. There is no os
> directory or os/__init__.py or os/path.py.
>
> Kent
> ___
> Tutor maillist  -  tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print items in a list...

2009-04-16 Thread Kent Johnson
On Thu, Apr 16, 2009 at 1:37 PM, Spencer Parker  wrote:
> I have a script that is putting elements into a list.  Now instead of adding
> a for blah in blah statement...is there an easy way to print out the
> elements in a list that is similar to this?  I don't have an absolute length
> on the size of the list either since it changes depending on what I am
> searching for.  I have it finding the elements through a regular expression
> and then putting those into a list.  Separate lists for each item I am
> searching for...if it find this put it into this list...else put into this
> list.  Then I want to write each list to a text file.  trying to clean up
> messy code for the most part.

I don't understand the question. An example of what you are doing, or
your current code, would help.

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


Re: [Tutor] print output

2009-04-16 Thread vishwajeet singh
try this print str(counter) + "\n" + str(miles) + "\n"+ name

On Thu, Apr 16, 2009 at 11:24 PM, mbikinyi brat wrote:

> **
> *Dear ALL,*
> I have defined variables as below and when I call them using the print
> function, I have something discontinous as in pink colour. How do I call it
> so that my output should be as in blue
> *>>> counter=101
> >>> miles=1000
> >>> name="joe"
> *
> >>> print counter
> 101
> >>> print miles
> 1000
> >>> print name
> joe
> >>>
> What I want:(Output)
>
> 101
> 1000
> joe
> Regards,
> Henry
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Cheers,
Vishwajeet
http://www.singhvishwajeet.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print output

2009-04-16 Thread Dave Angel

mbikinyi brat  wrote:


Dear ALL,
I have defined variables as below and when I call them using the print 
function, I have something discontinous as in pink colour. How do I call it so 
that my output should be as in blue
  

counter=101
miles=1000
name="joe"



  

print counter


101
  

print miles


1000
  

print name


joe
  
What I want:(Output)


101
1000
joe

Regards,
Henry


  
I assume you're just asking all the output to come out at the same 
time.  Two ways to do that:
 1)   put multiple print statements (not print functions, as you're not 
using Python 3.0) on the line, like

  print counter; print miles; print name

 2) Use newlines in a single print statement, to get them onto separate 
lines.

print counter, "\n", miles, "\n", name

  3) Write the whole thing in a definition, and call it.

   def  myfun():


counter=101
miles=1000
name="joe"
print counter
print miles
print name
  


myfun()

(I can't seem to get those funny vertical bars to edit right, so this 
may not be formatted quite right.)


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


[Tutor] beginners resources list, Python education events

2009-04-16 Thread wesley chun
hey all,

i'm trying to collate a complete newbie-to-programming list of
resources and was wondering if you could help me fill in the holes...
thx in advance!

BOOKS
Python Programming for the Absolute Beginner (Dawson, 2005)
Python For Dummies (Maruch, Maruch, 2006)
Python Programming: An Introduction to Computer Science (Zelle, 2003)
Introduction to Computing and Programming in Python (Guzdial, 2004)
How To Think Like a Computer Scientist (Elkner, Downey, Meyers, 2002)
Python: Visual QuickStart Guide (Fehily, 2001)
Learn to Program Using Python (Gauld, 2000)

ONLINE
How to Think like a Computer Scientist
http://openbookproject.net/thinkCSpy

Alan Gauld's Learning to Program
http://www.freenetpages.co.uk/hp/alan.gauld

LiveWires Python Course
http://livewires.org.uk/python

A Byte of Python
http://byteofpython.info

Guido van Robot
http://gvr.sf.net

Instant Hacking: Learning to Program with Python
http://hetland.org/writing/instant-hacking.html

Snake Wrangling for Kids
http://briggs.net.nz/log/writing/snake-wrangling-for-kids

please let me know if i'm missing anything as well as if any
information is incorrect or out-of-date. also, does anyone know the
latest about Alice (in particular, did they switch from Python to
Java?) also, are there any good turtle ot vpython (or other 3D)
drawing tools? also, are there any intros that use PyGame to teach
programming via game-writing?

on a somewhat related note, i'm doing 3 Python knowledge/education
events within the next few months.

1. APRIL: O'Reilly/Safari Books Online "What is Python?" webcast (FREE to all)
http://www.safaribooksonline.com/events/WhatIsPython.html

2. JUNE: Comprehensive Introduction to Python (3-day training course
in San Francisco open to everyone!)
http://mail.python.org/pipermail/python-list/2009-April/708428.html

3. JULY: O'Reilly Open Source Convention Tutorial instructor and
session speaker (San Jose, CA)
http://en.oreilly.com/oscon2009/public/schedule/speaker/44470

thanks!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
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


[Tutor] for loop

2009-04-16 Thread mbikinyi brat
Dear ALL,
I am a beginner in python and I just copied the code in blue below and execute 
and had the result in green. Then I thought letter should be a special word in 
python. Then I now replace letter whith chic  and yet the same results is 
obtained. I cannot reconcile them. Can someone explained to me please?
>>> for letter in "python":
 print "Current letter:", letter
 
Current letter: p
Current letter: y
Current letter: t
Current letter: h
Current letter: o
Current letter: n
 
>>> for chic in "python":
 print "chic:", chic
 
chic: p
chic: y
chic: t
chic: h
chic: o
chic: n

 
 
Regards,
Henry


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


Re: [Tutor] for loop

2009-04-16 Thread Emile van Sebille

mbikinyi brat wrote:

Dear ALL,
I am a beginner in python and I just copied the code in blue below and 
execute and had the result in green. Then I thought *letter* should be a 
special word in python. Then I now replace letter whith *chic*  and yet 
the same results is obtained. 


of course -- changing the name of the variable used to hold results 
shouldn't change the outcome.


I cannot reconcile them. Can someone

explained to me please?


If you expect something different, then you need to write a different 
program.  What did you want to happen?


Emile



*>>> for letter in "python":
 print "Current letter:", letter*
 
*Current letter: p

Current letter: y
Current letter: t
Current letter: h
Current letter: o*
*Current letter: n*
** 
*>>> for chic in "python":

 print "chic:", chic*
* 
chic: p

chic: y
chic: t
chic: h
chic: o
chic: n*
*
 *
* *
*Regards,*
*Henry*





___
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] beginners resources list, Python education events

2009-04-16 Thread David

wesley chun wrote:

hey all,

i'm trying to collate a complete newbie-to-programming list of
resources and was wondering if you could help me fill in the holes...
thx in advance!


Hi wesley,

A couple I liked;
One Day of IDLE Toying
http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html
Dive Into Python
http://www.diveintopython.org/toc/index.html
A Python Course
http://www.upriss.org.uk/python/PythonCourse.html


--
Powered by Gentoo GNU/Linux
http://linuxcrazy.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] importance of Docstring

2009-04-16 Thread Alan Gauld


"mbikinyi brat"  wrote 


What is really the importance of Docstring in Python???


Some really great comments about comments, however the 
significance of docstrings beyond normal comments is that 
tools like help() work with them


Thus:


def f(x):

'''f(x) -> x+5'''
return x+5


help(f)

Help on function f in module __main__:

f(x)
   f(x) -> x+5





So immediately help() knows about f and can read its docstring.
This is a huge help when using other peoples modules, or even 
your own modules that you haven't worked on for a while. You 
can import them and read the doctrings wiothout actually opening 
the code in an editor (which you would need to do for normal 
comments)


So they are not essential but they do have a very real use 
beyond simple comments..


I say a bit more about docstrings in my tutorial topic 
"Add a little style"


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] Building VST's with Python

2009-04-16 Thread Alan Gauld


"Logan Thomas"  wrote

I'm new to python but do have a little programming knowledge with 
C++I
got into programming so I could build Virtual Instruments because I love 
the

world of sound and creating instruments that shape it..I was wondering if
you could direct me to some tutorials or source code that might involve 
building

virtual instruments or even games


The obvious place would be pyGame I guess...

It does have some support for sounds.
There are also some sound modules in the standard library

And for the Mac the QuickTime stuff is available although I'm not
sure if it works for other platforms.


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



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


Re: [Tutor] print output

2009-04-16 Thread Alan Gauld


"mbikinyi brat"  wrote 
I have defined variables as below and when I call them using 
the print function, I have something discontinous as in pink colour. 
How do I call it so that my output should be as in blue

print counter

101

print miles

1000

print name

joe




This is because you are using the interactive interpreter which 
evaluates eah line as you type it. If you type your code into a file
called, say myprints.py and execute that from an OS prompt you 
will get the output you want.


Alternativbely at the >>> prompt you can either concatenate all
the variables separated by newlines or just put all 3 commands 
on a sinlgle line separated by semicolons:



print 5; print 6; print 7

5
6
7




HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] beginners resources list, Python education events

2009-04-16 Thread Alan Gauld


"wesley chun"  wrote


Alan Gauld's Learning to Program
http://www.freenetpages.co.uk/hp/alan.gauld


Now at:

http://www.alan-g.me.uk/  for Python v2.x
http://www.alan-g.me.uk/l2p for Python v3 and under construction,
   although well on the 
way now...


The freenetpages site is scheduled to close and
they are not allowing any updates, hence the move


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



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


Re: [Tutor] for loop

2009-04-16 Thread Alan Gauld
"mbikinyi brat"  wrote 

 Then I thought letter should be a special word in python. 


Why did you think that?
Python has a very specific (and small) set of keywords that have 
special meaning - things like for, while and print


But you can call variables pretty much whatever you like and 
Python will be happy. In fact the more meaningful the better, 
so letter is a very good choice.


Then I now replace letter whith chic and yet the same results 
is obtained. 



Current letter: p
Current letter: y

chic: p
chic: y

They are not the same results, one says 'letter' and the other 
says 'chic', just as you told it to. In every other respect your 
code is identical so the output is identical. Python just does 
what you tell it to, it is not actually intelligent, it just interprets 
the commands you give it.


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] beginners resources list, Python education events

2009-04-16 Thread Gregor Lingl



wesley chun schrieb:

hey all,
...
Python: Visual QuickStart Guide (Fehily, 2001)
  

This is outdated. There is a new one:

Toby Donaldson:  Python: Visual QuickStart Guide (2008)

Learn to Program Using Python (Gauld, 2000)

ONLINE
How to Think like a Computer Scientist
http://openbookproject.net/thinkCSpy

  

...

please let me know if i'm missing anything as well as if any
information is incorrect or out-of-date. also, does anyone know the
latest about Alice (in particular, did they switch from Python to
Java?) also, are there any good turtle ot vpython (or other 3D)
drawing tools? 
As I'm the author of Pythons new turtle module (2.6 and 3.0), I'm - 
naturally -
thinking that it is a good turtle drawing tool. In fact it is much more 
than merely a
turtle module and includes features to create event driven programs and 
other things.


As it is part of the Python standard distribution it can be used 'out of 
the box'


So I think this deserves to be mentioned.
(Did you not know about it or do you dislike it? If cou are really 
interested in it
have a look at this:  


http://pycon.blip.tv/file/1947495/

)

Generally the Edu-Sig page has been reorganized recently by Andre Roberge
and now contains a lot of up-to-date information on your topic.

Regards,
Gregor




also, are there any intros that use PyGame to teach
programming via game-writing?

on a somewhat related note, i'm doing 3 Python knowledge/education
events within the next few months.

1. APRIL: O'Reilly/Safari Books Online "What is Python?" webcast (FREE to all)
http://www.safaribooksonline.com/events/WhatIsPython.html

2. JUNE: Comprehensive Introduction to Python (3-day training course
in San Francisco open to everyone!)
http://mail.python.org/pipermail/python-list/2009-April/708428.html

3. JULY: O'Reilly Open Source Convention Tutorial instructor and
session speaker (San Jose, CA)
http://en.oreilly.com/oscon2009/public/schedule/speaker/44470

thanks!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
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


  

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


Re: [Tutor] importance of Docstring

2009-04-16 Thread wesley chun
 def f(x):
>
> '''f(x) -> x+5'''
> return x+5
>
 help(f)
>
> Help on function f in module __main__:
>
> f(x)
>   f(x) -> x+5

another thing that has not been mentioned is that if you put
docstrings everywhere, you can use tools like Epydoc, doxygen, or
sphinx to generate full documentation of an entire codebase, all
because developers made it easy by dropping in useful documentation in
their docstrings instead of just 1-line comments.

on top of this, you can add extra comments to DEMONSTRATE how the
function works, i.e.,

def f(x):
   """
   f(x) -> x+5

   >>> f(123)
   128
   """
   return x+5


this save your users critical time it takes to understand the code and
how it works. on top of THAT, you can use the doctest module to *run*
that code to confirm it works (or doesn't). try adding that code plus
the 5 lines below to a file called f.py:

def _test():
import doctest
doctest.testmod()

if __name__ == '__main__':
_test()

now run it:

$ f.py
$

you get no output which means everything worked! if you want to see it
in action, use "-v":

$ f.py -v
Trying:
f(123)
Expecting:
128
ok
2 items had no tests:
__main__
__main__._test
1 items passed all tests:
   1 tests in __main__.f
1 tests in 3 items.
1 passed and 0 failed.
Test passed.

it tells you how many possible places you can add doctests, how many
were found, and what the results were.

if you had an error, then you would definitely get output, with or
without the "-v". let's change our docstring output to the wrong
answer and rerun:

$ f.py
**
File "f.py", line 7, in __main__.f
Failed example:
f(124)
Expected:
128
Got:
129
**
1 items had failures:
   1 of   1 in __main__.f
***Test Failed*** 1 failures.

of course, you can also implement doctesting along with your normal
unit tests too and not necessarily have to add those 5 lines to all of
your modules.

hope you can now see the usefulness and importance of docstrings.
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
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 resources list, Python education events

2009-04-16 Thread Alan Gauld


"wesley chun"  wrote


i'm trying to collate a complete newbie-to-programming list of
resources and was wondering if you could help me fill in the holes...
thx in advance!


What about the free (and even the pay for) videos on the 
ShowMeDo site. I thought they were pretty good especially for 
showing how to use the tools, like IDLE, Eclipse/PyDev, pdb etc


Alan G.

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


[Tutor] list to string and string to list

2009-04-16 Thread johnf
I am dealing with a database field that can only store strings.
I want to save the list to the field and when I retrieve the string convert it 
back to a list.

But this does NOT work.
mylist=[1,2,3,4]
mystr=str(mylist)

newlist= list(mystr)

I keep thinking there must be a simple way of get this done.
-- 
John Fabiani
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list to string and string to list

2009-04-16 Thread johnf
On Thursday 16 April 2009 03:52:02 pm johnf wrote:
> I am dealing with a database field that can only store strings.
> I want to save the list to the field and when I retrieve the string convert
> it back to a list.
>
> But this does NOT work.
> mylist=[1,2,3,4]
> mystr=str(mylist)
>
> newlist= list(mystr)
>
> I keep thinking there must be a simple way of get this done.

Is this a good way?
 newlist = eval(mystr)


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


Re: [Tutor] Here's something to talk about

2009-04-16 Thread Ricardo Aráoz
Weidner, Ronald wrote:
> One of your points represents a great opportunity to make mine.  Suppose
> this code is several years old.  Now we have a new requirement that 
> states we need to work with the data as you described above. How
> much of the existing code would you have to change to make that change 
> happen?  The answer is exactly one line.  And here is the line...
>
> exporter = SQLExporter("itemTable")
>
> And you would change the line to something like...
>
> exporter = SQLTupleExporter("itemTable")
>
> or perhaps...
>
> exporter = ExportItemToDatabase('127.0.0.1', 'user', 'pass', 'schema')
>
> Of course, you would need to define a class to go along with your change
> but all of that will be NEW code.  Not a change to existing code.  So,
> all of your existing code should still be valid and tested.  The only
> thing you'll need to do validate and test your new code.
>   
The question I usually make to myself is not how many lines of code do I
have to change. I'd rather think of how much of the code do I have to
read and understand before I know which lines to change, and how hard
will it be to understand it.
If I read just a few lines and then know I have to replace """exporter =
SQLExporter("itemTable")""" with """exporter =
SQLTupleExporter("itemTable")""" in a thousand places, that's ok with
me, the editor can handle this flawlessly in a few seconds. I'd rather
have simpler code that I can understand easily and modular enough that I
can find what to change without reading and understanding the whole thing.




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


Re: [Tutor] list to string and string to list

2009-04-16 Thread Kent Johnson
On Thu, Apr 16, 2009 at 6:52 PM, johnf  wrote:
> I am dealing with a database field that can only store strings.
> I want to save the list to the field and when I retrieve the string convert it
> back to a list.
>
> But this does NOT work.
> mylist=[1,2,3,4]
> mystr=str(mylist)
>
> newlist= list(mystr)
>
> I keep thinking there must be a simple way of get this done.

You can use eval() but that is generally discouraged as a security hole.

If the list will just contain integers you can do something ad hoc:
In [7]: mylist=[1,2,3,4]

In [8]: mystr=str(mylist)

In [10]: newlist = map(int, mystr[1:-1].split(','))

In [11]: newlist
Out[11]: [1, 2, 3, 4]

In Python 3 you can use ast.literal_eval().
>>> mylist = [1, 2, 3, 4]
>>> mystr = str(mylist)
>>> import ast
>>> ast.literal_eval(mystr)
[1, 2, 3, 4]

You can use these recipes:
http://code.activestate.com/recipes/511473/
http://code.activestate.com/recipes/364469/

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


Re: [Tutor] list to string and string to list

2009-04-16 Thread Alan Gauld


"johnf"  wrote

I want to save the list to the field and when I retrieve the string 
convert

it back to a list.

But this does NOT work.
mylist=[1,2,3,4]
mystr=str(mylist)

newlist= list(mystr)

I keep thinking there must be a simple way of get this done.


Is this a good way?
newlist = eval(mystr)


eval has all sorts of security implications so I wouldn't recommend
it where you are reading data fropm an external source.

One thing that might work is this:


L = [1,2,3,4,5]
s1 = ','.join(str(n) for n in L)
s1

'1,2,3,4,5'

newlist = [int(n) for n in s1.split(',')]
newlist

[1, 2, 3, 4, 5]

Provided your original data doesn't have commas to start with
it should work, I think... And the data needs to be pretty
homogenous to allow a single conversion function.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



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


Re: [Tutor] list to string and string to list

2009-04-16 Thread johnf
On Thursday 16 April 2009 05:04:39 pm Alan Gauld wrote:
> "johnf"  wrote
>
> >> I want to save the list to the field and when I retrieve the string
> >> convert
> >> it back to a list.
> >>
> >> But this does NOT work.
> >> mylist=[1,2,3,4]
> >> mystr=str(mylist)
> >>
> >> newlist= list(mystr)
> >>
> >> I keep thinking there must be a simple way of get this done.
> >
> > Is this a good way?
> > newlist = eval(mystr)
>
> eval has all sorts of security implications so I wouldn't recommend
> it where you are reading data fropm an external source.
>
> One thing that might work is this:
> >>> L = [1,2,3,4,5]
> >>> s1 = ','.join(str(n) for n in L)
> >>> s1
>
> '1,2,3,4,5'
>
> >>> newlist = [int(n) for n in s1.split(',')]
> >>> newlist
>
> [1, 2, 3, 4, 5]
>
> Provided your original data doesn't have commas to start with
> it should work, I think... And the data needs to be pretty
> homogenous to allow a single conversion function.

Kent Johnson suggested 

newlist = map(int, mystr[1:-1].split(','))

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


Re: [Tutor] list to string and string to list

2009-04-16 Thread Martin Walsh
johnf wrote:
> On Thursday 16 April 2009 05:04:39 pm Alan Gauld wrote:
>> "johnf"  wrote
>>
 I want to save the list to the field and when I retrieve the string
 convert
 it back to a list.

 But this does NOT work.
 mylist=[1,2,3,4]
 mystr=str(mylist)

 newlist= list(mystr)

 I keep thinking there must be a simple way of get this done.
>>> Is this a good way?
>>> newlist = eval(mystr)
>> eval has all sorts of security implications so I wouldn't recommend
>> it where you are reading data fropm an external source.
>>
>> One thing that might work is this:
> L = [1,2,3,4,5]
> s1 = ','.join(str(n) for n in L)
> s1
>> '1,2,3,4,5'
>>
> newlist = [int(n) for n in s1.split(',')]
> newlist
>> [1, 2, 3, 4, 5]
>>
>> Provided your original data doesn't have commas to start with
>> it should work, I think... And the data needs to be pretty
>> homogenous to allow a single conversion function.
> 
> Kent Johnson suggested 
> 
> newlist = map(int, mystr[1:-1].split(','))
> 

Maybe that's a question in disguise, but I would think both suggestions
are valid.

Another, less valid, suggestion would be to pickle or shelve the list
before storing it in the database -- although this poses similar
security implications as eval. And the resulting string is not
particularly easy to read, if that's important to you.

import cPickle as pickle
mylist = [1,2,3,4]

mystr = pickle.dumps(mylist)
# '(lp1\nI1\naI2\naI3\naI4\na.'

newlist = pickle.loads(mystr)
# [1, 2, 3, 4]

HTH,
Marty


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


Re: [Tutor] list to string and string to list

2009-04-16 Thread Martin Walsh
Martin Walsh wrote:
> johnf wrote:
>> On Thursday 16 April 2009 05:04:39 pm Alan Gauld wrote:
>>> "johnf"  wrote
>>>
> I want to save the list to the field and when I retrieve the string
> convert
> it back to a list.
>
> But this does NOT work.
> mylist=[1,2,3,4]
> mystr=str(mylist)
>
> newlist= list(mystr)
>
> I keep thinking there must be a simple way of get this done.
 Is this a good way?
 newlist = eval(mystr)
>>> eval has all sorts of security implications so I wouldn't recommend
>>> it where you are reading data fropm an external source.
>>>
>>> One thing that might work is this:
>> L = [1,2,3,4,5]
>> s1 = ','.join(str(n) for n in L)
>> s1
>>> '1,2,3,4,5'
>>>
>> newlist = [int(n) for n in s1.split(',')]
>> newlist
>>> [1, 2, 3, 4, 5]
>>>
>>> Provided your original data doesn't have commas to start with
>>> it should work, I think... And the data needs to be pretty
>>> homogenous to allow a single conversion function.
>> Kent Johnson suggested 
>>
>> newlist = map(int, mystr[1:-1].split(','))
>>
> 
> Maybe that's a question in disguise, but I would think both suggestions
> are valid.
> 
> Another, less valid, suggestion would be to pickle or shelve the list
> before storing it in the database -- although this poses similar
> security implications as eval. And the resulting string is not
> particularly easy to read, if that's important to you.
> 
> import cPickle as pickle
> mylist = [1,2,3,4]
> 
> mystr = pickle.dumps(mylist)
> # '(lp1\nI1\naI2\naI3\naI4\na.'
> 
> newlist = pickle.loads(mystr)
> # [1, 2, 3, 4]

Sorry to reply to my own post, but the json module in python 2.6+
(formerly 3rd party, simplejson) might work for your purposes also. But
I must confess, I'm not that familiar.

import json

s = json.dumps([1, 2, 3, 4])
# '[1, 2, 3, 4]'
l = json.loads(s)
# [1, 2, 3, 4]

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


Re: [Tutor] for loop

2009-04-16 Thread R. Alan Monroe


 for letter in "python":
>  print "Current letter:", letter

 for chic in "python":
>  print "chic:", chic

When you write a for-in loop, you can use any variable name you feel
like using (letter, chic, mycoolvariable, x, etc.) It's that simple :)

Alan

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


Re: [Tutor] for loop

2009-04-16 Thread W W
On Thu, Apr 16, 2009 at 3:45 PM, mbikinyi brat wrote:

> Dear ALL,
> I am a beginner in python and I just copied the code in blue below and
> execute and had the result in green. Then I thought *letter* should be a
> special word in python. Then I now replace letter whith *chic*  and yet
> the same results is obtained. I cannot reconcile them. Can someone explained
> to me please?
>

I think your confusion lies in how python for loops work.

"Python" in this case is a string. You could replace it with "Spam"
"Knights" or "Ni", if you so desire. A string is an iterable - in other
words, you can iterate over it automatically.

With a language like C++ you would write something like this:

string foo = "python";
for(int x = 0; x < foo.size; x++){
cout << foo.at(x) << endl;
}

to iterate over the string. Python takes care of that for you with any
iterable type (list, set, etc.)

for item in [1, 2, 3]:
print item

for letter in "word":
print word

for foobar in (1, 0, 234, 'hello', 'foo'):
print foobar

It all works the same. It's rather useful :)

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