-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pf4d
committed
May 26, 2017
1 parent
11a5fcf
commit 456a472
Showing
7 changed files
with
821 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
## {{{ http://code.activestate.com/recipes/577814/ (r1) | ||
# Lorenz Attractor (projected onto XY-plane) | ||
# http://en.wikipedia.org/wiki/Lorenz_attractor | ||
# FB - 201107317 | ||
import random | ||
from PIL import Image | ||
imgx = 800 | ||
imgy = 600 | ||
image = Image.new("RGB", (imgx, imgy)) | ||
|
||
maxIt = 100000 # number of pixels to draw | ||
size = 30 | ||
xa = -size | ||
xb = size | ||
ya = -size | ||
yb = size | ||
|
||
# initial state | ||
x = random.random() * size * 2 - 1 | ||
y = random.random() * size * 2 - 1 | ||
z = random.random() * size * 2 - 1 | ||
|
||
# dx/dt = delta * (y - x) | ||
# dy/dt = r * x - y - x * z | ||
# dz/dt = x * y - b * z | ||
delta = float(10) # Prandtl number | ||
r = float(28) | ||
b = float(8) / 3 | ||
h = 1e-3 # time step | ||
def Lorenz(x, y, z): | ||
dx_dt = delta * (y - x) | ||
dy_dt = r * x - y - x * z | ||
dz_dt = x * y - b * z | ||
x += dx_dt * h | ||
y += dy_dt * h | ||
z += dz_dt * h | ||
return (x, y, z) | ||
|
||
for i in range(maxIt): | ||
(x, y, z) = Lorenz(x, y, z)#; print x, y, z | ||
xi = int((imgx - 1) * (x - xa) / (xb - xa)) | ||
yi = int((imgy - 1) * (y - ya) / (yb - ya)) | ||
if xi >=0 and xi < imgx and yi >= 0 and yi < imgy: | ||
image.putpixel((xi, yi), (255, 255, 255)) | ||
|
||
image.save("Lorenz_Attractor.png", "PNG") | ||
## end of http://code.activestate.com/recipes/577814/ }}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env python | ||
|
||
fg = '\033[38;5;' | ||
bg = '\033[48;5;' | ||
|
||
for i in range( 0, 256): | ||
n = str(i) | ||
fgstr = fg + n + 'm' + n | ||
bgstr = bg + n + 'm' 'XXXXX' | ||
print fgstr, bgstr, '\033[0m' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from datetime import * | ||
|
||
|
||
birth = datetime(2010, 11, 24) # birthdate Nov. 24th, 2010 | ||
today = datetime.today() # current date | ||
|
||
time = today - birth | ||
age = time.days/365 # current age | ||
dogAge = 7*time | ||
dogAge = dogAge.days/365 # current dog age | ||
|
||
sstime = timedelta(365*16/7) # time to turn 16 | ||
ssdate = birth + sstime # date of 16th birthday (in dog years) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import re | ||
import datetime as dt | ||
import calendar as cal | ||
import matplotlib.dates as dates | ||
from pylab import * | ||
|
||
|
||
data = open("groceries_in.txt") | ||
p = re.compile('([0-9]+.[0-9]+.[0-9]+)-([0-9]+.[0-9]+).*?\n') | ||
date = [] | ||
spent = [] | ||
|
||
# fill the arrays values from the .txt file : | ||
for l in data.readlines(): | ||
m = p.match(l) | ||
if m != None: | ||
dateSplit = m.group(1).rsplit('.') | ||
d = dt.datetime(int(dateSplit[2]), | ||
int(dateSplit[0]), | ||
int(dateSplit[1])) | ||
date.append(d) | ||
spent.append(float(m.group(2))) | ||
data.close() | ||
date.sort() | ||
|
||
# total time in days : | ||
start = date[0] - dt.timedelta(1) | ||
end = date[-1] | ||
totTime = end - start | ||
|
||
# total costs : | ||
costPerDay = sum(spent) / totTime.days | ||
total = sum(spent) | ||
|
||
|
||
# create an array of monthly expendatures : | ||
avg = [] | ||
for i in range(len(spent)): | ||
avgCostToDate = sum(spent[:i]) / (date[i] - start).days | ||
avg.append(avgCostToDate) | ||
|
||
|
||
# calculate monthly costs : | ||
month_total = [] | ||
month_tot_str = [] | ||
cont_date = [] | ||
tot = 0.0 | ||
month = date[0].month | ||
year = date[0].year | ||
date_n = date[0] | ||
for i,d in enumerate(date): | ||
# tallying monthly totals : | ||
if d.month == month and d.year == year: | ||
tot += spent[i] | ||
# calculate the monthly totals and append : | ||
else: | ||
month_tot_str.append('$' + str(round(tot, 2))) | ||
min_d, max_d = cal.monthrange(year, month) | ||
for i in range(date_n.day, max_d+2): | ||
month_total.append(tot) | ||
cont_date.append(date_n) | ||
date_n += dt.timedelta(1) | ||
tot = spent[i] | ||
month = d.month | ||
year = d.year | ||
|
||
# finish off the last part of the month : | ||
month_tot_str.append('$' + str(round(tot, 2))) | ||
for i in range(date_n.day, d.day+1): | ||
month_total.append(tot) | ||
cont_date.append(date_n) | ||
date_n += dt.timedelta(1) | ||
|
||
# update output file with statistics : | ||
data = open("groceries_out.txt", "w") | ||
data.write('Grocery / Dog Expenses:\n\n\n') | ||
for d, s in zip(date, spent): | ||
data.write(d.strftime("%m.%d.%y") + ' - ' + str(s) + '\n') | ||
data.write('\n\nTotal: $%.2f' % total) | ||
data.write('\n\nAverage cost per day: $%.2f' % costPerDay) | ||
data.close() | ||
|
||
# plotting : | ||
totTime = date[-1] - date[0] | ||
days = totTime.days | ||
fig = figure(figsize=(12,5)) | ||
tit = "Food Expendature for %s days : $%.2f" % (days, total) | ||
ax1 = fig.add_axes([.1,.2,.8,.7], title = tit) # [left, bottom, width, height] | ||
ax2 = ax1.twinx() | ||
ax1.grid() | ||
ax1.plot(date, spent, 'ro', lw=1, label='expendatures') | ||
ax1.plot(date, avg, 'b-', lw=2, label='average cost per day') | ||
ax2.plot(cont_date, month_total, 'g-', lw=2, label='monthly total', | ||
drawstyle='steps') | ||
leg = ax1.legend(loc='upper left') | ||
leg.get_frame().set_alpha(0.5) | ||
ax1.set_ylabel('Cost ($)') | ||
ax2.set_ylabel('Total Monthly Cost ($)', color='g') | ||
ax1.set_xlabel("Average Cost Per Day : $%.2f" % costPerDay) | ||
for tl in ax2.get_yticklabels(): | ||
tl.set_color('g') | ||
plt.show() | ||
|
||
|
||
|
Oops, something went wrong.