Skip to main content

GearUp

Have you ever had a moment, when the code is ready, you are eager to launch it, you want to know if your new and shiny method works or not, just to realize you need to write 100+ lines of argparse or click?

Gear up and get ready to go!

Quick intro

Assume your project contains main.py script with the following functions:

### main.py

def train(method: str, dataset: str, alpha: float):
  <do stuff>

def test_accuracy(method: str, dataset: str):
  <do testing>

def test_robustness(method: str):
  <do testing>

Just add:

if __name__ == '__main__':
  from gearup import gearup
  gearup(train, test=dict(accuracy=test_accuracy, robustness=test_robustness))()

and you are ready to go!

> python main.py train method=resnet dataset=mnist alpha=0.01
> python main.py test accuracy method=resnet dataset=mnist
> python main.py test robustness method=resnet

Installation

As usual:

pip install gearup

or

pip install git+https://gitlab.com/craynn/gearup.git

How it works

gearup, applied to a function, reads the signature of the function and infers which arguments should be passed to it:

def f(x: int, y: int):
  return x + y

When gear-uped function is called without arguments it reads sys.argv, alternatively, it can be called with a list of strings:

gearup(f)(['1', '2']) ### result = 3
gearup(f)() ### read from console arguments

gearup relies on omegaconf and PyYAML:

  • arguments are converted according to YAML rules, e.g., 1 is parsed as an integer, 1.0 --- as a float, '1' --- as a string;
  • keyword arguments, like classifier.alpha=1.0, are parsed as a dot-list;
  • positional arguments (ones that do not contain = sign) are always passed to the function.

After that the underlying function is called: f(*args, **kwargs), passing only the arguments required by the function...

Yes, no flags, no aliases, just launch script like a python function (Haskell style)...

> python main.py 1 y=2

Notes:

  • spaces should not appear between argument name, = and argument value:
    • a=x sets value of argument a to x;
    • a = x is interpreted as three separate arguments: two positional: a and x, and a keyword one (with empty name and value);
  • if you need to supply a value with a space character in it, use quotes: python main.py x='a b c';
  • if you need to supply a value with = character in it, just specify argument name: python main.py x=a=b or, better, python main.py x='a=b';
  • it is impossible to set one of variational positional arguments (*args) to a value, that contains = character;
  • lists are valid values: python main.py x=[1,2,3,4] or (in case you want to add spaces) python main.py 'x=[1, 2, 3, 4]'.

As a bonus, gearup.apply(f)(*args, **kwargs) provides a Python-friendly way to pass down a subconfig.

import gearup

def method1(x: int, y: int): return x + y
def method2(x: int, z: float): return x / z

def main(method, x: int, **kwargs):
  if method == 'method1':
    method = method1
  elif method == 'method2':
    method = method2
  else:
    raise ValueError()

  gearup.apply(method, x, **kwargs)

if __name__ == '__main__':
  gearup.gearup(main)()

Commands

Sometimes you need to pack several functions into one script:

gearup(train, test)()
### or
gearup(train=train, test=test)()
### or
gearup(train, test=test)()
> python main.py train <arguments for train>
> python main.py test <arguments for test>

More precisely, if supplied with more than one argument or at least one keyword argument, gearup consumes the first CLI argument and switches between provided functions.

Bonus: it is recursive!

def train(...): pass
def test_fast(...): pass
def test_slow(...): pass

gearup(
  train,
  test=dict(
    fast=test_fast,
    slow=test_slow
  )
)()
> python main.py train method=resnet alpha=0.1
> python main.py test slow method=resnet

Note: when a non-keyword argument is passed to gearup, it reads __name__ attribute of this argument. For example, gearup(f1, f2) is equivalent to gearup(f1=f1, f2=f2).

Help

Just add help:

> python examples/main.py help
Available commands:
train -> (method, power, alpha: float = 0.001, flag: bool = False)
         Trains method with alpha.
test -> a -> (method)
             Tests method...
        b -> (method)
             Undocumented test function.

The helper keyword can be overriden (or turned off) with:

gearup(...).without_helper()
gearup(...).with_helper(helper=None)
gearup(...).with_helper(helper='helpme')

Configuration file

gearup() can be supplied with a configuration file.

gearup(...).with_config('/path/to/config.yaml')

The config will be merged with CLI arguments, with the latter having priority:

### config.yaml

point:
  x: 1
  y: 4
### main.py

def norm(point):
  import math
  return math.sqrt(point['x'] ** 2 + point['y'] ** 2)

if __name__ == '__main__':
  import gearup
  result = gearup(norm).with_config('config.yaml')()
  print(result)
> python main.py
4.123105625617661
> python main.py point.x=3
5.0

Metadata

Release files for gearup 1.1.1

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

Source distribution (sdist)

Source distribution for gearup 1.1.1
File Size Uploaded Provenance Actions
gearup-1.1.1.tar.gz 12.5 kB Without provenance Details for gearup-1.1.1.tar.gz

Built distribution (wheel)

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

Total release size: 22.1 kB

Release files / gearup-1.1.1.tar.gz

Download URL gearup-1.1.1.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
05450503e001e3486eae90ba7c6e432611e0a4101d3e2c33e11209038118de95
BLAKE2b-256 checksum
How to use checksums
c7b9c5d7de1345e00cb7c6e02973b3489ad5c2e3c7e87daf2865be07bb65dccb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / gearup-1.1.1-py3-none-any.whl

Download URL gearup-1.1.1-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
42909ab6afe9b606cbbe851f1f817890393c71ae7a872d724a74359632044488
BLAKE2b-256 checksum
How to use checksums
b0eb1467792edccc17f641e4d713db8557edcf637aaa6542e60d3a15054001b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for gearup 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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