def safe_tabulate(tabular_data, headers, **kwargs): terminal_columns, _ = get_terminal_size() # fancy_grid format has utf-8 characters (in corners of table) # cp1252 fails to encode that fancy_grid = not windows_compatible_mode and not environ_enabled( ENV_DBND__NO_TABLES ) tablefmt = "fancy_grid" if fancy_grid else "grid" table = tabulate(tabular_data, headers=headers, tablefmt=tablefmt, **kwargs) if table and max(map(len, table.split())) >= terminal_columns: table = tabulate(tabular_data, headers=headers, tablefmt="plain", **kwargs) return table
def to_preview(self, table): # type: (SnowflakeTable) -> str # snowflake.connector does not handle incorrect utf-8 data fetched from db, # hence this fillding with encode/decode column_types = self.get_column_types(table) columns = ",".join( 'TRY_HEX_DECODE_STRING(HEX_ENCODE("{0}")) AS {0}'.format(column) for column in column_types.keys()) rows = self.query( "select {0} from {1.db_with_schema}.{1.table_name} limit {1.preview_rows}" .format(columns, table)) preview_table = tabulate(rows, headers="keys") + "\n..." return preview_table
def tabulate_objects( objects, fields_filter=lambda f: not f.startswith("_"), headers=None, to_local_time=True, ): if not headers: headers = [k for k in dir(objects[0]) if fields_filter(k)] values = [] for o in objects: o_values = [] values.append(o_values) for h in headers: value = o.__getattribute__(h) if to_local_time and value and isinstance(value, datetime.datetime): value = pendulum.from_timestamp(_timestamp(value), "local").to_rfc822_string() o_values.append(value) return tabulate(values, headers=headers)
def to_preview(self): rows = self._query("select * from {} limit 20".format(self.table_name)) preview_table = tabulate(rows, headers="keys") return preview_table