Background is I'm getting data from a JSON API where lots of fields are optional and I want most of the fields in a database. When a specific field isn't available I want an empty string (""
) written into the database.
Currently I've been doing:
if jsonobject.what_i_look_for:
dbstring = jsonobject.what_i_look_for
else:
dbstring = ""
And then inserted dbstring into the database. However I'm getting a lot more of these fields now and I want a much cleaner code rather than a function which consists about 80% of if-statements.
I've found if-shorthands and this shorthand to check if a variable is empty, but both don't seem to work directly as a string. I've tested this using print()
in an interactive python 3.5.2 shell:
>>> print(testvar or "")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'testvar' is not defined
>>> print(testvar if testvar else "")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'testvar' is not defined
This: echo (isset($testvar) ? $testvar : "");
is the PHP equivalent of what I seek.
Edit: Since it seems relevant: The object I am trying to process is coming from Telegram's JSON API. I'm using python-telegram-bot
as library and this is an example object.
We can use hasattr() function to find if a python object obj has a certain attribute or property. hasattr(obj, 'attribute'): The convention in python is that, if the property is likely to be there, simply call it and catch it with a try/except block.
If you want to determine whether a given object has a particular attribute then hasattr() method is what you are looking for. The method accepts two arguments, the object and the attribute in string format.
Method #1 : Using isinstance(x, str) By giving the second argument as “str”, we can check if the variable we pass is a string or not.
An instance/object attribute is a variable that belongs to one (and only one) object. Every instance of a class points to its own attributes variables. These attributes are defined within the __init__ constructor.
The Pythonic way is to look out for NameError
exception that would be raised when the variable is not defined, the name is not bound to any object to be precise.
So, for example:
try:
foobar
except NameError:
# Do stuffs
print('foobar is not defined')
raise # raise the original exception again, if you want
Names reside in namespaces e.g. local names reside in locals()
(dict
) namespace, global names reside in globals()
(dict
) namespace. You can define a function that takes name string and namespace as an argument to check for the existence, here is a hint passing namespace as a dict
and catching KeyError
:
In [1213]: def is_defined(name, namespace):
...: try:
...: namespace[name]
...: except KeyError:
...: return False
...: return True
...:
In [1214]: is_defined('spamegg', globals())
Out[1214]: False
In [1215]: spamegg = 10
In [1216]: is_defined('spamegg', globals())
Out[1216]: True
On the other hand, if you are looking to get the value of an atrribute string of an object, getattr
is the way to go:
getattr(obj, attr)
For example, the following two are equivalent:
obj.foobar
getattr(obj, 'foobar')
Even you can add a default when the object attribute is missing:
getattr(obj, 'foobar', 'spamegg')
The above will output the value obj.foobar
, if foobar
is missing it would output spamegg
.
You might also be interested in hasattr
that returns True
/False
for an attribute existence check, instead of needing to manually handle AttributeError
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With