<?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 Luis Costa Brochado on Medium]]></title>
        <description><![CDATA[Stories by Luis Costa Brochado on Medium]]></description>
        <link>https://medium.com/@lfilipecosta3?source=rss-f663aa265e85------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dT9Rj_D7dA8NjlV7QKihjg.jpeg</url>
            <title>Stories by Luis Costa Brochado on Medium</title>
            <link>https://medium.com/@lfilipecosta3?source=rss-f663aa265e85------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 22:50:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lfilipecosta3/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[C# — Converting a resume to HTML with Blazor WASM and ChatGPT — pt. 2]]></title>
            <link>https://medium.com/@lfilipecosta3/c-converting-a-resume-to-html-with-blazor-wasm-and-chatgpt-pt-2-bdf7d0ea36e9?source=rss-f663aa265e85------2</link>
            <guid isPermaLink="false">https://medium.com/p/bdf7d0ea36e9</guid>
            <category><![CDATA[blazor]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[design]]></category>
            <dc:creator><![CDATA[Luis Costa Brochado]]></dc:creator>
            <pubDate>Mon, 08 Apr 2024 21:12:06 GMT</pubDate>
            <atom:updated>2024-04-09T18:49:04.245Z</atom:updated>
            <content:encoded><![CDATA[<h3>C# — Converting a resume to HTML with Blazor WASM and ChatGPT — pt. 2</h3><h4>Following up part one by adjusting mobile rendering and refactoring smelly code</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8OBJ1hw9spSOqpm6" /><figcaption>Photo by <a href="https://unsplash.com/@yiranding?utm_source=medium&amp;utm_medium=referral">Yiran Ding</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Table of contents</h3><ol><li><a href="#7aa1"><strong>Goal</strong></a><br>— <a href="#f6cb">The result</a><br>— <a href="#a948">The starting point</a></li><li><a href="#0a0d"><strong>The Strategy</strong></a><br>— <a href="#d7cd">Header section</a><br>— <a href="#acd5">Main Container</a><br>— <a href="#379c">Left Column</a><br>— <a href="#6ce6">Fine tuning ‘Academic Projects’</a><br>— <a href="#6919">The ‘Hobbies’ challenge</a><br>— <a href="#5880">Right Column</a></li><li><a href="#c9f7"><strong>Applying formats to all other widths</strong></a><br>— <a href="#f2b5">Defining a threshold</a><br>— <a href="#b006">The loading animation</a></li><li><a href="#46ea"><strong>Critical Points</strong></a><br>— <a href="#14c5">Loading animations</a><br>— <a href="#9574">Lower dimensions</a><br>— <a href="#1831">Hot Reload</a><br>— <a href="#b99e">Lacking context and bad habits</a></li></ol><h3>1. Goal</h3><p>This story rolls out in a somewhat unusual way. The end result will be presented first, and the starting point right after that.</p><p>My intention is to provoke a bit of a visual impact, and clearly state that the goal was achieved, to only then explain the reason why I decided to start this set of improvements for mobile.</p><p>After that, this post delves into the process of de-constructing scattered HTML and CSS. In that stage, there are a couple funny episodes along with my strategies to overcome rendering conflicts, as I go through good practices and pain points.</p><p>I’ll finish by providing a serious reflection, exposing critical points one by one, and leaving my suggestions for yourself and wise advice for my future-self.</p><h4>— The result</h4><p>This is the result of all the work:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/1*TmVwO7eOFmIn1Owl7oxyrw.png" /><figcaption>The goal</figcaption></figure><h4>— The starting point</h4><p>To put ‘visual impact’ into perspective, here’s what I was working with, starting this saga:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/1*Dopo1MOZ3UgbIx5cZNYqyw.png" /><figcaption>The starting point</figcaption></figure><p>Confusing. To say the least.</p><h3>2. The strategy</h3><p>To keep the refactoring simple, and since the last strategy was fruitful, the ‘<a href="https://medium.com/@lfilipecosta3/c-convert-your-resume-to-html-with-blazor-wasm-and-chatgpt-cb32c2eb5115#494c">Breaking it down</a>’ approach from the previous post was repeated.</p><p>The process was straight forward: <strong><em>Start from top to bottom</em></strong>, <strong><em>left to right</em></strong>, and <strong><em>lower to higher dimensions</em></strong>, gradually cleaning it up. Decided I’d start at about <strong>375px</strong> maximum width, minimum width <strong>unrestricted</strong>.</p><p>From there, I’d define sets of dimensions and steadily climb my way up into other dimensions, until all were finished.</p><h4>— Header section</h4><p>Getting the header sorted took about half an hour. Zero help from <strong>ChatGPT</strong>. It involved adjustments to my profile picture’s size and positioning by bringing it to the center top, while decreasing the size of the text.</p><p>The transparent horizontal bar hanged on pretty well by itself, and I was happy to get a feel for where it was going:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/337/1*6MhmOzkDmbCAitL_QAmzJQ.png" /><figcaption>The first adjustments to the header</figcaption></figure><h4>— Main Container</h4><p>The next adjustments were made to the main container and its background. Padding, along with content organization — which relied on the division of the main container into two side-by-side vertical slices — made no more sense for mobile.</p><p>Here, I decided to reduce padding and invert the content dynamic, maintaining the naming of the left and right columns, and swapping the <strong>width</strong> and <strong>height</strong> values from width: 50% and height: 100% to:</p><pre>.left-column,<br>.right-column {    <br>    [...]<br>    width: 100%;<br>    height: 50%;<br>}</pre><p>This action stretched the contents of the page by pushing the right column below the left column, leaving it up to me to slim down and adjust content.</p><p>Me and the previously added white triangles had a disagreement in terms of position. One of them can be seen in action in the following image:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*__1hWSWBh1kg7dA2v7GUtg.png" /><figcaption>The triangle, below the header, on the top right corner</figcaption></figure><p>This was one of those seemingly ‘brilliant’ ideas that backfired on me.</p><p>In spite of attempting to position them in a specific corner, pointing at a specific angle, the ‘creative’ triangles had a will of their own. A frustrating experience.</p><p>Eventually, they ended up being merged together, becoming this rather exquisite up arrow in the intersection between the two vertical columns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*Kc-PB-mrHFrwnD15oqSmGA.png" /><figcaption>HTML and CSS, a world where ⵠ + ⵠ = 𐌣</figcaption></figure><h4>— Left Column</h4><p>With the previous adjustments, almost all the content had was close to the goal, prevailing the headings and their siblings’ sizes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*R6MxMe3szf1t1Irl6HgIwg.png" /><figcaption>Main container with default element formats</figcaption></figure><p>Fast forwarding, after a couple hours of boring adjustments to the size of the text, I was done with most text content. Here’s how the first touches looked:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/333/1*3_SPmTIjyWmYRGIF-TrgXQ.png" /><figcaption>Main container content</figcaption></figure><p>One of the main challenges was the ‘<strong>Academic projects</strong>’ section, since it had a mix of all the elements previously used on the ‘<strong>Contact</strong>’, ‘<strong>Languages</strong>’, and ‘<strong>Education</strong>’ sections.</p><h4>— Fine tuning ‘Academic Projects’</h4><p>The solution involved joining multiple CSS classes together, until eventually the right proportions were applied exclusively for these elements without interfering with similar elements on previous sections.</p><p>Here’s an example of that, as made here with .left-column, .framework-container and .frameworks, where each successive element was hierarchically contained within the previous one:</p><pre>.left-column .framework-container .frameworks {<br>    display: flex;<br>    margin-top: 10px;<br>    margin-bottom: 10px;<br>    margin-right: 5%;<br>}</pre><p>Once things came closer to what I pictured, and decent enough in terms of content disposition, I called it a day.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*w4Ra36Y7AK4IDgBjCq3dvg.png" /><figcaption>Main container — left column</figcaption></figure><p>Up next’s the ‘<strong>Hobbies’ </strong>section, which was probably the biggest challenge.</p><h4>— The ‘Hobbies’ challenge</h4><p>There are four images in the base HTML, each of which had been <strong>meticulously adjusted so that they could all fit </strong>together:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/919/1*-RbVJIYYCydSypRVYIUjIg.png" /><figcaption>The hobbies section</figcaption></figure><p>Now, I wanted to transform a row of four images into a two by two matrix, striking a balance between size and perception, while fitting on the remaining space. Nothing too fancy, but I admit it was a struggle.</p><p>After a lot of attempts, I wasn’t able to place the images above one another. So, I resorted to <strong>ChatGPT.</strong> It gave me this tip:</p><pre>@media screen and (max-width: 767px) { /* Adjust max-width as needed */<br>    .hobbies-container {<br>        display: flex;<br>        flex-wrap: wrap;<br>        justify-content: space-between;<br>    }<br><br>    .hobbies-container img {<br>        width: 48%; /* Adjust width as needed */<br>        margin-bottom: 20px; /* Adjust margin as needed */<br>    }<br>}</pre><p>Most of the heavy lifting is made by the flex-wrap: wrap;. Simply put, the <em>wrap </em>folds elements on the container below one another if they don’t fit the designated width. Something I tested myself at <a href="https://www.w3schools.com/cssref/playdemo.php?filename=playcss_flex-wrap&amp;preval=wrap">W3Schools</a> and worked fine.</p><p>After going in circles for a while, being unable to move or resize a single image, something in the HTML caught my attention. There was code I shouldn’t have previously left unattended.</p><pre>[...]<br>  &lt;h1&gt;Hobbies&lt;/h1&gt;<br>  &lt;div class=&quot;hobbies&quot;&gt; <br>      &lt;div class=&quot;hobbies-container&quot;&gt;<br>        &lt;img src=&quot;/curriculum-vitae/hobbies/gym.png&quot; alt=&quot;Gym&quot; style=&quot;width: 19%; height: 19%; margin-top: 6%; margin-left: 4%;&quot; /&gt;<br>        &lt;img src=&quot;/curriculum-vitae/hobbies/karate.png&quot; alt=&quot;Karate&quot; style=&quot;width: 19%; height: 19%; margin-top: 3%; margin-left: 5%;&quot; /&gt;<br>        &lt;img src=&quot;/curriculum-vitae/hobbies/reading.png&quot; alt=&quot;Reading&quot; style=&quot;width: 14%; height: 14%; margin-top: 7%; margin-left: 7%;&quot; /&gt;<br>        &lt;img src=&quot;/curriculum-vitae/hobbies/skateboarding.png&quot; alt=&quot;Skateboarding&quot; style=&quot;width: 16%; height: 16%; margin-top: 5%; margin-left: 7%&quot; /&gt;<br>      &lt;/div&gt;<br>  &lt;/div&gt;<br>[...]</pre><p>It’s most likely not noticeable at a glance, but I had resorted to <em>inline styling </em>back at the beginning of this project:</p><ul><li><strong>[...] style=&quot;width: 19%; height: 19% [...]</strong></li></ul><p>According to people who know more about front-end than me, this is impractical since it can turn into a problem later on. From my personal experience, it’s particularly painful when you have a lot of freehand CSS, and end up forgetting about the code for a while, only to come back to it months later.</p><p>Furthermore, using inline styles defeats the purpose of CSS, and it doesn’t enforce ‘SOLID’ practices. Regular CSS classes are the best approach for dealing with custom styling. Added to that there’s a lot more reasons why you shouldn’t do <em>it, </em>most of which are outlined in this <a href="https://stackoverflow.com/questions/15170261/why-is-using-the-style-attribute-in-html-bad"><em>stackoverflow</em> reply</a>.</p><p>To fix it, as previously stated, classes and width oriented specifications was the way to go:</p><pre>&lt;div class=&quot;hobbies&quot;&gt; <br>    &lt;div class=&quot;hobbies-container&quot;&gt;<br>        &lt;img class=&quot;gym&quot; src=&quot;/curriculum-vitae/hobbies/gym.png&quot; alt=&quot;Gym&quot; /&gt;<br>        &lt;img class=&quot;karate&quot; src=&quot;/curriculum-vitae/hobbies/karate.png&quot; alt=&quot;Karate&quot; /&gt;<br>        &lt;img class=&quot;reading&quot; src=&quot;/curriculum-vitae/hobbies/reading.png&quot; alt=&quot;Reading&quot; /&gt;<br>        &lt;img class=&quot;skateboarding&quot; src=&quot;/curriculum-vitae/hobbies/skateboarding.png&quot; alt=&quot;Skateboarding&quot; /&gt;<br>    &lt;/div&gt;<br>&lt;/div&gt;</pre><p>Eventually, with some adjustments, I minted the icons to match my desire. After all, they did become a matrix:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*IphOXkerld5fxSLfuFED4A.png" /><figcaption>The Hobbies matrix</figcaption></figure><h4><strong>— Right Column</strong></h4><p>For the second column, virtually no further adjustments to the CSS were required. The sections were aligned with what had already been done for the left column:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*urvWA0JDLqucJOKLMjt5ig.png" /><figcaption>Main container — right column</figcaption></figure><p>To be fair, most of the torment had already been overcome on the previous column and, for the final touch, the MudBlazor icons fitted perfectly in the end of the section.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*9__Jc68V1T9U2htvFZ9bFA.png" /><figcaption>Final section and ending</figcaption></figure><h3>3. Applying formats to all other widths</h3><p>To apply the formats, I required a formatting threshold. Here, I’d draw the line up until which the shape of the header would be kept, and the organization of the main container stretched.</p><p>From that point on, up until the maximum possible width, the header profile image would drop back to the horizontal transparent bar, and the main container would be laid out into two vertical parallel sections.</p><h4>— Defining a threshold</h4><p>Personally, I enjoyed how the mobile layout turned out and the threshold was defined at <strong>1368px</strong>, so that it could also include every other tablet model. Up until that width, I applied pretty much the same formatting to all sets of dimensions, slightly increasing the text and other elements’ sizes along the way.</p><p>Fortunately, it didn’t require as many adjustments as I imagined. Just a few key changes in the <strong>.framework-circle-container </strong>here, and a couple adjustments to circle sizes there. Adjustments which I ended up applying at a global level.</p><ul><li><strong>Changing elements width;</strong></li></ul><p>The .<strong>framework-circle-container</strong> elements were not flexible as device width dynamically expanded or contracted. Clearly a static width configuration issue.</p><p>To be able to see the bits and pieces dynamically adjusting, their width had to be flexible. Here’s an example of what I did for the <strong>.left-column</strong>:</p><pre>.left-column .framework-circle-container {<br>    display: flex;<br>    justify-content: right;<br>    align-items: center;<br>    width: calc(25vw + 15px);<br>    [...]<br>}</pre><p>The position is based on <strong><em>v</em></strong><em>iewport </em><strong><em>w</em></strong><em>idth</em>, to which I’m adding <strong>15px</strong>, just for the sake of it.</p><ul><li><strong>Removing the code smells</strong></li></ul><p>At first sight, this seems quite normal and, if anyone had asked me, I’d say there’s nothing wrong on the following snippet:</p><pre>[...]<br>  &lt;div class=&quot;framework-circle-container&quot;&gt;<br>    &lt;h4 class=&quot;circle-filled&quot;&gt;&lt;/h4&gt;<br>    &lt;h4 class=&quot;circle-filled&quot;&gt;&lt;/h4&gt;<br>    &lt;h4 class=&quot;circle-empty&quot;&gt;&lt;/h4&gt;<br>    &lt;h4 class=&quot;circle-empty&quot;&gt;&lt;/h4&gt;<br>    &lt;h4 class=&quot;circle-empty&quot;&gt;&lt;/h4&gt;<br>  &lt;/div&gt;<br>[...]</pre><p>Except there’s a catch, of course. I was using headings with no content. No text at all. The (free) SonarCloud GitHub add-on caught all the 210 occurrences. Looking at the suggestions, I realized that using heading with no text makes no sense. It’s more than appropriate to use a &lt;div&gt; for that. So the headings were replaced, and soon the project was back at zero code smells.</p><h4>— The loading animation</h4><p>To finish my changes, the last one was made to the loading animation. I ended up using a custom made loading screen, seen on the left, instead of the old one, seen on the right. Simple, no text, round edges, and slightly thicker width.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*Af1WFvGsAXpuPYTLLAXZIg.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*r9aDxKUT370DiRoWEEQfxQ.gif" /><figcaption>The loading animations</figcaption></figure><h3>4. Critical points</h3><h4><strong>Loading animations</strong></h4><p>I’m a <strong>Blazor</strong> militant and, even though my experience is limited to static webpages, I feel that its team brought out (and keeps bringing) really cool stuff.</p><p>With that said, I have compliments for the most part, but not for the loading animation. To be more specific, I dislike the text, and the lack of animation alternatives.</p><p>Loading animations are universal and adding text to seems pointless. Still, I can see it adding value for file processing or similar use cases. I guess it’s one of those things:</p><blockquote>Better have it and not need it, than need it and not have it.</blockquote><p>Bootstrap provides the option of choosing a progress bar in detriment of the circle. Other than that, you’re on your own. If you want to do anything fancy, be ready to do it all by yourself with custom CSS.</p><h4><strong>Lower dimensions</strong></h4><p>My smartphone was probably the main catalyst of this post. It doesn’t seem to be amongst the list of most common devices due to its small screen size. Because of that, a good amount of webpages I view on it aren’t rendered correctly.</p><p>This is why I decided to start from the lowest dimensions, going up. My rationale was:</p><blockquote>If it works on my smartphone, then it should do just fine on other ones.</blockquote><h4><strong>Hot reload</strong></h4><p>It’s becoming impossible to ignore. I find it terrible that neither .NET’s Hot Reload, nor dotnet watch run work as expected. For me, as a VS Code and C# Dev Kit user, having to recompile the project each time an adjustment is made’s a rather unpleasant experience. Worst than that, it’s a productivity killer.</p><p>There are frequent complaints about <em>it</em> <a href="https://www.reddit.com/r/Blazor/comments/1btsmzc/dev_experience/">on Reddit</a>. It becomes even more frustrating because <strong>Blazor</strong> is so good on so much stuff, but miles away from every other JavaScript framework on this.</p><p>Still, <a href="https://youtu.be/dOCSc37zdos?t=3415">according to the team working in .NET 9</a>, Hot Reload is being rebuilt and this will improve soon. I’ll be waiting.</p><h4><strong>Lacking context</strong> <strong>and bad habits</strong></h4><p>More than once, using AI tools with inappropriate context will lead to deceptive output. If you trust AI without due diligence, or skipping fact checking, then you may be putting yourself and others at risk.</p><p>Amongst things AI tools rarely consider by default, are good practices. And this is directly related to contextual awareness. Which is why I chose to join these two points under the same argument. If you build upon a defective foundation, then you’re risking imminent collapse.</p><p>I’d advise to prompt carefully for information or ideas, and always – I repeat always – deliberate.</p><p>If there’s a best practice for something, then the same AI tool you are using is most likely also aware of — and can easily outline — it.</p><p>To finish, I sincerely hope you can take something out of this post. I definitely did. You can check the resume at:</p><blockquote><a href="https://olevezinho.github.io/curriculum-vitae/">https://olevezinho.github.io/curriculum-vitae/</a></blockquote><p>As I made with the blog post preceding the current one I’ll link my ‘conversation’ with ChatGPT, in case you’re interested.</p><p><a href="https://chat.openai.com/share/f599dd2c-8994-46a9-ba92-e0b448c4f715">ChatGPT</a></p><p>Don’t forget to follow me and leave some claps if you fancied the post!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bdf7d0ea36e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[C# – Static analysis with NDepend]]></title>
            <link>https://medium.com/@lfilipecosta3/c-static-analysis-with-ndepend-32207d82ffe1?source=rss-f663aa265e85------2</link>
            <guid isPermaLink="false">https://medium.com/p/32207d82ffe1</guid>
            <category><![CDATA[ndepend]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[static-code-analysis]]></category>
            <dc:creator><![CDATA[Luis Costa Brochado]]></dc:creator>
            <pubDate>Mon, 23 Oct 2023 09:32:35 GMT</pubDate>
            <atom:updated>2023-10-29T18:10:55.206Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to use NDepend for static analysis on Windows and Cross-platform</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*6_gnWH41y1fVlKJgBb9LWQ.png" /></figure><h4>Table of contents</h4><ol><li><a href="#9315"><strong>ASATs Historical overview</strong></a><strong><br></strong>— <a href="#794f">Static analysis purpose</a><strong><br></strong> — <a href="#ccb7">Summary</a></li><li><a href="#4f18"><strong>NDepend — Windows platform</strong></a><strong><br></strong>— <a href="#7371">Overview of the Visual Studio plug-in</a><br>— <a href="#d3ae">Blazor WASM Webpage overview</a><br>— <a href="#014a">Apply a fix and view the results</a><br> — <a href="#d88d">Web API overview</a><br> — <a href="#9200">Code Coverage</a></li><li><a href="#8c53"><strong>NDepend — Linux and OSX</strong></a><strong><br></strong> — <a href="#0344">Overview of the CLI</a><br>—<a href="#08a8">NDepend API</a></li><li><a href="#8210"><strong>Critical Points</strong></a></li></ol><h3>1. ASATs Historical overview</h3><p>Over the last couple decades, ASATs (Automated Static Analysis Tools) consolidated their position in software development.</p><p>Static analysis rode a roller coaster in popularity terms. Historically, as <a href="https://blog.ndepend.com/static-code-analysis-changed-years/">Erik Dietrich described</a>, these tools haven’t always been given the credit they’re receiving nowadays.</p><p>The first couple generations of static analysis were very rudimentary. With time, the software development industry matured. This software evolved, and became a popular build tool again. Ultimately its utility and compatibility with both extreme programming practices and rising adoption of Agile methodologies were the main catalysts in terms of growth in adoption. <a href="https://owasp.org/www-pdf-archive/OWASP-AppSecEU08-Lebanidze.pdf">Fourth generation static analysis</a> proves <em>it</em>.</p><h4>— Static analysis purpose</h4><p>Overall, the <strong>fundamental goal of ASATs</strong> is to <strong>build trust in the code produced. </strong>Take, as an example, a strategy which consists in applying all suggestions static analysis reports, regardless of the development context. Most likely it won’t work. <strong>Implementing a strategy like <em>that</em> might even increase your technical debt. And that is probably the fundamental drawback.</strong> Telling someone ‘<em>you should use ASATs like x, y, and z</em>’ can be misleading. Be cautious. Let me put it this way:</p><blockquote>How serious are compilation warnings during a project build? What’s the importance of addressing technical debt right now? What’s your code coverage?</blockquote><p>Every case is unique. These are some of the factors to weigh in before report analysis:</p><ul><li>Development guidelines;</li><li>Prioritization;</li><li>Team/Company policy;</li><li>Context;</li><li>Timing;</li></ul><h4>— Summary</h4><p>There has been an ongoing discussion around ASATs lack of context for quite some time now, as reported by <a href="https://ieeexplore.ieee.org/document/8330195">a few authors</a>. Other important points, as a few <a href="https://arxiv.org/abs/2111.09188">other authors noticed</a>, include methods to improve identifying and handling <strong>false positives</strong>. These alarms make you think they’re important, when in reality they aren’t, hindering one’s ability to<strong><em> ‘</em></strong><em>separate the wheat from the chaff’.</em></p><p>When I use ASATs, I have three simple rules: (<strong>1) Cover at least 80% of my code with tests</strong> (90% if I’m feeling ambitious), <strong>(2) Don’t exclude any files unless absolutely necessary</strong>, <strong>(3) Ignore only the warnings that I know are wrong</strong> (and document why).</p><h3>2. NDepend — Windows platform</h3><p>In Windows development environments, the strongest points of NDepend are the <strong>Visual Studio’s IDE</strong> plug-in and <strong>PowerTools</strong>.</p><p>The plug-in produces a wide variety of architectural diagrams, such as <strong>Dependency graphs</strong>, <strong>Dependency Matrixes</strong>, <strong>Coupling diagrams</strong>, <strong>Code Metrics</strong> diagrams, and <strong>Class Inheritance</strong> diagrams. <strong>PowerTools </strong>are a set of short open-source static analyzers based on <em>NDepend.API, </em>which<em> </em>have a wide variety of features, from <strong>search for duplicate code</strong>, <strong>facilities for code review</strong>, and <strong>public API breaking changes detection</strong>.</p><p>To provide an overview of what can be done inside the IDE, I’ll go through the creation of some of the most common diagrams, and provide practical examples of how to handle errors, and what can be done code coverage-wise.</p><h4>— Overview of the Visual Studio plug-in</h4><p>The plug-in was tested taking two of my own code repos, a <a href="https://github.com/olevezinho/akaratefelgueiras">Blazor WASM webpage,</a> and a <a href="https://github.com/olevezinho/MySimpleCalculator">Web API</a>.</p><p>The webpage is uncovered by tests and untested against ASATs. Conversely, the Web API contains unit and integration tests, and has previously been tested against a couple different ASAT providers.</p><h4><strong>— Blazor WASM Webpage overview</strong></h4><ul><li><strong>Dependency graphs</strong></li></ul><p>Much like a detective inspecting a crime scene, I found myself facing the <a href="https://www.ndepend.com/docs/project-dependency-diagram">Dependency Graph</a> below where, on the <em>Program</em> box, a potential issue popped right out — an<em> ‘{</em><strong><em>anonymous namespace</em></strong><em>}’. </em>Additionally, the Program.cs class was also flagged on the report as being a potential security risk.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*kDOruw25_KaoJuk02_H5jw.png" /><figcaption>Dependency graph view</figcaption></figure><p>The CLR is not smart enough to see that there are no entrypoints, other than <em>Program.cs</em>. Meaning that <em>it</em> will assume this class is either going to be discoverable via reflection, or that there are classes inheriting from it. That’s considered a code smell since it adds a performance overhead.</p><p>Inside the IDE, you’ll be presented with a lot of information. Something similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*auHW061FjdjsmuFZ9F4DJA.png" /><figcaption>Dependency graph view — expanded</figcaption></figure><p>Popups contain links to<em> </em>bits of documentation about graphs creation, such as <strong>class inheritance graphs</strong> or <strong>coupling graphs.</strong></p><h4><strong>— Apply a fix and view the results</strong></h4><p>To provide an example of NDepend’s utility, I set out to add the <em>Program.cs</em> class to a namespace and seal <em>it</em>, on lines <strong>5</strong> and <strong>7</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d7b071577f67dd64882e7b4e6ff3a49/href">https://medium.com/media/4d7b071577f67dd64882e7b4e6ff3a49/href</a></iframe><p>After that, the report was re-generated, and the results re-assessed. With the <strong>dependency graph</strong> rebuilt, the result displayed a better outcome:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/791/1*XRGk9ecZqbFuS-eJSV6lQw.png" /><figcaption>Dependency graph view — after adding Program.cs to a namespace</figcaption></figure><p>Much more well organized. The expanded namespace reveals <em>Program.cs </em>moved to the appropriate place.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*b5ZN64IWCH_2JYw6bc8MgA.png" /><figcaption>Dependency graph view — expanded namespace</figcaption></figure><p>With these diagrams, you have the ability to zoom in and out in scope. It’s helpful in micro-services where bounded contexts should be well defined. Working with large monoliths though, diagrams like the one above will be tangled and, as dependencies increase, they’ll eventually grow too complex to be considered readable.</p><p><em>That </em>doesn’t mean dependency graphs aren’t useful on monoliths, rather that <em>these</em> should be coupled with the <strong>dependency matrix</strong> for more effectiveness.</p><h4>— Web API Overview</h4><p>The upcoming solution uses a custom NuGet Package. It’s a showcase C# project. This is <em>its </em>dependency graph:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/957/1*TpMhvJ_oQ3UwUiETmKXWZA.png" /><figcaption>Dependency graph view — Modularity</figcaption></figure><p>There’s a module — <em>MySimpleCalculator, </em>on the right — which exposes simple mathematical operations, and a Web API — <em>MySimpleCalculatorWebAPI</em>, on the left <em>— </em>using <em>it</em>.</p><ul><li><strong>Code metrics Heat map</strong></li></ul><p><a href="https://www.ndepend.com/docs/treemap-visualization-of-code-metrics">Code metrics</a> provides feedback in form of Heat Maps. Information such as coverage percentage, numbers of methods, can be visually represented in ‘’temperature’.</p><p>These metrics can be configured between combinations of different <strong>Levels </strong>and <strong>Size</strong>s. In the example below, the heat map is organized at the method level, comparing methods’ sizes in terms of lines of code, while the squares are colored depending on the percentage of code covered for each method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uU2ixRsJTdiCl0jea7VlTg.png" /><figcaption>Code Metrics — Heat map</figcaption></figure><p>If the <strong>Size</strong> or <strong>Level</strong> metrics are changed, the colors can also be adjusted accordingly. After adjusting the color scheme to organize it by IL Instructions, the color of the ConfigureServices() method subtly changed to orange:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TMb6x1OU6bcMsCwXD92Rew.png" /><figcaption>Code Metrics — Heat map</figcaption></figure><ul><li><strong>Dependency Structure Matrix (DSM)</strong></li></ul><p>As previously mentioned, <a href="https://www.ndepend.com/docs/dependency-structure-matrix-dsm">DSMs</a> are very powerful. <em>They</em> act as gateways to other insights. Through them, other diagrams such as <strong>component analysis </strong>graphs, <strong>coupling </strong>graphs, <strong>inheritance </strong>diagrams, and much more, are made available. Here’s an example of a matrix view:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wCoVV3pFN3T0x_eOaYS7Qg.png" /><figcaption>Matrix view</figcaption></figure><p>The green and blue squares represent dependencies. There are also mutual dependencies (not seen above), which are represented by black squares. Numbers inside the blue and green squares represent the amount of times a dependency is verified between two components.</p><p>Expanding the <em>MySimpleCalculatorWebAPI </em>and <em>MySimpleCalculator </em>namespaces, a symmetric matrix is made evident. This symmetry is a two way visualization. The green dots represent a column’s dependency on a given row while the blue dot, it’s counterpart, represents the row’s dependency on a given column:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/1*n0rTylO_CQeSp8K8QGW5YQ.png" /></figure><p>Even though <em>it</em> is less intuitive than the dependency graph, there are two key benefits in favor of DSMs:</p><ul><li>Scales well and, as applications grow, becomes more and more useful;</li><li>Very good at pinpointing non-obvious patterns, such as cycles, layered code, and popular elements;</li></ul><p>Even though the learning curve is steeper, investing some time into learning <em>it</em> and strategizing an approach towards improvements, will definitely be worth your time. Clicking in any of the “dependency” squares in the matrix, as seen below, leads to a dependency graph:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*Sc5AZqwIOyz66_d3Cepryg.png" /><figcaption>Dependency Matrix</figcaption></figure><p>Then, the respective dependency graph provides the option to build a more accurate graph of the code elements involved:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1ojED6epuoRhiMV_8spC8g.png" /><figcaption>Dependency view</figcaption></figure><p>From there on, its easy to generate a variety of graphs, such as:</p><ul><li><strong>Class inheritance graphs</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/769/1*GQqm-U9Zm-nEjEASSOg9Pw.png" /></figure><ul><li><strong>Coupling graphs (WebApp and module):</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*7lLNVGF7lhG_7TzaPt9oPQ.png" /></figure><ul><li><strong>Coupling graphs (module and primitive types):</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/476/1*WE7mU2mEsmcP5l_UnLmuMg.png" /></figure><h4>— Code Coverage</h4><p>In every ASAT, having the ability to gather and present code coverage details can be determining.</p><ul><li><strong>Main Dashboard</strong></li></ul><p>Main dashboard provides crucial information for code coverage purposes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OLvkONBu0TroKQAerh3k0A.png" /><figcaption>Main dashboard — code coverage</figcaption></figure><p>When analyzing coverage, notice how evidently the <em>Search Results </em>present all information. Additionally, it’s possible to select which namespaces are ‘<em>Not covered</em>’, or ‘<em>Full covered</em>’. With a quick analysis it becomes obvious that <em>CalculatorEx.cs</em> and <em>Program.cs </em>aren’t covered by tests<em>:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*LfxS6gFoYvyS8xQPiuezzw.png" /><figcaption>Search results — Search Method by Coverage</figcaption></figure><p>Pressing the <em>‘+’</em> sign enables the user to view the constructed <a href="https://learn.microsoft.com/en-us/dotnet/csharp/linq/"><em>LINQ</em></a> query that brought up a set of results. For instance, the following search displays <strong>namespace</strong> by <strong>coverage</strong> percentage:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*sqomwqgAvrV_YmZNc_MAlg.png" /><figcaption>Search results — Search Namespace by Coverage</figcaption></figure><p>This is the <em>LINQ</em> query that provided the code results displayed above:</p><pre>from n in Application.Namespaces <br>where n.PercentageCoverage &gt;= 0 <br>orderby n.PercentageCoverage descending, n.NbLinesOfCodeCovered, n.NbLinesOfCodeNotCovered, n.NbLinesOfCode <br>select new { n, n.PercentageCoverage, n.NbLinesOfCodeCovered, n.NbLinesOfCodeNotCovered, n.NbLinesOfCode }</pre><p>Say, if the goal is to view contained namespaces with coverage lower than 80%, by adjusting the where clause’s n.PercentageCoverage to <strong>lower than or equal to 80</strong>, the results will filter by that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*bOHgYX-5uK_lY1hhtCzAog.png" /><figcaption>Queries and Rules Edit — namespaces matched</figcaption></figure><p>Whatever the criteria, <em>it</em> can be manipulated in <em>LINQ.</em></p><h3>3. NDepend — Linux and OSX</h3><p>Given the investments made in the IDE, it’s natural to think that cross-platform would have gone the other way around. <strong>Not quite</strong>. As it turns out, developments are pretty solid for both Linux and OSX.</p><p>NDepend’s team made the decision to migrate all key libraries to netstandard2.0, which means that base libraries that are used for .NET Framework, can also be used for .NET 5 and above.</p><h4>— Overview of the CLI</h4><p>The <strong>Web API</strong> was chosen for this section since it is the best example to analyze. For those coming from a C# background, which also have some experience developing on environments other than Windows, this section is quite refreshing.</p><ul><li><strong>Cross-platform compilation</strong></li></ul><p>NDepend is not distributed as a <strong>dotnet</strong> tool, meaning it’s not possible to install it via dotnet CLI. Still, from this point on, we will be using <em>th</em>e dotnet command.</p><p>Both for Linux and OSX, the NDepend bundle contains dependencies such as <em>NDepend.Console.MultiOS.dll.</em> This bundle must be extracted and placed under <strong>~/ndepend</strong>. This is well documented in the <a href="https://www.ndepend.com/docs/getting-started-with-ndepend-linux-macos">NDepend blog</a>. The <strong>/ndepend</strong> folder structure resembles:</p><pre>.<br>├── Doc<br>├── Integration<br>├── <strong>Lib</strong><br>├── NDepend.Console.exe<br>├── NDepend.PowerTools.exe<br>├── NDepend.PowerTools.SourceCode<br>├── NDepend.VisualStudioExtension.Installer.exe<br>├── <strong>net5.0</strong><br>├── <strong>net6.0</strong><br>├── <strong>net7.0</strong><br>├── README.txt<br>└── VisualNDepend.exe</pre><p>The <strong>bold </strong>folders, <strong>/Lib , </strong><strong>/net5.0 , </strong><strong>/net6.0 </strong>and<strong> </strong><strong>/net7.0</strong>, contain most of the assemblies used next.</p><ul><li><strong>Creating the NDepend project</strong></li></ul><p>The <em>NDepend.Console.MultiOS.dll </em>assembly is much like a dotnet tool, used to create the NDepend project.</p><p>Sequentially, pass the <strong>NDepend project file</strong> ndproj, the <strong>solution file</strong> followed by a pipe ‘|’ to filter project names, and <strong>every project build assembly</strong> <em>(.dll)</em> to be included in the analysis:</p><pre>dotnet ~/ndepend/net6.0/NDepend.Console.MultiOS.dll -cp &quot;./&lt;Project-name&gt;.ndproj&quot; &quot;./&lt;Project-solution-name&gt;.sln|-filter&quot; &quot;./&lt;Project-1-folder&gt;/bin/Debug/net6.0/&lt;Project-1&gt;.dll&quot; &quot;./&lt;Project-2-folder&gt;/bin/Debug/net6.0/&lt;Project-2&gt;.dll&quot;</pre><p>For the Web API project, that command looked something like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*weqqjnM8c_p12-IahxJlMg.png" /><figcaption>Creating an NDepend project file</figcaption></figure><p>With the NDepend project file created, verify that the paths to the <em>.sln</em> file and all assemblies were referenced correctly. These values should reside respectively within the &lt;<strong>IDEFile</strong> FilePath=&quot;&quot;/&gt;, &lt;<strong>Dirs</strong>/&gt;, and &lt;<strong>Assemblies</strong>/&gt; tags. If any of those values isn’t correct, then the<em> </em>file should be updated:</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; standalone=&quot;yes&quot;?&gt;<br>&lt;NDepend AppName=&quot;MySimpleCalculator&quot; Platform=&quot;DotNet&quot; FileWrittenByProductVersion=&quot;2023.1.1.9675&quot;&gt;<br>  &lt;OutputDir KeepXmlFiles=&quot;False&quot;&gt;./NDependOut&lt;/OutputDir&gt;<br>  &lt;IDEFiles&gt;<br>    &lt;IDEFile FilePath=&quot;./MySimpleCalculator.sln&quot; Filters=&quot;-filter&quot; Configuration=&quot;DEBUG|AnyCPU&quot;&gt;<br>      &lt;RootDirResolvingInfo Enabled=&quot;False&quot; Hints=&quot;Debug|bin|.bin|b|AnyCPU|x64|x86|v*.*|net*&quot; TimeOut=&quot;10&quot;&gt;<br>        &lt;RootDir&gt;.&lt;/RootDir&gt;<br>      &lt;/RootDirResolvingInfo&gt;<br>    &lt;/IDEFile&gt;<br>  &lt;/IDEFiles&gt;<br>  &lt;RuntimeProfileDesc&gt;.NET 6.0&lt;/RuntimeProfileDesc&gt;<br>  &lt;Assemblies&gt;<br>    &lt;Name&gt;MySimpleCalculator&lt;/Name&gt;<br>    &lt;Name&gt;MySimpleCalculatorWebAPI&lt;/Name&gt;<br>  &lt;/Assemblies&gt;<br>  &lt;Dirs&gt;<br>    &lt;Dir&gt;./src/MySimpleCalculator/bin/Debug/net6.0&lt;/Dir&gt;<br>    &lt;Dir&gt;./src/MySimpleCalculatorWebAPI/bin/Debug/net6.0&lt;/Dir&gt;<br>  &lt;/Dirs&gt;<br>  &lt;MergeCodeGeneratedByCompiler&gt;True&lt;/MergeCodeGeneratedByCompiler&gt;<br>[...]<br>&lt;/NDepend&gt;</pre><p>For code coverage, <a href="https://www.nuget.org/packages/altcover">AltCover</a> was the tool chosen, as <em>it’s</em> the quickest one to setup. For code coverage to work, add a <strong>CoverageFiles</strong> section as such:</p><pre><br>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; standalone=&quot;yes&quot;?&gt;<br>&lt;NDepend AppName=&quot;MySimpleCalculator&quot; Platform=&quot;DotNet&quot; FileWrittenByProductVersion=&quot;2023.1.1.9675&quot;&gt;<br>[...]<br>  &lt;MergeCodeGeneratedByCompiler&gt;True&lt;/MergeCodeGeneratedByCompiler&gt;<br>[...]<br>  &lt;CoverageFiles CoverageDir=&quot;./test&quot; CoverageDirFilter=&quot;coverage.xml.0.acv&quot; UncoverableAttribute=&quot;&quot; CoverageExclusionFile=&quot;&quot;&gt;<br>    &lt;CoverageFile&gt;./test/MySimpleCalculator.UnitTests/coverage.xml&lt;/CoverageFile&gt;<br>    &lt;CoverageFile&gt;./test/MySimpleCalculatorWebAPI.IntegrationTests/coverage.xml&lt;/CoverageFile&gt;<br>  &lt;/CoverageFiles&gt;<br>[...]<br>&lt;/NDepend&gt;</pre><p>To run the static analysis, the dotnet and the<em> NDepend.Console.MultiOS.dll</em> assembly require only the path to the <strong>NDepend project file</strong> ndproj. For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*Ou2GICpfw7fojdETEnjDAg.png" /></figure><p>The report generated can be either open in a browser, or exported to be open in any other tool which can interpret HTML, such as Grafana.</p><ul><li><strong>HTML Report</strong></li></ul><p>The <strong>Overview</strong> tab displays an overview of all the information as the IDE’s main page report. The <strong>Issues</strong> tab presents code coverage information. The <strong>Rules</strong> tab presents all the compliant and violated rules NDepend applied. <strong>Quality Gates </strong>tab<strong> </strong>presents a summary of the ‘<em>coverage percentage’</em>, ‘<em>blockers / critical / high issues’</em>, and ‘<em>per namespace debt ratio’</em>. In the <strong>Trend</strong> tab all the essential charts are made available for analysis. The <strong>Logs</strong> tab presents all the build logs of the analysis.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*I_YHqMnOpSgZwZ5oiTbv8w.gif" /><figcaption>Animated GIF — NDepend v2023.2.1 report</figcaption></figure><p>Also, its important to mention that you can see detailed information on code coverage, but not benefit from using <em>LINQ</em> queries as easily as on the IDE. This doesn’t mean it’s not possible to use <em>LINQ</em>, rather than it’s not that easy to do so. It’s possible to perform those queries by either passing one or more queries to an <a href="https://www.ndepend.com/api/webframe.html#NDepend.API~NDepend.CodeModel.ICodeBase.html">ICodeBase</a> to gather an IIssueSet, or on a <a href="https://www.ndepend.com/api/webframe.html#NDepend.API~NDepend.CodeModel.ICompareContext.html">ICompareContext</a> to gather an IIssueSetDiff.</p><p>Even so, the Code coverage tab will present all the essential information:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RogjrIcnRM0EGvk_BRduPA.png" /><figcaption>NDepend report — code coverage</figcaption></figure><p>Last but not least, in code coverage, you can hop directly into the code on any class by clicking it. This is very useful, plus you get immediate CTAs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iFWfXE62SmzSgKJYSConmQ.png" /><figcaption>Code smells, immediate feedback</figcaption></figure><h4>— NDepend API</h4><p>Lets rewind for a bit, up until the point where the essential folders, such as <strong>/Lib</strong> were mentioned. There’s another tool in the NDepend bundle which is very important to go through — the <em>NDepend.API</em>. This is part of the lot which was fully migrated to netstandard2.0, meaning it’s cross platform.</p><p>To get started using <a href="https://www.ndepend.com/docs/ndepend-api-quickstart">NDepend.API</a>, while following a rationale one can resonate with, the best example is applying a code smell suggestion. The code rule below suggests that types 100% covered by tests should be tagged with the <strong>[FullCovered] </strong>attribute:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uXomW5hho3li2A5Ql8Rfjg.png" /><figcaption>Code Rules — ND1608 code smell</figcaption></figure><p>To be able to do so, there are some changes to be made. First, manually add<em> NDepend.API.dll</em> to your project file, e.g.:</p><pre>&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;<br>[...]<br>   &lt;ItemGroup&gt;<br>     &lt;Reference Include=&quot;NDepend.API&quot;&gt;<br>       &lt;HintPath&gt;$HOME/$ndepend/Lib/NDepend.API.dll&lt;/HintPath&gt;<br>     &lt;/Reference&gt;<br>   &lt;/ItemGroup&gt;<br>&lt;/Project&gt;</pre><p>Reference the attribute, and pass the <strong>NDepend.Attributes</strong> namespace, in the class which was flagged by the report e.g.:</p><pre>namespace MySimpleCalculator<br>{<br>    using System;<br>    using NDepend.Attributes;<br><br>    [FullCovered]<br>    public class Calculator : ICalculator<br>    {<br> [...]<br>    }<br>}</pre><p>After re-generating the report, the alert is gone. Plus, your team is now aware that the <strong><em>Calculator.cs </em></strong>is expected to be fully covered by unit tests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8GVy_Rc-uLdP0LX6_RIsEw.png" /><figcaption>ND1608 code query — Calculator.cs removed from the list</figcaption></figure><p>The alert was also removed from the code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kCdh6gha1-M1A2qMmYx64w.png" /><figcaption>Calculator.cs file — ND1608 code smell not present</figcaption></figure><ul><li><strong>Addressing false positives</strong></li></ul><p>To finalize, picture that the message above turned out to be a false positive. In that case, instead of the attribute, <em>it </em>could be suppressed with:</p><pre><br>namespace MySimpleCalculator<br>{<br>    using System;<br>    using System.Diagnostics.CodeAnalysis;<br><br>    [SuppressMessage(&quot;NDepend&quot;, &quot;ND1608:Types100PercentCoveredShouldBeTaggedWithFullCoveredAttribute&quot;, Justification=&quot;...&quot;)]<br>    public class Calculator : ICalculator<br>    {<br> [...]<br>    }<br>}</pre><p>This could also be seen directly on the <em>Calculator.cs</em> class,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uQlG8s5PwARDNocYWCK_Nw.png" /></figure><h3>4. Critical Points</h3><h4>Performance</h4><p>In terms of performance there’s currently no match in the market for NDepend.</p><p>Being specific for the .NET ecosystem, an analysis takes only a few seconds. This means that, if providing a report is part of your build process, your build times will be drastically reduced when you switch to NDepend.</p><h4>Compatibility</h4><p>NDepend has its own set of rules. Its rules are independent from the rules applied by <a href="https://github.com/dotnet/roslyn">Roslyn</a> until 2024. Starting next year, NDepend team will try to add Roslyn rules to its own ruleset.</p><p>This means that, until then, if Roslyn analyzers are already part of your static analysis process and you’re considering using an extra layer of validation, it makes perfect sense to use NDepend, as they’ll complement each other. This also means that NDepend complements all tools that use Roslyn as a base analyzer.</p><h4>Tooling</h4><ul><li>CLI PowerTool</li><li>Visual Studio extension</li><li>Visual NDepend standalone app that collaborates side by side with VS Code and Rider</li><li>Integrations, SQ, Jenkins, AppVeyor, VSCode, Rider</li><li>NDependAPI</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32207d82ffe1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[C# — Converting a resume to HTML with Blazor WASM and ChatGPT]]></title>
            <link>https://medium.com/@lfilipecosta3/c-convert-your-resume-to-html-with-blazor-wasm-and-chatgpt-cb32c2eb5115?source=rss-f663aa265e85------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb32c2eb5115</guid>
            <category><![CDATA[blazor]]></category>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Luis Costa Brochado]]></dc:creator>
            <pubDate>Wed, 14 Jun 2023 22:25:29 GMT</pubDate>
            <atom:updated>2024-04-14T09:08:04.286Z</atom:updated>
            <content:encoded><![CDATA[<h2>C# — Converting a resume to HTML with Blazor WASM and ChatGPT</h2><h4>My quest on converting a resume into a webpage with AI’s assistance</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5PHJpk40gzKNiMtOdVSVZg.jpeg" /><figcaption>Source: <a href="https://www.pexels.com/pt-br/@pavel-danilyuk/">Pavel Danilyuk</a> from <a href="https://medium.com/u/5c7773c5a002">Pexels</a></figcaption></figure><h4>Table of Contents</h4><ol><li><a href="#6ed6"><strong>Disclaimer</strong></a></li><li><a href="#d654"><strong>Rationale</strong></a></li><li><a href="#2083"><strong>The Resume</strong></a><strong><br>— </strong><a href="#494c">Breaking it down</a><strong><br> — </strong><a href="#de73">Milestone 1: replicating the header</a><strong><br> </strong>— <a href="#9c1b">Milestone 2: replicating the body</a><strong><br></strong> — <a href="#07a3">Milestone 3: add the HTML template to Blazor</a></li><li><a href="#0321"><strong>Critical Points</strong></a><strong><br> </strong>— <a href="#6d9d">AI tools</a><strong><br> </strong>—<a href="#3111"> Final consideration</a><strong><br></strong> — <a href="#66c1">End Result</a></li></ol><h3>1. Disclaimer</h3><p>This is neither a “<em>10 ChatGPT prompts…</em>” nor a “<em>10 ChatGPT plugins that will make you 100x …</em>” blog post. Instead, this is a practical opinion article with a use case on ‘<em>how can AI tools be used as rubber ducks while coding</em>’.</p><h3>2. Rationale</h3><p>This was a thought I came across after <a href="https://www.youtube.com/watch?v=8V_BUGFKdaI&amp;list=PLdo4fOcmZ0oVlqu_V8EXUDDnPsYwemxjn&amp;index=3">.NET Conf 2022</a>, more explicitly, while assisting the <a href="https://www.youtube.com/watch?v=SCJu7YPNtdQ">.NET Conf 2022 — Student Zone</a>.</p><p>Watching some of the presentations and the ideas being shared, there was something, in particular, that caught my attention. Matt Soucoup gifted the crowd with a presentation about Blazor WASM, where he went through <a href="https://github.com/microsoft/dotnetconf-studentzone/blob/main/Build%20your%20Project%20Portfolio%20website%20with%20.NET/README.md">the basics of it by creating a resume</a>.</p><blockquote>Being able to create webpages is an underrated skill</blockquote><p>A good website can potentially add lots of value. In extreme cases, it might even save businesses. Believe it or not, most people working in the IT industry are unable to satisfy this need, even with all the existing available technology. In fact, this was a gap I felt I had in professional terms, being something I proactively intend to improve on.</p><p>With the proper use case and the motivation to learn Blazor, by applying some C# knowledge, it made sense to take advantage of the circumstances and focus on improving my ability to manipulate HTML and CSS.</p><p>So, I set myself to learn Blazor the right way. By replicating a resume I designed with <strong>Canva</strong> in my college years, that serves me to this day. The sole conditions were mirroring as much as possible exactly what I had, while doing my best to maintain the styling and layout.</p><h3>3. The resume</h3><p>The resume itself isn’t that complex, but it does have a bit of detail and quirky customization that could be tricky to reproduce with code. <em>Those</em> can easily turn a one week endeavor into a couple of months of work, which meant that I needed help — this was when ChatGPT came along.</p><p>Fortunately for me, ChatGPT<em> </em>was pretty much exploding by that time, in late 2022. Honestly, <em>it</em> gave me a lot of momentum, in motivation terms, to keep things flowing. I felt that I actually had help.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CUNFX3p-IoS7Tz_GFL2a1A.png" /></figure><h4>Breaking it down</h4><blockquote>“When you’re stuck with a problem, break it down into smaller <strong>problems</strong>, and those into even smaller problems, until you can solve all the small problems, one by one…”</blockquote><p>With that in mind, I decided to approach the resume replication by breaking it down into small problems, and those into even smaller ones, and so on. All this while keeping all information in a single page.</p><ul><li><strong>Milestone 1: replicating the header:</strong><br>- Background image;<br>- Horizontal bar with transparency;<br>- Profile image;</li><li>M<strong>ilestone 2: replicating the body:</strong><br>- Body split into two columns;<br>- Sections;<br>- Customize each section;</li><li><strong>Milestone 3</strong>:<strong> add the HTML template to Blazor:<br></strong>- Move the HTML file to a Blazor WASM app;<br>- Icons and hobbies;<br>- Create and publish a docker image of the app;</li></ul><h4>Milestone 1: replicating the header</h4><p>Getting started is usually the toughest barrier to break. For me, this is one of the strongest points of ChatGPT. Since <em>it</em> doesn’t overthink, and usually provides the simplest entry point for you to get started on something, ChatGPT excels at making you kick-off your projects. I recall typing a simple <em>“Hi Chat”, </em>and shortly after I had something to look at.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*aXV1uas4vXJwAZc7d6Bqlw.png" /><figcaption>Screenshot of the first interaction with ChatGPT</figcaption></figure><p>Summarizing the goals of this milestone, it took me longer to browse the web for the image used on my resume’s header when it was created, than the actual time spent setting it as a background image. Surprisingly, I found it more challenging to create the horizontal bar where the text would sit on, than creating the circle that’d serve as a placeholder for my profile picture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YbYm-B09p83DyymmY5ycGA.png" /></figure><p>By midst March, <a href="https://github.com/olevezinho/curriculum-vitae/issues/4">this milestone</a> had been achieved.</p><h4>M<strong>ilestone 2: replicating the body</strong></h4><p>This was a challenge. The goal of this milestone was to replicate the body while having all the information of two pages condensed into a single page, so things became a bit messy. Dividing the body into two columns was the easy bit. With that done, I rushed from section to section, defining consistent styles for all sorts of headings, for the paragraphs, the colors, the fonts, for the font weights, and so on.</p><p>There were moments in which it was clear that I had to prompt ChatGPT in order to get unblocked, but there were other moments I felt like I didn’t quite need it. Overall, this stage had a lot of back and forth with the prompt, but I also had lots of moments where I just tried to use my own creativity to solve problems.</p><p>For instance, on the right column of the body I found it easier to just use triangles to mimic the white effect of the background. To me, that was the most natural way of imitating the layout.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H3_87ONbUUSZ9RK4HXQLdA.png" /></figure><p>The small circles that I used to quantify my level of understanding of an idiom, or the incidence of a specific skill within each working context, were replicated almost fully resorting to ChatGPT. My only concerns were positioning and coloring them accordingly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L5jR5MoTiuEkQC0xGaCcBA.png" /></figure><p>This milestone was <a href="https://github.com/olevezinho/curriculum-vitae/issues/6">set as finished</a> once I had only the icons and the hobbies sections’ images remaining.</p><h4>Milestone 3: add the HTML template to Blazor</h4><p>In this milestone ChatGPT felt a bit off. With Blazor being a relatively recent, quickly evolving, framework, which is sustained by an ecosystem that changes quite often, AI tools like ChatGPT seem to lack context.</p><p>The docker image wasn’t that hard to create. I had already created a docker image of the template I initially created. The first version of the containerized application image worked fine, served with NGINX. But the second version, with all the custom HTML and CSS, was not that easy, I must say. For some reason, I added PrivateAssets=&quot;All&quot; to the project’s dependencies. This resulted in me having trouble displaying the contents of the latest version of the app, after containerizing it into an image. ChatGPT wasn’t helpful at all, despite my best attempts to feed it with context.</p><p>Eventually I posted <a href="https://www.reddit.com/r/Blazor/comments/13z6nqb/unable_to_publish_my_blazor_wasm_app/">on r/Blazor in Reddit</a>, and someone did help me. But I lost quite a bit of time trying to figure if this was either a server or a deployment problem. I feel I wouldn’t have been able to figure it with ChatGPT’s assistance.</p><p>Mudblazor was the tool I chose to render the icons. Its fantastic documentation, left almost no margin for doubts, when it came to decide which framework to use. Highly advise trying it out.</p><p>The hobbies were laborious, mainly because they didn’t all have the same size, and had to be aligned in the proper positions, with their heights, sizes, and margins being meticulously adjusted to fit appropriately.</p><p>The image was eventually pushed to docker hub, and I’ll do my best to keep it up-to-date. It can be found <a href="https://hub.docker.com/repository/docker/olevezinho/curriculum-vitae/general">here</a>.</p><h3>4. Critical Points</h3><h4>AI tools</h4><ul><li>Maybe this is not the best use case to explore the potential of AI tools but it does help you improve and learn about what you are doing.</li><li>Code that writes itself is pointless. You have to put in the hours and the manual labor if you want to be able to control the solutions you create.</li><li>If an AI tool does all the work for you, you’ll quickly become engulfed by lack of context and, if the solution becomes too large, you’ll slowly but surely drown in cognitive complexity.</li><li>To avoid <em>that</em>, I’d rather look at AI tools as something that I can rely on as a productivity booster, rather than some sort of black magic that I can resort to whenever I’m feeling lazy.</li><li>AI tools might be permeable to bad input. If you think of AI tools as Data ingestion, Data processing, and Predictive modelling tools — which by the way they are — you’ll probably realize that ingesting poisoned data can eventually lead to poisonous predictions. This might end up in really, really bad decisions.</li><li>Having the PDF produced prior to this project, helped a lot.</li><li>Overall, I saved time by having a clear idea of what I wanted to do. Prompts weren’t so vague and the responses were more objective.</li><li>I fed my original two-paged resume to another tool, ChatPDF, to see whether would it produce something similar to what I ended up achieving. It failed brilliantly.</li><li>Being a custom layout with custom stylings, its mobile friendliness was rendered obsolete. This means additional future work.</li></ul><h4>Final consideration</h4><p>I consider the goal as accomplished. There’s a lot of dynamic resumes out there, with lots of good ideas, but this is my own and I want to have an easy way of keeping it up-to-date, while being able to showcase it with a few clicks.</p><h4>End Result</h4><p>I had to collapse sections, shift hobbies from the right side to the left, and remove more information than what I initially intended to. Even with all that, I stayed true to the original style and layout:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QhvrtkNb5CQTUT_iVW2E7Q.png" /></figure><p>Looks better than I expected. If you’re wondering, or think it might be of use, below is the full log of the conversation I had with ChatGPT.</p><p><a href="https://chat.openai.com/share/ec46cdf2-9717-4dc3-b884-608b13f6aa77">Curriculum Vitae</a></p><p>As for the next steps, I’ll follow up by having this template mobile friendly(er). I’ll link that second blog here:</p><p><a href="https://medium.com/@lfilipecosta3/c-converting-a-resume-to-html-with-blazor-wasm-and-chatgpt-pt-2-bdf7d0ea36e9">C# — Converting a resume to HTML with Blazor WASM and ChatGPT — pt. 2</a></p><p>Don’t forget to follow me and leave some claps for those updates if you fancied the post!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb32c2eb5115" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[C# — Extension methods with practical use cases]]></title>
            <link>https://medium.com/@lfilipecosta3/c-extension-methods-with-practical-use-cases-530948a8f8d9?source=rss-f663aa265e85------2</link>
            <guid isPermaLink="false">https://medium.com/p/530948a8f8d9</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[net-core]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <dc:creator><![CDATA[Luis Costa Brochado]]></dc:creator>
            <pubDate>Tue, 25 Apr 2023 10:43:33 GMT</pubDate>
            <atom:updated>2023-09-22T22:40:51.624Z</atom:updated>
            <content:encoded><![CDATA[<h2>C# — Extension methods with practical use cases</h2><h4>Extend external dependencies or your own modules by using inversion of control and dependency injection</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3c8aiiA9BKNxp42TiZtk5Q.jpeg" /><figcaption>Source: <a href="https://unsplash.com/@huzgin?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Huzaifa Ginwala</a> from <a href="https://medium.com/u/2053395ac335">Unsplash</a></figcaption></figure><p><strong>Table of Contents</strong></p><ol><li><a href="#96dd"><strong>Background</strong></a></li><li><a href="#cfce"><strong>Extension methods and their purpose</strong></a></li><li><a href="#e6be"><strong>Producing decoupled modules by extending IServiceCollection</strong></a><br>— <a href="#7faf">Following recommended good practices</a><br> — <a href="#0aa0">Working the extension</a><br> —<a href="#1fe0"> Inversion of control</a><br> —<a href="#71de"> Dependency injection</a></li><li><a href="#de45"><strong>Extending external dependencies</strong></a></li><li><a href="#1b70"><strong>Critical points</strong></a></li></ol><h3>1. Background</h3><p>A mesmerizing explanation of extension methods had made me wonder:</p><blockquote>‘Why extend a class I control, when I can simply add the functionality to it whenever needed?’</blockquote><p>As I didn’t usually manage code out of my control, that wasn’t quite a thorough first explanation. Extension methods remained a last resort for me for a while, as I refused to use them in almost every use case they made sense.</p><p>I needed another point of view. Since ChatGPT wasn’t available at that time, I relied mostly on experienced developers and Google to help me get answers to the questions I was asking. Their top tips pointed to the usual sale points: ‘it helps a lot to be able to extend basic types’. With that, I was given the usual example of extending the .ToString() method, to format a string, and that was about it.</p><p>I was aware that updating packages could involve a lot of work, but I didn’t instantly see how could extension methods reduce development time.</p><p>With time came experience and, with a solid amount of research, alongside periods of reflection, I started seeing how important this concept was. In fact, I became amazed by its brilliance, and its simplicity:</p><blockquote>‘By extending functionality of an existing codebase externally, I won’t be required to recompile, package and push a new version of a bunch of binaries from another codebase each time I need something added to <em>it</em>.’</blockquote><p>Of course things are not so linear, and <em>this</em> is not a jack of all trades. Sometimes, using an extension will only deepen a problem. For instance, if a feature is too large to be extended, it should be added to the library itself.</p><h3>2. Extension methods and their purpose</h3><p>Probably, the most obvious use case is when you’re producing decoupled modules and there’s the need to prepare the module to be <em>‘injectable’</em>. To be able to do that you’ll ideally enable the IoC container to be extended <strong>beforehand</strong>.</p><p>Imagine this other scenario: <br>- You’re working with micro services, where there’s no real control of the entire codebase used, and domain or service logic should reside within the service’s contextual boundaries. Ideally, under these circumstances, you’d be extending <em>a</em> third party library to be able to perform a specific task that the package maintainer didn’t include. If it’s a relatively small addition to the original package, than <em>that</em> makes a lot of sense.</p><p>These are, from my perspective, the most common cases in which creating an extension is a good practice. In fact, in similar contexts, it’s the <em>de facto</em> practice.</p><h3>3. Producing decoupled modules by extending IServiceCollection</h3><p>In <a href="https://faun.pub/remote-package-management-in-c-c959b6df3fe7">a previous blog</a>, where I wrote a bit about remote and private package management, I accidentally laid the foundations for this post. <em>That</em> was a merely illustrative code example, for educational purposes, so I tried my best to abstract from the functionality, and focus on the main objective, which was to produce a NuGet package, package and push it to a private package source.</p><p>That was the <strong>Calculator </strong>class. A class library <strong>MySimpleCalculator</strong> with the following functionalities:</p><pre>namespace MySimpleCalculator<br>{<br>    public class Calculator<br>    {<br>        public int Add(int? N1, int? N2)<br>        {<br>            CheckForNull(N1, N2);<br>            return N1.Value + N2.Value;<br>        }<br><br>        public int Subtract(int? N1, int? N2)<br>        {<br>            CheckForNull(N1, N2);<br>            return N1.Value - N2.Value;<br>        }<br><br>        public int Multiply(int? N1, int? N2)<br>        {<br>            CheckForNull(N1, N2);<br>            return N1.Value * N2.Value;<br>        }<br><br>        public double Divide(double? N1, double? N2)<br>        {<br>            CheckForNull(N1, N2);<br>            CheckDivisionZero(N2);<br>            return N1.Value / N2.Value;<br>        }<br><br>        private static void CheckForNull(int? N1, int? N2)<br>        {<br>            if (!N1.HasValue || !N2.HasValue)<br>                throw new ArgumentNullException();<br>        }<br><br>        private static void CheckForNull(double? N1, double? N2)<br>        {<br>            if (!N1.HasValue || !N2.HasValue)<br>                throw new ArgumentNullException();<br>        }<br><br>        private static void CheckDivisionZero(double? N2)<br>        {<br>            if (N2.Value == 0)<br>                throw new DivideByZeroException();<br>        }<br>    }<br>}</pre><p>In <em>that</em> situation I was making use of this <strong>Calculator</strong>, restoring it as an external dependency, and using it like a helper class. This meant the <strong>Calculator</strong> class had to be instantiated in each and every single one of the controller’s methods for an operation to be performed. But that’s not written on the books.</p><p>Fortunately, <em>that </em>circumstance has provided me with <em>this</em> opportunity, which is to explain extension methods in detail.</p><h4>Following recommended good practices</h4><p>In order to use methods from the <strong>Calculator</strong> module in other projects, register them with a dependency injection container, the <em>IServiceCollection</em>, from the <strong>Microsoft.Extensions.DependencyInjection</strong> library.</p><p>By adding a scoped service with the interface and the declaration, the <strong>Calculator</strong> is being registered as a service that can be injected into other components, this is dependency injection. <em>It</em> means that when an instance of the class that depends on the <strong>Calculator</strong> class is created, the dependency injection container will automatically create an instance of the <strong>Calculator</strong>.</p><h4>Working the extension</h4><p>Extract the interface from the <strong>Calculator</strong> class. This can be done with ease on Visual Studio, by pressing &#39;ctrl + .&#39; and clicking the “<em>extract interface</em>” option. It should end up being similar to the following:</p><pre>namespace MySimpleCalculator<br>{<br>    public interface ICalculator<br>    {<br>        int Add(int? N1, int? N2);<br>        double Divide(double? N1, double? N2);<br>        int Multiply(int? N1, int? N2);<br>        int Subtract(int? N1, int? N2);<br>    }<br>}</pre><p>After that, create a static class, namely <em>CalculatorEx</em>, where the static AddCalculatorService() method will return the extended <em>IServiceCollection, </em>within which the Calculator service has been injected:</p><pre>namespace MySimpleCalculator.Extensions<br>{<br>    public static class CalculatorEx<br>    {<br>        public static IServiceCollection AddCalculatorService(this IServiceCollection services)<br>        {<br>            services.AddSingleton&lt;ICalculator, Calculator&gt;();<br><br>            return services;<br>        }<br>    }<br>}</pre><p>By this stage, in the real world, one would either compile, package and push this set of binaries to a private repository of some sort, or have a CI framework in place that’d automate those steps.</p><p><em>Interested in doing those steps manually? Here’s </em><a href="https://faun.pub/remote-package-management-in-c-c959b6df3fe7#3f7f"><em>an insight</em></a><em> on how to do the packaging.</em></p><h4>Inversion of control</h4><p>With that done, create a new <em>Web API</em> project, in the same solution. Be sure to add the <em>NuGet</em> package which was created previously. Inject the <strong>Calculator </strong>in the<strong> </strong>Startup.cs using services.AddCalculatorService() on the <em>ConfigureServices</em> method, as can be seen below — between the references to <em>MVC</em> and <em>Swagger</em>:</p><pre>public void ConfigureServices(IServiceCollection services)<br>{<br>    services.AddMvc()<br>        .ConfigureApiBehaviorOptions(options =&gt; options.SuppressMapClientErrors = true)<br>        .AddMvcOptions(options =&gt; options.EnableEndpointRouting = false);<br><br>    services.AddCalculatorService();<br><br>    services.AddSwaggerGen(c =&gt;<br>    {<br>         c.SwaggerDoc(&quot;v1&quot;,<br>         new Microsoft.OpenApi.Models.OpenApiInfo<br>         {<br>              Title = &quot;Calculator Web App, &quot; + hosting.EnvironmentName,<br>              Version = &quot;v1&quot;,<br>              Description = &quot;Web API example, created with .NET 6&quot;<br>         });<br>    });<br>}</pre><h4>Dependency Injection</h4><p>With this done, the module will be automatically injected into the <em>Web API</em> whenever it’s booted up. The only thing that’s missing is adding it to a controller.</p><p>Create a <strong>CalcController</strong>, and paste in the following code:</p><pre>namespace MyCalculatorWebApp.Controllers<br>{<br>    [Produces(&quot;application/json&quot;)]<br>    [Route(&quot;api/[controller]&quot;)]<br>    [ApiController]<br>    public class CalcController : ControllerBase<br>    {<br>        private readonly ICalculator calculator;<br><br>        public CalcController(ICalculator calculator)<br>        {<br>            this.calculator = calculator;<br>        }<br><br>        [HttpPost(&quot;add/{n1}/{n2}&quot;)]<br>        public ActionResult&lt;string&gt; PostNumbersAdd(int n1, int n2)<br>        {<br>            try<br>            {<br>                return new ObjectResult(calculator.Add(n1, n2).ToString());<br>            }<br>            catch (ArgumentNullException ex)<br>            {<br>                return ex.Message;<br>            }<br>        }<br><br>        [HttpPost(&quot;sub/{n1}/{n2}&quot;)]<br>        public ActionResult&lt;string&gt; PostNumbersSub(int n1, int n2)<br>        {<br>            try<br>            {<br>                return new ObjectResult(calculator.Subtract(n1, n2).ToString());<br>            }<br>            catch (ArgumentNullException ex)<br>            {<br>                return ex.Message;<br>            }<br><br>        }<br><br>        [HttpPost(&quot;mul/{n1}/{n2}&quot;)]<br>        public ActionResult&lt;string&gt; PostNumbersMul(int n1, int n2)<br>        {<br>            try<br>            {<br>                return new ObjectResult(calculator.Multiply(n1, n2).ToString());<br>            }<br>            catch (ArgumentNullException ex)<br>            {<br>                return ex.Message;<br>            }<br>        }<br><br>        [HttpPost(&quot;div/{n1}/{n2}&quot;)]<br>        public ActionResult&lt;string&gt; PostNumbersDiv(int n1, int n2)<br>        {<br>            try<br>            {<br>                return new ObjectResult(calculator.Divide(n1, n2).ToString());<br>            }<br>            catch (DivideByZeroException ex)<br>            {<br>                return ex.Message;<br>            }<br>            catch (ArgumentNullException ex)<br>            {<br>                return ex.Message;<br>            }<br>        }<br>    }<br>}</pre><p>Notice how the private readonly ICalculator calculator is declared before the constructor. Inside the constructor, the dependency injection framework instantiates the <strong>ICalculator</strong> interface that was previously worked up on the Startup.cs class. Build and run it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LfBQ2oSbIA837MCBfe9Ung.png" /><figcaption>OpenAPI’s view of Calc controller</figcaption></figure><p>With the API tested, the next step is creating an extension method by adding another mathematical operation, modulo.</p><h3>4. Extending external dependencies</h3><p>The <strong>MySimpleCalculator</strong> package will be extended past its boundaries. For representative purposes, I want to know the remainder of a specific division. This means a new method will be added to the <strong>Calculator</strong>, without touching the original codebase.</p><p>The extension method must follow a similar approach to what was made on <a href="#0aa0">the extension sub-section</a>. The first parameter of the method should be the type being extended, and it should be preceded by the “<em>this</em>” keyword. The other two parameters will be two integers n1 and n2. A couple validations will also be made, just before returning the result of the modulo operation, the remainder.</p><pre>namespace MySimpleCalculatorWebAPI.Extensions<br>{<br>    public static class MySimpleCalculatorEx<br>    {<br>        public static int Mod(this ICalculator calculator, int? n1, int? n2)<br>        {<br>            if (!n1.HasValue || !n2.HasValue)<br>                throw new ArgumentNullException();<br><br>            if (n2.Value == 0)<br>                throw new DivideByZeroException(nameof(n2));<br><br>            return n1.Value % n2.Value;<br>        }<br>    }<br>}</pre><p>On the <em>CalcController</em> class, create another method. Here intellisense might trip a little. <strong>MySimpleCalculatorWebAPI.Extensions</strong> namespace should be included manually in the controller. Call the recently extended calculator’s .Mod() method on the controller:</p><pre>[HttpPost(&quot;mod/{n1}/{n2}&quot;)]<br>public ActionResult&lt;string&gt; PostNumbersMod(int n1, int n2)<br>{<br>    try<br>    {<br>        return new ObjectResult(calculator.Mod(n1, n2).ToString());<br>    }<br>    catch (DivideByZeroException ex)<br>    {<br>        return ex.Message;<br>    }<br>    catch (ArgumentNullException ex)<br>    {<br>        return ex.Message;<br>    }<br>}</pre><p>The Web API should present the new recently added method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OxLWJ1rPkbTYhBkeX0fN3w.png" /><figcaption>Recently added mod method</figcaption></figure><p>There are other, more refined, examples that can illustrate how this works. But this is a fairly simple one, which does well displaying the practical usage of this pattern.</p><h3>5. Critical points</h3><p>Personally, extension methods felt much like having a clunky third arm in those days.</p><ul><li><strong>Balance</strong><br> — At first, I didn’t quite know what to do with <em>that</em> information but, deep down, I was aware that it’d be useful. <br> — Then, up to a point I found myself using extension methods abusively. This sort of approach didn’t have an immediate drawback but, in some projects, the code wasn’t gradually becoming modular. In fact, its complexity grew, and it was getting harder to maintain, because these external modules and extensions were tightly coupling the codebase. <br> — Finally, I started balancing my decision more towards compliance with the SOLID principles, ease of maintenance, and testability.</li><li><strong>Flexibility</strong><br> — This is in my opinion the strongest sell point behind extension methods.<br> — An extension method is an enabler in essence. It’s also ergonomic if used appropriately.<br> — No extension methods in the codebase means you’re most like doing something wrong.<br>— Too many extension methods can reveal either a hidden problem in the external dependency being extended, or a problem in the codebase. Have too many of them, and replace the module being extended by another one, just to see how deep of a hole you’ve dug for yourself over time.</li><li><strong>Time consumption</strong><br> — Developing an extension method saves both time and resources. It can be used as future proof, for instance, when addressing technical debt. If you follow good practices, and common sense, your extension method’s code will serve as documentation when you address that debt.</li><li><strong>Limitations<br></strong> — From my perspective, extension methods should, for the most part, be temporary if they match these two conditions: <br> 1. The dependency being extended is simultaneously outside of the current codebase<br> 2. That same third party being extended is under your (or your team’s) control. <br>In other words, this means that the extension of <a href="https://github.com/olevezinho/MySimpleCalculator/tree/main/src/MySimpleCalculator">MySimpleCalculator</a> given as an example before, where the modulo was created, should ideally be temporary, since I control both the original package and the project where the extension was created.<br> — Extension methods can’t be used to override existing methods. In an edge case, you can mimic an existing method by mirroring most of its functionality, and adding the extra logic you need in order to solve a problem. All and all, this will duplicate the existing code. Plus, the method name can’t be similar to the original one.<br> — Extension methods cannot access private properties of the concrete type they implement. Fundamentally, they can’t access anything that’s private on that specific type. <br> — <a href="https://stackoverflow.com/questions/312184/disadvantages-of-extension-methods">You can only extend methods</a>. Not properties, not constructors, not operators, not indexers, anything other than methods. If an extension is too large of an addition, it has to be implemented inside the dependency.</li></ul><p>Here’s the code for the program above:</p><p><a href="https://github.com/olevezinho/MySimpleCalculator/tree/main/src">MySimpleCalculator/src at main · olevezinho/MySimpleCalculator</a></p><p>If you read this up until this point, and liked the content, don’t forget to give it a thumbs up and follow me for more content like this!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=530948a8f8d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Raspberry Pi as an  access point — FOSS and performance comparison]]></title>
            <link>https://medium.com/@lfilipecosta3/raspberry-pi-as-an-access-point-foss-and-performance-comparison-8f364b9eb6c5?source=rss-f663aa265e85------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f364b9eb6c5</guid>
            <category><![CDATA[wifi]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[network-security]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <dc:creator><![CDATA[Luis Costa Brochado]]></dc:creator>
            <pubDate>Tue, 07 Feb 2023 19:50:08 GMT</pubDate>
            <atom:updated>2023-04-14T19:44:48.621Z</atom:updated>
            <content:encoded><![CDATA[<h2>A Raspberry Pi as an access point? FOSS and performance comparison</h2><h4>Ever wondered if your Raspberry Pi can be used as an access point?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ElSwXHKJ1HfcS4agmUDIcA.jpeg" /><figcaption>Source: <a href="https://www.pexels.com/pt-br/foto/foto-aerea-de-edificios-e-estradas-681335/">Aleksejs Bergmanis</a> from <a href="https://medium.com/u/5c7773c5a002">Pexels</a></figcaption></figure><h4>Long story short</h4><p><em>It </em>can, but there are alternatives specifically designed for each feat. Briefly, you’ll be provided with enough information to decide the best solution for your use case.</p><p><strong>Table of Contents</strong></p><ol><li><a href="#3169"><strong>The dilemma</strong></a><br>— <a href="#3998">Exposing the use case</a> <br>— <a href="#824f">Interfacing ‘details’</a></li><li><a href="#a686"><strong>The list of options</strong></a></li><li><a href="#9d2d"><strong>The solutions</strong></a><br> —<a href="#b5dd"> It’s called RaspAP</a><br> — <a href="#977e">Discovering Balena OS</a><br> — <a href="#c750">Raspberry OS Network Manager</a></li><li><a href="#f83c"><strong>Critical points</strong></a><br> — <a href="#f307">Performance</a><br> — <a href="#200e">Installation and maintenance</a></li><li><a href="#62a4"><strong>Conclusion</strong></a></li></ol><h3>1. The dilemma</h3><p>Wi-Fi is unreliable. Better said, it’s susceptible to proximity. When it’s reach becomes a household problem, there’s a wide variety of applications to overcome any given situation. A connection’s reach can be increased, for example, by installing access points, adding Wi-Fi extenders, plugging USB dongles, or even by using other network adapters.</p><p>Regardless, neither option guarantees signal stability such as a cable connection.</p><p>At least <em>this </em>was my line of thought.</p><h4>Exposing the use case</h4><p>Having a router on a living room with a home office being about 10 meters away, I wasn’t expecting to have the greatest, stablest signal.</p><p>What I came to realize was how determining would <em>that</em> be during remote work. Having a good connection in that case, made all the difference. Increasing wireless range was out of question. So, I was set to do an investement that had already crossed my mind, but had systematically been adjourned.</p><p>Since phone cable ran through the apartment floor, from one wall socket to another, all I had to do was replace it by internet cable. So, since the infrastructure was in place, I required about a dozen meters of cable, a couple wall sockets, and a couple cable gutters. It wouldn’t be cheap, but I knew I wouldn’t regret it. Or so I thought.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*Nnp2NHjmEPSPjnsNydndvQ.png" /><figcaption>Network blueprint</figcaption></figure><h4>Interfacing ‘details’</h4><p>When using a laptop, the average IT worker is expecting to be able to plug a network cable to it. Thing is, some of the most recent laptops don’t come with RJ-45 ports.</p><p>That’s inconvenient. To say the least.</p><p>To solve this problem, and since my professional laptop couldn’t really handle ethernet, I rushed to find a quick and effective solution. By the title of the blog, my choice was obvious, but below I’ll shed some light on the reasoning behind the decisions made, and the context in which I made them.</p><h3>2. The list of options</h3><p>Searching for an access point in my stuff was fruitless. Wi-Fi extenders are, in general, very bad. Especially those that don’t have antennas. They never work half as good as they should, and they have sistematic signal interruptions, which insist on happening at the most disruptive moments. This was the first option I crossed from the list.</p><p>Alternatively, USB antennas — or Wi-Fi dongles — can be quite good depending on the chipset and the antenna’s size. Plus, they‘re pretty easy to setup. In most cases, plug it in, and it’s ready to connect. Conversely, the better they are, the more expensive they get. In my case, there were still cheaper solutions, that could’ve guaranteed the stability only a cable connection would have. Plus, this would’ve made my earlier investment redundant.</p><p>So, I started looking into RJ-45 USB adapters. They’re cheap, they offer a guaranteed minimal amount of network throughput, and they seem quite elegant. But they’d be an overkill. Don’t take me wrong, because it would have got the job done. With blazingly fast internet speeds. But I didn’t exactly require 500 MB’s of throughput capacity. In fact, my internet bandwidth wasn’t even close to that.</p><p>At that precise moment, I remembered the raspberry pi I had laying around in a dusty box. “It can probably be just enough to get me out of this situation for free” I thought. Additionally, with so many different purposes and applications it encompassed, it was virtually impossible that not a single soul had had the same idea before. Fortunately, I was correct.</p><h3>3. The solutions</h3><p>After browsing for a while, the first couple alternatives popped out. Namely, <strong>BalenaOS </strong>and <strong>Raspberry OS</strong> based access points.</p><h4>It’s called RaspAP</h4><p>And it’s quite interesting. Apparently it’s just Raspberry OS, but network oriented. It’s installed quite easily. Grab a Raspberry OS LITE image. Run the quick setup script, and press next until the installation is finished. After that, you have a mini router, with a decent browser UI.</p><pre>curl -sL https://install.raspap.com | bash</pre><p>There’s also a version of a docker image for Rasp-AP, but the out-of-the-box service is installed right on top of the operating system.</p><p>On the downside, the default broadband download and upload speeds are capped — as per google speedtest — at about 12 MB, which is pretty much unusable for normal remote working standards.</p><p>I’d be fine with about 30 to 40 MB, but that seemed impossible without playing around with frequencies, trying different channels — basically messing up the installation — and becoming unable to access the device due to a system misconfiguration.</p><p><a href="https://raspap.com/">RaspAP</a></p><p>Driven by the impulse of figuring out a quick working solution, and remembering one of the first search results I’d skipped, I got back to BalenaOS.</p><h4>Discovering BalenaOS</h4><p>Following the available documentation, I figured that it would take some time to create an account, flash the OS, and run it to implement the Wi-Fi access point.</p><pre>bash &lt;(curl -L https://github.com/balena-io/wifi-connect/raw/master/scripts/raspbian-install.sh)</pre><p>Much like RaspAP uses Raspberry OS, this AP is fundamentally similar in that it is making use of a base BalenaOS image to gear up a Wi-Fi access point.</p><p>The way how it’s done though, is unique. RaspAP is distributed by default as a subset of configurable apt packages, while this AP implementation comes in form of a plug-in docker image. It’s a tini tiny micro-service that provides Wi-Fi.</p><p>The numbers were way better, with default broadband download and upload speeds capping at a whopping 45~50MB. This meant that online meetings, screen sharing sessions and downloads, flowed way faster, and without interruption.</p><p><a href="https://github.com/balena-os/wifi-connect">GitHub - balena-os/wifi-connect: Easy WiFi setup for Linux devices from your mobile phone or laptop</a></p><p>I was already using Balena AP when I got word of a beta version of a Network Manager released late September 2022, which included the option of creating a Wi-Fi hotspot.</p><h4>Raspberry OS Network Manager</h4><p>Quoting <a href="https://www.tomshardware.com/how-to/raspberry-pi-access-point">Tom’s Hardware blog post</a>: Network Manager is ‘<em>a tool new to the Raspberry Pi that replaces dhcpcd as a means to manage networking’.</em></p><p>This can indeed replace the first RaspAP option. As this module comes out-of-the-box with the base Raspberry OS image, it simplifies creating and managing a Wi-Fi hotspot, which seems perfect for the use case.</p><p>There are still some minimal configuration settings which require the user’s attention for the Network Manager to be fully operational, but these are super easy to follow if one has explored Linux and networking basics.</p><p>Similarly to RaspAp, this version of a managed hotspot is distributed as a subset of configurable apt packages, as opposed to Balena’s Wi-Fi Connect.</p><p>Network Manager provides about 45~50MB of both download and upload speeds. Which means that it rivals toe-to-toe against Balena’s Wi-Fi Connect.</p><p>The main advantage of this AP over the remaining ones resides at the hardware level, which I’ll explain in detail on the critical points up next.</p><h3>4. Critical points</h3><p>The mentioned providers compose my <em>go-to</em> AP implementations for the techie swiss army knife. I’ve put together some of the most important aspects of each version of the AP, and compiled the data into a simple table.</p><p>All the performance data presented below was gathered after performing network tests using Google speedtest. To clarify, all the tests were made on a Raspberry Pi 4B board (2GB). The remaining data is based solely on my experience as a raspberry pi user:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fsheetsu.com%2Ftables%2Fe6051ae0ac&amp;dntp=1&amp;display_name=Sheetsu&amp;url=https%3A%2F%2Fsheetsu.com%2Ftables%2Fe6051ae0ac&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=sheetsu" width="700" height="248" frameborder="0" scrolling="no"><a href="https://medium.com/media/4adcd778e97c3eec3182a80df7f2de4f/href">https://medium.com/media/4adcd778e97c3eec3182a80df7f2de4f/href</a></iframe><h4>Performance</h4><p>For each provider, three speed tests were performed. In comparative terms, both <strong>Balena OS’s Wi-Fi Connect</strong>, and <strong>Raspberry OS’s Network Manager</strong>, have outstanding performance capabilities. Both of them reached similar speeds, which I suspect are the maximum throughput of the antenna on the Raspberry PI 4B’s board. Opposed to its counterparts,<strong> Rasp-AP</strong>’s implementation (on the default channel) was not as good.</p><h4>Installation and maintenance</h4><p>Installation is straight forward on every provider. Security is pretty much equal between them all, as they all support similar Wi-Fi network protocols, with maybe RaspAP being a bit more customizable. But maintenance is probably where they set themselves apart.</p><ul><li><strong>Network Manager</strong></li></ul><p>By far, the new Raspberry OS Network Manager has the easiest setup. Everything is done through the GUI, and the only command required is sudo raspi-config. I can see that the purpose of it is to be user friendly, which it clearly is. But, I also strongly guess that the same process can be scripted, if such is required.</p><p>Maintenance-wise, it follows the same approach. To make things easier, a QR code is provided to any user that needs to connect to the hotspot. Additionally, I had no trouble with the raspberry pi overheating, and everything worked as easy as could be.</p><p>Fortunately, there’s not that much to say about Network Manager. It’s also very recent, which means that issues may be reported on the upcoming times, but it also means that it can be further improved.</p><p>Updating the system is a breeze and it can be done with the usual sudo apt-get update &amp;&amp; sudo apt-get upgrade. As the Network Manager is bundled with the system’s packages, there will be no issues updating it.</p><ul><li><strong>Wi-Fi Connect</strong></li></ul><p>The installation process of the BalenaOS’s hotspot is a bit trickier, but it’s not something a begginer Linux user can’t handle. It will also be easier for a user that knows docker basics. This one is clearly enterprise driven, as it’s as easy to manage a single device, as it is to manage a fleet of devices. So it’s strong point is definitely scalability.</p><p>This was the provider I sticked with for longer. It’s my personal favorite, but it has <em>caveats </em>maintenance-wise. BalenaOS cloud provides a very good UI, which eases device management:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*avVZ-2qTj2eYGxR3QDBU-g.png" /><figcaption>Balena Cloud broswer UI</figcaption></figure><p>For instance, a lot of information can be seen there, such as the device temperature, CPU load, free storage percentage, memory under usage, and much more. On the darker side, sometimes for unknown reasons, my CPU load rose to 73% and above, reaching as far as 80% usage. This wasn’t something that could be seen as normal, and the device had to be restarted quite often in order to normalize CPU usage levels.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HPzdBrSjI9iDa-4ciLwEnA.png" /><figcaption>Balena AP management UI</figcaption></figure><p>Overheating was frequent. I ended up overcoming that by using quite an unorthodox approach. As I was using the Argon ONE (V2) case, and it doesn’t have a scripted setup for Balena OS, this meant that I had to control the fan — and it’s speed — manually, which was initially very challenging. Fortunately, some users had already reported similar issues with Argon ONE on raspberry forums:</p><p><a href="https://forums.raspberrypi.com/viewtopic.php?f=29&amp;t=275713&amp;sid=83e15126255784d5569dc19a98493b08#p1676412">Argon One Replacement Daemon - Raspberry Pi Forums</a></p><p>The system can be updated through the browser UI, where update versions can be browsed.</p><ul><li><strong>Rasp-AP</strong></li></ul><p>Even though Rasp AP does a lot more than simply service itself as an access point — which is it’s main advantage over its counterparts — for this specific domain it’s capabilities feel a bit tamed. If you use the quick installer script, it’s just like any ordinary setup.</p><p>In both setup and management terms, it’s very similar to Balena OS’s Wi-Fi connect. You’ll also be provided with a rich browser UI, and all the operations can be done through it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DS8vWP35Fcm0nTw4TtjJAg.png" /><figcaption>RaspAP browser UI</figcaption></figure><p>In hardware terms, not a single issue. The management UI also displays CPU temperature, memory usage, and more detailed traffic data than any other of its rivals. Which is good for some scenarios.</p><p>Updating the system can‘t be done through the UI, and it needs to be done separately from the base OS updates.</p><pre>curl -sL https://install.raspap.com | bash -s -- --upgrade</pre><p>Nevertheless, it’s not that hard, and if used properly, all the previous configuration is saved.</p><h3>5. Conclusion</h3><p>Each AP provider has its strong points. And all seem fitting, depending on the use case.</p><p>For <strong><em>RaspAP</em></strong>, I’d stress it’s<strong> security</strong>, <strong>personalization</strong>, and <strong>versatility. </strong>For <strong><em>Wi-Fi Connect</em></strong>, I’d say that <strong>speed</strong>, <strong>scalability</strong>, and <strong>upgradability</strong> were the main points for adoption. For <strong><em>Network Manager</em></strong>, I’d highlight it’s <strong>speed</strong>, <strong>ease-of-use</strong>, and <strong>maintainability</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f364b9eb6c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Continuous integration and delivery at the pace of DevOps]]></title>
            <link>https://blog.devops.dev/continuous-integration-and-delivery-at-the-pace-of-devops-b3f23459aef1?source=rss-f663aa265e85------2</link>
            <guid isPermaLink="false">https://medium.com/p/b3f23459aef1</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[continuous-delivery]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[university]]></category>
            <dc:creator><![CDATA[Luis Costa Brochado]]></dc:creator>
            <pubDate>Mon, 12 Dec 2022 21:01:12 GMT</pubDate>
            <atom:updated>2024-09-18T15:08:20.098Z</atom:updated>
            <content:encoded><![CDATA[<h4>It’s May 2018 and I’ve been selected for a summer internship starting next month. Barely do I know the influence this moment has in shaping my future.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YJaX10DdZ3I8etc0PvYNVg.jpeg" /><figcaption>Source: <a href="https://www.pexels.com/pt-br/foto/predios-edificios-luzes-da-cidade-luzes-urbanas-14804708/">Ekam Juneja</a> from <a href="https://medium.com/u/5c7773c5a002">Pexels</a></figcaption></figure><blockquote>“<a href="https://www.infopedia.pt/dicionarios/lingua-portuguesa/movimento">mo.vi.men.to</a> —<strong> muviˈmẽtu</strong>:</blockquote><blockquote>1. ato de mover ou de se mover […]</blockquote><blockquote>4. evolução de ideias; agitação política</blockquote><blockquote>5. alteração, mudança […]”</blockquote><p><strong>Table of contents</strong></p><ol><li><a href="#3725"><strong>Opening remarks</strong></a></li><li><a href="#eed1"><strong>The internship</strong></a><br> —<a href="#c8eb"> The settlement</a></li><li><a href="#76af"><strong>Critical points</strong></a></li><li><a href="#109e"><strong>Closing remarks</strong></a></li></ol><h3>1. Opening remarks</h3><p>Back in mid 2018, while concluding a degree in software engineering, a determining chapter of my life was happening — and I barely noticed it! Even though I felt overwhelmed during that period, absorbing tons of information, I must recognize how important it was to be able to go through it.</p><p>Throughout the previous year up until then, I created and maintained a website and collaborated on organizing events as an aspiring junior entrepreneur, pro-bono, having the chance to ‘learn by doing’ and collaborating with other young enterpreneurs.</p><p>That idea of junior entrepreneurship would eventually fade away, but that mindset prevailed. In all honesty, it wasn’t an easy decision and, had I stayed on that organization, by the time Covid came, organizing the smallest event would have been a burdensome undertaking.</p><h3>2. The internship</h3><p>I started working in IT with a small team of trainees. As we learned on-site the basics of .NET – programming on frameworks like Xamarin or .NET Core – and got acquainted with the typical office environment for the following two months, we were also given time and room to make mistakes.</p><p>Helped by more experienced developers, the group engineered creative solutions cross-collaborating with one another. Then it got to a point where the next steps in professional or academic terms started being considered.</p><p>When time came to analyze proposals, the most important for me was to balance that decision between what was best at the time and what would payoff long term.</p><h4>The settlement</h4><p>I remember choosing between two rather interesting options. Amongst some other topics, the senior team had asked whether I was interested in either building a website, or a prototype of a continuous integration and delivery pipeline.</p><p>Having previously created a website via a CMS, doing the same with .NET would’ve been different. Alternatively, I wanted to work on something holistically meaningful, and even though continuous integration and delivery could sound unrealistic, it was also a lot more appealing.</p><p>Pipelines had a lot of potential to reduce repetitive tasks. That held a lot of promise. I was curious, maybe even a bit suspicious. What seemed clear though, was how everyone recognized its added value to the software development lifecycle. In the enterpreneurial vocabulary fancy stuff like: <em>added value</em>, <em>product development lifecycle</em>, <em>time to market, </em>were concepts I’d heard a lot before. To my delight, it was exciting.</p><p>To give a bit of context, the senior team was at the final stage of developing and shipping an application, a layered system, which had been under development for quite a while and was estimated to go to market within six months. The business was also interested in exploring the viability of a medium-to-long term migration of the monolith to micro-services. The strongest argument for that discussion was the lower cost of deploying and maintaining micro-services in the cloud. I loved it and made the decision to go forward with that topic.</p><p>In my understanding, anything that could offer that team the possibility of leveraging and continuously test the application’s backend, was for sure going to be useful.</p><p>At a certain stage I felt a kick of impostor syndrome. Coming from academia, I’d had virtually zero contact with DevOps. As it pictured more and more evidently, I had little to no clue what was I really getting into. I felt dumb quite frequently, but I also instinctively rest assured that feeling would eventually fade and sooner or later, all those new concepts would start making sense.</p><p>Being a .NET trainee, I was thrilled to work with senior developers. Implementing unit and integration tests, learning continuous integration, continuous delivery, docker, bash, PowerShell, and using different communications protocols was demanding, sometimes too demanding. But I always had help, or someone who I could count on when I was in need.</p><p>In conceptual terms, I designed a continuous integration and delivery ecosystem to support distributed development. By using Jenkins and GitLab to automate builds on a first stage, and execute unit and integration tests, the developers wouldn’t need to do those steps manually before deploying. That would free the team from that monotony and empower them to do what they’re expecting to — which is to develop code. On a second stage, static analysis would be automatically executed by the pipeline, to perform a lexical and synthatical code analysis. This would provide useful insights to the developers, and stop most of the code-related problems from reaching higher, pre-production environments. Besides the output from the static analysis, developers also had the output of the results of the unit and integration tests on Jenkins. This was the core of the pipeline.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*DHtp-EdXSwasGld5krSeGQ.png" /><figcaption>Output of the unit and integration tests’ execution on Jenkins — screenshot taken in 2019</figcaption></figure><p>I went a bit further and also suggested the inclusion of other components to the ecosystem, all of them using containers of Sonatype Nexus, Docker Registry 2.0, and SonarQube. For instance, on a later stage, when the eventual migration to micro-services had started, an artifact repository could be used to store binaries like NuGet packages, and docker images. Only at that stage would it make sense to start thinking about investing time on the automated build and tag of the micro-services’ images to a private image registry.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EoUNrXm9WbZ0LiochE3iHw.png" /><figcaption>The pipeline prototype schema — designed in 2019</figcaption></figure><p>I didn’t suggest an integrated solution to approach the semantic versioning topic, but I did implement a system to monitor the status of the pipelines with the use of a raspberry pi and an LCD that we used to play Mario kart on lunch breaks. I recall feeling like a kid at an amusement park as I pushed through it until it was finally closed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cvM_NlWKDg2rHKrusNFdfg.png" /><figcaption>An example of using Build Monitor —screenshot taken in 2019</figcaption></figure><p>The work amounted to nine months of on site investigation and five months of trimming and shaping the final report, which totaled in about 14 to 15 months.</p><h3>3. Critical points</h3><ul><li>The goal was to help a team of developers build something that they hadn’t put any investment into yet. Since I started from scratch, having something in the end, as little as it may be, it’s obviously better than nothing. That was clearly achieved.</li><li>If the intention was for the team to iterate on that work or not, it wasn’t my decision to take. If the team hadn’t enough confidence that taking the monolith to market was the best decision, that was also out of my control. The best strategy was going to production quickly, then migrate when possible – if possible.</li><li>The research went better than I expected, but it felt like I should’ve gone a bit further, particularly on semantic versioning, and project management practices. But I wasn’t working for a PhD. Even though that’s a possibility that remains open in the future.</li><li>With a bit more experience today, I confess that the most evident mistakes of the report were: writing too much; diverging frequently; and, lack of objectivity; These are professional aspects with which I struggle to improve to this day but, since experience brings many things, I put strategies in place to avoid it happening as frequently as it did.</li></ul><h3>4. Closing remarks</h3><p>It was quite a challenge, and many start this endeavor and don’t finish it. Being one of many that didn’t quit halfway through, I highly advise anyone in a similar situation — <strong><em>do not quit</em></strong>. I admit that there were moments in which I thought about it but remembered: <strong><em>if you don’t quit, you won’t regret it</em></strong>. Guaranteed.</p><p>Frequently DevOps is referred to as a role in which certain professional profiles fit into. As Gary Gruver outlines in <a href="https://page.gitlab.com/rs/194-VVC-221/images/Starting%20and%20Scaling%20DevOps%20in%20the%20Enterprise.pdf">Starting and Scaling DevOps in the enterprise</a>, DevOps was born as a movement that was intended to bring people and technology together in achieving a common goal. DevOps can be many things, platform, software engineering, cloud infrastructure, systems administration, network engineering, continuous integration, continuous delivery, application support, even product and project management. Personally, I think of DevOps as more of a continuous improvement oriented <strong>inclusion </strong>mindset, similarly to the junior enterpreneurship mindset I initially mentioned.</p><p>In spite of my mistakes, or my goals for self-improvement, I feel that the work made deserves to be credited. Everyone learned, and that’s what mattered. As for the pipeline and the concept, not sure if it saw daylight in production. Quoting Chief Wiggum in <a href="https://getyarn.io/yarn-clip/f1a8c41f-9f0a-41a2-9af3-87d8f995067d">The Simpsons movie</a>: “<em>Well… they’re China’s problem now”.</em></p><p>I’m humbly grateful to those who helped me made those decisions back then. Particularly, my advisors, and my psychologist. Credit to them for having helped me whenever they had the chance.</p><p>For those who want to take a look at the report — and speak Portuguese — I’ll link the submission <a href="http://hdl.handle.net/10348/9726">here</a>, where some reader and location <a href="https://repositorio.utad.pt/stats?level=item&amp;type=access&amp;page=downviews-series&amp;object=item&amp;object-id=10348/9726">statistics</a> are also available. On my GitHub, I’ve created a repository containing the document, and fixed some lexical mistakes without changing a single sentence.</p><p><a href="https://github.com/olevezinho/relatorio_de_estagio">GitHub - olevezinho/relatorio_de_estagio</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b3f23459aef1" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/continuous-integration-and-delivery-at-the-pace-of-devops-b3f23459aef1">Continuous integration and delivery at the pace of DevOps</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating a Twitterbot with Python: Basics, Logging, and Job Scheduling]]></title>
            <link>https://python.plainenglish.io/python-twitterbot-series-chapter-2-basics-logging-and-job-scheduling-dbe0a2dda11c?source=rss-f663aa265e85------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbe0a2dda11c</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[twitter-marketing]]></category>
            <category><![CDATA[twitter]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[bash]]></category>
            <dc:creator><![CDATA[Luis Costa Brochado]]></dc:creator>
            <pubDate>Thu, 02 Jun 2022 20:09:20 GMT</pubDate>
            <atom:updated>2023-04-14T19:52:09.994Z</atom:updated>
            <content:encoded><![CDATA[<h3>Python — Twitterbot series, Chapter 2: Basics, Logging, and Job Scheduling</h3><h4>Building the bot</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*Qy1p6jTOahy9-kVDDUEypA.png" /><figcaption>Cool trademark guidelines advised logos</figcaption></figure><p><strong>Table of contents</strong></p><ol><li><a href="#fcae"><strong>Introduction</strong></a><br> — <a href="#23c9">Recap</a></li><li><a href="#6443"><strong>Deciding the flow of interactions</strong></a></li><li><a href="#a0ad"><strong>Isolating authentication functions</strong></a><br> — <a href="#d3dc">Tweepy client</a></li><li><a href="#d959"><strong>Isolating twitter interaction functions</strong></a><br> — <a href="#64d8">Geography</a><br> — <a href="#8998">Hashtags</a><br> — <a href="#9f00">Main function and logging configuration isolation</a><br> — <a href="#8cb1">Cron</a></li><li><a href="#50c0"><strong>Critical points</strong></a></li><li><a href="#d93b"><strong>Resources</strong></a></li></ol><h3>1. Introduction</h3><p>Following the first <a href="https://python.plainenglish.io/python-twitterbot-series-chapter-1-automating-interactions-via-rest-api-b3007bc2a2b0">chapter</a>, in which interactions with the Twitter API were automated, this follow-up blog’s scope is to address Python basics, logging, and job scheduling.</p><p>Basics are important to lay down the foundation upon which the <strong>Twitterbot </strong>is<strong> </strong>going to be sitting on. This means main functionalities will be grouped into the main file, and portions of the code that are dedicated to specific actions will be split into different functions, according to their purpose. This is known as <a href="https://docs.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/architectural-principles#separation-of-concerns">separation of concerns</a>.</p><p>Logging information is fundamental in every application since it provides immediate feedback on what’s happening at each stage. Python’s logging mechanic, which is fairly simple to understand, will be explained in some detail.</p><p>Cron will provide consistency to the application and, given a configuration, help it behave systematically and reliably.</p><h4>Recap</h4><p>Before moving on, a quick recap. The script will be searching for hot topics, saving these hot topics to a file, and, only then, reading and favoring each one of these topics.</p><p>Before doing any of this, authentication had to be performed. If you recall part 1 of this tutorial, there’s a code snippet that contains the authentication functions using <strong>Tweepy</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e81d2e2c8a41d5105c4aa7b532d40095/href">https://medium.com/media/e81d2e2c8a41d5105c4aa7b532d40095/href</a></iframe><p>If you haven’t read chapter 1 of this series, this is where I highly advise doing so.</p><h3>2. Deciding the flow of interactions</h3><p>Simulating the behavior of a human interacting with twitter opens a broad array of discussions. It’s hard to abstract from the fact that the algorithm is going to be systematically either favoring a set of statuses or re-tweeting someone’s status. Given how decisive is deciding this repeated behavior, and how defining can it be in terms of reaching the goal of this bot — which is to ultimately draw attention to an account, and gain followers — I brought this topic to a discussion, to a team of software developers.</p><p>I already had the basic like mechanism setup, as well as the automation, using cron, and I presented it to that team. With this exposed, my goal was to create a brainstorm of ideas, to get a grasp on what could be used to introduce a healthy randomness factor to the algorithm. Some of the points they raised included:</p><ol><li>Reducing the number of hashtags and increasing the interaction with relevant users/topics.</li><li>Focusing on Twitter, creating content, and following trends.</li><li>Dynamically select a range of trends, and investigate the trending part.</li><li>For a specific language, select all popular hashtags within that language.</li><li>Within a topic, try to understand if its sentiment analysis is positive or negative.</li></ol><p>I took my time to analyze these five points raised and decided to base my developments on what I felt would have a wider reach and ultimately had the most seemingly natural behavior, without having an exaggerated amount of logic.</p><p>Given this, I automatically excluded point number 5. Points 1 and 4 were excluded right off the bat, because I also didn’t want to be following specific hashtags, on any particular language, because that seemed biased, unnatural, and invariable. Instead, to have a wider reach, I’d be searching for universal trends, which are definitely those that interest me the most. This left me with points 2 and 3, which seem pretty reasonable. To create content, I’d have to mostly do it by myself, but I could definitely follow trends, and investigate them further to try and set up a simple method to dynamically select a range of trends, and perform interactions upon them. I also kept in mind that my personal goal was learning a bit about python.</p><h3>3. Isolating authentication functions</h3><h4>TweepyClient</h4><p>I’ll start by creating a <strong>tweepy_client.py</strong> file. This file will contain the <strong><em>TweepyClient</em></strong> class, on which these functions will be placed. This file will contain the __init__() method and a tweepy_auth() function, which will accept four parameters:</p><ul><li><strong>API_KEY</strong></li><li><strong>API_SECRET</strong></li><li><strong>ACCESS_TOKEN</strong></li><li><strong>ACCESS_SECRET</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a85875c8ccc696053835c6bf7d62b46/href">https://medium.com/media/3a85875c8ccc696053835c6bf7d62b46/href</a></iframe><p>The __init__() method is Python’s constructor. It must be declared inside a class, or else the class can’t be initialized inside other files. Inside the method itself, I’ll be referencing all the properties the class will require in order to function correctly. That can be seen in the <strong>TweepyClient </strong>class above, from lines <strong>5 </strong>to <strong>8</strong>.</p><h3>4. Isolating twitter interaction functions</h3><p>Next, I created a <strong>Hashtags</strong> class inside a file called <strong>twitter_hashtags.py</strong>. This class will contain all the functions I used to automate the interaction flow.</p><h4>Geography</h4><ul><li><strong><em>Browsing and selecting locations</em></strong></li></ul><p>Searching for locations where to find trends can be done by using the api.available_trends() API call. This function will require you to provide your API info, as well as the location. This will be divided with the location code being stored in the <strong>Geography</strong> class, and some other bits of code stored in the <strong>Hashtags </strong>class.</p><p>The code that follows will be used to create a file and write all locations to it:</p><pre>available_loc = api.available_trends()<br># writing a JSON file that has the available trends around the world<br>with open(&quot;./available_locs_for_trend.json&quot;,&quot;w&quot;) as wp:<br>    wp.write(json.dumps(available_loc, indent=1))</pre><p>Up next, I will store my location as an argument variable, and get the location of my system as latitude and longitude:</p><pre>loc = sys.argv[0]     # location as argument variable<br>g = geocoder.osm(loc) # getting object that has location&#39;s latitude and longitude</pre><p>Only then, will I store all the trends inside an array, and save them on a text file. First, I will store all trends on the array:</p><pre># Writing trends to a json array<br>closest_loc = api.closest_trends(g.lat, g.lng)<br>array = api.get_place_trends(closest_loc[0][&#39;woeid&#39;])</pre><p>Here’s the <strong>Geography</strong> class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/229704ba4b54bebd3b5aec4aff50b8a5/href">https://medium.com/media/229704ba4b54bebd3b5aec4aff50b8a5/href</a></iframe><h4>Hashtags</h4><ul><li><strong><em>Writing tags to a file</em></strong></li></ul><p>Writing the tags to a file is done with a few lines of code, as can be seen below.</p><pre>with open(filename, &#39;w&#39;) as f:<br>    for data in array:<br>        for trends in data[&#39;trends&#39;]:<br>            f.write(trends[&#39;name&#39;]+&#39;\n&#39;)<br>return None</pre><ul><li><strong><em>Reading tags from a file</em></strong></li></ul><p>After saving the tags, the tags can be read with a couple of lines, and this will be the basic write and read workflow of the twitterbot.</p><pre>with open(filename, &quot;r&quot;) as f:<br>    list_of_hashtags = list(f)<br>return list_of_hashtags</pre><ul><li><strong><em>Liking posts</em></strong></li></ul><p>A function to like posts will need to go through each one of the hashtags contained on the <strong>hashtags.txt</strong> file and favor a post after some validations:</p><pre>for i in hashtags:<br>    logger.info(f&quot;tweets about {i}&quot;)<br>    # search for the tweets related to a specific topic<br>    for tweet in api.search_tweets(q=i, lang=&quot;en&quot;):<br>        status = api.get_status(tweet.id)<br>        # fetching the favorited attribute<br>        favored = status.favorited<br>        # if already liked, then skip, else like<br>        if favored == True:<br>            logger.info(&quot;The authenticated user already liked the tweet.&quot;)<br>        # like the tweet<br>        else:<br>            logger.info(f&quot;tweet id: [{tweet.id}] tweeted at: [{status.created_at}]&quot;)<br>            api.create_favorite(tweet.id)<br>            count += 1<br>            logger.info(&quot;The tweet was favored!&quot;)</pre><ul><li><strong><em>Logging and basic error handling</em></strong></li></ul><p>Error handling is a high priority for this particular bot, much due to the untreated return types the API responds with when the response statuses are not what’s expected. These untreated exceptions would make the application stop, which is suboptimal.</p><p>The exception types that’ll be treated are contained inside the <strong>tweepy</strong> library, and they will be thrown as <strong>TweepyException</strong> type. Blocks like the following one will be seen in some functions:</p><pre>try:<br>[...]<br>except tweepy.TweepyException as e:                                              <br>     logger.error(e)</pre><p>Using a logger requires importing the logging package. Most loggers have at least 4 levels of logging — <strong>debug</strong>, <strong>info</strong>, <strong>warning</strong>, and <strong>error</strong>. Python’s logger has another one, that logs <strong>critical</strong> messages. On the code snippet above, is an example of logging an error message with logger.error(e), where <em>“e”</em> is the message to be printed by the logger.</p><p>Logging information can be delivered in different formats. It can be either written to the console, or stored inside a file. But I will ellaborate on that later.</p><p>After fine-tuning the function to like tags, and adding all of these functions to the <strong>Hashtags </strong>class, the structure is as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ed1ee1a1584f50f9b0a06d300d3af30/href">https://medium.com/media/9ed1ee1a1584f50f9b0a06d300d3af30/href</a></iframe><h4>Main function and logging configuration isolation</h4><p>After putting in practice the concept of separation of concerns, it’s nice to see that what was in line to become a big and messy <strong>main.py</strong> file, with loads of module references, is now closer to become a lot more organized and well structured.</p><p>It’s still missing one last thing before displaying the main file:</p><ul><li><strong><em>Isolating the logging configuration on a file</em></strong></li></ul><p>Isolating the logging configuration on a file wasn’t as easy and straight forward to begin with. I ended up basing the configuration for this logger on a couple of stackoverflow posts, mainly because I felt the need to log both to the console and to a file, and it seemed that the documentation wasn’t pointing me in the right direction. Even though it was a bit tricky, I managed to come up with the following configuration working just as I wanted:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d4a164f2cd39485a783e5551a09855a/href">https://medium.com/media/7d4a164f2cd39485a783e5551a09855a/href</a></iframe><p>From what I was able to understand, I had to configure two handlers, one for the console output, and another one for the file output. As can be seen on line <strong>20</strong> and <strong>26</strong> respectively, the <strong>handler_consoleHandler</strong> is responsible for printing to the console, while the <strong>handler_fileHandler</strong> does the printing to the file. These handlers need to be referenced on the <strong>handlers </strong>tag on line <strong>4</strong>, and all the remaining configuration is required.</p><p>I’d advise to keep the level as NOTSET, as this will be a configuration essential to set the logging level as the minimum, which means that all logging types will be called. Additionally, on the main file, have a look at lines <strong>1</strong>, <strong>2</strong>, <strong>22</strong> and <strong>23</strong> on the <strong>main.py</strong> file.</p><ul><li><strong><em>The main function:</em></strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/321b68e61346d8dc28456b3635ced951/href">https://medium.com/media/321b68e61346d8dc28456b3635ced951/href</a></iframe><p>The tree view of the folder structure of this project is the following:</p><pre>.<br>├── .env<br>├── logging.conf<br>├── main.py<br>├── logs<br>│   └── twitterbot.log<br>├── available_locs_for_trend.json<br>├── hashtags.txt<br>├── tweepy_client.py<br>├── twitter_hashtags.py<br>├── twitter_geography.py<br>└── requirements.txt</pre><p>Be sure to have a closer look through the files, and digest the information you’ve read carefully. This is essential in order to properly understand all the stuff that was done. Also, <strong>remember to create the </strong><strong>logs/ folder if you’re cloning the GitHub repository I’ll be linking below</strong>.</p><h4>Cron</h4><p>Cron will be configured to periodically run a bash script which is merely enabling the virtual environment, and running the main function. I will not give this next one for free. I’ll leave below the logic so that you can follow it, but it needs to be adjusted depending on your paths, and depending on how you structured your projects:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2f0042188fbeea47af13db2b330070e/href">https://medium.com/media/f2f0042188fbeea47af13db2b330070e/href</a></iframe><p><strong>Cron </strong>will first need to be enabled, or else this might not even work by default. To follow this up, go to your bash terminal and type in crontab -e:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UBHrHYsB7UmV64NtnOqiUg.png" /><figcaption>WSL terminal view</figcaption></figure><p>This will open up the default configuration on your favorite text editor, which you’ll be using to set an automated action. Please do read the comments on this file. It contains an example of how to set an automated backup of all your user accounts on the system. On the last line of the file, yo’ll be adding something like:</p><pre>0 */12 * * * /home/&lt;path-to-your-script&gt;/cron-job.bash</pre><p>The previous line of code is the same as saying:</p><p><em>“Hey cron, I’d like you to run this cron-job.bash task for me every 12 hours, all days of the week, forever.”</em></p><p>The diagram below provides a visual representation of the Twitterbot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*VMub1oY4I6i1j9OVqEdcmA.jpeg" /><figcaption>Components diagram</figcaption></figure><h3>5. Critical points</h3><p>We’ve:</p><ul><li>Learned about python basics and separation of concerns;</li><li>Written and read data to and from files;</li><li>Learned about logging and exception handling;</li><li>Created a cron job;</li></ul><h3>6. Resources</h3><p><strong>Tools</strong></p><ul><li><a href="https://app.diagrams.net/">https://app.diagrams.net/</a>;</li><li><a href="https://code.visualstudio.com/">VS Code</a>;</li></ul><p><strong>Trademark guidelines:</strong></p><ul><li><a href="https://about.twitter.com/en/who-we-are/brand-toolkit">Twitter</a>;</li><li><a href="https://www.python.org/psf/trademarks-faq/">Python</a>;</li><li><a href="https://github.com/odb/official-bash-logo">Bash</a>;</li></ul><p><strong>Web content:</strong></p><ul><li><a href="https://github.com/">GitHub for gists</a>;</li><li><a href="https://code.visualstudio.com/docs/python/environments">Coding with VS Code</a>;</li><li><a href="https://docs.python.org/3/library/logging.html#logging-to-multiple-destinations">Python logging to multiple destinations</a>;</li><li><a href="https://docs.python.org/3/library/logging.config.html">Python logging configuration</a>;</li><li><a href="https://stackoverflow.com/questions/4441842/python-logging-configuration-file">StackOverflow response on logging configuration</a>;</li><li><a href="https://www.udacity.com/blog/2021/11/__init__-in-python-an-overview.html">The __init__ method</a>;</li><li><a href="https://betterprogramming.pub/how-to-work-with-json-files-in-python-bedb5b37cbc9">Working with Python and JSON files</a>;</li><li><a href="https://note.nkmk.me/en/python-pip-install-requirements/">What’s a <strong>requirements.txt </strong>file</a>;</li></ul><p><strong>Repositories</strong></p><p><a href="https://github.com/olevezinho/python-series/tree/main/chapter-2">python-series/chapter-2 at main · olevezinho/python-series</a></p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a><em> and </em><a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><em>. Check out our </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Community Discord</em></strong></a><em> and join our </em><a href="https://inplainenglish.pallet.com/talent/welcome"><strong><em>Talent Collective</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbe0a2dda11c" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/python-twitterbot-series-chapter-2-basics-logging-and-job-scheduling-dbe0a2dda11c">Creating a Twitterbot with Python: Basics, Logging, and Job Scheduling</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PowerShell — Linux-like history command]]></title>
            <link>https://faun.pub/powershell-linux-like-history-command-bcb0ded5b839?source=rss-f663aa265e85------2</link>
            <guid isPermaLink="false">https://medium.com/p/bcb0ded5b839</guid>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[scripting]]></category>
            <category><![CDATA[powershell]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[windows]]></category>
            <dc:creator><![CDATA[Luis Costa Brochado]]></dc:creator>
            <pubDate>Wed, 23 Mar 2022 23:15:19 GMT</pubDate>
            <atom:updated>2024-02-12T09:39:57.967Z</atom:updated>
            <content:encoded><![CDATA[<h3>PowerShell — Linux-like history command</h3><h4>Using “history | grep …” — the PowerShell version</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*irkOn6Z6RcmlREeWZuTniw.png" /><figcaption>Image source (<a href="https://github.com/exercism/meta/issues/81">https://github.com/exercism/meta/issues/81</a>) (<a href="https://docs.microsoft.com/en-us/powershell/scripting/community/digital-art?view=powershell-7.2">https://docs.microsoft.com/en-us/powershell/scripting/community/digital-art?view=powershell-7.2</a>)</figcaption></figure><p>Table of Contents</p><ol><li><a href="#6f9a"><strong>Introduction</strong></a></li><li><a href="#d85b"><strong>Cloning Unix history command</strong></a><br> — <a href="#1401">The core concept</a><br> — <a href="#3e30">PowerShell’s take on history</a><br> — <a href="#aa13">Leveraging history consistently</a><br> — <a href="#4885">Piping output to Select-String</a></li><li><a href="#d125"><strong>Conclusion</strong></a></li><li><a href="#ea53"><strong>Resources</strong></a></li></ol><p>For software developers, systems administrators, or enthusiasts that use a lot of command line tools, it might sometimes be frustrating to press up arrow repeatedly on your command prompt, just so you can reach the cool command you’ve used moments ago, that you can’t quite remember later.</p><p>For those who just want to know how to configure this command on their profiles, just jump right to the end.</p><h3>1. Introduction</h3><p>PowerShell has been around for quite some time now. If you want to count in from the date when <a href="https://www.jsnover.com/Docs/MonadManifesto.pdf"><strong>Monad</strong></a><strong> </strong>came along, PowerShell has been around for almost 20 years at the time of writing. Even if you count starting from the year Monad was rebranded as PowerShell, and brought up to the market, you’ll find that 15 years have passed. It’s awesome.</p><p>In my point of view, PowerShell’s uniqueness relied in how it enabled Windows servers to be managed at scale. Plus, its role is also similar to python’s for Unix systems. Fundamentally, it’s an object oriented scripting language, that’s used as an automation engine, and a command line shell.</p><h3>2. Cloning Unix history command</h3><p>This is one of the reasons Linux/BSD administration is so easy to learn, but so hard to master.</p><p>Before developing and explaining this blog, there’s an honorable mention that I’d like to make, to the Get-History cmdlet, because it can also be useful. This cmdlet retrieves the history of the current session. It’s quite nice, with the exception that if you close your terminal and reopen it, all your history is gone.</p><h4>The core concept</h4><p>When I used to work with systems administrators and analysts, I was constantly amazed at their ability to remember commands. They knew thousands of them, for all purposes. Each and one of those commands which prompt the proper results, along with tools that, when coupled with them, output things all tidy and nice.</p><p>Truth is — not undermining systems engineers and their importance — most of the times, they don’t remember the commands themselves. And that was when I realized, I didn’t need to know them either, but at least I had to know their purpose, for the most part. I only had to know history. More specifically, history | grep &#39;&lt;something&gt;&#39;, something being a keyword, an argument or a flag used on that command which I can’t quite remember.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TepzVLqSaLMpcvTfCbaoeA.png" /><figcaption>Nice example of ‘history | grep’ usage</figcaption></figure><h4>PowerShell’s take on history</h4><p>I’ve struggled to find an out-of-the-box PowerShell command similar to Linux’s history, so I set out to make my own powershell history command. I found out a lot of information on PSReadline.</p><p>Microsoft created a huge manual for <em>it</em>, I’ll link it <a href="https://docs.microsoft.com/en-us/powershell/module/psreadline/about/about_psreadline?view=powershell-5.1">here</a>.</p><p>My first thought was to search for the place where PowerShell saves the information it needs to display to the user when up arrow is pressed. For <em>that </em>to be made, in some way, the commands need to be saved. That was when I read about the <strong>Get-PSReadLineOption</strong> cmdlet. This cmdlet contains the <strong>HistorySavePath</strong><em> </em>parameter, which got my attention:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MuVJEBAB__thD1U7d2H1fQ.png" /><figcaption>Get-PSReadLineOption cmdlet output</figcaption></figure><p>Highlighted in yellow above, this parameter points to the file where all the history from all powershell sessions is saved. That’s half-way through to the goal. Now it’s just a matter of printing that file to the screen, and querying it for some records.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/1*gQ0iok443Ez_nhcC56heJw.png" /><figcaption>History file path</figcaption></figure><p>I chose Get-Content (or cat) to print the output of the screen. Then, all that information was saved to a variable named $historyPrint:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/939e2f780bf6cc4b76a3cbb2bf501d5e/href">https://medium.com/media/939e2f780bf6cc4b76a3cbb2bf501d5e/href</a></iframe><p>This will print all of your previously used PowerShell commands. But, to be able to match the history command, there’s still one thing missing. I wanted to append the line numbers to the output of each command. This one was a bit trickier, but still fairly easy to achieve. There’s the Get-Content and the ForEach-Object cmdlets, which fit the needs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6033205481a20d73017c4bf0cc59df05/href">https://medium.com/media/6033205481a20d73017c4bf0cc59df05/href</a></iframe><p>On the code lines above I used cat and % which are aliases for Get-Content and ForEach-Object respectively.</p><h4>Leveraging history consistently</h4><p>To conclude, I added the previous two lines to my PowerShell profile. That can be done with the $profile PowerShell variable. Typing that on a powershell terminal will output the path of the file, which is loaded into your powershell session when you open a terminal.</p><p>This is the file you need to save the terminal configuration into.</p><p>At this point, these two commands are available all the time and I’m able to use $history to list all the previous commands I’ve used.</p><h4>Piping output to Select-String</h4><p>The goal was to use history | grep &#39;something&#39;, and PowerShell is capable of doing precisely the same, except with a different cmdlet.</p><p>Meet <strong>Select-String</strong>.</p><p>So, by piping the output of $history to it, and passing a string, one can filter the output by <em>that</em> specific string. To further shorten the command, use the alias sls :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*eXN7aTRm68WiOgfCabgZDA.png" /></figure><h3>3. Conclusion</h3><p>PowerShell is a powerful tool. It has lots of purposes, and can be used throughout a wide variety of applications. Additionally, PowerShell recently went cross-platform, remaining highly customizable and, in many contexts, perfect for productivity.</p><h3>4. Resources</h3><h4>Trademark guidelines:</h4><ul><li><a href="https://docs.microsoft.com/en-us/powershell/scripting/community/digital-art?view=powershell-7.2">Microsoft</a> PowerShell Core;</li></ul><h4>Web content:</h4><ul><li><a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.2">Microsoft ForEach-Object docs</a>;</li><li><a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content?view=powershell-7.2">Microsoft Get-Content docs</a>;</li><li><a href="https://www.jsnover.com/Docs/MonadManifesto.pdf">Monad Manifesto</a>;</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*-9tYt6j2RdBtNzD_.png" /></figure><h4>If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇</h4><h4>🚀Developers: Learn and grow by keeping up with what matters, <a href="https://faun.to/8zxxd">JOIN FAUN.</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bcb0ded5b839" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/powershell-linux-like-history-command-bcb0ded5b839">PowerShell — Linux-like history command</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python — Twitterbot Series, Chapter 1: Automating interactions via REST API]]></title>
            <link>https://python.plainenglish.io/python-twitterbot-series-chapter-1-automating-interactions-via-rest-api-b3007bc2a2b0?source=rss-f663aa265e85------2</link>
            <guid isPermaLink="false">https://medium.com/p/b3007bc2a2b0</guid>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[twitter]]></category>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Luis Costa Brochado]]></dc:creator>
            <pubDate>Tue, 25 Jan 2022 19:14:44 GMT</pubDate>
            <atom:updated>2023-04-14T19:53:35.787Z</atom:updated>
            <content:encoded><![CDATA[<h3>Python — Twitterbot Series, Chapter 1: Automating Interactions via REST API</h3><h4>Chapter 1: Automating interactions on social networks.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*l8Ec6Vc1ovg-Wu8WxdxeAA.png" /><figcaption>Cool trademark guidelines advised logos</figcaption></figure><p><strong>Table of Contents</strong></p><ol><li><a href="#0b31"><strong>Introduction</strong></a><br> — <a href="#8a6f">How come?</a></li><li><a href="#c074"><strong>Context and Challenge</strong></a><br> — <a href="#cc14">Why Twitter?</a><br> — <a href="#6985">Why python?</a><br> — <a href="#34d1">Goal</a></li><li><a href="#5795"><strong>Twitter bot</strong></a><br> — <a href="#5c8b">Good documentation is a game-changer</a><br> — <a href="#7ccc">Requesting twitter developer API access</a><br> — <a href="#25cd">Requirements</a><br> — <a href="#64b6">Virtual environments</a><br> — <a href="#2604">Authentication</a><br> — <a href="#c77a">Python module — Tweepy</a><br> — <a href="#f195">Calling the API</a><br> — <a href="#8cff">Improving the script</a><br> — <a href="#9235">Liking and retweeting</a></li><li><a href="#1d63"><strong>Critical points</strong></a><br> — <a href="#dac3">Follow up</a></li><li><a href="#ebd2"><strong>Resources</strong></a><br> — <a href="#8d4d">Tools</a><br> — <a href="#ae96">Trademark guidelines</a><br> — <a href="#4055">Web Content</a><br> — <a href="#3c11">Repositories</a></li></ol><h3>1. Introduction</h3><p>It’s not a prime necessity for me to become a social networking shark. I do admit that it has its perks, as well as a very useful purpose when it comes to sharing information. Without social networks, it’s a bit hard to reach a target audience.</p><h4>How come?</h4><p>Whether we’re thinking about personal blogs, or social media platforms like Reddit, Facebook, Instagram, Twitter, Pinterest, Quora, LinkedIn, etc, the opinions are generally positive. As this social networking movement grew, since the early 2000s, networking from a distance became possible and evolved.</p><p>In addition to that growth, an undisputable fact started to be recognized, more and more often, by members from all sorts of social <em>strata</em>: Social networks had changed their lives, some for the better, some for the worst. But the impact was there, regardless.</p><p>I do feel that, on social networks, the interactions I perform with other people are (and always have been) fundamentally the same for, say, the last 5–10 years. Meaning that, when I have enough time, most of the stuff I do is either scrolling through the feed, searching for a topic, or liking status updates. My intention is not to discredit the people who have put in the effort to develop and evolve these platforms, at all. My intention is to use my programmatic knowledge and take advantage of what’s available.</p><h3>2. Context and challenge</h3><p>I use mostly C# on daily basis, but I also enjoy using python, here and there, depending on the context. With that said, I’m always looking forward to finding motivating methods and improving my skills with different languages. New languages bring new modules, new methods, new concepts, and overall functionality. This was where I found a sweet spot with Twitter and Python.</p><h4>Why Twitter</h4><p>I don’t use Twitter that much, but I see it gets used more and more often. It’s perceivable, in my opinion, that Twitter has all interest in you becoming a developer and it provides a RESTful API, that you can access with ease.</p><p>Another important factor is the API’s request limiter, which on Twitter is a bit higher than in other cases. This enables the user to have more interactions, and reach a wider range of interests.</p><h4>Why python</h4><p>In my opinion, Python is by far the best tool if you want to do something small which behaves reliably without investing a ton of time. To name a few advantages of Python:</p><ol><li>There’s a huge (as infinite) amount of documentation;</li><li>A community that understands Python’s purpose, common difficulties, and limitations;</li><li>A broad usage of, and maintenance of, a wide variety of modules;</li><li>Different approaches to solve similar problems;</li></ol><h4>Goal</h4><p>The goal of this article is to explain how and under which frameworks am I using twitter’s modules for python. I will instantiate a very basic like and retweet mechanism, that logs information to the console, and optionally saves data into a database while browsing for topics of my interest. Not very surprisingly, it will be called <strong>Twitterbot</strong>.</p><p>Ultimately this will bring attention to your Twitter account, and most likely get you followers, even though the retention rate <strong>may be low</strong> and <strong>vary</strong>, depending on the topics you’re following.</p><h3>3. Twitter bot</h3><p>I’m aware that I’m not the first person that writes about this topic. I’ve been using a lot of similar information found across different blogs, to write and develop this specific bot. Furthermore, on this post will be shared most of the information I’ve followed, as well as some of the official documentation I’ve consulted to implement the core functionalities.</p><h4>Good documentation is a game-changer</h4><p>There’s nothing to argue about how crucial good documentation is, and how it influences decisions we, as developers, make. Especially to the detriment of outdated documentation.</p><h4>Requesting twitter developer API access</h4><p>Access the Twitter developer page <a href="https://developer.twitter.com/">here</a>. Do some Google queries about this to get started, search for some youtube videos about it, and you should be able to find all you need.</p><p>Be honest with why you’re requesting access so that Twitter doesn’t get suspicious about your intentions. To give my example, I chose some options, like <em>“Exploring the api”</em> being the reason why I requested API Keys. This is a more than reasonable motive, in my opinion.</p><p>Copy the keys that Twitter provides you at the end of the configuration:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/1*oa34FYARyWXZDB-5-d0fsw.png" /><figcaption>VIP access credentials</figcaption></figure><p>These are quite important, so don’t forget to <strong>save </strong>them. My suggestion would be to save them on some sort of app like <a href="https://keepass.info/">KeePass</a>, or a similar tool — these tools are amazing. You’ll be using these keys later on.</p><h4>Requirements</h4><ul><li>WSL 2/Linux VM;</li><li>Python 3 (I used <a href="https://www.python.org/downloads/release/python-373/">3.7.3</a>);</li><li>Text editor (I use VS Code);</li></ul><p>I would advise following this guide on WSL, or at least make sure you’ve got a Linux virtual machine at hand. You can make some google queries about that and get back at this guide, as soon as you’re finished with setting up the development machine.</p><figure><img alt="This is the official image of WSL, with a sitting penguin grabbing the Initials WSL" src="https://cdn-images-1.medium.com/max/539/0*kLMrqmnKXxh3mCvJ.png" /><figcaption>Image source: <a href="https://www.google.com/imgres?imgurl=https%3A%2F%2Fdocs.microsoft.com%2Fcs-cz%2Fwindows%2Fimages%2Fwindows-linux-dev-env.png&amp;imgrefurl=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fwsl%2F&amp;tbnid=iTlx3eoHiKKbQM&amp;vet=12ahUKEwicyZexydT1AhWGwuAKHcVjC9YQMygAegUIARCwAQ..i&amp;docid=bKKFeAnXnIB9jM&amp;w=539&amp;h=303&amp;itg=1&amp;q=windows%20subsystem%20for%20linux%20logo&amp;ved=2ahUKEwicyZexydT1AhWGwuAKHcVjC9YQMygAegUIARCwAQ">https://docs.microsoft.com/en-us/windows/wsl/</a></figcaption></figure><p>By now you should be asking yourself:</p><blockquote>“Okay, but why not do this on windows?”</blockquote><p>If you’re doing this on Windows, you might get some problems in later stages, where the script’s execution is timed with the help of bash and crontab.</p><p>Ideally, you’d want to have things done in a sandbox, where you can mess around with python packages, and potentially ruin a playground that you can easily erase, and start over from scratch if needed.</p><p>You’ll be creating a python bot inside a <a href="https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments"><strong><em>virtual environment</em></strong></a>. Find all about this cool concept on <a href="https://docs.python.org/3/tutorial/venv.html">python’s official documentation</a>. Use conda or just a plain <em>python venv, </em>I’ll be choosing the latter.</p><p>One of my limitations at the time of writing this code is knowledge of Python’s native concepts and development architectures. But, since I made a new year’s resolution where I refuse to develop software following poor coding practices, I’ll be trying my best to separate everything across distinct files, as soon as the idea is clear, and the base functionality is well perceived.</p><h4>Virtual environments</h4><p>Start by creating a folder and call it <strong>Twitterbot</strong>. Open your terminal, because you will start learning some cool commands. With Python it’s done with:</p><p>python3 -m venv twitterbot-env</p><p>This command will create a virtual environment called <strong>twitterbot-env</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*ltzkAkj3mM2FPaW6Nl2D5A.png" /><figcaption>Folder structure of the virtual environment</figcaption></figure><p>Package management is soon to become so easy, so refreshing, so clear … I lack the words to describe how much more organized your codebase has just become with this simple step, without dropping a single line of code. You’ll recognize the importance of this simple step later on, on stages where you won’t thank yourself enough for having done this. If you don’t believe me, read <a href="https://stackoverflow.com/questions/23948317/why-is-virtualenv-necessary">this stack overflow answer</a>.</p><p>Activating environments with python is also simple. Have a look at the folder structure in the picture above. Inside that bin/ folder, you’ll notice the existence of some <strong>activated</strong> files. These files will enable you to activate the sandbox which you’ll be using from now onwards.</p><p>Load that script into memory by using:</p><p>. twitterbot-env/bin/activate</p><p>The (twitterbot-env) prefix will indicate that you’ve successfully activated the virtual environment.</p><p>Since authentication can be confusing — given that it’s so important — and can follow different patterns, the next subsection will be fully dedicated to demystifying it.</p><h4>Authentication</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*6BZoinCqh-qqkdiYnyJSnA.png" /><figcaption>Developer portal menu</figcaption></figure><p>To better understand the authentication process, go back to <a href="http://developer.twitter.com/">developer.twitter.com</a> webpage.</p><p>On the portal, you’ll find a menu where you’ll be able to access your projects</p><p>On the created application, you’ll need to edit the user authentication settings, so that you can define whether you’re going to use <strong>GET</strong> requests, <strong>GET</strong> and <strong>POST</strong> requests, or even add direct messaging to your requests:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*f4IvQQtsLCfub4jcfF2mAQ.png" /><figcaption>User authentication settings menu</figcaption></figure><p>On this particular bot, I selected the <strong>Read/Write</strong> option, since there’s no point for me to direct message other users:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*Sbpm0dHp8_2TM-zzW7et9w.png" /><figcaption>App permissions</figcaption></figure><p>For the remaining options, select a callback URI, and a Website URL. I used <a href="https://linktr.ee/luiscostabrochado">my personal blog</a> — which I hereby urge you to visit regularly if you’re interested in both software development and operations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*ebrKK8o7QAMZtp2kfDqSEA.png" /><figcaption>General authentication settings</figcaption></figure><p>Now, to authenticate to Twitter API, you’re required to use four different strings. You’ve been provided with three so far:</p><ul><li>API Key;</li><li>API Key Secret;</li><li>Bearer Token;</li></ul><p>This OAuth method does not require the <strong>Bearer Token</strong>, but it does require the <strong>API Key,</strong> the <strong>API Key Secret</strong>, and two more tokens, which were not yet requested: <strong>Access Token</strong>, and the <strong>Access Token Secret</strong>. These tokens can be requested on the <strong>Keys and Tokens</strong> tab:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*GAXlUlwlQO1QAj-Y_pB2KQ.png" /><figcaption>Generating the Access Token and Secret</figcaption></figure><p>Make sure to save them once again, so you don’t lose them:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*Wusn4i8kOlP9ic03xg7Biw.png" /><figcaption>Access Token and secret</figcaption></figure><p>Now that the other two tokens were requested, an API caller, which will be used on all request headers, will be instantiated.</p><p>You can use <a href="https://www.tweepy.org/"><strong>Tweepy</strong></a>, <a href="https://mike.verdone.ca/twitter/"><strong>Twitter</strong></a>, or other python modules. I’ll explain the instantiation of both but will only follow through with Tweepy.</p><h4>Python module — Tweepy</h4><p>The package can be installed through the command line, with:</p><p><em>pip3 install [package-name]</em></p><p>With Tweepy, the authentication is made with four lines of code. First, you invoke the <em>OAuthHandler</em>, with the <strong>API Key</strong> and the <strong>API Key Secret</strong>, and then you pass in the <strong>Access Token</strong> and the respective <strong>Secret</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5c1be2ce1bbf1fe8e264dc315cdbf98/href">https://medium.com/media/f5c1be2ce1bbf1fe8e264dc315cdbf98/href</a></iframe><h4>Calling the API</h4><p>To give a good example, I want to search for some topics related to Python and view the information inside them. This can be done by appending:</p><p>api.search_tweets(q=&quot;python, lang=&quot;en&quot;)</p><p>to the end of the first (Tweepy) authentication file. This will retrieve a list of recent Tweets containing the Python keyword, or hashtag, so be sure to assign it to a variable named tweets.</p><p>Now, since you have a list of tweets, you’ll ideally want to iterate through each tweet. Validate that by printing some information about each one of those tweets. That can be done in the following way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a65adb35dd43a1844695c53e1b1cdb8c/href">https://medium.com/media/a65adb35dd43a1844695c53e1b1cdb8c/href</a></iframe><p>You can replace your credentials on the script above, and run the script to view if the authentication and query for “python” tweets were successful:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i7nLFd1rNKO_7NP2LKM-iQ.png" /><figcaption>authentication.py script</figcaption></figure><p>Pretty easy, and it should print out some nice information, like the <em>tweet id</em>, the <em>screen name</em> of the user that posted the tweet, and the tweet <em>text</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tP62BLUYGndZ2oXwm--5Lg.png" /><figcaption>Tweet info logged to the console</figcaption></figure><h4><em>Improving the script</em></h4><p>We have important data inside the script, which is less than ideal. But since we agreed (hopefully) to try our best and follow good coding practices, we’ll be storing the credentials we’re using, inside a .env file, which can later be ignored, in case we need to push the code to a remote repository. This is really common when you need to define private variables or access credentials that you don’t want to be exposed. Add the following to the file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/657/1*8LunR8N3htgziCUAyAPKWg.png" /><figcaption>.env file</figcaption></figure><p>Next, get back at the python file and, on your terminal, add python-dotenv package to your virtual environment, like so:</p><p>pip3 install python-dotenv</p><p>We’ll be using the <strong>os </strong>module, along with the load_dotenv() function from the <strong>dotenv</strong> module, where we’ll be defining the names for the authentication credentials. Make the following changes to the authentication.py script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c383d56ddf862cda0ae0069b1bc9bd42/href">https://medium.com/media/c383d56ddf862cda0ae0069b1bc9bd42/href</a></iframe><p>Be sure that the credentials string inside the os.getenv() functions on lines 7, 8, 9, and 10, match the names defined inside the .env file. I’ll drop a small schema displaying my mental model about what was just made:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*uoG7ARvOEpn1F2U71O2Meg.png" /><figcaption>WSL and the authentication.py script</figcaption></figure><h4>Liking and retweeting</h4><p>That’s it, we’re only missing two lines of code, to like and retweet the tweets. The functions required to do this are the create_favorite() and the retweet() functions. Both of them consume the <strong>tweet id</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a56092b3625ca2d685e49c54511ba99/href">https://medium.com/media/8a56092b3625ca2d685e49c54511ba99/href</a></iframe><p>Since this is part of a series, I chose not to make conclusions. Instead, I will point out some of the critical bits and pieces of information that we’ve gone through in each section.</p><p>That’s it for now, no need to dive much deeper.</p><h3>4. Critical points</h3><p>We’ve:</p><ol><li>Learned how to create and activate a virtual environment;</li><li>Managed authentication;</li><li>Used REST, even if you didn’t notice it;</li><li>Learned good coding practices;</li><li>Used a bit of Linux;</li><li>Interacted with the community without opening <a href="https://twitter.com/">twitter.com</a>;</li></ol><h4>Follow up</h4><p>In the next part, you’ll be learning new stuff, mainly about <a href="https://www.techopedia.com/definition/7882/job-scheduling">job scheduling</a>, <a href="https://docs.python.org/3/library/logging.html">logging</a>, and the concept of <a href="https://en.wikipedia.org/wiki/Separation_of_concerns">separation of concerns</a>:</p><ul><li>Cron;</li><li>Reading tags from a file;</li><li>Logging to a file or to the console;</li><li>Isolating authentication functions into a class;</li><li>Isolating the logging part on a file.</li></ul><p>Thanks for reading, hope you enjoyed this article!</p><h3>5. Resources</h3><h4><strong>Tools</strong></h4><ul><li><a href="https://app.diagrams.net/">https://app.diagrams.net/</a>;</li><li><a href="https://code.visualstudio.com/">VS Code</a>;</li></ul><h4>Trademark guidelines:</h4><ul><li><a href="https://about.twitter.com/en/who-we-are/brand-toolkit">Twitter</a>;</li><li><a href="https://www.python.org/psf/trademarks-faq/">Python</a>;</li><li><a href="https://github.com/odb/official-bash-logo">Bash</a>;</li></ul><h4>Web content:</h4><ul><li><a href="https://www.udemy.com/course/build-a-twitter-bot-with-python-tweepy-and-the-twitter-api/">Tweepy course (Udemy)</a>;</li><li><a href="https://github.com/">GitHub for gists</a>;</li><li><a href="https://code.visualstudio.com/docs/python/environments">Coding with VS Code</a>;</li><li><a href="https://python.plainenglish.io/i-made-a-twitter-bot-using-tweepy-ab62c7074402">Article: How I made a Twitterbot using Tweepy</a>;</li><li><a href="https://towardsdatascience.com/building-a-twitter-bot-with-python-89959ef2607f">Article: Building a Twitter bot with Python</a>;</li><li><a href="http://www.di.ubi.pt/~jpaulo/ensino/TRS/T/A04.pdf">UBI: Tecnologias de Redes Sociais</a>;</li></ul><h4>Repositories</h4><ul><li><a href="https://github.com/olevezinho/python-series/tree/main/chapter-1">python-series/chapter-1 at main · olevezinho/python-series</a></li><li><a href="https://github.com/Jman4190/udemy-twitter-bot/">GitHub - Jman4190/udemy-twitter-bot: Repo used for creating a twitter bot that retweets my likes</a></li></ul><p><em>More content at </em><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Get exclusive access to writing opportunities and advice in our </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>community Discord</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b3007bc2a2b0" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/python-twitterbot-series-chapter-1-automating-interactions-via-rest-api-b3007bc2a2b0">Python — Twitterbot Series, Chapter 1: Automating interactions via REST API</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bash — Operating git and GitHub through SSH]]></title>
            <link>https://blog.devops.dev/bash-operating-git-and-github-through-ssh-e6a0ad5ceb00?source=rss-f663aa265e85------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6a0ad5ceb00</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[wsl-2]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[bash]]></category>
            <dc:creator><![CDATA[Luis Costa Brochado]]></dc:creator>
            <pubDate>Wed, 13 Oct 2021 08:56:59 GMT</pubDate>
            <atom:updated>2023-04-18T15:46:27.969Z</atom:updated>
            <content:encoded><![CDATA[<h3>Bash — Operating git and GitHub through SSH</h3><h4>This is the story about why I wrote a bash script to automate a workflow of git operations</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*iOPDTLUlV5ASMVRhizLNRw.png" /><figcaption>Some cool and (hopefully) compliant logos</figcaption></figure><h4>Table of contents</h4><ol><li><a href="#d486"><strong>How come?</strong></a><strong><br></strong> — <a href="#2226">Top tips</a></li><li><a href="#7638"><strong>Pre-requisites</strong></a><strong><br></strong> — <a href="#e8d5">SSH authentication</a></li><li><a href="#02d1"><strong>Creating the repository and pushing the changes</strong></a><strong><br></strong> — <a href="#e14b">Create the repository with GH</a><strong><br></strong> — <a href="#dcdd">Workflow of a basic code check-in</a></li><li><a href="#99f7"><strong>Improving the script</strong></a><br> — <a href="#d4e3">Workflow with alternate paths — external arguments</a></li><li><a href="#1166"><strong>Resources</strong></a></li><li><a href="#7d34"><strong>Conclusion</strong></a></li></ol><h3>1. How come?</h3><p>One day I felt the need to write a bash script to automate git operations. My main objective was to avoid repeatedly typing the same commands to be able to check in code. My main motivation was that, at the very least, if I figured it was too complex for me, I’d learn new stuff about scripting.</p><p>Initially, the problem was easy and clearly understood:</p><blockquote>“I want to perform git operations, through SSH, with GitHub in a consistent way, but I don’t want to write all the git commands, over and over again. It would be optimal to do it in an automated Q/A prompt-like flow.”</blockquote><p>In honesty, part of the problem could easily be solved, since pushing code changes to GitHub via HTTPS is way simpler than pushing them through SSH. But, as I wanted to keep using secure-shell, this led me through a brief odyssey of investigation and self-improvement.</p><h4>Top tips</h4><p>Avoid as many problems as you can by <strong>fact-checking</strong>, and starting from the <strong>absolute </strong>and <strong>undeniable truths</strong>:</p><ul><li>A Shell script is interpreted at runtime;</li><li>Developing bash scripts requires Linux knowledge;</li><li>Using external modules or functions in bash, can be done by loading a script into memory during runtime execution;</li></ul><h3>2. Pre-requisites</h3><p>If you already know how SSH keys work, and you’re reading this simply to generate an SSH key, adding it to your account, and testing the connection, follow <a href="https://docs.github.com/pt/authentication/connecting-to-github-with-ssh"><strong>GitHub’s documentation</strong></a>. On the other hand, if you don’t know what SSH keys are, and how the SSH protocol works, you should probably <a href="https://www.ssh.com/academy/ssh-keys">have a look into that</a> before diving in.</p><p>These are the requirements to follow along:</p><ul><li>Install <a href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git">git CLI</a>;</li><li>Install <a href="https://cli.github.com/manual/installation">github CLI</a>;</li><li><a href="https://docs.github.com/pt/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent">Create SSH keys</a> and <a href="https://docs.github.com/pt/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account">setup SSH authentication</a>;</li></ul><h4>SSH authentication</h4><p>Assuming the previous <strong>GitHub documentation was followed</strong>, SSH should have been added to your settings. By now you should be at stage where you <a href="https://docs.github.com/pt/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection">tested and validated the SSH connection</a>. So, if the connection was successfull, a message similar to the following should be displayed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gB06yzM1ji6mv6Yh6xyUdw.png" /><figcaption>SSH connection established with GitHub</figcaption></figure><p>Log into your GitHub account with gh auth login. I will be using Debian WSL, which behaves like any other Linux distro. If you’re doing this on Windows, you should be fine, it is the same. If not, and you’re attempting to do this on a Mac, follow the appropriate documentation.</p><p>Either way, the steps should be similar to the ones below. It’s important to mention that you must remember that we’re sticking to <strong><em>SSH</em></strong>. After that, if you’ve already <strong>uploaded </strong>your <strong>SSH key</strong> to GitHub, be sure to <strong><em>skip</em></strong><em> </em>that step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*P_fsOVxX2nBEcVLQpGHMzQ.png" /><figcaption>github CLI — login</figcaption></figure><p>On the image above, notice the <strong>Tip</strong> which advises the user to generate a <strong>PAT </strong>— Personal Access Token. This will be displayed if the user selects the option to <strong><em>Paste an authentication token</em></strong>.</p><h3>3. Creating the repository and pushing changes</h3><p>For the current stage, I suggest you to open <a href="https://docs.github.com/en/github/importing-your-projects-to-github/importing-source-code-to-github/adding-an-existing-project-to-github-using-the-command-line#adding-a-project-to-github-with-github-cli"><strong>gh</strong> CLI</a> docs on creating repos.</p><h4>Create the git repository using GH</h4><p>Create a GitHub repository for your code. To do this, have a look on the <a href="https://cli.github.com/manual/gh_repo_create">CLI</a>’s options, namely the usage of gh repo create:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*PEIBTkkV51KDu2MRZp7Flw.png" /><figcaption>github CLI — repository creation through terminal</figcaption></figure><p>Be sure to use git pull --set-upstream origin main and make an initial commit. The script was not designed to be used out-of-the-box. So, it doesn’t perform that step.</p><p>Ideally, before every commit, one should pull from the main branch, and integrate into the local branch. This is specially useful when working within a development team, since most of the times your colleagues will be pushing changes to <strong>main</strong>. This will make it easier if you need to solve conflicts locally.</p><h4>Workflow of a basic code check-in</h4><p>To start, the workflow will be kept simple. Assuming you’ll be working by yourself, only git pull , git add, git commit , and git push commands are needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*y8HkVXrfVpFSarCZ8uM23Q.png" /><figcaption>Overly simplified workflow for the git commands</figcaption></figure><p>The visual representation of the git commands is simple enough for them to be integrated into a script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa5570759783b939dbda05a863aedba8/href">https://medium.com/media/fa5570759783b939dbda05a863aedba8/href</a></iframe><p>Starting from the top. On line <strong>1</strong> there’s a #!/bin/bash, a basic command, that’s present on every shell script. This is composed by a shebang #! and the script language /bin/bash, which will be used to interpret the set of commands.</p><p>Another important mention is the return status of the last command, which is represented by the $? symbol. If a command is successful, its return status is 0, if not, it differs. The read<strong> -p </strong>command did cut out from the script some unecessary echo calls, making it more objective and smaller. The flow of git commands previously drawn can be seen on lines <strong>5</strong>, <strong>15</strong>, <strong>17 </strong>and <strong>19</strong>. Before runnig the script, execution permissions must be given to it, with chmod a+x &lt;script-path.bash&gt;. List the file to see its permissions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*YbHSFVjAWDZre8TXWIcbIw.png" /><figcaption>Execution permissions highlighted in yellow</figcaption></figure><p>The script does basic tasks, but it’s lacking many other things. To further improve code organization, one can group the commands into a function.</p><h3>4. Improving the script</h3><p>To further improve on the code structure, the previous behavior will be included within the basic_git_operations function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42097dbf485c9ece957410ea97f9b4ba/href">https://medium.com/media/42097dbf485c9ece957410ea97f9b4ba/href</a></iframe><p>To run the script, remember to <strong>call the name of its function at the end</strong>, or else it won’t be executed.</p><h4>Workflow with alternate paths — external arguments</h4><p>On this segment, there’s an optional function with a complementary flow, and exit codes. The basic_git_operations.bash script, for instance, can be referenced onto a main function, inside another file, either with the use of the source<strong> </strong>keyword or by loading the file to memory, with the ‘.’.</p><p>The script’s function was re-structured to consume two external arguments. The <strong>first argument</strong> is a flag, which is used to validate if git pull is expected to be executed. The <strong>second argument</strong> is the name of the destination branch to where the user wants to perform a git push.</p><p>These user-provided external arguments are used on lines <strong>4</strong> and <strong>16:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/582450f11557d08e397f186d4ec80686/href">https://medium.com/media/582450f11557d08e397f186d4ec80686/href</a></iframe><p>From this point onward, the previous bash script will be referred to on the diagrams as <strong>basic_git_operations</strong>, since it represents the <strong>basic git operations </strong>function.</p><p>The final version of the script is a bit more complex in schematic terms, since it includes alternative execution paths, references one external function, contains a chain of commands, and performs subsequent operations that may vary in context.</p><p>This is the workflow of the main function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wtH57B33dihEwp-U5_eUeQ.jpeg" /><figcaption>workflow of the main function</figcaption></figure><p>Translating the previous diagram to a <strong>main</strong> function, the <strong>basic_git_operations </strong>function’s calls are present on lines <strong>24</strong>,<strong> 29</strong>, and <strong>34</strong>. The decision structures on lines <strong>14</strong> and <strong>19 </strong>are used to validate that the current execution is being made in a context where the git status command doesn’t return an error:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4dbc4adf3f17969efbf4df66bc222cae/href">https://medium.com/media/4dbc4adf3f17969efbf4df66bc222cae/href</a></iframe><p>I exported the basic_git_operations function on the <strong>third line</strong> of the script. A tool named <a href="https://linux.die.net/man/1/sed"><strong>sed</strong></a><strong> </strong>is used on line <strong>11</strong>, to retrieve the name of the default branch, and <a href="https://man7.org/linux/man-pages/man1/awk.1p.html"><strong>awk</strong></a> is also used on line <strong>17</strong>, between the if clauses, to retrieve the name of the active branch, by piping the output of git-symbolic-ref refs/remotes/origin/HEAD and git status respectively.</p><p>The remaining flow is used to perform some git steps to integrate code into main, the default remote branch.</p><h3>5. Resources</h3><p>Before concluding, I’d like to leave the online repository for this code. Feel free to suggest any improvement, or contribute to this project:</p><p><a href="https://github.com/olevezinho/gitconfig">GitHub - olevezinho/gitconfig: This is where I config my interactions with github via VSCode</a></p><h3>6. Conclusion</h3><p>Developing the script and gathering all the data to write this article was insightful, to say the least.</p><ol><li>For the first time, I referenced an external file into a bash script, to use its functions as an external code source.</li><li>For the first time, I also created a bash function and got acquainted with it’s basics.</li><li>By designing the diagrams, I noticed that I could further refactor my code.</li><li>Overall, if I had any sort of difficulty memorizing the git flow of operations — which I did — it completely vanished.</li></ol><p>There’s still some work to do when it comes to additional functionalities, and to the terminal output. The script outputs a lot of text to the screen, most of which is not helpful to the user.</p><p>There are some critical points about writing technical articles, even if they don’t bring imediate value to other developers at the time of writing. To name a few of them:</p><ul><li>Potentially, you will be helping someone in the future, who can be facing the same problem as you;</li><li>Designing and visualizing diagrams can be one way to improve your code;</li><li>By writing tech stuff often, you’ll be improving your communication skills;</li><li>You’ll definitely learn something new or improve on an existing skill.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6a0ad5ceb00" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/bash-operating-git-and-github-through-ssh-e6a0ad5ceb00">Bash — Operating git and GitHub through SSH</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>