Miscellaneous Notebook Functions¶
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.
encoded_str
(obj, encoding='utf-8')[source]¶ Takes an object and returns an encoded str human-readable representation.
string to bytes
EXAMPLES:
sage: from sagenb.misc.misc import encoded_str sage: encoded_str(u'\u011b\u0161\u010d\u0159\u017e\xfd\xe1\xed\xe9\u010f\u010e') == 'ěščřžýáíéďĎ' 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)[source]¶ 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 checkstart
- an int; the starting port number for the scanmax_tries
- an int (default: 100); how many ports to scanverbose
- 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.
ignore_nonexistent_files
(curdir, dirlist)[source]¶ 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 directorydirlist
- a list of strings; names ofcurdir
’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: [ope(opj(s, x)) for x in slist] [False, True, True] sage: [os.path.islink(opj(s, x)) for x in 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: [ope(opj(t, x)) for x in tlist] [True, True] sage: [os.path.islink(opj(t, x)) for x in tlist] # Note! [False, False]
-
sagenb.misc.misc.
pad_zeros
(s, size=3)[source]¶ 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='')[source]¶ 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 nameport
– an int; a port numbersecure
– 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.
unicode_str
(obj, encoding='utf-8')[source]¶ Takes an object and returns a unicode human-readable representation.
(bytes or string) to string
EXAMPLES:
sage: from sagenb.misc.misc import unicode_str sage: unicode_str('ěščřžýáíéďĎ') == u'\u011b\u0161\u010d\u0159\u017e\xfd\xe1\xed\xe9\u010f\u010e' True sage: unicode_str('abc') u'abc' sage: unicode_str(123) u'123'