Re: [Tutor] Python tutor

2006-03-28 Thread Anna Ravenscroft
On 3/27/06, Noufal Ibrahim <[EMAIL PROTECTED]
> wrote:
Greetings all,   Are there any programs for python that offer an "interactive" tutorial?Something on the lines of the builtin emacs tutorial (which isbasically just a buffer that tells you to try this and try that with
itself) or the Inkscape tutorial (which is an SVG document that comesalong with inkscape which has instructions on manipulating it so thatthe reader learns stuff). Another example that comes to mind is the

tcltutor program to learn TCL. It contains an instruction window, acode window and an output window. The user is told something, they tryit and the output is visible. I personally used it when I was learningTCL.
   The python tutorial is great and probably all one needs to learn thelanguage but I think a more interactive program to teach it might beuseful. I googled a little and found Guido van Robot although I'm not
sure if it's exactly like what I'm talking about. Are there any others?Do you all think it'll be a worthwhile project?There are several of us on the edupython list who want something like this but it hasn't (to my knowledge) been created yet. The best things out there so far, are  livewires,  guido von robot, and rur-ple. If you're interested in working on such a project, you're welcome to join us.
http://groups.google.com/group/edupython-- cordially,Anna--It is fate, but call it Italy if it pleases you, Vicar!

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


Re: [Tutor] Help with Game over program

2006-03-15 Thread Anna Ravenscroft
On 3/15/06, Brad Hills <[EMAIL PROTECTED]> wrote:
I'm 45 minutes into learning how to program using python. I am reading "Python programming for the absolute begginer" by  Michael Dawson, and the first thing I've done was write the "Game Over" program. Which I understand is also called the "Hello World" Program. My main problem at this point is when I try to run the program from the icon on my desktop it asks me which program to associate it with. The book makes it sound like it will automatically execute the program. Am I doing something wrong?

___Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutorWhat operating system are you using? Windowz? Mac? Anna
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] *args consumption

2006-03-12 Thread Anna Ravenscroft
On 3/12/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
Danny has given some reasons why this is not useful standard behaviour.If this is a behaviour you need for many functions, you could create adecorator that provides it so you don't have to include the same
boilerplate in each function.Decorators are functions that accept a function as an argument andreturn a new function as a result. Here is a decorator that will unpacka tuple argument:


Thanks for a great example of what decorators are good for. 

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


Re: [Tutor] Cannot Understand

2006-03-10 Thread Anna Ravenscroft
On 3/10/06, Alan Gauld <[EMAIL PROTECTED]> wrote:
>> Could you please explain this code?. f = lambda n: n-1 + abs(n-1) and f(n-1)*n or 1>> This is why lambdas are evil.It's not the lambda thats evil its the need in Python to limit
them to a single _expression_.If we writedef f(n):   return n-1 + abs(n-1) and f(n-1)*n or 1its just as obscure.

Someone writing a named function is less likely to feel constrained to do an obscure "one-liner".
if we writedef f(n)   if not ((n-1) + abs(n-1)):  return f(n-1) * n
   else return 1it gets a little clearer. And the only time the if _expression_is false is when n is zero or 1 so for the last time:def f(n)   if  n >1:  return f(n-1) * n   else:
  return 1

This, is clear, and the  kind of thinking that, imho, writing an
actual function promotes. The other useful thing with this is: if you
give it real name instead of an anonymous, meaningless f, you can
actually call the function from other modules, or reuse it in other
programs. Meaningful naming also enhances readability. How much did we
all have to go through to figure out that this was a factorial
function. What if, instead, it had actually been given a real name,
like, say "factor"... No need to work through the complex code just to
figure out what it was for. 
In fact its the factorial function in very strange disguise!If we could write the lambda as
f = lambda n:   if n>1:  return f(n-1) * n   else return 1is it so much more complex?> Officially, they are for creating "anonymous functions";> usually they only succeed in creating obscure unreadable drek.
Unfortunately that's true. But as a concept they are a fundamentalpart of computing science and its hard to understamnd higher orderprogramming or explore predicate calculus without themThey are also of course very useful as shortcuts but thats usually
where the cryptic code comes in. 

Precisely. They are used as shortcuts and usually are far too clever by
half. I've seen a few people like you who use them clearly;
unfortunately, that's not the norm. 

> In My Humble Opinion.And in mine, of course :-)

Well - the nice thing is that we both get to be right! ;-) And I think
it's useful to get the pros and cons out occasionally where newbies can
see that we don't all agree on everything, but that we can disagree
politely and still be helpful. 

Anna

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


Re: [Tutor] Cannot Understand

2006-03-10 Thread Anna Ravenscroft
On 3/10/06, Alan Gauld <[EMAIL PROTECTED]> wrote:
>> Could you please explain this code?. f = lambda n: n-1 + abs(n-1) and f(n-1)*n or 1>>>This is why lambdas are evil.I meant to add to my last post that even using lambdas this
is a weird attempt at a factorial fiunction.Here's the equivalent from my web tutor on functionalprogramming:>>> factorial = lambda n: ( (n <= 1) and 1) or...  
(factorial(n-1) * n)>>> factorial(5)120It uses the same basic constructs but much less esoteric trickery.Hopefully its slightly more readable, 

Much more readable. 

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


Re: [Tutor] Cannot Understand

2006-03-10 Thread Anna Ravenscroft
On 3/10/06, Edgar Antonio Rodriguez Velazco <[EMAIL PROTECTED]> wrote:
Hi,Could you please explain this code?.f = lambda n: n-1 + abs(n-1) and f(n-1)*n or 1
This is why lambdas are evil. Officially, they are for creating
"anonymous functions"; usually they only succeed in creating obscure
unreadable drek. In My Humble Opinion. 

Okay - start from the middle and work our way through:

n is your variable that you're passing as an argument to this unnamed function.

say, n were 10.

n-1 is 9
add EITHER:
absolute value of n-1 AND do a recursive call to f on 9 (and then on 8, and then on 7...), multiplied by n 
OR add 1.  

if you type it into your interpreter, here's the result:
>>> n = 10
>>> f = lambda n: n-1 + abs(n-1) and f(n-1)*n or 1
>>> print f(n)
3628800

Yuck. I hate reading lambdas. Personally, I think it's buttugly.

Anna

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


Re: [Tutor] problems with numbers in my python code

2006-03-10 Thread Anna Ravenscroft
On 3/10/06, sjw28 <[EMAIL PROTECTED]> wrote:
Basically, I have a code with is almost finished but I've having difficultlywith the last stage of the process. I have a program that gets assignsdifferent words with a different value via looking them up in a dictionary:
eg if THE is in the writing, it assigns 0.965 and once the whole passage is read it returns all the numbers in the formatas follows:['0.965', '1.000', '0.291', '1.000', '0.503']However, I can't seem to get the program to treat the numbers as numbers. If
I put them in the dictionary as 'THE' = int(0.965) the program returns 1.0and if I put 'THE' = float(0.965) it returns 0.9655549 or somethingsimilar. Neither of these are right! I basically need to access each item in
the string as a number, because for my last function I want to multiply themall together by each other.I have tried two bits of code for this last bit, but neither are working(I'm not sure about the first one but the second one should work I think if
I could figure out how to return the values as numbers):1st codevalue = codons[x] * codons[x+1]x = (int)x = 0print valuex +=2if (xnew_value = value * codons[x]
value = new_valuex +=1else:print new_valueThis gives the error messageTraceback (most recent call last):  File "C:\Python24\code2", line 88, in -toplevel-value = codons[x] * codons[x+1]
NameError: name 'x' is not definedCode 2 - the most likely codeprod = 1for item in (codons): prod *= itemprodprint prodGives this error message:Traceback (most recent call last):
  File "C:\Python24\code2", line 90, in -toplevel-for item in (codons): prod *= itemTypeError: can't multiply sequence by non-intCan anyone help me solve this problem?Thanks.


This is exactly what the decimal module was created for. 

It will take those strings, let you do your computations and give you
back the exact decimal values with exactly the precision you want. You
get to decide what type of rounding rules you want.

If you don't have it already (say you're using 2.3 for example) you can
get it from
http://www.taniquetil.com.ar/facundo/bdvfiles/get_decimal.html.
Otherwise, just import it and run help(decimal) to learn it. 

I'd show you but I just discovered I'm running 2.3 on this Mac so I need to upgrade. 

If you have trouble using decimal, let us know. 

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


Re: [Tutor] how does this list comprehension work?

2006-03-08 Thread Anna Ravenscroft
On 3/8/06, Christopher Spears <[EMAIL PROTECTED]> wrote:
I copied this program from Learning Python and got itto work on Windows XP:import sys, globprint sys.argv[1:]sys.argv = [item for arg in sys.argv for item inglob.glob(arg)]print sys.argv[1:]
What the program does is first print the glob and thena list of everything caught by the glob.  For example:['*.py']['showglob.py']The key to the script is the list comprehension, whichI am having trouble dissecting.  How do I go about
trying to figure out how it works?

Hi Christopher,

Others have ably deciphered this particular list comprehension (LC) for
you. I'm curious though, are you having trouble because this is a
nested list comprehension or do you not have a good grasp on LCs in
general? They can be a little tricky to grok at first. 

In case you're not completely clear on LCs in general, here's a bit
more description. If you are clear, just skip to the next email. ;-)


First off - a list comprehension creates a new list. It does so by
pulling items from an existing list or other iterable object.  So,
for example:

newlist = [item for item in oldlist]
print newlist

This example wasn't terribly useful - it just made a new list. You can,
however, *do* stuff to the item as you're passing it to the new list.
For example, if you want the squares of the items in the old list, you
coudl do:

oldlist = [1,2,3]
sqlist = [item*item for item in oldlist]

Another thing you can do is filter the items in the old list in some way. So, if you only want the even numbers you could do:

elist = [item for item in oldlist if item%2==0] # using modulo, which returns the remainder

You can use LCs with files, with dicts, with strings, with any iterable
object. It's really slick, really easy, pretty fast.  LCs can be a
little tricky, like I mentioned (especially when nested) but are quite
handy and fun once you get to know them. Just don't get so tricky that
it's hard to read your code. If it's getting hard to read, use a for
loop. 

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


Re: [Tutor] Analysing genetic code (DNA) using python

2006-03-06 Thread Anna Ravenscroft
On 3/6/06, sjw28 <[EMAIL PROTECTED]> wrote:
I have many notepad documents that all contain long chunks of geneticcode. They look something like this:atggctaaactgaccaagcgcatgcgtgttatccgcgagaaagttgatgcaaccaaacagtacgacatcaacgaagctatcgcactgctgaaagagctggcgactgctaaattcgtagaa
agcgtggacgtagctgttaacctcggcatcgacgctcgtaaatctgaccagaacgtacgtggtgcaactgtactgccgcacggtactggccgttccgttcgcgtagccgtatttacccaaBasically, I want to design a program using python that can open andread these documents. However, I want them to be read 3 base pairs at a
time (to analyse them codon by codon) and find the value that eachcodon has a value assigned to it. An example of this is below:** If the three base pairs were UUU the value assigned to it (from the
codon value table) would be 0.296The program has to read all the sequence three pairs at a time, then Iwant to get all the values for each codon, multiply them together andput them to the power of 1 / the length of the sequence in codons
(which is the length of the whole sequence divided by three).However, to make things even more complicated, the notebook sequencesare in lowercase and the codon value table is in uppercase, so thesequences need to be converted into uppercase. Also, the Ts in the DNA
sequences need to be changed to Us (again to match the codon valuetable). And finally, before the DNA sequences are read and analysed Ineed to remove the first 50 codons (i.e. the first 150 letters) and the
last 20 codons (the last 60 letters) from the DNA sequence. I've alsobeen having problems ensuring the program reads ALL the sequence 3letters at a time.I've tried various ways of doing this but keep coming unstuck along the
way. Has anyone got any suggestions for how they would tackle thisproblem?Thanks for any help recieved!
You've got a lot of pieces to your puzzle.


I would use  f.read() to read all of the file in, then a list
comprehension so you get only the codon characters (leaving out the
newlines).


A simple slicing of the list can give you each codon. 
something like might get you started:

f = open('codons.txt', 'r')

s = f.read()
l = [c for c in s if c != '\n']
r = len(l)

for x in range(0,r,3):
    y = x+3
    codon = l[x:y]
    print codon
    
f.close()


Use ''.join() to make them back into a string. From there, you could do
a lookup of the codon string in a dictionary. use the string method
s.upper() to uppercase your
codon string. 

Basically, figure out one problem at a time. Once that works, tackle
the next problem. Or, use something someone else already wrote for you,
like Kent suggests.
 
cordially,
Anna

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


Re: [Tutor] how to get the return value?

2006-03-05 Thread Anna Ravenscroft
On 3/5/06, ingo <[EMAIL PROTECTED]> wrote:
in news:[EMAIL PROTECTED] Kent Johnson wrote:[...]>>main(printtime(strf=None))>>[...]
 Anna, that results in an syntax error / invalid syntax>> It's very helpful to show the actual error and traceback.>Sorry, here it is:File "D:\Ingo\PyScript\VIDEOC~1.9\_ij\_time_lapse.py", line 23
def main(printtime(strf=None)):  ^SyntaxError: invalid syntax

Yep. I was suggesting calling printtime as part of your *call* to main, not when you define main. Sorry to be unclear. 

It appeared from your code that you never bound printtime to a variable
name when you called it (in fact, I don't remember you ever actually
*calling* it but I may be misremembering.)  If it's not bound to a
variable name, the function does its work and the return value just
disappears into the ether. So - to capture the return value, bind the
function call to a variable name. Or, alternately, pass the function
call directly as the argument for the function, in your case main(),
that wants to use the return value, such as:

main(printtime(strf=None))

Note that, when you define main in the first place, you'll need to ensure that it takes a parameter like:

def main(pt):
   dosomethingwith pt

so that when you later call main(), you can pass it an argument like
primetime() or whatever variable name you bound primetime()'s return
value to. 

Hope that makes more sense.

Anna

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


Re: [Tutor] how to get the return value?

2006-03-04 Thread Anna Ravenscroft
On 3/4/06, Ingo <[EMAIL PROTECTED]> wrote:
To make a time lapse video I've been playing with the sched module.There is one problem I run into, in the code below, how do I get thereturned value t from printtime into main?import timefrom sched import scheduler
class time_lapse(scheduler):def time_lapse(self, start_time, stop_time, interval, priority,action, argument):def lapse():action(*argument)i=self.enter(interval, priority, lapse, ())
if stop_time:if stop_timeself.cancel(i)self.enterabs(start_time, priority, lapse, ())def printtime(strf=None):t=time.time
()if strf:print time.strftime("%Y%m%d_%H%M%S")else:print time.localtime()return tdef main():schedule = time_lapse(time.time, time.sleep)start=
time.time()stop=list(time.localtime(start))stop[3]=stop[3]+2stop=time.mktime(stop)#schedule.time_lapse(None,None,5,1,printtime,()) #start now, runforeverschedule.time_lapse(start,stop,7,0,printtime,(1,))
schedule.run()if __name__ == "__main__":main()Ingo___Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
main(printtime(strf=None))

should do it. Alternately:

tt = printtime(strf=None)
main(tt)

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


Re: [Tutor] O.T.

2004-12-30 Thread Anna Ravenscroft
Jacob S. wrote:
I hate to sound weird...
Too late. ;-) Join the crowd!
Anna Martelli Ravenscroft
42, 2 children (13 and 11) live with their dad
Married this July to the martelli-bot (we read The Zen of Python at our 
wedding!). We currently live in Bologna, Italy.

Started learning Python in 2002 because I was bored to tears at my job 
as an Instruction Administrator. I'm not a progammer - I just use Python 
to get stuff done (and cuz it's fun!) I love that it is useful without 
having to become a hermit for 4 years studying the syntax. Before that, 
I had worked with computers as a "power" user and NT admin, and long 
long ago had programmed a little in Basic (on my beloved Commodore 64) 
and even learned a little Pascal back in College...

My background is in training and coaching, as well as office 
administration to pay the bills. I do presentations and editing as well.

Other interests include weightlifting, bicycling, reading, cooking.
We went hiking in the Italian Alps for our honeymoon and I fell in love 
with the mountains!

Great question. Nice to get to know the other folks on the list a little.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Strange Appending

2004-12-03 Thread Anna Ravenscroft
mdcooper wrote:
Hello,
I am trying to append a list to another list, but everytime I do, the new 
parent list has a new child list, but all the other lists have become the same 
as the new child list.

Code:
self._f.write(str(self.residue.atoms[int(t[0])-1].element) + ' ')
for m in t:
self._f.write(str(m)+' ')
self._f.write('\n')
self.a.append(t) # WHY DOES THIS NOT WORK?
print self.a
Output:
[[1, 234, 543]]
[[1, 234, 548], [1, 234, 548]]
[[1, 234, 59], [1, 234, 59], [1, 234, 59]]
[[1, 237, 543], [1, 237, 543], [1, 237, 543], [1, 237, 543]]
I'm guessing what you mean is:
for m in t:
self  # misc filewriting
self.a.append(m)  # appending *items* of t
print self.a
See if that little difference works.
Anna
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor