Created
October 31, 2017 23:09
-
-
Save blackhathedgehog/c88e1b85352e531fe6a98b0b13a96f2e to your computer and use it in GitHub Desktop.
Quick Monkeypatch example of the 0.3.0 to allow Literal Value to be directly mathematically operated on
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from python_jsonschema_objects.classbuilder import LiteralValue | |
# int and float are already implemented, and thus not shown here | |
LiteralValue.__getattr__ = lambda self, name: getattr(self._value, name) | |
LiteralValue.__add__ = lambda self, other: self._value + other | |
LiteralValue.__sub__ = lambda self, other: self._value - other | |
LiteralValue.__mul__ = lambda self, other: self._value * other | |
LiteralValue.__matmul__ = lambda self, other: self._value @ other | |
LiteralValue.__truediv__ = lambda self, other: self._value / other | |
LiteralValue.__floordiv__ = lambda self, other: self._value // other | |
LiteralValue.__mod__ = lambda self, other: self._value % other | |
LiteralValue.__divmod__ = lambda self, other: divmod(self._value, other) | |
LiteralValue.__pow__ = lambda self, other, modulo=None: pow(self._value, other, modulo) | |
LiteralValue.__lshift__ = lambda self, other: self._value << other | |
LiteralValue.__rshift__ = lambda self, other: self._value >> other | |
LiteralValue.__and__ = lambda self, other: self._value & other | |
LiteralValue.__xor__ = lambda self, other: self._value ^ other | |
LiteralValue.__or__ = lambda self, other: self._value | other | |
LiteralValue.__radd__ = lambda self, other: other + self._value | |
LiteralValue.__rsub__ = lambda self, other: other - self._value | |
LiteralValue.__rmul__ = lambda self, other: other * self._value | |
LiteralValue.__rmatmul__ = lambda self, other: other @ self._value | |
LiteralValue.__rtruediv__ = lambda self, other: other / self._value | |
LiteralValue.__rfloordiv__ = lambda self, other: other // self._value | |
LiteralValue.__rmod__ = lambda self, other: other % self._value | |
LiteralValue.__rdivmod__ = lambda self, other: divmod(other, self._value) | |
LiteralValue.__rpow__ = lambda self, other, modulo=None: pow(other, self._value, modulo) | |
LiteralValue.__rlshift__ = lambda self, other: other << self._value | |
LiteralValue.__rrshift__ = lambda self, other: other >> self._value | |
LiteralValue.__rand__ = lambda self, other: other & self._value | |
LiteralValue.__rxor__ = lambda self, other: other ^ self._value | |
LiteralValue.__ror__ = lambda self, other: other | self._value | |
def literalIAdd(self, other): | |
self._value += other | |
def literalISub(self, other): | |
self._value -= other | |
def literalIMul(self, other): | |
self._value *= other | |
def literalIMatMul(self, other): | |
self._value @= other | |
def literalITrueDiv(self, other): | |
self._value /= other | |
def literalIFloorDiv(self, other): | |
self._value //= other | |
def literalIMod(self, other): | |
self._value %= other | |
def literalIPow(self, other, modulo=None): | |
self._value = pow(self._value, other, modulo) | |
def literalILShift(self, other): | |
self._value <<= other | |
def literalIRShift(self, other): | |
self._value >>= other | |
def literalIAnd(self, other): | |
self._value &= other | |
def literalIXor(self, other): | |
self._value ^= other | |
def literalIOr(self, other): | |
self._value |= other | |
LiteralValue.__iadd__ = literalIAdd | |
LiteralValue.__isub__ = literalISub | |
LiteralValue.__imul__ = literalIMul | |
LiteralValue.__imatmul__ = literalIMatMul | |
LiteralValue.__itruediv__ = literalITrueDiv | |
LiteralValue.__ifloordiv__ = literalIFloorDiv | |
LiteralValue.__imod__ = literalIMod | |
LiteralValue.__ipow__ = literalIPow | |
LiteralValue.__ilshift__ = literalILShift | |
LiteralValue.__irshift__ = literalIRShift | |
LiteralValue.__iand__ = literalIAnd | |
LiteralValue.__ixor__ = literalIXor | |
LiteralValue.__ior__ = literalIOr | |
LiteralValue.__neg__ = lambda self: -self._value | |
LiteralValue.__pos__ = lambda self: +self._value | |
LiteralValue.__abs__ = lambda self: abs(self._value) | |
LiteralValue.__invert__ = lambda self: ~self._value | |
LiteralValue.__complex__ = lambda self: complex(self._value) | |
LiteralValue.__round__ = lambda self, n=None: round(self._value, n) | |
# this may need some extra validation. Eg, this is only defined for "integer" types, not all types. | |
# it should probably do a __propinfo__ check to make sure, then only return int() if it is a whole integer | |
# otherwise this violates the __index__ clause of __index__ only being defined for whole numbers | |
LiteralValue.__index__ = lambda self: int(self._value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment