Skip to content

Instantly share code, notes, and snippets.

@gamesbook
Created October 16, 2017 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamesbook/a15dc45965da35ff56b251778a07c9d1 to your computer and use it in GitHub Desktop.
Save gamesbook/a15dc45965da35ff56b251778a07c9d1 to your computer and use it in GitHub Desktop.
Convert imperial units (feet, inches) to metres
# -*- coding: utf-8 -*-
""" Purpose: Convert imperial units (feet, inches) to metres
Created: 2017-10-17
Author: dhohls@csir.co.za
Notes:
This is quite useful if you have a house plan where all the units are
Imperial (typically American or old European plans) and you need to
compare against modern plans.
Usages::
python feet2metres.py -n 3 -m 5
python feet2metres.py -i
"""
from __future__ import print_function
import argparse
__author__ = "Derek Hohls"
__license__ = "GPL"
__version__ = "1.0.1"
MIN = 5
MAX = 24
PRECISION = 1
def reformat(feet, inches, metres):
_feet = str(feet).rjust(2, ' ')
_inches = str(inches).rjust(2, ' ')
_metres = str(round(metres, PRECISION))
return _feet, _inches, _metres
def main(args):
minimum = args.min or MIN
maximum = args.max or MAX
interactive = args.interactive
#print (minimum, maximum, interactive)
if not interactive:
for feet in range(int(minimum), int(maximum)):
for inches in range(0, 12):
metres = feet * 0.3048 + inches * 0.0254
_feet, _inches, _metres = reformat(feet, inches, metres)
print('%s`%s" = %sm' % (_feet, _inches, _metres))
else:
imperial = None
while True:
imperial = raw_input('Enter imperial value (feet.inches): ')
if imperial and imperial in ['q', 'Q']:
break
try:
if '.' in str(imperial):
feet, inches = str(imperial).split('.')
else:
feet = str(imperial)
inches = '0'
metres = float(feet) * 0.3048 + float(inches) * 0.0254
_feet, _inches, _metres = reformat(feet, inches, metres)
print('%s`%s" = %sm' % (_feet, _inches, _metres))
except Exception as err:
pass
if __name__ == '__main__':
PARSER = argparse.ArgumentParser()
PARSER.add_argument(
'-i', '--interactive', default=False, action='store_true',
help="If set, then run in interactive mode (q to quit)")
PARSER.add_argument(
'-n', '--min',
help="Specify the minimum number of feet (default: 5)")
PARSER.add_argument(
'-m', '--max',
help="Specify the maximum number of feet (default: 24)")
PARSER.add_argument(
'-ll', '--loglevel', default='WARNING',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help="Set log level for service (default: WARNING)")
ARGS = PARSER.parse_args()
LOG_LEVEL = ARGS.loglevel
main(ARGS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment