veq package

Submodules

veq.calculator module

Containing module for the Calculator class.

exception veq.calculator.CalculationError(expression: str)

Bases: Exception

An exception that indicates there was an error in calculating the user’s equation.

class veq.calculator.Calculator(stream: Union[str, veq.tokens.TokenStream], stack: List[float] = [])

Bases: object

Class responsible for converting infix to postfix and calculating expressions.

Attribute stream

Expression in infix format for reading expressions.

Attribute stack

Calculation stack.

Value stack

[]

CONSTANTS = {'e': 2.718281828459045, 'g': 9.80665, 'pi': 3.141592653589793}
calculate(**variables) float

Calculate the Calculator’s expression.

Parameters

variables (dict) – Values to substitute for x in the expression.

Returns

The result of the equation.

Return type

float

infix_to_postfix()

Convert infix stream to postfix list of tokens.

Parameters

expression (str) – String to tokenize.

Returns

List of tokens

Return type

list(Token)

exception veq.calculator.ParsingError

Bases: Exception

An exception that indicates there was an error in parsing the user’s equation.

exception veq.calculator.VariableUndefinedError(variable: str)

Bases: Exception

An exception that indicates the user used an undefined variable.

veq.tokens module

Module containing all tokens and classes needed to tokenize expressions.

data TOKEN_REGEX

Regular Expression used for extracting specific token characters.

type TOKEN_REGEX

Pattern

class veq.tokens.AbsToken(stack: List)

Bases: veq.tokens.FunctionToken

Absolute Value token.

PRECEDENCE = 4
operation(a)
class veq.tokens.AddToken(stack: List)

Bases: veq.tokens.BinaryToken

Addition token.

PRECEDENCE = 1
operation(a, b)

Add a and b

class veq.tokens.ArcCosHToken(stack: List)

Bases: veq.tokens.FunctionToken

inverse hyperbolic cosine token.

PRECEDENCE = 4
operation(a)
class veq.tokens.ArcCosToken(stack: List)

Bases: veq.tokens.FunctionToken

Arc Cosine token.

PRECEDENCE = 4
operation(a)
class veq.tokens.ArcSinHToken(stack: List)

Bases: veq.tokens.FunctionToken

inverse hyperbolic sine token.

PRECEDENCE = 4
operation(a)
class veq.tokens.ArcSinToken(stack: List)

Bases: veq.tokens.FunctionToken

Arc Sine token.

PRECEDENCE = 4
operation(a)
class veq.tokens.ArcTanHToken(stack: List)

Bases: veq.tokens.FunctionToken

inverse hyperbolic tangent token.

PRECEDENCE = 4
operation(a)
class veq.tokens.ArcTanToken(stack: List)

Bases: veq.tokens.FunctionToken

Arc Tangent token.

PRECEDENCE = 4
operation(a)
class veq.tokens.BinaryToken(stack: List)

Bases: veq.tokens.Token

Binary Operation token.

execute()

Pop two items from the stack, run an operation on them, and push the result to the stack.

abstract operation(a, b)

What operation to execute.

class veq.tokens.CosToken(stack: List)

Bases: veq.tokens.FunctionToken

Cosine token.

PRECEDENCE = 4
operation(a)

Run cosine

class veq.tokens.CoshToken(stack: List)

Bases: veq.tokens.FunctionToken

Hyperbolic Cosine Token.

PRECEDENCE = 4
operation(a)
class veq.tokens.DegreesToken(stack: List)

Bases: veq.tokens.FunctionToken

Degrees Conversion Token.

PRECEDENCE = 4
operation(a)
class veq.tokens.DivideToken(stack: List)

Bases: veq.tokens.BinaryToken

Division token.

PRECEDENCE = 2
operation(a, b)

Divide a and b.

class veq.tokens.FunctionToken(stack: List)

Bases: veq.tokens.Token

Token representing a unary function.

execute()

Pop an item from the stack, run a function on it, and push the result to the stack.

abstract operation(a)
class veq.tokens.LogToken(stack: List)

Bases: veq.tokens.FunctionToken

Logarithm token.

PRECEDENCE = 4
operation(a)

Run natural logarithm.

class veq.tokens.ModuloToken(stack: List)

Bases: veq.tokens.BinaryToken

Modulo token.

PRECEDENCE = 2
operation(a, b)

What operation to execute.

class veq.tokens.MultiplyToken(stack: List)

Bases: veq.tokens.BinaryToken

Multiplication token.

PRECEDENCE = 2
operation(a, b)

Multiply a and b

class veq.tokens.NegateToken(stack: List)

Bases: veq.tokens.UnaryToken

PRECEDENCE = 5

Negation token.

operation(a)
class veq.tokens.PowerToken(stack: List)

Bases: veq.tokens.BinaryToken

Exponation token.

PRECEDENCE = 3
operation(a, b)

Exponate a to the power of b

class veq.tokens.RadiansToken(stack: List)

Bases: veq.tokens.FunctionToken

Radian Conversion Token.

PRECEDENCE = 4
operation(a)
class veq.tokens.RoundToken(stack: List)

Bases: veq.tokens.FunctionToken

Round Token.

PRECEDENCE = 4
operation(a)
class veq.tokens.SignToken(stack: List)

Bases: veq.tokens.FunctionToken

Sign Token.

PRECEDENCE = 4
operation(a)
class veq.tokens.SinToken(stack: List)

Bases: veq.tokens.FunctionToken

Sine token.

PRECEDENCE = 4
operation(a)

Run sin

class veq.tokens.SinhToken(stack: List)

Bases: veq.tokens.FunctionToken

Hyperbolic Sine Token.

PRECEDENCE = 4
operation(a)
class veq.tokens.SubtractToken(stack: List)

Bases: veq.tokens.BinaryToken

Addition token.

PRECEDENCE = 1
operation(a, b)

Subtract a and b

class veq.tokens.TanToken(stack: List)

Bases: veq.tokens.FunctionToken

Tangent token.

PRECEDENCE = 4
operation(a)

Run tangent

class veq.tokens.TanhToken(stack: List)

Bases: veq.tokens.FunctionToken

Hyperbolic Tangent Token.

PRECEDENCE = 4
operation(a)
class veq.tokens.Token(stack: List)

Bases: abc.ABC

Token Abstract Base Class.

abstract execute()

Execute the function.

property precedence

Precedence of a token.

class veq.tokens.TokenBuilder(stack: List)

Bases: object

Build tokens.

TOKENS: Dict[str, Callable[[], veq.tokens.Token]] = {'%': 'build_modulo', '*': 'build_multiply', '+': 'build_add', '/': 'build_divide', '^': 'build_power', 'abs(': 'build_abs', 'acos(': 'build_acos', 'acosh(': 'build_acosh', 'asin(': 'build_asin', 'asinh(': 'build_asinh', 'atan(': 'build_atan', 'atanh(': 'build_atanh', 'cos(': 'build_cos', 'cosh(': 'build_cosh', 'deg(': 'build_deg', 'log(': 'build_log', 'rad(': 'build_rad', 'round(': 'build_round', 'sign(': 'build_sign', 'sin(': 'build_sin', 'sinh(': 'build_sinh', 'tan(': 'build_tan', 'tanh(': 'build_tanh'}
build_abs(*args, **kwargs)

Build an instance of the AbsToken class.

build_acos(*args, **kwargs)

Build an instance of the ArcCosToken class.

build_acosh(*args, **kwargs)

Build an instance of the ArcCosHToken class.

build_add(*args, **kwargs)

Build an instance of the AddToken class.

build_asin(*args, **kwargs)

Build an instance of the ArcSinToken class.

build_asinh(*args, **kwargs)

Build an instance of the ArcSinHToken class.

build_atan(*args, **kwargs)

Build an instance of the ArcTanToken class.

build_atanh(*args, **kwargs)

Build an instance of the ArcTanHToken class.

build_cos(*args, **kwargs)

Build an instance of the CosToken class.

build_cosh(*args, **kwargs)

Build an instance of the CoshToken class.

build_deg(*args, **kwargs)

Build an instance of the DegreesToken class.

build_divide(*args, **kwargs)

Build an instance of the DivideToken class.

build_log(*args, **kwargs)

Build an instance of the LogToken class.

build_modulo(*args, **kwargs)

Build an instance of the ModuloToken class.

build_multiply(*args, **kwargs)

Build an instance of the MultiplyToken class.

build_negate(*args, **kwargs)

Build an instance of the NegateToken class.

build_power(*args, **kwargs)

Build an instance of the PowerToken class.

build_rad(*args, **kwargs)

Build an instance of the RadiansToken class.

build_round(*args, **kwargs)

Build an instance of the RoundToken class.

build_sign(*args, **kwargs)

Build an instance of the SignToken class.

build_sin(*args, **kwargs)

Build an instance of the SinToken class.

build_sinh(*args, **kwargs)

Build an instance of the SinhToken class.

build_subtract(*args, **kwargs)

Build an instance of the SubtractToken class.

build_tan(*args, **kwargs)

Build an instance of the TanToken class.

build_tanh(*args, **kwargs)

Build an instance of the TanhToken class.

build_value(value: float) veq.tokens.ValueToken

Build a value token.

build_variable(name: str) veq.tokens.VariableToken

Build a variable token.

class veq.tokens.TokenStream(expression: str)

Bases: object

A read-only representation of a stream of strings representing individual tokens.

Property text

The infix represenetation of the full expression.

reset()

Reset the stream to the beginning.

property text: str

The infix representation of the full expression.

class veq.tokens.UnaryToken(stack: List)

Bases: veq.tokens.Token

Token representing a unary operation.

execute()

Pop an item from the stack, run a function on it, and push the result to the stack.

abstract operation(a)
class veq.tokens.ValueToken(stack: List, value: float)

Bases: veq.tokens.Token

Token representing a number.

execute()

Push value to top of stack.

class veq.tokens.VariableToken(stack: List, name: str)

Bases: veq.tokens.Token

Variable token.

execute(value: float)

Push given value to top of stack.

Parameters

value (float) – What value to push.

property name: str

Return the read-only name property.

veq.tokens.add_builder(function_name: str, key: str)

This decorator allows us to dynamically add tokens to the TokenBuilder.

Parameters
  • function_name (str) – Name of the function to add.

  • key (str) – What represents the function in an equation.

veq.visualizer module

Equation visualization module.

class veq.visualizer.Equation(calculator: veq.calculator.Calculator, domain: Tuple[float, float], range: Tuple[float, float])

Bases: object

An equation with an expression, domain, and range.

Attribute calculator

The calculator holding the postfix expression to calculate.

Attribute domain

Domain of the equation.

Attribute range

Range of the equation.

shift(shift_by: Tuple[float, float])

Shift the domain and range of the equation.

Parameters

shift_by (tuple(float, float)) – What values to adjust by.

zoom(increase: float)

Adjust the domain and range by a given number to give the illusion of zooming in/out.

Parameters

increase (float) – Number to adjust by. Half will be subtracted from the left bound of the domain and range, while another half will be added to the right bound.

class veq.visualizer.Visualizer(equation: veq.visualizer.Equation, screen: pygame.Surface, *, color: Tuple[int, int, int] = (0, 0, 0), precision=2)

Bases: object

Graphical visualization of an equation.

Attribute equation

What equation to plot.

Attribute screen

pygame surface to plot on.

Attribute color

What color of line to plot.

Value color

tuple(0, 0, 0)

Property width

Width of the screen in pixels.

Property height

Height of the screen in pixels.

Property left

Left bound of the equation’s domain.

Property right

Right bound of the equation’s domain.

Property top

Left bound of the equation’s range.

Property bottom

Right bound of the equation’s range.

property bottom

Bottom bound of the equation.

draw_axis()

Draw the axis lines onto the screen.

draw_equation()

Plot the equation to the screen.

draw_grid(step: float)

Draw a grid onto the screen, with the lines seperated by a certain step.

Parameters

step (float) – What amount to seperate gridlines by.

draw_location(mouse_pos: Tuple[int, int])

Draw the location text information onto the screen.

Parameters

mouse_pos (tuple(int, int)) – Position of the mouse on the screen.

draw_saved()

Draw the saved location onto the screen.

draw_text()

Draw text onto the screen.

property dx: float

Change of x in pixels.

execute(x: float) Optional[float]

Calculate the Calculator’s expression with value x.

Parameters

x (float) – Value to substitute x for in the Visualizer’s equation.

Returns

The resulting expression, or None if there is no valid calculation for that x value.

Return type

float or None

property height

Height of the screen.

property left

Left bound of the equation.

on_screen(pixel: Tuple[int, int]) bool

Returns whether the given pixel is within the bounds of the screen.

Parameters

pixel (tuple(int, int)) – What pixel to check.

Returns

true if the pixel is within the bounds of the screen, and false if it isn’t.

Return type

bool

property precision

Precision of numbers in text format.

reset_t()

Reset the internal t variable.

property right

Right bound of the equation.

save(mouse_pos: Tuple[int, int])

Save the current location into the visualizer.

Parameters

mouse_pos (tuple(int, int)) – Position of the mouse on the screen.

property top

Top bound of the equation.

property width

Width of the screen.

veq.visualizer.remap(value: float, input_range: Tuple[float, float], output_range: Tuple[float, float]) float

Map a value from an input range to an output range.

Parameters
  • value (float) – Value to map.

  • input_range (tuple(float, float)) – Input range to map from.

  • output_range (tuple(float, float)) – Output range to map to.

Returns

Mapped value.

Return type

float

Module contents