#!/usr/bin/env python3
#=====================================================================================
# Python dictionaries and nested dictionaries
#=====================================================================================
print("Content-Type: text/html \n")     #http header with newline char (required for the web)

print('''
    <html>
    <body>
    <h1>Python Dictionary Functions</h1>
''')

stuff1 = {'first':'Sam', 'last':'Sultan', 'gender':'M'}

stuff2 = {1:'one', 2:'two', 3:'three', 4:'four', 5:'five', 6:'six'}

stuff3 = [{'id':1, 'fname':'Barbara', 'lname':'Burns',   'ssn':'000-01-0001', 'sex':'F'},       #a list of dictionaries 
          {'id':2, 'fname':'Vincent', 'lname':'Cambria', 'ssn':'000-01-0002', 'sex':'M'}, 
          {'id':3, 'fname':'Duncan',  'lname':'Davidson','ssn':'000-01-0003', 'sex':'M'}]

### Manipulate dictionary 1 ################################################
print('<table border=1>')
print('<tr><th colspan=2 bgcolor=tan>Stuff1')
print('<tr><td>The entire dictionary as coded       <th>',"{'first':'Sam', 'last':'Sultan', 'gender':'M'}")
print('<tr><td>The entire dictionary as per Python  <td>', stuff1, '<b><-- Order is not guaranteed')
print('<tr><td>The number of elements in dictionary <td>', len(stuff1))
print("<tr><td>The content of element 'gender'      <td>", stuff1['gender'])
stuff1['first']  = 'Samuel'
stuff1['id']     = '12345678'
stuff1['talent'] = 'none'
print('<tr><td>stuff1 after change and insert       <td>', stuff1)
del stuff1['talent']
print("<tr><td>stuff1 after delete element 'talent' <td>", stuff1)
print('</table>')
print('<br>')


### Manipulate dictionary 2 ################################################
print('<table border=1>')
print('<tr><th colspan=2 bgcolor=tan>Stuff2')
print('<tr><td>The entire dictionary <th>', stuff2, '<b><-- Order is not guaranteed')
stuff2_keys = stuff2.keys()
print('<tr><td>The keys only   <td>', stuff2_keys)
stuff2_vals = stuff2.values()
print('<tr><td>The values only <td>', stuff2_vals)
stuff2_key_asc  = sorted(stuff2.keys())
print('<tr><td>Sorting stuff2 by keys ascending    <td>', stuff2_key_asc)
stuff2_key_asc  = sorted(stuff2.keys(),reverse=True)
print('<tr><td>Sorting stuff2 by keys descending   <td>', stuff2_key_asc)
stuff2_val_asc  = sorted(stuff2.values())
print('<tr><td>Sorting stuff2 by values ascending  <td>', stuff2_val_asc)
stuff2_val_desc = sorted(stuff2.values(),reverse=True)
print('<tr><td>Sorting stuff2 by values descending <td>', stuff2_val_desc)
print('</table>')
print('<br>')


### Manipulate a list of dictionaries ################################################
print('<table border=1>')
print('<tr><th colspan=2 bgcolor=tan>Stuff3 (a list containg dictionaries)')
print('<tr><td>A list containg a dictionay <td><b>', stuff3)
print('<tr><td>Row 1 only                    <td>', stuff3[0] )
print('<tr><td>Row 2, last name              <td>', stuff3[1]['lname'] )
print('<tr><td>The length of stuff3          <td>', len(stuff3) )
print('<tr><td>The length of row 1           <td>', len(stuff3[0]) )

new_student = {'id':4,'fname':'Sam','lname':'Sultan','ssn':'000-01-0004','sex':'M'}
stuff3.append(new_student)                                                          #add another dictionary to the list

print('<tr><td>Adding a new dictionary to the list<td>', new_student) 

stuff3a = sorted(stuff3, key=lambda stuff3: stuff3['fname'])
print('<tr><td>Sorting on first name ascending  <td>', stuff3a)

stuff3a = sorted(stuff3, key=lambda stuff3: stuff3['fname'], reverse=True)
print('<tr><td>Sorting on first name descending <td>', stuff3a)
print('</table>')
print('<br>')

print('<table border=1>')
print('<tr><th colspan=5 bgcolor=tan>Looping thru list of dictionaries</th></tr>')

row1      = stuff3[0]
row1_keys = row1.keys()
print('<tr bgcolor=gray>')
for key in row1_keys:
    print('<th>', key, end='')
print('</tr>')

for row in stuff3:
    print('<tr>', end='')
    for key in row:
        print('<td>', row[key], end='')
    print('</tr>')
print('</table>')
print('<br>')

print(''' 
    </body> 
    </html> 
''')

      

#=== 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
#=================================================================================