anytree icon indicating copy to clipboard operation
anytree copied to clipboard

Share your anytree projects

Open c0fec0de opened this issue 7 years ago • 13 comments

Please like this ticket, if you are using anytree.

Feel free to share infos about your project.

c0fec0de avatar Nov 26 '17 16:11 c0fec0de

Thanks for anytree, it's an awesome library that allowed me to create this little project: https://github.com/deadc0de6/catcli - a CLI indexer for your offline data

deadc0de6 avatar Dec 14 '17 20:12 deadc0de6

I am using anytree's NodeMixin class to render dependency trees in johnnydep. Here is an example of johnnydep rendering the dep tree for the catcli project mentioned above:

$ johnnydep catcli
name                summary
------------------  ---------------------------------------------------------------
catcli              The command line catalog tool for your offline data
├── anytree         Powerful and Lightweight Python Tree Data Structure..
│   └── six>=1.9.0  Python 2 and 3 compatibility utilities
├── docopt          Pythonic argument parser, that will make you smile
└── psutil          Cross-platform lib for process and system monitoring in Python.

And here is johnnydep rendering its own dependency tree:

$ johnnydep johnnydep
name                      summary
------------------------  --------------------------------------------------------------------------
johnnydep                 Display dependency tree of Python distribution
├── anytree               Powerful and Lightweight Python Tree Data Structure..
│   └── six>=1.9.0        Python 2 and 3 compatibility utilities
├── cachetools            Extensible memoizing collections and decorators
├── colorama              Cross-platform colored terminal text.
├── oyaml                 Ordered YAML: drop-in replacement for PyYAML which preserves dict ordering
│   └── pyyaml            YAML parser and emitter for Python
├── packaging>=17         Core utilities for Python packages
│   ├── pyparsing>=2.0.2  Python parsing module
│   └── six               Python 2 and 3 compatibility utilities
├── pip                   The PyPA recommended tool for installing Python packages.
├── pkginfo>=1.4.2        Query metadatdata from sdists / bdists / installed packages.
├── pytoml                A parser for TOML-0.4.0
├── setuptools>=38.3      Easily download, build, install, upgrade, and uninstall Python packages
├── structlog             Structured Logging for Python
│   └── six               Python 2 and 3 compatibility utilities
├── tabulate              Pretty-print tabular data
├── wheel>=0.31.0         A built-package format for Python.
└── wimpy                 Anti-copy-pasta

wimglenn avatar Aug 16 '18 01:08 wimglenn

Anytree is used to model and navigate the modular data structure of lbuild: a generic, modular code generator.

 $ lbuild discover
Parser(lbuild)
╰── Repository(modm @ ../../..)   modm: a barebone embedded library generator
    ├── EnumerationOption(target) = stm32f469nih in [at90can128, at90can32, at90can64, ...
    ├── Module(modm:build)   Build System Generators
    │   ├── StringOption(build.path) = ../../../build/stm32f469_discovery/game_of_life ...
    │   ├── StringOption(openocd.cfg) = "" in [String]
    │   ├── StringOption(project.name) = game_of_life in [String]
    │   ├── Module(modm:build:cmake)   CMake Build Script Generator
    │   │   ╰── BooleanOption(include_makefile) = True in [True, False]
    │   ╰── Module(modm:build:scons)   SCons Build Script Generator
    │       ├── StringOption(cache_dir) = /cache in [String]
    │       ├── StringOption(image.source) = "" in [String]
    │       ├── BooleanOption(include_sconstruct) = True in [True, False]
    │       ├── BooleanOption(info.build) = False in [True, False]
    │       ╰── EnumerationOption(info.git) = Disabled in [Disabled, Info, Info+Status]
    ├── Module(modm:platform)   Platform HAL
    │   ├── Module(modm:platform:1-wire.bitbang)   Software 1-Wire
    │   ├── Module(modm:platform:adc)   Analog-to-Digital Converter (ADC)
    │   │   ├── Module(modm:platform:adc:1)   Instance 1
    │   │   ├── Module(modm:platform:adc:2)   Instance 2
    │   │   ╰── Module(modm:platform:adc:3)   Instance 3
    │   ├── Module(modm:platform:can)   Controller Area Network (CAN)
    │   │   ├── Module(modm:platform:can:1)   Instance 1
    │   │   │   ├── NumericOption(buffer.rx) = 32 in [1 .. 32 .. 65534]
    │   │   │   ╰── NumericOption(buffer.tx) = 32 in [1 .. 32 .. 65534]
    │   │   ╰── Module(modm:platform:can:2)   Instance 2
    │   │       ├── NumericOption(buffer.rx) = 32 in [1 .. 32 .. 65534]
    │   │       ╰── NumericOption(buffer.tx) = 32 in [1 .. 32 .. 65534]
    │   ├── Module(modm:platform:can.common)   CAN Common
    │   ├── Module(modm:platform:clock)   Reset and Clock Control (RCC)
    │   ├── Module(modm:platform:clock.cortex)   ARM Cortex-M SysTick
    │   ├── Module(modm:platform:core)   STM32 core module
    │   ├── Module(modm:platform:cortex-m)   ARM Cortex-M Core
    │   │   ├── EnumerationOption(allocator) = tlsf in [block, newlib, tlsf]
    │   │   ├── StringOption(linkerscript.memory) = SDRAM (rwx) : ORIGIN = 0xC0000000, ...
    │   │   ├── StringOption(linkerscript.sections) = .sdramdata :         {           ...
    │   │   ├── StringOption(linkerscript.table_extern.copy) = LONG (__sdramdata_load) ...
    │   │   ├── StringOption(linkerscript.table_extern.heap) = LONG (0x801f)         L ...
    │   │   ├── StringOption(linkerscript.table_extern.zero) = "" in [String]
    │   │   ├── NumericOption(main_stack_size) = 3040 in [256 .. 3040 .. 65536]
    │   │   ├── BooleanOption(stack_execution_guard) = False in [True, False]
    │   │   ├── EnumerationOption(vector_table_location) = rom in [ram, rom]
    │   │   ╰── PropertyQuery(substitutions)
   ...

It's also really nice to customize (we're using : as separator, and it just works™), extended path resolver filling in blanks using context (ie. ::scons is unique, expands to modm:build:scons), extended node rendering (we make some text bold and underline to make it easier to see options).

screenshot 2019-02-20 at 11 39 36

This is a great project, simple to use, well documented, just works as advertised. Thanks a lot!

salkinium avatar Feb 20 '19 10:02 salkinium

A classmate and I did an AI course project that generates Pokemon-playing scripts. We used anytree to create abstract syntax tree.

So an AST like this:

Rule.START
└── Rule.IF_BLOCK
    ├── Rule.BOOL_EXP
    │   └── Rule.AND_EXP
    │       ├── Rule.BOOL
    │       │   └── Rule.LIB_CALL
    │       │       ├── dsl.player_base_spec_defense
    │       │       ├── Rule.NUM_COMPARATOR
    │       │       │   └── <=
    │       │       └── Rule.INT_NUM
    │       │           └── 194
    │       └── Rule.BOOL
    │           └── Rule.LIB_CALL
    │               ├── dsl.check_move_sds_if_hits_opp
    │               └── move
    └── Rule.IF_BODY
        └── Rule.CHANGE_SCORE
            └── Rule.SCORE_NUM
                └── 5

Would be rendered as this:

class Script_9f8ccd13db044ef19aef7d204b40a325(Script):
    def choose_move(self, battle: Battle):
    
        available_moves = battle.available_moves
        if not available_moves:
            return self.choose_random_move(battle)
            
        dsl = DSL(battle)
        move_scores = []
        for move in battle.available_moves:
            score = 0
            if (((dsl.player_base_spec_defense() <= 194) and dsl.check_move_sds_if_hits_opp(move))):
                score += 5
            move_scores.append(score)
            
        best_move = available_moves[move_scores.index(max(move_scores))]
        return best_move

uduse avatar Apr 22 '20 21:04 uduse

Please like this ticket, if you are using anytree.

Feel free to share infos about your project.

Hello, I felt the need to get into Python syntax and way of thinking so I decided to build a lightweight ToDo app, and your solution for building trees felt like a perfect helping hand. Feel free to check it out here

jelicicm avatar May 20 '20 17:05 jelicicm

Hi! I used a lot anytree at my work for representing and manipulating hierarchical data in different contexts. It is a great library, with a simple yet powerful interface and very well documented.

I also wrote a short blog post about it, check it out here.

Thanks for developing this library!

madagra avatar Jun 07 '20 11:06 madagra

I packaged anytree for Alpine Linux (here), and use it in a Linux mobile-friendly password-store app: caerbannog

Thanks for the great library!

craftyguy avatar Feb 10 '21 07:02 craftyguy

We are using it in a machine learning project which deals with hierarchical data. It is private unfortunately and cannot give many details, but it such a huge help versus our old internal library.

mastercoms avatar Oct 26 '21 06:10 mastercoms

I used anytree to render a tree and here's the restriction I came up with. New to python, hence may not be seeing the light. In my case, a parent P1(attributes ap1) connects to a child C1(attributes ac1). I have another connection of parent P1(attributes ap2) to child C1(attributes ac2) and so on and so forth. So basically I need any array of attributes at every level with attributes depicted as jsons(dict).The attributes here lets me denote the different link pairs between the same parent and child. How can I achieve this ?

P1 [{its connection attributes to its parent like below}] |-----C1[{parentAttrib:ap1,childAttrib:ac1},{parentAttrib:ap2,childAttrib:ac2}]

Thanks in advance!

I used NodeMixin class and it was easy. Thinking a little more, I think I could do it using plain Node as well. I could do a hasattrib and find if the attributes exists, if does not then add a list to the attributes and add the data element. So everytime need to check if it exists or not before creating or appending an object(dict in my case). Though not implemented this.

Thanks again for the wonderful library.

sparvangada avatar Feb 18 '22 19:02 sparvangada

i used a combination of anytree and Rich to build the pdfalyzer, a tool to help visualize the logical structure of a PDF with an eye to finding malicious content. anytree handles the data model, rich handles the presentation. there's some images on the pdfalyzer's repo page, or here's an even bigger example showing the glory of the tree.

great library. since Rich added their own tree visualization tool not that long ago, putting them together is a super low cost way to get really great visualizations. SymlinkNode was particularly useful for this use case because PDFs contain lots of links between nodes beyond parent/child.

(and here's a twitter thread if you're curious about why i did that and what i did with it once i built it)

michelcrypt4d4mus avatar Sep 15 '22 04:09 michelcrypt4d4mus

Thanks for the library. I used it to visualize ASTs built by a compiler that I'm writing.


Can I create two different nodes with the same display name? If I create two Nodes with the same name using the Node(..) constructor, both the nodes get merged into one.

RamneekSingh24 avatar Sep 27 '22 15:09 RamneekSingh24

@RamneekSingh24 the NodeMixin class allows you to build a tree out of arbitrary python objects, at which point the sky's the limit as far as names etc.

michelcrypt4d4mus avatar Sep 28 '22 00:09 michelcrypt4d4mus