Reputation: 746
I use the dictionary at the very bottom to call a function with user input.
I want use a dictionary, list or any other solution to have the user to input the value for rds_cluster_name
. This is the line in question DBClusterIdentifier = rds_cluster_name.input("RDS cluster name: ")
When I do the above I get the error AttributeError: 'dict' object has no attribute 'input'
.
So I run the script and a value from here must be inputted funct_dict = {'start':start, 'stop':stop, 'status':status}
, if the function status
is called, the cluster name must be passed.
I can do this when I declare the variable rds_cluster_name = input("RDS Cluster: ")
and it will work, but they have to know the cluster name, I want users to see a list or something like this Choose cluster_name ('staging-cluster':1,'qa-cluster-1':2,'int1-cluster':3)
and they just to type down the number which correspond to the cluster name to be used.
How Can I accomplish this?.
rds_cluster_name = {'staging-cluster':1,'qa-cluster-1':2,'int1-cluster':3}
def status():
rds_client = boto3.client('rds')
response = rds_client.describe_db_clusters(
DBClusterIdentifier = rds_cluster_name.input("RDS cluster name: "),
MaxRecords=20,
)
status = response['DBClusters'][0]['Status']
status = status.upper()
print(f' RDS cluster "{rds_cluster_name}" is: {status}')
funct_dict = {'start':start, 'stop':stop, 'status':status}
if __name__ == "__main__":
command = input("Input action: ")
try:
funct_dict[command]()
except KeyError:
print(f'Command "{command}" is unknown. Only values accepted are: start, stop or status')
Upvotes: 0
Views: 200
Reputation: 746
ANSWERING TO MYSELF
I have managed to accomplish what I needed, the code was a bit different than I initially imagine it, but I'm not surprise as I'm learning Python and everything is new to me :-)
The variable that before I wanted to call rds_cluster_name
, now I call cluster_dict
.
If you scroll down you will find this cluster_dict = cluster_dict[x]['name']
and at that line is when the variable get loaded with the new value that the user has chosen from list.
I had a lot fun learning this, hopefully this help someone else, cheers.
# My dict variable with items numbered
#
cluster_dict = {
1: {'name': 'staging-cluster'},
2: {'name': 'qa-cluster-1'},
3: {'name': 'int1-cluster'}
}
print("Cluster to be chosen")
# Printing out the content of the variable
#
for x, y in cluster_dict.items():
print(x, ':', cluster_dict[x]['name'])
while True:
print("\nSelect a Cluster:")
# Right user input, all I needed
#
x = int(input())
# And here, if it is right just "break" out of the while, if not go back to iterate
#
if x in cluster_dict.keys():
x = int(x)
print("\nYou have chosen: {0}".format(cluster_dict[x]['name']))
print("\n")
# At this point is where the variable is loaded with the value chosen by the user
#
cluster_dict = cluster_dict[x]['name']
break
else:
print('\nNumber not in list, try again!')
Upvotes: 1
Reputation: 1775
I might be getting something wrong, but I don't think that dict
s has an input
attribute. You might be trying to get a dictionary key based on input, in which case:
DBClusterIdentifier = rds_cluster_name[input("RDS cluster name: ")]
This will set DBClusterIdentifier
to rds_cluster_name["whatever they inputted"]
(which should be staging-cluster
, qa-cluster-1
, or int1-cluster
, setting DBClusterIdentifier
to 1
, 2
, or 3
respectively).
Upvotes: 1