MCPcopy Index your code
hub / github.com/OmkarPathak/pygorithm / __str__

Method __str__

pygorithm/data_structures/quadtree.py:511–552  ·  view source on GitHub ↗

Create a human-readable representation of this quad tree .. caution:: Because of the complexity of quadtrees it takes a fair amount of calculation to produce something somewhat legible. All returned statistics have paired functions.

(self)

Source from the content-addressed store, hash-verified

509 return "quadtree(bucket_size={}, max_depth={}, location={}, depth={}, entities={}, children={})".format(self.bucket_size, self.max_depth, repr(self.location), self.depth, self.entities, self.children)
510
511 def __str__(self):
512 """
513 Create a human-readable representation of this quad tree
514
515 .. caution::
516
517 Because of the complexity of quadtrees it takes a fair amount of calculation to
518 produce something somewhat legible. All returned statistics have paired functions.
519 This uses only iterative algorithms to calculate statistics.
520
521 Example:
522
523 .. code-block:: python
524
525 from pygorithm.geometry import (vector2, rect2)
526 from pygorithm.data_structures import quadtree
527
528 # create a tree with a up to 2 entities in a bucket that
529 # can have a depth of up to 5.
530 _tree = quadtree.QuadTree(2, 5, rect2.Rect2(100, 100))
531
532 # add a few entities to the tree
533 _tree.insert_and_think(quadtree.QuadTreeEntity(rect2.Rect2(2, 2, vector2.Vector2(5, 5))))
534 _tree.insert_and_think(quadtree.QuadTreeEntity(rect2.Rect2(2, 2, vector2.Vector2(95, 5))))
535
536 # prints quadtree(at rect(100x100 at <0, 0>) with 0 entities here (2 in total); (nodes, entities) per depth: [ 0: (1, 0), 1: (4, 2) ] (allowed max depth: 5, actual: 1), avg ent/leaf: 0.5 (target 1), misplaced weight 0.0 (0 best, >1 bad)
537 print(_tree)
538
539 :returns: human-readable representation of this quad tree
540 :rtype: string
541 """
542
543 nodes_per_depth = self.find_nodes_per_depth()
544 _ents_per_depth = self.find_entities_per_depth()
545
546 _nodes_ents_per_depth_str = "[ {} ]".format(', '.join("{}: ({}, {})".format(dep, nodes_per_depth[dep], _ents_per_depth[dep]) for dep in nodes_per_depth.keys()))
547
548 _sum = self.sum_entities(entities_per_depth=_ents_per_depth)
549 _max_depth = max(_ents_per_depth.keys())
550 _avg_ent_leaf = self.calculate_avg_ents_per_leaf()
551 _mispl_weight = self.calculate_weight_misplaced_ents(sum_entities=_sum)
552 return "quadtree(at {} with {} entities here ({} in total); (nodes, entities) per depth: {} (allowed max depth: {}, actual: {}), avg ent/leaf: {} (target {}), misplaced weight {} (0 best, >1 bad)".format(self.location, len(self.entities), _sum, _nodes_ents_per_depth_str, self.max_depth, _max_depth, _avg_ent_leaf, self.bucket_size, _mispl_weight)
553
554 @staticmethod
555 def get_code():

Callers

nothing calls this directly

Calls 5

find_nodes_per_depthMethod · 0.95
sum_entitiesMethod · 0.95

Tested by

no test coverage detected