1

I am reading data from influxdb into pandas with the below code:-

import pandas as pd
from influxdb import InfluxDBClient
client = InfluxDBClient(host='10.0.0.0', port=7076, username='abcdefghi',
                        password='dsdsd', ssl=False, verify_ssl=False)
read_client.switch_database('test')

q = 'SELECT * FROM "abc"
df = pd.DataFrame(read_client.query(
       q, chunked=False, chunk_size=10000).get_points())
df

Then I am doing some processing.

Now, I have 50 domains and for some domains the influxdbclient is different. Now I want to do something so that it can take input from argpase and I can pass the read client on command line. I want to keep one common connecting string which will be default and if we pass --influx_host on command line then it should consider that. I am new to this kind of programming in python. I was able to achieve passing variables but not able to Create a connection string. I will appreciate any help.

Basically if I can do something like below or anything better.:-

python test.py -influx_host 10.0.0.10 --port 7076

1 Answers1

1

You should get arguments

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--influx_host', default='10.0.0.10')
parser.add_argument('--port', default=7076)
args = parser.parse_args()  # it will use values from `sys.argv` 

and use args.influx_host and args.port in code

client = InfluxDBClient(host=args.influx_host, port=args.port, ...)

And now you can run it with arguments

python test.py --influx_host 10.0.0.10 --port 7076

or without arguments

python test.py

or with only some of arguments

python test.py --influx_host 10.0.0.10
python test.py --port 7076

If you run without --influx_host and/or --port then it uses default values from .add_argument()


Not tested because I don't have influxdb but args shows expected values

import argparse
import pandas as pd
from influxdb import InfluxDBClient

# --- args ---

parser = argparse.ArgumentParser()
parser.add_argument('--influx_host', default='10.0.0.10')
parser.add_argument('--port', default=7076)
args = parser.parse_args()  # it will use values from `sys.argv`  
#args = parser.parse_args(['--influx_host', 'localhost', ]) # for tests

print(args)

# --- rest ---

client = InfluxDBClient(host=args.influx_host, port=args.port, username='abcdefghi',
                    password='dsdsd', ssl=False, verify_ssl=False)
read_client.switch_database('test')

q = 'SELECT * FROM "abc"'
df = pd.DataFrame(read_client.query(q, chunked=False, chunk_size=10000).get_points())
print(df)
furas
  • 134,197
  • 12
  • 106
  • 148