本文整理汇总了Python中Lexer.Lexer.scan方法的典型用法代码示例。如果您正苦于以下问题:Python Lexer.scan方法的具体用法?Python Lexer.scan怎么用?Python Lexer.scan使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lexer.Lexer
的用法示例。
在下文中一共展示了Lexer.scan方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestLinearEquation
# 需要导入模块: from Lexer import Lexer [as 别名]
# 或者: from Lexer.Lexer import scan [as 别名]
class TestLinearEquation():
@classmethod
def setupClass(self):
rules = {
"Number": r"\d+(\.\d+)?",
"Operator": r"[-+()*]",
}
self.lex = Lexer(rules, False)
pass
def TestKnownValues(self):
""" Tests some linear equation by testing against known output """
var = ['Cost','PaintA','PaintB','PaintC','Quantity']
tokens = [(group,value) for group, value in self.lex.scan("2PaintA+3PaintB-0.5Cost")]
par = Parser(tokens, {}, var)
assert par.parseLinearEquation() == [(2, 1), (3, 2), (-0.5, 0)]
tokens = [(group,value) for group, value in self.lex.scan("5Cost*3.1+(-2)8*Quantity-(-8)PaintA")]
par = Parser(tokens, {}, var)
assert par.parseLinearEquation() == [(15.5, 0), (-16, 4), (8, 1)]
tokens = [(group,value) for group, value in self.lex.scan("2(1+0.5)PaintA+33*Quantity-13Cost*(2.5+1)")]
par = Parser(tokens, {}, var)
assert par.parseLinearEquation() == [(3.0, 1), (33, 4), (-45.5, 0)]
def TestNonLinearEquations(self):
""" Tests that non-linear equations are flagged as Malformed Equation """
var = ['x','y','z']
test_eqn = [
"2xy+y-3z",
"2x+3xyz-8z",
"x-y-xy+2z"
]
for eqn in test_eqn:
tokens = [(group,value) for group, value in self.lex.scan(eqn)]
par = Parser(tokens, {}, var)
nose.tools.assert_raises(MalformedEquationError, par.parseLinearEquation)
def TestImproperBrackets(self):
""" Tests that improperly placed brackets in the equation raises MalformedEquationError
Though mathematically correct, for the function to work, brackets must be properly placed
and should not envelop more than one variable"""
var = ['x','y','z']
test_eqn = [
"(2x+y)-3z",
"(2*4x-(-3)y)-8z",
]
for eqn in test_eqn:
tokens = [(group,value) for group, value in self.lex.scan(eqn)]
par = Parser(tokens, {}, var)
nose.tools.assert_raises(MalformedEquationError, par.parseLinearEquation)
示例2: TestFunctionPriority
# 需要导入模块: from Lexer import Lexer [as 别名]
# 或者: from Lexer.Lexer import scan [as 别名]
def TestFunctionPriority(self):
""" Function names must be given top preference
Equations snippets to check:
exp(check)
log10(tx)
sin(ccos(y))
eexp(22)
"""
lex = Lexer(self.rules)
assert [(g,v) for g,v in lex.scan("exp(check)")].pop(0) == ('Function', 'exp(')
assert [(g,v) for g,v in lex.scan("log10(tx)")].pop(0) == ('Function', 'log10(')
dualFunc = [(g,v) for g,v in lex.scan("sin(ccos(y))")]
assert dualFunc[0] == ('Function', 'sin(')
assert dualFunc[2] == ('Function', 'cos(')
assert [(g,v) for g,v in lex.scan("eexp(22)")][1] == ('Function', 'exp(')
pass
示例3: TestCaseInsensitivity
# 需要导入模块: from Lexer import Lexer [as 别名]
# 或者: from Lexer.Lexer import scan [as 别名]
def TestCaseInsensitivity(self):
""" Function names should be case-insensitive
exp() = Exp() = EXP()
Equations to test:
aExp(bX) + SIN(theta) + cos(z)
dLog(LOG(x)) + hcosH(y)
"""
lex = Lexer(self.rules, False)
t1 = [(g,v) for g,v in lex.scan("aExp(bX) + SIN(theta) + cos(z)")]
assert t1[1] == ('Function', 'Exp(')
assert t1[6] == ('Function', 'SIN(')
assert t1[14] == ('Function', 'cos(')
t2 = [(g,v) for g,v in lex.scan("dLog(LOG(x)) + hcosH(y)")]
assert t2[1] == ('Function', 'Log(')
assert t2[2] == ('Function', 'LOG(')
assert t2[8] == ('Function', 'cosH(')
pass
示例4: TestVariablesNaming
# 需要导入模块: from Lexer import Lexer [as 别名]
# 或者: from Lexer.Lexer import scan [as 别名]
def TestVariablesNaming(self):
""" Numeric unknowns must be preceded by alphabetic unknowns
can also be phrased as,
variable names cannot begin with numerical digit.
Equations to test:
23x15sin(zy12*100u) + AF1atan(99c)
"""
lex = Lexer(self.rules)
test = []
for g,v in lex.scan("23x15sin(zy12*100u) + AF1atan(99c)"):
if g == 'Unknown' and re.match(r"\d+(\.\d+)?", v):
assert test[-1][0] == 'Unknown' and re.match(r"^[_a-zA-Z]$",test[-1][1])
test.append((g,v))
pass
示例5: main
# 需要导入模块: from Lexer import Lexer [as 别名]
# 或者: from Lexer.Lexer import scan [as 别名]
def main():
var = []
const = []
eqn = []
reserved = ['exp','log','log10','acos','asin','atan','cos','sin','tan','cosh','sinh','tanh']
f = open(r'C:\Stuff\work\inputmv.txt', 'r')
for line in f:
if line[-1]=="\n":
line = line[:-1]
if line.find('var',0,3) == 0:
var = line[line.find(' ')+1:].split(',')
elif line.find('const',0,5) == 0:
const = line[line.find(' ')+1:].split(',')
elif len(line) > 0:
eqn.append(line)
f.close()
# Make sure they are sorted by length
if len(const) > 0:
const.sort(lambda x, y: len(x)-len(y))
if len(var) > 0:
var.sort(lambda x, y: len(x)-len(y))
for word in reserved:
if word in var or word in const:
raise InvalidNameError(word)
rules = {
"Function": r"(exp|log|log10|acos|asin|atan|cos|sin|tan|cosh|sinh|tanh)\(",
"Number": r"\d+(\.\d+)?",
"Operator": r"[-+()*^]",
}
lex = Lexer(rules, False)
out = ['[']
for equation in eqn:
tokens = [(group,value) for group, value in lex.scan(equation)]
par = Parser(tokens, const, var)
out.append(par.parseTokens())
out.append(',')
out.pop()
out.append(']')
ret = ''.join(out)
print ret
# Rewrite code including safe locals and globals dict
#f = lambda x: eval(ret)
#result = fsolve(f,[1,1])
#print result
pass
示例6: TestNumbersTokens
# 需要导入模块: from Lexer import Lexer [as 别名]
# 或者: from Lexer.Lexer import scan [as 别名]
def TestNumbersTokens(self):
""" Numeric constants cannot appear immediately after unknowns.
They must be seperated by either * or () or any other operator
In other words:
Numerical constants must not be preceded by unknowns.
They can follow only after operator or functions.
Equation to test:
12xy2*15cz(2.54) + tan(23y)18c19(0.01)
"""
lex = Lexer(self.rules)
test = []
for g,v in lex.scan("12xy2*15cz(2.54) + tan(23y)18c19(0.01)"):
if g == 'Number':
if len(test) > 0:
assert test[-1] != 'Unknown'
test.append((g,v))
pass
示例7: TestComplexEquations
# 需要导入模块: from Lexer import Lexer [as 别名]
# 或者: from Lexer.Lexer import scan [as 别名]
#.........这里部分代码省略.........
('Operator', '*'),
('Number', '10'),
('Operator', '*'),
('Function', 'log10('),
('Unknown', 'l'),
('Unknown', 'o'),
('Unknown', 'g'),
('Operator', '*'),
('Number', '1'),
('Unknown', 'o'),
('Operator', '('),
('Function', 'log('),
('Number', '10'),
('Operator', ')'),
('Operator', ')'),
('Operator', ')'),
('Operator', ')')],
[('Unknown', 'a'),
('Operator', '*'),
('Function', 'sin('),
('Unknown', 'b'),
('Unknown', 'x'),
('Operator', '+'),
('Unknown', 'c'),
('Unknown', 'y'),
('Operator', '-'),
('Unknown', 'x'),
('Unknown', 'y'),
('Operator', '+'),
('Function', 'exp('),
('Unknown', 'u'),
('Unknown', 'v'),
('Operator', ')'),
('Operator', ')'),
('Function', 'asin('),
('Unknown', 'a'),
('Operator', '*'),
('Function', 'sin('),
('Unknown', 'a'),
('Unknown', 's'),
('Unknown', 'i'),
('Unknown', 'n'),
('Operator', '+'),
('Function', 'asin('),
('Unknown', 'c'),
('Unknown', 'y'),
('Operator', ')'),
('Operator', ')'),
('Operator', ')'),
('Operator', '+'),
('Function', 'tan('),
('Function', 'log10('),
('Number', '10'),
('Operator', ')'),
('Operator', ')')],
[('Unknown', 'p'),
('Unknown', 'r'),
('Unknown', 'i'),
('Unknown', 'c'),
('Function', 'exp('),
('Unknown', 'e'),
('Unknown', 'x'),
('Unknown', 'p'),
('Unknown', 'e'),
('Unknown', 'n'),
('Unknown', 's'),
('Unknown', 'e'),
('Unknown', 's'),
('Function', 'exp('),
('Unknown', 'e'),
('Unknown', 'x'),
('Unknown', 'p'),
('Unknown', 'e'),
('Unknown', 'n'),
('Unknown', 's'),
('Unknown', 'e'),
('Unknown', 's'),
('Operator', ')'),
('Operator', ')')],
[('Function', 'sin('),
('Unknown', 'x'),
('Operator', ')'),
('Operator', '^'),
('Number', '2'),
('Operator', '+'),
('Operator', '('),
('Function', 'cos('),
('Unknown', 'x'),
('Operator', ')'),
('Operator', ')'),
('Operator', '^'),
('Number', '2'),
('Operator', '-'),
('Number', '1')]
]
lex = Lexer(self.rules)
for eqn in equations:
out = [(g,v) for g,v in lex.scan(eqn)]
assert out == expectedOutput.pop(0)
pass
示例8: TestSimpleEquations
# 需要导入模块: from Lexer import Lexer [as 别名]
# 或者: from Lexer.Lexer import scan [as 别名]
def TestSimpleEquations(self):
""" Testing some simple equations:"""
equations = [
"2x+3y-5z-15",
"PaintA^2 + PaintB^3 - 100",
"x^2-y^2-6",
"aexp(bx) - 10"
]
expectedOutput = [
[('Number', '2'),
('Unknown', 'x'),
('Operator', '+'),
('Number', '3'),
('Unknown', 'y'),
('Operator', '-'),
('Number', '5'),
('Unknown', 'z'),
('Operator', '-'),
('Number', '15')],
[('Unknown', 'P'),
('Unknown', 'a'),
('Unknown', 'i'),
('Unknown', 'n'),
('Unknown', 't'),
('Unknown', 'A'),
('Operator', '^'),
('Number', '2'),
('Operator', '+'),
('Unknown', 'P'),
('Unknown', 'a'),
('Unknown', 'i'),
('Unknown', 'n'),
('Unknown', 't'),
('Unknown', 'B'),
('Operator', '^'),
('Number', '3'),
('Operator', '-'),
('Number', '100')],
[('Unknown', 'x'),
('Operator', '^'),
('Number', '2'),
('Operator', '-'),
('Unknown', 'y'),
('Operator', '^'),
('Number', '2'),
('Operator', '-'),
('Number', '6')],
[('Unknown', 'a'),
('Function', 'exp('),
('Unknown', 'b'),
('Unknown', 'x'),
('Operator', ')'),
('Operator', '-'),
('Number', '10')]
]
lex = Lexer(self.rules)
for eqn in equations:
out = [(g,v) for g,v in lex.scan(eqn)]
assert out == expectedOutput.pop(0)
pass
示例9: TestParser
# 需要导入模块: from Lexer import Lexer [as 别名]
# 或者: from Lexer.Lexer import scan [as 别名]
class TestParser():
@classmethod
def setupClass(self):
rules = {
"Function": r"(exp|log|log10|acos|asin|atan|cos|sin|tan|cosh|sinh|tanh)\(",
"Number": r"\d+(\.\d+)?",
"Operator": r"[-+()*^]",
}
self.lex = Lexer(rules)
pass
def TestKnownValues(self):
""" Tests some equations by comparing output to known results """
v = ['log']
c = {'exp':'1'}
tokens = [(group,value) for group, value in self.lex.scan('expexp(exp)explog10(exp(log))')]
par = Parser(tokens, c, v)
assert par.parseTokens() == '1*exp(1)*1*log10(exp(x[0]))'
v = ['x']
c = {}
tokens = [(group,value) for group, value in self.lex.scan('sin(x)^-2+(cos(x))^2-1')]
par = Parser(tokens, c, v)
assert par.parseTokens() == 'sin(x[0])**-2+(cos(x[0]))**2-1'
v = ['x','y','z']
c = {'a':'1.67e-5','b':'8'}
tokens = [(group,value) for group, value in self.lex.scan('aexp(bx+xy) + sin(z)log(by)*5')]
par = Parser(tokens, c, v)
assert par.parseTokens() == '1.67e-5*exp(8*x[0]+x[0]*x[1])+sin(x[2])*log(8*x[1])*5'
def TestEvalExpressions(self):
""" Tests that the expressions are evaluated successfully by eval() without
raising any exceptions
"""
var = ['x','y','u','v']
const = {'a':'0.25','b':'1','c':'2.6','asinc':'8'}
tokens = [(group,value) for group, value in self.lex.scan('a*sin(bx+cy-xy+exp(uv))asin(a*sin(asinc+asin(0.01cy)))+tan(log10(10))')]
par = Parser(tokens, const, var)
x = [1,1,1,1]
eval(par.parseTokens())
var = ['t','theta']
const = {'a':'1.8','k':'16','c':'1','azm':'1'}
tokens = [(group,value) for group, value in self.lex.scan('atan(aexp(-kt))+cktsin(azm*theta)')]
par = Parser(tokens, const, var)
x = [1,1]
eval(par.parseTokens())
@nose.tools.raises(MalformedEquationError)
def TestUnbalancedBrackets(self):
""" Tests that an equation with unbalanced brackets is flagged as Malformed Equation """
test_eqn = [
"21a*sin(bx+(cy+dz)",
"100log(ccos(xy+yz))+23(34)tan(exp(2x)",
"sin(x)^(-2x+3y+aexp(-kt)"
]
var = ['x','y','z','t']
const = {'a':'1','b':'1','c':'1','d':'1','k':'1'}
for eqn in test_eqn:
tokens = [(group,value) for group, value in self.lex.scan(eqn)]
par = Parser(tokens, const, var)
par.parseTokens()
def TestOperatorArrangement(self):
""" Tests that an equation with illegal operator arrangement is flagged as Malformed Equation """
test_eqn = [
"2x^-4*sin(exp())", #Zero arguments to exp()
"sin(ax+-by+cz)",
"dlog(x)*23sin(y*)",
"12x(y^3)+23(^0.5)tan(y)"
]
var = ['x','y','z']
const = {'a':'1','b':'1','c':'1','d':'1'}
for eqn in test_eqn:
tokens = [(group,value) for group, value in self.lex.scan(eqn)]
par = Parser(tokens, const, var)
nose.tools.assert_raises(MalformedEquationError, par.parseTokens)
def TestIdentifier(self):
""" Tests that the identifiers are properly parsed and ambiguous identifiers raise error """
const = {'b':'1','bat':'2'}
var = ['attr']
tokens = [(group,value) for group, value in self.lex.scan('battrexp(x)+log(bat^x)')]
par = Parser(tokens, const, var)
nose.tools.assert_raises(AmbiguousIdentifierError, par.parseTokens)
const = {'p':'3'}
var = ['rice','price']
tokens = [(group,value) for group, value in self.lex.scan('plog(rice)+price*rice')]
par = Parser(tokens, const, var)
par.parseTokens()
const = {'b':'1','a':'2', 'bet':'3'}
var = ['x','eta']
tokens = [(group,value) for group, value in self.lex.scan('2betalog(x)+bet*a+b*etaexp(eta)')]
par = Parser(tokens, const, var)
nose.tools.assert_raises(AmbiguousIdentifierError, par.parseTokens)