ring_multi_obj_types.py

#! /usr/bin/python3
# ==============================================================
# Demonstrate Ring Data Structure With Multiple Object Types
# ==============================================================

import ring
import math

################################################################
# ---- support functions ---------------------------------------

def get_user_input(prompt):
    '''
    Prompt the user and return their input
    '''
    return input(prompt)

def pause_program():
    '''
    pause the program until the user is ready
    '''
    get_user_input('\nPress enter to continue: ')

def display_obj(obj,msg=''):
    '''
    Display object information
    '''
    if msg: print(msg)
    print('obj {}  ({})'.
        format('None' if not obj else obj.data),type(obj.data))

def return_obj_data(obj):
    '''
    Return data from an object
    '''
    print('return_obj_data({}'.
    format('None' if not obj else obj.data))
    return 'None' if not obj else obj.data

def match_obj(obj,val):
    '''
    Match object data
    '''
    ##print('match_obj({},{})'.format(obj.data,val))
    return True if obj.data == val else False

################################################################

# ---- test object classes

class testobjint():                          # integer data

    def __init__(self,data):
        self.data = data

class testobjflt():                          # float data

    def __init__(self,data):
        self.data = data

class testobjstr():                          # string data

    def __init__(self,data):
        self.data = data

# ---- display object

def display_obj(obj,msg=''):
    if msg: print(msg)
    print('obj {}   ({})'.format(
        'None' if not obj else obj.data,
        type(obj.data)))

def return_obj_data(obj):
    if obj == 'None':
        return 'None'
    elif obj.data == None:
        return 'None'
    elif type(obj.data) is float:
        return '{:.2f}    (float)'.format(obj.data)
    elif type(obj.data) is int:
        return '{}    (int)'.format(obj.data)
    elif type(obj.data) is str:
        return obj.data + '    (str)'
    elif obj.data == None:
        return 'None'
    else:
        return 'Data type is unknown ' + str(type(obj.data))
    ######None' if not obj else obj.data

#???????????????????????????????????????????????????????????????
# ---- some special tests --------------------------------------
#
#objx = testobj(333)
#
#rng = r.Ring()
#
#print('---- empty ring (no beads) ----------------')
#display_obj(rng.fetch())
#print('---- delete current bead ------------------')
#o = rng.delete()
#print('True' if o else 'False')
#print('---- display ring -------------------------')
#rng.display_ring(return_obj_data)
#
#print('\n')
#
#print('---- ring with one bead -------------------')
#print('---- insert obj (333) ---------------------')
#rng.insert(objx)
#display_obj(rng.fetch())
#print('---- display ring -------------------------')
#rng.display_ring(return_obj_data)
#print('---- delete current bead ------------------')
#o = rng.delete()
#print('True' if o else 'False')
#print('---- display ring -------------------------')
#rng.display_ring(return_obj_data)
#
#quit()
#???????????????????????????????????????????????????????????????

# ---- create test objects

obj1 = testobjint(100)                  # integer
obj2 = testobjflt(200.00)               # float
##obj3 = testobjstr('300')              # string
obj3 = testobjstr(None)                 # Oops, bad user object?
obj4 = testobjint(400)                  # integer
obj5 = testobjstr(math.pi)              # float
obj6 = testobjflt('150')                # string
obj7 = testobjint(250)                  # integer

# ---- create ring

rng = ring.Ring()

# --------------------------------------------------------------
# ---- insert and display some objects

print('---- insert obj -------')
rng.insert(obj1)
display_obj(rng.fetch(),'--- cur obj ----------')

print('---- insert obj -------')
rng.insert(obj2)
print('---- forward ----------')
rng.forward()
display_obj(rng.fetch(),'--- cur obj ----------')

print('---- backward ---------')
rng.backward()
display_obj(rng.fetch(),'--- cur obj ----------')

print('---- insert obj -------')
rng.insert(obj3,False)
print('---- backward ---------')
rng.backward()
display_obj(rng.fetch(),'--- cur obj ----------')

print('\n------------------------------------------')
print(' display ring')
print('------------------------------------------\n')
rng.display_ring(return_obj_data)

print('\n------------------------------------------')
print(' insert more objects and display the ring')
print(' display the modified ring')
print('------------------------------------------\n')

rng.insert(obj4)
rng.insert(obj5,False)
rng.display_ring(return_obj_data)

print('\n------------------------------------------')
print(" delete the ring's current bead")
print(" display the modified ring")
print('------------------------------------------\n')

rng.delete()
rng.display_ring(return_obj_data)

print('\n------------------------------------------')
print(" search the ring for an object")
print('------------------------------------------')

while True:

    val = get_user_input('\nEnter obj integer value: ')

    sval = val.strip()

    if sval == '': break

    if sval.isdigit() != True:
        print('\nIllegal value entered ({})'.format(sval))
        pause_program()
        continue

    ival = int(sval)

    tf = rng.locate(ival,match_obj)

    if tf:
        print('\nFound the object {} in the ring'
            .format(ival))
        print("It is now the ring's current bead")
        display_obj(rng.fetch(),'\n--- cur obj -----')
    else:
        print('\nDid not find the object {} in the ring'
            .format(ival))
        print("The ring's current bead did not change")
        display_obj(rng.fetch(),'\n--- cur obj -----')