Skip to content

Instantly share code, notes, and snippets.

@kaizu
Last active May 23, 2017 06:30
Show Gist options
  • Save kaizu/43aa7fecc720451d72dda4dcf50bf565 to your computer and use it in GitHub Desktop.
Save kaizu/43aa7fecc720451d72dda4dcf50bf565 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This is a module for mathematical utility functions.
"""
from __future__ import print_function # This is for Python2
__author__ = "author"
__version__ = "1.0.0"
__copyright__ = "Copyright 2017, Planet Earth"
__license__ = "GPLv2"
__email__ = "mail@example.com"
__status__ = "Development" # "Prototype", "Development", or "Production"
__all__ = ["product"]
from logging import getLogger, StreamHandler, Formatter
_log = getLogger(__name__)
handler = StreamHandler()
formatter = Formatter(fmt='%(levelname)s:%(name)s:%(message)s')
handler.setFormatter(formatter)
from logging import DEBUG
lv = DEBUG # This is a log level: CRITICAL, ERROR, WARNING, INFO, or DEBUG
handler.setLevel(lv)
_log.setLevel(lv)
_log.addHandler(handler)
import functools
def product(values):
"""
Calculate the product of given integers.
Parameters
----------
values : iterable
A list of integers
Returns
-------
res : int
A product
Examples
--------
>>> product([])
1
>>> product([2])
2
>>> product([2, 3, 4, 5])
120
"""
_log.info("function 'product' was called with values={}.".format(str(values)))
res = functools.reduce(lambda a, b: a * b, values, 1)
_log.info("a result is {:d}.".format(res))
return res
def _main():
import platform
_log.info("the version is {}.".format(platform.python_version()))
(major, minor, _) = platform.python_version_tuple()
if major != '3':
raise RuntimeError("The version is too old '{}'. Use Python3.".format(platform.python_version()))
elif int(minor) < 5:
_log.warn("the version is a bit old '{}'. Use Python3.5 or later.".format(platform.python_version()))
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='*', help='an integer for the product')
args = parser.parse_args()
print(product(args.integers))
if __name__ == "__main__":
_main() # Do nothing except for calling _main
@kaizu
Copy link
Author

kaizu commented May 23, 2017

$ python product.py --help
$ python product.py 1 2 3
$ python -m doctest -v product.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment