| 6 | |
| 7 | |
| 8 | class PyTablesDB(DB): |
| 9 | |
| 10 | def __init__( |
| 11 | self, |
| 12 | nrows, |
| 13 | rng, |
| 14 | userandom, |
| 15 | datadir, |
| 16 | docompress=0, |
| 17 | complib="zlib", |
| 18 | kind="medium", |
| 19 | optlevel=6, |
| 20 | ): |
| 21 | DB.__init__(self, nrows, rng, userandom) |
| 22 | self.tprof = [] |
| 23 | # Specific part for pytables |
| 24 | self.docompress = docompress |
| 25 | self.complib = complib |
| 26 | # Complete the filename |
| 27 | filename = f"pro-{self.filename.name}-O{optlevel}-{kind}" |
| 28 | if docompress: |
| 29 | filename = f"{filename}-{complib}{docompress}" |
| 30 | self.datadir = Path(datadir) |
| 31 | if not self.datadir.is_dir(): |
| 32 | self.datadir.mkdir(parents=True, exist_ok=True) |
| 33 | print(f"Created {self.datadir}.") |
| 34 | self.filename = self.datadir / f"{self.filename}.h5" |
| 35 | # The chosen filters |
| 36 | self.filters = tb.Filters( |
| 37 | complevel=self.docompress, complib=self.complib, shuffle=1 |
| 38 | ) |
| 39 | print("Processing database:", self.filename) |
| 40 | |
| 41 | def open_db(self, remove=0): |
| 42 | if remove and Path(self.filename).is_file(): |
| 43 | Path(self.filename).unlink() |
| 44 | con = tb.open_file(self.filename, "a") |
| 45 | return con |
| 46 | |
| 47 | def close_db(self, con): |
| 48 | # Remove first the table_cache attribute if it exists |
| 49 | if hasattr(self, "table_cache"): |
| 50 | del self.table_cache |
| 51 | con.close() |
| 52 | |
| 53 | def create_table(self, con): |
| 54 | class Record(tb.IsDescription): |
| 55 | col1 = tb.Int32Col() |
| 56 | col2 = tb.Int32Col() |
| 57 | col3 = tb.Float64Col() |
| 58 | col4 = tb.Float64Col() |
| 59 | |
| 60 | con.create_table( |
| 61 | con.root, |
| 62 | "table", |
| 63 | Record, |
| 64 | filters=self.filters, |
| 65 | expectedrows=self.nrows, |