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

Move mathics.core.builtin initialization code into system_initialize() #648

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

rocky
Copy link
Member

@rocky rocky commented Dec 3, 2022

@mmatera and @TiagoCavalcante : this is kind of important to understand.

Although we now require a frontend to explicitly call initialize_system() and this may seem more awkward, it is helpful and important for a couple of reasons.

First: it provides better modularization and reduces circular imports.

mathics.builtin is close to top-level import and, in contrast to mathics.core.system_init there are a lot of modules underneath mathics.builtin. (There are none under mathics.core.system_init).

In general, it is considered bad Python coding practice to put a lot of code in __init__.py files.

As a result, we can have more import mathics.core.system_init from a module where we can't do the same for import mathics.builtin.

Second: by placing initialization of structures which do not change outside of development we are in a better position to write an administrative tool to load all of this and then dump it as a system image file, which can be read in rather than built it up from scratch as happens now. This new function initialize_system might have a parameter to read the image file rather than do the work it does now.

In its current form though initialize_system() does as not encapsulate system loading as much as it could.

@mmatera
Copy link
Contributor

mmatera commented Dec 3, 2022

@rocky, In the proposal I made in #639, there is no need to modify the front-ends. What is the improvement here over that branch?

(in that branch, there is no need to call _initialize_system_definitions() from outside of the mathics.builtin.system_init module mathics/builtin/system_init.py )

@rocky
Copy link
Member Author

rocky commented Dec 3, 2022

@rocky, In the proposal I made in #639, there is no need to modify the front-ends. What is the improvement here over that branch?

(in that branch, there is no need to call _initialize_system_definitions() from outside of the mathics.builtin.system_init module mathics/builtin/system_init.py )

There is a principle Explicit is better than Implicit.

In #639 (and master) magic initialization happens when the module is loaded. Exactly when is hard to determine, since it relies on what module imports what where and in what order.

The order of module loading for purposes of what gets initialize when is not something a programmer generally keeps track of nor wants to.

But since the intent here is to serve as a place for performing initialization that might be done to create a base image, this is something that I believe we want developers to know about and be able to know very definitely when and where this is triggered - the call to system_initialize. Again, this is something that I believe developers should be aware of.

@rocky
Copy link
Member Author

rocky commented Dec 3, 2022

BTW The main other problem I have with #639 is that tries to do too much and doesn't succeed in either activity as well as if we isolate the two concerns of reducing mathics.builtin (which does a bit of system initialization) and pulling out module loading from inside Definitions object creation.

That second aspect we have come to learn is a bit thorny and needs more attention. This PR (when done) lays a better foundation for that to occur.

@rocky rocky changed the title Move mathics.core.builtin initialization code into system_initialiize() Move mathics.core.builtin initialization code into system_initialize() Dec 3, 2022
@rocky rocky force-pushed the move-code-out-of-builtins_init branch 5 times, most recently from de81a34 to 2985323 Compare December 4, 2022 11:34
@rocky rocky marked this pull request as ready for review December 4, 2022 11:47
@rocky rocky force-pushed the move-code-out-of-builtins_init branch from 2985323 to cee92f0 Compare December 4, 2022 11:56
@rocky
Copy link
Member Author

rocky commented Dec 4, 2022

@mmatera this is about all the time I have this week for this. I don't think I was completely successful here since I'd like to move to move out all of the code from mathics.builtin.__init__ and some still remains.

The "sanity check" which looks for that Builtins have a "summary_text" should be turned into a standalone program outside of the code, and either made a separate program or added to one of the other consistency checks.

That there are this many changes and this many commits (with squashing down a number of commits into one) is a testament to the messiness we still have in or code and the problems with adding features before cleaning up a base that is wanting.

If you have are willing and have time to move this forward more, please do. Otherwise we can merge I will iterate next week.

@rocky rocky force-pushed the move-code-out-of-builtins_init branch from 3d73ddc to 6dac441 Compare December 4, 2022 12:15
@mmatera
Copy link
Contributor

mmatera commented Dec 5, 2022

@mmatera this is about all the time I have this week for this. I don't think I was completely successful here since I'd like to move to move out all of the code from mathics.builtin.__init__ and some still remains.

OK. I think that this is a progress.

The "sanity check" which looks for that Builtins have a "summary_text" should be turned into a standalone program outside of the code, and either made a separate program or added to one of the other consistency checks.

I think now the standalone program is there (test/consistency-and-style/test_summary_text.py) so it is safe to remove the sanity_check code.

That there are this many changes and this many commits (with squashing down a number of commits into one) is a testament to the messiness we still have in or code and the problems with adding features before cleaning up a base that is wanting.

If you have are willing and have time to move this forward more, please do. Otherwise we can merge I will iterate next week.

Since this seems a little different from what I have in mind, I will need some time to understand this. In particular, I am not convinced about the need to call the function that initializes the module from outside the module. Mainly because it is something that must be called exactly once when the module is imported. Also, before doing something that implies change the other projects, I would like to define better what is the API for this part of the code.
I will try to split this in smaller parts before the next weekend, and then let's discuss over it.

Although we now require a frontend to *explicitly* call
``initialize_system()`` and this may seem more akward, it is helpful
and important for a couple of reasons.

First, it provides better modularization and reduces circular imports:
``mathics.builtin``  is a close to top-level import and, in contrast to
``mathics.builtin.system_init`` there are a lot of modules underneath
``mathics.builtin``. (There are none under
``mathics.builtin.system_init``).

As a result we can have more ``import mathics.builtin.system_init`` from a module
where we can't do the same for ``import mathics.builtin``.

Second, by placing initialization of structures which do not
change outside of development we are in a better position to have write
and admninstrative tool to load all of this and dump it to as a system
image, which can be read in rather than built it up from scratch as
happens now. This new function ``initialize_system`` might have a parameter
to read the image file rather than do the work it does now.

In its current form though ``initialize_system()`` does as not
encapsulate system loading as much as it could.
Move system initialization module out of mathics.builtin.
(Similar to PR #639)

This also removes the need for Python to "partially initialize"
mathics.builtin since it doesn't import one of its children

Merge mess
@rocky rocky force-pushed the move-code-out-of-builtins_init branch from 12eb480 to e1e0c44 Compare December 7, 2022 22:27
system_builtins = {}


def add_builtins(new_builtins):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between this and

def add_builtins(new_builtins):
?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I have is a still a mess still.

I'd like to remove add_builtin from mathics/builtin/__init__.py and the call at the end to of that module. When I try though I am getting errors in evaluation. So right now duplication of code is as close as I can get.


initialize_system()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This routine is called each time Definitions(add_builtins=True) is called. And what it essentially does is to initialize (a part of) the mathics.builtins module, so if you do not call it, Definitions(add_builtins=True) can not do what it is expected. So, should not be better either
a) call it in mathics.buitlin.__init__ module.
b) import it (locally) and call it in mathics.core.definitions.Definitions.__init__, if the add_builtins==True?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to resolve. I don't like a) because we want to remove stuff from mathics.builtin.__init__ - that is the whole point of this.

We want a function, initialize_system() that is explicitly called that will load will initialize all predefined Builtins.

Once that is done, we are in a position to write a routine to dump out all of this to say a pickle file and then we have the possibility to replace that code with code that reads in the dumped information instead.

Equally important, this is conceptually simpler, and coding-wise we don't have critical stuff or magic code inside the an __init__ module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants