import urllib
import re
def get_quote(symbol):
base_url = 'http://finance.google.com/finance?q='
content = urllib.urlopen(base_url + symbol).read()
m = re.search('class="pr".*?>(.*?)<', content)
if m:
quote = m.group(1)
print symbol + " " + quote
else:
quote = 'no quote available for: ' + symbol
return quote
items = ["AAPL","GOOG","ININ","EWH","IAU"]
for n in items:
get_quote(n)
raw_input( )
Category Archives: Python
Python File Renamer
import os
illegal = list('''!"#$%&\'()*+,/:;<=>?@[\\]^_`{|}~''')
repl = " "
for root,dir,files in os.walk(os.getcwd()):
for fi in files:
for ill in illegal:
if ill in fi:
print "found ", os.path.join(root,fi)
newname = fi.replace(ill,repl)
os.rename( os.path.join(root,fi) , os.path.join(root,newname) )
Python text to dictionary
#Takes a text file of items and numbers on each line and returns a dictionary of them # list.txt is a file that contains the following lines: Apples 34 Bananas 10 Oranges 56 mystring = file.read() mylist = mystring.split('\n') mydict = {} mydict[l[0]] = l[1] #mydict yields {'Apples': '34', 'Oranges': '56', 'Bananas': '10'}
Python Cellular automata
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | # CellularAutomata.py: Wolfram-style cellular automata in Python # Options: # -h # Use a screen of height # for the simulation # -w # Use a screen of width # for the simulation # -r Use a random initial row (rather than the standard single 1 in the middle) # -R # Use rule # for the simulation """ import getopt,sys from random import randint def ca_data(height,width,dorandom,rulenum): # Create the first row, either randomly, or with a single 1 if dorandom: first_row = [randint(0,1) for i in range(width)] else: first_row = [0]*width first_row[width/2] = 1 results = [first_row] # Convert the rule number to a list of outcomes. rule = [(rulenum/pow(2,i)) % 2 for i in range(8)] for i in range(height-1): data = results[-1] # Determine the new row based on the old data. We first make an # integer out of the value of the old row and its two neighbors # and then use that value to get the outcome from the table we # computed earlier new = [rule[4*data[(j-1)%width]+2*data[j]+data[(j+1)%width]] for j in range(width)] results.append(new) return results def pil_render(data,height,width,fname="bs.png"): import Image, ImageDraw img = Image.new("RGB",(width,height),(255,255,255)) draw = ImageDraw.Draw(img) for y in range(height): for x in range(width): if data[y][x]: draw.point((x,y),(0,0,0)) img.save(fname,"PNG") return def main(): opts,args = getopt.getopt(sys.argv[1:],‘h:w:rR:’) height = 500 width = 500 dorandom = 0 rule = 22 for key,val in opts: if key == ‘-h’: height = int(val) if key == ‘-w’: width = int(val) if key == ‘-r’: dorandom = 1 if key == ‘-R’: rule = int(val) data = ca_data(height,width,dorandom,rule) pil_render(data,height,width) return if __name__ == ‘__main__’: main() |
Python Histogram
1 2 3 4 5 6 7 8 9 10 11 12 | A=[7,8,9,5,3,2,1,3,4,5,6,7,8,9,9,9,9,0,0,0,1,2,3] def histogram( A, flAsList=False ): #Return histogram of values in array A.""" H = {} for val in A: H[val] = H.get(val,0) + 1 if flAsList: return H.items() return H # Then the call is self-documenting: freq = histogram(A) print freq |
Jumble.py
Reply
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #!/bin/env python def find_jumble(jumble, word_file='/users/charleslsnyder/dictionary.txt'): #you have to reset the word file to your dictionary and path sorted_jumble = sort_chars(jumble) for dictword in open(word_file, 'r').xreadlines(): if sorted_jumble == sort_chars(dictword): yield dictword def sort_chars(word): w = list(word.strip().lower()) w.sort() return w while(1): inp = raw_input("Enter word: ") if not inp: break for ans in find_jumble(inp): print "Answer = ", ans |