def setup_logging(log_level, filename): """ Setup default logger to log to stderr or and possible also to a file. The default logger is used like this: >>> import logging >>> logging.error(text message) """ format= "%(asctime)s [%(levelname)s] %(message)s" # Setup logging to stderr console_handler = logging.StreamHandler() console_handler.setFormatter(logging.Formatter(format)) console_handler.setLevel(log_level) logging.getLogger().addHandler(console_handler) # Setup logging to file if filename is specified if filename: file_handler = logging.FileHandler(filename, "w") file_handler.setFormatter(logging.Formatter(format)) file_handler.setLevel(log_level) logging.getLogger().addHandler(file_handler) logging.getLogger().setLevel(log_level) loginfo("Logging set up with log level=%s, filename=%s", log_level, filename)
def get(self): person = getPerson() loc = person.location.split(":") skey = self.request.get("key") loginfo(person) loginfo(skey) if skey: person = ndb.Key(urlsafe=skey).get() template_vars = { "post_location" : "/editInformation", "value": [loc[0],loc[1],loc[2],person.eservice_info[0].get().number,person.eservice_info[1].get().number], } template = jinja_env.get_template("templates/form.html") self.response.write(template.render(template_vars))
def post(self): names = ["Police Department","Fire Department"] current_user = users.get_current_user().email() loc = self.request.get("Country")+":"+self.request.get("City")+":"+self.request.get("Zip") input_info =[ Information( name="Police Department", location=loc, number=self.request.get("Police") ), Information( name="Fire Department", location=loc, number=self.request.get("Fire") ), ] toplace_info = [] input_keys = [] for i in range(len(input_info)): l = Information.query().filter(ndb.AND(Information.name == input_info[i].name,Information.location == input_info[i].location)).fetch() input_keys.append(input_info[i].put()) most_common_number = mostCommon(l,"name",names[i],"number") loginfo(most_common_number) loginfo("K\n\n\n") if most_common_number != None: toplace_info.append(most_common_number) else: loginfo(input_info) loginfo("J\n\n\n") toplace_info.append(input_info[i]) super_persons = Person.query().filter(Person.id == loc).fetch() if len(super_persons) == 0: Person( id=loc, location=loc, eservice_info=[toplace_info[0].put(),toplace_info[1].put()], econtacts_info=[], ).put() else: super_person = super_persons[0] super_person.eservice_info=[toplace_info[0].put(),toplace_info[1].put()] Person( id=str(current_user), location=loc, eservice_info=[input_keys[0],input_keys[1]], econtacts_info=[], ).put() template_vars = { "top": "Setup complete", "redirect": "/emergency", "explaination": "Emergency Page" } template = jinja_env.get_template("templates/finished.html") self.response.write(template.render(template_vars))
def listContains(li,subli): loginfo(li) loginfo(subli) loginfo("EARTH\n\n\n") if len(subli) > len(li): return False consistent = -1 pos = 1 for item in subli: if item == "": pos = pos+1 consistent = consistent+1 continue if item in li: if consistent == -1: consistent = li.index(item) else: if consistent+1 == li.index(item): consistent = consistent+1 pos = pos+1 else: consistent = -1 pos = 1 if pos == len(subli): return True else: return False
def run_indicators(main=False, **kwargs): pargs = parse_args(None if main else [], main=main) loginfo('') loginfo('[+] From main : {}'.format(main)) if pargs.list_names: loginfo(', '.join(kwargs)) return 0 # success for name, testdata in kwargs.items(): if name != (pargs.name or name): continue RESULTS[name] = run_indicator(pargs, name, testdata, main=main) all_good = all(RESULTS.values()) loginfo('[+]' + '-' * 74) logging.info('[+] Global Result: {}'.format(all_good)) if not all_good: sys.exit(1)
def run_indicators(metatests, main=False): pargs = parse_args(None if main else [], main=main) loginfo('') loginfo('[+] From main : {}'.format(main)) if pargs.list_names: loginfo(', '.join(metatests)) return 0 # success if pargs.name: # requested specific indicators mtests = {k: v for k, v in metatests.items() if k in pargs.name} else: mtests = metatests if pargs.ad_hoc: for name in pargs.name: if name in mtests: continue mtests[name] = {} posttest = {} for name, testdata in mtests.items(): if isinstance(testdata, str): # delay test referred to other tests posttest[name] = testdata continue # skip for later RESULTS[name] = run_indicator(pargs, name, testdata, main=main) # run delayed string tests for name, testdata in posttest.items(): RESULTS[name] = run_indicator(pargs, name, testdata, main=main) all_good = all(RESULTS.values()) loginfo('[+]' + '-' * 74) logging.info('[+] Global Result: {}'.format(all_good)) if not all_good: sys.exit(1)
def run_indicator(pargs, name, testdata, main=False): loginfo('[+]' + '-' * 74) loginfo('[+] Running test for : {}'.format(name)) loginfo('[+] Testdata is : {}'.format(testdata)) # If a string has been passed, check and return the result if isinstance(testdata, str): rother = RESULTS.get(testdata, False) loginfo('[+] Test completed with : {}'.format(testdata)) loginfo('[+] Test Result : {} ({})'.format(rother, name)) return rother elif callable(testdata): loginfo('[+] Calling tesdata') try: ret = testdata(main=main) except AssertionError as e: ret = False _, _, tb = sys.exc_info() tb_info = traceback.extract_tb(tb) filename, line, func, text = tb_info[-1] logging.error('[-] Assertiont Message "{}"'.format(e)) logging.error('[-] File {} / Line {} / Text {}'.format( filename, line, text, )) loginfo('[+] Test Result : {} ({})'.format(ret, name)) return ret # bta-lib indicator calculation # The indicator is either the given test name or specified in test data btind = getattr(btalib, testdata.get('btind', name)) # The inputs are either specified in the testdata or the default from ind inputs = [df[x] for x in testdata.get('inputs', btind.inputs)] btkwargs = testdata.get('btkwargs', {}) if pargs.bt_overargs: btkwargs = eval('dict(' + pargs.bt_overargs + ')') elif pargs.bt_kwargs: btkwargs.update(eval('dict(' + pargs.bt_kwargs + ')')) btresult = btind(*inputs, **btkwargs) btouts = list(btresult.outputs) for a, b in testdata.get('swapouts', {}).items(): btouts[a], btouts[b] = btouts[b], btouts[a] checkminperiods = testdata.get('minperiods', []) if checkminperiods: eqperiods = btresult._minperiods == checkminperiods else: eqperiods = -1 # Now, determine the actual indicators. The name is the name from the # bta-lib indicator. Find the corresponding ta indicator # Either specified or capitalize the given name taind_name = testdata.get('taind', name.upper()) try: taind = getattr(talib, taind_name) except AttributeError: for taind_name in btind.alias: try: taind = getattr(talib, taind_name) except AttributeError: pass else: break else: logerror('[-] No ta-lib indicator found for: {}'.format(name)) return False takwargs = testdata.get('takwargs', {}) if pargs.ta_overargs: takwargs = eval('dict(' + pargs.ta_overargs + ')') elif pargs.ta_kwargs: takwargs.update(eval('dict(' + pargs.ta_kwargs + ')')) touts = taind(*inputs, **takwargs) if isinstance(touts, pd.Series): # check if single output touts = (touts, ) # consistent single-multiple result presentation # Result checking logseries = [] equal = True # innocent until proven guilty for tseries, btout in zip(touts, btouts): btseries = btout.series # Rounding to x decimals decimals = pargs.decimals if decimals is None: # no command line argument was given decimals = testdata.get('decimals', None) if decimals is not None and decimals >= 0: tseries = tseries.round(decimals=decimals) btseries = btseries.round(decimals=decimals) # Keep record of entire series for verbosity logseries.append([tseries, btseries, btseries.eq(tseries)]) # Minperiod test check settings test_minperiod = pargs.minperiod if test_minperiod is None: # nothing in command line test_minperiod = testdata.get('minperiod', 0) if not test_minperiod: minperiod = btresult._minperiod # global minperiod elif test_minperiod > 0: minperiod = btout._minperiod # per output minperiod else: # < 0 minperiod = 0 # no minperiod at all if minperiod: # check requested from non starting point tseries = tseries[minperiod:] btseries = btseries[minperiod:] equality = btseries.eq(tseries) # calculate equality of series allequal = equality.all() if not allequal: # make a nancheck if btseries.isna().all() and tseries.isna().all(): allequal = True # both deliver only nans ... are equal equal = equal and allequal # check if result still equal True logging.info('[+] Test Result : {} ({})'.format(equal, name)) if pargs.verbose: # if verbosity is requested # General Information logdebug('-' * 78) logdebug('Result : {}'.format(equal)) logdebug('Chk Minperiods : {} {}'.format( eqperiods, '(-1 if no check done)', )) logdebug('Decimals : {}'.format(decimals)) logdebug('-' * 78) logdebug('Indicator : {}'.format(btind.__name__)) logdebug('Inputs : {}'.format(btind.inputs)) logdebug('Outputs : {}'.format(btind.outputs)) logdebug('Def Params : {}'.format(btind.params)) logdebug('Params : {}'.format(dict(btresult.params))) logdebug('-' * 78) logdebug('Period Check : {} {}'.format( test_minperiod, ('(0: after max minperiod / 1: per line / -1: ignore)'), )) logdebug('Minperiods : {}'.format(btresult._minperiods)) logdebug('Minperiod : {}'.format(btresult._minperiod)) logdebug('-' * 78) # Generate logging dataframe pdct = {'count': range(1, len(df.index) + 1)} # visual period check for tseries, btseries, eqseries in logseries: name = btseries._name pdct['ta__' + name] = tseries pdct['bta_' + name] = btseries pdct['eq__' + name] = eqseries logdf = pd.DataFrame(pdct) logdebug(logdf.to_string()) return equal
def run_indicators(metatests, main=False): pargs = parse_args(None if main else [], main=main) loginfo('') loginfo('[+] From main : {}'.format(main)) if pargs.list_names: loginfo(', '.join(metatests)) sys.exit(0) if pargs.name: # requested specific indicators mtests = {k: v for k, v in metatests.items() if k in pargs.name} else: mtests = metatests if pargs.ad_hoc: for name in pargs.name: if name in mtests: continue mtests[name] = {} if not mtests: # empty test set ... logerror('[-] No tests could be found') if pargs.name: logerror('[-] Wanted Indicators: {}'.format(','.join(pargs.name))) sys.exit(0) posttest = {} for name, testdata in mtests.items(): if testdata is None: # skip continue if not testdata: # empty - create empty data testdata = {} if isinstance(testdata, int): # only minperiods specified testdata = dict(minperiods=testdata) if isinstance(testdata, str): # delay test referred to other tests posttest[name] = testdata continue # skip for later RESULTS[name] = run_indicator(pargs, name, testdata, main=main) # pseudo-run delayed string tests for name, othername in posttest.items(): loginfo('[+]' + '-' * 74) loginfo('[+] Test def is string: "{}"'.format(othername)) if othername not in RESULTS: logerror('[-] Test "{}" not run'.format(othername)) rother = False else: loginfo('[+] Test completed with : {}'.format(othername)) rother = RESULTS[othername] RESULTS[name] = rother loginfo('[+] Test Result : {} ({})'.format(rother, name)) all_good = all(RESULTS.values()) loginfo('[+]' + '-' * 74) logging.info('[+] Global Result: {}'.format(all_good)) if not all_good: sys.exit(1)
def run_indicator(pargs, name, testdata, main=False): loginfo('[+]' + '-' * 74) loginfo('[+] Running test for : {}'.format(name)) loginfo('[+] Testdata is : {}'.format(testdata)) # If a string has been passed, check and return the result if callable(testdata): loginfo('[+] Calling tesdata') try: ret = testdata(main=main) except AssertionError as e: ret = False _, _, tb = sys.exc_info() tb_info = traceback.extract_tb(tb) filename, line, func, text = tb_info[-1] logging.error('[-] Assertiont Message "{}"'.format(e)) logging.error('[-] File {} / Line {} / Text {}'.format( filename, line, text, )) loginfo('[+] Test Result : {} ({})'.format(ret, name)) return ret # bta-lib indicator calculation # The indicator is either the given test name or specified in test data btind = getattr(btalib, testdata.get('btind', name)) # The inputs are either specified in the testdata or the default from ind inames = testdata.get('inputs', btind.inputs) logging.info('[+] Gathering inputs {}'.format(inames)) inputs = [df[x] for x in inames] if 'inputop' in testdata: inputs = testdata['inputop'](*inputs) tacompat = testdata.get('talib', False) if tacompat: tacompat = not pargs.no_talib else: tacompat = pargs.talib btkwargs = testdata.get('btkwargs', {}) if tacompat: btkwargs.setdefault('_talib', tacompat) if pargs.bt_overargs: btkwargs = eval('dict(' + pargs.bt_overargs + ')') elif pargs.bt_kwargs: btkwargs.update(eval('dict(' + pargs.bt_kwargs + ')')) btresult = btind(*inputs, **btkwargs) btouts = list(btresult.outputs) for a, b in testdata.get('swapouts', {}).items(): btouts[a], btouts[b] = btouts[b], btouts[a] if 'minperiod' in testdata: checkminperiods = testdata['minperiod'] else: checkminperiods = testdata.get('minperiods', [1]) if isinstance(checkminperiods, int): checkminperiods = [checkminperiods] * len(btouts) if checkminperiods: eqperiods = btresult._minperiods == checkminperiods else: eqperiods = -1 logging.info('[+] Periods Test Result : {} ({})'.format(eqperiods, name)) if not pargs.standalone: touts = run_taindicator(name, testdata, inputs, btind, pargs) else: # in standalone mode, fake the outputs from a ta-ind touts = [] for output in btind.outputs: touts.append(pd.Series(np.nan, index=df.index)) # Result checking logseries = [] equal = True # innocent until proven guilty for tseries, btout in zip(touts, btouts): btseries = btout.series # Rounding to x decimals decimals = pargs.decimals if decimals is None: # no command line argument was given decimals = testdata.get('decimals', None) if decimals is not None and decimals >= 0: tseries = tseries.round(decimals=decimals) btseries = btseries.round(decimals=decimals) # Keep record of entire series for verbosity logseries.append([tseries, btseries, btseries.eq(tseries)]) # Minperiod test check settings test_minperiod = pargs.minperiod if test_minperiod is None: # nothing in command line test_minperiod = testdata.get('minperiod', 0) if not test_minperiod: minperiod = btresult._minperiod # global minperiod elif test_minperiod > 0: minperiod = btout._minperiod # per output minperiod else: # < 0 minperiod = 0 # no minperiod at all if minperiod: # check requested from non starting point tseries = tseries[minperiod:] btseries = btseries[minperiod:] equality = btseries.eq(tseries) # calculate equality of series allequal = equality.all() if not allequal: # make a nancheck if btseries.isna().all() and tseries.isna().all(): allequal = True # both deliver only nans ... are equal equal = equal and allequal # check if result still equal True logging.info('[+] Output Test Result : {} ({})'.format(equal, name)) serequal = equal if eqperiods != -1: equal = equal and eqperiods logging.info('[+] Test Result : {} ({})'.format(equal, name)) if pargs.verbose: # if verbosity is requested # General Information logdebug('-' * 78) logdebug('Result : {}'.format(equal)) logdebug('Series Result : {}'.format(serequal)) logdebug('Chk Minperiods : {} {}'.format( eqperiods, '(-1 if no check done)', )) logdebug('Decimals : {}'.format(decimals)) logdebug('-' * 78) logdebug('Indicator : {}'.format(btind.__name__)) logdebug('Inputs : {}'.format(btind.inputs)) logdebug('Outputs : {}'.format(btind.outputs)) logdebug('Def Params : {}'.format(btind.params)) logdebug('Params : {}'.format(dict(btresult.params))) logdebug('-' * 78) logdebug('Period Check : {} {}'.format( test_minperiod, ('(0: after max minperiod / 1: per line / -1: ignore)'), )) logdebug('Minperiods : {}'.format(btresult._minperiods)) logdebug('Minperiod : {}'.format(btresult._minperiod)) logdebug('-' * 78) # Generate logging dataframe pdct = {'count': range(1, len(df.index) + 1)} # visual period check for tseries, btseries, eqseries in logseries: name = btseries._name pdct['ta__' + name] = tseries pdct['bta_' + name] = btseries pdct['eq__' + name] = eqseries logdf = pd.DataFrame(pdct) logdebug(logdf.to_string()) return equal