Skip to content

Instantly share code, notes, and snippets.

Christopher O'Brien obriencj

Block or report user

Report or block obriencj

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@obriencj
obriencj / average.py
Last active Jul 6, 2019
example rolling average class in python
View average.py
class RollingAverage(object):
def __init__(self, values=(), start_sum=0, start_count=0):
self._sum = start_sum + sum(values)
self._count = start_count + len(values)
def append(self, value):
self._sum += value
self._count += 1
View gist:a32ad3de87487e4ee731625633f35aa0
=== Sample2 class code ===
Name: Sample
Filename: build_class.py
Argument count: 0
Kw-only arguments: 0
Number of locals: 0
Stack size: 4
Flags: 0x0
Constants:
0: 'sample_2.<locals>.Sample'
@obriencj
obriencj / build_class.py
Created May 6, 2018
building classes in python
View build_class.py
def sample_1(parent=object):
class Sample(parent):
def __init__(self, val=None):
self.value = val
return Sample
def sample_2(parent=object):
View sibsampl output
maybe:python-sibilant siege$ sibsampl timeit --of 1000 --times 1000
calculating fibonacci of 1000
answer is 43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
maximum recursion depth exceeded in comparison
maximum recursion depth exceeded
fibonacci with TCR over 1000 loops -> 0.6279 seconds (0.26% runtime)
fibonacci with TCO over 1000 loops -> 0.9989 seconds (0.41% runtime)
fibonacci no TC over 1000 loops -> 0.0000 seconds (0.00% runtime)
fibonacci LRU cache over 1000 loops -> 0.0000 seconds (0.00% runtime)
fibonacci loop over 1000 loops -> 0.8034 seconds (0.33% runtime)
View values_toys.py
class values(object):
def __init__(self, *args, **kwds):
self.__args = args
self.__iter__ = args.__iter__
self.__kwds = kwds
self.keys = kwds.keys
@obriencj
obriencj / output
Created Oct 25, 2017
timing different fibonacci functions in the sibilant compiler
View output
certainly:sample-project siege$ sibsampl timeit --fib 2000 --number 1000
calculating fibonacci of 2000
answer is 4224696333392304878706725602341482782579852840250681098010280137314308584370130707224123599639141511088446087538909603607640194711643596029271983312598737326253555802606991585915229492453904998722256795316982874482472992263901833716778060607011615497886719879858311468870876264597369086722884023654422295243347964480139515349562972087652656069529806499841977448720155612802665404554171717881930324025204312082516817125
fibonacci memoized -> maximum recursion depth exceeded
fibonacci via loop over 1000 loops -> 0.9649
fibonacci with TCO over 1000 loops -> 3.3678
fibonacci with TCR over 1000 loops -> 0.6501
certainly:sample-project siege$ sibsampl timeit --fib 300 --number 1000
calculating fibonacci of 300
answer is 222232244629420445529739893461909967206666939096499764990979600
@obriencj
obriencj / example
Created Sep 17, 2017
mapbind works just fine in sibilant
View example
maybe:python-sibilant siege$ sibilant
sibilant > (defimport mapbind)
sibilant > (define data (dict foo: 100 bar: 200 baz: 300 tacos: 'yum))
sibilant > data
{'tacos': <symbol 'yum'>, 'foo': 100, 'bar': 200, 'baz': 300}
sibilant > (define-values (foo bar baz) (mapbind.mapbind data))
sibilant > foo
100
sibilant > bar
200
@obriencj
obriencj / bindings.py
Last active Sep 14, 2017
local bindings from a map
View bindings.py
#! /usr/bin/env python3
# This is a Proof-Of-Concept, and only works in Python3.
# A Python2 port is almost certainly possible, haven't even tried.
from inspect import currentframe
from dis import get_instructions
@obriencj
obriencj / gist:ed947df0d8b843ff0e7f6018ae5d79de
Created Aug 17, 2017
sibilant with set-macro-character
View gist:ed947df0d8b843ff0e7f6018ae5d79de
sibilant > $100
NameError: name '$100' is not defined
sibilant > (set-macro-character "$" (lambda (stream c) `(dollars ,(read stream))) False)
sibilant > $100
NameError: name 'dollars' is not defined
sibilant > '$100
(dollars 100)
sibilant > (defun dollars (amt) (print "YOU FOUND" amt "DOLLARS!!"))
@obriencj
obriencj / specials.lspy
Created Aug 4, 2017
sibilant run and compile time special definition example
View specials.lspy
(defmacro pseudop (method . args)
(let ((as-name (symbol (+ "emit_" (str method))))
(method (symbol (+ "__compiler__.pseudop_" (str method)))))
(if args
`(defmacro ,as-name ,args
(cons ',method ,@args nil))
`(defmacro ,as-name ()
You can’t perform that action at this time.