Covenant Eyes Developer Portal

StringLike in Python

Let’s say you have a Python class that isn’t a string. Further imagine that aside from your class’ methods you wish it would act just like a normal Python string. How can you do that?
The stringlike package provides a class called StringLike that can make your class look and act just like a built-in string.

If you’re wondering why you would ever want your class to behave just like a Python string, take a gander at the example below.

How To Get StringLike

First things first: here’s how you can get StringLike onto your machine:

Install with pip:

Or download it from github at https://github.com/CovenantEyes/py_stringlike.

Example Scenario

Let’s say you want to send an email to your customers based on a couple possible template strings. Maybe they look something like this:

We can generically generate a new email body from any one of these templates by doing something like this:

We take some_template (which might be welcome_email or history_email or some other template) and populate all the possible keywords so they will be expanded to real values.

But there’s a major downside to this method. Some email templates use keywords that other templates don’t use. For example, some templates use the {history} keyword and some don’t. This wouldn’t really pose a problem if it weren’t for the fact that the sql_get_history function (which populates {history}) uses a mound of horrendous SQL queries that transfer gobs of data over thousands of treacherous miles…wirelessly. This means that templates not using the {history} keyword will still have to go through all the rigmarole to get the customer’s transaction history when it’s not even used! This huge waste of time and effort simply will not do.

Solution

If only we had a way to delay the execution of these SQL queries until they’re actually needed… As it turns out, “delayed execution” is a common necessity among programmers and it has a fitting name: laziness. That’s exactly what we’re going to attempt here. (StringLike is named after its lazy Haskell cousin.)

Of course, every language has “delayed execution.” They’re called functions. But we have a problem: functions aren’t strings. The Python string’s format method takes strings, not functions. What if we could create our own string class that would delay the string generation for us?

That’s where StringLike comes in. Without digressing, let it suffice to say that making your own class that behaves exactly like Python strings is a little tricky and very magical. StringLike does that for you.

We can implement our own MyLazyString class like this:

That’s it! Our MyLazyString class inherits from StringLike and overrides the __str__ magic method to return the results of a thunk (which is a fancy word for a function without any arguments).

Here’s how we use it to revise our email generation:

This uses lambda as a quick way to make a function without any arguments. You could create them the long way (with def) if you wanted.

Now none of those horrendous SQL queries will be executed unless they are actually needed!

And, just to sweeten your day, stringlike already includes LazyString and CachedLazyString which can be imported like this:

Conclusion

We hope our first contribution to PyPI proves helpful to someone out there. Log any bugs or feature-requests you may have to the github repo.

Leave a reply