<?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 Daniel Podrażka on Medium]]></title>
        <description><![CDATA[Stories by Daniel Podrażka on Medium]]></description>
        <link>https://medium.com/@daniep?source=rss-27dd5205141f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jSRa1i-hI475KJN6D96CEw.png</url>
            <title>Stories by Daniel Podrażka on Medium</title>
            <link>https://medium.com/@daniep?source=rss-27dd5205141f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 13:00:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@daniep/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[Pandas ETLs With Invisible Cache]]></title>
            <link>https://medium.com/@daniep/speed-up-developing-python-etls-with-invisible-cache-eb2eaadf6918?source=rss-27dd5205141f------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb2eaadf6918</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[remote-development]]></category>
            <category><![CDATA[etl]]></category>
            <dc:creator><![CDATA[Daniel Podrażka]]></dc:creator>
            <pubDate>Mon, 12 Dec 2022 01:25:18 GMT</pubDate>
            <atom:updated>2024-07-16T07:06:41.212Z</atom:updated>
            <content:encoded><![CDATA[<h4>Monkeypatching to the rescue</h4><p>If you develop Python scripts that access multiple and interdependent resources, you’ll end up loading data way too many times. The loading times quickly add up, waste your time and kick you out of flow.</p><p>But it doesn’t have to be this way.</p><p>What if I told you one import statement can liberate you from all this burden?</p><p>I decided to monkeypatch the most common functions that I use for loading data. The monkeypatched version is the same as original but it’s also wrapped by a caching function that saves the arguments and responses of the original function.</p><p>So, you only have to load the data once, the next time you try loading it, it will return the same response from your local drive. Immediately.</p><h3>How to do it</h3><p>For caching, I use the incredibly fast <a href="https://pypi.org/project/diskcache/">diskcache</a> library.</p><pre>import os<br>from diskcache import FanoutCache<br>DEFAULT_CACHE_SIZE = (2 ** 30) * 50  # 50GB<br>cache = FanoutCache(<br>    os.environ.get(<br>        &quot;LOCAL_CACHE_PATH&quot;, os.path.join(os.path.expanduser(&quot;~&quot;), &quot;localcache&quot;)<br>    ),<br>    size_limit=os.environ.get(&quot;LOCAL_CACHE_SIZE&quot;, DEFAULT_CACHE_SIZE),<br>)</pre><p>I decided to limit this to a few common Pandas functions:</p><pre>cache_functions = [<br>    &quot;read_feather&quot;,<br>    &quot;read_excel&quot;,<br>    &quot;read_csv&quot;,<br>    &quot;read_table&quot;,<br>    &quot;read_sql&quot;,<br>    &quot;read_sql_query&quot;,<br>    &quot;read_sql_table&quot;,<br>    &quot;read_parquet&quot;,<br>    &quot;read_json&quot;,<br>]</pre><p>This is how I monkeypatch the functions. Since Python is dynamic, you can assign anything to any function. Here I assign my wrapped functions to the Pandas object (pd). exec is used here just out of convenience.</p><pre>for func in cache_functions:<br>    if func in [&quot;read_sql&quot;, &quot;read_sql_query&quot;, &quot;read_sql_table&quot;]:<br>        exec(f&quot;pd.{func} = cache.memoize(ignore=(1,))(pd.{func})&quot;)<br>    else:<br>        exec(f&quot;pd.{func} = cache.memoize()(pd.{func})&quot;)</pre><p>Hint: ignore=(1,)is required for read_sql because the second argument is a DB connection, which shouldn’t be cached, since it changes every time.</p><p>Save these three snippets together in a file, i.e. cache_pandas.py and import it to your script:</p><pre>from cache_pandas import pd</pre><p>Now you can access all functions from cache_functions as normal Pandas functions, but with local cache. Loading will mostly depend on the speed of your local disk :)</p><p>Once your script is processing everything correctly, and you want to commit the changes, simply replace the import statement with</p><pre>import pandas as pd</pre><h3>Reloading the data</h3><p>Sometimes, you might change the underlying resource, or the resource is dynamic, and you need to reload the data.</p><p>I could not find a simple way of doing it, so I monkeypatched a part of the caching mechanism as well.</p><pre>from diskcache.core import args_to_key, full_name, ENOVAL, ft<br>from diskcache import FanoutCache<br><br>def memoize(<br>    self, name=None, typed=False, expire=None, tag=None, ignore=(), refresh=False<br>):<br>    if callable(name):<br>        raise TypeError(&quot;name cannot be callable&quot;)<br><br>    def decorator(func):<br>        &quot;&quot;&quot;Decorator created by memoize() for callable `func`.&quot;&quot;&quot;<br>        base = (full_name(func),) if name is None else (name,)<br><br>        @ft.wraps(func)<br>        def wrapper(*args, **kwargs):<br>            &quot;&quot;&quot;Wrapper for callable to cache arguments and return values.&quot;&quot;&quot;<br>            key = wrapper.__cache_key__(*args, **kwargs)<br>            if refresh and self.get(key):<br>                del self[key]<br>            result = self.get(key, default=ENOVAL, retry=True)<br>            if result is ENOVAL:<br>                result = func(*args, **kwargs)<br>                if expire is None or expire &gt; 0:<br>                    try:<br>                        self.set(key, result, expire, tag=tag, retry=True)<br>                    except TypeError as e:<br>                        print(f&quot;diskcache: Couldn&#39;t cache {key}&quot;)<br>            return result<br><br>        def __cache_key__(*args, **kwargs):<br>            &quot;&quot;&quot;Make key for cache given function arguments.&quot;&quot;&quot;<br>            return args_to_key(base, args, kwargs, typed, ignore)<br><br>        wrapper.__cache_key__ = __cache_key__<br>        return wrapper<br><br>    return decorator<br><br><br>FanoutCache.memoize = memoize</pre><p>The edited memoize method adds refresh argument, which removes the locally stored data.</p><h3>pd.reread</h3><p>I edited the original monkeycaching to include pd.reread methods, i.e. pd.reread_csv which will reload the data on each call and save the response in cache.</p><pre>for func in cache_functions:<br>    org_func = eval(f&quot;pd.{func}&quot;)<br>    if func in [&quot;read_sql&quot;, &quot;read_sql_query&quot;, &quot;read_sql_table&quot;]:<br>        exec(f&quot;pd.{func} = cache.memoize(ignore=(1,))(org_func)&quot;)<br>        exec(f&quot;pd.re{func} = cache.memoize(refresh=True, ignore=(1,))(org_func)&quot;)<br>    else:<br>        exec(f&quot;pd.{func} = cache.memoize()(org_func)&quot;)<br>        exec(f&quot;pd.re{func} = cache.memoize(refresh=True)(org_func)&quot;)</pre><p>With local cache in place, you can explore new ways of developing ETLs, where you can test changes instantly. For example, with unittests on file save. Short feedback loops like these are essential for efficient and reliable coding.</p><p>Monkeypatching is usually considered bad practice, however, in this case it allows your code to be production-ready by just removing the import statement that edits your loading functions.</p><h3>How about DB connections?</h3><p>To use read_sql, you need to be connected to a database. Depending on the rest of your setup, you might use a context manager that establishes a new connection before you load the resource. Something like this:</p><pre>with connect() as con:<br>    df = pd.read_sql(&quot;select yolk from eggs&quot;, con)<br></pre><p>However, there is no need to connect if you have the resource already cached. Believe me, a second wasted on connecting to DB can make a difference.</p><p>I monkeypatched it too. Connection is ignored in the cache, so I made the most basic Context manager:</p><pre>class cconect(object):<br>    def __enter__(self):<br>        return self<br><br>    def __exit__(self, type, value, traceback):<br>        ...</pre><p>If you need a second context manager that depends on the first one, as in:</p><pre>with connect() as con, con.begin():<br>    df = pd.read_sql(&quot;select yolk from eggs&quot;, con)</pre><p>You’ll need a more complex ContextManager. All attributes of self that alias self.__enter__ can be used as a dependent ContextManager:</p><pre>class cconnect(object):<br>    def __init__(self):<br>        self.begin = self.__enter__<br><br>    def __enter__(self):<br>        return self<br><br>    def __exit__(self, type, value, traceback):<br>        ...</pre><p>Get the full working snippet from <a href="https://gist.github.com/danielpodrazka/d65e77c07963a26f5672630b36e36caf">https://gist.github.com/danielpodrazka/d65e77c07963a26f5672630b36e36caf</a></p><p>Please comment on here or Github if you have any suggestions on how to improve the snippet. It is still work in progress and some objects might have unexpected behavior. Use at own risk.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb2eaadf6918" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Can’t visualize anything? Programming is your goldmine.]]></title>
            <link>https://medium.com/@daniep/so-you-cant-visualize-anything-programming-might-be-your-goldmine-e29c1d7c065f?source=rss-27dd5205141f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e29c1d7c065f</guid>
            <category><![CDATA[stem]]></category>
            <category><![CDATA[visualization]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[aphantasia]]></category>
            <category><![CDATA[money]]></category>
            <dc:creator><![CDATA[Daniel Podrażka]]></dc:creator>
            <pubDate>Fri, 22 Apr 2022 13:16:57 GMT</pubDate>
            <atom:updated>2024-07-16T07:07:16.572Z</atom:updated>
            <content:encoded><![CDATA[<h4>People with aphantasia often turn out to be great developers.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XGiY8Gm9I_0es_Jg55cOOA.jpeg" /></figure><p>Some time ago, I came across the term aphantasia while surfing the Internet.</p><p>It’s used to describe the inability to picture anything in your mind. It mostly refers to imagining visual stuff but can also relate to other senses such as sound or smell.</p><p>As it happens, I can only see what’s really there when I close my eyes — the darkness — maybe some red spots if I’m facing the sun.</p><p>Many people with aphantasia are really sad that they can’t visualize and see it as a disability.</p><p>Some feel the need to photograph everything, at least to remember a given special moment in this way.</p><p>Well, I try to be empathetic and I can see that it could be seen as a negative thing, especially if someone has lost that ability after head trauma, which unfortunately happens sometimes.</p><p>Personally, I cannot imagine a time when I didn’t have aphantasia. (Although I must admit that I like taking pictures too.)</p><p>And knowing that I have it has always fascinated me.</p><p>It means that the part of the brain that was used for visualizing has been taken over by something else!</p><p>It also means that the limits of people who need to visualize to imagine something don’t exist for me.</p><p>Of course, that doesn’t mean I can’t use my imagination at all — I just don’t display the output image. Like a headless browser.</p><p>And do you know what headless browsers are good for?</p><p>Saving all these resources and time that would be wasted by displaying the information on the screen. Instead, more energy can be allocated to processing.</p><p>As it turns out, quite a few people with aphantasia are great developers, and many have engineering or pure math degrees.<a href="https://www.sciencedirect.com/science/article/abs/pii/S0010945220301404">[1]</a></p><p>Recently, I’ve read this in a <a href="https://medium.com/codex/how-to-recognize-a-bad-software-developer-4b41b266a560">Medium article by Denys Opria</a>:</p><blockquote>Programming is the process of formalization of abstract thoughts.</blockquote><p>This was the perfect description for me as to why I’m obsessed with programming.</p><p>It gives people with aphantasia a creative outlet. A way to express the very abstract thoughts that often cannot be expressed in images.</p><p>Programming is not only perfectly suited for aphantasiacs but also it’s the perfect medium for us to transfer this otherworldly brain activity into something that has a meaning in this world.</p><p>And if you can imagine, you probably wouldn’t understand what I mean, but that’s fine.</p><p>–</p><p>If you also have aphantasia and work in the tech industry, write me a message or leave a comment. I would like to learn more about the experience of developers with aphantasia.</p><p><strong>References</strong></p><p>[1] Zeman, Adam, et al. “Phantasia–the psychological significance of lifelong visual imagery vividness extremes.” <em>Cortex</em> 130 (2020): 426–440. <a href="https://doi.org/10.1016/j.cortex.2020.04.003">https://doi.org/10.1016/j.cortex.2020.04.003</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e29c1d7c065f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>