• 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:

    A Big Problem With The Going To Bed Book

    One day my dad was reading this book called the "Going to Bed Book" to my sister Nora. The book is basically about a bunch of animals who are getting ready for bed on a boat. They go down the stairs, take a bath, hang their towels on the wall, find…

    via Lily Wise's Blog Posts September 18, 2023

    Investing in boundaries with young kids

    Putting in some work to get the behavior you want The post Investing in boundaries with young kids appeared first on Otherwise.

    via Otherwise August 15, 2023

    Self-driving car bets

    This month I lost a bunch of bets. Back in early 2016 I bet at even odds that self-driving ride sharing would be available in 10 US cities by July 2023. Then I made similar bets a dozen times because everyone disagreed with me. The first deployment to pot…

    via The sideways view July 29, 2023

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact