Advertisement
Guest User

Untitled

a guest
Jul 9th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. class Rect:
  2.     x_fns = {
  3.         frozenset(('left', 'right')): lambda d: (d['left'], d['right']),
  4.         frozenset(('left', 'width')): lambda d: (d['left'], d['left'] + d['width']),
  5.         frozenset(('left', 'center_x')): lambda d: (d['left'], d['center_x'] * 2 - d['left']),
  6.         ...: ...
  7.     }
  8.  
  9.     y_fns = {
  10.         frozenset(('top', 'bottom')): lambda d: (d['top'], d['right']),
  11.         frozenset(('top', 'height')): lambda d: (d['top'], d['top'] + d['height']),
  12.         frozenset(('top', 'center_y')): lambda d: (d['top'], d['center_y'] * 2 - d['top']),
  13.         ...: ...
  14.     }
  15.  
  16.     @classmethod
  17.     def resolve(cls, **kwargs):
  18.         ret = cls()
  19.         x_key = frozenset(v for v in "left right center_x width".split() if v in kwargs)
  20.         ret.left, ret.right = cls.x_fns[x_key](kwargs)
  21.         y_key = frozenset(v for v in "top bottom center_y height".split() if v in kwargs)
  22.         ret.top, ret.bottom = cls.y_fns[y_key](kwargs)
  23.         return ret
  24.  
  25. r = Rect.resolve(center_x=200, left=50, center_y=200, top=50)
  26. print(vars(r))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement