Skip to content

Commit

Permalink
gh-36745: pep8 for axiom and sympy interfaces
Browse files Browse the repository at this point in the history
    
pep8 cleanup for the 2 modified files

### 📝 Checklist

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
    
URL: #36745
Reported by: Frédéric Chapoton
Reviewer(s): Kwankyu Lee, Matthias Köppe
  • Loading branch information
Release Manager committed Dec 9, 2023
2 parents dea7789 + 8d8b28d commit ca0624a
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 61 deletions.
75 changes: 37 additions & 38 deletions src/sage/interfaces/axiom.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,7 @@ def _commands(self):
i = s.find(start)
end = "To get more information about"
j = s.find(end)
s = s[i+len(start):j].split()
return s
return s[i + len(start):j].split()

def _tab_completion(self, verbose=True, use_disk_cache=True):
"""
Expand Down Expand Up @@ -359,15 +358,15 @@ def _tab_completion(self, verbose=True, use_disk_cache=True):
print("To force rebuild later, delete %s." % self._COMMANDS_CACHE)
v = self._commands()

#Process we now need process the commands to strip out things which
#are not valid Python identifiers.
# Process we now need process the commands to strip out things which
# are not valid Python identifiers.
valid = re.compile('[^a-zA-Z0-9_]+')
names = [x for x in v if valid.search(x) is None]

#Change everything that ends with ? to _q and
#everything that ends with ! to _e
names += [x[:-1]+"_q" for x in v if x.endswith("?")]
names += [x[:-1]+"_e" for x in v if x.endswith("!")]
# Change everything that ends with ? to _q and
# everything that ends with ! to _e
names += [x[:-1] + "_q" for x in v if x.endswith("?")]
names += [x[:-1] + "_e" for x in v if x.endswith("!")]

self.__tab_completion = names
if len(v) > 200:
Expand Down Expand Up @@ -433,7 +432,7 @@ def _eval_line(self, line, reformat=True, allow_use_file=False,
if self._expect is None:
self._start()
if allow_use_file and self.__eval_using_file_cutoff and \
len(line) > self.__eval_using_file_cutoff:
len(line) > self.__eval_using_file_cutoff:
return self._eval_line_using_file(line)
try:
E = self._expect
Expand Down Expand Up @@ -468,7 +467,7 @@ def _eval_line(self, line, reformat=True, allow_use_file=False,
line = line.rstrip()
if line[:4] == ' (':
i = line.find('(')
i += line[i:].find(')')+1
i += line[i:].find(')') + 1
if line[i:] == "":
i = 0
outs = outs[1:]
Expand Down Expand Up @@ -603,14 +602,13 @@ def _richcmp_(self, other, op):
sage: f = axiom('sin(x)'); g = axiom('cos(x)') #optional - axiom
sage: f == g #optional - axiom
False
"""
P = self.parent()
if 'true' in P.eval("(%s = %s) :: Boolean" % (self.name(),other.name())):
if 'true' in P.eval("(%s = %s) :: Boolean" % (self.name(), other.name())):
return rich_to_bool(op, 0)
elif 'true' in P.eval("(%s < %s) :: Boolean" % (self.name(), other.name())):
return rich_to_bool(op, -1)
elif 'true' in P.eval("(%s > %s) :: Boolean" % (self.name(),other.name())):
elif 'true' in P.eval("(%s > %s) :: Boolean" % (self.name(), other.name())):
return rich_to_bool(op, 1)

return NotImplemented
Expand Down Expand Up @@ -642,7 +640,7 @@ def __len__(self):
P = self._check_valid()
s = P.eval('# %s ' % self.name())
i = s.rfind('Type')
return int(s[:i-1])
return int(s[:i - 1])

def __getitem__(self, n):
r"""
Expand Down Expand Up @@ -716,12 +714,12 @@ def _latex_(self):
i = s.find('$$')
j = s.rfind('$$')
s = s[i + 2:j]
s = multiple_replace({'\r':'', '\n':' ',
' \\sp ':'^',
'\\arcsin ':'\\sin^{-1} ',
'\\arccos ':'\\cos^{-1} ',
'\\arctan ':'\\tan^{-1} '},
re.sub(r'\\leqno\(.*?\)','',s)) # no eq number!
s = multiple_replace({'\r': '', '\n': ' ',
' \\sp ': '^',
'\\arcsin ': '\\sin^{-1} ',
'\\arccos ': '\\cos^{-1} ',
'\\arctan ': '\\tan^{-1} '},
re.sub(r'\\leqno\(.*?\)', '', s)) # no eq number!
return s

def as_type(self, type):
Expand Down Expand Up @@ -760,16 +758,13 @@ def unparsed_input_form(self):
s = P.eval('unparse(%s::InputForm)' % self._name)
if 'translation error' in s or 'Cannot convert' in s:
raise NotImplementedError
s = multiple_replace({'\r\n':'', # fix stupid Fortran-ish
'DSIN(':'sin(',
'DCOS(':'cos(',
'DTAN(':'tan(',
'DSINH(':'sinh('}, s)
r = re.search(r'"(.*)"',s)
if r:
return r.groups(0)[0]
else:
return s
s = multiple_replace({'\r\n': '', # fix stupid Fortran-ish
'DSIN(': 'sin(',
'DCOS(': 'cos(',
'DTAN(': 'tan(',
'DSINH(': 'sinh('}, s)
r = re.search(r'"(.*)"', s)
return r.groups(0)[0] if r else s

def _sage_(self):
"""
Expand Down Expand Up @@ -843,8 +838,8 @@ def _sage_(self):
from sage.rings.integer_ring import ZZ
prec = max(self.mantissa().length()._sage_(), 53)
R = RealField(prec)
x,e,b = self.unparsed_input_form().lstrip('float(').rstrip(')').split(',')
return R(ZZ(x)*ZZ(b)**ZZ(e))
x, e, b = self.unparsed_input_form().lstrip('float(').rstrip(')').split(',')
return R(ZZ(x) * ZZ(b)**ZZ(e))
elif type == "DoubleFloat":
from sage.rings.real_double import RDF
return RDF(repr(self))
Expand All @@ -858,16 +853,18 @@ def _sage_(self):
R = PolynomialRing(base_ring, vars)
return R(self.unparsed_input_form())
elif type.startswith('Fraction'):
return self.numer().sage()/self.denom().sage()
return self.numer().sage() / self.denom().sage()

#If all else fails, try using the unparsed input form
# If all else fails, try using the unparsed input form
try:
import sage.misc.sage_eval
vars = sage.symbolic.ring.var(str(self.variables())[1:-1])
if isinstance(vars,tuple):
return sage.misc.sage_eval.sage_eval(self.unparsed_input_form(), locals={str(x):x for x in vars})
if isinstance(vars, tuple):
return sage.misc.sage_eval.sage_eval(self.unparsed_input_form(),
locals={str(x): x for x in vars})
else:
return sage.misc.sage_eval.sage_eval(self.unparsed_input_form(), locals={str(vars):vars})
return sage.misc.sage_eval.sage_eval(self.unparsed_input_form(),
locals={str(vars): vars})
except Exception:
raise NotImplementedError

Expand Down Expand Up @@ -970,9 +967,10 @@ def is_AxiomElement(x):
return isinstance(x, AxiomElement)


#Instances
# Instances
axiom = Axiom(name='axiom')


def reduce_load_Axiom():
"""
Returns the Axiom interface object defined in
Expand All @@ -986,6 +984,7 @@ def reduce_load_Axiom():
"""
return axiom


def axiom_console():
"""
Spawn a new Axiom command-line session.
Expand Down

0 comments on commit ca0624a

Please sign in to comment.