From 98f945add1cec735e945a55755dabf4e040e38d9 Mon Sep 17 00:00:00 2001 From: junos Date: Thu, 31 Dec 2020 14:33:44 +0100 Subject: [PATCH] Imrpove code formatting. --- config/models.py | 97 ++++++++++++++++++++++++++++++++----------- test/test_database.py | 4 +- 2 files changed, 75 insertions(+), 26 deletions(-) diff --git a/config/models.py b/config/models.py index 676af22..b217326 100644 --- a/config/models.py +++ b/config/models.py @@ -1,11 +1,22 @@ from datetime import datetime -from sqlalchemy import Column, String, Boolean, \ - Integer, SmallInteger, BigInteger, \ - Float, TIMESTAMP, \ - ForeignKey, UniqueConstraint + +from sqlalchemy import ( + TIMESTAMP, + BigInteger, + Boolean, + Column, + Float, + ForeignKey, + Integer, + SmallInteger, + String, + UniqueConstraint, +) +from sqlalchemy.dialects.postgresql import ARRAY as PSQL_ARRAY +from sqlalchemy.dialects.postgresql import INTEGER as PSQL_INTEGER +from sqlalchemy.dialects.postgresql import JSONB as PSQL_JSONB +from sqlalchemy.dialects.postgresql import TEXT as PSQL_TEXT from sqlalchemy.ext.declarative import declarative_base, declared_attr -from sqlalchemy.dialects.postgresql import ARRAY as PSQL_ARRAY, \ - JSONB as PSQL_JSONB, TEXT as PSQL_TEXT, INTEGER as PSQL_INTEGER from sqlalchemy.orm import relationship Base = declarative_base() @@ -31,10 +42,10 @@ class Participant(Base): tester = Column(Boolean, index=True, nullable=False, default=False) def __repr__(self): - return 'Participant()' + return "Participant()" def __str__(self): - return ''.format(self.username) + return "".format(self.username) class AWAREsensor(object): @@ -49,18 +60,25 @@ class AWAREsensor(object): @declared_attr def participant_id(self): - return Column(Integer, ForeignKey('participants.id'), nullable=False, index=True) + return Column( + Integer, ForeignKey("participants.id"), nullable=False, index=True + ) @declared_attr def participant(self): - return relationship('Participant', lazy="select", backref=self.__tablename__) + return relationship("Participant", lazy="select", backref=self.__tablename__) @declared_attr def __table_args__(self): - return UniqueConstraint('device_id', '_id', name=self.__tablename__ + "_twice"), - # I think it makes more sense to create a Constraint on device_id and _id rather than relate it to participant_id. + return ( + UniqueConstraint("device_id", "_id", name=self.__tablename__ + "_twice"), + ) + + # I think it makes more sense to create a Constraint + # on device_id and _id rather than relate it to participant_id. # _id is a primary key, auto incremented by AWARE. - # However, I would expect it to reset back to 1 if the application was reinstalled, + # However, I would expect it to reset back to 1 + # if the application was reinstalled, # similarly to how device_id resets. @@ -82,8 +100,8 @@ class GoogleAR(Base, AWAREsensor): class Application(Base, AWAREsensor): __tablename__ = "applications" -# package_name = Column(String, nullable=False) -# application_name = Column(String) + # package_name = Column(String, nullable=False) + # application_name = Column(String) package_hash = Column(String(64), nullable=False) play_store_genre = Column(String, nullable=True) is_system_app = Column(Boolean) @@ -97,7 +115,8 @@ class Barometer(Base, AWAREsensor): class BarometerSensor(Base, AWAREsensor): __tablename__ = "barometer_sensor" - # Since this table is not really important, I will leave all columns as nullable. (nullable=True by default.) + # Since this table is not really important, + # I will leave all columns as nullable. (nullable=True by default.) double_sensor_maximum_range = Column(Float) double_sensor_minimum_delay = Column(Float) sensor_name = Column(String) @@ -222,8 +241,8 @@ class NetworkTraffic(Base, AWAREsensor): class Notification(Base, AWAREsensor): __tablename__ = "notifications" -# package_name = Column(String, nullable=False) -# application_name = Column(String, nullable=False) + # package_name = Column(String, nullable=False) + # application_name = Column(String, nullable=False) package_hash = Column(String(64), nullable=False) play_store_genre = Column(String, nullable=True) sound = Column(String, nullable=False) @@ -268,7 +287,8 @@ class Temperature(Base, AWAREsensor): class TemperatureSensor(Base, AWAREsensor): - # I left all of these nullable, as we haven't seen any data from this sensor anyway. + # I left all of these nullable, + # as we haven't seen any data from this sensor anyway. __tablename__ = "temperature_sensor" double_sensor_maximum_range = Column(Float) double_sensor_minimum_delay = Column(Float) @@ -293,9 +313,31 @@ class WiFi(Base, AWAREsensor): label = Column(String, nullable=True) -all_AWARE_tables = [ESM, Location, Screen, LightSensor, Call, SMS, Application, Notification, Battery, WiFi, Proximity, - Timezone, Processor, NetworkData, NetworkTraffic, Barometer, BarometerSensor, Temperature, - TemperatureSensor, Bluetooth, Accelerometer, GoogleAR, Speech] +all_AWARE_tables = [ + ESM, + Location, + Screen, + LightSensor, + Call, + SMS, + Application, + Notification, + Battery, + WiFi, + Proximity, + Timezone, + Processor, + NetworkData, + NetworkTraffic, + Barometer, + BarometerSensor, + Temperature, + TemperatureSensor, + Bluetooth, + Accelerometer, + GoogleAR, + Speech, +] all_AWARE_table_names = [table.__tablename__ for table in all_AWARE_tables] @@ -312,5 +354,12 @@ class AppCategories(Base): package_hash = Column(String(64), index=True, nullable=False, unique=True) play_store_genre = Column(String, nullable=True) play_store_response = Column(SmallInteger, nullable=False) - number_of_attempts = Column(SmallInteger, nullable=False, default=0, onupdate=increment_one) - last_attempt = Column(TIMESTAMP(timezone=False), nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow) + number_of_attempts = Column( + SmallInteger, nullable=False, default=0, onupdate=increment_one + ) + last_attempt = Column( + TIMESTAMP(timezone=False), + nullable=False, + default=datetime.utcnow, + onupdate=datetime.utcnow, + ) diff --git a/test/test_database.py b/test/test_database.py index b9242c8..f313aac 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -3,7 +3,7 @@ import unittest from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker -from config.models import Participant, LightSensor +from config.models import LightSensor, Participant from setup import db_uri @@ -30,4 +30,4 @@ class DatabaseConnection(unittest.TestCase): def test_get_light_data(self): light_0 = self.session.query(Participant).join(LightSensor).first() self.assertIsNotNone(light_0) - print(light_0) \ No newline at end of file + print(light_0)