Skip to content

Instantly share code, notes, and snippets.

@hamilton
Created October 28, 2011 22:00
Show Gist options
  • Save hamilton/1323695 to your computer and use it in GitHub Desktop.
Save hamilton/1323695 to your computer and use it in GitHub Desktop.
Implementing formula notation in python
# Here's a small experiment in hacking together a formula notation for
# Python, in the style of R. This is without reference to any matrices, but
# it's pretty straightforward to see how one might plug such a system into a
# matrix library.
# I bet this could easily extend to other R formula features.
class Predictor(object):
def __init__(self,label):
self.label = label
def __add__(self, next):
return PredictorSet(self, next)
def __str__(self):
return self.label
class PredictorSet(object):
def __init__(self, *args):
self.preds = args
def __add__(self, next):
self.preds.append(next)
def __str__(self):
return ' + '.join([str(p) for p in self.preds])
class Response(object):
def __init__(self, label):
self.label = label
def __or__(self, predictor_set):
return Formula(self, predictor_set)
def __str__(self):
return self.label
class Formula(object):
def __init__(self, r, p):
self.r = r
self.p = p
def __str__(self):
return str(self.r) + ' | ' + str(self.p)
if __name__ == "__main__":
# Ignore the fact that you have to define variables like this.
# The point is the formula class instantiation AS a formula,
# and not the var defs. That can easily be solved alongside a
# worthwhile data frame implementation.
a = Response('return')
b = Predictor('age')
c = Predictor('weight')
# this gives you an object of type 'Formula'. Bam.
formula = a | b + c
print formula
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment