Source code for ideas.examples.implicit_multiplication
"""
implicit_multiplication.py
---------------------------
This module is intended to demonstrate some unusual transformations
to allow someone to write equations as they would on paper
and have Python interpret them properly.
"""
from ideas import import_hook
import token_utils
[docs]def transform_source(source, **_kwargs):
"""This adds a multiplication symbol where it would be understood as
being implicit by the normal way algebraic equations are written but would
be a SyntaxError in Python. Thus we have::
2n -> 2*n
n 2 -> n* 2
2(a+b) -> 2*(a+b)
(a+b)2 -> (a+b)*2
2 3 -> 2* 3
m n -> m* n
(a+b)c -> (a+b)*c
The obvious one (in algebra) being left out is something like ``n(...)``
which is a function call - and thus valid Python syntax.
"""
tokens = token_utils.tokenize(source)
if not tokens:
return tokens
prev_token = tokens[0]
new_tokens = [prev_token]
for token in tokens[1:]:
# The code has been written in a way to demonstrate that this type of
# transformation could be done as the source is tokenized by Python.
if (
(
prev_token.is_number()
and (token.is_identifier() or token.is_number() or token == "(")
)
or (
prev_token.is_identifier()
and (token.is_identifier() or token.is_number())
)
or (prev_token == ")" and (token.is_identifier() or token.is_number()))
):
new_tokens.append("*")
new_tokens.append(token)
prev_token = token
return token_utils.untokenize(new_tokens)
[docs]def add_hook(**_kwargs):
"""Creates and automatically adds the import hook in sys.meta_path"""
hook = import_hook.create_hook(
transform_source=transform_source,
hook_name=__name__,
)
return hook