Unique Objects
Uniqueness
Unique Objects are a parent class of CrudObject, embedding it into all of our underlying entity classes.
Unique Objects are objects which have some set of conditions under which an entity-class instance can be considered unique in the database. The motivation for such a class, is our high-throughput entity generation interface. Because ursa enables an engineer to iteratively create massive numbers of TimeSeries objects (for instance), if desired, we require some backstop in order to prevent repetitive creation of identical objects. This is the UniqueObject class.
Factory Method
Description
UniqueObjects are fitted with a factory method called “create”, which does the following:
Checks if an identical object exists already in the database
If the object exists: return the already existing object. Otherwise: Create a new object instance and return it.
In the case where an identical object exists already in the database and it is returned after a call to TimeSeries.create() (for instance), the parameters which were passed to the initial call to create, specifically those parameters upon which “uniqueness” is not conditioned, will not be updated. Therefore it is good practice to double check after creating new objects whether or not a new object has been created, or an old one has been returned. A simple check for this is to access the object’s “id” attribute. If a new object was created, it will not yet have an id after being returned from create, as it has not yet been commited to the database.
Furthermore, the create method does not save objects. It is the responsibility of the creator to save new objects which are being created.
Example
# Import required objects
from ursa_sync.backend import TimeSeries
from ursa_sync import session
# Open a session with the database
db = session()
# Create a new DataTable object
DT = DataTable.create(
session=db,
name = "Test DataTable", # The name of the object
active = True, # Whether or not to actively update the table in memory
cache_n_rows_local=1000, # How many rows to actively maintain in memory
sync_frequency_local=60, # How often synchronize
timeseries = TimeSeries.load_by(
['frequency'],
['1m'],
db
) # Attaches all TimeSeries objects with a frequency of 1m
)
# Save the object
DT.save()
# Always close the database session.
db.close()
Unique Filter
Unique filters are specified individually for each entity class. The conditions under which a specific entity class can be considered unique are as follows:
TimeSeries
A TimeSeries object TS is unique when for no other TimeSeries object in the database, all of the following attributes are exactly identical: TS.underlying_id, TS.frequency, TS.series_type.
DataTable
A DataTable object DT is unique when there exist no other DataTable in the database, _DT, for which the set(DT.timeseries) == set(_DT.timeseries).
Underlying
An Underlying U is considered unique if it’s asset_ticker is unique. i.e. U.asset_ticker != _U.asset_ticker for all _U in Underlying.load_all(db).
Source
A Source is considered unique if it’s name is unique in the database.
PrivateKey
A PrivateKey is considered unique if it’s name is unique in the database.