make some of the dataclass fields read-only
partial_readonly provide a decorator for conveniently making some fields read-only in dataclass. No dependencies other than the Python Standard Library.
partail_readonly, we can easily make some of the fields in
dataclass read-only by creating property related to them. (Notice: We still can't make the origin field read-only, cause it still need to be set while calling
For example, the following code
from dataclasses import dataclass @dataclass class Data: id: int = 0 name: str = '' @property def full_name(self): return self.name
can be written like this by using
from dataclasses import dataclass @dataclass class Data: id: int = 0 _name: 'str alias=full_name' = ''
- Mark a field read-only by adding an underline prefix
- By default, the relative property named like field without underline prefix
_id: intwill generate a property named
- Use string annotation and add
alias=alias_you_wantat the end, and you'll have a property named
_id: 'int alias=security_idwill generate a property named
partial_readonly, simply use
pip install partial_readonly
- Making property read-only by set a getter[ and ban a setter]
- Using AST to build a getter for new property
- The idea about abusing annotation comes from zmitchell/annotation-abuse
Lastest python offer us dataclass for creating some class in convenient way.
But it's not convenient when we want not all the fields be readonly.
- Python >= 3.7
I built this for practicing AST and publish.
I think it work fine.
There are some more easily implementation.
If you sharing some idea about this feature I'll be greatful!
MIT License Copyright (c) 2018 book987 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size partial_readonly-0.0.3-py3-none-any.whl (4.2 kB)||File type Wheel||Python version py3||Upload date||Hashes View hashes|
|Filename, size partial_readonly-0.0.3.tar.gz (3.9 kB)||File type Source||Python version None||Upload date||Hashes View hashes|
Hashes for partial_readonly-0.0.3-py3-none-any.whl