<?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 Ken Huang</title>
    <link>https://blog.kenhuang.io/tags/python/</link>
    <description>Recent content in Python on Ken Huang</description>
    <generator>Hugo</generator>
    <language>en</language>
    <managingEditor>kenhuang512@gmail.com (Ken Huang)</managingEditor>
    <webMaster>kenhuang512@gmail.com (Ken Huang)</webMaster>
    <lastBuildDate>Sun, 19 Apr 2026 18:48:56 +0800</lastBuildDate>
    <atom:link href="https://blog.kenhuang.io/tags/python/rss.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>A Few Quick Notes About babashka/fs</title>
      <link>https://blog.kenhuang.io/blog/2024-12-07-babashka-fs-notes/</link>
      <pubDate>Sat, 07 Dec 2024 22:24:41 +0800</pubDate><author>kenhuang512@gmail.com (Ken Huang)</author>
      <guid>https://blog.kenhuang.io/blog/2024-12-07-babashka-fs-notes/</guid>
      <description>&lt;p&gt;&#xA;Recently I&amp;#39;ve used &lt;a href=&#34;https://github.com/babashka/fs/blob/master/API.md&#34;&gt;babashka/fs&lt;/a&gt; a little bit, here are some quick notes for it:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Path vs File.&#xA;&#xA;Use Path whenever possible, according to &lt;a href=&#34;https://stackoverflow.com/a/26658436&#34;&gt;this SO answer to &amp;#34;Java: Path vs File&amp;#34;&lt;/a&gt;.&#xA;&#xA;This is actually Java related.&lt;/li&gt;&#xA;&lt;li&gt;It&amp;#39;s ok to use a &lt;code class=&#34;verbatim&#34;&gt;path&lt;/code&gt; as a key for a clojure map.&#xA;&#xA;At my first try, I somehow came to the conclusion that it&amp;#39;s not ok, while I was refactoring the &lt;a href=&#34;https://github.com/scicloj/clay/blob/main/src/scicloj/clay/v2/live_reload.clj&#34;&gt;live reload&lt;/a&gt; for clay. And later I found that it&amp;#39;s totally fine to use it as a key. 😞&lt;/li&gt;&#xA;&lt;li&gt;Use &lt;a href=&#34;https://github.com/babashka/fs/blob/master/API.md#babashka.fs/path&#34;&gt;fs/path&lt;/a&gt; to construct file/directory paths, for example, &lt;code class=&#34;verbatim&#34;&gt;(fs/path &amp;#34;/tmp&amp;#34; &amp;#34;foo&amp;#34; &amp;#34;bar&amp;#34; &amp;#34;baz.clj&amp;#34;)&lt;/code&gt; will result in a path object for &lt;code class=&#34;verbatim&#34;&gt;/tmp/foo/bar/baz.clj&lt;/code&gt; on linux.&#xA;&#xA;If you&amp;#39;re coming from Python, it might remind you of &lt;a href=&#34;https://docs.python.org/3/library/os.path.html&#34;&gt;os.path.join(path, *paths)&lt;/a&gt;.&lt;/li&gt;&#xA;&lt;li&gt;Use &lt;code class=&#34;verbatim&#34;&gt;fs/createdirs&lt;/code&gt; for &lt;code class=&#34;verbatim&#34;&gt;mkdir -p&lt;/code&gt;, though its name is a bit misleading, I first thought it&amp;#39;s for creating a few dirs.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;fs/with-temp-dir&lt;/code&gt; is convenient for &lt;a href=&#34;https://github.com/scicloj/clay/blob/main/test/scicloj/clay/v2/live_reload_test.clj&#34;&gt;creating tests&lt;/a&gt;.&lt;/li&gt;&#xA;&lt;/ul&gt;</description>
    </item>
    <item>
      <title>How to append items to the CSV file without header row?</title>
      <link>https://blog.kenhuang.io/blog/2022-04-09-scrapy-csv-without-header/</link>
      <pubDate>Sat, 09 Apr 2022 22:32:11 +0800</pubDate><author>kenhuang512@gmail.com (Ken Huang)</author>
      <guid>https://blog.kenhuang.io/blog/2022-04-09-scrapy-csv-without-header/</guid>
      <description>&lt;figure&gt;&#xA;&lt;img src=&#34;https://docs.scrapy.org/en/latest/_images/scrapy_architecture_02.jpg&#34; alt=&#34;Scrapy Architecture&#34; title=&#34;Click to enlarge the image&#34;/&gt;&#xA;&lt;figcaption&gt;&#xA;Scrapy Architecture&#xA;&lt;/figcaption&gt;&#xA;&lt;/figure&gt;&#xA;&lt;p&gt;&#xA;Scrapy provides a few &lt;a href=&#34;https://docs.scrapy.org/en/latest/topics/exporters.html&#34;&gt;item exporters&lt;/a&gt; by default to export items in&#xA;commonly used file formats like CSV/JSON/XML. I usually use CSV to&#xA;export items, it is pretty convenient, and it comes in two ways:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;appending mode, for example, &lt;code&gt;scrapy crawl foo -o test.csv&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;overwriting mode with &lt;code class=&#34;verbatim&#34;&gt;-O&lt;/code&gt; option, like &lt;code&gt;scrapy crawl foo -O test.csv&lt;/code&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;But in the appending mode, it&amp;#39;s a bit annoying that it always appends&#xA;the header row before the newly scraped items, which is not correctly&#xA;in terms of CSV format.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Writing a Python Script in Emacs in 45 Minutes!</title>
      <link>https://blog.kenhuang.io/blog/2021-12-11-writing-python-in-emacs/</link>
      <pubDate>Sat, 11 Dec 2021 09:24:53 +0800</pubDate><author>kenhuang512@gmail.com (Ken Huang)</author>
      <guid>https://blog.kenhuang.io/blog/2021-12-11-writing-python-in-emacs/</guid>
      <description>&lt;p&gt;&#xA;Note: watch my live coding session of this article:&#xA;&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;&#xA;      &lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/MXF81Q0a91M?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;&#xA;    &lt;/div&gt;&#xA;&lt;/p&gt;&#xA;&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;&#xA;&lt;h2 id=&#34;headline-1&#34;&gt;&#xA;Intro&#xA;&lt;/h2&gt;&#xA;&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;&#xA;&lt;p&gt;&#xA;If you&amp;#39;ve heard some rumors of Emacs that it has a very steep learning curve (or that Emacs makes a computer slow), you may be too scared to look at it. It indeed has some learning curve (learning anything does have one), but it isn&amp;#39;t very steep. I learned this after getting my hands dirty with Emacs a few years ago.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Beautiful Soup 4 Cheatsheet</title>
      <link>https://blog.kenhuang.io/blog/2021-12-05-beautifulsoup4-cheatsheet/</link>
      <pubDate>Sat, 25 Sep 2021 23:01:40 +0800</pubDate><author>kenhuang512@gmail.com (Ken Huang)</author>
      <guid>https://blog.kenhuang.io/blog/2021-12-05-beautifulsoup4-cheatsheet/</guid>
      <description>&lt;figure&gt;&#xA;&lt;img src=&#34;https://www.crummy.com/software/BeautifulSoup/bs4/doc/_images/6.1.jpg&#34; alt=&#34;Beautiful Soup&#34; title=&#34;Click to enlarge the image&#34;/&gt;&#xA;&lt;figcaption&gt;&#xA;Beautiful Soup&#xA;&lt;/figcaption&gt;&#xA;&lt;/figure&gt;&#xA;&lt;p&gt;&#xA;Detailed docs: &lt;a href=&#34;https://www.crummy.com/software/BeautifulSoup/bs4/doc/&#34;&gt;the Beautiful Soup 4 Docs&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;&#xA;Assume &lt;code class=&#34;verbatim&#34;&gt;t&lt;/code&gt; is an object of &lt;code class=&#34;verbatim&#34;&gt;Tag&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;div id=&#34;outline-container-headline-1&#34; class=&#34;outline-2&#34;&gt;&#xA;&lt;h2 id=&#34;headline-1&#34;&gt;&#xA;Core concepts (classes)&#xA;&lt;/h2&gt;&#xA;&lt;div id=&#34;outline-text-headline-1&#34; class=&#34;outline-text-2&#34;&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;Tag&lt;/code&gt;, a Tag object corresponds to an XML or HTML tag.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;BeautifulSoup&lt;/code&gt;, the BeautifulSoup object represents the parsed document as a whole.&#xA;&#xA;You can treat it like a special Tag.&#xA;&#xA;It needs a parser to parse the document, a built-in parser is &lt;code class=&#34;verbatim&#34;&gt;&amp;#34;html.parser&amp;#34;&lt;/code&gt;, e.g. &lt;code&gt;soup = BeautifulSoup(&amp;#34;&amp;lt;html&amp;gt;a web page&amp;lt;/html&amp;gt;&amp;#34;, &amp;#39;html.parser&amp;#39;)&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;code class=&#34;verbatim&#34;&gt;NavigableString&lt;/code&gt;, a string corresponds to a bit of text (as you see it in the browser) within a tag.&#xA;&#xA;A NavigableString is just like a Python Unicode string, except that it also supports some of the features for navigating the tree and searching the tree.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/div&gt;&#xA;&lt;/div&gt;&#xA;&lt;div id=&#34;outline-container-headline-2&#34; class=&#34;outline-2&#34;&gt;&#xA;&lt;h2 id=&#34;headline-2&#34;&gt;&#xA;The &lt;code class=&#34;verbatim&#34;&gt;Tag&lt;/code&gt; class&#xA;&lt;/h2&gt;&#xA;&lt;div id=&#34;outline-text-headline-2&#34; class=&#34;outline-text-2&#34;&gt;&#xA;&lt;p&gt;&#xA;Object attributes:&lt;/p&gt;</description>
    </item>
    <item>
      <title>String Title Case in Clojure</title>
      <link>https://blog.kenhuang.io/blog/2021-07-25-clojure-string-title-case/</link>
      <pubDate>Sun, 25 Jul 2021 11:51:39 +0800</pubDate><author>kenhuang512@gmail.com (Ken Huang)</author>
      <guid>https://blog.kenhuang.io/blog/2021-07-25-clojure-string-title-case/</guid>
      <description>&lt;p&gt;&#xA;These days I like to write scripts for some tasks in Python instead of shell. One important reason I think that&amp;#39;s because Python is powerful at string manipulation.&lt;/p&gt;&#xA;&lt;p&gt;&#xA;Recently I&amp;#39;m learning Clojure, and I&amp;#39;m trying to find similar ways in Clojure, one of them is &lt;code class=&#34;verbatim&#34;&gt;s.title()&lt;/code&gt; for getting a title-cased version of a string. For example,&lt;/p&gt;&#xA;&lt;div class=&#34;src src-text&#34;&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&amp;gt;&amp;gt;&amp;gt; &amp;#39; Hello world&amp;#39;.title()&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&amp;#39; Hello World&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#xA;&lt;/div&gt;&#xA;&lt;p&gt;&#xA;How to do that in Clojure? To make the problem simple, let&amp;#39;s assume that the input string only has letters and spaces, that is, &lt;code class=&#34;verbatim&#34;&gt;[a-zA-Z ]&lt;/code&gt; in regex pattern.&lt;/p&gt;</description>
    </item>
    <item>
      <title>String Manipulation in Clojure</title>
      <link>https://blog.kenhuang.io/blog/2021-07-22-clojure-string-manipulation/</link>
      <pubDate>Thu, 22 Jul 2021 23:29:11 +0800</pubDate><author>kenhuang512@gmail.com (Ken Huang)</author>
      <guid>https://blog.kenhuang.io/blog/2021-07-22-clojure-string-manipulation/</guid>
      <description>&lt;p&gt;&#xA;Python string APIs are powerful and concise, that is an important reason I use it to do a lot of scripting these days, &lt;code class=&#34;verbatim&#34;&gt;join&lt;/code&gt;, &lt;code class=&#34;verbatim&#34;&gt;split&lt;/code&gt;, &lt;code class=&#34;verbatim&#34;&gt;strip&lt;/code&gt;, to name a few.&lt;/p&gt;&#xA;&lt;p&gt;&#xA;Since I am learning Clojure recently, I am wondering, how is string manipulation like in Clojure and how to implement equivalent ones?&lt;/p&gt;&#xA;&lt;p&gt;&#xA;I think it&amp;#39;s an excellent opportunity to get familiar with Clojure. Before diving into the implementation, how to declare a multi-line string?&lt;/p&gt;</description>
    </item>
    <item>
      <title>An Online Python re.findall Service</title>
      <link>https://blog.kenhuang.io/blog/2020-10-21-a-python-re.findall-service/</link>
      <pubDate>Wed, 21 Oct 2020 22:51:00 +0800</pubDate><author>kenhuang512@gmail.com (Ken Huang)</author>
      <guid>https://blog.kenhuang.io/blog/2020-10-21-a-python-re.findall-service/</guid>
      <description>&lt;p&gt;&#xA;As a programmer, I know that &lt;a href=&#34;https://www.gnu.org/s/grep/manual/grep.html&#34;&gt;grep&lt;/a&gt;, &lt;a href=&#34;https://www.gnu.org/software/sed/manual/sed.html&#34;&gt;sed&lt;/a&gt; and &lt;a href=&#34;https://www.gnu.org/software/gawk/manual/gawk.html&#34;&gt;awk&lt;/a&gt; are powerful for processing text, but they sometimes aren&amp;#39;t that straight-forward for specific tasks, as I need to think about how to filter the lines and the columns out.&lt;/p&gt;&#xA;&lt;p&gt;&#xA;So I wonder if there is a handy way to do these tasks?&lt;/p&gt;&#xA;&lt;p&gt;&#xA;After using it for a while, I think using regex directly can help, so I launched a &lt;a href=&#34;https://texttoolkit.com/re.findall&#34;&gt;re.findall service&lt;/a&gt; building on top of Python &lt;code class=&#34;verbatim&#34;&gt;re.findall&lt;/code&gt; API.&lt;/p&gt;</description>
    </item>
    <item>
      <title>How To Run Bleeding-edge Qtile Within a Virtualenv</title>
      <link>https://blog.kenhuang.io/blog/2019-09-29-how-to-run-the-bleeding-edge-code-of-qtile/</link>
      <pubDate>Sun, 29 Sep 2019 17:05:00 +0800</pubDate><author>kenhuang512@gmail.com (Ken Huang)</author>
      <guid>https://blog.kenhuang.io/blog/2019-09-29-how-to-run-the-bleeding-edge-code-of-qtile/</guid>
      <description>&lt;p&gt;&#xA;For having been using &lt;a href=&#34;https://www.gnome.org/&#34;&gt;GNOME&lt;/a&gt; for quite a long time, I was considering trying some tiling window managers to see what it&amp;#39;s like a few weeks ago. Along the way, I found a nice window manager written in Python: &lt;a href=&#34;http://www.qtile.org&#34;&gt;Qtile&lt;/a&gt;, what interests me most is that it&amp;#39;s a &lt;strong&gt;hackable&lt;/strong&gt; window manager, which makes it flexible to extend or change its behaviors.&lt;/p&gt;&#xA;&lt;p&gt;&#xA;Well, switching to use a tiling window manager is far simpler than I thought. There are two ways to have it:&lt;/p&gt;</description>
    </item>
    <item>
      <title>Generating org-mode Outlines for wikiHow Articles</title>
      <link>https://blog.kenhuang.io/blog/2019-07-20-generating-org-mode-outlines-for-wikihow-articles/</link>
      <pubDate>Sat, 20 Jul 2019 20:04:00 +0800</pubDate><author>kenhuang512@gmail.com (Ken Huang)</author>
      <guid>https://blog.kenhuang.io/blog/2019-07-20-generating-org-mode-outlines-for-wikihow-articles/</guid>
      <description>&lt;p&gt;&#xA;Recently I found some great articles on &lt;a href=&#34;https://www.wikihow.com&#34;&gt;wikiHow&lt;/a&gt;, then I want to keep notes of them in &lt;a href=&#34;https://orgmode.org/&#34;&gt;org-mode&lt;/a&gt; files.&lt;/p&gt;&#xA;&lt;p&gt;&#xA;At first, I manually copied the ToC of articles, but soon I found it&amp;#39;s tedious and takes a lot of time.&#xA;Today I wrote a &lt;a href=&#34;https://requests.readthedocs.io/&#34;&gt;requests&lt;/a&gt;-based Python script to help me extract the ToCs (Table of Content) into org-mode outlines. It takes two arguments, the first one is the URL, the second one is the containing heading&amp;#39;s level for the generated ToC in org-mode.&lt;/p&gt;</description>
    </item>
    <item>
      <title>ER Diagrams in Plain Text</title>
      <link>https://blog.kenhuang.io/blog/2019-07-06-render-erd-on-the-web/</link>
      <pubDate>Sat, 06 Jul 2019 11:59:00 +0800</pubDate><author>kenhuang512@gmail.com (Ken Huang)</author>
      <guid>https://blog.kenhuang.io/blog/2019-07-06-render-erd-on-the-web/</guid>
      <description>&lt;p&gt;&#xA;If you ever wonder how to plot &lt;a href=&#34;https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model&#34;&gt;ER diagrams&lt;/a&gt; in plain text, you may have already heard of &lt;a href=&#34;https://github.com/BurntSushi/erd&#34;&gt;erd&lt;/a&gt;.&#xA;It&amp;#39;s a cool command line program written by Andrew Gallant in Haskell, to &amp;#34;compile&amp;#34; plain text files into nicely looking images,&#xA;leveraging the power of &lt;a href=&#34;https://graphviz.org/&#34;&gt;GraphViz&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;&#xA;I&amp;#39;ve used erd for some time, it&amp;#39;s cool and the syntax is quite simple.&#xA;It&amp;#39;s also quite simple to install it on Linux, just install GraphViz and erd itself,&#xA;by following the instructions in the README page.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
