The AsciiDataTable class is meant to deal with data in the form of a table with a header and footer. It has the ability to change formating, load a file, and do simple manipulations.
Some key elements of the AsciiDataTable class:
To make a new data table:
new_table=AsciiDataTable()
#or
new_table=AsciiDataTable(header=["My Header","Line2" ],column_names=["a","b","c"],data=[[1,2,3]])
To open an existing table:
#in default format (csv with no #)
existing_table=AsciiDataTable("My_Table.txt")
# for an existing format with a saved schema
schema=read_schema("My_Schema")
existing_table=AsciiDataTable("My_Table.txt",**schema)
# As an example
import pyMez.Code.DataHandlers.GeneralModels as GeneralModels
#Lets make a table and put it into the AsciiDataTable
new_table=GeneralModels.AsciiDataTable()
# The data is a list of lists with the first dimension # of rows and the second dimension # of columns
table_data=[[i+j*3 for i in range(3)]for j in range(3)]
print(table_data)
#input the data directly
new_table.data=table_data
# assign the column names
new_table.column_names=['x1','x2','x3']
# when a string related command is called it uses build string to output the string print(new_table) also works
print(new_table.build_string())
# the attribute options stores the schema for the data
new_table.options['column_names_begin_token']='#'
print(new_table.build_string())
# changing a formating option doesn't change the data
new_table.options['column_names_begin_token']='['
new_table.options['column_names_end_token']=']'
print(new_table.build_string())
# There is the ability to control the output format in many ways
new_table.options['data_begin_token']='*'*80+'\n'
new_table.options['data_end_token']="\n"+'*'*80
print(new_table.build_string())
# the header is where we would store metadata
new_table.header=['My First Header Line','My Second Header Line']
print(new_table)
new_table.options['comment_begin']='#'
new_table.options['comment_end']='\n'
print(new_table)
# get_options returns all options printed in a nice form
new_table.get_options()
# when we created the file it autonamed itself. This is the place the file will save to using new_table.save()
new_table.path
# Each formatting option can be turned off
new_table.options['data_begin_token']=None
new_table.options['data_end_token']=None
print(new_table)
# We can build the table this way or using
# AsciiDataTable(header=new_table.header,column_names=new_table.column_names)
new_table2=GeneralModels.AsciiDataTable()
new_table2.header=new_table.header
new_table2.column_names=new_table.column_names
new_table2.options=new_table.options
print(new_table2)
new_table2.data=[[2*(i+j*3) for i in range(3)]for j in range(3)]
print(new_table2)
# Adding the tables, if column names are the same
new_table+new_table2
print(new_table)
print("\n"+" --End of Table 1 --"*5+"\n")
print(new_table2)
print(new_table)
print(new_table2.build_string())
str(new_table2).count("\n")
test_string=str(new_table2)
test_string
test_list=test_string.split('\n')
test_list
print(new_table)
new_table.options['inline_comment_begin']='(*'
new_table.options['inline_comment_end']='*)'
new_table.inline_comments=[["Hello, This is an inline comment",0,3]]
print(new_table)
import random
data_delimiters=[",",".",":",";","-"," ","\t","::","~"," | "]
begin_end_pairs=[["<",">"],["(*","*)"],["[","]"],["/*","*/"],["(",")"],["<","/>"],["#",""]]
random_int=random.randint(0,len(data_delimiters)-1)
random_list=[random.randint(0,len(data_delimiters)-1) for i in range(20)]
print(random_list)
random_delimiter_list_1=[data_delimiters[random.randint(0,len(data_delimiters)-1)] for i in range(20)]
random_delimiter_list_2=[data_delimiters[random.randint(0,len(data_delimiters)-1)] for i in range(20)]
random_pairs_1=[begin_end_pairs[random.randint(0,6)] for i in range(20)]
random_pairs_2=[begin_end_pairs[random.randint(0,6)] for i in range(20)]
print(begin_end_pairs)
new_table.inline_comments=[["Hello, This is an inline comment",0,-1]]
# An example of all the formats randomized
for index in range(len(random_delimiter_list_1)):
new_table.options["data_delimiter"]=random_delimiter_list_1[index]
new_table.options["column_names_delimiter"]=random_delimiter_list_2[index]
new_table.options["comment_begin"]=random_pairs_1[index][0]
new_table.options["comment_end"]=random_pairs_1[index][1]+"\n"
new_table.options["column_names_begin_token"]=random_pairs_2[index][0]
new_table.options["column_names_end_token"]=random_pairs_2[index][1]
print( "\n{0}\n".format(new_table))
GeneralModels.show_structure_script()