SHARE
TWEET

Untitled

a guest May 23rd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Setter:
  2.     def __init__(self, parent, current=0, prefixes=()):
  3.         self.parent = parent
  4.         self.prefixes = prefixes
  5.  
  6.     def __getattr__(self, attribute: str):
  7.         self.prefixes += (attribute,)
  8.         if self.prefixes[self.current] == 'is_a':
  9.             setattr(self.parent, 'is_a_' + 'attribute', True)
  10.         elif self.prefixes[self.current] == 'is_not_a':
  11.             setattr(self.parent, 'is_a_' + 'attribute', False)
  12.         elif self.prefixes[self.current] == 'is_the':
  13.             return Setter(self.parent, self.current + 1, self.prefixes)
  14.         elif self.prefixes[self.current - 1] == 'is_the':
  15.             return Setter(self, parent, self.current + 1, self.prefixes)
  16.         elif self.prefixes[self.current - 2] == 'is_the':
  17.             setattr(self.parent, self.prefixes[self.current - 1], self.prefixes[self.current])
  18.  
  19.  
  20. class Thing:
  21.     def __init__(self, name):
  22.         self.name = name
  23.  
  24.     def __getattr__(self, prefix: str) -> Setter:
  25.         return Setter(self, 0, (prefix,))
  26.  
  27.  
  28. jane = Thing('Jane')
  29. assert jane.name == 'Jane'
  30.  
  31. jane.is_a.person
  32. jane.is_a.woman
  33. jane.is_not_a.man
  34.  
  35. assert jane.is_a_person
  36. assert jane.is_a_woman
  37. assert not jane.is_a_man
  38.  
  39. jane.is_the.parent_of.joe
  40. assert jane.parent_of == 'joe'
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top