July 14th, 2007
Here’s my generalized markov module. Note that as it exists, it only does single word chains and expects a “#” character as paragraph delimiter. Feel free to tweak it. I’ll post some examples shortly.
I pasted the code here for convenience:
http://scrying.org/doku.php?id=workshop:generative
Posted in Uncategorized | No Comments »
July 14th, 2007
Martin has set up a wiki space for us to put some interesting source texts. DocuWiki supports file uploads so feel free to post some longer texts.
http://scrying.org/doku.php?id=workshop:generative
Posted in Uncategorized | No Comments »
July 14th, 2007
Modules are a very convenient way to organize reusable bits of code. Modules are imported using the “import” directive and are loaded into their own namespace. We can import the functions that we just defined in the last example and use them. It’s also possible to import some or all functions from a module into the current namespace. Here, we reuse the functions that we created in the previous example. Make sure the file containing the functions is saved as “functions.py”, otherwise python will not find the module. Also, note that any code in the main block of a module is executed when the module is imported.
[code lang=”python”]
#modules
import functions
a = [”boutros”, “boutros”, “gahli”]
print functions.selRand(a)
from functions import crit
b = [”armalite”, “kalashnikov”, “colt”, “walther”, “heckler and koch”, “steyr”]
c = [”shy”, “capricous”, “hyperactive”, “charming”, “precocious”, “sweet”]
crit(a,b,c)
[/code]
Posted in Uncategorized | No Comments »
July 13th, 2007
Functions are defined using the def statement. Functions can return multiple values in the form of a tuple. Another cool feature in python is the ability to set default values for parameters.
[code lang=”python”]
#critical paranoia
import random
def selRand(a):
“select a random element from a list”
index = random.randrange(0, len(a))
return a[index]
def crit(pos, cheeses, animals):
“prints a sentence”
sentence = “My ” + selRand(pos)
sentence += ” is like ” + selRand(cheeses)
sentence += “; it is ” + selRand(animals)
print sentence + “.”
return
a = [”ipod”, “laptop”, “shirt”, “cigarette lighter”, “keychain”, “coffee mug”]
b = [”camenbert”, “limburger”, “emmenthaler”, “manchego”, “reggiano”, \
“monterrey jack”]
c = [”known to travel in packs”, “able to run 70mph”, \
“found in the serengheti”, “striped”, “nocturnal”, \
“very aggressive when provoked” ]
crit(a, b, c)
[/code]
Posted in Uncategorized | No Comments »
July 13th, 2007
Dictionaries are like associative arrays or hash tables. They represent a set of data indexed by keys. Keys are typically strings but any immutable object (like a tuple or a number). Dictionaries are created by using curly braces and separating key/value pairs with a colon. Dictionary members are accessed the same way members of a list are accessed. Use del to delete a member from a dictionary.
[code lang=”python”]
#dictionaries
a = { “awesome”: “geil”,
“nerd”: “freak”,
“makeout”: “knutchen” }
print a.keys()
word = “awesome”
if a.has_key(word):
print word + ” means ” + a[word] + ” in German”
else:
print “I can’t translate ” + word
del a[”nerd”]
print a
[/code]
Posted in Uncategorized | No Comments »
July 13th, 2007
File input and output are done using file objects. open() returns a new file object. readline() reads one line at a time from the file including the terminating newline character. write() writes raw data to the file. You can also use the print statement to redirect output to a file instead of the terminal. Don’t forget to close() your files when you’re done with them.
[code lang=”python”]
#file i/o
speech = open(”lincoln.txt”)
outfile = open(”output.txt”, “w”)
line = speech.readline()
while line:
print line
line = speech.readline()
speech.close()
out = “done”
outfile.write(out)
outfile.close()
[/code]
Posted in Uncategorized | No Comments »
July 13th, 2007
Python has just two kinds of loops: while and for loops. While loops check for a logical condition and continue running the loop as long as the condition is true. For loops iterate through any arbitrary sequence, usually a list or a tuple. You’ll need to press control-C to kill this app as there is an infinite (jest) loop at the end.
[code lang=”python”]
#loops
import time
a = 0
while a < 5:
print “decoys”
a += 1
b = [ 99, “salmonella”, 3.14 ]
for item in b:
print item
for i in range(2, 8):
print i
for j in range(4):
print j
for k in range(0, 9, 3):
print k
while True:
print “some day, you will kill me”
time.sleep(1)
[/code]
Posted in Uncategorized | No Comments »
July 13th, 2007
Lists and tuples are two types of python sequences. They can be sequences of arbitrary objects and can be heterogeneous. The primary difference between tuples and lists is that lists are mutable and tuples are immutable.
[code lang=”python”]
#lists and tuples
a = [ “John”, “Foster”, “Dulles” ]
print a
a[0] = “David”
a[2] = “Wallace”
print a
b = ( “MK”, “Ultra” )
a = a + list(b)
print a
c = [ [”John”, “David”], “Foster”, [”Wallace”, “Dulles”] ]
print c[0][0] + c[1] + c[2][0]
[/code]
Posted in Uncategorized | No Comments »
July 13th, 2007
Strings can be created using single quotes or double quotes. If you use three quote symbols in a row, you can make a multi-line quote. Do note that other than the hard returns, additional whitespace is ignored in multi-line quotes.
Strings can be concatenated using the “+” operator.
Python strings are actually just sequences of characters and can be accessed as such using individual character indexes, like “a[0]” or slice notation to grab a sub-string, like a[0:3].
[code lang=”python”]
#strings
a = “William”
b = ‘Burroughs’
c = “Shatner”
d = “”" of Orange
is not
a very popular
guy
in Ireland.”"”
print a
print b
print a
print c
print a + ” ” + b
print a + ” ” + c[0] + “.”
print a + ” ” + b[3] + b[0:2] + b[4:]
print a + d
[/code]
Posted in Uncategorized | No Comments »
July 13th, 2007
Use “if” and “else” and “elif” statements to peform logic tests.
Important to note here is the use of whitespace to indicate conditional blocks. Part of what makes python code so easy to read is that indenting is used instead of curly brackets. Be careful that you are indenting the same amount for each line in a block. While most editing apps will take care of it for you, be careful not to mix tabs and spaces.
Also, note the use of “elif” statements for multiple-case tests.
[code lang=”python”]
#conditionals
a = 43
b = 16
c = 27
if a > b:
print “a is greater than b”
else:
print “a is not greater than b”
if c >= b and c <= a:
print “c is between a and b”
else:
print “c is not between a and b”
if a < 10:
print “a is less than 10″
elif a <= 20:
print “a is between 10 and 20″
elif a <= 30:
print “a is between 20 and 30″
else:
print “a is greater than 30″
[/code]
Posted in Uncategorized | No Comments »