├── book ├── .gitignore ├── add.py ├── 01-add.png ├── book.pdf ├── logo.png ├── 02-plus.png ├── 03-adder.png ├── 04-add7.png ├── flatten.awk ├── adder.py ├── header.tex ├── Makefile ├── timed.py ├── menu-after.py └── menu-before.py ├── dynamic-static ├── charts │ └── empty-file ├── web-down ├── index.cgi ├── web-up └── chart.cgi ├── tagcloud ├── mako │ ├── ext │ │ ├── __init__.py │ │ └── preprocessors.py │ └── __init__.py ├── Makefile └── tagcloud.mako ├── pycfg └── config_local.py ├── pytest-cmp ├── .gitignore ├── requirements.txt ├── mask.py ├── mask_cases.toml └── test_mask.py ├── cheetah-demo ├── Cheetah │ ├── Macros │ │ └── __init__.py │ ├── Utils │ │ ├── __init__.py │ │ ├── htmlDecode.py │ │ ├── htmlEncode.py │ │ └── optik │ │ │ ├── __init__.py │ │ │ └── errors.py │ ├── Unspecified.py │ ├── convertTmplPathToModuleName.py │ └── __init__.py ├── index.tmpl ├── index.cgi ├── master.tmpl └── web-server ├── state-machine ├── .gitignore ├── go.mod ├── Makefile ├── float_test.go └── state-machine.dot ├── config-extensions.cfg ├── react-table ├── README.md ├── Makefile ├── index.html ├── httpd.go └── table.jsx ├── .hgtags ├── dmd-mail ├── postfix │ ├── sasl_passwd │ └── main.cf ├── .sup │ ├── contacts.txt │ ├── sources.yaml │ └── config.yaml ├── README.md └── .getmail │ └── getmailrc ├── less-code ├── version-2 │ ├── crontab │ └── nfs-keep.py ├── version-3 │ └── crontab └── version-1 │ ├── crontab │ └── nfs-keep.py ├── go-protobuf-json ├── Makefile ├── go.mod ├── go.sum ├── job.proto └── job.go ├── js-sound ├── Makefile └── soundplayer.mxml ├── ll-freq.png ├── withopen.png ├── 2014-11-15.kmz ├── assembler.pdf ├── ctxdec ├── run-notebook.sh ├── README.md └── ctx.py ├── multi_col.png ├── comma ├── initial.py ├── fix.py ├── fix2.py └── debug.txt ├── CommentsIntoCode.pdf ├── go-asset ├── image.png ├── Makefile └── httpd.go ├── top-x ├── alice.txt.bz2 └── gendata.py ├── autobuild-c-ext ├── t.py └── greet │ ├── setup.py │ ├── _greet.c │ └── __init__.py ├── homedir.py ├── macapp ├── humblecalc.ico ├── Makefile ├── README.txt ├── Info.plist └── index.html ├── present-timer ├── 353.png ├── picard-why.jpg ├── xor.go ├── pi.go ├── num.go ├── unicode.go ├── stridx.go ├── vars2.go ├── init.go ├── empty-map.go ├── ch.go ├── sliceappend.go ├── time.go ├── tick.go ├── sleep-sort.go ├── err.go ├── timer.html └── quiz.slide ├── .ctags ├── docker-miniconda ├── image.jpg ├── Makefile ├── README.md ├── test.sh └── Dockerfile ├── push-cgi ├── myapp.py ├── load.cgi ├── push └── web-server ├── test_joke.py ├── Google Chrome ├── config ├── app.py └── config.py ├── go-from-c ├── README.md ├── hi.c ├── Makefile ├── hi.go └── hi.h ├── pandas-validation ├── nuke.sh ├── schema.sql ├── bootstrap.sh ├── stats.py ├── pull.py └── populate.py ├── result ├── nsqrt.h ├── nmath.go ├── result.c ├── result.h ├── nsqrt.c ├── nmath_test.go └── result.go ├── aws.sh ├── ilast.py ├── pip.conf ├── test.asm ├── pyexe.py ├── setup-for-swig.py ├── num_checkins ├── gae ├── push.sh ├── check.sh ├── run-local.sh ├── repl.sh ├── pypath.sh └── gae.py ├── ipython_fmt.py ├── import_any.py ├── binary ├── chrome.sh ├── orphan-branch.sh ├── pyindent ├── greet.sh ├── partial-demo.py ├── haproxy-es ├── docker-compose.yml └── haproxy.cfg ├── git-hooks ├── post-checkout └── pre-commit ├── tag-ok-build.sh ├── post-checkout ├── persist_id.py ├── .hgignore ├── bbembed ├── default_vs_none.py ├── e ├── powerset.py ├── rst ├── Makefile └── hw.txt ├── numphone ├── hardbreak.py ├── next_n.py ├── Makfile.test ├── source.py ├── genpatch ├── sbcl ├── gen-bbd.sh ├── today ├── test_data.py ├── deploy.sh ├── initgae.py ├── httpc.py ├── README.md ├── run-job.sh ├── sql-col-by-name.py ├── indent.py ├── py_defer ├── test_defer.py └── defer.py ├── redirect.html ├── clickreg ├── index.html └── jump.cgi ├── odbc-osx.py ├── hg-close ├── dd-demo.py ├── ipy.scpt ├── slice.py ├── couchnuke ├── httpserve ├── unique.py ├── animals.py ├── wait_for_server.go ├── rootdir.py ├── colorlog.sh ├── f.py ├── multiline-regexp.py ├── flatten.py ├── translate.sh ├── blogmentize ├── rotate.py ├── tmp-couch ├── websession ├── index.py └── web-server ├── glue ├── web ├── redirect.cgi └── lighttpd.conf.in ├── affinity_1.go ├── printobj.py ├── httpparse.py ├── mk ├── svncommit ├── web-install ├── st ├── teamspeak ├── wdays.sql ├── extract-audio ├── jmlr.py ├── la.py ├── pipe.py ├── num_words.py ├── timeit-demo.py ├── carrier ├── htmlents.py ├── is_zero.go ├── manual-bp.go ├── check_install.py ├── dashm.txt ├── auto_rety.py ├── config_wrapper.py ├── update_daemons.out ├── attr.py ├── enum2.py ├── gist-it ├── hiveloc ├── trampoline.py ├── myip.py ├── date-demo.sh ├── add_cert.py ├── enum1.py ├── logio.py ├── decrypt ├── noise-reduction.py ├── cd.py ├── openmodule.vim ├── jsonify.py ├── istail.py ├── encrypt ├── timer.py ├── pyhelp ├── nopeek.py ├── sock_close.py ├── exiter.py ├── feedme ├── feed.cgi ├── web-server └── index.html ├── daemon ├── bbot.py ├── mock.py ├── putpath ├── github-url ├── start ├── pkg-urls.py ├── pool.go ├── ll-freq.py ├── affinity.go ├── match.py ├── putclip ├── playwave.py ├── plot2d.py ├── jsonpretty ├── fade.html ├── sumtuples.py ├── named_decorator.py ├── mpweb.py ├── gcalc ├── sqldir.py ├── arch-install.txt ├── benchmark.py ├── cypher.py ├── webphone └── index.html ├── decimals.py ├── sentences.py ├── trylock.py ├── trends └── index.html ├── ssh-hostname ├── pmap.py ├── hist_stats.py ├── curl-and-couchdb.sh ├── aenumerate.py ├── find-pass.py ├── pool.py ├── LICENSE.txt └── solr.html /book/.gitignore: -------------------------------------------------------------------------------- 1 | book-flat.md 2 | -------------------------------------------------------------------------------- /dynamic-static/charts/empty-file: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tagcloud/mako/ext/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /pycfg/config_local.py: -------------------------------------------------------------------------------- 1 | web_port = 8000 2 | -------------------------------------------------------------------------------- /pytest-cmp/.gitignore: -------------------------------------------------------------------------------- 1 | .pytest_cache/ 2 | -------------------------------------------------------------------------------- /cheetah-demo/Cheetah/Macros/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | -------------------------------------------------------------------------------- /cheetah-demo/Cheetah/Utils/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | -------------------------------------------------------------------------------- /state-machine/.gitignore: -------------------------------------------------------------------------------- 1 | state-machine.png 2 | -------------------------------------------------------------------------------- /config-extensions.cfg: -------------------------------------------------------------------------------- 1 | [AutoComplete] 2 | popupwait = 1 3 | -------------------------------------------------------------------------------- /pytest-cmp/requirements.txt: -------------------------------------------------------------------------------- 1 | toml~=0.10 2 | pytest~=7.2 3 | -------------------------------------------------------------------------------- /react-table/README.md: -------------------------------------------------------------------------------- 1 | Using reactjs to add rows to a table. 2 | -------------------------------------------------------------------------------- /.hgtags: -------------------------------------------------------------------------------- 1 | 8a417986d4a929533cde80b3f8c5d8afc657817a initial-upload 2 | -------------------------------------------------------------------------------- /dmd-mail/postfix/sasl_passwd: -------------------------------------------------------------------------------- 1 | [127.0.0.1]:1025 bugs.bunny:XXXXXX 2 | -------------------------------------------------------------------------------- /less-code/version-2/crontab: -------------------------------------------------------------------------------- 1 | */10 * * * * /path/to/nfs-keep.py 2 | -------------------------------------------------------------------------------- /less-code/version-3/crontab: -------------------------------------------------------------------------------- 1 | */10 * * * * /bin/ls /path/to/nfs 2 | -------------------------------------------------------------------------------- /dmd-mail/.sup/contacts.txt: -------------------------------------------------------------------------------- 1 | gme: Miki Tebeka 2 | -------------------------------------------------------------------------------- /go-protobuf-json/Makefile: -------------------------------------------------------------------------------- 1 | gen-pb: 2 | protoc job.proto --go_out=pb 3 | -------------------------------------------------------------------------------- /js-sound/Makefile: -------------------------------------------------------------------------------- 1 | soundplayer.swf: soundplayer.mxml 2 | mxmlc $< 3 | -------------------------------------------------------------------------------- /book/add.py: -------------------------------------------------------------------------------- 1 | def add(x, y): 2 | """Addition""" 3 | return x + y 4 | -------------------------------------------------------------------------------- /less-code/version-1/crontab: -------------------------------------------------------------------------------- 1 | @reboot /usr/bin/python /path/to/nfs-keep.py 2 | -------------------------------------------------------------------------------- /ll-freq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/ll-freq.png -------------------------------------------------------------------------------- /withopen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/withopen.png -------------------------------------------------------------------------------- /2014-11-15.kmz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/2014-11-15.kmz -------------------------------------------------------------------------------- /assembler.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/assembler.pdf -------------------------------------------------------------------------------- /book/01-add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/book/01-add.png -------------------------------------------------------------------------------- /book/book.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/book/book.pdf -------------------------------------------------------------------------------- /book/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/book/logo.png -------------------------------------------------------------------------------- /ctxdec/run-notebook.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ipython notebook --pylab=inline 4 | -------------------------------------------------------------------------------- /multi_col.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/multi_col.png -------------------------------------------------------------------------------- /book/02-plus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/book/02-plus.png -------------------------------------------------------------------------------- /book/03-adder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/book/03-adder.png -------------------------------------------------------------------------------- /book/04-add7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/book/04-add7.png -------------------------------------------------------------------------------- /comma/initial.py: -------------------------------------------------------------------------------- 1 | colors = [ 2 | 'red', 3 | 'green', 4 | 'blue' 5 | ] 6 | -------------------------------------------------------------------------------- /CommentsIntoCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/CommentsIntoCode.pdf -------------------------------------------------------------------------------- /go-asset/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/go-asset/image.png -------------------------------------------------------------------------------- /go-protobuf-json/go.mod: -------------------------------------------------------------------------------- 1 | module main 2 | 3 | require github.com/golang/protobuf v1.2.0 4 | -------------------------------------------------------------------------------- /state-machine/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tebeka/pythonwise/state-machine 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /top-x/alice.txt.bz2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/top-x/alice.txt.bz2 -------------------------------------------------------------------------------- /autobuild-c-ext/t.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import greet 4 | 5 | greet.greet("Dude") 6 | -------------------------------------------------------------------------------- /comma/fix.py: -------------------------------------------------------------------------------- 1 | colors = [ 2 | 'red', 3 | 'green', 4 | 'blue' 5 | 'yellow' 6 | ] 7 | -------------------------------------------------------------------------------- /homedir.py: -------------------------------------------------------------------------------- 1 | >>> from os.path import expanduser 2 | >>> expanduser('~') 3 | '/Users/miki' 4 | 5 | -------------------------------------------------------------------------------- /macapp/humblecalc.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/macapp/humblecalc.ico -------------------------------------------------------------------------------- /present-timer/353.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/present-timer/353.png -------------------------------------------------------------------------------- /.ctags: -------------------------------------------------------------------------------- 1 | --langdef=XML 2 | --langmap=XML:.xml 3 | --regex-XML=/id="([a-zA-Z0-9_]+)"/\1/d,definition/ 4 | -------------------------------------------------------------------------------- /comma/fix2.py: -------------------------------------------------------------------------------- 1 | colors = [ 2 | 'red', 3 | 'green', 4 | 'blue', 5 | 'yellow', 6 | ] 7 | -------------------------------------------------------------------------------- /docker-miniconda/image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/docker-miniconda/image.jpg -------------------------------------------------------------------------------- /present-timer/picard-why.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tebeka/pythonwise/HEAD/present-timer/picard-why.jpg -------------------------------------------------------------------------------- /push-cgi/myapp.py: -------------------------------------------------------------------------------- 1 | def do_something_with_data(key, data): 2 | open("/tmp/%s" % key, "wb").write(data) 3 | 4 | -------------------------------------------------------------------------------- /test_joke.py: -------------------------------------------------------------------------------- 1 | # Minimal smoke test to be used with "nose" (or "py.test") 2 | def test_joke(): 3 | pass 4 | -------------------------------------------------------------------------------- /Google Chrome: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | "/Applications/Google Chrome.app/Contents/MacOS/_chrome" --enable-plugins $* 4 | -------------------------------------------------------------------------------- /book/flatten.awk: -------------------------------------------------------------------------------- 1 | # "flatten" multiple files to single one 2 | /INCLUDE/ { system("cat " $2); next} 3 | // { print $0 } 4 | -------------------------------------------------------------------------------- /comma/debug.txt: -------------------------------------------------------------------------------- 1 | In [2]: len(colors) 2 | Out[2]: 3 3 | 4 | In [3]: print(colors) 5 | ['red', 'green', 'blueyellow'] 6 | -------------------------------------------------------------------------------- /docker-miniconda/Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | docker build -t imgsrv . 3 | 4 | 5 | run: 6 | docker run -p 8080:8080 imgsrv 7 | -------------------------------------------------------------------------------- /present-timer/xor.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | n := 2 ^ 10 7 | fmt.Println(n) 8 | } 9 | -------------------------------------------------------------------------------- /config/app.py: -------------------------------------------------------------------------------- 1 | import config 2 | 3 | for key in dir(config): 4 | val = getattr(config, key) 5 | print(f'{key} → {val}') 6 | -------------------------------------------------------------------------------- /go-from-c/README.md: -------------------------------------------------------------------------------- 1 | # Go From C 2 | 3 | Example on using Go code from C. 4 | 5 | ## Running 6 | 7 | Run `make` and then `./hi` 8 | -------------------------------------------------------------------------------- /present-timer/pi.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var π = 22 / 7.0 7 | fmt.Println(π) 8 | } 9 | -------------------------------------------------------------------------------- /pandas-validation/nuke.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for step in pre post; do 4 | psql -c 'DELETE FROM points' points_${step} 5 | done 6 | -------------------------------------------------------------------------------- /pandas-validation/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE points ( 2 | x INTEGER 3 | , y INTEGER 4 | , z INTEGER 5 | , value FLOAT 6 | ); 7 | -------------------------------------------------------------------------------- /present-timer/num.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Println(0x1p-2) 9 | } 10 | -------------------------------------------------------------------------------- /present-timer/unicode.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | city := "Kraków" 7 | fmt.Println(len(city)) 8 | } 9 | -------------------------------------------------------------------------------- /result/nsqrt.h: -------------------------------------------------------------------------------- 1 | #ifndef NSQRT_H 2 | #define NSQRT_H 3 | 4 | #include "result.h" 5 | 6 | result_t nsqrt(double); 7 | 8 | #endif // NSQRT_H 9 | -------------------------------------------------------------------------------- /aws.sh: -------------------------------------------------------------------------------- 1 | export AWS_ACCESS_KEY_ID=ABCDEFGHIJK 2 | export AWS_SECRET_ACCESS_KEY=ABCDEFGHIJK/lmnopqrstuvwxyz 3 | export AWS_DEFAULT_REGION=us-east-1 4 | -------------------------------------------------------------------------------- /ilast.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | def ilast(col, N): 4 | "Get last N items in collection" 5 | return deque(iter(col), N) 6 | -------------------------------------------------------------------------------- /present-timer/stridx.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | city := "Kraków" 7 | fmt.Printf("%T\n", city[0]) 8 | } 9 | -------------------------------------------------------------------------------- /state-machine/Makefile: -------------------------------------------------------------------------------- 1 | test: 2 | go test -v 3 | 4 | image: state-machine.png 5 | 6 | state-machine.png: state-machine.dot 7 | dot -Tpng -o $@ $< 8 | -------------------------------------------------------------------------------- /pip.conf: -------------------------------------------------------------------------------- 1 | [install] 2 | use-wheel = true 3 | find-links = file:///Users/mikitebeka/.pip/wheelhouse 4 | download-cache = ~/.pip/cache 5 | 6 | # vim:ft=cfg 7 | -------------------------------------------------------------------------------- /present-timer/vars2.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | a, b := 1, 2 7 | b, c := 3, 4 8 | fmt.Println(a, b, c) 9 | } 10 | -------------------------------------------------------------------------------- /dmd-mail/README.md: -------------------------------------------------------------------------------- 1 | Email configuration example from the email setup 2 | [blog post](http://pythonwise.blogspot.com/2014/01/current-email-setup-osx-homebrew-sup.html) 3 | -------------------------------------------------------------------------------- /test.asm: -------------------------------------------------------------------------------- 1 | MEM_LOC1 = 0x200 # Example of using variable 2 | 3 | add(r0, r2, r3) 4 | sub(r2, r4, r4) 5 | load(r2, MEM_LOC1) 6 | label("L1") 7 | move(r2, r7) 8 | jmp(L1) 9 | -------------------------------------------------------------------------------- /book/adder.py: -------------------------------------------------------------------------------- 1 | def make_adder(n): 2 | """Returns a functions that add n to the argument""" 3 | def adder(val): 4 | return val + n 5 | 6 | return adder 7 | -------------------------------------------------------------------------------- /present-timer/init.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func init() { fmt.Printf("A ") } 6 | func init() { fmt.Print("B ") } 7 | 8 | func main() { fmt.Println() } 9 | -------------------------------------------------------------------------------- /present-timer/empty-map.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var m map[string]int 7 | m["errors"]++ 8 | fmt.Println(m["errors"]) 9 | } 10 | -------------------------------------------------------------------------------- /pyexe.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''To be used from Makefile 3 | 4 | PYTHON = $(shell pyexe.py) 5 | ''' 6 | 7 | from sys import executable 8 | print executable 9 | 10 | -------------------------------------------------------------------------------- /setup-for-swig.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup, Extension 2 | 3 | setup( 4 | ext_modules = [ 5 | Extension("_hello", sources=["hello.c", "hello.i"]) 6 | ] 7 | ) 8 | -------------------------------------------------------------------------------- /num_checkins: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # How many checking I did today? 3 | # Without arguments will default to current directory 4 | 5 | svn log -r"{`date +%Y%m%d`}:HEAD" $1 | grep "| $USER |" | wc -l 6 | -------------------------------------------------------------------------------- /present-timer/ch.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | c := make(chan int, 2) 7 | c <- 1 8 | c <- 2 9 | <-c 10 | close(c) 11 | fmt.Println(<-c) 12 | } 13 | -------------------------------------------------------------------------------- /gae/push.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Push new version to AppEngine and tag in mercurial 3 | 4 | set -e 5 | 6 | /opt/python2.5/bin/python /opt/google_appengine/appcfg.py update . 7 | hg tag -f appengine 8 | -------------------------------------------------------------------------------- /go-protobuf-json/go.sum: -------------------------------------------------------------------------------- 1 | github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= 2 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 3 | -------------------------------------------------------------------------------- /ipython_fmt.py: -------------------------------------------------------------------------------- 1 | def int_formatter(integer, pp, cycle): 2 | pp.text(f'{integer:,}') 3 | 4 | plain = get_ipython().display_formatter.formatters['text/plain'] 5 | plain.for_type(int, int_formatter) 6 | -------------------------------------------------------------------------------- /present-timer/sliceappend.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | s := []int{1, 2, 3} 7 | b := s[:] 8 | s = append(s, 4) 9 | s[1] = 99 10 | fmt.Println(b) 11 | } 12 | -------------------------------------------------------------------------------- /import_any.py: -------------------------------------------------------------------------------- 1 | from types import ModuleType 2 | 3 | def import_any(path, name=None): 4 | module = ModuleType(name or "dummy") 5 | execfile(path, globals(), module.__dict__) 6 | 7 | return module 8 | -------------------------------------------------------------------------------- /less-code/version-2/nfs-keep.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Keep NFS mount "warm", called from crontab''' 3 | 4 | from os import listdir 5 | 6 | if __name__ == "__main__": 7 | listdir("/path/to/nfs") 8 | -------------------------------------------------------------------------------- /react-table/Makefile: -------------------------------------------------------------------------------- 1 | ver=react-0.12.2 2 | zip=$(ver).zip 3 | 4 | fetch: 5 | curl -LO http://facebook.github.io/react/downloads/$(zip) 6 | unzip $(zip) 7 | 8 | 9 | server: 10 | go run httpd.go 11 | -------------------------------------------------------------------------------- /binary: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Show binary representation of a number 3 | 4 | if [ $# -ne 1 ]; then 5 | echo "usage: `basename $0` NUMBER" 1>&2 6 | exit 1 7 | fi 8 | 9 | echo "obase=2; $1" | bc -l 10 | -------------------------------------------------------------------------------- /dmd-mail/postfix/main.cf: -------------------------------------------------------------------------------- 1 | relayhost = [127.0.0.1]:1025 2 | smtp_sasl_auth_enable = yes 3 | smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd 4 | smtp_sasl_security_options = noanonymous 5 | smtp_use_tls = yes 6 | -------------------------------------------------------------------------------- /chrome.sh: -------------------------------------------------------------------------------- 1 | [17:31] ~ $cd /Applications/Google\ Chrome.app/Contents/MacOS/ 2 | [17:32] MacOS $sudo mv Google\ Chrome _chrome 3 | [17:32] MacOS $sudo gvim Google\ Chrome 4 | [17:33] MacOS $chmod +x Google\ Chrome 5 | 6 | -------------------------------------------------------------------------------- /docker-miniconda/README.md: -------------------------------------------------------------------------------- 1 | Example on how to use [MiniConda][mc] with [docker][dk], see `Makefile` on how to 2 | build and run. 3 | 4 | [mc]: http://conda.pydata.org/miniconda.html 5 | [dk]: https://www.docker.com/ 6 | -------------------------------------------------------------------------------- /orphan-branch.sh: -------------------------------------------------------------------------------- 1 | # Create empty orphan branch for code review 2 | 3 | git checkout --orphan code-review 4 | git rm -rf . 5 | git ci --allow-empty -m 'Code review - nuke all' 6 | git push --set-upstream origin code-review 7 | -------------------------------------------------------------------------------- /dmd-mail/.getmail/getmailrc: -------------------------------------------------------------------------------- 1 | [retriever] 2 | type = SimplePOP3Retriever 3 | server = localhost 4 | port = 1110 5 | username = bugs.bunny 6 | password = whatsUPdoc 7 | 8 | [destination] 9 | type = Maildir 10 | path = ~/Mail/ 11 | -------------------------------------------------------------------------------- /go-from-c/hi.c: -------------------------------------------------------------------------------- 1 | #include "hi.h" 2 | #include 3 | 4 | int 5 | main() { 6 | printf("C starts\n"); 7 | GoString name = { "Daffy", 5 }; 8 | Hi(name); 9 | printf("C ends\n"); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /pyindent: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Indent C files according to http://www.python.org/dev/peps/pep-0007/ 4 | # Miki Tebeka 5 | 6 | indent -cli4 -npcs -l78 -bad -bap -br -ce -nbc -di1 -psl -i4 -brs -lp -cdw -ut -ts4 $@ 7 | -------------------------------------------------------------------------------- /dynamic-static/web-down: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Kill web services 3 | 4 | if [ -f lighttpd.pid ]; then 5 | pid=$(cat lighttpd.pid) 6 | echo "Killing $pid" 7 | kill $pid 8 | else 9 | echo "lighttpd not running" 10 | fi 11 | -------------------------------------------------------------------------------- /greet.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Print messge in the middle of the screen 3 | 4 | # Print in middle of screen 5 | msg="Let's boogie, Mr Swine" 6 | offset=$((($(tput cols) - ${#msg})/2)) 7 | printf "\n\n\n\n%*s%s\n\n\n\n" ${offset} ' ' "${msg}" 8 | -------------------------------------------------------------------------------- /present-timer/time.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | timeout := 3 10 | fmt.Printf("before ") 11 | time.Sleep(timeout * time.Millisecond) 12 | fmt.Println("after") 13 | } 14 | -------------------------------------------------------------------------------- /cheetah-demo/index.tmpl: -------------------------------------------------------------------------------- 1 | #include "master.tmpl" 2 | 3 | #attr NAME = "INDEX" 4 | 5 | #def body 6 | This is my site index page, see also other page.
7 | 8 | Oh, and also random = $random; 9 | #end def 10 | -------------------------------------------------------------------------------- /gae/check.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Check files with pyflakes 3 | 4 | export PYTHONPATH="$(./pypath.sh)" 5 | 6 | if [ $# -eq 0 ]; then 7 | files=*.py 8 | else 9 | files=$@ 10 | fi 11 | 12 | /opt/python2.5/bin/pyflakes $files 13 | -------------------------------------------------------------------------------- /partial-demo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from functools import partial 4 | import re 5 | remove_prefix = partial(re.compile("prefix: ").sub, "") 6 | print remove_prefix("prefix: hello") # 'hello' 7 | print remove_prefix("hello") # 'hello' 8 | -------------------------------------------------------------------------------- /present-timer/tick.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | t := time.NewTicker(100) 10 | for range t.C { 11 | t.Stop() 12 | fmt.Printf("bye ") 13 | } 14 | fmt.Println("bye") 15 | } 16 | -------------------------------------------------------------------------------- /tagcloud/Makefile: -------------------------------------------------------------------------------- 1 | all: cloud.html 2 | 3 | cloud.html: tagcloud.cgi tagcloud.mako 4 | ./tagcloud.cgi | grep -v 'Content-Type' > cloud.html 5 | 6 | clean: 7 | rm cloud.html 8 | 9 | fresh: clean all 10 | 11 | .PHONY: all clean fresh 12 | -------------------------------------------------------------------------------- /tagcloud/mako/__init__.py: -------------------------------------------------------------------------------- 1 | # __init__.py 2 | # Copyright (C) 2006, 2007 Michael Bayer mike_mp@zzzcomputing.com 3 | # 4 | # This module is part of Mako and is released under 5 | # the MIT License: http://www.opensource.org/licenses/mit-license.php 6 | 7 | -------------------------------------------------------------------------------- /haproxy-es/docker-compose.yml: -------------------------------------------------------------------------------- 1 | elastic: 2 | image: elasticsearch 3 | 4 | haproxy: 5 | image: haproxy 6 | volumes: 7 | - ${PWD}:/usr/local/etc/haproxy 8 | links: 9 | - elastic 10 | ports: 11 | - "9200:9200" 12 | -------------------------------------------------------------------------------- /pytest-cmp/mask.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | # passwd: 1234 4 | # password: 1234 5 | passwd_re = re.compile('^(passw(or)?d):.*$', re.MULTILINE) 6 | 7 | 8 | def mask(text): 9 | """Mask passwords in text""" 10 | return passwd_re.sub(r'\1: XXXX', text) 11 | -------------------------------------------------------------------------------- /go-from-c/Makefile: -------------------------------------------------------------------------------- 1 | all: hi 2 | 3 | hi: hi.a hi.c 4 | gcc -pthread -o hi hi.c hi.a 5 | 6 | hi.a: hi.go 7 | go build -buildmode=c-archive -o $@ $< 8 | 9 | clean: 10 | rm -f hi.a 11 | rm -f hi 12 | 13 | fresh: clean all 14 | 15 | .PHONY: all clean fresh 16 | -------------------------------------------------------------------------------- /autobuild-c-ext/greet/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import Extension, setup 2 | 3 | MODULE_NAME = "_greet" 4 | DYNLIB = MODULE_NAME + ".so" 5 | SRC_FILE = MODULE_NAME + ".c" 6 | 7 | if __name__ == "__main__": 8 | setup(ext_modules=[Extension(MODULE_NAME, [SRC_FILE])]) 9 | -------------------------------------------------------------------------------- /gae/run-local.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Run development server on given port, we listen on 0.0.0.0 so we can access it 3 | # from outside of localhost 4 | 5 | port=${1-8080} 6 | 7 | /opt/python2.5/bin/python \ 8 | /opt/google_appengine/dev_appserver.py -p $port -a 0.0.0.0 . 9 | -------------------------------------------------------------------------------- /git-hooks/post-checkout: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Update tags when checking out/pulling 3 | 4 | # Create a symlink for this file with the name of "post-merge" as well 5 | # cd .git/hooks && ln -s post-checkout post-merge 6 | 7 | cd $(dirname $(git rev-parse --git-dir)) 8 | ctags -R 9 | -------------------------------------------------------------------------------- /less-code/version-1/nfs-keep.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Keep NFS mount "warm"''' 3 | 4 | from os import listdir 5 | from time import sleep 6 | 7 | if __name__ == "__main__": 8 | while 1: 9 | listdir("/path/to/nfs") 10 | sleep(10 * 60 * 60) 11 | -------------------------------------------------------------------------------- /tag-ok-build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Error on 1'st failure 4 | set -e 5 | 6 | tag=last-green-build 7 | revision=$(git rev-parse HEAD) 8 | 9 | echo "Tagging $commit as $tag" 10 | git tag -f $tag $commit 11 | git pull origin master 12 | git push --tags origin master 13 | -------------------------------------------------------------------------------- /post-checkout: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # A post checkout hook that update tasks when switching branches 4 | # This assumes your project is sources are in `awesome` and tests are in `tests` 5 | # directories. 6 | 7 | # Copy this script to .git/hooks 8 | 9 | ctags -R awesome tests 10 | -------------------------------------------------------------------------------- /persist_id.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Persistant ID generator 3 | 4 | from collections import defaultdict 5 | from itertools import count 6 | 7 | vector_index = defaultdict(count(0).next) 8 | print vector_index["a"] # 0 9 | print vector_index["a"] # 0 10 | print vector_index["b"] # 1 11 | -------------------------------------------------------------------------------- /.hgignore: -------------------------------------------------------------------------------- 1 | syntax: glob 2 | 3 | glue/lighttpd.conf 4 | glue/lighttpd.pid 5 | dynamic-static/charts/*.png 6 | dynamic-static/lighttpd.inc 7 | dynamic-static/lighttpd.pid 8 | macapp/HumbleCalc.* 9 | pandas-validation/points.h5 10 | venv 11 | go-asset/httpd 12 | go-asset/image.go 13 | withopen.png 14 | hard-bp 15 | -------------------------------------------------------------------------------- /bbembed: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | case $1 in 4 | -h | --help ) echo "usage: $(basename $0) FILE"; exit;; 5 | esac 6 | 7 | if [ $# -ne 1 ]; then 8 | $0 -h 9 | exit 1 10 | fi 11 | 12 | cat << EOF | xsel -b 13 | 14 | EOF 15 | -------------------------------------------------------------------------------- /book/header.tex: -------------------------------------------------------------------------------- 1 | % Have figures generated where they are defined 2 | % Taken from http://bit.ly/2vudAos 3 | 4 | \usepackage{float} 5 | \let\origfigure\figure 6 | \let\endorigfigure\endfigure 7 | \renewenvironment{figure}[1][2] { 8 | \expandafter\origfigure\expandafter[H] 9 | } { 10 | \endorigfigure 11 | } 12 | -------------------------------------------------------------------------------- /default_vs_none.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass 2 | 3 | 4 | @dataclass 5 | class Location: 6 | x: int 7 | y: int 8 | 9 | 10 | def get_default(locs, name): 11 | return locs.get(name, Location(0, 0)) 12 | 13 | 14 | def get_none(locs, name): 15 | return locs.get(name) or Location(0, 0) 16 | -------------------------------------------------------------------------------- /e: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Run nvim in new terminal window""" 3 | 4 | from subprocess import Popen 5 | from sys import argv 6 | from shlex import quote 7 | 8 | cmd = [ 9 | 'xfce4-terminal', 10 | '-T', 'nvim', 11 | '-e', ' '.join(['nvim'] + [quote(arg) for arg in argv[1:]]) 12 | ] 13 | Popen(cmd) 14 | -------------------------------------------------------------------------------- /powerset.py: -------------------------------------------------------------------------------- 1 | def power_set(iterable): 2 | """Yields the power set of iterable (including the empty set)""" 3 | items = list(iterable) 4 | nitems = len(items) 5 | for n in range(2**nitems): 6 | # Use n as bitmask to pick items 7 | yield [items[bit] for bit in range(nitems) if (n >> bit) & 1] 8 | -------------------------------------------------------------------------------- /rst/Makefile: -------------------------------------------------------------------------------- 1 | %.html: %.txt 2 | rst2html.py --stylesheet style.css $< $@ 3 | 4 | %.pdf: %.tex 5 | pdflatex $< 6 | 7 | %.tex: %.txt 8 | rst2latex.py $< $@ 9 | 10 | all: hw.html hw.pdf 11 | 12 | clean: 13 | rm -f *.aux *.log *.pdf *.html *.out 14 | 15 | fresh: clean all 16 | 17 | .PHONY: all clean fresh 18 | -------------------------------------------------------------------------------- /pandas-validation/bootstrap.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Create databases and populate with dummy data 3 | # Note: You might need to set PGUSER environment variable (probably to postgres) 4 | 5 | for step in pre post; do 6 | db=points_${step} 7 | createdb $db 8 | psql -f schema.sql $db 9 | done 10 | ./populate.py 11 | -------------------------------------------------------------------------------- /result/nmath.go: -------------------------------------------------------------------------------- 1 | package nmath 2 | 3 | // #include "nsqrt.h" 4 | import "C" 5 | 6 | // Sqrt returns square root of n 7 | func Sqrt(n float64) (float64, error) { 8 | res := NewResult(C.nsqrt(C.double(n))) 9 | if err := res.Err(); err != nil { 10 | return 0, err 11 | } 12 | 13 | return res.Float(), nil 14 | } 15 | -------------------------------------------------------------------------------- /cheetah-demo/Cheetah/Unspecified.py: -------------------------------------------------------------------------------- 1 | try: 2 | from ds.sys.Unspecified import Unspecified 3 | except ImportError: 4 | class _Unspecified: 5 | def __repr__(self): 6 | return 'Unspecified' 7 | def __str__(self): 8 | return 'Unspecified' 9 | Unspecified = _Unspecified() 10 | -------------------------------------------------------------------------------- /numphone: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Convert phone numbers to numeric (1-800-T-MOBILE -> 1-800-8-662453) 3 | 4 | if [ $# -ne 1 ]; then 5 | echo "usage: $(basename $0) PHONE-NUMBER" 6 | exit 1 7 | fi 8 | 9 | echo $1 | \ 10 | tr '[:lower:]' '[:upper:]' | \ 11 | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ 22233344455566677778889999 12 | 13 | -------------------------------------------------------------------------------- /gae/repl.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Run ipython repl (in a new gnome terminal) with right PYTHONPATH for AppEngine 3 | 4 | export PYTHONPATH="$(./pypath.sh)" 5 | 6 | # GUI mode 7 | if [ "$1" != "--no-fork" ]; then 8 | gnome-terminal -t '*PYTHON*' -x $0 --no-fork $*& 9 | exit 10 | fi 11 | 12 | shift 13 | /opt/python2.5/bin/ipython 14 | -------------------------------------------------------------------------------- /hardbreak.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | __author__ = "Miki Tebeka " 4 | 5 | '''*Hard* breakpoints''' 6 | from pdb import set_trace 7 | 8 | def buggy_function(): 9 | pass 10 | pass 11 | set_trace() # Break here 12 | pass 13 | pass 14 | 15 | if __name__ == "__main__": 16 | buggy_function() 17 | -------------------------------------------------------------------------------- /next_n.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from itertools import ifilter, islice 4 | 5 | def next_n(items, pred, count): 6 | return islice(ifilter(pred, items), count) 7 | 8 | if __name__ == "__main__": 9 | from gmpy import is_prime 10 | from itertools import count 11 | for prime in next_n(count(1), is_prime, 10): 12 | print prime 13 | -------------------------------------------------------------------------------- /Makfile.test: -------------------------------------------------------------------------------- 1 | # Example test rule in makefile 2 | 3 | test: 4 | # Clean .pyc 5 | find . -name '*.py[co]' -exec rm {} \; 6 | # Check for pdb calls 7 | test -z "$(shell find . -name '*.py' -exec grep pdb.set_trace {} \;)" 8 | flake8 . 9 | PYTHONPATH=$(PWD) python -m py.test -v tests 10 | 11 | .PHONY: all run deploy update-data test 12 | 13 | # vim:ft=make 14 | -------------------------------------------------------------------------------- /go-protobuf-json/job.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package pb; 3 | 4 | // A value of several possible types 5 | message Value { 6 | oneof value { 7 | int64 int = 1; 8 | string str = 2; 9 | } 10 | } 11 | 12 | // A job to execute 13 | message Job { 14 | string user = 1; 15 | int64 count = 2; 16 | map properties = 3; 17 | } 18 | -------------------------------------------------------------------------------- /source.py: -------------------------------------------------------------------------------- 1 | from subprocess import Popen, PIPE 2 | from os import environ 3 | 4 | def source(script, update=1): 5 | pipe = Popen(". %s; env" % script, stdout=PIPE, shell=True) 6 | data = pipe.communicate()[0] 7 | 8 | env = dict((line.split("=", 1) for line in data.splitlines())) 9 | if update: 10 | environ.update(env) 11 | 12 | return env 13 | -------------------------------------------------------------------------------- /genpatch: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Generate svn style diff for current hg feature branch 3 | 4 | branch=$(hg branch) 5 | if [ -z $branch ]; then 6 | echo "error: not a mercurial repo" 1>&2 7 | exit 1 8 | fi 9 | 10 | if [ "$branch" == "default" ]; then 11 | echo "error: in default branch" 1>&2 12 | exit 1 13 | fi 14 | 15 | hg diff --svn -r default > ${branch}.patch 16 | -------------------------------------------------------------------------------- /present-timer/sleep-sort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | "time" 7 | ) 8 | 9 | func main() { 10 | var wg sync.WaitGroup 11 | for _, n := range []int{3, 1, 2} { 12 | wg.Add(1) 13 | go func() { 14 | defer wg.Done() 15 | time.Sleep(time.Duration(n) * time.Millisecond) 16 | fmt.Printf("%d ", n) 17 | }() 18 | } 19 | wg.Wait() 20 | } 21 | -------------------------------------------------------------------------------- /book/Makefile: -------------------------------------------------------------------------------- 1 | book: book.pdf 2 | 3 | book.pdf: book-flat.md 4 | pandoc $< \ 5 | -V links-as-notes \ 6 | --toc \ 7 | --include-in-header header.tex \ 8 | -t latex \ 9 | -o $@ 10 | 11 | book-flat.md: book.md 12 | awk -f flatten.awk < $< > $@ 13 | 14 | clean: 15 | rm -f book.pdf book-flat.md 16 | 17 | fresh: clean book 18 | 19 | .PHONY: book clean fresh 20 | -------------------------------------------------------------------------------- /ctxdec/README.md: -------------------------------------------------------------------------------- 1 | A workshop on 2 | [decorators](http://docs.python.org/2/glossary.html#term-decorator) and [context 3 | managers](http://www.python.org/dev/peps/pep-0343/). 4 | 5 | Run the ipython server using `run-notebook.sh`. Then hit 6 | [http://localhost:8888](http://localhost:8888). 7 | 8 | See [here](http://ipython.org/notebook.html) for how to work with IPython 9 | notebooks. 10 | -------------------------------------------------------------------------------- /sbcl: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # From http://www.cliki.net/rlwrap 3 | # See above URL on how to generate ${HOME}/.sbcl_completions 4 | 5 | BREAK_CHARS="(){}[],^%$#@\"\";''|\\" 6 | rlwrap \ 7 | --remember \ 8 | --history-filename=${HOME}/.sbcl_history \ 9 | --histsize=1000000 \ 10 | -c \ 11 | -b ${BREAK_CHARS} \ 12 | -f ${HOME}/.sbcl_completions \ 13 | /usr/bin/sbcl $@ 14 | -------------------------------------------------------------------------------- /dmd-mail/.sup/sources.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - !supmua.org,2006-10-01/Redwood/Maildir 3 | uri: maildir:///Users/bugsbunny/Mail 4 | usual: true 5 | archived: false 6 | id: 1 7 | labels: [] 8 | - !supmua.org,2006-10-01/Redwood/MBox 9 | uri: mbox:///var/mail/bugsbunny 10 | usual: true 11 | archived: false 12 | id: 2 13 | labels: [] 14 | - !supmua.org,2006-10-01/Redwood/SentLoader {} 15 | -------------------------------------------------------------------------------- /gen-bbd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Generate "Bounding Box Diagonal" image 3 | 4 | convert \ 5 | -size 400x400 \ 6 | xc:skyblue \ 7 | -fill red \ 8 | -draw "circle 200,200 0,200" \ 9 | -stroke black \ 10 | -strokewidth 2 \ 11 | -draw "line 0,0 400,400" \ 12 | -stroke skyblue \ 13 | -pointsize 40 \ 14 | -draw "rotate 45 text 250,10 \"bbd\"" \ 15 | bbd.png 16 | -------------------------------------------------------------------------------- /today: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Launch wikipedia page for today''' 3 | 4 | __author__ = "Miki Tebkea " 5 | 6 | from time import strftime 7 | import webbrowser 8 | 9 | day = strftime("%d") 10 | # Change 06 -> 6 11 | if day.startswith("0"): 12 | day = day[1] 13 | 14 | url = "http://en.wikipedia.org/wiki/" + strftime("%B") + "_" + day 15 | webbrowser.open(url) 16 | -------------------------------------------------------------------------------- /cheetah-demo/index.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/python 2 | 3 | from Cheetah.Template import Template 4 | 5 | from random import randint 6 | 7 | 8 | def main(): 9 | random = randint(0, 100) 10 | 11 | print "Content-Type: text/html" 12 | print 13 | 14 | page = Template(file="index.tmpl", searchList=[locals()]) 15 | print page.respond() 16 | 17 | if __name__ == "__main__": 18 | main() 19 | -------------------------------------------------------------------------------- /test_data.py: -------------------------------------------------------------------------------- 1 | # Example on how to use generators with nose for data driven tests 2 | 3 | def check_mul(a, b, result): 4 | assert a * b == result, "{0}*{1} != {2}".format(a, b, result) 5 | 6 | def test_mul(): 7 | cases = [ 8 | [2, 2, 4], 9 | [-2, 2, -4], 10 | [0.1, 10, 1], 11 | ] 12 | for a, b, result in cases: 13 | yield check_mul, a, b, result 14 | 15 | -------------------------------------------------------------------------------- /gae/pypath.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Utility script that print PYTHONPATH for working with AppEngine 3 | 4 | root="/opt/google_appengine" 5 | pypath="" 6 | for dir in ${root}/lib/*; do 7 | inner="${dir}/lib" 8 | if [ -d "$inner" ]; then 9 | pypath="${pypath}${inner}:" 10 | else 11 | pypath="${pypath}${dir}:" 12 | fi 13 | done 14 | 15 | echo "${root}:${pypath}:${PYTHONPATH}" 16 | -------------------------------------------------------------------------------- /deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Deploy, meaning sync from last successful build 3 | 4 | tag=last-green-build 5 | 6 | # Fetch get the latest changes from remote but does not update working 7 | # directory, just the index 8 | git fetch --tags 9 | 10 | # Merge to the last successful bulid 11 | git merge ${tag} 12 | 13 | # Tag currently deployed revision 14 | git tag -f deployed ${tag} 15 | git push --tags 16 | 17 | -------------------------------------------------------------------------------- /initgae.py: -------------------------------------------------------------------------------- 1 | """Initianlize GAE Python environment so you can work with it from the REPL""" 2 | 3 | from os import environ 4 | import sys 5 | 6 | sys.path.insert(0, environ.get('GAE_PY_SDK', '/opt/google_appengine')) 7 | 8 | import dev_appserver # noqa 9 | dev_appserver.fix_sys_path() 10 | 11 | from google.appengine.ext import testbed # noqa 12 | 13 | tb = testbed.Testbed() 14 | tb.activate() 15 | tb.init_all_stubs() 16 | -------------------------------------------------------------------------------- /httpc.py: -------------------------------------------------------------------------------- 1 | from socket import socket 2 | from functools import partial 3 | 4 | host = 'www.353solutions.com' 5 | 6 | payload = f'''\ 7 | GET / HTTP/1.1 8 | Host: {host} 9 | Connection: close 10 | 11 | ''' 12 | 13 | with socket() as sock: 14 | sock.connect((host, 80)) 15 | sock.sendall(payload.encode('utf-8')) 16 | 17 | data = b''.join(iter(partial(sock.recv, 1024), b'')) 18 | 19 | 20 | print(data.decode('utf-8')[:400]) 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PythonWise 2 | 3 | **I no longer write in the Pythonwise blog. You can find my writing at https://www.ardanlabs.com/blog/ or https://medium.com/@tebeka** 4 | 5 | This is the code from [pythonwise.blogspot.com](pythonwise.blogspot.com). 6 | 7 | Feel free to look around and use what you think is useful. 8 | 9 | Enjoy - [Miki](mailto:miki.tebeka@gmail.com) 10 | 11 | --- 12 | Note: I don't accept pull requests to this repo. 13 | -------------------------------------------------------------------------------- /go-from-c/hi.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "C" 5 | "fmt" 6 | "sync" 7 | "time" 8 | ) 9 | 10 | //export Hi 11 | func Hi(name string) { 12 | fmt.Printf("Hi %s\n", name) 13 | var wg sync.WaitGroup 14 | for i := 0; i < 10; i++ { 15 | wg.Add(1) 16 | go func(n int) { 17 | defer wg.Done() 18 | time.Sleep(time.Second) 19 | fmt.Printf("%d done\n", n) 20 | 21 | }(i) 22 | } 23 | wg.Wait() 24 | } 25 | 26 | func main() {} 27 | -------------------------------------------------------------------------------- /run-job.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | jars=avro-1.6.0-SNAPSHOT.jar,avro-mapred-1.6.0-SNAPSHOT.jar 4 | 5 | hadoop jar hadoop-streaming-0.20.2-cdh3u0.jar \ 6 | -files $jars \ 7 | -libjars $jars \ 8 | -input /in/avro \ 9 | -output /out/avro \ 10 | -mapper avro-mapper.py \ 11 | -reducer avro-reducer.py \ 12 | -file avro-mapper.py \ 13 | -file avro-reducer.py \ 14 | -inputformat org.apache.avro.mapred.AvroAsTextInputFormat 15 | -------------------------------------------------------------------------------- /sql-col-by-name.py: -------------------------------------------------------------------------------- 1 | '''Get columns by name from SQL query''' 2 | 3 | # sqlite3 4 | import sqlite3 5 | db = sqlite3.connect(':memory:') 6 | db.row_factory = sqlite3.Row 7 | 8 | # psycopg2 9 | import psycopg2 10 | from psycopg2.extras import DictCursor 11 | db = psycopg2.connect('my-dbn-string') 12 | cur = db.cursor(cursor_factory=DictCursor) 13 | 14 | # Then 15 | cur.execute('select * from people') 16 | for row in cur: 17 | print(row['name']) 18 | -------------------------------------------------------------------------------- /indent.py: -------------------------------------------------------------------------------- 1 | def do_cool_stuff(n): 2 | print "%s is cool" % n 3 | 4 | def positive_logic(x): 5 | if x > 2: 6 | y = f(x) 7 | if y < 10: 8 | g = f(y) 9 | if g > 0: 10 | do_cool_stuff(g) 11 | 12 | def negative_logic(x): 13 | if x <= 2: 14 | return 15 | y = f(x) 16 | if y >= 10: 17 | return 18 | g = f(y) 19 | if g <= 0: 20 | return 21 | do_cool_stuff(g) 22 | -------------------------------------------------------------------------------- /result/result.c: -------------------------------------------------------------------------------- 1 | #include "result.h" 2 | 3 | #include 4 | 5 | const char *result_str(result_t res) { 6 | return res.sval; 7 | } 8 | 9 | double result_float(result_t res) { 10 | return res.fval; 11 | } 12 | 13 | long long result_int(result_t res) { 14 | return res.ival; 15 | } 16 | 17 | void *result_ptr(result_t res) { 18 | return res.ptr; 19 | } 20 | 21 | void set_result_err(result_t *r, const char *err) { 22 | r->err = strdup(err); 23 | } 24 | -------------------------------------------------------------------------------- /present-timer/err.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type OSError int 6 | 7 | func (e *OSError) Error() string { 8 | return fmt.Sprintf("error #%d", *e) 9 | } 10 | 11 | func FileExists(path string) (bool, error) { 12 | var err *OSError 13 | return false, err // TODO 14 | } 15 | 16 | func main() { 17 | if _, err := FileExists("/no/such/file"); err != nil { 18 | fmt.Printf("error: %s\n", err) 19 | } else { 20 | fmt.Println("OK") 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /cheetah-demo/master.tmpl: -------------------------------------------------------------------------------- 1 | #from time import ctime 2 | 3 | #attr NAME = "???" 4 | 5 | #def head 6 | #end def 7 | 8 | #def body 9 | OOOPS, head will roll... 10 | #end def 11 | 12 | 13 | 14 | 15 | $head 16 | 17 | 18 |
My Wonderful Site - $NAME
19 | $body 20 | 21 |
22 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /py_defer/test_defer.py: -------------------------------------------------------------------------------- 1 | from subprocess import Popen 2 | 3 | import pytest 4 | 5 | 6 | @pytest.fixture 7 | def defer(): 8 | funcs = [] 9 | 10 | yield funcs 11 | 12 | for fn in reversed(funcs): 13 | try: 14 | fn() 15 | except Exception as err: 16 | print(f'error: {err}') 17 | 18 | 19 | def test_kill(defer): 20 | p = Popen(['sleep', '1000']) # simulate server 21 | print(p.pid) 22 | defer.append(p.kill) 23 | -------------------------------------------------------------------------------- /redirect.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Pythonwise knows. 5 | 6 | 7 | 18 | 19 | -------------------------------------------------------------------------------- /clickreg/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Pythonwise knows. 5 | 6 | 7 | 18 | 19 | -------------------------------------------------------------------------------- /go-asset/Makefile: -------------------------------------------------------------------------------- 1 | httpd: httpd.go image.go 2 | go build -o $@ 3 | 4 | clean: 5 | rm -f httpd 6 | rm -f image.go 7 | 8 | fresh: clean httpd 9 | 10 | # Create image byte array using xxd, the sed command adds last comma 11 | image.go: image.png 12 | echo $< 13 | echo 'package main' > $@ 14 | echo 'var image = []byte{' >> $@ 15 | xxd -i $< | egrep -i '^[[:space:]]*0x' | sed 's/\([^,]\)$$/\1,/' >> $@ 16 | echo '}' >> $@ 17 | go fmt $@ 18 | 19 | .PHONY: clean fresh 20 | -------------------------------------------------------------------------------- /odbc-osx.py: -------------------------------------------------------------------------------- 1 | '''Using pyodbc with Netezza example''' 2 | import pyodbc 3 | 4 | user = '"daffy.duck"' # Need quotes 5 | password = 'ThisMeansWar' 6 | server = 'planet.looney' 7 | database = 'bugs' 8 | 9 | dsn = 'DRIVER=\{NetezzaSQL\};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s' % (server, database, user, password) 10 | conn = pyodbc.connect(dsn) 11 | 12 | with conn.cursor() as cur: 13 | cur.execute("select * from BUGS..MONEY") 14 | for row in cur: 15 | print(row) 16 | -------------------------------------------------------------------------------- /hg-close: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Close a branch in mercurial 3 | 4 | case $1 in 5 | -h | --help ) echo "usage: $(basename $0) BRANCH_TO_CLOSE [COMMENT]"; exit;; 6 | esac 7 | 8 | if [ $# -lt 1 ] || [ $# -gt 2 ]; then 9 | echo "error: wrong number of arguments" 1>&2 10 | exit 1 11 | fi 12 | 13 | 14 | comment=${2-Closing $1} 15 | 16 | # Exit on first error 17 | set -e 18 | current=$(hg branch) 19 | hg co -q -C $1 20 | hg ci --close-branch -m "${comment}" 21 | hg co -q -C $current 22 | -------------------------------------------------------------------------------- /dd-demo.py: -------------------------------------------------------------------------------- 1 | '''defaultdict demo''' 2 | 3 | from collections import defaultdict 4 | 5 | def histogram(text): 6 | histogram = defaultdict(int) # int() -> 0 7 | 8 | for word in text.split(): 9 | histogram[word] += 1 10 | 11 | return histogram 12 | 13 | def location_histogram(text): 14 | histogram = defaultdict(list) # list() -> [] 15 | 16 | for location, word in enumerate(text.split()): 17 | histogram[word].append(location) 18 | 19 | return histogram 20 | -------------------------------------------------------------------------------- /ipy.scpt: -------------------------------------------------------------------------------- 1 | #!/usr/bin/osascript 2 | 3 | -- Run ipython on a new iTerm (OS X version) 4 | -- See http://www.iterm2.com/#/section/documentation/scripting 5 | 6 | tell application "iTerm" 7 | activate 8 | set ipyterm to (make new terminal) 9 | tell ipyterm 10 | set ipysession to (make new session) 11 | tell ipysession 12 | set name to "IPython" 13 | exec command "/opt/local/bin/ipython" 14 | end tell 15 | end tell 16 | end tell 17 | 18 | 19 | -------------------------------------------------------------------------------- /macapp/Makefile: -------------------------------------------------------------------------------- 1 | # Create an applicaiton bundle and pack it in a zip file 2 | APP = HumbleCalc.app 3 | ZIP = HumbleCalc.zip 4 | 5 | all: zip 6 | 7 | zip: app 8 | zip -r9 $(ZIP) $(APP) 9 | 10 | app: 11 | mkdir -p $(APP)/Contents/MacOS 12 | mkdir -p $(APP)/Resources 13 | cp humblecalc.py $(APP)/Contents/MacOS 14 | cp humblecalc.ico $(APP)/Resources 15 | cp Info.plist $(APP)/Contents 16 | 17 | clean: 18 | rm -fr $(APP) 19 | rm -f $(ZIP) 20 | 21 | fresh: clean all 22 | 23 | .PHONY: all app clean fresh 24 | -------------------------------------------------------------------------------- /result/result.h: -------------------------------------------------------------------------------- 1 | #ifndef RESULT_H 2 | #define RESULT_H 3 | 4 | typedef struct { 5 | const char *err; 6 | union { 7 | const char *sval; 8 | double fval; 9 | long long ival; 10 | void *ptr; 11 | }; 12 | } result_t; 13 | 14 | void set_result_err(result_t *r, const char *err); 15 | 16 | // Access to union fields from Go 17 | const char *result_str(result_t); 18 | double result_float(result_t); 19 | long long result_int(result_t); 20 | void *result_ptr(result_t); 21 | 22 | #endif // RESULT_H 23 | -------------------------------------------------------------------------------- /slice.py: -------------------------------------------------------------------------------- 1 | # Example on using slice object for indexing 2 | 3 | 4 | def fn1(items, use_tail=False): 5 | if use_tail: 6 | key = items[-2:] 7 | else: 8 | key = items[0] 9 | 10 | print(key) 11 | 12 | 13 | items = [1, 2, 3, 4, 5] 14 | fn1(items) 15 | fn1(items, True) 16 | 17 | 18 | def fn2(items, use_tail=False): 19 | idx = slice(-2, None) if use_tail else 0 20 | key = items[idx] 21 | 22 | print(key) 23 | 24 | 25 | items = [1, 2, 3, 4, 5] 26 | fn2(items) 27 | fn2(items, True) 28 | -------------------------------------------------------------------------------- /cheetah-demo/Cheetah/Utils/htmlDecode.py: -------------------------------------------------------------------------------- 1 | """This is a copy of the htmlDecode function in Webware. 2 | 3 | @@TR: It implemented more efficiently. 4 | 5 | """ 6 | 7 | from Cheetah.Utils.htmlEncode import htmlCodesReversed 8 | 9 | def htmlDecode(s, codes=htmlCodesReversed): 10 | """ Returns the ASCII decoded version of the given HTML string. This does 11 | NOT remove normal HTML tags like

. It is the inverse of htmlEncode().""" 12 | for code in codes: 13 | s = s.replace(code[1], code[0]) 14 | return s 15 | -------------------------------------------------------------------------------- /couchnuke: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Delete all databases on a couchdb server 4 | 5 | # Exit on 1'st error 6 | set -e 7 | 8 | if [ $# -gt 1 ]; then 9 | echo "usage: $(basename $0) [DB_URL]" 10 | exit 1 11 | fi 12 | 13 | URL=${1-http://localhost:5984} 14 | 15 | echo -n "Delete *ALL* databases on $URL? [y/n] " 16 | read ans 17 | if [ "$ans" != "y" ]; then 18 | exit 19 | fi 20 | 21 | for db in $(curl -s $URL/_all_dbs | tr '",[]' ' \n '); 22 | do 23 | echo "Deleting $db" 24 | curl -X DELETE $URL/$db 25 | done 26 | -------------------------------------------------------------------------------- /haproxy-es/haproxy.cfg: -------------------------------------------------------------------------------- 1 | # Based on http://j.mp/1YHQFgZ 2 | # vim: ft=haproxy 3 | 4 | defaults 5 | timeout connect 5000 6 | timeout client 10000 7 | timeout server 10000 8 | 9 | frontend elastic 10 | bind :9200 11 | mode http 12 | acl is_delete method DELETE 13 | http-request deny if is_delete 14 | default_backend elastic 15 | 16 | backend elastic 17 | mode http 18 | option forwardfor 19 | balance source 20 | option httpclose 21 | server es1 elastic:9200 weight 1 check inter 1000 rise 5 fall 1 22 | -------------------------------------------------------------------------------- /httpserve: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Quickly serve files over HTTP 3 | 4 | # Miki Tebeka 5 | 6 | usage="usage: `basename $0` PATH [PORT]" 7 | 8 | if [ $# -ne 1 ] && [ $# -ne 2 ]; then 9 | echo $usage >&2 10 | exit 1 11 | fi 12 | 13 | case $1 in 14 | "-h" | "-H" | "--help" ) echo $usage; exit;; 15 | * ) path=$1; port=$2;; 16 | esac 17 | 18 | if [ ! -d $path ]; then 19 | echo "error: $path is not a directory" >&2 20 | exit 1 21 | fi 22 | 23 | cd $path 24 | python -m SimpleHTTPServer $port 25 | -------------------------------------------------------------------------------- /unique.py: -------------------------------------------------------------------------------- 1 | def unique(items): 2 | '''Remove duplicate items from a sequence, preserving order 3 | 4 | >>> unique([1, 2, 3, 2, 1, 4, 2]) 5 | [1, 2, 3, 4] 6 | >>> unique([2, 2, 2, 1, 1, 1]) 7 | [2, 1] 8 | >>> unique([1, 2, 3, 4]) 9 | [1, 2, 3, 4] 10 | >>> unique([]) 11 | [] 12 | ''' 13 | seen = set() 14 | 15 | def is_new(obj, seen=seen, add=seen.add): 16 | if obj in seen: 17 | return 0 18 | add(obj) 19 | return 1 20 | 21 | return filter(is_new, items) 22 | -------------------------------------------------------------------------------- /animals.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | class Animal(object): 4 | pass 5 | 6 | class Dog(Animal): 7 | def talk(self): 8 | return "whoof whoof" 9 | 10 | class Cat(Animal): 11 | def talk(self): 12 | return "miao" 13 | 14 | class Pig(Animal): 15 | def talk(self): 16 | return "oink oink" 17 | 18 | def all_animals(): 19 | return Animal.__subclasses__() 20 | 21 | if __name__ == "__main__": 22 | for animal in all_animals(): 23 | print "%s says: %s" % (animal.__name__, animal().talk()) 24 | -------------------------------------------------------------------------------- /wait_for_server.go: -------------------------------------------------------------------------------- 1 | package lassie 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "time" 7 | ) 8 | 9 | func waitForServer(URL string, timeout time.Duration) error { 10 | ch := make(chan bool) 11 | go func() { 12 | for { 13 | _, err := http.Get(URL) 14 | if err == nil { 15 | ch <- true 16 | } 17 | time.Sleep(10 * time.Millisecond) 18 | } 19 | }() 20 | 21 | select { 22 | case <-ch: 23 | return nil 24 | case <-time.After(timeout): 25 | return fmt.Errorf("server did not reply after %v", timeout) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /rootdir.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # If you're using Python, you can just write from root import ROOT_DIR, however 3 | # if you're using bash you can write ROOTDIR=`./rootdir.py` and then use 4 | # $ROOTDIR 5 | 6 | from os import environ 7 | from os.path import join 8 | from getpass import getuser 9 | 10 | if "DEBUG" in environ: 11 | # Every user has his/her own test root directory 12 | ROOT_DIR = join("/tmp", "testing", getuser()) 13 | else: 14 | ROOT_DIR = "/usr/local/cool" 15 | 16 | if __name__ == "__main__": 17 | print ROOT_DIR 18 | -------------------------------------------------------------------------------- /colorlog.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Color log lines according to level 3 | 4 | case $1 in 5 | -h | --help ) echo "usage: $(basename $0)"; exit;; 6 | esac 7 | 8 | if [ $# -ne 0 ]; then 9 | 2>&1 echo "error: wrong number of arguments" 10 | exit 1 11 | fi 12 | 13 | awk ' 14 | /INFO/ {print "\033[32m" $0 "\033[39m"; next} 15 | /WARNING/ {print "\033[33m" $0 "\033[39m"; next} 16 | /CRITICAL/ {print "\033[31m" $0 "\033[39m"; next} 17 | /FATAL/ {print "\033[31m" "\033[1m" $0 "\033[0m" "\033[39m"; next} 18 | // {print $0} 19 | ' 20 | -------------------------------------------------------------------------------- /f.py: -------------------------------------------------------------------------------- 1 | """f-strings in Python 2""" 2 | from inspect import currentframe, getouterframes 3 | 4 | 5 | # Sadly, Python 2 doesn't have ChainMap 6 | def chain_maps(*maps): 7 | cm = {} 8 | for m in reversed(maps): 9 | cm.update(m) 10 | return cm 11 | 12 | 13 | def f(s): 14 | """Format s with parameter from environment 15 | 16 | >>> x = 123 17 | >>> f('x = {x}') 18 | 'x = 123' 19 | """ 20 | caller = getouterframes(currentframe(), 2)[1][0] 21 | return s.format(**chain_maps(caller.f_locals, caller.f_globals)) 22 | -------------------------------------------------------------------------------- /multiline-regexp.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import re 3 | 4 | # 2007-04-01 11:20 5 | find_time = re.compile( 6 | "(?P\d{4})" # 4 digit year 7 | "-" 8 | "(?P\d{2})" # 2 digit month 9 | "-" 10 | "(?P\d{2})" # 2 digit day 11 | "\s+" # white space(s) 12 | "(?P\d{2})" # 2 digit hour 13 | ":" 14 | "(?P\d{2})" # 2 digit minute 15 | ).search 16 | 17 | match = find_time("The exact time is 2007-04-01 11:20.") 18 | assert match, "can't find time" 19 | print "MONTH: %s" % match.group("month") 20 | -------------------------------------------------------------------------------- /flatten.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | __author__ = "Miki Tebeka " 4 | 5 | def flatten(items): 6 | '''Flatten a nested list. 7 | 8 | >>> a = [[1], 2, [[[3]], 4]] 9 | >>> list(flatten(a)) 10 | [1, 2, 3, 4] 11 | >>> 12 | ''' 13 | for item in items: 14 | if getattr(item, "__iter__", None): 15 | for subitem in flatten(item): 16 | yield subitem 17 | else: 18 | yield item 19 | 20 | if __name__ == "__main__": 21 | from doctest import testmod 22 | testmod() 23 | -------------------------------------------------------------------------------- /translate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Use Google to translate from English to Hebrew 4 | 5 | # Based on 6 | # http://www.commandlinefu.com/commands/view/5516/google-translate 7 | 8 | if [ $# -ne 1 ]; then 9 | echo "usage: $(basename $0) WORD" 10 | exit 1 11 | fi 12 | 13 | case $1 in 14 | -h|--help ) echo "usage $(basename $0) WORD"; exit;; 15 | esac 16 | 17 | url="http://ajax.googleapis.com/ajax/services/language/translate?v=1.0" 18 | url="${url}&langpair=en|he&q=$1" 19 | curl -s "$url" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/' | \ 20 | fribidi --nopad 21 | -------------------------------------------------------------------------------- /book/timed.py: -------------------------------------------------------------------------------- 1 | from time import monotonic, sleep 2 | from functools import wraps 3 | 4 | 5 | def timed(fn): 6 | """Timing decorator""" 7 | @wraps(fn) 8 | def wrapper(*args, **kw): 9 | start = monotonic() 10 | try: 11 | return fn(*args, **kw) 12 | finally: 13 | duration = monotonic() - start 14 | print('{} took {:.2f}sec'.format(fn.__name__, duration)) 15 | return wrapper 16 | 17 | 18 | @timed 19 | def add(a, b): 20 | """Does addition""" 21 | sleep(a/10.0) # Simulate work 22 | return a + b 23 | -------------------------------------------------------------------------------- /blogmentize: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Generate HTML of code to blog 3 | 4 | # Miki Tebeka 5 | 6 | if [ $# -lt 1 ] || [ $# -gt 2 ]; then 7 | echo "usage: $0 FILE [TYPE]" 8 | exit 1 9 | fi 10 | 11 | filename=$1 12 | if [ ! -f "$filename" ]; then 13 | echo "error: can't find $filename" 14 | exit 1 15 | fi 16 | 17 | type=$2 18 | if [ -z "$type" ]; then 19 | # "#!/usr/bin/env python" -> "python" 20 | type=$(head -1 "$filename" | xargs -i basename {} | 21 | sed -e 's/env //' -e 's/[<>]//g') 22 | fi 23 | 24 | pygmentize -l $type -f html "$filename" 25 | -------------------------------------------------------------------------------- /rotate.py: -------------------------------------------------------------------------------- 1 | from operator import itemgetter 2 | from itertools import imap, chain, repeat 3 | 4 | def rotate(matrix): 5 | '''Rotate matrix 90 degrees''' 6 | def row(row_num): 7 | return map(itemgetter(row_num), matrix) 8 | 9 | return map(row, range(len(matrix[0]))) 10 | 11 | def stretch(items, times): 12 | '''stretch([1,2], 3) -> [1,1,1,2,2,2]''' 13 | return reduce(add, map(lambda item: [item] * times, items), []) 14 | 15 | def istretch(items, count): 16 | '''istretch([1,2], 3) -> [1,1,1,2,2,2] (generator)''' 17 | return chain(*imap(lambda i: repeat(i, count), items)) 18 | -------------------------------------------------------------------------------- /tmp-couch: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # A script to run a temporary couchdb instance 3 | 4 | if [ $# -ne 1 ]; then 5 | echo "usage: $(basename $0) PORT" 6 | exit 1 7 | fi 8 | 9 | port=$1 10 | 11 | base=$(mktemp -d) 12 | dbdir=$base/data 13 | config=$base/config.ini 14 | mkdir -p $dbdir 15 | 16 | cat < $config 17 | [httpd] 18 | port = $port 19 | 20 | [couchdb] 21 | database_dir = $dbdir 22 | view_index_dir = $dbdir 23 | 24 | [log] 25 | file = ${base}/couch.log 26 | EOF 27 | 28 | trap "rm -fr $base" SIGINT SIGTERM 29 | 30 | echo "couchdb directory is $base" 31 | couchdb -a $config 32 | 33 | -------------------------------------------------------------------------------- /websession/index.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from websession import WebSession 4 | 5 | def main(): 6 | 7 | session = WebSession() 8 | 9 | key = 'key' 10 | old = session.get(key, 0) 11 | new = old + 1 12 | session[key] = new 13 | 14 | print(session.cookie) 15 | html = '''Content-Type: text/html 16 | 17 | 18 |

19 |     session id: {}
20 |     old: {}
21 |     new: {}
22 |     
23 | '''.format(key, old, new) 24 | print(html) 25 | 26 | session.save() 27 | 28 | if __name__ == '__main__': 29 | main() 30 | -------------------------------------------------------------------------------- /cheetah-demo/Cheetah/convertTmplPathToModuleName.py: -------------------------------------------------------------------------------- 1 | import os.path 2 | import string 3 | 4 | l = ['_'] * 256 5 | for c in string.digits + string.letters: 6 | l[ord(c)] = c 7 | _pathNameTransChars = string.join(l, '') 8 | del l, c 9 | 10 | def convertTmplPathToModuleName(tmplPath, 11 | _pathNameTransChars=_pathNameTransChars, 12 | splitdrive=os.path.splitdrive, 13 | translate=string.translate, 14 | ): 15 | return translate(splitdrive(tmplPath)[1], _pathNameTransChars) 16 | -------------------------------------------------------------------------------- /pytest-cmp/mask_cases.toml: -------------------------------------------------------------------------------- 1 | [[mask_cases]] 2 | id = "passwd" 3 | in = """ 4 | name: daffy 5 | passwd: t0ps3cr3t 6 | kind: duck 7 | """ 8 | out = """ 9 | name: daffy 10 | passwd: XXXX 11 | kind: duck 12 | """ 13 | 14 | [[mask_cases]] 15 | id = "password" 16 | in = """ 17 | name: daffy 18 | password: t0ps3cr3t 19 | kind: duck 20 | """ 21 | out = """ 22 | name: daffy 23 | password: XXXX 24 | kind: duck 25 | """ 26 | 27 | [[mask_cases]] 28 | id = "no change" 29 | in = """ 30 | name: daffy 31 | kind: duck 32 | color: black 33 | """ 34 | out = """ 35 | name: daffy 36 | kind: duck 37 | color: black 38 | """ 39 | -------------------------------------------------------------------------------- /glue/web: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | start() { 4 | conf=lighttpd.conf 5 | sed -e "s=_PWD_=$PWD=" ${conf}.in > $conf 6 | lighttpd -f $conf 7 | } 8 | 9 | stop() { 10 | pidfile=lighttpd.pid 11 | if [ -f $pidfile ]; then 12 | kill `cat $pidfile` # lighttpd will delete the pid file 13 | fi 14 | } 15 | 16 | if [ $# -ne 1 ]; then 17 | echo "usage: `basename $0` start|stop|restart" 1>&2 18 | exit 1 19 | fi 20 | 21 | case $1 in 22 | start ) start;; 23 | stop ) stop;; 24 | restart ) stop; start;; 25 | * ) echo "error: unknown action - $1" 1>&2; exit 1;; 26 | esac 27 | -------------------------------------------------------------------------------- /affinity_1.go: -------------------------------------------------------------------------------- 1 | /* CPU affinity in Go */ 2 | 3 | package main 4 | 5 | import ( 6 | "fmt" 7 | "math/rand" 8 | "runtime" 9 | "time" 10 | ) 11 | 12 | /* 13 | #define _GNU_SOURCE 14 | #include 15 | */ 16 | import "C" 17 | 18 | func randSleep() { 19 | time.Sleep(time.Duration(rand.Intn(300)) * time.Millisecond) 20 | } 21 | 22 | func worker(id int) { 23 | for { 24 | fmt.Printf("worker: %d, CPU: %d\n", id, C.sched_getcpu()) 25 | randSleep() 26 | } 27 | } 28 | 29 | func main() { 30 | for i := 0; i < runtime.NumCPU(); i++ { 31 | go worker(i) 32 | } 33 | time.Sleep(2 * time.Second) 34 | } 35 | -------------------------------------------------------------------------------- /printobj.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Quick and dirty object "repr"''' 3 | 4 | __author__ = "Miki Tebeka " 5 | 6 | # FIXME: Find how to make doctest play with "regular" class definition 7 | 8 | def printobj(obj): 9 | ''' 10 | Quick and dirty object "repr" 11 | 12 | >>> class Point: pass 13 | >>> p = Point() 14 | >>> p.x, p.y = 1, 2 15 | >>> printobj(p) 16 | ('y', 2) 17 | ('x', 1) 18 | >>> 19 | ''' 20 | print "\n".join(map(str, obj.__dict__.items())) 21 | 22 | if __name__ == "__main__": 23 | from doctest import testmod 24 | testmod() 25 | -------------------------------------------------------------------------------- /clickreg/jump.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Example CGI implementation 4 | 5 | # Database schema: 6 | # CREATE TABLE clicks ( 7 | # url TEXT, 8 | # value TEXT, 9 | # ); 10 | 11 | from cgi import FieldStorage 12 | import sqlite3 13 | 14 | 15 | 16 | form = FieldStorage() 17 | 18 | url = form.getvalue("url", "").strip() 19 | value = form.getvalue("value", "").strip() 20 | 21 | if not (url and value): 22 | raise SystemExit("error: missing parameter") 23 | 24 | db = sqlite3.connect("clicks.db") 25 | cur = db.cursor() 26 | cur.execute("INSERT INTO clicks values (?, ?)", (url, value)) 27 | db.commit() 28 | -------------------------------------------------------------------------------- /state-machine/float_test.go: -------------------------------------------------------------------------------- 1 | package float 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | var testCases = []struct { 8 | s string 9 | ok bool 10 | }{ 11 | {"1", true}, 12 | {"13", true}, 13 | {"1.2", true}, 14 | {".2", true}, 15 | {"2.", true}, 16 | {"-3", true}, 17 | {"-1.2", true}, 18 | {"", false}, 19 | {".", false}, 20 | {"-", false}, 21 | } 22 | 23 | func TestIsFloat(t *testing.T) { 24 | for _, tc := range testCases { 25 | t.Run(tc.s, func(t *testing.T) { 26 | ok := IsFloat(tc.s) 27 | if tc.ok != ok { 28 | t.Errorf("expected %v, got %v", tc.ok, ok) 29 | } 30 | }) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /httpparse.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Parse HTTP response 4 | 5 | from StringIO import StringIO 6 | from httplib import HTTPResponse 7 | 8 | class FakeSocket(StringIO): 9 | def makefile(self, *args, **kw): 10 | return self 11 | 12 | def httpparse(fp): 13 | socket = FakeSocket(fp.read()) 14 | response = HTTPResponse(socket) 15 | response.begin() 16 | 17 | return response 18 | 19 | if __name__ == "__main__": 20 | from os import popen 21 | 22 | pipe = popen("curl -si http://google.com") 23 | response = httpparse(pipe) 24 | 25 | print response.getheaders() 26 | 27 | 28 | -------------------------------------------------------------------------------- /glue/redirect.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Get content of "foreign" web pages, enable cross-site AJAX''' 3 | 4 | from urllib2 import urlopen, URLError 5 | from cgi import FieldStorage 6 | 7 | if __name__ == "__main__": 8 | import cgitb; cgitb.enable() 9 | 10 | form = FieldStorage() 11 | url = form.getvalue("url", "") 12 | if not url: 13 | raise SystemExit("no url") 14 | 15 | try: 16 | o = urlopen(url) 17 | except (URLError, ValueError), e: 18 | raise SystemExit("error: %s" % e) 19 | 20 | print "Content-type: %(content-type)s\n" % o.headers 21 | print o.read() 22 | -------------------------------------------------------------------------------- /py_defer/defer.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | 4 | class defer: 5 | def __init__(self, *, name='defer'): 6 | self.name = name 7 | self.tasks = [] 8 | 9 | def __enter__(self): 10 | return self.tasks 11 | 12 | def __exit__(self, typ, val, tb): 13 | for fn in reversed(self.tasks): 14 | try: 15 | fn() 16 | except Exception as err: 17 | logging.warning( 18 | '%s: %s failed with %s', self.name, fn.__name__, err) 19 | 20 | 21 | with defer() as d: 22 | d.append(lambda: print(1)) 23 | d.append(lambda: print(2)) 24 | -------------------------------------------------------------------------------- /cheetah-demo/Cheetah/Utils/htmlEncode.py: -------------------------------------------------------------------------------- 1 | """This is a copy of the htmlEncode function in Webware. 2 | 3 | 4 | @@TR: It implemented more efficiently. 5 | 6 | """ 7 | htmlCodes = [ 8 | ['&', '&'], 9 | ['<', '<'], 10 | ['>', '>'], 11 | ['"', '"'], 12 | ] 13 | htmlCodesReversed = htmlCodes[:] 14 | htmlCodesReversed.reverse() 15 | 16 | def htmlEncode(s, codes=htmlCodes): 17 | """ Returns the HTML encoded version of the given string. This is useful to 18 | display a plain ASCII text string on a web page.""" 19 | for code in codes: 20 | s = s.replace(code[0], code[1]) 21 | return s 22 | -------------------------------------------------------------------------------- /mk: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Guess which make utility to use 3 | 4 | # Miki Tebeka 5 | 6 | makecmd="" 7 | if [ -f SConstruct ]; then 8 | makecmd="scons -Q -D" 9 | elif [ -f build.xml ]; then 10 | makecmd="ant" 11 | elif [ -f Makefile ]; then 12 | makecmd="make" 13 | elif [ -f makefile ]; then 14 | makecmd="make" 15 | elif [ "$OSTYPE" == "WINNT" ]; then 16 | proj=`ls *.dsp 2>/dev/null` 17 | if [ -f $proj ]; then 18 | makecmd="msdev $proj /MAKE" 19 | fi 20 | fi 21 | 22 | if [ -z "$makecmd" ]; then 23 | echo "can't find project file" 24 | exit 1 25 | fi 26 | 27 | $makecmd $@ 28 | -------------------------------------------------------------------------------- /pytest-cmp/test_mask.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from os.path import abspath, dirname 3 | import toml 4 | 5 | from mask import mask 6 | 7 | here = dirname(abspath(__file__)) 8 | 9 | 10 | def iter_mask_cases(): 11 | with open(f'{here}/mask_cases.toml') as fp: 12 | cfg = toml.load(fp) 13 | for case in cfg['mask_cases']: 14 | # id sets the test name being printed out 15 | yield pytest.param(case['in'], case['out'], id=case['id']) 16 | 17 | 18 | @pytest.mark.parametrize('text, expected', iter_mask_cases()) 19 | def test_mask(text, expected): 20 | out = mask(text) 21 | assert out == expected 22 | -------------------------------------------------------------------------------- /svncommit: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Template for "svn commit" 3 | # Add "export SVN_EDITOR=/path/to/this/file" to your .bashrc 4 | 5 | # Fail one first error 6 | set -e 7 | 8 | filename=$1 9 | editor=${EDITOR-vim} 10 | 11 | mv $filename /tmp 12 | 13 | # The template 14 | cat << EOF > $filename 15 | Bug # 16 | Reviewed-by: 17 | 18 | EOF 19 | 20 | # Add file list to template 21 | cat /tmp/$filename >> $filename 22 | mtime=$(stat -c %y $filename) 23 | $editor $filename 24 | 25 | # Restore old file so svn will see it didn't change 26 | if [ "$(stat -c %y $filename)" == "$mtime" ]; then 27 | mv -f /tmp/$filename $filename 28 | fi 29 | 30 | -------------------------------------------------------------------------------- /web-install: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Do the `./configure && make && sudo make install` dance, given a download URL 3 | 4 | if [ $# -ne 1 ]; then 5 | echo "usage: `basename $0` URL" 6 | exit 1 7 | fi 8 | 9 | set -e # Fail on errors 10 | 11 | url=$1 12 | 13 | wget --no-check-certificate $url 14 | archive=`basename $url` 15 | 16 | if echo $archive | grep -q .tar.bz2; then 17 | tar -xjf $archive 18 | else 19 | tar -xzf $archive 20 | fi 21 | 22 | cd ${archive/.tar*} 23 | 24 | if [ -f setup.py ]; then 25 | sudo python setup.py install 26 | else 27 | ./configure && make && sudo make install 28 | fi 29 | 30 | cd .. 31 | -------------------------------------------------------------------------------- /rst/hw.txt: -------------------------------------------------------------------------------- 1 | =========== 2 | Hello World 3 | =========== 4 | :Author: Miki Tebeka 5 | :Date: $Date: 2007-06-05 21:02:04 -0700 (Tue, 05 Jun 2007) $ 6 | 7 | .. contents:: 8 | 9 | Chapter 1 10 | ========= 11 | In the beginning ... 12 | 13 | Sub Chapter 1 14 | ------------- 15 | There was LaTex_ [#]_ 16 | 17 | Chapter 2 18 | ========= 19 | And then there was reST_ 20 | 21 | :: 22 | 23 | That can have preformatted 24 | text 25 | 26 | 27 | ---- 28 | 29 | .. _LaTex: http://tug.org 30 | .. _reST: http://docutils.sf.net/rst.html 31 | 32 | .. [#] Which is still very good 33 | 34 | .. comment: vim: ft=rst spell 35 | -------------------------------------------------------------------------------- /st: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Repository status 4 | 5 | root=$PWD 6 | while [ "$root" != "/" ]; 7 | do 8 | if [ -d "$root/.hg" ]; then 9 | hg status 10 | break 11 | fi 12 | if [ -d "$root/.git" ]; then 13 | git status --untracked-files=no 14 | break 15 | fi 16 | if [ -d "$root/.svn" ]; then 17 | svn status 18 | break 19 | fi 20 | if [ -d "$root/.bzr" ]; then 21 | bzr status 22 | break 23 | fi 24 | root=$(dirname "$root") 25 | done 26 | 27 | if [ "$root" == "/" ]; then 28 | echo "error: can't find a repository here" 29 | exit 1 30 | fi 31 | -------------------------------------------------------------------------------- /state-machine/state-machine.dot: -------------------------------------------------------------------------------- 1 | digraph float { 2 | graph [fontsize=64, rankdir=LR]; 3 | edge [fontsize=10, fontcolor=blue]; 4 | node [shape=box]; 5 | 6 | start -> sign [label="-"]; 7 | start -> dot [label="."]; 8 | start -> number [label="0-9"]; 9 | 10 | sign -> number [label="0-9"]; 11 | sign -> dot [label="."]; 12 | 13 | number -> number [label="0-9"]; 14 | number -> dot [label="."]; 15 | number -> end [label="end"]; 16 | 17 | dot -> decimal [label="0-9"]; 18 | dot -> end [label="end"]; 19 | 20 | decimal -> decimal [label="0-9"]; 21 | decimal -> end [label="end"]; 22 | } 23 | -------------------------------------------------------------------------------- /teamspeak: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Make teamspeak work on Ubuntu (basically disable pulseaudio before launching) 4 | 5 | conf=$HOME/.pulse/client.conf 6 | backup="${conf}.bak" 7 | 8 | if [ -f "$conf" ]; then 9 | mv "$conf" "$backup" 10 | fi 11 | echo "autospawn = no" > "$conf" 12 | pulseaudio --kill 13 | 14 | cleanup() { 15 | rm "$conf" 16 | if [ -f "$backup" ]; then 17 | mv "$backup" "$conf" 18 | fi 19 | pulseaudio -D 20 | } 21 | 22 | cd /opt/TeamSpeak3-Client-linux_amd64/ 23 | LD_LIBRARY_PATH=. ./ts3client_linux_amd64& 24 | pid=$! 25 | trap "kill -9 $pid; cleanup" SIGINT SIGTERM 26 | wait $pid 27 | cleanup 28 | 29 | -------------------------------------------------------------------------------- /wdays.sql: -------------------------------------------------------------------------------- 1 | -- Convert Python's datetime.weekday() to names 2 | -- Run with "psql -U -1 -f wdays.sql" 3 | 4 | CREATE TABLE weekdays ( 5 | num TEXT 6 | , weekday TEXT 7 | ); 8 | CREATE INDEX weekdays_num ON weekdays(num); 9 | 10 | INSERT INTO weekdays 11 | (num, weekday) 12 | VALUES 13 | ('0', 'Monday') 14 | , ('1', 'Tuesday') 15 | , ('2', 'Wednesday') 16 | , ('3', 'Thursday') 17 | , ('4', 'Friday') 18 | , ('5', 'Saturday') 19 | , ('6', 'Sunday') 20 | ; 21 | 22 | UPDATE facts 23 | SET day = weekdays.weekday 24 | FROM weekdays 25 | WHERE day = weekdays.num 26 | ; 27 | 28 | DROP TABLE weekdays CASCADE; 29 | -------------------------------------------------------------------------------- /autobuild-c-ext/greet/_greet.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static PyObject * 5 | greet_greet(PyObject *self, PyObject *args) 6 | { 7 | char *name; 8 | 9 | if (!PyArg_ParseTuple(args, "s", &name)) { 10 | return NULL; 11 | } 12 | 13 | printf("Hello %s\n", name); 14 | 15 | return Py_BuildValue(""); 16 | } 17 | 18 | static PyMethodDef GreetMethods[] = { 19 | { "greet", greet_greet, METH_VARARGS, 20 | "Print a friendly greeting." 21 | }, 22 | {NULL, NULL, 0, NULL} /* Sentinel */ 23 | }; 24 | 25 | PyMODINIT_FUNC 26 | init_greet(void) 27 | { 28 | Py_InitModule("_greet", GreetMethods); 29 | } 30 | -------------------------------------------------------------------------------- /extract-audio: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Extract audio from video files 3 | # Uses ffmpeg and lame 4 | 5 | # Miki Tebeka 6 | 7 | if [ $# -ne 2 ]; then 8 | echo "usage: `basename $0` INPUT_VIDEO OUTPUT_MP3" 9 | exit 1 10 | fi 11 | 12 | infile=$1 13 | outfile=$2 14 | 15 | if [ ! -f $infile ]; then 16 | echo "error: can't find $infile" 17 | exit 1 18 | fi 19 | 20 | if [ -f $outfile ]; then 21 | echo "error: $outfile exists" 22 | exit 1 23 | fi 24 | 25 | fifoname=/tmp/encode.$$ 26 | mkfifo $fifoname 27 | mplayer -vc null -vo null -ao pcm:fast -ao pcm:file=$fifoname $1& 28 | lame $fifoname $outfile 29 | rm $fifoname 30 | 31 | -------------------------------------------------------------------------------- /jmlr.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Search the documents in Journal of Machine Learning. 3 | 4 | import webbrowser 5 | from urllib import urlencode 6 | 7 | def jmlr(words): 8 | query = "site:http://jmlr.csail.mit.edu filetype:pdf " + " ".join(words) 9 | url = "http://www.google.com/search?" + urlencode([("q", query)]) 10 | 11 | webbrowser.open(url) 12 | 13 | if __name__ == "__main__": 14 | from optparse import OptionParser 15 | 16 | parser = OptionParser("usage: %prog WORD1 [WORD2 ...]") 17 | 18 | opts, args = parser.parse_args() 19 | if not args: 20 | parser.error("wrong number of arguments") # Will exit 21 | 22 | jmlr(args) 23 | -------------------------------------------------------------------------------- /la.py: -------------------------------------------------------------------------------- 1 | '''A better version of the builtin "dir", place in your .pythonrc.py''' 2 | 3 | # >>> from user import la 4 | # >>> import wx 5 | # >>> la(wx, "open") 6 | # ART_FILE_OPEN 7 | # ART_FOLDER_OPEN 8 | # EVT_MENU_OPEN 9 | # ID_OPEN 10 | # OPEN 11 | # Process_Open 12 | # wxEVT_MENU_OPEN 13 | # >>> 14 | 15 | def la(obj, key=None, ignore_case=1): 16 | '''List all attributes of object''' 17 | import re 18 | if key: 19 | if ignore_case: 20 | flags = re.I 21 | else: 22 | flags = 0 23 | func = re.compile(key, flags).search 24 | 25 | else: 26 | func = None 27 | print "\n".join(filter(func, dir(obj))) 28 | -------------------------------------------------------------------------------- /pipe.py: -------------------------------------------------------------------------------- 1 | from subprocess import Popen, PIPE, STDOUT 2 | 3 | def pipe(*commands, **kw): 4 | """Run a chain of commands, returns the output of the last one. 5 | 6 | Arguments are a list of command line arguments, an optional keyword "stderr" 7 | can be used to capture stdandard error as well. Example: 8 | 9 | pipe(["dmesg"], ["grep", "hda"]) is like "dmesg | grep hda" 10 | """ 11 | stderr = STDOUT if kw.get("stderr", False) else None 12 | pipe = None 13 | for cmdline in commands: 14 | stdin = pipe.stdout if pipe else None 15 | pipe = Popen(cmdline, stdin=stdin, stdout=PIPE, stderr=stderr) 16 | 17 | return pipe.communicate()[0] 18 | -------------------------------------------------------------------------------- /result/nsqrt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "result.h" 3 | 4 | #define abs(v) (((v) < 0) ? -(v) : (v)) 5 | #define ntries 10000 6 | 7 | // Calculate sqrt using Newton's method 8 | result_t nsqrt(double n) { 9 | result_t res; 10 | res.err = NULL; 11 | 12 | if (n < 0) { 13 | set_result_err(&res, "sqrt of negative number"); 14 | return res; 15 | } 16 | 17 | double guess = 1.0; 18 | double epsilon = 0.00001; 19 | for (int i = 0; i < ntries; i++) { 20 | if (abs(guess * guess - n) <= epsilon) { 21 | res.fval = guess; 22 | return res; 23 | } 24 | 25 | guess = (n/guess + guess) / 2.0; 26 | } 27 | 28 | res.err = "can't find sqrt"; 29 | return res; 30 | } 31 | -------------------------------------------------------------------------------- /num_words.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from functools import partial 4 | import re 5 | 6 | filter_comment = partial(re.compile("#.*$").sub, "") 7 | words = re.compile("[^ \t\n\r]+").findall 8 | 9 | def num_words(text): 10 | '''Return the number of words in a code segment, ignoring comments 11 | 12 | >>> num_words("") 13 | 0 14 | >>> num_words("1 + 1") 15 | 3 16 | >>> num_words("1 + 1 # add 1 to 1") 17 | 3 18 | ''' 19 | return sum(map(len, 20 | map(words, 21 | map(filter_comment, 22 | text.splitlines())))) 23 | 24 | 25 | if __name__ == "__main__": 26 | import doctest 27 | doctest.testmod() 28 | -------------------------------------------------------------------------------- /result/nmath_test.go: -------------------------------------------------------------------------------- 1 | package nmath 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | const ( 8 | epsilon = 0.001 9 | ) 10 | 11 | func abs(v float64) float64 { 12 | if v < 0 { 13 | return -v 14 | } 15 | return v 16 | } 17 | 18 | func TestSqrt(t *testing.T) { 19 | v, err := Sqrt(2) 20 | if err != nil { 21 | t.Fatalf("error in Sqrt(2) - %s", err) 22 | } 23 | expected := 1.4142 24 | if abs(v-expected) > epsilon { 25 | t.Fatalf("bad value, expected %f, got %f", expected, v) 26 | } 27 | } 28 | 29 | func TestSqrtNeg(t *testing.T) { 30 | _, err := Sqrt(-2) 31 | if err == nil { 32 | t.Fatalf("no error for negative number") 33 | } 34 | t.Logf("expected error: %s", err) 35 | } 36 | -------------------------------------------------------------------------------- /timeit-demo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from timeit import Timer 4 | 5 | def fast_fib(n): 6 | if n < 2: 7 | return 1 8 | 9 | a, b = 1, 1 10 | for i in range(n - 1): 11 | a, b = b, a + b 12 | 13 | return b 14 | 15 | def slow_fib(n): 16 | if n < 2: 17 | return 1 18 | 19 | return slow_fib(n - 1) + slow_fib(n - 2) 20 | 21 | 22 | INDEX = 20 23 | TIMES = 100 24 | 25 | fast_timer = Timer("fast_fib(INDEX)", "from __main__ import fast_fib, INDEX") 26 | slow_timer = Timer("slow_fib(INDEX)", "from __main__ import slow_fib, INDEX") 27 | 28 | print "slow:", slow_timer.timeit(TIMES) / TIMES 29 | print "fast:", fast_timer.timeit(TIMES) / TIMES 30 | -------------------------------------------------------------------------------- /carrier: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Find the carrier for a cell number using whitepages 4 | 5 | # Miki Tebeka 6 | 7 | if [ $# -ne 1 ]; then 8 | echo "usage: $(basename $0) NUMBER" 9 | exit 1 10 | fi 11 | 12 | # Fake Firefox 13 | agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) " 14 | agent="$agent Gecko/20061204 Firefox/2.0.0.1" 15 | 16 | url="http://www.whitepages.com/carrier_lookup?" 17 | url="${url}carrier=other&number_0=${1}&name_1=&number_1=&name_2=&number_2=" 18 | url="${url}&name_3=&number_3=&response=1" 19 | 20 | curl -A "$agent" -L -s "$url" | \ 21 | grep -A1 carrier_result | tail -1 | \ 22 | sed -e 's/.*(\(.*\))/\1/' -e 's/^ \+//' 23 | -------------------------------------------------------------------------------- /htmlents.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from urllib import urlopen 4 | import re 5 | import webbrowser 6 | 7 | W3_URL = "http://www.w3.org/TR/WD-html40-970708/sgml/entities.html" 8 | FILE_NAME = "/tmp/html-entities.html" 9 | find_entity = re.compile("!ENTITY\s+([A-Za-z][A-Za-z0-9]+)").search 10 | 11 | fo = open(FILE_NAME, "wt") 12 | 13 | print >> fo, "" 14 | 15 | for line in urlopen(W3_URL): 16 | match = find_entity(line) 17 | if match: 18 | entity = match.groups()[0] 19 | print >> fo, "" % (entity, entity) 20 | print >> fo, "
%s&%s;
" 21 | fo.close() 22 | 23 | webbrowser.open(FILE_NAME) 24 | -------------------------------------------------------------------------------- /pandas-validation/stats.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Display some validation statistics on points_pre vs points_post data.''' 3 | 4 | from pandas import HDFStore 5 | 6 | def main(argv=None): 7 | import sys 8 | from argparse import ArgumentParser 9 | 10 | argv = argv or sys.argv 11 | 12 | parser = ArgumentParser(description='show some statistics') 13 | parser.parse_args(argv[1:]) 14 | 15 | store = HDFStore('points.h5') 16 | pre, post = store['pre'], store['post'] 17 | 18 | # Calculate diff in % 19 | diff = (pre - post)/pre * 100 20 | 21 | # Initial statistics 22 | print(diff.describe()) 23 | 24 | 25 | if __name__ == '__main__': 26 | main() 27 | 28 | -------------------------------------------------------------------------------- /push-cgi/load.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from cgi import FieldStorage 4 | from myapp import do_something_with_data 5 | 6 | ERROR = "Error: %s" 7 | 8 | def main(): 9 | print "Content-Type: text/html" 10 | print 11 | 12 | form = FieldStorage() 13 | data = form.getvalue("data", "") 14 | key = form.getvalue("key", "").strip() 15 | if not (key and data): 16 | raise SystemExit(ERROR % "NO 'key' or 'data'") 17 | 18 | try: 19 | do_something_with_data(key, data) 20 | except Exception, e: 21 | raise SystemExit(ERROR % e) 22 | 23 | print "OK" 24 | 25 | if __name__ == "__main__": 26 | main() 27 | -------------------------------------------------------------------------------- /is_zero.go: -------------------------------------------------------------------------------- 1 | // Check if a value is the zero value for it's type 2 | package main 3 | 4 | import ( 5 | "fmt" 6 | "reflect" 7 | ) 8 | 9 | // IsZero returns true if val is the zero value for it's type (e.g. 0, 0.0, "", ...) 10 | func IsZero(val interface{}) bool { 11 | zero := reflect.Zero(reflect.TypeOf(val)) 12 | // We can't use == since it'll panic on maps, slices ... 13 | return reflect.DeepEqual(zero.Interface(), reflect.ValueOf(val).Interface()) 14 | } 15 | 16 | func main() { 17 | // A little test 18 | var m1 map[int]int 19 | m2 := map[int]int{1: 2} 20 | 21 | for _, val := range []interface{}{0, 7, "", "hi", m1, m2} { 22 | fmt.Printf("%v (%T) -> %v\n", val, val, IsZero(val)) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /autobuild-c-ext/greet/__init__.py: -------------------------------------------------------------------------------- 1 | def _build(): 2 | from os.path import getmtime, isfile, dirname, join 3 | from sys import executable 4 | from os import system 5 | 6 | from setup import DYNLIB, SRC_FILE 7 | 8 | MODULE_DIR = dirname(__file__) 9 | 10 | def _run(cmd): 11 | return system("(cd \"%s\" && %s) > /dev/null 2>&1" % (MODULE_DIR, cmd)) 12 | 13 | _full_src = join(MODULE_DIR, SRC_FILE) 14 | _full_dynlib = join(MODULE_DIR, DYNLIB) 15 | 16 | if (not isfile(_full_dynlib)) or (getmtime(_full_dynlib) < getmtime(_full_src)): 17 | assert _run("%s setup.py build_ext -i" % executable) == 0, "build error" 18 | 19 | _build() 20 | del _build 21 | from _greet import * 22 | -------------------------------------------------------------------------------- /manual-bp.go: -------------------------------------------------------------------------------- 1 | // "Manual" breapoing in go 2 | // Compile with 3 | // go build -gcflags "-N -l" manual-bp.go 4 | // Then 5 | // gdb manual-bp 6 | // (gdb) run 7 | // When you hit breakpoint, call "fin" twice to get to the current location 8 | // (gdb) fin 9 | // (gdb) fin 10 | // After that it's usuall gdb commands 11 | // (gdb) p i 12 | // $1 = 3 13 | package main 14 | 15 | import ( 16 | "fmt" 17 | "syscall" 18 | ) 19 | 20 | func foo() { 21 | for i := 0; i < 5; i++ { 22 | if i == 3 { // Some complicated condition 23 | // Initiate a breakpoint 24 | syscall.Kill(syscall.Getpid(), syscall.SIGTRAP) 25 | } 26 | fmt.Printf("i = %d\n", i) 27 | } 28 | } 29 | 30 | func main() { 31 | foo() 32 | } 33 | -------------------------------------------------------------------------------- /check_install.py: -------------------------------------------------------------------------------- 1 | import importlib 2 | from sys import version_info 3 | 4 | if version_info[:2] < (3, 6): 5 | raise SystemExit('error: Python 3.6+ required') 6 | 7 | missing = [] 8 | pkgs = ( 9 | ('jupyterlab', 'jupyterlab'), 10 | ('matplotlib', 'matplotlib'), 11 | ('pandas', 'pandas'), 12 | ('sklearn', 'scikit-learn'), 13 | ) 14 | 15 | for mod, pkg in pkgs: 16 | try: 17 | importlib.import_module(mod) 18 | except ImportError: 19 | missing.append(pkg) 20 | 21 | if not missing: 22 | print('You have all the requirement') 23 | raise SystemExit() 24 | 25 | missing = ', '.join(sorted(missing)) 26 | print('error: you are missing the following modules - {}'.format(missing)) 27 | -------------------------------------------------------------------------------- /docker-miniconda/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Test server by sending photo and viewing the output 3 | 4 | # Image by Trish Hamme (https://flic.kr/p/cYUQv7) 5 | 6 | if [ $# -ne 1 ]; then 7 | echo "error: wrong number of arguments" 8 | exit 1 9 | fi 10 | 11 | case $1 in 12 | -h | --help ) echo "usage: $(basename $0) edge|resize"; exit;; 13 | edge ) path=edge;; 14 | resize ) path=resize;; 15 | * ) echo "error: unknown operation - $1"; exit 1;; 16 | esac 17 | 18 | outfile=$(mktemp --suffix=.png) 19 | 20 | curl \ 21 | -s \ 22 | -X POST \ 23 | -H 'Content-Type: image/jpeg' \ 24 | --data-binary @image.jpg \ 25 | -o ${outfile} \ 26 | http://localhost:8080/${path} 27 | 28 | xdg-open ${outfile} 29 | -------------------------------------------------------------------------------- /dashm.txt: -------------------------------------------------------------------------------- 1 | # Files 2 | zipfile 3 | filecmp 4 | gzip 5 | 6 | # Encoding 7 | json.tool 8 | base64 9 | uu 10 | python encodings.rot_13 11 | #binhex 12 | mimify 13 | quopri 14 | 15 | # Humor 16 | this 17 | antigravity 18 | 19 | # Other 20 | calendar 21 | turtle 22 | webbrowser 23 | 24 | # Servers 25 | SimpleHTTPServer 26 | CGIHTTPServer 27 | smtpd 28 | 29 | # Network Clients 30 | ftplib 31 | poplib 32 | nttpserver 33 | smtplib (localhost) 34 | telnetlib 35 | 36 | # System Info 37 | locale 38 | mimetypes 39 | platform 40 | 41 | # Python 42 | compileall 43 | dis 44 | pdb 45 | pydoc 46 | site 47 | sysconfig 48 | tabnanny 49 | tokenize 50 | 51 | # Profile 52 | cProfile 53 | profile 54 | timeit 55 | pstats 56 | trace 57 | 58 | idle 59 | 60 | -------------------------------------------------------------------------------- /react-table/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | React Table 4 | 6 | 7 | 8 |
9 |
10 |
11 | 12 |
13 | 14 | 15 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /top-x/gendata.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Generate word frequency from alice''' 3 | 4 | import re 5 | from collections import Counter 6 | import json 7 | from operator import itemgetter 8 | from bz2 import decompress 9 | 10 | def main(): 11 | words = {} 12 | text = decompress(open('alice.txt.bz2', 'rb').read()).lower() 13 | c = Counter(m.group() for m in re.finditer('[a-z]{3,}', text)) 14 | 15 | words = sorted(c.iteritems(), key=itemgetter(1), reverse=True) 16 | data = [{'label' : word, 'data' : count} for word, count in words] 17 | 18 | fo = open('data.js', 'w') 19 | fo.write('var data = ') 20 | json.dump(data, fo) 21 | fo.write(';') 22 | 23 | if __name__ == '__main__': 24 | main() 25 | 26 | -------------------------------------------------------------------------------- /auto_rety.py: -------------------------------------------------------------------------------- 1 | 2 | def auto_retries(fn): 3 | '''Automatically retry task up to config..max_retries 4 | 5 | If config..max_retries is not present, does nothing. 6 | ''' 7 | attr = '{}.max_retries'.format(fn.__module__, fn.func_name) 8 | max_retries = getattr(config, attr, 0) 9 | if max_retries == 0: 10 | return fn 11 | 12 | @wraps(fn) 13 | def wrapper(*args, **kw): 14 | if not celery.current_task: 15 | return fn(*args, **kw) 16 | 17 | celery.current_task.max_retries = max_retries 18 | try: 19 | return fn(*args, **kw) 20 | except Exception as exc: 21 | celery.current_task.retry(exc=exc) 22 | 23 | return wrapper 24 | -------------------------------------------------------------------------------- /config_wrapper.py: -------------------------------------------------------------------------------- 1 | class ConfigWrapper: 2 | def __init__(self, cfg): 3 | self._cfg = cfg 4 | 5 | def __getattr__(self, attr): 6 | try: 7 | val = self._cfg[attr] 8 | if isinstance(val, dict): 9 | val = ConfigWrapper(val) 10 | return val 11 | except KeyError: 12 | raise AttributeError(attr) 13 | 14 | def __dir__(self): 15 | return list(self._cfg) 16 | 17 | 18 | # Demo 19 | cfg = { 20 | 'httpd': { 21 | 'port': 8080, 22 | 'interface': 'localhost', 23 | }, 24 | 'user': 'gary', 25 | } 26 | 27 | w = ConfigWrapper(cfg) 28 | user = w.user 29 | print(f'user: {user}') 30 | port = w.httpd.port 31 | print(f'httpd.port: {port}') 32 | -------------------------------------------------------------------------------- /glue/lighttpd.conf.in: -------------------------------------------------------------------------------- 1 | server.modules = ( 2 | "mod_cgi" 3 | ) 4 | 5 | server.document-root = "_PWD_" 6 | server.pid-file = "_PWD_/lighttpd.pid" 7 | static-file.exclude-extensions = ( ".cgi" ) 8 | index-file.names = ( "index.html" ) 9 | server.port = 9999 10 | cgi.assign = ( ".cgi" => "" ) 11 | 12 | # mimetype mapping 13 | mimetype.assign = ( 14 | ".gif" => "image/gif", 15 | ".jpg" => "image/jpeg", 16 | ".jpeg" => "image/jpeg", 17 | ".png" => "image/png", 18 | ".css" => "text/css", 19 | ".html" => "text/html", 20 | ".htm" => "text/html", 21 | ".js" => "text/javascript", 22 | "" => "text/plain" 23 | ) 24 | -------------------------------------------------------------------------------- /update_daemons.out: -------------------------------------------------------------------------------- 1 | $ ./updated.py 8000 8001 8002 2 | $ supervisorctl status 3 | httpd-8000 RUNNING pid 31768, uptime 0:00:04 4 | httpd-8001 RUNNING pid 31767, uptime 0:00:04 5 | httpd-8002 RUNNING pid 31766, uptime 0:00:04 6 | $ ./updated.py 8000 8004 8002 # Remove 8001, add 8004 7 | httpd-8001: disappeared 8 | httpd-8004: available 9 | httpd-8001: stopped 10 | httpd-8001: removed process group 11 | httpd-8004: added process group 12 | $ supervisorctl status 13 | httpd-8000 RUNNING pid 31768, uptime 0:00:12 14 | httpd-8002 RUNNING pid 31766, uptime 0:00:12 15 | httpd-8004 RUNNING pid 31785, uptime 0:00:02 16 | $ 17 | -------------------------------------------------------------------------------- /attr.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from operator import attrgetter 4 | from random import shuffle 5 | 6 | class Point: 7 | def __init__(self, x, y): 8 | self.x, self.y = x, y 9 | 10 | def sort1(points): 11 | points.sort(key = lambda p: p.x) 12 | 13 | def sort2(points): 14 | points.sort(key = attrgetter("x")) 15 | 16 | if __name__ == "__main__": 17 | from timeit import Timer 18 | 19 | points1 = [Point(x, 2 * x) for x in range(100)] 20 | points2 = points1[:] 21 | 22 | num_times = 10000 23 | 24 | t1 = Timer("sort1(points1)", "from __main__ import sort1, points1") 25 | print t1.timeit(num_times) 26 | 27 | t2 = Timer("sort2(points2)", "from __main__ import sort2, points2") 28 | print t2.timeit(num_times) 29 | -------------------------------------------------------------------------------- /enum2.py: -------------------------------------------------------------------------------- 1 | '''Yet another enum implemenation.''' 2 | from collections import namedtuple 3 | 4 | 5 | def enum(*fields, **kw): 6 | '''Return an object where each field is mappend to its location. 7 | 8 | You can specify `bitmask` keyword, then fields are mapped to bitwise 9 | location (for bit masks). 10 | ''' 11 | op = (lambda x: 1 << x) if kw.get('bitmask', False) else (lambda x: x) 12 | return namedtuple('Enum', fields)(*(op(i) for i, _ in enumerate(fields))) 13 | 14 | 15 | def _test(): 16 | Colors = enum('Red', 'Green', 'Blue') 17 | print(Colors.Green) # 1 18 | 19 | Interrupts = enum('TERM', 'KILL', 'HUP', 'INT', bitmask=True) 20 | print(Interrupts.HUP) # 4 21 | 22 | 23 | if __name__ == '__main__': 24 | _test() 25 | -------------------------------------------------------------------------------- /macapp/README.txt: -------------------------------------------------------------------------------- 1 | =============================== 2 | Quick and Easy OSX Applications 3 | =============================== 4 | 5 | :Author: Miki Tebeka 6 | 7 | This is a demo on how to quickly create OS X (Mac) application using the 8 | bundled Python. 9 | 10 | We use the browser as GUI (Desktop Web Application) and create an 11 | "Application Bundle" which is just a directory that ends with .app and has a 12 | certain structure. 13 | 14 | At the end we create a ZIP file with the application bundle in it, ready for 15 | distribution. I still need to find a good way to create a .dmg (mkisofs 16 | don't cut it). 17 | 18 | Most of the information about application bundles I got from 19 | http://tinyurl.com/yh3bh2h and http://tinyurl.com/yf7rqow. 20 | -------------------------------------------------------------------------------- /gist-it: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Print link to gist-it""" 3 | 4 | from argparse import ArgumentParser, FileType 5 | from subprocess import check_output 6 | from os import path 7 | 8 | parser = ArgumentParser(description=__doc__) 9 | parser.add_argument('script', help='script name', type=FileType('r')) 10 | 11 | args = parser.parse_args() 12 | 13 | root = check_output(['git', 'rev-parse', '--show-toplevel']).decode().strip() 14 | if root.endswith('/'): 15 | root = root[:-1] 16 | 17 | script = path.abspath(args.script.name) 18 | script = script[len(root)+1:] 19 | # gh_url = f'https://github.com/tebeka/pythonwise/blob/master/{script}' 20 | gh_url = f'/github/tebeka/pythonwise/blob/master/{script}' 21 | print(f'') 22 | -------------------------------------------------------------------------------- /hiveloc: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Show location in HDFS of a Hive table 3 | 4 | case $1 in 5 | -h | --help ) echo "usage: $(basename $0) TABLE"; exit;; 6 | esac 7 | 8 | if [ $# -ne 1 ]; then 9 | $0 -h 10 | exit 1 11 | fi 12 | 13 | 14 | # Store stderr to display error 15 | err=$(mktemp) 16 | # Cleanup the error file 17 | trap "rm -f ${err}" EXIT INT 18 | 19 | out=$(hive client -S -e "DESCRIBE EXTENDED ${1}" 2>${err}) 20 | 21 | # Of course hive won't return error to the os, assume empty output is error 22 | if [ -z "${out}" ]; then 23 | cat ${err} 24 | exit 1 25 | fi 26 | 27 | # We're looking for something like 28 | # location:hdfs://nameservice1/user/beeswax/warehouse/people, 29 | echo "${out}" | \ 30 | egrep -o 'location:[^,]+' | \ 31 | sed 's/location://' 32 | -------------------------------------------------------------------------------- /trampoline.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''A CGI "trampoline" to enable cross site AJAX (when JSONP is not possible)''' 3 | 4 | __author__ = "Miki Tebeka " 5 | 6 | from os import environ 7 | from sys import argv 8 | 9 | def get_query(): 10 | if len(argv) < 1: 11 | return argv[1] 12 | 13 | return environ.get("QUERY_STRING", "") 14 | 15 | 16 | def main(): 17 | from urllib import urlopen 18 | 19 | query = get_query() 20 | if query: 21 | query = "?" + query 22 | # Example search in citeseer 23 | url = "http://citeseer.ist.psu.edu/cis%s" % query 24 | 25 | o = urlopen(url) 26 | 27 | print "Content-type: %(content-type)s\n" % o.headers 28 | print o.read() 29 | 30 | if __name__ == "__main__": 31 | main() 32 | -------------------------------------------------------------------------------- /book/menu-after.py: -------------------------------------------------------------------------------- 1 | menu = {} 2 | 3 | 4 | def register(fn): 5 | """Register function to menu""" 6 | menu[fn.__name__] = fn 7 | return fn 8 | 9 | 10 | @register 11 | def copy(): 12 | """Copy text""" 13 | print('>>> COPY <<<') 14 | 15 | 16 | @register 17 | def paste(): 18 | """Paste text""" 19 | print('>>> PASTE <<<') 20 | 21 | 22 | def ui_loop(): 23 | """Runs the UI loop""" 24 | while True: 25 | print('=== Menu ===') 26 | print('\n'.join(sorted(menu))) 27 | name = input('# ').strip() # Use raw_input in Python 2 28 | if name == 'quit': 29 | break 30 | fn = menu.get(name) 31 | if not fn: 32 | print('ERROR: Unknown action - {}'.format(name)) 33 | continue 34 | fn() 35 | -------------------------------------------------------------------------------- /myip.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Find local machine IP (cross platform)''' 3 | 4 | from subprocess import Popen, PIPE 5 | from sys import platform 6 | import re 7 | 8 | COMMANDS = { 9 | "darwin" : "/sbin/ifconfig", 10 | "linux" : "/sbin/ifconfig", 11 | "linux2" : "/sbin/ifconfig", 12 | "win32" : "ipconfig", 13 | } 14 | 15 | def my_ip(): 16 | command = COMMANDS.get(platform, "") 17 | assert command, "don't know how to get IP for current platform" 18 | 19 | pipe = Popen([command], stdout=PIPE) 20 | pipe.wait() 21 | output = pipe.stdout.read() 22 | 23 | for ip in re.findall("(\d+\.\d+\.\d+\.\d+)", output): 24 | if ip.startswith("127.0.0"): 25 | continue 26 | return ip 27 | 28 | if __name__ == "__main__": 29 | print my_ip() 30 | -------------------------------------------------------------------------------- /date-demo.sh: -------------------------------------------------------------------------------- 1 | # Current date in default format 2 | $ date 3 | Sun Dec 7 16:56:26 IST 2014 4 | 5 | # Current date in ISO-8601 format (with seconds) 6 | $ date -Isec 7 | 2014-12-07T16:57:21+0200 8 | 9 | # Current date in UTC 10 | $ date -u 11 | Sun Dec 7 14:58:36 UTC 2014 12 | 13 | # Current date in different time zone 14 | $ TZ=US/Eastern date 15 | Sun Dec 7 09:59:08 EST 2014 16 | 17 | # Current date in custom format, see "date --help" for format options 18 | $ date +%Y-%m-%d 19 | 2014-12-07 20 | 21 | # Current date as epoch 22 | $ date +%s 23 | 1417964341 24 | 25 | # Date math 26 | $ date --date='-2 days' 27 | Fri Dec 5 16:59:37 IST 2014 28 | $ date --date='2 days ago' 29 | Fri Dec 5 17:00:08 IST 2014 30 | 31 | # Combination of math and format 32 | $ date --date='2 days ago' +%Y%m%d 33 | 20141205 34 | -------------------------------------------------------------------------------- /macapp/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleName 6 | HumbleCalc 7 | CFBundleDisplayName 8 | HumbleCalc 9 | CFBundleIdentifier 10 | com.mikitebeka.HumbleCalc 11 | CFBundleVersion 12 | 0.0.1 13 | CFBundlePackageType 14 | APPL 15 | CFBundleSignature 16 | HMCL 17 | CFBundleExecutable 18 | humblecalc.py 19 | CFBundleIconFile 20 | humblecalc.ico 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /react-table/httpd.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "log" 7 | "net/http" 8 | "strconv" 9 | ) 10 | 11 | func dataHandler(w http.ResponseWriter, r *http.Request) { 12 | val := r.FormValue("last") 13 | last, err := strconv.Atoi(val) 14 | if err != nil { 15 | last = 0 16 | } 17 | size := 10 18 | items := make([]map[string]string, size) 19 | for i := 0; i < size; i++ { 20 | items[i] = map[string]string{ 21 | "name": fmt.Sprintf("Name %d", i+last), 22 | "color": fmt.Sprintf("Color %d", i+last), 23 | } 24 | } 25 | enc := json.NewEncoder(w) 26 | enc.Encode(items) 27 | } 28 | 29 | func main() { 30 | http.HandleFunc("/data", dataHandler) 31 | fs := http.FileServer(http.Dir(".")) 32 | http.Handle("/", fs) 33 | log.Fatal(http.ListenAndServe(":8080", nil)) 34 | } 35 | -------------------------------------------------------------------------------- /dmd-mail/.sup/config.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | :editor: vim 3 | :thread_by_subject: false 4 | :edit_signature: false 5 | :ask_for_from: false 6 | :ask_for_to: true 7 | :ask_for_cc: true 8 | :ask_for_bcc: false 9 | :ask_for_subject: true 10 | :account_selector: true 11 | :confirm_no_attachments: true 12 | :confirm_top_posting: true 13 | :jump_to_open_message: true 14 | :discard_snippets_from_encrypted_messages: false 15 | :load_more_threads_when_scrolling: true 16 | :default_attachment_save_dir: "/tmp" 17 | :sent_source: sup://sent 18 | :archive_sent: true 19 | :poll_interval: 300 20 | :wrap_width: 0 21 | :slip_rows: 0 22 | :col_jump: 2 23 | :stem_language: english 24 | :accounts: 25 | :default: 26 | :name: Bugs Bunny 27 | :email: bugs.bunny@looneytunes.com 28 | :alternates: [] 29 | :sendmail: /usr/sbin/sendmail -oem -ti 30 | -------------------------------------------------------------------------------- /docker-miniconda/Dockerfile: -------------------------------------------------------------------------------- 1 | # Example Dockerfile for setting up Docker container with MiniConda and an 2 | # example app. 3 | 4 | FROM ubuntu:14.04 5 | MAINTAINER Miki Tebeka 6 | 7 | # System packages 8 | RUN apt-get update && apt-get install -y curl 9 | 10 | # Install miniconda to /miniconda 11 | RUN curl -LO http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh 12 | RUN bash Miniconda-latest-Linux-x86_64.sh -p /miniconda -b 13 | RUN rm Miniconda-latest-Linux-x86_64.sh 14 | ENV PATH=/miniconda/bin:${PATH} 15 | RUN conda update -y conda 16 | 17 | # Python packages from conda 18 | RUN conda install -y \ 19 | scikit-image \ 20 | flask \ 21 | pillow 22 | 23 | # Setup application 24 | COPY imgsrv.py / 25 | ENTRYPOINT ["/miniconda/bin/python", "/imgsrv.py"] 26 | EXPOSE 8080 27 | -------------------------------------------------------------------------------- /tagcloud/mako/ext/preprocessors.py: -------------------------------------------------------------------------------- 1 | """preprocessing functions, used with the 'preprocessor' argument on Template, TemplateLookup""" 2 | 3 | import re 4 | 5 | def convert_comments(text): 6 | """preprocess old style comments. 7 | 8 | example: 9 | 10 | from mako.ext.preprocessors import convert_comments 11 | t = Template(..., preprocessor=preprocess_comments)""" 12 | return re.sub(r'(?<=\n)\s*#[^#]', "##", text) 13 | 14 | # TODO 15 | def create_tag(callable): 16 | """given a callable, extract the *args and **kwargs, and produce a preprocessor 17 | that will parse for <% > and convert to an appropriate <%call> statement. 18 | 19 | this allows any custom tag to be created which looks like a pure Mako-style tag.""" 20 | raise NotImplementedError("Future functionality....") -------------------------------------------------------------------------------- /add_cert.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Add server certificate. 3 | 4 | You'll need certutil tool installed, probably: 5 | apt-get install libnss3-tools 6 | ''' 7 | 8 | from argparse import ArgumentParser, FileType 9 | from subprocess import check_call, CalledProcessError 10 | from os import environ 11 | 12 | parser = ArgumentParser(description=__doc__) 13 | parser.add_argument('name', help='certificate name (pick one :)') 14 | parser.add_argument('file', help='certificate file', type=FileType()) 15 | args = parser.parse_args() 16 | 17 | cmd = [ 18 | 'certutil', 19 | '-d', 'sql:%s/.pki/nssdb' % environ['HOME'], 20 | '-A', '-t', 'P,,', 21 | '-n', args.name, 22 | '-i', args.file.name, 23 | ] 24 | 25 | try: 26 | check_call(cmd) 27 | except CalledProcessError as err: 28 | raise SystemExit('error: %s' % err) 29 | -------------------------------------------------------------------------------- /dynamic-static/index.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import cgitb; cgitb.enable() 4 | from cgi import FieldStorage 5 | 6 | INDEX = ''' 7 | 8 | 9 | Lighty Dynamic Static Thingie 10 | 11 | 12 |

Get A Chart

13 |
14 | Chart Name:
15 | 16 |
17 | 18 | 19 | ''' 20 | 21 | def main(): 22 | form = FieldStorage() 23 | 24 | name = form.getvalue("name", "").strip() 25 | if not name: 26 | print "Content-type: text/html\n" 27 | print INDEX 28 | raise SystemExit 29 | 30 | # Redirect to chart 31 | print "Location: /charts/%s.png\n" % name 32 | 33 | if __name__ == "__main__": 34 | main() 35 | -------------------------------------------------------------------------------- /book/menu-before.py: -------------------------------------------------------------------------------- 1 | menu = {} 2 | 3 | 4 | def register(fn): 5 | """Register function to menu""" 6 | menu[fn.__name__] = fn 7 | return fn 8 | 9 | 10 | def copy(): 11 | """Copy text""" 12 | print('>>> COPY <<<') 13 | 14 | 15 | def paste(): 16 | """Paste text""" 17 | print('>>> PASTE <<<') 18 | 19 | 20 | # Register menu functions 21 | register(copy) 22 | register(paste) 23 | 24 | 25 | def ui_loop(): 26 | """Runs the UI loop""" 27 | while True: 28 | print('=== Menu ===') 29 | print('\n'.join(sorted(menu))) 30 | name = input('# ').strip() # Use raw_input in Python 2 31 | if name == 'quit': 32 | break 33 | fn = menu.get(name) 34 | if not fn: 35 | print('ERROR: Unknown action - {}'.format(name)) 36 | continue 37 | fn() 38 | -------------------------------------------------------------------------------- /dynamic-static/web-up: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Run web server 3 | 4 | # Stop on first error 5 | set -e 6 | 7 | INC=lighttpd.inc 8 | PORT=8080 9 | PYTHON=$(python -c 'import sys; print sys.executable') 10 | USAGE="usage: `basename $0` [-p PORT]" 11 | 12 | while getopts "u:p:nh" opt 13 | do 14 | case $opt in 15 | p ) PORT=$OPTARG;; 16 | h ) echo $USAGE; exit;; 17 | * ) echo "error: unrecognized option -$opt"; exit 1;; 18 | esac 19 | done 20 | 21 | shift $(($OPTIND - 1)) 22 | if [ $# -ne 0 ]; then 23 | echo "error: wrong number of arguments" 24 | echo $USAGE 25 | exit 1 26 | fi 27 | 28 | # Don't fail on this one 29 | ./web-down > /dev/null 2>&1 || true 30 | 31 | cat << EOF > $INC 32 | var.root = "$PWD" 33 | server.port = $PORT 34 | cgi.assign = ( ".cgi" => "$PYTHON" ) 35 | EOF 36 | 37 | lighttpd -f lighttpd.conf 38 | -------------------------------------------------------------------------------- /enum1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | def enum(*names, **kw): 4 | ''' 5 | Create an enum, names will be ordered starting from 1. 6 | 7 | If you specify `bitmask` keyword, then values will be power of two, useful 8 | when you're using a bitmask of flags. 9 | 10 | >>> flags = enum("A", "B", "C") 11 | >>> flags.A 12 | 1 13 | >>> flags.C 14 | 3 15 | >>> flags = enum("A", "B", "C", bitmask=True) 16 | >>> flags.A 17 | 1 18 | >>> flags.C 19 | 4 20 | ''' 21 | class Enum: pass 22 | value = (lambda i: 1 << i) if kw.get('bitmask', 0) else (lambda i: i + 1) 23 | 24 | enum = Enum() 25 | for i, name in enumerate(names): 26 | setattr(enum, name, value(i)) 27 | 28 | return enum 29 | 30 | if __name__ == '__main__': 31 | from doctest import testmod 32 | testmod(verbose=1) 33 | -------------------------------------------------------------------------------- /go-asset/httpd.go: -------------------------------------------------------------------------------- 1 | /* httpd that log requests in JSON format */ 2 | package main 3 | 4 | import ( 5 | "flag" 6 | "fmt" 7 | "log" 8 | "net/http" 9 | ) 10 | 11 | // For Content-Length, calculated on init 12 | var imageLen string 13 | 14 | func init() { 15 | /* image is generated from image.go, see Makefile */ 16 | imageLen = fmt.Sprintf("%d", len(image)) 17 | } 18 | 19 | // homeHandler serves the image 20 | func homeHandler(w http.ResponseWriter, r *http.Request) { 21 | w.Header().Set("Content-Type", "image/png") 22 | w.Header().Set("Content-Length", imageLen) 23 | w.Write(image) 24 | } 25 | 26 | func main() { 27 | port := 8080 28 | flag.IntVar(&port, "port", port, "port to listen on") 29 | flag.Parse() 30 | 31 | http.HandleFunc("/", homeHandler) 32 | addr := fmt.Sprintf(":%d", port) 33 | log.Fatal(http.ListenAndServe(addr, nil)) 34 | } 35 | -------------------------------------------------------------------------------- /logio.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | class logio: 4 | '''Class to emulate file IO for log files''' 5 | def __init__(self, logfn, strip=False): 6 | '''logfn 7 | Log function (such as logging.error ...) 8 | strip 9 | Flag to strip data (since logging appends newline) 10 | ''' 11 | self.logfn = logfn 12 | self.strip = strip 13 | 14 | def write(self, data): 15 | if self.strip: 16 | data = data.rstrip() 17 | self.logfn(data) 18 | 19 | if __name__ == '__main__': 20 | # Example showing creation of CSV log files 21 | import csv 22 | import logging as log 23 | 24 | log.basicConfig(format='%(message)s') 25 | 26 | writer = csv.writer(logio(log.error, True)) 27 | for i in range(10): 28 | writer.writerow([i, i+1, i+2]) 29 | -------------------------------------------------------------------------------- /decrypt: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Decrypt a file encrypted with gpg 4 | 5 | 6 | # Check command line 7 | if [ $# -lt 1 ]; then 8 | echo "usage: `basename $0` FILE[s]" 9 | exit 1 10 | fi 11 | 12 | err=0 13 | for file in $@ 14 | do 15 | if [ ! -f $file ]; then 16 | echo "$file: Not found" 17 | err=1 18 | continue 19 | fi 20 | 21 | # Output to current directory 22 | bfile=`basename $file` 23 | ext=`echo $file | sed -e 's/.*\.\([!.]*\)/\1/'` 24 | if [ "$ext" != "gpg" ] && [ "$ext" != "pgp" ]; then 25 | echo "Cowardly refusing to decrypt a file without gpg/pgp extension" 26 | err=1 27 | continue 28 | fi 29 | 30 | outfile=${bfile%.$ext} 31 | 32 | echo $file '->' $outfile 33 | gpg -d -o $outfile $file 34 | if [ $? -ne 0 ]; then 35 | echo "error decrypting $file" 36 | err=1 37 | fi 38 | done 39 | 40 | exit $err 41 | -------------------------------------------------------------------------------- /noise-reduction.py: -------------------------------------------------------------------------------- 1 | # View notebook at http://bit.ly/191MRIN 2 | from matplotlib import pyplot as plt 3 | import numpy as np 4 | 5 | values = np.random.binomial(1000, 0.5, 1000) 6 | plt.hist(values, bins=20) # We'll get the below before the chart 7 | # (array([ 3., 3., 7., 23., 39., 46., 75., 94., 94., 8 | # 118., 121., 104., 82., 66., 45., 46., 18., 8., 9 | # 6., 2.]), 10 | # array([ 454. , 458.65, 463.3 , 467.95, 472.6 , 477.25, 481.9 , 11 | # 486.55, 491.2 , 495.85, 500.5 , 505.15, 509.8 , 514.45, 12 | # 519.1 , 523.75, 528.4 , 533.05, 537.7 , 542.35, 547. ]), 13 | # ) 14 | 15 | # Option 1: Use assignment 16 | _ = plt.hist(values, bins=20) # Remove noise 17 | 18 | # Option 2: Dummy last expression 19 | plt.hist(values, bins=20) 20 | None # Remove noise 21 | -------------------------------------------------------------------------------- /cd.py: -------------------------------------------------------------------------------- 1 | '''Small cd context manger that changes directory for a given operation and 2 | then resotres current directory. 3 | 4 | Inspired by Fabric's cd context manager (http://bit.ly/PCBkIW) 5 | ''' 6 | from os import getcwd, chdir 7 | 8 | class cd: 9 | __doc__ = '''Context manager for changing directories for an operation 10 | 11 | >>> print(getcwd()) 12 | {cwd} 13 | >>> with cd('/tmp'): 14 | ... print(getcwd()) 15 | /tmp 16 | >>> print(getcwd()) 17 | {cwd} 18 | >>> 19 | '''.format(cwd=getcwd()) 20 | def __init__(self, path): 21 | self.path = path 22 | self.back = getcwd() 23 | 24 | def __enter__(self): 25 | chdir(self.path) 26 | 27 | def __exit__(self, type, value, trackback): 28 | chdir(self.back) 29 | 30 | 31 | if __name__ == '__main__': 32 | import doctest 33 | doctest.testmod() 34 | -------------------------------------------------------------------------------- /openmodule.vim: -------------------------------------------------------------------------------- 1 | " Open a module (in sys.path) in a new window (like CTRL-M in IDLE) 2 | " Place this somewhere in your .vimrc 3 | 4 | func OpenModule(name) 5 | python << EOF 6 | import os, sys 7 | sys.path.append(os.getcwd()) 8 | import vim 9 | name = vim.eval("a:name") 10 | try: 11 | module = __import__(name) 12 | if ("." in name): 13 | names = name.split(".")[1:] 14 | while names: 15 | name = names.pop(0) 16 | module = getattr(module, name) 17 | filename = getattr(module, "__file__", None) 18 | if filename: 19 | import re 20 | filename = re.sub(".py[co]", ".py", filename) 21 | vim.command("e %s" % filename) 22 | else: 23 | print "error: can't find module `%s` source" % name 24 | except ImportError: 25 | print "error: can't find `%s` module" % name 26 | EOF 27 | endfunc 28 | comm! -nargs=1 OM call OpenModule() 29 | -------------------------------------------------------------------------------- /jsonify.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from functools import wraps 4 | import json 5 | from cherrypy import response, expose 6 | 7 | def jsonify(func): 8 | '''JSON decorator for CherryPy''' 9 | @wraps(func) 10 | def wrapper(*args, **kw): 11 | value = func(*args, **kw) 12 | response.headers["Content-Type"] = "application/json" 13 | return json.dumps(value) 14 | 15 | return wrapper 16 | 17 | def example(): 18 | from cherrypy import quickstart 19 | from datetime import datetime 20 | class Time: 21 | @expose 22 | @jsonify 23 | def index(self): 24 | now = datetime.now() 25 | return { 26 | "date" : now.strftime("%Y-%m-%d"), 27 | "time" : now.strftime("%H:%M"), 28 | "day" : now.strftime("%A"), 29 | } 30 | 31 | quickstart(Time()) 32 | 33 | if __name__ == "__main__": 34 | example() 35 | -------------------------------------------------------------------------------- /istail.py: -------------------------------------------------------------------------------- 1 | from itertools import chain 2 | 3 | def istail(it): 4 | '''Check if iterator has exactly one more element. 5 | Return True/False and iterator.''' 6 | try: 7 | i = next(it) 8 | except StopIteration: 9 | return False, it 10 | 11 | try: 12 | j = next(it) 13 | return False, chain([i, j], it) 14 | except StopIteration: 15 | return True, chain([i], it) 16 | 17 | def ipeek(it): 18 | '''Peek into iterator, return first element and iterator.''' 19 | item = next(it) 20 | return item, chain([item], it) 21 | 22 | def _test(): 23 | t, it = istail(iter([])) 24 | print(t, list(it)) 25 | t, it = istail(iter([1])) 26 | print(t, list(it)) 27 | t, it = istail(iter([1, 2])) 28 | print(t, list(it)) 29 | 30 | items = (i for i in range(5)) 31 | i, items = ipeek(items) 32 | print(i, list(items)) 33 | 34 | if __name__ == '__main__': 35 | _test() 36 | -------------------------------------------------------------------------------- /encrypt: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Encrypt input using gnupg and shred it 4 | # THIS DELETES THE ORIGINAL FILE 5 | 6 | err=0 7 | for file in $@ 8 | do 9 | if [ $file != ${file/.gpg/} ]; then 10 | echo "$file already encrypted" 11 | continue 12 | fi 13 | if [ -d $file ]; then 14 | fname=$file.tar.bz2 15 | tar -cjf $fname $file 16 | find $file -type f -exec shred -u {} \; 17 | rm -r $file 18 | elif [ -f $file ]; then 19 | fname=$file 20 | else 21 | echo "ERROR: Unknown file type $file" 22 | err=1 23 | continue 24 | fi 25 | 26 | gpg -r miki -e $fname 27 | if [ $? -ne 0 ]; then 28 | echo "ERROR: can't encrypt" 29 | err=1 30 | continue 31 | fi 32 | 33 | shred -u $fname 34 | if [ $? -ne 0 ]; then 35 | echo "ERROR: can't shred" 36 | err=1 37 | continue 38 | fi 39 | done 40 | 41 | exit $err 42 | -------------------------------------------------------------------------------- /go-protobuf-json/job.go: -------------------------------------------------------------------------------- 1 | // Example of using Jobs 2 | package main 3 | 4 | import ( 5 | "encoding/json" 6 | "fmt" 7 | 8 | "main/pb" 9 | ) 10 | 11 | func main() { 12 | j1 := &pb.Job{ 13 | User: "Saitama", 14 | Count: 1, 15 | Properties: make(map[string]*pb.Value), 16 | } 17 | 18 | /* Old way before we added SetProperty method 19 | 20 | j1.Properties["retries"] = &pb.Value{ 21 | Value: &pb.Value_Int{Int: 3}, 22 | } 23 | j1.Properties["target"] = &pb.Value{ 24 | Value: &pb.Value_Str{Str: "Metal Knight"}, 25 | } 26 | */ 27 | 28 | j1.SetProperty("retries", 3) 29 | j1.SetProperty("target", "Metal Knight") 30 | 31 | fmt.Println("[j1] ", j1) 32 | 33 | data, err := json.Marshal(j1) 34 | if err != nil { 35 | panic(err) 36 | } 37 | 38 | fmt.Println("[json] ", string(data)) 39 | 40 | j2 := &pb.Job{} 41 | if err := json.Unmarshal(data, j2); err != nil { 42 | panic(err) 43 | } 44 | fmt.Println("[j2] ", j2) 45 | } 46 | -------------------------------------------------------------------------------- /timer.py: -------------------------------------------------------------------------------- 1 | # A simple timer to time portions of your code 2 | 3 | from sys import stdout 4 | from time import time 5 | 6 | class Timer(object): 7 | def __init__(self, name, log=None): 8 | '''name: timer name 9 | log: log function, most probably logger.info 10 | ''' 11 | self.name = name 12 | self.log = log or (lambda message: stdout.write(message + '\n')) 13 | 14 | def __enter__(self): 15 | self.start = time() 16 | self.log('[{}] started'.format(self.name)) 17 | 18 | def __exit__(self, type, value, traceback): 19 | duration = time() - self.start 20 | self.log('[{}] ended in {:.3f} seconds'.format(self.name, duration)) 21 | 22 | 23 | if __name__ == '__main__': 24 | from time import sleep 25 | 26 | # This will print something like 27 | # [test] started 28 | # [test] ended in 1.702 seconds 29 | with Timer('test'): 30 | sleep(1.7) 31 | 32 | -------------------------------------------------------------------------------- /pyhelp: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Show help for Python 4 | 5 | __author__ = "Miki Tebeka " 6 | __license__ = "BSD" 7 | 8 | def main(argv=None): 9 | import sys 10 | from optparse import OptionParser 11 | import webbrowser 12 | 13 | argv = argv or sys.argv 14 | 15 | parser = OptionParser("%prog [MODULE[.FUNCITON]]") 16 | opts, args = parser.parse_args(argv[1:]) 17 | 18 | if len(args) not in (0, 1): 19 | parser.error("wrong number of arguments") # Will exit 20 | 21 | root = "http://docs.python.org" 22 | 23 | if not args: 24 | url = "%s/%s" % (root, "modindex.html") 25 | elif "." in args[0]: 26 | module, function = args[0].split(".") 27 | url = "%s/library/%s.html#%s.%s" % (root, module, module, function) 28 | else: 29 | url = "%s/library/%s.html" % (root, args[0]) 30 | 31 | webbrowser.open(url) 32 | 33 | if __name__ == "__main__": 34 | main() 35 | -------------------------------------------------------------------------------- /nopeek.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Avoiding the need for "peek" with itertools.chain''' 3 | 4 | from itertools import chain 5 | 6 | def is_first_data(line): 7 | return line.startswith("Name:") 8 | 9 | def skip_header(data): 10 | data = iter(data) 11 | for line in data: 12 | if is_first_data(line): 13 | return chain([line], data) 14 | 15 | # FIXME: We might want to raise something here 16 | return [] 17 | 18 | 19 | if __name__ == "__main__": 20 | data = [ 21 | "this is the header", 22 | "it might change every time", 23 | "and you'll never find a good regexp for it", 24 | "The first line of data is easy to know", 25 | "Name: Duffy", 26 | "Type: Duck", 27 | "Anger: 10", 28 | "", 29 | "Name: Bugs", 30 | "Type: Bunny", 31 | "Anger: 0", 32 | ] 33 | 34 | data = skip_header(data) 35 | for line in data: 36 | print line 37 | -------------------------------------------------------------------------------- /tagcloud/tagcloud.mako: -------------------------------------------------------------------------------- 1 | 2 | 3 | Tag Cloud 4 | 25 | 26 | 27 |

Tag Cloud

28 | 29 |
34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /sock_close.py: -------------------------------------------------------------------------------- 1 | """Using contextlib.closing to make sure sockets are closed""" 2 | 3 | from contextlib import closing 4 | from functools import partial 5 | from socket import socket 6 | 7 | import sys 8 | 9 | if sys.version_info[:2] < (3, 0): 10 | from urlparse import urlparse 11 | else: 12 | from urllib.parse import urlparse 13 | 14 | request_template = '''\ 15 | GET %s HTTP/1.1 16 | Host: %s 17 | Connection: close 18 | 19 | ''' 20 | 21 | 22 | def get(url): 23 | """Get web page from URL using "raw" sockets""" 24 | url = urlparse(url) 25 | 26 | request = request_template % (url.path, url.netloc) 27 | sock = socket() 28 | with closing(sock): 29 | sock.connect((url.netloc, 80)) 30 | sock.sendall(request.encode('utf-8')) 31 | data = b''.join(iter(partial(sock.recv, 1024), b'')) 32 | return data 33 | 34 | 35 | if __name__ == '__main__': 36 | data = get('http://httpbin.org/ip') 37 | print(data.decode('utf-8')) 38 | -------------------------------------------------------------------------------- /exiter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | '''Make sure our atexit handler are called even on signals''' 4 | 5 | __author__ = 'Miki Tebeka ' 6 | 7 | from signal import signal, SIGTERM, SIGINT 8 | import atexit 9 | 10 | 11 | def signal_handler(signum, stackframe): 12 | '''Just make sure we exit gracefully''' 13 | raise SystemExit 14 | 15 | 16 | def install_exit_handler(exit_func): 17 | for signum in (SIGTERM, SIGINT): 18 | signal(signum, signal_handler) 19 | atexit.register(exit_func) 20 | 21 | 22 | if __name__ == '__main__': 23 | from os import kill, getpid 24 | 25 | def bye(): 26 | print('BYE') 27 | 28 | install_exit_handler(bye) 29 | 30 | print(''' 31 | Cause suicide is painless 32 | it brings on many changes 33 | and I can take or leave it if I please. 34 | 35 | ...and you can do the same thing if you please. 36 | - M.A.S.H. 37 | ''') 38 | 39 | kill(getpid(), SIGTERM) 40 | -------------------------------------------------------------------------------- /feedme/feed.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import feedparser 4 | from cgi import FieldStorage, escape 5 | from time import ctime 6 | 7 | ENTRY_TEMPLATE = ''' 8 | 13 | %(title)s 14 |
15 |
16 | %(summary)s 17 |
18 | ''' 19 | 20 | def main(): 21 | print "Content-type: text/html\n" 22 | 23 | form = FieldStorage() 24 | url = form.getvalue("url", "") 25 | if not url: 26 | raise SystemExit("error: not url given") 27 | 28 | feed = feedparser.parse(url) 29 | for enum, entry in enumerate(feed.entries): 30 | entry.eid = "entry%d" % enum 31 | try: 32 | html = ENTRY_TEMPLATE % entry 33 | print html 34 | except Exception, e: 35 | # FIXME: Log errors 36 | pass 37 | 38 | print "
%s" % ctime() 39 | 40 | if __name__ == "__main__": 41 | main() 42 | 43 | -------------------------------------------------------------------------------- /daemon: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Little utility to wrap a sever application with daemon like properties 4 | # (detach from tty ...) 5 | 6 | # Add the following to your crontab 7 | # @reboot (cd /path/to/my/app && ./daemon start) 8 | 9 | pidfile=mycoolapp.pid 10 | 11 | start() { 12 | stop 13 | if [ -f nohup.out ]; then 14 | mv nohup.out nohup.out.$(date +%Y%m%d%H%M%S) 15 | fi 16 | nohup python ./mycoolapp.py & 17 | echo $! > $pidfile 18 | } 19 | 20 | stop() { 21 | if [ ! -f $pidfile ]; then 22 | return 23 | fi 24 | 25 | pid=$(cat $pidfile) 26 | echo "killing $pid" 27 | kill $pid 28 | rm $pidfile 29 | } 30 | 31 | if [ $# -ne 1 ]; then 32 | echo "usage: $(basename $0) start/stop/restart" 33 | exit 1 34 | fi 35 | 36 | case $1 in 37 | -h | --help ) echo "usage: $(basename $0) start/stop/restart"; exit;; 38 | start|restart ) start;; 39 | stop ) stop;; 40 | * ) echo "error: unknown command - $1"; exit 1;; 41 | esac 42 | -------------------------------------------------------------------------------- /bbot.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Watch builds on buildbot and publish to twitter 3 | 4 | from time import time, sleep 5 | from xmlrpclib import ServerProxy 6 | from urllib import urlopen 7 | 8 | user, password = "tebeka", "SECRET_PASSWORD" 9 | bbot_url = "http://buildbot.example.com/xmlrpc" 10 | tweet_url = "http://%s:%s@twitter.com/statuses/update.xml" % (user, password) 11 | 12 | def main(): 13 | proxy = ServerProxy(bbot_url) 14 | last_time = time() 15 | 16 | while 1: 17 | now = time() 18 | builds = proxy.getAllBuildsInInterval(last_time, now) 19 | for build in builds: 20 | builder, build, status = build[0], build[1], build[5] 21 | status = "OK" if status == "success" else "BROKEN" 22 | message = "[%s] build %s is %s" % (builder, build, status) 23 | urlopen(tweet_url, "status=%s" % message) 24 | 25 | last_time = now if builds else last_time 26 | sleep(10) 27 | 28 | if __name__ == "__main__": 29 | main() 30 | -------------------------------------------------------------------------------- /mock.py: -------------------------------------------------------------------------------- 1 | class mock: 2 | '''Super simple mocking context manager. 3 | Example:: 4 | 5 | import foo 6 | from cStringIO import StringIO 7 | from mock import mock 8 | 9 | def test_get_data(): 10 | expected = 'Mary had a little lamb' 11 | with mock(foo, urlopen=lambda url: StringIO(expected)): 12 | result = foo.get_data('http://google.com') 13 | assert result == expected, 'bad data' 14 | ''' 15 | def __init__(self, obj, **kw): 16 | '''Create mock object 17 | obj - Object to be mocked 18 | kw - Mocked attributes 19 | ''' 20 | self.obj = obj 21 | self.mocks = kw.copy() 22 | 23 | def __enter__(self): 24 | self.orig = self.obj.__dict__.copy() 25 | self.obj.__dict__.update(self.mocks) 26 | return self 27 | 28 | def __exit__(self, type, value, trackback): 29 | self.obj.__dict__.update(self.orig) 30 | -------------------------------------------------------------------------------- /putpath: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Put a path in clipboard''' 3 | 4 | __author__ = "Miki Tebeka " 5 | 6 | from os import popen 7 | from os.path import abspath, exists 8 | 9 | def putpath(path): 10 | # putclip is another utility here 11 | pipe = popen("putclip", "w") 12 | pipe.write(abspath(path)) 13 | 14 | def main(argv=None): 15 | if argv is None: 16 | import sys 17 | argv = sys.argv 18 | 19 | from optparse import OptionParser 20 | 21 | # Command line parsing 22 | p = OptionParser("usage: %prog [PATH]") 23 | 24 | opts, args = p.parse_args(argv[1:]) 25 | if len(args) not in (0, 1): 26 | p.error("wrong number of arguments") # Will exit 27 | 28 | if len(args) == 0: 29 | path = "." 30 | else: 31 | path = args[0] 32 | 33 | if not exists(path): 34 | raise SystemExit("error: %s - no such file or directory" % path) 35 | 36 | putpath(path) 37 | 38 | if __name__ == "__main__": 39 | main() 40 | -------------------------------------------------------------------------------- /github-url: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Show github URL for a given file 3 | 4 | if [ $# -ne 1 ]; then 5 | echo "usage: $(basename $0) FILENAME" 6 | exit 1 7 | fi 8 | 9 | if [ ! -e $1 ]; then 10 | echo "error: can't find $1" 11 | exit 1 12 | fi 13 | 14 | if [ -d $1 ]; then 15 | cd $1 16 | dest=. 17 | else 18 | cd $(dirname $1) 19 | dest=$(basename $1) 20 | fi 21 | 22 | url=$(git config remote.origin.url | cut -d: -f2) 23 | if [ $? -ne 0 ]; then 24 | echo "error: can't get remote url" 25 | exit 1 26 | fi 27 | url=${url/.git/} 28 | 29 | path=$(git ls-files --full-name $dest | head -1) 30 | if [ $? -ne 0 ]; then 31 | echo "error: git does not know about $1" 32 | exit 1 33 | fi 34 | 35 | branch=$(git branch --no-color 2>/dev/null | grep '^* ' | sed 's/* //') 36 | prefix="https://github.com/$url" 37 | if [ "$dest" == "." ]; then 38 | path=$(dirname $path) 39 | url="${prefix}/tree/${branch}/$path" 40 | else 41 | url="${prefix}/blob/${branch}/$path" 42 | fi 43 | 44 | echo $url 45 | -------------------------------------------------------------------------------- /start: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Open a file from command line, multi OS 3 | 4 | # Miki Tebeka 5 | 6 | if [ $# -ne 1 ]; then 7 | echo "usage: `basename $0` PATH" 8 | exit 1 9 | fi 10 | 11 | # Taken from http://pypi.python.org/pypi/desktop/0.3 12 | linux_open() 13 | { 14 | if [ -n "$GNOME_DESKTOP_SESSION_ID" ]; then 15 | if which gvfs-open > /dev/null; then 16 | echo "gvfs-open" 17 | else 18 | echo "gnome-open" 19 | fi 20 | elif [ -n "$KDE_FULL_SESSION" ]; then 21 | echo "kfmclient exec" 22 | elif xprop -root _DT_SAVE_MODE 2>&1 | grep -iq xfce; then 23 | echo "exo-open" 24 | else 25 | echo "" 26 | fi 27 | } 28 | 29 | case `uname` in 30 | Linux) open=`linux_open`;; 31 | Darwin) open=open;; 32 | CYGWIN*) open=cygstart;; 33 | MINGW*) open=start;; 34 | esac 35 | 36 | if [ -z "$open" ]; then 37 | echo "error: no start program for `uname` platform" 1>&2 38 | exit 1 39 | fi 40 | 41 | $open "$1" > /dev/null 2>&1 42 | -------------------------------------------------------------------------------- /gae/gae.py: -------------------------------------------------------------------------------- 1 | # Script for "booting" AppEngine environment. 2 | # Place this at ~/.ipython and then "import gae; gae.setup()" from ipython shell 3 | 4 | # http://tech.einaregilsson.com/2010/10/11/unit-testing-model-classes-in-google-app-engine/#more-416 5 | def setup(dbfile=None, fresh=0): 6 | from os.path import isfile 7 | import yaml 8 | from os import environ, remove 9 | 10 | appfile = "app.yaml" 11 | 12 | if isfile(appfile): 13 | doc = yaml.load(open(appfile)) 14 | app_id = doc["application"] 15 | else: 16 | app_id = "dummyapp" 17 | 18 | dbfile = dbfile or "/tmp/dev_appserver.datastore" 19 | 20 | environ["APPLICATION_ID"] = app_id 21 | if fresh and isfile(dbfile): 22 | remove(dbfile) 23 | 24 | from google.appengine.api import apiproxy_stub_map,datastore_file_stub 25 | apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap() 26 | stub = datastore_file_stub.DatastoreFileStub(app_id, dbfile, '/') 27 | apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub) 28 | -------------------------------------------------------------------------------- /push-cgi/push: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from urllib import urlopen, urlencode 4 | 5 | CGI_URL = "http://localhost:8080/load.cgi" 6 | def push_data(key, data): 7 | query = urlencode([("data", data), ("key", key)]) 8 | try: 9 | urlopen(CGI_URL, query).read() 10 | except IOError, e: 11 | pass # FIXME: Handle error 12 | 13 | def main(argv=None): 14 | if argv is None: 15 | import sys 16 | argv = sys.argv 17 | 18 | from optparse import OptionParser 19 | from os.path import isfile, basename 20 | 21 | parser = OptionParser("usage: %prog FILENAME") 22 | 23 | opts, args = parser.parse_args(argv[1:]) 24 | if len(args) != 1: 25 | parser.error("wrong number of arguments") # Will exit 26 | 27 | filename = args[0] 28 | if not isfile(filename): 29 | raise SystemExit("error: can't find %s" % filename) 30 | 31 | key = basename(filename) 32 | data = open(filename, "rb").read() 33 | 34 | push_data(key, data) 35 | 36 | 37 | if __name__ == "__main__": 38 | main() 39 | -------------------------------------------------------------------------------- /pkg-urls.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Show download url for installed packages.''' 3 | 4 | from pip.util import get_installed_distributions 5 | 6 | 7 | def download_url(dist): 8 | '''Get download url from distribution PKG-INFO file.''' 9 | for line in dist._get_metadata('PKG-INFO'): 10 | if line.startswith('Download-URL:'): 11 | return line.split(':', 1)[1] 12 | 13 | 14 | def main(argv=None): 15 | import sys 16 | from argparse import ArgumentParser 17 | 18 | argv = argv or sys.argv 19 | 20 | parser = ArgumentParser( 21 | description='show download urls for installed packages') 22 | parser.add_argument('--unknown', help='show unknown urls', 23 | action='store_true', default=False) 24 | args = parser.parse_args(argv[1:]) 25 | 26 | for dist in get_installed_distributions(): 27 | url = download_url(dist) 28 | if url or args.unknown: 29 | print('{}: {}'.format(dist.project_name, url or 'UNKNOWN')) 30 | 31 | 32 | if __name__ == '__main__': 33 | main() 34 | -------------------------------------------------------------------------------- /pool.go: -------------------------------------------------------------------------------- 1 | // Pool in go using buffer channels 2 | package main 3 | 4 | import ( 5 | "fmt" 6 | "math/rand" 7 | "sync" 8 | "time" 9 | ) 10 | 11 | // worker simulates work of a goroutine 12 | func worker(id int, pool chan int, start chan bool, wg *sync.WaitGroup) { 13 | <-start // Wait for all goroutines 14 | rsc := <-pool // Get item from the pool 15 | defer func() { pool <- rsc }() // Return item at end 16 | defer wg.Done() // Signal we're dong 17 | 18 | time.Sleep(time.Duration(rand.Int()%100) * time.Millisecond) 19 | fmt.Printf("worker %d got resource %d\n", id, rsc) 20 | } 21 | 22 | func main() { 23 | var wg sync.WaitGroup 24 | start := make(chan bool) 25 | 26 | // Create and fill pool 27 | pool := make(chan int, 3) 28 | for i := 0; i < 3; i++ { 29 | pool <- i 30 | } 31 | 32 | // Run workers 33 | for i := 0; i < 10; i++ { 34 | wg.Add(1) 35 | go worker(i, pool, start, &wg) 36 | } 37 | 38 | close(start) // Signal to start 39 | wg.Wait() // Wait for goroutines to finish before exiting 40 | } 41 | -------------------------------------------------------------------------------- /ll-freq.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Show last letter frequency in English words''' 3 | 4 | from urllib import urlopen 5 | from collections import Counter 6 | from matplotlib import pyplot as plt 7 | import numpy as np 8 | 9 | 10 | def get_words(): 11 | '''Get list of dictionary words.''' 12 | # The North American Scrabble Tournament Word List (178,690 words) 13 | fo = urlopen('http://norvig.com/ngrams/TWL06.txt') 14 | return (w.strip() for w in fo) 15 | 16 | 17 | def plot(freq, filename): 18 | '''Bar chart of last letter frequency.''' 19 | width = 0.35 # Bar width (also used for xticks positioning) 20 | 21 | idx = np.arange(len(freq)) 22 | xs = sorted(freq) # Letters in alphabetic order 23 | ys = [freq[x] for x in xs] # Frequency 24 | 25 | plt.bar(idx, ys, width=width) 26 | plt.xticks(idx + width/2., xs) # Annotate X axis 27 | plt.title('Last Letter Frequency') 28 | plt.savefig(filename) 29 | 30 | 31 | if __name__ == '__main__': 32 | freq = Counter(w[-1].upper() for w in get_words()) 33 | plot(freq, 'll-freq.png') 34 | -------------------------------------------------------------------------------- /affinity.go: -------------------------------------------------------------------------------- 1 | /* CPU affinity in Go */ 2 | 3 | package main 4 | 5 | import ( 6 | "fmt" 7 | "math/rand" 8 | "os" 9 | "runtime" 10 | "time" 11 | ) 12 | 13 | /* 14 | #define _GNU_SOURCE 15 | #include 16 | #include 17 | 18 | void lock_thread(int cpuid) { 19 | pthread_t tid; 20 | cpu_set_t cpuset; 21 | 22 | tid = pthread_self(); 23 | CPU_ZERO(&cpuset); 24 | CPU_SET(cpuid, &cpuset); 25 | pthread_setaffinity_np(tid, sizeof(cpu_set_t), &cpuset); 26 | } 27 | */ 28 | import "C" 29 | 30 | func randSleep() { 31 | time.Sleep(time.Duration(rand.Intn(300)) * time.Millisecond) 32 | } 33 | 34 | func setAffinity(cpuID int) { 35 | runtime.LockOSThread() 36 | C.lock_thread(C.int(cpuID)) 37 | } 38 | 39 | func worker(id int, lock bool) { 40 | if lock { 41 | setAffinity(id) 42 | } 43 | 44 | for { 45 | fmt.Printf("worker: %d, CPU: %d\n", id, C.sched_getcpu()) 46 | randSleep() 47 | } 48 | } 49 | 50 | func main() { 51 | lock := len(os.Getenv("LOCK")) > 0 52 | for i := 0; i < runtime.NumCPU(); i++ { 53 | go worker(i, lock) 54 | } 55 | time.Sleep(2 * time.Second) 56 | } 57 | -------------------------------------------------------------------------------- /cheetah-demo/Cheetah/Utils/optik/__init__.py: -------------------------------------------------------------------------------- 1 | """optik 2 | 3 | A powerful, extensible, and easy-to-use command-line parser for Python. 4 | 5 | By Greg Ward 6 | 7 | See http://optik.sourceforge.net/ 8 | 9 | Cheetah modifications: added "Cheetah.Utils.optik." prefix to 10 | all intra-Optik imports. 11 | """ 12 | 13 | # Copyright (c) 2001 Gregory P. Ward. All rights reserved. 14 | # See the README.txt distributed with Optik for licensing terms. 15 | 16 | __revision__ = "$Id: __init__.py,v 1.2 2002/09/12 06:56:51 hierro Exp $" 17 | 18 | __version__ = "1.3" 19 | 20 | 21 | # Re-import these for convenience 22 | from Cheetah.Utils.optik.option import Option 23 | from Cheetah.Utils.optik.option_parser import \ 24 | OptionParser, SUPPRESS_HELP, SUPPRESS_USAGE, STD_HELP_OPTION 25 | from Cheetah.Utils.optik.errors import OptionValueError 26 | 27 | 28 | # Some day, there might be many Option classes. As of Optik 1.3, the 29 | # preferred way to instantiate Options is indirectly, via make_option(), 30 | # which will become a factory function when there are many Option 31 | # classes. 32 | make_option = Option 33 | -------------------------------------------------------------------------------- /match.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Support search syntax 4 | ''' 5 | >>> match("a", "a") 6 | True 7 | >>> match("a", "") 8 | False 9 | >>> match("a AND b", "a c b") 10 | True 11 | >>> match("a AND b", "a c") 12 | False 13 | >>> match("NOT ( a OR b )", "z") 14 | True 15 | >>> match("a OR b", "b") 16 | True 17 | ''' 18 | 19 | def is_operator(token): 20 | return token in set(["and", "not", "or", "(", ")"]) 21 | 22 | def should_insert_and(expr, token): 23 | if not expr: 24 | return 0 25 | 26 | if is_operator(expr[-1]): 27 | return 0 28 | 29 | if is_operator(token): 30 | return 0 31 | 32 | return 1 33 | 34 | def match(query, text): 35 | words = set(text.lower().split()) 36 | 37 | expr = [] 38 | for token in query.lower().split(): 39 | 40 | if should_insert_and(expr, token): 41 | expr.append("and") 42 | 43 | if is_operator(token): 44 | expr.append(token) 45 | else: 46 | expr.append(token in words) 47 | 48 | py_expr = " ".join(map(str, expr)) 49 | return eval(py_expr) 50 | 51 | if __name__ == "__main__": 52 | from doctest import testmod 53 | testmod() 54 | -------------------------------------------------------------------------------- /putclip: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Place stuff in clipboard - multi platform''' 3 | 4 | __author__ = "Miki Tebeka " 5 | 6 | from os import popen 7 | from sys import platform, stdin 8 | 9 | COMMDANDS = { # platform -> command 10 | "darwin" : "pbcopy", 11 | "linux2" : "xsel -i", 12 | "cygwin" : "/bin/putclip", 13 | } 14 | 15 | USAGE = '''usage: putclip [PATH] 16 | ''' 17 | 18 | def main(): 19 | from sys import argv 20 | 21 | if len(argv) not in (1, 2): 22 | raise SystemExit(USAGE) 23 | 24 | if (len(argv) == 1) or (argv[1] == "-"): 25 | from sys import stdin 26 | info = stdin 27 | else: 28 | infile = argv[1] 29 | try: 30 | info = open(infile) 31 | except IOError, e: 32 | raise SystemExit("error: can't open %s - %s" % (infile, e)) 33 | 34 | command = COMMDANDS.get(platform, None) 35 | if not command: 36 | error = "error: don't know how to handle clipboard on %s" % platform 37 | raise SystemExit(error) 38 | 39 | popen(command, "w").write(info.read()) 40 | 41 | if __name__ == "__main__": 42 | main() 43 | -------------------------------------------------------------------------------- /playwave.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Play a wav file on Linux 3 | 4 | Freely copied from 5 | http://www.velocityreviews.com/forums/t337346-how-to-play-sound-in-python.html 6 | ''' 7 | 8 | import wave 9 | import ossaudiodev 10 | 11 | def playwave(wavefile): 12 | fo = wave.open(wavefile, "rb") 13 | (nc, sw, fr, nf, comptype, compname) = fo.getparams() 14 | dsp = ossaudiodev.open("/dev/dsp", "w") 15 | dsp.setparameters(ossaudiodev.AFMT_S16_NE, nc, fr) 16 | data = fo.readframes(nf) 17 | fo.close() 18 | dsp.write(data) 19 | dsp.close() 20 | 21 | def main(argv=None): 22 | import sys 23 | from optparse import OptionParser 24 | from os.path import isfile 25 | 26 | argv = argv or sys.argv 27 | 28 | parser = OptionParser("%prog WAVEFILE") 29 | opts, args = parser.parse_args(argv[1:]) 30 | if len(args) != 1: 31 | parser.error("wrong number of arguments") # Will exit 32 | 33 | wavefile = args[0] 34 | if not isfile(wavefile): 35 | raise SystemExit("error: can't find %s" % wavefile) 36 | 37 | 38 | playwave(wavefile) 39 | 40 | if __name__ == "__main__": 41 | main() 42 | 43 | -------------------------------------------------------------------------------- /plot2d.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Quick and dirty way to show your data.''' 3 | 4 | import matplotlib.pyplot as plt 5 | from itertools import cycle 6 | from sklearn.decomposition import PCA 7 | 8 | def plot2d(data, target, target_names=None): 9 | '''Show data reduced to 2 dimensions as scatter plot. 10 | Colors mark the different labels. 11 | ''' 12 | pca = PCA(n_components=2, whiten=True) 13 | compressed = pca.fit_transform(data) 14 | target_names = set(target) if target_names is None else target_names 15 | colors = cycle('rgbcmykw') 16 | target_ids = range(len(target_names)) 17 | fig = plt.figure() 18 | ax = fig.add_subplot(1, 1, 1) 19 | for i, c, label in zip(target_ids, colors, target_names): 20 | ax.scatter(compressed[target == i, 0], compressed[target == i, 1], 21 | c=c, label=label) 22 | ax.legend() 23 | return fig 24 | 25 | 26 | if __name__ == '__main__': 27 | from sklearn.datasets import load_iris 28 | iris = load_iris() 29 | fig = plot2d(iris.data, iris.target, iris.target_names) 30 | plt.show() # We use plt.show so it'll block until the image is closed 31 | -------------------------------------------------------------------------------- /feedme/web-server: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Minimal Web Server''' 3 | 4 | __author__ = "Miki Tebeka " 5 | 6 | from CGIHTTPServer import CGIHTTPRequestHandler 7 | from BaseHTTPServer import HTTPServer 8 | 9 | def main(argv=None): 10 | if argv is None: 11 | import sys 12 | argv = sys.argv 13 | 14 | from optparse import OptionParser 15 | 16 | parser = OptionParser("usage: %prog [PORT]") 17 | 18 | opts, args = parser.parse_args(argv[1:]) 19 | if len(args) not in (0, 1): 20 | parser.error("wrong number of arguments") # Will exit 21 | 22 | port = int(args[0]) if args else 8080 23 | 24 | # Monkey patch the default CGI request handler 25 | def is_cgi(self): 26 | if not self.path.endswith(".cgi"): 27 | return 0 28 | self.cgi_info = ("", self.path[1:]) 29 | return 1 30 | CGIHTTPRequestHandler.is_cgi = is_cgi 31 | CGIHTTPRequestHandler.cgi_directories = [""] 32 | 33 | server = HTTPServer(("", port), CGIHTTPRequestHandler) 34 | 35 | print "Serving CGI on port %d" % port 36 | server.serve_forever() 37 | 38 | if __name__ == "__main__": 39 | main() 40 | -------------------------------------------------------------------------------- /jsonpretty: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''JSON prettifier''' 3 | 4 | def shift(array): 5 | try: 6 | return array.pop(0) 7 | except IndexError: 8 | return None 9 | 10 | def fopen(name, mode, default): 11 | if name in (None, "-"): 12 | return default 13 | return open(name, mode) 14 | 15 | def main(argv=None): 16 | import sys 17 | from optparse import OptionParser 18 | 19 | argv = argv or sys.argv 20 | 21 | indent = 4 22 | parser = OptionParser("%prog [INFILE [OUTFILE]]") 23 | parser.add_option("-i", "--indent", help="indent size (%s)" % indent, 24 | dest="indent", default=indent, type="int") 25 | opts, args = parser.parse_args(argv[1:]) 26 | if len(args) not in (0, 1, 2): 27 | parser.error("wrong number of arguments") # Will exit 28 | 29 | try: 30 | info = fopen(shift(args), "r", sys.stdin) 31 | outfo = fopen(shift(args), "w", sys.stdout) 32 | except IOError, e: 33 | raise SystemExit("error: %s" % e) 34 | 35 | import json 36 | json.dump(json.load(info), outfo, indent=opts.indent) 37 | 38 | if __name__ == "__main__": 39 | main() 40 | -------------------------------------------------------------------------------- /push-cgi/web-server: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Minimal Web Server''' 3 | 4 | __author__ = "Miki Tebeka " 5 | 6 | from CGIHTTPServer import CGIHTTPRequestHandler 7 | from BaseHTTPServer import HTTPServer 8 | 9 | def main(argv=None): 10 | if argv is None: 11 | import sys 12 | argv = sys.argv 13 | 14 | from optparse import OptionParser 15 | 16 | parser = OptionParser("usage: %prog [PORT]") 17 | 18 | opts, args = parser.parse_args(argv[1:]) 19 | if len(args) not in (0, 1): 20 | parser.error("wrong number of arguments") # Will exit 21 | 22 | port = int(args[0]) if args else 8080 23 | 24 | # Monkey patch the default CGI request handler 25 | def is_cgi(self): 26 | if not self.path.endswith(".cgi"): 27 | return 0 28 | self.cgi_info = ("", self.path[1:]) 29 | return 1 30 | CGIHTTPRequestHandler.is_cgi = is_cgi 31 | CGIHTTPRequestHandler.cgi_directories = [""] 32 | 33 | server = HTTPServer(("", port), CGIHTTPRequestHandler) 34 | 35 | print "Serving CGI on port %d" % port 36 | server.serve_forever() 37 | 38 | if __name__ == "__main__": 39 | main() 40 | -------------------------------------------------------------------------------- /websession/web-server: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Minimal Web Server''' 3 | 4 | __author__ = "Miki Tebeka " 5 | 6 | from CGIHTTPServer import CGIHTTPRequestHandler 7 | from BaseHTTPServer import HTTPServer 8 | 9 | def main(argv=None): 10 | if argv is None: 11 | import sys 12 | argv = sys.argv 13 | 14 | from optparse import OptionParser 15 | 16 | parser = OptionParser("usage: %prog [PORT]") 17 | 18 | opts, args = parser.parse_args(argv[1:]) 19 | if len(args) not in (0, 1): 20 | parser.error("wrong number of arguments") # Will exit 21 | 22 | port = int(args[0]) if args else 8080 23 | 24 | # Monkey patch the default CGI request handler 25 | def is_cgi(self): 26 | if not self.path.endswith(".py"): 27 | return 0 28 | self.cgi_info = ("", self.path[1:]) 29 | return 1 30 | CGIHTTPRequestHandler.is_cgi = is_cgi 31 | CGIHTTPRequestHandler.cgi_directories = [""] 32 | 33 | server = HTTPServer(("", port), CGIHTTPRequestHandler) 34 | 35 | print "Serving CGI on port %d" % port 36 | server.serve_forever() 37 | 38 | if __name__ == "__main__": 39 | main() 40 | -------------------------------------------------------------------------------- /cheetah-demo/web-server: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Minimal Web Server''' 3 | 4 | __author__ = "Miki Tebeka " 5 | 6 | from CGIHTTPServer import CGIHTTPRequestHandler 7 | from BaseHTTPServer import HTTPServer 8 | 9 | def main(argv=None): 10 | if argv is None: 11 | import sys 12 | argv = sys.argv 13 | 14 | from optparse import OptionParser 15 | 16 | parser = OptionParser("usage: %prog [PORT]") 17 | 18 | opts, args = parser.parse_args(argv[1:]) 19 | if len(args) not in (0, 1): 20 | parser.error("wrong number of arguments") # Will exit 21 | 22 | port = int(args[0]) if args else 8080 23 | 24 | # Monkey patch the default CGI request handler 25 | def is_cgi(self): 26 | if not self.path.endswith(".cgi"): 27 | return 0 28 | self.cgi_info = ("", self.path[1:]) 29 | return 1 30 | CGIHTTPRequestHandler.is_cgi = is_cgi 31 | CGIHTTPRequestHandler.cgi_directories = [""] 32 | 33 | server = HTTPServer(("", port), CGIHTTPRequestHandler) 34 | 35 | print "Serving CGI on port %d" % port 36 | server.serve_forever() 37 | 38 | if __name__ == "__main__": 39 | main() 40 | -------------------------------------------------------------------------------- /fade.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 39 | 40 | -------------------------------------------------------------------------------- /sumtuples.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Summarize tuples by key using a sqlite3''' 3 | 4 | import sqlite3 5 | 6 | def sumtuples(data, key_index): 7 | # In memory database 8 | cur = sqlite3.connect(':memory:').cursor() 9 | cols = ['i{0}'.format(i) for i, _ in enumerate(data[0])] 10 | 11 | # Create table 12 | schema = 'CREATE TABLE items ({0})'.format(','.join(cols)) 13 | cur.execute(schema) 14 | cur.connection.commit() 15 | 16 | # Insert data 17 | sql = 'INSERT INTO items VALUES ({0})'.format(','.join('?' for _ in cols)) 18 | cur.executemany(sql, data) 19 | cur.connection.commit() 20 | 21 | # SUM and GROUP BY 22 | key = cols[key_index] 23 | summed = ','.join(['SUM({0})'.format(col) for col in cols if col != key]) 24 | sql = 'SELECT {0}, {1} FROM items GROUP BY {0}'.format(key, summed) 25 | 26 | cur.execute(sql) 27 | # Cursor object is iterable, return it 28 | return cur 29 | 30 | if __name__ == '__main__': 31 | # Small test case 32 | data = [ 33 | ('a', 1, 2), 34 | ('b', 2, 3), 35 | ('a', 1, 2), 36 | ('b', 2, 3) 37 | ] 38 | 39 | print(list(sumtuples(data, 0))) 40 | -------------------------------------------------------------------------------- /named_decorator.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Simplify creating decorators with optional parameters''' 3 | 4 | from functools import wraps 5 | from time import ctime 6 | 7 | def logged(fn, name=None): 8 | '''Decorator to "log" function start and end. It can be used either in 9 | "bare" format 10 | @logged 11 | def f(): 12 | pass 13 | 14 | or with a "name" parameter 15 | @logged("foo") 16 | def f(): 17 | pass 18 | ''' 19 | 20 | # @logged("foo") used 21 | if isinstance(fn, basestring): 22 | return lambda f: logged(f, fn) 23 | 24 | # @logged used 25 | name = name or "{0}.{1}".format(__name__, fn.__name__) 26 | 27 | @wraps(fn) 28 | def wrapped(*args, **kw): 29 | print("[{0}] - {1} - START".format(ctime(), name)) 30 | value = fn(*args, **kw) 31 | print("[{0}] - {1} - END".format(ctime(), name)) 32 | 33 | return value 34 | return wrapped 35 | 36 | if __name__ == "__main__": 37 | @logged("blah") 38 | def f(x): 39 | print("f: {0}".format(x)) 40 | 41 | @logged 42 | def g(x): 43 | print("g: {0}".format(x)) 44 | 45 | 46 | f(8) 47 | g(7) 48 | -------------------------------------------------------------------------------- /cheetah-demo/Cheetah/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # $Id: __init__.py,v 1.10 2006/01/14 04:44:07 tavis_rudd Exp $ 3 | 4 | """Cheetah is an open source template engine and code generation tool. 5 | 6 | It can be used standalone or combined with other tools and frameworks. Web 7 | development is its principle use, but Cheetah is very flexible and is also being 8 | used to generate C++ game code, Java, sql, form emails and even Python code. 9 | 10 | Homepage 11 | ================================================================================ 12 | http://www.CheetahTemplate.org/ 13 | 14 | Documentation 15 | ================================================================================ 16 | For a high-level introduction to Cheetah please refer to the User's Guide 17 | at http://cheetahtemplate.org/learn.html 18 | 19 | Mailing list 20 | ================================================================================ 21 | cheetahtemplate-discuss@lists.sourceforge.net 22 | Subscribe at http://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss 23 | """ 24 | __author__ = "Tavis Rudd " 25 | __revision__ = "$Revision: 1.10 $"[11:-2] 26 | 27 | from Version import Version 28 | -------------------------------------------------------------------------------- /git-hooks/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Pyflakes pre commit hook 3 | 4 | # First commit ever, skip 5 | git rev-parse HEAD > /dev/null 2>&1 6 | if [ $? -ne 0 ]; then 7 | exit 8 | fi 9 | 10 | if [ "$(uname -s)" == "Darwin" ]; then 11 | tmp=$(mktemp -t tmp) 12 | else 13 | tmp=$(mktemp) 14 | fi 15 | out=/tmp/pyflakes.out 16 | 17 | error=0 18 | for filename in $(git diff --cached --name-only HEAD | egrep '\.py$'); 19 | do 20 | # Check that file exists, so we ignore deletes and renames 21 | if [ ! -f $filename ]; then 22 | continue 23 | fi 24 | 25 | echo "Checking $filename ..." 26 | 27 | # Some magic to check the file in staging area and not the ones on disk 28 | git show ":${filename}" > $tmp 29 | 30 | if grep -q -P '\t' $tmp; then 31 | echo "error: Found tabs" 32 | error=1 33 | fi 34 | 35 | pyflakes $tmp 2>&1 > $out 36 | ok=$? 37 | sed "s/^${tmp//\//\\/}/${filename//\//\\/}/" $out 38 | 39 | if [ $ok -ne 0 ]; then 40 | error=1 41 | fi 42 | done 43 | 44 | rm $tmp 45 | 46 | if [ $error -ne 0 ]; then 47 | echo 48 | echo "Aborting commit (use 'git commit --no-verify' to bypass)" 49 | exit 1 50 | fi 51 | -------------------------------------------------------------------------------- /present-timer/timer.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Timer 5 | 15 | 16 | 17 | 18 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /macapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | HumbleCalc 4 | 14 | 15 | 16 |

HumbleCalc

17 | = 18 | 19 | 20 | 21 | 43 | 44 | -------------------------------------------------------------------------------- /mpweb.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | '''Serving dynamic images with matplotlib (using flask). 3 | 4 | The data is served inside the tag, no need for extra resource 5 | handler. 6 | ''' 7 | 8 | # No windows should pop up in a web server 9 | import matplotlib 10 | matplotlib.use('Agg') 11 | 12 | import matplotlib.pyplot as plt 13 | import numpy as np 14 | from cStringIO import StringIO 15 | 16 | from flask import Flask 17 | app = Flask(__name__) 18 | 19 | # HTML template, we embed base64 encoded image data in the HTML element 20 | html = ''' 21 | 22 | 23 | 24 | 25 | 26 | ''' 27 | 28 | @app.route("/") 29 | def index(): 30 | 31 | # Plot sin and cos between -10 and 10 (1000 points) 32 | fig = plt.figure() 33 | ax = fig.add_subplot(1, 1, 1) 34 | xs = np.linspace(-10, 10, 1000) 35 | ax.plot(xs, np.sin(xs), label='sin(x)') 36 | ax.plot(xs, np.cos(xs), label='cos(x)') 37 | ax.legend() 38 | 39 | # Encode image to png in base64 40 | io = StringIO() 41 | fig.savefig(io, format='png') 42 | data = io.getvalue().encode('base64') 43 | 44 | return html.format(data) 45 | 46 | 47 | if __name__ == '__main__': 48 | app.run() 49 | -------------------------------------------------------------------------------- /gcalc: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Command line interface to Google calculator 3 | gcalc 100f c -> 37.7777778 degrees Celsius 4 | ''' 5 | 6 | # Idea taken from http://bit.ly/dVL4H3 7 | 8 | import json 9 | from urllib import urlopen 10 | import re 11 | 12 | 13 | def main(argv=None): 14 | import sys 15 | from optparse import OptionParser 16 | 17 | argv = argv or sys.argv 18 | 19 | parser = OptionParser("%prog FROM TO\n\t%prog 100f c") 20 | opts, args = parser.parse_args(argv[1:]) 21 | if len(args) != 2: 22 | parser.error("wrong number of arguments") # Will exit 23 | 24 | url = "http://www.google.com/ig/calculator?q=%s=?%s" % tuple(args) 25 | try: 26 | # We decode to UTF-8 since Google sometimes return funny stuff 27 | result = urlopen(url).read().decode("utf-8", "ignore") 28 | # Convert to valid JSON: {foo: "1"} -> {"foo" : "1"} 29 | result = re.sub("([a-z]+):", '"\\1" :', result) 30 | result = json.loads(result) 31 | except (ValueError, IOError) as err: 32 | raise SystemExit("error: %s" % err) 33 | 34 | if result["error"]: 35 | raise SystemExit("error: %s" % result["error"]) 36 | 37 | print(result["rhs"]) 38 | 39 | 40 | if __name__ == "__main__": 41 | main() 42 | -------------------------------------------------------------------------------- /sqldir.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | from glob import iglob 3 | import re 4 | 5 | 6 | class SQLDir(dict): 7 | """Fill attribute values from SQL files in a directory""" 8 | def __init__(self, root): 9 | self._root = root 10 | 11 | def __getattr__(self, attr): 12 | try: 13 | return self[attr] 14 | except KeyError: 15 | raise AttributeError(attr) 16 | 17 | def __missing__(self, key): 18 | try: 19 | with open(path.join(self._root, key + '.sql')) as fp: 20 | val = self[key] = fp.read() 21 | return val 22 | except OSError: 23 | raise KeyError(key) 24 | 25 | def __dir__(self): 26 | files = iglob(path.join(self._root, '*.sql')) 27 | bases = (path.basename(file) for file in files) 28 | return sorted(re.sub('.sql$', '', base) for base in bases) 29 | 30 | 31 | def example_usage(): 32 | import sqlite3 33 | from os import path 34 | 35 | here = path.dirname(path.realpath(__file__)) 36 | sqld = SQLDir(path.join(here, 'sql')) 37 | 38 | conn = sqlite3.connect(':memory:') 39 | if conn.execute(sqld.table_exists).fetchone() is None: 40 | conn.executescript(sqld.schema) 41 | conn.commit() 42 | # ... 43 | -------------------------------------------------------------------------------- /arch-install.txt: -------------------------------------------------------------------------------- 1 | * cgdisk /dev/sda 2 | * I do one partition 3 | * mkfs.ext4 /dev/sda1 4 | * mount /dev/sda1 /mnt 5 | * pacstrap -i /mnt base base-devel 6 | * genfstab -U -p /mnt >> /mnt/etc/fstab 7 | * arch-chroot /mnt /bin/bash 8 | * set -o vi 9 | * vi /etc/locale.gen 10 | * locale-gen 11 | * echo LANG=en_US.UTF-8 > /etc/locale.conf 12 | * export LANG=en_US.UTF-8 13 | * ln -s /usr/share/zoneinfo/Asia/Jerusalem /etc/localtime 14 | * hwclock --systohc --utc 15 | * echo HOSTNAME > /etc/hostname 16 | * vi /etc/hosts 17 | * 127.0.0.1 localhost.localdomain localhost myhostname 18 | * pacman -S networkmanager 19 | * systemctl enable NetworkManager 20 | * passwd 21 | * pacman -S gptfdisk syslinux 22 | * syslinux-install_update -i -a -m 23 | * vi /boot/syslinux/syslinux.cfg 24 | * Change `sda3` to `sda1` 25 | * exit 26 | * umount -R /mnt 27 | * shutdown -h now 28 | * Remove CD from CDROM 29 | * Start 30 | * pacman -S zsh 31 | * useradd -m -G wheel -s /usr/bin/zsh miki 32 | * passwd miki 33 | * pacman -S sudo 34 | * visudo 35 | * Uncomment line for wheel without password 36 | * pacman -S virtualbox-guest-utils 37 | * depmod 38 | * cat > /etc/modules-load.d/vbox.conf 39 | vboxguest 40 | vboxsf 41 | vboxvideo 42 | * pacman -S xfce4 xfce4-goodies 43 | * pacman -S gdm 44 | * systemctl enable gdm 45 | * REBOOT 46 | -------------------------------------------------------------------------------- /benchmark.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | def fast_fib(n): 3 | if n < 2: 4 | return 1 5 | 6 | a, b = 1, 1 7 | for i in range(n - 1): 8 | a, b = b, a + b 9 | 10 | return b 11 | 12 | def benchmark(): 13 | from timeit import Timer 14 | 15 | benchmark.index = 20 16 | timer = Timer("fast_fib(benchmark.index)", 17 | "from __main__ import fast_fib, benchmark") 18 | num_runs = 100 19 | 20 | print timer.timeit(num_runs) / num_runs 21 | 22 | def main(argv=None): 23 | if argv is None: 24 | import sys 25 | argv = sys.argv 26 | 27 | from optparse import OptionParser 28 | 29 | parser = OptionParser("usage: %prog [options] INDEX") 30 | parser.add_option("--benchmark", help="run benchmark", 31 | dest="benchmark", action="store_true", default=0) 32 | 33 | opts, args = parser.parse_args(argv[1:]) 34 | 35 | if opts.benchmark: 36 | benchmark() 37 | raise SystemExit() 38 | 39 | if len(args) != 1: 40 | parser.error("wrong number of arguments") # Will exit 41 | 42 | # Do main program stuff here 43 | try: 44 | print fast_fib(int(args[0])) 45 | except ValueError: 46 | raise SystemExit("error: %s - bad number" % args[0]) 47 | 48 | if __name__ == "__main__": 49 | main() 50 | -------------------------------------------------------------------------------- /cypher.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | from random import choice 3 | 4 | pages = [] 5 | index = defaultdict(list) # word -> [(page, i), (page, i) ...] 6 | 7 | with open('sherlock-holmes.txt') as fp: 8 | # ~300 words per page, ~10 words per sentence 9 | page, lnum, wnum = [], 1, 1 10 | for line in fp: 11 | if not line.strip(): 12 | continue 13 | for wnum, word in enumerate(line.lower().split(), wnum): 14 | page.append(word) 15 | index[word].append((len(pages) + 1, wnum)) 16 | wnum += 1 17 | lnum += 1 18 | 19 | if lnum == 30: 20 | pages.append(page) 21 | page, lnum, wnum = [], 1, 1 22 | if page: 23 | pages.append(page) 24 | 25 | 26 | def encode(sentence): 27 | dec = [] 28 | for word in sentence.lower().split(): 29 | dec += choice(index[word]) 30 | return dec 31 | 32 | 33 | def decode(cypher): 34 | cypher = [int(w) for w in cypher.split()] 35 | words = [] 36 | for i in range(0, len(cypher), 2): 37 | page = cypher[i] - 1 38 | wnum = cypher[i+1] - 1 39 | words.append(pages[page][wnum]) 40 | return ' '.join(words) 41 | 42 | 43 | 44 | #print(' '.join(str(n) for n in encode('london at afternoon'))) 45 | print(decode('117 278 243 249 87 213')) 46 | 47 | -------------------------------------------------------------------------------- /webphone/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | WebPhone 5 | 6 | 7 |

WebPhone

8 | 9 | 10 |
11 | Go ahead, search my day ... 12 |
13 | 14 | 15 | 41 | 42 | -------------------------------------------------------------------------------- /decimals.py: -------------------------------------------------------------------------------- 1 | '''Example of class decorator to make sure some attributes are always Decimal''' 2 | 3 | from decimal import Decimal 4 | from operator import attrgetter 5 | 6 | def decimals(cls): 7 | '''A class decorator that ensures all attributes specifiec in the class 8 | __decimals__ will be Decimal. 9 | 10 | Make sure your class is a new style class (inherits from object), otherwise 11 | this won't work. 12 | 13 | Example: 14 | >>> @decimals 15 | ... class Sale(object): 16 | ... __decimals__ = ['price'] 17 | ... def __init__(self, item, price): 18 | ... self.item = item 19 | ... self.price = price 20 | ... 21 | >>> s1 = Sale('socks', 11.2) 22 | >>> type(s1.price) 23 | 24 | >>> s1.price = 70 25 | >>> type(s1.price) 26 | 27 | >>> 28 | ''' 29 | def make_setter(name): 30 | def setter(self, value): 31 | setattr(self, name, Decimal(value)) 32 | 33 | return setter 34 | 35 | for attr in cls.__decimals__: 36 | name = '_{}'.format(attr) 37 | getter = attrgetter(name) 38 | setter = make_setter(name) 39 | setattr(cls, attr, property(getter, setter, None, attr)) 40 | 41 | return cls 42 | 43 | if __name__ == '__main__': 44 | import doctest 45 | doctest.testmod() 46 | -------------------------------------------------------------------------------- /result/result.go: -------------------------------------------------------------------------------- 1 | package nmath 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | "unsafe" 7 | ) 8 | 9 | /* 10 | #include 11 | #include "result.h" 12 | */ 13 | import "C" 14 | 15 | // Result is result from C 16 | type Result struct { 17 | r C.result_t 18 | err error 19 | str *string 20 | once sync.Once 21 | } 22 | 23 | // NewResult returns a new result 24 | func NewResult(r C.result_t) *Result { 25 | res := Result{r: r} 26 | if r.err != nil { 27 | res.err = fmt.Errorf(C.GoString(r.err)) 28 | C.free(unsafe.Pointer(r.err)) 29 | } 30 | 31 | return &res 32 | } 33 | 34 | // Err return the result error 35 | func (r *Result) Err() error { 36 | return r.err 37 | } 38 | 39 | // Int return integer value 40 | func (r *Result) Int() int { 41 | return int(C.result_int(r.r)) 42 | } 43 | 44 | // Float return float value 45 | func (r *Result) Float() float64 { 46 | return float64(C.result_float(r.r)) 47 | } 48 | 49 | // Str returns string 50 | func (r *Result) Str() string { 51 | r.once.Do(r.getString) 52 | return *r.str 53 | } 54 | 55 | // Ptr returns unsafe.Pointer 56 | func (r *Result) Ptr() unsafe.Pointer { 57 | return unsafe.Pointer(C.result_ptr(r.r)) 58 | } 59 | 60 | func (r *Result) getString() { 61 | cp := C.result_str(r.r) 62 | if cp == nil { 63 | *r.str = "" 64 | return 65 | } 66 | 67 | *r.str = C.GoString(cp) 68 | C.free(unsafe.Pointer(cp)) 69 | } 70 | -------------------------------------------------------------------------------- /feedme/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | FeedMe - A Minimal Web Based RSS Reader 4 | 5 | 6 | 22 | 23 | 24 |

FeedMe - A Minimal Web Based RSS Reader

25 |
26 | Feed URL: 27 | 28 |
29 |
30 |
31 |
32 | 33 | 34 | 46 | 47 | -------------------------------------------------------------------------------- /config/config.py: -------------------------------------------------------------------------------- 1 | """Environment based configuration""" 2 | 3 | import json 4 | import logging 5 | from dataclasses import dataclass 6 | from distutils.util import strtobool 7 | from os import environ 8 | 9 | _missing = object() 10 | _prefix = 'c_' 11 | 12 | 13 | @dataclass 14 | class Var: 15 | value: object 16 | key: str 17 | conv: callable = None 18 | 19 | def __post_init__(self): 20 | value = environ.get(self.key) 21 | if value is None: 22 | return 23 | 24 | conv = self.conv or type(self.value) 25 | if conv is bool: 26 | conv = strtobool # Support 'yes', 'on', 'no' and friends 27 | self.value = conv(value) 28 | 29 | 30 | def __dir__(): 31 | return [ 32 | k[len(_prefix):] 33 | for k, v in globals().items() 34 | if k.startswith(_prefix) and isinstance(v, Var) 35 | ] 36 | 37 | 38 | def __getattr__(attr): 39 | key = _prefix + attr 40 | var = globals().get(key, _missing) 41 | if var is _missing: 42 | raise AttributeError(attr) 43 | return var.value 44 | 45 | 46 | # Configuration values should start with c_, everything else is ignored 47 | 48 | c_log_level = Var(logging.INFO, 'LOG_LEVEL', lambda v: getattr(logging, v)) 49 | c_db_hosts = Var(['db1.local', 'db2.local'], 'DB_HOSTS', json.loads) 50 | c_http_port = Var(8080, 'HTTP_PORT') 51 | c_use_ssh = Var(False, 'USE_SSH') 52 | -------------------------------------------------------------------------------- /dynamic-static/chart.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''This CGI script is called by lighttpd 404 error handler when it tries to find 3 | /charts/XXX.png. 4 | 5 | This way the image is generated once and then we let lighttpd serve it as static 6 | content. 7 | ''' 8 | 9 | # The below is needed to tell pylab to work without a screen 10 | import matplotlib; matplotlib.use("Agg") 11 | from pylab import hist, randn, title, savefig 12 | 13 | from os import environ 14 | from os.path import basename 15 | from urlparse import urlparse 16 | 17 | def generate_chart(name, outfile): 18 | # Generate a random chart 19 | hist(randn(1000), 100) 20 | title("Chart for \"%s\"" % name) 21 | savefig(outfile) 22 | 23 | def main(): 24 | import cgitb; cgitb.enable() 25 | 26 | o = urlparse(environ.get("REQUEST_URI", "")) 27 | if not o.path: 28 | print "Content-type: text/plain\n" 29 | print "ERROR: Can't find path" 30 | raise SystemExit 31 | 32 | outfile = "charts/%s" % basename(o.path) 33 | name = basename(o.path).replace(".png", "") 34 | if not name: 35 | print "Content-type: text/plain\n" 36 | print "ERROR: 'name' not specified" 37 | raise SystemExit 38 | 39 | generate_chart(name, outfile) 40 | image = open(outfile, "rb").read() 41 | 42 | print "Content-type: image/png\n" 43 | print image 44 | 45 | if __name__ == "__main__": 46 | main() 47 | 48 | -------------------------------------------------------------------------------- /js-sound/soundplayer.mxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /sentences.py: -------------------------------------------------------------------------------- 1 | '''Split text to sentences. 2 | 3 | Original idea from Perl's Lingua::EN::Sentence, modifications by Raymond 4 | Hettinger. 5 | ''' 6 | 7 | PEOPLE = [ 8 | 'jr', 'mr', 'mrs', 'ms', 'dr', 'prof', 'sr', 'sen' 'sens', 'rep', 'reps', 9 | 'gov', 'atty', 'attys', 'supt', 'det', 'rev', 10 | ] 11 | 12 | ARMY = [ 13 | 'col', 'gen', 'lt', 'cmdr', 'adm', 'capt', 'sgt', 'cpl', 'maj', 14 | ] 15 | 16 | INSTITUTES = [ 17 | 'dept', 'univ', 'assn', 'bros', 18 | ] 19 | 20 | COMPANIES = [ 21 | 'inc', 'ltd', 'co', 'corp', 22 | ] 23 | 24 | PLACES = [ 25 | 'arc', 'al', 'ave', "blv?d", 'cl', 'ct', 'cres', 'dr', "expy?", 'dist', 26 | 'mt', 'ft', "fw?y", "hwa?y", 'la', "pde?", 'pl', 'plz', 'rd', 'st', 'tce', 27 | 'Ala' , 'Ariz', 'Ark', 'Cal', 'Calif', 'Col', 'Colo', 'Conn', 'Del', 'Fed' 28 | , 'Fla', 'Ga', 'Ida', 'Id', 'Ill', 'Ind', 'Ia', 'Kan', 'Kans', 'Ken', 'Ky' 29 | , 'La', 'Me', 'Md', 'Is', 'Mass', 'Mich', 'Minn', 'Miss', 'Mo', 'Mont', 30 | 'Neb', 'Nebr' , 'Nev', 'Mex', 'Okla', 'Ok', 'Ore', 'Penna', 'Penn', 'Pa' , 31 | 'Dak', 'Tenn', 'Tex', 'Ut', 'Vt', 'Va', 'Wash', 'Wis', 'Wisc', 'Wy', 'Wyo', 32 | 'USAFA', 'Alta' , 'Man', 'Ont', 'Qué', 'Sask', 'Yuk' 33 | ] 34 | 35 | MONTHS = ('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec','sept'); 36 | MISC = ( 'vs', 'etc', 'no', 'esp' ); 37 | 38 | ABBREVIATIONS = (@PEOPLE, @ARMY, @INSTITUTES, @COMPANIES, @PLACES, @MONTHS, @MISC ) 39 | -------------------------------------------------------------------------------- /trylock.py: -------------------------------------------------------------------------------- 1 | from functools import wraps 2 | from threading import RLock 3 | from collections import defaultdict 4 | 5 | class AlreadyRunningError(Exception): 6 | pass 7 | 8 | 9 | def try_lock(keyfn=None): 10 | '''Allow function to run only once. `keyfn` can be used to select a subset 11 | of the function arguments/kwargs to lock on. 12 | 13 | Will raise `AlreadyRunningError` if runtion already running. 14 | 15 | Example:: 16 | def push(day, records): 17 | ... 18 | 19 | And we care only about the day argument, when we'll do:: 20 | 21 | @try_lock(lambda args, kw: args[0]) 22 | def push(day, records): 23 | ... 24 | ''' 25 | keyfn = keyfn or (lambda args, kw: 7) # Return the same key 26 | by_key_locks = defaultdict(RLock) 27 | master_lock = RLock() 28 | 29 | def wrapper(func): 30 | @wraps(func) 31 | def wrapped(*args, **kw): 32 | key = keyfn(args, kw) 33 | 34 | # Get lock for key 35 | with master_lock: 36 | lock = by_key_locks[key] 37 | 38 | if not lock.acquire(blocking=False): 39 | raise AlreadyRunningError( 40 | "{} already running for {}".format(func.__name__, key)) 41 | 42 | try: 43 | return func(*args, **kw) 44 | finally: 45 | lock.release() 46 | return wrapped 47 | return wrapper 48 | -------------------------------------------------------------------------------- /cheetah-demo/Cheetah/Utils/optik/errors.py: -------------------------------------------------------------------------------- 1 | """optik.errors 2 | 3 | Exception classes used by Optik. 4 | """ 5 | 6 | __revision__ = "$Id: errors.py,v 1.1 2002/08/24 17:10:06 hierro Exp $" 7 | 8 | # Copyright (c) 2001 Gregory P. Ward. All rights reserved. 9 | # See the README.txt distributed with Optik for licensing terms. 10 | 11 | # created 2001/10/17 GPW (from optik.py) 12 | 13 | 14 | class OptikError (Exception): 15 | def __init__ (self, msg): 16 | self.msg = msg 17 | 18 | def __str__ (self): 19 | return self.msg 20 | 21 | 22 | class OptionError (OptikError): 23 | """ 24 | Raised if an Option instance is created with invalid or 25 | inconsistent arguments. 26 | """ 27 | 28 | def __init__ (self, msg, option): 29 | self.msg = msg 30 | self.option_id = str(option) 31 | 32 | def __str__ (self): 33 | if self.option_id: 34 | return "option %s: %s" % (self.option_id, self.msg) 35 | else: 36 | return self.msg 37 | 38 | class OptionConflictError (OptionError): 39 | """ 40 | Raised if conflicting options are added to an OptionParser. 41 | """ 42 | 43 | class OptionValueError (OptikError): 44 | """ 45 | Raised if an invalid option value is encountered on the command 46 | line. 47 | """ 48 | 49 | class BadOptionError (OptikError): 50 | """ 51 | Raised if an invalid or ambiguous option is seen on the command-line. 52 | """ 53 | -------------------------------------------------------------------------------- /react-table/table.jsx: -------------------------------------------------------------------------------- 1 | var Table = React.createClass({ 2 | componentDidMount: function() { 3 | var table = this; 4 | $('#load').click(function() { 5 | $.ajax({ 6 | url: '/data?last=' + table.state.values.length, 7 | dataType: 'json', 8 | success: function(objs) { 9 | var values = table.state.values.concat(objs); 10 | table.setState({values: values}); 11 | } 12 | }); 13 | }); 14 | }, 15 | getInitialState: function() { 16 | return {values: []}; 17 | }, 18 | render: function() { 19 | var rows = this.state.values.map(function(obj) { 20 | return ( 21 | 22 | {obj.name} 23 | {obj.color} 24 | 25 | ); 26 | }); 27 | 28 | return ( 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | {rows} 38 | 39 |
NameColor
40 | ); 41 | } 42 | }); 43 | 44 | function on_ready() { 45 | React.render( 46 | , 47 | $('#content')[0] 48 | ); 49 | } 50 | 51 | $(on_ready); 52 | -------------------------------------------------------------------------------- /trends/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Trends 5 | 6 | 39 | 40 | 41 |

Trends

42 | 45 | 46 | 47 | 56 | 57 | -------------------------------------------------------------------------------- /ssh-hostname: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Resolve SSH hostname for alias in ~/.ssh/config''' 3 | 4 | from os.path import expanduser 5 | 6 | 7 | def iter_cfg(fo): 8 | '''Iterate over SSH configuraton file, yield each section as dict. 9 | 10 | We assume a section is between two "Host" lines. 11 | ''' 12 | record = {} 13 | for line in fo: 14 | line = line.strip() 15 | if not line: 16 | continue 17 | 18 | if line.lower().startswith('host '): 19 | if record: 20 | yield record 21 | record = {} 22 | key, value = line.split(None, 1) 23 | record[key.lower()] = value 24 | 25 | if record: 26 | yield record 27 | 28 | 29 | def main(argv=None): 30 | import sys 31 | from argparse import ArgumentParser 32 | 33 | argv = argv or sys.argv 34 | 35 | parser = ArgumentParser(description=__doc__) 36 | parser.add_argument('host', help='host to resolve') 37 | args = parser.parse_args(argv[1:]) 38 | 39 | cfg_file = expanduser('~/.ssh/config') 40 | try: 41 | fo = open(cfg_file) 42 | except (IOError, OSError) as err: 43 | raise SystemExit('error: {}'.format(err)) 44 | 45 | for record in iter_cfg(fo): 46 | if record.get('host') == args.host: 47 | print(record['hostname']) 48 | raise SystemExit 49 | 50 | raise SystemExit('error: {} not found in {}'.format(args.host, cfg_file)) 51 | 52 | 53 | if __name__ == '__main__': 54 | main() 55 | -------------------------------------------------------------------------------- /present-timer/quiz.slide: -------------------------------------------------------------------------------- 1 | Go Quiz 2 | Guess the Output 3 | 4 | Miki Tebeka 5 | CEO, 353solutions 6 | miki@353solutions.com 7 | @tebeka 8 | 9 | * Miki Tebeka 10 | 11 | .image 353.png 12 | .html timer.html 13 | 14 | * Quiz Rules 15 | 16 | - I'll show a short program 17 | - You have 30 seconds to guess the output (countdown on bottom right) 18 | - Possible answers are: some output, won't compile, panic, deadlock 19 | - There are 14 questions total 20 | 21 | Speical thanks to [[https://twitter.com/davecheney][Dave Cheney]], [[https://twitter.com/bradfitz][Brad Fitzpatrick]] & others 22 | 23 | * 24 | 25 | .image picard-why.jpg 550 850 26 | 27 | * Number 28 | 29 | .play -edit -numbers num.go 30 | 31 | * Chars 32 | 33 | .play -edit -numbers stridx.go 34 | 35 | * Map 36 | 37 | .play -edit -numbers empty-map.go 38 | 39 | * Tick 40 | 41 | .play -edit -numbers tick.go 42 | 43 | * City 44 | 45 | .play -edit -numbers unicode.go 46 | 47 | * Vars 48 | 49 | .play -edit -numbers vars2.go 50 | 51 | * Sleep 52 | 53 | .play -edit -numbers time.go 54 | 55 | * Power 56 | 57 | .play -edit -numbers xor.go 58 | 59 | * Init 60 | 61 | .play -edit -numbers init.go 62 | 63 | * Append 64 | 65 | .play -edit -numbers sliceappend.go 66 | 67 | * Sleep Sort 68 | 69 | .play -edit -numbers sleep-sort.go /^func main/,/^}/ 70 | 71 | * π 72 | 73 | .play -edit -numbers pi.go 74 | 75 | * Error 76 | 77 | .play -edit -numbers err.go 78 | 79 | * Channel 80 | 81 | .play -edit -numbers ch.go 82 | 83 | * The End 84 | 85 | How many did you get right? 86 | -------------------------------------------------------------------------------- /go-from-c/hi.h: -------------------------------------------------------------------------------- 1 | /* Created by "go tool cgo" - DO NOT EDIT. */ 2 | 3 | /* package command-line-arguments */ 4 | 5 | /* Start of preamble from import "C" comments. */ 6 | 7 | 8 | 9 | 10 | /* End of preamble from import "C" comments. */ 11 | 12 | 13 | /* Start of boilerplate cgo prologue. */ 14 | 15 | #ifndef GO_CGO_PROLOGUE_H 16 | #define GO_CGO_PROLOGUE_H 17 | 18 | typedef signed char GoInt8; 19 | typedef unsigned char GoUint8; 20 | typedef short GoInt16; 21 | typedef unsigned short GoUint16; 22 | typedef int GoInt32; 23 | typedef unsigned int GoUint32; 24 | typedef long long GoInt64; 25 | typedef unsigned long long GoUint64; 26 | typedef GoInt64 GoInt; 27 | typedef GoUint64 GoUint; 28 | typedef __SIZE_TYPE__ GoUintptr; 29 | typedef float GoFloat32; 30 | typedef double GoFloat64; 31 | typedef float _Complex GoComplex64; 32 | typedef double _Complex GoComplex128; 33 | 34 | /* 35 | static assertion to make sure the file is being used on architecture 36 | at least with matching size of GoInt. 37 | */ 38 | typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; 39 | 40 | typedef struct { const char *p; GoInt n; } GoString; 41 | typedef void *GoMap; 42 | typedef void *GoChan; 43 | typedef struct { void *t; void *v; } GoInterface; 44 | typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; 45 | 46 | #endif 47 | 48 | /* End of boilerplate cgo prologue. */ 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | 55 | extern void Hi(GoString p0); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | -------------------------------------------------------------------------------- /pmap.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Parallel map (Unix only)''' 3 | 4 | # The big advantage of this implementation is that "fork" is very fast on 5 | # copying data, so if you pass big arrays as arguments and return small values 6 | # this is a win 7 | 8 | # FIXME 9 | # * For too many items, we get "Too many open files" 10 | # * Handle child exceptions 11 | 12 | from functools import partial 13 | from marshal import dump, load 14 | from os import fork, pipe, fdopen, waitpid, P_WAIT 15 | 16 | 17 | def spawn(func, data): 18 | read_fo, write_fo = map(fdopen, pipe(), ('rb', 'wb')) 19 | pid = fork() 20 | if pid: # Parent 21 | return pid, read_fo 22 | 23 | # Child 24 | dump(func(data), write_fo) 25 | write_fo.close() 26 | raise SystemExit 27 | 28 | 29 | def wait(child): 30 | pid, fo = child 31 | waitpid(pid, P_WAIT) 32 | value = load(fo) 33 | fo.close() 34 | return value 35 | 36 | 37 | def pmap(func, items): 38 | """Parallel map (using fork) of func over items 39 | >>> pmap(lambda x: x * 2, range(5)) 40 | [0, 2, 4, 6, 8] 41 | """ 42 | children = map(partial(spawn, func), items) 43 | return map(wait, children) 44 | 45 | 46 | if __name__ == '__main__': 47 | def fib(n): 48 | a, b = 1, 1 49 | while n > 1: 50 | a, b = b, a + b 51 | n -= 1 52 | return b 53 | 54 | nums = range(10) 55 | print('pmap(fib, %s)' % str(nums)) 56 | for num, val in zip(nums, pmap(fib, nums)): 57 | print('%d -> %d' % (num, val)) 58 | -------------------------------------------------------------------------------- /hist_stats.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """History statistics""" 3 | 4 | import shlex 5 | from argparse import ArgumentParser 6 | from datetime import datetime 7 | from os.path import expanduser 8 | import pandas as pd 9 | 10 | 11 | def parse_line(line): 12 | """Parse a history line, return None if can't""" 13 | #: 1523386209:0;convert -crop 600x400 raw/01-add.png 01-add.png 14 | try: 15 | line = line.decode('utf-8') 16 | except UnicodeDecodeError: 17 | return None 18 | 19 | line = line[2:] # Remove ': ' prefix 20 | 21 | fields = line.split(';', maxsplit=1) 22 | if len(fields) != 2: 23 | return None 24 | 25 | try: 26 | command = shlex.split(fields[1]) 27 | except ValueError: 28 | command = fields[1].split() 29 | 30 | ts, _ = fields[0].split(':') 31 | return { 32 | 'time': datetime.fromtimestamp(int(ts)), 33 | 'command': command[0], 34 | 'nargs': len(command[1:]), 35 | } 36 | 37 | 38 | def load_history(file_name): 39 | """Return DataFrame from history file""" 40 | with open(file_name, 'rb') as fp: 41 | records = filter(None, (parse_line(line) for line in fp)) 42 | return pd.DataFrame.from_records(records) 43 | 44 | 45 | parser = ArgumentParser(description=__doc__) 46 | parser.add_argument( 47 | '--count', help='number of items to show', default=20, type=int) 48 | args = parser.parse_args() 49 | 50 | histfile = expanduser('~/.zsh_history') 51 | df = load_history(histfile) 52 | vc = df['command'].value_counts() 53 | print(vc[:args.count]) 54 | -------------------------------------------------------------------------------- /curl-and-couchdb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # curl and couchdb - a love story 4 | 5 | set -v 6 | server=http://localhost:5984 7 | 8 | # Handshake 9 | curl -s $server 10 | read 11 | 12 | # Is something running? 13 | curl -s $server/_active_tasks 14 | read 15 | 16 | # List of all databases 17 | curl -s $server/_all_dbs 18 | read 19 | 20 | # Readable list of all databases 21 | curl -s $server/_all_dbs | python -m json.tool 22 | read 23 | 24 | # View database 25 | curl -s $server/people | python -m json.tool 26 | read 27 | 28 | # View all docs 29 | curl -s $server/people/_all_docs | python -m json.tool 30 | read 31 | 32 | # View all docs with content 33 | curl -s "$server/people/_all_docs?include_docs=true" | python -m json.tool 34 | read 35 | 36 | db=foo 37 | # Create database 38 | curl -s -X PUT $server/$db 39 | read 40 | 41 | # Create document 42 | id=$(uuid) 43 | curl -s -d'{"a" : 1, "b" : 2}' -X PUT $server/$db/$id 44 | read 45 | 46 | # Get document 47 | curl -s $server/$db/$id | python -m json.tool 48 | read 49 | 50 | # Edit document 51 | curl -s $server/$db/$id | python -m json.tool > doc.json 52 | vim doc.json 53 | curl -s -d@doc.json -X PUT $server/$db/$id 54 | curl -s $server/$db/$id | python -m json.tool 55 | read 56 | 57 | # Delete it (we need the revision) 58 | rev=$(curl -s $server/$db/$id | egrep -o '[0-9]+-[a-z0-9]{32}') 59 | curl -s -X DELETE "$server/$db/$id?rev=$rev" 60 | read 61 | 62 | # Delete the database 63 | curl -s -X DELETE $server/$db 64 | read 65 | 66 | # Run a view 67 | curl -s $server/people/_design/people/_view/managers | python -m json.tool 68 | read 69 | -------------------------------------------------------------------------------- /aenumerate.py: -------------------------------------------------------------------------------- 1 | """aenumerate - enumerate for async for""" 2 | 3 | import asyncio 4 | from collections import abc 5 | 6 | 7 | class aenumerate(abc.AsyncIterator): 8 | """enumerate for async for""" 9 | def __init__(self, aiterable, start=0): 10 | self._aiterable = aiterable 11 | self._i = start - 1 12 | 13 | async def __aiter__(self): 14 | self._ait = await self._aiterable.__aiter__() 15 | return self 16 | 17 | async def __anext__(self): 18 | # self._ait will raise the apropriate AsyncStopIteration 19 | val = await self._ait.__anext__() 20 | self._i += 1 21 | return self._i, val 22 | 23 | 24 | # Example usage 25 | async def iter_lines(host, port): 26 | """Iterator over lines from host:port, print them with line number""" 27 | rdr, wtr = await asyncio.open_connection(host, port) 28 | async for lnum, line in aenumerate(rdr, 1): 29 | line = line.decode().rstrip() 30 | print('[{}:{}] {:02d} {}'.format(host, port, lnum, line)) 31 | 32 | 33 | if __name__ == '__main__': 34 | from argparse import ArgumentParser 35 | parser = ArgumentParser(description='enumerate lines from TCP server') 36 | parser.add_argument('host', help='host to connect to') 37 | parser.add_argument('port', help='port to connect to', type=int) 38 | args = parser.parse_args() 39 | 40 | loop = asyncio.get_event_loop() 41 | loop.run_until_complete(iter_lines(args.host, args.port)) 42 | loop.close() 43 | 44 | # Run server: nc -lc -p 7654 < some-file 45 | # (or on osx: nc -l 7654 < some-file) 46 | -------------------------------------------------------------------------------- /find-pass.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from subprocess import check_output, STDOUT, CalledProcessError 4 | from sys import platform 5 | import re 6 | 7 | # password: "XXXXX" 8 | find_passwd = re.compile('password: "([^"]+)"').search 9 | # "acct"="miki.tebeka@gmail.com" 10 | find_user = re.compile('"acct"="([^"]+)"').search 11 | 12 | 13 | def find_key(fn, out): 14 | match = fn(out) 15 | return match and match.group(1) 16 | 17 | 18 | def main(argv=None): 19 | import sys 20 | from argparse import ArgumentParser 21 | 22 | argv = argv or sys.argv 23 | 24 | parser = ArgumentParser(description='') 25 | parser.add_argument('domain') 26 | args = parser.parse_args(argv[1:]) 27 | 28 | if platform != 'darwin': 29 | raise SystemExit('error: {} works only on OSX'.format(parser.prog)) 30 | 31 | domain = args.domain 32 | 33 | cmd = [ 34 | 'security', 35 | 'find-internet-password', 36 | '-g', 37 | '-s', domain, 38 | ] 39 | try: 40 | out = check_output(cmd, stderr=STDOUT) 41 | except CalledProcessError: 42 | raise SystemExit('error: not password for {}'.format(domain)) 43 | 44 | user = find_key(find_user, out) 45 | if not user: 46 | raise SystemExit('error: cannot find user for {}'.format(domain)) 47 | print(user) 48 | 49 | passwd = find_key(find_passwd, out) 50 | if not passwd: 51 | raise SystemExit('error: cannot find password for {}'.format(domain)) 52 | print(passwd) 53 | 54 | 55 | if __name__ == '__main__': 56 | main() 57 | -------------------------------------------------------------------------------- /ctxdec/ctx.py: -------------------------------------------------------------------------------- 1 | ## Greeter 2 | class greeter(object): 3 | def __enter__(self): 4 | print('Hai') 5 | 6 | def __exit__(self, exc_type, exc_value, traceback): 7 | print('Bai') 8 | 9 | 10 | ## timed_block 11 | from time import time 12 | 13 | 14 | class timed_block(object): 15 | def __init__(self, name): 16 | self.name = name 17 | 18 | def __enter__(self): 19 | self.start = time() 20 | 21 | def __exit__(self, exc_type, exc_value, traceback): 22 | duration = time() - self.start 23 | print('{} took {:0.2f}sec'.format(self.name, duration)) 24 | 25 | 26 | ## Closer 27 | class closing(object): 28 | def __init__(self, obj): 29 | self.obj = obj 30 | 31 | def __enter__(self): 32 | pass 33 | 34 | def __exit__(self, exc_type, exc_value, traceback): 35 | self.obj.close() 36 | 37 | 38 | ## Database 39 | class dbctx(object): 40 | def __init__(self, conn): 41 | self.conn = conn 42 | 43 | def __enter__(self): 44 | return self.conn.cursor() 45 | 46 | def __exit__(self, exc_type, exc_value, traceback): 47 | if exc_type is None: 48 | print('Committing') 49 | self.conn.commit() 50 | else: 51 | print('Rolling back') 52 | self.conn.rollback() 53 | 54 | 55 | ## contextlib 56 | from contextlib import contextmanager 57 | 58 | 59 | @contextmanager 60 | def dbctx(conn): 61 | try: 62 | yield conn.cursor() 63 | conn.commit() 64 | except: 65 | conn.rollback() 66 | raise # Need to re-raise 67 | -------------------------------------------------------------------------------- /pandas-validation/pull.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Pull data from database to .h5 storage.''' 3 | 4 | # Assuming our points tables have the following schema 5 | # CREATE TABLE points ( 6 | # x INTEGER 7 | # , y INTEGER 8 | # , z INTEGER 9 | # , value FLOAT 10 | # ); 11 | # We have two database points_pre and points_post 12 | 13 | import psycopg2 14 | from psycopg2.extras import DictCursor 15 | from pandas import DataFrame, HDFStore 16 | from threading import Thread 17 | 18 | 19 | def cursor(step): 20 | '''Return a DictCursor connected to step=pre/post database.''' 21 | conn = psycopg2.connect(database='points_{}'.format(step)) 22 | return conn.cursor(cursor_factory=DictCursor) 23 | 24 | 25 | if __name__ == '__main__': 26 | pre_cursor = cursor('pre') 27 | post_cursor = cursor('post') 28 | 29 | sql = 'SELECT x, y, z, value FROM points''' 30 | 31 | # Get data in two threads to speed things up 32 | pre_t = Thread(target=pre_cursor.execute, args=(sql,)) 33 | pre_t.start() 34 | post_t = Thread(target=post_cursor.execute, args=(sql,)) 35 | post_t.start() 36 | pre_t.join() 37 | post_t.join() 38 | 39 | 40 | # Create data frames 41 | pre = DataFrame.from_records([dict(row) for row in pre_cursor]) 42 | post = DataFrame.from_records([dict(row) for row in post_cursor]) 43 | 44 | # Store data frame in HDF5 data store 45 | store_file = 'points.h5' 46 | store = HDFStore(store_file) 47 | store['pre'] = pre 48 | store['post'] = post 49 | store.flush() 50 | 51 | print('Data stored at {}'.format(store_file)) 52 | -------------------------------------------------------------------------------- /pool.py: -------------------------------------------------------------------------------- 1 | """Simple Pool object""" 2 | 3 | from queue import Queue 4 | 5 | 6 | class Proxy: 7 | """Wraps original object with context manager that return the object to the 8 | pool.""" 9 | def __init__(self, obj, pool): 10 | self._obj = obj 11 | self._pool = pool 12 | 13 | def __enter__(self): 14 | return self._obj 15 | 16 | def __exit__(self, typ, val, tb): 17 | self._pool._put(self._obj) 18 | 19 | 20 | class Pool: 21 | """Pool of objects""" 22 | def __init__(self, objects): 23 | self._queue = Queue() 24 | for obj in objects: 25 | self._queue.put(obj) 26 | 27 | def lease(self): 28 | """Lease an object from the pool, should be used as contect manger. 29 | e.g.: 30 | 31 | with pool.lease() as conn: 32 | cur = conn.cursor() 33 | cur.execute('SELECT ...') 34 | """ 35 | return Proxy(self._queue.get(), self) 36 | 37 | def _put(self, obj): 38 | self._queue.put(obj) 39 | 40 | 41 | if __name__ == '__main__': 42 | from threading import Thread, Barrier 43 | from time import sleep 44 | from random import random 45 | 46 | n = 10 47 | b = Barrier(n) 48 | p = Pool([1, 2, 3]) 49 | 50 | def worker(n, barrier, pool): 51 | barrier.wait() # Wait for all threads to be ready 52 | sleep(random() / 10) 53 | with pool.lease() as val: 54 | print('worker %d got resource %d' % (n, val)) 55 | 56 | for i in range(n): 57 | Thread(target=worker, args=(i, b, p)).start() 58 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2008-2014 Miki Tebeka 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | * Neither the name of the "PythonWise" blog nor the names of its contributors 13 | may be used to endorse or promote products derived from this software without 14 | specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 20 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 23 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /pandas-validation/populate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | '''Populate with dummy data.''' 3 | 4 | from pull import cursor 5 | 6 | from random import randint, random 7 | from collections import namedtuple 8 | 9 | sql = ''' 10 | INSERT INTO points ( 11 | x 12 | , y 13 | , z 14 | , value 15 | ) VALUES ( 16 | %s 17 | , %s 18 | , %s 19 | , %s 20 | )''' 21 | 22 | Point = namedtuple('Point', ['x', 'y', 'z']) 23 | 24 | 25 | def rand_coord(): 26 | return randint(0, 1000) 27 | 28 | 29 | def rand_point(): 30 | return Point( 31 | rand_coord(), 32 | rand_coord(), 33 | rand_coord(), 34 | ) 35 | 36 | 37 | def gen_points(count): 38 | coords = set() 39 | while len(coords) < count: 40 | coords.add(rand_point()) 41 | return coords 42 | 43 | 44 | 45 | if __name__ == '__main__': 46 | pre_cur = cursor('pre') 47 | post_cur = cursor('post') 48 | 49 | points = gen_points(10000) 50 | for point in points: 51 | pre_value = random() * 1000 52 | post_value = pre_value 53 | 54 | # In 1% probability, generate different value 55 | if random() <= 0.01: 56 | # Up to 10% difference 57 | diff = (random() * 0.1) * pre_value 58 | if random() > 0.5: 59 | post_value += diff 60 | else: 61 | post_value -= diff 62 | 63 | pre_cur.execute(sql, (point.x, point.y, point.z, pre_value)) 64 | post_cur.execute(sql, (point.x, point.y, point.z, post_value)) 65 | 66 | pre_cur.connection.commit() 67 | post_cur.connection.commit() 68 | -------------------------------------------------------------------------------- /solr.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Solr Search 4 | 5 | 6 |

Solr Search

7 | 8 | Query: 9 | 10 |
11 |
12 |
13 | 14 | 15 | 51 | 52 | --------------------------------------------------------------------------------