Skip to main content

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…

Joined EatingHabits as CTO

Very recently, I've started a new journey by joining a Barcelona based startup, EatingHabits, as the Chief Technology Officer (CTO).

I'm actually the technical co-founder of the startup, where my job is basically to handle all the tech related stuff, and making sure everything is properly synchronized as far as the tech stack is concerned.

Read more…

Dear Developers, Let's bring some empathy to the table!

I won't probably be too wrong to say that, System administration is probably the most underappreciated and thankless genre among work units in any software development organization nowadays. So let’s talk about that in urging the snarky developers to change their point of view towards fellow system administrators.

We're living in the era of web applications, and almost all softwares are run on the someone else’s computer AKA cloud. So, software engineering/development has changed a lot; we've come from developing host-native apps to developing cloud-native apps. To keep up the pace, System administration has changed a lot as well; from manually building systems from scratch, we've introduced fully automated system building, packaging and what not!

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
        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…