11 from ctypes
import POINTER
12 from SQLamarr
import clib
16 clib.make_database.argtypes = (POINTER(ctypes.c_char),)
17 clib.make_database.restype = ctypes.c_void_p
19 clib.del_database.argtypes = (ctypes.c_void_p,)
21 clib.GlobalPRNG_get_or_create.argtypes = (ctypes.c_void_p, ctypes.c_int)
26 A database connection handler easying sharing the DB between C++ and Python.
29 def __init__(self, path: str =
"file::memory:?cache=shared"):
31 Open the connection for the C++ application, with shared cache to ease
32 access from Python to the same tables.
34 @param path: path-like or URI identifying the target resource; by
35 default, an non-threadsafe connection to an in-memory database is opened.
38 Connecting to an existing, prepared database stored on file `mydata.db`
40 db = SQLamarr.SQLite3DB("mydata.db")
43 Connecting to an empty, in-memory database for prototyping
45 db = SQLamarr.SQLite3DB()
48 Connecting to an empty, in-memory database in a multithreaded application
49 with each thread working on its own db.
51 db_wn1 = SQLamarr.SQLite3DB("file:wn1?mode=memory&cache=shared")
52 db_wn2 = SQLamarr.SQLite3DB("file:wn2?mode=memory&cache=shared")
55 self.
_path_path = path
if path.startswith(
56 "file:")
else f
"file:{path}?cache=shared"
58 self.
_pointer_pointer = clib.make_database(path.encode(
'ascii'))
61 """@private: Return the raw pointer to the algorithm."""
62 clib.del_database(self.
_pointer_pointer)
66 return self.
_path_path
69 """@private: Return the raw pointer to the database."""
74 Define the seed for the random number generated from Transformers
75 interacting with the database through this connection.
77 @param seed: integer seed for randomization
79 @returns SQLite3DB (self) instance
81 clib.GlobalPRNG_get_or_create(self.
_pointer_pointer, seed)
84 @contextlib.contextmanager
87 Python-like connection with a context-manager.
92 # Define the DB handler and seeds the random number generator
93 db = SQLamarr.SQLite3DB().seed(42)
95 # Load some event from HepMC2 ASCII file
96 loader = SQLamarr.HepMC2DataLoader(db)
97 loader.load(<file_path>, <evtNumber>,<runNumber>)
99 # Connect to the DB (while still open in C++ memory!)
100 with db.connect() as c:
101 # Loads in a pandas DataFrame the object
102 df = pandas.read_sql_query("SELECT * FROM GenParticles")
107 if self.
_path_path ==
":memory:":
108 raise NotImplementedError(
109 "Cannot connect to an in-memory database without cache sharing"
112 with sqlite3.connect(self.
_path_path, uri=
True)
as db:
A database connection handler easying sharing the DB between C++ and Python.
def seed(self, int seed)
Define the seed for the random number generated from Transformers interacting with the database throu...
def __init__(self, str path="file::memory:?cache=shared")
Open the connection for the C++ application, with shared cache to ease access from Python to the same...
def connect(self)
Python-like connection with a context-manager.