Commandline arguments python myprogram py 10 100 output

  • Slides: 17
Download presentation
Command-line arguments python myprogram. py 10 100 output. txt sys. argv: [ “myprogram. py”,

Command-line arguments python myprogram. py 10 100 output. txt sys. argv: [ “myprogram. py”, 100, “output. txt” ] First item in sys. argv list: program name, next items are command-line arguments 2

Long headers in Fasta file >AC 121234 Medicago truncatula clone mth 2 -19 o

Long headers in Fasta file >AC 121234 Medicago truncatula clone mth 2 -19 o 7, WO RKING DRAFT SEQUENCE, 5 unordered pieces. GGTGAAGGATGAGGATTTGCAAAAGACGGCCTTTAGGACACGTTATGGT CATTACGAGTACAAAGTGATGCCTTTCGGTGTTACTAAGGCGCCTGGTG TTTTTATGGAGTACATGAACCG… … • Some applications can’t handle long headers • Python program for “pruning” the headers, leaving just the unique ID. . ? 3

Instructions for how to use the function for line in infile: memory efficient! Note:

Instructions for how to use the function for line in infile: memory efficient! Note: line ends in newline prune. py No newline in first field after splitting the line; use print Newline still there, write the line as it is with no extra newline sys. argv 4

Before / after pruning >AC 121234 Medicago truncatula clone mth 2 -19 o 7,

Before / after pruning >AC 121234 Medicago truncatula clone mth 2 -19 o 7, WO RKING DRAFT SEQUENCE, 5 unordered pieces. GGTGAAGGATGAGGATTTGCAAAAGACGGCCTTTAGGACACGTTATGGT CATTACGAGTACAAAGTGATGCCTTTCGGTGTTACTAAGGCGCCTGGTG TTTTTATGGAGTACATGAACCG… … >AC 121234 GGTGAAGGATGAGGATTTGCAAAAGACGGCCTTTAGGACACGTTATGGT CATTACGAGTACAAAGTGATGCCTTTCGGTGTTACTAAGGCGCCTGGTG TTTTTATGGAGTACATGAACCG… … 5

More string methods: splitlines, join, replace Parents Music Resource Center Concerning: crude language in

More string methods: splitlines, join, replace Parents Music Resource Center Concerning: crude language in much of today’s music Task: implement censorship to remove bad words 6

Split text in list of lines censorship. py In each line, replace each bad

Split text in list of lines censorship. py In each line, replace each bad word with BEEP Join censored lines with newlines and return full text If any words were BEEPed, print line and play one beep per word 7

Program tested on two songs. Celine Dion: "In these moments, moments of our lives

Program tested on two songs. Celine Dion: "In these moments, moments of our lives All the world is ours And this world is so right You and I sharing this time together Sharing the same dream As the time goes by we will find These are the special times Times we'll remember These are the precious times The tender times we'll hold in our hearts forever These are the sweetest times These times together And through it all, one thing will always be true The special times are the times I share with you With each moment, moment passing by We'll make memories that will last all our lives As you and I travel through time together Living this sweet dream And every day we can say. . With each moment, moment p. BEEPing by Beeped words: 1 We find words containing a bad word: not desirable here. See exercise. 8

Crime Mob : Ol' stankin BEEP (Hoe) Jank BEEP (Hoe) Suck my BEEP you

Crime Mob : Ol' stankin BEEP (Hoe) Jank BEEP (Hoe) Suck my BEEP you (Hoe) Ol' fat BEEP (Hoe) But aiight! We finna get these lame BEEP niggaz You see a hoe BEEP nigga, call his BEEP out. Aye! Stomp his BEEP like (Hoe) Ol' lame BEEP (Hoe) I'ma tell you how it is nigga you betta get the BEEP back cause a nigga like me don't give a BEEP A nigga suppose to gon leave yo BEEP choked You sound like a BEEP yo BEEP I'ma hit we don't give a BEEP cause you is a lame One hitter quitter yo BEEP get popped Back the BEEP up 'fore I show you who reala Whats up wit ya BEEP nigga Ol' sucka BEEP, busta BEEP, cryin to yo momma BEEP I'ma keep up drama I'm a mutha. BEEPin plum BEEP See you just a dumb BEEP go on wit yo young BEEP Try me like a sucka but I know you just a lame BEEP In my section they glad to see a nigga that don't give a BEEP Stomp you to the floor and tell you get yo pussy BEEP up Pick that nigga BEEP up, tear his lame BEEP up Niggaz representin Ellenwood time to m. BEEP up Throwin blows like Johnny Cage, you think you wanna BEEP wit me Do this BEEP like Pastor Troy Uuh Huh I'm outside hoe Take my BEEPin word I ain't got no reason to lie hoe Beeped words: 34 9

Regular Expressions – Motivation Problem: search suspicious text for any Danish email address: <something>@<something>.

Regular Expressions – Motivation Problem: search suspicious text for any Danish email address: <something>@<something>. dk text 1 = "No Danish email here bush@whitehouse. org *@$@. hls. 29! fj 3 a“ text 2 = "But here: chili@daimi. au. dk what a *(. @#$ nice @#*. ( el ds“ text 3 = "And here perhaps? rubbish@junk. garbage@bogus@dk @. dk a@. dk" - Cumbersome using ordinary string methods. 10

Reg. Exp solution (to be explained later) Text 2 contains this Danish email address:

Reg. Exp solution (to be explained later) Text 2 contains this Danish email address: chili@daimi. au. dk 11

Regular Expressions • Instead of searching for a specific string we can search for

Regular Expressions • Instead of searching for a specific string we can search for a text pattern – Don’t have to search explicitly for ‘Monday’, ‘Tuesday’, ‘Wednesday’. . : there is a pattern in these search strings. – A regular expression is a text pattern • In Python, regular expression processing capabilities provided by module re 12

Example Simple regular expression: reg. Exp = “football” - matches only the string “football”

Example Simple regular expression: reg. Exp = “football” - matches only the string “football” To search a text for reg. Exp, we can use re. search( reg. Exp, text ) 13

Compiling Regular Expressions re. search( reg. Exp, text ) 1. Compile reg. Exp to

Compiling Regular Expressions re. search( reg. Exp, text ) 1. Compile reg. Exp to a special format (an SRE_Pattern object) 2. Search for this SRE_Pattern in text 3. Result is an SRE_Match object If we need to search for reg. Exp several times, it is more efficient to compile it once and for all: compiled. RE = re. compile( reg. Exp) 1. Now compiled. RE is an SRE_Pattern object compiled. RE. search( text ) 2. Use search method in this SRE_Pattern to search text 3. Result is same SRE_Match object 14

import re Searching for ‘football’ text 1 = "Here are the football results: Bosnia

import re Searching for ‘football’ text 1 = "Here are the football results: Bosnia - Denmark 0 -7" text 2 = "We will now give a complete list of python keywords. " regular. Expression = "football" compiled. RE = re. compile( regular. Expression) SRE_Match 1 = compiled. RE. search( text 1 ) SRE_Match 2 = compiled. RE. search( text 2 ) if SRE_Match 1: print "Text 1 contains the substring ‘football’" Compile regular expression and get the SRE_Pattern object Use the same SRE_Pattern object to search both texts and get two SRE_Match objects (or none if the search was unsuccesful) if SRE_Match 2: print "Text 2 contains the substring ‘football’" Text 1 contains the substring 'football' 15

Building more sophisticated patterns Metacharacters: ? : matches zero or one occurrences of the

Building more sophisticated patterns Metacharacters: ? : matches zero or one occurrences of the expression it follows +: matches one or more occurrences of the expression it follows *: matches zero or more occurrences of the expression it follows # search for zero or one t, followed by two a’s: reg. Exp 1 = “t? aa“ # search for g followed by one or more c’s followed by one a: reg. Exp 1 = “gc+a“ #search for ct followed by zero or more g’s followed by one a: reg. Exp 1 = “ctg*a“ 16

Use the SRE_Pattern objects to search the text and get SRE_Match objects Text contains

Use the SRE_Pattern objects to search the text and get SRE_Match objects Text contains the regular expression t? aa Text contains the regular expression gc+a Text contains the regular expression ctg*a 17

. . on to the exercises 18

. . on to the exercises 18