I am looking for a way to call an instance method from an other class and I wondered if/how I could could do the following
goal and context
The class I am writing is a data formater whose __rshift__
method should be able to adapt the format of a data batch
to many data base clients without the user needing to change the method used. Here is how I would like the >>
method to be called
batch = SomeExtractor(config).extract("some")
db_client = InfluxDBClient()
# this would be awesome
DataFormater(batch)>>db_client.write_points
the DataFormater
class DataFormater(object):
def __init__(self, batch: Batch):
self.batch = batch
@abstractmethod
def __rshift__(self, db):
db_client = some_way_to_get_the_called_client(db)
method = some_way_to_get_the_method(db)
if method == InfluxDBClient.write_points:
db_client.write_points({"measurement": self.batch.origin,
"time" : self.batch.date,
"tags": dict(zip(self.batch.dimensions_names, row["dimensions"])),
"fields": dict(zip(self.batch.metrics_names, row["values"])}
for row in self.batch.rows))
Googling my problem, added stack = inspect.stack(db)
in the __rshift__
method and got the following but I am not sure how to use it
frame = <frame at 0x7f6c2a1515c0, file '/usr/src/collector/collector.py', line 95, code __rshift__>
context = <bound method InfluxDBClient.write_points of <collector.InfluxDBClient object at 0x7f6c2a131510>>
How can I do this?
Aucun commentaire:
Enregistrer un commentaire