Posts

Showing posts with the label Python

Sentry Error Tracking: Catch Bugs Before Your Users Do

Image
Quick answer Sentry error tracking collects the exceptions your Python app raises in production and groups them into issues you can triage, with the stack trace and how many users were hit. Install sentry-sdk, call sentry_sdk.init() with your project DSN as early as possible, and every unhandled exception is reported automatically. It is worth it once real users can hit errors you never see in development. Your checkout worked on your laptop. It passed review, the tests were green, and the deploy went out on a Friday afternoon. Then a customer writes on Monday that their order failed, and you have nothing to go on. No log line, no stack trace, just a screenshot and a vague description. You open the logs and find a wall of lines, most harmless, while the one line that matters sits buried in the middle. That gap, between an error happening and your team knowing about it, is the whole problem. Sentry closes it by catching the exception the moment it happens and sending you the details. ...

OpenTelemetry Python: Trace a Checkout Flow End to End

Image
Quick answer Yes. OpenTelemetry Python adds distributed tracing to a Python service with a few lines of setup: create a tracer provider, attach an exporter, and wrap each step of a request in a span. Each span records a name, timing, and attributes, and spans from one request share a trace id. Start with the console exporter, then switch to a real backend when the traces look right. A customer writes in: checkout took thirty seconds, then failed, and nobody can say where the time went. The order service says the payment call was slow. The payment service says it never saw the request. The logs from both sides exist, but they disagree about what happened first. That is the gap tracing fills. A trace follows one request across every service it touches, giving you one timeline with a name, a duration, and a few facts per step. OpenTelemetry is the shared vocabulary for that timeline, and the Python SDK is the part you wire into your own code. What I would check before using it My firs...

LangChain Tutorial: Build Your First LLM App in Python

Image
Quick answer LangChain is a Python framework for building applications powered by large language models, and in current LangChain 1.x you compose those applications with LCEL, the LangChain Expression Language, written as prompt | model | parser. This post builds a working support-ticket triage chain with that pattern, running offline with a stand-in model so you can learn the shape before spending anything on API calls. Picture a support inbox on a Monday morning. Two hundred messages arrived over the weekend: billing questions, outage reports, one customer who cannot check out. Reading each one by hand takes hours, and the urgent ones hide among the routine ones. A language model can read and sort them in seconds, but wiring that into real code means formatting prompts, calling the API, parsing replies, and handling failures. That wiring is the job LangChain is built for, and this post shows it on a small, honest example. What I would check before using it My first check is wheth...

DuckDB: Analytics Queries on a File, Without a Server

Image
A small shop owner exports twelve orders to a CSV file and wants one answer: which city brought in the most money this week? The file sits on a laptop, the data fits in a spreadsheet, and yet getting the answer takes longer than it should. A spreadsheet means wrestling with filters and formulas, and the totals shift every time a row is added. A database server for a file this small is worse: installing, configuring, creating users, all for twelve rows. DuckDB sits in the middle. It is a small analytical database that runs inside your own program, reads the CSV directly, and answers with plain SQL. No server, no setup, no separate process to keep alive. Quick answer Yes, DuckDB can answer analytics questions straight from a CSV file, with no database server to install or run. It reads the file with SQL, so totals, rankings, and revenue per city come from a few queries in a short Python script. The catch is messy data: prices stored as text with a thousands comma, and blank values, nee...

SQLAlchemy: Turn Your Python Classes Into Database Tables

Image
Picture a bike repair shop. The owner keeps one notebook for customers, another for bikes, and a third for repair jobs. Each week she works out who owes what, matching names across three notebooks. A spreadsheet helps at first, until the same customer appears under two spellings and a paid job gets counted twice. For tables in Python without a spreadsheet, see Pandas Explained: Tables in Python, Without the Spreadsheet . It is all there, but the links between the pieces live only in her head. That is the point where a Python developer reaches for SQLAlchemy. It turns customers, bikes, and jobs into Python classes backed by tables, with the relationships written down in code instead of remembered. The question is whether it is the right weight for the job in front of you. Quick answer Yes, SQLAlchemy is worth it when a Python app has several related tables and the code will keep growing with them. It maps each table to a Python class, so customers, bikes, and jobs become objects with ...

Python asyncio: Run Jobs at the Same Time Without Threads

Picture a small shop dashboard on a Monday morning. Before the first order ships, someone has to check stock with four suppliers, one for tote bags, one for mugs, one for notebooks, one for lamps. Each check takes a couple of seconds while the supplier's system answers. Done one after another, that is eight seconds of staring at a spinner, and the computer spends almost all of it doing nothing, just waiting for a reply. The work is not hard, it is slow, and the slowness comes from waiting, not from thinking. That is the exact gap asyncio is built for: one program that starts several waits at once and picks each one up when its answer arrives. Quick answer Yes, asyncio is worth using when a program makes several independent waits, because it starts them all at once and the total time is the longest single wait, not the sum: four stock checks took 2.50 seconds together instead of 7.01 one by one. It needs libraries written for it, like aiohttp or asyncpg, and it does nothing for wo...

Matplotlib: Charts in Python Without Opening a Spreadsheet

Image
The numbers are there, but the pattern is not You have last week's sales in a CSV file: notebooks 42, pens 87, backpacks 23, mugs 61, desk lamps 35. You can read every number and still not answer the question your manager asked: which category is carrying the week? Rows of numbers hide shape. A bar chart shows it in one glance, pens towering over everything, backpacks barely registering. That gap is the whole reason to plot from inside Python. If your numbers already live in a script or a CSV you load with Python, opening a spreadsheet just to draw a chart is an extra round trip, and every round trip is a chance to paste the wrong column. For tables in Python, see Pandas Explained: Tables in Python, Without the Spreadsheet . Quick answer Yes, Matplotlib is the right default when your data is already in Python and the chart ends up in a report, a slide deck, or a document, because a bar chart is a handful of lines and it saves to PNG, PDF, and SVG. Use the figure-and-axes style f...

NumPy: Fast Arrays in Python Without the Loops

When seven numbers turn into a slow afternoon Picture a stationery shop at the end of the week. The owner has seven sales figures in a notebook, and a question to answer before ordering stock. Which day sold best, and what happens to the total if the weekend gets a ten percent discount? Seven numbers and a calculator will do the job. The trouble starts when the questions multiply. What about a year of daily sales, a sensor logging every minute, or a price list with a hundred thousand rows? Loops still work, but they get slower and longer, and each one is another place for a mistake to hide. For a shorter way to build lists in Python, see Python List Comprehensions Explained With Real Examples . That is the point where NumPy earns its place. Quick answer Yes, NumPy is worth learning the moment your work involves lists of numbers you add, average, compare, or reshape more than once, because it turns long loops into short lines that run far faster as the data grows: on 100,000 values i...

Pydantic: Catch Bad Data Before It Reaches Your Database

A library lets members borrow books through a web form. One day, a member types -5 into the loan period field, another mistypes her email, and a third pastes an ISBN with a letter in it. The form saves all three loans without complaint. The reminder email bounces, the -5 loan shows as overdue, and nobody can find the book with the broken ISBN. The database stored what it was given. The problem is that nobody checked what was given first. That is the gap Pydantic fills. You describe the shape of the data once, as a Python class, and Pydantic checks every record against it before your code trusts it. Quick answer Yes, Pydantic is worth adding the moment your code receives data it did not create, because you describe the shape of the data once as a Python class and Pydantic checks every record against it before your code trusts it. A loan with -5 days, a mistyped email, and an ISBN with a letter in it all failed with errors naming the exact field and rule, instead of being saved quietl...