-
Notifications
You must be signed in to change notification settings - Fork 0
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
Python-генератор кода JS-дескрипторов на основе MADescriptionProvider #131
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b38c561
Абстрактный MADescriptionProvider, а также TestModelDescriptorProvide…
konstantin-kalinin-wizn-tech b6c8851
Реализация синглетона в базовом MADescriptionProvider
konstantin-kalinin-wizn-tech b7dae08
Заготовка модуля для транспайлера дескрипторов JS
konstantin-kalinin-wizn-tech 7fb277b
Заготовка MADescriptionTranspiler_JS
konstantin-kalinin-wizn-tech cb37115
Рефакторинг MADescriptionProvider
konstantin-kalinin-wizn-tech d00487e
Параметризация MADescriptionTranspiler_JS
konstantin-kalinin-wizn-tech a762b75
Синглетонность и рефакторинг MADescriptionProvider
konstantin-kalinin-wizn-tech d591f09
Правки кодогенерации в MADescriptionProvider
konstantin-kalinin-wizn-tech a36c1e3
Добавлены недостающие поля дескрипторов
konstantin-kalinin-wizn-tech 84ff1b2
MAMetaSingleton вместо MADescriptionProviderMetaSingleton
konstantin-kalinin-wizn-tech d34e840
Декоратор @abstractmethod
konstantin-kalinin-wizn-tech ea69376
Правки MADescriptionTranspiler_JS (динамическое вычисление импортов, …
konstantin-kalinin-wizn-tech b29da98
Использование фактического класса контейнера вместо захардкоженого MA…
konstantin-kalinin-wizn-tech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
from abc import abstractmethod | ||
from Magritte.descriptions.MADescription_class import MADescription | ||
|
||
|
||
class MAMetaSingleton(type): | ||
_instances = {} | ||
def __call__(cls, *args, **kwargs): | ||
singleton_key = 'singleton' | ||
if singleton_key in kwargs: | ||
singleton = bool(kwargs.pop(singleton_key)) | ||
else: | ||
singleton = True # The default | ||
if singleton: | ||
if cls not in cls._instances: | ||
cls._instances[cls] = super(MAMetaSingleton, cls).__call__(*args, **kwargs) | ||
return cls._instances[cls] | ||
else: | ||
return super(MAMetaSingleton, cls).__call__(*args, **kwargs) | ||
|
||
class MADescriptionProvider(metaclass=MAMetaSingleton): | ||
|
||
def __init__(self): | ||
self._all_descriptions = list() | ||
self._descriptions_by_model_name = dict() | ||
self.instatiate_descriptions() | ||
|
||
def register_description(self, aDescription: MADescription): | ||
""" | ||
Should be called inside instatiate_descriptions only. | ||
Should be called only after a name is assigned to a description. | ||
""" | ||
model_name = aDescription.name | ||
if model_name is None: | ||
raise TypeError('register_description should be called only after a name is assigned to a description') | ||
if model_name in self._descriptions_by_model_name: | ||
return | ||
self._descriptions_by_model_name[model_name] = aDescription | ||
self._all_descriptions.append(aDescription) | ||
|
||
@abstractmethod | ||
def instatiate_descriptions(self): | ||
"""Method to generate the descriptions.""" | ||
pass | ||
|
||
@property | ||
def all_descriptions(self) -> list[MADescription]: | ||
"""Returns all descriptions.""" | ||
return self._all_descriptions | ||
|
||
def description_for(self, model_name: str) -> MADescription: | ||
"""Returns a model description for the given model type.""" | ||
if model_name in self._descriptions_by_model_name: | ||
return self._descriptions_by_model_name[model_name] | ||
raise ValueError(f'Unknown model type: {model_name}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
насколько я понимаю, в Python абстрактные методы принято обозначать декоратором
abstractmethod
из модуляabc
("abstract base class").Документация: https://docs.python.org/3/library/abc.html
Пример: https://www.geeksforgeeks.org/abstract-classes-in-python/