def db_save(x, name=None): r""" Save x to the Sage database. The database directory is ``$HOME/.sage/db``. """ try: x.db(name) except AttributeError: save(x, '%s/%s'%(SAGE_DB,name))
def _subprocess(self, f, dir, args, kwds={}): """ Setup and run evaluation of ``f(*args, **kwds)``, storing the result in the given directory ``dir``. This method is called by each forked subprocess. INPUT: - ``f`` -- a function - ``dir`` -- name of a directory - ``args`` -- a tuple with positional arguments for ``f`` - ``kwds`` -- (optional) a dict with keyword arguments for ``f`` TESTS: The method ``_subprocess`` is really meant to be run only in a subprocess. It doesn't print not return anything, the output is saved in pickles. It redirects stdout, so we save and later restore stdout in order not to break the doctester:: sage: saved_stdout = sys.stdout sage: F = sage.parallel.use_fork.p_iter_fork(2,3) sage: F._subprocess(operator.add, tmp_dir(), (1, 2)) sage: sys.stdout = saved_stdout """ import imp, os, sys from sage.structure.sage_object import save # Make it so all stdout is sent to a file so it can # be displayed. out = os.path.join(dir, '%s.out'%os.getpid()) sys.stdout = open(out, 'w') # Run some commands to tell Sage that its # pid has changed (forcing a reload of # misc). import sage.misc.misc imp.reload(sage.misc.misc) # The pexpect interfaces (and objects defined in them) are # not valid. if self.reset_interfaces: sage.interfaces.quit.invalidate_all() # Now evaluate the function f. value = f(*args, **kwds) # And save the result to disk. sobj = os.path.join(dir, '%s.sobj'%os.getpid()) save(value, sobj, compress=False)
def _subprocess(self, f, dir, args, kwds={}): """ Setup and run evaluation of ``f(*args, **kwds)``, storing the result in the given directory ``dir``. This method is called by each forked subprocess. INPUT: - ``f`` -- a function - ``dir`` -- name of a directory - ``args`` -- a tuple with positional arguments for ``f`` - ``kwds`` -- (optional) a dict with keyword arguments for ``f`` TESTS: The method ``_subprocess`` is really meant to be run only in a subprocess. It doesn't print not return anything, the output is saved in pickles. It redirects stdout, so we save and later restore stdout in order not to break the doctester:: sage: saved_stdout = sys.stdout sage: F = sage.parallel.use_fork.p_iter_fork(2,3) sage: F._subprocess(operator.add, tmp_dir(), (1, 2)) sage: sys.stdout = saved_stdout """ import imp, os, sys from sage.structure.sage_object import save # Make it so all stdout is sent to a file so it can # be displayed. out = os.path.join(dir, '%s.out' % os.getpid()) sys.stdout = open(out, 'w') # Run some commands to tell Sage that its # pid has changed (forcing a reload of # misc). import sage.misc.misc imp.reload(sage.misc.misc) # The pexpect interfaces (and objects defined in them) are # not valid. if self.reset_interfaces: sage.interfaces.quit.invalidate_all() # Now evaluate the function f. value = f(*args, **kwds) # And save the result to disk. sobj = os.path.join(dir, '%s.sobj' % os.getpid()) save(value, sobj, compress=False)
def _subprocess(self, f, dir, x): """ Setup and run evaluation of ``f(*x[0], **x[1])``, storing the result in the given directory ``dir``. This method is called by each forked subprocess. INPUT: - ``f`` -- a function - ``dir`` -- name of a directory - ``x`` -- 2-tuple, with args and kwds EXAMPLES: We have only this indirect test, since a direct test would terminate the Sage session. sage: F = sage.parallel.use_fork.p_iter_fork(2,3) sage: sorted(list( F( (lambda x: x^2), [([10],{}), ([20],{})]))) [(([10], {}), 100), (([20], {}), 400)] """ import imp, os, sys from sage.structure.sage_object import save try: # Make it so all stdout is sent to a file so it can # be displayed. out = os.path.join(dir, '%s.out' % os.getpid()) sys.stdout = open(out, 'w') # Run some commands to tell Sage that its # pid has changed (forcing a reload of # misc). import sage.misc.misc imp.reload(sage.misc.misc) # The pexpect interfaces (and objects defined in them) are # not valid. if self.reset_interfaces: sage.interfaces.quit.invalidate_all() # Now evaluate the function f. value = f(*x[0], **x[1]) # And save the result to disk. sobj = os.path.join(dir, '%s.sobj' % os.getpid()) save(value, sobj, compress=False) except Exception as msg: # Important to print this, so it is seen by the caller. print msg finally: sys.stdout.flush() os._exit(0)
def _subprocess(self, f, dir, x): """ Setup and run evaluation of ``f(*x[0], **x[1])``, storing the result in the given directory ``dir``. This method is called by each forked subprocess. INPUT: - ``f`` -- a function - ``dir`` -- name of a directory - ``x`` -- 2-tuple, with args and kwds EXAMPLES: We have only this indirect test, since a direct test would terminate the Sage session. sage: F = sage.parallel.use_fork.p_iter_fork(2,3) sage: sorted(list( F( (lambda x: x^2), [([10],{}), ([20],{})]))) [(([10], {}), 100), (([20], {}), 400)] """ import imp, os, sys from sage.structure.sage_object import save try: # Make it so all stdout is sent to a file so it can # be displayed. out = os.path.join(dir, '%s.out'%os.getpid()) sys.stdout = open(out, 'w') # Run some commands to tell Sage that its # pid has changed (forcing a reload of # misc). import sage.misc.misc imp.reload(sage.misc.misc) # The pexpect interfaces (and objects defined in them) are # not valid. if self.reset_interfaces: sage.interfaces.quit.invalidate_all() # Now evaluate the function f. value = f(*x[0], **x[1]) # And save the result to disk. sobj = os.path.join(dir, '%s.sobj'%os.getpid()) save(value, sobj, compress=False) except Exception as msg: # Important to print this, so it is seen by the caller. print msg finally: sys.stdout.flush() os._exit(0)
def _init(self, path): """ Create the database from scratch from the PARI files on John Jones's web page, downloaded (e.g., via wget) to a local directory, which is specified as path above. INPUT: - ``path`` - (default works on William Stein install.) path must be the path to Jones's Number_Fields directory http://hobbes.la.asu.edu/Number_Fields These files should have been downloaded using wget. EXAMPLE: This is how to create the database from scratch, assuming that the number fields are in the default directory above: From a cold start of Sage:: sage: J = JonesDatabase() sage: J._init() # not tested ... This takes about 5 seconds. """ from sage.misc.misc import sage_makedirs n = 0 x = PolynomialRing(RationalField(), "x").gen() self.root = {} self.root[tuple([])] = [x - 1] if not os.path.exists(path): raise IOError("Path %s does not exist." % path) for X in os.listdir(path): if X[-4:] == "solo": Z = path + "/" + X print(X) for Y in os.listdir(Z): if Y[-3:] == ".gp": self._load(Z, Y) sage_makedirs(JONESDATA) save(self.root, JONESDATA + "/jones.sobj")
def _init(self, path): """ Create the database from scratch from the PARI files on John Jones's web page, downloaded (e.g., via wget) to a local directory, which is specified as path above. INPUT: - ``path`` - (default works on William Stein install.) path must be the path to Jones's Number_Fields directory http://hobbes.la.asu.edu/Number_Fields These files should have been downloaded using wget. EXAMPLES: This is how to create the database from scratch, assuming that the number fields are in the default directory above: From a cold start of Sage:: sage: J = JonesDatabase() sage: J._init() # not tested ... This takes about 5 seconds. """ from sage.misc.misc import sage_makedirs n = 0 x = PolynomialRing(RationalField(), 'x').gen() self.root = {} self.root[tuple([])] = [x - 1] if not os.path.exists(path): raise IOError("Path %s does not exist." % path) for X in os.listdir(path): if X[-4:] == "solo": Z = path + "/" + X print(X) for Y in os.listdir(Z): if Y[-3:] == ".gp": self._load(Z, Y) sage_makedirs(JONESDATA) save(self.root, JONESDATA + "/jones.sobj")
def save_timeit_stats_to_file_named(self, output_filename): if self._collect_timeit_stats: from sage.structure.sage_object import save save(self._timeit_stats, filename=output_filename)