Skip to content

Commit

Permalink
Deepsource fixes (#19)
Browse files Browse the repository at this point in the history
* Ignore all virtual envs

* Fixed PYL-W0102

* State test_patterns

* Fix BAN-B101

* Fixed FLK-E501 regression

* Skip PYL-E1111

* Skip PYL-W0703

* Fix PYL-W0404

* Fix PYL-W0621

* Clean up unused imports

* Fix FLK-E266

* Better variable names

* Better global variable naming

* Fixed docstring utilities

* Fixed FLK-E501

* Cleaned up some trailing whitespace

* Fixed PYL-R1705

* Fixed FLK-W291

* Started adding missing docstrings

* Removed blank line whitespace

* Fix FLK-D200

* Fixed OrderedDict import

* Started fixing PYL-W0212

* Created FieldSchema for single-field data validation

* Silence PYL-W0212 (no alternative found)

* Added a note on our _get_current_object use

* Fixed some more PYL-W0212

* Fixed FLK-W293

* Fixed PYL-W0611

* Added dependency_file_paths
  • Loading branch information
jtc42 authored Feb 11, 2020
1 parent ab52c57 commit e3fae1d
Show file tree
Hide file tree
Showing 22 changed files with 294 additions and 112 deletions.
11 changes: 11 additions & 0 deletions .deepsource.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
version = 1

# Glob pattern of the test files
test_patterns = [
"tests/**",
"test_*.py"
]

[[analyzers]]
name = "python"
enabled = true

dependency_file_paths = [
"poetry.lock",
"pyproject.toml"
]

[analyzers.meta]
runtime_version = "3.x.x"
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ celerybeat.pid
*.sage.py

# Environments
.env
.venv
.env*
.venv*
env/
venv/
ENV/
Expand Down
23 changes: 13 additions & 10 deletions examples/nested_thing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@


class DataSet:
def __init__(self, xs, ys):
self.xs = xs
self.ys = ys
def __init__(self, x_values, y_values):
self.xs = x_values
self.ys = y_values


class DataSetSchema(Schema):
Expand All @@ -29,19 +29,19 @@ class DataSetSchema(Schema):

class MyComponent:
def __init__(self):
self.id = uuid.uuid4()
self.id = uuid.uuid4() # skipcq: PYL-C0103
self.x_range = range(-100, 100)
self.magic_denoise = 200

def noisy_pdf(self, x, mu=0.0, sigma=25.0):
def noisy_pdf(self, x_value, mu=0.0, sigma=25.0):
"""
Generate a noisy gaussian function (to act as some pretend data)
Our noise is inversely proportional to self.magic_denoise
"""
x = float(x - mu) / sigma
x_value = float(x_value - mu) / sigma
return (
math.exp(-x * x / 2.0) / math.sqrt(2.0 * math.pi) / sigma
math.exp(-x_value * x_value / 2.0) / math.sqrt(2.0 * math.pi) / sigma
+ (1 / self.magic_denoise) * random.random()
)

Expand All @@ -60,12 +60,15 @@ class MyComponentSchema(Schema):


"""
Create a view to view and change our magic_denoise value, and register is as a Thing property
Create a view to view and change our magic_denoise value,
and register is as a Thing property
"""


@ThingProperty # Register this view as a Thing Property
@PropertySchema( # Define the data we're going to output (get), and what to expect in (post)
# Register this view as a Thing Property
@ThingProperty
# Define the data we're going to output (get), and what to expect in (post)
@PropertySchema(
fields.Integer(
required=True,
example=200,
Expand Down
11 changes: 6 additions & 5 deletions examples/properties_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def get_state_schema(self):


"""
Create a view to view and change our magic_denoise value, and register is as a Thing property
Create a view to view and change our magic_denoise value,
and register is as a Thing property
"""


Expand All @@ -59,18 +60,18 @@ def get(self):
"""Show the current magic_denoise value"""

# When a GET request is made, we'll find our attached component
my_component = find_component("org.labthings.example.mycomponent")
return my_component.get_state()
found_my_component = find_component("org.labthings.example.mycomponent")
return found_my_component.get_state()

# Main function to handle PUT requests (update)
def put(self, new_property_value):
"""Change the current magic_denoise value"""

# Find our attached component
my_component = find_component("org.labthings.example.mycomponent")
found_my_component = find_component("org.labthings.example.mycomponent")

# Apply the new value
return my_component.set_state(new_property_value)
return found_my_component.set_state(new_property_value)


# Create LabThings Flask app
Expand Down
14 changes: 8 additions & 6 deletions examples/simple_thing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
ThingProperty,
PropertySchema,
use_args,
use_body,
marshal_task,
marshal_with,
)
from labthings.server.view import View
from labthings.server.find import find_component
Expand Down Expand Up @@ -64,12 +62,15 @@ def average_data(self, n: int):


"""
Create a view to view and change our magic_denoise value, and register is as a Thing property
Create a view to view and change our magic_denoise value,
and register is as a Thing property
"""


@ThingProperty # Register this view as a Thing Property
@PropertySchema( # Define the data we're going to output (get), and what to expect in (post)
# Register this view as a Thing Property
@ThingProperty
# Define the data we're going to output (get), and what to expect in (post)
@PropertySchema(
fields.Integer(
required=True,
example=200,
Expand Down Expand Up @@ -125,7 +126,8 @@ def get(self):

@ThingAction
class MeasurementAction(View):
# Expect JSON parameters in the request body. Pass to post function as dictionary argument.
# Expect JSON parameters in the request body.
# Pass to post function as dictionary argument.
@use_args(
{
"averages": fields.Integer(
Expand Down
11 changes: 6 additions & 5 deletions labthings/core/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

class StrictLock(object):
"""
Class that behaves like a Python RLock, but with stricter timeout conditions and custom exceptions.
Class that behaves like a Python RLock,
but with stricter timeout conditions and custom exceptions.
Args:
timeout (int): Time, in seconds, lock acquisition will wait before raising an exception
timeout (int): Time in seconds acquisition will wait before raising an exception
Attributes:
_lock (:py:class:`threading.RLock`): Parent RLock object
timeout (int): Time, in seconds, lock acquisition will wait before raising an exception
timeout (int): Time in seconds acquisition will wait before raising an exception
"""

def __init__(self, timeout=1):
Expand Down Expand Up @@ -46,11 +47,11 @@ class CompositeLock(object):
Args:
locks (list): List of parent RLock objects
timeout (int): Time, in seconds, lock acquisition will wait before raising an exception
timeout (int): Time in seconds acquisition will wait before raising an exception
Attributes:
locks (list): List of parent RLock objects
timeout (int): Time, in seconds, lock acquisition will wait before raising an exception
timeout (int): Time in seconds acquisition will wait before raising an exception
"""

def __init__(self, locks, timeout=1):
Expand Down
51 changes: 39 additions & 12 deletions labthings/core/tasks/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def tasks():
Returns:
list: List of tasks in default taskmaster
"""
global _default_task_master
return _default_task_master.tasks
global DEFAULT_TASK_MASTER
return DEFAULT_TASK_MASTER.tasks


def dict():
Expand All @@ -73,8 +73,8 @@ def dict():
Returns:
dict: Dictionary of tasks in default taskmaster
"""
global _default_task_master
return _default_task_master.dict
global DEFAULT_TASK_MASTER
return DEFAULT_TASK_MASTER.dict


def states():
Expand All @@ -83,38 +83,65 @@ def states():
Returns:
dict: Dictionary of task states in default taskmaster
"""
global _default_task_master
return _default_task_master.states
global DEFAULT_TASK_MASTER
return DEFAULT_TASK_MASTER.states


def cleanup_tasks():
global _default_task_master
return _default_task_master.cleanup()
"""Remove all finished tasks from the task list"""
global DEFAULT_TASK_MASTER
return DEFAULT_TASK_MASTER.cleanup()


def remove_task(task_id: str):
global _default_task_master
return _default_task_master.remove(task_id)
"""Remove a particular task from the task list
Arguments:
task_id {str} -- ID of the target task
"""
global DEFAULT_TASK_MASTER
return DEFAULT_TASK_MASTER.remove(task_id)


# Operations on the current task


def current_task():
"""Return the Task instance in which the caller is currently running.
If this function is called from outside a Task thread, it will return None.
Returns:
TaskThread -- Currently running Task thread.
"""
current_task_thread = threading.current_thread()
if not isinstance(current_task_thread, TaskThread):
return None
return current_task_thread


def update_task_progress(progress: int):
"""Update the progress of the Task in which the caller is currently running.
If this function is called from outside a Task thread, it will do nothing.
Arguments:
progress {int} -- Current progress, in percent (0-100)
"""
if current_task():
current_task().update_progress(progress)
else:
logging.info("Cannot update task progress of __main__ thread. Skipping.")


def update_task_data(data: dict):
"""Update the data of the Task in which the caller is currently running.
If this function is called from outside a Task thread, it will do nothing.
Arguments:
data {dict} -- Additional data to merge with the Task data
"""
if current_task():
current_task().update_data(data)
else:
Expand All @@ -132,7 +159,7 @@ def taskify(f):

@wraps(f)
def wrapped(*args, **kwargs):
task = _default_task_master.new(
task = DEFAULT_TASK_MASTER.new(
f, *args, **kwargs
) # Append to parent object's task list
task.start() # Start the function
Expand All @@ -142,4 +169,4 @@ def wrapped(*args, **kwargs):


# Create our default, protected, module-level task pool
_default_task_master = TaskMaster()
DEFAULT_TASK_MASTER = TaskMaster()
17 changes: 13 additions & 4 deletions labthings/core/tasks/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ThreadTerminationError(SystemExit):


class TaskThread(threading.Thread):
def __init__(self, target=None, name=None, args=(), kwargs={}, daemon=True):
def __init__(self, target=None, name=None, args=None, kwargs=None, daemon=True):
threading.Thread.__init__(
self,
group=None,
Expand All @@ -24,6 +24,11 @@ def __init__(self, target=None, name=None, args=(), kwargs={}, daemon=True):
kwargs=kwargs,
daemon=daemon,
)
# Handle arguments
if args is None:
args = ()
if kwargs is None:
kwargs = {}

# A UUID for the TaskThread (not the same as the threading.Thread ident)
self._ID = uuid.uuid4() # Task ID
Expand Down Expand Up @@ -96,7 +101,7 @@ def wrapped(*args, **kwargs):
try:
self._return_value = f(*args, **kwargs)
self._status = "success"
except Exception as e:
except Exception as e: # skipcq: PYL-W0703
logging.error(e)
logging.error(traceback.format_exc())
self._return_value = str(e)
Expand Down Expand Up @@ -133,7 +138,11 @@ def wait(self):
def async_raise(self, exc_type):
"""Raise an exception in this thread."""
# Should only be called on a started thread, so raise otherwise.
assert self.ident is not None, "Only started threads have thread identifier"
if self.ident is None:
raise RuntimeError(
"Cannot halt a thread that hasn't started. "
"No valid running thread identifier."
)

# If the thread has died we don't want to raise an exception so log.
if not self.is_alive():
Expand Down Expand Up @@ -166,7 +175,7 @@ def _is_thread_proc_running(self):
Returns:
bool: If _thread_proc is currently running
"""
could_acquire = self._running_lock.acquire(0)
could_acquire = self._running_lock.acquire(False) # skipcq: PYL-E1111
if could_acquire:
self._running_lock.release()
return False
Expand Down
Loading

0 comments on commit e3fae1d

Please sign in to comment.