def test_flash_minify(microbit_v1_5): view = mock.MagicMock() script = "#" + ("x" * 8193) + "\n" view.current_tab.text = mock.MagicMock(return_value=script) view.current_tab.label = "foo" view.show_message = mock.MagicMock() editor = mock.MagicMock() editor.minify = True editor.current_device = microbit_v1_5 mm = MicrobitMode(editor, view) mm.set_buttons = mock.MagicMock() with mock.patch("mu.modes.microbit.DeviceFlasher"), mock.patch( "mu.contrib.uflash._MAX_SIZE", 8188), mock.patch("nudatus.mangle", return_value="") as m: mm.flash() m.assert_called_once_with(script) ex = TokenError("Bad", (1, 0)) with mock.patch("nudatus.mangle", side_effect=ex) as m, mock.patch( "mu.contrib.uflash._MAX_SIZE", 8188): mm.flash() view.show_message.assert_called_with( 'Unable to flash "foo"', "Problem minifying script\nBad [1:0]", "Warning", )
def _add_factorial_tokens(name, result): if result == [] or result[-1][1] == '(': raise TokenError() beginning = [(NAME, name), (OP, '(')] end = [(OP, ')')] diff = 0 length = len(result) for index, token in enumerate(result[::-1]): toknum, tokval = token i = length - index - 1 if tokval == ')': diff += 1 elif tokval == '(': diff -= 1 if diff == 0: if i - 1 >= 0 and result[i - 1][0] == NAME: return result[:i - 1] + beginning + result[i - 1:] + end else: return result[:i] + beginning + result[i:] + end return result
def lambda_notation(tokens, local_dict, global_dict): """Substitutes "lambda" with its Sympy equivalent Lambda(). However, the conversion doesn't take place if only "lambda" is passed because that is a syntax error. """ result = [] flag = False toknum, tokval = tokens[0] tokLen = len(tokens) if toknum == NAME and tokval == 'lambda': if tokLen == 2: result.extend(tokens) elif tokLen > 2: result.extend([ (NAME, 'Lambda'), (OP, '('), (OP, '('), (OP, ')'), (OP, ')'), ]) for tokNum, tokVal in tokens[1:]: if tokNum == OP and tokVal == ':': tokVal = ',' flag = True if not flag and tokNum == OP and tokVal in ['*', '**']: raise TokenError("Starred arguments in lambda not supported") if flag: result.insert(-1, (tokNum, tokVal)) else: result.insert(-2, (tokNum, tokVal)) else: result.extend(tokens) return result
def test_flash_minify(): view = mock.MagicMock() script = '#' + ('x' * 8193) + '\n' view.current_tab.text = mock.MagicMock(return_value=script) view.show_message = mock.MagicMock() editor = mock.MagicMock() editor.minify = True mm = MicrobitMode(editor, view) mm.set_buttons = mock.MagicMock() with mock.patch('mu.modes.microbit.DeviceFlasher'): with mock.patch('nudatus.mangle', return_value='') as m: mm.flash() m.assert_called_once_with(script) ex = TokenError('Bad', (1, 0)) with mock.patch('nudatus.mangle', side_effect=ex) as m: mm.flash() view.show_message.assert_called_once_with('Problem with script', 'Bad [1:0]', 'Warning')
def test_flash_minify(): view = mock.MagicMock() script = "#" + ("x" * 8193) + "\n" view.current_tab.text = mock.MagicMock(return_value=script) view.show_message = mock.MagicMock() editor = mock.MagicMock() editor.minify = True mm = MicrobitMode(editor, view) mm.set_buttons = mock.MagicMock() with mock.patch("mu.modes.microbit.DeviceFlasher"): with mock.patch("nudatus.mangle", return_value="") as m: mm.flash() m.assert_called_once_with(script) ex = TokenError("Bad", (1, 0)) with mock.patch("nudatus.mangle", side_effect=ex) as m: mm.flash() view.show_message.assert_called_once_with( "Problem with script", "Bad [1:0]", "Warning" )
def _inner(tokens, local_dict, global_dict): """Group tokens between parentheses with ParenthesisGroup. Also processes those tokens recursively. """ result = [] stacks = [] stacklevel = 0 for token in tokens: if token[0] == OP: if token[1] == '(': stacks.append(ParenthesisGroup([])) stacklevel += 1 elif token[1] == ')': stacks[-1].append(token) stack = stacks.pop() if len(stacks) > 0: # We don't recurse here since the upper-level stack # would reprocess these tokens stacks[-1].extend(stack) else: # Recurse here to handle nested parentheses # Strip off the outer parentheses to avoid an infinite loop inner = stack[1:-1] inner = recursor(inner, local_dict, global_dict) parenGroup = [stack[0]] + inner + [stack[-1]] result.append(ParenthesisGroup(parenGroup)) stacklevel -= 1 continue if stacklevel: stacks[-1].append(token) else: result.append(token) if stacklevel: raise TokenError("Mismatched parentheses") return result
def lambda_notation(tokens, local_dict, global_dict): """Substitutes "lambda" with its Sympy equivalent Lambda(). However, the conversion doesn't take place if only "lambda" is passed because that is a syntax error. """ result = [] flag = False toknum, tokval = tokens[0] tokLen = len(tokens) if toknum == NAME and tokval == "lambda": if tokLen == 2 or tokLen == 3 and tokens[1][0] == NEWLINE: # In Python 3.6.7+, inputs without a newline get NEWLINE added to # the tokens result.extend(tokens) elif tokLen > 2: result.extend([ (NAME, "Lambda"), (OP, "("), (OP, "("), (OP, ")"), (OP, ")"), ]) for tokNum, tokVal in tokens[1:]: if tokNum == OP and tokVal == ":": tokVal = "," flag = True if not flag and tokNum == OP and tokVal in ["*", "**"]: raise TokenError( "Starred arguments in lambda not supported") if flag: result.insert(-1, (tokNum, tokVal)) else: result.insert(-2, (tokNum, tokVal)) else: result.extend(tokens) return result
def lambda_notation(tokens: List[TOKEN], local_dict: DICT, global_dict: DICT): """Substitutes "lambda" with its SymPy equivalent Lambda(). However, the conversion does not take place if only "lambda" is passed because that is a syntax error. """ result: List[TOKEN] = [] flag = False toknum, tokval = tokens[0] tokLen = len(tokens) if toknum == NAME and tokval == 'lambda': if tokLen == 2 or tokLen == 3 and tokens[1][0] == NEWLINE: # In Python 3.6.7+, inputs without a newline get NEWLINE added to # the tokens result.extend(tokens) elif tokLen > 2: result.extend([ (NAME, 'Lambda'), (OP, '('), (OP, '('), (OP, ')'), (OP, ')'), ]) for tokNum, tokVal in tokens[1:]: if tokNum == OP and tokVal == ':': tokVal = ',' flag = True if not flag and tokNum == OP and tokVal in ('*', '**'): raise TokenError( "Starred arguments in lambda not supported") if flag: result.insert(-1, (tokNum, tokVal)) else: result.insert(-2, (tokNum, tokVal)) else: result.extend(tokens) return result