Add numpy target formatter.
@@ -71,44 +71,42 @@ class Formatter(object):
raise NotImplementedError
-class JSONFormatter(Formatter):
+class TextFormatter(Formatter):
- name = "json"
+ name = "text"
@classmethod
def accepts(cls, path):
- return get_path(path).endswith(".json")
+ return get_path(path).endswith(".txt")
@classmethod
def load(cls, path, *args, **kwargs):
with open(get_path(path), "r") as f:
- return json.load(f, *args, **kwargs)
+ return f.read()
@classmethod
- def dump(cls, path, obj, *args, **kwargs):
+ def dump(cls, path, content, *args, **kwargs):
with open(get_path(path), "w") as f:
- return json.dump(obj, f, *args, **kwargs)
+ f.write(content)
-class ROOTFormatter(Formatter):
+class JSONFormatter(Formatter):
- name = "root"
+ name = "json"
@classmethod
def accepts(cls, path):
- return get_path(path).endswith(".root")
+ return get_path(path).endswith(".json")
@classmethod
- @contextmanager
def load(cls, path, *args, **kwargs):
- import ROOT
+ with open(get_path(path), "r") as f:
+ return json.load(f, *args, **kwargs)
- tfile = ROOT.TFile.Open(get_path(path), *args, **kwargs)
- try:
- yield tfile
- finally:
- if tfile.IsOpen():
- tfile.Close()
+ @classmethod
+ def dump(cls, path, obj, *args, **kwargs):
+ with open(get_path(path), "w") as f:
+ return json.dump(obj, f, *args, **kwargs)
class ZipFormatter(Formatter):
@@ -207,5 +205,59 @@ class TarFormatter(Formatter):
f.add(os.path.join(src, elem), elem)
+class NumpyFormatter(Formatter):
+
+ name = "numpy"
+
+ @classmethod
+ def accepts(cls, path):
+ path = get_path(path)
+ return path.endswith(".npy") or path.endswith(".npz") or path.endswith(".txt")
+
+ @classmethod
+ def load(cls, path, *args, **kwargs):
+ import numpy as np
+
+ path = get_path(path)
+ func = np.loadtxt if path.endswith(".txt") else np.load
+ return func(path, *args, **kwargs)
+
+ @classmethod
+ def dump(cls, path, *args, **kwargs):
+ import numpy as np
+
+ path = get_path(path)
+
+ if path.endswith(".txt"):
+ func = np.savetxt
+ elif path.endswith(".npz"):
+ func = np.savez
+ else:
+ func = np.save
+
+ func(path, *args, **kwargs)
+
+
+class ROOTFormatter(Formatter):
+
+ name = "root"
+
+ @classmethod
+ def accepts(cls, path):
+ return get_path(path).endswith(".root")
+
+ @classmethod
+ @contextmanager
+ def load(cls, path, *args, **kwargs):
+ import ROOT
+
+ tfile = ROOT.TFile.Open(get_path(path), *args, **kwargs)
+ try:
+ yield tfile
+ finally:
+ if tfile.IsOpen():
+ tfile.Close()
+
+
# trailing imports
from law.target.file import get_path
