<?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 Alexander Fomalhaut on Medium]]></title>
        <description><![CDATA[Stories by Alexander Fomalhaut on Medium]]></description>
        <link>https://medium.com/@afomalhaut?source=rss-a8aa7d110f14------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1xCw5LA4195mqwSQrkXSnA.jpeg</url>
            <title>Stories by Alexander Fomalhaut on Medium</title>
            <link>https://medium.com/@afomalhaut?source=rss-a8aa7d110f14------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 03:39:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@afomalhaut/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[Building a Custom AI Chat Interface: A Deep Dive into Python, Ollama, and Aiohttp]]></title>
            <link>https://medium.com/@afomalhaut/building-a-custom-ai-chat-interface-a-deep-dive-into-python-ollama-and-aiohttp-9ae0f03ed886?source=rss-a8aa7d110f14------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ae0f03ed886</guid>
            <dc:creator><![CDATA[Alexander Fomalhaut]]></dc:creator>
            <pubDate>Fri, 13 Nov 2026 16:21:29 GMT</pubDate>
            <atom:updated>2026-11-13T16:45:56.504Z</atom:updated>
            <content:encoded><![CDATA[<p>This tutorial provides a hands-on guide to implementing a custom, local AI chat interface powered by Ollama. While many turnkey solutions exist, this guide is intended for developers who want to move beyond high-level wrappers and gain granular control over how they interact with a raw Large Language Model (LLM). By the end of this article, you will understand how to manage HTTP requests, encapsulate connection logic, and maintain conversation state manually.</p><p><strong>Prerequisites</strong>: A working knowledge of Python and the fundamentals of asynchronous programming (asyncio).</p><h3>Preparation</h3><h4>Step 1. Install Ollama</h4><p>First, you need to install the Ollama engine. Visit the <a href="https://ollama.com/download">official Ollama website</a> and follow the installation instructions for your operating system.</p><p>Once installed, verify the installation by checking the version in your terminal:</p><pre>ollama --version</pre><h4>Step 2. Pull a Model</h4><p>You can explore the available model library at <a href="https://ollama.com/search">https://ollama.com/search</a>. When selecting a model for local execution, it is crucial to consider your hardware constraints. Ensure your system has sufficient RAM (or VRAM if using a GPU) to hold the model weights, plus a few extra gigabytes to accommodate the context window.</p><p>For this tutorial, we will use a lightweight model that runs efficiently on most modern hardware: <a href="https://ollama.com/library/lfm3">LFM3</a>.</p><pre>ollama pull lfm3</pre><p>Once the download is complete, you can test it immediately in your terminal:</p><pre>ollama run lfm3</pre><h4>Step 3. Set Up the Python Environment</h4><p>We will intentionally avoid using the official <a href="https://pypi.org/project/ollama/">Ollama Python wrapper</a>. While convenient, wrappers often abstract away the underlying HTTP mechanics. To truly understand the interaction, we will use aiohttp to make raw asynchronous requests.</p><p>Install the dependency via pip:</p><pre>pip install aiohttp</pre><h3>Development</h3><h4>1. The Single Request Pattern</h4><p>Our first objective is to design the simplest possible application: a script that sends a single query to the LLM and prints the response, without any memory or conversation history.</p><p>Before writing Python code, we can verify the Ollama API endpoint using curl. This ensures the service is reachable and the JSON structure is correct:</p><pre>curl -X POST http://localhost:11434/api/chat \<br>    -H &#39;content-type: application/json&#39; \<br>    -d &#39;{&quot;model&quot;: &quot;lfm3&quot;, &quot;stream&quot;: false, &quot;messages&quot;: [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;hi&quot;}]}&#39;</pre><p>If the service is running, you will receive a JSON response. A typical response looks like this:</p><pre>{<br>  &quot;model&quot;: &quot;lfm3&quot;,<br>  &quot;created_at&quot;: &quot;2026-11-13T13:59:36.434841537Z&quot;,<br>  &quot;message&quot;: {<br>    &quot;role&quot;: &quot;assistant&quot;,<br>    &quot;content&quot;: &quot;Hello! How can I help you today?&quot;,<br>    &quot;thinking&quot;: &quot;The user says \&quot;hi\&quot;. We need to respond in a friendly manner. There&#39;s no request for content, just greeting. So we can say hello and ask how we can help.\n\nWe should follow the policies: no disallowed content. It&#39;s fine.\n\nThus answer: \&quot;Hello! How can I help you today?\&quot; Possibly add something about being an AI assistant.\n\nProceed.&quot;<br>  },<br>  &quot;done&quot;: true,<br>  &quot;done_reason&quot;: &quot;stop&quot;,<br>  &quot;total_duration&quot;: 678263079,<br>  &quot;load_duration&quot;: 1708613,<br>  &quot;prompt_eval_count&quot;: 11,<br>  &quot;prompt_eval_cached_count&quot;: 0,<br>  &quot;prompt_eval_duration&quot;: 21343000,<br>  &quot;eval_count&quot;: 88,<br>  &quot;eval_duration&quot;: 641932000<br>}</pre><p>Now, let’s replicate this behavior in Python using aiohttp:</p><pre>import asyncio<br><br>import aiohttp<br><br><br>async def main():<br>    url = &#39;http://localhost:11434/api/chat&#39;<br>    data = {<br>        &#39;model&#39;: &#39;lfm3&#39;,<br>        &#39;stream&#39;: False,<br>        &#39;messages&#39;: [<br>            {<br>                &#39;role&#39;: &#39;user&#39;,<br>                &#39;content&#39;: &#39;hi&#39;,<br>            },<br>        ],<br>    }<br>    async with aiohttp.ClientSession() as session:<br>        async with session.post(url, json=data) as resp:<br>            result = await resp.json()<br>            print(result[&#39;message&#39;][&#39;content&#39;])<br><br><br>if __name__ == &quot;__main__&quot;:<br>    asyncio.run(main())</pre><h4>2. Implementing a Request Wrapper (The Connector)</h4><p>In a production-grade application, you wouldn’t want to manage raw URLs and JSON payloads every time you want to talk to the AI. Instead, you should encapsulate this logic within a “Connector” class. This abstraction allows you to manage authentication, connection pooling, and error handling in one place.</p><pre>import asyncio<br><br>import aiohttp<br><br><br>class Connector:<br>    def __init__(self, chat_url: str, model: str):<br>        self._session = None<br>        self._chat_url = chat_url<br>        self._model = model<br><br>    async def init(self):<br>        self._session = aiohttp.ClientSession()<br><br>    async def close(self):<br>        if self._session:<br>            await self._session.close()<br><br>    async def request(self, messages: list) -&gt; str:<br>        data = {<br>            &#39;model&#39;: self._model,<br>            &#39;stream&#39;: False,<br>            &#39;messages&#39;: messages,<br>        }<br>        async with self._session.post(self._chat_url, json=data) as resp:<br>            result = await resp.json()<br>            return result[&#39;message&#39;][&#39;content&#39;]<br><br><br>async def main():<br>    conn = Connector(chat_url=&#39;http://localhost:11434/api/chat&#39;, model=&#39;lfm3&#39;)<br>    await conn.init()<br>    try:<br>        result = await conn.request([{&#39;role&#39;: &#39;user&#39;, &#39;content&#39;: &#39;hi&#39;}])<br>        print(result)<br>    finally:<br>        await conn.close()<br><br><br>if __name__ == &quot;__main__&quot;:<br>    asyncio.run(main())</pre><h4>3. Managing Conversation Context</h4><p>A true “chat” requires memory. To achieve this, we must accumulate the history of the conversation (the “context”) and send the entire thread back to the model with every new request.</p><p>In this implementation, we create a Context class to manage the list of messages. While professional implementations might include advanced features like context window management (summarization or truncation), our basic version will focus on maintaining the message history.</p><pre>import asyncio<br><br>import aiohttp<br><br><br>class Connector:<br>    def __init__(self, chat_url: str, model: str):<br>        self._session = None<br>        self._chat_url = chat_url<br>        self._model = model<br><br>    async def init(self):<br>        self._session = aiohttp.ClientSession()<br><br>    async def close(self):<br>        if self._session:<br>            await self._session.close()<br><br>    async def request(self, messages: list) -&gt; str:<br>        data = {<br>            &#39;model&#39;: self._model,<br>            &#39;stream&#39;: False,<br>            &#39;messages&#39;: messages,<br>        }<br>        async with self._session.post(self._chat_url, json=data) as resp:<br>            result = await resp.json()<br>            return result[&#39;message&#39;][&#39;content&#39;]<br><br><br>class Context:<br>    def __init__(self, system_prompt: str = &quot;&quot;):<br>        self._messages = []<br>        if system_prompt:<br>            self._messages.append({&#39;role&#39;: &#39;system&#39;, &#39;content&#39;: system_prompt})<br><br>    @property<br>    def messages(self) -&gt; list:<br>        return self._messages<br><br>    def add_user_message(self, content: str):<br>        self._messages.append({&#39;role&#39;: &#39;user&#39;, &#39;content&#39;: content})<br><br>    def add_assistant_message(self, content: str):<br>        self._messages.append({&#39;role&#39;: &#39;assistant&#39;, &#39;content&#39;: content})<br><br><br>async def main():<br>    conn = Connector(chat_url=&#39;http://localhost:11434/api/chat&#39;, model=&#39;lfm3&#39;)<br>    await conn.init()<br>    ctx = Context(<br>        system_prompt=&quot;Your name is WALL-E. Reply as this well-known robot.&quot;<br>    )<br>    try:<br>        while True:<br>            query = input(&quot;&gt;&gt;&gt; &quot;).strip()<br>            if query == &quot;/exit&quot;:<br>                break<br>            if query:<br>                ctx.add_user_message(query)<br>                resp = await conn.request(ctx.messages)<br>                ctx.add_assistant_message(resp)<br>                print(resp)<br>    finally:<br>        await conn.close()<br><br><br>if __name__ == &quot;__main__&quot;:<br>    asyncio.run(main())</pre><h3>Conclusion</h3><p>You now have a functional, asynchronous chat client built from the ground up. By bypassing high-level libraries, you’ve gained the ability to manipulate the exact structure of your API calls — a skill that is essential when moving toward more complex implementations like tool-calling, streaming responses, or custom RAG (Retrieval-Augmented Generation) pipelines.</p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ae0f03ed886" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Create and Publish a Python Library, Github + Pypi + Pdoc]]></title>
            <link>https://medium.com/@afomalhaut/how-to-create-and-publish-a-python-library-github-pypi-pdoc-c33ed9692f0d?source=rss-a8aa7d110f14------2</link>
            <guid isPermaLink="false">https://medium.com/p/c33ed9692f0d</guid>
            <dc:creator><![CDATA[Alexander Fomalhaut]]></dc:creator>
            <pubDate>Tue, 29 Jul 2025 16:14:10 GMT</pubDate>
            <atom:updated>2025-09-19T20:39:15.405Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, I am going to show you how to deploy a simple Python library as an open source project supporing installation via pip, Github CI/CD and the documentation.</p><p>The full code is provided on Github: <a href="https://github.com/fomalhaut88/pylib25](https://github.com/fomalhaut88/pylib25)">https://github.com/fomalhaut88/pylib25</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kvc-6B7AKXsBc1dkzOLmTg.png" /></figure><h3>Step 1. Create your Python library</h3><p>Let the library be called pylib25. Do not ask me why 25 - it is because 5*5=25. So first, we create a folder pylib25 with the following structure:</p><pre>- pylib25/<br>    - .github/<br>        - workflows/<br>            - publish.yml<br>    - docs/<br>    - pylib25/<br>        - __init__.py<br>    - .gitignore<br>    - README.md<br>    - setup.py<br>    - tests.py</pre><p>Of course, the structure may be more complicated depending on the complexity of your project. But what is important is that the code of the library is located in its own package (inner pylib25 in our example) and also we keep README.md, setup.py, tests.py, .gitignore in the root. We also do not consider the case for non-python files, dependencies on other libraries or some other extra stuff, because simpler our project is easier to understand and remember the deploy specifics.</p><h3>Step 2. Write the code</h3><p>Now we write a function in __init__.py this way:</p><pre>&quot;&quot;&quot;<br>`pylib25` is a simple library to illustrate how to deploy a Python library.<br>&quot;&quot;&quot;<br><br>def sqr(x: float) -&gt; float:<br>    &quot;&quot;&quot;<br>    Get square of the given `x`.<br>    ```<br>    from pylib25 import sqr<br>    assert sqr(5.0) == 25.0<br>    ```<br>    &quot;&quot;&quot;<br>    return x ** 2</pre><p>Here is the function sqr that is public and supposed to be used from the outside. It includes the annotation and doc-string so the documentation page will contain this information. Also we added the doc-string to the whole library. Normally, it can be the same as README.md.</p><h3>Step 3. Add unit tests</h3><p>Since we are good guys, we are to test what we just implemented. To do this we add this code to tests.py:</p><pre>from unittest import TestCase<br><br>from pylib25 import sqr<br><br><br>class Test(TestCase):<br>    def test_sqr(self):<br>        self.assertEqual(sqr(5.0), 25.0)</pre><p>The test command is: python -m unittest tests. Try it to check there is no typo in the code. The result must be OK if everything is really OK. We do not use pytest or other extremely convenient ways for testing because we decided avoiding external dependencies. Since Python has built-in unittest module, it should be enough for our small example library.</p><h3>Step 4. Write setup.py</h3><p>Once we cared of the code and the tests, it is time to make the library installable. The easiest way to do it is to write setup.py this way:</p><pre>from setuptools import setup<br><br>def get_long_description():<br>    with open(&#39;README.md&#39;) as f:<br>        return f.read()<br><br>setup(<br>    name=&#39;pylib25&#39;,<br>    version=&quot;0.1.0&quot;,<br>    author=&#39;Alexander Khlebushchev&#39;,<br>    description=&quot;A simple library to illustrate how to deploy a Python library..&quot;,<br>    url=&quot;https://github.com/fomalhaut88/pylib25&quot;,<br>    project_urls={<br>        &quot;Homepage&quot;: &quot;https://pypi.org/project/pylib25/&quot;,<br>        &quot;Documentation&quot;: &quot;https://fomalhaut88.github.io/pylib25/&quot;,<br>        &quot;Source&quot;: &quot;https://github.com/fomalhaut88/pylib25&quot;,<br>        &quot;Issues&quot;: &quot;https://github.com/fomalhaut88/pylib25/issues&quot;,<br>    },<br>    packages=[&#39;pylib25&#39;],<br>    license=&quot;MIT&quot;,<br>    zip_safe=False,<br>    long_description=get_long_description(),<br>    long_description_content_type=&quot;text/markdown&quot;,<br>    python_requires=&#39;&gt;=3.12&#39;,<br>    install_requires=[],<br>    classifiers=[<br>        &quot;Development Status :: 5 - Production/Stable&quot;,<br>        &quot;Intended Audience :: Developers&quot;,<br>        &quot;Topic :: Software Development :: Libraries :: Python Modules&quot;,<br>        &quot;License :: OSI Approved :: MIT License&quot;,<br>        &quot;Programming Language :: Python :: 3.12&quot;,<br>        &quot;Operating System :: OS Independent&quot;,<br>    ],<br>)</pre><p>In order to check whether it works as expected, it is recommended to follow these steps to install it in a separate place.</p><ol><li>Create a new folder pylib25-example somewhere.</li><li>Inside it create a new Python virtualenv: python -m virtualenv .venv</li><li>Activate the virtualenv: source .venv/bin/activate (or .venv/Scripts/activate)</li><li>Install the library via PIP: pip install -U path/to/pylib25</li><li>Create a script main.py with the following content:</li></ol><pre>from pylib25 import sqr<br>print(sqr(5.0))</pre><p>6. Run the script: python main.py</p><p>You will see 25.0 in the end if everything is correct. Please note that in setup.py you should specify the author, URLs and other parameters correctly for your particular case. You need to have <a href="https://github.com/">Github</a> and <a href="https://pypi.org/">Pypi</a> accounts to proceed. Obviously, my author name is Alexander Khlebushchev and username is fomalhaut88, so in your case there will be something yours.</p><h3>Step 5. Publish on Github</h3><p>If you do not have a <a href="https://github.com/">Github</a> account, create one to continue.</p><p>Before publishing, we should specify some files to ignore (in .gitignore): some caches, temporary files, etc. Commonly, we ignore the folders __pycache__, build, dist and egg-info because they are auxiliary and generated automatically for your platform and Python version. So .gitignoremay look like this:</p><pre>__pycache__/<br>/build/<br>/dist/<br>/*.egg-info/</pre><p>Also you may need to fill README.md. It is time to do it maybe in a brief way so the published page will not look too blank.</p><p>After that we should follow these steps:</p><ol><li>Create a new repository in Github (we choose a public one so we will have Github documentation later) called pylib25 in our case.</li><li>Initialize Git in our project: git init</li><li>Make an initial commit: git add . and git commit -am &quot;Initial commit&quot;</li><li>Specify the remote repository: git remote add origin git@github.com:fomalhaut88/pylib25.git</li><li>Push the changes to Github: git push -u origin master</li></ol><p>Once it is done, the code will appear on Github by the link <a href="https://github.com/fomalhaut88/pylib25">https://github.com/fomalhaut88/pylib25</a> in our case.</p><h3>Step 6. CI/CD and publish on Pypi</h3><p><a href="https://pypi.org/">Pypi</a> is a standard place where Python libraries live, each one can be installed using pip by the name (for our case: pip install pylib25). If you do not have an account there, create one.</p><p>On Pypi in your “Account settings” find the section “API tokens” and create a new token called PYPI_API_TOKEN, its value must be something like pypi-.... After that, on Github in the project &quot;Settings&quot; find &quot;Secrets and variables&quot; -&gt; &quot;Actions&quot; and add &quot;New repository secret&quot; called PYPI_API_TOKEN and the value you just received on Pypi (started with pypi-). Once you did it, Github will get the access to publish projects on Pypi.</p><p>Now we are ready to create a workflow adding the file .github/workflows/publish.yml with the following content:</p><pre>name: Publish Python distributions to PyPI<br>on:<br>  push:<br>    branches: [ master ]<br>  pull_request:<br>    branches: [ master ]<br>jobs:<br>  build-n-publish:<br>    name: Build and publish to PyPI<br>    runs-on: ubuntu-latest<br>    steps:<br>      - uses: actions/checkout@v3<br>      - name: Set up Python 3.12<br>        uses: actions/setup-python@v4<br>        with:<br>          python-version: 3.12<br>      - name: Install build dependencies<br>        run: python -m pip install --upgrade pip setuptools wheel<br>      - name: Run unittests<br>        run: python -m unittest tests<br>      - name: Build binary wheel and a source tarball<br>        run: python setup.py sdist bdist_wheel<br>      - name: Publish distribution to PyPI<br>        uses: pypa/gh-action-pypi-publish@release/v1<br>        with:<br>          user: __token__<br>          password: ${{ secrets.PYPI_API_TOKEN }}</pre><p>After adding this file and pushing it to the repository, view “Actions” on Github. A new action will start to process. It may take a while, when it finishes. On success, you will see the library on Pypi (in our case by the URL <a href="https://pypi.org/project/pylib25/).">https://pypi.org/project/pylib25/).</a> On fail, you can click on the action to view the log and fix the error.</p><h3>Step 7. Documentation on Github</h3><p>One simple way to generate documentation is <a href="https://pypi.org/project/pdoc/">pdoc</a>. First, you need to install it locally using pip install pdoc. After that you can generate the documentation from Python doc-strings by the command pdoc pylib25 -o docs, so the folder docs will appear in the root having the necessary HTML content. You can open index.html locally in your browser to view the result. And do not forget to push docs to Github in the end.</p><p>Now we are to deploy it to a public endpoint. Fortunately, Github provides this opportunity in “Settings” -&gt; “Pages”. In the section “Build and deployment” choose the source as “Deploy from a branch” and specify the branch master and the folder docs. After a while the documentation will be deployed (in our case by the URL <a href="https://fomalhaut88.github.io/pylib25/).">https://fomalhaut88.github.io/pylib25/).</a></p><h3>Conclusion</h3><p>We considered how to publish a Python library, covering Github, Pypi and documentation with Pdoc. Of course, this tutorial is quite limited and in real projects you may need a lot of improvements. But what we’ve done still can be a good point to start adding new features one by one during the development process. Good luck!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c33ed9692f0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lbasedb — database for datasets]]></title>
            <link>https://medium.com/@afomalhaut/lbasedb-database-for-datasets-5918ed878bf7?source=rss-a8aa7d110f14------2</link>
            <guid isPermaLink="false">https://medium.com/p/5918ed878bf7</guid>
            <dc:creator><![CDATA[Alexander Fomalhaut]]></dc:creator>
            <pubDate>Sat, 12 Apr 2025 08:24:52 GMT</pubDate>
            <atom:updated>2025-05-28T14:31:51.431Z</atom:updated>
            <content:encoded><![CDATA[<h3>Lbasedb — database for datasets</h3><p>Lbasedb is a minimalist, column-oriented database designed for fast and efficient handling of large datasets. Built in Rust, it offers O(1) write performance, compact storage, and simple integration via REST API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4MiKv-MlTmL3_eoMzSRc1g.png" /></figure><h3>Introduction</h3><p>Nowadays, there are many databases that are universal, efficient and having very friendly user interface. The examples are: PostgreSQL, MySQL, MongoDB, ClickHouse, etc. Each one has its own unique features and advantages regarding the others that is done by the algorithms and the concept. There is no “the best” database because what has advantage in one tasks usually lacks in the other ones, so each database can be “the best” in the use cases it was developed for. In this article, we are researching the database Lbasedb specially designed for working with huge datasets. Most famous databases are general purpose, that means they are applicable in a set of standard data processing like adding new information, updating it or deleting it, performing an efficient search, supporting flexible data types and so on. Obviously, they have performance limits that can be a problem in some projects where we require a too fast data fetching or storing too many records on a small disk. What if we sacrifice some general purpose requirements in order to boost some special operations that are typical for huge datasets? This is where the motivation of Lbasedb comes from.</p><p>Datasets usually require:</p><ol><li>Small size in the storage</li><li>Fast adding new records</li><li>Fast extracting a subset in a known range and listed columns</li><li>Fast bulk update</li><li>Fast and convenient record iterating (one by one or in chunks)</li><li>Flexible working with adding, renaming and deleting columns</li><li>Wide compatibility with data processing tools (like Machine Learning tools in Python)</li></ol><p>Comparing to general purposes, datasets rarely require searching for special records, deleting the data. In other words, datasets do not have to support CRUD entirely.</p><p>Inspired by these ideas, technically Lbasedb has following features:</p><ol><li>Column-oriented architecture, so working with a column does not impact on the others.</li><li>Lack of indexes, so add operation has the complexity O(1) (adding is fast and does not depend on the whole database size).</li><li>Lack of deleting, that makes selecting and updating subsets easier and faster. Instead of deleting, truncate operation is supported that may be helpful in some cases.</li><li>Limited datatypes support: int32, int64, float32, float64, raw bytes (any size), nesting is not added so the record structure is always flat. Since these datatypes are universal it can be easily integrated with many other tools for dataset processing or visualizing.</li></ol><p>Under the hood, the core of Lbasedb is written in Rust (<a href="https://github.com/fomalhaut88/lbasedb">https://github.com/fomalhaut88/lbasedb</a>) making the access to disk with asynchronous tokio framework, that brings extremely high performance and simple integration of the pieces into other Rust projects as a common dependency (<a href="https://crates.io/crates/lbasedb">https://crates.io/crates/lbasedb</a>). Also Lbasedb has REST API interface in JSON format and can be used as a Docker container or built from source as a Rust executable project (<a href="https://github.com/fomalhaut88/lbasedb-api">https://github.com/fomalhaut88/lbasedb-api</a>).</p><h3>Installation</h3><ol><li>Pull the docker image:</li></ol><pre>docker pull fomalhaut88/lbasedb</pre><p>2. Create and run a container:</p><pre>docker run \<br>    -p 8080:8080 \<br>    --restart=always \<br>    --ulimit nofile=1024:1024 \<br>    --volume /var/lib/lbasedb:/app/db \<br>    --name lbasedb-api-app \<br>    --env WORKERS=16 \<br>    -d fomalhaut88/lbasedb</pre><p>After that following <a href="http://localhost:8080/version">http://localhost:8080/version</a> will show you the version.</p><p>For the API and environment details:</p><p><a href="https://hub.docker.com/r/fomalhaut88/lbasedb">https://hub.docker.com/r/fomalhaut88/lbasedb</a></p><h3>How to start</h3><h3>1. Create a feed</h3><p>Feed is a table in Lbasedb, it contains records and columns. In order to create one called “xyz” just perform POST http://localhost:8080/feed with the content {&quot;name&quot;: &quot;xyz&quot;}. After that GET http://localhost:8080/feed will list the available feeds.</p><h3>2. Create columns</h3><p>Once feed is created, you may need to have columns. To create a 32-bit integer column, perform POST http://localhost:8080/col?feed=xyz with the content {&quot;name&quot;: &quot;x&quot;, &quot;datatype&quot;: &quot;Int32&quot;}. We also as an example add the columns &quot;y&quot; and &quot;z&quot; of the same type. So now GET http://localhost:8080/col?feed=xyz shows the columns you just created.</p><h3>3. Add data</h3><p>To fill the feed with a data, perform POST http://localhost:8080/data?feed=xyz with the content {&quot;x&quot;: [1, 2, 3, 4, 5], &quot;y&quot;: [6, 7, 8, 9, 10], &quot;z&quot;: [11, 12, 13, 14, 15]} (if there were byte datatype, you would need to set bytes as Base64 strings). So after that GET http://localhost:8080/size?feed=xyz will return 5. As example, if you need a subset of 3 records starting with second one (index 1) and columns &quot;y&quot; and &quot;z&quot;, do GET <a href="http://localhost:8080/data?feed=xyz&amp;ix=1&amp;size=3&amp;col[]=y&amp;col[]=z.">http://localhost:8080/data?feed=xyz&amp;ix=1&amp;size=3&amp;col[]=y&amp;col[]=z.</a></p><h3>4. Update data</h3><p>Lbasedb supports bulk update that means we can update a whole subset in the stored dataset. For example, we would like to update “x” and “z” only starting with index 2 with the subset {&quot;x&quot;: [100, 100], &quot;z&quot;: [300, 300]}, so all we need is to perform PATCH http://localhost:8080/data?feed=xyz&amp;ix=2 passing the update subset in the body. &quot;y&quot; will not change, but if you use PUT method instead of PATCH the columns not listed in the subset will be zeroed.</p><h3>Extra</h3><p>We just considered very basic things to create a database structure and simple commands to work with datasets. Lbasedb also supports renaming, size change (it truncates the feed) and working with raw bytes instead of JSON that may be useful in some performance cases to avoid data converting on the DBMS side. See the details in the documentation: <a href="https://hub.docker.com/r/fomalhaut88/lbasedb">https://hub.docker.com/r/fomalhaut88/lbasedb</a>.</p><h3>Recommendations</h3><p>Mainly, Lbasedb is recommended to use if you need to save a data stream that will not have deleted records and is supposed to be analyzed or visualized by third-party tools. Lbasedb is also flexible to add, rename and remove columns. But since Lbasedb is not a general purpose database, it may be not suitable in some other cases. For example, Lbasedb does not support searching for records by a column value at all.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5918ed878bf7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to import C in Python]]></title>
            <link>https://medium.com/@afomalhaut/how-to-import-c-in-python-d85324c5680c?source=rss-a8aa7d110f14------2</link>
            <guid isPermaLink="false">https://medium.com/p/d85324c5680c</guid>
            <dc:creator><![CDATA[Alexander Fomalhaut]]></dc:creator>
            <pubDate>Tue, 18 Mar 2025 10:53:17 GMT</pubDate>
            <atom:updated>2025-06-02T12:33:01.209Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article I am going to tell you how to implement a function in C, to compile it and import in Python. This can be helpful for boosting performance in some certain code sections in your Python application. Below I will describe and implement the algorithm of the longest common subsequence (<a href="https://en.wikipedia.org/wiki/Longest_common_subsequence">https://en.wikipedia.org/wiki/Longest_common_subsequence</a>) in Python and C importing it into Python. And in the end I will benchmark these solutions.</p><p>The repository: <a href="https://github.com/fomalhaut88/how-to-import-c-in-python">https://github.com/fomalhaut88/how-to-import-c-in-python</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oRHrLtaH-7mJQ8HXTCe67w.png" /></figure><h3>The task</h3><h4>Formulation</h4><p>We have two sequences of elements (numbers or symbols, for example). It is necessary to find the longest subsequence each element of which is represented in both of given ones keeping the order. Read more about it in Wikipedia (<a href="https://en.wikipedia.org/wiki/Longest_common_subsequence">https://en.wikipedia.org/wiki/Longest_common_subsequence</a>).</p><p>I thing most of programmers know about this task. Particularly, the algorithm is included in the standard utility <strong>difflib</strong> (<a href="https://docs.python.org/3/library/difflib.html">https://docs.python.org/3/library/difflib.html</a>) that compares the contents of two files for differences.</p><p>As a sequence let us consider symbols in a string, and instead of the subsequence itself we will calculate its length only. Deep and comprehensive solution is not our goal is this article, we just need an example of an algorithm that is not trivial on one hand, and is easy to implement on another one.</p><h4>Python implementation</h4><pre># lcs_py.py<br><br>def solve(s1, s2):<br>    n1 = len(s1)<br>    n2 = len(s2)<br><br>    row = [0 for _ in range(n1)]<br>    for i2 in range(n2):<br>        row_p = row[:]<br>        for i1 in range(n1):<br>            if s1[i1] == s2[i2]:<br>                row[i1] = 1 + (row_p[i1 - 1] if i1 &gt; 0 else 0)<br>            else:<br>                row[i1] = max(row[i1 - 1] if i1 &gt; 0 else 0, row_p[i1])<br><br>    return row[-1]</pre><h4>Cythonization</h4><p>Cythonization is a way to boost the performance of a Python function. Shortly, the main idea is to modify your Python code in some places (in order to convert it into Cython code), then it is possible to compile it into a C-code and into DLL. After that this library is accessible as a Python module with the accustomed syntax.</p><pre># lcs_pyx.pyx<br><br>def solve(str s1, str s2):<br>    n1 = len(s1)<br>    n2 = len(s2)<br><br>    row = [0 for _ in range(n1)]<br>    for i2 in range(n2):<br>        row_p = row[:]<br>        for i1 in range(n1):<br>            if s1[i1] == s2[i2]:<br>                row[i1] = 1 + (row_p[i1 - 1] if i1 &gt; 0 else 0)<br>            else:<br>                row[i1] = max(row[i1 - 1] if i1 &gt; 0 else 0, row_p[i1])<br><br>    return row[-1]</pre><p>Before importing it we need an auxiliary file to compile:</p><pre># setup_pyx.py<br><br>from distutils.core import setup<br>from Cython.Build import cythonize<br><br>setup(<br>    ext_modules=cythonize(&quot;lcs_pyx.pyx&quot;)<br>)</pre><p>You also need to have Cython installed. If not, use this command:</p><pre>$ python -m pip install Cython</pre><p>Compilation command:</p><pre>$ python setup_pyx.py build_ext --inplace</pre><p>After that the compiled files will appear called build, lcs_pyx.c and lcs_pyx.cpython-312-x86_64-linux-gnu.so .</p><h4>C implementation</h4><p>Same algorithm in C would look like:</p><pre>/* lcs_c.h */<br><br>int solve(const char* s1, const char* s2);</pre><pre>/* lcs_c.c */<br><br>#include &lt;string.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>#include &quot;lcs_c.h&quot;<br><br>#define max(x, y) (((x) &gt;= (y)) ? x : y)<br><br><br>int solve(const char* s1, const char* s2) {<br>    int n1 = strlen(s1);<br>    int n2 = strlen(s2);<br><br>    int* row = (int*)calloc(sizeof(int), n1);<br>    int* row_p = (int*)calloc(sizeof(int), n1);<br><br>    memset(row, 0, sizeof(int) * n1);<br><br>    for (int i2 = 0; i2 &lt; n2; i2++) {<br>        memcpy(row_p, row, sizeof(int) * n1);<br>        for (int i1 = 0; i1 &lt; n1; i1++) {<br>            if (s1[i1] == s2[i2]) {<br>                row[i1] = 1 + ((i1 &gt; 0) ? row_p[i1 - 1] : 0);<br>            }<br>            else {<br>                row[i1] = max(<br>                    (i1 &gt; 0) ? row[i1 - 1] : 0,<br>                    row_p[i1]<br>                );<br>            }<br>        }<br>    }<br><br>    int result = row[n1 - 1];<br><br>    free(row);<br>    free(row_p);<br><br>    return result;<br>}</pre><p>Compiling it:</p><pre>$ gcc -fPIC lcs_c.c -shared -O3 -o lcs_c.so</pre><h3>Benchmark</h3><pre># bench.py<br><br>import os<br>import ctypes<br>from time import time<br>from random import choice<br><br>import lcs_py<br>import lcs_pyx<br><br>so_path = os.path.join(os.path.dirname(__file__), &#39;lcs_c.so&#39;)<br><br>lcs_c = ctypes.CDLL(so_path)<br>lcs_c_solve = lambda s1, s2: lcs_c.solve(<br>    ctypes.c_char_p(s1.encode(&#39;utf-8&#39;)),<br>    ctypes.c_char_p(s2.encode(&#39;utf-8&#39;))<br>)<br><br>LENGTH = 5000<br><br><br>def gen_random_word(length):<br>    return &#39;&#39;.join(choice(&#39;0123456789abcdef&#39;) for _ in range(length))<br><br><br>def test(func, word1, word2):<br>    tb = time()<br>    res = func(word1, word2)<br>    te = time()<br>    print(&quot;time: {:.3f} secs, result: {}&quot;.format(te - tb, res))<br><br><br>word1 = gen_random_word(LENGTH)<br>word2 = gen_random_word(LENGTH)<br><br>test(lcs_py.solve, word1, word2)<br>test(lcs_pyx.solve, word1, word2)<br>test(lcs_c_solve, word1, word2)</pre><p>In the script I generate words with length LENGTH = 5000 consisting of random digits and letters from a to f. After that I measure the execution time. The result impresses. On my machine it is following:</p><pre>$ python bench.py<br>time: 3.899 secs, result: 1954<br>time: 2.174 secs, result: 1954<br>time: 0.024 secs, result: 1954</pre><p>As we see, pure Python is about 150 times slower than C. Cythonization did help but it is about 2 times faster only.</p><h3>Conclusion</h3><p>I think the conclusion is obvious. Each approach is acceptable depending on the task, time for developing, performance requirements and other factors. For example, if performance is not the goal, Python seems to be a good choice. Otherwise you should write in C. Cython is an intermediate option, it is especially convenient if you already have a code in Python and it won’t take much time to make the necessary pyx-file adding the types and other little modifications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d85324c5680c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to install and configure a OpenVPN server on Ubuntu]]></title>
            <link>https://medium.com/@afomalhaut/how-to-install-and-configure-a-openvpn-server-on-ubuntu-355b41039d29?source=rss-a8aa7d110f14------2</link>
            <guid isPermaLink="false">https://medium.com/p/355b41039d29</guid>
            <dc:creator><![CDATA[Alexander Fomalhaut]]></dc:creator>
            <pubDate>Fri, 06 Dec 2024 14:53:47 GMT</pubDate>
            <atom:updated>2025-08-15T20:41:10.052Z</atom:updated>
            <content:encoded><![CDATA[<p>When I was doing it first time, this task was quite challengeable, because there were many tutorials how to do it, but each one had critical disavdantages so I was unable to complete. As a user, I did not want to dive into the inner stuff like changing algorithms and additional settings for the encryption. I even did not want to know what each key is needed for. So I decided to write a short step-by-step instruction that allows anyone to install OpenVPN server without special knowledge about how VPN works inside. This article is just an instruction to perform and make it work, so if you need advanced information go to another resource. As the base, I took the instruction from a Russian hosting provider Reg Ru (<a href="https://www.reg.ru/">https://www.reg.ru/</a>) translating it into English (the original link: <a href="https://www.reg.ru/blog/sozdayem-sobstvennyy-vpn-server/">https://www.reg.ru/blog/sozdayem-sobstvennyy-vpn-server/</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-VgUZI_-w8zW6MvLphBnVA.png" /></figure><p>Versions:</p><ul><li>OS: Ubuntu 24.04.1 LTS (from lsb_release -a)</li><li>OpenVPN: OpenVPN 2.6.12 (from openvpn --version)</li></ul><h4>Step 1. Install OpenVPN utilities</h4><ul><li>Update: apt update</li><li>Install utilities: apt install openvpn easy-rsa net-tools</li></ul><h4>Step 2. Prepare easy-rsa</h4><ul><li>Copy easy-rsa files into OpenVPN folder: cp -R /usr/share/easy-rsa /etc/openvpn/</li><li>Go to easy-rsa: cd /etc/openvpn/easy-rsa</li><li>Set easy-rsa environment variable: export EASYRSA=$(pwd)</li><li>Initialize: ./easyrsa init-pki</li><li>Build certificated: ./easyrsa build-ca (enter a password and remember it, let us call it Password 1)</li><li>Create a directory for certificates we are collecting: mkdir /etc/openvpn/certs/</li><li>Copy CA certificate: cp /etc/openvpn/easy-rsa/pki/ca.crt /etc/openvpn/certs/ca.crt</li></ul><h4>Step 3. Generate a private key</h4><ul><li>Generate a private key: ./easyrsa gen-req server nopass</li><li>Sign the key: ./easyrsa sign-req server server (enter “yes” and Password 1 when required)</li><li>Copy the certificate: cp /etc/openvpn/easy-rsa/pki/issued/server.crt /etc/openvpn/certs/</li><li>Copy the key: cp /etc/openvpn/easy-rsa/pki/private/server.key /etc/openvpn/certs/</li><li>Generate a params file: openssl dhparam -out /etc/openvpn/certs/dh2048.pem 2048</li><li>Create a HMAC file: openvpn --genkey secret /etc/openvpn/certs/ta.key</li></ul><h4>Step 4. Configure and run the server</h4><ul><li>Create a file /etc/openvpn/server.conf (using nano /etc/openvpn/server.conf) with the following content:</li></ul><pre>port 1194<br><br>;proto tcp<br>proto udp<br><br>;dev tap<br>dev tun<br><br>ca /etc/openvpn/certs/ca.crt<br>cert /etc/openvpn/certs/server.crt<br>key /etc/openvpn/certs/server.key<br><br>dh /etc/openvpn/certs/dh2048.pem<br><br>server 10.8.0.0 255.255.255.0<br><br>ifconfig-pool-persist /etc/openvpn/ipp.txt<br><br>push &quot;redirect–gateway def1 bypass–dhcp&quot;<br>;push &quot;dhcp-option DNS 8.8.8.8&quot;  # Customize DNS server<br><br>;duplicate–cn<br><br>keepalive 10 120<br><br>remote-cert-tls client<br>tls-auth /etc/openvpn/certs/ta.key 0<br><br>cipher AES-256-CBC<br><br>;compress lz4–v2<br>;push &quot;compress lz4–v2&quot;<br><br>;max–clients 100<br><br>;user nobody<br>;group nobody<br><br>persist-key<br>persist-tun<br><br>status openvpn–status.log<br><br>;log openvpn.log<br><br>;log–append openvpn.log<br><br>verb 4<br><br>explicit-exit-notify 1</pre><ul><li>Start OpenVPN service: systemctl start openvpn@server.service</li><li>Enable OpenVPN service: systemctl enable openvpn@server.service</li></ul><h4>Step 5. Configure routing</h4><ul><li>Create a directory for the configuration: mkdir /root/bin/</li><li>Create a file /root/bin/vpn_route.sh (using nano /root/bin/vpn_route.sh) with the following content:</li></ul><pre>#!/bin/sh<br>DEV=&#39;eth0&#39;  # From `route | grep &#39;^default&#39; | grep -o &#39;[^ ]*$&#39;`<br>PRIVATE=10.8.0.0/24<br># sysctl net.ipv4.ip_forward=1<br>iptables -I FORWARD -j ACCEPT<br>iptables -t nat -I POSTROUTING -s $PRIVATE -o $DEV -j MASQUERADE</pre><ul><li>Set DEV according to what the command route | grep &#39;^default&#39; | grep -o &#39;[^ ]*$’ returns.</li><li>Set permissions: chmod 755 /root/bin/vpn_route.sh</li><li>Create an autorun script openvpn-server-routing.service (using nano /etc/systemd/system/openvpn-server-routing.service) with the following content:</li></ul><pre>[Unit]<br>Description=Turn on OpenVPN routing<br>[Service]<br>ExecStart=/root/bin/vpn_route.sh<br>[Install]<br>WantedBy=multi-user.target</pre><ul><li>Enable the created service: systemctl enable openvpn-server-routing</li></ul><h4>Step 6. Configure client</h4><ul><li>Generate a private key for a client (called client1): ./easyrsa gen-req client1 nopass</li><li>Sign the generated key: ./easyrsa sign-req client client1 (enter “yes” and Password 1 when required)</li><li>Create a file client1.ovpn (using nano client1.ovpn) with the following content:</li></ul><pre>client<br> <br>remote 123.123.123.123  # Change it to the server IP address<br> <br>port 1194 <br> <br>dev tun<br> <br>;proto tcp<br>proto udp<br> <br>;remote my–server–1 1194<br>;remote my–server–2 1194<br> <br>;remote–random<br> <br>resolv-retry infinite<br> <br>nobind<br> <br>redirect-gateway def1 bypass-dhcp<br>;dhcp-option DNS 8.8.8.8  # Customize DNS<br> <br>persist-key<br>persist-tun<br> <br>;http–proxy–retry # retry on connection failures<br>;http–proxy [proxy server] [proxy port #]<br> <br>;mute–replay–warnings<br> <br>remote-cert-tls server <br> <br>key-direction 1<br> <br>cipher AES-256-CBC<br> <br>verb 3<br> <br>&lt;ca&gt;<br># Content of the file /etc/openvpn/certs/ca.crt<br>&lt;/ca&gt;<br>&lt;cert&gt;<br># Content of the file /etc/openvpn/easy-rsa/pki/issued/client1.crt<br>&lt;/cert&gt;<br>&lt;key&gt;<br># Content of the file /etc/openvpn/easy-rsa/pki/private/client1.key<br>&lt;/key&gt;<br>&lt;tls-auth&gt;<br># Content of the file /etc/openvpn/certs/ta.key<br>&lt;/tls-auth&gt;</pre><ul><li>In the parameter remote replace 123.123.123.123 with IP address of your server.</li><li>Insert the contents of the files /etc/openvpn/certs/ca.crt, /etc/openvpn/easy-rsa/pki/issued/client1.crt, /etc/openvpn/easy-rsa/pki/private/client1.key, /etc/openvpn/certs/ta.key into the tags &lt;ca&gt;&lt;/ca&gt;, &lt;cert&gt;&lt;/cert&gt;, &lt;key&gt;&lt;/key&gt;, &lt;tls-auth&gt;&lt;/tls-auth&gt;.</li></ul><h4>Additionally</h4><ol><li>Be sure that the port 1194 is forwarding (as UDP by default or TCP depending on the settings above).</li><li>Sometimes it is necessary to change DNS server. The best way for Linux users is to change “nameserver” in /etc/resolv.conf (to 8.8.8.8, for example), but keep in mind that usually /etc/resolv.conf is a symlink referring to /run/systemd/resolve/stub-resolv.conf that is managed by systemd-resolved service so you cannot just open it with nano and modify it, the update will not be applied after reboot. Instead, you should remove the symlink and create the file /etc/resolv.conf having the necessary content. As for Windows, just uncomment the line push &quot;dhcp-option DNS 8.8.8.8&quot; in /etc/openvpn/server.conf on the server side (do not forget to restart the service with systemctl start openvpn@server.service) or dhcp-option DNS 8.8.8.8 in client1.ovpn on the client side.</li></ol><h4>If you change the server connection</h4><p>In case if your server connection has changed, for example you are having a new internet provider, or physically moved the computer somewhere, or you changed the way it is connected to the internet (from cable to wifi or back), you have to make some changes in the configuration.</p><ol><li>If the server IP is changed update the client configuration in client1.ovpn : in the 3-rd line after remote white down your new server IP address (it must be static).</li><li>Do not forget to ensure the forwarded port 1194 in your router settings.</li><li>Modify DEV in the file /root/bin/vpn_route.sh according to your new network settings (try route | grep &#39;^default&#39; | grep -o &#39;[^ ]*$&#39; to view the necessary device for DEV). Use bash /root/bin/vpn_route.sh to apply the changes.</li><li>If you updated the versions of Ubuntu or Open VPN, this article may be not up-to-date, so you need to search for the answers in different places.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=355b41039d29" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to mount disks on Ubuntu]]></title>
            <link>https://medium.com/@afomalhaut/how-to-mount-disks-on-ubuntu-75bf6be28ded?source=rss-a8aa7d110f14------2</link>
            <guid isPermaLink="false">https://medium.com/p/75bf6be28ded</guid>
            <dc:creator><![CDATA[Alexander Fomalhaut]]></dc:creator>
            <pubDate>Tue, 05 Nov 2024 11:14:26 GMT</pubDate>
            <atom:updated>2025-06-02T17:09:34.434Z</atom:updated>
            <content:encoded><![CDATA[<p>If your computer has multiple drives, often Ubuntu does not mount them properly in some cases. In my life, I often have Windows and Linux installed together and also there are additional drives I want to access from the both systems. So in Ubuntu I almost always have the issue to access the disks, so I have to fix it manually. In this article I will show you how.</p><p>The main point of the fix is to configure /etc/fstab so the disks will be mounted to certain directories.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6R8Yo_M5NZR24wLPY0Qg_w.png" /></figure><h4>Step 1. View the drives</h4><p>Run the command to view all the drives seen by the operating system: sudo blkid.</p><p>This is my output:</p><pre>/dev/nvme0n1p2: UUID=&quot;7071ed56-f921-4459-9b40-52190eed538c&quot; BLOCK_SIZE=&quot;4096&quot; TYPE=&quot;ext4&quot; PARTUUID=&quot;d9b637e7-7ae3-4f30-9d83-cae505123d17&quot;<br>/dev/loop1: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/nvme0n1p1: UUID=&quot;692B-0A9B&quot; BLOCK_SIZE=&quot;512&quot; TYPE=&quot;vfat&quot; PARTLABEL=&quot;EFI System Partition&quot; PARTUUID=&quot;fd87e3eb-5326-4c0b-994c-2578af88948c&quot;<br>/dev/nvme0n1p4: BLOCK_SIZE=&quot;512&quot; UUID=&quot;68C26D9DC26D706C&quot; TYPE=&quot;ntfs&quot; PARTLABEL=&quot;Basic data partition&quot; PARTUUID=&quot;c19c0d96-a1c3-40bc-aa6c-0cf885b1f79c&quot;<br>/dev/nvme3n1p2: LABEL=&quot;Aorus3&quot; BLOCK_SIZE=&quot;512&quot; UUID=&quot;F68CC1028CC0BE7D&quot; TYPE=&quot;ntfs&quot; PARTLABEL=&quot;Basic data partition&quot; PARTUUID=&quot;547472c3-6209-4ff0-bdc3-bab57782c952&quot;<br>/dev/loop8: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/loop15: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/loop6: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/loop13: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/loop4: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/loop11: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/nvme2n1p1: LABEL=&quot;Aorus2&quot; BLOCK_SIZE=&quot;512&quot; UUID=&quot;84E2B14FE2B145E8&quot; TYPE=&quot;ntfs&quot; PARTUUID=&quot;a59d90df-01&quot;<br>/dev/loop2: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/loop0: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/loop9: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/loop16: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/loop7: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/nvme1n1p1: LABEL=&quot;Aorus1&quot; BLOCK_SIZE=&quot;512&quot; UUID=&quot;C66AA2AB6AA297A7&quot; TYPE=&quot;ntfs&quot;<br>/dev/sda2: LABEL=&quot;Data&quot; BLOCK_SIZE=&quot;512&quot; UUID=&quot;A8A0A1C8A0A19CF6&quot; TYPE=&quot;ntfs&quot; PARTLABEL=&quot;Basic data partition&quot; PARTUUID=&quot;be006d23-4421-49b4-9255-eadbed2c069c&quot;<br>/dev/loop14: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/loop5: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/nvme4n1p2: LABEL=&quot;Aorus4&quot; BLOCK_SIZE=&quot;512&quot; UUID=&quot;CA04D2EB04D2D991&quot; TYPE=&quot;ntfs&quot; PARTLABEL=&quot;Basic data partition&quot; PARTUUID=&quot;ba2c9c5b-aa91-4720-8ef7-8130e414f90d&quot;<br>/dev/loop12: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/loop3: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;<br>/dev/loop10: BLOCK_SIZE=&quot;131072&quot; TYPE=&quot;squashfs&quot;</pre><p>Since we are interested in disks, we should ignore the loop devices. The other ones have UUID and TYPE that are required below. In my case, I need to mount sda2, nvme0n1p2, nvme0n1p4, nvme1n1p1, nvme2n1p1, nvme3n1p2, nvme4n1p2. If you doubt what drives to choose, you can read the additional information about them (like size, model, etc) by the command sudo fdisk -x.</p><h4>Step 2. Create the directories for mount</h4><p>You can choose any directories you like. I personally used to mount everything in /media. So to create all the directories I run:</p><pre>cd /media<br>sudo mkdir Win<br>sudo mkdir Data<br>sudo mkdir Aorus1<br>sudo mkdir Aorus2<br>sudo mkdir Aorus3<br>sudo mkdir Aorus4</pre><h4>Step 3. Configure /etc/fstab</h4><p>So now we are to bind the created directories and the disks we chose. This configuration is stored in the file /etc/fstab, so we need to update it (with the root privileges). I use nano for this purpose: sudo nano /etc/fstab.</p><p>The structure of the record in the line is following: Identifier (UUID, for example), Directory (to mount), Type (NTFS, EXT4 or other), Options (usually set to defaults), Dump (usually set to 0), Pass (usually set to 0). More information is available by man fstab.</p><p>So in my case I just add the following lines:</p><pre>UUID=68C26D9DC26D706C /media/Win ntfs3 defaults 0 0<br>UUID=A8A0A1C8A0A19CF6 /media/Data ntfs-3g defaults 0 0<br><br>UUID=C66AA2AB6AA297A7 /media/Aorus1 ntfs3 defaults 0 0<br>UUID=84E2B14FE2B145E8 /media/Aorus2 ntfs3 defaults 0 0<br>UUID=F68CC1028CC0BE7D /media/Aorus3 ntfs3 defaults 0 0<br>UUID=CA04D2EB04D2D991 /media/Aorus4 ntfs3 defaults 0 0</pre><p>After that it is necessary to restart the computer.</p><h4>Issues in /etc/fstab</h4><ol><li>If you mount ntfs sometimes it is necessary to set the type ntfs3 or ntfs-3g, if you don’t want to have read-only access to the files.</li><li>In case of exfat, instead of just defaults you should add umask=0000 (so the options line should be defaults,umask=0000) so you can modify the files and folders.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75bf6be28ded" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Something about binary search]]></title>
            <link>https://medium.com/@afomalhaut/something-about-binary-search-8bac9dca5edf?source=rss-a8aa7d110f14------2</link>
            <guid isPermaLink="false">https://medium.com/p/8bac9dca5edf</guid>
            <dc:creator><![CDATA[Alexander Fomalhaut]]></dc:creator>
            <pubDate>Sat, 11 May 2024 12:51:09 GMT</pubDate>
            <atom:updated>2025-06-02T17:11:13.832Z</atom:updated>
            <content:encoded><![CDATA[<p>It looks like what can be easier than the binary search? This is one of the most popular algorithm in programming, that is often asked on the interviews. There are a lot of literatute about it. Nevertheless, I would like to share my own experience in this direction, giving some little known conclusions, that make the life easier in some practices.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KAW2B08AsMv0KUW0DPYRnA.png" /></figure><h4>What the theory tells</h4><p>It is known, that the binary search is an algorithm to find a certain element in a sorted array using the dichotomy method. This means, first, we take the middle of the array and compare it with the given element. After that, we continue the same approach for the left or the right half depending on the comparison result. We repeat these steps again and again until the size of the halves is 1. Since each step making the number of considering elements two times smaller, the total number of the steps is log N (where N is the initial size of the array), so this is the complexity of the algorithm. <a href="https://en.wikipedia.org/wiki/Binary_search_algorithm">https://en.wikipedia.org/wiki/Binary_search_algorithm</a></p><h4>Why we need the generalization</h4><p>In practice the task above is too narrow because of the following reasons:</p><p>1. It is not clear, what is the result if there is no searching element in the array.<br>2. It is not clear, what is the result if there are several searching elements in the array.<br>3. It is not clear, what is the result if we are searching for a place to insert the element keeping the order.<br>4. In some tasks we search for a so called appropriate element instead of exact one (for example, if we search for an IP range on a sorted list of IP ranges by a single IP as input).</p><p>In the literature, for any certain case above they suggest to modify the algorithm itself, keeping the same dichotomy approach, but using some changes in the comparison and index edge effects. It is almost impossible to remember and keep it in mind all the time. And if you try to implement it by your own, often there are errors in special cases, so you will need proper tests all the time. This is why it may be a good idea to separate the algorithm itself, implementing it one time, and ways to use it in any particular case flexibly.</p><p>I suggest rephrasing the initial task. Instead of an array sized N, we consider a function that by given index from 0 to N returns a boolean value (trueor false), it is also required this function to be monotonic, such that for any indices i&lt;j there must be f(i)≤f(j) (in other words, after the series of false values the series of true goes). In the case of searching for an array’s element, the function must be f(i)=arr[i]≥elem, where arr is the array, elem is the element to search. The fact that the array is sorted ensures the function to be monotonic.</p><p>From this rephrasing, the binary search task is actually searching for the minimum index for which the function returnstrue. And if such index does not exist, the algorithm should return the total number of elements. In other words, this is a search of the border between false and true for the function.</p><h4>Implementation in Rust</h4><pre>/// Function `binary_search` returns the smallest index, for which the passed<br>/// function `cond` (`usize` -&gt; `bool`) returns `true`, or `size` if `cond` <br>/// never returns `true`. `cond` must be a monotonic function<br>/// such that for any `i &lt; j`: `cond(i) &lt;= cond(j)`.<br>pub fn binary_search&lt;F: Fn(usize) -&gt; bool&gt;(size: usize, cond: F) -&gt; usize {<br>    let mut d = 0;<br>    let mut u = size;<br><br>    while u - d &gt; 1 {<br>        let m = (u + d) &gt;&gt; 1;<br>        if cond(m) {<br>            u = m;<br>        } else {<br>            d = m;<br>        }<br>    }<br><br>    if d == 0 {<br>        if cond(0) {<br>            0<br>        } else {<br>            1<br>        }<br>    }<br>    else {<br>        u<br>    }<br>}</pre><h4>Particular case: array element search</h4><pre>pub fn array_binary_search&lt;T: PartialOrd&gt;(arr: &amp;[T], elem: &amp;T) -&gt; usize {<br>    binary_search(arr.len(), |i| arr[i] &gt;= *elem)<br>}</pre><p>This function returns the minimum index of the element such that it is more or equal to the givenelem. So we can use the same fuction to search for a first element equal to elem or an index to insert keeping the order. In the case of insert, this is the index of the first element in the series of equal elements. If we need to insert after the last element in the series, obviously we should use this implementation:</p><pre>pub fn array_binary_search_insert_last&lt;T: PartialOrd&gt;(arr: &amp;[T], elem: &amp;T) -&gt; usize {<br>    binary_search(arr.len(), |i| arr[i] &gt; *elem)<br>}</pre><h4>Real world example: search for a geo location by IP from the file</h4><p>As a real world example, we consider the search for a geo location by IP. In the net there are many free databases in which for any IP range there is a certain location (a database example <a href="https://db-ip.com/db/lite.php">https://db-ip.com/db/lite.php</a>). The IP value itself can be obviously encoded and a 32-bit unsigned integer (the type uint32). If the IP ranges are sorted and saved in an array ip_list, the following function implements the necessary binary search:</p><pre>pub fn ip_binary_search(ip_list: &amp;[(u4, u4)], ip: u4) -&gt; usize {<br>    binary_search(ip_list.len(), |i| ip_list[i].1 &gt; *elem)<br>}</pre><p>As we see in the cases above, it is not complicated to make out what function suits the task, avoiding the most of errors.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8bac9dca5edf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to compile DLL in Rust and import it in Python, useful hints]]></title>
            <link>https://medium.com/@afomalhaut/how-to-compile-dll-in-rust-and-import-it-in-python-useful-hints-466be4e7475b?source=rss-a8aa7d110f14------2</link>
            <guid isPermaLink="false">https://medium.com/p/466be4e7475b</guid>
            <dc:creator><![CDATA[Alexander Fomalhaut]]></dc:creator>
            <pubDate>Thu, 20 Jul 2023 16:26:29 GMT</pubDate>
            <atom:updated>2026-03-11T09:49:04.973Z</atom:updated>
            <content:encoded><![CDATA[<p>Here I am providing a number of examples to compile DLL (<a href="https://en.wikipedia.org/wiki/Dynamic-link_library">https://en.wikipedia.org/wiki/Dynamic-link_library</a>) in Rust. In order to check the final DLL I am using Python, although DLL standard is compatible with almost all the widely known programming languages. The examples include plain functions, input and output arrays, structures and OOP-like approach. I also attached a benchmark. In the end, I am describing a way to create a Python package powered by Rust, that is ready to distribute on PyPI (<a href="https://pypi.org/">https://pypi.org/</a>). Source code of the project is available on <a href="https://github.com/fomalhaut88/rust-dll-example">https://github.com/fomalhaut88/rust-dll-example</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MOopaMdCUborBgkLxpJeiw.png" /></figure><p><strong>How to create a DLL project in Rust</strong></p><ol><li>Create a project as a library (using the flag --lib):</li></ol><pre>cargo new rust-dll-example --lib</pre><p>2. Add the following section in Cargo.toml to specify the type of the library:</p><pre>[lib]<br>crate-type = [&quot;cdylib&quot;]</pre><p>3.Add #[unsafe(no_mangle)] and extern &quot;C&quot; to the exporting functions like this:</p><pre>#[unsafe(no_mangle)]<br>pub extern &quot;C&quot; fn add(left: usize, right: usize) -&gt; usize {<br>    left + right<br>}</pre><p>4. Build the project: cargo build --release</p><p>After that the DLL file will appear by path ./target/release/rust_dll_example.dll if you are using Windows or something similar on different platforms (usually .so for Linux and .dylib for Mac OS X).</p><p>It is a good practice to cover your functions with standard Rust tests and benchmarks, so you can control the correctness and the performance.</p><p><strong>Plain functions</strong></p><p>Here are two plain functions written in Rust:</p><pre>/// An addition of two unsigned integer numbers.<br>#[unsafe(no_mangle)]<br>pub extern &quot;C&quot; fn add(left: usize, right: usize) -&gt; usize {<br>    left + right<br>}<br><br><br>/// Square of a float value.<br>#[unsafe(no_mangle)]<br>pub extern &quot;C&quot; fn sqr(x: f64) -&gt; f64 {<br>    x * x<br>}</pre><p>There are two differences to normal functions: the keyword extern so the function is linked to DLL interface and the attribuue no_mangle that is needed to disable the standard name encoding on the compilation stage (read more about it here: <a href="https://doc.rust-lang.org/reference/abi.html#the-no_mangle-attribute">https://doc.rust-lang.org/reference/abi.html#the-no_mangle-attribute</a>).</p><p>To call this functions in Python, there is a following code:</p><pre>import ctypes<br><br>dll = ctypes.CDLL(&quot;./target/release/rust_dll_example.dll&quot;)<br><br># Test add<br>assert dll.add(2, 3) == 5<br><br># Test sqr<br>dll.sqr.restype = ctypes.c_double<br>dll.sqr.argtypes = [ctypes.c_double]<br>assert dll.sqr(6.0) == 36.0</pre><p>Notice, that for sqr function we had to specify input and output types explicitly before the call, so Python knows how to interpret the data. It is recommended to avoid skipping types specification as it is done for add function, despite of usually integer data types are set by default. Do you remember the phrase Explicit is better than implicit. from The Zen of Python? (The full text of it can always be outputed by import this)</p><p><strong>Passing arrays</strong></p><p>There are two ways to handle arrays: by reference (norammly for fixed size arrays) and by pointer (it the size is unknown or mutable from call to call), and both a supported in DLLs.</p><pre>/// Sum of elements of an array given by pointer and the size.<br>#[unsafe(no_mangle)]<br>pub extern &quot;C&quot; fn array_sum(size: usize, arr: *const f64) -&gt; f64 {<br>    let mut res = 0.0;<br>    for idx in 0..size {<br>        unsafe {<br>            res += *arr.offset(idx as isize);<br>        }<br>    }<br>    res<br>}<br><br><br>/// Fill the given array with a float value.<br>#[unsafe(no_mangle)]<br>pub extern &quot;C&quot; fn array_set(size: usize, arr: *mut f64, val: f64) {<br>    for idx in 0..size {<br>        unsafe {<br>            *arr.offset(idx as isize) = val;<br>        }<br>    }<br>}<br><br><br>/// Set elements of a fixed size array to zeros.<br>#[unsafe(no_mangle)]<br>pub extern &quot;C&quot; fn array3_zero(arr: &amp;mut [f64; 3]) {<br>    for idx in 0..arr.len() {<br>        arr[idx] = 0.0;<br>    }<br>}<br><br><br>/// Return fixed size array filled with the given value.<br>#[unsafe(no_mangle)]<br>pub extern &quot;C&quot; fn array5_fill(val: f64) -&gt; Box&lt;[f64; 5]&gt; {<br>    Box::new([val; 5])<br>}</pre><p>Accessing to the elements of an array passed by pointer is an unsafe operation for Rust, so it is reflected by the unsafe sections. If we return the array as the result, we can do it by reference (as it is in array_concat) or as a boxed fixed size array.</p><p>In order by call these functions from Python, we can use following commands:</p><pre># Create array<br>arr_type = (ctypes.c_double * 5)<br>arr = arr_type(*[1.0, 2.0, 3.0, 4.0, 5.0])<br><br># Test array_sum<br>dll.array_sum.restype = ctypes.c_double<br>dll.sqr.argtypes = [ctypes.c_uint64, arr_type]<br>assert dll.array_sum(5, arr) == 15.0<br><br># Test array_set<br>dll.array_set.argtypes = [ctypes.c_uint64, arr_type, ctypes.c_double]<br>dll.array_set(5, arr, ctypes.c_double(3.0))<br>assert list(arr) == [3.0] * 5<br><br># Test array3_zero<br>dll.array3_zero(arr)<br>assert list(arr) == [0.0, 0.0, 0.0, 3.0, 3.0]<br><br># Test array5_fill<br>dll.array5_fill.argtypes = [ctypes.c_double]<br>dll.array5_fill.restype = ctypes.POINTER(ctypes.c_double * 5)<br>arr = dll.array5_fill(2.5)<br>assert list(arr.contents) == [2.5] * 5</pre><p>What is interesting there? First of all, we define array data type as a product of the type of element and the size. And after that we must convert Python list to the understandable by Rust format, because Python lists are totally not the same as C-compatible arrays. Also there is no difference from Python side regarding passing by reference or by pointer.</p><p>In two last functions we return arrays as the results: as a pointer and as a fixed size array. In both cases, in Python we should set restype to ctypes.POINTER, so after that we can extract the values from .contents attribute.</p><p>Working with strings (or bytes) is similar, because string is represented as an array of chars (or uint8), so on the Rust side they should have the types *u8 or [u8; SIZE], and on the Python side it is ctypes.c_char * SIZE.</p><p><strong>C-compatible structures</strong></p><p>We also are allowed to work with complex data types like structures as with base types, but there are some additions. Let us look at the Rust code that implements a few functions for complex numbers:</p><pre>#[derive(Debug, PartialEq)]<br>#[repr(C)]<br>pub struct Complex {<br>    pub x: f64,<br>    pub y: f64,<br>}<br><br><br>#[unsafe(no_mangle)]<br>pub extern &quot;C&quot; fn complex_len(z: Complex) -&gt; f64 {<br>    (z.x * z.x + z.y * z.y).sqrt()<br>}<br><br><br>#[unsafe(no_mangle)]<br>pub extern &quot;C&quot; fn complex_conj(z: Complex) -&gt; Complex {<br>    Complex {<br>        x: z.x,<br>        y: -z.y,<br>    }<br>}<br><br>impl Complex {<br>    #[unsafe(export_name=&quot;complex_real&quot;)]<br>    pub extern &quot;C&quot; fn real(&amp;self) -&gt; f64 {<br>        self.x<br>    }<br><br>    #[unsafe(export_name=&quot;complex_image&quot;)]<br>    pub extern &quot;C&quot; fn image(&amp;self) -&gt; f64 {<br>        self.y<br>    }<br><br>    #[unsafe(export_name=&quot;complex_mul&quot;)]<br>    pub extern &quot;C&quot; fn mul(&amp;mut self, val: f64) {<br>        self.x *= val;<br>        self.y *= val;<br>    }<br>}</pre><p>We can see already familiar functions with extern and no_mangle keywords and passing variables is done as it would be for standard data types. I also added an OOP-like part (impl section), so the functions inside can be considered as methods of the structure. They also have extern and no_mangle but there is also export_name attribute that customizes the name of the linked function to reach it outside.</p><p>Notice, that the structure has the attribute repr(C) that is important, otherwise external programs barely can understand the way the data is stored in a structure instance. This is because there are several standards to manage inner data using different alignment algorithms (you can read more about it in <a href="https://en.wikipedia.org/wiki/Data_structure_alignment">https://en.wikipedia.org/wiki/Data_structure_alignment</a>).</p><p>As for Python code:</p><pre># Complex struct<br>class Complex(ctypes.Structure):<br>    _fields_ = [<br>        (&#39;x&#39;, ctypes.c_double),<br>        (&#39;y&#39;, ctypes.c_double),<br>    ]<br><br>    def __repr__(self):<br>        return f&quot;Complex(x={self.x}, y={self.y})&quot;<br><br>    def __eq__(self, other):<br>        return self.x == other.x and self.y == other.y<br><br># Test complex_len<br>dll.complex_len.argtypes = [Complex]<br>dll.complex_len.restype = ctypes.c_double<br>assert dll.complex_len(z) == 5.0<br><br># Test complex_conj<br>dll.complex_conj.argtypes = [Complex]<br>dll.complex_conj.restype = Complex<br>assert dll.complex_conj(z) == Complex(x=3.0, y=4.0)<br><br># Test real<br>dll.complex_real.argtypes = [ctypes.c_void_p]<br>dll.complex_real.restype = ctypes.c_double<br>assert dll.complex_real(ctypes.byref(z)) == 3.0<br><br># Test image<br>dll.complex_image.argtypes = [ctypes.c_void_p]<br>dll.complex_image.restype = ctypes.c_double<br>assert dll.complex_image(ctypes.byref(z)) == -4.0<br><br># Test mul<br>dll.complex_mul.argtypes = [ctypes.c_void_p, ctypes.c_double]<br>dll.complex_mul(ctypes.byref(z), 2.0)<br>assert z == Complex(x=6.0, y=-8.0)</pre><p>Fortunately, ctypes supports the opportunity to define structure data type in a very friendly way. So after that we can work with Complex as with an ordinary data type, passing it to argtypes and restype attributes. But there is a peculiarity if the structure instance is supposed to be mutable: in this case we have to add ctypes.byref as it is done in complex_mul function. The argument type is set to ctypes.c_void_p instead of Complex.</p><p>This approach is good but it does not cover all the needs. The main lack is that only C-compatible data types are allowed in structure fields. If we are not going to share inner data of the structure outside there is a different approach shown in the next section.</p><p><strong>OOP example</strong></p><p>In spite of the previous example, here we can use any data types in our structure we want. But we cannot access the fields outside, though we usually do not need it if we require OOP that supposes encapsulation.</p><pre>struct Counter {<br>    val: usize,<br>}<br><br><br>impl Counter {<br>    #[unsafe(export_name=&quot;counter_new&quot;)]<br>    pub extern &quot;C&quot; fn new() -&gt; Box&lt;Self&gt; {<br>        Box::new(Self {<br>            val: 0,<br>        })<br>    }<br><br>    #[unsafe(export_name=&quot;counter_get&quot;)]<br>    pub extern &quot;C&quot; fn get(&amp;self) -&gt; usize {<br>        self.val<br>    }<br><br>    #[unsafe(export_name=&quot;counter_increment&quot;)]<br>    pub extern &quot;C&quot; fn increment(&amp;mut self) {<br>        self.val += 1;<br>    }<br>}</pre><p>There is no repr(C) attribute and new returns boxed instance because we need to allocate the instance in the heap. The other methods are implemented similar to what we had for Complex structure.</p><pre>dll.counter_new.restype = ctypes.c_void_p<br>dll.counter_get.argtypes = [ctypes.c_void_p]<br>dll.counter_increment.argtypes = [ctypes.c_void_p]<br><br>class Counter:<br>    _dll = dll<br><br>    def __init__(self):<br>        self._counter = self._dll.counter_new()<br><br>    def get(self):<br>        return self._dll.counter_get(self._counter)<br><br>    def increment(self):<br>        self._dll.counter_increment(self._counter)<br><br># Create an instance<br>counter = Counter()<br><br># Get value<br>assert counter.get() == 0<br><br># Increment value<br>counter.increment()<br><br># Get value<br>assert counter.get() == 1</pre><p>On the Python level, we can define Counter as a class with the same methods, a class object keeps the Rust counter instance inside. Notice, that self._counter is a pointer that has the type ctypes.c_void_p.</p><p><strong>Benchmark example</strong></p><p>Usually, developers combine Python and a low-level programming language to improve the performance. This is the most frequent reason why DLL is needed. So this article will not be complete if I do not include a real world case with a benchmark. For this purpose I implemented a sort of Levenshtein distance (<a href="https://en.wikipedia.org/wiki/Levenshtein_distance">https://en.wikipedia.org/wiki/Levenshtein_distance</a>) algorithm. I did not much care about the performance on Rust level, so it may work slower than the solutions you can find in the net, although the performance will be surely comparable.</p><pre>use std::{cmp, slice};<br><br><br>/// Levenshtain distance algorithm<br>#[unsafe(export_name=&quot;levenshtein_distance&quot;)]<br>pub extern &quot;C&quot; fn distance(n1: usize, p1: *const u8, <br>                       n2: usize, p2: *const u8) -&gt; usize {<br>    let s1 = unsafe { slice::from_raw_parts(p1, n1) };<br>    let s2 = unsafe { slice::from_raw_parts(p2, n2) };<br>    let lcs = lcs_solve(s1, s2);<br>    cmp::max(n1, n2) - lcs<br>}<br><br><br>/// Calculate Longest Common Subsequence length<br>pub fn lcs_solve&lt;T: PartialEq&gt;(s1: &amp;[T], s2: &amp;[T]) -&gt; usize {<br>    let n1 = s1.len();<br>    let n2 = s2.len();<br><br>    let mut row = vec![0usize; n1];<br><br>    for i2 in 0..n2 {<br>        let mut prev = 0;<br><br>        for i1 in 0..n1 {<br>            let prev_new = row[i1];<br><br>            if s1[i1] == s2[i2] {<br>                row[i1] = prev + 1;<br>            } else if i1 &gt; 0 {<br>                if row[i1] &lt; row[i1 - 1] {<br>                    row[i1] = row[i1 - 1];<br>                }<br>            }<br><br>            prev = prev_new;<br>        }<br>    }<br><br>    row[n1 - 1]<br>}</pre><p>Also I attached Rust benchmark:</p><pre>#[cfg(test)]<br>mod tests {<br>    use super::*;<br>    use test::Bencher;<br><br>    #[bench]<br>    fn bench_distance(bencher: &amp;mut Bencher) {<br>        let s1 = &quot;lewenstein&quot;;<br>        let s2 = &quot;levenshtein&quot;;<br><br>        bencher.iter(|| {<br>            distance(s1.len(), s1.as_ptr(), <br>                     s2.len(), s2.as_ptr());<br>        });<br>    }<br>}</pre><p>Before run the benchmark do not forget to switch to the Nightly toolchain (<a href="https://doc.rust-lang.org/book/appendix-07-nightly-rust.html">https://doc.rust-lang.org/book/appendix-07-nightly-rust.html</a>):</p><pre>rustup default nightly</pre><p>On Python level we have following script (file rust_dll_example/__init__.py):</p><pre>import ctypes<br><br><br>_dll = ctypes.CDLL(&quot;./target/release/rust_dll_example.dll&quot;)<br><br>_dll.levenshtein_distance.restype = ctypes.c_uint64<br>_dll.levenshtein_distance.argtypes = [<br>    ctypes.c_uint64, ctypes.c_char_p, ctypes.c_uint64, ctypes.c_char_p<br>]<br><br><br>def levenshtein_distance(s1, s2):<br>    b1 = s1.encode()<br>    b2 = s2.encode()<br>    return _dll.levenshtein_distance(len(b1), b1, len(b2), b2)</pre><p>If you want to benchmark the result on Python level, you can always do it with the help of <strong>timeit</strong> (<a href="https://docs.python.org/3/library/timeit.html">https://docs.python.org/3/library/timeit.html</a>) with the command:</p><pre>python -m timeit -s &#39;from rust_dll_example import levenshtein_distance&#39; &#39;levenshtein_distance(\&quot;lewenstein\&quot;, \&quot;levenshtein\&quot;)&#39;</pre><p><strong>How to prepare a Python package</strong></p><p>If we are going to distribute the Python library with compiled DLL inside, first, it is necessary to prepare setup.py having some non-standard lines. Here is an example:</p><pre>import os<br>import json<br>import subprocess as sp<br><br>from setuptools import find_packages, setup<br><br><br>def build_src():<br>    sp.Popen([&quot;cargo&quot;, &quot;build&quot;, &quot;--release&quot;]).communicate()<br><br><br>def get_version():<br>    if os.path.exists(&#39;version&#39;):<br>        with open(&#39;version&#39;) as f:<br>            return f.read()<br><br>    else:<br>        out, _ = sp.Popen([&quot;cargo&quot;, &quot;metadata&quot;], stdout=sp.PIPE).communicate()<br>        metadata = json.loads(out.decode())<br>        version = metadata[&#39;packages&#39;][0][&#39;version&#39;]<br><br>        with open(&#39;version&#39;, &#39;w&#39;) as f:<br>            f.write(version)<br><br>        return version<br><br><br>def get_long_description():<br>    with open(&#39;README.md&#39;) as f:<br>        return f.read()<br><br><br>def get_dll_paths():<br>    return [<br>        &#39;./target/release/rust_dll_example.dll&#39;,<br>    ]<br><br><br># Build from source<br>build_src()<br><br><br># Setup<br>setup(<br>    name=&#39;rust-dll-example&#39;,<br>    version=get_version(),<br>    packages=find_packages(),<br>    license=&quot;MIT&quot;,<br>    description=&quot;&quot;,<br>    long_description=get_long_description(),<br>    long_description_content_type=&quot;text/markdown&quot;,<br>    install_requires=[],<br>    data_files=[(&#39;dlls&#39;, get_dll_paths()), (&#39;&#39;, [&#39;version&#39;])],<br>)</pre><p>Here we specified the path to DLL file as a data file for our package. Obviously, the old path ./target/release/rust_dll_example.dll will not work. So we need to make some changes in __init__.py to import DLL correctly:</p><pre>...<br>_dll_path = os.path.join(sys.prefix, &#39;dlls&#39;, &#39;rust_dll_example.dll&#39;)<br>_dll = ctypes.CDLL(_dll_path)<br>...</pre><p>Once setup.py is created and the path to DLL is corrected, we can build our Python package:</p><pre>python setup.py sdist</pre><p>After that the file dist/rust-dll-example-0.1.0.tar.gz will appear. It can be installed with pip and distributed on PyPI (<a href="https://pypi.org/">https://pypi.org/</a>). In order to upload it on PyPI, run the following command:</p><pre>twine upload dist/rust-dll-example-0.1.1.tar.gz</pre><p>After that the project will be available on <a href="https://pypi.org/project/rust-dll-example/">https://pypi.org/project/rust-dll-example/</a>.</p><p>Notice! As far as DLL is compiled under a single platform (Windows 11 in my case), the library can be successfully installed only for the users with the same platform. Otherwise, the attached DLL file cannot be executed correctly. If you would like your library to be crossplatform, you need to build several DLLs for each platform and implement a more tricky way to catch the right DLL on Python level.</p><h4>Import in Rust</h4><p>It is possible to import the considered DLL in Rust as well. See how to do it here: <a href="https://github.com/fomalhaut88/rust-dll-example/blob/master/rust-dll-test/src/main.rs">https://github.com/fomalhaut88/rust-dll-example/blob/master/rust-dll-test/src/main.rs</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=466be4e7475b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create a GUI and distribute a Python script]]></title>
            <link>https://medium.com/@afomalhaut/how-to-create-a-gui-and-distribute-a-python-script-b90255111ea3?source=rss-a8aa7d110f14------2</link>
            <guid isPermaLink="false">https://medium.com/p/b90255111ea3</guid>
            <dc:creator><![CDATA[Alexander Fomalhaut]]></dc:creator>
            <pubDate>Sat, 03 Jun 2023 15:43:57 GMT</pubDate>
            <atom:updated>2025-06-02T17:21:29.977Z</atom:updated>
            <content:encoded><![CDATA[<p>Did it happen, if somebody was interested in your Python script, you wanted to share, but you had no idea how to do it? Because it requires Python installed for the user with all the necessary dependencies. Additionally, the black console window of the application scares people who are far from programming. Fortunately, there are ways to build a simple GUI and pack the script into an executable file (EXE-file in case of Windows, for example). This is what I would like to share with you below. Having a certain example, step by step we will consider how to transform a raw Python console application into an executable file that is ready to distribute.</p><p>The repository with the files and configurations we are considering below is represented here: <a href="https://github.com/fomalhaut88/count-lines">https://github.com/fomalhaut88/count-lines</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FD0ziHOe9uO1ZDKnjKFtCQ.png" /></figure><h3>Step 1. Writing an application</h3><p>Let our program count the total number of lines for the files with specified extensions and located in a given folder. This can be helpful, for example, to estimate the quantity of code for different programming languages in a project. It can be easily developed, here is an implementation in Python (file <em>count_lines_script.py</em>):</p><pre>import os<br><br><br>def _check_ext(name, ext_list):<br>    if ext_list:<br>        # The given name ends with any of the extensions in the given list<br>        return any(name.endswith(&#39;.&#39; + ext) for ext in ext_list)<br>    else:<br>        # If no list of extensions provided we return True<br>        return True<br><br><br>def _get_lines_count(path):<br>    try:<br>        with open(path) as f:<br>            # This for-loop iterates lines as a generator,<br>            # so it is an efficient way to count the total number of lines<br>            return sum(1 for _ in f)<br>    except UnicodeDecodeError:<br>        # We return 0 if the file can&#39;t be opened normally as a text<br>        return 0<br><br><br>def count_lines(folder, ext_list=None, recursive=False):<br>    # Result variable<br>    result = 0<br><br>    # Loop for files and directories inside of the given folder<br>    for name in os.listdir(folder):<br>        # Path to the file<br>        path = os.path.join(folder, name)<br><br>        # If item is a file check its extension and increase the result<br>        if os.path.isfile(path):<br>            if _check_ext(name, ext_list):<br>                result += _get_lines_count(path)<br><br>        # Else if item is a directory and recursive flag is set we count the number<br>        # of lines recursively and add it to the result<br>        elif recursive and os.path.isdir(path):<br>            result += count_lines(path, ext_list=ext_list, recursive=recursive)<br><br>    # Return the result<br>    return result<br><br><br>if __name__ == &quot;__main__&quot;:<br>    result = count_lines(r&#39;C:\Users\alexander\Development\gui-args-framework&#39;, <br>                         ext_list=[&#39;py&#39;], recursive=True)<br>    print(result)</pre><h3>Step 2. Build a GUI</h3><p>The disadvantage of the script above is the need to modify it every time when we need to run it for a different folder, different extensions and other options. Therefore it is time to think about a convenient GUI. And in this case <a href="https://pypi.org/project/gui-args-framework/">gui-args-framework</a> may help, that creates a window of the application with all the necessary interactive elements by several modifications.</p><p>To install <em>gui-args-framework</em> run:</p><pre>pip install gui-args-framework</pre><p>The changes that we need to make in our script (file <em>count_lines_gui.py</em>):</p><pre>...<br><br>from gui_args_framework.args_window import ArgsWindow<br>from gui_args_framework.fields import DirectoryField, StringField, BooleanField<br><br>...<br><br>class TestWindow(ArgsWindow):<br>    title = &quot;Count lines&quot;<br>    args = [<br>        DirectoryField(name=&#39;folder&#39;, label=&#39;Path to folder&#39;),<br>        StringField(name=&#39;ext_list&#39;, <br>                    label=&#39;Extensions separated by | (example: txt|py)&#39;,<br>                    required=False),<br>        BooleanField(name=&#39;recursive&#39;, label=&#39;Recursive&#39;, default=True),<br>    ]<br>    description = &quot;This program calculates total number of lines &quot; \<br>                  &quot;of the files in the given folder.&quot;<br><br>    def main(self, this):<br>        result = count_lines(<br>            this[&#39;folder&#39;],<br>            ext_list=this[&#39;ext_list&#39;].split(&#39;|&#39;),<br>            recursive=this[&#39;recursive&#39;],<br>        )<br>        this.message(f&quot;Total number of lines: {result}&quot;)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    TestWindow.run()</pre><p>As we can see in the example, it was enough to write down some parameters for the window and bind the function <em>count_lines</em> to the handler <em>main</em>, that is run in the GUI. Full documentation of <a href="https://github.com/fomalhaut88/gui-args-framework">gui-args-framework</a> is available by the link.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*Oo_5ZP0u15I5j9w2fhFBvQ.png" /></figure><h3>Step 3. Compile into an executable file</h3><p>In order to compile the project into a single executable file, we can use <a href="https://pypi.org/project/pyinstaller/">Pyinstaller</a>. It can be installed by following command:</p><pre>pip install pyinstaller</pre><p>After that we need a configuration <em>count-lines.spec</em>:</p><pre># -*- mode: python -*-<br><br>block_cipher = None<br><br>datas = []<br>a = Analysis([&#39;count_lines_gui.py&#39;],<br>             pathex=[],<br>             binaries=None,<br>             datas=datas,<br>             hiddenimports=[],<br>             hookspath=[],<br>             runtime_hooks=[],<br>             excludes=[],<br>             win_no_prefer_redirects=False,<br>             win_private_assemblies=False,<br>             cipher=block_cipher)<br>pyz = PYZ(a.pure, a.zipped_data,<br>          cipher=block_cipher)<br>exe = EXE(pyz,<br>          a.scripts,<br>          a.binaries,<br>          a.zipfiles,<br>          a.datas,<br>          name=&#39;count-lines&#39;,<br>          debug=False,<br>          strip=False,<br>          upx=True,<br>          console=False)</pre><p>To compile we run:</p><pre>pyinstaller count-lines.spec</pre><p>Once it is done, the executable file will appear in the folder <em>dist</em>. Actually, it is enough for distribution. Although, in case of Windows, we may need to make our program installable. And this is what <strong>Step 4</strong> is about.</p><h3>Step 4. Create a Windows installer</h3><p>To create a setup wizard we are using <a href="https://jrsoftware.org/isinfo.php">Inno Setup</a> (it can be downloaded and installed from the official website). And we also need the configuration <em>count-lines.iss</em>:</p><pre>; Script generated by the Inno Setup Script Wizard.<br>; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!<br><br>#define MyAppName &quot;Count Lines&quot;<br>#define MyAppVersion &quot;0.1.0&quot;<br>#define MyAppPublisher &quot;Your name&quot;<br>#define MyAppURL &quot;https://example.com/&quot;<br>#define MyAppSnakeName &quot;count-lines&quot;<br>#define MyAppExeName &quot;count-lines.exe&quot;<br>#define MyAppId &quot;F0A1B35B-41D5-4542-B9B3-413795B3447F&quot;<br><br>[Setup]<br>; NOTE: The value of AppId uniquely identifies this application.<br>; Do not use the same AppId value in installers for other applications.<br>; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)<br>AppId={{{#MyAppId}}<br>AppName={#MyAppName}<br>AppVersion={#MyAppVersion}<br>;AppVerName={#MyAppName} {#MyAppVersion}<br>AppPublisher={#MyAppPublisher}<br>AppPublisherURL={#MyAppURL}<br>AppSupportURL={#MyAppURL}<br>AppUpdatesURL={#MyAppURL}<br>DefaultDirName={pf}\{#MyAppName}<br>DefaultGroupName={#MyAppName}<br>OutputDir=.\dist<br>OutputBaseFilename={#MyAppSnakeName}64-setup-{#MyAppVersion}<br>; SetupIconFile=.\{#MyAppSnakeName}.ico<br>Compression=lzma<br>SolidCompression=yes<br><br>[Languages]<br>Name: &quot;english&quot;; MessagesFile: &quot;compiler:Default.isl&quot;<br><br>[Tasks]<br>Name: &quot;desktopicon&quot;; Description: &quot;{cm:CreateDesktopIcon}&quot;; GroupDescription: &quot;{cm:AdditionalIcons}&quot;; Flags: unchecked<br><br>[Files]<br>Source: &quot;.\dist\{#MyAppExeName}&quot;; DestDir: &quot;{app}&quot;; Flags: ignoreversion<br>; NOTE: Don&#39;t use &quot;Flags: ignoreversion&quot; on any shared system files<br><br>[Icons]<br>Name: &quot;{group}\{#MyAppName}&quot;; Filename: &quot;{app}\{#MyAppExeName}&quot;<br>Name: &quot;{commondesktop}\{#MyAppName}&quot;; Filename: &quot;{app}\{#MyAppExeName}&quot;; Tasks: desktopicon<br><br>[Run]<br>Filename: &quot;{app}\{#MyAppExeName}&quot;; Description: &quot;{cm:LaunchProgram,{#StringChange(MyAppName, &#39;&amp;&#39;, &#39;&amp;&amp;&#39;)}}&quot;; Flags: nowait postinstall skipifsilent</pre><p>For every new application it is necessary to edit first lines of the configuration (started with <em>#define</em>), so the application will have the correct name, version, etc. Pay attention at the option <em>MyAppId</em>, this is a unique ID, that is used by Windows to distinguish applications. It must be different for different applications and the same for different versions of one application. To generate a new value of <em>MyAppId</em> there are many ways. Here is how to do it with Python:</p><pre>python -c &quot;from uuid import uuid4; print(str(uuid4()).upper())&quot;</pre><p>Once the configuration file is ready, run this command to create a setup wizard:</p><pre>iscc count-lines.iss</pre><p>After that in the folder <em>dist</em> the installer <em>count-lines64-setup-0.1.0.exe</em> will appear. It can be uploaded to <a href="https://sourceforge.net/">Source Forge</a> or anywhere else.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b90255111ea3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[winrk — a wrk alternative for Windows users]]></title>
            <link>https://medium.com/@afomalhaut/winrk-a-wrk-alternative-for-windows-users-8c6aa6c6e23b?source=rss-a8aa7d110f14------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c6aa6c6e23b</guid>
            <dc:creator><![CDATA[Alexander Fomalhaut]]></dc:creator>
            <pubDate>Fri, 20 Jan 2023 19:56:32 GMT</pubDate>
            <atom:updated>2025-06-02T17:16:14.459Z</atom:updated>
            <content:encoded><![CDATA[<h3>winrk — a wrk alternative for Windows users</h3><p><strong>winrk</strong> is a load testing tool for Windows that provides a command line interface only. It works similar to a well known tool called <strong>wrk</strong> that is available for Unix users only. It is possible to specify the number of connections, number of threads and the duration. And as the result the program outputs RPS, latencies and other statistics, the examples you will find below in this article. Since winrk is powered by Rust including <a href="https://tokio.rs/">Tokio</a> and <a href="https://docs.rs/reqwest/latest/reqwest/">Reqwest</a> libraries, it has extremely high performance.</p><p>winrk is an open source project. The source code can be found here: <a href="https://github.com/fomalhaut88/winrk">https://github.com/fomalhaut88/winrk</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5zrzjQPQrNmgy8cEs2AjsQ.png" /></figure><h3>Basic example</h3><p>In order to load the URL <a href="https://example.com/">https://example.com</a> with GET requests during 5 seconds, having 200 simultaneous connections and using 4 CPU threads run the following command:</p><pre>winrk https://example.com -t 4 -d 5 -c 200</pre><p>The output will look like this:</p><pre>Input:<br>    url: https://example.com<br>    method: GET<br>    threads: 4<br>    duration: 5s<br>    connections: 200<br><br>Result:<br>    total: 3943 requests<br>    errors: 0 errors<br>    error percentage: 0.0%<br>    latency min: 209.2183ms<br>    latency median: 213.1443ms<br>    latency average: 249.390382ms<br>    latency max: 987.9829ms<br>    transfers: 4952408 bytes<br>    rps: 802.0 requests per sec</pre><h3>Installation</h3><ol><li>Find the installer on SourceForge: <a href="https://sourceforge.net/projects/winrk/files/latest/download">https://sourceforge.net/projects/winrk/files/latest/download</a></li><li>Run the downloaded file</li><li>Follow the wizard instructions</li><li>Restart your computer</li></ol><h3>How to make a POST request</h3><pre>winrk http://someurl.com -t 4 -d 5 -c 200 -m POST -H &#39;Content-Type: application/json&#39; -H &#39;X-token: Aij5lahcheoThei6ahgh7laeHoh9uShe&#39; -T 10 -D &#39;{&quot;x&quot;: 42}&#39;</pre><p>In this example we specified the HTTP method POST, two headers: Content-Type: application/json and X-token: Aij5lahcheoThei6ahgh7laeHoh9uShe, the maximum duration as 10 seconds and the payload {&quot;x&quot;: 42}.</p><h3>Arguments</h3><pre>    --help (-h)        - print this help.<br>    --version (-v)     - print the version.<br>    --duration (-d)    - duration of the test in seconds (default 10).<br>    --connections (-c) - number of parallel connections (default 100).<br>    --threads (-t)     - number of CPU workers (default 1).<br>    --method (-m)      - HTTP method (default GET).<br>    --header (-H)      - HTTP header, can be multiple (default empty).<br>    --data (-D)        - HTTP payload (default &#39;&#39;).<br>    --timeout (-T)     - connection timeout (default empty).<br>    url                - URL to request.</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c6aa6c6e23b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>