Re: [Tutor] Automating web page parsing

2006-03-29 Thread Bob Gailer
Srinivas Iyyer wrote:
> Dear group, 
>
> ***Disclaimer***Not suitable for BioPython list***
>
> I work with GeneChips to analyze human gene expression
> patterns. These genechips are various kinds one of the
> variety is made at Stanford University. In a typical
> experiment, an experimenter uses roughly over 40
> chips. 
>
> For a third party to analyze data from that chip, we
> should know the design of that chip and that
> information is one file. In this case it is GAL file.
> Since it is difficult and cumbersome to identify each
> design file type of all chips and get it into your
> directory.  However, on their website SMD
> (http://genome-www5.stanford.edu/), it is possible to
> go to each design file and obtain the data.  Since
> this is a time taking procedure, I wrote a socket
> script that would give me the URL of the file and
> allowing me to download.  The first barrier is, their
> database does not allow sockets programming. 
>
> Unfortunately, I have to access each file (there could
> be 40 - 100 files), get redirected to another page and
> there I can be able to download.
>
> Is there a method to automate this procedure through a
> browser. 
>
> Is there any alternative for such clicks. 
>
> Example:
> http://smd.stanford.edu/cgi-bin/data/viewDetails.pl?fullID=32898GENEPIX0
>   
Unfortunately for me when I go to that link I get a login form, not what 
you describe. So I can't help until I know how to log in.
> In this page at the bottom there is a link to
> 'Generate GAL file', that URL will allow me to get GAL
> File. 
>
> I cannot sit for whole evening and click ~40x30 times
> and download that. It is painful. Are there any smart
> ways to hack this process. 
>
> Thanks
> Sri.
>
> __
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.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


[Tutor] Beginner can't finish code

2006-03-29 Thread Tamaryn Sarusi-Kiss






Hi,
    I'm a beginner and can't finish the code I'm working on below. I want the code below to have "hints" when the user asks for them , and I don't know enough to do this.What would be the best way of introducing "hints" and allowing the user to exit when he wants to. Any help would  be appreciated.
 
 
 
   The Word Jumble Game
>> import random # create a sequence of words to choose from>> WORDS = ("python", "easy")
>> # pick one word randomly from the sequence>> word = random.choice(WORDS)
>> # create a variable to use later to see if the guess is correct>> correct = word
>> # create variable to use for hint if needed> hint = "hint"
>> # create a jumbled version of the word>> jumble =""
>> while word:>> position = random.randrange(Len(word))>> jumble += word[position]>> word = word[:position] + word[(position + 1):] # start the game>> print \>> """>> Welcome to Word Jumble! Unscramble the letters to make a word.>> (Press the enter key at the prompt to quit.)>> """>> print "The jumble is:", jumble
>> print "If you need a hint type 'hint' and Hit enter.">> guess = raw_input("\nYour guess: ")>> guess = guess.lower()
>> while (guess != correct) and (guess != "")and (guess != hint): >> print "Sorry, that's not it.">> guess = raw_input("Your guess: ")>> guess = guess.lower()>> if guess == hint:>> print "not hard but">> guess = raw_input("Your guess: ")>> guess = guess.lower() while word == python:> hints = {'easy':'not hard but','python':'snake'}> if guess == hint:> print hints[correct]> guess = raw_input("Your guess: ")> guess = guess.lower() if guess == correct:>> print "That's it! You guessed it!\n" print "Thanks for playing." raw_input("\n\nPress the enter key to exit.")
 
   
 
 







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


Re: [Tutor] Program for outputing the letter backward

2006-03-29 Thread Hoffmann
--- Kent Johnson <[EMAIL PROTECTED]> wrote:

> Hoffmann wrote:
> > We are almost there. I changed the code and, at
> least,
> > I got the correct output. However, I also got a
> > traceback. I didn't understand the traceback.
> Could
> > you clarify that?
> > Thanks,
> > Hoffmann
> > ps: The new code:
> > 
> > 
> vehicle='car'
> index = -1  #index of the last letter
> lenght = len(vehicle)
> last = vehicle[lenght-1]
> 
> while last >= vehicle[0]:
> > 
> > letter=vehicle[index]
> > print letter
> > index -= 1
> 
> You are still confusing the index of a letter and
> the letter itself.
> 
> In [1]: vehicle = 'car'
> 
> In [2]: last = vehicle[-1]
> 
> In [3]: last
> Out[3]: 'r'
> 
> last is a letter, not a number.
> 
> In [5]: vehicle[0]
> Out[5]: 'c'
> 
> vehicle[0] is also a letter. So when you write
>while last >= vehicle[0]:
> you are comparing two characters, which is not
> really helpful in the 
> current context. What you really want to do is
> compare the index of the 
> current character with 0. Here is a working version
> in the same style:
> 
> In [6]: index = len(vehicle)-1
> 
> In [7]: while index >= 0:
> ...: print vehicle[index]
> ...: index -= 1
> ...:
> ...:
> r
> a
> c
> 
> The best way to reverse a string is with a slice and
> negative index:
> 
> In [8]: vehicle[::-1]
> Out[8]: 'rac'
> 
> but I'm going to have to leave explanation of that
> to another day or 
> another poster.
> 
> Kent
> 
> 
> ___

Hello Guys,

Thank you very much all of you (in special: Kent,
John, and Adam), for the nice explanations about my
excercise. I am a newbie that is studying Python
programming by myself. I appreciated your attention.

See you on my next post :-)

Best,

Hoffmann

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How does it function

2006-03-29 Thread Terry Carroll
On Wed, 29 Mar 2006, Steve Nelson wrote:
> On 3/29/06, Kaushal Shriyan <[EMAIL PROTECTED]> wrote:
> >
> > Just wanted to know the detailed explanation about the below statement
> >
> > if __name__ == "__main__":
>
> Simple answer - any python program you write is effectively a
> 'module'.  Modules have an attribute __name__.  If you've imported the
> module from elsewhere, the __name__ is set to the name of the module,
> otherwise it is __name__.

I don't mean to nitpick, but I see Steve had a small but crucial slip
here.  I think he means, " If you've imported the module from elsewhere,
the __name__ is set to the name of the module, otherwise it is "__main__".

> This means that you can write a test that says:If the code we're
> trying to run is the main program, go ahead and start running the
> functions we need.

Yes; a typical usage is:

[bulk of program is here, in the form of one or more callable methods]

if __name__ == "__main__":
   [stuff]

Where "[stuff]" is either code to invoke your callable methods as you'd 
normally have the program run, or, (commonly for modules intended to be 
imported and used) code to test the module and confirm that a number of 
predetermined invocations with predetermined expected output actually 
produce that output.

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


Re: [Tutor] get a character of an ascii-value

2006-03-29 Thread Keo Sophon
On Thursday 30 March 2006 01:25, Hugo González Monteverde wrote:
> Hi,
>
> Python strings are binary strings as they can contain any value,
> including 0 (NULL) Depending on the encoding of the string, this may or
> may not be printable, and characters over ASCII 127 will mean different
> letters and symbols.
>
> Check the docs for strings and encodings:
>
> http://docs.python.org/lib/standard-encodings.html
> http://python.active-venture.com/api/stringObjects.html
>
> Hugo
>

Thanks.

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


Re: [Tutor] Program for outputing the letter backward - almost there!

2006-03-29 Thread Kent Johnson
Hoffmann wrote:
> We are almost there. I changed the code and, at least,
> I got the correct output. However, I also got a
> traceback. I didn't understand the traceback. Could
> you clarify that?
> Thanks,
> Hoffmann
> ps: The new code:
> 
> 
vehicle='car'
index = -1  #index of the last letter
lenght = len(vehicle)
last = vehicle[lenght-1]

while last >= vehicle[0]:
> 
>   letter=vehicle[index]
>   print letter
>   index -= 1

You are still confusing the index of a letter and the letter itself.

In [1]: vehicle = 'car'

In [2]: last = vehicle[-1]

In [3]: last
Out[3]: 'r'

last is a letter, not a number.

In [5]: vehicle[0]
Out[5]: 'c'

vehicle[0] is also a letter. So when you write
   while last >= vehicle[0]:
you are comparing two characters, which is not really helpful in the 
current context. What you really want to do is compare the index of the 
current character with 0. Here is a working version in the same style:

In [6]: index = len(vehicle)-1

In [7]: while index >= 0:
...: print vehicle[index]
...: index -= 1
...:
...:
r
a
c

The best way to reverse a string is with a slice and negative index:

In [8]: vehicle[::-1]
Out[8]: 'rac'

but I'm going to have to leave explanation of that to another day or 
another poster.

Kent


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


Re: [Tutor] Program for outputing the letter backward - almost there!

2006-03-29 Thread Adam
> Hi John,
>
> We are almost there. I changed the code and, at least,
> I got the correct output. However, I also got a
> traceback. I didn't understand the traceback. Could
> you clarify that?
> Thanks,
> Hoffmann
> ps: The new code:
>
> >>> vehicle='car'
> >>> index = -1  #index of the last letter
> >>> lenght = len(vehicle)
> >>> last = vehicle[lenght-1]
> >>>
> >>> while last >= vehicle[0]:
> letter=vehicle[index]
> print letter
> index -= 1
>
>
> r
> a
> c
>
> Traceback (most recent call last):
>   File "", line 2, in -toplevel-
> letter=vehicle[index]
> IndexError: string index out of range

while last >= vehicle[0]:

The problem is is that neither vehicle[0] nor last change during the
loop so it is always satisified and index eventually becomes a number
that doesn't correspond to an index of the string.
I would suggest something along these lines instead:

for i in range(len(vehicle)-1, -1, -1):
print vehicle[i]

which is basically what my list comp did but printing out the letters
rather than returning a list
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Automating web page parsing

2006-03-29 Thread Kent Johnson
Srinivas Iyyer wrote:

> For a third party to analyze data from that chip, we
> should know the design of that chip and that
> information is one file. In this case it is GAL file.
> Since it is difficult and cumbersome to identify each
> design file type of all chips and get it into your
> directory.  However, on their website SMD
> (http://genome-www5.stanford.edu/), it is possible to
> go to each design file and obtain the data.  Since
> this is a time taking procedure, I wrote a socket
> script that would give me the URL of the file and
> allowing me to download.  The first barrier is, their
> database does not allow sockets programming. 

What did you try? How did it fail?

The website requires a form-based login which probably returns a cookie 
to your browser. Your socket solution needs to take this into account. 
There are some articles here with more info:
http://www.voidspace.org.uk/python/articles.shtml#http

> 
> Unfortunately, I have to access each file (there could
> be 40 - 100 files), get redirected to another page and
> there I can be able to download.
> 
> Is there a method to automate this procedure through a
> browser. 
> 
> Is there any alternative for such clicks. 

There are several packages intended to help script web sites, take a look at
twill http://www.idyll.org/~t/www-tools/twill/
mechanize and ClientForm http://wwwsearch.sf.net/
http://python.org/pypi/mechanoid

Kent

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


Re: [Tutor] Newbie: Passing variable into re.compile

2006-03-29 Thread Kent Johnson
Jerome Jabson wrote:
> Hello,
> 
>  
> 
> I’m having trouble trying to pass a variable into
> re.compile(), using the “r” option. Let’s say I
> have the following:
> 
>  
> 
> import re
> 
> arg1 = ‘10”
> 
> p = re.compile(r + arg1 + '\.(\d+\.\d+\.\d+)')
> 
>  
> 
> This works if I do:
> 
>  
> 
> p = re.compile(r ‘10\.(\d+\.\d+\.\d+)')
> 
>  
> 
> Is there something special I need to do for the
> “r” option? Like escape it when using a variable?

'r' is not an option to re.compile(), it is a modifier for the string 
itself that affects how the string is parsed. So you can use
   p = re.compile(arg1 + r'\.(\d+\.\d+\.\d+)')

or use string substitution as John suggests.

Kent

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


Re: [Tutor] Program for outputing the letter backward - almost there!

2006-03-29 Thread Hoffmann
--- Adam <[EMAIL PROTECTED]> wrote:

> I just wanted to throw in a couple of ideas for you
> on this subject.
> These are the ways I would personally think of going
> about this
> problem:
> 
> >>> ''.join([s[n] for n in range(len(s)-1, -1, -1)])
> 'rac'
> Which looks a bit fugly but is nice and short if you
> can manage list comps.
> 
> and now my favourite:
> 
> >>> l = list("car")
> >>> l.reverse()
> >>> ''.join(l)
> 'rac'
> 
> hth
> 

Hi Adam,

Defenitely your second alternative is really great! 

Regarding that my 'bad' alternative, do you have any
suggestion about that traceback? I not only would like
to have the exercise done. And after your nice
suggestion, I ALREADY have it, but also I would like
to learn about that traceback I got previously.

Thanks,
Hoffmann


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie: Passing variable into re.compile

2006-03-29 Thread John Fouhy
On 30/03/06, Jerome Jabson <[EMAIL PROTECTED]> wrote:
> import re
>
> arg1 = '10"
>
> p = re.compile(r + arg1 + '\.(\d+\.\d+\.\d+)')

Have a look at string substitutions :-)  ---
http://docs.python.org/lib/typesseq-strings.html

p = re.compile(r'%s\.(\d+\.\d+\.\d+)' % arg1)

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


[Tutor] Newbie: Passing variable into re.compile

2006-03-29 Thread Jerome Jabson
Hello,

 

I’m having trouble trying to pass a variable into
re.compile(), using the “r” option. Let’s say I
have the following:

 

import re

arg1 = ‘10”

p = re.compile(r + arg1 + '\.(\d+\.\d+\.\d+)')

 

This works if I do:

 

p = re.compile(r ‘10\.(\d+\.\d+\.\d+)')

 

Is there something special I need to do for the
“r” option? Like escape it when using a variable?

 

Thanks,
Jerome


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Automating web page parsing

2006-03-29 Thread Srinivas Iyyer
Dear group, 

***Disclaimer***Not suitable for BioPython list***

I work with GeneChips to analyze human gene expression
patterns. These genechips are various kinds one of the
variety is made at Stanford University. In a typical
experiment, an experimenter uses roughly over 40
chips. 

For a third party to analyze data from that chip, we
should know the design of that chip and that
information is one file. In this case it is GAL file.
Since it is difficult and cumbersome to identify each
design file type of all chips and get it into your
directory.  However, on their website SMD
(http://genome-www5.stanford.edu/), it is possible to
go to each design file and obtain the data.  Since
this is a time taking procedure, I wrote a socket
script that would give me the URL of the file and
allowing me to download.  The first barrier is, their
database does not allow sockets programming. 

Unfortunately, I have to access each file (there could
be 40 - 100 files), get redirected to another page and
there I can be able to download.

Is there a method to automate this procedure through a
browser. 

Is there any alternative for such clicks. 

Example:
http://smd.stanford.edu/cgi-bin/data/viewDetails.pl?fullID=32898GENEPIX0
In this page at the bottom there is a link to
'Generate GAL file', that URL will allow me to get GAL
File. 

I cannot sit for whole evening and click ~40x30 times
and download that. It is painful. Are there any smart
ways to hack this process. 

Thanks
Sri.

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program for outputing the letter backward - almost there!

2006-03-29 Thread Adam
I just wanted to throw in a couple of ideas for you on this subject.
These are the ways I would personally think of going about this
problem:

>>> ''.join([s[n] for n in range(len(s)-1, -1, -1)])
'rac'
Which looks a bit fugly but is nice and short if you can manage list comps.

and now my favourite:

>>> l = list("car")
>>> l.reverse()
>>> ''.join(l)
'rac'

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


[Tutor] for loops and exceptions

2006-03-29 Thread Matthew White
Hello,

>From a general style and/or programmatic perspective, which is a "better"
way to write this bit of code?

try:
(dn, attrs) = conn.search_s(search_base, search_scope, search_filter, 
search_attrs):
except Exception, e:
warn_the_user(e)
do_something_useful()

for (name, data) in (dn, attrs):
print '%s' % (name)
for key, value in data.iteritems():
print '%s => %s' % (key, value)

OR

try:
for dn, attrs in conn.search_s(search_base, search_scope, search_filter, 
search_attrs):
print dn
for key, value in attrs.iteritems():
print '\t%s => %s' % (key, value)
print
except Exception, e:
 warn_the_user(e)
 do_something_useful


Personally I like the second method more.  To my eyes it is compact
and efficient.  Coming from a perl background I tend to like to get
very compact, which can be an impediment to code readability.  As I get
started with python I want to make sure that I don't repeat the same
mistakes and let too many perl-isms creep into my python code.

-mtw


-- 
Matthew White - District Systems Administrator
Tigard/Tualatin School District
503.431.4128

"The greatest thing in this world is not so much where we are, but in
what direction we are moving."   -Oliver Wendell Holmes

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


Re: [Tutor] newbie question about default arguments

2006-03-29 Thread Kent Johnson
Josh Adams wrote:
> Thanks for your help.  That makes a lot more sense.  
> 
> Not to ask too many stupid questions, but why does the L2 assignment in the
> if-block create a new L variable?  Shouldn't the scope from the function
> definition dominate the inner scope of the if-block?

It doesn't create a new variable, it binds a new value to the name L. if 
statements don't introduce a new scope so you are right about that.

Default values for functions are evaluated just once, when the function 
is defined. Rebinding L to a new list inside the function makes each 
execution get a fresh list.

This might help you understand Python name-binding semantics:
http://effbot.org/zone/python-objects.htm

Kent

> 
> Thanks,
> Josh
> 
> 
>>Josh,
>>
>>If you print the id() of your L inside your f2(), you will notice something..
>>In short, the default value stays the same; what you modified was another
>>copy of []. Hope it helps.
>>
>>def f2(a, L=[]):
>>  print "id(L) = ", id(L)
>>  if L==[]:
>>L=[]
>>print "id(L2) =", id(L)
>>  L.append(a)
>>  return L
>>
>>
>print f2(1)
>>
>>id(L)= 11788336
>>id(L2)= 12047184
>>[1]
>>
>>Kenny
>>
>>
> 
> 
> 
> 
> ___
> 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] get a character of an ascii-value

2006-03-29 Thread Hugo González Monteverde
Hi,

Python strings are binary strings as they can contain any value, 
including 0 (NULL) Depending on the encoding of the string, this may or 
may not be printable, and characters over ASCII 127 will mean different 
letters and symbols.

Check the docs for strings and encodings:

http://docs.python.org/lib/standard-encodings.html
http://python.active-venture.com/api/stringObjects.html

Hugo

In a
> 
> I just got the answer from Pujo. How about r is bigger than 127. How can i 
> get 
> the character? For example, r = 191.
> 
> Thanks,
> Sophon
> ___
> 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] newbie question about default arguments

2006-03-29 Thread Josh Adams
Thanks for your help.  That makes a lot more sense.  

Not to ask too many stupid questions, but why does the L2 assignment in the
if-block create a new L variable?  Shouldn't the scope from the function
definition dominate the inner scope of the if-block?

Thanks,
Josh

> Josh,
> 
> If you print the id() of your L inside your f2(), you will notice something..
> In short, the default value stays the same; what you modified was another
> copy of []. Hope it helps.
> 
> def f2(a, L=[]):
>   print "id(L) = ", id(L)
>   if L==[]:
> L=[]
> print "id(L2) =", id(L)
>   L.append(a)
>   return L
> 
> >>> print f2(1)
> id(L)= 11788336
> id(L2)= 12047184
> [1]
> 
> Kenny
> 
> 



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


Re: [Tutor] newbie question about default arguments

2006-03-29 Thread Pujo Aji
Hello Josh,you wrote a different problem. The tutorial should be like this:
Important warning:  The default value is evaluated only once.
This makes a difference when the default is a mutable object such as a
list, dictionary, or instances of most classes.  For example, the
following function accumulates the arguments passed to it on
subsequent calls:


def f(a, L=[]):L.append(a)return Lprint f(1)print f(2)print f(3)


This will print


[1][1, 2][1, 2, 3]


If you don't want the default to be shared between subsequent calls,
you can write the function like this instead:


def f(a, L=None):if L is None:L = []L.append(a)return LLook the second example use None instead of []To explain more detail let's create an example :
def f1(a, L=[]):    L.append(a)    return Ldef f2(a, L=None):    if L is None:    L = []    L.append(a)    return Ldef main():    m = f2(3)    print m # produce [3]
    m = f2(4)    print m # produce [4]        #     m = f1(3)    print m # produce[3]    m = f1(4)    print m # produce[3,4]    passin f1 : when we don't put the second argument the L=[] is created only once. The second input in f1 will be accumulated.
in f2: when we don't put the second argument L is given a None value. Inside this function if L is None then L = [] this will always make L = [] that's way no accumulation happened.Hope this help.pujo
On 3/29/06, Josh Adams <[EMAIL PROTECTED]> wrote:
Hi all,I was going through the tutorial at http://docs.python.org/tut/node6.html when Icame to the bit about default arguments with this code:def f(a, L=[]):
L.append(a)return Lprint f(1)print f(2)print f(3)returns:[1][1, 2][1, 2, 3]>From the postings here, I think I understand that this occurs because L is only
initialized when f is first run.  However, this code gives some different results:def f2(a, L=[]): if L == []: L = [] L.append(a) return Lprint f2(1)print f2(2)
print f2(3)returns:[1][2][3]I'm not too clear on why this doesn't return the same results as the first.  Cansomeone enlighten me?Thanks,Josh___
Tutor maillist  -  Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] newbie question about default arguments

2006-03-29 Thread Josh Adams
Hi all,
I was going through the tutorial at http://docs.python.org/tut/node6.html when I
came to the bit about default arguments with this code:

def f(a, L=[]):
L.append(a)
return L

print f(1)
print f(2)
print f(3)

returns:
[1]
[1, 2]
[1, 2, 3]

>From the postings here, I think I understand that this occurs because L is only
initialized when f is first run.  However, this code gives some different 
results:

def f2(a, L=[]):
 if L == []:
 L = []
 L.append(a)
 return L

print f2(1)
print f2(2)
print f2(3)

returns:
[1]
[2]
[3]

I'm not too clear on why this doesn't return the same results as the first.  Can
someone enlighten me?

Thanks,
Josh


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


Re: [Tutor] Program for outputing the letter backward - almost there!

2006-03-29 Thread Hoffmann
--- John Fouhy <[EMAIL PROTECTED]> wrote:

> On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote:
> > Hi John,
> >
> > (1) vehicle[index] is: 'c'
> > (2) If index = index = 1, so vehicle[index]
> becomes:
> > 'a'
> 
> What I'm getting at here is that, by changing index,
> we can change
> which letter we are looking at.  And index is a
> number, which means
> it's easier to reason about than letters are.
> 
> Let's have a look at a possible solution:
> 
> >>> vehicle = 'car'
> >>> index = 2
> >>> print vehicle[index]
> r
> >>> index = 1
> >>> print vehicle[index]
> a
> >>> index = 0
> >>> print vehicle[index]
> c
> 
> Notice that the three print statements are
> identical.  That suggests
> we could write the code in a loop, with 'print
> vehicle[index]' in the
> body of the loop.  Can you have a go at that?
> 
> --
> John.
> ___
 
Hi John,

We are almost there. I changed the code and, at least,
I got the correct output. However, I also got a
traceback. I didn't understand the traceback. Could
you clarify that?
Thanks,
Hoffmann
ps: The new code:

>>> vehicle='car'
>>> index = -1  #index of the last letter
>>> lenght = len(vehicle)
>>> last = vehicle[lenght-1]
>>> 
>>> while last >= vehicle[0]:
letter=vehicle[index]
print letter
index -= 1


r
a
c

Traceback (most recent call last):
  File "", line 2, in -toplevel-
letter=vehicle[index]
IndexError: string index out of range 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How does it function

2006-03-29 Thread Steve Nelson
On 3/29/06, Kaushal Shriyan <[EMAIL PROTECTED]> wrote:
> Hi ALL
>
> Just wanted to know the detailed explanation about the below statement
>
> if __name__ == "__main__":

Simple answer - any python program you write is effectively a
'module'.  Modules have an attribute __name__.  If you've imported the
module from elsewhere, the __name__ is set to the name of the module,
otherwise it is __name__.

This means that you can write a test that says:If the code we're
trying to run is the main program, go ahead and start running the
functions we need.

You can read more about it here:

http://swaroopch.info/text/Byte_of_Python:Modules

and also here:

http://diveintopython.org/getting_to_know_python/testing_modules.html

More detailed info here:

http://www.python.org/doc/current/ref/import.html

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


[Tutor] How does it function

2006-03-29 Thread Kaushal Shriyan
Hi ALL

Just wanted to know the detailed explanation about the below statement

if __name__ == "__main__":

Thanks

Regards

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


Re: [Tutor] Alternating patterns

2006-03-29 Thread Kent Johnson
kevin parks wrote:
>>From: Kent Johnson <[EMAIL PROTECTED]>
>>itertools.cycle() will repeat a sequence indefinitely:
>>In [2]: from itertools import cycle
>>
>>In [3]: i=cycle([1,2])
>>
>>In [5]: for j in range(6):
>>...: print i.next()
>>...:
>>...:
>>1
>>2
>>1
>>2
>>1
>>2
>>
>>For non-repeating sequences I would look at writing a generator 
>>function
>>for the sequences.
>>
>>Kent
> 
> okay.. i am painfully unaware of generators, iterators, sets, genexes 
> and a lot of the new stuff post 2.3 & 2.4 stuffs... my problem is that 
> i find the online docs kind of terse and few of the Python books yet 
> cover these newer constructs in detail
> 
> itertools looks very cool are there any toots on the above and on 
> Sets & itertools? It really seems like it would help to know these for 
> my work... That liddo bit right up there with itertools.cycle already 
> has me a drooling... (so helpful that would be!)

The best introduction to new features is usually in the What's New 
document accompanying the release where the feature was added. Of course 
it helps to know when the feature was added...here are some generator 
examples:
http://www.python.org/doc/2.2.3/whatsnew/node5.html

Generators are excellent for encapsulating the generation of a sequence 
when there is state that must be maintained between elements. For 
example here is a generator that takes a sequence argument, and yields 
this sequence of sequences:
   the original sequence
   the original sequence with the first element incremented by one
   the original sequence
   the original sequence with the second element incremented by one
   etc until each element has been incremented

In [2]: def change_each(seq):
...: seq = list(seq) # Copy and ensure it's a list
...: for i in range(len(seq)):
...: yield seq
...: seq[i] += 1
...: yield seq
...: seq[i] -= 1
...:
...:

In [3]: s = [1, 3]

In [5]: for n in change_each(s):
...: print n
...:
...:
[1, 3]
[2, 3]
[1, 3]
[1, 4]

If you wanted to repeat this sequence indefinitely you could just wrap 
it with itertools.cycle().

The module docs for itertools contain quite a few examples:
http://docs.python.org/lib/itertools-recipes.html

itertools.cycle() is pretty simple, it just loops endlessly over the 
sequence you give it.

The itertools docs shows equivalent Python functions for each of the 
itertools functions. Most of them are implemented using generator 
functions so by looking at them you can learn about itertools and 
generators at the same time.
http://docs.python.org/lib/itertools-functions.html

Kent

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


Re: [Tutor] newbie exercises

2006-03-29 Thread Steve Nelson
On 3/29/06, Keo Sophon <[EMAIL PROTECTED]> wrote:

> Is it bitwise operator? Could you give one example?

>>> for a in range(2):
... for b in range(2):
... print a, b, a&b
...
0 0 0
0 1 0
1 0 0
1 1 1

> Sophon

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


Re: [Tutor] newbie exercises

2006-03-29 Thread Keo Sophon
On Monday 27 March 2006 19:53, Steve Nelson wrote:
> On 3/27/06, josip <[EMAIL PROTECTED]> wrote:
> > Can someone give me exercises to do with loops, maybe functions to?
>
> How about a program that produces truth tables for the basic gates?
> AND, NAND, NOT, OR, XOR?
>
> You could write a function for each gate, and one to produce a truth 
> table.
>
> S.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

Is it bitwise operator? Could you give one example?

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


Re: [Tutor] Python Code

2006-03-29 Thread Noufal Ibrahim

On Wed, March 29, 2006 2:50 pm, Kaushal Shriyan wrote:
> Hi
>
> I am unable to execute the below code, I have put this in test.py file
> and made it executable, when I run this I see no output, Please
> explain me as what is exactly going on with the below code
>
> The below code is from http://www.ibiblio.org/obp/thinkCSpy/chap11.htm
>
> def copyFile(oldFile, newFile):
>   f1 = open(oldFile, "r")
>   f2 = open(newFile, "w")
>   while 1:
> text = f1.read(50)
> if text == "":
>   break
> f2.write(text)
>   f1.close()
>   f2.close()
>   return

Did you have only the copyFile definition or did you have an invocation as
well? You have to call it to work. :)



-- 
-NI

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


[Tutor] Python Code

2006-03-29 Thread Kaushal Shriyan
Hi

I am unable to execute the below code, I have put this in test.py file
and made it executable, when I run this I see no output, Please
explain me as what is exactly going on with the below code

The below code is from http://www.ibiblio.org/obp/thinkCSpy/chap11.htm

def copyFile(oldFile, newFile):
  f1 = open(oldFile, "r")
  f2 = open(newFile, "w")
  while 1:
text = f1.read(50)
if text == "":
  break
f2.write(text)
  f1.close()
  f2.close()
  return

Thanks in Advance

Regards

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


Re: [Tutor] removing file from zip archive.

2006-03-29 Thread Keo Sophon
On Wednesday 29 March 2006 15:38, Tim Golden wrote:
> | How can we remove one file inside of a zip archive?
> |
> | I'm using this method:
> |
> | import zipfile
> |
> | ziparchive = zipfile.ZipFile('test.odt', 'r')
> | xmldata = ziparchive.read('content.xml')
> | ziparchive.close
>
> (Warning: not my area of expertise, but...)
>
> Your example *reads* one file from within a zip
> archive, and is a perfectly good way of doing it
> (indeed pretty much the only way of doing it).
> Do I take it you want to end up with the same
> archive but without that file?
>
> If so, I don't believe it's possible as such.
> What you'd have to do -- and this is almost
> certainly what any front-end tool will do for
> you behind-the-scenes -- is to copy the contents
> of the archive to a temporary file omitting the
> file(s) in question, and then to rename the
> temporary file over the original.
>
> TJG
>

Thank you.

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


Re: [Tutor] removing file from zip archive.

2006-03-29 Thread Tim Golden
| How can we remove one file inside of a zip archive? 
| 
| I'm using this method:
| 
|   import zipfile
| 
|   ziparchive = zipfile.ZipFile('test.odt', 'r')
|   xmldata = ziparchive.read('content.xml')
|   ziparchive.close

(Warning: not my area of expertise, but...)

Your example *reads* one file from within a zip
archive, and is a perfectly good way of doing it
(indeed pretty much the only way of doing it).
Do I take it you want to end up with the same 
archive but without that file?

If so, I don't believe it's possible as such.
What you'd have to do -- and this is almost
certainly what any front-end tool will do for
you behind-the-scenes -- is to copy the contents
of the archive to a temporary file omitting the
file(s) in question, and then to rename the
temporary file over the original.

TJG


This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk

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


Re: [Tutor] Python tutor

2006-03-29 Thread Noufal Ibrahim

On Wed, March 29, 2006 4:35 am, Anna Ravenscroft wrote:
> 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

Thanks. I'll look at GvR, livewires and rur-ple in detail soon. I'm still
stuck without a reliable internet connection (should get one in 2 weeks).
I've joined the list. Let me come up with a framework and then I'll post
again.


-- 
-NI

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


Re: [Tutor] ASCII

2006-03-29 Thread Pujo Aji
Hello,Basicaly, You use ASCII or Unicode when you have to deal with character conversion for example coding with a different language symbol, chinese letter etc. Sincerely Yours,Pujo
On 3/29/06, Kaushal Shriyan <[EMAIL PROTECTED]> wrote:
On 3/29/06, Pujo Aji <[EMAIL PROTECTED]> wrote:>> Hi Kaushal,>> Please clarify the problem more specific.> Or you can tell us that you have a problem and want to use python to solve
> it?>> Sincerely Yours,> pujo>>> On 3/29/06, Kaushal Shriyan <[EMAIL PROTECTED]> wrote:> >> Hi All
>> How do i use this ASCII values in my day to day activities, I am going> through> learning python,>> Please illustrate with examples>> Thanks in Advance>
> Regards>> Kaushal> ___> Tutor maillist  -  Tutor@python.org> 
http://mail.python.org/mailman/listinfo/tutor>>Hi PujoIts a very general question not related to python at all, I have aminimum knowledge in ASCII just wanted to know how it is used and how
it helps outRegardsKaushal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] number of nodes

2006-03-29 Thread kakada
Hello everyone,

textp = xmldoc.getElementsByTagName('text:p')

from the example above, How can I know how many  node are there?

Thanks,
da


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


Re: [Tutor] ASCII

2006-03-29 Thread Kaushal Shriyan
On 3/29/06, Pujo Aji <[EMAIL PROTECTED]> wrote:
>
> Hi Kaushal,
>
> Please clarify the problem more specific.
> Or you can tell us that you have a problem and want to use python to solve
> it?
>
> Sincerely Yours,
> pujo
>
>
> On 3/29/06, Kaushal Shriyan <[EMAIL PROTECTED]> wrote:
> >
> Hi All
>
> How do i use this ASCII values in my day to day activities, I am going
> through
> learning python,
>
> Please illustrate with examples
>
> Thanks in Advance
>
> Regards
>
> Kaushal
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>

Hi Pujo

Its a very general question not related to python at all, I have a
minimum knowledge in ASCII just wanted to know how it is used and how
it helps out

Regards

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