This question already has an answer here:
How to use boto3 to get a list of EBS snapshots owned by me?
(1 answer)
Closed 2 years ago.
Im trying to print out every snapshot that hasn't got the specific Tag "CostReference" inside my aws account.
To Iterate through the snapshots I'm using:
for snapshot in snapshots:
if(not costreferencetag_isset_snapshot(snapshot)):
print("[SNAPSHOT] " + str(snapshot))
print("[INFO]: No CostReferenceTag!! \n")
missingtagginginfo = missingtagginginfo + str(snapshot) + ": No CostReferenceTag\n"
count_snapshot += 1
continue
else:
costreference_snapshot = get_costreference_snapshot(snapshot)
if costreference_snapshot not in managedpsp:
print("[SNAPSHOT] " + str(snapshot))
print("[INFO]: The PSP: " + costreference_snapshot + " of: " + str(snapshot) + " is WRONG! \n")
missingtagginginfo = missingtagginginfo + str(snapshot) + " " + costreference_snapshot + ": Wrong PSP\n"
count_snapshot += 1
print(count_snapshot)
So far so good the code is working, but I'm also getting the public Snapshots that are owned by amazon, wich have no relevance for me.
Is there any way to filter those public snapshots?
Greets
Code for the other functions:
def costreference_isset(instance):
#Searching for Instance without CostReference-tags
if instance.tags is None:
print("[INFO]: No Tags have been set yet:")
return(False)
#Searching for CostReference-tags
for t in instance.tags:
if t['Key'] == 'CostReference':
return(True)
return(False)
def get_costreferencetag(instance):
for t in instance.tags:
if t['Key'] == "CostReference":
return(str(t['Value']))
return(False)
managedpsp is a list with valid Costreference - tags
Solved it with:
for snapshot in snapshots.filter(OwnerIds= ['self']):
if(not costreferencetag_isset_snapshot(snapshot)):
print("[SNAPSHOT] " + str(snapshot))
print("[INFO]: No CostReferenceTag!! \n")
missingtagginginfo = missingtagginginfo + str(snapshot) + ": No CostReferenceTag\n"
count_snapshot += 1
continue
else:
costreference_snapshot = get_costreference_snapshot(snapshot)
if costreference_snapshot not in managedpsp:
print("[SNAPSHOT] " + str(snapshot))
print("[INFO]: The PSP: " + costreference_snapshot + " of: " + str(snapshot) + " is WRONG! \n")
missingtagginginfo = missingtagginginfo + str(snapshot) + " " + costreference_snapshot + ": Wrong PSP\n"
count_snapshot += 1
print(count_snapshot)
Try using the filter owner-id with your own account number.
Check here for the CLI reference: describe-snapshots — AWS CLI Command Reference
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I'm looking for a way to loop the code below... so that it'll go through and print the first sentence, then the second sentence over and over until one entity's health drops to zero.
From my research (bearing in mind I'm a knucklehead) the 'for' function seems to only work with single words/numbers... but perhaps I'm wrong.
CODE:
if sentence == 1:
print ("The" + " " + enemy_1 + " " + random.choice(attack_verb) + " " + main_character + " " + "with its" + " " + random.choice(enemy_1_weapon))
enemy_1_health -= 20
print (enemy_1_health)
sentence = 0
if sentence == 0:
print (main_character + " " + random.choice(attack_verb) + " " + "the" + " " + enemy_1 + " " + "with his" + " " + random.choice(main_character_weapon))
enemy_1_health -= 20
print (enemy_1_health)
sentence = 1
#And then repeat and repeat over and over :)
you could do something like this
while enemy_1_health > 0:
if sentence == 1:
print ("The" + " " + enemy_1 + " " + random.choice(attack_verb) + " " + main_character + " " + "with its" + " " + random.choice(enemy_1_weapon))
enemy_1_health -= 20
print(enemy_1_health)
sentence = 0
if sentence == 0:
print (main_character + " " + random.choice(attack_verb) + " " + "the" + " " + enemy_1 + " " + "with his" + " " + random.choice(main_character_weapon))
enemy_1_health -= 20
print(enemy_1_health)
sentence = 1
Since you want the loop to happen while a particular condition is true, rather than for each item in a list/range/etc, you want a while loop:
while enemy_1_health > 0:
print(f"The {enemy_1} {random.choice(attack_verb)} {main_character} with its {random.choice(enemy_1_weapon)}")
enemy_1_health -= 20
print(enemy_1_health)
print(f"{main_character} {random.choice(attack_verb)} the {enemy_1} with his {random.choice(main_character_weapon)}")
enemy_1_health -= 20
print(enemy_1_health)
Note that you don't need the if sentence stuff because you're always setting sentence in such a way as to make it equivalent to if True, at which point you can dispense with the whole thing.
Here's an example of how you could use a for loop to iterate over the different attack scenarios:
while enemy_1_health > 0:
for attacker, attacker_pronoun, victim, weapons in [
(f"The {enemy_1}", "its", main_character, enemy_1_weapon),
(main_character, "his", f"the {enemy_1}", main_character_weapon),
]:
print(f"{attacker} {random.choice(attack_verb)} {victim} with {attacker_pronoun} {random.choice(weapons)}")
enemy_1_health -= 20
print(enemy_1_health)
Hi Need clarification for python variable stored as wrong value , here is code :
userinput1 = int(input('enter start value\n'))
userinput2 = int(input('enter stop value\n'))
userinput3 = int(input('enter rampup time in seconds\n'))
userinput4 = float(input('enter increments delta \n'))
userinput5 = input('Enter sequence channels: A A A A or D D D D - A Ascend, D Descent , E Exclude \n')
command1 = "RAMP " + str(userinput5) + " " + userinput1 + " " + userinput2 + " " + userinput4 + " " + userinput3
port.write(command1.encode())
#### ERROR #####
command1 = str("RAMP " + str(userinput5) + " " + userinput1 + " " + userinput2 + " " + userinput4 + " " + userinput3)
TypeError: can only concatenate str (not "int") to str
Can you please clarify me correct method to store both type variable input in single variable command. type caste was done already.
You can concate only strings, so before concate all your userinputs you must "convert" them into strings
Example1:
command1 = "RAMP " + " ".join(map(str, [userinput5, userinput1, userinput2, userinput4, userinput3]))
Example2:
command1 = f"RAMP {userinput5} {userinput1} {userinput2} {userinput4} {userinput3}"
I'm trying to write a program that prints the values and keys in a dictionary depending of the input the user types. The problem appears when the elif statement on line 11 gets skipped. It doesn't matter if the if statement is false, the elif statement gets skipped. I'm learning so I don't really know where my error is. Thanks for the help!
areaM = {str(1) + " acre" : str(160) + " sq rods"}
linearM = {str(1) + " ft" : str(12) + " in", str(1) + " yd": str(3) + " ft"}
def displayConversion(conv):
for k, v in conv.items():
print(str(v) + " = " + str(k))
while True:
print("Enter a conversion")
if input() == "Area Meassure":
displayConversion(areaM)
elif input() == "Linear Meassure":
displayConversion(linearM)
else:
print("Conversion not available")
Maybe this as the full code (too much inputss):
areaM = {str(1) + " acre" : str(160) + " sq rods"}
linearM = {str(1) + " ft" : str(12) + " in", str(1) + " yd": str(3) + " ft"}
def displayConversion(conv):
for k, v in conv.items():
print(str(v) + " = " + str(k))
while True:
a=input("Enter a conversion\n")
if a == "Area Meassure":
displayConversion(areaM)
break
elif a == "Linear Meassure":
displayConversion(linearM)
break
else:
print("Conversion not available")
So this result can have less than 10 answers at times. I am having a hard time figuring out how to ignore if 'overbought1' is less than 10. For example, if there are only 8 results I would like it to display 8 and ignore the last two that aren't there.
try:
for i in range(10):
(runo[i])
except:
pass
overbought1 = ("Top 10 overbought today: $" + runo[0] + " $" + runo[1] + " $" + runo[2] + " $" + runo[3] + " $" +runo[4] + " $" + runo[5] + " $" + runo[6] + " $" + runo[7]+ " $" + runo[8]+ " $" + runo[9])
await client.say(overbought1)
While you entire example make little sense, here is one of the ways to avoid IndexError:
for i in range(min(10, len(runo))): # loop at most to the minimum
# between ten and len(runo)
(runo[i]) # <-- this does nothing here!
Another way:
for v in runo[:10]: # access no more than first 10 elements
v # <-- also does nothing. v is equivalent to runo[i] from the previous loop
Now, if you want to fix your overbought1 string, then do this:
overbought1 = "Top 10 overbought today: $" + " $".join(runo[:10])
or even:
overbought1 = ("Top %d overbought today: $" % len(runo)) + " $".join(runo[:10])
I am making a custom language and writing it in Python. I have if statements with number equalities and inequalities, but I´m having trouble implementing the conditional execution of the code in the if statement block. Here's my code for the tokenizer of if statements:
elif tok == "IF":
tokens.append("IF")
tok = ""
elif tok == "THEN":
if expr != "" and isexpr == 0:
tokens.append("NUM:" + expr)
expr = ""
tokens.append("THEN")
tok = ""
I also use a string, "ENDIF", to end the if statement. Essentially, the "THEN" is the opening bracket and "ENDIF" is the closing bracket, if this was a language that used brackets.
The parser for int eq/ineq if statements:
elif toks[i] + " " + toks[i+1][0:3] + " " + toks[i+2] + " " + toks[i+3][0:3] + " " + toks[i+4] == "IF NUM LESSTHAN NUM THEN":
if toks[i+1][4:] < toks[i+3][4:]:
print("TRUE, " + toks[i+1][4:] + " is less than " + toks[i+3][4:])
else:
print("TRUE, " + toks[i+1][4:] + " is not less than " + toks[i+3][4:])
i += 5
elif toks[i] + " " + toks[i+1][0:3] + " " + toks[i+2] + " " + toks[i+3][0:3] + " " + toks[i+4] == "IF NUM MORETHAN NUM THEN":
if toks[i+1][4:] > toks[i+3][4:]:
print("TRUE, " + toks[i+1][4:] + " is greater than " + toks[i+3][4:])
else:
print("FALSE, " + toks[i+1][4:] + " is not greater than " + toks[i+3][4:])
i += 5
elif toks[i] + " " + toks[i+1][0:3] + " " + toks[i+2] + " " + toks[i+3][0:3] + " " + toks[i+4] == "IF NUM LESSOREQUAL NUM THEN":
if toks[i+1][4:] <= toks[i+3][4:]:
print("TRUE, " + toks[i+1][4:] + " is less than or equal to " + toks[i+3][4:])
else:
print("FALSE, " + toks[i+1][4:] + " is not less than or equal to " + toks[i+3][4:])
i += 5
elif toks[i] + " " + toks[i+1][0:3] + " " + toks[i+2] + " " + toks[i+3][0:3] + " " + toks[i+4] == "IF NUM MOREOREQUAL NUM THEN":
if toks[i+1][4:] >= toks[i+3][4:]:
print("TRUE, " + toks[i+1][4:] + " is greater than or equal to " + toks[i+3][4:])
else:
print("FALSE, " + toks[i+1][4:] + " is not greater than or equal to " + toks[i+3][4:])
i += 5
elif toks[i] + " " + toks[i+1][0:3] + " " + toks[i+2] + " " + toks[i+3][0:3] + " " + toks[i+4] == "IF NUM NOTEQUAL NUM THEN":
if toks[i+1][4:] != toks[i+3][4:]:
print("TRUE, " + toks[i+1][4:] + " is not equal to " + toks[i+3][4:])
else:
print("FALSE, " + toks[i+1][4:] + " is equal to " + toks[i+3][4:])
i += 5
here's an example of an if statement in my language:
IF 1 == 2 THEN
OUT "Hello, world!"
ENDIF
Obviously, that will return false, but it still prints Hello World as of now, because I don't know how to skip the code if it returns false... I was thinking of skipping to the next endif in the file... I think I know how to do it though. I just need to find the place of the token in the tokens[] array... if I can do that, then I can set the iterator variable past the ENDIF. Is there a function that will let me search an array for the next appearance of a string after a certain place (for example, tokens[4:]), then return the index value of that element in the array?
Thanks in advance!
I figured it out!
While it is very primitive and doesn't support nesting (yet), I have a simple solution to my problem. Basically, whenever it detects the 'IF' token, it loops through all of the tokens after that until it finds and 'ENDIF' token. While doing that, it increments an iterator called 'ii' and at the end, sets the main iterator past the 'ENDIF', ignoring the code within the if statement. This is only done if the condition parser returns false.
code:
def doIF_FALSE(tokens):
ii = 0
for token in tokens:
if token == "IF":
ii = 1
elif token == "ENDIF":
ii += 1
break
else:
ii += 1
return ii
for the call, I set the iterator value like this: i = doIF_FALSE(tokens[i:])
Example code:
IF 1 > 2 THEN
OUT "1 is greater than 2"
ENDIF
OUT "You passed the if statement"
Output:
"You passed the if statement"
NOTE: Yes, I realize that this is the same answer as another question I asked, but they're basically the same question...