本文整理匯總了Python中IPython.lib.pretty.pretty方法的典型用法代碼示例。如果您正苦於以下問題:Python pretty.pretty方法的具體用法?Python pretty.pretty怎麽用?Python pretty.pretty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類IPython.lib.pretty
的用法示例。
在下文中一共展示了pretty.pretty方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: repr_pretty_delegate
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def repr_pretty_delegate(obj):
# If IPython is already loaded, then might as well use it. (Most commonly
# this will occur if we are in an IPython session, but somehow someone has
# called repr() directly. This can happen for example if printing an
# container like a namedtuple that IPython lacks special code for
# pretty-printing.) But, if IPython is not already imported, we do not
# attempt to import it. This makes patsy itself faster to import (as of
# Nov. 2012 I measured the extra overhead from loading IPython as ~4
# seconds on a cold cache), it prevents IPython from automatically
# spawning a bunch of child processes (!) which may not be what you want
# if you are not otherwise using IPython, and it avoids annoying the
# pandas people who have some hack to tell whether you are using IPython
# in their test suite (see patsy bug #12).
if optional_dep_ok and "IPython" in sys.modules:
from IPython.lib.pretty import pretty
return pretty(obj)
else:
return _mini_pretty(obj)
示例2: repr_pretty_impl
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def repr_pretty_impl(p, obj, args, kwargs=[]):
name = obj.__class__.__name__
p.begin_group(len(name) + 1, "%s(" % (name,))
started = [False]
def new_item():
if started[0]:
p.text(",")
p.breakable()
started[0] = True
for arg in args:
new_item()
p.pretty(arg)
for label, value in kwargs:
new_item()
p.begin_group(len(label) + 1, "%s=" % (label,))
p.pretty(value)
p.end_group(len(label) + 1, "")
p.end_group(len(name) + 1, ")")
示例3: testUnicodeInputAndOutput
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def testUnicodeInputAndOutput(self):
# "貓" is "cats" in simplified Chinese and its representation requires
# three bytes. Force reading only one byte at a time and ensure that the
# character is preserved.
_system_commands._PTY_READ_MAX_BYTES_FOR_TEST = 1
cmd = u'r = %shell read result && echo "You typed: $result"'
run_cell_result = self.run_cell(cmd, provided_inputs=[u'貓\n'])
captured_output = run_cell_result.output
self.assertEqual('', captured_output.stderr)
self.assertEqual(u'貓\nYou typed: 貓\n', captured_output.stdout)
result = self.ip.user_ns['r']
self.assertEqual(0, result.returncode)
self.assertEqual(u'貓\nYou typed: 貓\n', result.output)
# Ensure that ShellResult objects don't have a "pretty" representation. This
# ensures that no output is printed if the magic is the only statement in
# the cell.
self.assertEqual(u'', pretty.pretty(result))
示例4: test_multivector
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def test_multivector(self, g2):
p = g2.MultiVector(np.arange(4, dtype=np.int32))
assert pretty.pretty(p) == repr(p)
expected = textwrap.dedent("""\
MultiVector(Layout([1, 1],
ids=BasisVectorIds.ordered_integers(2),
order=BasisBladeOrder.shortlex(2),
names=['', 'e1', 'e2', 'e12']),
[0, 1, 2, 3],
dtype=int32)""")
# ipython printing only kicks in in ugly mode
try:
clifford.ugly()
assert pretty.pretty(p) == expected
finally:
clifford.pretty()
示例5: test_multivector_predefined
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def test_multivector_predefined(self):
""" test the short printing of predefined layouts """
from clifford.g2 import layout as g2
p_i = g2.MultiVector(np.arange(4, dtype=np.int32))
p_f = g2.MultiVector(np.arange(4, dtype=np.float64))
assert pretty.pretty(p_i) == repr(p_i)
assert pretty.pretty(p_f) == repr(p_f)
# float is implied
expected_i = "clifford.g2.layout.MultiVector([0, 1, 2, 3], dtype=int32)"
expected_f = "clifford.g2.layout.MultiVector([0.0, 1.0, 2.0, 3.0])"
# ipython printing only kicks in in ugly mode
try:
clifford.ugly()
assert pretty.pretty(p_i) == expected_i
assert pretty.pretty(p_f) == expected_f
finally:
clifford.pretty()
示例6: test_invariants
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def test_invariants(self, g2):
e1, e2 = g2.basis_vectors_lst
matrix = np.array([[ 0, 1],
[-1, 0]])
f = transformations.OutermorphismMatrix(matrix, g2)
# test the vector transform is as requested
assert f(e1) == -e2
assert f(e2) == e1
# test the generalization is correct
assert f(e1^e2) == f(e1)^f(e2)
assert f(g2.scalar) == g2.scalar
# test that distributivity is respected
assert f(g2.scalar + 2*e1 + 3*(e1^e2)) == f(g2.scalar) + 2*f(e1) + 3*f(e1^e2)
assert pretty.pretty(f) == textwrap.dedent("""\
OutermorphismMatrix(array([[ 0, 1],
[-1, 0]]),
Layout([1, 1],
ids=BasisVectorIds(['u', 'v']),
order=BasisBladeOrder.shortlex(2),
names=['', 'eu', 'ev', 'euv']))""")
示例7: __repr__
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def __repr__(self):
bindings = ['edges', 'nodes', 'source', 'destination', 'node',
'edge_label', 'edge_color', 'edge_size', 'edge_weight', 'edge_title', 'edge_icon', 'edge_opacity',
'edge_source_color', 'edge_destination_color',
'point_label', 'point_color', 'point_size', 'point_weight', 'point_title', 'point_icon', 'point_opacity',
'point_x', 'point_y']
settings = ['height', 'url_params']
rep = {'bindings': dict([(f, getattr(self, '_' + f)) for f in bindings]),
'settings': dict([(f, getattr(self, '_' + f)) for f in settings])}
if util.in_ipython():
from IPython.lib.pretty import pretty
return pretty(rep)
else:
return str(rep)
示例8: test_PushbackAdapter
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def test_PushbackAdapter():
it = PushbackAdapter(iter([1, 2, 3, 4]))
assert it.has_more()
assert six.advance_iterator(it) == 1
it.push_back(0)
assert six.advance_iterator(it) == 0
assert six.advance_iterator(it) == 2
assert it.peek() == 3
it.push_back(10)
assert it.peek() == 10
it.push_back(20)
assert it.peek() == 20
assert it.has_more()
assert list(it) == [20, 10, 3, 4]
assert not it.has_more()
# The IPython pretty-printer gives very nice output that is difficult to get
# otherwise, e.g., look how much more readable this is than if it were all
# smooshed onto one line:
#
# ModelDesc(input_code='y ~ x*asdf',
# lhs_terms=[Term([EvalFactor('y')])],
# rhs_terms=[Term([]),
# Term([EvalFactor('x')]),
# Term([EvalFactor('asdf')]),
# Term([EvalFactor('x'), EvalFactor('asdf')])],
# )
#
# But, we don't want to assume it always exists; nor do we want to be
# re-writing every repr function twice, once for regular repr and once for
# the pretty printer. So, here's an ugly fallback implementation that can be
# used unconditionally to implement __repr__ in terms of _pretty_repr_.
#
# Pretty printer docs:
# http://ipython.org/ipython-doc/dev/api/generated/IPython.lib.pretty.html
示例9: pretty
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def pretty(self, obj):
if hasattr(obj, "_repr_pretty_"):
obj._repr_pretty_(self, False)
else:
self.text(repr(obj))
示例10: _mini_pretty
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def _mini_pretty(obj):
printer = _MiniPPrinter()
printer.pretty(obj)
return printer.getvalue()
示例11: _repr_pretty_
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def _repr_pretty_(self, p, cycle):
if cycle:
p.text("MyList(...)")
else:
with p.group(3, "MyList(", ")"):
for (i, child) in enumerate(self.content):
if i:
p.text(",")
p.breakable()
else:
p.breakable("")
p.pretty(child)
示例12: test_indentation
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def test_indentation():
"""Test correct indentation in groups"""
count = 40
gotoutput = pretty.pretty(MyList(range(count)))
expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")"
nt.assert_equal(gotoutput, expectedoutput)
示例13: test_dispatch
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def test_dispatch():
"""
Test correct dispatching: The _repr_pretty_ method for MyDict
must be found before the registered printer for dict.
"""
gotoutput = pretty.pretty(MyDict())
expectedoutput = "MyDict(...)"
nt.assert_equal(gotoutput, expectedoutput)
示例14: test_callability_checking
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def test_callability_checking():
"""
Test that the _repr_pretty_ method is tested for callability and skipped if
not.
"""
gotoutput = pretty.pretty(Dummy2())
expectedoutput = "Dummy1(...)"
nt.assert_equal(gotoutput, expectedoutput)
示例15: test_sets
# 需要導入模塊: from IPython.lib import pretty [as 別名]
# 或者: from IPython.lib.pretty import pretty [as 別名]
def test_sets():
"""
Test that set and frozenset use Python 3 formatting.
"""
objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]),
frozenset([1, 2]), set([-1, -2, -3])]
expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}',
'frozenset({1, 2})', '{-3, -2, -1}']
for obj, expected_output in zip(objects, expected):
got_output = pretty.pretty(obj)
yield nt.assert_equal, got_output, expected_output