Conditions | 4 |
Total Lines | 56 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | from io import StringIO |
||
159 | def write_table_to_postgres( |
||
160 | df, db_table, drop=False, index=False, if_exists="append" |
||
161 | ): |
||
162 | """ |
||
163 | Helper function to append df data to table in db. Fast string-copy is used. |
||
164 | Only predefined columns are passed. If column is missing in dataframe a |
||
165 | warning is logged. Dtypes of columns are taken from table definition. The |
||
166 | writing process happens in a scoped session. |
||
167 | |||
168 | Parameters |
||
169 | ---------- |
||
170 | df: pd.DataFrame |
||
171 | Table of data |
||
172 | db_table: declarative_base |
||
173 | Metadata of db table to export to |
||
174 | drop: boolean, default False |
||
175 | Drop db-table before appending |
||
176 | index: boolean, default False |
||
177 | Write DataFrame index as a column. |
||
178 | if_exists: {'fail', 'replace', 'append'}, default 'append' |
||
179 | - fail: If table exists, do nothing. |
||
180 | - replace: If table exists, drop it, recreate it, and insert data. |
||
181 | - append: If table exists, insert data. Create if does not exist. |
||
182 | |||
183 | """ |
||
184 | logger.info("Write table to db") |
||
185 | # Only take in db table defined columns and dtypes |
||
186 | columns = { |
||
187 | column.key: column.type for column in db_table.__table__.columns |
||
188 | } |
||
189 | |||
190 | # Take only the columns defined in class |
||
191 | # pandas raises an error if column is missing |
||
192 | try: |
||
193 | df = df.loc[:, columns.keys()] |
||
194 | except KeyError: |
||
195 | same = df.columns.intersection(columns.keys()) |
||
196 | missing = same.symmetric_difference(df.columns) |
||
197 | logger.warning(f"Columns: {missing.values} missing!") |
||
198 | df = df.loc[:, same] |
||
199 | |||
200 | if drop: |
||
201 | db_table.__table__.drop(bind=engine, checkfirst=True) |
||
202 | db_table.__table__.create(bind=engine) |
||
203 | else: |
||
204 | db_table.__table__.create(bind=engine, checkfirst=True) |
||
205 | |||
206 | with db.session_scope() as session: |
||
207 | df.to_sql( |
||
208 | name=db_table.__table__.name, |
||
209 | schema=db_table.__table__.schema, |
||
210 | con=session.connection(), |
||
211 | if_exists=if_exists, |
||
212 | index=index, |
||
213 | method=psql_insert_copy, |
||
214 | dtype=columns, |
||
215 | ) |
||
216 |