This post is a compilation of all the answers of mine (topic-wise, across the network) that are my personal favorites (each one worthy of a blog post).
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):
Django by default uses the
username field for authentication purposes of a user, but it's becoming a common trend to use
username field can be somewhat dropped. Also, the
username can be used, so it does not break any internal identification structure as well.
In the later sections, when I mentiond the (default)
User model please think of the default Django
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
User model for authentication purposes.
Now, let's check out the usual ways to modify/extend the django
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.
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!
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
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.
Why exception name bindings are deleted while exiting the
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
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
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
await keywords were introduced to support the Async operations natively, with:
asynckeyword replaces the need for an explicit decorator to create a coroutine
awaitis just a syntactic sugar for
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.
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
While doing e2e (end to end) tests for Dealiable.com frontend (
Vue.JS), i've bumped into something interesting, which was really fun to fix/find a workaround of.
Firstly, for e2e tests, i've used Nightwatch.js, which uses the Selenium API, and is written in Node.js.
The problem was that, I was not being able to clear some
input tag field data using the API provided by Nightwatch --
browser.clearValue. For example: