Skip to main content

mvdef

Documentation CI Status Coverage Checked with mypy Code style: black

Package providing command line tools to move/copy function/classes and their associated import statements between files

Read The Docs

Requires

  • Python 3.10+

Installation

pip install mvdef

Usage

mvdef

Moves functions named by -m/--mv and their associated imports from src to dst, or just previews the changes as a diff if passed -d/--dry-run.

usage: mvdef [-h] -m [MV ...] [-d] [-e] [-c] [-f] [-v] src dst

  Move function definitions from one file to another, moving/copying
  any necessary associated import statements along with them.

  Option     Description                                Type        Default
  —————————— —————————————————————————————————————————— ——————————— ———————
• src        source file to take definitions from       Path        -
• dst        destination file (may not exist)           Path        -
• mv         names to move from the source file         list[str]   -
• dry_run    whether to only preview the change diffs   bool        False
• escalate   whether to raise an error upon failure     bool        False
• cls_defs   whether to use only class definitions      bool        False
• func_defs  whether to use only function definitions   bool        False
• verbose    whether to log anything                    bool        False

positional arguments:
  src
  dst

options:
  -h, --help            show this help message and exit
  -m [MV ...], --mv [MV ...]
  -d, --dry-run
  -e, --escalate
  -c, --cls-defs
  -f, --func-defs
  -v, --verbose

cpdef

Copies functions named by -m/--mv and their associated imports from src to dst, or just previews the changes as a diff if passed -d/--dry-run.

Has the same flags and signature as mvdef, but never changes src.

usage: cpdef [-h] -m [MV ...] [-d] [-e] [-c] [-f] [-v] src dst

  Copy function definitions from one file to another, and any necessary
  associated import statements along with them.

  Option     Description                                Type        Default
  —————————— —————————————————————————————————————————— ——————————— ———————
• src        source file to copy definitions from       Path        -
• dst        destination file (may not exist)           Path        -
• mv         names to copy from the source file         list[str]   -
• dry_run    whether to only preview the change diffs   bool        False
• escalate   whether to raise an error upon failure     bool        False
• cls_defs   whether to use only class definitions      bool        False
• func_defs  whether to use only function definitions   bool        False
• verbose    whether to log anything                    bool        False

positional arguments:
  src
  dst

options:
  -h, --help            show this help message and exit
  -m [MV ...], --mv [MV ...]
  -d, --dry-run
  -e, --escalate
  -c, --cls-defs
  -f, --func-defs
  -v, --verbose

lsdef

Has a similar signature, but no dst (it operates on just one file) and the mv argument is replaced by match, which can specify regular expressions (default * matches any name).

usage: lsdef [-h] [-m [MATCH ...]] [-d] [-l] [-e] [-c] [-f] [-v] src

  List function definitions in a given file.

  Option     Description                                Type        Default
  —————————— —————————————————————————————————————————— ——————————— ———————
• src        source file to list definitions from       Path        -
• match      name regex to list from the source file    list[str]   ['*']
• dry_run    whether to print the __all__ diff          bool        False
• list       whether to print the list of names         bool        False
• escalate   whether to raise an error upon failure     bool        False
• cls_defs   whether to use only class definitions      bool        False
• func_defs  whether to use only function definitions   bool        False
• verbose    whether to log anything                    bool        False

positional arguments:
  src

options:
  -h, --help            show this help message and exit
  -m [MATCH ...], --match [MATCH ...]
  -d, --dry-run
  -l, --list
  -e, --escalate
  -c, --cls-defs
  -f, --func-defs
  -v, --verbose

How it works

The structure of a mvdef invocation

When you call mvdef foo.py bar.py -d -c -m A, equivalent to:

mvdef foo.py bar.py --dry-run --cls-defs --mv A

You're requesting to show the file diffs it'd take (--dry-run) to move the class definition (--cls-defs) named A (--mv A) from foo.py (the src, first positional argument) to bar.py (the dst, second positional argument).

Parsing the request

The request to move a definition is stored on a dataclass mvdef.transfer.MvDef immediately upon invoking the program command.

Upon creation, this class stores 2 attributes src_diff and dst_diff (both are mvdef.diff.Differ objects) which will coordinate the creation of patches, or 'diffs'. These start their life with empty agendas (mvdef.agenda.Agenda).

Next, the main MvDef class calls its check() method, which returns an exception (or raises it if escalate is True), preventing further work if the source file does not contain a class named A as requested.

If the source file has the required definitions to fulfil the request, then the MvDef.diffs() method gets called next, populates the empty agendas on the Differ objects for the 2 files, then produces the diffs they imply.

If the dry run setting is not used, the source and destination files are overwritten with the changes, instead of just displaying the diffs.

lsdef approach

lsdef is similar, but instead of making a src_diff it makes a src_manifest (mvdef.manifest.Manifest object), and there is no dst file to handle.

Error handling is the same as above.


mvdef is available from PyPI, and the code is on GitHub

Metadata

Release files for mvdef 0.9.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mvdef 0.9.4
File Size Uploaded
mvdef-0.9.4.tar.gz 411.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mvdef 0.9.4
File Interpreter ABI Platform
mvdef-0.9.4-py3-none-any.whl Python 3 none any Details

Total release size: 821.4 kB

Release files / mvdef-0.9.4.tar.gz

Download URL mvdef-0.9.4.tar.gz
Size 411.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1f7ed65276eecb6a448aace12932dfd74b2bd72e1c95f03a30a529fac985d25f
BLAKE2b-256 checksum
How to use checksums
742afd7d63bdd6e375e6d42dfba5717dbd167edcea69085a0e3a91b169f0cf3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.12.4 CPython/3.10.6

Release files / mvdef-0.9.4-py3-none-any.whl

Download URL mvdef-0.9.4-py3-none-any.whl
Size 410.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e17fe8ad1a182da281585e62577b127037d59c22757134169b52011cd73df048
BLAKE2b-256 checksum
How to use checksums
d9d998817d1d0eba6ff97bba0c1d035dcb3cd69b25307d3adae6d1249c1ca131
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.12.4 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.9.4 This release

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.9

2 release files

0.5.8

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page