Miscellaneous Notebook Functions

TESTS:

Check that github issue #195 is fixed:

sage: from sagenb.misc.misc import mathjax_macros
sage: type(mathjax_macros)
<type 'list'>
sagenb.misc.misc.N_(message)
sagenb.misc.misc.cputime(t=0)
sagenb.misc.misc.encoded_str(obj, encoding='utf-8')

Takes an object and returns an encoded str human-readable representation.

EXAMPLES:

sage: from sagenb.misc.misc import encoded_str
sage: encoded_str(u'ěščřž\xfd\xe1\xed\xe9ďĎ') == 'ěščřžýáíéďĎ'
True
sage: encoded_str(u'abc')
'abc'
sage: encoded_str(123)
'123'
sagenb.misc.misc.find_next_available_port(interface, start, max_tries=100, verbose=False)

Find the next available port at a given interface, that is, a port for which a current connection attempt returns a ‘Connection refused’ error message. If no port is found, raise a RuntimeError exception.

INPUT:

  • interface - address to check
  • start - an int; the starting port number for the scan
  • max_tries - an int (default: 100); how many ports to scan
  • verbose - a bool (default: True); whether to print information about the scan

OUTPUT:

  • an int - the port number

EXAMPLES:

sage: from sagenb.misc.misc import find_next_available_port
sage: find_next_available_port('127.0.0.1', 9000, verbose=False)   # random output -- depends on network
9002
sagenb.misc.misc.get_languages()
sagenb.misc.misc.ignore_nonexistent_files(curdir, dirlist)

Returns a list of non-existent files, given a directory and its contents. The returned list includes broken symbolic links. Use this, e.g., with shutil.copytree(), as shown below.

INPUT:

  • curdir - a string; the name of the current directory
  • dirlist - a list of strings; names of curdir‘s contents

OUTPUT:

  • a list of strings; names of curdir‘s non-existent files

EXAMPLES:

sage: import os, shutil
sage: from sagenb.misc.misc import ignore_nonexistent_files
sage: opj = os.path.join; ope = os.path.exists; t = tmp_dir()
sage: s = opj(t, 'src'); t = opj(t, 'trg'); hi = opj(s, 'hi.txt');
sage: os.makedirs(s)
sage: f = open(hi, 'w'); f.write('hi'); f.close()
sage: os.symlink(hi, opj(s, 'good.txt'))
sage: os.symlink(opj(s, 'bad'), opj(s, 'bad.txt'))
sage: slist = sorted(os.listdir(s)); slist
['bad.txt', 'good.txt', 'hi.txt']
sage: map(lambda x: ope(opj(s, x)), slist)
[False, True, True]
sage: map(lambda x: os.path.islink(opj(s, x)), slist)
[True, True, False]
sage: shutil.copytree(s, t)
Traceback (most recent call last):
...
Error: [('.../src/bad.txt', '.../trg/bad.txt', "[Errno 2] No such file or directory: '.../src/bad.txt'")]
sage: shutil.rmtree(t); ope(t)
False
sage: shutil.copytree(s, t, ignore = ignore_nonexistent_files)
sage: tlist = sorted(os.listdir(t)); tlist
['good.txt', 'hi.txt']
sage: map(lambda x: ope(opj(t, x)), tlist)
[True, True]
sage: map(lambda x: os.path.islink(opj(t, x)), tlist)  # Note!
[False, False]
sagenb.misc.misc.is_Matrix(x)
sagenb.misc.misc.nN_(message_singular, message_plural)
sagenb.misc.misc.open_page(address, port, secure, path='')
sagenb.misc.misc.pad_zeros(s, size=3)

EXAMPLES:

sage: pad_zeros(100)
'100'
sage: pad_zeros(10)
'010'
sage: pad_zeros(10, 5)
'00010'
sage: pad_zeros(389, 5)
'00389'
sage: pad_zeros(389, 10)
'0000000389'
sagenb.misc.misc.print_open_msg(address, port, secure=False, path='')

Print a message on the screen suggesting that the user open their web browser to a certain URL.

INPUT:

  • address – a string; a computer address or name
  • port – an int; a port number
  • secure – a bool (default: False); whether to prefix the URL with ‘http’ or ‘https’
  • path – a string; the URL’s path following the port.

EXAMPLES:

sage: from sagenb.misc.misc import print_open_msg
sage: print_open_msg('localhost', 8080, True)
┌──────────────────────────────────────────────────┐
│                                                  │
│ Open your web browser to https://localhost:8080  │
│                                                  │
└──────────────────────────────────────────────────┘
sage: print_open_msg('sagemath.org', 8080, False)
┌────────────────────────────────────────────────────┐
│                                                    │
│ Open your web browser to http://sagemath.org:8080  │
│                                                    │
└────────────────────────────────────────────────────┘
sage: print_open_msg('sagemath.org', 90, False)
┌──────────────────────────────────────────────────┐
│                                                  │
│ Open your web browser to http://sagemath.org:90  │
│                                                  │
└──────────────────────────────────────────────────┘
sage: print_open_msg('sagemath.org', 80, False)
┌────────────────────────────────────────────────┐
│                                                │
│  Open your web browser to http://sagemath.org  │
│                                                │
└────────────────────────────────────────────────┘
sagenb.misc.misc.register_with_cleaner(pid)
sagenb.misc.misc.set_permissive_permissions(filename)
sagenb.misc.misc.set_restrictive_permissions(filename, allow_execute=False)
sagenb.misc.misc.stub(f)
sagenb.misc.misc.translations_path()
sagenb.misc.misc.unicode_str(obj, encoding='utf-8')

Takes an object and returns a unicode human-readable representation.

EXAMPLES:

sage: from sagenb.misc.misc import unicode_str
sage: unicode_str('ěščřžýáíéďĎ') == u'ěščřž\xfd\xe1\xed\xe9ďĎ'
True
sage: unicode_str('abc')
u'abc'
sage: unicode_str(123)
u'123'
sagenb.misc.misc.walltime(t=0)
sagenb.misc.misc.word_wrap(s, ncols=85)

Previous topic

Notebook Registration Challenges

Next topic

Support for Notebook Introspection and Setup

This Page