# Path: singleton.py
# class Singleton(type):
#   _instances = {}
#   def __call__(cls, *args, **kwargs):
#     if cls not in cls._instances:
#       cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
#     return cls._instances[cls]


import numpy as np
import pandas as pd
import pandas.io.sql
import logging
import random, re
import sys
import sqlite3
import traceback
import six
import pandas.core.algorithms
import data_cfg
  import json
    import json
from timeit import default_timer as currentTime
from six.moves import range
  from sqlalchemy import create_engine
from singleton import Singleton
    subset = runFilters(df,query['filter'],cache) # use the original df here to ensure that cache of id(df) works
    before = len(df.index)
    newdf = newdf.loc[subset,:]
    # TODO: this could be done using the query interface...
    #newdf = newdf.query(query['filter2'],local_dict={ 'null' : np.array([None] * before) } ) #pd.Series([None] * before) } )
    print(('filter: %d -> %d' % (before,len(newdf.index))))
  if query['aggregator'] is not None:
    aggCols = list(query['aggregator'].keys())
    for agg in query['aggregator']:
      grps = newdf.groupby(agg)
      newdf = grps.agg(query['aggregator'][agg][0])
      if (agg in cols): 
        newdf[agg] =  grps.size()
  # strip down to just the cols we are interested in
  if cols is not None and cols[0] is not None:
    evalExpr = re.findall('eval\((.*)\)',cols[0])
    #print cols
    #print newdf.columns.values
    #print evalExpr
    if len(evalExpr) > 0:
      print("Dynamic column %s" % evalExpr)
      cols = ['derived']                        # set the filter columns to the derived one
      newdf[cols[0]] = newdf.eval(evalExpr[0])  # add the derived values to the data frame
    newdf = newdf[list(cols)] 
  if query['cumsum'] is not None:
    # post process - applications include cumsum, random sub sampling, and targeted sub-sampling 
    # see also inplace=True arg for sort
    #chicago.sort('salary', ascending=False, inplace=True)
    #chicago = chicago.groupby('department').apply(ranker)
    cumdf = None

# Path: singleton.py
# class Singleton(type):
#   _instances = {}
#   def __call__(cls, *args, **kwargs):
#     if cls not in cls._instances:
#       cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
#     return cls._instances[cls]


# Path: DataService.py
import numpy as np
import pandas as pd
import pandas.io.sql
import logging
import random, re
import sys
import sqlite3
import traceback
import six
import pandas.core.algorithms
import data_cfg
  import json
    import json
from timeit import default_timer as currentTime
from six.moves import range
  from sqlalchemy import create_engine
from singleton import Singleton

    subset = runFilters(df,query['filter'],cache) # use the original df here to ensure that cache of id(df) works
    before = len(df.index)
    newdf = newdf.loc[subset,:]
    # TODO: this could be done using the query interface...
    #newdf = newdf.query(query['filter2'],local_dict={ 'null' : np.array([None] * before) } ) #pd.Series([None] * before) } )
    print(('filter: %d -> %d' % (before,len(newdf.index))))
  if query['aggregator'] is not None:
    aggCols = list(query['aggregator'].keys())
    for agg in query['aggregator']:
      grps = newdf.groupby(agg)
      newdf = grps.agg(query['aggregator'][agg][0])
      if (agg in cols): 
        newdf[agg] =  grps.size()
  # strip down to just the cols we are interested in
  if cols is not None and cols[0] is not None:
    evalExpr = re.findall('eval\((.*)\)',cols[0])
    #print cols
    #print newdf.columns.values
    #print evalExpr
    if len(evalExpr) > 0:
      print("Dynamic column %s" % evalExpr)
      cols = ['derived']                        # set the filter columns to the derived one
      newdf[cols[0]] = newdf.eval(evalExpr[0])  # add the derived values to the data frame
    newdf = newdf[list(cols)] 
  if query['cumsum'] is not None:
    # post process - applications include cumsum, random sub sampling, and targeted sub-sampling 
    # see also inplace=True arg for sort
    #chicago.sort('salary', ascending=False, inplace=True)
    #chicago = chicago.groupby('department').apply(ranker)
    cumdf = None

    for col in cols: