Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==================== Current approach ====================
- @classmethod
- def from_partial(cls, **kwargs):
- assert 2 == sum(1 for name in ("left", "right", "center_x", "width") if name in kwargs), "x coordinates are overspecified or underspecified"
- if "width" in kwargs and "left" in kwargs:
- right = kwargs["width"] + kwargs["left"]
- elif "right" in kwargs and "width" in kwargs:
- left = kwargs["right"] - kwargs["width"]
- elif "center_x" in kwargs and "right" in kwargs:
- left = 2*kwargs["center_x"] - kwargs["right"]
- elif "center_x" in kwargs and "left" in kwargs:
- right = 2*kwargs["center_x"] - kwargs["left"]
- elif "center_x" in kwargs and "width" in kwargs:
- left = kwargs["center_x"] - kwargs["width"]/2
- right = kwargs["center_x"] + kwargs["width"]/2
- if "left" in kwargs: left = kwargs["left"]
- if "right" in kwargs: right = kwargs["right"]
- assert 2 == sum(1 for name in ("top", "bottom", "center_y", "height") if name in kwargs), "y coordinates are overspecified or underspecified"
- if "height" in kwargs and "top" in kwargs:
- bottom = kwargs["height"] + kwargs["top"]
- elif "bottom" in kwargs and "height" in kwargs:
- top = kwargs["bottom"] - kwargs["height"]
- elif "center_y" in kwargs and "bottom" in kwargs:
- top = 2*kwargs["center_y"] - kwargs["bottom"]
- elif "center_y" in kwargs and "top" in kwargs:
- bottom = 2*kwargs["center_y"] - kwargs["top"]
- elif "center_y" in kwargs and "height" in kwargs:
- top = kwargs["center_y"] - kwargs["height"]/2
- bottom = kwargs["center_y"] + kwargs["height"]/2
- if "top" in kwargs: top = kwargs["top"]
- if "bottom" in kwargs: bottom = kwargs["bottom"]
- return cls(left, top, right, bottom)
- #==================== Namespacey approach ====================
- @classmethod
- def from_partial(cls, **kwargs):
- d = create_magical_namespace(kwargs)
- assert 2 == sum(1 for name in ("left", "right", "center_x", "width") if name in d), "x coordinates are overspecified or underspecified"
- if "width" in d and "left" in d:
- right = d.width + d.left
- elif "right" in d and "width" in d:
- left = d.right - d.width
- elif "center_x" in d and "right" in d:
- left = 2*d.center_x - d.right
- elif "center_x" in d and "left" in d:
- right = 2*d.center_x - d.left
- elif "center_x" in d and "width" in d:
- left = d.center_x - d.width/2
- right = d.center_x + d.width/2
- if "left" in d: left = d.left
- if "right" in d: right = d.right
- assert 2 == sum(1 for name in ("top", "bottom", "center_y", "height") if name in d), "y coordinates are overspecified or underspecified"
- if "height" in d and "top" in d:
- bottom = d.height + d.top
- elif "bottom" in d and "height" in d:
- top = d.bottom - d.height
- elif "center_y" in d and "bottom" in d:
- top = 2*d.center_y - d.bottom
- elif "center_y" in d and "top" in d:
- bottom = 2*d.center_y - d.top
- elif "center_y" in d and "height" in d:
- top = d.center_y - d.height/2
- bottom = d.center_y + d.height/2
- if "top" in d: top = d.top
- if "bottom" in d: bottom = d.bottom
- return cls(left, top, right, bottom)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement