Example #1
0
    def save_mangled(self, name, dir=None):
        r"""
        Save an object using its standardized name.

        INPUT:

        - ``self`` -- the current object.
        - ``name`` -- string: the file name suffix (without ".obj")
          part of the standardized name.
        - ``dir`` -- string, optional. The directory where the object
          is to be saved. Default is None, meaning the current directory.

        OUTPUT:

        None.

        EFFECT:

        A file is created and the object ``self`` is saved into the file.

        EXAMPLES:

        ::

            sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved as BFI
            sage: a = BFI([0,1,0,0])
            sage: d = tmp_dir()
            sage: a.save_mangled("a", dir=d)
            sage: file_name = BFI.mangled_name("a.sobj", dir=d)
            sage: os.path.isfile(file_name)
            True
            sage: BFI.remove_mangled("a", dir=d)
            sage: os.rmdir(d)
        """
        save(self, self.__class__.mangled_name(name, dir=dir))
Example #2
0
    def _create_col_dict_sobj(self, sobj_path=None):
        r"""
        Create ``sobj`` files containing the number of knots and a dictionary
        for the columns of the table.

        EXAMPLES::

            sage: from sage.databases.knotinfo_db import KnotInfoDataBase
            sage: ki_db = KnotInfoDataBase()
            sage: ki_db._create_col_dict_sobj() # not tested (used on installation)
        """
        knot_list = self.knot_list()
        knot_column_names = knot_list[0]

        link_list = self.link_list()
        link_column_names = link_list[0]

        column_dict = {}

        if not sobj_path:
            sobj_path = self._sobj_path

        # ----------------------------------------------------------------
        # Columns that exist for knots and links
        # ----------------------------------------------------------------
        for col in knot_column_names:

            name = knot_column_names[col]
            if not name and col not in [
                    'knot_atlas_anon', 'knotilus_page_anon'
            ]:
                # not of interest
                continue

            col_type = KnotInfoColumnTypes.OnlyKnots
            if col in link_column_names:
                col_type = KnotInfoColumnTypes.KnotsAndLinks
            column_dict[col] = [name, col_type]

        # ----------------------------------------------------------------
        # Columns that exist for links only
        # ----------------------------------------------------------------
        for col in link_column_names:

            name = link_column_names[col]
            if not name and col not in [
                    'knot_atlas_anon', 'knotilus_page_anon'
            ]:
                # not of interest
                continue

            if col in knot_column_names:
                # already used
                continue

            col_type = KnotInfoColumnTypes.OnlyLinks
            column_dict[col] = [name, col_type]

        save(column_dict,
             '%s/%s' % (sobj_path, self.filename.knots.sobj_column()))
Example #3
0
    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 os
        import sys
        try:
            from importlib import reload
        except ImportError:
            from imp import reload
        from sage.misc.persist 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
        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 backup(self):
        path = self.path
        prec = self.prec

        print "Writing to '" + path + ".sobj'."
        save(self,path)
        if prec != None: save(prec,path+"_prec"+repr(prec))
        return self
Example #5
0
    def __call__(self, *args, **kwds):
        key = self.key(*args, **kwds)
        h = self._hash(key)
        name = '%s_%s.sobj' % (self._prefix, h)

        if os.path.exists(name):
            key2, val = persist.load(name)
            if key == key2:
                # We save and test equality of keys to avoid
                # the (extremely remote) possibility of a hash
                # collision.  Correctness is crucial in mathematics.
                return val

        val = self._func(*args, **kwds)
        persist.save((key, val), name)
        return val
Example #6
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.


        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
        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")
Example #7
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.


        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
        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")
Example #8
0
    def create_filecache(self, force=False):
        r"""
        Create the internal files containing the database.

        INPUT:

        - ``force`` -- optional boolean. If set to ``True`` the existing
          file-cache is overwritten

        EXAMPLES::

            sage: from sage.databases.knotinfo_db import KnotInfoDataBase
            sage: ki_db = KnotInfoDataBase()
            sage: ki_db.create_filecache()    # optional - database_knotinfo
        """
        if not self._feature.is_present():
            return

        if os.path.isdir(self._sobj_path):
            # if it exists then remove it if it belongs to an older version of
            # the database or should be reset by the user because it is damaged.
            test_version = os.path.join(
                self._sobj_path, self.filename.knots.num_knots(self.version()))
            if force or not os.path.isfile(test_version):
                import shutil
                shutil.rmtree(self._sobj_path)

        from sage.misc.temporary_file import atomic_dir
        with atomic_dir(self._sobj_path) as d:
            sobj_path = d.name
            num_knots_file = os.path.join(
                sobj_path, self.filename.knots.num_knots(self.version()))
            knot_list = self.knot_list()
            num_knots = len(knot_list) - 1
            save(num_knots, num_knots_file)
            self._num_knots = num_knots
            self._create_col_dict_sobj(sobj_path=sobj_path)
            self._create_data_sobj(sobj_path=sobj_path)
        return
Example #9
0
 def backup(self):
     print "Writing to `" + self.path + ".sobj'."
     save(self,self.path)
     if self.prec != None: save(self.prec,self.path+"_prec"+repr(self.prec))
     return self
Example #10
0
def show(plt=PLT,
         comment=None,
         folder=os.path.join(TMP_FOLDER, 'matplotlib'),
         html_filename='index.html',
         verbose=False,
         track_caller=True,
         sage=False):
    """
    Plot to a png file and append <img> in a html file.
    """
    if not os.path.isdir(folder):
        os.mkdir(folder)
    full_html_filename = os.path.join(folder, html_filename)
    if not os.path.isfile(full_html_filename):
        with open(folder + '/' + html_filename, 'w') as html_file:
            html_file.write(HTML_HULL)
    full_img_filename = os.path.join(
        folder,
        str(datetime.datetime.now()).replace(' ', '-').replace('', '') +
        '.png')
    if sage:
        import sage.misc.persist as p
        p.save(plt, full_img_filename, axes=False, aspect_ratio=True)
        #os.system('open -a Preview /tmp/dom.png')
    else:
        plt.savefig(full_img_filename)
        plt.gcf().clear()

    if track_caller:
        code = inspect.currentframe().f_back.f_code
        caller_filename = code.co_filename
        if not os.path.isfile(caller_filename):
            track_caller = False
        else:
            with open(caller_filename) as f:
                code_html = highlight(f.read(), PythonLexer(),
                                      HtmlFormatter(linespans='line'))
                code_css = HtmlFormatter(
                    linespans='line').get_style_defs('.highlight')
            calling_code_html = os.path.join(
                folder,
                str(datetime.datetime.now()).replace(' ', '-') + '.html')
            with open(calling_code_html, 'w') as html_file:
                tmp = HTML_HULL.replace(
                    '<!--PLACEHOLDER-->', """<p class='p_image'><img src='""" +
                    full_img_filename + """'/><br/>
                       <pre>""" + full_img_filename +
                    ("-" + comment if comment else "") + "</pre></p>" +
                    ("caller: <a href='" + calling_code_html + "#line-" +
                     str(code.co_firstlineno) + "'>" + caller_filename +
                     "</a>" if track_caller else "") + "<br/>" + code_html
                ).replace(
                    '<!--STYLEPLACEHOLDER-->',
                    code_css + "\n#line-" + str(code.co_firstlineno) +
                    "{ background-color: #ddff36;} html, body {font-family: Verdana,sans-serif;} a {text-decoration: none; color: hotpink; } a:visited { color: grey; }"
                )
                html_file.write(tmp)

    with open(folder + '/' + html_filename, 'r') as html_file:
        html_old = html_file.read()
    with open(folder + '/' + html_filename, 'w') as html_file:
        html_file.write(
            html_old.replace(
                '<!--PLACEHOLDER-->', """<!--PLACEHOLDER-->
<p class='p_image'><img src='""" + full_img_filename + """'/><br/>
                   <pre>""" + full_img_filename +
                ("-" + comment if comment else "") + "</pre>" +
                ("caller: <a href='" + calling_code_html + "#line-" +
                 str(code.co_firstlineno) + "'>" + caller_filename +
                 "</a>" if track_caller else "") + "</p>").
            replace(
                '<!--STYLEPLACEHOLDER-->',
                'html, body {font-family: Verdana,sans-serif;} a {text-decoration: none; color: hotpink; } a:visited { color: grey; }'
            ))
    if verbose:
        print('plot viewable in=', full_html_filename)
Example #11
0
    def _create_data_sobj(self, sobj_path=None):
        r"""
        Create ``sobj`` files containing the contents of the whole table.
        To each column there is created one file containing a list of
        strings giving the entries of the database table.

        The length of these lists depends on the type of the corresponding
        column. If a column is used in both tables
        (``KnotInfoColumnTypes.KnotsAndLinks``) the list of proper links
        is appended to the list of knots.  In both other cases the lenght
        of the list corresponds to the number of listed knots and proper
        links respectively.

        EXAMPLES::

            sage: from sage.databases.knotinfo_db import KnotInfoDataBase
            sage: ki_db = KnotInfoDataBase()
            sage: ki_db._create_data_sobj() # not tested (just used on installation)
        """
        knot_list = self.knot_list()
        link_list = self.link_list()
        len_knots = len(knot_list)
        len_links = len(link_list)

        row_dict = {}

        if not sobj_path:
            sobj_path = self._sobj_path

        # ----------------------------------------------------------------
        # Columns that exist for knots and links
        # ----------------------------------------------------------------
        column_dict = load('%s/%s' %
                           (sobj_path, self.filename.knots.sobj_column()))
        cols = KnotInfoColumns('ColsTemp', column_dict)
        for col in cols:
            val_list = []

            if col.column_type() != col.types.OnlyLinks:
                for i in range(1, len_knots):
                    if col.name == self._names_column:
                        row_dict[self._knot_prefix +
                                 knot_list[i][col.name]] = [i - 1, 1]

                    val_list.append(knot_list[i][col.name])

            if col.column_type() != col.types.OnlyKnots:
                for i in range(1, len_links):
                    if col.name == self._names_column:
                        link_name = link_list[i][col.name]
                        link_name = link_name.replace('{', '_')
                        link_name = link_name.replace(',', '_')
                        link_name = link_name.replace('}', '')

                        num_comp = int(link_list[i][self._components_column])
                        row_dict[link_name] = [i + len_knots - 2, num_comp]

                    val_list.append(link_list[i][col.name])

            if val_list:
                save(val_list,
                     '%s/%s' % (sobj_path, self.filename.knots.sobj_data(col)))

        save(row_dict, '%s/%s' % (sobj_path, self.filename.knots.sobj_row()))