Skip to content

Instantly share code, notes, and snippets.

View lambdaman2's full-sized avatar

Lambdaman lambdaman2

View GitHub Profile
@lambdaman2
lambdaman2 / Snipplr-50835.py
Created October 1, 2012 12:48
Django: Strip/Remove HTML tags (django utils)
# 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)
@lambdaman2
lambdaman2 / Snipplr-53250.py
Created October 1, 2012 12:48
Python: Ways to Move up and Down the dir structure in Python
#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
@lambdaman2
lambdaman2 / Snipplr-36847.py
Created October 1, 2012 12:47
Python: Open URL - Python
# 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()
@lambdaman2
lambdaman2 / Snipplr-25248.py
Created October 1, 2012 12:47
Python: Python: SYS module
# 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',
@lambdaman2
lambdaman2 / Snipplr-25250.py
Created October 1, 2012 12:47
Python: Python: error handling
## 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")
@lambdaman2
lambdaman2 / Snipplr-27238.py
Created October 1, 2012 12:48
Python: Wait function - python
from time import sleep
sleep(5) #sleeps for 5 seconds
@lambdaman2
lambdaman2 / Snipplr-25240.py
Created October 1, 2012 12:47
Python: Python: raising exceptions
if not resulttype_dict: raise Exception, "This is my customised error message."
@lambdaman2
lambdaman2 / Snipplr-25233.py
Created October 1, 2012 12:47
Python: Python: monkeyPatch methods
"""
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::
@lambdaman2
lambdaman2 / Snipplr-25246.py
Created October 1, 2012 12:47
Python: Python: TIME module
## 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)
@lambdaman2
lambdaman2 / Snipplr-25262.py
Created October 1, 2012 12:47
Python: Python: formatting strings
>>>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