Beispiel #1
0
def lmfdb_genus_representatives(label):
    url = 'http://www.lmfdb.org/Lattice/' + label + '/download/sage/genus_reps'
    u = urlopen(url)
    s = u.read()
    with open(tmp_filename(ext='.sage'), 'w') as f:
        f.write(s)
    load(f.name, globals())
    return data
Beispiel #2
0
def reload_attached_files_if_modified():
    r"""
    Reload attached files that have been modified

    This is the internal implementation of the attach mechanism.

    EXAMPLES::

        sage: sage.repl.attach.reset()
        sage: from sage.repl.interpreter import get_test_shell
        sage: shell = get_test_shell()
        sage: tmp = tmp_filename(ext='.py')
        sage: open(tmp, 'w').write('a = 2\n')
        sage: shell.run_cell('attach({0})'.format(repr(tmp)))
        sage: shell.run_cell('a')
        2
        sage: sleep(1)   # filesystem mtime granularity
        sage: open(tmp, 'w').write('a = 3\n')

    Note that the doctests are never really at the command prompt
    where the automatic reload is triggered. So we have to do it
    manually::

        sage: shell.run_cell('from sage.repl.attach import reload_attached_files_if_modified')
        sage: shell.run_cell('reload_attached_files_if_modified()')
        ### reloading attached file tmp_....py modified at ... ###

        sage: shell.run_cell('a')
        3
        sage: shell.run_cell('detach({0})'.format(repr(tmp)))
        sage: shell.run_cell('attached_files()')
        []
        sage: shell.quit()
    """
    ip = get_ipython()
    for filename, mtime in modified_file_iterator():
        basename = os.path.basename(filename)
        timestr = time.strftime('%T', mtime)
        notice = '### reloading attached file {0} modified at {1} ###'.format(
            basename, timestr)
        if ip and ip.pt_cli:
            with ip.pt_cli.patch_stdout_context(raw=True):
                print(notice)
                code = load_wrap(filename, attach=True)
                ip.run_cell(code)
        elif ip:
            print(notice)
            code = load_wrap(filename, attach=True)
            ip.run_cell(code)
        else:
            print(notice)
            load(filename, globals(), attach=True)
Beispiel #3
0
def reload_attached_files_if_modified():
    r"""
    Reload attached files that have been modified

    This is the internal implementation of the attach mechanism.

    EXAMPLES::

        sage: sage.repl.attach.reset()
        sage: from sage.repl.interpreter import get_test_shell
        sage: shell = get_test_shell()
        sage: tmp = tmp_filename(ext='.py')
        sage: open(tmp, 'w').write('a = 2\n')
        sage: shell.run_cell('attach({0})'.format(repr(tmp)))
        sage: shell.run_cell('a')
        2
        sage: sleep(1)   # filesystem mtime granularity
        sage: open(tmp, 'w').write('a = 3\n')

    Note that the doctests are never really at the command prompt
    where the automatic reload is triggered. So we have to do it
    manually::

        sage: shell.run_cell('from sage.repl.attach import reload_attached_files_if_modified')
        sage: shell.run_cell('reload_attached_files_if_modified()')
        ### reloading attached file tmp_....py modified at ... ###

        sage: shell.run_cell('a')
        3
        sage: shell.run_cell('detach({0})'.format(repr(tmp)))
        sage: shell.run_cell('attached_files()')
        []
        sage: shell.quit()
    """
    ip = get_ipython()
    for filename, mtime in modified_file_iterator():
        basename = os.path.basename(filename)
        timestr = time.strftime('%T', mtime)
        notice = '### reloading attached file {0} modified at {1} ###'.format(basename, timestr)
        if ip and ip.pt_cli:
            with ip.pt_cli.patch_stdout_context():
                print(notice)
                code = load_wrap(filename, attach=True)
                ip.run_cell(code)
        elif ip:
            print(notice)
            code = load_wrap(filename, attach=True)
            ip.run_cell(code)
        else:
            print(notice)
            load(filename, globals(), attach=True)
Beispiel #4
0
def attach(*files):
    """
    Attach a file or files to a running instance of Sage and also load
    that file.

    INPUT:

    - ``files`` -- a list of filenames (strings) to attach.

    OUTPUT:

    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).

    .. SEEALSO::

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

    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')  # not tested

    Here we test attaching multiple files at once::

        sage: sage.repl.attach.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)
Beispiel #5
0
def attach(*files):
    """
    Attach a file or files to a running instance of Sage and also load
    that file.

    .. NOTE::

        Attaching files uses the Python inputhook, which will conflict
        with other inputhook users. This generally includes GUI main loop
        integrations, for example tkinter. So you can only use tkinter or
        attach, but not both at the same time.

    INPUT:

    - ``files`` -- a list of filenames (strings) to attach.

    OUTPUT:

    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).

    .. SEEALSO::

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

    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')  # not tested

    Here we test attaching multiple files at once::

        sage: sage.repl.attach.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)