Skip to main content

Turns Python functions into CLI via Python annotations

Project description

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 (and only) intro

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

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

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

Just add:

from gearup import gearup

if __name__ == '__main__':
  gearup(train, test)()

and you are ready to go!

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

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 signature of the function and infers types of its arguments from the annotations:

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

Annotations here can be any callable of type str -> A, that raises either ValueError or TypeError when its argument is not a proper representation of any instance of type A.

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

Then, gear-uped function parses arguments using the following rules:

  • if = symbol is present in the argument: k=v, value v is assigned to the argument k and added to kwargs;
  • otherwise, the argument is treated as a positional one and appended to args.

After that the underlying function is called: f(*args, **kwargs), converting arguments in their respective types beforehand...

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

> python main.py 1 y=2

As a bonus, gearup.apply(f, *args, **kwargs) provides a Python-friendly way to do the same thing, which is useful when your script contains multiple methods with non-identical sets of parameters.

import gearup

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

def main(method: gearup.choice(method1, method2), x: int, **kwargs):
  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).

Misc.

Flags

As bool type behaves strangely in Python (e.g., bool('False') == True), annotation bool is automatically replaced by gearup.common.boolean, that parses strings that represent boolean values properly.

Help

Just add --help:

> python examples/main.py --help
Available commands:
train -> (method: {nonlogreg, logreg}, power: [-2, 5), alpha: float)   Trains method with alpha.
test -> slow -> (method: {nonlogreg, logreg})   Tests method...
        fast -> (method: {nonlogreg, logreg, inception})   Undocumented test function.

--help also works with commands:

> python examples/main.py test --help
Available commands:
slow -> (method: {logreg, nonlogreg})   Tests method...
fast -> (method: {logreg, inception, nonlogreg})   Undocumented test function.
> python examples/main.py test slow --help

  Tests method...

  A long
  several lines
  long
  description.

(method: {nonlogreg, logreg})

Non-standard types

gearup also defines several non-standard types:

  • choice(x_1, x_2, ..., x_n, k_1=v_1, k_2=v_2, ..., k_m=v_m) --- only accepts arguments from the provided set; for a keyword argument k=v, k is used to retrieve the value v, for a positional argument x x.__name__ as the key, and str(x) if __name__ attribute is absent; works nicely with functions, e.g. choice(function1, function2). Don't use with numbers as a single number has multiple string representations, e.g., choice(1, 2, 3) does not accept string ''01', use interval instead.
  • member[module] --- similar to choice, but retrieves elements from module.__all__ or dir(object) if __all__ is not defined. For example, given a module utils, member[utilss] allows to switch between functions defined in the module.
  • either[type_1, type_2, ..., type_n] --- tries to convert supplied value to one of the provided types; note, that type_i has priority over type_j if i < j, thus, e.g., either[float, int] is equivalent to float as any string representing int is also a valid float.
  • interval[a:b] --- half-open interval a <= x < b, type (int or float) is inferred from types of a and b; also a more complete constructor exists: interval(start, stop, left=True, right=False, cast=None).
  • a < number, a <= number, number < b, number <= b - an alternative syntax for constructing intervals, intervals can also be combined via &, e.g., (a < number) & (number < b) (note, that parenthesis are required as almost every operator has higher priority than comparison operators). Unfortunately, Python does not support overloading chained comparisons, thus, a nice a < number < b syntax is not available, however, (a < number) < b works fine.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gearup-0.1.2.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gearup-0.1.2-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file gearup-0.1.2.tar.gz.

File metadata

  • Download URL: gearup-0.1.2.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.6

File hashes

Hashes for gearup-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d118efda8a0ff4ba4e105cef8ac57ca03d4743ab11724b9e9dcfea127c170787
MD5 80d6b1e8d5f89e5e8346af701f92b531
BLAKE2b-256 594245478b3e0d284e9c4f7a265088372a404476e151bb876399c4cb07adb618

See more details on using hashes here.

File details

Details for the file gearup-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: gearup-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.6

File hashes

Hashes for gearup-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 543b368c4719c7d35d136cfb432e51531fa3bbc63a0b9b2426017073d4d12ddc
MD5 b3745a82cfb3a748f1ef6ff7b42b82bb
BLAKE2b-256 578c69f8e3be64cc989fada3008dc6714c3e38f56f31569932f527478eda5235

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page