-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsample.py
110 lines (89 loc) · 2.57 KB
/
sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from pytile38.tilebase import (
TilePointBase,
TilePointSerializer
)
from pytile38.tilebase import (
Key,
Value
)
import json
# Creating a custom serializer for Tile commnands
class Serializer(object):
@classmethod
def is_valid(cls, response):
if response.result:
return True
return False
@classmethod
def get(cls, instance=None, response=None, id=None, **kwargs):
result = response.result
fields = result[1] if (isinstance(result, list) and len(result) > 1) else None
instance = instance()
lat, lon = 0.0, 0.0
if response.kwargs.get("withfields"):
instance.fields = [tuple(fields[i:i+2]) for i in range(0, len(fields), 2)]
result = result[0]
if response.kwargs.get("type") == "point":
lon, lat = list(map(float, result))
elif response.kwargs.get("type") == None:
lon, lat = list(map(float, result.get("coordinates")))
instance.lat = lat
instance.lon = lon
instance.id = id
return instance
@classmethod
def ids(cls, response=None, **kwargs):
data = response.result[1]
result = []
for d in data:
name, info = d[0:2]
name = name
result.append(name)
return result
@classmethod
def nearby(cls, response=None, **kwargs):
data = response.result[1]
result = []
for d in data:
name, info = d[0:2]
lat, lon = info.get("coordinates")[::-1]
coordinates = {"lat": lat, "long": lon}
item = {"name":name, "coordinates": coordinates}
result.append(item)
return result
# Models can be directly queried
# either by creating the object and using Tile command
# or using class instance for manual querying
class Shop(TilePointBase):
serializer = Serializer
identifier = Key("shop")
id = Value("")
lat = Value(0.0)
lon = Value(0.0)
class Customer(TilePointBase):
serializer = Serializer
identifier = Key("customer")
id = Value("")
lat = Value(0.0)
lon = Value(0.0)
class CustomerNeighbours(TilePointBase):
serializer = Serializer
identifier = Key("shop")
id = Value("")
lat = Value(0.0)
lon = Value(0.0)
def set(self, **kwargs):
raise NotImplementedError()
# override default implementations
def nearby(self, **kwargs):
result = {}
neighbours = super(CustomerNeighbours, self).nearby(**kwargs)
myinfo = dict(self)
result[myinfo.get("id")] = neighbours
return result
def geinfo(self):
result = {}
myinfo = dict(self)
result["name"] = myinfo.get("id")
result["coordinates"] = {"lat": myinfo.get("lat"), "long": myinfo.get("lon")}
return result