添加后端API和前端项目的初始目录结构,包括应用层模块、工具模块和模型定义 配置前后端的.dockerignore文件以排除开发环境文件 添加前端样式文件,包括字体、Tailwind CSS和主题配置 初始化Python虚拟环境中的类型标记文件
113 lines
2.9 KiB
Plaintext
113 lines
2.9 KiB
Plaintext
Metadata-Version: 2.4
|
|
Name: sentinels
|
|
Version: 1.1.1
|
|
Summary: Various objects to denote special meanings in python
|
|
Project-URL: Homepage, https://github.com/vmalloc/sentinels
|
|
Author-email: Rotem Yaari <vmalloc@gmail.com>
|
|
License: BSD 3-Clause License
|
|
License-File: LICENSE
|
|
Classifier: Programming Language :: Python :: 3.9
|
|
Classifier: Programming Language :: Python :: 3.10
|
|
Classifier: Programming Language :: Python :: 3.11
|
|
Classifier: Programming Language :: Python :: 3.12
|
|
Classifier: Programming Language :: Python :: 3.13
|
|
Requires-Python: >=3.9
|
|
Provides-Extra: testing
|
|
Requires-Dist: pylint; extra == 'testing'
|
|
Requires-Dist: pytest; extra == 'testing'
|
|
Description-Content-Type: text/markdown
|
|
|
|
# Overview
|
|
|
|
The sentinels module is a small utility providing the Sentinel class, along with useful instances.
|
|
|
|
# What are Sentinels?
|
|
|
|
Sentinels are objects with special meanings. They can be thought of as singletons, but they service the need of having 'special' values in your code, that have special meanings (see example below).
|
|
|
|
# Why Do I Need Sentinels?
|
|
|
|
Let's take *NOTHING* for example. This sentinel is automatically provided with the sentinels import:
|
|
|
|
```python
|
|
>>> from sentinels import NOTHING
|
|
|
|
```
|
|
|
|
Let's say you're writing a wrapper around a Python dictionary, which supports a special kind of method, *get_default_or_raise*. This method behaves like *get*, but when it does not receive a default and the key does not exist, it raises a *KeyError*. How would you implement such a thing? The naive method is this:
|
|
|
|
```python
|
|
>>> class MyDict(dict):
|
|
... def get_default_or_raise(self, key, default=None):
|
|
... if key not in self and default is None:
|
|
... raise KeyError(key)
|
|
... return self.get(key, default)
|
|
|
|
```
|
|
|
|
Or even this:
|
|
|
|
```python
|
|
>>> class MyDict(dict):
|
|
... def get_default_or_raise(self, key, default=None):
|
|
... returned = self.get(key, default)
|
|
... if returned is None:
|
|
... raise KeyError(key)
|
|
... return returned
|
|
|
|
```
|
|
|
|
But the problem with the above two pieces of code is the same -- when writing a general utility class, we don't know how it will be used later on. More importantly, **None might be a perfectly valid dictionary value!**
|
|
|
|
This is where NOTHING comes in handy:
|
|
|
|
```python
|
|
>>> class MyDict(dict):
|
|
... def get_default_or_raise(self, key, default=NOTHING):
|
|
... returned = self.get(key, default)
|
|
... if returned is NOTHING:
|
|
... raise KeyError(key)
|
|
... return returned
|
|
|
|
```
|
|
|
|
And Tada!
|
|
|
|
# Semantics
|
|
|
|
Sentinels are always equal to themselves:
|
|
|
|
```python
|
|
>>> NOTHING == NOTHING
|
|
True
|
|
|
|
```
|
|
|
|
But never to another object:
|
|
|
|
```python
|
|
>>> from sentinels import Sentinel
|
|
>>> NOTHING == 2
|
|
False
|
|
>>> NOTHING == "NOTHING"
|
|
False
|
|
|
|
```
|
|
|
|
Copying sentinels returns the same object:
|
|
|
|
```python
|
|
>>> import copy
|
|
>>> copy.deepcopy(NOTHING) is NOTHING
|
|
True
|
|
|
|
```
|
|
|
|
And of course also pickling/unpickling:
|
|
|
|
```python
|
|
>>> import pickle
|
|
>>> NOTHING is pickle.loads(pickle.dumps(NOTHING))
|
|
True
|
|
|
|
``` |