Python Programming Blueprints by Daniel Furtado

Python Programming Blueprints by Daniel Furtado

Author:Daniel Furtado
Language: eng
Format: epub, mobi
Tags: COM051360 - COMPUTERS / Programming Languages / Python, COM051230 - COMPUTERS / Software Development and Engineering / General, COM060080 - COMPUTERS / Web / General
Publisher: Packt Publishing
Published: 2018-08-14T08:57:12+00:00


Retrieving all messages

Similar to our previous steps, we will need to add a new method to our Redis dependency in order to add more functionality. This time, we will be creating a method that will iterate through all of our keys in Redis and return the corresponding messages in a list.

Adding a get all messages method to our Redis client

Let's add the following to our RedisClient:

def get_all_messages(self): return [ { 'id': message_id, 'message': self.redis.get(message_id) } for message_id in self.redis.keys() ]

We start off by using self.redis.keys() to gather all keys that are stored in Redis, which, in our case, are the message IDs. We then have a list comprehension that will iterate through all of the message IDs and create a dictionary for each one, containing the message ID itself and the message that is stored in Redis, using self.redis.get(message_id).

For large scale applications in a production environment, it is not recommended to use the Redis KEYS method, since this will block the server until it has finished completing its operation. For more information, see: http://url.marcuspen.com/rediskeys.

Personally, I prefer to use a list comprehension here to build the list of messages, but if you are struggling to understand this method, I recommend writing it as a standard for loop.

For the sake of this example, see the following code for the same method built as a for loop:

def get_all_messages(self): message_ids = self.redis.keys() messages = [] for message_id in message_ids: message = self.redis.get(message_id) messages.append( {'id': message_id, 'message': message} ) return messages

Both of these methods do exactly the same thing. Which do you prefer? I'll leave that choice to you...

Whenever I write a list or dictionary comprehension, I always start by having a test that checks the output of my function or method. I then write my code with a comprehension and test it to ensure the output is correct. I'll then change my code to a for loop and ensure the test still passes. After that, I look at both versions of my code and decide which one looks the most readable and clean. Unless the code needs to be super efficient, I always opt for code that reads well, even if that means a few more lines. This approach pays off in the long run when it comes to reading back and maintaining that code later!

We now have a way to obtain all messages in Redis. In the preceding code, I could have simply returned a list of messages with no dictionaries involved, just the string value of the message. But what if we wanted to add more data to each message later? For example, some metadata to say when the message was created or how long the message has until it expires... we'll get to that part later! Using a dictionary here for each message will allow us to easily evolve our data structures later on.

We can now look at adding a new RPC to our MessageService that will allow us to get all of the messages.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.