<?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 Mr Stucknet on Medium]]></title>
        <description><![CDATA[Stories by Mr Stucknet on Medium]]></description>
        <link>https://medium.com/@mr.stucknet?source=rss-1145b919138------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*UxWeRkYn_E_Za0ZY</url>
            <title>Stories by Mr Stucknet on Medium</title>
            <link>https://medium.com/@mr.stucknet?source=rss-1145b919138------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 15:42:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mr.stucknet/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[Python for Networking part 3]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@mr.stucknet/python-for-networking-part-3-91da81a90058?source=rss-1145b919138------2"><img src="https://cdn-images-1.medium.com/max/1736/1*YBywQz7c93OObmrJENGbQw.jpeg" width="1736"></a></p><p class="medium-feed-snippet">Looping over lists in Python</p><p class="medium-feed-link"><a href="https://medium.com/@mr.stucknet/python-for-networking-part-3-91da81a90058?source=rss-1145b919138------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@mr.stucknet/python-for-networking-part-3-91da81a90058?source=rss-1145b919138------2</link>
            <guid isPermaLink="false">https://medium.com/p/91da81a90058</guid>
            <category><![CDATA[loop]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[loops-in-python]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Mr Stucknet]]></dc:creator>
            <pubDate>Mon, 25 Nov 2024 11:14:31 GMT</pubDate>
            <atom:updated>2024-11-25T11:14:31.084Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Python for Networking Part 2]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@mr.stucknet/python-for-networking-part-2-11eb636be5db?source=rss-1145b919138------2"><img src="https://cdn-images-1.medium.com/max/768/1*V3mhK7L7AaXzEeYlrFPlbA.jpeg" width="768"></a></p><p class="medium-feed-snippet">Converting between data types in Python</p><p class="medium-feed-link"><a href="https://medium.com/@mr.stucknet/python-for-networking-part-2-11eb636be5db?source=rss-1145b919138------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@mr.stucknet/python-for-networking-part-2-11eb636be5db?source=rss-1145b919138------2</link>
            <guid isPermaLink="false">https://medium.com/p/11eb636be5db</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python-programs]]></category>
            <dc:creator><![CDATA[Mr Stucknet]]></dc:creator>
            <pubDate>Mon, 25 Nov 2024 10:51:19 GMT</pubDate>
            <atom:updated>2024-11-25T10:51:19.403Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Python for Networking Part 1]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@mr.stucknet/python-for-networking-part-1-62474ffdc7c3?source=rss-1145b919138------2"><img src="https://cdn-images-1.medium.com/max/1328/1*xM2VB3gPQSkOhi9lL_OyiA.jpeg" width="1328"></a></p><p class="medium-feed-snippet">Since its development in the early 1990s, Python has become one of the most widely used programming languages in the world.</p><p class="medium-feed-link"><a href="https://medium.com/@mr.stucknet/python-for-networking-part-1-62474ffdc7c3?source=rss-1145b919138------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@mr.stucknet/python-for-networking-part-1-62474ffdc7c3?source=rss-1145b919138------2</link>
            <guid isPermaLink="false">https://medium.com/p/62474ffdc7c3</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python-programming]]></category>
            <dc:creator><![CDATA[Mr Stucknet]]></dc:creator>
            <pubDate>Thu, 14 Nov 2024 05:41:09 GMT</pubDate>
            <atom:updated>2024-11-14T05:41:09.294Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[LEVERAGING ENTERPRISE AI OPPORTUNITIES]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@mr.stucknet/leveraging-enterprise-ai-opportunities-408363acf9f5?source=rss-1145b919138------2"><img src="https://cdn-images-1.medium.com/max/1152/1*2OvMEEHDt-9eC9yKnpjAFQ.jpeg" width="1152"></a></p><p class="medium-feed-snippet">The rise of AI and the recent developments in generative AI have been incredible. Let&#x2019;s discuss the reasons behind such a surge.</p><p class="medium-feed-link"><a href="https://medium.com/@mr.stucknet/leveraging-enterprise-ai-opportunities-408363acf9f5?source=rss-1145b919138------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@mr.stucknet/leveraging-enterprise-ai-opportunities-408363acf9f5?source=rss-1145b919138------2</link>
            <guid isPermaLink="false">https://medium.com/p/408363acf9f5</guid>
            <category><![CDATA[mls]]></category>
            <category><![CDATA[artificalintelligence]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Mr Stucknet]]></dc:creator>
            <pubDate>Thu, 14 Nov 2024 05:10:15 GMT</pubDate>
            <atom:updated>2024-11-14T05:10:15.434Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Enterprise Transformation with AI in the Cloud Part 1]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@mr.stucknet/enterprise-transformation-with-ai-in-the-cloud-part-1-5a7de215426d?source=rss-1145b919138------2"><img src="https://cdn-images-1.medium.com/max/720/1*nOhBDVRna2CPyQwsJs_QAw.jpeg" width="720"></a></p><p class="medium-feed-snippet">The future of computing is at the edge, powered by AI and the cloud.</p><p class="medium-feed-link"><a href="https://medium.com/@mr.stucknet/enterprise-transformation-with-ai-in-the-cloud-part-1-5a7de215426d?source=rss-1145b919138------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@mr.stucknet/enterprise-transformation-with-ai-in-the-cloud-part-1-5a7de215426d?source=rss-1145b919138------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a7de215426d</guid>
            <category><![CDATA[artificalintelligence]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[mlops]]></category>
            <dc:creator><![CDATA[Mr Stucknet]]></dc:creator>
            <pubDate>Wed, 13 Nov 2024 05:53:13 GMT</pubDate>
            <atom:updated>2024-11-13T05:53:13.619Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[How to install Parrot OS on your Windows System]]></title>
            <link>https://medium.com/@mr.stucknet/how-to-install-parrot-os-on-your-windows-system-42be1db940a6?source=rss-1145b919138------2</link>
            <guid isPermaLink="false">https://medium.com/p/42be1db940a6</guid>
            <category><![CDATA[how-to-install-linux-os]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[how-to-install-parrot-os]]></category>
            <category><![CDATA[how-to]]></category>
            <category><![CDATA[how-to-install-linux]]></category>
            <dc:creator><![CDATA[Mr Stucknet]]></dc:creator>
            <pubDate>Mon, 14 Oct 2024 22:32:11 GMT</pubDate>
            <atom:updated>2024-10-14T22:32:11.495Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*WILQ-xELd4ooh2NDBEI-YQ.png" /></figure><p>To install Parrot OS on your Windows base system you need to fulfil the following steps:</p><p>1- Virtual Box</p><p>2- Parrot OS Image</p><p>You can find both of them here.</p><p><a href="https://www.virtualbox.org/">Oracle VirtualBox</a></p><p><a href="https://www.parrotsec.org/">Parrot Security</a></p><h3>Now let’s Begin</h3><h3>Step1:</h3><p>First Download the Virtual Box by going to the download page and clicking on the Windows host button</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hUj_D3CpN3qZqp2_YQlahA.png" /></figure><h3>Step2:</h3><p>Now let it download and go to the parrot os download page. There you have to select the virtual machine option.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rvw0DPrnwgE29Qs-oNH_Kg.png" /></figure><h3>Step3:</h3><p>Then Select the Security Distribution</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yAsgyEaH63J_mjbOmj2nsA.png" /></figure><h3>Step4:</h3><p>Then Select the AMD64 Architecture and in case of ARM64 choose it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AxSCknVcKLS9HAG-yyiMIA.png" /></figure><h3>Step5:</h3><p>All Set</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n_FLq_gvr6n8eymyeHTygA.png" /></figure><h3>Step6:</h3><p>Now scroll down and click on the download button then clock on the virtual box and your virtual machine will start downloading</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hAWH55kcEAPaqg4IimA7PQ.png" /></figure><h3>Step7:</h3><p>Both of them look like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*VqRfg5aAVzKYgJU92M2PTg.png" /></figure><h3>Step8:</h3><p>Install the Virtualbox first by first clicking on the exe and then just following the instructions after it.</p><h3>Step9:</h3><p>Then Your VirtualBox will look like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SGK7mrN-NHDsmcZ4OIJZVw.png" /></figure><h3>Step10:</h3><p>Now just double click on the parrot machine in your download folder and you will have a option like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wX40JG75LC4SbyVSlB-Zwg.png" /></figure><p>Just Click on the Finish Button and click on agree and it will get imported.</p><h3>Last Step:</h3><p>Click on the Machine and press the start button and you machine will start working.</p><p>Thanks for your time see you soon.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42be1db940a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Streamlining Workflow: Integrating GUIs with Automation Scripts Part 3]]></title>
            <link>https://medium.com/@mr.stucknet/streamlining-workflow-integrating-guis-with-automation-scripts-part-3-531951c4e741?source=rss-1145b919138------2</link>
            <guid isPermaLink="false">https://medium.com/p/531951c4e741</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[gui-in-python]]></category>
            <category><![CDATA[python-programming]]></category>
            <dc:creator><![CDATA[Mr Stucknet]]></dc:creator>
            <pubDate>Sat, 12 Oct 2024 02:51:41 GMT</pubDate>
            <atom:updated>2024-10-12T02:51:41.901Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*jM32niM7l4_2yeikZgB8rw.jpeg" /></figure><h3>The business logic</h3><p>Now that we’ve seen the scaffolding, let’s dive deep into the actual logic (if it looks intimidating, don’t worry; we’ll go through it together).</p><p>Within the script, this logic lies after the imports and before the parsing (before the if __name__ clause):</p><pre> def scrape(url, format_, type_):<br> try:<br>        page = requests.get(url)<br> except requests.RequestException as err:<br>        print(str(err))<br> else:<br>        soup = BeautifulSoup(page.content, &#39;html.parser&#39;)<br>        images = fetch_images(soup, url)<br>        images = filter_images(images, type_)<br>        save(images, format_)</pre><p>Let’s start with the scrape() function. The first thing it does is fetch the page at the given url argument. Whatever error may happen while doing this, we trap it in RequestException (err) and print it. RequestException is the base exception class for all the exceptions in the requests library.</p><p>However, if things go well, and we have a page back from the GET request, then we can proceed (else branch) and feed its content to the BeautifulSoup parser. The BeautifulSoup library allows us to parse a web page in no time, without having to write all the logic that would be needed to find all the images in a page, which we really don’t want to do. It’s not as easy as it seems, and reinventing the wheel is never good. To fetch images, we use the fetch_images() function and we filter them with filter_images(). Finally, we call save with the result.</p><p>Splitting the code into different functions with meaningful names allows us to read it more easily. Even if you haven’t seen the logic of the fetch_images(), filter_images(), and save() functions, it’s not hard to predict what they do, right? Check out the following:</p><pre>def fetch_images(soup, base_url):<br>    images = []<br> for img in soup.findAll(&#39;img&#39;):<br>        src = img.get(&#39;src&#39;)<br>        img_url = f&#39;{base_url}/{src}&#39;<br>        name = img_url.split(&#39;/&#39;)[-1]<br>        images.append(dict(name=name, url=img_url))<br> return images</pre><p>The Fix spacing fetch_images() function takes a BeautifulSoup object and a base URL. All it does is loop through all of the images found on the page and fill in the name and url information about them in a dictionary (one per image). All dictionaries are added to the images list, which is returned at the end.</p><p>There is some trickery going on when we get the name of an image. We split the img_url (for example, <a href="http://localhost:8000/img/my_image_name.png)">http://localhost:8000/img/my_image_name.png)</a> string using ‘/’ as a separator, and we take the last item as the image name. There is a more robust way of doing this, but for this example it would be overkill. If you want to see the details of each step, try to break this logic down into smaller steps, and print the result of each of them to help yourself understand. You can read about more efficient ways of doing this in Chapter 11, Debugging and Profiling</p><p>For example, if we add print(images) at the end of the fetch_images() function, we get this:</p><pre> [{&#39;url&#39;: &#39;http://localhost:8000/img/owl-alcohol.png&#39;, &#39;name&#39;: &#39;owl<br>alcohol.png&#39;}, {&#39;url&#39;: &#39;http://localhost:8000/img/owl-book.png&#39;, <br>&#39;name&#39;: &#39;owl-book.png&#39;}, ...]</pre><p>We truncated the result for brevity. You can see each dictionary has a url and name key/value pair, which we can use to fetch, identify, and save our images as we like. At this point, something interesting to consider is: what would happen if the images on the page were specified with an absolute path instead of a relative one? The answer is that the script would fail to download them because this logic expects relative paths.</p><p>We hope you find the body of the filter_images() function below interesting. We wanted to show you how to check through multiple extensions using a mapping technique:</p><pre> def filter_images(images, type_):<br>    if type_ == &#39;all&#39;:<br>      return images<br>    ext_map = {<br>      &#39;png&#39;: [&#39;.png&#39;],<br>      &#39;jpg&#39;: [&#39;.jpg&#39;, &#39;.jpeg&#39;],<br>    }<br>    return [<br>      img for img in images<br>      if matches_extension(img[&#39;name&#39;], ext_map[type_])<br>    ]<br> def matches_extension(filename, extension_list):<br>      extension = Path(filename.lower()).suffix<br>      return extension in extension_list</pre><p>In this function, if type_ is all, then no filtering is required, so we just return all the images. On the other hand, when type_ is not all, we get the allowed extensions from the ext_map dictionary, and use it to filter the images in the list comprehension that ends the function body. You can see that by using another helper function, matches_extension(), we have made the list comprehension simpler and more readable.</p><p>All that matches_extension() does is get the extension from the image name and check whether it is within the list of allowed ones.</p><p>In case you’re wondering why we have collected all the images in the images list and then filtered out some of them, instead of checking whether we wanted to save them before adding them to the list, there are three reasons for that. The first reason is that we will need fetch_images() in the GUI application as it is now. The second reason is that combining fetching and filtering would produce a longer and more complicated function, and we are trying to keep the complexity level down. The third reason is that this could be a nice exercise for you to do.</p><p>Let’s keep going through the code and inspect the save() function:</p><pre> def save(images, format_):<br>    if images:<br>        if format_ == &#39;img&#39;:<br>            save_images(images)<br>        else:<br>            save_json(images)<br>        print(&#39;Done&#39;)<br>    else:<br>        print(&#39;No images to save.&#39;)<br> def save_images(images):<br>    for img in images:<br>        img_data = requests.get(img[&#39;url&#39;]).content<br>        with open(img[&#39;name&#39;], &#39;wb&#39;) as f:<br>            f.write(img_data)<br> def save_json(images):<br>    data = {}<br>    for img in images:<br>        img_data = requests.get(img[&#39;url&#39;]).content<br>        b64_img_data = base64.b64encode(img_data)<br>        str_img_data = b64_img_data.decode(&#39;utf-8&#39;)<br>        data[img[&#39;name&#39;]] = str_img_data<br>   with open(&#39;images.json&#39;, &#39;w&#39;) as ijson:<br>        ijson.write(json.dumps(data))</pre><p>You can see that, when images isn’t empty, this acts as a dispatcher. We either call save_images() or save_json(), depending on what information is stored in the format_ variable</p><p>We are almost done. Let’s jump to save_images(). We loop over the images list and for each dictionary we find in it, we perform a GET request on the image URL and save its content in a file, which we name as the image itself.</p><p>Finally, let’s now step into the save_json() function. It’s very similar to the previous one. We basically fill in the data dictionary. The image name is the key, and the Base64 representation of its binary content is the value. When we’re done populating our dictionary, we use the json library to dump it in the images.json file. Here’s a small preview of that:</p><pre> # images.json (truncated)<br> {<br> &quot;owl-alcohol.png&quot;: &quot;iVBORw0KGgoAAAANSUhEUgAAASwAAAEICA...<br> &quot;owl-book.png&quot;: &quot;iVBORw0KGgoAAAANSUhEUgAAASwAAAEbCAYAA...<br> &quot;owl-books.png&quot;: &quot;iVBORw0KGgoAAAANSUhEUgAAASwAAAElCAYA...<br> &quot;owl-ebook.jpg&quot;: &quot;/9j/4AAQSkZJRgABAQEAMQAxAAD/2wBDAAEB...<br> &quot;owl-rose.jpeg&quot;: &quot;/9j/4AAQSkZJRgABAQEANAA0AAD/2wBDAAEB...<br> }</pre><p>And that’s it! Now, before proceeding to the next section, make sure you play with this script and understand how it works. Try to modify something, print out intermediate results, add a new argument or functionality, or scramble the logic. We’re going to migrate it into a GUI application now, which will add a layer of complexity simply because we’ll have to build the GUI interface, so it’s important that you’re well acquainted with the business logic — it will allow you to concentrate on the rest of the code.</p><h3>That’s it for today. See you tomorrow.</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=531951c4e741" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Streamlining Workflow: Integrating GUIs with Automation Scripts Part 2]]></title>
            <link>https://medium.com/@mr.stucknet/streamlining-workflow-integrating-guis-with-automation-scripts-part-2-027311fb681d?source=rss-1145b919138------2</link>
            <guid isPermaLink="false">https://medium.com/p/027311fb681d</guid>
            <category><![CDATA[gui-in-python]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[automation-in-python]]></category>
            <dc:creator><![CDATA[Mr Stucknet]]></dc:creator>
            <pubDate>Sat, 10 Aug 2024 05:37:11 GMT</pubDate>
            <atom:updated>2024-08-10T05:37:11.424Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*m3Lm5mSM4V4G2uE7VnaiVg.jpeg" /></figure><h3>First approach: scripting</h3><p>Now, let’s start writing the script. We’ll go through the source in three steps: imports, argument parsing, and business logic.</p><h3>The imports</h3><p>Here’s how the script starts:</p><pre># scrape.py<br>import argparse<br>import base64<br>import json<br>from pathlib import Path<br>from bs4 import BeautifulSoup<br>import requests</pre><p>Going through the imports from the top, you can see that we’ll need to parse the arguments, which we’ll feed to the script itself (using argparse). We will need the base64 library to save the images within a JSON file (so we will also need json), and we’ll need to open files for writing (using pathlib). Finally, we’ll need BeautifulSoup for scraping the web page easily, and requests to fetch its content. We assume you’re familiar with requests as we used it in series 8, Files and Data Persistence.</p><blockquote>We will explore the HTTP protocol and the requests mechanism<br>in series14, Introduction to API Development, so for now, let’s<br>just (simplistically) say that we perform an HTTP request to fetch<br>the content of a web page. We can do it programmatically using<br>a library, such as requests, and it’s more or less the equivalent<br>of typing a URL in your browser and pressing Enter (the browser<br>then fetches the content of a web page and displays it to you).</blockquote><p>Of all these imports, only the last two don’t belong to the Python standard library, so make sure you have them installed. You can check if that is the case by running the following command:</p><pre>$ pip freeze | egrep -i &quot;soup4|requests&quot;<br>beautifulsoup4==4.9.3<br>requests==2.26.0</pre><blockquote>The above command won’t work on Windows. If that is your<br>operating system, you can use the findstr command, instead<br>of egrep. Alternatively, simply type $ pip freeze, and skim<br>through to get the desired versions.</blockquote><p>As we saw in the previous chapter, JSON is one of the most popular formats for data exchange between applications. It’s widely used for other purposes too, for example, to save data in a file. In our script, we’re going to offer the user the ability to save images as image files, or as a single JSON file. Within the JSON, we’ll put a dictionary with keys as the image names and values as their content. The only issue is that saving images in the binary format is tricky, and this is where the base64 library comes to the rescue.</p><p>The base64 library is quite useful. For example, every time you send an email with an image attached to it, the image gets Base64-encoded before the email is sent. On the recipient end, images are automatically decoded into their original binary format so that the email client can display them.</p><pre>We used Base64 in series 9, Cryptography and Tokens. If you<br>skipped it, this would be a good time to check it out.</pre><h3>Parsing arguments</h3><p>Now that the technicalities are out of the way, let’s see the next section of our script, argument parsing, which should be at the end of the scrape.py module:</p><pre>if __name__ == &quot;__main__&quot;:<br>  parser = argparse.ArgumentParser(<br>    description=&#39;Scrape a webpage.&#39;)<br>  parser.add_argument(<br>    &#39;-t&#39;,<br>    &#39;--type&#39;,<br>    choices=[&#39;all&#39;, &#39;png&#39;, &#39;jpg&#39;],<br>    default=&#39;all&#39;,<br>    help=&#39;The image type we want to scrape.&#39;)<br>  parser.add_argument(<br>    &#39;-f&#39;,<br>    &#39;--format&#39;,<br>    choices=[&#39;img&#39;, &#39;json&#39;],<br>    default=&#39;img&#39;,<br>    help=&#39;The format images are saved to.&#39;)<br>  parser.add_argument(<br>    &#39;url&#39;,<br>    help=&#39;The URL we want to scrape for images.&#39;)<br>  args = parser.parse_args()<br>  scrape(args.url, args.format, args.type)</pre><p>Look at that first line; it is a very common idiom when it comes to scripting. According to the official Python documentation, the ‘__main__’ string is the name of the scope in which top-level code executes. A module’s __name__ attribute is set to ‘__main__’ when read from standard input, a script, or from an interactive prompt.</p><p>Therefore, if you put the execution logic under that if, it will be run only when you run the script directly, as its __name__ will be ‘__main__’ in that case. On the other hand, should you import objects from this module, then its name will be set to something else, so the logic under the if won’t run.</p><p>The first thing we do is define our parser. There are several useful libraries out there nowadays to write parsers for arguments, like Docopt (<a href="https://github.com/docopt/">https://github.com/docopt/</a>docopt) and Click (<a href="https://click.palletsprojects.com/">https://click.palletsprojects.com/</a>), but in this case, we prefer to use the standard library module, argparse, which is simple enough and quite powerful.</p><p>We want to feed our script three different pieces of data: the types of images we want to save, the format in which we want to save them, and the URL of the page to be scraped.</p><p>The types can be PNGs, JPGs, or both (the default option), while the format can be either image or JSON, image being the default. URL is the only mandatory argument.</p><p>So, we add the -t option, allowing also the long version, — type. The choices are ‘all’, ‘png’, and ‘jpg’. We set the default to ‘all’ and we add a help message.</p><p>We do a similar procedure for the format argument, allowing both the short and long syntax (-f and — format), and finally, we add the url argument, which is the only one that is specified differently so that it won’t be treated as an option, but rather as a positional argument.</p><p>In order to parse all the arguments, all we need is parser.parse_args(). Simple, isn’t it?</p><p>The last line is where we trigger the actual logic by calling the scrape() function, passing all the arguments we just parsed. We will see its definition shortly. The nice thing about argparse is that if you call the script by passing -h, it will print some nice usage text for you automatically. Let’s try it out:</p><pre>$ python scrape.py -h<br>usage: scrape.py [-h] [-t {all,png,jpg}] [-f {img,json}] url<br><br>Scrape a webpage.<br><br>positional arguments:<br>url                             The URL we want to scrape for images.<br><br>optional arguments:<br>-h, --help                      show this help message and exit<br>-t {all,png,jpg}, --type {all,png,jpg}    The image type we want to scrape.<br>-f {img,json}, --format {img,json}         The format images are saved to.</pre><p>If you think about it, the one true advantage of this is that we just need to specify the arguments and we don’t have to worry about the usage text, which means we won’t have to keep it in sync with the arguments’ definitions every time we change something. This translates to time saved.</p><p>Here are a few different ways to call the scrape.py script, which demonstrate that type and format are optional, and how you can use the short and long syntaxes to employ them:</p><pre>$ python scrape.py http://localhost:8000<br>$ python scrape.py -t png http://localhost:8000<br>$ python scrape.py --type=jpg -f json http://localhost:8000</pre><p>The first one uses default values for type and format. The second one will save only PNG images, and the third one will save only JPGs, but in JSON format.</p><h3>That’s it for today. See you tomorrow.</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=027311fb681d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Streamlining Workflow: Integrating GUIs with Automation Scripts Part 1]]></title>
            <link>https://medium.com/@mr.stucknet/streamlining-workflow-integrating-guis-with-automation-scripts-part-1-c976fda8645b?source=rss-1145b919138------2</link>
            <guid isPermaLink="false">https://medium.com/p/c976fda8645b</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[gui-in-python]]></category>
            <category><![CDATA[python-automation]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Mr Stucknet]]></dc:creator>
            <pubDate>Tue, 06 Aug 2024 11:39:05 GMT</pubDate>
            <atom:updated>2024-08-06T11:39:05.125Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6cw1uN51TmFHkwbOugUOCA.jpeg" /></figure><h3>Introduction</h3><p>“A user interface is like a joke. If you have to explain it, it’s not that good.”<br>– Martin LeBlanc</p><p>In this series , we’re going to work on a project together. We are going to write a simple scraper that finds and saves images from a web page. We’ll focus on three parts</p><ul><li>A simple HTTP web server in Python<br>•A script that scrapes a given URL<br>•A GUI application that scrapes a given URL</li></ul><blockquote>A graphical user interface (GUI) is a type of interface that allows<br>the user to interact with an electronic device through graphical<br>icons, buttons, and widgets, as opposed to text-based or command-<br>line interfaces, which require commands or text to be typed on<br>the keyboard. In a nutshell, any browser, any office suite such as<br>LibreOffice, and, in general, anything that pops up when you click<br>on an icon, is a GUI application.</blockquote><p>So, if you haven’t already done so, this would be the perfect time to start a console and position yourself in a folder called ch12 in the root of your project for this series. Within that folder, we’ll create two Python modules (scrape.py and guiscrape.py) and a folder (simple_server). Within simple_server, we’ll write our HTML page:<br>index.html. Images will be stored in simple_server/img.</p><p>The structure in ch12 should look like this:</p><pre>$ tree -A<br>.<br>├── guiscrape.py<br>├── scrape.py<br>└── simple_server<br>  ├── img<br>  │ ├── owl-alcohol.png<br>  │ ├── owl-book.png<br>  │ ├── owl-books.png<br>  │ ├── owl-ebook.jpg<br>  │ └── owl-rose.jpeg<br>  ├── index.html<br>  └── serve.sh</pre><p>If you’re using either Linux or macOS, you can put the code to start the HTTP server in a serve.sh file. On Windows, you’ll probably want to use a batch file. The purpose of this file is just to give you an example, but for such a straightforward scenario, you can simply type its content into a console and you’ll be fine.</p><p>The HTML page we’re going to scrape has the following structure:</p><pre># simple_server/index.html<br>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>  &lt;head&gt;&lt;title&gt;Cool Owls!&lt;/title&gt;&lt;/head&gt;<br>  &lt;body&gt;<br>    &lt;h1&gt;Welcome to our owl gallery&lt;/h1&gt;<br>    &lt;div&gt;<br>      &lt;img src=&quot;img/owl-alcohol.png&quot; height=&quot;128&quot; /&gt;<br>      &lt;img src=&quot;img/owl-book.png&quot; height=&quot;128&quot; /&gt;<br>      &lt;img src=&quot;img/owl-books.png&quot; height=&quot;128&quot; /&gt;<br>      &lt;img src=&quot;img/owl-ebook.jpg&quot; height=&quot;128&quot; /&gt;<br>      &lt;img src=&quot;img/owl-rose.jpeg&quot; height=&quot;128&quot; /&gt;<br>    &lt;/div&gt;<br>    &lt;p&gt;Do you like these owls?&lt;/p&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>It’s a simple page, so let’s just note that we have five images, three of which are PNGs and two of which are JPGs (note that even though they are both JPGs, one ends with .jpg and the other with .jpeg, which are both valid extensions for this format).</p><p>Python gives you a rudimentary HTTP server that can serve static pages. You can start it with the following command (make sure you run it from the simple_server folder):</p><pre>$ python -m http.server 8000<br>Serving HTTP on :: port 8000 (http://[::]:8000/) ...<br>::1 - - [04/Sep/2021 10:40:07] &quot;GET / HTTP/1.1&quot; 200 -<br>...<br>The last</pre><p>The last line is the log you get when you access <a href="http://localhost:8000/">http://localhost:8000</a>, where the owl page will be served. Alternatively, you can put that command in a file called serve.sh, and just run it (make sure it’s executable):</p><pre>$ ./serve.sh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/1*tK0MZU3_RH1mWWZnT5N43g.png" /></figure><p>if you wish to compare the source HTML with that in the page, you can right-click on the page and your browser should offer you an option like View Page Source (or something similar).</p><p>Feel free to use any other set of images, as long as you use at least one PNG and one JPG, and in the src tag, you use relative paths, not absolute ones. We got these lovely owls from <a href="https://openclipart.org/">https://openclipart.org/</a>.</p><h3>That’s it for today. See you tomorrow.</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c976fda8645b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Python Debugging Techniques Part 8 [The End]]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/pythons-gurus/mastering-python-debugging-techniques-part-8-the-end-8d3afc1ac532?source=rss-1145b919138------2"><img src="https://cdn-images-1.medium.com/max/1120/1*05obtnROjFkLHu73vC9Xvg.jpeg" width="1120"></a></p><p class="medium-feed-snippet">Measuring execution time</p><p class="medium-feed-link"><a href="https://medium.com/pythons-gurus/mastering-python-debugging-techniques-part-8-the-end-8d3afc1ac532?source=rss-1145b919138------2">Continue reading on Python’s Gurus »</a></p></div>]]></description>
            <link>https://medium.com/pythons-gurus/mastering-python-debugging-techniques-part-8-the-end-8d3afc1ac532?source=rss-1145b919138------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d3afc1ac532</guid>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[debugging-in-python]]></category>
            <dc:creator><![CDATA[Mr Stucknet]]></dc:creator>
            <pubDate>Fri, 02 Aug 2024 08:22:21 GMT</pubDate>
            <atom:updated>2024-08-14T03:29:39.469Z</atom:updated>
        </item>
    </channel>
</rss>