Python Classes, Modules, Time Functions, and Regular Expressions, Study notes of Computer Science

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

Pre 2010

Uploaded on 09/17/2009

koofers-user-edt-3
koofers-user-edt-3 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Python [3]
Alessio Signorini <alessi[email protected]u>
Introduction to Networks
and their Applications
22C:118, Fall 2005
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Python Classes, Modules, Time Functions, and Regular Expressions and more Study notes Computer Science in PDF only on Docsity!

Introduction to Python [3]

Alessio Signorini <alessio [email protected]>

Introduction to Networks

and their Applications

22C:118, Fall 2005

Class Objects

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

Importing modules

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

Importing modules

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

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+')

Regular Expressions

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

Regular Expressions Examples

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)

Regular Expressions Examples

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!”