Skip to content

Commit

Permalink
Convert ThreadWithResult staticmethods into module level functions
Browse files Browse the repository at this point in the history
- notice that a module level function with 2 leading underscores CANNOT
  be called from inside a class/instance method
  - attempting to call a module level function with 2 leading underscores
    from inside the class/instance will result in a `NameError` such as:

`NameError: name '_ThreadWithResult__time_perf_counter' is not defined`
`NameError: name '_ThreadWithResult_format_perf_counter_info' is not defined`
  • Loading branch information
shailshouryya committed Feb 15, 2023
1 parent 37c9a44 commit f076b16
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions python/save_thread_result/__init__.py
Expand Up @@ -171,7 +171,7 @@ def closure_function():
log_condition = self.log_thread_status is True or self.log_files is not None
if log_condition:
time_start = time.time()
perf_counter_start = self.__time_perf_counter()
perf_counter_start = _time_perf_counter()
thread_name = '[' + threading.current_thread().name + ']'
utc_offset = time.strftime('%z')
now = lambda: datetime.now().isoformat() + utc_offset + ' '
Expand All @@ -180,8 +180,8 @@ def closure_function():
self.result = target(*args, **kwargs)
if log_condition:
time_end = time.time()
perf_counter_end = self.__time_perf_counter()
formatted_perf = self.__format_perf_counter_info(perf_counter_start, perf_counter_end)
perf_counter_end = _time_perf_counter()
formatted_perf = _format_perf_counter_info(perf_counter_start, perf_counter_end)
message = now() + thread_name.rjust(12) + ' Finished thread! This thread took ' + str(time_end - time_start) + ' time.time() seconds' + formatted_perf + ' to complete.'
self.__log(message)
if sys.version_info.major == 3 and sys.version_info.minor >= 10:
Expand Down Expand Up @@ -245,15 +245,13 @@ def __log(self, message):
print(message)


# use helper functions for time.perf_counter() since function became available only after python release 3.3
@staticmethod
def __time_perf_counter():
if sys.version_info.major == 3 and sys.version_info.minor >= 3:
return time.perf_counter()
return None
# use helper functions for time.perf_counter() since function became available only after python release 3.3
def _time_perf_counter():
if sys.version_info.major == 3 and sys.version_info.minor >= 3:
return time.perf_counter()
return None

@staticmethod
def __format_perf_counter_info(perf_counter_start, perf_counter_end):
if sys.version_info.major == 3 and sys.version_info.minor >= 3:
return ' (' + str(perf_counter_end - perf_counter_start) + ' time.perf_counter() seconds)'
return ''
def _format_perf_counter_info(perf_counter_start, perf_counter_end):
if sys.version_info.major == 3 and sys.version_info.minor >= 3:
return ' (' + str(perf_counter_end - perf_counter_start) + ' time.perf_counter() seconds)'
return ''

0 comments on commit f076b16

Please sign in to comment.