/timm's /charming /python /tricks

libd.py

Download libd.py.
Read more on How to be Charming (in Python).


01: import sys
02: sys.dont_write_bytecode=True
03: from lib import *
04: 
05: @demo
06: def mediand():
07:   print median([1,2,3,4,5,6])
08:   print median([1,2,3,4,5])
09: 
10: @demo
11: def oned():
12:   "Get anything out of a list."
13:   resetSeed(1)
14:   print one(list('abcd'))
15: 
16: @demo
17: def fewd():
18:   "Get any 3 things out of a list."
19:   resetSeed(1)
20:   print few(list('abcd'),3)
21: 
22: @demo
23: def somed():
24:   "Sample from some dictionary weights."
25:   resetSeed(1)
26:   d= {'box':30,'circle':20,'line':10}
27:   out = []
28:   for key in some(d, 10):
29:     out += [key]
30:   print sorted(out)
31: 
32: @demo
33: def thingd():
34:   """
35:   Shows the auto print of a thing. Note:
36:   the _cache var not printed (since it can
37:   get VERY big).
38:   """
39:   eg = Sample()
40:   print eg
41: 
42: @demo
43: def structd():
44:   "Create an object holding data in some fields."
45:   x = Struct(lines=[], rows = 0, font='courier')
46:   x.rows += 1
47:   x.lines += ['a new line']
48:   print x
49: 
50: @demo
51: def sampled():
52:   "Keep 20 items out of one million."
53:   resetSeed(1)
54:   sample = Sample(size=20)
55:   for x in range(10**6):
56:     sample.seen(x)
57:   print sample.sorted()
58: 
59: if __name__ == '__main__' : 
60:   eval(cmd('copyleft()'))
61: 

This file is part of Timm's charming Python tricks.
© 2014, Tim Menzies: tim.menzies@gmail.com, http://menzies.us.

Timm's charming Python tricks are free software: you can redistribute it and/or modify it under the terms of the GNU Lesser Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Timm's charming Python tricks are distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU Lesser Public License along with Foobar. If not, see http://www.gnu.org/licenses.