跳转到主要内容

查找代码对象及其引用

项目描述

Codefind

  • 从文件名和qualname查找代码对象
  • 查找具有特定代码的所有函数
  • 更改函数的代码
  • jurigged 使用

find_code

from codefind import find_code

def f(x):
    return x + x

def adder(x):
    def f(y):
        return x + y

    return f

add1 = adder(1)

assert find_code("f" filename=__file__) is f.__code__

# Can find inner closures
assert find_code("adder", "f", filename=__file__) is add1.__code__

# Also works with module name
assert find_code("adder", "f", module=__module__) is add1.__code__

get_functions

from codefind import get_functions

def f(x):
    return x + x

def adder(x):
    def f(y):
        return x + y

    return f

add1 = adder(1)
add2 = adder(2)
add3 = adder(3)

assert get_functions(f.__code__) == [f]

# Finds all functions with the same code (in any order)
assert set(get_functions(add1.__code__)) == {add1, add2, add3}

conform

简单用法

from codefind import conform

def f(x):
    return x + x

def g(x):
    return x * x

print(f(5))  # 10
conform(f, g)
print(f(5))  # 25

更新所有闭包

def adder(x):
    def f(y):
        return x + y

    return f

def muller(x):
    def f(y):
        return x * y

    return f

add1 = adder(1)
add2 = adder(2)
add3 = adder(3)

print(add1(5))  # 6
print(add2(5))  # 7
print(add3(5))  # 8

conform(add1.__code__, muller(0).__code__)

print(add1(5))  # 5
print(add2(5))  # 10
print(add3(5))  # 15

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。

源代码分布

codefind-0.1.7.tar.gz (20.0 kB 查看散列)

上传时间: 源代码

构建分布

codefind-0.1.7-py3-none-any.whl (3.9 kB 查看散列)

上传时间: Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面