forked from respec/HSPsquared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.py
61 lines (54 loc) · 1.9 KB
/
fetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
''' Copyright (c) 2020 by RESPEC, INC.
Author: Robert Heaphy, Ph.D.
License: LGPL2
'''
from pandas import HDFStore, read_csv
from io import StringIO
def fetchtable(hdfname, path, names=[], usercol=None, usercolvalue=None, CSV=False):
'''
Fetch HSP2 table and after user modification, use it to update HDF5 file.
If CSV=True is used, the return to fetch() must also be a CSV string!
Parameters
----------
hdfname : str
HSP2 HDF5 filename
path : str
HDF5 path to desired dataset (table)
names : list of strings, optional
Columns in selected table to return
The default is [] (all columns).
usercol : str, optional
Name of user defined column of strings in desired table
The default is None.
usercolvalue : str, optional
Rows of table matching this value are to be returned
The default is None. REQUIRED if usercol is specified
CSV : bool
Output table is CSV string
The default is False
Returns
-------
table : Pandas DataFrame
Table at path (with possible subsetting by names and usercol) for user
to modify.
replace : (closure) function
Call this function with modified table to update table in HDF5 file
'''
with HDFStore(hdfname) as store:
dforiginal = store[path]
df = dforiginal.copy()
if usercol: # subset rows using user defined table
df = df[df[usercol] == usercolvalue]
if names: # subset columns by names in list
df = df[names]
df = df.copy()
if CSV:
df = df.to_csv()
def replace(dff):
nonlocal dforiginal
if CSV:
dff = read_csv(StringIO(dff), index_col=0)
print(dff)
dforiginal.update(dff)
dforiginal.to_hdf(store, path, format='table', data_columns=True)
return df, replace