






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to creating classes in python, importing modules, working with time functions, and using regular expressions. It covers the basics of creating a class, special methods, importing modules, time functions, and regular expression patterns. It also includes examples of using regular expressions to search and match strings.
Typology: Study notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!







Alessio Signorini <alessio [email protected]>
22C:118, Fall 2005
Create a new class in Python is simple. Just write class MyClass (class1, class2, ...): “This is what this class does, nothing for now!” do something Special methods of each class are init(self) # when istance is created del(self) # when istance is deleted getattribute(self, name) # when attrib is requested doc # documentation string
A huge number of Python modules are available. Look on the web to discover them. An important builtin modules is sys. It supports important attributes as argv # list of the commandline arguments maxint # return the largest integer available platform # string with the name of the platform version # return the python version stdin, stdout # predefinite file object for input/output stderr # predefinite file object for error output
Modules are imported at the beginning of a program with from sys import argv or from sys import * # import all attributes of sys To use an imported attributes just write something like print sys.argv[0] # print the command launched or for argument in sys.argv print argument # print list of all the arguments
Regular expressions are powerful tools that allows to scan trough strings identifying interesting parts or replacing characters. In Python regular expressions are supported thanks to the module re. (add import re at the beginning of your code) To use the module, you have to create a regular expression object with your settings for the search: reo = re.compile(r'\w+')
Useful patterns for regular expression are \w (\W) # matches one alphanumeric character \d (\D) # matches one digit \s (\S) # matches one whitespace \A # empty string at the start of the string \b (\B) # empty string at start or end of a word \Z # empty string at the end of the string
. # matches any character except \n
r/'aa.*bb' # matches “aabb” as “aaabb” or “aacbbc” r/'aa.+bb' # matches “aa23bb” or “aacbb” but not “aabb” r/'\bhis\b' # matches “his” but not “this” or “history” r/'\bher\w+' # matches “her” or “herbal” but not “mother” r/'\d+' # matches “123” and “aa23bb” r/'\b\d{6,6}\b' # matches only numbers of six digits r/'\d\d' # matches two following digits as in or “123” r/'\d\d\w\w\w' # matches 2 digits followed by 3 chars (12jan)
import re r1 = re.compile(r'box') if r1.match(“inbox”): print “Match succeded!” else: print “Match failed!” # prints: match failed if r1.search(“inbox)”: print “Search succeded!” # prints: search succeded else: print “Search failed!”