## klenwell information services : SpecialCocktailNapkin

#### Revision [1992]

This is an old revision of SpecialCocktailNapkin made by KlenwellAdmin on 2011-12-11 20:19:07.

## online cocktail napkin

links and notes for later organization

Mise:
['HHVVVH', 'HVHVVH', 'HVVHVH', 'HVVVHH', 'VHHVVH', 'VHVHVH', 'VHVVHH', 'VVHHVH', 'VVHVHH']
v = 'V'
h = 'H'

def order_markers():
order = {}
pos = range(6)

for p in pos:
marker = (p % 2 == 1) and h or v

if p < 2:
options = list(set(range(3)) - set(order.keys()))

else:
options = list(set(range(6)) - set(order.keys()))

place = random.choice(options)

order[place] = marker

sorder = sorted(order.items(), key=lambda t: t[0])

return [m for p,m in sorder]

orderings = []
for trial in range(100):
order = ''.join(order_markers())
orderings.append(order)

mise = list(set(orderings))

# H-last orderings
h_last = [o for o in mise if o[-1] == 'H']

App Engine Pagination (with cursors):
http://www.learningtechnicalstuff.com/2010/04/pagedquery-easy-paging-using-cursors-on.html
http://stackoverflow.com/questions/3347700/datastore-cursor-paging-in-reverse-supported
Bayes Theorem
```P(A|B) = (P(B|A) * P(A)) / P(B)
P(A) -> Prob of A
P(A|B) -> Prob of A given B```

What this assumes: we know prob of A, B, and B given A. We want to know A given B. Applied: http://allendowney.blogspot.com/2011/10/all-your-bayes-are-belong-to-us.html
http://steshaw.org/economics-in-one-lesson/chap03p1.html
"It was merely our old friend, the broken-window fallacy, in new clothing, and grown fat beyond recognition. This time it was supported by a whole bundle of related fallacies. It confused need with demand."
"To most people this seemed like an increase in total demand, as it partly was in terms of dollars of lower purchasing power. But what mainly took place was a diversion of demand to these particular products from others"
without diversion/need: wealthy -> finance -> bonds (government/corp) -> demand (not need) -> GOP blockage/Corporate anxiety
with diversion/need: wealthy -> finance -> bonds -> need (war or commitment to social infrastructure) -> Government spending
> Some demands are more urgent than others. A marginal dollar is discretionary for the wealthy. The wealthy have the luxury of discretion, of demand. The poor have needs.

System Status in Fabric source
def sys_status():
disk_usage = run('df -kP')
mem_usage = run('cat /proc/meminfo')
cpu_usage = run('cat /proc/stat')

print 'Disk Usage, Mem Usage, CPU Usage:', disk_usage, mem_usage, cpu_usage

[http://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard Filesystem Hierarchy Standard (FHS)] -> HUFHS (Home User FHS)
Load All Tests in Module
if __name__ == "__main__":
mod = sys.modules[globals()['__name__']]
unittest.TextTestRunner(verbosity=2).run(suite)

Polygon Points
>>> xs = [('x0','x1','x2','x3'), ('x4','x5','x6','x7')]
>>> ys = [('y0','y1','y2','y3'), ('y4','y5','y6','y7')]
>>> zip(xs,ys)
[(('x0', 'x1', 'x2', 'x3'), ('y0', 'y1', 'y2', 'y3')), (('x4', 'x5', 'x6', 'x7'), ('y4', 'y5', 'y6', 'y7'))]

matplotlib colormaps
dict obj
ref: http://stackoverflow.com/questions/1305532/
#
# Module Globals and Constants
#
data = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
def p(k,v): print 'setting', k, v, type(v)

nest = {
'y1' : {
't1' : {
'c1': 1,
'c2': [1,2,3]
},
't2' : {}
},
'y2' : {

}
}

#
# Data Structure
#
class ObjectDict(dict):
'''The recursive class for building and representing objects with.'''
seqs = tuple, list, set, frozenset

def __init__(self, obj):
for k, v in obj.iteritems():
if isinstance(v, dict):
setattr(self, k, ObjectDict(v))
elif isinstance(v, self.seqs):
vseq = []
for item in v:
if isinstance(item, dict):
vseq.append(ObjectDict(item))
else:
vseq.append(item)
setattr(self, k, vseq)
else:
setattr(self, k, v)
def __getitem__(self, val):
return self.__dict__[val]
def __repr__(self):
return '{%s}' % str(', '.join('%s : %s' % (k, repr(v)) for
(k, v) in self.__dict__.iteritems()))

#
# Test
#
d = ObjectDict(data)
print d
assert d.a == 1
assert d.b.c == 2
assert d.d[1].foo == 'bar'

n = ObjectDict(nest)
print n
assert n.y1.t1.c1 == 1
assert n.y1.t1.c2 == [1,2,3]
assert n.y1.t2 == {}

logarithms
• logX = Y ---> eY = X
• logXY = Z ---> XZ = Y
• log21024 = 10 ---> 210 = 1024
• http://repl.it/B4e
```Python 2.7.2 (default, Jul 20 2011, 02:32:18)
[GCC 4.2.1 (LLVM, Emscripten 1.5, Empythoned)] on linux2
import math
math.log.__doc__
=> 'log(x[, base])\n\nReturn the logarithm of x to the given base.\nIf the base not specified, returns the natural logarithm (base e) of x.'
math.pow(2, 10)
=> 1024.0
math.log(1024, 2)
=> 10.0
math.e
=> 2.7182818284590451
math.log(1, math.e)
=> 0.0```

Cron RPM
from random import randint

def cron_rpm(run_rate, job_rate):
"""runs per m (1000)"""
hr = 60
day = 24 * hr

tmap = {'hr': hr, 'day': day}

run_num, run_den = run_rate
job_num, job_den = job_rate

if run_den in tmap:
run_den = tmap[run_den]

if job_den in tmap:
job_den = tmap[job_den]

job_factor = float(job_den) / job_num

run_now = randint(1, int(run_den)) <= run_num * job_factor

return run_now

def test():
test_cases = [
# run_rate, job_rate, job_runs, expect
((5, 100), (1,1), 100, 5),
((1, 60), (1,3), 60, 3),
((3, 'day'), (30, 'hr'), 30*24, 3),
]

def run_test(run_rate, job_rate, runs, expect):
total = 0
for n in range(runs):
if cron_rpm(run_rate, job_rate):
total += 1

for run_rate, job_rate, runs, expect in test_cases:
trials = 100
total_sum = 0
for n in range(trials):
total = run_test(run_rate, job_rate, runs, expect)
total_sum += total

avg = total_sum / float(trials)
print 'avg, expect (%s trials)' % (trials), '%.1f' % (avg), expect

test()

Pyplot Example
# plot
import matplotlib.pyplot as pyplot

title = 'Conditional Probabilities'
xlabel = 'weeks'
ylabel = 'cond prob'
x1 = [x for x,y in first_probs]
y1 = [y for x,y in first_probs]
x2 = [x for x,y in other_probs]
y2 = [y for x,y in other_probs]

pyplot.plot(x1, y1, 'm-', label='first births')
pyplot.plot(x2, y2, 'b-', label='other births')
pyplot.title(title)
pyplot.xlabel(xlabel)
pyplot.ylabel(ylabel)
pyplot.xlim([25,50])
pyplot.legend(loc=2)
pyplot.show()

python reconstructing a traceback:
import traceback, sys
trace_str = ''.join(traceback.format_tb(sys.exc_info()[2]))

Pastebin20110628 - appswell min-tree.txt
State Machines
• Difference between state machine and program?
Information / Entropy
• Difference between energy, heat, radiation?
http://en.wikipedia.org/wiki/Deletionpedia
• Only active February to September 2008
http://pastebin.com/CyxzKLSP
for i in range(8):
print 'left 1 << %s:' % (i), 1<<i
print 'right 2**8 >> %s:' % (i), 2**8>>i

OUTPUT:
left 1 << 0: 1
right 2**8 >> 0: 256
left 1 << 1: 2
right 2**8 >> 1: 128
left 1 << 2: 4
right 2**8 >> 2: 64
left 1 << 3: 8
right 2**8 >> 3: 32
left 1 << 4: 16
right 2**8 >> 4: 16
left 1 << 5: 32
right 2**8 >> 5: 8
left 1 << 6: 64
right 2**8 >> 6: 4
left 1 << 7: 128
right 2**8 >> 7: 2

Python Decorators
"""
A simple demo of decorators
"
""
from random import (randint, choice)

def cache_service(f):
"""simulates a cache that randomly finds data in it and then acts
accordingly"
""
def wrapper():
cache = choice([True, False])
if cache:
print "cache found: will return that"
else:
print "cache not found: will call %s" % (f)
f()
return wrapper

def cache_resource(key, duration=60):
def inner_cache_data(fn):
def cacher(*args, **kwargs):
cache = choice(['cached data', False])
if cache:
print "cache found: will return that"
return cache
else:
print "cache not found: will call %s" % (fn)
data = fn(*args, **kwargs)
print "will now cache data with key %s for %ss" % (key, duration)
return data
return cacher
return inner_cache_data

@cache_service
def get_data_from_service():
print "getting data from service"
return 'data from service'

@cache_resource('resource-cache-key')
def get_data_from_resource():
print "getting data from resource"
return 'data from resource'

#
# Tests
#
for i in range(3):
print '\nrun get_data_from_service test %s' % (i)
print get_data_from_service()

for i in range(5):
print '\nrun get_data_from_resource test %s' % (i)
print 'returns: %s' % (get_data_from_resource())

Arestin:
• http://www.drugs.com/pro/arestin.html
• http://www.arestin.com/
• Sections
• Intro: not this, not that, just someone who's looked at trial results. No rec.
• Background: Why I Took It
• Relationship with hygienist and office
• History of bleeding gums: not terrible and better with improved dental hygiene
• No choice
• Then she presented the brochure: knew I'd been had.
• The Brochure
• What it shows: the simple mental model: Wouldn't be surprised if Hyg believes it works.
• What it hides, literally: Trial Data
• What does the data show? stats (% effectiveness), real-world impact
• What If It Works? The Bigger Problem
• 6 mos. Pocket reduced 1mm. What would it mean?
• Reflexive dependency on drugs to handle problems
• No discussion of alternatives: wait-and-see, increased cleaning, periodontal referral
• The Interface Problem: How to Reform Drug Usage in America
• Trial Data
• Probable Results
• Cost to Consumer, Insurance, Government
• Questions To Ask
• Can you explain trial results to me?
• Why do you think this will help me?
• Chance of Side effects: what does the data here mean?
• Would you take this drug if you were in my position? Would you prescribe it to your own child/partner?
• Do you receive any financial incentives that you think you should disclose?

Kindle: ConnectBot Without Password: http://goo.gl/Mvx2F
Press: APC white screen issue

Julian Date in Python (2011.05.11)
```from datetime import datetime
juliandate = '%s%s' % (datetime.now().timetuple().tm_year, datetime.now().timetuple().tm_yday)```

More Encoding Issues (fixing encoding issue with hmac):
`input = unicode(special_chars).encode('utf-8')`

import hmac, sha, random

pound_sign = u'\u00A3'
cent_sign = u'\u00A2'
null_sign = u'\u0000'
random_sign = unichr(random.randint(1000,10000))

special_chars = 'derp... %s%s%s%s' % (pound_sign, cent_sign, null_sign,
random_sign)

# reproduces problem
try:
hmac_digest = hmac.new('key', special_chars, sha).hexdigest()
fail('expected exception')
except TypeError, e:
assert(str(e).find("can't encode characters") != -1)

# test unicode (ref: http://goo.gl/vJKlo)
input = unicode(special_chars).encode('utf-8')
assert(type(input) == str)
hmac_digest = hmac.new('key', input, sha).hexdigest()
print hmac_digest

A test:
def test_encoding_normalization(self):
pound_sign = u'\u00A3'
cent_sign = u'\u00A2'
nonascii = 'derp... %s%s' % (pound_sign, cent_sign)
self.assertEqual(type(nonascii), unicode)

nonascii_str = nonascii.encode('utf-8')
nonascii_uni = nonascii_str.decode('utf-8')
ascii_str = nonascii_uni.encode('ascii', 'replace')
self.assertEqual(type(nonascii_str), str)
self.assertEqual(type(nonascii_uni), unicode)

# if default encoding is ascii, this fails
self.assertRaises(UnicodeDecodeError, unicode, nonascii_str)
self.assertRaises(UnicodeEncodeError, str, nonascii_uni)

# so to normalize either str or unicode, first decode, then encode
def normalize_utf8(input):
if type(input) == unicode:
input = input.encode('utf-8')
return input

for test_input in (nonascii_str, nonascii_uni, ascii_str):
normalized_input = normalize_utf8(test_input)
self.assertEqual(type(normalized_input), str)

# make sure no issues with string formatting
print 'string formatting: %s' % (normalized_input)

# convert utf8 str to ascii str
ascii_str = nonascii_str.decode('utf-8').encode('ascii', 'replace')
self.assertTrue(ascii_str.find('??') != -1)

MYSQL: convert a squiggly (encoded) password to hex and back
```mysql> SELECT HEX('abc');
+------------+
| HEX('abc') |
+------------+
| 616263     |
+------------+
1 row in set (0.06 sec)

mysql> SELECT X'616263';
+-----------+
| X'616263' |
+-----------+
| abc       |
+-----------+
1 row in set (0.00 sec)```

*Idle Thoughts on the Next Market Bubble (Hacker News comment)
*Purple Cloud
*TweetNest Forum Post (Fix for date offset)
A Day at the Zoo
*Why Chinese Mothers are Not Superior (from a female Chinese engineer) (news.ycombinator.com)
*HR 2 (Krugman)
NYR: CE Francis

CategorySpecial