2

I am currently designing an auto FIFO queue using redis for my own 3d-printer, i am a new towards python scripting and redis. Any better ideas from you is appreciated.

I am having trouble in creating a python script to check do a check if a key value is available or not.

Currently my python script is

import redis
import os
import time

r = redis.StrictRedis(host='172.16.114.54', port=6379, db=0)

if r.lrange('stlfile',0 ,0) == None:
        print 'there is no key'
else:
        print r.lrange('stlfile',0 ,0)

Output:

root@user:~/scripts# python autoslice.py
['172.16.114.162/registered/uploads/teemo.stl']

while my redis have these values

172.16.114.54:6379> lrange stlfile 0 -1
1) "172.16.114.162/registered/uploads/teemo.stl"
2) "172.16.114.162/registered/uploads/hunter_knife.stl"

i currently can do a r.lrange('stlfile', 0,0) and my 1st key value will output.

1st question : is how do i do a python script to check if a value is there or a 'nil' appear? Should i do a string check?

2nd question : my value r.lrange('stlfile', 0,0) appeared, which is what i wanted but without the ['xxx'] (quotes), how do i remove them automately? (only leaving the xxx value)

i have tried to remove my quotes using this but to no avail python How can I strip first and last double quotes

string = string[1:-1]

>>>r = redis.StrictRedis(host='172.16.114.54', port=6379, db=0)
>>>string = r.lrange ('stlfile', 0, 0)
>>>x = string[1:-1]
>>>print x
[]

not too sure why my output is [] instead of string without quotes

1 Answer 1

1

1) You can test your return value against None

if key_value == None:
   print 'there was no key'
else:
   print 'key_value was', key_value

2) What makes you think that you think that the returned value has quotes? Are you sure that it does? Look at this:

    ~ mgregory$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> one = "a"
>>> print one
a
>>> one
'a'
>>> result = [one]
>>> print result
['a']
>>> value = result[0]
>>> print value
a
>>> 

Note that the value that is returned from redis.StrictRedis() is not a string, it's an array.

We know this because x is an array - an empty one when you finally print it.

It's not surprising its empty, because it started with one element in it, then you subtracted two elements from it.

If you really think that the items coming from redis do have quotes in them, then you will need to do

result = r.lrange ('stlfile', 0, 0)
string = result[0]
x = string[1:-1]
print x

I predict this will result in your string missing the first and last characters - I don't believe there is in fact quotes in the result coming from redis.

Maybe the heart of your problem is that you think that result is a string, when actually it is an array....

Sign up to request clarification or add additional context in comments.

6 Comments

when i lrange stlfile 0 0 my values come out as ['XXXXX'] tho
That looks like an array containing a single string. Suppose you got this result like this result = redis.StrictRedis(host='172.16.114.54', port=6379, db=0). Try this: print result[0]. I think you will find that it doesn't actually have quotes, it's just the print representation of an array. I have updated the "look at this" in my answer to demonstrate
This would work if my variable is declared in python script itself. But i am trying to check if any variable is available in redis. i have added more information in my post above.
Your If else helped me though! Thanks! :D
Just an update, your prediction was right! The print x now prints my expected result! Thanks for your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.