code: def database_to_csv(self, path, orderby='type'):
          
          tableinfo = self.get_table_info()
          stats_master = database_to_json(self.dbcon_master, tableinfo)
          stats_partial = database_to_json(self.dbcon_part, tableinfo)
          with open(path, 'w') as f:
              csvfile = csv.writer(f)
              csvfile.writerow(['Name', 'Type', 'Value', 'Description'])
              rows = []
              for tablename, info in tableinfo.iteritems():
                  # Attempt to get the latest stat (from partial), if it doesn't exist get it from master
                  value = stats_partial.get(tablename, {}).get('data', ValueError) or stats_master.get(tablename, {}).get('data', ValueError)
                  if value is ValueError:
                      # The trackable was registered but no table values are found
                      value = self[tablename].current_value
                      if value is NO_STATE:
                          value = 'No Initial State'
  
                  rows.append([tablename, info['type'], value, info['description']])
  
              if orderby == 'type':
                  rows.sort(key=lambda x: x[1]) # Sort by type
              elif orderby == 'name':
                  rows.sort(key=lambda x: x[0])  # Sort by type
              csvfile.writerows(rows)
docs: Create a CSV file for the latest usage stats.
          :param path: path to output CSV file
          :param dbconn_master: master database connection
          :param dbconn_part: partial database connection
          :param tableinfo: table header information