Skip to main content

A structured return object for Python functions — carries status code, message, data, and details.

Project description

fry-return

A structured return object for Python functions. Instead of returning bare dicts or raising exceptions for every non-happy path, wrap your result in a FryReturnObj — it carries a typed status code, a human-readable message, optional data payload, and a list of detail strings.

Installation

pip install fry-return

Quick start

from fry_return import FryReturnObj, FryReturnCode

Direct usage — data goes into data_dict

Simple but less IDE-friendly: you get no autocomplete on the payload fields.

def get_user(user_id: int) -> FryReturnObj:
    user = db.find(user_id)
    if user is None:
        return FryReturnObj.failed("用户不存在")
    return FryReturnObj.success("查询成功").set_data("user", user)

result = get_user(42)
if result.is_success:
    print(result.data_dict["user"])
else:
    print(result.message_str)

Inheritance — core data as typed attributes (recommended)

Define a subclass and add your payload as proper typed fields. Editors will autocomplete everything.

from dataclasses import dataclass, field
from fry_return import FryReturnObj, FryReturnCode

@dataclass
class GetUserResult(FryReturnObj):
    user_id: int = 0
    username: str = ""
    email: str = ""

def get_user(user_id: int) -> GetUserResult:
    user = db.find(user_id)
    if user is None:
        return GetUserResult(code=FryReturnCode.FAILED, message_str="用户不存在")

    result = GetUserResult(code=FryReturnCode.SUCCESS, message_str="查询成功")
    result.user_id = user.id
    result.username = user.name
    result.email = user.email
    return result

result = get_user(42)
if result.is_success:
    print(result.username)   # 编辑器可以自动补全
    print(result.email)

Chaining

result = (
    FryReturnObj.info("开始处理")
    .add_detail("步骤1完成")
    .add_detail("步骤2完成")
    .set_success("全部完成")
    .set_data("count", 10)
)

Serialization

d = result.to_dict()          # -> dict,可直接 JSON 序列化
r = FryReturnObj.from_dict(d) # 从 dict 还原

Status codes (FryReturnCode)

名称 说明
DEBUG 100 调试信息
INFO 200 普通信息
IMPORTANT 300 重要信息
WARNING 400 警告
VALIDATE 500 验证失败
FAILED 600 操作失败
BUG 700 程序错误
SUCCESS 800 完全成功
PARTIAL_SUCCESS 820 部分成功

Key properties

属性 说明
is_success / is_full_success 是否为 SUCCESS
is_partial_success 是否为 PARTIAL_SUCCESS
code_name 状态码名称字符串
code_value 状态码数值

License

MIT

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

fry_return-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

fry_return-0.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file fry_return-0.1.0.tar.gz.

File metadata

  • Download URL: fry_return-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for fry_return-0.1.0.tar.gz
Algorithm Hash digest
SHA256 58f84496f93c2b342c01e1c95deb423dd0ba7f3c0f548fcfafd071eed5cda06f
MD5 a96129ffd95391149c1723a850f095c9
BLAKE2b-256 8412d19ea7fb072730fe97771d839bd183cf87f6e412b6a3cd3c253a9cb590c3

See more details on using hashes here.

File details

Details for the file fry_return-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fry_return-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for fry_return-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5cf0889e168e8b20adbdb8b32aabb40a2d702de79b00661f7faf4507aa012478
MD5 375ac9458123398846b329b2f22316b2
BLAKE2b-256 65237b0c411ff10e47ed0e527196fe2979e30b5b4f4e1e574e347163a489ae5a

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