示例#1
0
文件: server.py 项目: fleupold/pycast
def energy_data(request):
	"""
		Connects to the database and loads Readings for device 8.
	"""
	cur = db.cursor().execute("""SELECT timestamp, current FROM Readings""")
	original = TimeSeries()
	original.initialize_from_sql_cursor(cur)
	original.normalize("day", fusionMethod = "sum")
	return Response(json.dumps(original, cls=PycastEncoder), content_type='application/json') 
示例#2
0
def energy_data():
    """
        Connects to the database and loads Readings for device 8.
    """
    cur = db.cursor().execute("""SELECT timestamp, current FROM Readings""")
    original = TimeSeries()
    original.initialize_from_sql_cursor(cur)
    original.normalize("day", fusionMethod = "sum")
    return itty.Response(json.dumps(original, cls=PycastEncoder), content_type='application/json')
示例#3
0
    def select_to_many_attributes_test(self):
        """SELECT timestamp, value, junk, FROM TestTable

        This function tests if statements like 

        SELECT timestamp, value, junk, ... FROM

        can be used to initialize a TimeSeries instance. TimeSeries should therefore only
        take the first two attributes for data initialization, regardless of their names.
        """
        ## read the number of rows from the database
        cur = self._db.cursor().execute("""SELECT COUNT(*) from TestTable""")
        nbrOfTuples = cur.fetchall()[0][0]

        ## initialize a TimeSeries instance from a database cursor
        cur = self._db.cursor().execute("""SELECT timestamp, value, junk_one, junk_two FROM TestTable""")
        ts = TimeSeries()
        ts.initialize_from_sql_cursor(cur)

        ## check if all values of the database got inserted into the TimeSeries
        assert len(ts) == nbrOfTuples
    def select_star_test(self):
        """SELECT * FROM TestTable

        This function tests if statements like

        SELECT * FROM

        can be used to initialize a TimeSeries instance. TimeSeries should therefore only
        take the first two attributes for data initialization, regardless of their names.
        """
        # read the number of rows from the database
        cur = self._db.cursor().execute("""SELECT COUNT(*) from TestTable""")
        nbrOfTuples = cur.fetchall()[0][0]

        # initialize a TimeSeries instance from a database cursor
        cur = self._db.cursor().execute("""SELECT * FROM TestTable""")
        ts = TimeSeries()
        ts.initialize_from_sql_cursor(cur)

        # check if all values of the database got inserted into the TimeSeries
        assert len(ts) == nbrOfTuples
示例#5
0
    def check_for_consistency_test(self):
        """Tests if database initialization and manual initialization create equal TimeSeries instances."""
        ## read the number of rows from the database
        cur = self._db.cursor().execute("""SELECT COUNT(*) from TestTable""")
        nbrOfTuples = cur.fetchall()[0][0]

        ## SQL extraction statement
        sqlstmt = """SELECT timestamp, value FROM TestTable ORDER BY timestamp ASC"""

        ## Initialize one TimeSeries instance manually
        tsManual = TimeSeries()
        data     = self._db.cursor().execute(sqlstmt).fetchall()
        for entry in data:
            tsManual.add_entry(*entry)

        ## Initialize one TimeSeries from SQL cursor
        tsAuto = TimeSeries()
        tsAuto.initialize_from_sql_cursor(self._db.cursor().execute(sqlstmt))

        ## check if those TimeSeries are equal
        if not (nbrOfTuples   == len(tsManual)): raise AssertionError
        if not (nbrOfTuples   == len(tsAuto)):   raise AssertionError
        if not (len(tsManual) == len(tsAuto)):   raise AssertionError
        if not (tsManual      == tsAuto):        raise AssertionError
    def check_for_consistency_test(self):
        """Tests if database initialization and manual initialization create equal TimeSeries instances."""
        # read the number of rows from the database
        cur = self._db.cursor().execute("""SELECT COUNT(*) from TestTable""")
        nbrOfTuples = cur.fetchall()[0][0]

        # SQL extraction statement
        sqlstmt = """SELECT timestamp, value FROM TestTable ORDER BY timestamp ASC"""

        # Initialize one TimeSeries instance manually
        tsManual = TimeSeries()
        data = self._db.cursor().execute(sqlstmt).fetchall()
        for entry in data:
            tsManual.add_entry(str(entry[0]), entry[1])

        # Initialize one TimeSeries from SQL cursor
        tsAuto = TimeSeries()
        tsAuto.initialize_from_sql_cursor(self._db.cursor().execute(sqlstmt))

        # check if those TimeSeries are equal
        assert (nbrOfTuples == len(tsManual))
        assert (nbrOfTuples == len(tsAuto))
        assert (len(tsManual) == len(tsAuto))
        assert (tsManual == tsAuto)