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

Adding an ImmutableConfigValue class #95

Merged
merged 5 commits into from
Apr 9, 2020

Conversation

michaelbynum
Copy link
Contributor

Changes proposed in this PR:

This PR adds a class derived from ConfigValue which can be made temporarily immutable by setting the _mutable attribute to False.

Legal Acknowledgement

By contributing to this software project, I agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

@michaelbynum
Copy link
Contributor Author

@jsiirola Please let me know what you think of this implementation. The idea is that you just set the _mutable flag to False when you want the config value to be immutable.

@coveralls
Copy link

coveralls commented Mar 30, 2020

Coverage Status

Coverage increased (+0.1%) to 61.194% when pulling 9cc57f0 on michaelbynum:immutable-config into ad6043c on PyUtilib:master.

@jsiirola
Copy link
Contributor

What about this:

class ImmutableConfigValue(ConfigValue):
    def set_value(self, value):
        raise RuntimeError(str(self) + ' is currently immutable.')

class MarkImmutable(object):
    def __init__(self, *args):
        self._locked = []
        try:
            for arg in args:
                if type(arg) is not ConfigValue:
                    raise ValueError()
                arg.__class__ = ImmutableConfigValue
                self._locked.append(arg)
        except:
            self.release_lock()

    def release_lock(self):
        for arg in self._locked:
            arg.__class__ = ConfigValue
        self._locked = []

...this would allow you to lock configurations that you didn't actually create. With a little more work, we could make it so that the locks are either exclusive (or support nested).

@michaelbynum
Copy link
Contributor Author

@jsiirola Good suggestion.

@michaelbynum michaelbynum changed the title [WIP] adding an ImmutableConfigValue class Adding an ImmutableConfigValue class Mar 31, 2020
@codecov-io
Copy link

codecov-io commented Mar 31, 2020

Codecov Report

Merging #95 into master will increase coverage by 0.08%.
The diff coverage is 86.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #95      +/-   ##
==========================================
+ Coverage   63.13%   63.21%   +0.08%     
==========================================
  Files          87       87              
  Lines        8788     8818      +30     
==========================================
+ Hits         5548     5574      +26     
- Misses       3240     3244       +4     
Impacted Files Coverage Δ
pyutilib/misc/config.py 92.27% <86.66%> (-0.24%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ad6043c...9cc57f0. Read the comment docs.

@jsiirola jsiirola merged commit 6f02a99 into PyUtilib:master Apr 9, 2020
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.

4 participants