コード例 #1
0
def test_inline_list_decompile():
    d = Decompiler()
    x = [1, 1, 2., 3j, False, 3L, '3', u'3']
    d.ingest(x)
    ref = d.mgr.reference(x)
    result = "%s = %r" % (ref, x)
    assert d.render() == result
    _test_equality(d.render(), ref, x)
コード例 #2
0
def test_dict_decompile(arg):
    d = Decompiler()
    x = dict([('b', arg), ('a', arg)])
    d.ingest(x)
    ref = d.mgr.reference(x)
    result = "%s = { 'a': %r, 'b': %r }" % (ref, arg, arg)

    assert d.render() == result
    _test_equality(d.render(), ref, x)
コード例 #3
0
def test_name_hint():
    """In the absence of name conflicts, output ref should use name_hint"""
    x = [5, 5, 5, 5, 5, 5]
    d = Decompiler()
    d.ingest(x, name_hint='zzz')

    answer = "zzz = [5, 5, 5, 5, 5, 5]"
    result = d.render()

    assert answer == result
コード例 #4
0
def test_unsupported_type():
    """Raise type error when trying to ingest an unsupported type"""
    class Foo(object):
        pass
    d = Decompiler()

    with pytest.raises(TypeError) as exc:
        d.ingest(Foo())
    assert exc.value.args[0] == ("Don't know how to decompile objects of "
                                 "type %s" % type(Foo()))
コード例 #5
0
def test_numpy_decompile():
    import numpy as np
    x = np.array([1,2,3,4])
    s = x.dumps()
    d = Decompiler()
    d.ingest(x)

    ref = d.mgr.reference(x)
    answer = "import numpy as np\n%s = np.loads(%r)" % (ref, s)

    assert d.render() == answer
    exec(d.render())
    np.testing.assert_array_equal(locals()[ref],  x)
コード例 #6
0
def test_magic_method():
    """Decompiler looks for __expfac__ for expression factories"""
    class Foo(object):
        def __init__(self, x):
            self.x = x

        def __expfac__(self, decompiler, obj):
            return [Expression("Foo({{x}})", x=obj.x, output_ref = obj)]

    f = Foo(4)
    d = Decompiler()
    d.ingest(f)

    ref = d.mgr.reference(f)
    answer = "%s = Foo(4)" % ref
    result = d.render()

    assert result == answer
コード例 #7
0
ファイル: demo.py プロジェクト: ChrisBeaumont/mpl-decompile
""" Example use of the Decompiler

Try running this script like
python demo.py > out.py
python out.py

ISSUES:
-------
Axes limits are not being set correctly?
"""
import matplotlib.pyplot as plt
from decompiler import Decompiler

p, = plt.plot([1,2,3], [2,3,4], 'ro-', alpha = .3, markerfacecolor='b')
p.axes.set_xlim(-2, 20)
p.axes.set_ylim(-50, 50)
d = Decompiler()
d.ingest(p.figure)
print d.render()
print 'plt.show()'