Advertisement
Guest User

Untitled

a guest
Jul 9th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #==================== Current approach ====================
  2.  
  3. @classmethod
  4. def from_partial(cls, **kwargs):
  5. assert 2 == sum(1 for name in ("left", "right", "center_x", "width") if name in kwargs), "x coordinates are overspecified or underspecified"
  6. if "width" in kwargs and "left" in kwargs:
  7. right = kwargs["width"] + kwargs["left"]
  8. elif "right" in kwargs and "width" in kwargs:
  9. left = kwargs["right"] - kwargs["width"]
  10. elif "center_x" in kwargs and "right" in kwargs:
  11. left = 2*kwargs["center_x"] - kwargs["right"]
  12. elif "center_x" in kwargs and "left" in kwargs:
  13. right = 2*kwargs["center_x"] - kwargs["left"]
  14. elif "center_x" in kwargs and "width" in kwargs:
  15. left = kwargs["center_x"] - kwargs["width"]/2
  16. right = kwargs["center_x"] + kwargs["width"]/2
  17. if "left" in kwargs: left = kwargs["left"]
  18. if "right" in kwargs: right = kwargs["right"]
  19.  
  20. assert 2 == sum(1 for name in ("top", "bottom", "center_y", "height") if name in kwargs), "y coordinates are overspecified or underspecified"
  21. if "height" in kwargs and "top" in kwargs:
  22. bottom = kwargs["height"] + kwargs["top"]
  23. elif "bottom" in kwargs and "height" in kwargs:
  24. top = kwargs["bottom"] - kwargs["height"]
  25. elif "center_y" in kwargs and "bottom" in kwargs:
  26. top = 2*kwargs["center_y"] - kwargs["bottom"]
  27. elif "center_y" in kwargs and "top" in kwargs:
  28. bottom = 2*kwargs["center_y"] - kwargs["top"]
  29. elif "center_y" in kwargs and "height" in kwargs:
  30. top = kwargs["center_y"] - kwargs["height"]/2
  31. bottom = kwargs["center_y"] + kwargs["height"]/2
  32. if "top" in kwargs: top = kwargs["top"]
  33. if "bottom" in kwargs: bottom = kwargs["bottom"]
  34. return cls(left, top, right, bottom)
  35.  
  36. #==================== Namespacey approach ====================
  37. @classmethod
  38. def from_partial(cls, **kwargs):
  39. d = create_magical_namespace(kwargs)
  40. assert 2 == sum(1 for name in ("left", "right", "center_x", "width") if name in d), "x coordinates are overspecified or underspecified"
  41. if "width" in d and "left" in d:
  42. right = d.width + d.left
  43. elif "right" in d and "width" in d:
  44. left = d.right - d.width
  45. elif "center_x" in d and "right" in d:
  46. left = 2*d.center_x - d.right
  47. elif "center_x" in d and "left" in d:
  48. right = 2*d.center_x - d.left
  49. elif "center_x" in d and "width" in d:
  50. left = d.center_x - d.width/2
  51. right = d.center_x + d.width/2
  52. if "left" in d: left = d.left
  53. if "right" in d: right = d.right
  54.  
  55. assert 2 == sum(1 for name in ("top", "bottom", "center_y", "height") if name in d), "y coordinates are overspecified or underspecified"
  56. if "height" in d and "top" in d:
  57. bottom = d.height + d.top
  58. elif "bottom" in d and "height" in d:
  59. top = d.bottom - d.height
  60. elif "center_y" in d and "bottom" in d:
  61. top = 2*d.center_y - d.bottom
  62. elif "center_y" in d and "top" in d:
  63. bottom = 2*d.center_y - d.top
  64. elif "center_y" in d and "height" in d:
  65. top = d.center_y - d.height/2
  66. bottom = d.center_y + d.height/2
  67. if "top" in d: top = d.top
  68. if "bottom" in d: bottom = d.bottom
  69. return cls(left, top, right, bottom)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement