<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jelan Mathew James on Medium]]></title>
        <description><![CDATA[Stories by Jelan Mathew James on Medium]]></description>
        <link>https://medium.com/@jelanmathewjames1234?source=rss-a1e03849802e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DLPNqe6CaZ4T7eDI5kOTpg.jpeg</url>
            <title>Stories by Jelan Mathew James on Medium</title>
            <link>https://medium.com/@jelanmathewjames1234?source=rss-a1e03849802e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 22:02:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jelanmathewjames1234/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[It’s Been a Decade Since Python Made __init__.py Optional. Do You Know Why?]]></title>
            <link>https://medium.com/@jelanmathewjames1234/its-been-a-decade-since-python-made-init-py-optional-do-you-know-why-6cc4db808255?source=rss-a1e03849802e------2</link>
            <guid isPermaLink="false">https://medium.com/p/6cc4db808255</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[Jelan Mathew James]]></dc:creator>
            <pubDate>Wed, 19 Nov 2025 11:05:00 GMT</pubDate>
            <atom:updated>2025-11-19T11:28:27.137Z</atom:updated>
            <content:encoded><![CDATA[<p>We still dutifully create <em>__init__.py</em> files to make our Python directories into packages. It is muscle memory for most developers. But did you know that since the release of <strong>Python 3.3</strong> on <strong>September 29, 2012</strong>, this file has actually been optional?</p><p>This change wasn’t just about saving you the trouble of creating an empty file or simplifying folder structures. It was a deliberate move to solve a deep architectural challenge known as<strong> Namespace Packages</strong>. Understanding this shift is the key to understanding how to write modern, scalable Python code.</p><p>To understand why this happened, we first need to know what is a <strong>Regular Packages</strong> and <strong>Namespace Packages</strong></p><p>🗂️ <strong>The Difference: Regular vs. Namespace Packages</strong><br>For most of us, a package is a simple concept. You have a directory, you put an <em>__init__.py</em> inside it, and it becomes a <strong>Regular Package</strong>. This file is the anchor; it tells Python that this specific directory is a single, cohesive unit containing modules. It is usually installed as a single artifact.</p><p>A <strong>Namespace Package</strong> is completely different.</p><p>Imagine you are <strong>Google.</strong></p><p>You maintain hundreds of different cloud services —</p><p><strong>Cloud Storage, BigQuery, Vision AI, Translation</strong>, and more.</p><p>You have a problem: You cannot ship a single, massive <strong>5GB</strong> installer containing the code for every single service you offer. It would be bloated and impossible to manage. A user who just wants to upload a file to Cloud Storage shouldn’t be forced to download the code for Vision AI.</p><p>So, you split them into independent installers that users can grab from</p><pre>pip install google-cloud-storage<br>pip install google-cloud-bigquery</pre><p>However, to keep your code organized and professional, you want them all to live under the same “<strong>Google</strong>” umbrella in the code. You want the user to be able to type:</p><pre>from google.cloud import storage<br>from google.cloud import bigquery</pre><p>This is the challenge. You have two completely different libraries, installed at different times, trying to live inside the exact same google folder on your computer.</p><p>If google were a <strong>Regular Package</strong>, the first library you installed (e.g., Storage) would create a <strong><em>google/__init__.py</em></strong>. When you tried to install the second one <strong>(BigQuery)</strong>, it would see that <strong><em>google/ folder</em> </strong>and overwrite the <em>__init__.py</em>. The first library would break.</p><p>By making google a <strong>Namespace Package</strong>, both installers can dump their files into the <strong>google/</strong> directory without fighting over a shared <em>__init__.py</em> file. Python simply looks at the folder, sees no <em>__init__.py</em>, and merges the contents of both libraries into one seamless experience.</p><p>❌ <strong>The Problem: Why __init__.py Failed Namespace Packages</strong><br>Before <strong>Python 3.3</strong>, creating a namespace package was a headache because of one specific rule: The Last Write Wins.</p><p>If you tried to make google a regular package, every distribution (database, utils, web) would need to include an <strong><em>google/__init__.py </em></strong>file. When a user installed the second distribution, its <strong><em>__init__.py</em></strong> would overwrite the first one. If the first file had important code, it was lost. You simply could not share a directory name across multiple installations if that directory required a single physical file to exist.</p><p>🛠️ <strong>The “Hack” Era: <em>pkgutil</em> and <em>setuptools</em></strong><br>To get around this, developers used two legacy methods, both of which were brittle.</p><ul><li>The first was the <strong><em>pkgutil</em></strong> method. Developers had to put a specific “magic spell” inside the <strong><em>__init__.py</em></strong> file:</li></ul><pre>from pkgutil import extend_path; <br>__path__ = extend_path(__path__, __name__)</pre><p>This code told Python, “Don’t stop looking here! Go check the rest of the system path for other folders with this name and merge them.”</p><pre>FOLDER STRUCTURE<br><br>site-packages/<br>├── my_namespace_fragment_a/  &lt;-- Unique directory for Distribution A<br>│   └── my_namespace/         &lt;-- The actual package directory<br>│       ├── __init__.py       &lt;-- MUST contain the extend_path code<br>│       └── submodule_a.py    &lt;-- The unique module from Distribution A<br>└── my_namespace_fragment_b/  &lt;-- Unique directory for Distribution B<br>    └── my_namespace/         &lt;-- The actual package directory<br>        ├── __init__.py       &lt;-- MUST contain the extend_path code<br>        └── submodule_b.py    &lt;-- The unique module from Distribution B</pre><ul><li>The second was the <strong><em>setuptools</em></strong> method. This involved declaring <strong><em>namespace_packages</em></strong> in the setup configuration. Setuptools would then create special <strong><em>.pth</em></strong> files or unique directory structures to trick Python into looking in multiple places.</li></ul><pre>FOLDER STRUCTURE<br><br>site-packages/<br>├── my_namespace_fragment_a/  &lt;-- Unique, isolated directory for Distribution A<br>│   └── my_namespace/         &lt;-- The package&#39;s folder<br>│       ├── __init__.py       &lt;-- Contains custom logic (safe from overwrite)<br>│       └── submodule_a.py<br>├── my_namespace_fragment_b/  &lt;-- Unique, isolated directory for Distribution B<br>│   └── my_namespace/         &lt;-- The package&#39;s folder<br>│       ├── __init__.py       &lt;-- Contains custom logic (safe from overwrite)<br>│       └── submodule_b.py<br>└── easy-install.pth          &lt;-- Installer file that registers both paths</pre><p>Both methods were complex. They required boilerplate code, and if one developer forgot the magic lines, the whole namespace broke.</p><p>✨ <strong>The Solution: Implicit Namespace Packages</strong><br>Python 3.3 introduced the elegant solution: Implicit Namespace Packages. The logic was simple: if a directory acts as a container for other modules, why does it need a file inside it at all?</p><p>They changed the import machinery to follow a new rule. When Python scans your directories looking for a package:</p><p>If it finds a directory with <strong><em>__init__.py</em></strong>, it treats it as a <strong>Regular Package </strong>and stops searching.</p><p>If it finds a directory without <strong><em>__init__.py</em></strong>, it assumes it is a <strong>Namespace</strong>. It records the directory path and continues searching the rest of the system path for other directories with the same name.</p><pre>FOLDER STRUCTURE<br><br>site-packages/<br>└── my_namespace/             &lt;-- The shared directory name<br>    ├── submodule_a.py        &lt;-- Module contributed by Distribution A<br>    └── submodule_b.py        &lt;-- Module contributed by Distribution B<br>    <br>    // **CRITICAL POINT: NO __init__.py FILE HERE**</pre><p>Once it finishes scanning, it merges all those directories into a single virtual package. This made namespace packages incredibly easy to develop. You simply delete the <strong><em>__init__.py</em></strong> file from the top-level folder, and Python handles the merging automatically. No conflicts, no overwrites, no magic code.</p><p>🤔 <strong>So, Why Do We Still Use __init__.py?</strong><br>If namespace packages are so easy, why haven’t we stopped using <strong><em>__init__.py</em></strong> entirely?</p><p>The answer is that Implicit Namespace Packages are purely containers. Because they don’t have an <strong><em>__init__.py</em></strong> file, there is no place to write code that runs when the package is imported.</p><p>We still use <strong><em>__init__.py</em></strong> for Regular Packages because we need that functionality. We use it to:</p><p>Expose an API: You can import functions from sub-modules into the <strong><em>__init__.py</em></strong> so users can import them nicely.</p><pre># We can use this<br>from mypkg import run<br><br># Instead of <br>from mypkg.core.runner import run</pre><p>Run Initialization Logic: If your package needs to check environment variables, set up logging, or validate versions on startup, that code must live in <strong><em>__init__.py</em></strong>.</p><p>Create a Boundary: It explicitly marks a directory as a self-contained library that shouldn’t be merged with anything else.</p><p>In summary: Use <strong><em>__init__.py</em></strong> when you are building a standalone library. Delete it only when you need to split a single namespace across multiple distributions.</p><p><strong>References</strong></p><p><a href="https://peps.python.org/pep-0420/">https://peps.python.org/pep-0420/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6cc4db808255" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Keep Logic Out of DRF Views]]></title>
            <link>https://medium.com/@jelanmathewjames1234/build-crud-apis-in-django-rest-framework-with-minimal-code-ee8c7ea42a60?source=rss-a1e03849802e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee8c7ea42a60</guid>
            <category><![CDATA[django-framework]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Jelan Mathew James]]></dc:creator>
            <pubDate>Mon, 10 Nov 2025 17:16:01 GMT</pubDate>
            <atom:updated>2025-11-28T04:58:19.052Z</atom:updated>
            <content:encoded><![CDATA[<p>When building REST APIs in Django REST Framework (DRF), we often repeat the same code in views for simple CRUD operations. But what if we could build fully functional APIs with zero business logic inside the view just specify the model, serializer, and pagination, and let DRF handle the rest?</p><p>In this article, we’ll explore how to do this using DRF’s built-in ViewSets, focusing on how to achieve minimal-logic CRUD operations.</p><p>⚙️ Prerequisites</p><ul><li>Basic understanding of Django and DRF</li></ul><p>🏗️ My Project Setup</p><ul><li>uv==0.9.8</li><li>python&gt;=3.12</li><li>django&gt;=5.2.8</li><li>djangorestframework&gt;=3.16.1</li></ul><p>Set up a Django REST framework project.</p><p><a href="https://www.django-rest-framework.org/tutorial/quickstart/">Quickstart</a></p><ol><li><strong>Create a app named book and create a Book model</strong></li></ol><pre>uv run python manage.py startapp book<br>touch book/urls.py book/serializers.py</pre><pre># models.py<br>from django.db import models<br><br>class Book(models.Model):<br>    title = models.CharField(max_length=200)<br>    author = models.CharField(max_length=100)<br>    published_date = models.DateField()<br><br>    def __str__(self):<br>        return self.title</pre><p>2. <strong>Create the Serializer</strong></p><p>DRF’s ModelSerializer automatically generates fields from your model no extra work required.</p><pre># serializers.py<br>from rest_framework import serializers<br>from .models import Book<br><br>class BookSerializer(serializers.ModelSerializer):<br>    class Meta:<br>        model = Book<br>        fields = &#39;__all__&#39;</pre><p>3. <strong>Create the View</strong></p><p>Here’s the magic part the ModelViewSet class provides built-in actions (list, create, retrieve, update, destroy), so you don’t have to write them manually.</p><pre># views.py<br>from rest_framework import viewsets<br>from rest_framework.pagination import PageNumberPagination<br>from .models import Book<br>from .serializers import BookSerializer<br><br>class BookPagination(PageNumberPagination):<br>    page_size = 10<br><br>class BookViewSet(viewsets.ModelViewSet):<br>    queryset = Book.objects.all()<br>    serializer_class = BookSerializer<br>    pagination_class = BookPagination</pre><p>That’s it.<br>No logic for CRUD methods DRF takes care of it.</p><p>4. <strong>Routing the ViewSet</strong></p><p>DRF’s DefaultRouter automatically generates the URL patterns for all CRUD endpoints.</p><pre># urls.py<br>from rest_framework.routers import DefaultRouter<br>from .views import BookViewSet<br><br>router = DefaultRouter()<br>router.register(r&#39;books&#39;, BookViewSet, basename=&#39;book&#39;)<br><br>urlpatterns = router.urls</pre><p>link this urlpatterns with your main projects urls.py</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0F9nl8LoAGxpJ2FgmtJmVQ.png" /><figcaption>Generated Endpoints</figcaption></figure><p>5. <strong>Add Filtering, Searching, Ordering and extra actions (Optional)</strong></p><p>You can still keep it low-code and extend features easily. Extra actions automatically create new API routes, and DRF handles the URL registration for you.</p><pre># views.py<br>from rest_framework import filters<br>from rest_framework.decorators import action<br><br>class BookViewSet(viewsets.ModelViewSet):<br>    queryset = Book.objects.all()<br>    serializer_class = BookSerializer<br>    pagination_class = BookPagination<br>    filter_backends = [filters.SearchFilter, filters.OrderingFilter]<br>    search_fields = [&#39;title&#39;, &#39;author&#39;]<br>    ordering_fields = [&#39;published_date&#39;]<br><br>    @action(detail=True, methods=[&#39;post&#39;])<br>    def custom_action(self, request, pk=None):<br>        ...</pre><p>/books/?search=tolkien <br>/books/?ordering=-published_date</p><p>6. <strong>Why This Approach Is a Minimal-Logic API Setup</strong></p><p>✅ No custom CRUD logic — everything is pre-built<br>✅ Automatic URL generation<br>✅ Simple pagination and filtering setup<br>✅ Easily extendable with permissions, throttling, etc.</p><p><strong>Bonus Want More Control?</strong></p><p>You can switch from prebuilt ViewSets to GenericAPIView + mixins for finer customization — while still keeping the code minimal:</p><pre>from rest_framework import generics<br>from .models import Book<br>from .serializers import BookSerializer<br><br>class BookListCreateView(generics.ListCreateAPIView):<br>    queryset = Book.objects.all()<br>    serializer_class = BookSerializer<br><br>class BookDetailView(generics.RetrieveUpdateDestroyAPIView):<br>    queryset = Book.objects.all()<br>    serializer_class = BookSerializer</pre><p>References:</p><ul><li><a href="https://www.django-rest-framework.org/tutorial/quickstart/">Quickstart</a></li><li><a href="https://www.django-rest-framework.org/api-guide/viewsets/#readonlymodelviewset">Viewsets - Django REST framework</a></li><li><a href="https://github.com/encode/django-rest-framework/">GitHub - encode/django-rest-framework: Web APIs for Django. 🎸</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee8c7ea42a60" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>