Skip to main content

Patching decorators in Python

Being a syntactic sugar decorators are essentially a usually callable (e.g. a function or a class) that wraps another callable. And essentially they are applied/called when the module is being read.

For example, the following two chunks of codes are identical:

@decorator
def function():
    return 42
def function():
    return 42

function = decorator(function)

As we can see the decorator in this example is called when the file/module containing the above would be read. So how do we patch such an object?

Read more…

Prevent editor invocation on `git pull` or `git merge`

While doing git pull or git merge, if it's a mechanical merge (i.e. not a fast-forward merge), git invokes the configured editor with an auto-generated merge commit message to give more flexibility to explain the merge. But the invocation of an interactive editor breaks the CI/CD workflow, hence we need something that can automatically save the merge commit with the auto-generated message without invoking the editor (given that's fine with your use case).

git provides an environment variable to make this easier -- GIT_MERGE_AUTOEDIT. We can run git pull (or git merge) with the value of this environment variable set to no i.e.:

Read more…

Django: Clone a model instance on update

Often times, we need to clone a Django model instance while updating it's fields' values. For example, if we have a price model that needs updating, it needs to be cloned first to keep the current related objects referring to the current pricing values. So while updating, we need to keep the current instance as-is and create a new instance with existing data and update the new instance with updated data instead.

If we don't do the above, when we change any relevant field's value, the related objects will subject to the new values, this will in turn trigger new pricing which would be different from the originally calculated/saved one. This might cause serious reliability problems like the initial price/charge was 20 USD, now it has become 30 USD (for example), and so on.

So to handle this automatic cloning of instances on update, I've written a decorator that can be used as a decorator on the save method of the model. Here it is (all the necesary details are in the docstring):

Read more…

Django: Custom User model without username field (and using email in place of it)

Django by default uses the username field for authentication purposes of a user, but it's becoming a common trend to use email field for that purpose. As email identifies a user uniquely, the idea of an additional username field can be somewhat dropped. Also, the email can be used in the places where a username can be used, so it does not break any internal identification structure as well.

In the later sections, when I mentioned the (default) User model please think of the default Django User model (settings.AUTH_USER_MODEL).

So to make sure we're on the same page, our eventual goal is to get rid of the username field and use email field in the User model for authentication purposes.

Now, let's check out the usual ways to modify/extend the django User model:

Read more…

Python : Can we use magic/dunder methods as instance attributes directly instead of putting them in type definition?

No, we can't.

The magic/dunder methods are looked up implicitly on the type definition while special functions, keywords, or operators are to be resolved by the runtime. They don't follow the regular attribute lookup procedure i.e. doesn't follow the __getattribute__ chain. This makes them fast to access as the runtime only needs to search on the type, not on instance dict.

So, what about classes themselves? What dunder methods should be used when any special function is called on them?

As any class is an instance of a metaclass, the dunder methods defined on the metaclass would be used.


Let's see a few examples to get a clear overview of all these. We'll be using the repr function, for which the interpreter will call __repr__ dunder method implicitly, in order to get the representation of an object.

Read more…

Why exception name bindings are deleted while exiting the `except` block in Python, and how to break reference cycles in traceback object's stack frame local namespace

Why exception name bindings are deleted while exiting the except block?

TL;DR: To prevent the creation of reference cycles.

Traceback object attached with any exception object has a stack frame of execution containing the local, global, builtin namespaces, the code object, and other necessary entities attached. This might seem complex at first but when we follow the attribute chain to get a higher level view, it would be easier to reason about.

Let's define a simple function that will do thing but return an intentionally raised exception (which is a bad idea, i'll explain the why in a jiffy):

def foobar(num):
    spam = 'egg'
    baz = num
    try:
        raise RuntimeError
    # Bind `RuntimeError` object to name `exc`
    except RuntimeError as exc:
        return exc

Read more…

Async `requests` in Python : Do async I/O on a blocking object

After the introduction of the send method of generators in Python 2.5, it was possible to send values into generators, essentially making them a two-way object. This leads to the possibility of single-threaded coroutine-based co-operative multitasking. Before 3.5, one could create a coroutine by the asyncio.coroutine (or types.coroutine) decorator, and pause a coroutine (when doing I/O) by using yield from which basically is a syntactic sugar for yield-ing values from another generator (using a for loop, for example).

In Python 3.5, the async-await keywords were introduced to support the Async operations natively, with:

  • The async keyword replaces the need for an explicit decorator to create a coroutine
  • The await is just a syntactic sugar for yield from

Read more…

Brief intro to `strace` : Finding all files a process is opening in GNU/Linux

Many times while debugging, we need to find all the files (e.g. shared libraries, configuration files, caches, logs, dumps, state files, journals, and so on) a process is opening for reading data from to get hints about the program's design and workflow.

Otherwise we want to follow the lengthy route of reading the source code of the program, we could leverage the powerful system call tracing tool strace to get the job done fairly easily.

Background:

strace binary comes with the strace package; so we need to install it first (if not done already):

% dpkg -S "$(command -v strace)"
strace: /usr/bin/strace

Read more…