Skip to content
Snippets Groups Projects
Commit d81805bf authored by Matthew Westphall's avatar Matthew Westphall
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
*
!*/
build/
dist/
!.gitignore
!*.py
!*.md
## Glances
A python package for concatenating multiple glances.txt files into a single
pandas DataFrame. Structured with a (variableName,statistic) multiIndex for
columns and file names for indices.
### Installation:
```bash
$ python setup.py install
```
### Example Usage:
```python
import glances
from pandas import IndexSlice as idx
#May take 10-15 seconds depending on number of files globbed
glance_df = glances.dataframe_from_glances('glance*.txt')
#use pandas.IndexSlice to access subsections
slice = df.loc[idx['glance-g001.txt':'glance-g005.txt'],idx['lat':'nedn_lw','shape':]]
print(slice)
```
from .glances import dataframe_from_glances
import pandas as pd
import numpy as np
import glob
import os
CACHED_COLUMNS = {}
def parse_line(glance_line):
"""Convert a data line from a glance file into a value that can be written
to a pandas DataFrame
"""
data = glance_line.split(':')[-1]
if 'None' in data:
return np.nan
elif '(' in data:
return data
else:
return np.float(data)
def df_fr_glance(glance_fname,idx):
"""Constructs a 3-tiered multiindex dataframe based on the file's parameters"""
with open(glance_fname,'r') as glance:
cols = []
vals = []
next_is_top = False
for line in glance.readlines():
#trim newline and leading spaces
line = line.strip()
if len(line) == 0:
#ignore empty lines
continue
if(line[0] == '-'):
#lines of all '-------' signify a new header
next_is_top = True
elif next_is_top:
next_is_top = False
top_key = line
elif ':' in line:
#read the bottom level key and append its hierarchy to the list
low_key = line[:line.index(':')]
datum = parse_line(line[line.index(':')+1:])
vals.append(datum)
cols.append((top_key,low_key))
cols = pd.MultiIndex.from_tuples(cols)
df = pd.DataFrame([vals],columns = cols,index=[idx])
return df
def dataframe_from_glances(fnames_or_glob):
"""Concatenates a series of dataframes constructed from a list
of glance files
"""
if isinstance(fnames_or_glob,str):
glance_files = glob.glob(fnames_or_glob)
glance_files.sort()
elif isinstance(fnames_or_glob,list):
glance_files = fnames_or_glob
else:
raise TypeError("Can only parse lists and strings into list of glance files.")
df_generator = (df_fr_glance(fname,os.path.basename(fname)) for i, fname in enumerate(glance_files))
return pd.concat(df_generator)
setup.py 0 → 100644
from setuptools import setup, find_packages
setup(
name='glances',
version='0.1',
packages=find_packages(),
url='',
license='',
author='mwestphall',
author_email='westphall@wisc.edu',
description='',
install_requires=[
'pandas',
'numpy',
],
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment