GithubHelp home page GithubHelp logo

lazyopt's Introduction

lazyopt

pip install lazyopt

the lazy coder's option parser

most python scripts accumuluate a bunch of constants. eventually you might like to be able to set values for some of those constants with the command line. you could spend FOREVER writing an argument parser with argparse like this:

# main.py
import argparse

IT_WAS_A_CONST = 5
BUT_NOW_I_WANNA_CHANGE_IT = "damn"
UGH_I_HATE_THIS = True

def main():

  parser = argparse.ArgumentParser(description="some stupid script")
  parser.add_argument("--it-was-a-const", default=5,
                     dest='was_const')
  parser.add_argument("--but-now-i-wanna-change-it", default="damn",
                      dest='now_change')
  parser.add_argument("--ugh-i-hate-this", default=True,
                      dest='hate')

  args = parser.parse_args()
  IT_WAS_A_CONST = args.was_const
  BUT_NOW_I_WANNA_CHANGE_IT = args.now_change
  UGH_I_HATE_THIS = args.hate

  # FINALLY 
  do_what_you_came_for() 

if __name__ == "__main__" : main() 

just so you can run

python main.py --but-now-i-wanna-change-it 22    

with lazyopt, you can just do this!

# main.py
import lazyopt

IT_WAS_A_CONST = 5
BUT_NOW_I_WANNA_CHANGE_IT = "damn"
UGH_I_HATE_THIS = True

def main():

  lazyopt.apply_all() 
  do_what_you_came_for()

if __name__ == "__main__" : main()

and you can still run

python  main.py --BUT-NOW-I-WANNA-CHANGE-IT 22

how

lazyopt will parse any command in arguments like this:

--module_name.sub_module.var-name value-here

and apply value-here to the variable var_name in the module module_name.sub-module. if no such variable exists, you'll get a nice ConfigurationError to let you know. note that lazyopt converts dashes to underscores for you.

if you do not specify module_name , lazyopt will 'value-here to var_name in the module you use to call apply_all. most of the time this will be the main script you execute.

argument values are typecast where appropriate: "False", "True" , and "None" take on their keyword cousins. numbers are integers unless a decimal is present; everything else is a string.

if you pass arguments without values, like so:

--a-flag --another-flag

those argument names will be assigned the value 'True'

note that lazyopt does not currently provide documentation or enforce any rules. it's lazy like that.

warning

lazyopt is best for adding command line configuration to constants in a single script. it can work in more complicated situations, but you'll want to call lazyopt.apply_all in the python file you execute from the command line.

future plans

  • allow for capitalization correction where possible
  • add option to create actual arg parser
  • use comments as docs, default values for type checking
  • keep on keepin' on

why?

laziness

who?

mark neyer, gentleman coder

bsd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.