Expression

Use this object to create mathmatical expressions and convert expressions into usable code.

class cake.Expression(expression: Union[str, list], *default_args, **default_kwargs)

Create simple mathmatical expressions and solve/substitute by providing values. If you supply an expression with multiple lines, it splits them and returns a list of expressions instead of a single expression object

>>> from cake import expression
>>> circle, line = expression('(x ** 2) + (y ** 2)

2x + y’)

expression: str

The statement used when processing your statement

*default_args: Any:

Default set of arguments used when substituting the expression, IF they are not supplied during substitution. If you supply some, it skips the provided ones and uses the ones that havent been provided.

>>> from cake import expression
>>> y = expression("2x + 3", 1)
>>> y._sub()
[Symbol('('), Integer(1), ..., Operator(+), Integer(3)]
**default_kwargs: Any]

A set of keyword arguments use when substituting the expression, IF they have not been supplied during substitution. Compared to default_args, this allows you to choose which arguments to supply.

>>> from cake import expression
>>> eq = expression("x ** 2 + y ** 2")
>>> eq._sub(y=10)
[Unknown(x), ..., Operator(+), Integer(10), ...]
property expression

Returns a copy of the expression used when initialsing the class

property mapping

Returns a copy of the variable mappings for unknowns

solve(*args, **kwargs)

Equals your expression to 0 and solves it mathmatically.

Parameters
  • *args – Arguments to supply in your expression

  • **kwargs – Keyworded arguments to supply into your expression.

substitute(update_mapping: bool = False, *args, **kwargs)

Supply values, or use pre-existing values to substitute into your expression. Returns the result of the evaluated expression as a tuple.

Parameters
  • *args (Any) – Arguments to supply in your expression

  • **kwargs (Any) – Keyworded arguments to supply into your expression.

update_variables(overwrite: bool = True, *args, **kwargs) Optional[dict]

Update the built in mapping for unknown values

overwrite: bool

Should overwrite the current mapping, or return the mapping which was just created.

Defaults to True.

*args: Any

Arguments to replace, it works by adding new arguments to the start and trimming down the current args.

So if you had 10, 20 as your arguments and you supply 30 it becomes 30, 20.

**kwargs: Any

Keyworded arguments to change or add to your expression unknown mapping

wrap_all(operator: str, ending: str, *eq_args, **eq_kwargs) None

Places the entire of your current expression into brackets and adds and operator with another query.

Example

>>> from cake import expression
>>> y = expression("2x + 2")
>>> y.wrap_all("*", "2")
>>> y
(2x + 2) * 2
Parameters
  • operator (str) – The operator to use with the ending, check out how the Operator class works for more details.

  • ending (str) – A seperate query to append onto the end

  • *args (Any) – Arguments for any new vars, its best to do list(expression.args) + [...]

  • **kwargs (Any) – Keyworded arguments, works the same way as *args, except is much easier to implement.