I have a Python application with a Firebase database backend.
When I retrieve data from my database, I want to check if these values ββare (if not, this means that the database is somehow damaged, because there are no credential fields)
My current implementation is as follows:
self.foo = myDbRef.get('foo') self.bar = myDbRef.get('bar') self.bip = myDbRef.get('bip') self.plop = myDbRef.get('plop') if self.foo is None or self.bar is None or self.bip is None or self.plop is None: self.isValid = False return ErrorCode.CORRUPTED_DATABASE
This works great, compact, but has a serious problem: I will get information that the database is damaged, but not that the field is missing (there can only be one of them, or more, or all!)
The idiomatic approach should be
if self.foo is None: self.isValid = False return ErrorCode.CORRUPTED_DATABASE, "FOO IS MISSING" # could be a string, an enum value, whatever, I have the information if self.bar is None: self.isValid = False return ErrorCode.CORRUPTED_DATABASE, "BAR IS MISSING" if self.bip is None: self.isValid = False return ErrorCode.CORRUPTED_DATABASE, "BIP IS MISSING"
But it is not beautiful, it does not factorize (all my functions "init from db" use the same template ... I do not want to multiply my number of lines by 10 times for this case).
This is not a "100% python" question, but I hope langage has something for me to handle it like a boss (this is python: it usually does!)