1

I am using the following code to retrieve variables from a database that Python uses to run an automated machine. I set the variables through a PHP driven web interface. Python reads the variables and acts according to instructions.

However, during calibration of the machine, we are forced to restart python to accept any variable changes. Python isn't my first language and neither is it the first language of my colleagues. It would obviously save a lot of time if we didn't have to restart python to accept variable changes.

Our variable list class is constructed like the following;

class VariableList():
connectdb = DbConnector(host='localhost', user='a', password='b', database='c')
result = connectdb.selectDb('variablelist','varA,varB')
for row in result:
    # INPUTS
    varA = row[1]
    varB = row[2] 

What is the Pythonic way to get around this issue? Getters/Setters? @property? An example to follow would very much appreciated...

1
  • Python isn't different from any other language here. You fetch data from a database, and if the database changes, you need to read those changes again. You'll probably want to write a signal handler that causes Python to reinitialize the variables when it receives, for example, SIGUSR1. Then when you change the database, send that signal to your Python process to trigger the reinitialization. Commented Mar 4, 2019 at 16:07

1 Answer 1

1

easy one. Python implementation goes like this:


class VariableList():

    def __init__(self):
        self.db_con = DbConnector(host='localhost', user='a', password='b', database='c')

    @property
    def varA(self):
        return self.db_con.selectDb('variablelist','varA')

    @property.setter
    def varA(self, value):
        self.db_con.updateDb('variablelist', value)

and also you can refactor your model with SQLAlchemy framework. for example

from sqlalchemy (
    create_engine,
    Column, 
    Integer, 
    String,
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()
engine = create_engine(DB_DNS, 
                       pool_size=DB_POOL_SIZE, 
                       max_overflow=DB_MAX_OVERFLOW,
                       pool_recycle=DB_POOL_RECYCLE,
                       isolation_level="READ UNCOMMITTED",  # attention, the last one is important!
         )
Session = sessionmaker(bind=engine,
                       autocommit=False,
                       expire_on_commit=False)

class MyTable(Base):

    __tablename__ = MY_TABLE_NAME

    id = Column(Integer, primary=True)
    name = Column(String(32), nullable=True, default='', doc='user_name')

# query something
result = Session().query(MyTable).filter(CONDITION).all()
Sign up to request clarification or add additional context in comments.

4 Comments

I'll give your suggestion a shot (64 vars to work with). In my case, the setter is a PHP script.
The SQLAlchemy framework guarantees that the properties in the ORM object are consistent with the database, as soon as setting isolation_level with "READ UNCOMMITTED"
did you refactor with SQLAlchemy ?
We are on a tight deadline but I will give it a go for the next project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.