#!/usr/bin/python3
################################################################################
# Perform analytics on....
# 1. a single dimension list
# 2. Two dimensional list (row oriented analytics)
# 3. Two dimensional list (column oriented analytics)
################################################################################
import statistics

print("Content-Type: text/html \n")                     #required http response header
print('<br>')

list1 = [5.0,6.5,-44,0,88,102,216,131,-55,67,0.6,0,13,99,150,67,0,32.3]

list2 = [[1.0,6.5,-44,0.0,88,102,216,131,-55,67,0.6,0.0,13,99,150,67,0.0,32.3],
         [2.0,7.5,-45,0.0,89,103,217,132,-56,68,0.7,0.0,14,98,151,68,1.0,32.4],
         [3.0,8.5,-46,0.0,90,104,218,133,-57,69,0.8,0.0,15,99,152,66,0.0,32.5],
         [4.0,9.5,-47,0.0,91,105,219,134,-58,70,0.9,0.0,16,99,153,70,0.0,32.6],
         [5.0,4.5,-48,0.0,92,106,220,135,-59,71,0.4,0.0,17,99,154,71,0.0,32.7],
         [6.0,3.5,-49,0.0,93,107,221,136,-60,72,0.3,0.0,18,99,155,72,0.0,32.8]]

### Function to perform analytics #################
def analytics(list):                                    #produce analytics on a list of numbers
    analytics = {}                                      #create an empty dictionary
    analytics['count']  = len(list)
    analytics['sum']    = sum(list)
    analytics['avg']    = statistics.mean(list)
    analytics['min']    = min(list)
    analytics['max']    = max(list)
    analytics['median'] = statistics.median(list)
    try:
        analytics['mode'] = statistics.mode(list)       #list has a single mode
    except:
       analytics['mode'] = float('-inf')               #multiple mode. Set to -infinity
    analytics['stdDev'] = statistics.stdev(list)
    return analytics

### Function to return a column slice ################
def slice(list2, idx):                                  #slice of a 2dimensional list
    slice = []                                          #create an empty list
    for row in list2:
        slice.append(row[idx])                          #add element to the list
    return(slice)
######################################################

print("Part I ------------------------------------------ <br>")
print("SINGLE DIMENSION LIST - ANALYTICS... <br>")

srtList = sorted(list1)
dict1   = analytics(srtList)
print(srtList, '<br>')
print('<b>', dict1,'</b><br>')
print('<br>')


print("\nPart II ------------------------------------------ <br>")
print("2 DIMENSIONAL LIST - ROW ORIENTED ANALYTICS... <br>")

def prt(dictionary):                                    #optional better print
    for key in dictionary:
        print('%s=%.2f ' % (key,dictionary[key]), end='')

for row in list2:
    dict1 = analytics(row)
    print(row, '--> <b>', end='')
    prt(dict1)
    print('</b><br>')
print('<br>')


print("\nPart III ------------------------------------------  <br>")
print("SAME 2 DIMENSIONAL LIST - COLUMN ORIENTED ANALYTICS... <br>")
    
col_width = len(list2[0])               #length of the first row

print('<table>')
for idx in range(col_width):
    cols  = slice(list2, idx)
    dict1 = analytics(cols)
    print('<tr><td>', cols, '<td> --> <b>', end='')
    prt(dict1)
    print('</tr>')
print('</table>')
print()    
    
    
    
#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/s/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================