MCPcopy Create free account
hub / github.com/algorithmiaio/algorithmia-python / DataFile

Class DataFile

Algorithmia/datafile.py:18–190  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16
17
18class DataFile(DataObject):
19 def __init__(self, client, dataUrl):
20 super(DataFile, self).__init__(DataObjectType.file)
21 self.client = client
22 # Parse dataUrl
23 self.path = re.sub(r'^data://|^/', '', dataUrl)
24 self.url = '/v1/data/' + self.path
25 self.last_modified = None
26 self.size = None
27
28 def set_attributes(self, attributes):
29 self.last_modified = datetime.strptime(attributes['last_modified'], '%Y-%m-%dT%H:%M:%S.%fZ')
30 self.size = attributes['size']
31
32 # Deprecated:
33 def get(self):
34 return self.client.getHelper(self.url)
35
36 # Get file from the data api
37 def getFile(self, as_path=False):
38 exists, error = self.existsWithError()
39 if not exists:
40 raise DataApiError('unable to get file {} - {}'.format(self.path, error))
41 # Make HTTP get request
42 response = self.client.getHelper(self.url)
43 with tempfile.NamedTemporaryFile(delete=False) as f:
44 for block in response.iter_content(1024):
45 if not block:
46 break
47 f.write(block)
48 f.flush()
49 if as_path:
50 return f.name
51 else:
52 return open(f.name)
53
54 def getAsZip(self):
55 """Download/decompress file/directory and return path to file/directory.
56
57 Expects the `DataFile` object to contain a data API path pointing to a file/directory compressed with a zip-based compression algorithm.
58 Either returns the directory or a path to the file, depending on whether a directory or file was zipped.
59 """
60 local_file_path = self.getFile(as_path=True)
61 directory_path = tempfile.mkdtemp()
62 with zipfile.ZipFile(local_file_path, 'r') as ziph:
63 ziph.extractall(directory_path)
64 if len(ziph.namelist()) > 1:
65 output_path = directory_path
66 else:
67 filename = ziph.namelist()[0]
68 output_path = os.path.join(directory_path, filename)
69 return output_path
70
71 def getName(self):
72 _, name = getParentAndBase(self.path)
73 return name
74
75 def getBytes(self):

Callers 5

fileMethod · 0.90
_iterate_filesMethod · 0.90
test_set_attributesMethod · 0.90
test_putJson_getJsonMethod · 0.90

Calls

no outgoing calls

Tested by 3

test_set_attributesMethod · 0.72
test_putJson_getJsonMethod · 0.72