python - 如何在 Python 3 的内部类中动态初始化对象?

标签 python influxdb-python

我正在利用 InfluxDB 库的 SeriesHelper 对象(请查看 https://influxdb-python.readthedocs.io/en/latest/examples.html#tutorials-serieshelper)将数据点集推送到 InfluxDB。 SeriesHelper类需要被继承,子类需要初始化各种对象作为它的元属性,以覆盖父类中对象的默认值。

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

此处 'series_name' 对象在作为脚本运行之前被初始化(硬编码)。我的用例是根据传递给此脚本的运行时参数来初始化 'series_name'。 我尝试定义一个全局变量,其值在运行时提供,并将该全局变量分配给“series_name”,如下所示,但没有成功。

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()

运行时出现的错误是

'NoneType' object has no attribute 'format'

它推断对象 'series_name' 没有用值初始化。有什么方法可以正确初始化它吗?

最佳答案

python interpreter检查代码(逐行)它定义了所有类的静态变量。 在从类创建实例之前设置静态变量。 这意味着当您达到以下程度时:

autocommit = True

series_name 的值已经设置为 None(因为那是此时 series_configured 的值)。

以下示例显示在我创建实例之前已经设置了 static 变量:

>>> 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

如果您想更改 Meta.series_configured static 变量,您必须在 series_configured 更改其内容后设置它。

尝试以下主要内容。

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()

关于python - 如何在 Python 3 的内部类中动态初始化对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59540941/

相关文章:

python - 如何使用 Django 分离测试类型

Python:展平函数在控制台中有效但在文件中无效?

python - 在 Python influxdb_client 的 Flux 查询中使用参数

python - 如何使用Python迭代influxDB结果

python - 尝试打印页面显示奇怪的布局

python - Python 中 for 循环和打印列表的顺序是什么?

python - 无法将列表中的值写入 csv python

python - 如何使用 python 客户端将大量行从 InfluxDB 导出到 CSV?

python - 如何在 BigQuery Web UI 中创建隐藏数据集,同时保持数据工作室连接可用?