ATLAS Offline Software
Loading...
Searching...
No Matches
python.trfUtils.memFileToTable Class Reference

Extract a table of data from a txt file. More...

Collaboration diagram for python.trfUtils.memFileToTable:

Public Member Functions

 getTable (self, filename, header=None, separator="\t")

Protected Member Functions

 _defineTableDictKeys (self, header, fields, separator)
 Define the keys for the tabledict dictionary.

Detailed Description

Extract a table of data from a txt file.

E.g. header="Time nprocs nthreads wtime stime utime pss rss swap vmem" or the first line in the file each of which will become keys in the dictionary, whose corresponding values are stored in lists, with the entries corresponding to the values in the rows of the input file. The output dictionary will have the format {'Time': [ .. data from first row .. ], 'VMEM': [.. data from second row], ..}

Parameters
filenamename of input text file, full path (string).
headerheader string.
separatorseparator character (char).
Returns
dictionary.

Definition at line 1511 of file trfUtils.py.

Member Function Documentation

◆ _defineTableDictKeys()

python.trfUtils.memFileToTable._defineTableDictKeys ( self,
header,
fields,
separator )
protected

Define the keys for the tabledict dictionary.

Parameters
headerheader string.
fieldsheader content string.
separatorseparator character (char).
Returns
tabledict (dictionary), keylist (ordered list with dictionary key names).

Definition at line 1547 of file trfUtils.py.

1547 def _defineTableDictKeys(self, header, fields, separator):
1548 tabledict = {}
1549 keylist = []
1550
1551 if not header:
1552 # get the dictionary keys from the header of the file
1553 for key in fields:
1554 # first line defines the header, whose elements will be used as dictionary keys
1555 if key == '':
1556 continue
1557 if key.endswith('\n'):
1558 key = key[:-1]
1559 tabledict[key] = []
1560 keylist.append(key)
1561 else:
1562 # get the dictionary keys from the provided header
1563 keys = header.split(separator)
1564 for key in keys:
1565 if key == '':
1566 continue
1567 if key.endswith('\n'):
1568 key = key[:-1]
1569 tabledict[key] = []
1570 keylist.append(key)
1571
1572 return tabledict, keylist
1573
1574

◆ getTable()

python.trfUtils.memFileToTable.getTable ( self,
filename,
header = None,
separator = "\t" )

Definition at line 1513 of file trfUtils.py.

1513 def getTable(self, filename, header=None, separator="\t"):
1514 tabledict = {}
1515 keylist = []
1516 try:
1517 f = open(filename, 'r')
1518 except Exception as e:
1519 msg.warning("failed to open file: {0}, {1}".format(filename, e))
1520 else:
1521 firstline = True
1522 for line in f:
1523 fields = line.split(separator)
1524 fields = [''.join(filter(str.isprintable, field)) for field in fields]
1525 if firstline:
1526 firstline = False
1527 tabledict, keylist = self._defineTableDictKeys(header, fields, separator)
1528 if not header:
1529 continue
1530 # from now on, fill the dictionary fields with the input data
1531 i = 0
1532 for field in fields:
1533 # get the corresponding dictionary key from the keylist
1534 key = keylist[i]
1535 # store the field value in the correct list
1536 tabledict[key].append(float(field))
1537 i += 1
1538 f.close()
1539
1540 return tabledict
1541

The documentation for this class was generated from the following file: