[Tutor] Learning about programming
On Sun, Feb 9, 2014 at 3:38 AM, abhinav raj kp nirmallur wrote: > Sir I don't know nothing about programming but I have a great intrest > in it so that now a days I began to study python, many of my friends > and teachers suggest me it. But still I have no tutor, can you please > suggest me to study python using book or any good websit. Hi Abhinav, I am changing the subject line. In the future, please use appropriate subject lines for fresh questions. Doing so helps others on the list to know if they are interested in your question. You have said: "Still I have no tutor". You are very mistaken. You have the folks on this mailing list. We will be happy to help. We do not work for free, of course. You have to try to ask good questions. But do so, and you will have people falling out of the aisles here who will be overjoyed to serve. We all know what it's like to start as a beginner. You've asked where you can find good resources. Do you have access to the following web page? https://wiki.python.org/moin/BeginnersGuide That page has links to tutorials that may be helpful for you. You should be able to get started by picking one of them, and reading and practicing them. If you have trouble with them, come back here and ask questions about the difficulty. Folks here will be happy to help. Finally, take a few minutes and read through: http://www.catb.org/~esr/faqs/smart-questions.html That document might be a little obnoxious at first, but it provides some guidelines for asking questions to a particular and strange group of humanity, namely computer programmers. I recommend this because, as a beginner, you might not know about certain expectations that these weird programmers have. If you try to meet those cultural expectations, you'll almost certainly get better answers in turn from technical forums. Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] second if
By the way, if you are trying to write your own FASTA parser, please reconsider. A good FASTA parser has been written by the folks at BioPython.org. Use that one unless you really know what you're doing. See: http://biopython.org/DIST/docs/tutorial/Tutorial.html#sec12 for example usage. If you are using your own home-grown FASTA parser, consider switching to the biopython one. (As Lincoln Stein of Bioperl fame has observed, reimplementing a FASTA parser should not be a rite of passage. Reference: the talk "Bioinformatics: Building a Nation from a Land of City States", mentioned in http://www.oreillynet.com/pub/a/network/2002/01/29/bioday2.html. Sadly, the audio to his keynote speech does not appear to be on Dr. Dobbs Technetcast anymore, as that web site appears to have fallen on hard times.) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trace / profile every function with inputs
Hi, Have tried inspect module in python.See this code def foo(): print inspect.stack()[0][3] >>> foo()foo>>> this shall meet your purpose I believe thanks,rakesh > From: r.cziv...@research.gla.ac.uk > To: tutor@python.org > Date: Fri, 7 Feb 2014 16:07:58 + > Subject: [Tutor] trace / profile every function with inputs > > Hi All, > > I am struggling with a "simple" problem: I would like to print out every > function that is being executed while my Python program is running. I can not > modify the Python programs that I would like to profile. > > Let me give an example. A program contains a function and a call like this: > > def foo(x): > y = math.cos(x) > time.sleep(y+1) > return x * 50 > > print foo(100) > > I would like to retrieve an execution trace that shows me each function > called (with the value or hash of the function arguments). According to the > example, I am looking for a way to extract something similar: > > foo(100) > math.cos(100) > time.sleep(0.87) > > Things I have tried with only partial success: > - trace: couldn't get the function names in some cases > - profile / cProfile: no information about the arguments > > Could you suggest me a way of doing this? > > Thanks, > Richard > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if >= 0
CCing Tutor list. here are the entire code > > >import sys >in_file = open(sys.argv[1], 'r').readlines() >locus = '' >accession = '' >organism = '' > >OK, as suspected you initialise these to empty strings so they almost >certainly are being printed they just don't contain anything. Its easilyy proved by making the initial value something visible like a dash '-'... say for element in in_file: > > if element.find(LOCUS'): > locus += element > > >The only time this is not executed is if LOCUS is at the > >very start of the line. In *every* other case this will >be executed. Even if LOCUS is not in the element. > > > >why is that?Because the Python 'if' test treats any number other than zero as >True. And the string find() method returns the index at which the string is found or -1. So the only time you get zero(False) is if the string starts on the first character. If the string is missing you get a -1 which is considered True When you added the >=0 you eliminated the cases where find() returned -1, ie where the string was not found and so you checked the other search cases. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Your confirmation is required to leave the Tutor mailing list
On Mon, 10 Feb 2014, tutor-confirm+c3fa710640d780363ebaec9fd955eefa81f1b...@python.org wrote: Mailing list removal confirmation notice for mailing list Tutor We have received a request for the removal of your email address, "carr...@tjc.com" from the tutor@python.org mailing list. To confirm that you want to be removed from this mailing list, simply reply to this message, keeping the Subject: header intact. Or visit this web page: https://mail.python.org/mailman/confirm/tutor/c3fa710640d780363ebaec9fd955eefa81f1b46c Or include the following line -- and only the following line -- in a message to tutor-requ...@python.org: confirm c3fa710640d780363ebaec9fd955eefa81f1b46c Note that simply sending a `reply' to this message should work from most mail readers, since that usually leaves the Subject: line in the right form (additional "Re:" text in the Subject: is okay). If you do not wish to be removed from this list, please simply disregard this message. If you think you are being maliciously removed from the list, or have any other questions, send them to tutor-ow...@python.org. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python .decode issue
On 10/02/2014 23:16, Pierre Dagenais wrote: On 14-02-08 12:55 AM, james campbell wrote: header_bin = header_hex.decode('hex') AttributeError: 'str' object has no attribute 'decode' What I understand is that you are trying to decode 'hex', which is a string. The interpreter is telling you that strings cannot be decoded. I do not know header_hex.decode(), but I suspect it's expecting an hex number, check it out. PierreD. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor "AttributeError: 'str' object has no attribute 'decode'" is saying that header_hex has no attribute decode, the string 'hex' doesn't enter into the equation. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python .decode issue
On 14-02-08 12:55 AM, james campbell wrote: > header_bin = header_hex.decode('hex') > AttributeError: 'str' object has no attribute 'decode' What I understand is that you are trying to decode 'hex', which is a string. The interpreter is telling you that strings cannot be decoded. I do not know header_hex.decode(), but I suspect it's expecting an hex number, check it out. PierreD. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recommendation For A Complete Noob
On 14-02-09 05:37 PM, Altrius wrote: > Hi, > > I’m completely new to programming in general and everything I have read so > far has pointed me to Python. I’ll put this another way: All I know is that a programming language is a medium for a human to tell a computer what to do. After that I’m kinda lost. I was just hoping to get some input as to where I might start. I'd start with Guido van Rossum http://www.network-theory.co.uk/docs/pytut/ for a gentle introduction. Then I'd follow up with http://swaroopch.com http://www.diveintopython3.net/ by Mark Pilgrim and Learning to program by Alan Gauld http://www.freenetpages.co.uk/hp/alan.gauld/ You may also want to check http://www.linuxlinks.com/article/20121228122317781/20oftheBestFreePythonBooks-Part1.html for more advice. Welcome and good luck, PierreD. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if >= 0
On 10/02/14 15:32, rahmad akbar wrote: hey again guys, i am trying to understand these statements, if i do it like this, it will only print the 'print locus' part for element in in_file: if element.find('LOCUS'): locus += element The only time this is not executed is if LOCUS is at the very start of the line. In *every* other case this will be executed. Even if LOCUS is not in the element. elif element.find('ACCESSION'): accession += element elif element.find('ORGANISM'): organism += element print locus print accession print organism I assume by not printing you mean the other two print statements don't print anything visible? Or are you actually getting an error message? Without seeing how accession and organism are defined its hard to know why you are not seeing anything. The print statements are not inside the loop so should always (all three) print something,even if the something is an empty string. once i add >= 0 to each if conditions like so, the entire loop works and prints the 3 items for element in in_file: if element.find('LOCUS') >= 0: locus += element This should always be executed unless LOCUS happens to be at the start of the line *or missing*. The greater than zero test eliminates the casers where LOCUS does not exist, in these cases it will move onto the next elif clause. Presumably thats why you now get 3 values printed. But that assumes you initialised the variables to empty strings at the start? elif element.find('ACCESSION') >= 0: accession += element elif element.find('ORGANISM') >= 0: organism += element print locus print accession print organism why without '>= 0' the loop doesnt works? Can you show us some output? I don't really understand what you mean by "doesn't work". Do you get any error messages? Or is it just the lack of print outputs (that really have nothing to do with the loop part)? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] second if
I guess the replies by Alan and Peter precisely answer to your question? Best 2014-02-10 12:46 GMT+01:00 rahmad akbar : > David, > > thanks for your reply. i cant figure out why the if at that point and what > is the 'if' try to accompolish > > > On Mon, Feb 10, 2014 at 11:52 AM, David Palao > wrote: >> >> Also, could you explain better what is your doubt? You don't >> understand what "index >= 1" means, or why this "if" at this point, or >> anything else? >> >> Best >> >> 2014-02-07 17:14 GMT+01:00 rahmad akbar : >> > he guys, i am trying to understand this code: i understand the first if >> > statement (if line.startswith..) in read_fasta function but couldnt >> > understand the next one(if index >=...). thanks in advance!! >> > >> > import sys >> > #class declaration with both attributes we need >> > class Fasta: >> > def __init__(self, name, sequence): >> > #this will store the sequence name >> > self.name = name >> > #this will store the sequence itself >> > self.sequence = sequence >> > >> > #this function will receive the list with the file >> > #contents, create instances of the Fasta class as >> > #it scans the list, putting the sequence name on the >> > #first attribute and the sequence itself on the second >> > #attribute >> > def read_fasta(file): >> > #we declare an empty list that will store all >> > #Fasta class instances generated >> > items = [] >> > index = 0 >> > for line in file: >> > #we check to see if the line starts with a > sign >> > if line.startswith(">"): >> >#if so and our counter is large than 1 >> >#we add the created class instance to our list >> >#a counter larger than 1 means we are reading >> >#from sequences 2 and above >> >if index >= 1: >> >items.append(aninstance) >> >index+=1 >> >#we add the line contents to a string >> >name = line[:-1] >> >#and initialize the string to store the sequence >> >seq = '' >> >#this creates a class instance and we add the attributes >> >#which are the strings name and seq >> >aninstance = Fasta(name, seq) >> > else: >> >#the line does not start with > so it has to be >> >#a sequence line, so we increment the string and >> >#add it to the created instance >> > seq += line[:-1] >> > aninstance = Fasta(name, seq) >> > >> > #the loop before reads everything but the penultimate >> > #sequence is added at the end, so we need to add it >> > #after the loop ends >> > items.append(aninstance) >> > #a list with all read sequences is returned >> > return items >> > >> > fastafile = open(sys.argv[1], 'r').readlines() >> > mysequences = read_fasta(fastafile) >> > >> > print mysequences >> > >> > for i in mysequences: >> > print i.name >> > >> > -- >> > many thanks >> > mat >> > >> > ___ >> > Tutor maillist - Tutor@python.org >> > To unsubscribe or change subscription options: >> > https://mail.python.org/mailman/listinfo/tutor >> > > > > > > -- > many thanks > mat ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if >= 0
On Feb 10, 2014 10:33 AM, "rahmad akbar" wrote: > > hey again guys, i am trying to understand these statements, > > if i do it like this, it will only print the 'print locus' part > > for element in in_file: # do this print element > if element.find('LOCUS'): > locus += element > elif element.find('ACCESSION'): > accession += element > elif element.find('ORGANISM'): > organism += element > > print locus > print accession > print organism > > > once i add >= 0 to each if conditions like so, the entire loop works and prints the 3 items > > for element in in_file: > if element.find('LOCUS') >= 0: > locus += element > elif element.find('ACCESSION') >= 0: > accession += element > elif element.find('ORGANISM') >= 0: > organism += element > > print locus > print accession > print organism > > why without '>= 0' the loop doesnt works? > -- > many thanks > mat > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if >= 0
rahmad akbar wrote: > hey again guys, i am trying to understand these statements, > > if i do it like this, it will only print the 'print locus' part > > for element in in_file: > if element.find('LOCUS'): > locus += element > elif element.find('ACCESSION'): > accession += element > elif element.find('ORGANISM'): > organism += element > > print locus > print accession > print organism > > > once i add >= 0 to each if conditions like so, the entire loop works and > prints the 3 items > > for element in in_file: > if element.find('LOCUS') >= 0: > locus += element > elif element.find('ACCESSION') >= 0: > accession += element > elif element.find('ORGANISM') >= 0: > organism += element > > print locus > print accession > print organism > > why without '>= 0' the loop doesnt works? element.find(some_string) returns the position of some_string in element, or -1 if some_string doesn't occur in element. All integers but 0 are true in a boolean context, i. e. if -1: print -1 # printed if 0: print 0 # NOT printed if 123456789: print 123456789 # printed So unlike what you might expect if element.find(some_string): # WRONG! print "found" else: print "not found" will print "not found" if element starts with some_string and "found" for everything else. As you are not interested in the actual position of the potential substring you should rewrite your code to if "LOCUS" in element: locus += element elif "ACCESSION" in element: accession += element elif "ORGANISM" in element: organism += element which unlike the str.find() method works as expected. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] if >= 0
hey again guys, i am trying to understand these statements, if i do it like this, it will only print the 'print locus' part for element in in_file: if element.find('LOCUS'): locus += element elif element.find('ACCESSION'): accession += element elif element.find('ORGANISM'): organism += element print locus print accession print organism once i add >= 0 to each if conditions like so, the entire loop works and prints the 3 items for element in in_file: if element.find('LOCUS') >= 0: locus += element elif element.find('ACCESSION') >= 0: accession += element elif element.find('ORGANISM') >= 0: organism += element print locus print accession print organism why without '>= 0' the loop doesnt works? -- many thanks mat ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trace / profile function calls with inputs
On 02/08/2014 05:36 PM, Richard Cziva wrote: Hi All, I am trying to print out every function that is being called while my Python program is running (own functions and library calls too). I can not modify the Python programs I am trying to profile. Let me give an example. A program contains a function like this: def foo(x): y = math.cos(x) z = 1 + 1 time.sleep(y+1) return x * 50 And it calls the function: print foo(100) I would like to retrieve an execution trace that shows me each function called with the value or hash of its arguments. According to the example, I am looking for a technique to extract something similar: foo(100) math.cos(100) time.sleep(0.87) Things I have tried with only partial success: - trace module - profile module / cProfile Could you suggest me a way of doing this? You need to wrap every function call in a tracing wrapper function that (1) does what you want (2) calls the wrapped function. Something like this: def trace (func, *args): # trace func_name = func.__name__ arg_strings = (str(arg) for arg in args) arg_string = ", ".join(arg_strings) print("%s(%s)" % (func_name, arg_string)) # call result = func(*args) if result: return result def f (x,y): return (x+y) / 2 def g (x,y): print((x+y) / 2) trace(g, 3, 7) z = trace(f, 3, 7) print(z) ==> g(3, 7) 5.0 f(3, 7) 5.0 As you see, there is a subtility about the distinction between functions that _do_ something (actions, in fact) and function that compute a product (function properly speaking). But actually you could ignore because the former implicitely return none. You cannot always have the same call expression as in the actual calling code: you always have the _values_. For example: a = 9 y = 7 trace(f(math.sqrt(a), y) will show: f(3, 7) Python does not let you know _the code_. You would need a "homoiconic" language like Lisp for that: a language in which "code is data" (data of the language types). In python, code is opaque, it is plain raw data (bit string) without any language type & value. So, I guess we cannot do much better than the above, but I may be wrong. The right way to do this would in fact be using so-called "decorators". But the example above shows the principle (and the code you'd have to put in a decorator). d ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trace / profile function calls with inputs
CCing tutor list. Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos > > >> You say functions. But what about the functions used to implement >> operators like +,-,*,+= etc? >I don't need them now. > >> to? If so what about the operations on built in classes? >> The x * 50 above for example? What about operations on literals? >> z = 1+1 for instance? >Neither them this time. > >> How are you planning on analyzing the results? Sometimes a more >> intelligent and considered approach is better than just logging >> everything... > >I need all the functions called by the program in a simple txt file. I >would like to create a statistic of the common functions called (yes, >identified by only name and input) between different software products. > > >Ah. That's what I feared. If you were trying to understand how some system functioned or trying to improve performance in some way a more considered subset would be feasible. But if you want to collect stats for their own sake then you have no option. And I don't really know how to help you. Maybe one of our python internals gurus can help there... Alan g. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] second if
On 07/02/14 16:14, rahmad akbar wrote: he guys, i am trying to understand this code: i understand the first if statement (if line.startswith..) in read_fasta function but couldnt understand the next one(if index >=...). thanks in advance!! I'm not sure what you don't understand about it. But so far as I can see its effect is to miss out the first line in the file that starts with '>' The logic appears to be, in pseudo code: read the file line buy line if line starts with > if not the first time add instance to the collection add one to index create an instance. It has to miss the first one because an instance doesn't exist yet. It seems odd and I'd probably have done it this way: read the file line buy line if line starts with > create an instance. add instance to the collection But there may be reasons why the author didn't do that. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] second if
rahmad akbar wrote: > he guys, i am trying to understand this code: i understand the first if > statement (if line.startswith..) in read_fasta function but couldnt > understand the next one(if index >=...). thanks in advance!! Every time a line starts with a ">" sign the current Fasta instance is stored in the items list and new Fasta instance is created. But the first time a line starts with ">" there is not yet a "current" instance which can be appended to the list. The index variable keeps track of the number of Fasta instances created, so the first time around the index is 0 and the if index >= 1: items.append(aninstance) suite is not executed. By the way, can you figure out what happens if the file contains no lines at all? How would you need to change the script to avoid raising an exception in this case? > import sys > #class declaration with both attributes we need > class Fasta: > def __init__(self, name, sequence): > #this will store the sequence name > self.name = name > #this will store the sequence itself > self.sequence = sequence > > #this function will receive the list with the file > #contents, create instances of the Fasta class as > #it scans the list, putting the sequence name on the > #first attribute and the sequence itself on the second > #attribute > def read_fasta(file): > #we declare an empty list that will store all > #Fasta class instances generated > items = [] > index = 0 > for line in file: > #we check to see if the line starts with a > sign > if line.startswith(">"): >#if so and our counter is large than 1 >#we add the created class instance to our list >#a counter larger than 1 means we are reading >#from sequences 2 and above >if index >= 1: >items.append(aninstance) >index+=1 >#we add the line contents to a string >name = line[:-1] >#and initialize the string to store the sequence >seq = '' >#this creates a class instance and we add the attributes >#which are the strings name and seq >aninstance = Fasta(name, seq) > else: >#the line does not start with > so it has to be >#a sequence line, so we increment the string and >#add it to the created instance > seq += line[:-1] > aninstance = Fasta(name, seq) > > #the loop before reads everything but the penultimate > #sequence is added at the end, so we need to add it > #after the loop ends > items.append(aninstance) > #a list with all read sequences is returned > return items > > fastafile = open(sys.argv[1], 'r').readlines() > mysequences = read_fasta(fastafile) > > print mysequences > > for i in mysequences: > print i.name > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python as Teaching Language
On Mon, 2014-02-10 at 00:17 +, Alan Gauld wrote: […] > And in my tutorial I deliberately don't teach many of the "standard" > Python idioms because I'm trying to teach programming rather than > Python. So if python has an insanely great way to do stuff but virtually > no other language has it I will ignore it. (Or more > likely mention it as an aside/footnote.) In the case of file handling and the with statement, indeed any resource management, it is a standard idiom across languages so well worth covering in Python: RAII in C++, ARM in Java, etc. > What's interesting (to me) is that I'm currently working on a new > project aimed at beginners who have progressed beyond the first > steps but are not confident in putting together a bigger program. > That is allowing me to address many of the idiomatic aspects > of Python that my first book didn't permit. It means that although there > is some overlap in coverage the style and content are quite different. > > Context and target make a big difference in what and how you teach. Definitely. Good luck with the new project. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python as Teaching Language
On Sun, 2014-02-09 at 13:36 +, Oscar Benjamin wrote: […] > I agree entirely, but what you've overlooked is that my examples are > carefully targeted at a particular part of a tutorial-based class. > We're talking about iteration so this is quite early in the course. At > this stage I want my students to understand that closing a file is an > explicit action that needs to occur. Later in the course I will teach > exception handling and explain that the above should be rewritten as > > f = open('myfile.txt') > try: > for line in f: > print(line.upper()) > finally: > f.close() > > Shortly after that we will look at using (but not creating) context > managers and I'll explain that file objects are also context managers. Works for me. Personally I would ensure I put in forward signposts at the time of covering earlier codes to ensure people realize there is a progression and that the current code is initial not final. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] second if
Also, could you explain better what is your doubt? You don't understand what "index >= 1" means, or why this "if" at this point, or anything else? Best 2014-02-07 17:14 GMT+01:00 rahmad akbar : > he guys, i am trying to understand this code: i understand the first if > statement (if line.startswith..) in read_fasta function but couldnt > understand the next one(if index >=...). thanks in advance!! > > import sys > #class declaration with both attributes we need > class Fasta: > def __init__(self, name, sequence): > #this will store the sequence name > self.name = name > #this will store the sequence itself > self.sequence = sequence > > #this function will receive the list with the file > #contents, create instances of the Fasta class as > #it scans the list, putting the sequence name on the > #first attribute and the sequence itself on the second > #attribute > def read_fasta(file): > #we declare an empty list that will store all > #Fasta class instances generated > items = [] > index = 0 > for line in file: > #we check to see if the line starts with a > sign > if line.startswith(">"): >#if so and our counter is large than 1 >#we add the created class instance to our list >#a counter larger than 1 means we are reading >#from sequences 2 and above >if index >= 1: >items.append(aninstance) >index+=1 >#we add the line contents to a string >name = line[:-1] >#and initialize the string to store the sequence >seq = '' >#this creates a class instance and we add the attributes >#which are the strings name and seq >aninstance = Fasta(name, seq) > else: >#the line does not start with > so it has to be >#a sequence line, so we increment the string and >#add it to the created instance > seq += line[:-1] > aninstance = Fasta(name, seq) > > #the loop before reads everything but the penultimate > #sequence is added at the end, so we need to add it > #after the loop ends > items.append(aninstance) > #a list with all read sequences is returned > return items > > fastafile = open(sys.argv[1], 'r').readlines() > mysequences = read_fasta(fastafile) > > print mysequences > > for i in mysequences: > print i.name > > -- > many thanks > mat > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] learning recursion
On Sun, 2014-02-09 at 13:56 -0500, Dave Angel wrote: […] > Not as bad as attributing that code to me. Apologies for that implication, bad editing on my part, I should have retained the link to the author. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor