In this question, don't change code just following the instructions
#!/usr/bin/env python
"""
An example python script to illustrate how a projection might be simulated,
and lead to an example GROUP BY ( using dictionaries to group and aggregate )
"""
import sys, os, time
import csv
from datetime import datetime
from optparse import OptionParser
class SQLProjectionExample:
def __init__(self):
"""
The SQLProjectionExample constructor includes the logic for arg handling.
In effect, we assume this class will be run on the command line with a main.
"""
self._opt_defaults = {}
self._opt_parser = OptionParser()
self._opt_parser.add_option(
'--table', '-t',
action = 'store',
dest = 'table',
help = 'Table'
)
self._opt_parser.add_option(
'--columns', '-c',
action = 'append',
dest = 'columns',
metavar = 'col',
help = 'column'
)
self._opt_defaults['test'] = False
(self._opts, self._args) = self._opt_parser.parse_args()
def show(self):
"""
Simple standard utility function for illustrating argument state.
"""
#print self._opts
print self._opt_defaults
def read_table(self, table_file_name):
"""
This function serves as a simple mechanism for pulling a table off of disk.
The function assumes that the format of the table is double quoted,
comma delimited, and new line separated.
"""
row_list = []
rows = csv.reader( open( table_file_name, "rb" ), delimiter=",", quotechar="\"" )
for row in rows:
row_list.append( row )
return row_list
def get_col_def(self, table):
"""
This implementation of get_col_def assumes that the table reference is a
set (list) of tuples with the first being our definition of columns.
"""
return table[0]
def get_rows(self, table):
"""
This assumes that the data are all rows after the first in table.
"""
return table[1:]
def display_rows(self, rows):
for row in rows:
print ",".join( row )
def get_columns_indexes(self, table_def, columns):
"""
Retrieve the indexes of the columns that we are going to SELECT.
We check to see if the requested set of columns are in the table defintion,
and add the corresponding index to the return ref. If there are no
columns specified, return all of the table column indexes in order to
render all of them.
"""
col_idxs = []
# print columns
# print table_def
for idx, col in enumerate( table_def ):
# print idx
if not columns:
col_idxs.append( idx )
elif col in columns:
col_idxs.append( table_def.index(col) )
# print col
# print table_def.index(col)
return col_idxs
def project_table_cols(self, rows, idxs):
"""
This function is the guts of this example. Given the rows from the table,
and a list of the indexes of which columns we wish to project (SELECT), we
make use of some python syntactic sugar to add each column per row to a
list which we return as the final result set.
"""
print "**** TABLE OUTPUT ****"
projection = []
for row in rows:
row_cols = [i for j, i in enumerate(row) if j in idxs]
projection.append( row_cols )
return projection
def display_results(self, results):
"""
Take a list, and print it out separated by vertical bar.
"""
for r in results:
print "|%s|" % ( "|".join( map( str, r ) ) )
if __name__=="__main__":
example = SQLProjectionExample()
if not example._opts.table:
example._opt_parser.print_help()
sys.exit("Missing table argument, required.")
table = example.read_table( example._opts.table )
table_def = example.get_col_def( table )
rows = example.get_rows( table )
col_idxs = example.get_columns_indexes( table_def, example._opts.columns )
result_set = example.project_table_cols( rows, col_idxs )
example.display_results( result_set )
Q/ Can someone help work with the above code and print out the following solutions? Can you use any CSV file you want to print the solution, please! Don't change code only it python postgreSQL
Extracted text: . It consumes a csv file, identifies columns from the header, matches them with command-line arguments for the target columns, and displays those columns. Here is how it might be run, in order to simulate the same logic for a "SELECT Name From customer_order" SQL statement. Below: $ python ./SQLProjectionExample.py -t customer-orders.csv -c Name **** TABLE OUTPUT **** I Mom and Popl |Franchise Hardwarel I Department Storel I Department Storel ILumber Yard| Currently, the program is case sensitive. Therefore, if the value of the -c (--columns) argument was "name" instead of "Name" it would fail to discover any matching columns (but the target columns list, would not be empty so it would not return to the default behavior of displaying all columns when no columns are specified...). So here is what a working solution might look like: $ python ./SQLGroupBy.py -t customer-orders.csv -c Name -g Amount -f SUM **** TABLE OUTPUT **** | Mom and Pop15| IFranchise Hardware|100| I Department Store|100| ILumber Yard|50|