Professional Documents
Culture Documents
txt
#
# Example code snippets for Up and Running with Python
# NOTE: THESE SNIPPETS ARE FOR PYTHON 2.7. IF YOU ARE USING PYTHON 3, USE THE Ex
ampleSnippets3.txt FILE
---------------------------------------------------------*********************
CHAPTER 2:
*********************
--- HelloWorld --def main():
print "hello world!"
if __name__ == "__main__":
main()
--- Variables --# Declare a variable and initialize it
f = 0;
print f
# re-declaring the variable works
f = "abc"
print f
# ERROR: variables of different types cannot be combined
#print "string type " + 123
print "string type " + str(123)
# Global vs. local variables in functions
def someFunction():
#global f
f = "def"
print f
someFunction()
print f
del f
print f
--- Functions --# define a function
def func1():
print "I am a function"
# function that takes arguments
def func2(arg1, arg2):
print arg1, " ", arg2
# function that returns a value
def cube(x):
return x*x*x
a while loop
< 5):
x
+ 1
meetday = weektwo[calendar.FRIDAY]
print "%10s %2d" % (calendar.month_name[m], meetday)
*********************
Chapter 4:
*********************
--- Files --# Open a file for writing and create it if it doesn't exist
f = open("textfile.txt","w+")
# Open the file for appending text to the end
# f = open("textfile.txt","a+")
# write some lines of data to the file
for i in range(10):
f.write("This is line %d\r\n" % (i+1))
# close the file when done
f.close()
# Open the file back up and read the contents
f = open("textfile.txt","r")
if f.mode == 'r': # check to make sure that the file was opened
# use the read() function to read the entire file
# contents = f.read()
fl = f.readlines() # readlines reads the individual lines into a list
for x in fl:
print x
--- Path utilities --import os
from os import path
import datetime
from datetime import date, time, timedelta
import time
# Print the name of the OS
print os.name;
# Check for
print "Item
print "Item
print "Item
*********************
Chapter 5:
*********************
--- Internet Data --import urllib2
def main():
# open a connection to a URL using urllib2
webUrl = urllib2.urlopen("http://joemarini.com")
# get the result code and print it
metacount = 0;
# create a subclass and override the handler methods
class MyHTMLParser(HTMLParser):
# function to handle an opening tag in the doc
# this will be called when the closing ">" of the tag is reached
def handle_starttag(self, tag, attrs):
global metacount
print "Encountered a start tag:", tag
if tag == "meta":
metacount += 1
pos = self.getpos() # returns a tuple indication line and character
print "At line: ", pos[0], " position ", pos[1]
if attrs.__len__ > 0:
print "\tAttributes:"
for a in attrs:
print "\t", a[0],"=",a[1]
# function to handle the ending tag
def handle_endtag(self, tag):
print "Encountered an end tag:", tag
pos = self.getpos()
print "At line: ", pos[0], " position ", pos[1]
# function to handle character and text data (tag contents)
def handle_data(self, data):
print "Encountered some data:", data
pos = self.getpos()
print "At line: ", pos[0], " position ", pos[1]
# function to handle the processing of HTML comments
def handle_comment(self, data):
print "Encountered comment:", data
pos = self.getpos()
print "At line: ", pos[0], " position ", pos[1]
skills = doc.getElementsByTagName("skill")
print "%d skills:" % skills.length
for skill in skills:
print skill.getAttribute("name")
# create a new XML tag and add it into the document
newSkill = doc.createElement("skill")
newSkill.setAttribute("name", "jQuery")
doc.firstChild.appendChild(newSkill)
skills = doc.getElementsByTagName("skill")
print "%d skills:" % skills.length
for skill in skills:
print skill.getAttribute("name")