GP launched 013: Update your RegExp!

Grameenphone quietly rolled out its 013 series of numbers and it wrecked everything. No web service, including Google is working with this 013 series of numbers. Its time for developers to fix this mess and update every validation logic you may have for validating mobile numbers or MSISDNs.

I enlisted a few RegExps for de-terrorizing you, even if a bit.

Enough reading, update your RegExps now!

Flask-like “global” request context in Sanic (asyncio)

Although something like Flask’s globally accessible request object is considered a terrible way of writing code (explicit is better than implicit), sometimes it makes sense to use it. For example, while passing a Correlation-ID to track a request’s life cycle through your micro-services.

You can memorize the Correlation-ID throughout the lifecycle of a request without explicitly passing it around like juggling balls. This is actually a good approach as the Correlation ID is not a core business logic – just a distraction. We’ll see how we can implement such a request-bound “global” context in Sanic, and how to setup a simple Correlation ID implementation.

aiotask-context

This nifty Python module can maintain a distinct context against each asyncio Task. Which means, each request can have an associated context we can use to store and pass around passive details.

Now we have a simple Sanic app with the context hooked up.

Implementing Correlation ID Generation and Passing

Now, let’s grab the correlation ID if it comes with the request, or generate our own otherwise. Afterwards, we need to save that value to the context for future use in other parts of the code (i.e. logging, requests to other microservices and so on).

And throughout the code, if you need the cid or any context value you have set, simply use context.get(key).

And the last step is definitely all about responding with the Correlation ID. We’ll be sticking to the middleware for this too. We just need to update the response object from the context.

Great! Now we don’t have to write spaghetti code and get lost in passing CIDs from functions to functions. Cheers!

pyenv build depencies on Ubuntu

pyenv can get particularly annoying during building Python. Dependencies are often missing resulting in failed builds. This short blog post is outlining the required dependencies to reduce headaches for future me, and anyone else reading this.

You have probably come here seeing errors like:

Well, just install these and you’ll be good to go.

You’re welcome.

Viewing OpenCV matrices with matplotlib (w/ Jupyter Notebook)

While working with OpenCV, constantly popping up a new Window to view the results might not be the most effective way of work. However, we can minimize the burden if we are working on Jupyter Notebook; with matplotlib. Essentially we plot the matrices with matplotlib.pyplot.imshow.

Continue reading Viewing OpenCV matrices with matplotlib (w/ Jupyter Notebook)

Reading Bangladeshi NID and Smart Cards with ZXing

Bangladeshi NID and Smart Cards come printed with a 2D Datamatrix Barcode, known as PDF417. The information on the cards can be extracted without using an OCR solution through the barcode. We’ll be using Google’s ZXing library; learn about the basics of using it. Continue reading Reading Bangladeshi NID and Smart Cards with ZXing

Simple Python Profiling with IPython

Profiling is imperative to understand your application. However, getting started with profiling might seem to be a steep learning curve; but it doesn’t have to be! IPython has a lot of built-in magic commands, and some of them are helpful to profile your Python code. %timeit and %prun are among the ones I found most useful. Continue reading Simple Python Profiling with IPython

Mitigating High-Traffic with Simple Static Content Generation

Your application probably does not need to execute some code each time it is used. We have been caching for decades; reducing database queries, using key-value stores, OPcode caching and the list runs on. Things become overwhelming. I believe often we can find the best answers in simplicity; complexity should not be introduced where a simple solution can help. Continue reading Mitigating High-Traffic with Simple Static Content Generation