Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "instrumentation" in the context of SQLAlchemy?

In SQLAlchemy's tutorial, it mentions "instrumentation" but doesn't appear to properly define what instrumentation is:

These class attributes exist as Python descriptors, and define instrumentation for the mapped class. The functionality of this instrumentation includes the ability to fire on change events, track modifications, and to automatically load new data from the database when needed.

What is instrumentation in this context?

like image 335
Matty Avatar asked Mar 14 '12 00:03

Matty


1 Answers

instrumentation is the process of attaching attributes to a class, which are implemented as Python Descriptors (this link is mentioned in that sentence) such that any attribute get, set, or delete operation, that is:

# __get__
print myobject.someattribute

# __set__
myobject.someattribute = "foo"

# __del__
del myoject.someattribute

... will invoke Python code for each event, rather than using Python's default behavior of accessing/manipulating myobject.__dict__ directly. SQLAlchemy takes advantage of these hooks to provide behaviors such as lazy loading, as well as to record when the value of an attribute changes, for the purpose of implementing the unit of work pattern, where only those elements that have changed are rolled into UPDATE statements to be emitted to the database upon flush.

like image 128
zzzeek Avatar answered Sep 28 '22 06:09

zzzeek