Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Json dictionnary on Redis
#2
The function 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([]) == False
You 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!
Reply


Messages In This Thread
Json dictionnary on Redis - by katsu707 - Dec-04-2018, 08:38 AM
RE: Json dictionnary on Redis - by DeaD_EyE - Dec-04-2018, 11:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionnary indexing error Ander 6 2,007 Sep-10-2021, 12:22 PM
Last Post: Ander
  python dictionnary Omar_Yatim 3 2,931 Dec-08-2019, 05:12 AM
Last Post: scidam
  Dictionnary brackets issue Reldaing 1 1,900 Nov-10-2019, 11:54 PM
Last Post: ichabod801
  Jython macro which uses PythonInterpreter and Redis script gives an error rkanumola 2 2,324 Oct-30-2019, 07:37 AM
Last Post: rkanumola
  Access to the elements of a dictionnary Reims 1 1,685 Oct-02-2019, 12:48 PM
Last Post: SheeppOSU
  from Json Time Serie file to python dictionnary Reims 1 2,102 Sep-11-2019, 08:17 AM
Last Post: DeaD_EyE
  convert a json file to a python dictionnary of array Reims 2 2,319 Sep-10-2019, 01:08 PM
Last Post: Reims
  Receive data from Redis RQ worker process freak14 0 1,953 Jul-15-2019, 12:39 PM
Last Post: freak14
  dictionnary lateublegende 1 2,502 Apr-29-2019, 09:10 PM
Last Post: Yoriz
  Why do we need setdefault() method for dictionnary? DJ_Qu 3 2,773 Apr-21-2019, 11:00 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020