<?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 Cory Gough on Medium]]></title>
        <description><![CDATA[Stories by Cory Gough on Medium]]></description>
        <link>https://medium.com/@gough.cory?source=rss-34f709f6039------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*tM20ZfX-J78Dyn4FvcMvPQ.png</url>
            <title>Stories by Cory Gough on Medium</title>
            <link>https://medium.com/@gough.cory?source=rss-34f709f6039------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 05:23:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gough.cory/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[Supercharge Your Web Scraping with Consumer Producer Pattern!]]></title>
            <link>https://medium.com/@gough.cory/supercharge-your-web-scraping-with-consumer-producer-pattern-80c1a725b360?source=rss-34f709f6039------2</link>
            <guid isPermaLink="false">https://medium.com/p/80c1a725b360</guid>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Cory Gough]]></dc:creator>
            <pubDate>Mon, 09 Sep 2019 17:55:51 GMT</pubDate>
            <atom:updated>2019-09-09T17:55:51.040Z</atom:updated>
            <content:encoded><![CDATA[<p>If you have done any web scraping it probably looked something like the following.</p><pre>for some_url in list_of_urls:<br>    web_data = request_data(some_url)<br>    save(web_data)</pre><p>This is a straight forward way to do it, but if you have hundreds or thousands of pages to load then it will take hours! Thankfully we can speed things up by running request_data() in multiple threads. An entire blog post could be devoted to threads, but to keep it simple, think of them as subprocess that can run in parallel. Instead of one synchronous call to request_data() for each url, we can run many requests in parallel. You could visualize it as a store opening more cash registers to allow for parallel checkouts so you don’t have to wait in line as long.</p><p>Multithreading and databases like sqlite can have some issues especially if you have too many threads trying to insert at the same time. To work around this, we will implement the consumer producer pattern. The basic concept is many producer threads gather information and pass it to a consumer thread via a queue. The consumer thread will take take data from the queue and then persist it. Because we can control how many consumers there are, we can limit the number of insert locks on the database. In my code example we only have 1 consumer, so we will not have any issues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*FktgjsArq4R9u8OiVm71LA.png" /></figure><p>In the following gist, you can see a working consumer producer pattern. You just need to fill in the functions to pull the web data and determine where you want to store it. Now you just need to try to not get caught scraping too much data!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15a79f47974652038059264b984c9c09/href">https://medium.com/media/15a79f47974652038059264b984c9c09/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80c1a725b360" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Python Namespaces Through Recursion]]></title>
            <link>https://medium.com/@gough.cory/understanding-python-namespaces-through-recursion-c921cbd4e522?source=rss-34f709f6039------2</link>
            <guid isPermaLink="false">https://medium.com/p/c921cbd4e522</guid>
            <category><![CDATA[namespaces]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Cory Gough]]></dc:creator>
            <pubDate>Mon, 05 Aug 2019 14:04:56 GMT</pubDate>
            <atom:updated>2019-08-05T14:04:56.892Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article we’ll take a look at how Python handles scope of variables through namespaces. We’ll review how different levels of scope are resolved and to help drive the point home we’ll review one recursive example.</p><p>In Python, a namespace is a dictionary that maps names to objects. When you assign a string to a variable or create a new object Python is adding it to the namespace dictionary. When you access this variable later in your program Python will refer to the namespace to retrieve it. There are multiple levels to this lookup process. The order goes from local -&gt; enclosing -&gt; global -&gt; builtins. To better understand this, let’s review the following example.</p><p>Each module has a global namespace and every function creates a separate local namespace. In this example, we have declared ‘a’ three times. When we call inner_namespace, Python first looks for ‘a’ in the local namespace. If it is not found, it will look at the next level, enclosing. If it is not found there, then it will try global. Notice how ‘y’ is defined globally, but not locally. Python attempted to resolve ‘y’ in the local and enclosing namespace, but finally had to resort to global.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*a2rQOJ6Ji2zghXVhYipHJQ.png" /></figure><p>Wait! What about print? Where did it come from? This function among several others are in the highest level known as builtins. When the Python interpreter starts, it loads builtins.py and makes it universally available. Here are a few that you are probably familiar with: [‘sorted’, ‘staticmethod’, ‘str’, ‘sum’, ‘super’, ‘tuple’, ‘type’, ‘vars’, ‘zip’]. Run the following code to see the full list.</p><pre>import builtins<br>print(dir(builtins))</pre><p>Python allows for manipulation of scope through two keywords: global and nonlocal. In this example, we made ‘a’ global, so we are able to access it outside of the inner function. Otherwise, an error would be thrown because ‘a’ does not exist in the global namespace. The other keyword, nonlocal, allows us to alter the namespace of test_namespace(). These are good to know and understand, but try not to rely on them unless you have a good reason to use them.</p><pre>def test_namespace():<br>    y = &#39;enclosing y&#39;<br>    <br>    def inner_namespace():<br>        global a<br>        a = &#39;local a made global&#39;<br>        <br>        nonlocal y<br>        y = &#39;nonlocal y&#39;<br>            <br>    inner_namespace()<br>    print(y)</pre><pre>test_namespace()<br>print(a)</pre><pre>---------output----------<br>nonlocal y<br>local a made global</pre><p>If you are ever curious as to what is in the global or local namespace, you can retrieve the dictionary by calling the following functions.</p><pre>globals()<br>locals()</pre><p>Calling locals() is useful for understanding recursion. In the following example, we’ll see how to recursively compute the factorial of any number.</p><pre>def recursive_factorial(fact: list) -&gt; int:<br>    print(locals())<br>    <br>    if len(fact) == 1:<br>        print(&#39;Hit base case&#39;)<br>        return fact[0]<br>    <br>    factorial = fact[0] * recursive_factorial(fact[1:])<br>    print(locals())<br>    <br>    return factorial</pre><pre>factorial = [5, 4, 3, 2, 1]<br>recursive_factorial(factorial)</pre><pre>---------output----------<br>{&#39;fact&#39;: [5, 4, 3, 2, 1]}<br>{&#39;fact&#39;: [4, 3, 2, 1]}<br>{&#39;fact&#39;: [3, 2, 1]}<br>{&#39;fact&#39;: [2, 1]}<br>{&#39;fact&#39;: [1]}<br>Hit base case<br>{&#39;fact&#39;: [2, 1], &#39;factorial&#39;: 2}<br>{&#39;fact&#39;: [3, 2, 1], &#39;factorial&#39;: 6}<br>{&#39;fact&#39;: [4, 3, 2, 1], &#39;factorial&#39;: 24}<br>{&#39;fact&#39;: [5, 4, 3, 2, 1], &#39;factorial&#39;: 120}<br>120</pre><p>At each step the local namespace is changing. First we pass in the entire list and clearly see it in the namespace dict returned by locals(). Then we multiply the first element of the list by the factorial of the remaining elements until we hit the base case. Once the base case it hit, we unwind the calls and compute the factorial of each step. This code implements the factorial formula: n!=n×(n−1)!.</p><p>Now you can see why namespaces are a very important feature of Python. At each level we are able to have separate assignments for the same variable name. After all, it is stated as a “honking great idea” in this.py.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c921cbd4e522" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Performance of Numpy Array vs Python List]]></title>
            <link>https://medium.com/@gough.cory/performance-of-numpy-array-vs-python-list-194c8e283b65?source=rss-34f709f6039------2</link>
            <guid isPermaLink="false">https://medium.com/p/194c8e283b65</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python-list]]></category>
            <category><![CDATA[numpy]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Cory Gough]]></dc:creator>
            <pubDate>Mon, 22 Jul 2019 00:01:58 GMT</pubDate>
            <atom:updated>2019-07-22T00:01:58.186Z</atom:updated>
            <content:encoded><![CDATA[<p>Most of us have been told numpy arrays have superior performance over python lists, but do you know why?</p><p>The following graph plots the performance of taking two random arrays/lists and adding them together. We can clearly see that this operation in numpy is practically the same for 100 elements and 10,000,000 elements. While python grows at a at fast rate! To begin understanding why, we must first take a quick look at how RAM works and how arrays are stored in RAM compared to lists.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*rQoLViAcg2Hj8AD1EmONAg.png" /><figcaption>More questions on how this graph was generated? Refer to <a href="https://github.com/Pballer/dc-ds-071519/blob/master/blogs/array_vs_list/numpy_array_vs_py_list.ipynb">the code</a>.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LQDMZ2OfCEOwhxyNHFBfng.jpeg" /></figure><p>RAM is our computer’s working memory. All of your processes from streaming YouTube to reading this article resides in RAM. You could visualize RAM as a bookshelf in your house. Each book is a memory location that stores a value such as an int, double, or reference to another location in memory(aka a pointer), but more on this later. Each byte has an address that allows for quick access to any point of data. Hence the name Random Access Memory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fsib5-b1pMLSNV1iKqgwWw.jpeg" /></figure><p>Numpy arrays store one defined type of data and the number of elements is given up front . This is necessary because they are stored as one contiguous block of memory. It’s like encyclopedias, all in alphabetical order on your bookshelf. This image represents an array of type 1 byte int and length 5. Knowing the starting memory location of the array, we can simply add the index to immediately jump to any element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2WdhwdoAzk8hmQvTZaBlVA.jpeg" /><figcaption><a href="https://docs.python.org/3/faq/design.html#how-are-lists-implemented-in-cpython">Refer to the python documentation</a>.</figcaption></figure><p>Python lists are not stored like this. You don’t have to specify how many elements you will append to your list nor do you have to define a data type. This is possible because Python lists are actually arrays of pointers. A pointer is a reference to an object in memory. When you retrieve the first element in your list, python is taking two steps: First, retrieve the pointer. Second, go to the memory location of the pointer to finally get the object you want. This extra lookup in memory contributes to the major performance decrease in lists.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3TkFpmkXQZFsAeGLyjAIOw.jpeg" /><figcaption>Array being copied to CPU Cache.</figcaption></figure><p>It gets worse. Your CPU has a small amount of local memory known as a cache. To avoid making repetitive roundtrips to RAM, the CPU will retrieve and store blocks of contiguous memory in the cache. Imagine your nightstand is the cache and your bookshelf is RAM. When you go to get the first book in your set of encyclopedias, you decide to take them all back to your nightstand cache. After all, it is convenient because they are all next to each other. Now you can quickly access all of the books without having to get up! With lists, we do not get this benefit. The data is scattered all throughout your RAM and referenced by pointers. Each time we need a piece of data, we must get the pointer and then go look it up. It is as if our encyclopedias were scattered and we had to run around our mansion home’s library looking for books!</p><p>The topic of memory and how each data structure is stored can get very dense very quickly, but hopefully this high level explanation gives you a mental image of what is happening under the python hood. The main takeaway is understanding the pros and cons of arrays vs lists and when to use which. Simply put, use arrays for large sets of data that are all of the same type. If you find the topic interesting, try researching how this can be done even faster on a GPU!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=194c8e283b65" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>