def __find_by_template_scan__(self, t, fields=None, limit=None, offset=None): """ Returns a new, derived table containing rows that match the template and the requested fields if any. Returns all row if template is None and all columns if fields is None. :param t: The template representing a select predicate. :param fields: The list of fields (project fields) :param limit: Max to return. Not implemented :param offset: Offset into the result. Not implemented. :return: New table containing the result of the select and project. """ if limit is not None or offset is not None: raise DataTableExceptions.DataTableException( -101, "Limit/offset not supported for CSVTable") # If there are rows and the template is not None if self.__rows__ is not None: result = [] # Add the rows that match the template to the newly created table. for r in self.__rows__: if self.matches_template(r, t): result.append(r) result = self.project(result, fields) else: result = None return result
def project(self, rows, fields): """ Perform the project. Returns a new table with only the requested columns. :param fields: A list of column names. :return: A new table derived from this table by PROJECT on the specified column names. """ try: if fields is None: # If there is not project clause, return the base table return rows # Should really return a new, identical table but am lazy. else: result = [] for r in rows: # For every row in the table. tmp = {} # Not sure why I am using range. for j in range( 0, len(fields) ): # Make a new row with just the requested columns/fields. v = r[fields[j]] tmp[fields[j]] = v else: result.append(tmp) # Insert into new table when done. return result except KeyError as ke: # happens if the requested field not in rows. raise DataTableExceptions.DataTableException( -2, "Invalid field in project")
def __load__(self): try: fn = self.__get_file_name__() with open(fn, "r") as csvfile: # CSV files can be pretty complex. You can tell from all of the options on the various readers. # The two params here indicate that "," separates columns and anything in between " ... " should parse # as a single string, even if it has things like "," in it. reader = csv.DictReader(csvfile, delimiter=",", quotechar='"') # Get the names of the columns defined for this table from the metadata. column_names = self.__get_column_names__() # Loop through each line (well dictionary) in the input file. for r in reader: # Only add the defined columns into the in-memory table. The CSV file may contain columns # that are not relevant to the definition. projected_r = self.project([r], column_names)[0] self.__add_row__(projected_r) except IOError as e: raise DataTableExceptions.DataTableException( code=DataTableExceptions.DataTableException.invalid_file, message="Could not read file = " + fn)
def update(self, t, change_values): raise DataTableExceptions.DataTableException( code=DataTableExceptions.DataTableException.not_implemented, message="Updated not implemented")
def delete(self, t): raise DataTableExceptions.DataTableException( code=DataTableExceptions.DataTableException.not_implemented, message="Delete not implemented")
def insert(self, r): raise DataTableExceptions.DataTableException( code=DataTableExceptions.DataTableException.not_implemented, message="Insert not implemented")