I have a method in a class that accepts either a datetime or string parameter. It is shown in the last code block (I didn't include the class). A little background is that I am defining a wrapper around an API, and I want the end user to be able to pass either a string or a date but convert that into the appropriate string for the API query.
I wrote a decorator that validates the dates, and converts the datetime to the appropriate string format for the subsequent call to the API. The decorator I wrote is defined outside of the class as a function.
- Should I include
validate_date_helper
in the scope of the wrapper? - Is defining these functions outside of the class pythonic and is this a valid approach for what I am trying to accomplish?
- Are there other ways to accomplish validations like this?
from datetime import datetime as dt from typing import Union def validate_date_helper(date): """ validates a start date or updated after date. end """ utc_format = "%Y-%m-%dT%H:%M:%S" if isinstance(date, dt): date = date.strftime(utc_format) elif isinstance(date, str): utc_format_date = date + "T00:00:00" dt.strptime(utc_format_date, utc_format) else: raise ValueError(f"{date} must be datetime object or string") return date def validate_dates(func): def func_wrapper(self, *args, **kwargs): """ validates dates for the between function """ new_args = [] new_kwargs = {} for arg in args: arg = validate_date_helper(arg) new_args.append(arg) for key, val in kwargs.items(): if val is not None: val = validate_date_helper(val) new_kwargs[key] = val res = func(self, *new_args, **new_kwargs) return res return func_wrapper @validate_dates def between(self, start: Union[dt, str], end=None): """ sets startime and endtime for API query """ self.query["starttime"] = start # self.query is an empty dictionary at class instantiation if end is not None: self.query["endtime"] = end return self
convert_date_to_string
.\$\endgroup\$