
# using data from
# https://www.gapminder.org/data/

# https://docs.python.org/3/library/csv.html

import csv

class Util:
  #static fields of a class
  unitNames   = [ 'k', 'K', 'm', 'M', 'g', 'G', 't', 'T', 'p', 'P' ]
  unitValues  = { 'k': 10**3,   'K': 10**3,  \
                  'm': 10**6,   'M': 10**6,  \
                  'g': 10**9,   'G': 10**9,  \
                  't': 10**12,  'T': 10**12, \
                  'p': 10**15,  'P': 10**15  }

   
  @staticmethod
  # returns specific value (None, 0, etc)  when the string is not decoded 
  def strValue( str ):
    specValue = 0
    
    if str in [None, ""]: return specValue
    str = str.strip()  # remowe leading/trailing whitespaces, tabs
    # no unit 
    if str[-1].isdigit():
      try:
        value = int( str )
        return value
      except:
        return specValue
    # unit specified
    else: 
      unitChar = str[-1]
      # unit unknown
      if not unitChar in Util.unitNames:
        return specValue
      # unit known
      try:
        value  =  float( str[:-1] ) 
        return  round( value * Util.unitValues[unitChar] )
      except:
        return specValue
      

# ---------------------------


filename = "net_users_num.csv"

fieldNames = []  # field Names first line in 
rows = []

with open(filename, 'r') as csvfile:
    # creating a csv reader object
    csvreader = csv.reader(csvfile)
     
    # extracting field names through first row
    fieldNames = next(csvreader)
 
    # extracting each data row one by one
    for row in csvreader:
        rows.append(row)
  
    print("Total no. of data rows : %d"%(len(rows)) )
    
    # -- with

print('Field names are:' + ', '.join(field for field in fieldNames))

#extract maximum val from the table
maxval = 0
for row in  rows:
  rvals = [ Util.strValue( s ) for s in row[1:] ]
  print( row[0], rvals )
  maxval = max( [maxval] + rvals )

print(" maxval", maxval)

for row in  rows:
  rvals = [ Util.strValue( s ) for s in row ]
  print( row[0] )
  for j in range(1, len(row) ) :
    #print( fieldNames[j], rvals[j] ) 
    print( fieldNames[j], "X" * int(round( 50 * rvals[j]/maxval ) ) )



