<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Python on Han's XYZ</title><link>https://han8931.github.io/categories/python/</link><description>Recent content in Python on Han's XYZ</description><generator>Hugo</generator><language>en</language><managingEditor>tabularasa8931@gmail.com (Han)</managingEditor><webMaster>tabularasa8931@gmail.com (Han)</webMaster><copyright>This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.</copyright><lastBuildDate>Sun, 30 Aug 2026 21:51:06 +0900</lastBuildDate><atom:link href="https://han8931.github.io/categories/python/index.xml" rel="self" type="application/rss+xml"/><item><title>uv workspace: effective management of Python apps</title><link>https://han8931.github.io/uv-packages/</link><pubDate>Mon, 08 Dec 2025 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/uv-packages/</guid><description>&lt;h2 id="understanding-uv-workspaces"&gt;Understanding uv Workspaces&lt;/h2&gt;
&lt;p&gt;The official uv website explains workspaces very clearly:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Inspired by Cargo, a &lt;em&gt;uv workspace&lt;/em&gt; is a collection of one or more Python packages (workspace members) managed together in a single repo. Each package has its own &lt;code&gt;pyproject.toml&lt;/code&gt;, but the workspace shares one &lt;code&gt;lockfile&lt;/code&gt;, keeping dependencies consistent across apps and libraries. Commands like &lt;code&gt;uv lock&lt;/code&gt; operate on the whole workspace, while &lt;code&gt;uv run&lt;/code&gt; and &lt;code&gt;uv sync&lt;/code&gt; default to the workspace root but can target a specific member via &lt;code&gt;--package&lt;/code&gt; &lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;</description></item><item><title>Agentic AI with Pydantic-AI Part 1.</title><link>https://han8931.github.io/pydantic-ai/</link><pubDate>Sun, 31 Aug 2025 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/pydantic-ai/</guid><description>&lt;h2 id="introduction"&gt;Introduction&lt;/h2&gt;
&lt;p&gt;AI has already changed how we interact with technology. The real shift is happening now with &lt;strong&gt;agents&lt;/strong&gt;: AI systems that can reason, make decisions, and take action.&lt;/p&gt;
&lt;p&gt;Unlike a chatbot that passively replies, an agent can &lt;strong&gt;break down complex tasks&lt;/strong&gt;, call APIs or databases, use tools, and deliver structured results. This is what makes the idea of &lt;em&gt;Agentic AI&lt;/em&gt; so powerful — it&amp;rsquo;s not just about conversation, it&amp;rsquo;s about &lt;strong&gt;problem-solving with initiative&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Clean Validation with Pydantic v2</title><link>https://han8931.github.io/pydantic/</link><pubDate>Sat, 30 Aug 2025 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/pydantic/</guid><description>&lt;blockquote&gt;
&lt;p&gt;📝 &lt;strong&gt;Update&lt;/strong&gt; (2025-08): This post was originally published in &lt;strong&gt;April 2024&lt;/strong&gt; and has been updated to reflect changes in &lt;strong&gt;Pydantic v2&lt;/strong&gt;, including the new &lt;em&gt;field validator&lt;/em&gt;, &lt;em&gt;model validator&lt;/em&gt;, and &lt;em&gt;Annotated&lt;/em&gt;-based validation patterns. Also, this post now includes a &lt;strong&gt;new section on using Pydantic with MongoDB&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Python&amp;rsquo;s dynamic typing system is indeed convenient, allowing you to create variables without explicitly declaring their types. While this flexibility can streamline development, it can also introduce unexpected behavior, particularly when handling data from external sources like APIs or user input.&lt;/p&gt;</description></item><item><title>A Lesson from a Naive Binary Search</title><link>https://han8931.github.io/binary-search/</link><pubDate>Mon, 30 Jun 2025 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/binary-search/</guid><description>&lt;h2 id="a-lesson-from-a-naive-binary-search"&gt;A Lesson from a Naive Binary Search&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ve been grinding hard every day, solving coding problems to get better at algorithms. Recently, I came across something interesting—a naive implementation of binary search can actually cause a bug. It&amp;rsquo;s a small detail, but it matters.&lt;/p&gt;
&lt;div class="code-block code-line-numbers" style="counter-reset: code-block 0"&gt;
 &lt;div class="code-header language-python"&gt;
 &lt;span class="code-title"&gt;&lt;i class="arrow fas fa-angle-right fa-fw" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
 &lt;span class="ellipses"&gt;&lt;i class="fas fa-ellipsis-h fa-fw" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
 &lt;span class="copy" title="Copy to clipboard"&gt;&lt;i class="far fa-copy fa-fw" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
 &lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;binary_search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;It works fine in Python—but I recently learned that the way I calculate &lt;code&gt;mid&lt;/code&gt; can cause problems in some cases.&lt;/p&gt;</description></item><item><title>Introduction to logging in Python</title><link>https://han8931.github.io/python-logging/</link><pubDate>Sat, 17 May 2025 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/python-logging/</guid><description>&lt;h2 id="a-gentle-practical-introduction-to-logging-in-python"&gt;A gentle, practical introduction to &lt;code&gt;logging&lt;/code&gt; in Python&lt;/h2&gt;
&lt;hr&gt;
&lt;h3 id="why-bother-with-a-dedicated-logging-library"&gt;Why bother with a dedicated logging library?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Prints don’t scale.&lt;/strong&gt; &lt;code&gt;print()&lt;/code&gt; is fine during quick experiments, but real programs need a record that can be filtered, rotated, or shipped elsewhere.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Separation of concerns.&lt;/strong&gt; You decide &lt;em&gt;what&lt;/em&gt; to log in your code; &lt;code&gt;logging&lt;/code&gt; decides &lt;em&gt;where&lt;/em&gt; and &lt;em&gt;how&lt;/em&gt; to write it (console, file, etc.).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built-in, no extra dependency.&lt;/strong&gt; The standard library’s &lt;code&gt;logging&lt;/code&gt; module is powerful enough for most applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id="core-concepts"&gt;Core concepts&lt;/h3&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Concept&lt;/th&gt;
 &lt;th&gt;Role in the ecosystem&lt;/th&gt;
 &lt;th&gt;Typical examples&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Logger&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;The entry point your code calls (&lt;code&gt;logger.info(...)&lt;/code&gt;). You can have many, one per module.&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;&amp;quot;__main__&amp;quot;&lt;/code&gt;, &lt;code&gt;&amp;quot;my_package.worker&amp;quot;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Handler&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Decides &lt;em&gt;where&lt;/em&gt; the record goes.&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;StreamHandler&lt;/code&gt; (stdout), &lt;code&gt;FileHandler&lt;/code&gt;, &lt;code&gt;TimedRotatingFileHandler&lt;/code&gt;, &lt;code&gt;SMTPHandler&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Formatter&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Decides &lt;em&gt;how&lt;/em&gt; the record looks.&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;'%(asctime)s - %(levelname)s - %(name)s - %(message)s'&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="a-minimal-logger"&gt;A minimal logger&lt;/h3&gt;
&lt;div class="code-block code-line-numbers open" style="counter-reset: code-block 0"&gt;
 &lt;div class="code-header language-python"&gt;
 &lt;span class="code-title"&gt;&lt;i class="arrow fas fa-angle-right fa-fw" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
 &lt;span class="ellipses"&gt;&lt;i class="fas fa-ellipsis-h fa-fw" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
 &lt;span class="copy" title="Copy to clipboard"&gt;&lt;i class="far fa-copy fa-fw" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
 &lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;logging&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;basicConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;INFO&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="si"&gt;%(levelname)s&lt;/span&gt;&lt;span class="s2"&gt; | &lt;/span&gt;&lt;span class="si"&gt;%(message)s&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;Hello, world!&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;basicConfig&lt;/code&gt; is a one-liner good for small scripts.&lt;/li&gt;
&lt;li&gt;In bigger projects, mixing multiple modules / log files, you&amp;rsquo;ll want finer control.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id="rotating-files-at-midnight"&gt;Rotating files at midnight&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Rotating a log file means creating a new log file after a certain time or size limit is reached&lt;/strong&gt;. In this case, a new file is created every night at midnight. Only the most recent two log files are kept—yesterday&amp;rsquo;s and today’s—while older ones are deleted automatically.&lt;/p&gt;</description></item><item><title>Rediscovering Python's Pathlib</title><link>https://han8931.github.io/pathlib/</link><pubDate>Sat, 17 May 2025 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/pathlib/</guid><description>&lt;h2 id="from-type-hint-to-power-tool--pythons-pathlib"&gt;From Type Hint to Power Tool: Python&amp;rsquo;s &lt;code&gt;Pathlib&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;For a long time, I used &lt;code&gt;Path&lt;/code&gt; from Python&amp;rsquo;s &lt;code&gt;pathlib&lt;/code&gt; module purely as a &lt;strong&gt;type hint&lt;/strong&gt; - a way to make function signatures look more modern and semantically clear. Like this:&lt;/p&gt;
&lt;div class="code-block code-line-numbers open" style="counter-reset: code-block 0"&gt;
 &lt;div class="code-header language-python"&gt;
 &lt;span class="code-title"&gt;&lt;i class="arrow fas fa-angle-right fa-fw" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
 &lt;span class="ellipses"&gt;&lt;i class="fas fa-ellipsis-h fa-fw" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
 &lt;span class="copy" title="Copy to clipboard"&gt;&lt;i class="far fa-copy fa-fw" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
 &lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;pathlib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;It changed when I started building an application that handled user-uploaded documents. I had to create temporary folders, write intermediate files, manage output paths, and ensure directories existed before saving results. That&amp;rsquo;s when &lt;code&gt;Path&lt;/code&gt; went from &lt;em&gt;just a type hint&lt;/em&gt; to a core part of my file management logic.&lt;/p&gt;</description></item><item><title>UV Tutorial: All‑in‑One Python Package Manager!</title><link>https://han8931.github.io/uv-package-manager/</link><pubDate>Sun, 13 Apr 2025 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/uv-package-manager/</guid><description>&lt;blockquote&gt;
&lt;p&gt;📝&lt;strong&gt;Update&lt;/strong&gt; (2025-09-06): I&amp;rsquo;ve added a new section on using system certificates with corporate proxies. It covers why uv may fail with SSL errors at work and how to fix it by making uv trust your system certificates.&lt;/p&gt;
&lt;p&gt;📝&lt;strong&gt;Update&lt;/strong&gt; (2026-08-30): Since uv &lt;strong&gt;0.11.0&lt;/strong&gt; (March 2026), &lt;code&gt;--native-tls&lt;/code&gt; is deprecated in favor of &lt;code&gt;--system-certs&lt;/code&gt;. The section below has been rewritten to use the new flag.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id="meet-uva-blazinglyfast-allinone-python-packagemanager"&gt;Meet &lt;strong&gt;uv&lt;/strong&gt; – A Blazingly Fast, All‑in‑One Python Package Manager&lt;/h2&gt;
&lt;p&gt;In my last post, I covered &lt;strong&gt;&lt;a href="https://han8931.github.io/poetry/" target="_blank" rel="noopener noreffer "&gt;Poetry&lt;/a&gt;&lt;/strong&gt;. It&amp;rsquo;s a solid dependency manager—but it still leaves you juggling other tools: pip for installs, virtualenv for isolation, pyenv for Python versions, and pip-tools or Pipenv for locks. That bounce between tools adds friction. &lt;strong&gt;uv&lt;/strong&gt; removes it. This single, Rust-built project manager—now one of the most popular tools in the Python ecosystem—installs Python, creates virtual environments, resolves and locks dependencies, and even publishes to PyPI, all from one blazing-fast CLI (often 10–1000× faster).&lt;/p&gt;</description></item><item><title>Abstract Classes or Protocols</title><link>https://han8931.github.io/python-protocol-abstract-classes/</link><pubDate>Tue, 28 Jan 2025 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/python-protocol-abstract-classes/</guid><description>&lt;h1 id="introduction"&gt;Introduction&lt;/h1&gt;
&lt;p&gt;When it comes to writing clean, maintainable, and scalable Python code, design matters. As your projects grow, you&amp;rsquo;ll often find yourself needing to enforce structure, ensure consistency, and promote reusability. This is where Python&amp;rsquo;s &lt;a href="https://docs.python.org/3/library/abc.html" target="_blank" rel="noopener noreffer "&gt;Abstract Base Classes (ABCs)&lt;/a&gt; and &lt;a href="https://www.python.org/dev/peps/pep-0544/" target="_blank" rel="noopener noreffer "&gt;Protocols&lt;/a&gt; come into play—two powerful features that help you design better software.&lt;/p&gt;
&lt;p&gt;Abstract classes act as &lt;strong&gt;blueprints for other classes, allowing you to define methods that must be implemented by any subclass&lt;/strong&gt;. They&amp;rsquo;re typically used for creating a shared foundation while enforcing a specific structure. Protocols, on the other hand, take a more flexible approach. Instead of relying on inheritance, they &lt;strong&gt;let you define interfaces based on behavior&lt;/strong&gt;, making them ideal for &lt;em&gt;duck typing&lt;/em&gt; (or &lt;em&gt;structural subtyping&lt;/em&gt;) and runtime flexibility.&lt;/p&gt;</description></item><item><title>Asyncio in Python: A Deep Dive into Asynchronous I/O</title><link>https://han8931.github.io/asyncio/</link><pubDate>Sun, 05 Jan 2025 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/asyncio/</guid><description>&lt;p&gt;For the past few months, I&amp;rsquo;ve been working on an exciting internal project at my company: taking users&amp;rsquo; documents and running them through LLM APIs to translate and summarize their content, somewhat similar to &lt;a href="https://www.deepl.com/" target="_blank" rel="noopener noreffer "&gt;DeepL&lt;/a&gt;. The output is a collection of translated documents, each overlaid with the newly translated text. Our goal is to provide a stable service that can handle large files efficiently for thousands of employees at Samsung—no small task! To achieve this, we needed a concurrency strategy that supports high throughput while remaining responsive. &lt;strong&gt;That&amp;rsquo;s where Asyncio comes in.&lt;/strong&gt;&lt;/p&gt;</description></item><item><title>Dependency Management in Python: Poetry</title><link>https://han8931.github.io/poetry/</link><pubDate>Sun, 07 Jul 2024 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/poetry/</guid><description>&lt;h1 id="introduction"&gt;Introduction&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Poetry&lt;/strong&gt; is a dependency management and packaging tool in Python, aiming to improve how you define, install, and manage project dependencies.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Installation: You can install Poetry through its custom installer script or using package managers. The recommended way is to use their installer script to ensure you get the latest version.&lt;/li&gt;
&lt;li&gt;Creating a New Project: Use &lt;code&gt;poetry new &amp;lt;project-name&amp;gt;&lt;/code&gt; to create a new project with a standard layout.&lt;/li&gt;
&lt;li&gt;Adding Dependencies: Add new dependencies directly to your project using &lt;code&gt;poetry add &amp;lt;package&amp;gt;&lt;/code&gt;. Poetry will resolve the dependencies and update the &lt;code&gt;pyproject.toml&lt;/code&gt; and &lt;code&gt;poetry.lock&lt;/code&gt; files.&lt;/li&gt;
&lt;li&gt;Installing Dependencies: Running &lt;code&gt;poetry install&lt;/code&gt; in your project directory will install all dependencies defined in your &lt;code&gt;pyproject.toml&lt;/code&gt; file.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="poetry-example"&gt;Poetry Example&lt;/h2&gt;
&lt;h3 id="setting-up-a-new-project"&gt;Setting Up a New Project&lt;/h3&gt;
&lt;p&gt;To create a new project named &lt;code&gt;example_project&lt;/code&gt; with Poetry and manage its dependencies:&lt;/p&gt;</description></item><item><title>Why Use Python's `pdb` Debugger Over an IDE?</title><link>https://han8931.github.io/pdb/</link><pubDate>Sat, 27 Apr 2024 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/pdb/</guid><description>&lt;p&gt;When it comes to debugging Python code, most programmers reach for an Integrated Development Environment (IDE) because of its convenience and powerful features. However, there&amp;rsquo;s a classic, built-in tool that shouldn&amp;rsquo;t be overlooked: Python&amp;rsquo;s own debugger, &lt;code&gt;pdb&lt;/code&gt;. This tool might seem basic at first glance, but it offers some compelling advantages, especially in scenarios where an IDE might be less effective. Here&amp;rsquo;s why you might consider using &lt;code&gt;pdb&lt;/code&gt; for debugging your Python projects:&lt;/p&gt;</description></item><item><title>Enumerate variables with Enum!</title><link>https://han8931.github.io/enum/</link><pubDate>Fri, 26 Apr 2024 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/enum/</guid><description>&lt;p&gt;&lt;code&gt;Enum&lt;/code&gt; is a way that Python enumerate variables. The &lt;code&gt;enum&lt;/code&gt; module allows for the creation of enumerated constants—unique, immutable data types that are useful for representing a fixed set of values. These values, which are usually related by their context, are known as enumeration members.&lt;/p&gt;
&lt;p&gt;Enum provides&amp;hellip;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Uniqueness&lt;/strong&gt; - Each member of an &lt;code&gt;Enum&lt;/code&gt; is unique within its definition, meaning no two members can have the same value. Attempting to define two members with the same value will result in an error unless you explicitly allow aliases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Immutability&lt;/strong&gt; - Enum members are immutable. Once the &lt;code&gt;Enum&lt;/code&gt; class is defined, you cannot change the members or their values.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Iterability and Comparability&lt;/strong&gt; - Enum classes support iteration over their members and can be compared using identity and equality checks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Accessing Members&lt;/strong&gt; - You can access enumeration members by their names or values:&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto&lt;/strong&gt; - If you want to automatically assign values to enum members, you can use the &lt;code&gt;auto()&lt;/code&gt; function from the same module:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="code-block code-line-numbers open" style="counter-reset: code-block 0"&gt;
 &lt;div class="code-header language-python"&gt;
 &lt;span class="code-title"&gt;&lt;i class="arrow fas fa-angle-right fa-fw" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
 &lt;span class="ellipses"&gt;&lt;i class="fas fa-ellipsis-h fa-fw" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
 &lt;span class="copy" title="Copy to clipboard"&gt;&lt;i class="far fa-copy fa-fw" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
 &lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;enum&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Enum&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Enum&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;	&lt;span class="n"&gt;PLAYING&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;	&lt;span class="n"&gt;PAUSED&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;	&lt;span class="n"&gt;GAME_OVER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;If we just want to make sure them to be unique and automatically assigned, then use &lt;code&gt;auto()&lt;/code&gt;&lt;/p&gt;</description></item><item><title>Unit Test with Pytest</title><link>https://han8931.github.io/unit-tests/</link><pubDate>Fri, 26 Apr 2024 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/unit-tests/</guid><description>&lt;p&gt;Unit testing involves testing individual components of software in &lt;strong&gt;isolation to ensure they function correctly&lt;/strong&gt;. Automated frameworks facilitate this process, which &lt;strong&gt;is integral to ensuring that new changes do not disrupt existing functionality&lt;/strong&gt;. Unit tests also serve as practical documentation and encourage better software design. This testing method boosts development speed and confidence by confirming component reliability before integration. Early bug detection through unit testing also helps minimize future repair costs and efforts.&lt;/p&gt;</description></item><item><title>How to keep sensitive data in Python?</title><link>https://han8931.github.io/hide-sensitive-data/</link><pubDate>Sat, 20 Apr 2024 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/hide-sensitive-data/</guid><description>&lt;p&gt;An app&amp;rsquo;s &lt;em&gt;config&lt;/em&gt; is everything that is likely to vary between &lt;a href="https://12factor.net/codebase" target="_blank" rel="noopener noreffer "&gt;deploys&lt;/a&gt; (staging, production, developer environments, etc). This includes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Resource handles to the database, Memcached, and other &lt;a href="https://12factor.net/backing-services" target="_blank" rel="noopener noreffer "&gt;backing services&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Credentials to external services such as Amazon S3 or Twitter&lt;/li&gt;
&lt;li&gt;Per-deploy values such as the canonical hostname for the deploy&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Apps sometimes store config as constants in the code. This is a violation of twelve-factor, which requires &lt;strong&gt;strict separation of config from code&lt;/strong&gt;. Config varies substantially across deploys, code does not.&lt;/p&gt;</description></item><item><title>Type hint in Python</title><link>https://han8931.github.io/type-hint/</link><pubDate>Sat, 20 Apr 2024 00:00:00 +0000</pubDate><author>tabularasa8931@gmail.com (Han)</author><guid>https://han8931.github.io/type-hint/</guid><description>&lt;p&gt;Type hinting is &lt;em&gt;not mandatory&lt;/em&gt;, but it can make your code easier to understand and debug by&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Improved readability&lt;/li&gt;
&lt;li&gt;Better IDE support: IDEs and linters can use type hints to check your code for potential errors before runtime.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;While type hints can be simple classes like &lt;a href="https://docs.python.org/3/library/functions.html#float" title="float" target="_blank" rel="noopener noreffer "&gt;&lt;code&gt;float&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://docs.python.org/3/library/stdtypes.html#str" title="str" target="_blank" rel="noopener noreffer "&gt;&lt;code&gt;str&lt;/code&gt;&lt;/a&gt;, they can also be more complex. The &lt;a href="https://docs.python.org/3/library/typing.html#module-typing" title="typing: Support for type hints (see :pep:`484`)." target="_blank" rel="noopener noreffer "&gt;&lt;code&gt;typing&lt;/code&gt;&lt;/a&gt; module provides a vocabulary of more advanced type hints.&lt;/p&gt;</description></item></channel></rss>