def log_sample_info(self, index, varlist, funclist, student_eval, comparer, comparer_params_eval, comparer_result): """Add sample information to debug log""" pp = PrettyPrinter(indent=4) if index == 0: header = self.debug_appendix_header_template.format( grader=self.__class__.__name__, # The regexp replaces memory locations, e.g., 0x10eb1e848 -> 0x... functions_allowed=pp.pformat({ f: funclist[f] for f in funclist if f in self.permitted_functions }), functions_disallowed=pp.pformat({ f: funclist[f] for f in funclist if f not in self.permitted_functions }), ) self.log(re.sub(r"0x[0-9a-fA-F]+", "0x...", header)) self.log( self.debug_appendix_sample_template.format( sample_num=index + 1, # to account for 0 index samples_total=self.config['samples'], variables=pp.pformat(varlist), student_eval=student_eval, comparer=re.sub(r"0x[0-9a-fA-F]+", "0x...", str(comparer)), comparer_result=pp.pformat(comparer_result), compare_parms_eval=pp.pformat(comparer_params_eval)))
def index(request): context = {} if request.method == 'POST': form = forms.BasicPurchaseForm(request.POST) if form.is_valid(): cart = HiiCart.objects.create() if form.cleaned_data['fancypants']: LineItem.objects.create(cart=cart, quantity=1, **forms.PANTS) if form.cleaned_data['monkeybutler']: LineItem.objects.create(cart=cart, quantity=1, **forms.MONKEYBUTLER) if form.cleaned_data['subscription']: RecurringLineItem.objects.create(cart=cart, quantity=1, **forms.SUBSCRIPTION) result = cart.submit(form.cleaned_data['gateway']) if result.type == 'url': return HttpResponseRedirect(result.url) else: return HttpResponseRedirect('/paypal_redirect') else: form = forms.BasicPurchaseForm() p = PrettyPrinter(width=40) context['paypal'] = (p.pformat(settings.HIICART_SETTINGS['PAYPAL']), validate_gateway('paypal')) context['paypal2'] = (p.pformat(settings.HIICART_SETTINGS['PAYPAL2']), validate_gateway('paypal2')) context['paypal_adaptive'] = (p.pformat(settings.HIICART_SETTINGS['PAYPAL_ADAPTIVE']), validate_gateway('paypal_adaptive')) context['google'] = (p.pformat(settings.HIICART_SETTINGS['GOOGLE']), validate_gateway('google')) context['amazon'] = (p.pformat(settings.HIICART_SETTINGS['AMAZON']), validate_gateway('amazon')) context['form'] = form return render_to_response('index.html', context)
def serialize(self): namedtuples_to_dict = lambda v: { i: j._asdict() if isinstance(j, tuple) else j for i, j in v._asdict().items() } serialized_channel = {} to_save_ref = self.to_save() for k, v in to_save_ref.items(): if isinstance(v, tuple): serialized_channel[k] = namedtuples_to_dict(v) else: serialized_channel[k] = v dumped = ChannelJsonEncoder().encode(serialized_channel) roundtripped = json.loads(dumped) reconstructed = Channel(roundtripped) to_save_new = reconstructed.to_save() if to_save_new != to_save_ref: from pprint import PrettyPrinter pp = PrettyPrinter(indent=168) try: from deepdiff import DeepDiff except ImportError: raise Exception( "Channels did not roundtrip serialization without changes:\n" + pp.pformat(to_save_ref) + "\n" + pp.pformat(to_save_new)) else: raise Exception( "Channels did not roundtrip serialization without changes:\n" + pp.pformat(DeepDiff(to_save_ref, to_save_new))) return roundtripped
def __str__(self): pp = PrettyPrinter(indent=4) if len(self.tail) > 0: return pp.pformat(self.head)[:-1] + ' ... ' + pp.pformat( list(self.tail))[1:] else: return pp.pformat(self.head)
async def device_test(self, smile=Smile, testdata=None): """Perform basic device tests.""" _LOGGER.info("Asserting testdata:") device_list = smile.get_all_devices() self._write_json("get_all_devices", device_list) location_list, dummy = smile.scan_thermostats() _LOGGER.info("Gateway id = %s", smile.gateway_id) _LOGGER.info("Hostname = %s", smile.smile_hostname) self.show_setup(location_list, device_list) pp4 = PrettyPrinter(indent=4) pp8 = PrettyPrinter(indent=8) _LOGGER.debug("Device list:\n%s", pp4.pformat(device_list)) for dev_id, details in device_list.items(): data = smile.get_device_data(dev_id) self._write_json("get_device_data/" + dev_id, data) _LOGGER.debug( "%s", "Device {} id:{}\nDetails: {}\nData: {}".format( details["name"], dev_id, pp4.pformat(details), pp8.pformat(data) ), ) for testdevice, measurements in testdata.items(): assert testdevice in device_list # if testdevice not in device_list: # _LOGGER.info("Device {} to test against {} not found in device_list for {}".format(testdevice,measurements,self.smile_setup)) # else: # _LOGGER.info("Device {} to test found in {}".format(testdevice,device_list)) for dev_id, details in device_list.items(): if testdevice == dev_id: data = smile.get_device_data(dev_id) _LOGGER.info( "%s", "- Testing data for device {} ({})".format( details["name"], dev_id ), ) _LOGGER.info(" + Device data: %s", data) for measure_key, measure_assert in measurements.items(): _LOGGER.info( "%s", " + Testing {} (should be {})".format( measure_key, measure_assert ), ) if isinstance(data[measure_key], float): if float(data[measure_key]) < 10: measure = float( "{:.2f}".format(round(float(data[measure_key]), 2)) ) else: measure = float( "{:.1f}".format(round(float(data[measure_key]), 1)) ) assert measure == measure_assert else: assert data[measure_key] == measure_assert
def __ls(models=None, yamldata=None, yamlfile=None, encoding=None, model=None, output = sys.stdout, yaml_mark_subref = YAML_MARK_SUBREF, yaml_file_header = YAML_FILE_HEADER, yaml_data_header = YAML_DATA_HEADER, datastore_header = DATASTORE_HEADER, raw_data_format = RAW_DATA_FORMAT, raw_data_format_options = None, model_instance_format = MODEL_INSTANCE_FORMAT, **options): if not output: output = StringIO() if yamldata is not None or yamlfile is not None: if raw_data_format_options is None: raw_data_format_options = dict(RAW_DATA_FORMAT_OPTIONS) if options: raw_data_format_options.update(options) raw_data_pp = PrettyPrinter(stream=output, **raw_data_format_options) if models is not None: for m in models: if datastore_header: output.write(datastore_header % dict(model=m.__name__)) for i in m.all(): if model_instance_format: output.write(model_instance_format % i) if yamldata is not None: if yaml_data_header: output.write(yaml_data_header % dict(model=model and model.__name__ or '-')) for d in yaml_records(yamldata, encoding=encoding, yaml_mark_subref=yaml_mark_subref): if raw_data_format: output.write(raw_data_format % raw_data_pp.pformat(d)) if model: i = model(**d) if model_instance_format: output.write(model_instance_format % i) if yamlfile is not None: if yaml_file_header: output.write(yaml_file_header % dict(yamlfile=yamlfile, encoding=encoding, model=model and model.__name__ or '-')) for d in yaml_records(file(yamlfile).read(), encoding=encoding, yaml_mark_subref=yaml_mark_subref): if raw_data_format: output.write(raw_data_format % raw_data_pp.pformat(d)) if model: i = model(**d) if model_instance_format: output.write(model_instance_format % i) # __ls(yamldata=file(yamlfile).read(), encoding=encoding, model=model, output=output # yaml_mark_subref = yaml_mark_subref, # yaml_data_header = '', # file header was output, skip data header # raw_data_format = raw_data_format, # raw_data_format_options = raw_data_format_options, # model_instance_format = model_instance_format, # ) return getattr(output, 'getvalue', lambda: None)() # output if stringio, or None
def _readable(self): """The readable parsed article""" if self.candidates: LOG.debug('Candidates found:') pp = PrettyPrinter(indent=2) # cleanup by removing the should_drop we spotted. [n.drop_tree() for n in self._should_drop if n.getparent() is not None] # right now we return the highest scoring candidate content by_score = sorted([c for c in self.candidates.values()], key=attrgetter('content_score'), reverse=True) LOG.debug(pp.pformat(by_score)) # since we have several candidates, check the winner's siblings # for extra content winner = by_score[0] LOG.debug('Selected winning node: ' + str(winner)) updated_winner = check_siblings(winner, self.candidates) LOG.debug('Begin final prep of article') updated_winner.node = prep_article(updated_winner.node) if updated_winner.node is not None: doc = build_base_document(updated_winner.node, self.fragment) else: LOG.warning('Had candidates but failed to find a cleaned winning doc.') doc = self._handle_no_candidates() else: LOG.warning('No candidates found: using document.') LOG.debug('Begin final prep of article') doc = self._handle_no_candidates() return doc
def report_input(topology, trajectories): """Report on topology and trajectory file paths used as input.""" pp = PrettyPrinter() trajs = pp.pformat(trajectories) log.info(S('loading trajectory(ies): {}', trajs)) log.info(S('with topology: {}', topology)) return
def setup(self): default_context = self.contexts['default'] default_context.add_context_variables( authentication_enabled=True, zookeeper_enabled=is_enabled(self.config.get('zookeeper_enabled')), auth_login='******', auth_password='******', ) self.auth_creds = [ 'no_access', 'task_execute', 'admin_ops', 'admin_cache', ] for auth_cred_name in self.auth_creds: context = self.create_test_context(auth_cred_name) context.add_context_variables( authentication_enabled=True, zookeeper_enabled=is_enabled( self.config.get('zookeeper_enabled')), auth_login=auth_cred_name, auth_password=auth_cred_name, ) pp = PrettyPrinter() print_blue('Credentials: \n' + pp.pformat(self.auth_creds)) super().setup()
def _readable(self): """The readable parsed article""" if not self.candidates: logger.info("No candidates found in document.") return self._handle_no_candidates() # right now we return the highest scoring candidate content best_candidates = sorted( (c for c in self.candidates.values()), key=attrgetter("content_score"), reverse=True) printer = PrettyPrinter(indent=2) logger.debug(printer.pformat(best_candidates)) # since we have several candidates, check the winner's siblings # for extra content winner = best_candidates[0] updated_winner = check_siblings(winner, self.candidates) updated_winner.node = prep_article(updated_winner.node) if updated_winner.node is not None: dom = build_base_document( updated_winner.node, self._return_fragment) else: logger.info( 'Had candidates but failed to find a cleaned winning DOM.') dom = self._handle_no_candidates() return self._remove_orphans(dom.get_element_by_id("readabilityBody"))
class EchoClient(object): def __init__(self): self._root = None self._pp = PrettyPrinter() def call(self, *args): dfr = self._root.callRemote(*args) dfr.addBoth(self.print_response, args) return dfr def root_received(self, root): self._root = root #d = result.callRemote("get_data_point", "connections_count") self.call("set_config", "PUSH_SHARES__MINING_SLOTS", True) self.call("set_config", "PUSH_SHARES__MINING_SHARES", False) #d = result.callRemote('explore', 'for_sessions', ('!', 'sess_stats')) #d = result.callRemote('explore', 'for_sessions', # ('limit', 2), ('!', 'sess_convert')) #d = result.callRemote('reconnect', -1, 50) #d = result.callRemote('cancel_reconnect') #d = result.callRemote('get_reconnect_state') reactor.callLater(2, reactor.stop) def print_response(self, response, args): print "%s\n%s" % (args, self._pp.pformat(response))
class EchoClient(object): def __init__(self): self._root = None self._pp = PrettyPrinter() def call(self, *args): dfr = self._root.callRemote(*args) dfr.addBoth(self.print_response, args) return dfr def root_received(self, root): self._root = root #d = result.callRemote("get_data_point", "connections_count") self.call("set_config", "PUSH_SHARES__MINING_SLOTS", True) self.call("set_config", "PUSH_SHARES__MINING_SHARES", False) #d = result.callRemote('explore', 'for_sessions', ('!', 'sess_stats')) #d = result.callRemote('explore', 'for_sessions', # ('limit', 2), ('!', 'sess_convert')) #d = result.callRemote('reconnect', -1, 50) #d = result.callRemote('cancel_reconnect') #d = result.callRemote('get_reconnect_state') reactor.callLater(2, reactor.stop) def print_response(self, response, args): print "%s\n%s" % (args, self._pp.pformat(response))
def _readable(self): """The readable parsed article""" if not self.candidates: logger.info("No candidates found in document.") return self._handle_no_candidates() # right now we return the highest scoring candidate content best_candidates = sorted((c for c in self.candidates.values()), key=attrgetter("content_score"), reverse=True) printer = PrettyPrinter(indent=2) logger.debug(printer.pformat(best_candidates)) # since we have several candidates, check the winner's siblings # for extra content winner = best_candidates[0] updated_winner = check_siblings(winner, self.candidates) updated_winner.node = prep_article(updated_winner.node) if updated_winner.node is not None: dom = build_base_document(updated_winner.node, self._return_fragment) else: logger.info( 'Had candidates but failed to find a cleaned winning DOM.') dom = self._handle_no_candidates() return self._remove_orphans(dom.get_element_by_id("readabilityBody"))
def get_nice_schema(collection): """stupid proxy for stdlib pprint""" schema = get_schema(collection) from pprint import PrettyPrinter pp = PrettyPrinter(indent=2, width=16) nice = pp.pformat(schema) return nice
def _readable(self): """The readable parsed article""" doc = self.orig.html # cleaning doesn't return, just wipes in place html_cleaner(doc) doc = drop_tag(doc, "noscript", "iframe") doc = transform_misused_divs_into_paragraphs(doc) candidates, should_drop = find_candidates(doc) if candidates: LOG.debug("Candidates found:") pp = PrettyPrinter(indent=2) # right now we return the highest scoring candidate content by_score = sorted([c for c in candidates.values()], key=attrgetter("content_score"), reverse=True) LOG.debug(pp.pformat(by_score)) # since we have several candidates, check the winner's siblings # for extra content winner = by_score[0] LOG.debug("Selected winning node: " + str(winner)) updated_winner = check_siblings(winner, candidates) LOG.debug("Begin final prep of article") updated_winner.node = prep_article(updated_winner.node) doc = build_base_document(updated_winner.node, self.fragment) else: LOG.warning("No candidates found: using document.") LOG.debug("Begin final prep of article") # since we've not found a good candidate we're should help this # cleanup by removing the should_drop we spotted. [n.drop_tree() for n in should_drop] doc = prep_article(doc) doc = build_base_document(doc, self.fragment) return doc
def order_stored_shipment(self): """Create a stored shipment.""" _logger.warning("MyTag: Got to: order_stored_shipment in improved") if self.carrier_tracking_ref: raise Warning(_('Transport already ordered (there is a Tracking ref)')) if self.shipmentid: raise Warning(_('The stored shipment has already been confirmed (there is a Shipment id).')) if self.stored_shipmentid: self.delete_stored_shipment() rec = self.build_stored_shipment_dict() if self.param_ids: self.param_ids.add_to_record(rec) response = self.carrier_id.unifaun_send('stored-shipments', None, rec) printer = PrettyPrinter() if type(response) == dict: _logger.warn('\n%s\n' % response) self.stored_shipmentid = response.get('id', '') self.env['mail.message'].create({ 'body': _(u"Unifaun<br/>rec %s<br/>resp %s<br/>" % (printer.pformat(rec), printer.pformat(response))), # ~ 'body': _(u"Unifaun<br/>rec %s<br/>resp %s<br/>" % (rec, response)), 'subject': "Order Transport", 'author_id': self.env['res.users'].browse(self.env.uid).partner_id.id, 'res_id': self.id, 'model': self._name, 'type': 'notification', }) self.set_unifaun_status(response.get('statuses') or []) if response.get('status') == 'READY': self.state = 'sent' # ~ elif response.get('status') == 'INVALID': else: self.state = 'error' else: self.env['mail.message'].create({ # ~ 'body': _("Unifaun error!<br/>rec %s<br/>resp %s<br/>" % (rec, response)), 'body': _("Unifaun error!<br/>rec %s<br/>resp %s<br/>" % (printer.pformat(rec), printer.pformat(response))), 'subject': "Order Transport", 'author_id': self.env['res.users'].browse(self.env.uid).partner_id.id, 'res_id': self.id, 'model': self._name, 'type': 'notification', }) _logger.info('Unifaun Order Transport: rec %s response %s' % (rec, response))
def writeDataToFile(filename, data, pretty=False): with open(filename, 'w') as fp: if pretty: from pprint import PrettyPrinter pp = PrettyPrinter(indent=4) fp.write(pp.pformat(data)) else: json.dump(data, fp)
def adjacencies(self, pretty=False): """ We return the adjacency dictionary. """ if pretty: pp = PrettyPrinter() return pp.pformat(self._adj) elif not pretty: return self._adj
def request_view(request): """ display the request metadata :param request: :return: """ pp = PrettyPrinter() s = '<pre>request.META =\n' + pp.pformat(request.META) + '</pre>' return HttpResponse(s)
def pformat(self, object): fs = PrettyPrinter.pformat(self, object) rs = _StringIO() for line in fs.split('\n'): if len(line) != 0: rs.write(self.__per_line_indent_str) rs.write(line) rs.write('\n') return rs.getvalue()
def generate_python(d, destfile, src, license): pp = PrettyPrinter(indent=2, width=120) varname = destfile[:-3].split('/')[-1].upper() with open(destfile, 'w', encoding='utf-8') as fd: fd.write('# -*- coding: UTF-8 -*-\n') fd.write('# source: %s\n' % ',\n# '.join(src)) fd.write('# license: %s\n\n' % license) fd.write('%s = \\\n%s\n' % (varname, pp.pformat(d))) print('[+] %s file generated' % destfile)
def pformat(self, object): fs = PrettyPrinter.pformat(self, object) rs = _StringIO() for line in fs.split('\n'): if len(line) != 0: rs.write(self.__per_line_indent_str) rs.write(line) rs.write('\n') return rs.getvalue()
def simplices(self, pretty=False): """ Return the simplices. """ if pretty: pp = PrettyPrinter() return pp.pformat(self._simplices) elif not pretty: return self._simplices return self._simplices
def get_config(self, args): parser = argparse.ArgumentParser() parser.add_argument('-p', '--pretty', action='store_true', help='Pretty print') args = parser.parse_args(args) config = self.alias_service.get_config() if args.pretty: printer = PrettyPrinter() logger.info(printer.pformat(config)) else: logger.info(json.dumps(config))
def __str__(self): pretty_print = PrettyPrinter(indent=2) props = dict(name=self.name, display_name=self.display_name, description=self.description, parameters=pretty_print.pformat(self.parameters)) return Template(''' name: ${name} display_name: ${display_name} description: ${description} parameters: ${parameters} ''').substitute(props)
def write_new_doc(tb_name, fct_name): if mv_old_doc(tb_name, fct_name): nfp = Nt2_fct_props(tb_name, fct_name) od = nfp.get_fct_dict_list() p = nfp.get_fct_doc_path() nd = construct_new_dict_0_1(od) pp = PrettyPrinter() s = pp.pformat(nd) # print s # write(p,s,False) else: print "doc is already updatted"
def prettyFormat(value): """ Pretty format a value. @param value: value to pretty format @type value: python object @return: the value pretty formated @rtype: str """ pp = PrettyPrinter() prettyStr = pp.pformat(value) return prettyStr
def prettyFormat(value): """ Pretty format a value. @param value: value to pretty format @type value: python object @return: the value pretty formated @rtype: str """ pp = PrettyPrinter() prettyStr = pp.pformat(value) return prettyStr
def format_repr(self, nested=False): if self.should_repr_pretty: from pprint import PrettyPrinter pprinter = PrettyPrinter(compact=True) _repr = pprinter.pformat(dict(self)) else: _repr = '{0!r}'.format(dict(self)) if nested: return 'DSLDict({0})'.format(_repr) else: return '<DSLDict {0}>'.format(_repr)
def index(request): context = {} if request.method == 'POST': form = forms.BasicPurchaseForm(request.POST) if form.is_valid(): cart = HiiCart.objects.create() if form.cleaned_data['fancypants']: LineItem.objects.create(cart=cart, quantity=1, **forms.PANTS) if form.cleaned_data['monkeybutler']: LineItem.objects.create(cart=cart, quantity=1, **forms.MONKEYBUTLER) if form.cleaned_data['subscription']: RecurringLineItem.objects.create(cart=cart, quantity=1, **forms.SUBSCRIPTION) result = cart.submit(form.cleaned_data['gateway']) if result.type == 'url': return HttpResponseRedirect(result.url) else: return HttpResponseRedirect('/paypal_redirect') else: form = forms.BasicPurchaseForm() p = PrettyPrinter(width=40) context['paypal'] = (p.pformat(settings.HIICART_SETTINGS['PAYPAL']), validate_gateway('paypal')) context['paypal2'] = (p.pformat(settings.HIICART_SETTINGS['PAYPAL2']), validate_gateway('paypal2')) context['paypal_adaptive'] = (p.pformat( settings.HIICART_SETTINGS['PAYPAL_ADAPTIVE']), validate_gateway('paypal_adaptive')) context['google'] = (p.pformat(settings.HIICART_SETTINGS['GOOGLE']), validate_gateway('google')) context['amazon'] = (p.pformat(settings.HIICART_SETTINGS['AMAZON']), validate_gateway('amazon')) context['form'] = form return render_to_response('index.html', context)
def application(request): printer = PrettyPrinter(indent=2) path = request.headers['PATH_INFO'] headers = { 'Content-Type': 'text/plain' } if path == '/': message = 'Hello World!' else: message = 'Goodbye World!' message += '\n\n' + printer.pformat(request.headers) return Response(Status.Ok, headers, message)
def cprint(to_print: str, color=None, on_color=None, have_to_pprint=False) -> None: '''This is the some to termcolor.cprint(), but prints the string line by Line''' if have_to_pprint: printer = PrettyPrinter(stream=None, indent=1, width=80, depth=None, compact=False, sort_dicts=True) to_log = printer.pformat(to_log) for line in to_print.split('\n'): col_print(line, color, on_color)
def print_transaction(txns): """ print the contents of a tx message :param txns: the unmarshalled tx message """ print(' TX') print(' --------------------------------------------------------') if len(txns) > _MAX_TXN_COUNT: txns_hex = txns_to_hex(txns[:_MAX_TXN_COUNT]) else: txns_hex = txns_hex(txns) pp = PrettyPrinter() for i in pp.pformat(txns_hex).splitlines(): print(' ' + i[:100] + ('' if len(i) < 100 else '...')) if len(txns) > _MAX_TXN_COUNT: print('\n Hiding {} of {} transactions'.format(len(txns)-_MAX_TXN_COUNT, \ len(txns)))
def setup(self): self.auth_creds = { '': { 'name': 'server', 'context': 'default', }, 'read_only': { 'name': 'read_only', 'context': 'read_only', }, 'no_access': { 'name': 'no_access', 'context': 'no_access', }, } pp = PrettyPrinter() for auth_cred_name in self.auth_creds: # aka 'server_login' / 'server_password', etc. default_login = self.auth_creds[auth_cred_name]['name'] + '_user' default_password = self.auth_creds[auth_cred_name][ 'name'] + '_password' self.auth_creds[auth_cred_name].update({ 'login': self.config.get('auth_' + auth_cred_name + 'login', default_login), 'password': self.config.get('auth_' + auth_cred_name + 'password', default_password), }) context_name = self.auth_creds[auth_cred_name]['context'] if context_name != 'default': context = self.create_test_context(context_name) else: context = self.contexts['default'] context.add_context_variables( authentication_enabled=True, zookeeper_enabled=is_enabled( self.config.get('zookeeper_enabled')), auth_login=self.auth_creds[auth_cred_name]['login'], auth_password=self.auth_creds[auth_cred_name]['password'], ) print_blue('Credentials: \n' + pp.pformat(self.auth_creds)) super().setup()
class process_log(object): __slots__ = ['__logger', '__pretty'] def __init__(self, name): # set up nice printer self.__pretty = PrettyPrinter() #set up log file name log_filename = "/var/log/greenscreen/%s"% name # create logger self.__logger = logging.getLogger("Greenscreen") self.__logger.setLevel(logging.DEBUG) # self.__logger.addLevelName(logging.EMAIL, "email") # Add the log message handler to the logger file_handler = logging.handlers.RotatingFileHandler(log_filename, maxBytes=20971520, backupCount=5) formatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s","%Y-%m-%d %H:%M:%S") file_handler.setFormatter(formatter) file_handler.setLevel(logging.DEBUG) # file_handler.addLevelName(logging.EMAIL, "email") self.__logger.addHandler(file_handler) mailhost = "mx.tribalbrands.mobi" fromaddr = "GS Processing <*****@*****.**>" toaddrs = ["*****@*****.**"] subject = "GS Error on %s"% gethostname().split('.')[0] credentials = None smtp_handler = logging.handlers.SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials) smtp_handler.setFormatter(formatter) # smtp_handler.addLevelName(logging.EMAIL, "email") smtp_handler.setLevel(logging.ERROR) self.__logger.addHandler(smtp_handler) self.__logger.debug("="*80) self.__logger.debug(" Starting run ".center(80, "=")) self.__logger.debug("="*80) def email(self, message, pretty=False): if pretty == True: message = self.__pretty.pformat(message) self.__logger.log(logging.EMAIL, message) def debug(self, message, pretty=False): if pretty == True: message = self.__pretty.pformat(message) self.__logger.debug(message) def info(self, message, pretty=False): if pretty == True: message = self.__pretty.pformat(message) self.__logger.info(message) def warn(self, message, pretty=False): if pretty == True: message = self.__pretty.pformat(message) self.__logger.warn(message) def error(self, message, pretty=False): if pretty == True: message = self.__pretty.pformat(message) self.__logger.error(message) def critical(self, message, pretty=False): if pretty == True: message = self.__pretty.pformat(message) self.__logger.critical(message)
def _cmd_loop(self): _fmt = PrettyPrinter() _kbint = False cn = imap.cn while cn.state != 'logout': try: # Read command _line = input(sys.ps1).rstrip() _ret = None _kbint = False if not _line: continue if _line in ('quit', 'exit'): break # Execute command if _line.startswith('UID '): _parts = _line.split(None, 2)[1:] _parts[0] = 'UID ' + _parts[0] else: _parts = _line.split(None, 1) if _parts[0] in _imap_cmds: cn(*_parts).wait() else: _co = self._compile(_line) while _co is None: _line += '\n' + input(sys.ps2) _co = self._compile(_line) _ret = eval(_co) # Print results if isinstance(_ret, (dict, list, tuple, set, frozenset)): print(_fmt.pformat(_ret)) elif _ret is not None: print(_ret) except KeyboardInterrupt: print('^C') if _kbint: break _kbint = True except Exception: log.exception('General error')
def test_devel(self): pp = PrettyPrinter(indent=2) app = self._get_test_app() #1 create a package logger.debug( '*************************************************************') #url = url_for(controller='package', action='new') env, response = _get_package_new_page_as_sysadmin(app) form = response.forms['dataset-edit'] assert_true('humps' in form.fields, msg='!!!!!!!!!!! humps not in form !!!!!!!!!!!') with open("/home/vagrant/response.html", "w") as f: f.write(resp._body) logger.debug('*************************** dataset-edit fields:') logger.debug(pp.pformat(form.__dict__)) form['title'] = 'DS1' form['name'] = 'ds1' form['notes'] = 'test' form['date_created'] = '01/01/2001' form['date_modified'] = '01/01/2001' resp = helpers.submit_and_follow(self.app, form, env, 'save') #pkg = model.Package.by_name('ds1') #logger.debug(pp.pformat(pkg)) #2 clone # This doesn't work - form expects opendata package #resp = app.get(url='/dataset/ds1', extra_environ=env) #resp = app.get(url=url_for('clone', id='ds1'), extra_environ=env) #assert_true('id-clone-ds-form' in resp, msg='Response did not contain clone form') logger.debug('********************* RESPONSE:') logger.debug(str(resp)) logger.debug( '*************************************************************') assert_true( False, msg= '***************** THIS ACTUALLY COMPLETED :O ******************')
def gen_tables(): import re from pprint import PrettyPrinter pp = PrettyPrinter(indent=4, width=76) def indent(s): return re.sub('^', ' '*4, s, flags=re.M) output = { 'knight_table': gen_knight_table(), 'bishop_table': gen_bishop_table(), 'rook_table': gen_rook_table(), 'king_table': gen_king_table(), } return '\n'.join('%s = \\\n%s\n' % (k, indent(pp.pformat(v))) for k, v in output.iteritems()).rstrip()
def _setattr(obj, attr, value): try: obj = deepcopy(obj) obj.__setattr__(attr, value) except TypeError as err: tb = sys.exc_info()[2] from pprint import PrettyPrinter pp = PrettyPrinter() obj_repr = "<" + obj.__class__.__name__ + ">: " \ + pp.pformat(obj.__dict__) msg = "In `_setattr` we deepcopy the object *during runtime*. " \ "If you're sure that what you're doing is safe, you can " \ " overload `__deepcopy__` to get more efficient code. " \ "However, something went " \ "wrong here: \n" + err.args[0] + '\n' + obj_repr raise TypeError(msg).with_traceback(tb) return obj
def console_log(to_log: str, color=None, on_color=None, have_to_pprint=False) -> None: '''Same as data.custom.functions.cprint(), but puts a timestamp before printing the line, and also puts the line into logs.txt''' logs = open("console.log", "a") if have_to_pprint: printer = PrettyPrinter(stream=None, indent=1, width=80, depth=None, compact=False, sort_dicts=True) to_log = printer.pformat(to_log) for line in to_log.split('\n'): to_print = f'[{time.strftime("%a, %d %b %Y %I:%M:%S %p %Z", time.gmtime())}] {line}' col_print(to_print, color, on_color) logs.write(to_print + '\n') logs.close()
def _setattr(obj, attr, value): try: obj = deepcopy(obj) obj.__setattr__(attr, value) except TypeError as err: tb = sys.exc_info()[2] from pprint import PrettyPrinter pp = PrettyPrinter() obj_repr = "<" + obj.__class__.__name__ + ">: " \ + pp.pformat(obj.__dict__) msg = "In `_setattr` we deepcopy the object *during runtime*. " \ "If you're sure that what you're doing is safe, you can " \ " overload `__deepcopy__` to get more efficient code. " \ "However, something went " \ "wrong here: \n" + err.args[0] + '\n' + obj_repr raise TypeError(msg).with_traceback(tb) return obj
def __exit__(self, *exc): if exc[0]: return pp = PrettyPrinter(indent = 4) code = "\n".join( ("%s = %s" % (a, pp.pformat(getattr(self, a)))) for a in [ "saves", "hidden", "logging", ] ) try: with open(self.path + ".tmp", "w") as f: f.write(code) except: print_exc() else: move(self.path + ".tmp", self.path)
def generate_sample(op_name, op_spec, data_params_are_present, model_name, full_spec, jinja_env): template_name = "snippet_bravado.j2" operation_arguments = { k: '"{}"'.format(v['type']) for k, v in op_spec.get("parameters", {}).get('path', {}).items() } if data_params_are_present: body = body_generator.generate_model_sample(model_name, full_spec) body = utils.filter_data_params(op_name, op_spec[OperationField.METHOD], body) printer = PrettyPrinter(width=1) operation_arguments["body"] = printer.pformat(body) template = jinja_env.get_template(template_name) return template.render(var=op_spec['tags'][0].lower(), tag=op_spec['tags'][0], operation=op_name, operation_arguments=operation_arguments)
def learn_feed(self, url, user_agent=None, data=None): """Get Feed from page content in data field without using cached data""" self.init_session() printer = PrettyPrinter() self.log.save('learn_rss_data', 'Download url') if data is None: data = self.fetch(url, user_agent) edata = decode_html(data) self.log.save('learn_rss_data', 'Decode data') try: document = fromstring(edata) except: document = None self.log.save('learn_rss_data', 'Parsed data') feed = self.initfeed(document, url) clusters = self.getclusters(document, url) self.log.save('learn_rss_data', 'Clusters %s' % (printer.pformat(clusters))) feed = self.process_clusters(url, clusters, feed) self.log.save('learn_rss_data', 'End of log') session = self.clear_session() return feed, session
class Tester: def __init__(self, func, **kwargs): self.func = func self.kwargs = kwargs self.pprinter = PrettyPrinter(**pprint_option) def eval(self, X): return self.func(X, **self.kwargs) def set_params(self, **params): for name in params: if name in self.kwargs: self.kwargs[name] = params[name] def __repr__(self): return "Tester with attributes: " + self.pprinter.pformat( { "func": self.func, "kwargs": self.kwargs })
def test_devel(self): pp = PrettyPrinter(indent=2) app = self._get_test_app() #1 create a package logger.debug('*************************************************************') #url = url_for(controller='package', action='new') env, response = _get_package_new_page_as_sysadmin(app) form = response.forms['dataset-edit'] assert_true('humps' in form.fields, msg='!!!!!!!!!!! humps not in form !!!!!!!!!!!') with open("/home/vagrant/response.html", "w") as f: f.write(resp._body) logger.debug('*************************** dataset-edit fields:') logger.debug(pp.pformat(form.__dict__)) form['title'] = 'DS1' form['name'] = 'ds1' form['notes'] = 'test' form['date_created'] = '01/01/2001' form['date_modified'] = '01/01/2001' resp = helpers.submit_and_follow(self.app, form, env, 'save') #pkg = model.Package.by_name('ds1') #logger.debug(pp.pformat(pkg)) #2 clone # This doesn't work - form expects opendata package #resp = app.get(url='/dataset/ds1', extra_environ=env) #resp = app.get(url=url_for('clone', id='ds1'), extra_environ=env) #assert_true('id-clone-ds-form' in resp, msg='Response did not contain clone form') logger.debug('********************* RESPONSE:') logger.debug(str(resp)) logger.debug('*************************************************************') assert_true(False, msg='***************** THIS ACTUALLY COMPLETED :O ******************')
def load(model, yamldata, cleanup=False, encoding=None, compressed=False, output = sys.stdout, yaml_mark_subref = YAML_MARK_SUBREF, yaml_data_header = YAML_DATA_HEADER, raw_data_format = RAW_DATA_FORMAT, raw_data_format_options = None, model_instance_format = MODEL_INSTANCE_FORMAT, **options): """ Loads model data from yaml data source into DataStore * datastore access works server-side only * encoding defaults to UTF-8 """ if not output: output = StringIO() if raw_data_format_options is None: raw_data_format_options = dict(RAW_DATA_FORMAT_OPTIONS) if options: raw_data_format_options.update(options) raw_data_pp = PrettyPrinter(stream=output, **raw_data_format_options) if yaml_data_header: output.write(yaml_data_header % dict(model=model.__name__, cleanup=cleanup)) if cleanup: rm(model, remove_message='') if compressed: yamldata = bz2.decompress(base64.decodestring(yamldata)) for d in yaml_records(yamldata, encoding=encoding, yaml_mark_subref=yaml_mark_subref): if raw_data_format: output.write(raw_data_format % raw_data_pp.pformat(d)) i = model(**d) if model_instance_format: output.write(model_instance_format % i) i.put() return getattr(output, 'getvalue', lambda: None)() # output if stringio, or None
api = DynectRest() # We need to login print "Logging in to API" args = { 'customer_name': 'ciscocloud', 'user_name': 'UserName', 'password': '******', } response = api.execute('/REST/Session/', 'POST', args) if response['status'] != 'success': pretty = PrettyPrinter() msg = "Login to API failed: %s" % pretty.pformat(response) sys.exit(msg) zone_srv_name = 'gslb.com' zones = { 'zone' : 'gslb.com', } print "Creating the zone gslb.com with rr www.gslb.com" url = "/REST/Zone/%s" % zone_srv_name response = api.execute(url,'PUT',zones); if response['status'] != 'success':
def __str__(self): pp = PrettyPrinter(indent=0) return pp.pformat(self)
# Log in print "Logging in to API" arguments = { 'customer_name': 'ciscocloud', 'user_name': 'UserName', 'password': '******', } # ciscocloud UserName Cisco!gs1b response = rest_iface.execute('/Session/', 'POST', arguments) if response['status'] != 'success': sys.exit("Incorrect credentials") # Perform action print "Get GSLB services as authentication succeded" response = rest_iface.execute('/REST/GSLB/gslb.com/www.gslb.com', 'GET') if response['status'] != 'sucess': pretty = PrettyPrinter() msg = "Getting gslb service failed: %s " % pretty.pformat(response) sys.exit(msg) zone_resources = response['data'] print "Getting gslb succeded" print zone_resources # Log out, to be polite print "Deleting session" rest_iface.execute('/Session/', 'DELETE')
print "Logging in to API" arguments = { 'customer_name': 'ciscocloud', 'user_name': 'UserName', 'password': '******', } # ciscocloud UserName Cisco!gs1b response = rest_iface.execute('/Session/', 'POST', arguments) if response['status'] != 'success': sys.exit("Incorrect credentials") # Perform action print "Get AF services as authentication succeded" response = rest_iface.execute('/REST/Failover/gslb.com/www.gslb.com', 'GET') if response['status'] != 'success': pretty = PrettyPrinter() msg = "Getting AF service failed: %s " % pretty.pformat(response) sys.exit(msg) zone_resources = response['data'] print "Getting AF succeded" pretty = PrettyPrinter() print pretty.pformat(zone_resources) # Log out, to be polite print "Deleting session" rest_iface.execute('/Session/', 'DELETE')
# Log in print "Logging in to API" arguments = { 'customer_name': 'ciscocloud', 'user_name': 'UserName', 'password': '******', } # ciscocloud UserName Cisco!gs1b response = rest_iface.execute('/Session/', 'POST', arguments) if response['status'] != 'success': sys.exit("Incorrect credentials") # Perform action print "Get password as authentication succeded" username = { 'user_name' : 'mahepate',} response = rest_iface.execute('/REST/UpdateUserPassword/mahepate', 'GET', username) if response['status'] != 'sucess': pretty = PrettyPrinter() msg = "Getting Password failed: %s " % pretty.pformat(response) sys.exit(msg) zone_resources = response['data'] print "Getting password succeded" # Log out, to be polite print "Deleting session" rest_iface.execute('/Session/', 'DELETE')
from pprint import PrettyPrinter rest_iface = DynectRest() # Log in print "Logging in to API" arguments = {"customer_name": "ciscocloud", "user_name": "UserName", "password": "******"} # ciscocloud UserName Cisco!gs1b response = rest_iface.execute("/Session/", "POST", arguments) if response["status"] != "success": sys.exit("Incorrect credentials") # Perform action print "Get User as authentication succeded" response = rest_iface.execute("/REST/User/", "GET") if response["status"] != "success": pretty = PrettyPrinter() msg = "Getting User failed: %s " % pretty.pformat(response) sys.exit(msg) zone_resources = response["data"] print "Getting User succeded" print zone_resources # Log out, to be polite print "Deleting session" rest_iface.execute("/Session/", "DELETE")
api = DynectRest() # We need to login print "Logging in to API" args = { 'customer_name': 'ciscocloud', 'user_name': 'UserName', 'password': '******', } response = api.execute('/REST/Session/', 'POST', args) if response['status'] != 'success': pretty = PrettyPrinter() msg = "Login to API failed: %s" % pretty.pformat(response) sys.exit(msg) traffic_srv_id = '54eHrIuDkcKpg9DdnmrThgpGCsI' response_pool= { 'label':'AsiaDC', 'publish' : 'Y' # Mahendra - always publish otherwise it will not be seen on the UI, as changes will get discarded. } """ response_pool= { 'serviceid' : traffic_srv_id, 'label' : 'Response Pool Created From API', 'core_set_count' : '1', 'index' : '2', 'rs_chains' : [{
# Log in print "Logging in to API" arguments = { 'customer_name': 'ciscocloud', 'user_name': 'UserName', 'password': '******', } # ciscocloud UserName Cisco!gs1b response = rest_iface.execute('/Session/', 'POST', arguments) if response['status'] != 'success': sys.exit("Incorrect credentials") # Perform action print "Get Zone as authentication succeded" response = rest_iface.execute('/Zone/', 'GET') if response['status'] != 'sucess': pretty = PrettyPrinter() msg = "Getting zone failed: %s " % pretty.pformat(response) sys.exit(msg) zone_resources = response['data'] print "Getting zone succeded" # Log out, to be polite print "Deleting session" rest_iface.execute('/Session/', 'DELETE')
def store_db(db, filename): pp = PrettyPrinter(indent=2) f = codecs.open(filename, 'w', 'utf-8') f.write(pp.pformat(db)) f.close()
import sys api = DynectRest() # We need to login print "Logging in to API" args = { 'customer_name': 'ciscocloud', 'user_name': 'UserName', 'password': '******', } response = api.execute('/REST/Session/', 'POST', args) if response['status'] != 'success': pretty = PrettyPrinter() msg = "Login to API failed: %s" % pretty.pformat(response) sys.exit(msg) print "Logging in success" print "geo service name= gslb geo" svc_name = 'gslb geo' print "Deleting the Geo Service" url = "/REST/Geo/%s/" % svc_name response = api.execute(url, 'DELETE') print "Logging out" api.execute('/REST/Session/', 'DELETE')
response = rest_iface.execute('/Session/', 'POST', arguments) if response['status'] != 'success': sys.exit("Incorrect credentials") # Perform action print "Get traffic director service as authentication succeded" services = { 'label' : 'Test*', 'detail' : 'true', } response = rest_iface.execute('/REST/DSF/', 'GET', services) if response['status'] != 'success': pretty = PrettyPrinter() msg = "Getting traffic director service : %s " % pretty.pformat(response) sys.exit(msg) print "Rest call response" pretty = PrettyPrinter() msg = "Response : %s " % pretty.pformat(response['data']) print msg # Log out, to be polite print "Deleting session" rest_iface.execute('/Session/', 'DELETE')
api = DynectRest() # We need to login print "Logging in to API" args = { 'customer_name': 'ciscocloud', 'user_name': 'UserName', 'password': '******', } response = api.execute('/REST/Session/', 'POST', args) if response['status'] != 'success': pretty = PrettyPrinter() msg = "Login to API failed: %s" % pretty.pformat(response) sys.exit(msg) job_id = '1953472563' jobs = { 'job_id' : '1953472563',} url = "/REST/Job/%s" % job_id response = api.execute(url,'GET',jobs); if response['status'] != 'sucess': pretty = PrettyPrinter() msg = "Getting the Jobs failed: %s" % pretty.pformat(response) sys.exit(msg)
from dynect.DynectDNS import DynectRest from pprint import PrettyPrinter import sys api = DynectRest() # We need to login print "Logging in to API" args = {"customer_name": "ciscocloud", "user_name": "UserName", "password": "******"} response = api.execute("/REST/Session/", "POST", args) if response["status"] != "success": pretty = PrettyPrinter() msg = "Login to API failed: %s" % pretty.pformat(response) sys.exit(msg) user_name = "testuser" print "Deleting User" url = "/REST/User/%s" % user_name response = api.execute(url, "DELETE") if response["status"] != "success": pretty = PrettyPrinter() msg = "Deleting the User failed: %s" % pretty.pformat(response) sys.exit(msg)