This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To strip/remove HTML tags from an existing string we can use the strip_tags function. | |
# import the strip_tags | |
from django.utils.html import strip_tags | |
# simple string with html inside. | |
html = '<p>paragraph</p>' | |
print html # will produce: <p>paragraph</p> | |
stripped = strip_tags(html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Moving up/down dir structure | |
print os.listdir('.') # current level | |
print os.listdir('..') # one level up | |
print os.listdir('../..') # two levels up | |
# more complex example: | |
# This will walk the file system beginning in the directory the script is run from. It | |
# deletes the empty directories at each level |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# if you are on the internet you can access the HTML code of a | |
# given web site | |
# using the urlopen() method/function from the module urllib2 | |
import urllib2 | |
urlStr = 'http://www.python.org/' | |
try: | |
fileHandle = urllib2.urlopen(urlStr) | |
str1 = fileHandle.read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The sys module provides an interface to access the environment of the Python interpreter. | |
# The following examples illustrate some of the most common uses of the sys module. | |
# The argv attribute of the sys module is a list. The first item in the argv list is the | |
# path to the module; the rest of the list is made up of arguments that were passed to the | |
# module at the beginning of execution. The sample code shows how to use the argv list to | |
# access command-line parameters passed to a Python module: | |
>>>print sys.argv | |
['C:\\books\\python\\CH1\\code\\print_it.py', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Error handling in Python is done through the use of exceptions that are caught in try blocks and handled in except blocks. If an error is encountered, a TRy block code execution is stopped and transferred down to the except block, as shown in the following syntax: | |
try: | |
f = open("test.txt") | |
except IOError: | |
print "Cannot open file." | |
## In addition to using an except block after the try block, you can also use the finally block. The code in the finally block will be executed regardless of whether an exception occurs. | |
f = open("test.txt") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import sleep | |
sleep(5) #sleeps for 5 seconds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if not resulttype_dict: raise Exception, "This is my customised error message." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
These are internal helpers. Do not rely on their presence. | |
http://mail.python.org/pipermail/python-dev/2008-January/076194.html | |
""" | |
def monkeypatch_method(cls): | |
""" | |
A decorator to add a single method to an existing class:: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## The time module provides a portable interface to time functions on the system on which the program is executing. The following examples illustrate some of the most common uses of the time module. | |
##The time.time() function returns the current system time in terms of the number of seconds since the UTC (Coordinated Universal Time). This value is typically collected at various points in the program and is used in delta operations to determine the amount of time since an event occurred. | |
>>>print time.time() | |
1155333864.11 | |
##The time.localtime(secs) function returns the time, specified by secs since the UTC, in the form of tuple (year, month, day, hour, minute, second, day of week, day of year, daylight savings). If no time is specified, the current time is used as follows: | |
>>>print time.localtime() | |
(2006, 8, 11, 16, 4, 24, 4, 223, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>>list = ["Brad", "Dayley", "Python Phrasebook", | |
2006] | |
>>>letter = """ | |
>>>Dear Mr. %s,\n | |
>>>Thank you for your %s book submission. | |
>>>You should be hearing from us in %d.""" | |
>>>display = """ | |
>>>Title: %s |
NewerOlder