Вы находитесь на странице: 1из 9

What is __init__.py?

It is used to import a module in a directory, which is called package import.


If we have a module, dir1/dir2/mod.py, we put __init__.py in each directories so
that we can import the mod like this:
import dir1.dir2.mod

The __init__.py is usually an empty py file. The hierarchy gives us a convenient


way of organizing the files in a large system.

Build a string with the numbers from 0


to 100, "0123456789101112..."
We may want to use str.join rather than appending a number every time.
>>> ''.join([`x` for x in xrange(101)])
'012345678910111213141516171819202122232425262728293031323334353637
3839404142434445464748495051525354555657585960616263646566676869707
172737475767778798081828384858687888990919293949596979899100'
>>>

Note that the (`) is a backquote not a regiular single quote ('):
>>> type(1)
<type 'int'>
>>> type(`1`)
<type 'str'>

Note that we cannot use double quote(") or single quote(') to make n a string:
>>> type("1")
<type 'str'>

>>> ''.join(["n" for n in range(10)])


'nnnnnnnnnn'
>>> ''.join(['n' for n in range(10)]) #
'nnnnnnnnnn'
>>> n = 1
>>> print `n`
1
>>> print "n"
n

Note: join() returns a string in which the string elements of sequence have
been joined by string separator.
We can use str(x) instead:
>>> ''.join([str(x) for x in range(10)])
'0123456789'

Also, since the xrange() is replaced with range in Python 3.x, we should
use range()instead for compatibility. The range() in Python 3.x just returns
iterator. That means it does not produce the results in memory any more, and if
we want to get list fromrange(), we need to force it to do so: list(range(...)).

Basic file processing: Printing contents


of a file.
try:
with open('filename','r') as f:
print f.read()
except IOError:
print "No such file exists"

How can we get home directory using


'~' in Python?
We need to import os module, and add just one line:
import os
print os.path.expanduser('~')

Output:
/home/k

The usage of os.path.dirname() &


os.path.basename()?
For example, we have the path like this, /home/k/TEST/PYTHON/p.py:
We can get the dir and file using the following:
First, we need to import os module:
>>> import os

Then, we do:
>>> os.path.dirname('/home/k/TEST/PYTHON/p.py')
'/home/k/TEST/PYTHON'
>>> os.path.basename('/home/k/TEST/PYTHON/p.py')
'p.py'

Or we can get them at once in tuple using os.path.split():

>>> os.path.split('/home/k/TEST/PYTHON/p.py')
('/home/k/TEST/PYTHON', 'p.py')

If we want to combine and make a full path:


>>> os.path.join('/home/k/TEST/PYTHON', 'p.py')
'/home/k/TEST/PYTHON/p.py'

Default Libraries
We should be able to answer the questions about the standard library.
Such as "Do you know if there's a standard library for recursive file renaming?",
or "In which library would you use for regular expression?"
os: operating system support
os.path: Common pathname manipulations:
>>> import os
>>> print(os.getcwd())
C:\Python32
>>> cur_dir = os.curdir
>>> print(cur_dir)
.
>>> scripts_dir = os.path.join(os.curdir, 'Tools\Scripts')
>>> print(scripts_dir)
.\Tools\Scripts
>>> diff_py = os.path.join(scripts_dir, 'diff.py')
>>> print(diff_py)
.\Tools\Scripts\diff.py
>>> os.path.basename(diff_py)
' diff.py'
>>> os.path.splitext(diff_py)
('.\\Tools\\Scripts\\diff', '.py')

The os.path.join() function constructs a pathname out of one or more


partial pathnames. In this case, it simply concatenates strings.
Other convenient ones are: dirname() and basename(), which are the 1st
and 2nd element of split(), respectively:
>>> import os
>>> print(os.getcwd())
C:\TEST\dirA\dirB\dirC
>>> print(os.path.dirname(os.getcwd()))
C:\TEST\dirA\dirB
>>> print(os.path.basename(os.getcwd()))
dirC
>>> print(os.path.split(os.getcwd()))
('C:\\TEST\\dirA\\dirB', 'dirC')
>>> pathname = os.path.join(os.getcwd(),'myfile.py')
>>> pathname
'C:\\TEST\\dirA\\dirB\\dirC\\myfile.py'
>>> (dirname, filename) = os.path.split(pathname)
>>> dirname
'C:\\TEST\\dirA\\dirB\\dirC'
>>> filename
'myfile.py'

The split function splits a full pathname and returns a tuple containing the
path and filename. We could use multi-variable assignment to return
multiple values from a function. The os.path.split() function does exactly
that. We assign the return value of the split function into a tuple of two
variables. Each variable receives the value of the corresponding element
of the returned tuple.
The first variable, dirname, receives the value of the first element of the
tuple returned from the os.path.split() function, the file path. The second

variable,filename, receives the value of the second element of the tuple


returned from theos.path.split() function, the filename.
os.path also contains the os.path.splitext() function, which splits a
filename and returns a tuple containing the filename and the file extension.
The os.path.expanduser() function :
>>> print(os.path.expanduser('~'))
C:\Users\KHong

will expand a pathname that uses ~ to represent the current user's home
directory. This works on any platform where users have a home directory,
including Linux, Mac OS X, and Windows. The returned path does not
have a trailing slash, but the os.path.join() function doesn't mind:
re:
Regular
expression
Visit Regular Expressions with Python

operations

itertools:
Functions
creating
iterators
for
efficient
looping.
It includes permutations, combinations and other useful iterables.

>>> [x for x in itertools.permutations('123')]

[('1', '2', '3'), ('1', '3', '2'), ('2', '1', '3'), ('2', '3',
'1'), ('3', '1', '2'), ('3', '2', '1')]

>>> [x for x in itertools.permutations('123',2)]

[('1', '2'), ('1', '3'), ('2', '1'), ('2', '3'), ('3', '1'),
('3', '2')]

>>>

range vs xrange

>>> sum(range(1,101))
5050
>>> sum(xrange(1,101))
5050
>>>

range() returns a list to the sum function containing all the numbers from 1 to
100. Butxrange() returns an iterator rather than a list, which makes it more
lighter in terms of memory use as shown below.
>>> range(1,5)
[1, 2, 3, 4]
>>> xrange(1,5)
xrange(1, 5)
>>>

Note: Since the xrange() is replaced with range in Python 3.x, we should
use range()instead for compatibility. The range() in Python 3.x just returns
iterator. That means it does not produce the results in memory any more, and if
we want to get list fromrange(), we need to force it to do so: list(range(...)).

Iterators
Python defines several iterator objects to support iteration over general and
specific sequence types, dictionaries.
Any object with a __next__() method to advance to a next result is considered
iterable.
For more info, please visit
http://www.bogotobogo.com/python/python_iterators.php.

Generators
Generators allow us to declare a function that behaves like an iterator, i.e. it can
be used in a for loop. It's a function type generator, but there is another type of
generator that may be more familiar to us - expression type generator used in list
comprehension:
>>> # List comprehension makes a list
>>> [ x ** 3 for x in range(5)]
[0, 1, 8, 27, 64]
>>>
>>> # Generator expression makes an iterable
>>> (x ** 3 for x in range(5))
<generator object <genexpr> at 0x000000000315F678>

With the generator expression, to make list comprehension, we can just wrap it
withlist() call:
>>> list(x ** 3 for x in range(5))
[0, 1, 8, 27, 64]

For more information about generators, please visit:


http://www.bogotobogo.com/python/python_generators.php

docstrings vs comments
A docstring is the documentation string for a function. We use it as shown
below:
function_name.__doc__

We can declare it like this:

def my_function():
"""our docstring"""

or:
def my_function():
'''our docstring'''

Everything between the triple quotes (with double quotes, """ or with single
quotes,''') is the function's docstring, which documents what the function does. A
docstring, if it exists, must be the first thing defined in a function. In other words,
it should appear on the next line after the function declaration. We don't
technically need to give our function a docstring, but we always should. The
docstring will be available at runtime as an attribute of the function.
Writing documentation for our program this way makes the code more readable.
We can also use comments for clarification of what the code is doing. In general,
docstrings are for documentation, comments are for a code reader.

Вам также может понравиться