0

I have multiple scripts that calculates the different means given a list of numbers as arguments when calling the python script (https://github.com/alvations/meanie):

For example, harmonic_mean():

# -*- coding: utf-8 -*-

import math

def harmonic_mean(x): # x is a list of values.
  """ Returns the harmonic mean given a list of values. """
  return len(x) / sum([1/i for i in x])

def main(means):
  print "Input:", means
  means = map(float,means)
  print "harmonic mean = ", harmonic_mean(means)

if __name__ == '__main__':
  import sys
  if len(sys.argv) < 2:
    sys.stderr.write('Usage: python %s mean1 mean2 mean3 ... \n' % sys.argv[0])
    sys.exit(1)
  main(sys.argv[1:])

and root_mean_square()

# -*- coding: utf-8 -*-

import math

def root_mean_square(x): # x is a list of values.
  """ Returns the root mean square (rms) given a list of values. """
  return math.sqrt(sum([i*i for i in x])/len(x))

def main(means):
  print "Input:", means
  means = map(float,means)
  print "root mean square = ", root_mean_square(means)

if __name__ == '__main__':
  import sys
  if len(sys.argv) < 2:
    sys.stderr.write('Usage: python %s mean1 mean2 mean3 ... \n' % sys.argv[0])
    sys.exit(1)
  main(sys.argv[1:])

As you see, the two script does exactly the same thing but just calling different mean() functions and prints different outputs at the main(). How could I combine them and allow users to give a mean option parameter/argument when they call it at the console/terminal? E.g. the combined script is call meannie.py :

$ python meanie.py harmonic 1 2 3
$ python meanie.py hm 1 2 3 

The above command should give the same output as calling (this is how users call the harmonic_mean() script now):

$ python hm.py 1 2 3 

1 Answer 1

1

Python has first-class functions; they're objects like everything else and can be passed around in the same way. So you just need a way of mapping the user input onto the specified function and then pass it into your main function:

def harmonic_mean(x):
    ...

def root_mean_square(x):
    ...

MEAN_FUNCS_MAP = {
    'hm': harmonic_mean,
    'harmonic': harmonic_mean,
    'rms':  root_mean_square,
}

def main(mean_fn, means):
    print "Input:", means
    means = map(float,means)
    print "root mean square = ", mean_fn(means)    

if __name__ == '__main__':
    import sys
    if len(sys.argv) < 3 or sys.argv[1] not in MEAN_FUNCS_MAP:
        sys.stderr.write('Usage: python %s <function> <mean1> <mean2> <mean3> ... \n' % sys.argv[0])
        sys.exit(1)
    main( MEAN_FUNCS_MAP[sys.argv[1]], sys.argv[2:] )

You should possibly also take a look at the argparse module, it's a lot less error prone than rolling your own command line interface.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.