Add Python Fire support (#550)

This commit is contained in:
eugen-ajechiloae-clearml
2022-02-07 13:38:11 +02:00
committed by GitHub
parent 9794135aed
commit 296cb7d899
9 changed files with 443 additions and 1 deletions

View File

@@ -0,0 +1,21 @@
# ClearML - Example of Python Fire integration, processing commands derived from a class
#
from clearml import Task
import fire
class BrokenCalculator(object):
def __init__(self, offset=1):
self._offset = offset
def add(self, x, y):
return x + y + self._offset
def multiply(self, x, y):
return x * y + self._offset
if __name__ == "__main__":
Task.init(project_name="examples", task_name="fire class command")
fire.Fire(BrokenCalculator)

View File

@@ -0,0 +1,23 @@
# ClearML - Example of Python Fire integration, with commands derived from a dictionary
#
from clearml import Task
import fire
def add(x, y):
return x + y
def multiply(x, y):
return x * y
if __name__ == "__main__":
Task.init(project_name="examples", task_name="fire dict command")
fire.Fire(
{
"add": add,
"multiply": multiply,
}
)

View File

@@ -0,0 +1,44 @@
# ClearML - Example of Python Fire integration, with commands grouped inside classes
#
from clearml import Task
import fire
class Other(object):
def status(self):
return "Other"
class IngestionStage(object):
def __init__(self):
self.other = Other()
def run(self):
return "Ingesting! Nom nom nom..."
def hello(self, hello_str):
return hello_str
class DigestionStage(object):
def run(self, volume=1):
return " ".join(["Burp!"] * volume)
def status(self):
return "Satiated."
class Pipeline(object):
def __init__(self):
self.ingestion = IngestionStage()
self.digestion = DigestionStage()
def run(self):
self.ingestion.run()
self.digestion.run()
if __name__ == "__main__":
Task.init(project_name="examples", task_name="fire grouping command")
fire.Fire(Pipeline)

View File

@@ -0,0 +1,22 @@
# ClearML - Example of Python Fire integration, processing multiple commands, when fire is initialized with no component
#
from clearml import Task
import fire
def hello(count, name="clearml", prefix="prefix_", suffix="_suffix", **kwargs):
for _ in range(count):
print("Hello %s%s%s!" % (prefix, name, suffix))
def serve(addr, port, should_serve=False):
if not should_serve:
print("Not serving")
else:
print("Serving on %s:%s" % (addr, port))
if __name__ == "__main__":
Task.init(project_name="examples", task_name="fire multi command")
fire.Fire()

View File

@@ -0,0 +1,19 @@
# ClearML - Example of Python Fire integration, with commands derived from an object
#
from clearml import Task
import fire
class Calculator(object):
def add(self, x, y):
return x + y
def multiply(self, x, y):
return x * y
if __name__ == "__main__":
Task.init(project_name="examples", task_name="fire object command")
calculator = Calculator()
fire.Fire(calculator)

View File

@@ -0,0 +1,15 @@
# ClearML - Example of Python Fire integration, with a single command passed to Fire
#
from clearml import Task
import fire
def hello(count, name="clearml", prefix="prefix_", suffix="_suffix", **kwargs):
for _ in range(count):
print("Hello %s%s%s!" % (prefix, name, suffix))
if __name__ == "__main__":
Task.init(project_name="examples", task_name="fire single command")
fire.Fire(hello)

View File

@@ -0,0 +1,2 @@
clearml
fire