I`m doing an unit test for the first time and I need to check if a success key is the same as the other one, how can I check a success key without checking the whole dictionary?
I just need to check the success key not all the other ones
I need to do it with assert using the unit test module
dict 1 = {'success': RC.input_validation,
'return_msg': return_msg,
'debug_data': debug_data}
dict 2 = {'success': RC.success,
'return_msg': return_msg,
'debug_data': debug_data}
class MyTestCase(unittest.TestCase):
def test_something(self):
call_result = {}
debug_data = []
test = SetShape()
call_result = SetShape.setShape(test, shapechoosing=1)
debug_data.append(call_result)
print("10")
if __name__ == '__main__':
unittest.main()
Access the values of the 'success' key in each dictionary and check if they're equal
if dict1['success'] == dict2['success']:
print("they're the same")
This should work for you.
dict_1 = {'success': "RC.input_validation",
'return_msg': "return_msg",
'debug_data': "debug_data"}
dict_2 = {'success': "RC.success",
'return_msg': "return_msg",
'debug_data': "debug_data"}
dict_1.get('success')==dict_2.get('success')
Is this what you're looking for?
if dict_1.get('success') == dict_2.get('success'):
# your body of code
Hope it helps!!
Related
I wrote a function that validates if all the fields exist in a python dictionary. Below is the code.
def validate_participants(self, xml_line):
try:
participant_type = xml_line["participants"]["participant_type"]
participant_role = xml_line["participants"]["participant_role"]
participant_type = xml_line["participants"]["participant_type"]
participant_id = xml_line["participants"]["participant_id"]
return True
except KeyError as err:
log.error(f'{err}')
return False
This raises an error about the missing key that it finds first and breaks execution. I want to go through the entire set of fields and raise error with all the missing fields. What's the best/efficient way to solve the problem?
Using a set you can get the difference and if it is empty the keys are not missing.
def validate_participants(self, xml_line):
keys = {"participant_type", "participant_role", "participant_id"}
return keys - xml_line["participants"].keys() or True
The or True means return the set of missing keys if there are missing keys otherwise return True
Edit:
To answer your comment there is no need to use a try/except if you check first:
def validate_participants(self, xml_line):
keys = {"participant_type", "participant_role", "participant_id"}
missing_keys = keys - xml_line["participants"].keys()
if missing_keys:
#return False or
raise Value_Error(f"Missing values: {', '.join(missing_keys)}")
#access the values/do work or
return True
I would define a set of the expected keys and subtract the actual keys:
expected_keys = {...}
actual_keys = xml_line["participants"].keys()
key_diff = expected_keys - actual_keys
Now create a message from key_diff about which keys are missing.
I am trying to make an API endpoint that will take input like http://127.0.0.1:8000/getslots?car_number=3and give output as car_number or slot_parking. Here is what I have done:
slots = {'ka9865': 1, 'ka9866': 2, 'ka9867': 3}
def get_car(request):
if request.method == 'GET':
try:
car_number = request.GET['car_number']
for key,value in slots.items():
if key == car_number:
car_slot = value
response = json.dumps([{'slot': car_slot}])
except (UnboundLocalError):
slot = request.GET['car_number']
for key,value in slots.items():
if value == slot:
car_number1 = key #not entering inside this loop
response = json.dumps([{ 'car_number': car_number1}])
except:
response = json.dumps([{ 'Error': 'No car with that name/slot'}])
return HttpResponse(response, content_type='text/json')
But I am getting error of UnboundLocalError: local variable 'response' referenced before assignment
I am not able to figure out how to do this and also please suggest me if there is any better way of implementing this.
Thank you.
You need to creatwe response already before otherwise the variable will be just in the namespace of the exception block.
slots = {'ka9865': 1, 'ka9866': 2, 'ka9867': 3}
def get_car(request):
response = None # Create default reponse here
if request.method == 'GET':
#...
I have a really long nested list, that looks something like this
['a','b',['c','d',['e',['a','b',['c','d',['e',['a','b',['c','d',['e4',['a','b',['c','d',['e',['a','b',['c','d',['e',['a','b',['c','d',['e14']]]]]],]]]]]]]]],]]]
I'm trying to find value inside that list, however in my case a normal for loops doesn't works because i need to loop through each one and that would take so long.
I came up with this recursive function:
def locate(seq :list, value:int):
for item in seq:
if item.__class__ is list:
locate(item, value)
else:
if(item == value):
return True
return False
So here is the unit test that i created for my algorithm:
import unittest
class TestCase(unittest.TestCase):
def test_locate(self):
self.assertEqual(locate(['a','b',['c','d',['e']]],'e'), True)
def test_locate_two(self):
self.assertEqual(locate(['a','b',['c','d',['e',['a','b',['c','d',['e',['a','b',['c','d',['e4',['a','b',['c','d',['e',['a','b',['c','d',['e',['a','b',['c','d',['e14']]]]]],]]]]]]]]],]]],'e'), True)
if __name__ == '__main__':
unittest.main()
Test result:
Ran 2 tests in 0.001s
FAILED (failures=2)
So, I tried this modification:
def locate(seq :list, value:int):
el_check = False
for item in seq:
if item.__class__ is list:
el_check = el_check or locate(item, value)
else:
if(item == value):
return True
return el_check
Seems to work in couple tests, if you find a case it does not work at, please post so I can try to have a look at it.
I have a dictionary and I want to add some index of the dictionary to variables.
I know that try except is more pythonic than else if. And I tried with try except and it's works perfectly but I have a lot of key to check and I can't figure out of which code is more pythonic
Here is my dictionary :
test = {"token":"eating", "stemm": "eat", "lemm": "eat", "pos":"VPP"}
def method_one(test):
try:
token = test["token"]
except KeyError:
token = None
try:
stemm = test["stemm"]
except KeyError:
stemm = None
def method_two(test):
token = None
stemm = None
if "token" in test:
token = test["token"]
if "stemm" in test:
stemm = test["stemm"]
I also tried one try except for all but when one failed, I can't know which one is failing so this is useless.
Any ideas of a third method? Or method one is the good one?
dict has get method that will return value or None. you could do item = dict.get(key) and this will return either the value(if the key exists) or None otherwise, which is what you are looking for it seems :)
>>> d = {'foo': 'bar'}
>>> d.get('foo')
'bar'
>>> item = d.get('fooo')
>>> item is None
True
There is a direct lookup construct where create a set of valid keys that should exist in the dictionary
test = {"token":"eating", "stemm": "eat", "lemm": "eat", "pos":"VPP"}
def validate(d, valkeys):
keyset = set(test.keys())
if valkeys.issubset(keyset):
return True
else:
return False
if __name__ == "__main__":
val = set(["token", "stemm"])
inval = set(["token", "stemm", "st"])
assert validate(test, val) == True
assert validate(test, inval) == False
In order to make CSV files with many columns, I have many, many instances of
try:
printlist.append(text['data'])
except:
printlist.append('')
Is it possible to condense these 4 lines into 1 or 2 (mostly for easier reading of the code)? I've tried with this function but I haven't discovered a way to pass something that doesn't exist.
def tryexcept(input):
try:
printlist.append(input)
except:
printlist.append('')
return printlist
UPDATE I should mention that 'text' is actually a dict value, so it should look like
printlist.append(text['data'])
(changed above)
What about:
printlist.append(text['data'] if 'data' in text else '')
Or even better as #bruno desthuilliers suggested:
printlist.append(text.get('data',''))
[EDIT]
For nested dict I normally use my dict selector:
class TokenDataType:
LIST = "list"
DICT = "dict"
def _select_key(keyitt, data):
try:
new_key = keyitt.next()
except StopIteration:
return data
if new_key["t"] == TokenDataType.DICT:
return _select_key(keyitt, data[new_key["k"]])
elif new_key["t"] == TokenDataType.LIST:
return _select_key(keyitt, data[new_key["i"]])
def tokenize_query(query):
tokens = []
for token in query.split("."):
token = token.strip()
if token:
ttype = TokenDataType.LIST if "[" in token else TokenDataType.DICT
if ttype == TokenDataType.LIST:
index = None
if len(token) >= 3:
index = int(token.replace("[", "").replace("]", ""))
tokens.append({"k":token, "t":ttype, "i":index})
else:
tokens.append({"k":token, "t":ttype})
return tokens
def normalize_query(query=None, tokens=None):
if tokens == None:
tokens = tokenize_query(query)
return ".".join([token["k"] for token in tokens])
def select(query, data, throw_exception_on_key_not_found=False):
tokens = tokenize_query(query)
try:
return _select_key(iter(tokens), data)
except Exception as e:
if throw_exception_on_key_not_found:
raise e
return None
DQ = select
if __name__ == "__main__":
test = {"bla":1, "foo":{"bar":2}, "baz":[{"x":1}, {"x":2}]}
print(DQ(".bla", test))
print(DQ("bla", test))
print(DQ("nothere", test))
print(DQ(".foo", test))
print(DQ("foo.bar", test))
print(DQ("baz", test))
print(DQ("baz.[0]", test))
print(DQ("baz.[1].x", test))
print(DQ("baz.[2].x", test))
for your case (appends None when one of the keys is not found):
printlist.append(DQ("data.someotherkey.yetanotherkey", text))
There's a dictionary method that does exactly this, and it let's you specify any default value.
input = text.get('data', default='')
printlist.append(input)
It checks if the key exists in the dictionary, and if not, it returns the default value. More on dictionaries here.
Try this simple wrapper:
def execute_with_exception_handling(f):
try:
return f()
except:
raise
Then you execute your function:
def my_func():
return 0 / 0
execute_with_exception_handling(my_func)
You can also add arguments to the function with *args. And you can even use decorators...that you can google because I recall off the top of my head how that works.
Why do you need to pass something that does not exist?
You can simply call function if the value that is being passed in is not "None" (Or any other unwanted value).
tryexcept( x ) if x is not None else None
Edit:
Are you are trying to see if the variable is declared or not? If yes, one way to get around this would be to declare the variable beforehand:
x = None
...
tryexcept( x ) if x is not None else None