<?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 Hari Hara Subramanyam S on Medium]]></title>
        <description><![CDATA[Stories by Hari Hara Subramanyam S on Medium]]></description>
        <link>https://medium.com/@HariHaran15?source=rss-d33dcc1d7536------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nzEwPef4H1j-9NC801ABvA.jpeg</url>
            <title>Stories by Hari Hara Subramanyam S on Medium</title>
            <link>https://medium.com/@HariHaran15?source=rss-d33dcc1d7536------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 01:32:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@HariHaran15/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[Part 3 of 4 — Building Production-Ready Flask Apps]]></title>
            <link>https://medium.com/@HariHaran15/part-3-of-4-building-production-ready-flask-apps-955247d9e94a?source=rss-d33dcc1d7536------2</link>
            <guid isPermaLink="false">https://medium.com/p/955247d9e94a</guid>
            <dc:creator><![CDATA[Hari Hara Subramanyam S]]></dc:creator>
            <pubDate>Fri, 20 Nov 2026 06:21:22 GMT</pubDate>
            <atom:updated>2026-11-20T06:21:22.412Z</atom:updated>
            <content:encoded><![CDATA[<p>One command to Rule Them All: Dockerizing Flask, Celery &amp; Redis</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uSRZbhQs4hvCOTYw" /><figcaption>Photo by <a href="https://unsplash.com/@brechtcorbeel?utm_source=medium&amp;utm_medium=referral">Brecht Corbeel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>By the end of Part 2, you had a working Flask + Celery + Redis stack. But running it required three open terminal windows, a locally installed Redis, and everyone on your team having the exact same environment set up. Ask a new colleague to get it running and you’ll spend an hour debugging their machine.</p><p>Docker fixes this. One command and the whole stack comes up — Flask, Celery worker, Redis, all of it. And because everything runs in containers, it’s identical on your laptop, your teammate’s laptop, and your production server.</p><p>Here’s what we’re covering:</p><ul><li>Writing a Dockerfile for the Flask app</li><li>Running the Celery worker from the same image(no second Dockerfile needed)</li><li>Wiring everything together with Docker Compose</li><li>Handling environment variables properly</li><li>Multi-stage builds to keep your image lean</li><li>Getting the whole stack running with one command</li></ul><p><strong>The Big Picture</strong></p><p>Before we write anything, here’s what we’re building:</p><pre>┌─────────────────────────────────────────────┐<br>│              docker-compose                 │<br>│                                             │<br>│  ┌──────────┐   ┌──────────┐  ┌─────────┐   │<br>│  │  Flask   │   │  Celery  │  │  Redis  │   │<br>│  │  (web)   │   │ (worker) │  │         │   │<br>│  └──────────┘   └──────────┘  └─────────┘   │<br>│       │               │             │       │<br>│       └───────────────┴─────────────┘       │<br>│                  same network               │<br>└─────────────────────────────────────────────┘</pre><p>Flask and the Celery worker run from the <strong>same Docker image</strong> — same code, same dependencies. The only difference between them is the command used to start each one. Redis comes from the official Redis image. Docker Compose handles the networking so services can talk to each other by name instead of IP address.</p><p><strong>The Dockerfile</strong></p><p>Create a Dockerfile in your project root:</p><pre>dockerfile<br># ── Stage 1: Builder ──────────────────────────────────────────────────────────<br>FROM python:3.12-slim AS builder<br><br>WORKDIR /app<br><br>COPY requirements.txt .<br>RUN pip install --no-cache-dir --prefix=/install -r requirements.txt<br><br><br># ── Stage 2: Production ───────────────────────────────────────────────────────<br>FROM python:3.12-slim<br><br>WORKDIR /app<br><br># Copy installed packages from builder — not pip, not build tools<br>COPY --from=builder /install /usr/local<br><br>COPY . .<br><br># Don&#39;t run as root<br>RUN useradd --create-home appuser<br>USER appuser<br><br>EXPOSE 5000<br><br>CMD [&quot;gunicorn&quot;, &quot;--bind&quot;, &quot;0.0.0.0:5000&quot;, &quot;--workers&quot;, &quot;2&quot;, &quot;run:app&quot;]</pre><p>A few things worth explaining here.</p><p><strong>Multi-stage build. </strong>The builder stage installs all your dependencies. The final stage copies only the installed packages — not pip, not any build tools. Smaller image, smaller attack surface. Worth the extra four lines.</p><p><strong>python:3.12-slim </strong>not<strong> </strong><strong>python:3.12-alpine </strong>Alpine uses muslibc instead of glibc, which causes weird compilation failures with some Python packages. Slim is a Debian-based, strips out unnecessary stuff, and actually works. Use slim.</p><p><strong>Non-root user. </strong>Running as appuser instead of root is basic security hygiene. If something goes sideways inside the container, you want the blast radius contained.</p><p><strong>Gunicorn insteaf of Flask’s dev server. </strong>Flask’s built-in server is single-threaded and definitely not for production. Gunicorn handles concurrent requests properly. Add it to your requirements:</p><pre>pip install gunicorn<br>pip freeze &gt; requirements.txt</pre><p><strong>Docker Compose</strong></p><p>Create docker-compose.yml :</p><pre>version: &quot;3.9&quot;<br><br>services:<br><br>  redis:<br>    image: redis:7-alpine<br>    ports:<br>      - &quot;6379:6379&quot;<br>    volumes:<br>      - redis_data:/data<br>    healthcheck:<br>      test: [&quot;CMD&quot;, &quot;redis-cli&quot;, &quot;ping&quot;]<br>      interval: 5s<br>      timeout: 3s<br>      retries: 5<br><br>  web:<br>    build: .<br>    ports:<br>      - &quot;5000:5000&quot;<br>    env_file:<br>      - .env<br>    depends_on:<br>      redis:<br>        condition: service_healthy<br>    volumes:<br>      - .:/app<br>    command: &gt;<br>      gunicorn<br>        --bind 0.0.0.0:5000<br>        --workers 2<br>        --reload<br>        run:app<br><br>  worker:<br>    build: .<br>    env_file:<br>      - .env<br>    depends_on:<br>      redis:<br>        condition: service_healthy<br>    volumes:<br>      - .:/app<br>    command: &gt;<br>      celery<br>        -A app.celery_app:celery<br>        worker<br>        --loglevel=info<br>        --concurrency=2<br><br>  flower:<br>    build: .<br>    ports:<br>      - &quot;5555:5555&quot;<br>    env_file:<br>      - .env<br>    depends_on:<br>      - worker<br>    command: &gt;<br>      celery<br>        -A app.celery_app:celery<br>        flower<br>        --port=5555<br><br>volumes:<br>  redis_data:</pre><p>web , worker and flower all use build: — the same Dockerfile. The only thing that differs between them is command . That’s the whole trick.</p><p>depends_on with condition: service_healthy is important. Without it, Flask and Celery will try to connect to Redis before Redis is ready and you’ll see a bunch of connection errors on startup. The health check makes the other services wait until Redis responds to ping .</p><p><strong>Environment Variables (Do This Right)</strong></p><p>Create a .env file:</p><pre># Flask<br>SECRET_KEY=change-this-in-production<br>FLASK_ENV=development<br><br># Celery / Redis<br># &#39;redis&#39; is the service name in docker-compose — not localhost<br>CELERY_BROKER_URL=redis://redis:6379/0<br>CELERY_RESULT_BACKEND=redis://redis:6379/0</pre><p>This trips everyone up the first time: the Redis hostname is redis , not localhost . Inside Docker Compose, services resolve each other by service name. localhost inside a container means that container itself, not your other services.</p><p>Add .env to .gitignore immediately:</p><pre>echo &quot;.env&quot; &gt;&gt; .gitignore</pre><p>Then commit a .env.example so other people know what to fill in:</p><pre># .env.example — copy to .env and fill in real values<br>SECRET_KEY=<br>FLASK_ENV=development<br>CELERY_BROKER_URL=redis://redis:6379/0<br>CELERY_RESULT_BACKEND=redis://redis:6379/0</pre><p>And update app/__init__.py to read from the environment instead of hardcoded values:</p><pre>import os<br>from flask import Flask<br>from app.celery_app import make_celery<br><br>celery = None<br><br>def create_app():<br>    global celery<br>    app = Flask(__name__)<br><br>    app.config.update(<br>        SECRET_KEY=os.environ.get(&quot;SECRET_KEY&quot;, &quot;dev-fallback-secret&quot;),<br>        CELERY_BROKER_URL=os.environ.get(&quot;CELERY_BROKER_URL&quot;, &quot;redis://localhost:6379/0&quot;),<br>        CELERY_RESULT_BACKEND=os.environ.get(&quot;CELERY_RESULT_BACKEND&quot;, &quot;redis://localhost:6379/0&quot;),<br>    )<br><br>    celery = make_celery(app)<br><br>    from app.routes.tasks import tasks_bp<br>    app.register_blueprint(tasks_bp)<br><br>    return app</pre><p>The fallback values let you still run outside Docker for quick testing if you ever need to.</p><p><strong>Let’s Run It</strong></p><p>Build the image:</p><pre>docker compose build</pre><pre>docker compose up</pre><p>You’ll see logs from all four services interleaved in the same terminal. To run it in the background:</p><pre>docker compose up -d</pre><p>Check that everything came up healthy:</p><pre>docker compose ps</pre><p>You want to see something like this:</p><pre>NAME                    IMAGE           STATUS          PORTS<br>task-manager-flower-1   task-manager    Up              0.0.0.0:5555-&gt;5555/tcp<br>task-manager-redis-1    redis:7-alpine  Up (healthy)    0.0.0.0:6379-&gt;6379/tcp<br>task-manager-web-1      task-manager    Up              0.0.0.0:5000-&gt;5000/tcp<br>task-manager-worker-1   task-manager    Up</pre><p>Give it a test:</p><pre>curl -X POST http://localhost:5000/tasks/ \<br>  -H &quot;Content-Type: application/json&quot; \<br>  -d &#39;{&quot;title&quot;: &quot;Containerized task&quot;}&#39;</pre><p>Hit http://localhost:5555 for Flower. Your task should show up there within a few seconds.</p><p>Commands You’ll Actually Use</p><p>While you’re working on this day to day:</p><pre># Rebuild after changing requirements.txt<br>docker compose build<br><br># Tail logs for a specific service<br>docker compose logs -f worker<br><br># Run a one-off command inside the web container<br>docker compose exec web python -c &quot;from app import create_app; print(&#39;OK&#39;)&quot;<br><br># Stop everything (keeps volumes — Redis data survives)<br>docker compose down<br><br># Nuclear option — blow away containers AND volumes<br>docker compose down -v<br><br># Restart just one service without stopping everything else<br>docker compose restart worker</pre><p><strong>Dev vs Production Compose Files</strong></p><p>The setup above is tuned for development — code is volume-mounted so changes show up immediately, Gunicorn has --reload on. You don’t want any of that in production.</p><p>Create docker-compose.prod.yml :</p><pre>version: &quot;3.9&quot;<br><br>services:<br><br>  web:<br>    build: .<br>    ports:<br>      - &quot;5000:5000&quot;<br>    env_file:<br>      - .env.prod<br>    depends_on:<br>      redis:<br>        condition: service_healthy<br>    # No volume mount — use the code baked into the image<br>    command: &gt;<br>      gunicorn<br>        --bind 0.0.0.0:5000<br>        --workers 4<br>        --access-logfile -<br>        --error-logfile -<br>        run:app<br><br>  worker:<br>    build: .<br>    env_file:<br>      - .env.prod<br>    depends_on:<br>      redis:<br>        condition: service_healthy<br>    command: &gt;<br>      celery<br>        -A app.celery_app:celery<br>        worker<br>        --loglevel=warning<br>        --concurrency=4</pre><p>Run it with:</p><pre>docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d</pre><p>The second file overrides values from the first. Keeps things clean — one source of truth for the base setup, production overrides layered on top.</p><p><strong>Where We’re At</strong></p><p>Your full project structure:</p><pre>task-manager/<br>├── app/<br>│   ├── __init__.py<br>│   ├── celery_app.py<br>│   ├── tasks/<br>│   │   ├── __init__.py<br>│   │   └── notifications.py<br>│   └── routes/<br>│       └── tasks.py<br>├── run.py<br>├── requirements.txt<br>├── Dockerfile<br>├── docker-compose.yml<br>├── docker-compose.prod.yml<br>├── .env                   ← local only, in .gitignore<br>└── .env.example           ← committed to git</pre><p>The whole stack spins up with one command. No more “works on my machine.” The image is lean, runs as a non-root user, and reads config from environment variables. You’re ready to ship this.</p><p><strong>What’s next</strong></p><p>In Part 4, we take this Docker setup and put it on an actual server. We’ll cover picking a cloud provider, getting Ngix in front of Gunicorn, managing production secrets, setting up logging, and making sure the Celery worker comes back up if it crashes.</p><p>Catch you in Part 4.</p><blockquote><a href="https://medium.com/@HariHaran15/part-2-of-4-building-production-ready-flask-apps-127a50beee79">Part 2 of 4 — Building Production-Ready Flask Apps</a></blockquote><p><em>Watch this space for more articles on Python. If you like to read more of my articles do follow me on </em><a href="https://medium.com/@HariHaran15"><strong>Medium </strong></a><em>and </em><a href="https://www.linkedin.com/in/hari-hara-subramanyam-s-b2692636"><strong>LinkedIn</strong></a><strong>.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=955247d9e94a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Part 2 of 4 — Building Production-Ready Flask Apps]]></title>
            <link>https://medium.com/@HariHaran15/part-2-of-4-building-production-ready-flask-apps-127a50beee79?source=rss-d33dcc1d7536------2</link>
            <guid isPermaLink="false">https://medium.com/p/127a50beee79</guid>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[background-task]]></category>
            <category><![CDATA[celery-flower]]></category>
            <category><![CDATA[python-programming]]></category>
            <dc:creator><![CDATA[Hari Hara Subramanyam S]]></dc:creator>
            <pubDate>Sun, 03 May 2026 17:22:35 GMT</pubDate>
            <atom:updated>2026-05-03T17:27:45.245Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*68saFpoGloOx0-Ht" /><figcaption>Photo by <a href="https://unsplash.com/@brechtcorbeel?utm_source=medium&amp;utm_medium=referral">Brecht Corbeel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Making Your Flask App Actually Fast: Background Jobs with Celery &amp; Redis</p><p>In Part 1, we built a Flask task manager. Everything works fine when requests are quick. But here’s where things get messy in the real world.</p><p>Let’s say your app needs to send an email when someone creates a task. Or generate a report. Or call some slow third-party API. If you do all that inside the request handler, your users are just sitting there waiting. Seconds can feel like forever. And if you’ve got a bunch of users doing this at the same time? Your server basically becomes a queue and everything crawls.</p><p>The fix? Don’t make the user wait. Hand that slow work off to a background worker and come back to them with a 200 response immediately. That’s what Celery and Redis do for you.</p><p>Here’s what we’re gonna cover:</p><ul><li>The problem with doing slow stuff inside request handlers.</li><li>How Celery actually works(it’s not magic)</li><li>Getting Redis set up</li><li>Writing background tasks that don’t suck</li><li>How to check on what your workers are doing(Flower)</li><li>Adding email notifications to our task manager.</li></ul><p><strong>Why Your Sync Code Breaks</strong></p><p>Let me show you what goes wrong. Here’s what happens when you put slow work in your route:</p><pre>@tasks_bp.route(&quot;/&quot;, methods=[&quot;POST&quot;])<br>def create_task():<br>    task = build_task(request.get_json())<br>    tasks.append(task)<br>    <br>    send_email_notification(task)  # Oops, 2-3 seconds every time<br>    <br>    return jsonify(task), 201</pre><p>Now every request to create a task takes atleast 2–3 seconds just waiting for the email to send. If you’ve got even 10 people hitting this endpoint simultaneously? Your server’s gonna be tied up and everything slows down to a crawl. That’s what background queues are for.</p><p>What we want is something like this:</p><pre>Client → Flask (returns in 50ms) → Redis Queue → Background Worker (handles the slow stuff)</pre><p><strong>How Celery Actually Works</strong></p><p>Celery is a distributed job queue. You tell it “Hey, run this function in the background” and it figures out the rest. The way it does this is by using a message broker — think of it like a middleman that says “Flask, I’ll take that task” and then hands it to a worker process.</p><p>Here’s the flow:</p><ol><li>You call a Celery task from your Flask app(it’s async, so it returns immediately)</li><li>The task gets dumped onto a message queue(Redis in our case)</li><li>A background worker picks it up and runs it</li><li>If you care about the result, it gets stored somewhere you can check later(also Redis)</li></ol><p>That’s it. Redis is perfect for this because it’s crazy fast and works great as both the queue and the results store. Plus everyone uses it, so there’s tons of examples and docs.</p><p><strong>Getting Setup</strong></p><p>First, install the packages:</p><pre>pip install celery redis flask-celery-helper<br>pip freeze &gt; requirements.txt</pre><p>You also need Redis actually running. On your local machine the easiest way is:</p><pre># On Mac<br>brew install redis &amp;&amp; brew services start redis<br><br># On Linux<br>sudo apt install redis-server &amp;&amp; sudo systemctl start redis<br><br># Or if you&#39;ve got Docker<br>docker run -d -p 6379:6379 redis:alpine</pre><p>Test that it’s working:</p><pre>redis-cli ping<br># Should say PONG</pre><p>Simple enough.</p><p><strong>Hooking Celery Into Flask</strong></p><p>Your project structure will look like this now:</p><pre>task-manager/<br>├── app/<br>│   ├── __init__.py<br>│   ├── celery_app.py      &lt;- new<br>│   ├── tasks/<br>│   │   ├── __init__.py<br>│   │   └── notifications.py   &lt;- new<br>│   └── routes/<br>│       └── tasks.py<br>├── run.py<br>└── requirements.txt</pre><p>Create a file called app/celery_app.py :</p><pre>from celery import Celery<br><br>def make_celery(app):<br>    celery = Celery(<br>        app.import_name,<br>        backend=app.config[&quot;CELERY_RESULT_BACKEND&quot;],<br>        broker=app.config[&quot;CELERY_BROKER_URL&quot;],<br>    )<br>    celery.conf.update(app.config)<br><br>    class ContextTask(celery.Task):<br>        def __call__(self, *args, **kwargs):<br>            with app.app_context():<br>                return self.run(*args, **kwargs)<br><br>    celery.Task = ContextTask<br>    return celery</pre><p>That ContextTask thing might look weird but it’s actually important. Without it, your Celery tasks won’t have access to Flask’s current_app , your database, or any Flask extensions. They’ll just crash. So yeah, keep that in there.</p><p>Now upate app/__init__.py :</p><pre>from flask import Flask<br>from app.celery_app import make_celery<br><br>celery = None<br><br>def create_app():<br>    global celery<br>    app = Flask(__name__)<br>    <br>    app.config.update(<br>        SECRET_KEY=&quot;dev-secret-change-in-production&quot;,<br>        CELERY_BROKER_URL=&quot;redis://localhost:6379/0&quot;,<br>        CELERY_RESULT_BACKEND=&quot;redis://localhost:6379/0&quot;,<br>    )<br><br>    celery = make_celery(app)<br><br>    from app.routes.tasks import tasks_bp<br>    app.register_blueprint(tasks_bp)<br><br>    @app.errorhandler(404)<br>    def not_found(e):<br>        from flask import jsonify<br>        return jsonify({&quot;error&quot;: &quot;not found&quot;}), 404<br><br>    return app</pre><p>Just pointing Celery at your Redis instance and telling it to use Flask’s app context.</p><p><strong>Writing a Celery Task</strong></p><p>Create app/tasks/notifications.py :</p><pre>import time<br>from app import celery<br><br>@celery.task(bind=True, max_retries=3)<br>def send_task_notification(self, task_id, task_title):<br>    &quot;&quot;&quot;<br>    Simulate sending an email notification when a task is created.<br>    In production this would call an email service like SendGrid.<br>    &quot;&quot;&quot;<br>    try:<br>        print(f&quot;[Worker] Sending notification for task {task_id}: &#39;{task_title}&#39;&quot;)<br>        time.sleep(2)  # Simulate slow email API call<br>        print(f&quot;[Worker] Notification sent for task {task_id}&quot;)<br>        return {&quot;status&quot;: &quot;sent&quot;, &quot;task_id&quot;: task_id}<br>    except Exception as exc:<br>        # Retry with exponential backoff: 2s, 4s, 8s<br>        raise self.retry(exc=exc, countdown=2 ** self.request.retries)</pre><p>A couple things to notice here:</p><p><strong>bind=True </strong>means the task gets a reference to itself(that’s self ). This lets you do things like retry the task if it fails.</p><p><strong>max_retries=3 </strong>with exponential backoff is the way to go when you’re calling external services. Transient failures happen all the time(network hiccup, API temporarily down, whatever). You want automatic retries that back off exponentially — retry in 2 seconds, then 4, then 8. Don’t just give up silently or hammer the service on failure.</p><p><strong>Actually Calling Your Background Tasks</strong></p><p>Now update app/routes.tasks.py to use these background tasks:</p><pre>from flask import Blueprint, jsonify, request<br>from app.tasks.notifications import send_task_notification<br><br>tasks_bp = Blueprint(&quot;tasks&quot;, __name__, url_prefix=&quot;/tasks&quot;)<br>tasks = []<br>task_counter = 1<br><br><br>@tasks_bp.route(&quot;/&quot;, methods=[&quot;POST&quot;])<br>def create_task():<br>    global task_counter<br>    data = request.get_json()<br><br>    if not data or &quot;title&quot; not in data:<br>        return jsonify({&quot;error&quot;: &quot;title is required&quot;}), 400<br><br>    task = {<br>        &quot;id&quot;: task_counter,<br>        &quot;title&quot;: data[&quot;title&quot;],<br>        &quot;done&quot;: False,<br>    }<br>    tasks.append(task)<br>    task_counter += 1<br><br>    # Fire it off and don&#39;t wait around<br>    job = send_task_notification.delay(task[&quot;id&quot;], task[&quot;title&quot;])<br>    <br>    # Give back the job ID so they can check on it later if they want<br>    return jsonify({**task, &quot;notification_job_id&quot;: job.id}), 201</pre><p>The magic is .delay() . That puts the task on queue and comes right back- it doesn’t wait for anything. Your client gets their response in milliseconds, and the slow work happens whenever a worker picks it up. This is the whole point.</p><p><strong>Checking On Your Jobs</strong></p><p>Sometimes you want to know if a task is done or if it failed. Here’s an endpoint for that:</p><pre>from celery.result import AsyncResult<br><br>@tasks_bp.route(&quot;/jobs/&lt;job_id&gt;&quot;, methods=[&quot;GET&quot;])<br>def get_job_status(job_id):<br>    from app import celery<br>    job = AsyncResult(job_id, app=celery)<br>    <br>    response = {<br>        &quot;job_id&quot;: job_id,<br>        &quot;status&quot;: job.status,  # PENDING, STARTED, SUCCESS, FAILURE, RETRY<br>    }<br>    <br>    if job.status == &quot;SUCCESS&quot;:<br>        response[&quot;result&quot;] = job.result<br>    elif job.status == &quot;FAILURE&quot;:<br>        response[&quot;error&quot;] = str(job.result)<br>    <br>    return jsonify(response)</pre><p>This is polling — your client fires off a request, gets a job ID back, and can check in on it whenever. Not fancy, but it works. If you want real-time updates you’d use WebSockets, but honestly for most things polling is totally fine.</p><p><strong>Running It All</strong></p><p>You’re gonna need three separate terminal windows running at the same time. Go ahead and open them up:</p><p><strong>Terminal 1 — Your Flask app:</strong></p><pre>python run.py</pre><p><strong>Terminal 2 — Celery worker(the thing that actually runs the tasks):</strong></p><pre>celery -A app.celery_app:celery worker --loglevel=info</pre><p><strong>Terminal 3 — Flower (dashboard to watch everything):</strong></p><pre>pip install flower<br>celery -A app.celery_app:celery flower --port=5555</pre><p>Now go ahead and create a task:</p><pre>curl -X POST http://localhost:5000/tasks/ \<br>  -H &quot;Content-Type: application/json&quot; \<br>  -d &#39;{&quot;title&quot;: &quot;Learn Celery&quot;}&#39;</pre><p>You’ll get back something like:</p><pre>{<br>  &quot;id&quot;: 1,<br>  &quot;title&quot;: &quot;Learn Celery&quot;,<br>  &quot;done&quot;: false,<br>  &quot;notification_job_id&quot;: &quot;b3d4e5f6-1234-5678-abcd-ef1234567890&quot;<br>}</pre><p>Watch the worker terminal and you’ll see it pick up the task immediately. Then go to http://localhost:5555 and you can see a whole dashboard of what’s happening.</p><p>Check on your job status:</p><pre>curl http://localhost:5000/tasks/jobs/b3d4e5f6-1234-5678-abcd-ef1234567890</pre><p>It’s kind of satisfying to see it all working.</p><p><strong>Things to Remember</strong></p><p>If you start using Celery seriously, there a few gotchas worth knowing:</p><p><strong>Make your tasks idempotent:</strong> If a task ends up running twice(due to retry or whatever), it shouldn’t cause duplicate emails or charge someone twice. Think through that edge case.</p><p><strong>Don’t pass whole objects as arguments:</strong> Pass IDs instead. Then the task fetches what it needs from the database. Serializing a huge object through Redis is slow and can break in weird ways.</p><pre># Nope<br>send_task_notification.delay(task)<br><br># Yes<br>send_task_notification.delay(task[&quot;id&quot;])  # Fetch it in the worker</pre><p><strong>Set time limits:</strong> If a task gets stuck and never finishes, it’ll tie up a worker forever. Not great. Do this:</p><pre>@celery.task(bind=True, max_retries=3, time_limit=30, soft_time_limit=25)<br>def send_task_notification(self, task_id, task_title):<br>    ...</pre><p>The soft_time_limit throws an exception you can catch if you want to do cleanup. The time_limit just kills the worker if the soft limit gets ignored.</p><p><strong>Where We’re At</strong></p><p>Your task manager now:</p><ul><li>Responds to requests in milliseconds, not seconds</li><li>Handles slow work in the background with Celery</li><li>Uses Redis to queue up jobs and store results.</li><li>Has automatic retries with backoff for resilience</li><li>Has a way to check on what jobs are doing</li><li>Comes with Flower so you can actually see what’s happening</li></ul><p>Your folder structure looks like:</p><pre>task-manager/<br>├── app/<br>│   ├── __init__.py<br>│   ├── celery_app.py<br>│   ├── tasks/<br>│   │   ├── __init__.py<br>│   │   └── notifications.py<br>│   └── routes/<br>│       └── tasks.py<br>├── run.py<br>└── requirements.txt</pre><p>But here’s the annoying part: actually running all this locally means you need three terminal windows open at all times, plus Redis running separately. Getting a new team member up and running? Total nightmare. Everyone configures it slightly differently and then weird things break.</p><p><strong>What’s Coming Next</strong></p><p>In Part 3 we’re gonna fix this with Docker. One command and everything spins up — Flask, the workers, Redis all of it running in containers. Same on your laptop, same on production, same everywhere. Way cleaner</p><p>Catch you in Part 3.</p><blockquote><a href="https://medium.com/@HariHaran15/part-1-of-4-building-production-ready-flask-apps-c8eba43e11d5">Part 1 of 4 — Building Production-Ready Flask Apps</a></blockquote><p><strong>References:</strong></p><p><a href="https://docs.celeryq.dev/en/stable/">Celery</a></p><p><em>Watch this space for more articles on Python. If you like to read more of my articles do follow me on </em><a href="https://medium.com/@HariHaran15"><strong>Medium </strong></a><em>and </em><a href="https://www.linkedin.com/in/hari-hara-subramanyam-s-b2692636"><strong>LinkedIn</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=127a50beee79" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Part 1 of 4 - Building Production-Ready Flask Apps]]></title>
            <link>https://medium.com/@HariHaran15/part-1-of-4-building-production-ready-flask-apps-c8eba43e11d5?source=rss-d33dcc1d7536------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8eba43e11d5</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[python-flask]]></category>
            <category><![CDATA[asynchronous-programming]]></category>
            <category><![CDATA[python-programming]]></category>
            <dc:creator><![CDATA[Hari Hara Subramanyam S]]></dc:creator>
            <pubDate>Fri, 24 Apr 2026 19:49:52 GMT</pubDate>
            <atom:updated>2026-05-03T17:29:23.542Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OEYC4nN1NoV4-7mB" /><figcaption>Photo by <a href="https://unsplash.com/@brechtcorbeel?utm_source=medium&amp;utm_medium=referral">Brecht Corbeel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>So you know Python pretty well by now. You have maybe written scripts for reading data from a database and writing it to an excel file and now, you are ready to take the next step, you want to put something on the web. It could be a REST API or even a small microservice, something that people can call from their browser or any API client(for e.g. Postman). Flask is exactly what will help you do it.</p><p>Over the next four articles, we are going to build something real. It wil not be the usual “Hello World” tutorial, but a fully working application that can be deployed on the cloud with Docker and Kubernetes. Each article builds on the previous one.</p><p><strong>Here is what we will be covering today:</strong></p><ol><li>Why is Flask actually perfect for beginners</li><li>How to structure the project for larger applications</li><li>Everything you need to know about routing, templates and how requests work</li><li>Blueprints — the secret to keeping Flask code organized</li><li>A REST API for a task manager app that we will be building over the course of this series.</li></ol><p><strong>What exactly is Flask?</strong></p><p>The answer is, Flask is a micro web- framework. Which means it only has the essentials out of the box — routing and request handling — and you add whatever else you actually need, for example a database layer, an authentication layer, etc.</p><p>Flask lets you write your application mostly in plain python functions. You define a function, decorate it with a route, and Flask handles getting the request to that function. That’s it! <br>It feels natural to create an endpoint with Flask.</p><p><strong>Getting started with the Project</strong></p><p>Firstly, let us set up a python environment and install Flask</p><pre>mkdir task-manager &amp;&amp; cd task-manager<br>#Assuming python is already installed on your system<br>python -m venv venv <br>source venv/bin/activate <br>#On windows: venv\Scripts\activate<br>pip install flask</pre><p>Instead of keeping all routes in app.py, which would be a very normal temptation, we will start with a structure that keeps the codebase organized as it grows.</p><pre>task-manager/<br>|-- app/<br>|   |-- __init__.py<br>|   |-- routes/<br>|   |   |-- __init__.py<br>|   |   |-- tasks.py<br>|   |__ templates/<br>|       |__ index.html<br>|__ run.py</pre><p>To populate the requirements.txt you can use the below command from the virtual environment just created</p><pre>pip freeze &gt; requirements.txt</pre><p><strong>The Application Factory Pattern</strong></p><p>This is where Flask shows its elegance. In app/__init__.py , we are going to write a function that creates and configures our Flask app. This might seem like an overhead but it will make testing easier and lets us run multiple app instances with different configurations.</p><pre>from Flask import Flask<br>def create_app():<br>  app = Flask(__name__)<br>  app.config[&#39;SECRET_KEY&#39;] = &quot;dev-secret-key&quot;<br>  from app.routes.tasks import tasks_bp<br>  app.register_blueprint(tasks_bp)<br>  return app</pre><p>Then create run.py :</p><pre>from app import create_app<br><br>app = create_app()<br><br>if __name__ == &quot;__main__&quot;:<br>    app.run(debug=True)</pre><p>Run the above script</p><pre>python run.py</pre><p>You should see similar to the below output on your terminal:</p><pre>Your Flask development server is now running at `http://localhost:5000`</pre><p>Now that the Flask server is up and running, let us add the routes</p><p><strong>Routing: Turning URLs into function calls</strong></p><p>Flask uses decorators to map URLs to the functions we define. Open app/routes/tasks.py and let us build a complete CRUD API:</p><pre>from flask import Blueprint, jsonify, request<br><br>tasks_bp = Blueprint(&quot;tasks&quot;, __name__, url_prefix=&quot;/tasks&quot;)<br><br># For now, tasks live in memory. We&#39;ll add a real database later.<br>tasks = []<br>task_counter = 1<br><br><br>@tasks_bp.route(&quot;/&quot;, methods=[&quot;GET&quot;])<br>def get_tasks():<br>    return jsonify(tasks)<br><br><br>@tasks_bp.route(&quot;/&quot;, methods=[&quot;POST&quot;])<br>def create_task():<br>    global task_counter<br>    data = request.get_json()<br><br>    if not data or &quot;title&quot; not in data:<br>        return jsonify({&quot;error&quot;: &quot;title is required&quot;}), 400<br><br>    task = {<br>        &quot;id&quot;: task_counter,<br>        &quot;title&quot;: data[&quot;title&quot;],<br>        &quot;done&quot;: False,<br>    }<br>    tasks.append(task)<br>    task_counter += 1<br><br>    return jsonify(task), 201<br><br><br>@tasks_bp.route(&quot;/&lt;int:task_id&gt;&quot;, methods=[&quot;GET&quot;])<br>def get_task(task_id):<br>    task = next((t for t in tasks if t[&quot;id&quot;] == task_id), None)<br>    if task is None:<br>        return jsonify({&quot;error&quot;: &quot;task not found&quot;}), 404<br>    return jsonify(task)<br><br><br>@tasks_bp.route(&quot;/&lt;int:task_id&gt;&quot;, methods=[&quot;PATCH&quot;])<br>def update_task(task_id):<br>    task = next((t for t in tasks if t[&quot;id&quot;] == task_id), None)<br>    if task is None:<br>        return jsonify({&quot;error&quot;: &quot;task not found&quot;}), 404<br><br>    data = request.get_json()<br>    if &quot;done&quot; in data:<br>        task[&quot;done&quot;] = data[&quot;done&quot;]<br>    if &quot;title&quot; in data:<br>        task[&quot;title&quot;] = data[&quot;title&quot;]<br><br>    return jsonify(task)<br><br><br>@tasks_bp.route(&quot;/&lt;int:task_id&gt;&quot;, methods=[&quot;DELETE&quot;])<br>def delete_task(task_id):<br>    global tasks<br>    task = next((t for t in tasks if t[&quot;id&quot;] == task_id), None)<br>    if task is None:<br>        return jsonify({&quot;error&quot;: &quot;task not found&quot;}), 404<br><br>    tasks = [t for t in tasks if t[&quot;id&quot;] != task_id]<br>    return &quot;&quot;, 204</pre><p>Let me walkthrough what is happening here:</p><p><strong>Blueprints: </strong>These are Flask’s way of letting us organize routes into logical modules. You define a blueprint in one file, register it in your app factory, and everything in it gets attached. As the app grows, you might have users_bp , auth_bp , notifications_bp — each in its own file. This scales better than keeping all 50 routes in one giant file.</p><p><strong>url_prefix=”/tasks”: </strong>This prefixes every route in the blueprint. So @tasks_bp.route(&quot;/&quot;) isn’t actually GET / it is GET /tasks/ . The url_prefix saves you from repeating /tasks in every decorator.</p><p><strong>request.get_json(): </strong>This pulls in the JSON body of the request. If there is no JSON or the content type is not right, it returns None . That’s why we check for it.</p><p>Now that we have a basic flask app up and running, it would be a good time to test these endpoints with curl:</p><pre>curl -X POST http://localhost:5000/tasks/ \<br>  -H &quot;Content-Type: application/json&quot; \<br>  -d &#39;{&quot;title&quot;: &quot;Write part 2 of the series&quot;}&#39;<br><br># Get all tasks<br>curl http://localhost:5000/tasks/<br><br># Mark it done<br>curl -X PATCH http://localhost:5000/tasks/1 \<br>  -H &quot;Content-Type: application/json&quot; \<br>  -d &#39;{&quot;done&quot;: true}&#39;</pre><p>Your API is working!!.</p><p>Now that we have our API working, let us quickly understand how Flask actually handles the request</p><p><strong>Note</strong>: This is a high-level overview — there’s a lot happening internally with WSGI and the web server, but on a conceptual level, you can think of it like this: When a request hits your app, here’s the flow:</p><ol><li>Flask matches the incoming URL and HTTP method to one of your route functions.</li><li>It sets up an <strong>application context </strong>and a <strong>request context </strong>(thread-local data that Flask uses internally)</li><li>Your function executes, and the request object is available. wherever you need it because Flask has made it a proxy to the current request</li><li>Your function returns something — a string, a Response object or a tuple</li><li>Flask cleans up both contexts and returns the response back.</li></ol><p>The reason I mention this flow is that understanding contexts matters when you start debugging things. The most common error of “no request context” when you try to access request outside of a route would be clear when you read the above flow.</p><p>The jsonify() function is just a convenience function which takes whatever you give it and converts it into JSON and wraps it in a Response object with the right content type. In newer versions of Flask, you can just return a dict and it will serialize automatically.</p><p><strong>Handling Errors Gracefully</strong></p><p>You cannot just let Flask return its default error pages(which are HTML) to the API clients expecting JSON. So register error handlers in your app factory;</p><pre># In app/__init__.py, inside create_app()<br><br>@app.errorhandler(404)<br>def not_found(e):<br>    return jsonify({&quot;error&quot;: &quot;not found&quot;}), 404<br><br>@app.errorhandler(405)<br>def method_not_allowed(e):<br>    return jsonify({&quot;error&quot;: &quot;method not allowed&quot;}), 405<br><br>@app.errorhandler(500)<br>def internal_error(e):<br>    return jsonify({&quot;error&quot;: &quot;internal server error&quot;}), 500</pre><p>Now all error come back as JSON with a consistent format. Your API clients won’t break on unexpected HTML.</p><p><strong>Templates: When you actually need HTML</strong></p><p>Although we are building an API-first application, it is worth knowing how Flask handles templates because you will need alteast one HTML page. Flask uses Jinja2, which has a nice readable syntax.</p><p>Create app/templates/index.html :</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>    &lt;title&gt;Task Manager&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;h1&gt;Tasks&lt;/h1&gt;<br>    &lt;ul&gt;<br>        {% for task in tasks %}<br>            &lt;li class=&quot;{{ &#39;done&#39; if task.done else &#39;&#39; }}&quot;&gt;<br>                {{ task.title }}<br>            &lt;/li&gt;<br>        {% endfor %}<br>    &lt;/ul&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>Then add a route to render it:</p><pre>from flask import render_template<br><br>@tasks_bp.route(&quot;/view&quot;)<br>def view_tasks():<br>    return render_template(&quot;index.html&quot;, tasks=tasks)</pre><p>The syntax is pretty straightforward: {{variable}} outputs values, and {% %} wraps logic like loops and conditionals. That is 90% of what you will ever need from Jinja2.</p><p><strong>What we have built</strong></p><p>In a single article, we have learned enough to build a functional web application. You now have:</p><ol><li>A project structure that won’t embarass you in six months.</li><li>A working REST API with full CRUD operations</li><li>Blueprints keeping your code organized and modular.</li><li>Proper error handling that does not look like a Python stack trace.</li><li>HTML templating if and when you need it.</li></ol><p>Here is what the current project structure looks like:</p><pre>task-manager/<br>|-- app/<br>|   |-- __init__.py<br>|   |-- routes/<br>|   |   |-- __init__.py<br>|   |   |-- tasks.py<br>|   |__ templates/<br>|       |__ index.html<br>|-- run.py<br>|__ requirements.txt</pre><p><strong>Coming Up: Part 2</strong></p><p>Here is a problem that would be faced by almost every web app eventually, some operations taking too long to finish during a request. You cannot make your user wait 30 seconds for a response while you process a file or send emails or generate a report. The request would timeout.</p><p>In the next article, we are going to add backgrounf jobs to our application using <strong>Celery </strong>and <strong>Redis</strong>. We will give the task manager a new feature where creating a task triggers a background worker to do something time-consuming. Your API stays responsive, and the work happens in the background.</p><p>Thanks for reading. See you in Part 2.</p><blockquote><a href="https://medium.com/@HariHaran15/part-2-of-4-building-production-ready-flask-apps-127a50beee79">Part 2 of 4 — Building Production-Ready Flask Apps</a></blockquote><h3>References:</h3><p><a href="https://flask.palletsprojects.com/en/stable/">Flask</a></p><p><em>Watch this space for more articles on Python. If you like to read more of my articles do follow me on </em><a href="https://medium.com/@HariHaran15"><strong>Medium </strong></a><em>and </em><a href="https://www.linkedin.com/in/hari-hara-subramanyam-s-b2692636"><strong>LinkedIn</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8eba43e11d5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Comprehensions in Python]]></title>
            <link>https://python.plainenglish.io/comprehensions-in-python-a244e55aa2e5?source=rss-d33dcc1d7536------2</link>
            <guid isPermaLink="false">https://medium.com/p/a244e55aa2e5</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[dict]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[set]]></category>
            <dc:creator><![CDATA[Hari Hara Subramanyam S]]></dc:creator>
            <pubDate>Wed, 23 Feb 2022 10:31:05 GMT</pubDate>
            <atom:updated>2026-04-24T18:22:32.886Z</atom:updated>
            <content:encoded><![CDATA[<h4>A comprehensive guide on Comprehensions</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2oI-74btRXhWjmSL" /><figcaption>Photo by <a href="https://unsplash.com/@hiteshchoudhary?utm_source=medium&amp;utm_medium=referral">Hitesh Choudhary</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As programmers, one of the first things we learn is the <strong>for </strong>loop. The use of <strong>for </strong>loop is so frequent that it becomes second nature to use them while we code. Although looping is unavoidable, there are times when we can make looping more elegant and pythonic for creating new sequences — Comprehensions.</p><p>In today’s article, we will be looking at the different comprehensions available in Python and learning how to use them.</p><p>Table of Contents:</p><ul><li><a href="#7f53">List Comprehension</a></li><li><a href="#d94c">Dict Comprehension</a></li><li><a href="#7a79">Set Comprehension</a></li></ul><p><strong>List Comprehensions:</strong></p><p>List Comprehensions are an elegant way to create new lists. They also make the code more pythonic. Let us look at a few examples below:</p><ul><li><strong>With a single for loop:</strong></li></ul><pre>#Using for loop<br>num = []<br>for i in range(100):<br>    num.append(i*2)<br>print(num)</pre><pre>#Result<br>[0,2,4,6,...]</pre><pre># Using List comprehension<br>num = [i*2 for i in range(100)]<br>print(num)</pre><pre>#Result<br>[0,2,4,6,...]</pre><ul><li><strong>With an If condition:</strong></li></ul><pre>#Using for loop<br>even = []<br>for i in range(10):<br>    if i%2==0:<br>        even.append(i)<br>print(even)</pre><pre>#Result<br>[0,2,4,6,8]</pre><pre># Using List comprehension<br>even = [i for i in range(10) if i%2 ==0]<br>print(even)</pre><pre>#Result<br>[0,2,4,6,8]</pre><ul><li><strong>With nested for loops:</strong></li></ul><pre># Using nested for loops <br>mat = []<br>for i in range(3):<br>    mat.append([])<br>    for j in range(5):<br>        mat[i].append(j)<br>print(mat)</pre><pre>#Result<br>[[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4]]</pre><pre>#Using List comprehension<br>mat = [[j for j in range(5)] for i in range(3)]<br>print(mat)</pre><pre>#Result<br>[[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4]]</pre><p><strong>Dict Comprehensions:</strong></p><p>Similar to List comprehension, we can create dictionaries by using Dict Comprehension. Let us look at a few examples below.</p><ul><li><strong>With a single for loop:</strong></li></ul><pre>#Using for loop<br>square = {}<br>for i in range(10):<br>    square[i] = i**2<br>print(square)</pre><pre>#Result<br>{0:0,1:2,2:4,3:9,...}</pre><pre># Using Dict comprehension<br>square= {i:i**2 for i in range(10)}<br>print(square)</pre><pre>#Result<br>{0:0,1:2,2:4,3:9,...}</pre><ul><li><strong>With an If condition:</strong></li></ul><pre>#Using for loop<br>even_square = {}<br>for i in range(10):<br>    if i%2 == 0:<br>        even_square[i] = i**2<br>print(even_square)</pre><pre>#Result<br>{0:0,2:4,4:16,6:36,8:64}</pre><pre># Using Dict comprehension<br>even_square= {i:i**2 for i in range(10) if i%2 ==0}<br>print(even_square)</pre><pre>#Result<br>{0:0,2:4,4:16,6:36,8:64}</pre><ul><li><strong>With if-else condition:</strong></li></ul><pre># Using if-else condition<br>num = [0,1,2,3,4,5,6,7,8,9]<br>dic = {}<br>for i in num:<br>    if i%2 == 0:<br>        dic[i] = &#39;even&#39;<br>    else:<br>        dic[i] = &#39;odd&#39;<br>print(dic)</pre><pre>#Result<br>{0:&#39;even&#39;,1:&#39;odd&#39;,2:&#39;even&#39;,3:&#39;odd&#39;,...}</pre><pre><br>#Using Dict comprehension<br>num = [0,1,2,3,4,5,6,7,8,9]<br>dic = {i : &#39;even&#39; if i%2==0 else &#39;odd&#39; for i in num}<br>print(dic)</pre><pre>#Result<br>{0:&#39;even&#39;,1:&#39;odd&#39;,2:&#39;even&#39;,3:&#39;odd&#39;,...}</pre><p><strong>Set Comprehensions:</strong></p><p>Similar to List comprehension, we can create a set by using Set Comprehension. Let us look at a few examples below.</p><ul><li><strong>With a single for loop:</strong></li></ul><pre>#Using for loop<br>square = set()<br>for i in range(10):<br>    square.add(i**2)<br>print(square)</pre><pre>#Result<br>{0, 1, 64, 4, 36, 9, 16, 49, 81, 25}</pre><pre># Using Set comprehension<br>square = {i**2 for i in range(10)}<br>print(square)</pre><pre>#Result<br>{0, 1, 64, 4, 36, 9, 16, 49, 81, 25}</pre><p><strong>Note: </strong>Since the set is an unordered collection of items, the result will be displayed in random order.</p><ul><li><strong>With an If condition:</strong></li></ul><pre>#Using for loop<br>even = set()<br>for i in range(10):<br>    if i%2 == 0:<br>        even.add(i)<br>print(even)</pre><pre>#Result<br>{0, 2,4,6,8}</pre><pre># Using Set comprehension<br>even= {i for i in range(10) if i%2 ==0}<br>print(even)</pre><pre>#Result<br>{0, 2,4,6,8}</pre><p>We have seen how to use comprehensions to create sequences like <strong>List</strong>, <strong>Dict, </strong>and <strong>Set</strong>. Although comprehensions are an elegant and pythonic way to create a sequence, it can become difficult to read if used with multiple for loops and if conditions. Hence it is advised to factor in the readability of the code when using Comprehensions.</p><p><strong>References (Official Python Documentation):</strong></p><p><a href="https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions">Comprehensions</a></p><p><em>Watch this space for more articles on Python. If you like to read more of my articles do follow me on </em><a href="https://medium.com/@HariHaran15"><strong>Medium </strong></a><em>and </em><a href="https://www.linkedin.com/in/hari-hara-subramanyam-s-b2692636"><strong>LinkedIn</strong></a></p><p>Thank you for reading!!</p><p><em>More content at</em><strong><em> </em></strong><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Get exclusive access to writing opportunities and advice in our</em><strong><em> </em></strong><a href="https://discord.gg/GtDtUAvyhW"><strong><em>community Discord</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a244e55aa2e5" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/comprehensions-in-python-a244e55aa2e5">Comprehensions in Python</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Accessing the Web in Python Using Requests]]></title>
            <link>https://medium.com/better-programming/accessing-the-web-in-python-using-requests-5fe5bb2bd822?source=rss-d33dcc1d7536------2</link>
            <guid isPermaLink="false">https://medium.com/p/5fe5bb2bd822</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[https]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Hari Hara Subramanyam S]]></dc:creator>
            <pubDate>Thu, 17 Feb 2022 12:33:18 GMT</pubDate>
            <atom:updated>2022-02-21T07:21:02.859Z</atom:updated>
            <content:encoded><![CDATA[<h4>A quick guide on making HTTP requests in Python</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*knh4CFQbJzuhXiv0" /><figcaption>Photo by <a href="https://unsplash.com/@artnok?utm_source=medium&amp;utm_medium=referral">Nicolas Picard</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The Requests library is a simple HTTP library built for Python. It comes with a lot features allowing one to send a request with custom headers and SSL Verification to name a few.</p><p><strong>Table of Contents:</strong></p><ol><li><a href="#3817">What is HTTP?</a></li><li><a href="#c3ae">GET Requests</a></li><li><a href="#1a85">Analyzing the Response</a></li><li><a href="#db4e">POST Request</a></li><li><a href="#5963">Cookies</a></li><li><a href="#9ca2">Sessions</a></li></ol><p><strong>What is HTTP?<br></strong>HTTP (Hypertext Transfer Protocol) is a set of protocols using which the communication between a server and client is enabled. It works on a request-response model.</p><p>There are different methods through which the communication is carried out. Below are the most common methods:</p><ul><li><strong>GET: </strong>To retrieve data from the server</li><li><strong>POST: </strong>To publish data to the server</li><li><strong>PUT: </strong>To replace an existing resource on the server</li><li><strong>DELETE: </strong>To delete the specified resource.</li></ul><p>Let us now look at using the requests library with different HTTP methods:</p><p>Before getting started, install the requests library</p><pre>pip install requests</pre><p>Import the requests library into the python script as below</p><pre>import requests</pre><p><strong>GET Requests:</strong></p><ul><li>A simple GET request:<br>The return value will be a response object containing information about the response from the server.</li></ul><pre>response = requests.get(&#39;https://httpbin.org/get&#39;)</pre><ul><li>GET request with parameters:</li></ul><pre>parameters = <strong>{</strong>&#39;key1&#39;<strong>:</strong> &#39;value1&#39;<strong>,</strong> &#39;key2&#39;<strong>:</strong> &#39;value2&#39;<strong>}</strong><br>response = requests.get(&#39;https://httpbin.org/get&#39;, params=parameters)</pre><ul><li>GET request with custom headers:</li></ul><pre>headers = {&#39;Bearer&#39;:<em>Authorization_token</em>}<br>response = request.get(&#39;https://httpbin.org/get&#39;, headers=headers)</pre><ul><li>GET request with stream parameter: <br>This is used to prevent large responses from entirely loading into the memory at once.</li></ul><pre>response = request.get(url_to_download_large_file, stream=True)</pre><p><strong>Analyzing the Response:</strong></p><p>The request that is sent will receive response from the server. It will be stored in a response object. The response object provides all the information related to the response. Let us look at a few below:</p><ul><li><strong>Viewing the Content from the server response</strong>:</li></ul><p><strong>Text response: </strong>The content can be accessed using the text<strong> </strong>property.<br>Requests uses the encoding format received in the Headers to decode the content.</p><pre>response = requests.get(&#39;https://httpbin.org/get&#39;)<br>print(response.text)</pre><pre>#Result<br>{<br>  &quot;args&quot;: {<br>    &quot;key1&quot;: &quot;value1&quot;, <br>    &quot;key2&quot;: &quot;value2&quot;  <br>  },...</pre><p><strong>Non-Text response: </strong>The content can be accessed as bytes using content property. By default, <strong>gzip </strong>and <strong>deflate </strong>encoding are decoded.</p><pre>response = requests.get(&#39;https://httpbin.org/get&#39;)<br>print(response.content)</pre><pre># Result<br>b&#39;{\n  &quot;args&quot;: {\n    &quot;key1&quot;: &quot;value1&quot;, \n    &quot;key2&quot;: &quot;value2&quot;\n  }, \n  &quot;headers&quot;: {\n    &quot;Accept&quot;: &quot;*/*&quot;, \n <br>   &quot;Accept-Encoding&quot;: &quot;gzip, deflate&quot;,...</pre><p><strong>JSON response: </strong>If the response from the server is in a JSON format, that can be easily decoded using the json() method</p><pre>response = requests.get(&#39;https://httpbin.org/get&#39;)<br>print(response.json())</pre><pre>#Result<br>{&#39;args&#39;: {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}, &#39;headers&#39;: {&#39;Accept&#39;: &#39;*/*&#39;, &#39;Accept-Encoding&#39;: &#39;gzip, deflate&#39;, &#39;Host&#39;: &#39;httpbin.org&#39;, &#39;User-Agent&#39;...</pre><p>Requests also provides the option of viewing the raw response from the server via the raw property. To do this, we must pass stream=True in the initial request.</p><ul><li><strong>To download an image or a large file:</strong></li></ul><p>For dealing with a large file being sent in the response, we can use the iter_content() method along with the chunk_size keyword-argument. The chunk size specifies the number of bytes to be read at once.</p><pre>response = request.get(url_to_download_large_file, stream=True)<br>with open<strong>(</strong>filename<strong>,</strong> &#39;wb&#39;<strong>)</strong> as f<strong>:</strong><br>    for chunk in response.iter_content<strong>(</strong>chunk_size=128<strong>):</strong><br>        f.write<strong>(</strong>chunk<strong>)</strong></pre><ul><li><strong>Viewing the encoding format:</strong></li></ul><p>The encoding property provides the encoding method which was used. We can reassign the property to a different encoding and Requests will use the new value of response.encoding whenever you call response.text.</p><pre>response = requests.get(&#39;https://httpbin.org/get&#39;)<br>print(response.encoding)</pre><pre>#Result<br>utf-8</pre><pre>#Reassign a new encoding<br>response.encoding = &#39;ISO-8859-1&#39;</pre><p><strong>Other useful properties in the Response Object:</strong></p><ul><li>status_code : Provides the HTTP status code of the response.</li><li>elapsed_time : The time elapsed between sending a request to receiving the response.</li><li>url : Gives the actual URL from which the response is sent. This is useful in identifying if the request was redirected.</li><li>cookies : This property contains the cookies sent back by the server.</li><li>headers : The headers sent by the server.</li><li>history : This property is used to check if the request was redirected. The property will contain a list of Response objects created to complete the request starting from oldest to newest.<br><strong>Note: </strong>To avoid redirect, we can pass in allow_redirects=False in the request.</li></ul><p>The Response object also contains the details of the request. This can be accessed using the request property of the response object</p><pre>params = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<br>response = requests.get(&quot;https://httpbin.org/get&quot;, params=params)<br>print(response.request.__dict__)</pre><pre>#Result <br>{&#39;method&#39;: &#39;GET&#39;, &#39;url&#39;: &#39;<a href="https://httpbin.org/get?key1=value1&amp;key2=value2&#39;">https://httpbin.org/get?key1=value1&amp;key2=value2&#39;</a>, &#39;headers&#39;: {&#39;User-Agent&#39;: &#39;python-requests/2.26.0&#39;...</pre><p><strong>POST Requests:</strong></p><ul><li>A simple POST request to send form encoded data:<br>To send a form encoded data, we will use the data keyword.</li></ul><pre>data = <strong>{</strong>&#39;key1&#39;<strong>:</strong> &#39;value1&#39;<strong>,</strong> &#39;key2&#39;<strong>:</strong> &#39;value2&#39;<strong>}<br></strong>response = requests.post<strong>(</strong>&quot;https://httpbin.org/post&quot;<strong>,</strong> data=data<strong>)<br></strong>print(response.text)</pre><pre>#Result<br>{<br>  &quot;args&quot;: {},<br>  &quot;data&quot;: &quot;&quot;,<br>  &quot;files&quot;: {},<br>  &quot;form&quot;: {<br>    &quot;key1&quot;: &quot;value1&quot;,<br>    &quot;key2&quot;: &quot;value2&quot;<br>  },...</pre><ul><li>POST request containing multiple values for a key:</li></ul><pre>data = <strong>{</strong>&#39;key1&#39;<strong>:</strong> [&#39;value1&#39;<strong>,</strong> &#39;value2&#39;]<strong>} <br>or<br></strong>data = [(&#39;key1&#39;,&#39;value1&#39;),(&#39;key1&#39;,&#39;value2&#39;)]<br>response = requests.post<strong>(</strong>&quot;https://httpbin.org/post&quot;<strong>,</strong> data=data<strong>)<br></strong>print(response.text)</pre><pre>#Result<br>{<br>  &quot;args&quot;: {},<br>  &quot;data&quot;: &quot;&quot;,<br>  &quot;files&quot;: {},<br>  &quot;form&quot;: {<br>    &quot;key1&quot;: [<br>      &quot;value1&quot;,<br>      &quot;value2&quot;<br>    ],,,</pre><ul><li>POST request to send a Multipart Encoded file:<br>To send a multipart encoded file, we use the files keyword.</li></ul><pre>files = <strong>{</strong>&#39;file&#39;<strong>:</strong> open<strong>(</strong>file<strong>,</strong> &#39;rb&#39;<strong>)}</strong><br>response = requests.post<strong>(</strong>&#39;https://httpbin.org/post&#39;<strong>,</strong> files=files<strong>)</strong></pre><ul><li>POST request to send JSON encoded data:<br>JSON data can be sent using the keyword json</li></ul><pre>data = <strong>{</strong>&#39;key1&#39;<strong>:</strong> &#39;value1&#39;<strong>,</strong> &#39;key2&#39;<strong>:</strong> &#39;value2&#39;<strong>}<br></strong>response = requests.post<strong>(</strong>&quot;https://httpbin.org/post&quot;<strong>,</strong> json=data<strong>)<br></strong>print(response.text)</pre><p>The above will send a POST request with the Content-Type as application/json <br><strong>Note:</strong> The json keyword is ignored if either data or files keyword is used.</p><p><strong>Cookies:</strong></p><ul><li>Sending Cookies in the request:</li></ul><pre>cookies = {&#39;my-cookie&#39;:&#39;value&#39;}<br>response = requests.get<strong>(</strong>&#39;https://httpbin.org/cookies&#39;<strong>, </strong>cookies=cookies<strong>)<br></strong>print(response.text)</pre><pre>#Result<br>{<br>  &quot;cookies&quot;: {<br>    &quot;my-cookie&quot;: &quot;value&quot;<br>  }<br>}</pre><p>The Cookies are returned in a Requests Cookie Jar which acts like a dictionary but also offers a more complete interface, suitable for use over multiple domains or paths.</p><p>We can create an instance of the RequestsCookieJar and set the cookies to be sent for specific routes. Only those cookies will be sent during the request.</p><pre>cookie_jar = requests.cookies.RequestsCookieJar()<br>cookie_jar.set(&#39;my-cookie1&#39;, &#39;value1&#39;, domain=&#39;httpbin.org&#39;, path=&#39;/cookies&#39;)<br>cookie_jar.set(&#39;my-cookie2&#39;, &#39;value2&#39;, domain=&#39;httpbin.org&#39;, path=&#39;/anything&#39;)<br>cookie_jar.set(&#39;my-cookie3&#39;, &#39;value3&#39;, domain=&#39;httpbin.org&#39;, path=&#39;/cookies&#39;)<br>response1 = requests.get(&#39;https://httpbin.org/cookies&#39;, cookies=cookie_jar)</pre><pre>print(response1.text)</pre><pre>#Result<br>{<br>  &quot;cookies&quot;: {<br>    &quot;my-cookie1&quot;: &quot;value1&quot;,<br>    &quot;my-cookie3&quot;: &quot;value3&quot;<br>  }<br>}</pre><p><strong>Sessions:</strong></p><p>Sessions are used to persist certain parameters across requests. The cookies sent using the session instance is also persisted across requests. Since the urllib3’s connection pooling is used, multiple requests made to the same host would result in a considerable performance improvement.</p><p>Session objects have all the methods as that of the Requests API.</p><pre>s = requests.Session()<br>s.cookies.update({&#39;my_cookie1&#39;:&#39;value1&#39;})<br>response = s.get(&#39;http://httpbin.org/cookies&#39;)<br>print(response.text)</pre><pre>#Result<br>{<br>  &quot;cookies&quot;: {<br>    &quot;my_cookie1&quot;: &quot;value1&quot;<br>  }<br>}</pre><pre>response = s.get(&#39;http://httpbin.org/cookies&#39;,cookies={&#39;my_cookie2&#39;:&#39;value2&#39;})<br>print(response.text)</pre><pre>#Result<br>{<br>  &quot;cookies&quot;: {<br>    &quot;my_cookie1&quot;: &quot;value1&quot;,<br>    &quot;my_cookie2&quot;: &quot;value2&quot;<br>  }<br>}</pre><p>As we can see, the cookies will be set at the session level, and any new cookie sent as part of the request will get appended with the session level cookie.</p><p>The usual pattern of using Session is with a with block, as the session would be closed automatically once the context manager is exited</p><pre>with requests.Session() as s:<br>    s.cookies.update({&#39;my_cookie1&#39;:&#39;value1&#39;})<br>    response = s.get(&#39;http://httpbin.org/cookies&#39;)<br>    print(response.text)</pre><p>We have covered the basics of the Requests library and also looked at how to send GET and POST requests. Similarly, PUT and DELETE requests can also be sent by using the respective methods on the requests API.</p><p>With this, you should now be able to make basic requests to a server by passing cookies, headers and also send/download files.</p><h4><strong>References(Official Python Documentation)</strong></h4><p><a href="https://docs.python-requests.org/en/latest/user/quickstart/">Requests</a></p><pre><strong>Want to Connect?</strong></pre><pre>If you like to read more of my articles do follow me on <a href="https://www.linkedin.com/in/hari-hara-subramanyam-s-b2692636">LinkedIn</a>.</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5fe5bb2bd822" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/accessing-the-web-in-python-using-requests-5fe5bb2bd822">Accessing the Web in Python Using Requests</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Look at the SQL Speaking Python… Pandas!]]></title>
            <link>https://medium.com/better-programming/the-sql-speaking-python-pandas-869f6207c021?source=rss-d33dcc1d7536------2</link>
            <guid isPermaLink="false">https://medium.com/p/869f6207c021</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[pandas]]></category>
            <dc:creator><![CDATA[Hari Hara Subramanyam S]]></dc:creator>
            <pubDate>Thu, 10 Feb 2022 06:49:25 GMT</pubDate>
            <atom:updated>2022-02-18T13:18:11.690Z</atom:updated>
            <content:encoded><![CDATA[<h4>From SQL to Pandas — know the equivalents</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qCwazxzK7nkI_7Cg" /><figcaption>Photo by <a href="https://unsplash.com/@billow926?utm_source=medium&amp;utm_medium=referral">billow926</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Pandas is a widely used library for data manipulation and analysis. Speaking of data, the most common way to store data is in a database and we would use <em>SQL</em>(Structured Query Language) for querying it.</p><p>We can use pandas for inserting/retrieving and modifying data to/from the database. Pandas can also be used with raw SQL queries. Let’s look at SQL and its pandas equivalent below.</p><p>Before starting, ensure that Pandas are installed in your machine.</p><pre>pip install pandas</pre><p>Post which, pandas can be imported into your code as below:</p><pre>import pandas as pd</pre><p>Before proceeding, we will get the data from a table table1<em> </em>and store it in a DataFrame called table.</p><pre>-- SQL<br>SELECT * FROM table1;</pre><pre>--Pandas<br>table = pd.read_sql(&#39;SELECT * FROM TABLE1&#39;, con=<em>engine</em>/connection_string)</pre><pre>The <em>engine </em>is an <a href="https://docs.sqlalchemy.org/en/14/core/engines.html">SQLAlchemy</a> engine.</pre><p><strong>Note: </strong>A DataFrame is a data structure that is 2-dimensional, having data in the form of rows and columns. It can be considered to be similar to an excel spreadsheet. To know more about DataFrame, kindly refer the <a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html">pandas official documentation</a> on it.</p><h3><strong>1. Select</strong></h3><ul><li>Select all columns from a table:</li></ul><pre>--SQL <br>SELECT * FROM table1;</pre><pre>--Pandas<br>table</pre><ul><li>Select coulmn1, column2 from a table:</li></ul><pre>--SQL <br>SELECT column1, column2 FROM table1;</pre><pre>--Pandas<br>table[[column1,column2]]</pre><ul><li>Select all unique records from a column</li></ul><pre>--SQL <br>SELECT DISTINCT(column1) FROM table1;</pre><pre>--Pandas<br>table[column1].unique()</pre><ul><li>Select the count of all unique records from a column</li></ul><pre>--SQL<br>SELECT COUNT(DISTINCT(column1)) FROM table1;</pre><pre>--Pandas<br>table[column1].nunique()</pre><h3>2.<strong> Select with a Where clause</strong></h3><ul><li>Select records where column1 <strong>is equal</strong> to value:</li></ul><pre>--SQL <br>SELECT * FROM table1 WHERE column1 = <em>value</em>;</pre><pre>--Pandas<br>table.loc[table[column1]==<em>value</em>]</pre><ul><li>Select record where column1 is greater than value1 and column2 is less than value2:</li></ul><pre>--SQL <br>SELECT * FROM table1 WHERE column1 = <em>value1</em> AND column2 = <em>value2</em>;</pre><pre>--Pandas<br>table.loc[(table[column1] &gt; <em>value1</em>) &amp; (table[column2]&lt; <em>value2</em>)]</pre><ul><li>Select records where column1 contains<strong> </strong>a string ‘Hello’:</li></ul><pre>--SQL<br>SELECT * FROM table1 WHERE column1 LIKE &#39;%Hello%&#39;;</pre><pre>--Pandas<br>table.loc[table[column1].str.contains(&#39;Hello&#39;)]</pre><ul><li>Select records which are not null:</li></ul><pre>--SQL<br>SELECT * FROM table1 WHERE column1 IS NOT NULL;</pre><pre>--Pandas<br>table.loc[table[column1].notnull()]</pre><h3>3. <strong>Select with Group By clause</strong></h3><ul><li>Select column1 and sum of column2 grouping on column1:</li></ul><pre>--SQL <br>SELECT coulmn1, SUM(column2) FROM table1 GROUP BY column1;</pre><pre>--Pandas<br>table.groupby(column1).sum()</pre><ul><li>Select column1 and minimum of column2 grouping on column1 where the minimum value of column2 is greater than <em>value</em>:</li></ul><pre>--SQL <br>SELECT coulmn1, MIN(column2) as min FROM table1 GROUP BY column1 HAVING MIN(column2)&gt; <em>value</em>;</pre><pre>--Pandas<br>table_group = table.groupby(column1).min()<br>table_group[table_group[column2] &gt; <em>value</em>]</pre><h3>4. <strong>SELECT with Order By clause (Sorting)</strong></h3><ul><li>Sort the results in <strong>ascending </strong>order based on a single column:</li></ul><pre>--SQL<br>SELECT * FROM table1 ORDER BY column1 ASC;</pre><pre>--Pandas<br>table.sort_values(by=column1,ascending=True,inplace=True)</pre><ul><li>Sort the results in descending order based on a single column:</li></ul><pre>--SQL<br>SELECT * FROM table1 ORDER BY column1 DESC;</pre><pre>--Pandas<br>table.sort_values(by=column1,ascending=False,inplace=True)</pre><ul><li>Sort the results in ascending order based on multiple columns:</li></ul><pre>--SQL<br>SELECT * FROM table1 ORDER BY column1, column2 ASC;</pre><pre>--Pandas<br>table.sort_values(by=[column1,column2],ascending=True,inplace=True)</pre><ul><li>Sort the results of column1 in ascending order and column2 in descending order:</li></ul><pre>--SQL<br>SELECT * FROM table1 ORDER BY column1 ASC, column2 DESC;</pre><pre>--Pandas<br>table.sort_values(by=[column1,column2],ascending=[True,False],inplace=True)</pre><h3>5. <strong>JOINS</strong></h3><h4><strong>INNER JOIN:</strong></h4><pre>--SQL <br>SELECT * FROM table1 INNER JOIN table2 ON table1.column1 = table2.column1;</pre><pre>--Pandas<br>join_table = table1.merge(table2, on=column1, how=&#39;inner&#39;)</pre><pre># We can also use the join method as given below.<br>join_table = table1.join(table2.set_index(column1), on=column1, how=inner)</pre><h4><strong>LEFT JOIN on multiple columns:</strong></h4><pre>--SQL <br>SELECT * FROM table1 LEFT JOIN table2 ON table1.column1 = table2.column1 AND table1.column2=table2.column2;</pre><pre>--Pandas<br>join_table = table1.merge(table2, how=&#39;left&#39;, left_on=[column1,column2], right_on=[column1,column2])</pre><h4><strong>RIGHT JOIN:</strong></h4><pre>--SQL <br>SELECT * FROM table1 RIGHT JOIN table2 ON table1.column1 = table2.column1;</pre><pre>--Pandas<br>join_table = table1.merge(table2, on=column1, how=&#39;right&#39;)</pre><h3>6. <strong>Update</strong></h3><ul><li>Updating a column based on criteria:</li></ul><pre>--SQL <br>UPDATE table1 SET column2 = 1 WHERE column1 = &#39;YES&#39;;</pre><pre>--Pandas<br>table.loc[table[column1]==&#39;YES&#39;, column2] = 1</pre><ul><li>Updating a column based on multiple criteria:</li></ul><pre>--SQL<br>UPDATE table1 SET column3 = 1 WHERE column1 = &#39;YES&#39; AND column2 = &#39;ACTIVE&#39;;</pre><pre>--Pandas<br>table.loc[(table[column1] == &#39;YES&#39;) &amp; (table[column2] == &#39;ACTIVE&#39;), column3] = 1</pre><h3>7. <strong>Insert</strong></h3><ul><li>Inserting the data from the dataframe into a database table.</li></ul><pre>--SQL<br>INSERT INTO table1 VALUES (column1,column2,column3)</pre><pre>--Pandas<br>table.to_sql(table1, con=<em>engine</em>/connection_string)</pre><p>These are just a small set of functionalities in Pandas. Kindly refer the <a href="https://pandas.pydata.org/docs/index.html"><em>Pandas documentation</em></a> for the full feature set.</p><p>I would also be posting many more Pandas features in upcoming stories. Do stay tuned.</p><pre><strong>Want to Connect?</strong></pre><pre>Reach me out on <a href="https://www.linkedin.com/in/hari-hara-subramanyam-s-b2692636">LinkedIn</a>.</pre><p>Thank you for reading!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=869f6207c021" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/the-sql-speaking-python-pandas-869f6207c021">A Look at the SQL Speaking Python… Pandas!</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[8 Useful functions in Python]]></title>
            <link>https://towardsdev.com/8-useful-functions-in-python-5aefa1e0d629?source=rss-d33dcc1d7536------2</link>
            <guid isPermaLink="false">https://medium.com/p/5aefa1e0d629</guid>
            <category><![CDATA[built-in-function]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Hari Hara Subramanyam S]]></dc:creator>
            <pubDate>Thu, 03 Feb 2022 06:31:50 GMT</pubDate>
            <atom:updated>2022-02-21T07:42:41.466Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tkvbmj3d8JRC1fWT" /><figcaption>Photo by <a href="https://unsplash.com/@cdr6934?utm_source=medium&amp;utm_medium=referral">Chris Ried</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Python is a powerful language and has a plethora of built-in functions. More often than not, the basic operations which we require can be easily done with the help of these built-in functions. Be it sorting a list, getting the index of an element or getting the smallest value in a dictionary, all of which can be done using these built-in functions. Given below are few such functions which would not only make the job easier but also help in writing more <em>pythonic code.</em></p><ol><li><strong>sorted(): </strong>This function is used to sort the input and return a sorted list. The default sorting is in ascending order. The input must be an <a href="https://docs.python.org/3/glossary.html#term-iterable"><strong>iterable</strong></a>. There are 2 optional keyword-only arguments:</li></ol><ul><li><strong>reverse:</strong><em> </em>If set to<em> </em><strong>True</strong><em>, </em>will<em> </em>perform the sorting in descending order.</li><li><strong>key: </strong>which accepts a function to be used while sorting the items.</li></ul><figure><img alt="Jupyter notebook example of sorted function" src="https://cdn-images-1.medium.com/max/832/1*j3iuo0fOeKZBitiSneHJ2g.png" /><figcaption>Fig 1. Output of <strong>sorted() </strong>function for a list and string input</figcaption></figure><p><strong>2.</strong> <strong>reversed(): </strong>This function will return an iterator which would yield the elements of the input sequence in the reverse order.</p><p><strong>Note: </strong>The items returned would <strong>not </strong>be sorted. They will be returned with only the positions reversed.</p><figure><img alt="Jupyter notebook example of reversed function" src="https://cdn-images-1.medium.com/max/940/1*wx2VTBoVIGqatwVWWfdVtQ.png" /><figcaption>Fig 2. Output of <strong><em>reversed() </em></strong>function</figcaption></figure><p><strong>3.</strong> <strong>map(): </strong>This will apply a given function to 1 or more iterables. For each item in the iterable, the given function would be applied and the result will be a map object in Python 3 and a list in Python2. It accepts multiple iterables and expects that the function to be applied also accepts the same number of parameters for example, if we are passing in 3 iterables, the function which would be applied should also accept 3 parameters.</p><figure><img alt="Jupyter notebook output of map function in Python3 interpreter." src="https://cdn-images-1.medium.com/max/940/1*QAUkwWlRHPfw5DykAbh0YQ.png" /><figcaption>Fig 3a. Python3 output of <strong>map() </strong>function</figcaption></figure><figure><img alt="Python2 interpreter output of map function." src="https://cdn-images-1.medium.com/max/940/1*5uVuCM5lWuM_bXYcHTNvJA.png" /><figcaption>Fig 3b. Python2 output of <strong>map() </strong>function</figcaption></figure><p><strong>4.</strong> <strong>filter(): </strong>This function will apply a given function to the iterable and return only the elements which satisfies the condition and results in a Boolean <strong>True. </strong>The return type is a filter object which can be iterated upon to get all the values. If function is <strong>None</strong>, then all elements which are <a href="https://docs.python.org/3/library/stdtypes.html#truth-value-testing">truthy</a> will be returned.</p><figure><img alt="Jupyter Notebook showing filter function results" src="https://cdn-images-1.medium.com/max/1024/1*zAgb_sk7MhiBIGpxkVfmkg.png" /><figcaption>Fig 4. Output of <strong>filter() </strong>function</figcaption></figure><p><strong>5. enumerate(): </strong>This function accepts an iterable and returns an enumerate object. Upon iterating over the returned object, we would be getting a tuple consisting of the index and the element present at the given index. The indexing by default starts from 0.</p><p>There is an optional keyword-only argument:</p><ul><li><strong>start: </strong>The value from which the indexing should start.</li></ul><figure><img alt="Jupyter notebook output of enumerate function" src="https://cdn-images-1.medium.com/max/940/1*wtSQBZCPdAiEPg4AUcWutg.png" /><figcaption>Fig 5. Output of <strong>enumerate() </strong>function</figcaption></figure><p><strong>6</strong>. <strong>min()/max():</strong> Used to find the smallest/ largest item. If only 1 positional argument is given, then it must be an iterable and the function would return the smallest/largest item in the iterable. If multiple positional arguments are given, then the comparison would be among all the positional arguments passed.</p><figure><img alt="Jupyter Notebook output for min and max functions" src="https://cdn-images-1.medium.com/max/1024/1*-HUFvxLduHCku5sJ6qIzUA.png" /><figcaption>Fig 6a. Output of <strong>min()/max()</strong> functions.</figcaption></figure><p>There are 2 optional keyword-only arguments:</p><ul><li><strong>key: </strong>Any ordering function which accepts a single parameter can be used.</li></ul><figure><img alt="Jupyter Notebook containing the output of min()/max() with key parameter" src="https://cdn-images-1.medium.com/max/1024/1*k1PfZcWbTA_oLJxg52Y5cw.png" /><figcaption>Fig 6b. Output of <strong>min()/max()</strong> with key parameter</figcaption></figure><p>The output of <strong>min()/max() </strong>is the smallest/largest value in the dictionary based on the length.</p><ul><li><strong>default: </strong>The default value to be outputted if the iterable is empty.</li></ul><figure><img alt="Jupyter Notebook output for min()/max() with default optional keyword" src="https://cdn-images-1.medium.com/max/1024/1*ZHEIPR-qDf8zBt_hEeZvjA.png" /><figcaption>Fig 6c. Output of <strong>min()/max()</strong> without and with the default optional keyword</figcaption></figure><p><strong>Note: </strong>This optional keyword-only parameter is available from Python 3.4</p><p><strong>7.</strong> <strong>all()/any():</strong> These functions are used to check if all/any of the items in an iterable are True. <strong>all() </strong>will return a True if the iterable is empty whereas <strong>any() </strong>will return False.</p><figure><img alt="Jupyter Notebook code showing the ouput of any() and all() function" src="https://cdn-images-1.medium.com/max/1024/1*SSZyhHXWuILtdvlCYJan8Q.png" /><figcaption>Fig 7. Output of <strong>all()/any() </strong>function.</figcaption></figure><p>We can also pass in a generator expression to <strong>all()/any()</strong>. Below are a few examples of the same.</p><figure><img alt="Jupyter Notebook output for all()/any() having a generator expression" src="https://cdn-images-1.medium.com/max/1024/1*B2E0W6xlV9v8Hbluo1pjUQ.png" /><figcaption>Fig 7b. Output of <strong>all()/any() </strong>with generator expressions.</figcaption></figure><p><strong>8. zip(): </strong>Zip() accepts 1 or more iterables and returns an iterator of tuples containing an element from each iterable in the tuple.</p><blockquote>As per the official Python documentation:<br>More formally: <a href="https://docs.python.org/3/library/functions.html#zip">zip()</a> returns an iterator of tuples, where the <em>i</em>-th tuple contains the <em>i</em>-th element from each of the argument iterables.</blockquote><p>The iterables can be of different lengths and in such cases, the result will be equal to the length of the shortest iterable.</p><figure><img alt="Jupyter Notebook output of zip() function" src="https://cdn-images-1.medium.com/max/1024/1*8dv3flzvby9vTqaqnqbowA.png" /><figcaption>Fig 8a. Output of <strong>zip()</strong> function.</figcaption></figure><p><strong><em>References (Python Documentation):</em></strong></p><ul><li><a href="https://docs.python.org/3/library/functions.html"><strong>Built-in functions</strong></a></li><li><a href="https://docs.python.org/3/library/functions.html#zip"><strong>zip()</strong></a></li></ul><p><em>Watch this space for more articles on Python. If you like to read more of my articles do follow me on </em><a href="https://medium.com/@HariHaran15"><strong>Medium </strong></a><em>and </em><a href="https://www.linkedin.com/in/hari-hara-subramanyam-s-b2692636"><strong>LinkedIn</strong></a></p><p>Thank you for reading!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5aefa1e0d629" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/8-useful-functions-in-python-5aefa1e0d629">8 Useful functions in Python</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>