[Tutor] string indexing

2014-01-19 Thread rahmad akbar
hey guys, super  noob here, i am trying to understand the following code
from google tutorial which i failed to comprehend

#code start
# E. not_bad
# Given a string, find the first appearance of the
# substring 'not' and 'bad'. If the 'bad' follows
# the 'not', replace the whole 'not'...'bad' substring
# with 'good'.
# Return the resulting string.
# So 'This dinner is not that bad!' yields:
# This dinner is good!
def not_bad(s):
  # +++your code here+++
  # LAB(begin solution)
  n = s.find('not')
  b = s.find('bad')
  if n != -1 and b != -1 and b > n:
s = s[:n] + 'good' + s[b+3:]
  return s
#code end

 on the following lines, what is -1, is that index number? and i dont
understand the entire second line

if n != -1 and b != -1 and b > n:
s = s[:n] + 'good' + s[b+3:]

-- 
many thanks
mat
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string indexing

2014-01-20 Thread rahmad akbar
Spir and Peter, thanks for the specifics, super helpful. Alan, super thanks
for the general advice, you guys are awesome!!




On Mon, Jan 20, 2014 at 5:34 AM,  wrote:

> Send Tutor mailing list submissions to
> tutor@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> tutor-requ...@python.org
>
> You can reach the person managing the list at
> tutor-ow...@python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>1. Re: Understanding Classes (Alan Gauld)
>2. Re: string indexing (Keith Winston)
>3. Re: Question on os.popen (Alan Gauld)
>4. Re: Question on os.popen (SM)
>5. Re: Question on os.popen (eryksun)
>
>
> --
>
> Message: 1
> Date: Sun, 19 Jan 2014 23:50:59 +
> From: Alan Gauld 
> To: tutor@python.org
> Subject: Re: [Tutor] Understanding Classes
> Message-ID: 
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 19/01/14 21:59, Christian Alexander wrote:
> > Looked all over the net for class tutorials
> > Unable to understand the "self" argument
> > Attempting to visual classes
>
> If you read my OOP tutorial there is a section there specifically about
> self.
>
> And the v3 tutor includes an introduction to the formal visualisation
> technique for OOP called UML. The diagrams illustrating the designs may
> help.
>
> http://www.alan-g.me.uk/l2p/tutclass.htm
>
> > I have searched high and low, for easy to follow tutorials regarding
> > classes.  Although I grok the general concept of classes,
>
> Do you also grok the concept of objects?
> Classes on their own are fairly useless (unless you are using Java)
> it is only when you create a universe of objects from those classes that
> they become useful.
>
> If you can repeat to us your understanding of classes and their
> relationship with objects that will help us understand your
> level and shape our responses accordingly.
>
> > to visually understand what exactly "self" does, or why it is even
> > necessary.  It seems very "magic" to me.
>
> When you define a class you define the data (attributes) that
> the class instances will have. Each instance will have a copy of the
> data defined in the __init__() method.
> You also define a set of operations or methods that are associated
> with the class. Those methods are shared by the instances.
>
> Note the difference. Instances get a copy of the attributes
> but they all share the methods.
>
> Thus when you invoke a method on an instance the instance relays that
> call to the class. For the class to know which instance is being
> operated on, and for the method to be able to access the correct
> instance's data it needs a reference to the instance. That reference
> is typically called 'self' or 'this'. (In some languages it's fixed
> but in Python self is only a convention, you can use any name you like).
>
> You can make the call to the class explicit and it will still work.
> See below:
>
> # define a class
> class MyClass:
>  def __init__(self,x): self.x = x
>  def myMethod(self): print(self.x)
>
> # create some instances
> ObjA = MyClass(2)
> ObjB = MyClass(4)
> ObjC = MyClass(6)
>
> # send some messages/call methods
> objA.myMethod()   # call from the instance
> MyClass.myMethod(ObjB)  # call explicitly to the class
> objC.myMethod()  # direct again
>
> All 3 calls do the same thing except the middle one
> passes the object identifier directly to the class
> whereas the first and last both do that internally
> within the object structure.
>
> > difficult with the "__init__()" method in classes,
>  > and why that is also required.
>
> It is not *required* as such. You can create a class
> without an init but it's unusual.
>
> When you create an instance of a class it creates a
> data structure in memory referenced by the name of
> the instance. But that structure is empty, it has
> no data. So to populate the data for the instances
> you must initialize it. That's what __init__() does.
> It takes the arguments you provide and applies them
> to the instance along with any static data definitions
> you may define.
>
> In the example we create an instance variable, x,
> within the instances and assign the value of the
> argument passed to init. Like any other method the
> actual code lives in the class so we could initialize
> it by calling init like so:
>
> MyClass.__init__(objC, 66)
>
> which is almost the same as doing:
>
> objC = MyClass(66)
>
> The difference is that the first case requires the object ObjC
> to already exist, the second example creates a new instance and
> then calls init on that instance.
>
> > Keep in mind that I am a visual person (maybe I should have
> > been a graphic designer), therefore most programmi

[Tutor] if >= 0

2014-02-10 Thread rahmad akbar
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] if >= 0

2014-02-11 Thread rahmad akbar
Alan,

roger that and thanks a bunch. now replying with reply all, i am a super
noob and thanks for the kind adivce


On Tue, Feb 11, 2014 at 10:31 AM, ALAN GAULD wrote:

> CCing the list. Please use Reply All when responding.
>
> > thanks Alan, i understand now zero is False.
>
> That's right. but...
>
> > so if one of the 'if' test is false, that 'for' loop is also halted?
> > and does not proceed to the next element?
>
> This bit  is wrong.
>
>
> The for loop will continue to completion every time.
>
>
> >>> for element in in_file:
> >>>   if  element.find(LOCUS'):
>
> >>> locus += element
> >>>   elif element.find(...)
>
> What happens is that because the find(LOCUS) is the first test it will
> almost
> always be true. So the locus += element will be executed on almost every
> line
> Because that branch of the if construct is executed no other branch is
> executed.
>
> In an if/elif/else structure only one branch ever gets executed in a given
> iteration of the for loop. elif stands for "else if" so the test in the
> elif statements
> only get executed if all the preceding tests fail. So the order of the
> tests is
> very important. Now in your case the first branch nearly always succeeds
> and so the subsequent branches never get executed.
>
> When you add the >=0 condition you stop the first test from catching
> everything.
>
> So the lines that do not contain LOCUS now get tested for the other values
> and as a result update their respective string variables.
>
> But the for loop continues regardless of which branch is used, it is only
> controlled by the number of lines in the file. It does not care what the
> code
> inside the loop body does (unless it explicitly uses return or break or
> continue).
>
> HTH
>
> Alan g.
>



-- 
many thanks
mat
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] next element in list

2014-02-26 Thread rahmad akbar
hey guys

i have this file i wish to parse, the file looks something like bellow.
there are only four entry here (AaaI, AacLI, AaeI, AagI). the complete file
contains thousands of entries

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
REBASE, The Restriction Enzyme Database   http://rebase.neb.com
Copyright (c)  Dr. Richard J. Roberts, 2014.   All rights reserved.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Rich RobertsJan 30 2014

AaaI (XmaIII) C^GGCCG
AacLI (BamHI) GGATCC
AaeI (BamHI)  GGATCC
AagI (ClaI)   AT^CGAT


the strategy was to mark the string 'Rich Roberts' as the start. i wrote
the following function. but then i realized i couldn't do something like
.next() to the var in_file which is a list. so i added a flag start = False
in which will be turned to True upon 'Rich Roberts' found. is the any
simpler way to move to the next element in the list. like built in method
or something like that.

def read_bionet(bionetfile):
  res_enzime_dict = {}
  in_file = open(bionetfile, 'r').readlines()
  start = False
  for line in in_file:
if line.startswith('Rich Roberts'):
  start = True
if start and len(line) >= 10:
line = line.split()
res_enzime_dict[line[0]] = line[-1]
  return res_enzime_dict


-- 
many thanks
mat
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] next element in list

2014-02-26 Thread rahmad akbar
David, Peter

roger that and thanks so much!!


On Wed, Feb 26, 2014 at 1:29 PM, Peter Otten <__pete...@web.de> wrote:

> rahmad akbar wrote:
>
> > hey guys
> >
> > i have this file i wish to parse, the file looks something like bellow.
> > there are only four entry here (AaaI, AacLI, AaeI, AagI). the complete
> > file contains thousands of entries
> >
> > =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> > REBASE, The Restriction Enzyme Database   http://rebase.neb.com
> > Copyright (c)  Dr. Richard J. Roberts, 2014.   All rights reserved.
> > =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> >
> > Rich RobertsJan 30
> > 2014
> >
> > AaaI (XmaIII) C^GGCCG
> > AacLI (BamHI) GGATCC
> > AaeI (BamHI)  GGATCC
> > AagI (ClaI)   AT^CGAT
> >
> >
> > the strategy was to mark the string 'Rich Roberts' as the start. i wrote
> > the following function. but then i realized i couldn't do something like
> > .next() to the var in_file which is a list. so i added a flag start =
> > False in which will be turned to True upon 'Rich Roberts' found. is the
> > any simpler way to move to the next element in the list. like built in
> > method or something like that.
> >
> > def read_bionet(bionetfile):
> >   res_enzime_dict = {}
> >   in_file = open(bionetfile, 'r').readlines()
> >   start = False
> >   for line in in_file:
> > if line.startswith('Rich Roberts'):
> >   start = True
> > if start and len(line) >= 10:
> > line = line.split()
> > res_enzime_dict[line[0]] = line[-1]
> >   return res_enzime_dict
>
> As David says, don't call readlines() which reads the lines of the file
> into
> a list, iterate over the file directly:
>
> def read_bionet(bionetfile):
> with open(bionetfile) as in_file:
> # skip header
> for line in in_file:
> if line.startswith("Rich Roberts"):
> break
>
> # populate dict
> res_enzimes = {}
> for line in in_file: # continues after the line with R. R.
> if len(line) >= 10:
> parts = line.split()
> res_enzimes[parts[0]] = parts[-1]
>
> # file will be closed now rather than at
> # the garbage collector's discretion
>
> return res_enzimes
>
>
> ___
> 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


[Tutor] trying to understand pattern matching code

2014-04-25 Thread rahmad akbar
hey guys,
i am trying to understand this code pasted bellow,
1. what is line means? mask[c] |= bit
2. then the line bit *=2, this increment the bit to 2 for each character?
3. what is this line means? accept_state = bit //2
4. lastly, what is this scary line means? D = (( D << 1) + 1) & masks [ c
def ShiftAnd (P , T ):
  m = len ( P )
  masks = dict () # empty dictionary
  bit = 1
  for c in P :
if c not in masks : masks [ c ] = 0
masks [ c ] |= bit
bit *= 2
  accept_state = bit // 2
  D = 0 # bit - mask of active states
  i = 0
  for c in T :
D = (( D << 1) + 1) & masks [ c ]
  if ( D & accept_state ) != 0:
yield i
  i += 1





-- 
many thanks
mat
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trying to understand pattern matching code

2014-04-25 Thread rahmad akbar
Thanks joel

Could you elaborate more on bitwise, and why do we need to double bit. I
experimented with some input

ShiftAnd('pattern', 'textcontainingpatternandpatern')

print out the (masks, accept_state), looks like bellow, the rest make sense
couse the numbers are doubled, but the 't' goes 4+8 = 12. why is this?

({'a': 2, 'e': 16, 'n': 64, 'p': 1, 'r': 32, 't': 12}, 64)


and  i could not make sense of this line yet :  D = (( D << 1) + 1) & masks
[ c ]


to Dave,

i do i do the text mode? i had no idea this was on html
Am 25.04.2014 14:16 schrieb "Joel Goldstick" :

>
> On Apr 25, 2014 7:14 AM, "rahmad akbar"  wrote:
> >
> > hey guys,
> > i am trying to understand this code pasted bellow,
> > 1. what is line means? mask[c] |= bit
> This bitwise or.  It sets the rightmost bit in masks[c]
> > 2. then the line bit *=2, this increment the bit to 2 for each characters
> This doubles bit.
> > 3. what is this line means? accept_state = bit //2
> Integer division
> > 4. lastly, what is this scary line means? D = (( D << 1) + 1) & masks [
> c
>
> The << is bitwise shift left
> > def ShiftAnd (P , T ):
> >   m = len ( P )
> >   masks = dict () # empty dictionary
> >   bit = 1
> >   for c in P :
> > if c not in masks : masks [ c ] = 0
> > masks [ c ] |= bit
> > bit *= 2
> >   accept_state = bit // 2
> >   D = 0 # bit - mask of active states
> >   i = 0
> >   for c in T :
> > D = (( D << 1) + 1) & masks [ c ]
> >   if ( D & accept_state ) != 0:
> > yield i
> >   i += 1
> >
> >
> You should sprinkle some print statements in and run with various arguments
> >
> >
> >
> > --
> > 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] trying to understand pattern matching code

2014-04-26 Thread rahmad akbar
Danny and Dave,
super thanks on the plain text advice. i'm sending this mail on plain
text, please let me know if it turned out to be otherwise

back to the problem
just googled binary and i now have some idea on it. i now understand
turning on 16 and 8 gives 24 and thus 4+8 = 12. why is this? i now
understand this bit.

but  i still couldnt get this line though

D = (( D << 1) + 1) & masks [ c ]

On Sat, Apr 26, 2014 at 12:37 AM, Dave Angel  wrote:
> rahmad akbar  Wrote in message:
>>
>>
>  to Dave,
>>   i do i do the text mode? i had no idea this was on html
>
> Whatever mail program you're using is apparently defaulting to
>  html.  Find a menu item that specifies 'text' or 'plain text' or
>  'text only'.
>
> Or tell us what email program you're using,  what os, and versions
>  for each. Maybe someone will have experience with
>  it.
>
> Back to your problem,  do you understand what binary is, and how
>  to convert (mentally) to and from decimal? Do you know that the
>  bits have values of 1, 2, 4, 8, 16, etc? And that 24 is created
>  by turning on the 16 bit and the 8 bit, commonly referred to as
>  bits 4 and 3, respectively.
>
>
> --
> DaveA
>
> ___
> 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] trying to understand pattern matching code

2014-04-26 Thread rahmad akbar
hi Alan,

your explanation clears most things, super thanks!!

On Sat, Apr 26, 2014 at 10:58 AM, Alan Gauld  wrote:
> On 26/04/14 09:36, rahmad akbar wrote:
>
>> but  i still couldnt get this line though
>>
>> D = (( D << 1) + 1) & masks [ c ]
>
>
> Do you understand the concept of bit shifting?
> ie
> 000110 shifted left gives
> 001100
>
> and
> 000110 shifted right gives
> 11
>
> In other words the bit pattern moves left or
> right and the missing bits are replaced with
> zeros.
>
> The effect of shift left is to multiply the
> number by two.
>
> + 1 just adds 1 to the resulting number
>
> So the first bit of your line is the same as
>
> D = ((D*2)+1)
>
> The second part uses bitwise and with a mask chosen from a list of masks,
> A bitwise and has the effect of zeroing any bit that is zero in the mask and
> keeping any bit that is one in the mask.
>
> So
>
> 11100011  -> My data
>   -> my mask, designed to return the right hand 4 bits
> 0011  -> data & mask
>
> Similarly
> 11100011  -> data
>   -> mask for leftmost 4 bits
> 1110  -> data & mask
>
> So which bits are preserved in your D after the math and masking depends on
> what the masks[c] mask looks like.
>
> You might want to use some print statements using the bin()
> function to see the actual bit patterns. (If you do you might
> find that long bit patterns don;t show what you expect, if
> that happens try masking the result to say 16 places
> like this:
>
> print bin(mydata & 0x)  #  => 
>
> 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



-- 
many thanks
mat
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] class to function

2014-05-25 Thread rahmad akbar
Hi guys

i am trying to understand this code:
http://nbviewer.ipython.org/gist/BenLangmead/6665861

i understand functions quite alright . but i have no idea about
classes yet. the code is written using class and i could not make much
sense out of it. all this init and self thingy drive me crazy. can you
guys help explain. super thanks

-- 
many thanks
mat
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: class to function

2014-05-26 Thread rahmad akbar
thanks guys for the replies,

Danny, your explanation helped alot, i'll have a go on Alan Gaud's
tutorial. super thanks

On Mon, May 26, 2014 at 9:04 AM, Danny Yoo  wrote:
>
>
> -- Forwarded message --
> From: diliup gabadamudalige 
> Date: Sunday, May 25, 2014
> Subject: [Tutor] class to function
> To: Danny Yoo 
>
>
> Is it wrong to say that  __init__() is to initialize all the conditions you
> setupin the class? That's how i see it.
> I give below the way I see a class and it's workings. Please Correct if any
> mistakes.
>
> Say you have a class for a Spaceship
> Class Spaceship():
>
> under the def __init__():
> you set up all the details you want the spaceship to have. For example,
> self.engine=100
> self. guns=100
> self.shield=250
> self.life=5
>
> self if the Spaceship which will be built using the details given under the
> init in the Class Spaceship. So a class is a blu print to build the space
> ship and the init function contains all the things the space ship will
> contain.
> Now when you want a ne space ship say a RED one
>  you can call the class with
> RedShip=Spaceship()
> and you can now giv e a new character to the space ship. A colour! like this
> RedShip.colour=Red
> Now along with the
> RedShip.enging=100
> RedShip.guns=100
> RedShip.shield=250
> RedShip.life=5
> the RedShip also has a NEW ATTRIBUTE. Colour.
> RedShip.colour=Red
>
> The def__init__() can also be setup so that you can give all the attributes
> its values like this.
> def__init__(power,bullets,energy,life)
>
> RedShip.enging=power
> RedShip.guns=bullets
> RedShip.shield=energy
> RedShip.life=life
>
> this way each ship can have different attributes. But they are all
> Spaceships.
>
>
>
>
> On Mon, May 26, 2014 at 6:38 AM, Danny Yoo  wrote:
>>
>> > i am trying to understand this code:
>> > http://nbviewer.ipython.org/gist/BenLangmead/6665861
>>
>> I'm slightly familiar with the purpose of the code.  It's constructing
>> a Suffix Tree, though not in linear time.
>>
>> Reading the code... ah.  I see.  This is enumerating through all
>> suffixes, and building it by repeated insertion into a trie.
>>
>> http://en.wikipedia.org/wiki/Trie
>>
>> The classes are used here to represent structured data and operations
>> to be performed on that structured data.  The code fundamentally has a
>> structured value called a Node, with two fields to represent the
>> string label and the links to other nodes.
>>
>>
>>
>> Aside: there is a very good book by Dan Gusfield that talks about
>> suffix trees and how to construct them called "Algorithms on Strings,
>> Trees and Sequences: Computer Science and Computational Biology"
>>
>>
>> http://www.amazon.com/Algorithms-Strings-Trees-Sequences-Computational/dp/0521585198
>>
>> which you may want to look at if you're interested in these algorithms.
>>
>>
>> It is probably not a good idea to try to intermix trying to understand
>> an algorithm like this at the same time as you're learning basic
>> features in your programming language.  Consider a slightly simpler
>> example to learn about "classes", apart from the algorithms you are
>> studying.  Most good Python tutorials should cover how to use classes
>> to build structured data and manipulate it.  Alan Gauld's Python
>> tutorial, for example, should have a section on this.
>>
>> http://www.alan-g.me.uk/tutor/tutclass.htm
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>
>
>
>
> --
> Diliup Gabadamudalige
>
> http://www.diliupg.com
> http://soft.diliupg.com/
>
> **
> This e-mail is confidential. It may also be legally privileged. If you are
> not the intended recipient or have received it in error, please delete it
> and all copies from your system and notify the sender immediately by return
> e-mail. Any unauthorized reading, reproducing, printing or further
> dissemination of this e-mail or its contents is strictly prohibited and may
> be unlawful. Internet communications cannot be guaranteed to be timely,
> secure, error or virus-free. The sender does not accept liability for any
> errors or omissions.
> **
>
>
>
> ___
> 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