PyLamarr
Pythonizations for the ultra-fast simulation option for the LHCb experiment
 
Loading...
Searching...
No Matches
PandasLoader.py
1import PyLamarr
2from functools import partial
3from dataclasses import dataclass
4from typing import Dict, Any
5from dataclasses import dataclass
6
7@dataclass
9 dataframe_dict: Dict[str, Any] = None
10 database: Any = None
11
12 def load(self):
13 with self.database.connect() as c:
14 for name, df in self.dataframe_dict.items():
15 df.to_sql(name, c, **self._kwargs)
16
17
18
20 """
21 Ease loading a set of pandas DataFrames to the SQLite event model.
22 As the other python test_data loaders, PandasLoader should be configured in the
23 constructor.
24 The configured object is then called during the event loop to
25 pass the updated connection to the SQLamarr.SQLite3DB instance.
26
27 The configuration keyword-arguments passed to the constructor are passed
28 to the pandas.DataFrame.to_sql function.
29
30 Some default values, however, is overridden as much more commonly adopted
31 in the context of Lamarr. In particular,
32 * `if_exists` defaults to `"append"`
33 * `index` defaults to `False`
34
35
36 """
37 def __init__(self,
38 **to_sql_kwargs
39 ):
40 self._db = None
41
42
43 self._kwargs = dict(if_exists='append', index=False)
44 self._kwargs.update(to_sql_kwargs)
45
46 def __call__(self, database):
47 self._db = database
48 return self
49
50 def load(self, **dataframe_dict):
51 """
52 Internal.
53 """
54 if self._db is None:
55 raise ValueError("PandasLoader tried loading with uninitialized db.\n"
56 "Missed ()?")
57
58 yield PandasEventBatch(dataframe_dict=dataframe_dict, database=self._db)
59
60
61
62
Ease loading a set of pandas DataFrames to the SQLite event model.
_kwargs
Override of the default values.
load(self, **dataframe_dict)
Internal.