| 晓天's profile心田上,梦园中PhotosBlogLists | Help |
|
June 23 Python study notes 61 Regular Expressions
a) Regular expressions are a powerful and standardized way of searching, replacing, and parsing text with complex patterns of characters.
b) In python, all functionality related to regular expressions is contained in the re module.
c) The $ means "end of string", the caret ^ means "beginning of the string"
d) \b means "a word boundary must occur right here", in python, the '\' character in a string must itself be escaped.
e) To work around the backslash plague, you can use what is called a raw string, by prefixing the string with the letter r. This tells Python that nothing in this string should be escaped.
f) M? optionally match a single M character.
g) Pattern in parenthese defines a set of exclusive patterns, separated by vertical bars. (CM|CD|D?C?) etc.
2 Using the {n,m} Syntax
a) ^M{0,3} means match the start of the string, then anywhere from zero to three M characters, M{3} means exactly three times.
b) There's no way to programmatically determine that two regular expressions are equivalent.
3 Verbose Regular Expressions
a) a Verbose Regular Expression is different from a compact regular expression in two ways: Whitespace is ignored. Comments are ignored.
b) A comment in a verbose regular expression starts with a # character and goes until the end of the line
c) When using verbose regular expression, you need to pass an extra argument when working with them: re.VERBOSE.
d) \d means any numeric digit(0 through 9)
e) putting pattern in parenthese means remember them as a group that I can ask for later.
f) to access the groups that the regular expression parser remembered along the way, use groups() method on the object that the search function returns. It will return a tuple of however many gourps were defined in the regular expression.
g) \D means any character except a numeric digit, and + means 1 or more
h) * means zore or more characters. June 20 Python study notes 51 Handling exceptions
a) Python uses try...except to handle exceptions and raise to generate them.
b) A try...except block can have an else clause, like an if statement. If no exception is raised during the try block, the else clause is executed afterwards.
2 Working with File Objects
a) Python has a built-in function, open, for opening a file on disk. open returns a file object, which has methods and attributes for getting information about and manipulating the opened file.
b) The open method can take up to three parameters: a filename, a mode, and a buffering parameter. Only the first one, the filename, is required.
c) The mode attribute of a file object tells you in which mode the file was opened.
d) The name attribute of a file object tells you the name of the file that the file object has open.
e) The tell method of a file object tells you your current position in the open file.
f) The seek method of a file object moves to another position in the open file. The second parameter specifies what the first one means.
g) The read method reads a specified number of bytes from the open file and returns a string with the data that was read. The optional parameter specifies the maximum number of bytes to read.
h) The closed attribute of a file object indicates whether the object has a file open or not. To close a file, call the close method of the file object.
i) try...finally block: code in the finally block will always be executed, even if something in the try block raises an exception.
j) There are two modes for writing to files: "Append" mode will add data to the end of the file, "Write" mode will overwrite the file. "Append" works even if the file didn't exist, it will create the file if necessary.
3 Iterating with for loops
a) syntax of iterating a list: for s in li: syntax of doing a normal counter: for i in range(n):
b) range produces a list of intergers
c) os.environ is a dictionary of the enviroment variables defined on your system.
4 Using sys.modules
a) you can always get a reference to a module through the global dictionary sys.modules
b) The sys module contains system-level information, such as the version of Python you're running, and system-level options
c) sys.modules is a dictionary containing all the modules that have ever been imported since Python was started. The key is the module name, the value is the module object
d) Given the name(as a string) of any previously-imported module, you can get a reference to the module itself through the sys.modules dictionary
e) Every Python class has a built-in class attribute __module__, which is the name of the module in which the class is defined.
5 Working with Directories
a) os.path is a reference to a module -- which module depends on your platform
b) expanduser will expand a pathname that uses ~ to represent the current user's home directory
c) The split function splits a full pathname and returns a tuple containing the path and filename
d) splitext splits a filename and returns a tuple containing the filename and the file extension
e) The listdir function takes a pathname and returns a list of the contents of the directory
f) os.path.isfile takes a pathname and returns 1 if the path represents a file, and 0 otherwise.
g) use os.path.join can ensure a full pathname
h) isdir function returns 1 if the path represents a directory, and 0 otherwise.
i) whenever possible, you should use the functions in os and os.path for file, directory, and path manipulations. These modules are wrappers for platform-specific modules
j) The glob module takes a wildcard and returns the full path of all files and directories matching the wildcard.
k) nested functions can be called only from the function in which it is defined.
June 19 看了5章dive into python最深刻的感想Python is a damn well flexible, simple and efficient language!
据说youtube是用python写的,有没有人能证实一下 Python study notes 41 Importing modules using from module import
a) syntax: from object import object, the attribute and methods of the imported module types are imported directly into the local namespace.
b) from module import * in Python like import module.* in java
c) If you access attributes and methods from a module very often, or if you want to selectively import some attributes or methods but not others, use from module import
2 Defining classes
a) Python is fully object-oriented: you can define your own classes, inherit from your own or built-in classes, and instantiate the classes you've defined.
b) syntax: class Name:
c) pass is a Python reserved word that just means "moving along, nothing to see here". The pass statement in Python is like an empty set of braces({}) in Java or C
d) In python, the ancestor of a class is simply listed in parenthese immediately after the class name. syntax: class FileInfo(UserDict):
e) Python supports multiple inheritance. In the parentheses following the class name, you can list as many ancestor classes as you like, separated by commas.
f) __init__ is called immediately after an instance of the class is created. It would be tempting but incorrect to call this the constructor of the class.
g) The first argument of every class method, including __init__, is always a reference to the current instance of the class. By convention, this argument is always named self. In the __init__ method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called. You do not specfic this argument when calling the methodl; Python will add it for you automatically.
h) you must explicitly call ancestor's __init__ method if needed.
i) __init__ method never returns a value.
j) __init__ method are optional, but when you define one, you must remember to explicitly call the ancestor's __init__ mehtod(if it defines one)
3 Instantiating classes
a) Instantiating classes in Python is straightforward. To instantiate a class, simply call the class as if it were a function, passing the arguments that the __init__ method defines. The returned value will be the newly created object.
b) In general, there's no need to explicitly free instances, because they are freed automatically when the variables assigned to them go out of scope.
c) Python keeps a list of references to every instance created.
d) Python supports data attributes. Data attributes are pieces of data held by a specific instance of a class
e) Python has no form of function overloading
f) Python supports data attributes, they are pieces of data help by a specific instance of a class
4 Special class methods
a) __setitem__, __getitem__ Called to implement assignment to
self[key]. Same note as for __getitem__b) When accessing data attributes within a class, you need to qualify the attribute name: self.attribute.
c) When calling other methods within a class, you need to qualify the method name: self.method
d) When a subclass has no __init__ method of its own, Python walks up the ancestor and finds the __init__ method
e) __repr__ is a special method that is called when you call repr(instance). The repr function is a built-in function that returns a string representation of an object.
f) __cmp__ is called when you compare object, Python will call your __cmp__ when you use == to compare objects
g) __len__ is called when you call len(instance). The len function is a built-in function that returns the length of an object
h) __delitem__ is called when you call del instance[key]
i) To compare the physical memory location, use object1 is object2, to compare value, use ==
j) The __call__ method lets a class act like a function, allowing you to call a class instance directly.
5 Class attributes
a) class attributes are available both through direct reference to the class and through any instance of the class
b) In python, only class attributes can be defined immediately after the class definition, data attributes are defined in the __init__ method
c) There are no constants in Python, everything can be changed if you try hard enough
6 Private functions
a) If the name of a Python function, class method, or attribute starts with (but doesn't end with) two underscores, it's private. Everything else is public
b) Python has no concept of protected class methods.
c) In Python, all special methods (like __setitem__) and built-in attribute (like __doc__) follow a standard naming convention: they both start with and end with two underscores.
June 18 Python study notes 31 Using optional and named arguments
a) Python allow function arguments to have default values, if the function is called without the arguemnt, the argument gets its default value
b) Pattern: def info(object, spacing=10, collapse=1):, object is required, spaceing and collapse are optional
c) In Python, arguments can be specified by name, in any order
2 Using type, str, dir and other built-in fuctions
a) The type function returns the datatype of any arbitrary object.
b) type takes anything: integers, strings, modules, functions....
c) The str function coerces data into a string. Every datatype can be coerced into a string
d) dir returns a list of the attributes and methods of any object: modules, functions, strings, lists, dictionaries...
e) the callable function takes any object and returns True if the object can be called, or False otherwise. Callable objects include functions, class methods, even classes.
f) type, str, dir and all the rest of Python's built-in functions are grouped into a special module called __builtin__, python will automatically import it.
3 Getting object references with getattr
a) you can get a reference to a function without knowing its name until run-time, by using the getattr function
b) Pattern: getattr(object,"name")
c) getattr isn't just for built-in datatypes. It also works on modules.
d) A common usage pattern of getattr is as a dispatcher
e) getattr has a third argument, it is a default value that is returned if the attribute or method specified by the second argument wasn't found
4 Filtering Lists
a) Mapping lists is combined with a filtering mechanism, where some elements in the list are mapped while others are skipped entirely
b) pattern: [mapping-expression for element in source-list if filter-expression]
c) count is a list method that returns the number of times a value occurs in a list
5 The peculiar nature of and and or
a) and and or perform boolean logic as you would expect, but they do not return boolean values; instead, they return one of the actual values they are comparing.
b) When using and, values are evaluated in a boolean context from left to right. 0, ' ', [], {}, and None are false in a boolean context, everything else is true
c) If all values are true in a boolean context, and returns the last value
d) If any value is false in a boolean context, and returns the first false value
e) If any value is true, or returns that value immediately.
f) If all values are false, or returns the last value.
g) or evaluates values only until it finds one that is true in a boolean context, and then it ignores the rest.
6 Using lambda function
a) Python supports defining one-line mini functions on the fly.
b) Pattern: lambda x: x*2 no parentheses around the parameter and no return keyword
c) split without any arguments split on whitespace.
d) ljust(n) pads the string with spaces to the given length. If the given length is smaller than the length of string, ljust will simply return the string unchanged.
June 17 Python study notes 21 Dictionaries
a) One of Python's built-in datatype is the dictionary, which defines one-to-one relationships between keys and values.
b) A dictionary in Python is like an instance of the Hashtable class in Java
c) pattern: d={"key1":"value1","key2":"value2"} search pattern: d["key1"]
d) Assigning a value to an existing key will wipe out the old value
e) You can add new key-value pairs at any time. This syntax is identical to modifying existing values.
f) Dictionaries have no concept of order among elements and keys are case-sensitive
g) Dictionary values can be any datatype, the values don't all need to be the same type; Dictionary keys are more restricted, but they can be strings, integers and a few other types.
h) del lets you delete individual items from a dictionary by key, clear deletes all items from a dictionary.
2 Lists
a) A list in Python is like an instance of the ArrayList class in Jave
b) pattern: l=["value1","value2","value3"] search pattern: l[0]
c) A negative index accesses elements from the end of the list counting backwards. The last element of any non-empty list is always l[-1]
d) You can get a subset of a list, called a "slice", by specifying two indices. Starting with the first slice index, up to but not including the second slice index.
e) l[:n] will always return the first n elements, and l[n:] will return the rest, regardless of the length of the list
f) append adds a single element to the end of the list, it takes one argument, which can be any data type, and simply adds it to the end of the list
g) insert inserts a single element into a list.
h) extend concatenates lists, extend always takes a single argument, which is always a list.
i) index finds the first occurrence of a value in the list and returns the index, if the value is not found in the list, Python raises an exception.
j) To test whether a value is in the list, use in, which returns True if the value is found or False if it is not.
k) remove removes the first and only the first occurrence of a value from a list. If the value is not found, Python raises an exception.
l) pop removes the last element of the list and it returns the value that it removed.
m) len() returns the length of a list
n) list=list+otherlist has the same result as list.extend(otherlist), extend is faster
o) Python supports the += operator, and the * operator works on lists as a repeater.
3 Tuples
a) A tuple is defined in the same way as a list, except the the whole set of elements is enclosed in parentheses instead of square brackets, pattern: t=("a","b")
b) search pattern: t[0], slicing works too, just like a list.
c) you can't add/remove/find elements in a tuple. you can, however, use in to see if an elements exists in the tuple
d) Tuples are faster than lists. If you are defining a constant set of values and all you're ever going to do with it is iterate through it, use tuple
e) Tuples can be used as keys in a dictionary.
f) Tuples can be converted into lists using list(tuple), lists can be converted into tuples using tuple(list)
4 Declaring variables
a) When a command is split among sevaral lines with the line-continuation marker('\'), the continued line can be indented in any manner.
b) You never declare a variable, you just assign a value to it
c) The built-in range function returns a list of integers. It takes an upper limit and returns a zero-based list counting up to but not including the upper limit
5 Formatting Strings
a) Pattern: "%s=%s" % (a,b)
b) A comma after the last element is required when defining a tuple with one element. (a,)
c) Trying to concatenate a string with a non-string raises an exception
d) String formatting works with integers by specifying %d instead of %s
e) %f string formatting option treats the value as a decimal, and prints it to six decimal places. Some modifiers can be used to format the number
6 Mapping Lists
a) List comprehension provides a compact way of mapping a list into another list by applying a function to each of the element in the list
b) Pattern: [elem*2 for elem in li]
c) List comprehension do not change the original list, and it is safe to assign the result of a list comprehension to the variable you are mapping
d) items() function returns a list of tuples of all the data in the dictionary
7 Joining Lists and Splitting Strings
a) The join method joins the elements of the list into a single string, with each element separated by any string.
b) split reverses join by splitting a string into a multi-element list. The delimiter will be stripped out completely.
c) split takes an optional second argument , which is the number of times to split. anystring.split(delimiter,1) is very useful
June 16 Python study notes 11 Declaring functions:
a) Python has functions like most other languages, but it does not have separate header files like c++
b) It does not have interface/implementation mode like Pascal. When you need a function, just declare it like:
def function(param):
c) Functions doesn't not define a return datatype. It even don't specify whether or not return a value
2 Python's datatypes:
Python is a dynamically typed and strongly typed language.
3 Documenting functions:
Triple quotes signify a multi-line string:
def function(param):
"""doc string"""
4 Everything is an object
a) A function, like everything else in Python, is an object.
b) "import test" imports the test program as a module. When you want to use functions defined in imported module, you need to include the module name: test.func()
c) sys module is a built-in module written in C.
d) sys.path will list the search path it is looking for. You can add a new directory to Python's search path at runtime by appending the directory name to sys.path
e) Everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function.
5 Indenting Code
a) Python function have no explicit begin or end, and no curly braces to mark where the function code starts and stops.
b) The only delimiter is a colon(:) and the indention of the code itself.
c) White spaces is significant and must be consistent.
d) Indenting starts a block and unindenting ends it.
e) Python uses carriage return to separate statements and a colon and indentation to separate code blocks
6 Testing modules
a) parentheses are not required around the if expression, if statement ends with a colon and is followed by indented code.
b) all modules have a built-in attribute "__name__". If you import the module, then "__name__" is the module's file name. If you run the module directly as a standalone program, the "__name__" will be "__main__". You can use this trick to develop and debug new modules before intergrating them into a larger program.
|
|
|