make some of the dataclass fields read-only
Project description
partial readonly
partial_readonly
provide a decorator for conveniently making some fields read-only in dataclass. No dependencies other than the Python Standard Library.
Usage
Quick Start
With 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 __init__
)
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 partial_readonly
from dataclasses import dataclass
@dataclass
class Data:
id: int = 0
_name: 'str alias=full_name' = ''
Details
- Mark a field read-only by adding an underline prefix
_id: int
meansid
is read-only
- By default, the relative property named like field without underline prefix
_id: int
will generate a property namedid
- Use string annotation and add
alias=alias_you_want
at the end, and you'll have a property namedalias_you_want
_id: 'int alias=security_id
will generate a property namedsecurity_id
Installation
To install partial_readonly
, simply use pip
:
pip install partial_readonly
Or use easy_install
:
easy_install partial_readonly
Deep down
- 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
Why partial_readonly
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.
Requirements
- Python >= 3.7
Postscript
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!
License
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for partial_readonly-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17ac4e5ca7cd6faa81640f47b38dc01dcb796013d7e421d06fa6d8481de9e05e |
|
MD5 | 8082e9696a9275c57a8ba95f9b69b9c2 |
|
BLAKE2b-256 | 510313a40b713c5fd6bdb57944f30bb5dc51488516e991f84714e1f4a7af2ace |