-
Notifications
You must be signed in to change notification settings - Fork 1
Sourcery refactored master branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -212,13 +212,11 @@ def strResult(self, format='', exponent=0): | |
| """ | ||
| if self.isSymbolic(): | ||
| return self.strSymbolic() | ||
| f = format if format else self.format | ||
| f = format or self.format | ||
| e = exponent if exponent != 0 else self.exponent | ||
| result = self.value | ||
| if e == 0: | ||
| if result < 0.: | ||
| return r'\left( %s \right)' % f % result | ||
| return '%s' % f % result | ||
| return r'\left( %s \right)' % f % result if result < 0. else f'{f}' % result | ||
|
Comment on lines
-215
to
+219
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| val = self.value*math.pow(10, -e) | ||
| if self.value < 0.: | ||
| return r'\left( %s %s \right)' % (f % val, '\cdot 10^{%d}' % e) | ||
|
|
@@ -293,8 +291,8 @@ def __str__(self): | |
| F | ||
| """ | ||
| if self.isSymbolic(): | ||
| return '%s' % (self.name) | ||
| return '%s = %s' % (self.name, self.strResultWithUnit()) | ||
| return f'{self.name}' | ||
| return f'{self.name} = {self.strResultWithUnit()}' | ||
|
Comment on lines
-296
to
+295
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def toLaTeXVariable(self, name, what='float', command='def'): | ||
| r"""Returns latex expression converting receiver to LaTeX variable using \def, \newcommand, or \renewcommand LaTeX command | ||
|
|
@@ -317,10 +315,20 @@ def toLaTeXVariable(self, name, what='float', command='def'): | |
| """ | ||
| what = what.lower() | ||
| whats = ('float', 'str', 'valunit', 'all', 'subst') | ||
| if not what in whats: | ||
| raise LaTeXExpressionError('%s not in %s' % (what, whats)) | ||
| val = self.value if what == 'float' else self.strResult() if what == 'str' else self.strResultWithUnit( | ||
| ) if what == 'valunit' else str(self) if what == 'all' or what == 'subst' else None | ||
| if what not in whats: | ||
| raise LaTeXExpressionError(f'{what} not in {whats}') | ||
| val = ( | ||
| self.value | ||
| if what == 'float' | ||
| else self.strResult() | ||
| if what == 'str' | ||
| else self.strResultWithUnit() | ||
| if what == 'valunit' | ||
| else str(self) | ||
| if what in ['all', 'subst'] | ||
| else None | ||
| ) | ||
|
|
||
|
Comment on lines
-320
to
+331
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return toLaTeXVariable(name, val, command) | ||
|
|
||
| def toLaTeXVariableFloat(self, name, command='def'): | ||
|
|
@@ -452,9 +460,11 @@ class Operation(object): | |
| exponent = 0 # see :py:attr:`Variable.exponent` | ||
|
|
||
| def __init__(self, type, *args): | ||
| if not type in _supportedOperations: | ||
| raise LaTeXExpressionError('operation %s not in supported operations %s' % ( | ||
| type, str(_supportedOperations))) | ||
| if type not in _supportedOperations: | ||
| raise LaTeXExpressionError( | ||
| f'operation {type} not in supported operations {str(_supportedOperations)}' | ||
| ) | ||
|
|
||
|
Comment on lines
-455
to
+467
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| self.type = type | ||
| self.args = self.__checkArgs(args) | ||
| self.format = '%g' | ||
|
|
@@ -471,7 +481,9 @@ def __checkArgs(self, args): | |
| ret.append(Variable('%g' % a, a, format='%g')) | ||
| else: | ||
| raise TypeError( | ||
| "wrong argunemt type (%s) in Operation constructor" % a.__class__.__name__) | ||
| f"wrong argunemt type ({a.__class__.__name__}) in Operation constructor" | ||
| ) | ||
|
|
||
|
Comment on lines
+474
to
+486
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return ret | ||
|
|
||
| def __str(self, what): | ||
|
|
@@ -495,11 +507,11 @@ def __str(self, what): | |
| v0 = getattr(a[0], what)() | ||
| v1 = getattr(a[1], what)() | ||
| if t == _SUB: | ||
| return r'%s - %s' % (v0, v1) | ||
| return f'{v0} - {v1}' | ||
| if t == _DIV: | ||
| return r'\frac{ %s }{ %s }' % (v0, v1) | ||
| if t == _DIV2: | ||
| return r'%s / %s' % (v0, v1) | ||
| return f'{v0} / {v1}' | ||
|
Comment on lines
-498
to
+514
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if t == _POW: | ||
| return r'{ %s }^{ %s }' % (v0, v1) | ||
| if t == _ROOT: | ||
|
|
@@ -520,7 +532,7 @@ def __str(self, what): | |
| if t == _ABS: | ||
| return r'\left| %s \right|' % v | ||
| if t == _SQR: | ||
| return r'%s^2' % v | ||
| return f'{v}^2' | ||
| if t == _SQRT: | ||
| return r'\sqrt{ %s }' % v | ||
| if t == _SIN: | ||
|
|
@@ -552,8 +564,9 @@ def __str(self, what): | |
| if _DEBUG: | ||
| print(t) | ||
| raise LaTeXExpressionError(t) | ||
| raise LaTeXExpressionError('operation %s not in supported operations %s' % ( | ||
| self.type, str(_supportedOperations))) | ||
| raise LaTeXExpressionError( | ||
| f'operation {self.type} not in supported operations {str(_supportedOperations)}' | ||
| ) | ||
|
|
||
| def strSymbolic(self): | ||
| r"""Returns string of symbolic representation of receiver | ||
|
|
@@ -606,12 +619,10 @@ def strResult(self, format='', exponent=0): | |
| if self.isSymbolic(): | ||
| return self.strSymbolic() | ||
| r = float(self) | ||
| f = format if format else self.format | ||
| f = format or self.format | ||
| e = exponent if exponent != 0 else self.exponent | ||
| if e == 0: | ||
| if r < 0.: | ||
| return r'\left( %s \right)' % f % r | ||
| return '%s' % f % r | ||
| return r'\left( %s \right)' % f % r if r < 0. else f'{f}' % r | ||
|
Comment on lines
-609
to
+625
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| val = r*math.pow(10, -e) | ||
| if r < 0.: | ||
| return r'\left( %s %s \right)' % (f % val, '\cdot 10^{%d}' % e) | ||
|
|
@@ -707,8 +718,9 @@ def result(self): | |
| if _DEBUG: | ||
| print(t) | ||
| raise LaTeXExpressionError(t) | ||
| raise LaTeXExpressionError('operation %s not in supported operations %s' % ( | ||
| self.type, str(_supportedOperations))) | ||
| raise LaTeXExpressionError( | ||
| f'operation {self.type} not in supported operations {str(_supportedOperations)}' | ||
| ) | ||
|
Comment on lines
-710
to
+723
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def __float__(self): | ||
| """Returns numeric result of the receiver | ||
|
|
@@ -758,7 +770,7 @@ def __str__(self): | |
| """ | ||
| if self.isSymbolic(): | ||
| return self.strSymbolic() | ||
| return '%s = %s' % (self.strSymbolic(), self.strSubstituted()) | ||
| return f'{self.strSymbolic()} = {self.strSubstituted()}' | ||
|
Comment on lines
-761
to
+773
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def toVariable(self, newName='', **kw): | ||
| """Returns new Variable instance with attributes copied from receiver | ||
|
|
@@ -1129,12 +1141,10 @@ def strResult(self, format='', exponent=0): | |
| if self.isSymbolic(): | ||
| return self.operation.strSubstituted() | ||
| r = float(self) | ||
| f = format if format else self.format | ||
| f = format or self.format | ||
| e = exponent if exponent != 0 else self.exponent | ||
| if e == 0: | ||
| if r < 0: | ||
| return r'\left(%s\right)' % f % r | ||
| return '%s' % f % r | ||
| return r'\left(%s\right)' % f % r if r < 0 else f'{f}' % r | ||
|
Comment on lines
-1132
to
+1147
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| val = float(self)*math.pow(10, -e) | ||
| if r < 0: | ||
| return r'\left( %s %s \right)' % (f % val, '\cdot 10^{%d}' % e) | ||
|
|
@@ -1229,8 +1239,8 @@ def __str__(self): | |
| E_2 = \frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } } = 2.14906 \ \mathrm{mm} | ||
| """ | ||
| if self.isSymbolic(): | ||
| return '%s = %s' % (self.name, self.operation) | ||
| return '%s = %s = %s' % (self.name, self.operation, self.strResultWithUnit()) | ||
| return f'{self.name} = {self.operation}' | ||
| return f'{self.name} = {self.operation} = {self.strResultWithUnit()}' | ||
|
Comment on lines
-1232
to
+1243
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def toLaTeXVariable(self, name, what='float', command='def'): | ||
| r"""Returns latex expression converting receiver to LaTeX variable using \def, \newcommand, or \renewcommand LaTeX command | ||
|
|
@@ -1260,8 +1270,8 @@ def toLaTeXVariable(self, name, what='float', command='def'): | |
| """ | ||
| what = what.lower() | ||
| whats = ('float', 'str', 'valunit', 'symb', 'subst', 'all') | ||
| if not what in whats: | ||
| raise LaTeXExpressionError('%s not in %s' % (what, whats)) | ||
| if what not in whats: | ||
| raise LaTeXExpressionError(f'{what} not in {whats}') | ||
|
Comment on lines
-1263
to
+1274
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| val = float(self) if what == 'float' else self.strResult() if what == 'str' else self.strResultWithUnit() if what == 'valunit' else self.strSymbolic( | ||
| ) if what == 'symb' else self.strSubstituted() if what == 'subst' else str(self) if what == 'all' else None | ||
| return toLaTeXVariable(name, val, command) | ||
|
|
@@ -1386,7 +1396,7 @@ def toLaTeXVariable(name, what, command='def'): | |
| """ | ||
| if command == 'def': | ||
| return r'\def\%s{%s}' % (name, what) | ||
| elif command == 'newcommand' or command == 'renewcommand': | ||
| elif command in ['newcommand', 'renewcommand']: | ||
|
Comment on lines
-1389
to
+1399
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return r'\%s{\%s}{%s}' % (command, name, what) | ||
| else: | ||
| raise LaTeXExpressionError( | ||
|
|
@@ -1436,9 +1446,9 @@ def toLaTeXVariable(name, what, command='def'): | |
| print(v8) | ||
|
|
||
| v3 = Variable('F', 4.34, 'kN', exponent=-2) | ||
| print(str(v3)) | ||
| print(v3) | ||
| v8 = Variable('F', None, 'kN') | ||
| print(str(v8)) | ||
| print(v8) | ||
|
Comment on lines
-1439
to
+1451
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
|
|
||
| v1 = Variable('a_{22}', 3.45, 'mm') | ||
| print(v1.strSymbolic()) | ||
|
|
@@ -1488,7 +1498,7 @@ def toLaTeXVariable(name, what, command='def'): | |
| v2 = Variable('F', 5.876934835, 'kN') | ||
| v3 = Variable('F', 4.34, 'kN', exponent=-2) | ||
| o3 = (v1+v2)/v3 | ||
| print(str(o3)) | ||
| print(o3) | ||
|
|
||
| v1 = Variable('a_{22}', 3.45, 'mm') | ||
| v2 = Variable('F', 5.876934835, 'kN') | ||
|
|
@@ -1548,7 +1558,7 @@ def toLaTeXVariable(name, what, command='def'): | |
| v2 = Variable('F', 5.876934835, 'kN') | ||
| v3 = Variable('F', 4.34, 'kN', exponent=-2) | ||
| e2 = Expression('E_2', (v1+v2)/v3, 'mm') | ||
| print(str(e2)) | ||
| print(e2) | ||
|
|
||
| v1 = Variable('a_{22}', 3.45, 'mm') | ||
| v2 = Variable('F', 5.876934835, 'kN') | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,7 +78,7 @@ def _operation2sympy(arg, varMap=None, substituteFloats=True): | |
| if isinstance(arg, latexexpr.Expression): | ||
| return _operation2sympy(arg.operation, varMap, sf) | ||
| if not isinstance(arg, latexexpr.Operation): | ||
| raise TypeError("TODO " + str(type(arg)) + str(arg)) | ||
| raise TypeError(f"TODO {str(type(arg))}{str(arg)}") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| t = arg.type | ||
| if t in latexexpr._supportedOperationsN: | ||
| if t == latexexpr._ADD: | ||
|
|
@@ -94,7 +94,7 @@ def _operation2sympy(arg, varMap=None, substituteFloats=True): | |
| if t == latexexpr._SUB: | ||
| sympyOp, args = sympy.Add, (_o2s( | ||
| a[0], varMap, sf), sympy.Mul(-1, _o2s(a[1], varMap, sf))) | ||
| elif t == latexexpr._DIV or t == latexexpr._DIV2: | ||
| elif t in [latexexpr._DIV, latexexpr._DIV2]: | ||
| sympyOp, args = sympy.Mul, (_o2s( | ||
| a[0], varMap, sf), sympy.power.Pow(_o2s(a[1], varMap, sf), -1)) | ||
| elif t == latexexpr._POW: | ||
|
|
@@ -172,11 +172,6 @@ def _sympy2operation(sympyExpr, varMap): | |
| return -args[1] | ||
| if isinstance(args[1], latexexpr.Variable) and args[1].name == '-1': | ||
| return -args[0] | ||
| elif len(args) == 2 and isinstance(args[1], latexexpr.Operation) and args[1].type == latexexpr._DIV: | ||
| if args[1].args[0].value == 1.: | ||
| return args[0] / args[1].args[1] | ||
| if all(a.type == latexexpr._LN for a in (args[0], args[1].args[0])): | ||
| return latexexpr._LOG(args[0], args[0].args[1]) | ||
|
Comment on lines
-175
to
-179
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| for i, a in enumerate(args): | ||
| t = a.type if isinstance(a, latexexpr.Operation) else a.operation.type if isinstance( | ||
| a, latexexpr.Expression) else None | ||
|
|
@@ -273,8 +268,7 @@ def simplify(arg, substituteFloats=False, **kw): | |
| s, lVars = _operation2sympy(arg, substituteFloats=substituteFloats) | ||
| s = sympy.simplify(s, **kw) | ||
| return _sympy2operation(s, lVars) | ||
| raise TypeError("Unsupported type (%s) for simplify" % | ||
| (arg.__class__.__name__)) | ||
| raise TypeError(f"Unsupported type ({arg.__class__.__name__}) for simplify") | ||
|
Comment on lines
-276
to
+271
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| latexexpr.Expression.simplify = lambda self, substituteFloats=False, **kw: _setOperation( | ||
|
|
@@ -316,8 +310,7 @@ def expand(arg, substituteFloats=False, **kw): | |
| s, lVars = _operation2sympy(arg, substituteFloats=substituteFloats) | ||
| s = sympy.expand(s, **kw) | ||
| return _sympy2operation(s, lVars) | ||
| raise TypeError("Unsupported type (%s) for expand" % | ||
| (arg.__class__.__name__)) | ||
| raise TypeError(f"Unsupported type ({arg.__class__.__name__}) for expand") | ||
|
Comment on lines
-319
to
+313
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| latexexpr.Expression.expand = lambda self, substituteFloats=False, **kw: _setOperation( | ||
|
|
@@ -358,8 +351,7 @@ def factor(arg, substituteFloats=False, **kw): | |
| s, lVars = _operation2sympy(arg, substituteFloats=substituteFloats) | ||
| s = sympy.factor(s, **kw) | ||
| return _sympy2operation(s, lVars) | ||
| raise TypeError("Unsupported type (%s) for factor" % | ||
| (arg.__class__.__name__)) | ||
| raise TypeError(f"Unsupported type ({arg.__class__.__name__}) for factor") | ||
|
Comment on lines
-361
to
+354
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| latexexpr.Expression.factor = lambda self, substituteFloats=False, **kw: _setOperation( | ||
|
|
@@ -402,8 +394,7 @@ def collect(arg, syms, substituteFloats=False, **kw): | |
| sympy.Symbol(s.name) for s in syms] | ||
| s = sympy.collect(s, syms, **kw) | ||
| return _sympy2operation(s, lVars) | ||
| raise TypeError("Unsupported type (%s) for collect" % | ||
| (arg.__class__.__name__)) | ||
| raise TypeError(f"Unsupported type ({arg.__class__.__name__}) for collect") | ||
|
Comment on lines
-405
to
+397
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| latexexpr.Expression.collect = lambda self, syms, substituteFloats=False, **kw: _setOperation( | ||
|
|
@@ -447,8 +438,7 @@ def cancel(arg, substituteFloats=False, **kw): | |
| s, lVars = _operation2sympy(arg, substituteFloats=substituteFloats) | ||
| s = sympy.cancel(s, **kw) | ||
| return _sympy2operation(s, lVars) | ||
| raise TypeError("Unsupported type (%s) for cancel" % | ||
| (arg.__class__.__name__)) | ||
| raise TypeError(f"Unsupported type ({arg.__class__.__name__}) for cancel") | ||
|
Comment on lines
-450
to
+441
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| latexexpr.Expression.cancel = lambda self, substituteFloats=False, **kw: _setOperation( | ||
|
|
@@ -484,8 +474,7 @@ def apart(arg, substituteFloats=False, **kw): | |
| s, lVars = _operation2sympy(arg, substituteFloats=substituteFloats) | ||
| s = sympy.apart(s, **kw) | ||
| return _sympy2operation(s, lVars) | ||
| raise TypeError("Unsupported type (%s) for apart" % | ||
| (arg.__class__.__name__)) | ||
| raise TypeError(f"Unsupported type ({arg.__class__.__name__}) for apart") | ||
|
Comment on lines
-487
to
+477
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| latexexpr.Expression.apart = lambda self, substituteFloats=False, **kw: _setOperation( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
printExprrefactored with the following changes:use-fstring-for-concatenation)