Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create typed Class-Structure Diagram #26

Open
krlng opened this issue Jan 13, 2021 · 0 comments
Open

Create typed Class-Structure Diagram #26

krlng opened this issue Jan 13, 2021 · 0 comments
Labels

Comments

@krlng
Copy link

krlng commented Jan 13, 2021

This isn't a bug, but a question for help / hints.

I would like to use lolviz to create a graph of my python class structure. Every class-attribute is type annotated, so it should be possible to like their interactions without creating class instances. Here is a minimal example:

from copy import deepcopy
from lolviz import *
class Workout:
    def __init__(
        self,
        date: dt.date,
        name: str = "",
        duration: int = 0
    ):
        # assert isinstance(tss, (np.number, int))
        self.date: dt.date = date
        self.name: str = name
        self.duration: int = duration  # in seconds
        self.done: boolean = False

class Athlete:
    def __init__(
        self,
        name: str,
        birthday: dt.date,
        sports: List[str]
    ):
        self.name: str = name
        self.birthday: dt.date = birthday
        self.sports: List[str] = sports


class DataContainer:
    def __init__(self, 
                 athlete: Athlete, 
                 tasks: List[Workout] = [], 
                 fulfilled: List[Workout] = []):
        self.athelete: Athlete = athlete
        self.tasks: List[Workout] = [w for w in tasks if isinstance(w, Workout)]
        self.fulfilled: List[Workout] = [w for w in fulfilled if isinstance(w, Workout)]

me = Athlete("nico", dt(1990,3,1).date(), sports=["running","climbing"])
t1 = Workout(date=dt(2020,1,1).date(), name="5k Run")
f1 = deepcopy(t1)
f1.done = True
dc1 = DataContainer(me, tasks=[t1], fulfilled=[f1])

Now I can use objviz(dc1) to create the following diagram:
Screenshot 2021-01-13 at 16 47 07

What I actually would like to achieve is a command like classviz(DataContainer) which will give me a similar chart, but not with the actual attribute values but their types. For sure there will be other small changes, but that's the basic idea.

What I already can do is something like:

def get_types(annotated_class):
    return (annotated_class.__name__, {k: v.__name__ for k,v in annotated_class.__init__.__annotations__.items()})

get_types(Workout)

which gives me something like: ('Workout', {'date': 'date', 'name': 'str', 'duration': 'int'}). How ever I don't find a proper way to create similar table-elements which contain Workout in the header and the name-attribute mapping in it's body.

Can someone give me a hint, how to create such tables manually? I am also happy for any additional advices

@parrt parrt added the feature label Jan 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants