Sunday, March 4, 2012

User shelve object database example in Python


import os
import shelve

class Userdata:

    def closeUserfile(self):
        self.userdatafile.close()

    def saveUser(self, user, userdata):
        self.user = user
        self.__checkreadFile()
        if self.__verifyUser():
            self.userdatafile[self.user] = userdata
        print('User data saved.')
        self.userdatafile.close()

    def saveUserdata(self, userdata):
        self.__checkreadFile()
        #self.userdatafile[self.user] = userdata
        if self.__verifyUser():
            self.userdatafile[self.user] = userdata
            print('User data saved.')
        self.userdatafile.close()

    def addUser(self, user):
        self.user = user
        self.__checkreadFile()
        usercheck = self.__verifyUser()
        if not usercheck:
            self.userdatafile[self.user] = {}
            print('User added.')
        else:
            print('User already exists.')
        return usercheck
        self.userdatafile.close()
            
    def retrieveUser(self, user):
        self.user = user
        self.__checkreadFile()
        if self.__verifyUser():
            self.userdata = self.userdatafile[self.user]
        self.userdatafile.close()
        return self.userdata

    def retrieveUsername(self):
        return self.user

    def retrieveUserlist(self):
        userlist = []
        self.__checkreadFile()
        for user in self.userdatafile:
            if not user == 'High Score':
               userlist.append(user) 
        self.userdatafile.close()
        return userlist

    def isUserdatempty(self):
        check = False
        self.__checkreadFile()
        if len(self.userdatafile) == 0:
            check = True
        self.userdatafile.close()
        return check

    def returnFirstuser(self):
        self.__checkreadFile()
        userlist = list(self.userdatafile.keys())
        firstuserkey = userlist[0]
        self.userdatafile.close()
        return [firstuserkey, self.userdatafile[firstuserkey]]
            
    def __verifyUser(self):
        check = False
        if self.user in self.userdatafile:
            check = True
        return check
        
    def verifyUser(self, username):
        check = False
        self.__checkreadFile()
        if username in self.userdatafile:
            check = True
        self.userdatafile.close()
        return check        
        
    def __readFile(self):
        self.userdatafile = shelve.open(self.pathfile, 'w')

    def __createFile(self):
        self.userdatafile = shelve.open(self.pathfile, 'n')
        
    def __checkreadFile(self):
        file = ''
        if os.name == 'nt':
            file = '\\yourdatafilenamehere'
        elif os.name == 'posix':
            file = '/yourdatafilenamehere'
        self.pathfile = self.path+ file
        if os.access(self.pathfile, os.F_OK):
                if os.access(self.pathfile, os.R_OK):
                    if os.access(self.pathfile, os.W_OK):
                        self.__readFile()
                    else:
                        self.__createFile()
                else:
                    self.__createFile()
        else:
            self.__createFile()
    
##    def checkUser(self):
##        
##        self.__checkreadFile()
##        self.__verifyUser()
        
    def __init__(self):
        self.path = os.getcwd()
        self.user = ''
        #self.__checkreadFile() 


Something of an alternative shelve object database storage class alternative code offering outside the CouchDB.  I'd mention some issues experienced with Python shelve objects:

- Heavy read write loads to shelve objects aren't good at least with respect read write from permanent (disk based) memory storage.  Thus I've typically housed much of the data in such a way that reading from disk ensures likely all the information (in terms of transient storage) will be there to minimise read/write callbacks.  Thus I actually wrote a database program which parses additionally embedded dictionary trees for reading such data.  In effect, you hadn't need develop something like an sql database program to do this, since the pertinent data that you needed for your program may remain exact in so far as object data were concerned.  You just need to make sure that you have it structured, in this case, in a way, so that your data reader knows where to find the data in the tree hierarchy in doing so.  If you need some alternate method for storing dynamic data to a given database, you might consider using python's sql module interfaces, this fyi provides some basic sql code examples.  I'll post my tree structure interface here probably in a day or so, that at least provides how an extensive data tree process  handling might look like.

-You may notice above, generally after reading calls to a shelve object file, such shelve object file is closed.  This is done per recommends of python, and as I've experienced, if you don't track closing and opening, errors can result when attempting to re open an already open file or reading from a closed file.  Thus, wise to make sure that routine orderliness applies here.

In the __checkreadFile() function call above, the program attempts to verify that the datafile exists in the installation directory path...this installation directory path is established by the command os.getcwd() .  If the file exists it reads, otherwise, it needs to be created.

Generally idea here is that a user data can be stored or retrieved, and user key information can be searched for, and lists of users can be furnished through this given class .


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...