Skip to content

Commit

Permalink
autopep8
Browse files Browse the repository at this point in the history
  • Loading branch information
mynameisfiber committed Aug 2, 2014
1 parent ac9c6d2 commit 01cefe9
Show file tree
Hide file tree
Showing 177 changed files with 1,765 additions and 1,026 deletions.
15 changes: 10 additions & 5 deletions 01_profiling/cpu_profiling/julia1.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ def show_greyscale(output_raw, width, height, max_iterations):
def show_false_greyscale(output_raw, width, height, max_iterations):
"""Convert list to array, show using PIL"""
# convert our output to PIL-compatible input
assert width * height == len(output_raw) # sanity check our 1D array and desired 2D form
# sanity check our 1D array and desired 2D form
assert width * height == len(output_raw)
# rescale output_raw to be in the inclusive range [0..255]
max_value = float(max(output_raw))
output_raw_limited = [int(float(o) / max_value * 255) for o in output_raw]
# create a slightly fancy colour map that shows colour changes with
# increased contrast (thanks to John Montgomery)
output_rgb = ((o + (256 * o) + (256 ** 2) * o) * 16 for o in output_raw_limited) # fancier
output_rgb = array.array('I', output_rgb) # array of unsigned ints (size is platform specific)
output_rgb = (
(o + (256 * o) + (256 ** 2) * o) * 16 for o in output_raw_limited) # fancier
# array of unsigned ints (size is platform specific)
output_rgb = array.array('I', output_rgb)
# display with PIL/pillow
im = Image.new("RGB", (width, height))
# EXPLAIN RGBX L 0 -1
Expand Down Expand Up @@ -76,7 +79,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
height = len(y)
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -92,7 +96,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
secs = end_time - start_time
print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980

if draw_output:
#show_false_greyscale(output, width, height, max_iterations)
Expand Down
6 changes: 4 additions & 2 deletions 01_profiling/cpu_profiling/julia1_nopil.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
xcoord += x_step
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -52,7 +53,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
secs = end_time - start_time
print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980


if __name__ == "__main__":
Expand Down
9 changes: 6 additions & 3 deletions 01_profiling/decorator_time/julia1_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def measure_time(*args, **kwargs):
t1 = time.time()
result = fn(*args, **kwargs)
t2 = time.time()
print ("@timefn:" + fn.func_name + " took " + str(t2 - t1) + " seconds")
print (
"@timefn:" + fn.func_name + " took " + str(t2 - t1) + " seconds")
return result
return measure_time

Expand Down Expand Up @@ -49,7 +50,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
xcoord += x_step
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -65,7 +67,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
secs = end_time - start_time
print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980


# Calculate the Julia set using a pure Python solution with
Expand Down
6 changes: 4 additions & 2 deletions 01_profiling/dowser/julia1_dowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
# pre-rounding desired width and height
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -71,7 +72,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
secs = end_time - start_time
print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980

print "now waiting..."
while True:
Expand Down
9 changes: 6 additions & 3 deletions 01_profiling/guppy/julia1_guppy.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
x.append(xcoord)
xcoord += x_step

from guppy import hpy; hp = hpy()
from guppy import hpy
hp = hpy()
print "heapy after creating y and x lists of floats"
h = hp.heap()
print h
Expand All @@ -46,7 +47,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
# pre-rounding desired width and height
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -73,7 +75,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
print h
print

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980


# Calculate the Julia set using a pure Python solution with
Expand Down
6 changes: 4 additions & 2 deletions 01_profiling/line_profiler/julia1_lineprofiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
# pre-rounding desired width and height
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -56,7 +57,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
secs = end_time - start_time
print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980


# Calculate the Julia set using a pure Python solution with
Expand Down
6 changes: 4 additions & 2 deletions 01_profiling/line_profiler/julia1_lineprofiler2.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
# pre-rounding desired width and height
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -61,7 +62,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
secs = end_time - start_time
print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980


# Calculate the Julia set using a pure Python solution with
Expand Down
6 changes: 4 additions & 2 deletions 01_profiling/line_profiler/julia1_lineprofiler3.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
# pre-rounding desired width and height
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -56,7 +57,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
secs = end_time - start_time
print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980


# Calculate the Julia set using a pure Python solution with
Expand Down
6 changes: 4 additions & 2 deletions 01_profiling/line_profiler/julia1_nopil_originalif.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
xcoord += x_step
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -52,7 +53,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
secs = end_time - start_time
print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980


if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions 01_profiling/line_profiler/julia1_nopil_swappedif.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
xcoord += x_step
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -52,7 +53,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
secs = end_time - start_time
print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980


if __name__ == "__main__":
Expand Down
7 changes: 4 additions & 3 deletions 01_profiling/memory_profiler/julia1_memoryprofiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
# pre-rounding desired width and height
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -56,12 +57,12 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
secs = end_time - start_time
print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980


# Calculate the Julia set using a pure Python solution with
# reasonable defaults for a laptop
# set draw_output to True to use PIL to draw an image
calc_pure_python(draw_output=False, desired_width=1000, max_iterations=300)
#calc_pure_python(draw_output=False, desired_width=100, max_iterations=300)

7 changes: 4 additions & 3 deletions 01_profiling/memory_profiler/julia1_memoryprofiler2.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
# pre-rounding desired width and height
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -62,12 +63,12 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
secs = end_time - start_time
print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980


# Calculate the Julia set using a pure Python solution with
# reasonable defaults for a laptop
# set draw_output to True to use PIL to draw an image
calc_pure_python(draw_output=False, desired_width=1000, max_iterations=300)
#calc_pure_python(draw_output=False, desired_width=10, max_iterations=300)

6 changes: 4 additions & 2 deletions 01_profiling/memory_profiler/julia1_memoryprofiler2_xrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
height = len(y)
# build a list of co-ordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed,
# we use it to simulate a real-world scenario with several inputs to our function
# we use it to simulate a real-world scenario with several inputs to our
# function
zs = []
cs = []
for ycoord in y:
Expand All @@ -62,7 +63,8 @@ def calc_pure_python(draw_output, desired_width, max_iterations):
secs = end_time - start_time
print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

assert sum(output) == 33219980 # this sum is expected for 1000^2 grid with 300 iterations
# this sum is expected for 1000^2 grid with 300 iterations
assert sum(output) == 33219980


if __name__ == "__main__":
Expand Down
14 changes: 8 additions & 6 deletions 01_profiling/noop_profile_decorator/ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
# bit of a hack to check
# if 1) __builtin__ exists (for when using nosetests)
# or 2) if 'profile' has been injected by line_profiler
#if '__builtin__' not in dir() or not hasattr(__builtin__, 'profile'):
#def profile(func):
#def inner(*args, **kwargs):
#return func(*args, **kwargs)
#return inner
# if '__builtin__' not in dir() or not hasattr(__builtin__, 'profile'):
# def profile(func):
# def inner(*args, **kwargs):
# return func(*args, **kwargs)
# return inner

# memory profile
if 'profile' not in dir():
Expand All @@ -17,17 +17,19 @@ def inner(*args, **kwargs):
return func(*args, **kwargs)
return inner


@profile
def some_fn(nbr):
return nbr * 2


class TestCase(unittest.TestCase):

def test(self):
result = some_fn(2)
self.assertEquals(result, 4)


if __name__ == "__main__":
print "do some work:", some_fn(2)
#unittest.main()
# unittest.main()
4 changes: 2 additions & 2 deletions 01_profiling/visualise_nonconvergence/nonconvergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
from matplotlib import pyplot as plt
f = plt.figure(1)
plt.clf()
plt.plot(zs1, 'o-', linewidth=4, label="z="+str(z1))
plt.plot(zs2, 'd:', linewidth=4, label="z="+str(z2))
plt.plot(zs1, 'o-', linewidth=4, label="z=" + str(z1))
plt.plot(zs2, 'd:', linewidth=4, label="z=" + str(z2))

plt.annotate("cutoff", (25, 1.9), fontsize="large")
plt.hlines(2, 0, 50, colors='k', linewidth=4, linestyles="dashed")
Expand Down
3 changes: 2 additions & 1 deletion 02_understanding/check_prime.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import math


def check_prime(number):
sqrt_number = math.sqrt(number)
number_float = float(number)
for i in xrange(2, int(sqrt_number)+1):
for i in xrange(2, int(sqrt_number) + 1):
if (number_float / i).is_integer():
return False
return True
Expand Down
Loading

0 comments on commit 01cefe9

Please sign in to comment.