Skip to content

Instantly share code, notes, and snippets.

@ijp
Created March 29, 2015 14:55

Revisions

  1. ijp created this gist Mar 29, 2015.
    40 changes: 40 additions & 0 deletions parameters.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    # a sketch of how Scheme fluids/parameters would work in python

    obarray = {}

    class Manager(object):
    def __init__(self, parameter, value):
    self.parameter = parameter
    self.value = value

    def __enter__(self):
    self.old_value = self.parameter.get()
    obarray[self.parameter] = self.value

    def __exit__(self, exc_type, exc_value, traceback):
    ## A real pro would handle exceptions
    obarray[self.parameter] = self.old_value

    class Parameter(object):
    def __init__(self, value):
    obarray[self] = value

    def get(self):
    return obarray[self]

    def __call__(self, new_value):
    return Manager(self, new_value)

    current_foo = Parameter(10)

    print "1", current_foo.get()

    with current_foo(20):
    print "2", current_foo.get()

    with current_foo(30):
    print "3", current_foo.get()

    print "2", current_foo.get()

    print "1", current_foo.get()