async def listcogs(self, ctx): """ Lists all currently loaded cogs. """ content = StringBuilder("```\n") extensions = defaultdict(list) for cog_name, cog in self.bot.cogs.items(): ext_name = cog.__module__.rsplit(".", 1)[0] ext_name = ext_name.rsplit(".", 1)[1] extensions[ext_name].append(cog_name) # I hate this but tree-format uses lists and tuples for some reason # So this takes the nice dictionary and converts it to that extensions = [(ext, [(cog_name, []) for cog_name in cog]) for ext, cog in extensions.items()] tree = ("futaba", [("cogs", extensions)]) content.writeln( format_tree(tree, format_node=itemgetter(0), get_children=itemgetter(1))) content.writeln("```") await ctx.send(content=str(content))
def list_assignments(course: Course, assignments_provider, long=False, submissions=False, upcoming=False): assignments = assignments_provider(course.id) if upcoming: assignments = filter_future_assignments(assignments) if long: if submissions: assignment_ids = map(lambda assignment: assignment.id, assignments) assignment_submissions = course.get_multiple_submissions(assignment_ids=assignment_ids) submissions_by_assignment = defaultdict(list) tabulated_submissions = tabulate_dict(submission_info_items, assignment_submissions) for submission, formatted in tabulated_submissions.items(): submissions_by_assignment[submission.assignment_id].append((formatted, [])) tabulated_assignments = tabulate_dict(assignment_info_items, assignments) tree = (unique_course_code(course), [(formatted, submissions_by_assignment[assignment.id]) for assignment, formatted in tabulated_assignments.items()]) get_outputter().poutput(format_tree(tree, format_node=itemgetter(0), get_children=itemgetter(1))) else: get_outputter().poutput(tabulate(map(assignment_info_items, assignments), tablefmt='plain')) else: get_outputter().poutput('\n'.join([assignment.name for assignment in assignments]))
def remove_circles_and_print(pkgs: Iterable[ResolvedPkg], nixpkgs: NixpkgsIndex): import networkx as nx print("\n### Resolved Dependencies ###\n") indexed_pkgs = {p.name: p for p in sorted(pkgs, key=lambda p: p.name)} roots: Iterable[ResolvedPkg] = sorted([p for p in pkgs if p.is_root], key=lambda p: p.name) edges = set() for p in pkgs: for child in p.build_inputs + p.prop_build_inputs: edges.add((p.name, child)) G = nx.DiGraph(sorted(list(edges))) cycle_count = 0 removed_edges = [] for root in roots: try: while True: cycle = nx.find_cycle(G, root.name) cycle_count += 1 remove_dependecy(indexed_pkgs, G, cycle[-1][0], cycle[-1][1]) removed_edges.append((cycle[-1][0], cycle[-1][1])) except NetworkXNoCycle: continue for node, removed_node in removed_edges: mark_removed_circular_dep(indexed_pkgs, G, node, removed_node) class Limiter: visited = set() def name(self, node_name): if node_name in self.visited: if indexed_pkgs[node_name].build_inputs + indexed_pkgs[ node_name].prop_build_inputs == []: return make_name(indexed_pkgs[node_name], nixpkgs) return f"{make_name(indexed_pkgs[node_name], nixpkgs)} -> ..." return make_name(indexed_pkgs[node_name], nixpkgs) def get_children(self, node_name): if node_name in self.visited: return [] self.visited.add(node_name) return list( set(indexed_pkgs[node_name].build_inputs + indexed_pkgs[node_name].prop_build_inputs)) for root in roots: limiter = Limiter() print( format_tree(root.name, format_node=limiter.name, get_children=limiter.get_children)) print(f"Total number of python modules: {len(indexed_pkgs)}") print(f"Removed circular dependencies: {cycle_count}\n")
def __str__(self): def name(x) -> str: if isinstance(x, Token): return ' "' + x.content + '"' else: return '' return format_tree( self, format_node=lambda x: f'{type(x).__name__}{name(x)}', get_children=lambda x: x.children)
def run(self, parsed_args): if not parsed_args.path: objs = [self.context.group] else: objs = [self.context.group[str(p)] for p in parsed_args.path] out = [ format_tree(obj, format_node=format_obj, get_children=get_children) for obj in objs ] self.context.print(*out, sep="\n\n") return Signal.SUCCESS
def print_matrix(self): msg = 'Test matrix' LOG.info(msg) tree = tuple(('', [(scenario.name, [(action, []) for action in scenario.sequence]) for scenario in self.all])) tf = tree_format.format_tree(tree, format_node=operator.itemgetter(0), get_children=operator.itemgetter(1)) LOG.out(tf) LOG.out('')
def topology(args: Namespace) -> None: project_data = utils.project.get_project_data(args.project_dir) if project_data is None: raise RuntimeError(f"No project found in '{project_data.directory}'") with open(file=project_data.cluster, mode='r') as f: cls: structures.cluster.Cluster = structures.cluster.Cluster.from_yaml(data=f) head_extra_info = None workers_extra_info = [None] * len(cls.provider.worker_ips) if args.full: head_extra_info = get_extra_node_info(ip=cls.provider.head_ip, username=cls.auth.ssh_user, ssh_key=cls.auth.ssh_private_key) workers_extra_info = [] for ip in cls.provider.worker_ips: extra_info = get_extra_node_info(ip=ip, username=cls.auth.ssh_user, ssh_key=cls.auth.ssh_private_key) workers_extra_info.append(extra_info) root = TreeNode(name=project_data.name) head = TreeNode(name="Head") workers = TreeNode(name="Workers") root.children.append(head) root.children.append(workers) # Add the head description head_node = TreeNode(name=node_description(ip=cls.provider.head_ip, username=cls.auth.ssh_user, extra_info=head_extra_info)) head.children.append(head_node) # Add the workers descriptions for ip, extra_info in zip(cls.provider.worker_ips, workers_extra_info): worker_node = TreeNode(name=node_description(ip=ip, username=cls.auth.ssh_user, extra_info=extra_info)) workers.children.append(worker_node) print(format_tree( node=astuple(root), format_node=itemgetter(0), get_children=itemgetter(1)))
def print_matrix(self): msg = 'Test matrix' LOG.info(msg) tree = tuple(('', [(scenario.name, [(term, []) for term in scenario.sequence]) for scenario in self.all])) tf = tree_format.format_tree( tree, format_node=operator.itemgetter(0), get_children=operator.itemgetter(1)) LOG.out(tf.encode('utf-8'))
def render_task_nodes_unicode(write, nodes, field_limit, ignored_task_keys=None, human_readable=False, colorize=False): """ Render a tree of task nodes as an ``ASCII`` tree. :type write: ``callable`` taking a single ``text_type`` argument :param write: Callable to write the output. :type nodes: ``list`` of ``(text_type, _TaskNode)``. :param nodes: List of pairs of task UUID and task nodes, as obtained from ``Tree.nodes``. :type field_limit: ``int`` :param field_limit: Length at which to begin truncating, ``0`` means no truncation. :type ignored_task_keys: ``set`` of ``text_type`` :param ignored_task_keys: Set of task key names to ignore. :type human_readable: ``bool`` :param human_readable: Should this be rendered as human-readable? :type colorize: ``bool`` :param colorize: Should the output be colorized? """ if ignored_task_keys is None: ignored_task_keys = DEFAULT_IGNORED_KEYS colors = COLORS(colored if colorize else _no_color) format_value = _default_value_formatter(human_readable=human_readable, field_limit=field_limit) get_children = get_children_factory(ignored_task_keys, format_value) get_name = get_name_factory(colors) for root in nodes: write(format_tree(root, get_name, get_children)) write(u'\n')
def render_tasks(write, tasks, field_limit=0, ignored_fields=None, human_readable=False, colorize=False, write_err=None, format_node=format_node, format_value=None, filter_unnamed=False): """ Render Eliot tasks as an ASCII tree. :type write: ``Callable[[text_type], None]`` :param write: Callable used to write the output. :type tasks: ``Iterable`` :param tasks: Iterable of parsed Eliot tasks, as returned by `eliottree.tasks_from_iterable`. :param int field_limit: Length at which to begin truncating, ``0`` means no truncation. :type ignored_fields: ``Set[text_type]`` :param ignored_fields: Set of field names to ignore, defaults to ignoring most Eliot metadata. :param bool human_readable: Render field values as human-readable? :param bool colorize: Colorized the output? :type write_err: Callable[[`text_type`], None] :param write_err: Callable used to write errors. :param format_node: See `format_node`. :type format_value: Callable[[Any], `text_type`] :param format_value: Callable to format a value. """ if ignored_fields is None: ignored_fields = DEFAULT_IGNORED_KEYS colors = COLORS(colored if colorize else _no_color) caught_exceptions = [] if format_value is None: format_value = _default_value_formatter(human_readable=human_readable, field_limit=field_limit) _format_value = track_exceptions(format_value, caught_exceptions, u'<value formatting exception>') _format_node = track_exceptions( partial(format_node, _format_value, colors), caught_exceptions, u'<node formatting exception>') _get_children = partial(get_children, ignored_fields) for task in tasks: tree = format_tree(task, _format_node, _get_children, join_lines=not filter_unnamed) filtered = _filter_unnamed(list(tree)) if filter_unnamed else tree write(filtered) if write_err and caught_exceptions: write_err( colors.error( u'Exceptions ({}) occurred during processing:\n'.format( len(caught_exceptions)))) for exc in caught_exceptions: for line in traceback.format_exception(*exc): if not isinstance(line, text_type): line = line.decode('utf-8') write_err(line) write_err(u'\n')
# pickle nltk tree with open(os.path.join(opts.out_dir, opts.dataset + '_tree.pkl'), 'wb') as f: pickle.dump(nltk_tree, f) # also dumps a list of all paths with open(os.path.join(opts.out_dir, opts.dataset + '_paths.txt'), 'w') as f: writer = csv.writer(f, delimiter=' ') for i in range(len(wnids)): writer.writerow([wnids[i]] + [n for n in paths[wnids[i]]]) # and another (somewhat) human readable file with the tree (using tree_format lib) def convert_for_printing(label, children): if children: return (words[label], [ convert_for_printing(k, children[k]) for k in sorted(children.keys()) ]) else: return (words[label], []) tree_for_printing = convert_for_printing(root, tree[root]) fmt = format_tree(tree_for_printing, format_node=itemgetter(0), get_children=itemgetter(1)) with open(os.path.join(opts.out_dir, opts.dataset + '_hierarchy.txt'), 'w') as f: f.write(fmt)
def list_grades(course: Course, long=False, hide_ungraded=False): try: tree = grades_tree(course) @lru_cache(maxsize=None) def group_ratio(id): pass def format_node(node): item = node[0] if isinstance(item, Course): course = item groups_item = node[1] course_name = course_name_or_unique_course_code(course) def weighted_contribution(group, assignment_submission_pairs): ratio = calculate_group_ratio(group, assignment_submission_pairs)[0] return (group.group_weight/100) * ratio if ratio is not None else group.group_weight/100 weighted_sum = sum(weighted_contribution(group, assignments) for (group, assignments) in groups_item) if weighted_sum > 0: percentage = percentage_string(weighted_sum, 1) color = best_color(weighted_sum) if weighted_sum > 0 else '' return color + course_name + ' ' + percentage + Style.RESET_ALL else: return course_name elif isinstance(item, AssignmentGroup): group = item assignment_submission_pairs = node[1] ratio, total_points, total_possible = calculate_group_ratio(group, assignment_submission_pairs) fraction = rstripped_fraction(total_points, total_possible) if total_possible != 0: ratio = total_points / total_possible percentage = percentage_string(ratio, 1) color = best_color(ratio) else: percentage = 'N/A' color = '' if long: items = [group.name, f'(Weight={percentage_string(group.group_weight/100, 0)})', fraction, percentage] else: items = [group.name, fraction, percentage] return color + ' '.join(items) + Style.RESET_ALL elif isinstance(item, Assignment): assignment, submission = node return ' '.join([str(x) for x in tabulate_grade_row(assignment, submission, long=long)]) def get_children(node): if isinstance(node[1], Submission) or node[1] is None: return [] else: return list(filter(lambda item: not hide_ungraded or item[1] is not None, node[1])) get_outputter().poutput(format_tree(tree, format_node=format_node, get_children=get_children), end='') except Unauthorized: get_outputter().poutput(f'{course_name_or_unique_course_code(course)}: Unauthorized') except CanvasException as e: get_outputter().poutput(f'{course_name_or_unique_course_code(course)}: {str(e)}')
def print_tree(root_node): d = tree_to_dict(root_node) print(format_tree(d, format_node=itemgetter(0), get_children=itemgetter(1)))
def print(self): print( format_tree(self.node, format_node=itemgetter(0), get_children=itemgetter(1)))
def generate(self, root: AST.Node) -> str: """ Generates string representation of given AST tree """ return format_tree(root, self._format_name, self._format_children)
def __str__(self): return format_tree(self, format_node=lambda node: node.token, get_children=lambda node: node.children)
def __str__(self): return format_tree( self.nodes, format_node=itemgetter(0), get_children=itemgetter(1) )
def tree_to_str(root): return format_tree(root, format_node=get_val, get_children=get_children)
def __str__(self): return format_tree(self, format_node=lambda x: f'{type(x.node).__name__} "{x.content}"', get_children=lambda x: x.children)
def render_tree(self) -> str: return format_tree( self, lambda e: e.pokemon_id, lambda e: (evo_line for evo, evo_line in e.evolutions))