Denis Callau
Denis Callau

Reputation: 285

Incremental ID shared between classes

I'm coding a virtual assistant in Python, and I want to store each request, response and possible error in a database. I'm using one class for request, another class for response and another class for error.
How can I create an ID variable that is shared for the respectives classes instances, for example:

First run of the program (the normal and correct running of the program):

request_id = 1
response_id = 1

Second run (an error occurred and stopped the program to proceed to the response class):

request_id = 2
error_id = 2

Third run (the program ran fine and the response class skipped the id 2 - that is the behavior that I want):

request_id = 3
response_id = 3

Note that in the third run, that response_id received the id 3 and the response_id = 2 will never exist, cause in the second run the proccess started with request and stopped in the error.

The ID variable must be always unique, even when my program crashes and I must restart him. I know I could grab the last id in the database when my program runs, but there's a way to do it without envolving the database?

Upvotes: 0

Views: 69

Answers (2)

Alberto
Alberto

Reputation: 496

a possible solution would be to use Pyro4 instead of a DB if you don't want it. You can use the following code:

Tracker.py

import Pyro4

@Pyro4.expose
@Pyro4.behavior(instance_mode="single")
class Tracker(object):
    def __init__(self):
        self._id = None

    def setId(self, value):
        print "set well :)", value
        self._id = value
    print self._id

    def getId(self):
    print "returning", self._id
        return self._id

daemon = Pyro4.Daemon()                
uri = daemon.register(Tracker)   

print("URI: ", uri)      
daemon.requestLoop()                 

Status.py

import Pyro4

class Status(object):
    def __init__(self, id):
        self._id = id
        self._pyro = None

    def connect(self, target):
        self._pyro = Pyro4.Proxy(target)

    def updateId(self):
        if ( not self._pyro is None ):
            self._pyro.setId(self._id)
            print "updated"
        else:
            print "please connect"

    def getId(self):
        if ( not self._pyro is None ):
            return self._pyro.getId()
        else:
            print "please connect"

Success.py

from Status import *
class Success(Status):
    def __init__(self):
        super(Success,self).__init__(1)

Wait.py

from Status import *
class Wait(Status):
    def __init__(self):
        super(Wait,self).__init__(1)

Error.py

from Status import *
class Error(Status):
    def __init__(self):
        super(Error,self).__init__(3)

run.py

from Success import *
from Wait import *
from Error import *

#just an example
s = Success()
w = Wait()
e = Error()

s.connect("PYRO:obj_c98931f8b95d486a9b52cf0edc61b9d6@localhost:51464")
s.updateId()
print s.getId()
w.connect("PYRO:obj_c98931f8b95d486a9b52cf0edc61b9d6@localhost:51464")
w.updateId()
print s.getId()
e.connect("PYRO:obj_c98931f8b95d486a9b52cf0edc61b9d6@localhost:51464")
e.updateId()
print s.getId()

Of course you would need to use a different URI but you should have a good idea now. using Pyro you could also specify a static URI name if needed.

The output should be:

$ c:\Python27\python.exe run.py
updated                        
1                              
updated                        
2                              
updated                        
3

HTH

Upvotes: 0

shanmuga
shanmuga

Reputation: 4499

Since you are using database to store the request and response why don't you use database to generate this id for you.

This can be done by creating the table with primary key int auto increment. Every request/response should be inserted into database, and the database will generate an unique id for each record inserted.

Upvotes: 1

Related Questions