How to implement postresql ranges #907
Answered
by
joachimhuet
DanielAndreasen
asked this question in
Questions
-
First Check
Commit to Help
Example Codefrom typing import Optional, Tuple
from sqlalchemy.dialects.postgresql import NUMRANGE
from sqlmodel import Column, Field, SQLModel
class Example(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
required_field: bool = Field(default=False)
window: Tuple[float, float] = Field(default=None, sa_column=Column(NUMRANGE))
ex = Example(name="Test", window=(1, 2))
print(1.5 in ex.window) # Returns False, since my range is just a tuple DescriptionMy underlying postgresql database contain a Hod do I go about implementing this? Operating SystemLinux Operating System DetailsUp to date debian SQLModel Version0.0.16 Python VersionPython 3.12.0 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
joachimhuet
Apr 26, 2024
Replies: 1 comment 1 reply
-
Well for that you will need to create your own class NumRange and customize it for your usage. class NumRange:
def __init__(self, lower: Optional[int] = None, upper: Optional[int] = None, bounds: str = '[]'):
assert not (lower is None and upper is None), 'Both lower and upper bounds cannot be None'
assert bounds in ['[]', '()', '(]', '[)'], 'Invalid bounds'
self.lower = lower
self.upper = upper
self.bounds = bounds
def __repr__(self):
return f'NumRange({self.lower}, {self.upper}, {self.bounds})'
def __str__(self):
return f'{self.bounds[0]}{self.lower if self.lower else ""};{self.upper if self.upper else ""}{self.bounds[1]}'
def __eq__(self, other):
return isinstance(other, NumRange) and \
self.lower == other.lower and \
self.upper == other.upper and \
self.bounds == other.bounds
def __contains__(self, value):
if self.lower is None:
if self.bounds[1] == ']':
return value <= self.upper
if self.bounds[1] == ')':
return value < self.upper
if self.upper is None:
if self.bounds[0] == '[':
return self.lower <= value
if self.bounds[0] == '(':
return self.lower < value
if self.bounds[0] == '[' and self.bounds[1] == ']':
return self.lower <= value <= self.upper
if self.bounds[0] == '[' and self.bounds[1] == ')':
return self.lower <= value < self.upper
if self.bounds[0] == '(' and self.bounds[1] == ']':
return self.lower < value <= self.upper
if self.bounds[0] == '(' and self.bounds[1] == ')':
return self.lower < value < self.upper
return False
# ex:
myrange = NumRange(1, None, "[)")
0.5 in myrange # False
1 in myrange # True
15324 in myrange # True
myrange = NumRange(None, 1, "[)")
0.5 in myrange # True
1 in myrange # False
-123456 in myrange # True
myrange = NumRange(1, 10, "[)")
0.5 in myrange # False
1 in myrange # True
5 in myrange # True
10 in myrange # False |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
DanielAndreasen
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well for that you will need to create your own class NumRange and customize it for your usage.
Here is a quick example of what you could do: