documenting

This commit is contained in:
Martin Diehl 2019-09-19 12:32:15 -07:00
parent 2e25a03d13
commit f1f88610dd
2 changed files with 143 additions and 34 deletions

View File

@ -66,7 +66,20 @@ class DADF5():
def __manage_visible(self,datasets,what,action):
"""Manages the visibility of the groups."""
"""
Manages the visibility of the groups.
Parameters
----------
datasets : list of str or Boolean
name of datasets as list, supports ? and * wildcards.
True is equivalent to [*], False is equivalent to []
what : str
attribute to change (must be in self.visible)
action : str
select from 'set', 'add', and 'del'
"""
# allow True/False and string arguments
if datasets is True:
datasets = ['*']
@ -74,7 +87,7 @@ class DADF5():
datasets = []
choice = [datasets] if isinstance(datasets,str) else datasets
valid = [e for e_ in [glob.fnmatch.filter(getattr(self,what) ,s) for s in choice] for e in e_]
valid = [e for e_ in [glob.fnmatch.filter(getattr(self,what),s) for s in choice] for e in e_]
existing = set(self.visible[what])
if action == 'set':
@ -94,19 +107,60 @@ class DADF5():
def set_by_time(self,start,end):
"""
Sets active time increments based on start and end time.
Parameters
----------
start : float
start time (included)
end : float
end time (exclcuded)
"""
self.__manage_visible(self.__time_to_inc(start,end),'increments','set')
def add_by_time(self,start,end):
"""
Adds to active time increments based on start and end time.
Parameters
----------
start : float
start time (included)
end : float
end time (exclcuded)
"""
self.__manage_visible(self.__time_to_inc(start,end),'increments','add')
def del_by_time(self,start,end):
"""
Delets from active time increments based on start and end time.
Parameters
----------
start : float
start time (included)
end : float
end time (exclcuded)
"""
self.__manage_visible(self.__time_to_inc(start,end),'increments','del')
def iter_visible(self,what):
"""Iterates over visible items by setting each one visible."""
"""
Iterates over visible items by setting each one visible.
Parameters
----------
what : str
attribute to change (must be in self.visible)
"""
datasets = self.visible[what]
last_datasets = datasets.copy()
for dataset in datasets:
@ -120,14 +174,50 @@ class DADF5():
def set_visible(self,what,datasets):
"""
Sets active groups.
Parameters
----------
datasets : list of str or Boolean
name of datasets as list, supports ? and * wildcards.
True is equivalent to [*], False is equivalent to []
what : str
attribute to change (must be in self.visible)
"""
self.__manage_visible(datasets,what,'set')
def add_visible(self,what,datasets):
"""
Adds to active groups.
Parameters
----------
datasets : list of str or Boolean
name of datasets as list, supports ? and * wildcards.
True is equivalent to [*], False is equivalent to []
what : str
attribute to change (must be in self.visible)
"""
self.__manage_visible(datasets,what,'add')
def del_visible(self,what,datasets):
"""
Removes from active groupse.
Parameters
----------
datasets : list of str or Boolean
name of datasets as list, supports ? and * wildcards.
True is equivalent to [*], False is equivalent to []
what : str
attribute to change (must be in self.visible)
"""
self.__manage_visible(datasets,what,'del')

View File

@ -205,6 +205,14 @@ class return_message():
"""Object with formatted return message."""
def __init__(self,message):
"""
Sets return message.
Parameters
----------
message : str or list of str
message for output to screen
"""
self.message = message
def __repr__(self):
@ -468,45 +476,56 @@ def curve_fit_bound(f, xdata, ydata, p0=None, sigma=None, bounds=None, **kw):
return (popt, pcov, infodict, errmsg, ier) if return_full else (popt, pcov)
class Worker(Thread):
class ThreadPool:
"""Pool of threads consuming tasks from a queue."""
class Worker(Thread):
"""Thread executing tasks from a given tasks queue."""
def __init__(self, tasks):
Thread.__init__(self)
self.tasks = tasks
self.daemon = True
self.start()
""" Worker for tasks."""
Thread.__init__(self)
self.tasks = tasks
self.daemon = True
self.start()
def run(self):
while True:
func, args, kargs = self.tasks.get()
try:
func(*args, **kargs)
except Exception as e:
# An exception happened in this thread
print(e)
finally:
# Mark this task as done, whether an exception happened or not
self.tasks.task_done()
while True:
func, args, kargs = self.tasks.get()
try:
func(*args, **kargs)
except Exception as e:
# An exception happened in this thread
print(e)
finally:
# Mark this task as done, whether an exception happened or not
self.tasks.task_done()
class ThreadPool:
"""Pool of threads consuming tasks from a queue."""
def __init__(self, num_threads):
"""
Thread pool.
def __init__(self, num_threads):
self.tasks = Queue(num_threads)
for _ in range(num_threads):
Worker(self.tasks)
Parameters
----------
num_threads : int
number of threads
def add_task(self, func, *args, **kargs):
"""Add a task to the queue."""
self.tasks.put((func, args, kargs))
"""
self.tasks = Queue(num_threads)
for _ in range(num_threads):
self.Worker(self.tasks)
def map(self, func, args_list):
"""Add a list of tasks to the queue."""
for args in args_list:
self.add_task(func, args)
def add_task(self, func, *args, **kargs):
"""Add a task to the queue."""
self.tasks.put((func, args, kargs))
def wait_completion(self):
"""Wait for completion of all the tasks in the queue."""
self.tasks.join()
def map(self, func, args_list):
"""Add a list of tasks to the queue."""
for args in args_list:
self.add_task(func, args)
def wait_completion(self):
"""Wait for completion of all the tasks in the queue."""
self.tasks.join()