User Interface Functions

File: user_interface.py

#! /usr/bin/python3
# ===================================================================
# user interface functions
# (see the example code at the end of this file)
# ===================================================================

import os
import sys
import platform

# -------------------------------------------------------------------
# ---- running Python3?
# -------------------------------------------------------------------

def running_python3():
    if sys.version_info[0] == 3:
        return True
    return False

# -------------------------------------------------------------------
# ---- prompt the user for input
# -------------------------------------------------------------------

def get_user_input(prompt):
    return input(prompt).strip()

# -------------------------------------------------------------------
# ---- pause program
# -------------------------------------------------------------------

def pause():
    print()
    get_user_input('Press enter to continue ')

# -------------------------------------------------------------------
# ---- clear the terminal screen (window)
# -------------------------------------------------------------------

def clear_screen():
    if platform.system() == 'Linux':
        os.system('clear')
    elif platform.system() == 'Windows':
        os.system('clear')
    else:
        os.system('cls')

# -------------------------------------------------------------------
# ---- Function: convert a string to a float
# -------------------------------------------------------------------

def is_float(s):
    try:
        n = float(s)
        return (True,n)
    except:
        return (False,0.0)

# -------------------------------------------------------------------
# ---- Function: convert a string to an integer
# -------------------------------------------------------------------

def is_int(s):
    try:
        n = int(s)
        return (True,n)
    except:
        return (False,0)

def is_integer(s):
        return is_int(s)

# -------------------------------------------------------------------
# ---- is a number (integer, float, scientific notation)
# -------------------------------------------------------------------

def is_a_number(s):

    tf,n = is_int(s)
    if tf:
        return True

    tf,n = is_float(s)
    if tf:
        return True

    return False
 
# -------------------------------------------------------------------
# ---- query yes/no
# -------------------------------------------------------------------

def query_yes_no(p=' [yY]: '):

    ans = ui.get_user_input(p)

    if not ans:
        return False
        
    if ans[0] == 'y' or ans[0] == 'Y':
        return True 

    return False

# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------

if __name__ == '__main__':

    if not running_python3():
        print()
        print('Must run Python3 - exit program')
        print()
        sys.exit()

    while True:                # loop

        clear_screen()
        print()
        s = get_user_input('Enter data: ')
        if not s:              # empty string?
            break

        (tf,n) = is_int(s)
        if tf:
            print()
            print(f'You entered an integer {n}')
            pause()
            continue

        (tf,n) = is_float(s)
        if tf:
            print()
            print(f'You entered a float {n}')
            pause()
            continue

        print()
        print(f'You entered a string "{s}"')
        pause()

    print()