diff --git a/README.md b/README.md index 19e5d28..0d0bb0f 100644 --- a/README.md +++ b/README.md @@ -1635,7 +1635,6 @@ last_el = op.methodcaller('pop')() Eval ---- -### Basic ```python >>> from ast import literal_eval >>> literal_eval('1 + 2') @@ -1646,51 +1645,6 @@ Eval ValueError: malformed node or string ``` -### Using Abstract Syntax Trees -```python -import ast -from ast import Num, BinOp, UnaryOp -import operator as op - -LEGAL_OPERATORS = {ast.Add: op.add, # + - ast.Sub: op.sub, # - - ast.Mult: op.mul, # * - ast.Div: op.truediv, # / - ast.Pow: op.pow, # ** - ast.BitXor: op.xor, # ^ - ast.USub: op.neg} # - - -def evaluate(expression): - root = ast.parse(expression, mode='eval') - return eval_node(root.body) - -def eval_node(node): - node_type = type(node) - if node_type == Num: - return node.n - if node_type not in [BinOp, UnaryOp]: - raise TypeError(node) - operator_type = type(node.op) - if operator_type not in LEGAL_OPERATORS: - raise TypeError(f'Illegal operator {node.op}') - operator = LEGAL_OPERATORS[operator_type] - if node_type == BinOp: - left, right = eval_node(node.left), eval_node(node.right) - return operator(left, right) - elif node_type == UnaryOp: - operand = eval_node(node.operand) - return operator(operand) -``` - -```python ->>> evaluate('2 ^ 6') -4 ->>> evaluate('2 ** 6') -64 ->>> evaluate('1 + 2 * 3 ** (4 ^ 5) / (6 + -7)') --5.0 -``` - Coroutine --------- diff --git a/index.html b/index.html index 5024dbd..846cd20 100644 --- a/index.html +++ b/index.html @@ -1387,7 +1387,6 @@ LogicOp = enum.Enum('LogicOp', {'pop')(<list>)

#Eval

-

Basic

>>> from ast import literal_eval
 >>> literal_eval('1 + 2')
 3
@@ -1396,47 +1395,6 @@ last_el          = op.methodcaller('pop')(<l
 >>> literal_eval('abs(1)')
 ValueError: malformed node or string
 
-

Using Abstract Syntax Trees

-
import ast
-from ast import Num, BinOp, UnaryOp
-import operator as op
-
-LEGAL_OPERATORS = {ast.Add:    op.add,      # <el> + <el>
-                   ast.Sub:    op.sub,      # <el> - <el>
-                   ast.Mult:   op.mul,      # <el> * <el>
-                   ast.Div:    op.truediv,  # <el> / <el>
-                   ast.Pow:    op.pow,      # <el> ** <el>
-                   ast.BitXor: op.xor,      # <el> ^ <el>
-                   ast.USub:   op.neg}      # - <el>
-
-def evaluate(expression):
-    root = ast.parse(expression, mode='eval')
-    return eval_node(root.body)
-
-def eval_node(node):
-    node_type = type(node)
-    if node_type == Num:
-        return node.n
-    if node_type not in [BinOp, UnaryOp]:
-        raise TypeError(node)
-    operator_type = type(node.op)
-    if operator_type not in LEGAL_OPERATORS:
-        raise TypeError(f'Illegal operator {node.op}')
-    operator = LEGAL_OPERATORS[operator_type]
-    if node_type == BinOp:
-        left, right = eval_node(node.left), eval_node(node.right)
-        return operator(left, right)
-    elif node_type == UnaryOp:
-        operand = eval_node(node.operand)
-        return operator(operand)
-
-
>>> evaluate('2 ^ 6')
-4
->>> evaluate('2 ** 6')
-64
->>> evaluate('1 + 2 * 3 ** (4 ^ 5) / (6 + -7)')
--5.0
-

#Coroutine

  • Similar to generator, but generator pulls data through the pipe with iteration, while coroutine pushes data into the pipeline with send().
  • @@ -1626,7 +1584,7 @@ run(host='0.0.0.0', port='arsenal f.c.', 2.44, 3.29]

    #Profile

    -

    Basic

    +

    Basic

    from time import time
     start_time = time()  # Seconds since Epoch.
     ...