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.

Source Distribution

partial_readonly-0.0.3.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

partial_readonly-0.0.3-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file partial_readonly-0.0.3.tar.gz.

File metadata

  • Download URL: partial_readonly-0.0.3.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

File hashes

Hashes for partial_readonly-0.0.3.tar.gz
Algorithm Hash digest
SHA256 97a4ab43769288d549a05a74069fc4f2c3c95d3b1183d13b12167cd8fc756c2a
MD5 3b6bf560a2cf7a7f48ce7405edc8d17b
BLAKE2b-256 6540029a015974895b2e028616afdccf509fa102c8c7eddd264a46e0e32ea8b6

See more details on using hashes here.

File details

Details for the file partial_readonly-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: partial_readonly-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

File hashes

Hashes for partial_readonly-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 17ac4e5ca7cd6faa81640f47b38dc01dcb796013d7e421d06fa6d8481de9e05e
MD5 8082e9696a9275c57a8ba95f9b69b9c2
BLAKE2b-256 510313a40b713c5fd6bdb57944f30bb5dc51488516e991f84714e1f4a7af2ace

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page