- Minor fixes related to pep8

- Using list comprehension instead lambda expression to make the code more readable
- Removed SkillSetting since this class is not business related
pull/30/head
Matheus Lima 2018-12-26 20:03:56 -03:00
parent acb77b1566
commit 3b4483275c
6 changed files with 18 additions and 29 deletions

View File

@ -1,2 +1,2 @@
from .device import get_device_by_id
from .device import get_devices_by_account_id
from .device import get_devices_by_account_id

View File

@ -27,7 +27,7 @@ def get_device_by_id(db, device_id: str) -> Device:
:param db: psycopg2 connection to mycroft database
:param device_id: uuid
:return:
:return: Device entity
"""
query = DatabaseQuery(
file_path=path.join(SQL_DIR, 'get_device_by_id.sql'),
@ -43,7 +43,7 @@ def get_devices_by_account_id(db, account_id: str) -> list[Device]:
:param db: psycopg2 connection to mycroft database
:param account_id: uuid
:return:
:return: List of User's devices
"""
query = DatabaseQuery(
file_path=path.join(SQL_DIR, 'get_devices_by_account_id.sql'),
@ -51,4 +51,4 @@ def get_devices_by_account_id(db, account_id: str) -> list[Device]:
singleton=False
)
sql_results = fetch(db, query)
return list(map(lambda result: Device(**result), sql_results))
return [Device(**result) for result in sql_results]

View File

@ -1 +1 @@
SELECT * FROM device.device WHERE id = %(device_id)s
SELECT * FROM device.device WHERE id = %(device_id)s

View File

@ -1 +1 @@
SELECT * FROM device.device WHERE account_id = %(account_id)s
SELECT * FROM device.device WHERE account_id = %(account_id)s

View File

@ -1,3 +1 @@
from .skill import get_setting_sections_by_device_id_and_setting_version
from .skill import get_setting_by_section_id
from .skill import get_setting_by_device_id_and_setting_version_hash

View File

@ -34,22 +34,13 @@ class SettingSection(object):
settings: list = None
@dataclass
class SkillSetting(object):
"""Representation of the link between a device and a skill setting """
id: str
device_skill_id: str
setting_id: str
value: str = None
def get_setting_sections_by_device_id_and_setting_version(db, device_id, setting_version_hash):
""" Fetch all sections of a skill for a given device and setting version
:param db: psycopg2 connection to the mycroft database
:param device_id: uuid
:param setting_version_hash: version_hash for a given skill setting
:return:
:return: list of sections for a given skill
"""
query = DatabaseQuery(
file_path=path.join(SQL_DIR, 'get_setting_sections_by_device_id_and_setting_version.sql'),
@ -57,15 +48,15 @@ def get_setting_sections_by_device_id_and_setting_version(db, device_id, setting
singleton=False
)
sql_results = fetch(db, query)
return list(map(lambda result: SettingSection(**result), sql_results))
return [SettingSection(**result) for result in sql_results]
def get_setting_by_section_id(db, setting_section_ids):
""" Fetch all settings for a given list of section ids
""" Fetch all settings whose id is in a given list of section ids
:param db: psycopg2 connection to the mycroft database
:param setting_section_ids: list of section ids
:return:
:return: list of settings
"""
query = DatabaseQuery(
file_path=path.join(SQL_DIR, 'get_setting_by_section_ids.sql'),
@ -73,7 +64,7 @@ def get_setting_by_section_id(db, setting_section_ids):
singleton=False
)
sql_results = fetch(db, query)
return list(map(lambda result: Setting(**result), sql_results))
return [Setting(**result) for result in sql_results]
def get_skill_settings_by_device_id_and_version_hash(db, device_id, setting_version_hash):
@ -82,7 +73,7 @@ def get_skill_settings_by_device_id_and_version_hash(db, device_id, setting_vers
:param db: psycopg2 connection to the mycroft database
:param device_id: uuid
:param setting_version_hash:
:return:
:return: list of tuples (setting_id, setting_value)
"""
query = DatabaseQuery(
file_path=path.join(SQL_DIR, 'get_skill_setting_by_device_id_and_version_hash.sql'),
@ -90,7 +81,7 @@ def get_skill_settings_by_device_id_and_version_hash(db, device_id, setting_vers
singleton=False
)
sql_results = fetch(db, query)
return list(map(lambda result: SkillSetting(**result), sql_results))
return [(result['setting_id'], result['value']) for result in sql_results]
def get_setting_by_device_id_and_setting_version_hash(db, device_id, setting_version_hash):
@ -98,7 +89,7 @@ def get_setting_by_device_id_and_setting_version_hash(db, device_id, setting_ver
:param db: psycopg2 connection to the mycroft database
:param device_id: uuid
:param setting_version_hash:
:return:
:return: list of sections, each section filled with its settings
"""
sections = get_setting_sections_by_device_id_and_setting_version(db, device_id, setting_version_hash)
setting_sections_ids = tuple(map(lambda s: s.id, sections))
@ -106,13 +97,13 @@ def get_setting_by_device_id_and_setting_version_hash(db, device_id, setting_ver
skill_settings = get_skill_settings_by_device_id_and_version_hash(db, device_id, setting_version_hash)
# Fills each setting with the correspondent value from the skill setting
for skill_setting in skill_settings:
s = next(filter(lambda setting: setting.id == skill_setting.setting_id, settings), None)
for setting_id, setting_value in skill_settings:
s = next(filter(lambda setting: setting.id == setting_id, settings), None)
if s:
s.value = skill_setting.value
s.value = setting_value
# Fills each setting with its list of settings
for section in sections:
section.settings = list(filter(lambda setting: setting.setting_section_id == section.id, settings))
section.settings = [setting for setting in settings if setting.setting_section_id == section.id]
return sections