Skip to content

SourceCodeMaker solves the problem of getting the Final Source Code of a Python Class that was extended from multiple classes

License

Notifications You must be signed in to change notification settings

ryucoder/source-code-maker

Repository files navigation

SourceCodeMaker

SourceCodeMaker solves a real life problem of every python developer on the planet
i.e. getting the Final Source Code of a Python Class that was extended from multiple classes.

Best example of this would be Class Based Views in Django. CBV in Django are very hard to understand, as the class is being extended from many different classes and mixins. As the source code is scattered across multiple different python files and classes, understanding them can be very tricky and time consuming. Because none can see the final source code of the CBV in one single place. It becomes even messier because of the super() in python 😵. Debugging is a real pain in the ass 😂.

My code makes it super duper easy to get the final source code of a python class that is extended from multiple classes. Just give a call the constructor of a SourceCodeMaker and the source code is available as an attribute. If a call to super() method was made, its source code is also included. All of above problems are solved with just one line of code 😎.

That's not it, it takes it even further. Send kwarg metadata=True to SourceCodeMaker constructor and added information like MRO, which attributes and methods belongs to which class are also shown. If some attributes were overwritten in a parent class that is also shown 🤘 👏 👍.

Why to use it? What is the benefit of this?

It's intended to make the life of a developer little bit easy. It is most useful during the development process.
Especially debugging classes that were extended from multiple classes.

Just make sure to pass metadata=True to the constructor,
you will get much information that is otherwise not readily available in one single place.

How to use it?

It's damn simple. Please refer to below examples for more details.

Example No.1 :

All you have to do is import the SourceCodeMaker class and give call to its constructor
and you can access the final source code of that class in the final_cource_code attribute. Tada!
If call to super() was given inside a method, its source code is also shown.

***** main.py *****

from SourceCodeMaker import SourceCodeMaker
from django.views.generic import CreateView

source = SoureCodeMaker(CreateView).final_source_code
print(source)

Example No.2 :

Passing metadata=True in the constructor will show some metadata information of the class.
Metadata includes the method resolution order (MRO) of the class.
Metadata also includes which attributes and methods belongs to which class in the mro.
If some attributes are overwritten in a parent class that is also shown in the source code.
If call to super() was given inside a method, its source code is also shown.

***** main.py *****

from SourceCodeMaker import SourceCodeMaker
from django.views.generic import CreateView

source = SoureCodeMaker(CreateView, metadata=True).final_source_code
print(source)

Example No.3 :

If the main.py file was inside the desktop folder
a new file would be created in that same folder with the name of the class, in this case CreateView.py

***** main.py *****

from SourceCodeMaker import SourceCodeMaker
from django.views.generic import CreateView

source = SoureCodeMaker(CreateView).dump_source_to_current_folder()

Example No.4 :

If the main.py file was inside the desktop folder and absolute path is given,
a new file would be created in the folder that you specify as the abs_path

***** main.py *****

from SourceCodeMaker import SourceCodeMaker
from django.views.generic import CreateView

abs_path = "Type the full absolute path of a folder"
source = SoureCodeMaker(CreateView).dump_source_to_specific_folder(abs_path)

Example No.5 :

If for any reason, you wish to access the final source code in raw string format,
just give a call to get_raw_source() method of the SourceCodeMaker class.

***** main.py *****

from SourceCodeMaker import SourceCodeMaker
from django.views.generic import CreateView

source = SoureCodeMaker(CreateView)
raw_source = source.get_raw_source()
print(raw_source)

About

SourceCodeMaker solves the problem of getting the Final Source Code of a Python Class that was extended from multiple classes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages