diff --git a/frictionless/resources/table.py b/frictionless/resources/table.py index cba3ae4cee..0efedf339f 100644 --- a/frictionless/resources/table.py +++ b/frictionless/resources/table.py @@ -303,6 +303,7 @@ def __open_row_stream(self): enumerated_content_stream = self.dialect.read_enumerated_content_stream( self.cell_stream ) + # Create row stream def row_stream(): @@ -332,17 +333,8 @@ def row_stream(): # Primary Key Error if is_integrity and self.schema.primary_key: try: - if self.dialect.header_case: - cells = tuple(row[name] for name in self.schema.primary_key) - else: # Ignore case - cells = () - lower_primary_keys = [ - pk.lower() for pk in self.schema.primary_key - ] - # cells = tuple(row[label] if (label.lower() for label in row.field_names) in lower_primary_keys ) - for label in row.field_names: - if label.lower() in lower_primary_keys: - cells = cells + (row[label],) + cells = self.primary_key_cells(row, + self.dialect.header_case) except KeyError: # Row does not have primary_key as key # There should already be a missing-label error in @@ -417,8 +409,27 @@ def row_stream(): # # Create row stream self.__row_stream = row_stream() - # Read + def primary_key_cells( + self, + row: Row, + case_sensitive: bool + ) -> Tuple[Row, Any]: + """Create a tuple containg all cells related to primary_key + """ + if case_sensitive: + cells = tuple(row[name] for name in self.schema.primary_key) + else: # Ignore case + cells = () + lower_primary_keys = [ + pk.lower() for pk in self.schema.primary_key + ] + # cells = tuple(row[label] if (label.lower() for label in row.field_names) in lower_primary_keys ) + for label in row.field_names: + if label.lower() in lower_primary_keys: + cells = cells + (row[label],) + return cells + # Read def read_cells(self, *, size: Optional[int] = None) -> List[List[Any]]: """Read lists into memory