Skip to content

Commit a00e698

Browse files
authored
refactor models (#339)
1 parent fe71bd8 commit a00e698

17 files changed

+152
-147
lines changed

fakeredis/_basefakesocket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import redis
99
from redis.connection import DefaultParser
1010

11+
from fakeredis.model import XStream
12+
from fakeredis.model import ZSet
1113
from . import _msgs as msgs
1214
from ._command_args_parsing import extract_args
1315
from ._commands import Int, Float, SUPPORTED_COMMANDS, COMMANDS_WITH_SUB, Signature, CommandItem, Hash
@@ -21,8 +23,6 @@
2123
QUEUED,
2224
decode_command_bytes,
2325
)
24-
from ._stream import XStream
25-
from ._zset import ZSet
2626

2727

2828
def _extract_command(fields: List[bytes]) -> Tuple[Any, List[Any]]:

fakeredis/commands_mixins/generic_mixin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Hash,
1818
)
1919
from fakeredis._helpers import compile_pattern, SimpleError, OK, casematch, Database, SimpleString
20-
from fakeredis._zset import ZSet
20+
from fakeredis.model import ZSet
2121

2222

2323
class GenericCommandsMixin:

fakeredis/commands_mixins/geo_mixin.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from fakeredis._command_args_parsing import extract_args
77
from fakeredis._commands import command, Key, Float, CommandItem
88
from fakeredis._helpers import SimpleError, Database
9-
from fakeredis._zset import ZSet
10-
from fakeredis.geo import geohash
11-
from fakeredis.geo.haversine import distance
9+
from fakeredis.model import ZSet
10+
from fakeredis.geo import distance, geo_encode, geo_decode
11+
1212

1313
UNIT_TO_M = {"km": 0.001, "mi": 0.000621371, "ft": 3.28084, "m": 1}
1414

@@ -71,7 +71,7 @@ def _find_near(
7171
"""
7272
results = list()
7373
for name, _hash in zset.items():
74-
p_lat, p_long, _, _ = geohash.decode(_hash)
74+
p_lat, p_long, _, _ = geo_decode(_hash)
7575
dist = distance((p_lat, p_long), (lat, long)) * conv
7676
if dist < radius:
7777
results.append(GeoResult(name, p_long, p_lat, _hash, dist))
@@ -120,7 +120,7 @@ def geoadd(self, key: CommandItem, *args: bytes) -> int:
120120
data[i + 2],
121121
)
122122
if (name in zset and not xx) or (name not in zset and not nx):
123-
if zset.add(name, geohash.encode(lat, long, 10)):
123+
if zset.add(name, geo_encode(lat, long, 10)):
124124
changed_items += 1
125125
if changed_items:
126126
key.updated()
@@ -137,7 +137,7 @@ def geohash(self, key: CommandItem, *members: bytes) -> List[bytes]:
137137
@command(name="GEOPOS", fixed=(Key(ZSet), bytes), repeat=(bytes,))
138138
def geopos(self, key: CommandItem, *members: bytes) -> List[Optional[List[bytes]]]:
139139
gospositions = map(
140-
lambda x: geohash.decode(x) if x is not None else x,
140+
lambda x: geo_decode(x) if x is not None else x,
141141
map(key.value.get, members),
142142
)
143143
res = [
@@ -158,7 +158,7 @@ def geodist(self, key: CommandItem, m1: bytes, m2: bytes, *args: bytes) -> Optio
158158
geohashes = [key.value.get(m1), key.value.get(m2)]
159159
if any(elem is None for elem in geohashes):
160160
return None
161-
geo_locs = [geohash.decode(x) for x in geohashes]
161+
geo_locs = [geo_decode(x) for x in geohashes]
162162
res = distance((geo_locs[0][0], geo_locs[0][1]), (geo_locs[1][0], geo_locs[1][1]))
163163
unit = translate_meters_to_unit(args[0]) if len(args) == 1 else 1
164164
return res * unit
@@ -236,13 +236,13 @@ def georadius(self, key: CommandItem, long: float, lat: float, radius: float, *a
236236
@command(name="GEORADIUSBYMEMBER", fixed=(Key(ZSet), bytes, Float), repeat=(bytes,))
237237
def georadiusbymember(self, key: CommandItem, member_name: bytes, radius: float, *args: bytes):
238238
member_score = key.value.get(member_name)
239-
lat, long, _, _ = geohash.decode(member_score)
239+
lat, long, _, _ = geo_decode(member_score)
240240
return self.georadius(key, long, lat, radius, *args)
241241

242242
@command(name="GEORADIUSBYMEMBER_RO", fixed=(Key(ZSet), bytes, Float), repeat=(bytes,))
243243
def georadiusbymember_ro(self, key: CommandItem, member_name: bytes, radius: float, *args: float) -> List[Any]:
244244
member_score = key.value.get(member_name)
245-
lat, long, _, _ = geohash.decode(member_score)
245+
lat, long, _, _ = geo_decode(member_score)
246246
return self.georadius_ro(key, long, lat, radius, *args)
247247

248248
@command(name="GEOSEARCH", fixed=(Key(ZSet),), repeat=(bytes,))

fakeredis/commands_mixins/sortedset_mixin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
null_terminate,
2727
Database,
2828
)
29-
from fakeredis._zset import ZSet
29+
from fakeredis.model import ZSet
3030

3131
SORTED_SET_METHODS = {
3232
"ZUNIONSTORE": lambda s1, s2: s1 | s2,

fakeredis/commands_mixins/streams_mixin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from fakeredis._command_args_parsing import extract_args
66
from fakeredis._commands import Key, command, CommandItem, Int
77
from fakeredis._helpers import SimpleError, casematch, OK, current_time, Database, SimpleString
8-
from fakeredis._stream import XStream, StreamRangeTest, StreamGroup, StreamEntryKey
8+
from fakeredis.model import XStream, StreamRangeTest, StreamGroup, StreamEntryKey
99

1010

1111
class StreamsCommandsMixin:

fakeredis/geo/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from .geohash import geo_encode, geo_decode
2+
from .haversine import distance
3+
4+
__all__ = [
5+
"geo_encode",
6+
"geo_decode",
7+
"distance",
8+
]

fakeredis/geo/geohash.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
decodemap = {base32[i]: i for i in range(len(base32))}
88

99

10-
def decode(geohash: str) -> Tuple[float, float, float, float]:
10+
def geo_decode(geohash: str) -> Tuple[float, float, float, float]:
1111
"""
12-
Decode the geohash to its exact values, including the error
13-
margins of the result. Returns four float values: latitude,
14-
longitude, the plus/minus error for latitude (as a positive
15-
number) and the plus/minus error for longitude (as a positive
16-
number).
12+
Decode the geohash to its exact values, including the error margins of the result. Returns four float values:
13+
latitude, longitude, the plus/minus error for latitude (as a positive number) and the plus/minus error for longitude
14+
(as a positive number).
1715
"""
1816
lat_interval, lon_interval = (-90.0, 90.0), (-180.0, 180.0)
1917
lat_err, lon_err = 90.0, 180.0
@@ -51,10 +49,10 @@ def decode(geohash: str) -> Tuple[float, float, float, float]:
5149
return lat, lon, lat_err, lon_err
5250

5351

54-
def encode(latitude: float, longitude: float, precision: int = 12) -> str:
52+
def geo_encode(latitude: float, longitude: float, precision: int = 12) -> str:
5553
"""
56-
Encode a position given in float arguments latitude, longitude to
57-
a geohash which will have the character count precision.
54+
Encode a position given in float arguments latitude, longitude to a geohash which will have the character count
55+
precision.
5856
"""
5957
lat_interval, lon_interval = (-90.0, 90.0), (-180.0, 180.0)
6058
geohash, bits = [], [16, 8, 4, 2, 1] # type: ignore

fakeredis/geo/haversine.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,6 @@
22
from typing import Tuple
33

44

5-
# class GeoMember:
6-
# def __init__(self, name: bytes, lat: float, long: float):
7-
# self.name = name
8-
# self.long = long
9-
# self.lat = lat
10-
#
11-
# @staticmethod
12-
# def from_bytes_tuple(t: Tuple[bytes, bytes, bytes]) -> 'GeoMember':
13-
# long = Float.decode(t[0])
14-
# lat = Float.decode(t[1])
15-
# name = t[2]
16-
# return GeoMember(name, lat, long)
17-
#
18-
# def geohash(self):
19-
# return geohash.encode(self.lat, self.long)
20-
21-
225
def distance(origin: Tuple[float, float], destination: Tuple[float, float]) -> float:
236
"""Calculate the Haversine distance in meters."""
247
radius = 6372797.560856 # Earth's quatratic mean radius for WGS-84

fakeredis/model/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from ._stream import XStream, StreamEntryKey, StreamGroup, StreamRangeTest
2+
from ._timeseries_model import TimeSeries, TimeSeriesRule, AGGREGATORS
3+
from ._topk import HeavyKeeper
4+
from ._zset import ZSet
5+
6+
__all__ = [
7+
"XStream",
8+
"StreamRangeTest",
9+
"StreamGroup",
10+
"StreamEntryKey",
11+
"ZSet",
12+
"TimeSeries",
13+
"TimeSeriesRule",
14+
"AGGREGATORS",
15+
"HeavyKeeper",
16+
]
File renamed without changes.

0 commit comments

Comments
 (0)