MCPcopy Index your code
hub / github.com/algorithmiaio/algorithmia-python / LocalDataFile

Class LocalDataFile

Algorithmia/datafile.py:193–272  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

191
192
193class LocalDataFile():
194 def __init__(self, client, filePath):
195 self.client = client
196 # Parse dataUrl
197 self.path = filePath.replace('file://', '')
198 self.url = '/v1/data/' + self.path
199 self.last_modified = None
200 self.size = None
201
202 def set_attributes(self, attributes):
203 self.last_modified = datetime.strptime(attributes['last_modified'], '%Y-%m-%dT%H:%M:%S.%fZ')
204 self.size = attributes['size']
205
206 # Get file from the data api
207 def getFile(self):
208 exists, error = self.existsWithError()
209 if not exists:
210 raise DataApiError('unable to get file {} - {}'.format(self.path, error))
211 return open(self.path)
212
213 def getName(self):
214 _, name = getParentAndBase(self.path)
215 return name
216
217 def getBytes(self):
218 exists, error = self.existsWithError()
219 if not exists:
220 raise DataApiError('unable to get file {} - {}'.format(self.path, error))
221 f = open(self.path, 'rb')
222 bts = f.read()
223 f.close()
224 return bts
225
226 def getString(self):
227 exists, error = self.existsWithError()
228 if not exists:
229 raise DataApiError('unable to get file {} - {}'.format(self.path, error))
230 with open(self.path, 'r') as f: return f.read()
231
232 def getJson(self):
233 exists, error = self.existsWithError()
234 if not exists:
235 raise DataApiError('unable to get file {} - {}'.format(self.path, error))
236 return json.loads(open(self.path, 'r').read())
237
238 def exists(self):
239 return self.existsWithError()[0]
240
241 def existsWithError(self):
242 return os.path.isfile(self.path), ''
243
244 def put(self, data):
245 # First turn the data to bytes if we can
246 if isinstance(data, six.string_types) and not isinstance(data, six.binary_type):
247 data = bytes(data.encode())
248 with open(self.path, 'wb') as f: f.write(data)
249 return self
250

Callers 2

fileMethod · 0.90
fileMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected