Skip to content

A "null-safe-like" approach to Python programming language.

License

Notifications You must be signed in to change notification settings

gus-caribe/voidsafe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VoidSafe

     VoidSafe is a package that brings a null-safe-like approach to Python3-written scripts and applications. It provides software developers with resources to apply void-safety principles to their codebase, allowing coalescing operations to be performed on Potentially Unsafe Instances [2.1] , which helps assuring that your software will be able to handle many memory-access problems at runtime.

     By being entirely type-hinted, this package works well when associated with most static type checkers. When combined with a linter that's capable of predicting unboud and undefined variable problems, VoidSafe helps developers to identify problems with their code even before running it.

Inspiration

     The inspiration for this package comes from the concept of Null Safety, which has been adopted by several programming languages, such as Dart, Kotlin and C#. Since the core developers have shown little interest in implementing those principles canonically, the opportunity came in as a great challenge.

     Python doesn't exactly adopt the concept of null. One can argue that None serves for the same purpose, but since Python doesn't handle namespaces by '{blocks}', the None constant doesn't actually cover every case where a given instance can't be used for operations. For instance, Python allows running scripts containing variables that have never been defined or assigned with absolutely no warnings.

     Because there's support for changing local variables dynamically, voidsafe can't protect your code from undefined and unboud instances, but it can protect you from accessing Potentially Unsafe Resources [2.2] by returning a Special Constant [1.3] named Undefined whenever you try to access an '.attribute', '[item]' or 'method()' that hasn't been defined.

     That's where Void Special Constant comes in. It unites the concept of None and Undefined into a single constant, allowing developers to check if a resource is ready to be used for further operations.

Ok, but... Why bother?

Why wearing a seatbelt when you can just drive safely, right?

     It is known that many professionals use Python as a practical tool, a means to achieve a result where the result holds the business value intended. Data scientists, mathmaticians and physicists often use Python as a "fancy calculator" or a "plotting tool" to elaborate and extract valuable data and information. It's easy to see that this library won't be able to contribute much to those use cases, it would probably only lead to a processing overhed during execution.

     But when it comes to Software Engineering, part of the business value is delivered in the form of code quality, maintainability and service availability, which can't be offered if your project is full of lines of code craving to raise Errors and Exceptions. That is true especially for server-side applications that are supposed to run 24/7 interacting with third-party services. This library intends to help software developers building up robust and reliable applications with Python3.

Installation

     VoidSafe is on PyPi. You can install its latest version via pip install as follows:

pip install voidsafe

Examples

     Take, for instance, the following Dart code snippet:

     See this article about Dart's null-aware operators.

void main() {
    Map<String, String?> unsafeDict = {'item': null};
    String safeStr = '\t safeStr #1';

    safeStr += unsafeDict['item'] ?? '\t safeStr #2';
    safeStr += unsafeDict['item']?.toUpperCase() ?? '\t safeStr #3';

    unsafeDict['item'] ??= '\t unsafeDict #1';
    unsafeDict['item'] ??= '\t unsafeDict #2';

    safeStr += unsafeDict['item'] ?? '\t safeStr #4';
    safeStr += unsafeDict['item']?.toUpperCase() ?? '\t safeStr #5';

    print(safeStr);
}

     With the help of voidsafe, the code above could be similarly brought up to Python like this:

from voidsafe import VoidSafe, ifvoid, value
from typing import Optional

unsafeDict: dict[str, Optional[str]] = {'item': None}
safeStr: str = '\t safeStr #1'

safeStr += unsafeDict['item'] <<ifvoid>> '\t safeStr #2'
safeStr += VoidSafe(unsafeDict['item']).upper() <<ifvoid>> '\t safeStr #3'

VoidSafe(unsafeDict)['item'] = ifvoid << '\t unsafeDict #1'
VoidSafe(unsafeDict)['item'] = ifvoid << '\t unsafeDict #2'

safeStr += unsafeDict['item'] <<ifvoid>> '\t safeStr #4'
safeStr += VoidSafe(unsafeDict['item']).upper() <<ifvoid>> '\t safeStr #5'

print(safeStr)

     By running any of the scripts above, you will get the exact same output:

    safeStr #1	 safeStr #2	 safeStr #3	 unsafeDict #1	 UNSAFEDICT #1

     You can see usage examples all over this project. This repository provides the API.md file, which explores barely every possible use for this package's public resources.

     There's a directory named /examples which contains commented scripts that can be modified and executed in order to understand the implications that an event that's "taken for granted" will result in the output.

     Besides that, voidsafe module's core.py implements docstrings with examples for basically every declaration that's coded inside it, even the private ones.

     A Wiki is under construction at Caribe's Docs and it will be released soon. It will help developers to understand the concepts introduced by this library.

Support the Project

Contributing

     This project is being maintained by a sole developer DevTeam. For now, there are no resources available to keep a frequent development and code review routine to guarantee that the code pulled by other developers won't compromise the library's principles.

     Despite that, you can help this project by giving a donation. That way, this one-person DevTeam can dedicate its time and effort to collaborate with the community by developing free and open-source software.

Donating

     If this package has helped adding value to your software or if you appreciated the initiative, consider giving a donation to help this project keep being maintained and improved.

     When submitting a donation, you can leave a comment telling us how this package has helped you and what could be done to make it even better. That would be much appreciated.

  • PayPal
    paypal
  • Buy me a Coffee
    "Buy Me A Coffee"

...