Skip to content

Commit

Permalink
#155: Make things missed by Black mostly abide by 88 characters
Browse files Browse the repository at this point in the history
- Black mostly missed docstrings and some really long strings, but that's understandable. Black doesn't directly edit strings for fear of editing their contents
  • Loading branch information
MRichards99 committed Aug 13, 2020
1 parent 93b4d85 commit 4aff02d
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 87 deletions.
40 changes: 29 additions & 11 deletions common/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def logout(self, session_id):
@abstractmethod
def get_with_filters(self, session_id, entity_type, filters):
"""
Given a list of filters supplied in json format, returns entities that match the filters for the given entity type
Given a list of filters supplied in json format, returns entities that match the
filters for the given entity type
:param session_id: The session id of the requesting user
:param entity_type: The type of entity
:param filters: The list of filters to be applied
Expand All @@ -55,7 +57,9 @@ def get_with_filters(self, session_id, entity_type, filters):
@abstractmethod
def create(self, session_id, entity_type, data):
"""
Create one or more entities, from the given list containing json. Each entity must not contain its ID
Create one or more entities, from the given list containing json. Each entity
must not contain its ID
:param session_id: The session id of the requesting user
:param entity_type: The type of entity
:param data: The entities to be created
Expand All @@ -66,7 +70,9 @@ def create(self, session_id, entity_type, data):
@abstractmethod
def update(self, session_id, entity_type, data):
"""
Update one or more entities, from the given list containing json. Each entity must contain its ID
Update one or more entities, from the given list containing json. Each entity
must contain its ID
:param session_id: The session id of the requesting user
:param entity_type: The type of entity
:param data: the list of updated values or a dictionary
Expand All @@ -77,7 +83,8 @@ def update(self, session_id, entity_type, data):
@abstractmethod
def get_one_with_filters(self, session_id, entity_type, filters):
"""
returns the first entity that matches a given filter, for a given entity type
Returns the first entity that matches a given filter, for a given entity type
:param session_id: The session id of the requesting user
:param entity_type: The type of entity
:param filters: the filter to be applied to the query
Expand All @@ -88,7 +95,9 @@ def get_one_with_filters(self, session_id, entity_type, filters):
@abstractmethod
def count_with_filters(self, session_id, entity_type, filters):
"""
returns the count of the entities that match a given filter for a given entity type
Returns the count of the entities that match a given filter for a given entity
type
:param session_id: The session id of the requesting user
:param entity_type: The type of entity
:param filters: the filters to be applied to the query
Expand All @@ -100,6 +109,7 @@ def count_with_filters(self, session_id, entity_type, filters):
def get_with_id(self, session_id, entity_type, id):
"""
Gets the entity matching the given ID for the given entity type
:param session_id: The session id of the requesting user
:param entity_type: The type of entity
:param id: the id of the record to find
Expand All @@ -111,6 +121,7 @@ def get_with_id(self, session_id, entity_type, id):
def delete_with_id(self, session_id, entity_type, id):
"""
Deletes the row matching the given ID for the given entity type
:param session_id: The session id of the requesting user
:param table: the table to be searched
:param id: the id of the record to delete
Expand All @@ -121,6 +132,7 @@ def delete_with_id(self, session_id, entity_type, id):
def update_with_id(self, session_id, entity_type, id, data):
"""
Updates the row matching the given ID for the given entity type
:param session_id: The session id of the requesting user
:param entity_type: The type of entity
:param data: The dictionary that the entity should be updated with
Expand All @@ -133,7 +145,9 @@ def get_instrument_facilitycycles_with_filters(
self, session_id, instrument_id, filters
):
"""
Given an instrument_id get facility cycles where the instrument has investigations that occur within that cycle
Given an instrument_id get facility cycles where the instrument has
investigations that occur within that cycle
:param session_id: The session id of the requesting user
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
Expand All @@ -146,8 +160,9 @@ def count_instrument_facilitycycles_with_filters(
self, session_id, instrument_id, filters
):
"""
Given an instrument_id get the facility cycles count where the instrument has investigations that occur within
that cycle
Given an instrument_id get the facility cycles count where the instrument has
investigations that occur within that cycle
:param session_id: The session id of the requesting user
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
Expand All @@ -160,7 +175,9 @@ def get_instrument_facilitycycle_investigations_with_filters(
self, session_id, instrument_id, facilitycycle_id, filters
):
"""
Given an instrument id and facility cycle id, get investigations that use the given instrument in the given cycle
Given an instrument id and facility cycle id, get investigations that use the
given instrument in the given cycle
:param session_id: The session id of the requesting user
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
Expand All @@ -174,8 +191,9 @@ def count_instrument_facilitycycles_investigations_with_filters(
self, session_id, instrument_id, facilitycycle_id, filters
):
"""
Given an instrument id and facility cycle id, get the count of the investigations that use the given instrument in
the given cycle
Given an instrument id and facility cycle id, get the count of the
investigations that use the given instrument in the given cycle
:param session_id: The session id of the requesting user
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
Expand Down
76 changes: 51 additions & 25 deletions common/database/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@
PythonICATIncludeFilter as IncludeFilter,
)
else:
# TODO - Check this way of string formatting works
raise ApiError(
"Cannot select which implementation of filters to import, check the config file has a valid backend type"
"Cannot select which implementation of filters to import, check the config file"
" has a valid backend type"
)

log = logging.getLogger()
Expand Down Expand Up @@ -84,8 +86,9 @@ def wrapper_requires_session(*args, **kwargs):

class Query(ABC):
"""
The base query class that all other queries extend from. This defines the enter and exit methods, used to handle
sessions. It is expected that all queries would be used with the 'with' keyword in most cases for this reason.
The base query class that all other queries extend from. This defines the enter and
exit methods, used to handle sessions. It is expected that all queries would be used
with the 'with' keyword in most cases for this reason.
"""

@abstractmethod
Expand Down Expand Up @@ -160,7 +163,9 @@ def __init__(self, table, row):
self.inserted_row = None

def execute_query(self):
"""Determines if the row is a row object or dictionary then commits it to the table"""
"""
Determines if the row is a row object or dictionary then commits it to the table
"""
if type(self.row) is not dict:
record = self.row
else:
Expand Down Expand Up @@ -206,10 +211,10 @@ def get_query_filter(filter):
"""
Given a filter return a matching Query filter object
This factory is not in common.filters so the created filter can be for the correct backend.
Moving the factory into that file would mean the filters would be based off the abstract
classes (because they're in the same file) which won't enable filters to be unique to the
backend
This factory is not in common.filters so the created filter can be for the
correct backend. Moving the factory into that file would mean the filters would
be based off the abstract classes (because they're in the same file) which won't
enable filters to be unique to the backend
:param filter: dict - The filter to create the QueryFilter for
:return: The QueryFilter object created
Expand Down Expand Up @@ -260,8 +265,10 @@ def create_row_from_json(table, data):

def create_rows_from_json(table, data):
"""
Given a List containing dictionary representations of entities, or a dictionary representation of an entity, insert
the entities into the table and return the created entities
Given a List containing dictionary representations of entities, or a dictionary
representation of an entity, insert the entities into the table and return the
created entities
:param table: The table to insert the entities in
:param data: The entities to be inserted
:return: The inserted entities
Expand All @@ -273,7 +280,9 @@ def create_rows_from_json(table, data):

def get_row_by_id(table, id):
"""
Gets the row matching the given ID from the given table, raises MissingRecordError if it can not be found
Gets the row matching the given ID from the given table, raises MissingRecordError
if it can not be found
:param table: the table to be searched
:param id: the id of the record to find
:return: the record retrieved
Expand All @@ -287,7 +296,9 @@ def get_row_by_id(table, id):

def delete_row_by_id(table, id):
"""
Deletes the row matching the given ID from the given table, raises MissingRecordError if it can not be found
Deletes the row matching the given ID from the given table, raises
MissingRecordError if it can not be found
:param table: the table to be searched
:param id: the id of the record to delete
"""
Expand All @@ -300,6 +311,7 @@ def delete_row_by_id(table, id):
def update_row_from_id(table, id, new_values):
"""
Updates a record in a table
:param table: The table the record is in
:param id: The id of the record
:param new_values: A JSON string containing what columns are to be updated
Expand All @@ -311,7 +323,9 @@ def update_row_from_id(table, id, new_values):

def get_filtered_read_query_results(filter_handler, filters, query):
"""
Given a filter handler, list of filters and a query. Apply the filters and execute the query
Given a filter handler, list of filters and a query. Apply the filters and execute
the query
:param filter_handler: The filter handler to apply the filters
:param filters: The filters to be applied
:param query: The query for the filters to be applied to
Expand All @@ -329,8 +343,9 @@ def get_filtered_read_query_results(filter_handler, filters, query):

def _get_results_with_include(filters, results):
"""
Given a list of entities and a list of filters, use the include filter to nest the included entities requested in
the include filter given
Given a list of entities and a list of filters, use the include filter to nest the
included entities requested in the include filter given
:param filters: The list of filters
:param results: The list of entities
:return: A list of nested dictionaries representing the entity results
Expand All @@ -342,8 +357,9 @@ def _get_results_with_include(filters, results):

def _get_distinct_fields_as_dicts(results):
"""
Given a list of column results return a list of dictionaries where each column name is the key and the column value
is the dictionary key value
Given a list of column results return a list of dictionaries where each column name
is the key and the column value is the dictionary key value
:param results: A list of sql alchemy result objects
:return: A list of dictionary representations of the sqlalchemy result objects
"""
Expand All @@ -356,7 +372,9 @@ def _get_distinct_fields_as_dicts(results):

def get_rows_by_filter(table, filters):
"""
Given a list of filters supplied in json format, returns entities that match the filters from the given table
Given a list of filters supplied in json format, returns entities that match the
filters from the given table
:param table: The table to checked
:param filters: The list of filters to be applied
:return: A list of the rows returned in dictionary form
Expand Down Expand Up @@ -395,7 +413,9 @@ def get_filtered_row_count(table, filters):

def patch_entities(table, json_list):
"""
Update one or more rows in the given table, from the given list containing json. Each entity must contain its ID
Update one or more rows in the given table, from the given list containing json.
Each entity must contain its ID
:param table: The table of the entities
:param json_list: the list of updated values or a dictionary
:return: The list of updated rows.
Expand Down Expand Up @@ -440,7 +460,9 @@ def __init__(self, instrument_id):

def get_facility_cycles_for_instrument(instrument_id, filters):
"""
Given an instrument_id get facility cycles where the instrument has investigations that occur within that cycle
Given an instrument_id get facility cycles where the instrument has investigations
that occur within that cycle
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
:return: A list of facility cycle entities
Expand Down Expand Up @@ -469,8 +491,9 @@ def __init__(self, instrument_id):

def get_facility_cycles_for_instrument_count(instrument_id, filters):
"""
Given an instrument_id get the facility cycles count where the instrument has investigations that occur within
that cycle
Given an instrument_id get the facility cycles count where the instrument has
investigations that occur within that cycle
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
:return: The count of the facility cycles
Expand Down Expand Up @@ -504,7 +527,9 @@ def get_investigations_for_instrument_in_facility_cycle(
instrument_id, facility_cycle_id, filters
):
"""
Given an instrument id and facility cycle id, get investigations that use the given instrument in the given cycle
Given an instrument id and facility cycle id, get investigations that use the given
instrument in the given cycle
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
:param facility_cycle_id: the ID of the facility cycle
Expand Down Expand Up @@ -539,8 +564,9 @@ def get_investigations_for_instrument_in_facility_cycle_count(
instrument_id, facility_cycle_id, filters
):
"""
Given an instrument id and facility cycle id, get the count of the investigations that use the given instrument in
the given cycle
Given an instrument id and facility cycle id, get the count of the investigations
that use the given instrument in the given cycle
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
:param facility_cycle_id: the ID of the facility cycle
Expand Down
3 changes: 2 additions & 1 deletion common/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def __init__(self, included_filters):

class FilterOrderHandler(object):
"""
The FilterOrderHandler takes in filters, sorts them according to the order of operations, then applies them.
The FilterOrderHandler takes in filters, sorts them according to the order of
operations, then applies them.
"""

def __init__(self):
Expand Down
4 changes: 3 additions & 1 deletion common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ def is_valid_json(string):

def get_filters_from_query_string():
"""
Gets a list of filters from the query_strings arg,value pairs, and returns a list of QueryFilter Objects
Gets a list of filters from the query_strings arg,value pairs, and returns a list of
QueryFilter Objects
:return: The list of filters
"""
log.info(" Getting filters from query string")
Expand Down
10 changes: 6 additions & 4 deletions common/icat/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ class PythonICATBackend(Backend):
"""

def __init__(self):
# Client object is created here as well as in login() to avoid uncaught exceptions
# where the object is None. This could happen where a user tries to use an endpoint before
# logging in. Also helps to give a bit of certainty to what's stored here
# Client object is created here as well as in login() to avoid uncaught
# exceptions where the object is None. This could happen where a user tries to
# use an endpoint before logging in. Also helps to give a bit of certainty to
# what's stored here
self.client = icat.client.Client(
config.get_icat_url(), checkCert=config.get_icat_check_cert()
)

def login(self, credentials):
# Client object is re-created here so session IDs aren't overwritten in the database
# Client object is re-created here so session IDs aren't overwritten in the
# database
self.client = icat.client.Client(
config.get_icat_url(), checkCert=config.get_icat_check_cert()
)
Expand Down
Loading

0 comments on commit 4aff02d

Please sign in to comment.