pandas_TextParser_hdlr.py 1.31 KB
Newer Older
Irene Perez Gonzalez's avatar
Irene Perez Gonzalez committed
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr  2 10:34:56 2019

Assumes we are never writing a header!

@author: iregon
"""

import pandas as pd
import mdf_reader.common.logging_hdlr as logging_hdlr

logger = logging_hdlr.init_logger(__name__,level = 'DEBUG')

def restore(TextParser_ref,TextParser_options):
    try:
        TextParser_ref.seek(0)
        TextParser = pd.read_csv( TextParser_ref, names = TextParser_options['names'],chunksize = TextParser_options['chunksize'], dtype = TextParser_options['dtype']) #, skiprows = options['skiprows'])
        return TextParser
    except Exception as e:
        logger.error('Failed to restore TextParser', exc_info=True)
        return TextParser

def is_not_empty(TextParser):
    try:
        TextParser_ref = TextParser.f
        TextParser_options = TextParser.orig_options
    except Exception as e:
        logger.error('Failed to process input. Input type is {}'.format(type(TextParser)), exc_info=True)
        return
    try:
        first_chunk = TextParser.get_chunk()
        TextParser = restore(TextParser_ref,TextParser_options)
        if len(first_chunk) > 0:
            logger.debug('Is not empty')
            return True, TextParser
        else:
            return False, TextParser
    except Exception:
         return False, TextParser