Skip to main content

Pythonic Java optional

Project description

Maypy is python implementation of the well known Java Optional API. It's designed to help you handle potential None values, reducing error from NoneType.

Features :

  • Brings functional programming
  • Easy to use
  • Fully typed and compatible with mypy !
  • Lightweight

installation

pip install maypy

That's all ! Ready to use

Usage

Description

It's not rare to handle return from function that maybe either a value or None. Like .get from a dictionary, results from api or ORM etc. With no more if value is None with MayPy, encapsulate your value and do what you want to.

MayPy brings Maybe a wrapper (container), it is either empty if the value passed was None or valuated, in this case it contains the value, and we can perform operation on it.

Examples

Checking value presence:

from maypy import Maybe, Empty, maybe

assert Empty().is_empty()
assert maybe(None).is_empty()
assert maybe("value").is_present()

Using chaining operation over wrapped value:

from maypy import Maybe
from typing import List, Optional


def convert_to_celsius(fahrenheit: float) -> float:
  return (fahrenheit - 32) * 5 / 9


fahrenheit_temperatures: List[Optional[float]] = [32.45, None, 26.6, 100, 72, None, 10]

for temp in fahrenheit_temperatures:
  Maybe.of(temp).map(convert_to_celsius).filter(lambda celsius: celsius > 0)

Defining return from database result:

from dataclasses import dataclass
from enum import StrEnum, unique

from maypy import Maybe, maybe
from pydantic import BaseModel
from sqlalchemy.orm import Session


@unique
class Gender(StrEnum):
  MALE = "M"
  FEMALE = "F"
  UNKNOWN = "X"


@dataclass
class User:
  id: str
  email: str
  gender: Gender


class UserEntity(BaseModel):
  id: str
  email: str
  gender: str

  class Config:
    orm_mode = True


def get_user(db: Session, user_id: str) -> Maybe[User]:
  return maybe(db.query(UserEntity).filter(UserEntity.id == user_id).first()).map(to_domain)


def to_domain(user_dto: UserEntity) -> User:
  return User(
    id=user_dto.id,
    email=user_dto.email,
    gender=Gender(user_dto.gender)
  )


user = get_user(db, "481efz4x1d").or_else_raise(Exception("User id<481efz4x1d> not found"))

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

maypy-2.1.0.tar.gz (709.1 kB view details)

Uploaded Source

Built Distribution

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

maypy-2.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file maypy-2.1.0.tar.gz.

File metadata

  • Download URL: maypy-2.1.0.tar.gz
  • Upload date:
  • Size: 709.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for maypy-2.1.0.tar.gz
Algorithm Hash digest
SHA256 f7d9fc3bcf13e0ba6ce33c6c553a064268bf169a74f3fe92bceb229a678079f9
MD5 fbd9923748de21821fdcc5ba63bd2b1b
BLAKE2b-256 31265d3f1848a295f6d31022b1666fec93e3e2c6bb614062b01a6050720b7d80

See more details on using hashes here.

Provenance

The following attestation bundles were made for maypy-2.1.0.tar.gz:

Publisher: release.yml on mLetrone/maypy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maypy-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: maypy-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for maypy-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e64ddf96ecfe31c04ddcab3ef23f4599bbb6e438666d6467a74ea8675670d2a
MD5 dd23fba322764160757803687c3421c9
BLAKE2b-256 65a17e3e7c54e495107ec43475c0478d80a02b8054a4f6e1df12954300feb10b

See more details on using hashes here.

Provenance

The following attestation bundles were made for maypy-2.1.0-py3-none-any.whl:

Publisher: release.yml on mLetrone/maypy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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