示例#1
0
class TwoPSet:
    def __init__(self):
        self.A = GSet()
        self.R = GSet()

    def add(self, elem):
        self.A.add(elem)

    def remove(self, elem):
        self.R.add(elem)

    def query(self, elem):
        return self.A.query(elem) and not self.R.query(elem)

    def compare(self, tps2):
        return self.A.compare(tps2.A) and self.R.compare(tps2.R)

    def merge(self, tps2):
        self.A.merge(tps2.A)
        self.R.merge(tps2.R)
        # self.display()

    def display(self):
        print("A: ", end="")
        self.A.display()
        print("R: ", end="")
        self.R.display()

    def addedValues(self):
        addedValues = []
        for elem in self.A.payload:
            if self.A.payload.count(elem) > self.R.payload.count(elem):
                if elem not in addedValues:
                    addedValues.append(elem)
        return addedValues

    def removedValues(self):
        removedValues = []
        for elem in self.R.payload:
            if self.R.payload.count(elem) > self.A.payload.count(elem):
                if elem not in removedValues:
                    removedValues.append(elem)
        return removedValues

    def toDict(self):
        self.A = self.A.toDict()
        self.R = self.R.toDict()
        # print(self.__dict__)
        return self.__dict__

    def loadFromDict(self, dict_input):
        A = GSet()
        R = GSet()
        self.A = A.loadFromDict(dict_input['A'])
        self.R = R.loadFromDict(dict_input['R'])
        return self
示例#2
0
def get_user_update_msg(user):

    user_update = Users_update()
    user_Gset = GSet()
    user_Gset.add(user.to_json())
    user_update.users = json_util.dumps(user_Gset.toDict())

    msg = {"type": 'Users_update', "data": {'users': user_update.users}}
    return json_util.dumps(msg)
示例#3
0
def merge_users(users):
    middleware_users = middlewareDatabaseRead.get_user_updates()
    if middleware_users==None:
        middleware_user_update = User_update()
        middleware_user_update.users = users
        middleware_user_update.save()
        return


    middleware_users_Gset = GSet().loadFromDict(json_util.loads(middleware_users.users))
    users_Gset = GSet().loadFromDict(json_util.loads(users))
    
    middleware_users_Gset.merge(users_Gset)
    
    middleware_users.users = json_util.dumps(middleware_users_Gset.toDict())
    middleware_users.save()