Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Json dictionnary on Redis
#1
Hi everyone, I'm a complete beginner and here's my issue:

I have Flask as a webserver, Redis is counting the number of hits on the root. I added a arg "name".
I want to store and return a different count that I wish to link to the "name" var.
I got some help and they advised to use a JSON dictionnary. I'm struggling with the implementation. My code looks like this:

from flask import Flask
from redis import Redis
import json

app = Flask(__name__)
redis = Redis(host='redis', port=6379)

@app.route('/')
@app.route('/<name>')
def hello(name):
    counts = json.loads(redis.get('hits'),('{}'))
    try:
         counts[name] += 1
    except KeyError:
         counts[name] = 1
    redis.set('hits', json.dumps(counts))
    return 'Hello {} I have been seen {} times.\n'.format(name,counts[name])

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, debug=True)
I keep getting this error and I don't know why...
Error:
File "/app/app3.py", line 11, in hello counts = json.loads(redis.get('hits'),('{}')) TypeError: loads() takes 1 positional argument but 2 were given
Thanks a lot!
K.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionnary indexing error Ander 6 1,833 Sep-10-2021, 12:22 PM
Last Post: Ander
  python dictionnary Omar_Yatim 3 2,756 Dec-08-2019, 05:12 AM
Last Post: scidam
  Dictionnary brackets issue Reldaing 1 1,784 Nov-10-2019, 11:54 PM
Last Post: ichabod801
  Jython macro which uses PythonInterpreter and Redis script gives an error rkanumola 2 2,207 Oct-30-2019, 07:37 AM
Last Post: rkanumola
  Access to the elements of a dictionnary Reims 1 1,603 Oct-02-2019, 12:48 PM
Last Post: SheeppOSU
  from Json Time Serie file to python dictionnary Reims 1 1,990 Sep-11-2019, 08:17 AM
Last Post: DeaD_EyE
  convert a json file to a python dictionnary of array Reims 2 2,205 Sep-10-2019, 01:08 PM
Last Post: Reims
  Receive data from Redis RQ worker process freak14 0 1,868 Jul-15-2019, 12:39 PM
Last Post: freak14
  dictionnary lateublegende 1 2,417 Apr-29-2019, 09:10 PM
Last Post: Yoriz
  Why do we need setdefault() method for dictionnary? DJ_Qu 3 2,662 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