Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/misc/dev_tools.py
4036 views
1
r"""
2
Some tools for developers
3
"""
4
#*****************************************************************************
5
# Copyright (C) 2011 Nicolas M. Thiery <nthiery at users.sf.net>
6
#
7
# Distributed under the terms of the GNU General Public License (GPL)
8
# http://www.gnu.org/licenses/
9
#******************************************************************************
10
11
def runsnake(command):
12
"""
13
Graphical profiling with ``runsnake``
14
15
INPUT:
16
17
- ``command`` -- the command to be run as a string.
18
19
EXAMPLES::
20
21
sage: runsnake("list(SymmetricGroup(3))") # optional - requires runsnake
22
23
``command`` is first preparsed (see :func:`preparse`)::
24
25
sage: runsnake('for x in range(1,4): print x^2') # optional - requires runsnake
26
1
27
4
28
9
29
30
:func:`runsnake` requires the program ``runsnake``. Due to non
31
trivial dependencies (python-wxgtk, ...), installing it within the
32
Sage distribution is unpractical. Hence, we recommend installing
33
it with the system wide Python. On Ubuntu 10.10, this can be done
34
with::
35
36
> sudo apt-get install python-profiler python-wxgtk2.8 python-setuptools
37
> sudo easy_install RunSnakeRun
38
39
See the ``runsnake`` website for instructions for other platforms.
40
41
:func:`runsnake` further assumes that the system wide Python is
42
installed in ``/usr/bin/python``.
43
44
.. seealso::
45
46
- `The runsnake website <http://www.vrplumber.com/programming/runsnakerun/>`_
47
- ``%prun``
48
- :class:`Profiler`
49
50
"""
51
import cProfile, os
52
from sage.misc.misc import tmp_filename, get_main_globals
53
from sage.misc.preparser import preparse
54
tmpfile = tmp_filename()
55
cProfile.runctx(preparse(command.lstrip().rstrip()), get_main_globals(), locals(), filename=tmpfile)
56
os.system("/usr/bin/python -E `which runsnake` %s &"%tmpfile)
57
58
def import_statements(*objects, **options):
59
"""
60
Display import statements for the given objects
61
62
INPUT:
63
64
- ``*objects`` -- a sequence of objects
65
- ``lazy`` -- a boolean (default: True)
66
67
EXAMPLES::
68
69
sage: import_statements(WeylGroup, lazy_attribute)
70
from sage.combinat.root_system.weyl_group import WeylGroup
71
from sage.misc.lazy_attribute import lazy_attribute
72
73
If ``lazy`` is True, then :func:`lazy_import` statements are
74
displayed instead::
75
76
sage: import_statements(WeylGroup, lazy_attribute, lazy=True)
77
from sage.misc.lazy_import import lazy_import
78
lazy_import('sage.combinat.root_system.weyl_group', 'WeylGroup')
79
lazy_import('sage.misc.lazy_attribute', 'lazy_attribute')
80
81
.. todo::
82
83
This is not correct::
84
85
sage: import_statements(ZZ)
86
from sage.categories.euclidean_domains import EuclideanDomains.parent_class
87
88
This should be::
89
90
sage: import_statements(ZZ) # todo: not implemented
91
from sage.rings.integer_ring import ZZ
92
"""
93
lazy = "lazy" in options and options["lazy"]
94
if lazy:
95
print "from sage.misc.lazy_import import lazy_import"
96
for obj in objects:
97
if hasattr(obj, '__name__'):
98
name = obj.__name__
99
else:
100
name = obj
101
if lazy:
102
print "lazy_import('%s', '%s')"%(obj.__module__, name)
103
else:
104
print "from %s import %s"%(obj.__module__, name)
105
106
107