-
Notifications
You must be signed in to change notification settings - Fork 31
Dynamically Add Extra Tags to User Object #27
Comments
It appears I can potentially just "redecorate" if needed, after checking if I need extra tags, but before using my custom object. This should work for me, but going to leave this open as this seems like potentially a terrible idea that could easily break :). |
Yeah, it's not really possible when writing user-defined classes (or raw lineprotocol). See this for more details:
I think the best thing I could do is update For future reference, here's a workaround you could try: Assuming you have a user-defined class like below: from aioinflux import *
from typing import NamedTuple
import time
@lineprotocol
class Foo(NamedTuple):
time: TIMEINT
x: INT
y: INT
print(Foo(time.time_ns(), 1, 2).to_lineprotocol())
# b'Foo x=1i,y=2i 1591019472699583000' You can "redecorate" your class by getting the arguments used in the first decoration from from copy import deepcopy
opts = deepcopy(Foo.to_lineprotocol.opts) # deep copy to avoid modyfing the original `extra_tags` dict)
opts['extra_tags'].update({'foo': 'boo'})
print(lineprotocol(Foo, **opts)(1, 2, 3).to_lineprotocol())
b'Foo,foo=boo x=2i,y=3i 1591020382248841000' Alternatively, in case you know then tag field key beforehand, you could make that a @lineprotocol(rm_none=True)
class Foo(NamedTuple):
time: TIMEINT
x: INT
mynullabletag: TAG = None
print(Foo(time.time_ns(), 1).to_lineprotocol())
# b'Foo x=1i 1591020531182752000'
print(Foo(time.time_ns(), 1, 'mytag').to_lineprotocol())
# b'Foo,mynullabletag=mytag x=1i 1591020544735611000' |
Is it possible to add extra tags to a user object directly from the the client's
.write()
method? I'd like to be able to add extra tags when callingclient.write(data, **extra_tags)
, but this appears to only be possible with a dictionary, not a user defined class.The text was updated successfully, but these errors were encountered: