Python: A good way to get a function and all the dependencies in one file?

I am working on a large Python code base that grows, grows and grows. This is not one application - it’s more than a bunch of experiments that use common code.

Everyone so often, I want to make a public release of this experiment. I don’t want to release all of my terrible code base, just the parts needed to run this experiment. So basically I would like to scan something through the whole import and copy any functions (or at least all the imported modules) into one file, which I can release as a demo. Of course, I would like to do this only for files defined in the current project (non-dependent package, for example numpy).

Now I am using PyCharm and could not find this functionality. Is there any tool that does this?

Change . I created a public-release package to solve this problem. Given the main module, it looks at the dependent modules and copies them to a new repo.

+4
source share
5 answers

So, to solve our problem, I made a tool called public-release , which collects all the dependencies for the module you want to release, drops them into a separate repo using installation scripts and all, so that the code can be easily run later.

+1
source

- . , . ; , - (, - ), , - . .

. , , numpy. , numpy, , - , . , , , , .

. :

  • .
  • .

- . , , , . , , . ( SVN git), . , , . , , .

, , , , git . , , , .

- . . , , ; . , zip (export SVN archive git), ; .

, , setuptools, , . , . setup.py script , ; setup.py script , . script , , , . . PyPI, .

, setuptools, , .

+4

, sys.modules .

PyCharm, , , , . , export_func, to_export.py F10:

Macro action

, ,

from utils import factorize


def my_func():
    print(factorize(100))

utils.py,

import numpy as np
from collections import Counter
import sys
if sys.version_info.major >= 3:
    from functools import lru_cache
else:
    from functools32 import lru_cache


PREPROC_CAP = int(1e6)


@lru_cache(10)
def get_primes(n):
    n = int(n)
    sieve = np.ones(n // 3 + (n % 6 == 2), dtype=np.bool)
    for i in range(1, int(n ** 0.5) // 3 + 1):
        if sieve[i]:
            k = 3 * i + 1 | 1
            sieve[k * k // 3::2 * k] = False
            sieve[k * (k - 2 * (i & 1) + 4) // 3::2 * k] = False
    return list(map(int, np.r_[2, 3, ((3 * np.nonzero(sieve)[0][1:] + 1) | 1)]))


@lru_cache(10)
def _get_primes_set(n):
    return set(get_primes(n))


@lru_cache(int(1e6))
def factorize(value):
    if value == 1:
        return Counter()
    if value < PREPROC_CAP and value in _get_primes_set(PREPROC_CAP):
        return Counter([value])
    for p in get_primes(PREPROC_CAP):
        if p ** 2 > value:
            break
        if value % p == 0:
            factors = factorize(value // p).copy()
            factors[p] += 1
            return factors
    for p in range(PREPROC_CAP + 1, int(value ** .5) + 1, 2):
        if value % p == 0:
            factors = factorize(value // p).copy()
            factors[p] += 1
            return factors
    return Counter([value])

my_func F10, to_export.py:

from utils import factorize


def my_func():
    print(factorize(100))

factorize to_export.py F10

from collections import Counter
from functools import lru_cache

from utils import PREPROC_CAP, _get_primes_set, get_primes


def my_func():
    print(factorize(100))


@lru_cache(int(1e6))
def factorize(value):
    if value == 1:
        return Counter()
    if value < PREPROC_CAP and value in _get_primes_set(PREPROC_CAP):
        return Counter([value])
    for p in get_primes(PREPROC_CAP):
        if p ** 2 > value:
            break
        if value % p == 0:
            factors = factorize(value // p).copy()
            factors[p] += 1
            return factors
    for p in range(PREPROC_CAP + 1, int(value ** .5) + 1, 2):
        if value % p == 0:
            factors = factorize(value // p).copy()
            factors[p] += 1
            return factors
    return Counter([value])

PREPROC_CAP, _get_primes_set get_primes   F10

from collections import Counter
from functools import lru_cache

import numpy as np


def my_func():
    print(factorize(100))


@lru_cache(int(1e6))
def factorize(value):
    if value == 1:
        return Counter()
    if value < PREPROC_CAP and value in _get_primes_set(PREPROC_CAP):
        return Counter([value])
    for p in get_primes(PREPROC_CAP):
        if p ** 2 > value:
            break
        if value % p == 0:
            factors = factorize(value // p).copy()
            factors[p] += 1
            return factors
    for p in range(PREPROC_CAP + 1, int(value ** .5) + 1, 2):
        if value % p == 0:
            factors = factorize(value // p).copy()
            factors[p] += 1
            return factors
    return Counter([value])


PREPROC_CAP = int(1e6)


@lru_cache(10)
def _get_primes_set(n):
    return set(get_primes(n))


@lru_cache(10)
def get_primes(n):
    n = int(n)
    sieve = np.ones(n // 3 + (n % 6 == 2), dtype=np.bool)
    for i in range(1, int(n ** 0.5) // 3 + 1):
        if sieve[i]:
            k = 3 * i + 1 | 1
            sieve[k * k // 3::2 * k] = False
            sieve[k * (k - 2 * (i & 1) + 4) // 3::2 * k] = False
    return list(map(int, np.r_[2, 3, ((3 * np.nonzero(sieve)[0][1:] + 1) | 1)]))

, , .

+4

, Python . (, , .)

, , .

PyCharm . vulture .

, . .

+1

PyCharm , , - Refactor → Copy (F6 , , ). ( ) . .

0

Source: https://habr.com/ru/post/1660094/


All Articles