Skip to main content

Simple constants sets for Python

Project description

Constants-set

  • Simple constants sets for Python.
  • Kind of like enums, reusable constants.
  • Kind of convenient for Django and DRF.

Install

pip install constants-set

Use cases

Simple python

from constants_set import ConstantsSet


class Book:
    TYPES = ConstantsSet(["ROMANCE", "ACTION"])

    def __init__(self, type):
        self.type = type

    def get_global_feeling(self):
        if self.type == Book.TYPES.ROMANCE:
            return 'love'
        if self.type == Book.TYPES.ACTION:
            return 'intensity'

Django model

from django.db import models


class BookModel(models.Model):
    type = models.CharField(max_length=30, choices=Book.TYPES.to_choices(), default=Book.TYPES.ROMANCE)

Django rest framework serializer

from rest_framework import serializers

class BookSerializer(serializers.Serializer):
    type = serializers.ChoiceField(required=True, choices=Book.TYPES.to_choices())

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

constants_set-0.5-py3-none-any.whl (2.1 kB view hashes)

Uploaded Python 3

Supported by

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