Skip to main content

Panyc

Super simple python package to solve 2 problems:

  • In Python assert is not guaranteed to work (see the -O flag)
  • The very convenient todo()/unreachable()/... functions to panic in case reached, are missing

Usage

from panyc import raise_if

def is_even(x):
	raise_if(type(x) != int, f"expected an integer, got {type(x)}")
	return x % 2 == 0

def main():
	x = 6.9
	print("Is", x, "even?", is_even(x))

main()

Will result in AssertionError: /path/to/file/example.py:4: expected an integer, got <class 'float'>

A couple of notice:

  • first of all the condition must be true to raise, in the contrary of assert where it must be false
  • secondly it raise an AssertionError as well, so the error handling remain the same

So if you plan to rewrite your asserts you have to change the condition and nothing else

from panyc import todo

def end_world_hungry():
	todo()

def main():
	end_world_hungry()

main()

Will result in NotImplementedError: /path/to/file/example.py:4: Function "end_world_hungry" not implemented

from panyc import unreachable

def sound(x):
	if x == "cat":
		return "meow"
	elif x == "dog":
		return "woof"
	elif x == "duck":
		return "quack"
	unreachable() # we do not expect other animals

def main():
	print(sound("crocodile"))

main()

Will result in NotImplementedError: /path/to/file/example.py:10: Reached unreachable code in "sound"

Installation

Just copy the only file panyc.py in your project.

If you like the slop, there's also the package on pypi:

$ pip install panyc

Release files for panyc 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for panyc 1.0.0
File Size Uploaded
panyc-1.0.0.tar.gz 2.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for panyc 1.0.0
File Interpreter ABI Platform
panyc-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 6.5 kB

Release files / panyc-1.0.0.tar.gz

Download URL panyc-1.0.0.tar.gz
Size 2.7 kB
Tags Source
SHA-256 checksum
How to use checksums
67084af566e6667d38e408e7daf637e37bb816deced97247a688346170aec268
BLAKE2b-256 checksum
How to use checksums
0a7d4f58e57ed8e174748f60d0adb78dfd88e72dcc1a9f57f5d15126d615bb14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / panyc-1.0.0-py3-none-any.whl

Download URL panyc-1.0.0-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c09c495ea2d8d3f1a316282fbe2309381867eecb5f62d1aa21a7604aa0ccbc81
BLAKE2b-256 checksum
How to use checksums
7d7a28fe73867551185b07f18515cd3c5fcdd04bbd0eab479c4e99286316f1fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page