Skip to content

Instantly share code, notes, and snippets.

@jmafc
Created February 8, 2013 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmafc/4739450 to your computer and use it in GitHub Desktop.
Save jmafc/4739450 to your computer and use it in GitHub Desktop.
Minimal implementation in Python of C.J.Date's ELLIPSE - CIRCLE type hierarchy. TYPE ELLIPSE POSSREP (A LENGTH, B LENGTH, CTR POINT) CONSTRAINT A >= B); TYPE CIRCLE IS ELLIPSE CONSTRAINT THE_A(ELLLIPSE) = THE_B(ELLIPSE) POSSREP (R = THE_A(ELLIPSE), CTR = THE_CTR(ELLIPSE));
#!/usr/bin/python
class Point(object):
def __init__(self, x, y):
assert isinstance(x, int)
assert isinstance(y, int)
self.x = x
self.y = y
def __repr__(self):
return "Point(x=%s, y=%s)" % (self.x, self.y)
class Ellipse(object):
def __init__(self, a, b, ctr):
assert isinstance(a, int)
assert isinstance(b, int)
assert isinstance(ctr, Point)
assert a >= b
self.a = a
self.b = b
self.ctr = ctr
def __repr__(self):
return "Ellipse(a=%s, b=%s, ctr=%r)" % (self.a, self.b, self.ctr)
def __setattr__(self, name, value):
if not hasattr(self, 'ctr'):
self.__dict__[name] = value
return
if name == 'a':
assert value >= self.b
elif name == 'b':
assert self.a >= value
self.__dict__[name] = value
class Circle(Ellipse):
def __init__(self, r, ctr):
assert isinstance(r, int)
assert isinstance(ctr, Point)
super(Circle, self).__init__(r, r, ctr)
self.r = r
def __setattr__(self, name, value):
if hasattr(self, 'r') and (name == 'a' or name == 'b'):
raise KeyError
elif name == 'r':
super(Circle, self).__setattr__('a', value)
super(Circle, self).__setattr__('b', value)
else:
super(Circle, self).__setattr__(name, value)
def __repr__(self):
return "Circle(r=%s, ctr=%r)" % (self.a, self.ctr)
if __name__ == '__main__':
p = Point(0, 0)
e = Ellipse(4, 2, p)
c = Circle(1, p)
print("Initial")
print(p)
print(e)
print(c)
print()
e = c
print("After e = c")
print(e)
print(c)
print()
e.a = 3
print("After e.a = 3")
print(e)
print(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment