<?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 Mátyás Fodor on Medium]]></title>
        <description><![CDATA[Stories by Mátyás Fodor on Medium]]></description>
        <link>https://medium.com/@MTYS_FDR?source=rss-4181bb403990------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_QIFNAGL9MpOp9MC_efXIA.jpeg</url>
            <title>Stories by Mátyás Fodor on Medium</title>
            <link>https://medium.com/@MTYS_FDR?source=rss-4181bb403990------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 24 May 2026 02:29:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@MTYS_FDR/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[Efficient zip function in JavaScript]]></title>
            <link>https://medium.com/@MTYS_FDR/efficient-zip-function-in-javascript-9e4921cd11be?source=rss-4181bb403990------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e4921cd11be</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Mátyás Fodor]]></dc:creator>
            <pubDate>Sun, 31 Oct 2021 13:38:46 GMT</pubDate>
            <atom:updated>2021-10-31T13:43:36.180Z</atom:updated>
            <content:encoded><![CDATA[<h3>Efficient zip function in JavaScript</h3><p>The Python <em>zip</em> function is a clever analogy of a real zipper: It allows you to iterate over multiple arrays element-wise without having to worry about indices. It is quite handy when you have to manually pair elements coming from two (or more) data sources:</p><pre>&gt;&gt;&gt; scoreList = [5, 3, 6, 8]<br>&gt;&gt;&gt; playerList = [‘Mary’, ‘John’, ‘Emma’, ‘Gavin’]<br>&gt;&gt;&gt; list(zip(scoreList, playerList))<br># [(5, ‘Mary’), (3, ‘John’), (6, ‘Emma’), (8, ‘Gavin’)]</pre><p>Please note the <em>list()</em> call is needed to convert the result to a list, otherwise it returns something like</p><pre>&gt;&gt;&gt; zip(scoreList, playerList)<br># &lt;zip object at 0x109b19d00&gt;</pre><p>which is an object implementing the iterator interface. Why the hassle with this object? Why is it not just a list? The iterator interface allows lazy-evaluation, meaning it won’t create a list, only if it’s asked. This object instead allows generating elements on-demand, which can be more efficient. Let’s say I want to find the first player with score 6:</p><pre>for score, player in zip(scoreList, playerList):<br> if score == 6:<br> print(f’player {player} has score 6&#39;)<br> break</pre><p>In this case, the iteration stops with (<em>(6, ‘Emma’)</em>) and the last pair (<em>(8, ‘Gavin’)</em>) would never be constructed.</p><p>The implementation is quite interesting, because <em>zip</em> not only works with two lists, it can more than two arrays as input parameters. This can be particularly useful when trying to transpose a list of lists (a matrix). If you’re not familiar with the term <em>transpose</em>, it simply means a list of list is simply flipped by its diagonal. The transposition of</p><pre>[<br> [1, 2, 3],<br> [4, 5, 6],<br> [7, 8, 9]<br>]</pre><p>is</p><pre>&gt;&gt;&gt; list(zip(*[<br>… [1, 2, 3],<br>… [4, 5, 6],<br>… [7, 8, 9]<br>… ]))<br>[<br> [1, 4, 7],<br> [2, 5, 8],<br> [3, 6, 9]<br>]</pre><p>What happens if the input arrays do not have the same length? According to the <a href="https://docs.python.org/3.3/library/functions.html#zip">Python docs</a>, <em>it stops when the shortest input iterable is exhausted</em>, meaning it will return as many elements as long the shortest array is:</p><pre>&gt;&gt;&gt; list(zip([1, 2, 3], [4, 5]))<br>[(1, 4), (2, 5)]</pre><p>If all elements need to be returned, <a href="https://docs.python.org/3/library/itertools.html#itertools.zip_longest">itertools.zip_longest</a> should be used.</p><p>And just to spice things up the little, let me mention that as the <em>zip</em> function expects iterables as input, not specifically arrays. Iterables are more generic than arrays, without going too much into the details, they are objects that can be iterated over one element at the time, and they can signal if they are exhausted (iteration finished). In practice, it means that any object that can be iterated over can be used as inputs of zip: tuples, sets, dictionaries, range, or even results of other zips. Mind-blowing, right? 🤯 It is also possible to create an infinite zip. Let me use <a href="https://docs.python.org/3/library/itertools.html#itertools.count">itertools.count</a> to demonstrate it. It is very similar to <em>range()</em> except it has no stopping criteria, so if it is used in a for loop it keeps yielding values unless it is stopped.</p><pre>&gt;&gt;&gt; for a, b in zip(itertools.count(start=0, step=2), itertools.count(start=1, step=2)):<br>… print(a, b)<br>1 2<br>3 4<br>5 6<br>…</pre><p>I really hope I could convince you by now, how cool and versatile this Python standard library function is. Why can’t we have nice things in JavaScript? Well we can, you just probably end up hunting for third-parties on npm or ready-made solutions on Stack Overflow. But is there anything more satisfying than using your home-grown utilities? You can find it out on <a href="https://matyasfodor.com/blog/efficient-zip#how-javascript-could-do-it">my website</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e4921cd11be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to outsmart incognito block]]></title>
            <link>https://medium.com/free-code-camp/disabling-browser-incognito-check-cc84288e89b3?source=rss-4181bb403990------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc84288e89b3</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[tampermonkey]]></category>
            <category><![CDATA[incognito]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Mátyás Fodor]]></dc:creator>
            <pubDate>Wed, 27 Dec 2017 05:34:54 GMT</pubDate>
            <atom:updated>2019-02-18T12:20:37.181Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BgAlYI-fuv9BrHQHPnNZoQ.jpeg" /></figure><p>Recently I came across several sites that showed a warning or paywall because I was using incognito mode. I think it is unfair. I should be allowed to use whatever browser and mode I want. This is a way to enforce their tracking tools. I know that <a href="https://thevpn.guru/is-incognito-mode-safe-secure/">incognito is not safe</a> but it’s the bare minimum you can do to avoid ads to track you.</p><p>The whole thing took me about two hours and I learned a lot about browser extensions and hacking client-side code as an end user. I thought this might be worth sharing.</p><p>First, I had to look up how to detect private mode. As per my best knowledge, there’s no browser API to detect private mode directly, so I was pretty sure it is a sneaky little script. <a href="https://stackoverflow.com/a/27805491/2419215">This</a> StackOverflow answer gave me a hint, so I knew I’d have to look for webkitRequestFileSystem. I found <a href="https://gist.github.com/matyasfodor/15e8863ab15baf4791a5fa4c748b64af">this</a> bit in one of those private loathing sites’ minified JavaScript code. Here’s the exciting part:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cda994c39d59a7ba81f611b3789d0f76/href">https://medium.com/media/cda994c39d59a7ba81f611b3789d0f76/href</a></iframe><p>I could test the <a href="https://gist.github.com/matyasfodor/15e8863ab15baf4791a5fa4c748b64af">module</a> by pasting it in incognito and public browser window dev console and run:</p><pre>var module = {};<br>incognito(null, module);<br>module.exports.detectIncognito().then(console.log)</pre><p>Bingo! This is it, I just have to find a way not to call the error callback in window.webkitRequestFileSystem(..). The easiest way is to monkey patch the function:</p><pre>(function(webkitRequestFileSystem) {<br>  window.webkitRequestFileSystem = function(t, s, success, error) {<br>    webkitRequestFileSystem(t, s, success, success);<br>  }<br>})(window.webkitRequestFileSystem);</pre><p>If you’re not familiar with the technique, <a href="https://www.audero.it/blog/2016/12/05/monkey-patching-javascript/">monkey patching</a> is a way to add, modify, or suppress the default behavior of a piece of code at runtime without changing its original source code.</p><p><strong>Detour 1:</strong> First I started writing my own chrome extension using <a href="https://extensionizr.com">Extensionizr</a>. It’s a great tool generating Chrome extension boilerplate code. But in the end, I found an easier solution.</p><p>Whenever it comes to customizing websites I use <a href="https://tampermonkey.net/">Tampermonkey</a> (for example <a href="https://gist.github.com/rjrudman/a472924d3fb078bd73bb12066e0319a0">hiding job ads</a> on Stack Overflow when I really shouldn’t spend time on looking for new positions). You don’t have to install a n+1th extension, and it provides a nice interface to manage your scripts. Ok, nice is probably an exaggeration, it’s ugly but handy.</p><p>So I added the monkey patch script I referenced above, already giggling how easy it was, but bummer, it didn’t work. I tried a few other things for example:</p><pre>window.foobar = &#39;baz&#39;;</pre><p>But in the dev console, this property was absent from the window variable. It turned out <a href="https://stackoverflow.com/a/20513730/2419215">content scripts</a> are running in an isolated environment, they only share the DOM with the webpage’s scripts. I started to work with the referenced solution from SO. There was one very important thing though, I had to execute this code before the current page’s code. Here’s what I came up with:</p><pre>function injectScript(file_path, node) {<br>        var element = document.createElement(&#39;script&#39;);<br>        element.setAttribute(&#39;type&#39;, &#39;text/javascript&#39;);<br>        element.setAttribute(&#39;src&#39;, file_path);<br>        element.setAttribute(&#39;async&#39;, false);<br>        node.appendChild(element);<br>}<br>injectScript(url, document.documentElement);</pre><p><strong>Detour 2: </strong>As I started with an extension, loading another JavaScript file was trivial. However it’s not the case with Tampermonkey scripts (at least I don’t know about it). So I decided to put my code in a GitHub gist, and tried to load the <a href="https://gist.githubusercontent.com/matyasfodor/ab6c92e32a35ebae0bebedff8e7cf569/raw/4f97a8fb702ae8710ba9542b5a7a8127495cf9e4/fakepublic.js">raw file</a>. But then the browser was complaining about its MIME type. Finally I ended up using <a href="https://rawgit.com/">https://rawgit.com/</a>, which was exactly the right tool for this.</p><p>I realized that I should add those few lines of monkey patch code as a string, and fill in the script element’s text with that. Here’s my final solution:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4b93c733238e408360a2ab065209a12/href">https://medium.com/media/b4b93c733238e408360a2ab065209a12/href</a></iframe><p>An important thing to know if you work with Tampermonkey in incognito mode: your changes made in incognito mode won’t appear in normal mode, and vice versa: you have to close all your private windows if you want to try your latest changes made in public mode.</p><p><strong>Be careful!</strong> If you decide to use my script, you have to know that this <em>might</em> (although not very likely) break some web pages. You can always turn these off in Tampermonkey. Use it at your own risk.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc84288e89b3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/disabling-browser-incognito-check-cc84288e89b3">How to outsmart incognito block</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reverse engineer a Python object]]></title>
            <link>https://medium.com/@MTYS_FDR/reverse-engineer-a-python-object-3fd365b6c0d0?source=rss-4181bb403990------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fd365b6c0d0</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Mátyás Fodor]]></dc:creator>
            <pubDate>Sun, 16 Jul 2017 21:33:56 GMT</pubDate>
            <atom:updated>2017-12-27T21:21:49.740Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_r1gUtdyxmXKal9p5ci4oQ.jpeg" /></figure><p>I faced an interesting challenge at work the other day. I felt like sharing because it might save a few hours for others, or reveal some insights about the Python internals.</p><p>There was a function which takes an object (let’s call it Foo) as an argument. I knew how the serialized object looks like, I had it in JSON format. Setting up the whole Python environment and provisioning the database looked like too much trouble, so I decided to restore the original object based on the JSON response. There must be a way, right?</p><p>It was pretty straightforward to parse the JSON as an object (of objects). What the following piece of code does it basically recursively turns each dict of the JSON into an object where the properties serve as the keys of the original dict. (Credits to <a href="https://stackoverflow.com/a/15882054/2419215">this</a> SO answer)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f04a520779657b78d3ebf7fc82a5cb2e/href">https://medium.com/media/f04a520779657b78d3ebf7fc82a5cb2e/href</a></iframe><p>So far so good, but there was a surprise in the function I wanted to run:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d9a9f9fe626a6b83de4c45d81bf344d/href">https://medium.com/media/8d9a9f9fe626a6b83de4c45d81bf344d/href</a></iframe><p>Wait a minute. So this code expects a dictionary in some cases.. What to do now? I was sure about one thing: I don’t want to implement my own JSON parser. Of course I could write a more elaborate <em>object_hook</em> callback function instead of that lambda, but it felt like I’d micromanage things that way. Why not parse it as it is, then align it for the requested format? It’s easy to convert a named tuple to a dict with the <em>._asdict() call.</em> So I tried</p><pre>foo.bar.bazs = foo.bar.bazs._asdict()</pre><p>But of course it raised an <em>AttributeError</em>, since namedtuples are immutable.</p><p>Ok, so I googled “mutable namedtuple” and <a href="https://stackoverflow.com/a/29419745/2419215">this</a> SO answer suggested me to use <em>recordclass</em>. How cool is this? It overcomes the standard library’s limitation. The only downside is that it’ll be another dependency. Well not quite the only one, as we’ll see. I tried to run the original function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d9a9f9fe626a6b83de4c45d81bf344d/href">https://medium.com/media/8d9a9f9fe626a6b83de4c45d81bf344d/href</a></iframe><p>And it turns out the keys are meant to be objects, not just the ids of those objects. Luckily my JSON response also contains a list of baz objects. So I could just create a dict from them:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a919cf224ee3639311c2827b9328761/href">https://medium.com/media/3a919cf224ee3639311c2827b9328761/href</a></iframe><p>This raises an error saying that type: ‘X’ is unhashable. What is hashability? From the <a href="https://docs.python.org/2/glossary.html#term-hashable">Python Glossary</a>:</p><blockquote><strong>hashable</strong></blockquote><blockquote>An object is <em>hashable</em> if it has a hash value which never changes during its lifetime (it needs a <a href="https://docs.python.org/2/reference/datamodel.html#object.__hash__"><strong>__hash__()</strong></a> method), and can be compared to other objects (it needs an <a href="https://docs.python.org/2/reference/datamodel.html#object.__eq__"><strong>__eq__()</strong></a> or <a href="https://docs.python.org/2/reference/datamodel.html#object.__cmp__"><strong>__cmp__()</strong></a> method). Hashable objects which compare equal must have the same hash value.</blockquote><blockquote>Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.</blockquote><blockquote>All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal (except with themselves), and their hash value is derived from their <a href="https://docs.python.org/2/library/functions.html#id"><strong>id()</strong></a>.</blockquote><p>So this is not an Excalibur. Namedtuples are immutable for a reason. their hash value is based on their property value’s hash value, and it never changes. Note: Namedtuples are only hashable if their property values are hashable. See:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31efe0e03a094a32c78cadf26eb2d619/href">https://medium.com/media/31efe0e03a094a32c78cadf26eb2d619/href</a></iframe><p>(The dict object is mutable, therefore it’s not hashable =&gt; the namedtuple is not hashable).</p><p>It is possible to compare recordclass objects, and it works nicely, however the hash value is for the object’s entire lifetime. For instance you expect</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a198b26082418d12d8ecf893af3db28b/href">https://medium.com/media/a198b26082418d12d8ecf893af3db28b/href</a></iframe><p>to work, but that’s granted by the namedtuple’s immutable nature. The two separate instances point to the same value.</p><p>I could have somehow automagically combine these mutable and immutable data structures together, in the <em>json.loads</em>’s <em>object_hook</em>, but again, it seemed like too much work.</p><p>I recently finished a book called <a href="http://shop.oreilly.com/product/0636920032519.do">Fluent Python</a> and I remembered there was a code recipe for a nametuple like object:</p><p><a href="https://github.com/fluentpython/example-code/blob/599c141a1d427dea8954c9362871be61b9f418c6/21-class-metaprog/factories.py#L31">fluentpython/example-code</a></p><p>so this returns an object, which is hashable by default (according the glossary), and mutable.</p><p>I tried to run the code, no luck as it lacks of the method <em>_asdict()</em>. I could quickly fix this by adding the following method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/26d6fc8b7b8139b22e3acda4771adc64/href">https://medium.com/media/26d6fc8b7b8139b22e3acda4771adc64/href</a></iframe><p>This is all good, made my code run. Of course, the hash function is not working as expected. But that’s not too hard to fix. Since most of my objects had an <strong><em>id</em></strong> property, we can say that two objects are equal if their id’s are the same. If there’s no id, we can still rely on the Python object id:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/695d4424fa0da73719fb4f589a2e844b/href">https://medium.com/media/695d4424fa0da73719fb4f589a2e844b/href</a></iframe><p>Of course this implementation won’t account for property changes, but we can make this assumption that two instances are equal if they have the same id (or object id).</p><p>I hope you enjoyed this reading. If something’s unclear or I missed something, feel free to reach out to me.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fd365b6c0d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>