Best Python code snippet using pandera_python
support_tools.py
Source:support_tools.py
1# coding: utf-82"""3 Support Tools4 ~~~~~~~~~~~~~5"""6# Imports7import re8import pandas as pd9import dateutil.parser10from datetime import datetime11# ----------------------------------------------------------------------------------------------------------12# General13# ----------------------------------------------------------------------------------------------------------14def cln(i, extent=1):15 """16 String white space 'cleaner'.17 :param i: input str18 :type i: ``str``19 :param extent: 1 --> all white space reduced to length 1; 2 --> removal of all white space.20 :return: cleaned string21 :rtype: ``str``22 """23 if isinstance(i, str) and i != "":24 if extent == 1:25 return re.sub(r"\s\s+", " ", i)26 elif extent == 2:27 return re.sub(r"\s+", "", i)28 else:29 return i30def min_max(iterable):31 """32 Get the min and max value of an iterable of numerics.33 :param iterable: any iterable data structure of numerics34 :type iterable: ``tuple``, ``list``, ``np.array``35 :return: (min(iterable), max(iterable))36 :rtype: ``tuple``37 """38 return (min(iterable), max(iterable))39def sort_range_reverse(iterable, action='range'):40 """41 Sort, reverse, or get range of an iterable.42 :param iterable: any iterable43 :type iterable: ``iterable``44 :param action: An action to perform on the iterable.45 Must be one of: 'sort', 'reverse' or 'range'.46 `iterable` must only contain numerics for 'range'.47 Defaults to 'range'.48 :param action: ``str``49 :return: i. sorted or reversed iterable; ii. the min and max of the iterable.50 :rtype: ``list``51 """52 if not isinstance(iterable, list):53 return iterable54 elif action == 'sort':55 return sorted(iterable)56 elif action == 'reverse':57 return list(reversed(iterable))58 elif action == 'range':59 return list(min_max(iterable))60 else:61 return iterable62# ----------------------------------------------------------------------------------------------------------63# Mathematical64# ----------------------------------------------------------------------------------------------------------65def closest_value(value, list_of_values):66 """67 Get the closet value in a list of values.68 :param value: a numeric value.69 :type value: ``int``, ``float`` or ``datetime``70 :param list_of_values: an iterable.71 :type list_of_values: ``tuple``, ``list``, ``np.array``72 :return: item in `list_of_values` closet to `value`.73 :rtype: ``int``, ``float`` or ``datetime``74 """75 # Source: http://stackoverflow.com/a/12141207/489800476 return min(list_of_values, key=lambda i: abs(i - value))77# ----------------------------------------------------------------------------------------------------------78# Dates79# ----------------------------------------------------------------------------------------------------------80def datetimer(date, from_format=None):81 """82 Converts a date string into a datetime.83 :param date: a date84 :type date: ``str``85 :param from_format: the format of `date`. If none, an attempt will be made to guess the format. Defaults to `None`.86 :type from_format: ``str`` or ``None``87 :return: a datetime object.88 :rtype: ``datetime``89 """90 formatter = datetime.strptime if from_format != None else dateutil.parser.parse91 return formatter(date, from_format)92def date_reformat(date, to_format='%d/%m/%Y', from_format=None):93 """94 Reformat a date to a given form.95 :param date: a date96 :type date: ``str``97 :param to_format: the new format of the date.98 :type to_format: ``str``99 :param from_format: the format of the string supplied.100 :type from_format: ``str``101 :return:102 """103 if not isinstance(date, str):104 return date105 return datetimer(date, from_format).strftime(to_format)106def date_sort(list_of_dates, from_format="%d/%m/%Y"):107 """108 Sort a list of datetimes.109 :param list_of_dates: iterable of date strings110 :type list_of_dates: ``iterable``111 :param from_format: the format of the strings in `list_of_dates`. Defaults to "%d/%m/%Y".112 :type from_format: ``str``113 :return: sorted date strings114 :rtype: ``list``115 """116 return sorted(list_of_dates, key=lambda x: datetime.strptime(x, from_format))117def min_max_dates(list_of_dates, from_format="%d/%m/%Y"):118 """119 Get the min and max date in a list of date strings.120 :param list_of_dates: list of date strings.121 :type list_of_dates: ``iterable``122 :param from_format: the format of the strings in `list_of_dates`. Defaults to "%d/%m/%Y".123 :type from_format: ``str``124 :return: (min(list_of_dates), max(list_of_dates))125 :rtype: tuple126 """127 pandas_datetime = pd.to_datetime(pd.Series(list_of_dates), format=from_format)128 return (pandas_datetime.min().strftime(from_format), pandas_datetime.max().strftime(from_format))129def fast_date_range(dates, from_format='%d/%m/%Y'):130 """131 Fast function to find the min and max date in an iterable of date strings.132 :param dates: iterable of strings133 :type dates: ``iterable``134 :param from_format: date format. Defaults to '%d/%m/%Y'.135 :type from_format: ``str``136 :return: [min(dates), max(dates)]137 :rtype: ``list``138 """139 return [i.strftime(from_format) for i in min_max([datetime.strptime(j, from_format) for j in dates])]140def closest_date(date, list_of_dates, from_format="%d/%m/%Y"):141 """142 Get the closet date (string) in a list of datestrings.143 :param date: a date.144 :type date: ``str``145 :param list_of_dates: an iterable of date strings.146 :type list_of_dates: ``iterable``147 :param from_format: the format of the strings in `list_of_dates`. Defaults to "%d/%m/%Y".148 :type from_format: ``str``149 :return: closest date in `list_of_dates` to `date`.150 :rtype: ``str``151 """152 list_of_datetimes = [datetimer(d, from_format) for d in list_of_dates]153 closest = closest_value(datetimer(date, from_format), list_of_datetimes)154 return closest.strftime(from_format)155def _canonical_datetime(date_format):156 """157 Converts a date format to a human-readable format.158 :param date_format: a python-recognized date format containing any of: '%d', '%m' or '%Y'.159 :type date_format: ``str``160 :return: %d --> DD; %m --> MM; %Y --> YYYY.161 :rtype: ``str``162 """163 for f, c in zip(['%d', '%m', '%Y'], ['DD', 'MM', 'YYYY']):164 date_format = date_format.replace(f, c)165 return date_format166def date_format_check(date, from_format="%d/%m/%Y"):167 """168 Check whether or not a date is of a given form.169 :param date_format: a date.170 :type date: ``str``171 :param from_format: a python-recognized date format.172 :type from_format: ``str``173 :return: True if `date` is of the form `from_format` else raises ValueError.174 :rtype: ``bool``175 """176 try:177 datetime.strptime(date, from_format)178 return True179 except ValueError:180 raise ValueError("Invalid date format.\n"181 "Please supply a date of the form: %s." % (_canonical_datetime(from_format)))182def year_extract(date):183 """184 Extract year from a date.185 Assumes `date` to contain a year of the form: YYYY.186 :param date: a string containing a year187 :type date: ``str``188 :return: the first year found in string189 :rtype: ``str`` or ``None``190 """191 year = ""192 for c in str(date):193 if len(year) == 4:194 break195 if c.isdigit():196 year += c197 elif not c.isdigit() and len(year) < 4:198 year = ""199 return year if len(year) == 4 else None200# ----------------------------------------------------------------------------------------------------------201# Money Formatting202# ----------------------------------------------------------------------------------------------------------203def _money_formater(amount, precision, currency=''):204 """205 Formats money to be human-readable.206 :param amount: amount of money207 :type amount: ``float``, ``str`` or ``int``208 :param precision: number of place after the decimal to round to.209 :type amount: ``int``210 :param currency: A currency code to append to the result. Defaults to none ('').211 :type currency: ``str``212 :return: formated money213 :rtype: ``str``214 """215 return ('{:,.2f}'.format(round(float(amount), precision)) + ' ' + currency).strip()216def mint(amount, precision, currency='', pretty_print=False):217 """218 Print Money or Return it as a float.219 :param amount: amount of money220 :type amount: ``float``, ``str`` or ``int``221 :param precision: number of place after the decimal to round to.222 :param currency: A currency code to append to the result. Defaults to none ('').223 :type currency: ``str``224 :param pretty_print:225 :type pretty_print: ``bool``226 :return: money rounded to the requested precision or printed.227 :rtype: ``float`` or ``None``228 """229 if pretty_print:230 print(_money_formater(amount, precision, currency))231 else:...
config.py
Source:config.py
1import string2import os3BASE_DIR=os.path.abspath(os.path.join(4 os.path.dirname(__file__), '..'))5HOST='0.0.0.0'6PORT=50007CONVERSION_FOLDER=os.path.join(BASE_DIR, 'files')8PANDOC_CONFIGS=[9 { # From HTML10 'from_extension': 'html',11 'from_format': 'html',12 'to_extension': 'docx',13 'to_format': 'docx',14 'extra_args': []15 },16 {17 'from_extension': 'html',18 'from_format': 'html',19 'to_extension': 'md',20 'to_format': 'markdown',21 'extra_args': []22 },23 {24 'from_extension': 'html',25 'from_format': 'html',26 'to_extension': 'rst',27 'to_format': 'rst',28 'extra_args': []29 },30 { # From MD31 'from_extension': 'md',32 'from_format': 'markdown',33 'to_extension': 'html',34 'to_format': 'html',35 'extra_args': []36 },37 {38 'from_extension': 'md',39 'from_format': 'markdown',40 'to_extension': 'docx',41 'to_format': 'docx',42 'extra_args': []43 },44 {45 'from_extension': 'md',46 'from_format': 'markdown',47 'to_extension': 'rst',48 'to_format': 'rst',49 'extra_args': []50 },51 { # From RST52 'from_extension': 'rst',53 'from_format': 'rst',54 'to_extension': 'html',55 'to_format': 'html',56 'extra_args': []57 },58 {59 'from_extension': 'rst',60 'from_format': 'rst',61 'to_extension': 'md',62 'to_format': 'md',63 'extra_args': []64 },65 {66 'from_extension': 'rst',67 'from_format': 'rst',68 'to_extension': 'docx',69 'to_format': 'docx',70 'extra_args': []71 },72 { # From DOCX73 'from_extension': 'docx',74 'from_format': 'docx',75 'to_extension': 'html',76 'to_format': 'html',77 'extra_args': [78 '--self-contained',79 ]80 },81 {82 'from_extension': 'docx',83 'from_format': 'docx',84 'to_extension': 'md',85 'to_format': 'md',86 'extra_args': []87 },88 {89 'from_extension': 'docx',90 'from_format': 'docx',91 'to_extension': 'rst',92 'to_format': 'rst',93 'extra_args': []94 }...
parse_time.py
Source:parse_time.py
2import re3def parse_time(string):4 match = re.match(r'(\d{4}\d{2}\d{2})(\d{2})?(\d{2})?', string)5 if match.group(3):6 return pendulum.from_format(string, 'YYYYMMDDHHmm')7 if match.group(2):8 return pendulum.from_format(string, 'YYYYMMDDHH')9 else:10 return pendulum.from_format(string, 'YYYYMMDD')11def parse_time_range(string):12 match =re.match(r'(\d{4}\d{2}\d{2}\d{2})-(\d{4}\d{2}\d{2}\d{2})', string)13 if not match:14 raise argparse.ArgumentError('"' + string + '" is not a time range (YYYYMMDDHH-YYYYMMDDHH)!')15 return (pendulum.from_format(match.group(1), 'YYYYMMDDHH'), pendulum.from_format(match.group(2), 'YYYYMMDDHH'))16def parse_forecast_hours(string):17 match = re.match(r'(\d+)-(\d+)\+(\d+)', string)18 if match:19 start = int(match.group(1))20 end = int(match.group(2))21 step = int(match.group(3))22 return [x for x in range(start, end + step, step)]23 match = re.findall(r'(\d+):?', string)24 if match:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!