Skip to content

Instantly share code, notes, and snippets.

@ragingbal
Created January 21, 2016 12:53
Show Gist options
  • Save ragingbal/e618050545988d7668f1 to your computer and use it in GitHub Desktop.
Save ragingbal/e618050545988d7668f1 to your computer and use it in GitHub Desktop.
import sys,cmd
import ClassifierLib
import argparse
class ClassifierShell(cmd.Cmd):
intro = 'Welcome to the Classifier shell. Type help or ? to list commands.\n'
prompt = '(classifier) '
file = open('history.txt','a')
# ----- basic classifier commands -----
def do_add_term(self, arg):
'find signatures that match query and classify them : do_find_and_classify -t Term To Find -c Category Name'
parser = argparse.ArgumentParser()
parser.add_argument('-t', nargs='+')
parser.add_argument('-c', nargs='*')
args = parser.parse_args(arg.split())
term = ' '.join(args.t)
category = ' '.join(args.c)
ClassifierLib.addTerm(term,category)
def do_find_and_classify(self,arg):
'find signatures that match query and classify them : do_find_and_classify -t Term To Find -c Category Name'
parser = argparse.ArgumentParser()
parser.add_argument('-t', nargs='+')
parser.add_argument('-c', nargs='*')
args = parser.parse_args(arg.split())
term = ' '.join(args.t)
category = ' '.join(args.c)
ClassifierLib.findAndClassify(term,category)
def do_find_desc_and_classify(self,arg):
'find description that match query and classify them : do_find_and_classify -t Term To Find -c Category Name'
parser = argparse.ArgumentParser()
parser.add_argument('-t', nargs='+')
parser.add_argument('-c', nargs='*')
args = parser.parse_args(arg.split())
term = ' '.join(args.t)
category = ' '.join(args.c)
ClassifierLib.findDescriptionAndClassify(term,category)
def do_classify_merchant_signature(self,arg):
'classify a merchant signature'
parser = argparse.ArgumentParser()
parser.add_argument('-t', nargs='+')
parser.add_argument('-c', nargs='*')
args = parser.parse_args(arg.split())
term = ' '.join(args.t)
category = ' '.join(args.c)
ClassifierLib.classifyMerchantSignature(term,category)
def do_find_desc_and_classify_loose(self,arg):
'find description that match query and classify them : do_find_and_classify -t Term To Find -c Category Name'
parser = argparse.ArgumentParser()
parser.add_argument('-t', nargs='+')
parser.add_argument('-c', nargs='*')
args = parser.parse_args(arg.split())
term = ' '.join(args.t)
category = ' '.join(args.c)
ClassifierLib.findDescriptionAndClassifyLoose(term,category)
def do_find_desc_by_category(self,arg):
'find all transaction descriptions in a specified category'
ClassifierLib.findDescriptionByCategory(arg)
def do_export_rules(self,arg):
'export a file will with all rules'
ClassifierLib.exportRules(arg)
def do_remove_term(self, arg):
'remove term from db'
ClassifierLib.removeTerm(arg)
#forward(*parse(arg))
print(arg)
def do_clean_merchant_signatures(self,arg):
'clean up the merchant signatures'
ClassifierLib.cleanMerchantSignatures()
def do_set_category(self,arg):
'set the category for the rest of the session'
self.category = arg
def do_exportClassifications():
'export classifications from current session'
#forward(*parse(arg))
print(arg)
def do_import_terms(self,arg):
'import a set of terms from a file'
ClassifierLib.importTerms(arg)
#forward(*parse(arg))
print(arg)
def do_list_terms(self,arg):
'shows all terms'
ClassifierLib.listTerms()
def do_find_by_title(self,arg):
'find a set of matching terms'
#forward(*parse(arg))
ClassifierLib.findByTitle(arg)
def do_find_by_category(self,arg):
'find a set of terms in category'
#forward(*parse(arg))
ClassifierLib.findByCategory(arg)
def do_create_rules_from_ga_merchants(self,arg):
'create rules for merchants in a ga category'
parser = argparse.ArgumentParser()
parser.add_argument('-f', nargs='+')
parser.add_argument('-t', nargs='*')
args = parser.parse_args(arg.split())
fromClassification = ' '.join(args.f)
toClassification = ' '.join(args.t)
ClassifierLib.createRulesFromGAMerchants(fromClassification,toClassification)
def do_change_terms_classification(self,arg):
'change the classification of multiple terms from one to other options -f fro from -t for to'
parser = argparse.ArgumentParser()
parser.add_argument('-f', nargs='+')
parser.add_argument('-t', nargs='*')
args = parser.parse_args(arg.split())
fromClassification = ' '.join(args.f)
toClassification = ' '.join(args.t)
ClassifierLib.changeTermsClassification(fromClassification,toClassification)
def do_find_description_by_term(self,arg):
'find transaction descriptions by term'
ClassifierLib.findDescriptionByTerm(arg)
def do_find_debit_card_merchants_by_category(self,arg):
'export a list of merchant descriptions by category'
ClassifierLib.findDebitCardMerchantsByCategory(arg)
def do_find_debit_card_merchant_term_classifications(self,arg):
'update map of merchat terms to classifications'
ClassifierLib.findDebitCardMerchantsTermClassifications()
def do_analyze_transactions_for_merchant_signatures(self,arg):
'get number of transactions for each unique merchant signature in db'
ClassifierLib.analyzeTransactionsForMerchantSignatures()
def do_bye(self, arg):
'Stop recording, close the Classifier window, and exit: BYE'
print('Thank you for using The Classifier')
ClassifierLib.closeDB()
self.close()
return True
# ----- record and playback -----
def do_record(self, arg):
'Save future commands to filename: RECORD rose.cmd'
self.file = open(arg, 'w')
def do_playback(self, arg):
'Playback commands from a file: PLAYBACK rose.cmd'
self.close()
with open(arg) as f:
self.cmdqueue.extend(f.read().splitlines())
def precmd(self, line):
line = line.lower()
if self.file and 'playback' not in line:
print(line, file=self.file)
return line
def close(self):
if self.file:
self.file.close()
self.file = None
def parse(arg):
'Convert a series of zero or more numbers to an argument tuple'
return tuple(map(int, arg.split()))
def get_args(arg):
'Convert a series of separated arguments into a list'
#return map(int, arg.split('\''))
if __name__ == '__main__':
ClassifierShell().cmdloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment