bag.util.math

Module Contents

Classes

HalfInt

A class that represents a half integer.

Calculator

A simple calculator.

class bag.util.math.HalfInt(dbl_val: Any)[source]

Bases: numbers.Integral

A class that represents a half integer.

property value: float[source]
property is_integer: bool[source]
property dbl_value: int[source]
classmethod convert(val: Any) HalfInt[source]
div2(round_up: bool = False) HalfInt[source]
to_string() str[source]
up() HalfInt[source]
down() HalfInt[source]
up_even(flag: bool) HalfInt[source]
down_even(flag: bool) HalfInt[source]
__str__()[source]

Return str(self).

__repr__()[source]

Return repr(self).

__hash__()[source]

Return hash(self).

__eq__(other)[source]

self == other

__ne__(other)[source]

Return self!=value.

__le__(other)[source]

self <= other

__lt__(other)[source]

self < other

< on Reals defines a total ordering, except perhaps for NaN.

__ge__(other)[source]

Return self>=value.

__gt__(other)[source]

Return self>value.

__add__(other)[source]

self + other

__sub__(other)[source]

self - other

__mul__(other)[source]

self * other

__truediv__(other)[source]

self / other: Should promote to float when necessary.

__floordiv__(other)[source]

self // other: The floor() of self/other.

__mod__(other)[source]

self % other

__divmod__(other)[source]

divmod(self, other): The pair (self // other, self % other).

Sometimes this can be computed faster than the pair of operations.

__pow__(other, modulus=None)[source]

self ** exponent % modulus, but maybe faster.

Accept the modulus argument if you want to support the 3-argument version of pow(). Raise a TypeError if exponent < 0 or any argument isn’t Integral. Otherwise, just implement the 2-argument version described in Complex.

__lshift__(other)[source]

self << other

__rshift__(other)[source]

self >> other

__and__(other)[source]

self & other

__xor__(other)[source]

self ^ other

__or__(other)[source]

self | other

__radd__(other)[source]

other + self

__rsub__(other)[source]

other - self

__rmul__(other)[source]

other * self

__rtruediv__(other)[source]

other / self

__rfloordiv__(other)[source]

other // self: The floor() of other/self.

__rmod__(other)[source]

other % self

__rdivmod__(other)[source]

divmod(other, self): The pair (self // other, self % other).

Sometimes this can be computed faster than the pair of operations.

__rpow__(other)[source]

base ** self

__rlshift__(other)[source]

other << self

__rrshift__(other)[source]

other >> self

__rand__(other)[source]

other & self

__rxor__(other)[source]

other ^ self

__ror__(other)[source]

other | self

__iadd__(other)[source]
__isub__(other)[source]
__imul__(other)[source]
__itruediv__(other)[source]
__ifloordiv__(other)[source]
__imod__(other)[source]
__ipow__(other)[source]
__ilshift__(other)[source]
__irshift__(other)[source]
__iand__(other)[source]
__ixor__(other)[source]
__ior__(other)[source]
__neg__()[source]

-self

__pos__()[source]

+self

__abs__()[source]

Returns the Real distance from 0. Called for abs(self).

__invert__()[source]

~self

__complex__()[source]

complex(self) == complex(float(self), 0)

__int__()[source]

int(self)

__float__()[source]

float(self) == float(int(self))

__index__()[source]

Called whenever an index is needed, such as in slicing

__round__(ndigits=0)[source]

Rounds self to ndigits decimal places, defaulting to 0.

If ndigits is omitted or None, returns an Integral, otherwise returns a Real. Rounds half toward even.

__trunc__()[source]

trunc(self): Truncates self to an Integral.

Returns an Integral i such that:
  • i>0 iff self>0;

  • abs(i) <= abs(self);

  • for any Integral j satisfying the first two conditions, abs(i) >= abs(j) [i.e. i has “maximal” abs among those].

i.e. “truncate towards 0”.

__floor__()[source]

Finds the greatest Integral <= self.

__ceil__()[source]

Finds the least Integral >= self.

class bag.util.math.Calculator(namespace: Mapping[str, Any])[source]

Bases: ast.NodeVisitor

A simple calculator.

Modified from: https://stackoverflow.com/questions/33029168/how-to-calculate-an-equation-in-a-string-python

user mgilson said in a comment that he agrees to distribute code with Apache 2.0 license.

property namespace: Mapping[str, Any][source]
_OP_MAP[source]
__getitem__(name: str) Any[source]
visit_BinOp(node)[source]
visit_UnaryOp(node)[source]
visit_Num(node)[source]
visit_Expr(node)[source]
visit_Name(node)[source]
eval(expression: str)[source]
classmethod evaluate(expr: str, namespace: Mapping[str, Any])[source]