def closest_ds_partition(<EOL>table, ds, before=True, schema="<STR_LIT:default>",<EOL>metastore_conn_id='<STR_LIT>'):
from airflow.hooks.hive_hooks import HiveMetastoreHook<EOL>if '<STR_LIT:.>' in table:<EOL><INDENT>schema, table = table.split('<STR_LIT:.>')<EOL><DEDENT>hh = HiveMetastoreHook(metastore_conn_id=metastore_conn_id)<EOL>partitions = hh.get_partitions(schema=schema, table_name=table)<EOL>if not partitions:<EOL><INDENT>return None<EOL><DEDENT>part_vals = [list(p.values())[<NUM_LIT:0>] for p in partitions]<EOL>if ds in part_vals:<EOL><INDENT>return ds<EOL><DEDENT>else:<EOL><INDENT>parts = [datetime.datetime.strptime(pv, '<STR_LIT>')<EOL>for pv in part_vals]<EOL>target_dt = datetime.datetime.strptime(ds, '<STR_LIT>')<EOL>closest_ds = _closest_date(target_dt, parts, before_target=before)<EOL>return closest_ds.isoformat()<EOL><DEDENT>
This function finds the date in a list closest to the target date.
An optional parameter can be given to get the closest before or after.

:param table: A hive table name
:type table: str
:param ds: A datestamp ``%Y-%m-%d`` e.g. ``yyyy-mm-dd``
:type ds: list[datetime.date]
:param before: closest before (True), after (False) or either side of ds
:type before: bool or None
:returns: The closest date
:rtype: str or None

>>> tbl = 'airflow.static_babynames_partitioned'
>>> closest_ds_partition(tbl, '2015-01-02')
'2015-01-01'