I need to return the output from multiple functions inside a class in a dictionary format
I have tried using Python.
dict={}
class Compute():
def vm(self):
for obj in data['profile']:
for region_name in obj['region']:
conn = boto3.resource('ec2', aws_access_key_id=obj["access_key"], aws_secret_access_key=obj["secret_key"],
region_name=region_name)
instances = conn.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running', 'stopped']}])
for instance in instances:
instance_count.append(instance)
instanceCount = str(len(instance_count))
dict['VM'] = len(instance_count)
#Subnet
def subnet(self):
subnet_count=0
for obj in data['profile']:
for region_name in obj['region']:
conn = boto3.client('ec2', aws_access_key_id=obj["access_key"], aws_secret_access_key=obj["secret_key"],
region_name=region_name)
subnet = conn.describe_subnets()
#print('subnet'+ ' '+ region_name + ' ' +str(len(subnet['Subnets'])))
subSize = len(subnet['Subnets'])
subnet_count+=subSize
dict['Networks'] = subnet_count
#VPCS
def vpc(self):
for obj in data['profile']:
for region_name in obj['region']:
conn = boto3.resource('ec2', aws_access_key_id=obj["access_key"], aws_secret_access_key=obj["secret_key"],
region_name=region_name)
vpcs = conn.vpcs.filter()
for vpc in vpcs:
vpc_count.append(vpc)
vpcCount = str(len(vpc_count))
dict['VPCs'] = len(vpc_count)
print(dict) #this only prints {}
def runcompute(self):
if __name__ == '__main__':
Thread(target=self.vm).start()
Thread(target=self.subnet).start()
Thread(target=self.vpc).start()
if __name__ == '__main__':
try:
if sys.argv[1]=='compute':
run = Compute()
run.runcompute()
"Now How to print the results in json/ dict format in the console. I expect out put in {"VM": 45, "VPCs": 23, "Networks": 35} format But it print {} but that is wrong."
dictonly exists within the methods of your class. This is not the samedictinsidevm()and the one declared at the top. Also you should really consider not using dict as a variable name since it's a python Keyword.