Sunday, March 4, 2012

Example data tree parser (for nested dictionaries) in Python


import datetime

class Workdictmanager:

 
    def __checkinitworkdict(self, sessiontime):

        check = False

        if 'workdict' in sessiontime:

            check = True

            self.workdict = sessiontime['workdict']

        return check

    

    def putWorkdict(self, sessiontime):

        return self.__checkinitworkdict(sessiontime)

##        self.workdict = sessiontime['workdict']

 
    def qwordCheck(self, qword):

        check = False

        if qword in self.workdict:

            check = True

        return check

        

    def addWorkvals(self, qword, valtup):

        #here valtup is expected in list or tuple form

        #this is an ordered set, (akey, score, att)

        if qword in self.workdict:

            self.qworddir = self.workdict[qword]

            self.qworddir['score'] += valtup[1]

            self.qworddir['totalattempts'] += valtup[2]

            self.workdict[qword] = self.qworddir

        else:

            self.qworddir = {}    

            self.qworddir['answer'] = valtup[0]

            self.qworddir['score'] = valtup[1]

            self.qworddir['totalattempts'] = valtup[2]

        self.workdict[qword] = self.qworddir

        

    def retrieveDictval(self, qword, dirkey):

        self.qworddir = self.workdict[qword]

        return self.qworddir[dirkey]

    

    def retrieveWorkdict(self):

        return self.workdict        

    

    def __init__(self):

        self.workdict = {}

        

        

class Statsmanager:

 
    #what is the difference between saveStat and put, puts an already

    #recorded stat session into the Statmanager class object whereas

    # saveStat saves an individual stat in the class object

 
    def __checkinitstats(self, sessiontime):

        check = False

        if 'stats' not in sessiontime:

            self.save()

        else:

            check = True

            self.stats = sessiontime['stats']

            self.totalwords = self.stats['totalwords']

            self.totalcorrect = self.stats['totalcorrect']

            self.totalincorrect = self.stats['totalincorrect']

        return check

 
    def save(self):

        self.stats['totalwords'] = self.totalwords

        self.stats['totalcorrect'] = self.totalcorrect

        self.stats['totalincorrect'] = self.totalincorrect

    

    def putStats(self, sessiontime):

        return self.__checkinitstats(sessiontime)

##        self.stats = sessiontime['stats']

 
    def addStats(self, valtup):

        self.stats['totalwords'] += valtup[0]

        self.stats['totalcorrect'] += valtup[1]

        self.stats['totalincorrect'] += valtup[2]

 
    def saveStat(self, statkey, value):

        if statkey == 'totalwords':

            self.totalwords = value

            self.stats['totalwords'] = self.totalwords #int value

        elif statkey == 'totalcorrect':

            self.totalcorrect = value

            self.stats['totalcorrect'] = self.totalcorrect #int value

        elif statkey == 'totalincorrect':

            self.totalincorrect = value

            self.stats['totalincorrect'] = self.totalincorrect #int value

 
    def retrieveStat(self, statkey):

        stat = 0

        if statkey == 'totalwords':

            stat = self.stats['totalwords']

        elif statkey == 'totalcorrect':

            stat = self.stats['totalcorrect']

        elif statkey == 'totalincorrect':

            stat = self.stats['totalincorrect']

        return stat

    

    def retrieveStats(self):

        return self.stats

    

    def __init__(self):

        self.stats = {}

        

        self.totalwords = 0

        self.totalcorrect = 0

        self.totalincorrect = 0

        self.stats['totalwords'] = self.totalwords

        self.stats['totalcorrect'] = self.totalcorrect

        self.stats['totalincorrect'] = self.totalincorrect

        

class Sessioncachemanager:

 
    def addStats(self, statstup):

        self.statsmngr.addStats(statstup)

 
    def retrieveStats(self):

        return self.statsmngr.retrieveStats()

 
    def retrieveWork(self):

        return self.workdictmngr.retrieveWorkdict()

 
    def retrieveSessiontimeelapsed(self):

        return self.sessiontimeelapse

 
    def retrieveSessiontimefromlast(self):

        return self.sessiontimefromlast

 
    def addWorkvals(self, qword, workvalstup):

        self.workdictmngr.addWorkvals(qword, workvalstup)

 
    def __checkSessionTime(self, sessiontimekey):

        check = False

        if sessiontimekey in self.session:

            check = True

        return check

 
    def __recordEndSession(self):

        self.__sessionrecord(False)

 
    def __recordBeginSession(self):

        self.__sessionrecord(True)

 
    def clearSession(self, time):

        #measure current time then compare timedelta from now

        #to all session datetime objects, then convert this

        # to total seconds those exceeding in total seconds

        #will be keys removed from the session.

        #The variable 'time' is measured in seconds

        currenttime = datetime.datetime.now()

        timestamps = list(self.session.keys())

        if type(time) == str:

            if time == 'All':

                for timestamp in timestamps:

                    del self.session[timestamp]

        elif type(time) == int:                        

            for timestamp in timestamps:

                timedelt = currenttime - timestamp

                timedeltsec = timedelt.total_seconds()

                if time > timedeltsec:

                    del self.session[timestamp]

 
    def newSession(self, userdict):

        self.session = userdict['session']

        self.__recordBeginSession()

        self.session[self.sessionbegin] = self.sessiontime

        self.statsmngr = Statsmanager()

        self.workdictmngr = Workdictmanager()

 
    def putSessiontime(self, sessiontimekey, userdict):

        self.session = userdict['session']

        if self.__checkSessionTime(sessiontimekey):

            self.sessiontime = self.session[sessiontimekey]

            self.statsmngr.putStats(self.sessiontime)

            self.workdictmngr.putWorkdict(self.sessiontime)

        else:

            print('Session Time value does not exist!')

 
    def putSession(self, userdict):

        self.session = userdict['session']

 
    def retrieveSessiontimekeys(self):

        return list(self.session.keys())

 
    def saveSession(self):

##        self.sessiontime = {}

##        self.statsmngr.putStats(self.sessiontime)

##        self.workdictmngr.putWorkdict(self.sessiontime)

        #calls from stats class

        stats = self.statsmngr.retrieveStats()

        self.sessiontime['stats'] = stats

        #call from workdict

        workdict = self.workdictmngr.retrieveWorkdict()

        self.sessiontime['workdict'] = workdict

        self.__recordEndSession()

        print('session begin ',self.sessionbegin)

        print('session end ', self.sessionend)

        self.__sessiontimeelapse()

        self.__sessiontimeelapsefromlast()

        self.sessiontime['sessiontime'] = self.sessiontimeelapse

        self.session[self.sessionbegin] = self.sessiontime

        sessionuserdict = self.session

        self.sessionlast = self.__recordTime()

        return sessionuserdict

 
    def retrieveSession(self, userdict):

        self.session = userdict['session']

        

    def __recordTime(self):

        return datetime.datetime.now()

    

    def __sessiontimeelapse(self):

        #subtraction of timedate object yields timedelta object

        self.sessiontimeelapse = self.sessionend - self.sessionbegin

 
    def __sessiontimeelapsefromlast(self):

        self.sessiontimefromlast = self.sessionend - self.sessionlast

        

        

    def __sessionrecord(self, flag):

        #true flag records begin session while false flag records end stamp

        if flag:

            self.sessionbegin = self.__recordTime()

            self.sessionlast = self.__recordTime()

        else:

            self.sessionend = self.__recordTime()

            

    def __init__(self):

        self.session = {}

        self.sessiontime = {}

        self.sessiontimeelapse = 0

        self.statsmngr = Statsmanager()

        self.workdictmngr = Workdictmanager()

 
class Overallcachemanager:

 
    def __checkinitoverallstats(self, check):

        if not check:

            self.overall['stats'] = self.statsmngr.retrieveStats()

 
    def __checkinitoverallworkdict(self, check):

        if not check:

            self.overall['workdict'] = {}

            

    def addStats(self, statstup):

        self.statsmngr.addStats(statstup)

 
    def clearOverall(self, typeflag):

        if typeflag == 'All':

            self.statsmngr = Statsmanager()

            self.workdictmngr = Workdictmanager()

        elif typeflag == 'Stats':

            self.statsmngr = Statsmanager()

        elif typeflag == 'Workdict':

            self.workdictmngr = Workdictmanager()

 
    def retrieveStats(self):

        return self.statsmngr.retrieveStats()

 
    def retrieveWork(self):

        return self.workdictmngr.retrieveWorkdict()

 
    def addWorkvals(self, qword, workvalstup):

        self.workdictmngr.addWorkvals(qword, workvalstup)

                  

    def newOverall(self):

        self.overall = {}

        self.statsmngr = Statsmanager()

        self.workdictmngr = Workdictmanager()

 
    def save(self):

        self.overall['stats'] = self.statsmngr.retrieveStats()

        self.overall['workdict'] = self.workdictmngr.retrieveWorkdict()

        return self.overall

                  

    def put(self, userdict):

        if 'overall' in userdict:

            print('hitting if overall in userdict')

            self.overall = userdict['overall']

            if 'sessiontime' not in self.overall:

                self.overall['sessiontime'] = datetime.timedelta()

            check = self.statsmngr.putStats(self.overall)

            self.__checkinitoverallstats(check)

            check = self.workdictmngr.putWorkdict(self.overall)

            self.__checkinitoverallworkdict(check)

        else:

            userdict['overall'] = {}

            self.overall = {}

            self.overall['sessiontime'] = datetime.timedelta()

            self.statsmngr = Statsmanager()

            self.workdictnmngr = Workdictmanager()

 
    def addSessiontime(self, timedelt):

        self.overall['sessiontime'] += timedelt

 
    def retrieveSessiontime(self):

        return self.overall['sessiontime']

        

    def __init__(self):

        self.overall = {}

        self.overall['sessiontime'] = datetime.timedelta()

        self.statsmngr = Statsmanager()

        self.workdictmngr = Workdictmanager()

        

class Cachemanager:

 
    def addSessionstatdat(self, statdat):

        self.sessioncache.addStats(statdat)

 
    def addSessionworkdat(self, qword, workdat):

        #workdat

        self.sessioncache.addWorkvals(qword, workdat)

 
    def addOverallstatdat(self, statdat):

        self.overallcache.addStats(statdat)

 
    def addOverallworkdat(self, qword, workdat):

        self.overallcache.addWorkvals(qword, workdat)

 
    def addOverallsessiontime(self, sessiontimeelapsed):

        self.overallcache.addSessiontime(sessiontimeelapsed)

 
    def clearOverall(self, typeflag):

        #'typeflag' is a variable set to one of the following:

        #'All', 'Stats', or 'Workdict'

        self.overallcache.clearOverall(typeflag)

 
    def clearSession(self, time):

        self.sessioncache.clearSession(time)

 
    def putSession(self):

        self.sessioncache.putSession(self.user)

 
    def putSessiontime(self, sessiontime, user):

        #sessiontime is a datetime object

        self.sessioncache.putSessiontime(sessiontime, user)

 
    def retrieveSessiontimekeys(self):

        return self.sessioncache.retrieveSessiontimekeys()

 
    def savereturnSession(self):

        self.user['session'] = self.sessioncache.saveSession()

        totalsessiontime = self.sessioncache.retrieveSessiontimefromlast()

        self.overallcache.addSessiontime(totalsessiontime)

 
    def savereturnOverall(self):

        self.user['overall'] = self.overallcache.save()

 
    def retrieveSessionwork(self):

        return self.sessioncache.retrieveWork()

 
    def retrieveSessionstats(self):

        return self.sessioncache.retrieveStats()

 
    def retrieveOverallstats(self):

        return self.overallcache.retrieveStats()

 
    def retrieveOverallwork(self):

        return self.overallcache.retrieveWork()

 
    def newSession(self):

        self.sessioncache.newSession(self.user)

 
    def __checkandinituserdat(self):

        if 'session' not in self.user:

            self.user['session'] = {}

        if 'overall' not in self.user:

            self.user['overall'] = {}

    

    def __init__(self, userdat, newSessionflag):

        self.user = userdat

        self.__checkandinituserdat()

        self.overallcache = Overallcachemanager()

        self.sessioncache = Sessioncachemanager()

        self.overallcache.put(self.user)

        if not newSessionflag:

               self.sessioncache.putSession(self.user)

        else:

               self.sessioncache.newSession(self.user) 



Here is my userdata tree parser used in conjuction to the previous shelve object read/write interface class.

Data tree can generally be expressed as follows:
user session data, dictionary keys are 'session'>sessiondatetime
and 'Overall' subkeys of each are:
                              'workdict' > qword > 'answer' > akey                                                                                                               > 'score'  > score(int)
                                                   > 'totalattempts'> att(int)   
                                       'stats' > 'totalwords' > totalwords(int)                                            
                                                   > 'totalcorrect' > totalcorrect(int)                                         
                                                   > 'totalincorrect' > totalincorrect(int)                 
                          'sessiontime' > timespent(int min)

Descending from top to bottom in order in so far as program structure, handlers on data tree starting at leaf node and then descending to parent node of all data trees,
thus the Cachemanager class represents the top of the data tree hierarchy and it is through this class that all subsequent data handling assignments are received and transmitted through the respective chain of class above it.  Also it is worth noting excepting the child leaf node classes, namely, workdictmanager and statsmanager classes above, all parent classes initialise and transmit data to a respective data child classmanager for such data level.

The simplest way of thinking about this is comes by the following example:

a = { b: dictionary of b, c: dictionary of c}
dictionary of b = {d: dictionary of d, e: dictionary of e}
dictionary of c = {f: dictionary of f, g: dictionary of g}

Here, the dictionary a would have its data class handler which controls the passing of data to all embedded dictionaries inside a...this means data sent to b, c, d, e, f, and g.

The class handler of dictionary a would decide and appropriately send to data in the tree to:
- b if data needed to be sent/received from  child leaf node d or e,
or
-c if data needed to  be sent/received from child leaf node f or g.

In turn data sent or received according to design structure above, passes data to the handler of b or c respectively, and the handlers of b or c, in turn pass data to the handlers of d, e, f, or g as required.

Thus each trunk/branch/leaf all have respective class data handlers for their respective positions on the data tree in this database parsing model.                          


In the case of more complex data trees, class handling models of this type become more complex at the top level hierarchies, in terms of the potential function handling possibilities in so far as data pathing structures here.


In conjunction to the previous posting regarding shelve object storage of both user and user data...I can in one read to permanent storage action, read all the user's given data stored on disk, in one call, and pull up all of the user's relevant history in so far as previous interactions.  I minimise the write to calls for the user's present session use, by only writing all relevant data to be added, once the user's session is finalised which occurs either at user account change or at program termination.  Thus maintaining higher read/write efficiency in doing so...i.e., avoiding numerous disk reads and writes to get at the same sets of data.

No comments:

Post a Comment

Oblivion

 Between the fascination of an upcoming pandemic ridden college football season, Taylor Swift, and Kim Kardashian, wildfires, crazier weathe...