Python : How to replace single or multiple characters in a string ?

Replace multiple characters/strings in a string

str.replace() function can replace the occurrences of one given sub string only. But what if we want to replace multiple sub strings in a given string ?

Suppose we have a string i.e.

Now, how to replace all the occurrences of these three characters ‘s’, ‘l’, ‘a’ with this string ‘AA’ ?
Let’s create a new function over replace() to do that i.e.

It will replace all the occurrences of strings in List toBeReplaces with newString in the main given list mainString.
Let’s see how to replace the occurrences of [‘s’, ‘l’, ‘a’] with “AA” i.e.

What’s the difference between the zero width non-joiner and the zero width space?

In Break it up, you two!: The zero width non-joiner I discussed the purpose of the zero width non-joiner, which is to request that two adjacent characters be rendered without a ligature. Conversely, the zero width joiner requests that they be rendered with a ligature. Of course, it is up to the rendering engine to make the final decision as to how the rendering is done, but at least you can make your intentions clear.

Another character that seems very similar to the zero width non-joiner is the zero width space. Both of them have no width, and both of them break up ligatures. So what’s the difference?

Well, one of them is a space, and the other one isn’t.

The zero width space is used to indicate where one word ends and another word begins, even though there should not be any space rendered at the word boundary. This is significant for languages which have the concept of words, but not of spaces. For example, the Thai and Korean languages use multiple characters to represent words, but traditionally do not insert spaces between words. The words just run together, and readers are expected to use their experience with the language to know where one word ends and the next begins.

Rate Limiting with Python and Redis (GitHub)

 

import time

 

from client import get_redis_client
from exceptions import RateLimitExceeded
def rate_per_second(count):
    def _rate_per_second(function):
        def __rate_per_second(*args, **kwargs):
client = get_redis_client()
            key = frate-limit:{int(time.time())}
            if int(client.incr(key)) > count:
                raise RateLimitExceeded
            if client.ttl(key) == 1# timeout is not set
                client.expire(key, 1# expire in 1 second
            return function(*args, *kwargs)
        return __rate_per_second
    return _rate_per_second
@rate_per_second(100# example: 100 requests per second
def my_function():
    pass  # do something
if __name__ == __main__:
    success = fail = 0
    for i in range(2000):
        try:
            my_function()
            success += 1
        except RateLimitExceeded:
            fail += 1
        time.sleep(5/1000# sleep every 5 milliseconds
    print(fSuccess count = {success})
    print(fFail count = {fail})

 

Tech and Liberty: Stratechery Quotations

According to Randall Munroe, the author, the “Right to Free Speech” is granted by the First Amendment, which was precisely the outcome Hamilton feared in Federalist No. 84:

I go further, and affirm that bills of rights, in the sense and to the extent in which they are contended for, are not only unnecessary in the proposed Constitution, but would even be dangerous. They would contain various exceptions to powers not granted; and, on this very account, would afford a colorable pretext to claim more than were granted. For why declare that things shall not be done which there is no power to do? Why, for instance, should it be said that the liberty of the press shall not be restrained, when no power is given by which restrictions may be imposed? I will not contend that such a provision would confer a regulating power; but it is evident that it would furnish, to men disposed to usurp, a plausible pretense for claiming that power. They might urge with a semblance of reason, that the Constitution ought not to be charged with the absurdity of providing against the abuse of an authority which was not given, and that the provision against restraining the liberty of the press afforded a clear implication, that a power to prescribe proper regulations concerning it was intended to be vested in the national government. This may serve as a specimen of the numerous handles which would be given to the doctrine of constructive powers, by the indulgence of an injudicious zeal for bills of rights.

Hamilton’s argument is that because the U.S. Constitution was created not as a shield from tyrannical kings and princes, but rather by independent states, all essential liberties were secured by the preamble (emphasis original):

WE, THE PEOPLE of the United States, to secure the blessings of liberty to ourselves and our posterity, do ORDAIN and ESTABLISH this Constitution for the United States of America.

Hamilton added:

Here, in strictness, the people surrender nothing; and as they retain every thing they have no need of particular reservations.

Munroe, though, assumes the opposite: liberty, in this case the freedom of speech, is an artifact of law, only stretching as far as government action, and no further. Pat Kerr, who wrote a critique of this comic on Medium in 2016, argued that this was the exact wrong way to think about free speech:

Coherent definitions of free speech are actually rather hard to come by, but I would personally suggest that it’s something along the lines of “the ability to voluntarily express (and receive) opinions without suffering excessive penalties for doing so”. This is a liberal principle of tolerance towards others. It’s not an absolute, it isn’t comprehensive, it isn’t rigorously defined, and it isn’t a law.

What it is is a culture.