Python Forum
Statically checking physical units in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Statically checking physical units in Python
#1
Lightbulb 
Hi everyone !

This is my first post here so I hope I am not posting things in the wrong category.

I wanted to present to you the new library I programmed called impunity.
It will hunt physical annotations in the Python AST and check if there are any inconsistencies between physical units.
It simply uses a decorator for the checks for now.

The code is here.

It works like that :

@impunity
def speed(d: "m", t: "s") -> "m / s":
   return d / t

speed(10, 10) # returns 1

@impunity
def speed(d: "m", t: "s") -> "km / h":
   return d / t

speed(10, 10) # returns 3.6
And will raise an error when conversions are not possible.
It uses the Pint library to get the conversions but will have almost no overhead since the checks are statics.

I really look forward to have some people giving feedbacks and / or contribute if this library can help them.

Have a great day ! Heart
buran write Jul-24-2023, 04:22 AM:
Thread moved to Code sharing section.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020