organization in the src structure, modification of README

This commit is contained in:
Mari Wahl 2015-01-06 18:30:04 -05:00
parent c2ca11f247
commit 6afe96fa4d
165 changed files with 64 additions and 184 deletions

View file

@ -0,0 +1,17 @@
#!/usr/bin/env python
__author__ = "bt3"
def simple2(a, *args):
print args
def simple(*args):
print args
def simple3(**kwargs):
print kwargs
simple(1, 2, 3)
simple2(1, 2, 3)
simple3(x=1, y=2)

View file

@ -0,0 +1,28 @@
#!/usr/bin/env python
__author__ = "bt3"
import random
def benchmark(func):
import time
def wrapper(*args, **kwargs):
t = time.clock()
res = func(*args, **kwargs)
print("\t%s" % func.__name__, time.clock()-t)
return res
return wrapper
@benchmark
def random_tree(n):
temp = [n for n in range(n)]
for i in range(n+1):
temp[random.choice(temp)] = random.choice(temp)
return temp
if __name__ == '__main__':
random_tree(10000)

View file

@ -0,0 +1,37 @@
#!/usr/bin/env python
__author__ = "bt3"
a = [3, 4, 5, 6, 7]
# Filter elements greater than 4
# Bad:
b = []
for i in a:
if i > 4:
b.append(i)
print b
# Good:
print [i for i in a if i > 4]
# Or:
print filter(lambda x: x > 4, a)
# Add three to all list members:
# Bad
b = []
for i in range(len(a)):
b.append(a[i] + 3)
print b
# Good:
print [i + 3 for i in a]
# Or:
print map(lambda i: i + 3, a)

View file

@ -0,0 +1,42 @@
#!/usr/bin/env python
__author__ = "bt3"
def logger(func):
def inner(*args): #1
print "Arguments were: {0}".format(args)
return func(*args)
return inner
@logger
def foo(x, y):
return x+y
print foo(1, 2)
def sum(func):
s = 0
for i in func():
s += i
return s
@sum
def interate():
a = []
for i in range(10):
a.append(i)
return a
print interate
# which is the same as
def interate():
a = []
for i in range(10):
a.append(i)
return a
print sum(interate)

View file

@ -0,0 +1,15 @@
#!/usr/bin/python
__author__ = "bt3"
'''
The doctest module automatically runs any statement beginning with >>>
and compares the following line with the output from the interpreter.
>>> 1 == 1
False
'''
if __name__ == '__main__':
import doctest
doctest.testmod()

View file

@ -0,0 +1,27 @@
#!/usr/bin/env python
__author__ = "bt3"
def interate(x):
for i in range(x):
yield i
def gen1():
a = interate(10)
print a.next()
print a.next()
print a.next()
def reverse(data):
for i in range(len(data)-1, -1, -1):
yield data[i]
def gen2():
for c in reverse('awesome'):
print c
if __name__ == '__main__':
gen1()
gen2()

View file

@ -0,0 +1,7 @@
#!/usr/bin/env python
__author__ = "bt3"
test = lambda x: x**2
print test(3)

View file

@ -0,0 +1,21 @@
#!/usr/bin/env python
__author__ = "bt3"
import logging
LOG_FILENAME = 'logging_example.out'
logging.basicConfig(filename=LOG_FILENAME,
level=logging.DEBUG,
)
logging.debug('This message should go to the log file')
f = open(LOG_FILENAME, 'rt')
try:
body = f.read()
finally:
f.close()
print 'FILE:'
print body

View file

@ -0,0 +1,10 @@
#!/usr/bin/env python
__author__ = "bt3"
filename = raw_input('Enter a file name: ')
try:
f = open(filename, "r")
except:
print 'There is no file named', filename

View file

@ -0,0 +1,58 @@
#!/usr/bin/env python
__author__ = "bt3"
import pickle
def import_pickle(filename):
fh = None
try:
fh = open(filename, "rb")
mydict2 = pickle.load(fh)
return mydict2
except (EnvironmentError) as err:
print ("{0}: import error: {0}".format(os.path.basename(sys.arg[0]), err))
return false
finally:
if fh is not None:
fh.close()
def test_import_pickle():
pkl_file = 'test.dat'
mydict = import_pickle(pkl_file)
print(mydict)
def export_pickle(data, filename='test.dat', compress=False):
fh = None
try:
if compress:
fh = gzip.open(filename, "wb") # write binary
else:
fh = open(filename, "wb") # compact binary pickle format
pickle.dump(data, fh, pickle.HIGHEST_PROTOCOL)
except(EnvironmentError, pickle.PickingError) as err:
print("{0}: export error: {1}".format(os.path.basename(sys.argv[0], err)))
return False
finally:
if fh is not None:
fh.close()
def test_export_pickle():
mydict = {'a': 1, 'b': 2, 'c': 3}
export_pickle(mydict)
if __name__ == '__main__':
test_export_pickle()
test_import_pickle()

View file

@ -0,0 +1,13 @@
#!/usr/bin/env python
__author__ = "bt3"
import Queue
q = Queue.Queue()
for i in range(10):
q.put(i)
for i in range(10):
print q.get(i)

View file

@ -0,0 +1,35 @@
#!/usr/bin/env python
__author__ = "bt3"
import socket
def netcat(hostname, port, content):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((hostname, port))
s.sendall(content)
adata = []
while 1:
data = s.recv(1024)
if data == '':
break
adata.append(data)
s.close()
return adata
if __name__ == '__main__':
PORT = 12345
HOSTNAME = '54.209.5.48'
message = netcat(HOSTNAME, PORT, 'Hello!')[1]
print message

View file

@ -0,0 +1,11 @@
#!/usr/bin/env python
__author__ = "bt3"
foo = 'foo'
bar = 'bar'
print '%s%s' % (foo, bar) # It is OK
print '{0}{1}'.format(foo, bar) # It is better
print '{foo}{bar}'.format(foo=foo, bar=bar) # It is best

View file

@ -0,0 +1,8 @@
#!/usr/bin/env python
__author__ = "bt3"
import subprocess,os
os.system('ls')
subprocess.call(['ls', '-1'], shell=True)

View file

@ -0,0 +1,22 @@
#!/usr/bin/env python
__author__ = "bt3"
from telnetlib import Telnet
# examples of telnet connections
PORT = 12345
HOST = '54.209.5.48'
# creating connection
tn = Telnet(HOST ,PORT)
# reading input
msg_in2 = tn.read_all().dec_msg()
tn.read_until(b'psifer text: ')
# writing outputs
tn.write(msg.encode() + b'\n')

View file

@ -0,0 +1,46 @@
#!/usr/bin/env python
__author__ = "bt3"
def test_doctest():
'''
>>> 1 == 1
False
'''
pass
if __name__ == '__main__':
import doctest
doctest.testmod()
#####
import unittest
class BasicsTestCase(unittest.TestCase):
def test_find_name(self):
self.assertTrue(1 == 1)
self.assertFalse(1 == 2)
if __name__ == '__main__':
unittest.main()
#####
# content of test_example.py, run with $ py.test
#
# run tests over the directory
# $ nosetest
def func(x):
return x + 1
def test_answer():
assert func(3) == 4

View file

@ -0,0 +1,16 @@
#!/usr/bin/env python
__author__ = "bt3"
import threading
def worker(num):
"""thread worker function"""
print 'Worker: %s' % num
return
threads = []
for i in range(5):
t = threading.Thread(target=worker, args=(i,))
threads.append(t)
t.start()

View file

@ -0,0 +1,24 @@
#!/usr/bin/python
__author__ = "bt3"
''' a simple example of how to time a function '''
import time
def sumOfN2(n):
start = time.time()
theSum = 0
for i in range(1,n+1):
theSum = theSum + i
end = time.time()
return theSum,end-start
if __name__ == '__main__':
n = 5
print("Sum is %d and required %10.7f seconds"%sumOfN2(n))
n = 200
print("Sum is %d and required %10.7f seconds"%sumOfN2(n))

View file

@ -0,0 +1,42 @@
#!/usr/bin/python3
__author__ = "bt3"
from functools import wraps
from do_benchmark import benchmark
def memo(func):
''' an example of dynamic programming using a memoizing decorator '''
cache = {}
@wraps(func)
def wrap(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
return wrap
@memo
def find_fibonacci_seq_rec(n):
''' implements the nth fibonacci value in a recursive exponential runtime '''
if n < 2: return n
return find_fibonacci_seq_rec(n - 1) + find_fibonacci_seq_rec(n - 2)
def test_memo():
n = 50
# find_fibonacci_seq_rec = memo(find_fibonacci_seq_rec)
# @benchmark
print(find_fibonacci_seq_rec(n))
if __name__ == '__main__':
test_memo()

View file

@ -0,0 +1,85 @@
#!/usr/bin/python3
__author__ = "bt3"
from itertools import combinations
from bisect import bisect
from memo import memo
from do_benchmark import benchmark
def naive_longest_inc_subseq(seq):
''' naive (exponential) solution to the longest increasing subsequence problem '''
for length in range(len(seq), 0, -1):
for sub in combinations(seq, length):
if list(sub) == sorted(sub):
return len(sub)
def longest_inc_subseq1(seq):
''' an iterative algorithm for the longest increasing subsequence problem '''
end = []
for val in seq:
idx = bisect(end, val)
if idx == len(end): end.append(val)
else: end[idx] = val
return len(end)
def longest_inc_subseq2(seq):
''' another iterative algorithm for the longest increasing subsequence problem '''
L = [1] * len(seq)
for cur, val in enumerate(seq):
for pre in range(cur):
if seq[pre] <= val:
L[cur] = max(L[cur], 1 + L[pre])
return max(L)
def memoized_longest_inc_subseq(seq):
''' a memoized recursive solution to find the longest increasing subsequence problem '''
@memo
def L(cur):
res = 1
for pre in range(cur):
if seq[pre] <= seq[cur]:
res = max(res, 1 + L(pre))
return res
return max(L(i) for i in range(len(seq)))
@benchmark
def test_naive_longest_inc_subseq():
print(naive_longest_inc_subseq(s1))
benchmark
def test_longest_inc_subseq1():
print(longest_inc_subseq1(s1))
@benchmark
def test_longest_inc_subseq2():
print(longest_inc_subseq2(s1))
@benchmark
def test_memoized_longest_inc_subseq():
print(memoized_longest_inc_subseq(s1))
if __name__ == '__main__':
from random import randrange
s1 = [randrange(100) for i in range(25)]
print(s1)
test_naive_longest_inc_subseq()
test_longest_inc_subseq1()
test_longest_inc_subseq2()
test_memoized_longest_inc_subseq()

View file

@ -0,0 +1,62 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
import math
class Point(object):
def __init__(self, x=0, y=0): # self: object reference to the object itself
self.x = x # data attribute
self.y = y
def distance_from_origin(self):
return math.hypot(self.x, self.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __repr__(self):
return "point ({0.x!r}, {0.y!r})".format(self)
def __str__(self): # cannot be passed to eval
return "({0.x!r}, {0.y!r})".format(self)
class Circle(Point):
def __init__(self, radius, x=0, y=0):
super().__init__(x,y) # creates and initializes self.x and self.y
self.radius = radius
def edge_distance_from_origin(self):
return abs(self.distance_from_origin() - self.radius)
def area(self):
return math.pi*(self.radius**2)
def circumference(self):
return 2*math.pi*self.radius
def __eq__(self, other): # let us avoid infinite recursion
return self.radius == other.radius and super().__eq__(other)
def __repr__(self):
return "circle ({0.radius!r}, {0.x!r})".format(self)
def __str__(self):
return repr(self)
if __name__ == '__main__':
a = Point(3,4)
print(a.distance_from_origin())
c = Circle(3,2,1)
print(c)
print(c.circumference())
print(c. edge_distance_from_origin())

View file

View file

@ -0,0 +1,30 @@
#!/usr/bin/env python
__author__ = "bt3"
import os
import sys
import shutil
def change_file_ext():
""" read a file and an extension from the command line and produces a copy with its extension changed"""
if len(sys.argv) < 2:
print("Usage: change_ext.py filename.old_ext 'new_ext'")
sys.exit()
name = os.path.splitext(sys.argv[1])[0] + "." + sys.argv[2]
print (name)
try:
shutil.copyfile(sys.argv[1], name)
except OSError as err:
print (err)
if __name__ == '__main__':
change_file_ext()

View file

@ -0,0 +1,28 @@
#!/usr/bin/python
__author__ = "bt3"
import collections
import string
import sys
def count_unique_word_file():
if len(sys.argv) < 2:
print "Usage: python count_unique_word.py NAMEFILE"
words = collections.defaultdict(int)
strip = string.whitespace + string.punctuation + string.digits + "\"'"
for filename in sys.argv[1:]:
with open(filename) as file:
for line in file:
for word in line.lower().split():
word = word.strip(strip)
if len(word) > 2:
words[word] = +1
for word in sorted(words):
print("'{0}' occurs {1} times.".format(word, words[word]))
if __name__ == '__main__':
count_unique_word_file()

View file

@ -0,0 +1,15 @@
#!/usr/bin/python
__author__ = "bt3"
import collections
import sys
def count_unique_word_freq():
return collections.Counter(\
sys.stdin.read().lower().split()).most_common(n)
if __name__ == '__main__':
count_unique_word_freq()

View file

@ -0,0 +1,27 @@
#!/usr/bin/env python
__author__ = "bt3"
import sys
def grep_word_from_files():
''' using iterator enumerate to create a grep command '''
word = sys.argv[1]
for filename in sys.argv[2:]:
with open(filename) as file:
for lino, line in enumerate(file, start=1):
if word in line:
print("{0}:{1}:{2:.40}".format(filename, lino, line.rstrip()))
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: grep_word_from_files.py word infile1 [infile2...]")
sys.exit()
else:
grep_word_from_files()

View file

@ -0,0 +1,54 @@
#!/usr/bin/env python
__author__ = "bt3"
import os
import sys
def read_data(filename):
lines = []
fh = None
try:
fh = open(filename)
for line in fh:
if line.strip():
lines.append(line)
except (IOError, OSError) as err:
print(err)
finally:
if fh is not None:
fh.close()
return lines
def write_data(lines, filename):
fh = None
try:
fh = open(filename, "w")
for line in lines:
fh.write(line)
except (EnvironmentError) as err:
print(err)
finally:
if fh is not None:
fh.close()
def remove_blank_lines():
""" read a list of filenames on the command line and for each one produces another file with the same content but with no blank lines """
if len(sys.argv) < 2:
print ("Usage: noblank.py infile1 [infile2...]")
for filename in sys.argv[1:]:
lines = read_data(filename)
if lines:
write_data(lines, filename)
if __name__ == '__main__':
remove_blank_lines()