Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Python equivalent of Julia's `@edit` macro?

In Julia, calling a function with the @edit macro from the REPL will open the editor and put the cursor at the line where the method is defined. So, doing this:

julia> @edit 1 + 1

jumps to julia/base/int.jl and puts the cursor on the line:

(+)(x::T, y::T) where {T<:BitInteger} = add_int(x, y)

As does the function form: edit(+, (Int, Int))

Is there an equivalent decorator/function in Python that does the same from the Python REPL?

like image 892
Jensun Ravichandran Avatar asked Nov 04 '20 20:11

Jensun Ravichandran


People also ask

How can I make Julia code in Python?

Julia codes can easily be made by converting C or Python codes. It is very difficult to make Python codes by converting C codes or the other way around. Julia arrays are 1-indexed, i.e. arrays start with 1-n not 0-n. It might cause a problem with programmers having habit of using other languages.

How to evaluate a macro in Julia?

Macro is one of the ways to evaluate the input expression and gives the resultant output expression. The process in the Julia language works as follows: it first parses and evaluates the macro, and the processed code produced by the macro is eventually evaluated like an ordinary expression. Macro can be referred to as a code of code.

Is Julia a good alternative to Python?

Julia started development in 2009 and was first released to the public as Julia 1.0 in 2012. Although it is yet to have a big fanbase as indicated by the TIOBE index, Python is the third most popular language after C and Java while Julia ranks somewhere in the 40s, it is still worth a visit.

How do I convert Julia data types to their Python equivalent?

In order to figure out the types to pass to an IAI function from the Python interface, you can refer to the equivalent function in the Julia API and translate the types to their Python equivalent. Most literal data types convert in a straighforward manner, for example:


1 Answers

Disclaimer: In the Python ecosystem, this is not the job of the core language/runtime but rather tools such as IDEs. For example, the ipython shell has the ?? special syntax to get improved help including source code.

Python 3.8.5 (default, Jul 21 2020, 10:42:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.18.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import random

In [2]: random.uniform??
Signature: random.uniform(a, b)
Source:
    def uniform(self, a, b):
        "Get a random number in the range [a, b) or [a, b] depending on rounding."
        return a + (b-a) * self.random()
File:      /usr/local/Cellar/[email protected]/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/random.py
Type:      method

The Python runtime itself allows viewing source code of objects via inspect.getsource. This uses a heuristic to search the source code as available; the objects themselves do not carry their source code.

Python 3.8.5 (default, Jul 21 2020, 10:42:08)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> print(inspect.getsource(inspect.getsource))
def getsource(object):
    """Return the text of the source code for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a single string.  An
    OSError is raised if the source code cannot be retrieved."""
    lines, lnum = getsourcelines(object)
    return ''.join(lines)

It is not possible to resolve arbitrary expressions or statements to their source; since all names in Python are resolved dynamically, the vast majority of expressions does not have a well-defined implementation unless executed. A debugger, e.g. as provided by pdb.set_trace(), allows inspecting the expression as it is executed.

like image 156
MisterMiyagi Avatar answered Oct 22 '22 21:10

MisterMiyagi