Skip to content

Instantly share code, notes, and snippets.

@tuulos
Created February 5, 2022 00:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuulos/1fb3b8d89686eddbf0c1b98267b34052 to your computer and use it in GitHub Desktop.
Save tuulos/1fb3b8d89686eddbf0c1b98267b34052 to your computer and use it in GitHub Desktop.
magic dir
from metaflow import FlowSpec, step
from functools import wraps
from functools import wraps
dir = 'mydir'
def magicdir(f):
artifact = 'magicdir'
@wraps(f)
def func(self):
from io import BytesIO
from tarfile import TarFile
existing = getattr(self, artifact, None)
if existing:
buf = BytesIO(existing)
with TarFile(mode='r', fileobj=buf) as tar:
tar.extractall()
f(self)
buf = BytesIO()
with TarFile(mode='w', fileobj=buf) as tar:
tar.add(dir)
setattr(self, artifact, buf.getvalue())
return func
class MagicDirFlow(FlowSpec):
@magicdir
@step
def start(self):
with open('mydir/output1', 'w') as f:
f.write('hello world')
with open('mydir/output2', 'w') as f:
f.write('hello world again')
self.next(self.end)
@magicdir
@step
def end(self):
print('first', open('mydir/output1').read())
print('second', open('mydir/output1').read())
if __name__ == "__main__":
MagicDirFlow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment