def append_new_line(self, str_log_line): """Append log line to log file on a new line UNIT TESTED Creates the folders along the path and the log file if it doesn't exist and adds the log line as the last line of the file on a new line Args: str_log_line: A string that you wish to send to add to the log file Returns: None Raises: Assertion if you send a non-String as an argument """ assert isinstance( str_log_line, str ), "PYMADEEASY LOG FAIL! You sent a non-String to append_new_line()" if self.debug: p.print_str(str_log_line) if self.logging: os.create_folders_along_path(self.location) with open(self.location, "a") as f: f.write(str_log_line) f.write("\n")
def test_log(): p.print_message_highlighted('TESTING LOG.PY') dict_log_config = dict(str_filename='test_log.py', bool_debug=True, str_log_directory='log/', str_function='test_log') p.print_str(str(dict_log_config)) this_log = Log.Log(**dict_log_config) this_log.start() this_log.change_debug(False) this_log.append('DEBUG=False SHOULD NOT SEE THIS IN CONSOLE') this_log.change_debug(True) this_log.append('DEBUG=True OUTPUT BACK TO CONSOLE') assert this_log.debug, "PYMADEEASY LOG FAIL! The Log debug did not change to True!" temp_function_change_str = 'testing_test_log' this_log.change_function(temp_function_change_str) this_log.append('<==testing_test_log') assert this_log.function == temp_function_change_str, "PYMADEEASY LOG FAIL! The Log function name was not changed!" temp_function_change_str = 'test_log' this_log.change_function(temp_function_change_str) this_log.append('<==test_log') assert this_log.function == temp_function_change_str, "PYMADEEASY LOG FAIL! The Log function was not changed!" this_log.change_filename('testing_filename_test_log.py') this_log.append('FILENAME CHANGE') temp_file_name_change_str = 'test_log.py' this_log.change_filename(temp_file_name_change_str) assert this_log.filename == temp_file_name_change_str, "PYMADEEASY LOG FAIL! The Log filename was not changed!" this_log.finish()
def __init__(self, str_filename='', bool_debug=False, bool_off=False, str_log_directory='log/', str_function='', str_separator='|'): """Initialize the log file and class object UNIT TESTED Creates a local Log file and Log class object used to write logging and error statements to a local file for debugging purposes. Args: str_filename: A string used to set filename attribute bool_debug: A boolean used to set debug attribute bool_off: A boolean used to turn off all write operations for this Log object str_log_directory: A string used to designate where the log files written by this class should go str_function: A string used to set function attribute str_separator: A string used to set separator attribute Returns: An example string: 2019-04-27 12:03:21.037656 or the datetime object Raises: None Attributes: self.start_time: A string used to capture when the logging started and calculate overall run time self.filename: A string used to as the name of the file you are logging with this class and is used for logging statements written to the log file this class creates self.location: A string used as the location where the log file should be written self.debug: A boolean used to print log statements out to the console self.logging: A boolean, if True will write to file. If False, all write to file operations will be stopped self.function: A string used as the name of the function you are logging with this class and is used for logging statements written to the log file this class creates self.separator: A string used to separate the filename from the function from the log statement when writing to the file """ self.start_time = os.get_current_time() self.filename = str_filename self.location = os.get_current_date_time( as_string=True, now_format='file') + self.filename + '.log' self.debug = bool_debug self.function = str_function self.separator = str_separator self.logging = not bool_off self.location = "%s%s" % (str_log_directory, self.location) if self.debug: p.print_str('log.py' + self.separator + 'self.location (full name)' + self.separator + self.location) self.append('LOG CREATED')
def append(self, str_log_line): """Append a log line to the log file UNIT TESTED Creates the folders along the path and the log file if it doesn't exist and adds the log line as the last line of the file Args: str_log_line: A string that you wish to send to add to the log file Returns: None Raises: Assertion if you send a non-String as an argument """ assert isinstance( str_log_line, str), "PYMADEEASY LOG FAIL! You sent a non-string to append()" log_line = os.get_current_date_time(as_string=True, now_format='log') + self.separator # https://stackoverflow.com/questions/1557571/how-do-i-get-time-of-a-python-programs-execution log_line = log_line + str( round(os.get_current_time() - self.start_time, 2)) + ' sec' log_line = log_line + self.separator + self.filename log_line = log_line + self.separator + self.function log_line = log_line + self.separator + str_log_line if self.debug: p.print_str(log_line) if self.logging: os.create_folders_along_path(self.location) with open(self.location, "a") as f: f.write(log_line) f.write("\n")
def test_get_api(str_uri='https://httpbin.org/get'): p.print_message_highlighted('TESTING API.PY') dict_log_config = dict(str_filename='test_api.py', bool_debug=True, str_log_directory='log/', str_function='test_api', str_separator='|:|') this_log = Log.Log(**dict_log_config) this_log.append('START') this_log.append(str_uri) this_log.append('Testing get_api_response function') response = api.get_api_response(str_uri) field_width_int = 15 type_width_int = 6 if response.status_code != 200: fstr_output = f""" ---------------------------------------------- {'API FUNCTION RESPONSE DETAILS':>30} {'FIELD':>{field_width_int}}|{'TYPE':^{type_width_int}}|DETAILS ---------------------------------------------- {'Status Code':>{field_width_int}}|{'int':^{type_width_int}}|{response.status_code} ---------------------------------------------- {'Header':>{field_width_int}}|{'dict':^{type_width_int}}|{response.headers} ---------------------------------------------- {'History':>{field_width_int}}|{'str':^{type_width_int}}|{response.history} ---------------------------------------------- {'Response':>{field_width_int}}|{'str':^{type_width_int}}|{response.text} ---------------------------------------------- {'Response':>{field_width_int}}|{'dict':^{type_width_int}}|{str('')}""" else: fstr_output = f""" ---------------------------------------------- {'API FUNCTION RESPONSE DETAILS':>30} {'FIELD':>{field_width_int}}|{'TYPE':^{type_width_int}}|DETAILS ----------------------------------------------- {'Status Code':>{field_width_int}}|{'int':^{type_width_int}}|{response.status_code} ---------------------------------------------- {'Header':>{field_width_int}}|{'dict':^{type_width_int}}|{response.headers} ---------------------------------------------- {'History':>{field_width_int}}|{'str':^{type_width_int}}|{response.history} ---------------------------------------------- {'Response':>{field_width_int}}|{'str':^{type_width_int}}|{response.text} ---------------------------------------------- {'Response':>{field_width_int}}|{'dict':^{type_width_int}}|{str(response.json())}""" this_log.append(fstr_output) if response.status_code == 200: pass else: this_log.append('get_api_response function FAILED') this_log.append('Testing API class') response_class = api.APIResponse(str_url=str_uri) response_class.get() this_log.append(f'{response_class}') if response_class.status_str == 'OK': this_log.append('FINISH') return else: p.print_str('API ERROR: Check logs!') print
def test_operating_system_functions(): p.print_message_highlighted('TESTING OPERATING_SYSTEM_FUNCTIONS.PY') p.print_str("This should be the exact time now as a string: %s" % os.get_current_date_time(as_string=True)) os.run_command('ls -la') p.print_str( "Above should be the list of files in the current directory: run_command test" ) p.print_str(os.convert_string_to_url("I can't get no satisfaction!")) p.print_str( "Above should be: I-cant-get-no-satisfaction: convert_string_to_url test" ) test_string = "This is my tweet http://example.com/blah check it out" p.print_str(test_string) p.print_str(os.find_url_in_string(test_string)) p.print_str("Above should be the URL in the statement above it") p.print_str("The current working directory is %s" % os.get_current_working_directory()) p.print_str( "It correctly returned a string: %s" % str(os.check_variable_type(os.get_current_working_directory(), str)))
assert_is_data_frame(data_frame, called_by='value_is_in_data_frame_index') return value in data_frame.index # NOT UNIT TESTED def value_is_a_data_frame_column_name(data_frame, value): assert_is_data_frame(data_frame, called_by='value_is_a_data_frame_column_name') return value in data_frame.columns if __name__ == '__main__': p.print_message_standard('START data_frame_functions.py') temp_df = create_blank_data_frame() assert_is_data_frame(temp_df, called_by='data_frame_functions.py') p.print_str('temp_df blank DataFrame created') assert is_data_frame( temp_df), "is_data_frame() FAIL! temp_df is not a DataFrame!" p.print_str('temp_df is_data_frame()') assert is_empty_data_frame( temp_df), "is_data_frame_empty() FAIL! temp_df should be empty!" p.print_str('temp_df is_empty_data_frame()') p.print_str('temp_df output should be empty:') p.print_str(output_data_frame_as_string(temp_df)) p.print_standard_line() # example inspired by https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html p.print_str( 'DataFrame objects are a collection of Series objects with a common key known as an "index"' ) p.print_str('Dictionaries are a "key: value" pair data structure') p.print_standard_line()