TESTS:
Check that github issue #195 is fixed:
sage: from sagenb.misc.misc import mathjax_macros
sage: type(mathjax_macros)
<type 'list'>
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'
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:
OUTPUT:
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
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:
OUTPUT:
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]
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'
Print a message on the screen suggesting that the user open their web browser to a certain URL.
INPUT:
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 │
│ │
└────────────────────────────────────────────────┘
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'