Пример #1
0
def attach(*files):
    """
    Attach a file or files to a running instance of Sage and also load
    that file.

    USAGE:

    ``attach file1 ...`` - space-separated list of ``.py``, ``.pyx``,
    and ``.sage`` files, or ``attach('file1', 'file2')`` - filenames as
    strings, given as arguments to :func:`attach`.

    :meth:`~sage.misc.preparser.load` is the same as :func:`attach`, but
    doesn't automatically reload a file when it changes.

    .. NOTE::

       On the Sage prompt you can also just type ``attach "foo.sage"``
       as a short-hand for ``attach('foo.sage')``. However this
       alternate form is not part of the Python language and does not
       work in Python scripts.

    EFFECT:

    Each file is read in and added to an internal list of watched files.
    The meaning of reading in a file depends on the file type:

    -  ``.py`` files are read in with no preparsing (so, e.g., ``2^3`` is 2
       bit-xor 3);

    -  ``.sage`` files are preparsed, then the result is read in;

    - ``.pyx`` files are *not* preparsed, but rather are compiled to a
       module ``m`` and then ``from m import *`` is executed.

    The contents of the file are then loaded, which means they are read
    into the running Sage session. For example, if ``foo.sage`` contains
    ``x=5``, after attaching ``foo.sage`` the variable ``x`` will be set
    to 5. Moreover, any time you change ``foo.sage``, before you execute
    a command, the attached file will be re-read automatically (with no
    intervention on your part).

    EXAMPLES:

    You attach a file, e.g., ``foo.sage`` or ``foo.py`` or
    ``foo.pyx``, to a running Sage session by typing::

        sage: attach foo.sage   # or foo.py or foo.pyx or even a URL to such a file (not tested)

    or::

        sage: attach('foo.sage')  # not tested

    Here we test attaching multiple files at once::

        sage: sage.misc.attached_files.reset()
        sage: t1 = tmp_filename(ext='.py')
        sage: open(t1,'w').write("print 'hello world'")
        sage: t2 = tmp_filename(ext='.py')
        sage: open(t2,'w').write("print 'hi there xxx'")
        sage: attach(t1, t2)
        hello world
        hi there xxx
        sage: set(attached_files()) == set([t1,t2])
        True

    .. SEEALSO::

        - :meth:`attached_files` returns a list of
          all currently attached files.

        - :meth:`detach` instructs Sage to remove a
          file from the internal list of watched files.

        - :meth:`load_attach_path` allows you to
          get or modify the current search path for loading and attaching
          files.
    """
    try:
        ipy = get_ipython()
    except NameError:
        ipy = None
    global attached
    for filename in files:
        if ipy:
            code = load_wrap(filename, attach=True)
            ipy.run_cell(code)
        else:
            load(filename, globals(), attach=True)
Пример #2
0
from random import shuffle

from sage.graphs.graph import Graph
from sage.misc.preparser import load
from sage.graphs.base.dense_graph import DenseGraph

log = logging.getLogger("hom")
log.setLevel(logging.INFO)

# Initialize logging if not already initialized
if not logging.root.handlers:
    logging.basicConfig(format="%(asctime)s %(levelname)s [%(name)s]: %(msg)s",
                        datefmt="%Y-%m-%d %H:%M:%S")

try:
    load("homomorphisms_c.pyx", locals())
except Exception as e:
    log.fatal("homomorphisms_c.pyx failed to load: %s", e)

### Heurisrics for branching vertex order


def degree_within(G, v, w):
    "Return the number of neighbors of `v` in `w`."
    return len(set(G.neighbors(v)).intersection(set(w)))


def sort_vertices_by_degree(G, vs, within=None):
    """
  Stable inplace sort of vs by degree in G, optionally
  only considering neighbors in `within`.
Пример #3
0
def attach(*files):
    """
    Attach a file or files to a running instance of Sage and also load
    that file.

    USAGE:

    ``attach file1 ...`` - space-separated list of ``.py``, ``.pyx``,
    and ``.sage`` files, or ``attach('file1', 'file2')`` - filenames as
    strings, given as arguments to :func:`attach`.

    :meth:`~sage.misc.preparser.load` is the same as :func:`attach`, but
    doesn't automatically reload a file when it changes.

    .. NOTE::

       On the Sage prompt you can also just type ``attach "foo.sage"``
       as a short-hand for ``attach('foo.sage')``. However this
       alternate form is not part of the Python language and does not
       work in Python scripts.

    EFFECT:

    Each file is read in and added to an internal list of watched files.
    The meaning of reading in a file depends on the file type:

    -  ``.py`` files are read in with no preparsing (so, e.g., ``2^3`` is 2
       bit-xor 3);

    -  ``.sage`` files are preparsed, then the result is read in;

    - ``.pyx`` files are *not* preparsed, but rather are compiled to a
       module ``m`` and then ``from m import *`` is executed.

    The contents of the file are then loaded, which means they are read
    into the running Sage session. For example, if ``foo.sage`` contains
    ``x=5``, after attaching ``foo.sage`` the variable ``x`` will be set
    to 5. Moreover, any time you change ``foo.sage``, before you execute
    a command, the attached file will be re-read automatically (with no
    intervention on your part).

    EXAMPLES:

    You attach a file, e.g., ``foo.sage`` or ``foo.py`` or
    ``foo.pyx``, to a running Sage session by typing::

        sage: attach foo.sage   # or foo.py or foo.pyx or even a URL to such a file (not tested)

    or::

        sage: attach('foo.sage')  # not tested

    Here we test attaching multiple files at once::

        sage: sage.misc.attached_files.reset()
        sage: t1 = tmp_filename(ext='.py')
        sage: open(t1,'w').write("print 'hello world'")
        sage: t2 = tmp_filename(ext='.py')
        sage: open(t2,'w').write("print 'hi there xxx'")
        sage: attach(t1, t2)
        hello world
        hi there xxx
        sage: set(attached_files()) == set([t1,t2])
        True

    .. SEEALSO::

        - :meth:`attached_files` returns a list of
          all currently attached files.

        - :meth:`detach` instructs Sage to remove a
          file from the internal list of watched files.

        - :meth:`load_attach_path` allows you to
          get or modify the current search path for loading and attaching
          files.
    """
    try:
        ipy = get_ipython()
    except NameError:
        ipy = None
    global attached
    for filename in files:
        if ipy:
            code = load_wrap(filename, attach=True)
            ipy.run_cell(code)
        else:
            load(filename, globals(), attach=True)
Пример #4
0
from random import shuffle

from sage.graphs.graph import Graph
from sage.misc.preparser import load
from sage.graphs.base.dense_graph import DenseGraph


log = logging.getLogger("hom")
log.setLevel(logging.INFO)

# Initialize logging if not already initialized
if not logging.root.handlers:
  logging.basicConfig(format="%(asctime)s %(levelname)s [%(name)s]: %(msg)s", datefmt="%Y-%m-%d %H:%M:%S")

try:
  load("homomorphisms_c.pyx", locals())
except Exception as e:
  log.fatal("homomorphisms_c.pyx failed to load: %s", e)


### Heurisrics for branching vertex order


def degree_within(G, v, w):
  "Return the number of neighbors of `v` in `w`."
  return len(set(G.neighbors(v)).intersection(set(w)))


def sort_vertices_by_degree(G, vs, within=None):
  """
  Stable inplace sort of vs by degree in G, optionally