Skip to content

Commit

Permalink
Blackened
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Jan 20, 2020
1 parent f00ddf3 commit daed3c9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
10 changes: 3 additions & 7 deletions examples/properties_dictionary.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from fractions import Fraction

from labthings.server.quick import create_app
from labthings.server.decorators import (
ThingProperty,
PropertySchema,
)
from labthings.server.decorators import ThingProperty, PropertySchema
from labthings.server.view import View
from labthings.server.find import find_component
from labthings.server.types import data_dict_to_schema
Expand All @@ -29,9 +26,7 @@ def __init__(self):
self.some_property = Fraction(5, 2)
self.some_string = "Hello"

self.prop_keys = [
"magic_denoise", "some_property", "some_string", "x_range"
]
self.prop_keys = ["magic_denoise", "some_property", "some_string", "x_range"]

def get_state(self):
return {key: getattr(self, key) for key in self.prop_keys}
Expand All @@ -46,6 +41,7 @@ def get_state_schema(self):
s = data_dict_to_schema(self.get_state())
return s


my_component = MyComponent()


Expand Down
5 changes: 4 additions & 1 deletion labthings/core/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ def rapply(data, func, apply_to_iterables=True):

# If the object is a dictionary
if isinstance(data, collections.abc.Mapping):
return {key: rapply(val, func, apply_to_iterables=apply_to_iterables) for key, val in data.items()}
return {
key: rapply(val, func, apply_to_iterables=apply_to_iterables)
for key, val in data.items()
}
# If the object is iterable but NOT a dictionary or a string
elif apply_to_iterables and (
isinstance(data, collections.abc.Iterable)
Expand Down
4 changes: 3 additions & 1 deletion labthings/server/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def __init__(self, schema, code=200):
elif isinstance(self.schema, Schema):
self.converter = self.schema.jsonify
else:
raise TypeError(f"Unsupported schema type {type(self.schema)} for marshal_with")
raise TypeError(
f"Unsupported schema type {type(self.schema)} for marshal_with"
)

def __call__(self, f):
# Pass params to call function attribute for external access
Expand Down
7 changes: 5 additions & 2 deletions labthings/server/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def make_primative(value):
global DEFAULT_BUILTIN_CONVERSIONS, DEFAULT_TYPE_MAPPING

logging.debug(f"Converting {value} to primative type...")
value_typestrings = [x.__module__+"."+x.__name__ for x in inspect.getmro(type(value))]
value_typestrings = [
x.__module__ + "." + x.__name__ for x in inspect.getmro(type(value))
]

for typestring in value_typestrings:
if typestring in DEFAULT_BUILTIN_CONVERSIONS:
Expand Down Expand Up @@ -108,6 +110,7 @@ def data_dict_to_schema(data_dict):

return working_dict


# TODO: Deserialiser with inverse defaults
# TODO: Option to switch to .npy serialisation/deserialisation (or look for a better common array format)

Expand All @@ -126,4 +129,4 @@ def data_dict_to_schema(data_dict):
"val4": range(1, 5)
}
"""
"""
12 changes: 9 additions & 3 deletions labthings/server/views/docs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,21 @@ def get(self):
if hasattr(prop, "get") and not (
hasattr(prop, "post") or hasattr(prop, "put")
):
prop_schema = convert_schema(get_spec(prop.get).get("_schema").get(200), swag)
prop_schema = convert_schema(
get_spec(prop.get).get("_schema").get(200), swag
)
# If prop is write-only
elif not hasattr(prop, "get") and (
hasattr(prop, "post") or hasattr(prop, "put")
):
if hasattr(prop, "post"):
prop_schema = convert_schema(get_spec(prop.post).get("_params"), swag)
prop_schema = convert_schema(
get_spec(prop.post).get("_params"), swag
)
elif hasattr(prop, "put"):
prop_schema = convert_schema(get_spec(prop.put).get("_params"), swag)
prop_schema = convert_schema(
get_spec(prop.put).get("_params"), swag
)
else:
prop_schema = {}

Expand Down

0 comments on commit daed3c9

Please sign in to comment.