3

I am leveraging the SeriesHelper object of InfluxDB library(please have a look at https://influxdb-python.readthedocs.io/en/latest/examples.html#tutorials-serieshelper) to push set of data points to InfluxDB. The SeriesHelper class has to be inherited and the child class needs to initialize various objects as its meta attributes, so as to override the default values of the objects in the Parent class.

Actual code

class MySeriesHelper(SeriesHelper):
    """Instantiate SeriesHelper to write points to the backend."""

    class Meta:
        """Meta class stores time series helper configuration."""
        client = myclient
        series_name = 'rf_results'
        fields = ['some_stat', 'other_stat']
        tags = ['server_name']
        bulk_size = 5
        autocommit = True

Here the 'series_name' object is initialized(hard-coded) right before it is ran as a script. My use case is to initialize 'series_name' based on the runtime arguments that are passed to this script. I tried by defining a global variable whose value is providing at runtime and assigning that global variable to the 'series_name' like the below one, but in vain.

Problematic code

series_configured = None
class MySeriesHelper(SeriesHelper):
    """Instantiate SeriesHelper to write points to the backend."""

    class Meta:
        """Meta class stores time series helper configuration."""
        client = myclient
        series_name = series_configured
        fields = ['some_stat', 'other_stat']
        tags = ['server_name']
        bulk_size = 5
        autocommit = True

def main():
    global series_configured
    series_configured = args.series_name

    MySeriesHelper(server_name='server_A', some_stat='Master server', other_stat='Controller')
    MySeriesHelper.commit()

if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument("--series_name", dest='series_name', 
                        help="The measurement to be used for storing the data points",required=True)
    args = parser.parse_args()
    main()

Error seen while running is

'NoneType' object has no attribute 'format'

It infers that the object 'series_name' is not initialized with a value. Is there any way of properly initializing it ?

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Ashwin Kanna
  • 75
  • 1
  • 6

1 Answers1

1

When the python interpreter go over the code (line by line) it define all the classes static variable. It's set static variable before you create an instance from a class. That mean when you reach the point of:

autocommit = True

The value of series_name is already set to None (because that is the value of series_configured at the point).

The following example show that the static variable are already set before I created an instance:

>>> series_configured = None
>>> class MySeriesHelper:
    """Instantiate SeriesHelper to write points to the backend."""

    class Meta:
        """Meta class stores time series helper configuration."""
        series_name = series_configured
        fields = ['some_stat', 'other_stat']
        tags = ['server_name']
        bulk_size = 5
        autocommit = True

>>> print(MySeriesHelper.Meta.series_name)
None

If you want to change the Meta.series_configured static variable you will have to set it after the series_configured change its content.

Try the following main.

def main():
    global series_configured
    series_configured = args.series_name
    # The following line will set the variable at the inner Meta class.
    MySeriesHelper.Meta.series_name = series_configured

    MySeriesHelper(server_name='server_A', some_stat='Master server', other_stat='Controller')
    MySeriesHelper.commit()
Amiram
  • 1,227
  • 6
  • 14