• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • Lazy Python Argument Parsing

    November 2nd, 2022
    python, tech
    When I'm writing real Python programs I use argparse. When I'm writing quick scripts, I destructure sys.argv[1:]. For example:

    width, depth, height = sys.argv[1:]
    ...
    

    Or, if I need to import the file:

    def start(width, depth, height):
      ...
    
    if __name__ == '__main__':
      start(*sys.argv[1:])
    

    This is a very simple way of handling positional arguments. It ignores sys.argv[0] which is likely the name of the script, and then assigns the remaining arguments to the variables. If I provide the wrong number of arguments it's a bit shouty, but it's clear enough for a quick script and does remind me of the intended arguments:

    Traceback (most recent call last):
      File "example.py", line 2, in 
        width, depth, height = sys.argv[1:]
    ValueError: too many values to unpack (expected 3)
    

    Comment via: facebook, lesswrong

    Recent posts on blogs I like:

    How much time and money does an additional child take?

    Some things scale, others don't. The post How much time and money does an additional child take? appeared first on Otherwise.

    via Otherwise March 19, 2023

    What does Bing Chat tell us about AI risk?

    Early signs of catastrophic risk? Yes and no.

    via Cold Takes February 28, 2023

    Why Neighborhoods Should Have Speed Bumps

    I have several reasons I think why neighborhoods should have speed bumps. First, speed bumps are very useful to stop cars from hitting people in the streets. Second, when construction workers installed speed bumps on the street in front of our house it was v…

    via Lily Wise's Blog Posts February 27, 2023

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact