Commits

Markus Zapke-Gründemann  committed 20114ea

Initial commit.

  • Participants

Comments (0)

Files changed (4)

+Copyright (c) 2012, Markus Zapke-Gründemann
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice,
+      this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in the
+      documentation and/or other materials provided with the distribution.
+
+Neither the name of the authors nor the names of its contributors may be used
+to endorse or promote products derived from this software without specific
+prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+**********
+hours2days
+**********
+
+A simple command line tool to convert hours to days.
+
+Usage
+=====
+
+You can either use the command line tool or the Python function.
+
+::
+
+    $ ./hours2days.py 2
+    0.25
+    $ ./hours2days.py 8
+    1.0
+    $ ./hours2days.py 24 30
+    3.06
+    $ ./hours2days.py 24 -b 6
+    4.0
+    $ ./hours2days.py 14 27 -p 0
+    1.80625
+
+::
+
+    >>> from hours2days import hours2days
+    >>> hours2days(2)
+    0.25
+    >>> hours2days(8)
+    1.0
+    >>> hours2days(24, 30)
+    3.0625
+    >>> hours2days(24, base=6)
+    4.0
+    >>> hours2days(12, 45)
+    1.59375
+    >>> hours2days(24, 30, precision=2)
+    3.06
+    >>> hours2days(16, 15, base=4, precision=3)
+    4.063

File hours2days.py

+#!/usr/bin/env python
+"""A simple command line tool to convert hours to days."""
+
+
+def hours2days(hours, minutes=0, base=8, precision=0):
+    """Converts hours (and minutes) to days.
+
+    Use the base argument to define the length of a day.
+
+    >>> hours2days(2)
+    0.25
+    >>> hours2days(8)
+    1.0
+    >>> hours2days(16)
+    2.0
+    >>> hours2days(24, base=6)
+    4.0
+    >>> hours2days(24, 30)
+    3.0625
+    >>> hours2days(12, 45)
+    1.59375
+    >>> hours2days(24, 30, precision=2)
+    3.06
+    >>> hours2days(16, 15, base=4, precision=3)
+    4.063
+    """
+    base = float(base)
+    days = (hours + minutes / 60.) / base
+    if precision:
+        days = round(days, precision)
+    return days
+
+
+if __name__ == '__main__':
+    import sys
+    try:
+        import argparse
+    except ImportError:
+        sys.stderr.write('Your Python version has no argparse module.\n')
+        sys.stderr.write('Please run "pip install argparse" to install it\n')
+        sys.stderr.write('or upgrade your Python installation\n')
+        sys.exit()
+
+    description = """Converts hours (and minutes) to days.
+
+    Use --base to define the length of a day.
+
+    Set --precision to 0 to disable rounding.
+    """
+    parser = argparse.ArgumentParser(description=description,
+        formatter_class=argparse.RawDescriptionHelpFormatter)
+    parser.add_argument('HOURS', type=int, help='number of hours')
+    parser.add_argument('MINUTES', type=int, nargs='?', default=0,
+        help='number of minutes (optional)')
+    parser.add_argument('-b', '--base', type=int, default=8,
+        help='length of a day in hours (default: %(default)d)')
+    parser.add_argument('-p', '--precision', type=int, default=2,
+        help='round to given precision (default: %(default)d)')
+    args = parser.parse_args()
+    out = hours2days(args.HOURS, args.MINUTES, args.base, args.precision)
+    sys.stdout.write(str(out) + '\n')
+"""Runs the test suite."""
+import doctest
+
+import hours2days
+
+if __name__ == '__main__':
+    doctest.testmod(hours2days)