PostgresPanFactory

PostgresPanFactory(self, pan_dat_factory)

Primary class for reading/writing PostGres databases with PanDat objects.

Don't create this object explicitly. A PostgresPanFactory will automatically be associated with the pgsql attribute of the parent PanDatFactory.

Will need to have pandas installed to do anything.

postgres doesn't support brackets, and putting spaces in postgres field names is frowned upon. https://bit.ly/2xWLZL3. You are encouraged to continue to use field names like "Min Nutrition" in your ticdat Python code, and the pgtd code here will match such fields up with postgres field names like min_nutrition when reading/writing from a postgres DB. (Non alphamnumeric characters in general, and not just spaces, are replaced with underscores for generating PGSQL field names).

create_pan_dat

PostgresPanFactory.create_pan_dat(engine, schema, active_fld='')

Create a PanDat object from a PostGres connection

:param engine: A sqlalchemy connection to the PostGres database

:param schema : The name of the schema to read from

:param active_fld: if provided, a string for a boolean filter field. Must be compliant w PG naming conventions, which are different from ticdat field naming conventions. Typically developer can ignore this argument, designed for expert support.

:return: a PanDat object populated by the matching tables. Missing tables issue a warning and resolve to empty.

write_data

PostgresPanFactory.write_data(pan_dat,
                              engine,
                              schema,
                              pre_existing_rows=None,
                              active_fld='',
                              progress=None,
                              table_specific_context_manager=None)

write the PanDat data to a postgres database

:param pan_dat: a PanDat object

:param engine: A sqlalchemy connection to the PostGres database

:param schema: The postgres schema to write to (call self.write_schema explicitly as needed)

:param pre_existing_rows: if provided, a dict mapping table name to either "delete" or "append" default behavior is "delete"

:param active_fld: if provided, a string for a boolean filter field which will be populated with True. Must be compliant w PG naming conventions, which are different from ticdat field naming conventions. Typically developer can ignore this argument, designed for expert support.

:param progress: if provided, a ticdat.Progress object that is called every time a table is uploaded

:param table_specific_context_manager: if provided, a dict mapping table name to a context manager factory. That is to say, table_specific_context_manager.values() should be zero argument calleables that return context manager objects. The DataFrame.to_sql statement writing to the database will happen within this resulting context manager for every entry in this dict. This is an expert level only feature - don't use it without studying this whole subroutine

:return:

PostgresTicFactory

PostgresTicFactory(self, tic_dat_factory)

Primary class for reading/writing PostGres databases with TicDat objects. You need the sqlalchemy package to be installed to use it.

Don't create this object explicitly. A PostgresTicFactory will automatically be associated with the pgsql attribute of the parent TicDatFactory.

postgres doesn't support brackets, and putting spaces in postgres field names is frowned upon. https://bit.ly/2xWLZL3. You are encouraged to continue to use field names like "Min Nutrition" in your ticdat Python code, and the pgtd code here will match such fields up with postgres field names like min_nutrition when reading/writing from a postgres DB. (Non alphamnumeric characters in general, and not just spaces, are replaced with underscores for generating PGSQL field names)

create_tic_dat

PostgresTicFactory.create_tic_dat(engine,
                                  schema,
                                  freeze_it=False,
                                  active_fld='')

Create a TicDat object from a PostGres connection

:param engine: A sqlalchemy connection to the PostGres database

:param schema : The name of the schema to read from

:param freeze_it: boolean. should the returned object be frozen?

:param active_fld: if provided, a string for a boolean filter field. Must be compliant w PG naming conventions, which are different from ticdat field naming conventions. Typically developer can ignore this argument, designed for expert support.

:return: a TicDat object populated by the matching tables. Missing tables issue a warning and resolve to empty.

find_duplicates

PostgresTicFactory.find_duplicates(engine, schema, active_fld='')

Find the row counts for duplicated rows.

:param engine: A sqlalchemy Engine object that can connect to our postgres instance

:param schema: Name of the schema within the engine's database to use

:param active_fld: if provided, a string for a boolean filter field. Must be compliant w PG naming conventions, which are different from ticdat field naming conventions. Typically developer can ignore this argument, designed for expert support.

:return: A dictionary whose keys are table names for the primary-ed key tables. Each value of the return dictionary is itself a dictionary. The inner dictionary is keyed by the primary key values encountered in the table, and the value is the count of records in the postgres table with this primary key. Row counts smaller than 2 are pruned off, as they aren't duplicates

write_data

PostgresTicFactory.write_data(tic_dat,
                              engine,
                              schema,
                              dsn=None,
                              pre_existing_rows=None,
                              active_fld='')

write the ticDat data to a PostGres database

:param tic_dat: the data object to write

:param engine: a sqlalchemy database engine with drivertype postgres

:param schema: the postgres schema to write to (call self.write_schema explicitly as needed)

:param dsn: optional - if truthy, a dict that can be unpacked as arguments to psycopg2.connect. Will speed up bulk writing compared to engine.execute If truthy and not a dict, then will be passed directly to psycopg2.connect as the sole argument.

:param pre_existing_rows: if provided, a dict mapping table name to either "delete" or "append" default behavior is "delete"

:param active_fld: if provided, a string for a boolean filter field which will be populated with True. Must be compliant w PG naming conventions, which are different from ticdat field naming conventions. Typically developer can ignore this argument, designed for expert support. :return: