Variables¶
VariablesDicts¶
-
class
ruffini.
VariablesDict
(variables=None, **kwargs)¶ Bases:
object
A VariablesDict is a sort of dictionary with special features, created to manage the variables of a monomial.
In this case, we’ll call keys variables and values exponents.
Its main features are:
- A VariablesDict is immutable
- The default exponent is 0; therefore, variables with exponent 0 aren’t stored
- Variables aren’t case sensitive
- Variables must be letters from the latin alphabet and one-character long
- Exponents must be positive integer
-
__init__
(variables=None, **kwargs)¶ Initialize the VariablesDict by giving it the pairs variable: exponent storing them in a dict (variables) or as keyword arguments:
>>> VariablesDict({'x': 5, 'y': 3}) {'x': 5, 'y': 3} >>> VariablesDict(x=5, y=3) {'x': 5, 'y': 3}
NB: Variables are sorted
>>> VariablesDict(k=2, b=3) {'b': 3, 'k': 2}
As said above, variables aren’t case sensitive, so they’re always made lowercase
>>> VariablesDict(X=2) {'x': 2}
It also converts the exponent in integer if it’s a whole number
>>> VariablesDict(x=3.0) {'x': 3}
It can raise an error if:
- variable is not a string
>>> VariablesDict({9: 9}) Traceback (most recent call last): ... TypeError: variable's name must be a string
- variable is too long
>>> VariablesDict(xy=3) Traceback (most recent call last): ... ValueError: variable's name length must be one
- variable is not alphabetical
>>> VariablesDict(x2=9) Traceback (most recent call last): ... ValueError: variable's name must be alphabetical
- exponent is not an integer (or a whole number)
>>> VariablesDict(k=[]) Traceback (most recent call last): ... TypeError: variable's exponent must be int
>>> VariablesDict(z=7.13) Traceback (most recent call last): ... TypeError: variable's exponent must be a whole number
- exponent is negative
>>> VariablesDict(f=-3) Traceback (most recent call last): ... ValueError: variable's exponent must be positive
It also checks if the dict is empty:
>>> VariablesDict(a=2, b=8, c=3).is_empty False >>> VariablesDict(x=0).is_empty True
Raise: TypeError, ValueError
-
__getitem__
(variable)¶ Returns the exponent for the given variable
>>> VariablesDict(a=2)['a'] 2
It returns 0 if that variable does not exists
>>> VariablesDict(a=2)['b'] 0
It can return an error if:
- variable is not a string
>>> VariablesDict({9: 9}) Traceback (most recent call last): ... TypeError: variable's name must be a string
- variable is too long
>>> VariablesDict(xy=3) Traceback (most recent call last): ... ValueError: variable's name length must be one
- variable is not alphabetical
>>> VariablesDict(x2=9) Traceback (most recent call last): ... ValueError: variable's name must be alphabetical
Return type: int
-
exponents
()¶ Returns the exponents of the VariablesDict as a tuple
>>> VariablesDict(a=2, b=3).exponents() (2, 3)
Return type: tuple
-
items
()¶ Returns the pairs variable-exponent of the VariablesDict as a tuple of tuples
>>> VariablesDict(a=2, b=3).items() (('a', 2), ('b', 3))
Return type: tuple
-
__iter__
()¶ Returns the iterator of the VariablesDict
>>> iter(VariablesDict(a=2, b=3)) {'a': 2, 'b': 3}
For more informations see
VariablesDict.__next__()
.Return type: VariablesDict
-
__next__
()¶ Gets the next variable in the VariablesDict
>>> i = iter(VariablesDict(a=2, b=3)) >>> next(i) 'a' >>> next(i) 'b' >>> next(i) Traceback (most recent call last): ... StopIteration
Return type: str Raise: StopIteration
-
__len__
()¶ Returns the dict’s len
>>> len(VariablesDict(a=2, b=3)) 2
Return type: int
-
__str__
()¶ Returns the VariablesDict as a string, like a normal dict
>>> str(VariablesDict(x=2, y=3)) "{'x': 2, 'y': 3}"
Return type: str
-
__repr__
()¶ Returns the VariablesDict as a string
>>> repr(VariablesDict(y=5)) "{'y': 5}"
For more informations see
VariablesDict.__str__()
.Return type: str
-
__add__
(other)¶ Sums two VariablesDict, returning a VariablesDict whose exponents are the sum of the starting VariablesDicts’ ones
>>> VariablesDict(x=5, y=3) + VariablesDict(y=5) {'x': 5, 'y': 8}
It raises a TypeError if other is not a VariablesDict
>>> VariablesDict(x=1) + 3 Traceback (most recent call last): ... TypeError: unsupported operand type(s) for +: 'VariablesDict' and 'int'
Return type: VariablesDict Raise: TypeError
-
__sub__
(other)¶ Return a VariablesDict whose values are the difference between the starting VariablesDicts’ ones
>>> VariablesDict(x=5, y=3) - VariablesDict(x=1, y=2) {'x': 4, 'y': 1}
If any exponent becomes negative, a ValueError will be raised instead:
>>> VariablesDict(c=2) - VariablesDict(c=3) Traceback (most recent call last): ... ValueError: variable's exponent must be positive
It raises a TypeError if other is not a VariablesDict
>>> VariablesDict(x=1) - 3 Traceback (most recent call last): ... TypeError: unsupported operand type(s) for -: 'VariablesDict' and 'int'
Return type: VariablesDict Raise: ValueError, TypeError
-
__mul__
(other)¶ Returns a VariablesDict whose exponents are this one’s, multiplied by a given (integer) number
>>> VariablesDict(a=2, b= 5) * 3 {'a': 6, 'b': 15}
If the number is negative, a ValueError is raised
>>> VariablesDict() * (-15) Traceback (most recent call last): ... ValueError: can't multiply a VariablesDict by a negative number
It raises a TypeError if other is not an integer
>>> VariablesDict(x=1) * '3' Traceback (most recent call last): ... TypeError: unsupported operand type(s) for *: 'VariablesDict' and 'str'
Return type: VariablesDict Raise: TypeError, ValueError
-
__truediv__
(other)¶ Returns a VariablesDict whose values are this one’s divided by a given (integer) number
>>> VariablesDict(a=4, b=2) / 2 {'a': 2, 'b': 1}
If the VariableDict is not divisible by the given number, it will raise a ValueError
>>> VariablesDict(x=7) / 3 Traceback (most recent call last): ... ValueError: can't divide this VariablesDict by 3
To see if a VariablesDict is divisible by a number, you can use modulus operator (see more at
VariablesDict.__mod__()
):It raises a TypeError if other is not an integer
>>> VariablesDict(x=1) / '3' Traceback (most recent call last): ... TypeError: unsupported operand type(s) for /: 'VariablesDict' and 'str'
Return type: VariablesDict Raises: ValueError, TypeError
-
__mod__
(other)¶ Checks if the VariablesDict can be divided by a number (True => can be divided by other).
>>> VariablesDict(a=2, b=4) % 2 True >>> VariablesDict(a=2, b=4) % 3 False
It raises ValueError if other isn’t a positive integer
>>> VariablesDict(k=2) % (-7) Traceback (most recent call last): ... ValueError: can't use modulus with VariablesDict and negative numbers
It raises a TypeError if other is not an integer
>>> VariablesDict(x=1) % '3' Traceback (most recent call last): ... TypeError: unsupported operand type(s) for %: 'VariablesDict' and 'str'
Return type: bool Raise: TypeError, ValueError
-
__eq__
(other)¶ Checks if two variablesDict are equivalent
>>> VariablesDict(a=2, b=4) == VariablesDict(b=4, a=2) True
If other is not a VariablesDict it always returns False
>>> VariablesDict(a=2, b=4) == 3 False
Return type: bool
-
__hash__
()¶ Returns the hash of the VariablesDict by hashing the result of
VariablesDict.items()
.Return type: int
-
__bool__
()¶ Returns the opposite of is_empty.
>>> VariablesDict().is_empty True >>> bool(VariablesDict()) False
Return type: int