Image for post
Image for post
Docker volumes

Agenda

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


Image for post
Image for post

This post is targeted towards Celery beginners. It discusses different ways to run Celery.

  • Using celery with a single module
  • Using celery with multiple modules.
  • Using celery with multiple modules split across packages.

Prerequisites

You should have Celery and Redis installed. We will use Redis as our broker.

Using celery with single module

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…


Image for post
Image for post

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:

  • Attempted relative import in non-package
  • No module named package.module
  • You want to understand how relative imports work

Modules

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…


Image for post
Image for post
Photo by Suzanne D. Williams on Unsplash

Reduce your test execution time with mock

Agenda

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.

When to use mock library

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'
...: …


Let’s revise our annotations and aggregation knowledge

Image for post
Image for post
Photo by Adeolu Eletu on Unsplash

Agenda

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.

Models

Let’s consider the following models for this tutorial:

Models

We plan to calculate the following things in this piece:

  • Number of questions
  • Number of choices
  • Number of choices per question
  • Number of choices per question after filtering on question_text
  • Number of votes per question
  • Questions with a maximum number of votes
  • Questions with a minimum number of…


Image for post
Image for post
Photo by Mohamed Ajufaan on Unsplash

Agenda

This post assumes a basic familiarity with Django REST Framework.

We would discuss the following:

  • How to add custom field validation
  • How to add cross field validation
  • When and how to override to_internal_value()
  • When and how to override create()

Basic serializer

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]: True
In [5]: serializer.save()
Out[5]: <User: john>

Custom field validation

Custom field validation can be accomplished by providing a method validate_<field_name>. …


Image for post
Image for post
Series is the backbone of DataFrame

Agenda

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.

What is a series

A Pandas series can be conceptualized in two ways. It can be envisioned as a single column of tabular data. …


Using the source argument, SerializerMethodField, and to_representation

Image for post
Image for post
Django REST framework serializers

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:

  • Multiple ways of using the serializer’s source argument.
  • How and when to use SerializerMethodField.
  • How and when to use to_representation.

How To Use the Source Keyword Argument

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…


Set search fields using the query parameter

Image for post
Image for post
Dynamic search fields in DRF

The Django REST framework provides search functionality out of the box.

Search can be accomplished by following three steps:

  • Setting search_fields in the class
  • Setting filter_backends in the class
  • Sending search query parameter in the request

search 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.

Usefulness

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

Akshar Raaj

Engineer | Open Source | Blogger | Speaker. My posts have been read over a million times. https://www.agiliq.com/authors/#Akshar https://bit.ly/2TaRqji

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store