Dec-04-2018, 11:59 AM
The function
The error message is very helpful.
In line number 13, you should change the code from:
json.loads
takes 1 positional argument but 2 were given.The error message is very helpful.
In line number 13, you should change the code from:
counts = json.loads(redis.get('hits'),('{}'))to
counts = json.loads(redis.get('hits'))When your intention was to have a default argument, if the key does not exist in redis (returns None), you can use following trick:
counts = json.loads(redis.get('hits')) or {'hits': 0, 'something-else': 'foo'}First the key 'hits' is returned by redis. If this key is
None
the dict on the right side is assigned to the name counts. The first object, which evaluates to True
is taken.0
, 0.0
, (0.0+0j)
, False
, None
, empty: bytes
, bytearray
, list
, tuple
, dict
, set
evaluates to False
.bool(None) == False bool({}) == False bool([]) == FalseYou can use this knowledge for default values:
def hello(greeting=''): ''' print greeting on the screen if greeting is empty, Hello World is printed ''' text = greeting or 'Hello World' print(text)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!