<commit_before>#!/usr/bin/env python
"""Simple interface interface to a postgres database taking
connection infromation from '.desservices.ini' file.

For more documentation on desservices, see here:
https://opensource.ncsa.illinois.edu/confluence/x/lwCsAw

"""

import os
import logging

import psycopg2
import numpy as np

class Database(object):
    
    def __init__(self,dbname='db-fnal'):
        self.dbname = dbname
        self.conninfo = self.parse_config(section=dbname)
        self.connection = None
        self.cursor = None

    def __str__(self):
        ret = str(self.connection)
        return ret

    def parse_config(self, filename=None, section='db-fnal'):
        #if not filename: filename=os.getenv("DES_SERVICES")
        if os.path.exists(".desservices.ini"):
            filename=os.path.expandvars("$PWD/.desservices.ini")
        else:
            filename=os.path.expandvars("$HOME/.desservices.ini")
        logging.debug('.desservices.ini: %s'%filename)

        # ConfigParser throws "no section error" if file does not exist...
        # That's confusing, so 'open' to get a more understandable error
        open(filename) 
        import ConfigParser
        c = ConfigParser.RawConfigParser()
        c.read(filename)

        d={}
        d['host']     = c.get(section,'server')
        d['dbname']   = c.get(section,'name')
        d['user']     = c.get(section,'user')
        d['password'] = c.get(section,'passwd')
        d['port']     = c.get(section,'port')
        return d

    def connect(self):
        self.connection = psycopg2.connect(**self.conninfo)
        self.cursor = self.connection.cursor()

    def disconnect(self):
        self.cursor.close()
        self.cursor = None
        self.connection.close()
        self.connection = None

    def execute(self,query):
        self.cursor.execute(query)      
        try: 
            return self.cursor.fetchall()
        except Exception, e:
            self.reset()
            raise(e)
        
    def reset(self):
        self.connection.reset()

    def get_columns(self):
        return [d[0] for d in self.cursor.description]

    def query2recarray(self,query):
        # Doesn't work for all data types
        data = self.execute(query)
        names = self.get_columns()
        return np.rec.array(data,names=names)

        
if __name__ == "__main__":
    import argparse
    description = "python script"
    parser = argparse.ArgumentParser(description=description)
    opts = parser.parse_args()

    db = Database()
    db.connect()
    print db
<commit_msg>deal with empty selection<commit_after>#!/usr/bin/env python
"""Simple interface interface to a postgres database taking
connection infromation from '.desservices.ini' file.

For more documentation on desservices, see here:
https://opensource.ncsa.illinois.edu/confluence/x/lwCsAw

"""

import os
import logging

import psycopg2
import numpy as np

class Database(object):
    
    def __init__(self,dbname='db-fnal'):
        self.dbname = dbname
        self.conninfo = self.parse_config(section=dbname)
        self.connection = None
        self.cursor = None

    def __str__(self):
        ret = str(self.connection)
        return ret

    def parse_config(self, filename=None, section='db-fnal'):
        #if not filename: filename=os.getenv("DES_SERVICES")
        if os.path.exists(".desservices.ini"):
            filename=os.path.expandvars("$PWD/.desservices.ini")
        else:
            filename=os.path.expandvars("$HOME/.desservices.ini")
        logging.debug('.desservices.ini: %s'%filename)

        # ConfigParser throws "no section error" if file does not exist...
        # That's confusing, so 'open' to get a more understandable error
        open(filename) 
        import ConfigParser
        c = ConfigParser.RawConfigParser()
        c.read(filename)

        d={}
        d['host']     = c.get(section,'server')
        d['dbname']   = c.get(section,'name')
        d['user']     = c.get(section,'user')
        d['password'] = c.get(section,'passwd')
        d['port']     = c.get(section,'port')
        return d

    def connect(self):
        self.connection = psycopg2.connect(**self.conninfo)
        self.cursor = self.connection.cursor()

    def disconnect(self):
        self.cursor.close()
        self.cursor = None
        self.connection.close()
        self.connection = None

    def execute(self,query):
        self.cursor.execute(query)      
        try: 
            return self.cursor.fetchall()
        except Exception, e:
            self.reset()
            raise(e)
        
    def reset(self):
        self.connection.reset()

    def get_columns(self,query=None):
        if query: self.select(query)
        return [d[0] for d in self.cursor.description]

    def query2recarray(self,query):
        # Doesn't work for all data types
        data = self.execute(query)
        names = self.get_columns()
        if not len(data):
            msg = "No data returned by query"
            raise ValueError(msg)
        return np.rec.array(data,names=names)

if __name__ == "__main__":
    import argparse
    description = "python script"
    parser = argparse.ArgumentParser(description=description)
    opts = parser.parse_args()

    db = Database()
    db.connect()
    print db

{'fraction_non_alphanumeric': 0.11850740671291955, 'fraction_numerical': 0.0015000937558597412, 'mean_word_length': 2.3738140417457307, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3481480', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1554, 'n_words': 361}