This post is an attempt to understand Docker volumes. We will discuss:
- Why volumes are required
- What are volumes
- How to work with volumes
This post assumes that you have a basic understanding of Docker images and containers.
Why and when to use volumes
Let’s understand the purpose volumes serve. Docker volumes come into picture when we want to persist the data generated while working on a Docker container.
Assume you are using Docker to run your database. You would expect this data to be persisted across different runs of the container.
Similarly, suppose you are using Docker to run your queue, say Rabbitmq. You would expect this data to be persisted across different runs of the container. …
This post is targeted towards Celery beginners. It discusses different ways to run Celery.
You should have Celery and Redis installed. We will use Redis as our broker.
Let’s create a module called tasks.py
with the following content:
from celery import Celeryapp = Celery('tasks', broker='redis://localhost')@app.task
def hello():
return 'hello'
Let’s start the Celery worker
celery -A tasks worker -l INFO
Let’s start a shell and queue a task.
In [1]: from tasks import hello
In [2]: hello.apply_async()
Out[2]: <AsyncResult…
This post attempts to clear frequently occurring confusion around Python modules and packages.
You should read this post if you frequently grapple with the following errors:
We will create two Python modules. These modules are without a package.
A Python package doesn’t come into picture unless there is an __init__.py
defined.
Let’s create a directory called anydir
in your home directory.
╰─$ mkdir anydir
╰─$ cd anydir
Let’s create a Python module called hello.py
with following code:
def hello():
return 'hello'
Let’s create another Python module called say_hello.py
with the following…
This post will cover when and how to use unittest.mock
library.
Python docs aptly describe the mock library:
unittest.mock allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.
The short answer is ‘most of the times’. The following examples would make this claim clearer:
Define the following three functions on shell.
In [1]: def cow():
...: print("cow")
...: return {'cow': 'moo'}
...:In [2]: def dog():
...: print("dog")
...: return {'dog': 'bark'}
...:In [3]: def animals():
...: data = cow()
...: data.update(dog())
...: data['pig'] = 'oink'
...: …
Django’s mechanism for performing a SQL group by
is through annotate
and aggregate
.
In this piece, let’s revise our annotate and aggregate knowledge.
This post is beginner-friendly and you should be able to follow along even if you haven’t previously used Django annotations.
Let’s consider the following models for this tutorial:
We plan to calculate the following things in this piece:
This post assumes a basic familiarity with Django REST Framework.
We would discuss the following:
Let’s write a serializer which will allow creating User
instances.
Let’s validate some data and create a user.
In [1]: from accounts.serializers import UserSerializerIn [2]: data = {'first_name': 'john', 'last_name': 'doe', 'username': 'john', 'password': 'abc123'}In [3]: serializer = UserSerializer(data=data)In [4]: serializer.is_valid()
Out[4]: TrueIn [5]: serializer.save()
Out[5]: <User: john>
Custom field validation can be accomplished by providing a method validate_<field_name>
. …
The two central data structures of Pandas are Series and DataFrame. This post is an attempt to have a proper understanding of Pandas series.
The foundation of a DataFrame is a Series. The docstring of DataFrame defines a DataFrame as:
Can be thought of as a dict-like
container for Series objects
Many operations on dataframe return series instance. It is thus essential that we have a solid understanding of Series.
A Pandas series
can be conceptualized in two ways. It can be envisioned as a single column of tabular data. …
This post assumes a basic familiarity with the Django REST framework.
We will discuss how we can effectively use serializers during read operations. We will look at three powerful features that will help us achieve the desired results with less code.
We will discuss:
source
argument.SerializerMethodField
.to_representation
.The DRF serializer provides a keyword argument, called source
, which is extremely smart and can help avoid a lot of common patterns.
Let’s write a serializer that can create serialized representations of a User
.
Let’s use this serializer to serialize a…
The Django REST framework provides search functionality out of the box.
Search can be accomplished by following three steps:
search_fields
in the classfilter_backends
in the classsearch
query parameter in the requestsearch
specifies the pattern that needs to be matched. search_fields
specify the database columns against which the pattern needs to be matched.
DRF default behavior works with static search_fields
.
In this piece, we discuss the limitations of static search_fields
and how to make search_fields
dynamic.
Dynamic searching ability in the back end will allow users to perform more granular searches.
This will come in handy in a scenario where your front end lists all the fields/columns of the model and allows users to select the fields against which the pattern should be matched. …
About