Skip to content

Commit

Permalink
Update l2db.py and bump implementation version
Browse files Browse the repository at this point in the history
* Add try…except to try to save numbers as double even if they failed the checks in helper `num2bin`
  • Loading branch information
Lampe2020 committed Aug 14, 2023
1 parent 07af4ba commit 6465528
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ table below) and `key_type` being the key's type Identifier (see table below), o
| Whole number | `int` | Any positive or negative 64-bit whole number. (aka.`long`[^2]) If a positive number too large for a normal `long` is tried to assign, implicitly convert the key to a `uin` if that allows for storing the value, otherwise fail. |
| Positive whole number | `uin` | Any positive 64-bit whole number. (aka.`unsigned long`[^2]) If a negative number is tried to assign, implicitly convert the key to a `int` if that allows for storing the value, otherwise fail. |
| Floating point number | `flt` | Any positive or negative 64-bit number. (aka.`double`[^2]) <br>*Note that this will sooner or later be removed in favor of `fpn`* |
| Number | `fpn` | Any positive or negative 64-bit number, stored in a custom format. <br>*Note: `fpn` should currently automatically get converted to `flt` and strict implementations should emit a warning with the message `'fpn' is not implemented yet as there is no standard for it`* |
| Fixed point number | `fpn` | Any positive or negative 64-bit number, stored in a custom format. <br>*Note: `fpn` should currently automatically get converted to `flt` and strict implementations should emit a warning with the message `'fpn' is not implemented yet as there is no standard for it`* |
| Boolean | `bol` | True or False. Is stored in a single byte which is set to either 0x01 (True) or 0x00 (False). If a `int`, `uin` or `flt` 0 or 1 or raw `null`-byte (`\0`) or one-byte (`\1`) is tried to assign, implicitly convert the value to `True` for 1 and `False` for 0. If `null` is tried to assign, implicitly convert the key to `nul`.<br>*Note: in strict implementations the `DIRTY` bit should be set if this byte is anything other than 0x00 or 0x01!* |
| String | `str` | Any UTF-8 encoded string. |
| Raw | `raw` | Any sequence of bytes. |
Expand Down
13 changes: 8 additions & 5 deletions l2db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import _io # Only used for type hints

spec_version:str = '1.2.0' # See SPEC.md
implementation_version:str = '0.3.6-pre-alpha+python3-above-.7'
implementation_version:str = '0.3.7-pre-alpha+python3-above-.7'

__doc__:str = f"""
L2DB {spec_version} - implementation {implementation_version}
Expand Down Expand Up @@ -38,15 +38,15 @@ def __init__(self, db_ver:str='0.0.0-please+replace', imp_ver:str=spec_version)
class L2DBTypeError(L2DBError):
"""Raised when conversion between value types fails"""
def __init__(self, key:str='', vtype:str='inv') -> None: # Renamed `type` to `vtype`: `type` is a Python3-builtin
toreplace:tuple = ("'", "\\'")
toreplace:tuple[str] = ("'", "\\'")
self.message = f"Could not convert key '{key.replace(*toreplace)}' to type '{vtype.replace(*toreplace)}'" if (
key!=None) else f"Could not convert value to type '{vtype.replace(*toreplace)}'"
super().__init__(self.message)

class L2DBKeyError(L2DBError):
"""Raised when an unaccessible key is accessed"""
def __init__(self, key:str='') -> None:
toreplace = ("'", "\\'")
toreplace:tuple[str] = ("'", "\\'")
self.message = f"Key '{key.replace(*toreplace)}' could not be found"
super().__init__(self.message)

Expand Down Expand Up @@ -156,8 +156,11 @@ def num2bin(n:int|float, unsigned:bool=False) -> bytes:
elif (n>dblmax[0])and(dblmax[1]>n):
return struct.pack('>d', n)
else:
warnings.warn(f"L2DB helper num2bin(n): Failed to store {n} as float or double")
return b''
try:
return struct.pack('>d', n) # Just try it if it failed the tests but is possible
except (struct.error, OverflowError) as err:
warnings.warn(f"L2DB helper num2bin(n): Failed to store {n} as float or double")
return b''
case other:
warnings.warn(f"L2DB helper num2bin(n): 'n' is of type '{other}', must be a number")
return b''
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup
setup(
name='L2DB',
version='0.3.6-pre-alpha',
version='0.3.7-pre-alpha',
scripts=['l2db.py'],
install_requires=['semver~=3.0.0']
)

0 comments on commit 6465528

Please sign in to comment.