# HG changeset patch
# User Mike Hansen <mhansen@gmail.com>
# Date 1219967693 25200
# Node ID da10dca01a97328d6411756980d502321791e1c8
# Parent d66b9d4289394adb0760a2ee0b4eca78d8982fc0
Get coverage for sage/interfaces/sage0.py up to 100%
diff r d66b9d428939 r da10dca01a97 sage/interfaces/sage0.py
a

b


107  107  server_tmpdir = None, 
108  108  remote_cleaner = True, 
109  109  **kwds): 
 110  """ 
 111  EXAMPLES: 
 112  sage: sage0 == loads(dumps(sage0)) 
 113  True 
 114  """ 
110  115  if python: 
111  116  if server: 
112  117  command = "sage python u" 
… 
… 

154  159  return float(s) 
155  160  
156  161  def trait_names(self): 
 162  """ 
 163  EXAMPLES: 
 164  sage: t = sage0.trait_names() 
 165  sage: len(t) > 100 
 166  True 
 167  sage: 'gcd' in t 
 168  True 
 169  
 170  """ 
157  171  return eval(self.eval('globals().keys()')) 
158  172  
159  173  def quit(self, verbose=False): 
 174  """ 
 175  EXAMPLES: 
 176  sage: s = Sage() 
 177  sage: s.eval('2+2') 
 178  '4' 
 179  sage: s.quit() 
 180  """ 
160  181  import signal 
161  182  if not self._expect is None: 
162  183  pid = self._expect.pid 
… 
… 

189  210  pass 
190  211  self._expect = None 
191  212  
192   def _remote_tmpfile(self): 
193   try: 
194   return self.__remote_tmpfile 
195   except AttributeError: 
196   self.__remote_tmpfile = eval(self.eval('import sage.interfaces.expect as e; e.tmp')) 
197   return self.__remote_tmpfile 
198   
199  213  def __call__(self, x): 
 214  """ 
 215  EXAMPLES: 
 216  sage: a = sage0(4) 
 217  sage: a.parent() 
 218  Sage 
 219  sage: a is sage0(a) 
 220  True 
 221  """ 
200  222  if isinstance(x, SageElement) and x.parent() is self: 
201  223  return x 
202  224  if isinstance(x, str): 
… 
… 

211  233  return SageElement(self, 'loads(open("%s").read())'%self._remote_tmpfile()) 
212  234  
213  235  def __reduce__(self): 
 236  """ 
 237  EXAMPLES: 
 238  sage: sage0.__reduce__() 
 239  (<function reduce_load_Sage at 0x...>, ()) 
 240  
 241  """ 
214  242  return reduce_load_Sage, tuple([]) 
215  243  
216  244  def _quit_string(self): 
 245  """ 
 246  EXAMPLES: 
 247  sage: sage0._quit_string() 
 248  'from sage.misc.misc import delete_tmpfiles; delete_tmpfiles()' 
 249  """ 
217  250  return 'from sage.misc.misc import delete_tmpfiles; delete_tmpfiles()' 
218  251  
219  252  def preparse(self, x): 
 253  """ 
 254  Returns the preparsed version of the string s. 
 255  
 256  EXAMPLES: 
 257  sage: sage0.preparse('2+2') 
 258  'Integer(2)+Integer(2)' 
 259  
 260  """ 
220  261  return sage.misc.preparser.preparse(x) 
221  262  
222  263  def eval(self, line, strip=True): 
… 
… 

230  271  INPUT: 
231  272  line  input line of code 
232  273  strip  ignored 
 274  
 275  EXAMPLES: 
 276  sage: sage0.eval('2+2') 
 277  '4' 
233  278  """ 
234  279  if self._preparse: 
235  280  line = self.preparse(line) 
… 
… 

238  283  def set(self, var, value): 
239  284  """ 
240  285  Set the variable var to the given value. 
 286  
 287  EXAMPLES: 
 288  sage: sage0.set('x', '2') 
 289  sage: sage0.get('x') 
 290  '2' 
241  291  """ 
242  292  cmd = '%s=%s'%(var,value) 
243  293  out = self.eval(cmd) 
… 
… 

247  297  def get(self, var): 
248  298  """ 
249  299  Get the value of the variable var. 
 300  
 301  EXAMPLES: 
 302  sage: sage0.set('x', '2') 
 303  sage: sage0.get('x') 
 304  '2' 
250  305  """ 
251  306  return self.eval('print %s'%var) 
252  307  
253   #def clear(self, var): 
254   # """ 
255   # Clear the variable named var. 
256   # """ 
257   # self.eval('del %s'%var) 
 308  def clear(self, var): 
 309  """ 
 310  Clear the variable named var. 
 311  
 312  EXAMPLES: 
 313  sage: sage0.set('x', '2') 
 314  sage: sage0.get('x') 
 315  '2' 
 316  sage: sage0.clear('x') 
 317  sage: sage0.get('x') 
 318  "...NameError: name 'x' is not defined" 
 319  
 320  """ 
 321  self.eval('del %s'%var) 
258  322  
259  323  def _contains(self, v1, v2): 
 324  """ 
 325  EXAMPLES: 
 326  sage: sage0._contains('2', 'QQ') 
 327  'True' 
 328  """ 
260  329  return self.eval('%s in %s'%(v1,v2)) 
261  330  
262  331  def _is_true_string(self, t): 
 332  """ 
 333  EXAMPLES: 
 334  sage: sage0._is_true_string('True') 
 335  True 
 336  sage: sage0._is_true_string(True) 
 337  False 
 338  """ 
263  339  return t == "True" 
264  340  
265  341  def console(self): 
 342  """ 
 343  Spawn a new Sage commandline session. 
 344  
 345  EXAMPLES: 
 346  sage: sage0.console() #not tested 
 347   
 348   SAGE Version ..., Release Date: ...  
 349   Type notebook() for the GUI, and license() for information.  
 350   
 351  ... 
 352  """ 
266  353  sage0_console() 
267  354  
268  355  def version(self): 
269   return eval(sage0_version()) 
 356  """ 
 357  EXAMPLES: 
 358  sage: sage0.version() 
 359  'SAGE Version ..., Release Date: ...' 
 360  sage: sage0.version() == version() 
 361  True 
 362  """ 
 363  return sage0_version() 
270  364  
271  365  def _object_class(self): 
 366  """ 
 367  EXAMPLES: 
 368  sage: sage0._object_class() 
 369  <class 'sage.interfaces.sage0.SageElement'> 
 370  """ 
272  371  return SageElement 
273  372  
274  373  def new(self, x): 
 374  """ 
 375  EXAMPLES: 
 376  sage: sage0.new(2) 
 377  2 
 378  sage: _.parent() 
 379  Sage 
 380  
 381  """ 
275  382  return SageElement(self, x) 
276  383  
277  384  class SageElement(ExpectElement): 
278  385  def __getattr__(self, attrname): 
 386  """ 
 387  EXAMPLES: 
 388  sage: m = sage0(4) 
 389  sage: four_gcd = m.gcd 
 390  sage: type(four_gcd) 
 391  <class 'sage.interfaces.sage0.SageFunction'> 
 392  """ 
279  393  self._check_valid() 
280  394  return SageFunction(self, attrname) 
281  395  
… 
… 

300  414  
301  415  class SageFunction(FunctionElement): 
302  416  def __call__(self, *args, **kwds): 
 417  """ 
 418  EXAMPLES: 
 419  sage: four_gcd = sage0(4).gcd 
 420  sage: four_gcd(6) 
 421  2 
 422  """ 
303  423  P = self._obj.parent() 
304  424  args = [P(x) for x in args] 
305  425  args = ','.join([x.name() for x in args]) 
… 
… 

317  437  return z 
318  438  
319  439  def __repr__(self): 
320   return str(self.eval('%s.%s'%(self._obj._name, self._name))) 
 440  """ 
 441  EXAMPLES: 
 442  sage: sage0(4).gcd 
 443  <builtin method gcd of sage.rings.integer.Integer object at 0x...> 
 444  
 445  """ 
 446  
 447  return str(self._obj.parent().eval('%s.%s'%(self._obj._name, self._name))) 
321  448  
322  449  
323  450  
324  451  sage0 = Sage() 
325  452  
326  453  def reduce_load_Sage(): 
 454  """ 
 455  EXAMPLES: 
 456  sage: from sage.interfaces.sage0 import reduce_load_Sage 
 457  sage: reduce_load_Sage() 
 458  Sage 
 459  """ 
327  460  return sage0 
328  461  
329  462  def reduce_load_element(s): 
330   return sage0('loads(%s)'%s) 
 463  """ 
 464  EXAMPLES: 
 465  sage: from sage.interfaces.sage0 import reduce_load_element 
 466  sage: s = dumps(1/2) 
 467  sage: half = reduce_load_element(s); half 
 468  1/2 
 469  sage: half.parent() 
 470  Sage 
 471  """ 
 472  import base64 
 473  s = base64.b32encode(s) 
 474  sage0.eval('import base64') 
 475  return sage0('loads(base64.b32decode("%s"))'%s) 
331  476  
332  477  
333  478  import os 
334  479  def sage0_console(): 
 480  """ 
 481  Spawn a new Sage commandline session. 
 482  
 483  EXAMPLES: 
 484  sage: sage0_console() #not tested 
 485   
 486   SAGE Version ..., Release Date: ...  
 487   Type notebook() for the GUI, and license() for information.  
 488   
 489  ... 
 490  """ 
335  491  os.system('sage') 
336  492  
337  493  def sage0_version(): 
338   return sage0('version()') 
 494  """ 
 495  EXAMPLES: 
 496  sage: from sage.interfaces.sage0 import sage0_version 
 497  sage: sage0_version() == version() 
 498  True 
 499  """ 
 500  return str(sage0('version()')) 
339  501  
340  502  #def irun(filename): 
341  503  # """ 