Skip to main content

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

  1. Mark a field read-only by adding an underline prefix
    • _id: int means id is read-only
  2. By default, the relative property named like field without underline prefix
    • _id: int will generate a property named id
  3. Use string annotation and add alias=alias_you_want at the end, and you'll have a property named alias_you_want
    • _id: 'int alias=security_id will generate a property named security_id

Installation

To install partial_readonly, simply use pip:

pip install partial_readonly

Or use easy_install:

easy_install partial_readonly

Deep down

  1. Making property read-only by set a getter[ and ban a setter]
  2. Using AST to build a getter for new property
  3. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for partial-readonly, version 0.0.3
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

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page