• Stars
    star
    131
  • Rank 275,492 (Top 6 %)
  • Language
    Julia
  • License
    MIT License
  • Created over 5 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

It's editing-time, do you know where your methods are?

CodeTracking

Build status Coverage

CodeTracking can be thought of as an extension of Julia's InteractiveUtils library. It provides an interface for obtaining:

  • the strings and expressions of method definitions
  • the method signatures at a specific file & line number
  • location information for "dynamic" code that might have moved since it was first loaded
  • a list of files that comprise a particular package.

CodeTracking is a minimal package designed to work with Revise.jl (for versions v1.1.0 and higher). CodeTracking is a very lightweight dependency.

Examples

@code_string and @code_expr

julia> using CodeTracking, Revise

julia> print(@code_string sum(1:5))
function sum(r::AbstractRange{<:Real})
    l = length(r)
    # note that a little care is required to avoid overflow in l*(l-1)/2
    return l * first(r) + (iseven(l) ? (step(r) * (l-1)) * (l>>1)
                                     : (step(r) * l) * ((l-1)>>1))
end

julia> @code_expr sum(1:5)
[ Info: tracking Base
quote
    #= toplevel:977 =#
    function sum(r::AbstractRange{<:Real})
        #= /home/tim/src/julia-1/base/range.jl:978 =#
        l = length(r)
        #= /home/tim/src/julia-1/base/range.jl:980 =#
        return l * first(r) + if iseven(l)
                    (step(r) * (l - 1)) * l >> 1
                else
                    (step(r) * l) * (l - 1) >> 1
                end
    end
end

@code_string succeeds in that case even if you are not using Revise, but @code_expr always requires Revise. (If you must live without Revise, you can use Meta.parse(@code_string(...)) as a fallback.)

"Difficult" methods are handled more accurately with @code_expr and Revise. Here's one that's defined via an @eval statement inside a loop:

julia> @code_expr Float16(1) + Float16(2)
:(a::Float16 + b::Float16 = begin
          #= /home/tim/src/julia-1/base/float.jl:398 =#
          Float16(Float32(a) + Float32(b))
      end)

whereas @code_string cannot return a useful result:

julia> @code_string Float16(1) + Float16(2)
"# This file is a part of Julia. License is MIT: https://julialang.org/license\n\nconst IEEEFloat = Union{Float16, Float32, Float64}"

Consequently it's recommended to use @code_expr in preference to @code_string wherever possible.

@code_expr and @code_string have companion functional variants, code_expr and code_string, which accept the function and a Tuple{T1, T2, ...} of types.

@code_expr and @code_string are based on the lower-level function definition; you can read about it with ?definition.

Location information

julia> using CodeTracking, Revise

julia> m = @which sum([1,2,3])
sum(a::AbstractArray) in Base at reducedim.jl:648

julia> Revise.track(Base)  # also edit reducedim.jl

julia> file, line = whereis(m)
("/home/tim/src/julia-1/usr/share/julia/base/reducedim.jl", 642)

julia> m.line
648

In this (ficticious) example, sum moved because I deleted a few lines higher in the file; these didn't affect the functionality of sum (so we didn't need to redefine and recompile it), but it does change the starting line number of the file at which this method appears. whereis reports the current line number, and m.line the old line number. (For technical reasons, it is important that m.line remain at the value it had when the code was lowered.)

Other methods of whereis allow you to obtain the current position corresponding to a single statement inside a method; see ?whereis for details.

CodeTracking can also be used to find out what files define a particular package:

julia> using CodeTracking, Revise, ColorTypes

julia> pkgfiles(ColorTypes)
PkgFiles(ColorTypes [3da002f7-5984-5a60-b8a6-cbb66c0b333f]):
  basedir: /home/tim/.julia/packages/ColorTypes/BsAWO
  files: ["src/ColorTypes.jl", "src/types.jl", "src/traits.jl", "src/conversions.jl", "src/show.jl", "src/operations.jl"]

You can also find the method-signatures at a particular location:

julia> signatures_at(ColorTypes, "src/traits.jl", 14)
1-element Array{Any,1}:
 Tuple{typeof(red),AbstractRGB}

julia> signatures_at("/home/tim/.julia/packages/ColorTypes/BsAWO/src/traits.jl", 14)
1-element Array{Any,1}:
 Tuple{typeof(red),AbstractRGB}

CodeTracking also helps correcting for Julia issue #26314:

julia> @which uuid1()
uuid1() in UUIDs at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.1\UUIDs\src\UUIDs.jl:50

julia> CodeTracking.whereis(@which uuid1())
("C:\\Users\\SomeOne\\AppData\\Local\\Julia-1.1.0\\share\\julia\\stdlib\\v1.1\\UUIDs\\src\\UUIDs.jl", 50)

A few details

CodeTracking has limited functionality unless the user is also running Revise, because Revise populates CodeTracking's internal variables. (Using whereis as an example, CodeTracking will just return the file/line info in the method itself if Revise isn't running.)

CodeTracking is perhaps best thought of as the "query" part of Revise.jl, providing a lightweight and stable API for gaining access to information it maintains internally.

Limitations (without Revise)

  • parsing sometimes starts on the wrong line. Line numbers are determined by counting '\n' in the source file, without parsing the contents. Consequently quoted- or in-code '\n' can mess up CodeTracking's notion of line numbering
  • default constructor methods for structs are not found

More Repositories

1

Revise.jl

Automatically update function definitions in a running Julia session
Julia
1,201
star
2

ProgressMeter.jl

Progress meter for long-running computations
Julia
693
star
3

ProfileView.jl

Visualization of Julia profiling data
Julia
353
star
4

SnoopCompile.jl

Provide insights about latency (TTFX) for Julia packages
Julia
309
star
5

AdvancedScientificComputing

A short course on Julia and open-source software development
Jupyter Notebook
300
star
6

Rebugger.jl

An expression-level debugger for Julia with a provocative command-line (REPL) user interface
Julia
171
star
7

MethodAnalysis.jl

Utilities to analyze Julia's method tables
Julia
94
star
8

FlameGraphs.jl

Analysis of profiling data using trees
Julia
51
star
9

QuadDIRECT.jl

Global optimization without derivatives
Julia
50
star
10

Grid.jl

Interpolation and related operations on grids
Julia
46
star
11

Cpp.jl

Utilities for calling C++ from Julia
Julia
46
star
12

PkgCacheInspector.jl

Inspect the contents of Julia package cache files
Julia
39
star
13

ComputationalResources.jl

Julia package for selecting hardware, libraries, and algorithms for a computation
Julia
34
star
14

PositiveFactorizations.jl

Positive-definite "approximations" to matrices
Julia
32
star
15

FastAnonymous.jl

Fast "anonymous functions" for Julia
HTML
30
star
16

DebuggingUtilities.jl

Simple utilities for debugging julia code
Julia
25
star
17

Ratios.jl

Faster Rational-like types for Julia
Julia
23
star
18

ArrayIteration.jl

Testing new ideas for array iteration
Julia
20
star
19

AffineTransforms.jl

Computational geometry with affine transformations
Julia
19
star
20

PkgImages.jl

Prototypes for next-generation package caches in Julia
Julia
19
star
21

Cartesian.jl

Fast multidimensional algorithms
Julia
18
star
22

IProfile.jl

Profilers for Julia
Julia
18
star
23

MzXML.jl

Load mass spectrometry mzXML files
Julia
17
star
24

BasicBlockRewriter.jl

Code-regrouping to reduce latency in Julia code compilation
Julia
15
star
25

JuliaCon2022_Precompilation

Presentation for JuliaCon 2022 on precompilation
Julia
15
star
26

ANTsRegistration.jl

Convenience wrapper for image registration using the Advanced Normalization Tools
Julia
11
star
27

AggregateBy.jl

Aggregate collections by keys
Julia
11
star
28

MultilevelCoordinateSearch.jl

Global optimization without derivatives
Julia
11
star
29

MzPlots.jl

Plotting utilities for mass spectrometry data
Julia
10
star
30

ScheduleMeetings.jl

Julia
9
star
31

AxisAlgorithms.jl

Efficient filtering and linear algebra routines for multidimensional arrays
Julia
9
star
32

HalideCall.jl

Use shared libraries created by Halide from Julia
Julia
8
star
33

CallGraphs.jl

Analysis of source callgraphs for julia
Julia
7
star
34

RestrictProlong.jl

Efficient multigrid operators for Julia
Julia
7
star
35

HeaderREPLs.jl

Custom interactive REPL prompts that convey status
Julia
7
star
36

MzCore.jl

Traits and low-level utilities for mass spectrometry
Julia
6
star
37

IntroToJuliaWashU

A Jupyter notebook giving an overview of the Julia programming language
Jupyter Notebook
6
star
38

CoordinateSplittingPTrees.jl

Accurate and efficient full-degree multidimensional polynomial interpolation
Julia
6
star
39

KernelTools.jl

Fast kernel/stencil operations in Julia
Julia
6
star
40

NamedAxesArrays.jl

Performant arrays where each axis can be named
Julia
5
star
41

SymbolicLP.jl

Symbolic linear programming and linear constraints
Julia
5
star
42

Units.jl

Infrastructure for handling physical units for the Julia programming language
Julia
5
star
43

ThickNumbers.jl

Abstract type and utility functions for numbers that also act like sets/intervals
Julia
5
star
44

VTK.jl

Proof of concept VTK bindings for the Julia language
Julia
5
star
45

Layout.jl

Graphics layout management for Julia
Julia
3
star
46

TypeTreesIO.jl

Julia
3
star
47

IRCodeInterpreter.jl

A toy demonstration of interpreter speed for JuliaCon 2024
Julia
3
star
48

ArrayViewsAPL.jl

Generic array-view type with APL indexing semantics
Julia
2
star
49

HemirealNumbers.jl

Implementation of hemireal arithmetic for Julia
Julia
1
star
50

ZMQancient.jl

An archival version of ZMQ.jl for julia_matlab
Julia
1
star
51

MacroExpandJL.jl

Save the result of macro-expanded functions to Julia files
Julia
1
star
52

AssignGroups.jl

Assign students to collaborative groups
Julia
1
star
53

OptimizeQCQP.jl

Pure-julia quadratically-constrained quadratic programming solvers
Julia
1
star
54

HemirealFactorizations.jl

Matrix factorizations over the hemireals
Julia
1
star
55

ImagineFormat.jl

Read .imagine files in Julia
Julia
1
star
56

LinAlgHeaders.jl

Wrap headers for julia's linear algebra dependencies
Julia
1
star