forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature.py
More file actions
208 lines (171 loc) · 5.31 KB
/
Copy pathfeature.py
File metadata and controls
208 lines (171 loc) · 5.31 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Copyright 2018 The Feast Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import enum
import json
import yaml
from google.protobuf.json_format import Parse
from feast.sdk.utils.print_utils import spec_to_yaml
from feast.specs.FeatureSpec_pb2 import FeatureSpec
class ValueType(enum.Enum):
"""
Type of the feature's value
"""
UNKNOWN = 0
BYTES = 1
STRING = 2
INT32 = 3
INT64 = 4
DOUBLE = 5
FLOAT = 6
BOOL = 7
TIMESTAMP = 8
class Feature:
"""
Wrapper class for feast feature
"""
def __init__(self,
name='',
entity='',
owner='',
value_type=ValueType.DOUBLE,
description='',
uri='',
group='',
tags=[],
options={}):
"""Create feast feature instance.
Args:
name (str): name of feature, in lower snake case
entity (str): entity the feature belongs to, in lower case
owner (str): owner of the feature
value_type (feast.sdk.resources.feature.ValueType): defaults to
ValueType.DOUBLE. value type of the feature
description (str): defaults to "". description of the feature
uri (str): defaults to "". uri pointing to the source code or
origin of this feature
group (str, optional): feature group to inherit from
tags (list[str], optional): tags assigned to the feature
options (dic, optional): additional options for the feature
"""
id = '{}.{}'.format(entity, name).lower()
self.__spec = FeatureSpec(
id=id,
name=name,
entity=entity,
owner=owner,
description=description,
uri=uri,
valueType=value_type.value,
group=group,
tags=tags,
options=options)
@property
def spec(self):
return self.__spec
@property
def id(self):
return self.__spec.id
@property
def name(self):
return self.__spec.name
@name.setter
def name(self, value):
self.__spec.name = value
id_split = self.id.split('.')
id_split[1] = value
self.__spec.id = '.'.join(id_split)
@property
def entity(self):
return self.__spec.entity
@entity.setter
def entity(self, value):
self.__spec.entity = value
id_split = self.id.split('.')
id_split[0] = value
self.__spec.id = '.'.join(id_split)
@property
def owner(self):
return self.__spec.owner
@owner.setter
def owner(self, value):
self.__spec.owner = value
@property
def description(self):
return self.__spec.description
@description.setter
def description(self, value):
self.__spec.description = value
@property
def uri(self):
return self.__spec.uri
@uri.setter
def uri(self, value):
self.__spec.uri = value
@property
def value_type(self):
return ValueType(self.__spec.valueType)
@value_type.setter
def value_type(self, value):
self.__spec.valueType = value
@property
def group(self):
return self.__spec.group
@group.setter
def group(self, value):
self.__spec.group = value
@property
def tags(self):
return self.__spec.tags
@tags.setter
def tags(self, value):
del self.__spec.tags[:]
self.__spec.tags.extend(value)
@property
def options(self):
return self.__spec.options
@options.setter
def options(self, value):
for key in self.__spec.options:
del self.__spec.options[key]
for (key, value) in value.items():
self.__spec.options[key] = value
@classmethod
def from_yaml(cls, path):
"""Create an instance of feature from a yaml spec file
Args:
path (str): path to yaml spec file
"""
with open(path, 'r') as file:
content = yaml.safe_load(file.read())
feature = cls()
feature.__spec = Parse(
json.dumps(content),
FeatureSpec(),
ignore_unknown_fields=False)
return feature
def __str__(self):
"""Print the feature in yaml format
Returns:
str: yaml formatted representation of the entity
"""
return spec_to_yaml(self.__spec)
def dump(self, path):
"""Dump the feature into a yaml file.
It will replace content of an existing file.
Args:
path (str): destination file path
"""
with open(path, 'w') as file:
file.write(str(self))
print("Saved spec to {}".format(path))