From 0b087b84518d5723bd1fec5498c8e64eab73b4b1 Mon Sep 17 00:00:00 2001 From: Chang She Date: Wed, 4 Apr 2012 22:05:40 -0400 Subject: [PATCH 1/3] add factor function for HDFStore that's a contextmanager --- pandas/io/pytables.py | 13 +++++++++++++ pandas/io/tests/test_pytables.py | 12 +++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 63e203dfaab6b..2a89cad57e3c9 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -13,6 +13,7 @@ from pandas.core.common import adjoin import pandas.core.common as com import pandas._tseries as lib +from contextlib import contextmanager # reading and writing the full object in one go _TYPE_MAP = { @@ -55,6 +56,18 @@ def _tables(): _table_mod = tables return _table_mod +@contextmanager +def get_store(path, mode='a', complevel=None, complib=None, + fletcher32=False): + store = None + try: + store = HDFStore(path, mode=mode, complevel=complevel, + complib=complib, fletcher32=False) + yield store + finally: + if store is not None: + store.close() + class HDFStore(object): """ dict-like IO interface for storing pandas objects in PyTables diff --git a/pandas/io/tests/test_pytables.py b/pandas/io/tests/test_pytables.py index 7d09d5625d57d..9082c4d549fef 100644 --- a/pandas/io/tests/test_pytables.py +++ b/pandas/io/tests/test_pytables.py @@ -7,7 +7,7 @@ import numpy as np from pandas import Series, DataFrame, Panel, DateRange, MultiIndex -from pandas.io.pytables import HDFStore +from pandas.io.pytables import HDFStore, get_store import pandas.util.testing as tm try: @@ -31,6 +31,16 @@ def tearDown(self): self.store.close() os.remove(self.path) + def test_factory_fun(self): + try: + with get_store(self.path) as tbl: + raise ValueError('blah') + except ValueError: + pass + + with get_store(self.path) as tbl: + pass + def test_len_keys(self): self.store['a'] = tm.makeTimeSeries() self.store['b'] = tm.makeStringSeries() From f587b3459953c2f22a7fed94dbadbb266ff8f52b Mon Sep 17 00:00:00 2001 From: Chang She Date: Fri, 6 Apr 2012 10:03:57 -0400 Subject: [PATCH 2/3] TST: added a couple more tests for get_store function --- pandas/io/tests/test_pytables.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pandas/io/tests/test_pytables.py b/pandas/io/tests/test_pytables.py index 9082c4d549fef..81beeadd864c9 100644 --- a/pandas/io/tests/test_pytables.py +++ b/pandas/io/tests/test_pytables.py @@ -33,13 +33,19 @@ def tearDown(self): def test_factory_fun(self): try: - with get_store(self.path) as tbl: + with get_store(self.scratchpath) as tbl: raise ValueError('blah') except ValueError: pass - with get_store(self.path) as tbl: - pass + with get_store(self.scratchpath) as tbl: + tbl['a'] = tm.makeDataFrame() + + with get_store(self.scratchpath) as tbl: + self.assertEquals(len(tbl), 1) + self.assertEquals(type(tbl['a']), DataFrame) + + os.remove(self.scratchpath) def test_len_keys(self): self.store['a'] = tm.makeTimeSeries() @@ -483,6 +489,8 @@ def test_store_datetime_fractional_secs(self): self.store['a'] = series self.assertEquals(self.store['a'].index[0], dt) + + def curpath(): pth, _ = os.path.split(os.path.abspath(__file__)) return pth From cb3981b7db927eb87b15d290224ab3ad29b7c9b8 Mon Sep 17 00:00:00 2001 From: Chang She Date: Fri, 6 Apr 2012 10:10:06 -0400 Subject: [PATCH 3/3] DOC: docstring for get_store function --- pandas/io/pytables.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 2a89cad57e3c9..32fe2c62bae50 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -59,6 +59,40 @@ def _tables(): @contextmanager def get_store(path, mode='a', complevel=None, complib=None, fletcher32=False): + """ + Creates an HDFStore instance. This function can be used in a with statement + + Parameters + ---------- + path : string + File path to HDF5 file + mode : {'a', 'w', 'r', 'r+'}, default 'a' + + ``'r'`` + Read-only; no data can be modified. + ``'w'`` + Write; a new file is created (an existing file with the same + name would be deleted). + ``'a'`` + Append; an existing file is opened for reading and writing, + and if the file does not exist it is created. + ``'r+'`` + It is similar to ``'a'``, but the file must already exist. + complevel : int, 1-9, default 0 + If a complib is specified compression will be applied + where possible + complib : {'zlib', 'bzip2', 'lzo', 'blosc', None}, default None + If complevel is > 0 apply compression to objects written + in the store wherever possible + fletcher32 : bool, default False + If applying compression use the fletcher32 checksum + + Examples + -------- + >>> with get_store('test.h5') as store: + >>> store['foo'] = bar # write to HDF5 + >>> bar = store['foo'] # retrieve + """ store = None try: store = HDFStore(path, mode=mode, complevel=complevel,