<?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 Griffin Poole on Medium]]></title>
        <description><![CDATA[Stories by Griffin Poole on Medium]]></description>
        <link>https://medium.com/@griffinpoole5?source=rss-3d24a95d9086------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*e0-zmICHZQv3EmZI7JgAhQ.jpeg</url>
            <title>Stories by Griffin Poole on Medium</title>
            <link>https://medium.com/@griffinpoole5?source=rss-3d24a95d9086------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 04:45:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@griffinpoole5/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[Dice Roll Distributions: Statistics, and the Importance of Runtime Efficiency]]></title>
            <link>https://medium.com/swlh/dice-roll-distributions-statistics-and-the-importance-of-runtime-efficiency-d8ce3402db15?source=rss-3d24a95d9086------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8ce3402db15</guid>
            <dc:creator><![CDATA[Griffin Poole]]></dc:creator>
            <pubDate>Sat, 29 Aug 2020 15:31:02 GMT</pubDate>
            <atom:updated>2020-10-30T21:13:33.169Z</atom:updated>
            <content:encoded><![CDATA[<h4>Nerds love tabletops because of statistics; Change my mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WpHc-k0YsNwMk5VH" /></figure><p>Around the start of the COVID-19 outbreak earlier this year, my friends and I were working out ways to adjust to a life on lockdown. In addition to the physical separation of the quarantine, I had recently accepted a job out-of-state that would require me to move in the coming year, and so it was important for us to find a way to stay in touch over the time and distance.</p><p>As an answer to this, I decided to start running a Dungeons and Dragons campaign for my friends. As a varied collection of assorted nerds, this would feed into our collective hobbyist, RPG enthusiast, and geeky natures, and the idea was very positively received by everyone involved.</p><p>Setting up the game and running it came with some challenges, which led me to a very interesting mathematical and coding problem, but first, I’ll explain a little about the game.</p><h3>Dungeons and Dragons, a Dice-Rolling RPG</h3><p>For those of you unfamiliar with D&amp;D, Dungeons and Dragons is a tabletop fantasy role-playing game. Its hallmarks are a group of friends gathering around a table, placing themselves in the shoes of warriors and wizards to go on adventures, undergo quests, and explore dungeons. The game is run and organized by a single person, the Game Master(GM) and the “players” create characters that they use for role-play, combat, exploration, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*uizYPnLggKWpeUURUXSJdA.jpeg" /><figcaption>An example of the classic dungeons and dragons dice set consisting of d4, d6, d8, d10, d12, d20, and d100 dice.</figcaption></figure><p>The game is very open in how it’s played, but one factor is consistent across the majority of rulesets; outcomes and results in the game are decided by dice rolls taking the notation of <strong><em>n</em></strong>d<strong><em>s</em></strong><em>, </em>with <strong><em>n</em></strong> being the number of die to roll and <strong><em>s</em></strong> being the number of sides of each die.</p><p>For example, in combat the success or failure of an attack might be determined by a roll of 1d20, or one twenty-sided die. The higher the role, the more likely you are to successfully hit something. Similarly, multiple dice can be used. For the damage of a certain spell, the rules may ask you to roll 3d8, which simply means roll three eight-sided die, and the damage you do is the sum of these results.</p><p>As the GM of our game session, the responsibilities of building the world that my players would explore fell to me. This includes writing the story, creating the non-player characters, and perhaps most importantly, designing and orchestrating the combat encounters. As a part of this, it is important for me as the GM to create encounters that challenge my players, without sending something at them that just kills them outright in a turn or two.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kln5GnJd5ArkTNSZ46_Shw.jpeg" /><figcaption>Above: An example of something that may very well kill my players outright.</figcaption></figure><p>There is a problem with this however. If you follow the rulesets properly, more difficult encounters with higher-level monsters invariably carry a higher risk of accidentally killing off the party. Stronger creatures mean higher die counts and rolls to their damage, and I can’t control the outcome of these rolls without fudging the rules a bit (something you can technically do, but is generally avoided if at all possible).</p><p>So I started looking into the nature of dice rolls, and the way that their outcomes are distributed across various combinations. If I wanted to avoid falsifying dice rolls, I instead had to work with the likelihood of die outcomes and design encounters that maybe <em>looked</em> more difficult than they actually were. To do this, let’s talk about dice roll outcomes and statistics.</p><h3>Distributions of Die Outcomes</h3><p>Let’s say I know that my most delicate player has a max health of 22. Through various manipulations, I can balance an encounter around having creatures with only a 5% chance of taking them out in one shot, while also consistently being able to do around 60–80% of their health in a single hit. In this way, I guarantee that my players will feel challenged and most likely terrified by what I throw at them, while also avoiding the potentially un-fun outcome of being taken out in the first turn and not being able to contribute to an encounter at all.</p><p>To understand this, let’s look at die distributions. As common sense would dictate, rolling any fair die will result in each side coming up approximately the same number of times (randomness doesn’t actually work that way necessarily, but that’s a different article). Below is a probability distribution for one twenty-sided die:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r7v34v20dUxoDPz7bdfRmw.png" /><figcaption>Top: Percent chance of each die outcome, Bottom: Cumulative chance of rolling the given value or lower</figcaption></figure><p>If we think about this distribution in terms of damage, a player could take anywhere between 1 and 20 damage, taking an average of 10.5 damage. But the variance here is as high as it can possibly be. On any given roll, a player is as likely to take 20dmg as they are to take 10dmg or even 1dmg. This inconsistency might be sought after in some cases, but there are ways to represent similar ranges with more consistent results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DrZi1NUxlBCcS0jX9FxDpA.png" /><figcaption>Top: Probability distribution, Bottom: Cumulative distribution</figcaption></figure><p>This distribution above shows a slightly different range of 2–20, but the outcomes much more consistently hover around the average. In this scenario, the odds of rolling max damage of 20 and min damage of 2 have dropped from 5% to just under 1% odds. Furthermore, the result of the approximate average of 11 has gone from a 5% odds to just under 10%, almost double the likelihood. Similarly, the values flanking the average have also seen their odds jump.</p><p>As it turns out, in dice rolling, the greater the number of dice that you roll compared to the number of sides on the die, the more closely the distribution approximates a normal distribution with discrete values. To show this, observe what happens when we maintain the number of sides on the die, but repeatedly increase the number rolled:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pdhtPJ_8d-0TJ_78ysnRwA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kFotvK35CBXlSYQYe4GdRg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pp4yZjkBpZVd1R_0BavGaw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DsJNlAhVhwwrC23fbCQrCw.png" /></figure><p>Similarly, there seems to be a direct relationship between this ratio and the standard deviation of the output distribution. A higher number of dice reduces the standard deviation, and the outcomes more strongly cluster around the average. On the other hand, increasing the number of sides on the die increases the standard deviation, and spreads the outcome to make the extremes more likely.</p><p>Understanding these concepts makes it surprisingly easy to design encounters that run much more consistently with what you as the engineer have in mind, while still providing the illusion of randomness and the “just-good-enough” roll that feels so satisfying to play. Players can take near-fatal blows that just miss killing them, without realizing that your manipulations make the odds of a near-fatal blow about 90%, while the odds of an actually fatal blow lie below 2%.</p><p>This understanding greatly helped me improve the quality of my games, as I could design encounters that felt threatening and kept my players on their toes. Encounters that challenged them to think and plan with real danger in the midst. At the same time, I’m able to avoid a game-breaking snowball, where one of my players dies from the outset, and a fight planned for five people is now only four strong.</p><p>With this new understanding at hand, I decided to generate a toolkit to help me produce these distributions. I wanted a way to plug in these die rolls and print a distribution output so I could easily see how likely I was to kill my players.</p><p>As it turns out, this problem was more difficult than I originally thought.</p><h3>Generating Die Outcomes, and Doing it Fast</h3><p>I chose to approach this problem in the same language that I do most of my quick scratch-work: Python. Python has the benefit of already containing a large online repo of math-based libraries, and given the ease of coding in it, I figured it was a good place to start.</p><p>I started this approach the way a lot of people start a lot of problems. The good-ol’ brute force method! With the help of my good friend Kyle Silver (who is substantially better at abstract math than me), we made an algorithm to generate all possible outcomes of <strong><em>n</em></strong> rolls of <strong><em>s</em></strong> sided die through a clever mathematical trick.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PbLqRidRm4rSDlQAvbhVEg.png" /><figcaption>The code for our initial base number method</figcaption></figure><p>The way this algorithm works is as follows:</p><ol><li>First, we figure out how many possible outcomes of die rolls there are and iterate through these in a loop (for n in range(0,<strong>sides</strong>**<strong>num</strong>))</li><li>Next, for each number, we represent the number in mod base =&gt; #<strong>sides</strong>. For example, in mod 4, 10 would be represented as 22.</li><li>Last, for each number, we chop off digits <strong>num</strong> number of times, and add 1 to each digit. This represents a single die outcome in the roll.</li></ol><p>This produces the effect of generating all outcomes of n rolls by counting from 0 to the number of expected outcomes in mod base of the number of die sides. This is probably better explained with an example…</p><p>Say I roll 2d3, or two three-sided die. The number of possible outcomes for this combination is 2³ or 8 possible outcomes. To generate the number of outcomes, we count from 0 to 8 in base 3 with two digits, which looks like this:</p><ol><li>0 = 00 =&gt; (1,1)</li><li>1 = 01 =&gt; (1,2)</li><li>2 = 02 =&gt; (1,3)</li><li>3 = 10 =&gt; (2,1)</li><li>4 = 11 =&gt; (2,2)</li><li>5 = 12 =&gt; (2,3)</li><li>6 = 20 =&gt; (3,1)</li><li>7 = 21 =&gt; (3,2)</li><li>8 = 22 =&gt; (3,3)</li></ol><p>As you can see above, with this counting method, we can mathematically generate all possible outcomes of the set of die rolls consistently across any combination of dice. By taking the sums of these resulting digit outcomes, we can generate our distribution. But this algorithm comes with a problem:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*kKE9YXl4Pkt_iXf4Nm2nkQ.gif" /><figcaption>The first algorithm in runtime (14d6)</figcaption></figure><p>This method is <em>sloooooooooooooow</em>. The example I show above is the algorithm running through a 14d6 calculation, which is the damage for a 9th level Fireball in D&amp;D if anyone is curious. I didn’t test the full runtime of it, but the reason I didn’t take the time is because I projected how long it would take after tracking its pace for about an hour, and it came out to approximately two-weeks of runtime to finish the 14d6 operation. I decided not to wait that long, killed the program, and talked to my buddy Kyle. In his own words, “Hmmm, it could probably use some optimization.”</p><p>So I poured over the problem for an evening, did some research and finally came to a realization. By generating every possible combination of numbers from 1 to <strong>[</strong>#sides of the die<strong>]</strong> allowing for duplicates, I could produce a list of all possible number combinations that would appear in a series of die rolls of a certain number of sides. For example in a 3d4 setting, I could generate all combinations as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/329/1*AzEsR33I1KtPbNT3VSr8cQ.png" /><figcaption>All combinations of numbers allowing for duplicates in a 3d4 setting</figcaption></figure><p>With these combinations, I could generate all possible die roll outcomes as permutations of each set. For example 1,2,4 could be 2,1,4 or 4,2,1, etc. All of these are valid rolls, and the benefit of this method is that the sum of each combination (and thus all of its permutations) would be the same, and I’d only need to calculate it once per combination.</p><p>The downside was twofold. For starters, generating all of the permutations from the combinations was part of the problem in the first algorithm, and tended to be fairly slow. Second, many of the combinations had duplicates, and generating permutations that swapped duplicate values (e.g. 1,1,2 becoming 1,1,2 by ‘swapping’ the 1&#39;s, despite the outcome being the same) would have to be checked against and removed, decreasing speed further. But as it turns out, I didn’t need to generate all the permutations of each combination in order to produce the distribution. All I needed to know was how <strong><em>many</em></strong><em> </em>unique permutations there are for each combination, and increment the sum bin accordingly. As it turns out, there’s a formula just for this:</p><ul><li>(factorial of the length of the set)/(factorials of the duplicates)</li></ul><p>What this <strong><em>means</em></strong> is that if I have the following combination: (1,1,2,3,4,4,4,5), I have a set of length 8, a duplicate set of length 2, and a duplicate set of length 3. Thus, the total number of <strong><em>unique</em></strong> permutations for this set can be calculated as follows:</p><p>8!/(2!3!)</p><p>Essentially, you take the factorial of the length, which is the standard way of determining permutations of a set, and divide out the permutations of the subsets of duplicates. Because of this operation, I no longer had to generate every possible outcome if I just generated every combination and performed a mathematical operation on each. The code came out something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bdSOK9N2mZOJf9T2wbtJww.png" /><figcaption>Final processing code for the combination-method of generating the distribution</figcaption></figure><p>After constructing this method, I wanted to test it on the same set I faced a problem with in the previous algorithm. So I ran it for 14d6…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*qLPP6Ax6-2wRWxGlTP7fIQ.gif" /><figcaption>The second algorithm in runtime (14d6)</figcaption></figure><p>And it goes without saying, compared to the first build, the second method in unexaggeratedly, blindingly fast compared to the first one, completing the 14d6 distribution in less than a second. In fact, I went on to test this method for higher, more absurd dice counts, and it performs ‘reasonably’ even for 20d10 counts or 100d5 counts (and by ‘reasonably’, I mean it finishes within 10–20min, which is really quite impressive for the latter).</p><p>I haven’t really done any analysis as to the actual BigO notation for both of these methods, but they did strike me as very easy-to-recognize examples of just how important BigO efficiency is. Even in these problems, a totally plausible and useful real world example of generating the distribution of 14d6 had a runtime difference of between 1-second and 2-weeks depending on the algorithm we used, and the faster algorithm in this case allowed us to query sets that would have been completely unreasonable or impossible to run with the original build.</p><p>I still haven’t been able to find a faster way to generate the distribution of die outcomes for an arbitrary number of arbitrarily sided die, despite my research. I’m strongly convinced that there is something out there to help, but I just haven’t known where to look. Regardless, this was a fun side-project to work on that I’ve been able to get a good deal of personal use out of, an exciting math problem to approach in my own time, and a solid reminder of the importance of runtime efficiency. Last but not least, this project has given me a cool, cathartic screen saver for a number-nerd like myself, and there’s always a way to push the algorithm to last as long as I want.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*9qPu2NX6WjIuf1EIiOvF8A.gif" /><figcaption>100d5 in transit (7.8886*10^69 possible die outcomes)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5rIutsKGHeB9PWESWJCq3A.png" /><figcaption>100d5, the longest distribution I had the patience to sit through (7.8886*10^69 possible die outcomes)</figcaption></figure><p>Below I’ve included another research article about a somewhat related, and somewhat unrelated dice problem analysis. I came across this article after I had done the work on this project, and if you liked this write-up, you will also certainly enjoy the one below. I hope you’ve enjoyed this project, and thanks for reading!</p><ul><li><a href="https://blog.revolutionanalytics.com/2017/11/role-playing-with-probabilities.html">Role Playing with Probabilities: The Importance of Distributions</a></li><li><a href="https://github.com/griftheorange/dice_distribution_generator/blob/master/generator.py">griftheorange/dice_distribution_generator</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8ce3402db15" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/dice-roll-distributions-statistics-and-the-importance-of-runtime-efficiency-d8ce3402db15">Dice Roll Distributions: Statistics, and the Importance of Runtime Efficiency</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Executable Bash Files on MacOS]]></title>
            <link>https://medium.com/swlh/executable-bash-files-on-macos-762dd62aee47?source=rss-3d24a95d9086------2</link>
            <guid isPermaLink="false">https://medium.com/p/762dd62aee47</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[bash]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Griffin Poole]]></dc:creator>
            <pubDate>Sun, 28 Jun 2020 20:01:47 GMT</pubDate>
            <atom:updated>2020-07-01T20:48:25.246Z</atom:updated>
            <content:encoded><![CDATA[<p>Control your “.commands”!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v4o2AXLIJaHSZmqYZk26qA.jpeg" /><figcaption>Bash, the default shell for most MacOS versions and Linux Distributions.</figcaption></figure><p>Ever since I began my development journey, I’ve dreamed of my first completion of a fully installable Desktop application. The ability to code a project, compile it down to its runnable code, and develop an installer to automate setup on supported operating systems; these steps have always struck me as a culmination of software engineering skill.</p><p>Today is not the day that I developed my first fully-installable application.</p><p>Today is, however, a day that I have gotten one step closer. Over time, as I progressed along my coding track, I have slowly been picking up tools and tricks of the UNIX/Bash Shell language. The UNIX shell is essentially a command-line-interface to engage with the operating systems features, and in MacOS’s case, it provides us a window to engage our shell through the built-in Terminal application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PebpJenogHXw9D2iCE26Rg.png" /><figcaption>The classic Mac Terminal window.</figcaption></figure><p>Bash provides much closer interaction with the Operating System, and supports a variety of tools for developers and computer nerds alike. Understanding Bash (and Windows shell of course) is an extremely powerful tool for us techies, often being the simplest way to install libraries/frameworks, run compilers, execute code and traverse our computer’s file system. In the past, I have briefly mentioned <a href="https://medium.com/analytics-vidhya/code-switching-the-magic-of-modularization-and-communication-in-programming-2e8a927c38c4?source=friends_link&amp;sk=f09a9212032e16e3d1f0e57f514ffc73">some of the things you can do with Bash</a>, but in short, an understanding of Bash can save you a lot of time in your personal work (aliasing and functions for shortcuts) and can help you develop projects usable by non-developers(installers, executables, etc.).</p><p>In this case, I was developing a small applet for my partner to help her manage her tax allocations at a new job. The app was fairly simple. It was mostly a calculator for deciding how much of a paycheck needs to be saved for taxes and a table to display old paycheck entries:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*HT2P1z88O5wbX9HYErLmPA.gif" /></figure><p>The application was written in the IntelliJ IDEA environment, and utilized the JavaFX library for its GUI. I have done a write up on the <a href="https://medium.com/@griffinpoole5/javafx-javas-built-in-gui-development-api-7bab49ab6d65?source=friends_link&amp;sk=31bd0b4aa48c4461a5048736c81b938b">JavaFX library before</a>, but all you need to know is that it is a GUI support package that used to be included with the JDK, but is no longer supported by default.</p><p>Now I <em>WAS</em> just going to include JavaFX as a dependency library in my IntelliJ setup, build a JAR artifact, and send that over as the app. JAR files are executable on Mac and Windows systems with Java installed, and having all the compiled code in a simple clickable file was all I needed for the project.</p><p>The problem: JavaFX was no longer included in the JDK, and IntelliJ (at the time I’m writing this) has a known issue of JAR artifacts not being buildable for JavaFX apps due to a missing -fx:build in the JDK. Instead, we have to include the JavaFX library as a module dependency and build a normal JAR artifact. Then it works, right?!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*PtXR6lgvzbnTI-VQRzUmBQ.gif" /><figcaption>The dreaded runtime environment error</figcaption></figure><p>Big nope on that. Essentially, the JVM is missing the relevant JavaFX modules during runtime to execute the code that we require in our compiled project. It took me a while to figure out the solution to this, but I found a helpful stack overflow solution <a href="https://stackoverflow.com/questions/53450011/build-and-deploy-javafx-application-using-java11">here</a>.</p><p>Long story short, I could run the JAR through a Bash command, include the modules’ file location, and list the modules that I needed to import from it in the execute command.</p><pre>java --module-path /Users/griffinpoole/Desktop/appTest/javafx-sdk-11.0.2/lib/ --add-modules javafx.controls,javafx.fxml -jar JuliaApplet.jar</pre><p>To be clear, the code above is one line, and what we’re doing is specifying the path to the JavaFX module library that I’ve installed into the projects folder, then specifying that we need to import the .controls and .fxml modules, and finally running the JuliaApplet.jar file. Thanks, Bash!</p><p>But this is far from simple, and far from clickable. I still wanted to avoid having to put my partner through learning how to open up a Terminal, cd to a directory, and copy in a scary and complicated command. So I did some research, and cue the Bash executable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kzvdAOHhUd4cCFONOj-ssw.gif" /><figcaption>A text file with Bash commands being executed by a double-click.</figcaption></figure><p>It is possible to write a text file containing Bash commands where each new line is a separate command to be executed in top-down order. The specific commands can be any valid Bash call, and there can be as many commands as you can fit in a valid file. Below I’ve copied the same three lines of code above, and listed what they do:</p><pre>cd ~/Desktop/appTest</pre><pre>java --module-path javafx-sdk-11.0.2/lib/ --add-modules javafx.controls,javafx.fxml -jar JuliaApplet.jar</pre><pre>exit 0</pre><ol><li>cd the new shell into the project directory for easy file addresses</li><li>Run the JAR file pointing to the JavaFX library and importing modules</li><li>Gracefully exit the shell</li></ol><p>So we can make a new text file and edit its contents to match the code above (P.S. If you append .command to the file’s name, it is automatically recognized as a shell file, but you still need to set executable permissions like we’ll do later).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qnFDLn0w3QBsb6E2X3rRwg.png" /><figcaption>The new text file and its contents.</figcaption></figure><p>Once we have our code in the file, we need to make it executable by updating its access permission. In UNIX shell systems, access permissions are used to control who is allowed to interact with system files and in what way. They are specified by two attributes: user classes, and permission modes. The different user classes are:</p><ol><li>Users — The owner of the file</li><li>Group — Members of the files group</li><li>Other — Users that are not the file’s owner, and not a member of the group</li></ol><p>These cover all possible users who might want to access a file on a given system. Every file has access permission modes for each user class, and they can be toggled on and off depending on the system’s needs. The most important of the permission modes for our purposes are:</p><ol><li>read — Can view the contents of a file</li><li>write — Can change the contents of a file</li><li>execute — Can execute a file</li></ol><p>The UNIX command to update file permissions is chmod, and we want to change our text files permissions so that all users can <em>execute</em> the file. The resulting Bash command to set the file’s properties is:</p><pre>chmod a+x run</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yihcslGOw-ZaAiM4t6gJ4A.gif" /><figcaption>Updating the files permission to be executable.</figcaption></figure><p>Success! Just like that we’ve worked around a complicated bug from version updates by hardwiring into our terminal and setting up the environment ourselves. We were even able to make it easy enough for non-coders to use. Is it pretty? No. Is it clean? Probably not. Is it professional? Almost definitely not. Does it work? You bet it does.</p><p>Aside from being useful for workarounds like this, Bash executables can be great for shortcuts and toolkits on a wide variety of projects. In the past for example (to streamline the opening of multi-sided projects), I’ve created Bash aliases that open up multiple terminal windows, cd the shells into various project directories, open up files, boot up development servers and so on all in one step. The same processes can be separated into a Bash executable file, so that all the setup to resume work on a complicated project can be started with a double-click.</p><p>I hope you’ve found this article useful. I, for one, love finding new and creative ways to streamline my work process, and I hope I’ve inspired some of you to explore more of the capabilities of the Bash Shell. Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=762dd62aee47" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/executable-bash-files-on-macos-762dd62aee47">Executable Bash Files on MacOS</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploying your React App to HostGator, Quick and Painless]]></title>
            <link>https://blog.devgenius.io/deploying-your-react-app-to-hostgator-quick-and-painless-9807cf939532?source=rss-3d24a95d9086------2</link>
            <guid isPermaLink="false">https://medium.com/p/9807cf939532</guid>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[shared-hosting]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Griffin Poole]]></dc:creator>
            <pubDate>Mon, 22 Jun 2020 01:56:38 GMT</pubDate>
            <atom:updated>2020-07-01T16:18:28.384Z</atom:updated>
            <content:encoded><![CDATA[<h4>HTML, Javascript, and Apache Servers</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Os_lo-6C-8NwhsTnqyFvSw.jpeg" /><figcaption>An alligator, generally less enthused about web development than us.</figcaption></figure><p>A few weeks ago, I had finished development on my personal portfolio site (at least as far as I stomach in one sitting) and decided it was time to send it out on the net. This was my first time deploying an application to the web, and while many of my friends and colleagues deployed their first sites using Heroku, I wanted the benefit of a no-downtime hosting service to properly host my portfolio.</p><p>I ended up using HostGator, one of the longest standing hosting services out there. It’s a shared hosting service, meaning that it handles multiple websites off of a single server connected to the Internet. One downside of this is that if the request load spikes on a few of the websites on the server, you may experience delays in responses for your domain as well. The upside, however, is that it is cheap and more than enough for smaller scale low-traffic sites.</p><p>There are a couple steps to setting up a site deployment: domain setup, server hosting, connection and deployment. I knew next to nothing about server setup going into this, so I will do my best to lay out the steps and provide reasoning/context where I had to do my research. I hope you find this useful!</p><h3>Domain Setup</h3><p>First and foremost, the highlight of any website is its domain name. You can’t have a high-traffic game changing webpage without a catchy, snazzy, on-brand domain name. There’s an extremely large amount of domain hosting services out there with domains available for purchase. Personally, I used Hover. It was cheap, easy enough for my purposes, and I was lucky to be able to grab the griffinpoole.com domain for my personal site. It was as simple as this: make an account, search a domain name that I wanted, and set up the subscription.</p><p>What is a domain and how does it work, you ask? I know that you didn’t ask, but I’m happy to explain it anyway. Domain names are essentially aliases for your computers online ID, your IP address. When you set up a DNS server, you instruct a DNS database to pair your domain name with an IP address. This IP can be off your personal computer (not recommended), from a hosting service, or from a company server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*XcT9qFbbxO52tuWbIjnXEA.png" /><figcaption>Taken from the Wordpress tutorial site, wpbeginner.com</figcaption></figure><p>Because of this, when you host a website, you often are paying for two separate features: the DNS servers providing you with that sleek, stylish domain name and the server host providing you with the always-on computer power. On that note, let’s talk about the next part of deployment.</p><h3>Server Hosting</h3><p>Next we need a computer to host and serve your webpage files for serving out in response to domain requests. Technically, this can be done from your own computer, but unless you…</p><ol><li>Know very well what you are doing</li><li>Are okay with leaving the computer on all the time</li><li>Are okay with calls from your power company as your super awesome site scales up</li></ol><p>…it is more advised to pay for a specialized service. Hosting services make their living off of setting up, optimizing, and maintaining the hardware required for website hosting. Also, they’re pretty fairly priced all things considered. Generally speaking, they’re the best thing for the job.</p><p>There are a <em>lot </em>of hosting services out there, and unlike domain hosting, they actually have a variety of differences that can make shopping for them difficult. The biggest three that I can provide advice on are:</p><ul><li><strong>Shared Hosting:</strong> This I mentioned earlier, but essentially a single server hosts multiple websites. While having the benefit of much lower cost, it has the downside of sharing the negative effects of high server load across all sites on the server. This is usually mitigated by setting up traffic limits for shared host sites to avoid one site outcompeting others on its server. Good for smaller scale low-traffic sites or new sites! Sites can always be scaled up to different services as needed.</li><li><strong>Dedicated Hosting:</strong> For larger-scale sites, you can get a dedicated hosting server. This is exactly as it sounds, the server is all yours and you have full root and administrative access to the server settings. Upsides of this are all traffic on the server is exclusively to your site(s). This means potentially faster responses, custom traffic tracking, and the ability to customize server settings to your purposes. The downsides of this are obviously higher costs, and needing to set up more on your own. Although, these services do come in managed versions (also at a higher cost).</li><li><strong>Cloud Hosting:</strong> A newer hosting method, cloud hosting hosts server data on a cloud-based system, meaning that users may not necessarily have any control over where their data is stored. Cloud hosting services usually offer pay-by-use services, meaning that users only pay for the resources their site uses (number of visits, processing power utilized, storage space etc.). This pay-by-use is one benefit of the service, the downsides being the de-centralization of data may be problematic for users with security concerns.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ALJhCqh4N4XBTzlEuZZMCQ.png" /><figcaption>Hosting visual examples from namecheap.com</figcaption></figure><p>For my purposes, I used a shared-hosting service through HostGator. Similar to the Hover purchase, it was as easy as setting up an account, choosing a plan that best fits your needs, and purchasing the plan. HostGator provides a few shared hosting plans, with the major differences being traffic support, number of domain names, and search engine optimization (SEO).</p><p>Now that we have purchased our Domain name and our server space, we can setup the connection between the two…</p><h3>Connecting Domain to Server</h3><p>Connecting a domain to a server is relatively simple to do, but it did take me longer than I care to admit to figure out how to do it the first time. Most of it was me not knowing the terminology I needed to research to figure out what I was doing, something which I will try to mitigate for you all here.</p><p>DNS servers specialize in mapping domain names and addresses to IP addresses and other domain names (it gets crazy). They do this through something called ‘records’. Records are very simply entries that say ‘you get this address, you route to this address’. They come in a few forms:</p><ul><li><strong>A Records (Address Record):</strong> The fundamental record. These records point a domain or subdomain to an IP address. In a lot of cases (mine included), this is the only record you’ll need to use. You can specify the primary naked domain (@), a wildcard for all domains(*), and subdomains.</li><li><strong>CNAME Records:</strong> CNAME records, as I understand them, are sort of like aliases. They intercept host requests and direct them to other hostnames. I personally haven’t made use of them, but they can be used for more complicated server management systems.</li><li><strong>MX Records</strong>: These records specify server locations for receiving email. Uniquely, these are assigned priority values so that if one server can’t handle the mail request, it can go to a different server. Some mail setups have only one server and others have many many more.</li><li><strong>TXT Records:</strong> These records can be used to store text information to be returned on request. They can be used for a variety of security purposes, but typically have niche uses.</li></ul><p>So how do we do it? Again, for our purposes, we just need to point requests for our domain name to the IP of the server provided us by HostGator. On HostGator, you want to make your primary domain, or an addon domain for the domain name you’ve purchased. This can be done by going to your HostGator portal, selecting the ‘Hosting’ tab, and setting your primary domain name there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pNrxnJgK3nm7UyVlIJz-3g.png" /></figure><p>Next, we need the IP address of our server. This will vary service to service, but on HostGator you can access this by going to the control panel (cPanel) and checking the sidebar for the Site IP. This is also a good time to make sure that your primary domain matches your purchased domain name.</p><p>Now that you have the IP address, we can go to our Hover account and make a set of records for our domain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o6EI7eHiCSlU_wv2C90p5g.png" /></figure><p>Above I’ve added two simple A records, both pointing to the IP provided me by HostGator.</p><p>And that’s it! With those records, we’ve successfully directed our griffinpoole.com requests to the proper IP address. The last thing we need to do is put the files for our web page on the server to be sent up on request.</p><h3>Adding the Webpage</h3><p>This is another step that will differ somewhat between services, but generally a hosting service will route your IP and domain to given folders based on the request type. For HostGator, this is a folder called public_html.</p><p>In my case, I built a website in React, so first I ran ‘npm run build’ in my project directory to crate an optimized production build for my site.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*_mb8o8xT3nJ3oMQyIKpkzg.png" /></figure><p>These build files, in their entirety, are to be uploaded into the public_html folder on HostGator.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5KoLRZiosHU9aVPAu-y-fw.png" /></figure><p>Note the cgi-bin folder. This folder is in the directory by default, and can be left alone. For the curious, it is meant to be a folder to store scripts that interact with the web browser for functionality on the frontend. Generally, the system is obsolete as the same features are provided by Javascript in most modern apps, but the tools are always there for any who want to use them.</p><p>At this point, our website <em>SHOULD</em> be set up. These files will be properly sent to a browser in response to an http request to griffinpoole.com. But there was <em>ONE</em> more problem that I specifically needed to address. Instead of serving up different documents in response to different url requests, I used React-Router to handle all of my websites routing. This worked perfectly fine if a user visited the root domain and clicked around across the site. The JS on the frontend could handle the page changes. When a user tried to directly url request to or refresh on a page that was not the home page, however, I’d get a directory not found error.</p><p>The problem was that on a page refresh/request, HostGator would receive a request to a subdirectory (for example griffinpoole.com/project) that didn’t exist. This worked fine when the page was already loaded, as the frontend JS could intercept the URL and render the page as needed. However on refresh, the current JS would get dropped and HostGator would be asked for a doc from a directory that didn’t exist.</p><p>To solve this, I had to dive <em>deep into the Apache server documentation…</em></p><p>I actually found a code snippet off of a Stack Overflow response that solved the problem completely.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8lIko8sHqDZACleRCRDnNQ.png" /></figure><p>This code needs to be put in a .htaccess file that sits in the same public_html directory. Once this is done, the webpage works completely. We’re done! But this wouldn’t be one of my blogs if I didn’t go into unnecessary detail about a problem solved… so here’s why this works.</p><p>HostGator runs their servers with Apache, and in a shared server setting, server-wide configuration settings are unaccessible by us as users. Cue the .htaccess file. An Apache server running a domain will recognize an .htaccess file and overwrite the default configurations with the settings specified in the file. This allows dedicated servers to have domain-specific configurations, and it allows us to run Apache settings on our shared portion of the server.</p><p>So what are we doing here? Essentially what we’re doing is catching the incoming URL, checking if it fails to match an existing file address or directory address, then rerouting to index.html on failure. HostGator’s Apache environment comes with some apache modules plugged in. The one we use here is mod_rewrite. To the best of my understanding, here’s how it works.</p><p>Apache’s mod_rewrite sets a series of Rules and Conditions for URL’s and file addresses matched against <a href="https://medium.com/analytics-vidhya/regular-expressions-making-your-life-easier-and-your-code-harder-to-read-e171cabe2997?source=friends_link&amp;sk=183df7400689d40e849368219e96b7e8">regular expressions</a>. In other words, it intercepts the incoming requests with Apache server variables included, and can control the file addresses that actually get returned. Here’s how the above code works:</p><ol><li>The <strong>‘&lt;IfModule mod_rewrite.c&gt;’</strong> tags specify that the code inside is only to be run if the environment is running with the rewrite module installed. This will always be true on the HostGator servers, but is good practice to avoid bizarre errors if changing hosting services.</li><li><strong>‘RewriteEngine on’</strong> activates the rewrite module. This only needs to be run once.</li><li><strong>‘RewriteBase /’</strong> defines the base where the directory route to-be-matched will start from. For example, take a route request of ‘griffinpoole.com/projects/first’. If your base is ‘/’, then everything after the root domain will be matched against the regex (‘/projects/first’). If your base is ‘/projects’, then your regex will be compared against ‘/first’.</li><li><strong>‘RewriteRule ^index\.html$ - [L]’ </strong>This line sets a rule to be followed if the URL matches the provided regex. The ‘^index\.html$’ regex will match only the exact file address ‘index.html’. The [L] flag is the instruction to be followed here. The L flag just specifies that this is the ‘Last’ rule. In other words, if the file address matches ‘index.html’, skip the rest of the instructions.</li><li><strong>‘RewriteCond %{REQUEST_FILENAME} !-f’</strong> This sets a condition for the next rule. The condition first accesses the REQUEST_FILENAME variable provided by Apache that holds the request URL. The -f flag means true if the request finds a valid file. The ! flips it. So, this condition says, ‘If the request does not find a valid file, follow the next rule’.</li><li><strong>‘RewriteCond %{REQUEST_FILENAME} !-d’ </strong>This does almost the exact same thing as the previous, but instead of checking for a file, it checks for a directory.</li><li><strong>‘RewriteRule . /index.html [L]’</strong> This last rule is run if a file or directory can’t be found. It matches ANY request, and substitutes it for ‘index.html’.</li></ol><p>In summary, the .htaccess file can be used in this way to intercept any routes that result in file/directory not found error, and can instruct the server to return the index.html response as a default. In this way, the base html and JS can be sent up, and when React JS reads the url in-browser, it will load the proper components on the page. Easy!……..</p><h3>Conclusion</h3><p>So that got a little messy at the end, but most of the details with the .htaccess file editing don’t really need to be understood. It’s just boilerplate code you can pull from here or off of a StackOverflow posting that’s been fixing the same ReactRouter problems for years now.</p><p>All in all, React deployment can be simplified into four steps: Get your domain, get your server, connect them with records, and load in your build files. Many of the problems that come along with it can be troubleshot and googled for easy copy-paste solutions. But here, I wanted to provide a comprehensive overview of what goes into web deployment, and an example of some of the problems that can crop up.</p><p>Other options for React site deployment include Heroku (if you don’t mind startup times) and Netlify, which I haven’t used, but have heard is very popular. Overall, I hope this helped explain the fun and miseries of web deployment, and thank you for reading!</p><ul><li><a href="http://glennmessersmith.com/pages/wphtaccess.html">WP .htaccess Rules</a></li><li><a href="https://medium.com/analytics-vidhya/regular-expressions-making-your-life-easier-and-your-code-harder-to-read-e171cabe2997">Regular Expressions: Making Your Life Easier and Your Code Harder to Read!</a></li><li><a href="http://griffinpoole.com/">Griffin Poole, Web Developer</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9807cf939532" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/deploying-your-react-app-to-hostgator-quick-and-painless-9807cf939532">Deploying your React App to HostGator, Quick and Painless</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaFX: Java’s Built-In GUI Development API]]></title>
            <link>https://medium.com/@griffinpoole5/javafx-javas-built-in-gui-development-api-7bab49ab6d65?source=rss-3d24a95d9086------2</link>
            <guid isPermaLink="false">https://medium.com/p/7bab49ab6d65</guid>
            <category><![CDATA[gui]]></category>
            <category><![CDATA[javafx]]></category>
            <category><![CDATA[markup]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Griffin Poole]]></dc:creator>
            <pubDate>Sun, 14 Jun 2020 22:24:04 GMT</pubDate>
            <atom:updated>2020-06-14T22:24:04.024Z</atom:updated>
            <content:encoded><![CDATA[<h4>A markup-based GUI development framework for Java Applications</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*klBSXYn1Hr-FtHWrF2eeEQ.png" /><figcaption>Snapshot of some Dialogue Window Markup in FXML. See the Project Repo linked at the end.</figcaption></figure><p>Over the past month and a half, I have been slowly working through Tim Buchalka’s ‘<a href="https://www.udemy.com/course/java-the-complete-java-developer-course/">Java Masterclass Course</a>’ on Udemy (a course that I <em>highly</em> recommend). Java was the first language I ever coded in, and I started the course as a way to sharpen my beginner-level Java skills into a more fleshed-out knowledge of the language as a whole. The course is broken up into many sub-modules, and one of them covers the JavaFX API, a built-in toolkit for Graphic User Interface (GUI) development.</p><p>While not necessarily a standard tool today, the JavaFX library is very useful for simple Java app development. On a more personal side, learning the library also served as an eye-opener for me as far as what a markup language is, and how understanding a few could lead to better understanding them all.</p><p>I’d like to do a brief write-up on its implementation, its benefits, and the power of markup languages as a whole.</p><h3>What is JavaFX?</h3><p>JavaFX is a Java library that was originally released in 2008, and was first included in the core Java distribution in 2014 with the release of Java 8. Oracle originally intended JavaFX to replace Swing, another popular GUI dev kit for Java that I unfortunately have little experience with at this time. In 2018 however, JavaFX was removed from the core distribution as of JDK 11. At the time of writing this, support for the JDK 8 release is still continuing until approximately 2022, but most of the support of the JavaFX library has been halted in favor of more modern standardized kits (e.g. HTML5).</p><p>That does <em>NOT</em> mean, however, that the JavaFX library is now suddenly unusable. ‘If it ain’t broke, don’t fix it’. The JavaFX library, while losing official support, is still completely functional as a GUI dev kit for Java apps, and a quick Google search will confirm it as one of the top recommendations for Java GUIs.</p><h3>How does it work?</h3><p>A JavaFX app’s function can be (very abstractly) boiled down into this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A8NmI9f9YJwFaO9YpVzuqg.png" /><figcaption>An Example of the “Code Flow” of a JavaFX app, starting at Main.</figcaption></figure><p>You initialize your JavaFX app in whatever portion of your Java code is appropriate (the main function, for example). Once running, JavaFX renders content in objects it calls ‘Stages’ (equivalent to a Window). The content inside of a Stage is referred to as ‘Scenes’. Scenes are Node structures of JavaFX objects that can be constructed manually, or abstracted to FXML files — FXML being a markup language similar to XML that is specifically designed to generate JavaFX node trees for Scene content.</p><p>In your FXML or manual assignment, ‘Controller’ files can be assigned to nodes that contain Java functions to be run in response to user interaction. For those of you with experience in HTML and Javascript, these controller files essentially provide event handler functions that can respond to listeners for any node ‘downstream’ of the node they were assigned to. I will clarify this bit a little further on in the article.</p><p>Lastly, ‘Dialogues’ (sub-windows/sub-menus) can be rendered in response to events. These act somewhat like Stages <em>inside of</em> a Stage and can render their scenes from FXML with their own controllers as well. This can go as deep as you need and the cycle continues ad nauseam.</p><p>To help understand this better, let’s go through a step-by-step setup of a JavaFX app with some visual aids. I used the IntelliJ IDEA development environment which does a lot of favors for you when developing in Java, and I highly recommend it.</p><h3>Step by step</h3><ol><li>A Java method (usually main) calls ‘launch(args)’, ‘launch’ being a built-in JavaFX method that starts running the library’s code.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*1-LONvzXGqy3xxW6tCQvsg.png" /><figcaption>Simple enough so far.</figcaption></figure><p>2. Next, JavaFX runs two methods on startup, the first being ‘init()’. Here you can write in any environment setup you need the app to run on start-up. Afterwards, it runs the ‘start()’ method, at which point your app becomes visible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x7fISb6IiOXvl2r1UNrvuA.png" /><figcaption>The init and start methods get called on start-up of the app.</figcaption></figure><p>Inside of start, you load in your FXML from a file address. The FXMLLoader class will generate the appropriate Node Tree setup from the FXML markup you load in. Then, setting the generated Node structure as the Stage’s scene and calling show() on the stage will open your window for the users to see.</p><p>But before we show, let’s roll back to the FXML load, and take a look at an FXML file and how it is structured.</p><p>3. An FXML file is loaded in from somewhere in your projects directory. The JavaFX library contains parsers and interpreters for the FXML format that can generate the appropriate node trees for a Scene.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PRm-L7toyTUuqfRwWNVvNQ.png" /><figcaption>Another Example of an FXML file. See the project repo linked at the end.</figcaption></figure><p>There is a lot to go over in the FXML format, but here are the basics: FXML is designed to behave very similarly to XML as a markup language. However, instead of each tag representing a type of DOM element on a webpage, each tag represents a Java Class from the FXML library that inherits from JavaFX.Node, or a property of one of these classes. A good rule of thumb is ‘If the tag is capital, it’s a class instance. If it’s lowercase, it’s a property’.</p><p>FXML Scenes are wrapped in special tags called ‘layouts’ designed to help developers position their GUI’s elements. Layouts can also be nested within each other, and so they end up being very powerful for organizing your GUI. A full list of built-in layouts can be found in the docs <a href="https://docs.oracle.com/javafx/2/layout/builtin_layouts.htm">here</a>.</p><p>The most critical components I want to talk about with the markup, however, are the following:</p><ul><li><strong>fx:controller</strong> — This property specifies the controller file that provides the response functions for event listeners triggered on any downstream child nodes. In other words, it ‘scopes’ the functions in the controller file for all nodes inside of the parent it was assigned to. For example, in the above photo, the ‘com.griffinpoole.todolist.Controller’ file provides the event handlers for all of the Nodes downstream of the BorderPane Node.</li><li><strong>fx:id </strong>— This property assigns an id to the node that is scoped to be accessible by controller file functions. For a Javascript equivalent, this is just like a tag id that can be used to set properties of, or delete an element during run-time of the application.</li><li><strong>Event Listeners (#*) </strong>— Most if not all FXML nodes can respond to user sourced events like clicks, mouse-enters, drags etc. Event listener properties specify the functions to be called from the controller in scope by prepending the function name with a ‘#’ character. For example, the above ToggleButton tag, when clicked, will call the ‘handleFilterButton’ function in the Controller class it is scoped in.</li></ul><p>Combined, these tools and structure provide a very similar experience to web development with Javascript and HTML (Javascript being replaced by Java in this case, and HTML being replaced by FXML).</p><p>4. The Controller file’s methods and variables are connected to the appropriate event listeners and Scene nodes from the FXML load-in. A controller file contains both the methods that respond to event firings, and variables that reference Nodes on the Apps view model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x5oAFPyRJw_zYTKgbNoxLg.png" /><figcaption>A trimmed example of a Controller class.</figcaption></figure><p>In the above snapshot, I’ve deleted most of the code in the controller that actually makes it function in order to more clearly explain what’s going on. For the full code, see the <a href="https://github.com/griftheorange/Java-TodoList">repo</a>.</p><p>In this controller, at the very top, we are assigning four private variables with the <em>EXACT </em>same name as fx:ids that I assigned in my FXML file. In this way, the JavaFX library is able to locate these Nodes, and reference these variables to them for workability in the Controller file.</p><p>Further down, you’ll see the definition of the ‘handleFilterButton’ method that I assigned to my ToggleButton in the section above. Inside this function, we can access the button through the ‘filterToggleButton’ variable, and check its status. If it is pressed, we can filter out all items that aren’t scheduled for today, and if not pressed, we can go back to showing all items. This function will fire every time the button is pressed, and consequently, will toggle the filtered list on and off.</p><p>In this way, we can fully build-out our layout and visuals from FXML and provide full responsiveness and adaptability through our controllers.</p><p>Lastly, we can use a tool called Dialogues to bring up sub-windows if we need to prompt users for further input.</p><p>5. Controllers can initialize and render Dialogues that pull from their own FXML files with their own Controllers if necessary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XaTnTzRtcrXp0XXVsLIJLA.png" /><figcaption>A handler function that initializes, shows and processes the results of a Dialogue Window.</figcaption></figure><p>The above code initializes a Dialogue with the main Stage as its owner, and loads in a separate FXML file for its Scene (see the title-card image). The Dialogue then gets two button types: OK and Cancel buttons. It shows the Dialogue and waits for a user response. This ‘ showAndWait()’ prevents any interaction from occurring on the main window until the user is done with the Dialogue window.</p><p>Once the user presses one of the buttons, if the button pressed was ‘OK’, it pulls the results of the Dialogue form from the Dialogues controller, and generates a new list item from the processed results.</p><p>This nested window behavior can technically go as deep as you want and be as extensive as you need, but typically it is kept relatively shallow for positive user experience.</p><p>Once combined, all of the above features result in a setup that feels very flexible for anyone with experience in both web dev and Java coding. The power of JavaFX allows developers to carefully organize their file-structures into main project code, FXML views, and their associated controllers. The end result: clean and (relatively) easy GUI generation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EOxMAFKXKO4QcwxKPQxxpg.gif" /><figcaption>A fundamental TodoList App using the JavaFX library for the GUI. <a href="https://github.com/griftheorange/Java-TodoList">Repo</a>.</figcaption></figure><h3>What I Learned:</h3><p>As a final note on this article, I’d like to address a personal revelation I had while learning the JavaFX library. In the past, I’ve had experience working with markup languages (Ruby on Rail’s template engine, HTML, Github’s .md interpretation, Flask’s Jinja2 Engine, pugs.js, to name a few). With Java being my oldest language, however, JavaFX was the first time I learned a markup language with a stronger background in the language it was sourced from. In other words, it was the first time I had the perspective of writing markup from a compiler point of view rather than learning the markup first and taking it for granted.</p><p>This opened my eyes somewhat as far as the power of markup languages and how they fit into a developer’s toolkit as a whole. At the end of the day, both coding languages and markup languages work with the same thing: computer memory, addresses, and processing power.</p><p>Coding languages are designed to be able to lay out instructions for a computer in a syntax which is (more) accessible to humans. They are the backbone of development and provide an interface for developers to access computer hardware and get it to do whatever they need to do.</p><p>Coding languages are great for listing out these instructions, but when it comes to large-scale, highly complicated object relationship models/data structures, the code for the setup of these can get rather messy.</p><p>This is where the power of markup languages come in. When it comes to statically generating large-scale, complicated relationship models, markup languages come out way on top. They are designed to represent the same object instances and memory allocations possible in their engine languages, but instead of running instructions, a markup language’s purpose is almost exclusively to initialize a complicated relationship model to be passed back into the engine’s code for later computation.</p><p>This can be seen in many of the most popular markup languages and beyond:</p><p><strong>HTML, XML — </strong>Markup to generate DOM Node trees</p><p><strong>Jinja2, Rails Templates, pugs.js — </strong>Markups for generating HTML/XML in Python, Ruby, and Javascript respectively</p><p><strong>YAML (Yes… I know what the name stands for) — </strong>A data serialization format usually used for environment configuration</p><p><strong>DBML, Rails Schema — </strong>Markups for defining and editing database schemas</p><p>Where a coding language shines in terms of instructional layout for a project’s processes, markup languages shine in providing templates for complex relationships <em>between </em>processes. Markup languages excel at initializing relationships and generating environments for underlying mechanisms to work with. These environments can be app configurations, object relationships, database schema, visual layout instructions and more. And while none of this may be news to anyone, I wanted to mark JavaFX as having a special place in my heart for so clearly illustrating this relationship.</p><p>My experience with JavaFX has lead me to a new appreciation of markup, and the belief that as you further begin to understand the underlying engine-work of any language, its associated markup will become more intuitive to the point of being second nature.</p><p>I’ve provided links to a few of the JavaFX docs pages below, as well as the link to my JavaTodo project repo and a project overview page. I hope you enjoyed my article on the JavaFX library, and thanks for reading!</p><ul><li><a href="https://www.oracle.com/java/technologies/javase/javafx-docs.html">JavaFX Documentation</a></li><li><a href="https://docs.oracle.com/javafx/2/layout/builtin_layouts.htm">Working With Layouts in JavaFX</a></li><li><a href="https://github.com/griftheorange/Java-TodoList">griftheorange/Java-TodoList</a></li><li><a href="http://griffinpoole.com/project_overview/JavaTodo">Griffin Poole, Web Developer</a></li><li><a href="https://www.udemy.com/course/java-the-complete-java-developer-course/">Complete Java Software Developer Masterclass (for Java 10)</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7bab49ab6d65" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flask: A Flexible Micro-Framework for Backend Dev in Python]]></title>
            <link>https://levelup.gitconnected.com/flask-a-flexible-micro-framework-for-backend-dev-in-python-9cfaf1114095?source=rss-3d24a95d9086------2</link>
            <guid isPermaLink="false">https://medium.com/p/9cfaf1114095</guid>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Griffin Poole]]></dc:creator>
            <pubDate>Sun, 07 Jun 2020 17:19:56 GMT</pubDate>
            <atom:updated>2020-07-01T15:55:36.095Z</atom:updated>
            <content:encoded><![CDATA[<h4>Cooking up compact, high-power servers</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BnJl3worSrIfaJfhSPgu2Q.jpeg" /><figcaption>Image by <a href="https://unsplash.com/@guilhermestecanella">Guilherme Stecanella</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><p>If you haven’t read my previous articles, let me preface this one with a brief personal declaration: I am a bit of a data nut, and I am certainly a graph enthusiast — something that my friends and colleagues will heartily attest to.</p><p>As a result of my interests, I often find myself working with data tables in my projects. Excel sheets, CSV files, SQL tables… you get the idea. For example, over the past two to three years, I have tracked and categorized every single transaction I’ve made on a personal finance sheet that I keep super secret.</p><p>Now before you think I’m completely crazy, it’s easier than it sounds. Most of the work is just downloading transaction data from my card providers and chucking it into an ongoing table. Pretty straightforward. A few weeks ago, however, my friend gave me the great idea of building an application to automate my tracking even further. At the time I was looking for my next project anyway, so this sounded like a great opportunity to build something that I could personally make use of.</p><p>Now when I think of data table manipulation, I think of Python. Specifically, the Pandas and Numpy libraries, two of the most powerful modern-day toolkits for data analysis and table manipulation. This project would give me an excellent opportunity to explore the capabilities of both.</p><p>The problem was this: I wanted to build my GUI with React (mostly out of familiarity), and at the time the only backend framework I had experience with was the Ruby on Rails framework. Coercing a Rails backend to interface with Python scripting, while probably a fun project, would have been highly involved and entirely unnecessary for what I was trying to do. So instead, I decided to use a backend framework written in Python to make the most out of a toolkit better suited for the job I had in mind.</p><h3>My solution: The Flask Python Framework</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/419/1*K5teGZsxHzreleIAWNRJSg.png" /><figcaption>The Flask Logo and Motto</figcaption></figure><p>Flask is a micro framework for web development in the Python language. What micro framework means specifically is that it comes with very little in terms of available features and boilerplate code at the start of a project’s development. Now that’s not to say there aren’t libraries upon libraries of features and plugins available for the Flask framework, but by default, Flask will tend to <em>not</em> include a feature unless you as a developer have specified otherwise.</p><p>This differs largely from frameworks like Rails or Django (the latter being another highly popular Python web framework), where a large amount of features and conveniences are generated for you on project creation. For example, both of these frameworks provide a Database-Abstraction layer that allows developers to easily read and write to databases through object models (e.g. Rails through Active Model, Django through their Object-relational Mapper).</p><p>The downside of a micro framework is obviously the lack of these features on start-up. Many if not all of the features in the more extensive frameworks exist in the Flask library, but they have to be included explicitly. The major upside, however, is that if you aren’t going to use those features, it’s a lot easier to just make a Flask app and avoid needing to delete a lot of unnecessary files and dependencies. Micro framework applications come with a lot less code bloat, and can always be scaled up with additional tools as needed.</p><p>For my purposes, my backend simply needed to be an API for reading and writing data files, and returning formatted JSON objects. In fact, I didn’t even need a database. In my case, the Flask micro framework was much easier to implement, and provided me the flexibility to include further Python modules as needed.</p><h4>Setting It Up:</h4><p>Setting up a flask server was surprisingly simple. First and foremost, you <a href="https://flask.palletsprojects.com/en/1.1.x/installation/">install</a> the flask module into a python environment manager of your choice.</p><p>With your activated environment, create a new file (naming it app.py is the default that Flask expects) and, in it, type the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6da01f7d61ec4752613875f290bccfb/href">https://medium.com/media/f6da01f7d61ec4752613875f290bccfb/href</a></iframe><p>And that’s it! In six lines, we have written enough code for a running Flask server. It’s that easy. Typing ‘flask run’ into your command line at this point would boot up a server with a single route at the root domain that returns the string ‘Hello, World!’. Flask interprets the route request, and runs the function written for that particular route. The return value of the function is what gets returned as the response to the URL request, but we’ll explore request responses much more later.</p><p>The six-line server primarily speaks to the power of Flask as a micro framework; we can produce a running server in an extremely minimal amount of code and add on as we need to. This server, however, is far from useful and will require a lot more features to run exactly how we need it to, so let’s take an overview of some of Flask’s built-in features and see what we can build.</p><h3>Routing</h3><p>Flask provides a full interface to control routing including standard routing, dynamic URLs and URL arguments.</p><h4>Standard Routing:</h4><p>Routes can be manually included using the route decorator to direct URLs to controller functions for unique response values. For example, manually writing out multiple routes could look something like below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f4923e9622b36716f148d272c00b8ba/href">https://medium.com/media/1f4923e9622b36716f148d272c00b8ba/href</a></iframe><p>Also, be aware that routes can be specifically told what type of request they accept using the optional methods argument. This way, routing responses can be fully controlled and customized to fit the needs of your application.</p><h4>Parameters:</h4><p>Dynamic URL parameters can also be included. When receiving a request, Flask is capable of parsing a URL and extracting parameters that can be used to control request responses. For example, a request could be made to /users/*username_here* that could locate a user’s data based on the supplied URL slug.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e06649eeef236090d96141cbf9ecec9b/href">https://medium.com/media/e06649eeef236090d96141cbf9ecec9b/href</a></iframe><p>The above code would pass whatever username was supplied in the URL to the get_data function, returning appropriate values for different users.</p><h4>Arguments:</h4><p>Similar to parameters, Flask can read URL arguments passed for additional specifications. It gets the argument values from the request object, an optional feature of the Flask framework. Make sure you also import ‘request’ from the ‘flask’ module alongside the ‘Flask’ constructor.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28306879066650fe715ac9d8f0da26d9/href">https://medium.com/media/28306879066650fe715ac9d8f0da26d9/href</a></iframe><p>The above, for example, may make a query to a database for posts by a given user between the date ranges passed along in the URL arguments.</p><p>With these features, Flask provides full flexibility for URL responsiveness built-in to the framework, and all of these features are still completely manageable from a single file.</p><h3>Cross Origin Resource Sharing (CORS)</h3><p>By default, Flask follows the CORS convention, meaning that without additional setup, a running Flask server will only respond to requests made from the same domain that the server is running on. This works fine for a Flask server that’s meant to serve back HTML views, but for building an API (like what I needed to do), this is problematic, as the point of our API is to interface with applications running on other domains.</p><p>Flask provides a full suite for customizing CORS settings, and (as is the Flask way) it is not included by default! We can simply install the ‘flask_cors’ module into our python environment and import it into our app.py by adding ‘from flask_cors import CORS’ to the imports.</p><p>There are a few ways we can customize our CORS settings, and I’ll list a few from least to most restrictive:</p><h4>Accept All Domain Requests:</h4><p>Accepting requests from any and all domains is probably the easiest to implement. You simply pass the app variable in which you’ve stored your initialized Flask instance into the CORS constructor you’ve imported. See below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdc1cbbb0cf3ea93f7c70161e28e98d3/href">https://medium.com/media/bdc1cbbb0cf3ea93f7c70161e28e98d3/href</a></iframe><h4>Applying CORS Restrictions To Multiple Routes of Given Pattern:</h4><p>This one may get a little complicated, but say for example you wanted all domains to be able to access your backend, but only through a certain /api/ route that you’ve built out to be publicly available. The CORS constructor contains a series of optional arguments including ‘origins’, a list of all accepted origin domains; and ‘methods’, a list of accepted methods that foreign domains can make requests on; and a few header options.</p><p>The most flexible of these optional arguments, however, is the ‘resources’ argument, into which you can pass a dictionary of regex URLs as keys with CORS options as their respective values. What this means is that if a URL gets passed in from a foreign domain, it checks the ‘resources’ keys for a key whose regex results in a match. If a match is found, the options for that key are used to restrict and/or permit requests from the matched domain. In other words, you can compactly organize and customize access from any possible URLs you expect to receive requests from in the singular ‘requests’ optional arguments. I’ve done my best to lay out some examples in the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7f6a41346129c064bc2b291f2c66e1a/href">https://medium.com/media/f7f6a41346129c064bc2b291f2c66e1a/href</a></iframe><p>For more details, consult the <a href="https://flask-cors.readthedocs.io/en/latest/">Flask Docs</a>.</p><h4>Route Specific CORS Restrictions:</h4><p>This last one is the most specific, and definitely easier to implement than the resources key in many cases. You can apply custom CORS specs to a single route using the ‘@cross_origin’ decorator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f86bb0f0040cd443f5e5a28571062069/href">https://medium.com/media/f86bb0f0040cd443f5e5a28571062069/href</a></iframe><p>The above code, for example, would allow fetches to the users only from the accepted users domain, and fetches to the pictures only from the accepted pictures domain. This is the method that I used primarily, as I found it easier to implement.</p><p>The features listed above come together to provide full customization of accessibility to your API or service from sources outside your host domain. Now that we’ve covered routing and accessibility, let’s talk about formatting values to return to the request source.</p><h3>Return Values</h3><p>Flask can return a variety of data types as return values, and it’s actually fairly nice to developers as far as intuitively converting certain Python data types to what you would expect them to be in a response. Below, I’ve listed out some of the most standard return types and how they are implemented.</p><h4>HTML/Templates:</h4><p>If the return value of a route function is a string, Flask will automatically construct a properly formatted return object with the passed string as the body of the response. Technically, this means that if you are returning static HTML, you could just store your HTML in a series of text files in a ‘view’ folder and serve them out as needed. This, while 100% safe from HTML injection, is absolutely not dynamic and is definitely not the best practice in most modern-day cases. For dynamic HTML templating, we can actually rely on one of the few libraries that Flask DOES include by default: the Jinja2 template engine.</p><p>If you’ve used Rails before, this engine functions very similarly to the dynamic view files that rails serves out. Variables can be passed to the template files and code can be executed within the template, allowing for dynamic HTML generation dependent on the request. For example, given the following route:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e461814bf624e85ae77004b5d5a93e0/href">https://medium.com/media/9e461814bf624e85ae77004b5d5a93e0/href</a></iframe><p>And the following greeting.html file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8d3333c883225453398417b4c8fc362/href">https://medium.com/media/b8d3333c883225453398417b4c8fc362/href</a></iframe><p>One of the two following HTML layouts would be returned:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7858b3ce02fda2fc7268dfd8269f7f1b/href">https://medium.com/media/7858b3ce02fda2fc7268dfd8269f7f1b/href</a></iframe><p>The Jinja2 library also scrubs variable inputs to prevent harmful HTML injections from occurring. As a result, templates both reduce the amount of code you write, provide more flexibility, and provide security for your site. For more details, feel free to consult the <a href="https://jinja.palletsprojects.com/en/2.11.x/">Jinja2 Docs</a>.</p><h4>JSON Objects:</h4><p>For API generation (which is what I needed for my finance app), JSON objects are generally the most common format of data to return. Python does include libraries for jsonifying dictionaries, but it turns out that Flask knows that too. Flask is nice to us in this regard since, if you return a dict from a route function, it recognizes and formats the dictionary as a JSON object for you.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a965e2dfe6522bd600e82236126d1a08/href">https://medium.com/media/a965e2dfe6522bd600e82236126d1a08/href</a></iframe><p>The above code will attempt to add a new entry through a Data Handler class. If successful, it returns a dict with the status ‘Success’ and the request body. If unsuccessful it returns a ‘Failure’ status. In both cases, Flask automatically converts the dicts to JSON.</p><p>In my experience, returning List objects results in similarly successful behavior on the sending and receiving ends as well. So if all you need to send forward is a list of items, it is possible to return the list the same way that you would a dictionary, but there may be unintended side effects.</p><h4>Files:</h4><p>Lastly, Flask is able to send files to the frontend for download or cross-server data transfer. This can be done by also importing the ‘send_file’ method from the ‘flask’ module.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc3cd9332c0bb13eea210e076c0ba5d0/href">https://medium.com/media/bc3cd9332c0bb13eea210e076c0ba5d0/href</a></iframe><p>Altogether, these filetypes form the majority of what you’ll be sending as responses to any URL request, and as always, the <a href="https://flask.palletsprojects.com/en/1.1.x/">Flask Docs</a> can be consulted for further reference. Lastly, I want to mention the ease of customization in Flask as a server framework by overviewing how simple it is to import custom modules into your servers environment.</p><h3>Custom Code and Python Power</h3><p>While this might not strike anybody as a feature of Flask in particular, I do think it’s worth mentioning how easy it is to include your own code, organizations and file structure into any Flask project. Remember, everything gone over in this article so far could be done in a single file, and compactly to boot. But it’s all still python code.</p><p>Just like we can import the ‘flask’ and ‘flask_cors’ modules, we can import any other python modules we want. Pandas, Numpy, Xlrd, our own custom modules — the world is our oyster! Getting back to our origins, this exploration of Flask was started as a means to develop a Python backend for a finance tracking application. I’d like to end this article overviewing how simple it was to interface Flask with all the other Python libraries I needed to use for my app to perform the tasks that were required of it.</p><p>Up until now everything we’ve reviewed in Flask can (and most often should) be done in a single app.py or routes.py file. So our file tree right now looks something like this:</p><pre>app.py</pre><p>More like a stump than a tree, but this file alone is capable of handling all the routing control that our server needs to be able to run. As for the app’s functionality, I divided the executable code into three separate files:</p><ol><li><strong>loaders.py — </strong>Imported ‘Pandas’-Responsible for loading data-files into Pandas Dataframes and saving Dataframes into data-files</li><li><strong>data_handlers.py — </strong>Imported ‘Pandas’ and ‘Numpy’-Received Dataframes from loaders.py and handled all traversal, calculation, and table manipulation</li><li><strong>json_parsers.py</strong> — Imported ‘json’-Received processed data from data_handlers and formatted responses into proper JSON format. Returned JSON to app.py</li></ol><p>These executable modules allowed me to separate the responsibilities of my app, and all of these were organized into a folder named ‘controllers’. (A habit from my Rails days…)</p><p>I also kept all of my data-files in a ‘resources’ folder with sub-folders for different file formats. This is what the loaders module accessed to read and write data.</p><p>Lastly, I had a few App-wide constants that I kept in a top-level ‘constants’ module. This let me organize all my sets and properties in one place importable by any of my other modules. By the end, my file-tree looked something like this:</p><pre>app.py<br>constants.py<br>/controllers<br>    json_parsers.py<br>    data_handlers.py<br>    loaders.py<br>/resources<br>    data.p<br>    /xl_files<br>    /csv_files<br>    /pickle_files<br>    /exports</pre><p>This highlights one of the primary benefits of and motivations behind using a micro framework like Flask. By the end of the project, the files and folders that exist in your repo are ONLY files and folders that the project uses. This is something that you don’t get in Rails, which pre-generates a lot of the file structure for you.</p><p>Thanks to Flask, a backend this compact and controlled is capable of handling all the data processing required to support a full-featured frontend finance tracking app for fiscal fanatics, like me!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*OcMS0dTX_muPkEoTJOIQMg.gif" /><figcaption>Snapshot of my <a href="http://griffinpoole.com/project_overview/Budget.web">Budget.py</a> App, written with Backend Flask</figcaption></figure><p>I hope you’ve enjoyed my article on Flask as a compact backend development tool for Python. Below, I’ve collected links to the Flask documentation, Jinja2 resources, my backend’s github repo, and a highlight page for my Budget.py project for any who are curious to learn more.</p><p>If I’ve managed to convince any of you to use Flask as a backend in your next data-heavy project, then I definitely consider this a win. Thanks for reading!</p><ul><li><a href="https://flask.palletsprojects.com/en/1.1.x/">Welcome to Flask - Flask Documentation (2.0.x)</a></li><li><a href="https://jinja.palletsprojects.com/en/2.11.x/">Jinja - Jinja Documentation (2.11.x)</a></li><li><a href="https://github.com/griftheorange/budget_web_py_backend">griftheorange/budget_web_py_backend</a></li><li><a href="http://griffinpoole.com/project_overview/Budget.web">Griffin Poole, Web Developer</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9cfaf1114095" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/flask-a-flexible-micro-framework-for-backend-dev-in-python-9cfaf1114095">Flask: A Flexible Micro-Framework for Backend Dev in Python</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TopoJSON, GeoJSON, and Projections: Developing Interactive Map Visuals for Web Apps]]></title>
            <link>https://medium.com/better-programming/topojson-geojson-and-projections-developing-interactive-map-visuals-for-web-apps-79e2d50abb19?source=rss-3d24a95d9086------2</link>
            <guid isPermaLink="false">https://medium.com/p/79e2d50abb19</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[frontend-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[maps]]></category>
            <dc:creator><![CDATA[Griffin Poole]]></dc:creator>
            <pubDate>Mon, 03 Feb 2020 23:51:41 GMT</pubDate>
            <atom:updated>2020-02-03T23:51:41.241Z</atom:updated>
            <content:encoded><![CDATA[<h4>If you didn’t like maps before, you probably still won’t — but hear me out</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*YztNV1q0k-qcwtiQ4KJTgg.jpeg" /><figcaption>Image by <a href="https://pixabay.com/users/claudia0spencer-5304639/">Claudia Spencer</a> on <a href="https://pixabay.com/photos/maps-topographical-maps-geography-2294276/">Pixabay</a></figcaption></figure><p>A few weeks ago, I took up a small proof-of-concept project as a follow-up to learning the D3 library.</p><p>I had just finished a stint in data visualization, and it occurred to me it’d be exciting to be able to carry these visuals into an even more relatable real-world format. Two weeks prior, I had visited the Museum of Modern Art in New York and saw a display in their 21st century–tech exhibit, where the artist had animated wind patterns over the United States across the years.</p><p>It struck me how elegant the implementation looked and found myself imagining how they did it. The more I thought about it, the more difficult it seemed to me. I left the museum that day impressed, with a cherished memory of a piece of code elegant enough to be exhibited as art.</p><p>Somebody out there was able to take gross collections of wind data, process them down to their position and directionality, and place them over a map of the US in an elegant, easy to follow visual. It was clean, it was relatable, and it was nice to look at. People liked it so much they even put it in a museum.</p><p>This experience, combined with my exploration of the D3 library, inspired me to look into mapping data in geographic frameworks. It’s a concept we see every day: GPS routing in Google Maps, weather patterns mapped on the news, and local restaurants/stores dropped as pins in your area. Mapping data to geography is powerful for both its ease of interpretation and relatability. We’re exposed to maps so often nowadays we recognize their context at a glance — and relate them to ourselves almost immediately.</p><p>So I set up a project for myself, simple at first but easy to expand on: I wanted to write a web app that rendered a map of the globe that responded to my actions and supported plotting of data based on latitude and longitude. Over the few days I worked on it, I was able to learn about a variety of tools and resources for coding with maps, but I want to highlight two in particular that I think are the most important for getting started: GeoJSON/TopoJSON file formats and map-projection libraries.</p><h3>GeoJSON vs. TopoJSON: What Are They?</h3><p>When trying to render a map, the first and most important resource you need is, unsurprisingly, the map. There’s an almost absurd number of APIs and formats out there that store data for map drawing, but two of the most prevalent (and the ones I used) are TopoJSON and GeoJSON files.</p><p>So what are they? Well, TopoJSON files are a derivative of GeoJSON, so let’s start with the parent.</p><h4>GeoJSON</h4><p>GeoJSON files are a format for storing map shapes in JavaScript Object Notation (JSON). They’re often formatted in a Feature Collection for interpretation by coding libraries and contain arrays of attributes to draw (each attribute is called a Feature).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df4958e4318802286dad00efaa094bfa/href">https://medium.com/media/df4958e4318802286dad00efaa094bfa/href</a></iframe><p>GeoJSON features have two keys: geometry and properties.</p><p>The properties key stores general information often used for the identification/labeling of the feature.</p><p>The geometry key is the more critical part for rendering. It stores the type of geometry the Feature is and a set of coordinates, the format of which varies depending on the geometry type. A Feature’s type ranges through: Point, LineString, Polygon, MultiPoint, MultiLineString, and MultiPolygon.</p><p>From these types, any arrangement of visual properties can be generated. Rendering libraries use this type in combination with the collection of coordinates to draw the map features correctly with respect to each other.</p><p>GeoJSON files are notable for their relative ease of readability and the convenience of being able to extract/add/transfer features to and from a collection as easily as a simple copy-paste. Every shape drawn on a map from a GeoJSON has a corresponding feature and can be manipulated as such.</p><p>The downside of GeoJSON, however, comes from the same principle — that every shape in a GeoJSON is its own feature. As a result, a lot of borders between adjacent features end up getting stored in memory twice/thrice/four times. GeoJSON files, especially for detailed layouts like the United States map below, are notoriously large, and they aren’t always best for the ease of transfer over a network.</p><p>That’s where GeoJSON’s cousin, the TopoJSON file, comes in.</p><figure><img alt="A wire mesh map of the united states" src="https://cdn-images-1.medium.com/max/1024/1*GGTqpHwCbajzG2_2qaRGcg.png" /><figcaption>Wire mesh map of the United States, taken from the <a href="https://github.com/topojson/topojson/wiki">TopoJSON GitHub page</a></figcaption></figure><h4>TopoJSON</h4><p>TopoJSON files are similar to GeoJSON in their storage as JavaScript objects.</p><p>However, in terms of readability, TopoJSON files are very, very mean. Below I’ve copied a rough depiction of a TopoJSON object logged to the browser console. The general gist is this: Instead of storing each feature as its own shape, TopoJSON files instead store every line/edge on a map as an arc<em>.</em></p><p>They also store a collection of objects to be drawn as a Geometry Collection that references back to the arc array. For readability, it’s a nightmare of arrays in arrays of arrays and nested geometry objects referencing indexes in the nested arrays. I wouldn’t bother trying to make sense of it unless you’re very interested in the format, but the syntax does have some major benefits.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ef99890cc731a8d89851039aeed315b/href">https://medium.com/media/9ef99890cc731a8d89851039aeed315b/href</a></iframe><p>Because TopoJSON files construct the shapes from shared edges instead of tracking each individually, they’re often an order of magnitude smaller in file size than a GeoJSON file referring to the same map. For example, the TopoJSON file storing the geometry for the above United States map is about one-fifth the size of its corresponding GeoJSON. For portability and transferring, TopoJSON files come out on top, especially as your maps get more complicated.</p><p>Many rendering libraries require GeoJSON-esque file formats to draw, but plenty of tools exist for translating between the two. For example, in JavaScript, the topojson-client library has a Feature function that extracts GeoJSON features from TopoJSON objects, allowing for storage in the smaller file size and the handling benefits of the GeoJSON format.</p><p>So now that we have our source for the map, how do we draw it? Or, more accurately, how do we <em>want</em> to draw it?</p><h3><strong>Map-Projection Libraries</strong></h3><p>Map-geography files store features, coordinates, and geometries in terms of the geographic-coordinate system — i.e., they orient everything based on latitude, longitude, and its spatial position on a globe.</p><p>Unfortunately for us, computer screens are not spherical — they’re flat. So to fix this problem, we end up exploring the world of map projections and the code libraries that support them.</p><h4>What is a map projection?</h4><p>At its simplest, a map projection is a way of flattening coordinates for drawing on a map. It’s easy enough to follow but potentially very difficult to implement from scratch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dbdrqzU9Un7CukWZFZHjgQ.jpeg" /><figcaption>A visual of how the Mercator Projection is stretched and unrolled</figcaption></figure><p>Map projections typically flatten, compact, and distort map features to fit them to a surface that can be unrolled onto a flat page.</p><p>For example, the Mercator Projection, one of the most common projection types used in schools, is a <em>cylindrical projection. </em>Cylindrical projections are some of the most common since they tend to maintain map accuracy around the most heavily populated areas of the globe.</p><p>However, there are hundreds of other options with various benefits. For example, below is an animation of the Dymaxion Projection, one of my personal favorites. It’s notable for extremely low degrees of map distortion (<a href="https://en.wikipedia.org/wiki/Tissot%27s_indicatrix">for those interested</a>) and almost completely avoids land breaks in its unrolling. It’s great for mapping <a href="http://geographer-at-large.blogspot.com/2011/08/fun-with-map-projections-oblique-case.html">human-migratory patterns</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*VyhVx46g4gYf0XI6xm_btw.gif" /><figcaption>Approximate example of Dymaxion Projection unrolling</figcaption></figure><p>But back to the point: How do we take our map files and generate flat visual projections for rendering on screen? Our answer is libraries-code libraries.</p><p>People out there who are much better than me at math have already put together the functions and operations required to translate longitude/latitude to 2D <em>x</em> and <em>y</em> positions, and they’ve included those functions in code libraries. Almost every major language will have libraries built for this purpose, open source and otherwise. Since we are working with a JSON format, ideally we’d use one in JavaScript. Fortunately, I found out D3 has a sublibrary, d3-geo, dedicated to just this.</p><p>With this library, we’re able to translate map data to any 2D projection supported by D3. We finally have all the tools and knowledge needed to start handling, rendering, and manipulating our maps. That being said, let’s get to building our globe.</p><h3>Building a Globe</h3><p>To generate my globe, I built the application in a React framework, partially as a test and partially to use the responsiveness of React state as a way to interact with my map dynamically.</p><p>To start, I rendered a SVG in a component I named Canvas to contain all the downstream paths I’d need to draw for the globe features. Then, I rendered a child component called Globe that handled the generation of the map features.</p><p>Inside the SVG, we can begin the steps for drawing. First, I imported my TopoJSON file into the component, then used topojson-client to extract the features into the GeoJSON format.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f713cb6c24c7ad3e27ff143d45b503a/href">https://medium.com/media/1f713cb6c24c7ad3e27ff143d45b503a/href</a></iframe><p>This will store our features in a new array we’ve named data. These features are what we can plug into D3 to generate paths for drawing.</p><p>To do this, we first use d3-geo to generate a projection function. To test, we’ll start with a Mercator Projection. Once we configure our projection function, we pass it into another d3-geo function, geoPath. This will give us back another function that accepts a GeoJSON feature and returns a path attribute. If this is difficult to follow, please feel free to reference the code below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19963be008e5bd81a1fddef6c709784a/href">https://medium.com/media/19963be008e5bd81a1fddef6c709784a/href</a></iframe><p>Now, path will store a function for generating our SVG paths. Using this, we can pass path each of our GeoJSON features, and it’ll return us the <em>d</em> attribute of a path.</p><p>To implement this, I made a Continent component that excepted one feature as a datum prop. Using this prop and my path generator, I could render paths for each of my features in the collection. For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8799019e69b041244f31d9fbdd683dc/href">https://medium.com/media/c8799019e69b041244f31d9fbdd683dc/href</a></iframe><p>Notice because each continent path is its own component, I’m able to add event listeners to each continent specifically and make them respond uniquely. Combining all these functions, I can draw all of my features to my SVG and make them do fancy things. Like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RiPxffRuLjnagGjgruniMQ.gif" /><figcaption>Mercator Projection in my topoGlobe project</figcaption></figure><p>Now we’ve got our paths rendering, but there’s still some work to do. First of all, it’s not quite a globe. Second, it’s not rotating. Third and last, we need to make sure we can render visuals to the globe appropriately based on lat/long. The first is actually quite an easy fix, and the second follows along smoothly.</p><p>d3-geo has an orthographic projection type, so rendering as a globe is as simple as changing our projection function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d1af87240b767999c69cf25426fbedd/href">https://medium.com/media/6d1af87240b767999c69cf25426fbedd/href</a></iframe><p>However, this will only show us the front part of the globe, covering anything that should appear behind it. We can compensate for this by adding a rotation configuration to our projection that turns the map on three axes. By customizing these values, we can change the part of the globe that’s facing us. Furthermore, if we make this rotation value dependent on state, we can leave it up to the user to turn the globe as they please.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*iKlqrx1Bf-6_46InC-EP8A.gif" /><figcaption>Changing the projection type to orthographic and setting the rotation to be dependent on the state</figcaption></figure><p>We’re nearly there. With rotation dependent on state, adding autorotate is as simple as setting a timer/interval to increment the state periodically. Lastly, we need to make sure we can render data points on the map corresponding to appropriate lat/long.</p><p>Because of the way we implemented this map, this turns out to be surprisingly easy. As long as we’re able to generate an appropriate GeoJSON object, we can actually just pass it into the same path generator we used to make the continents. The projection and path-generation functions will handle all the difficult parts for us, and GeoJSON formats are much easier to custom create and manipulate versus other formats.</p><p>As it turns out, d3-geo comes to our rescue again and provides us with a method to generate GeoJSON circles with only the lat, long, and radius. Using the geoCircle function, we can custom create our circles, like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efd3e762aba805d3005c8394a752db6a/href">https://medium.com/media/efd3e762aba805d3005c8394a752db6a/href</a></iframe><p>With this, we’ve generated two circles and their shadows for visual effect: circle1 at 0, 0 and circle2 10 degrees longitude to the east.</p><p>We can draw our circles with a higher scale value in our projection to make them appear raised off the surface of the globe. Then we can draw the shadows on the same scale as the globe to make them appear cast by our flying saucers.</p><p>In the end, you’ll have something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16e3c3c7ffee8562623b37cebeff205a/href">https://medium.com/media/16e3c3c7ffee8562623b37cebeff205a/href</a></iframe><p>It’ll render our globe with aesthetically pleasing, accurate visuals.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4kXxDtFkmuXQoNTGitYZjg.gif" /><figcaption>The flying red discs are spotted at the equator</figcaption></figure><h3>Conclusions</h3><p>Map generation and data plotting have become small passions of mine over the past month, and I’m working on a few projects using these techniques to consolidate understanding and test limits. As I first began to explore mapping and geographic rendering, I didn’t have any idea exactly <em>how</em> deep it could go. It’s an incredibly deep rabbit hole to dive into, but playing around with projection types and dynamic map rendering has been some of the most fun I’ve ever had while coding.</p><p>Understanding map-data formats and being able to traverse them are incredibly powerful skills, and I strongly encourage anyone with an interest in this material to explore its libraries and resources. D3’s website has <a href="https://bl.ocks.org/atanumallick/8d18989cd538c72ae1ead1c3b18d7b54">dozens</a> <a href="http://bl.ocks.org/PatrickStotz/1f19b3e4cb848100ffd7">of</a> <a href="https://bl.ocks.org/larsvers/dab7c2d6ea5ab964d10df0ef1470b90e">examples</a> of similar implementations.</p><p>I’ll link the Git repo for my topojson-test project below, and I hope I’ve inspired some of you to take up similar projects as a challenge for yourself. Thank you for reading, and good luck!</p><ul><li><a href="https://github.com/griftheorange/topojson-test">griftheorange/topojson-test</a></li><li><a href="https://github.com/d3/d3-geo">GitHub - d3/d3-geo: Geographic projections, spherical shapes and spherical trigonometry.</a></li><li><a href="https://geojson.org/">GeoJSON</a></li><li><a href="https://github.com/topojson/topojson">topojson/topojson</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79e2d50abb19" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/topojson-geojson-and-projections-developing-interactive-map-visuals-for-web-apps-79e2d50abb19">TopoJSON, GeoJSON, and Projections: Developing Interactive Map Visuals for Web Apps</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[D3: Responsive and Dynamic Visualizations for Data and Other Easy Recipes]]></title>
            <link>https://medium.com/better-programming/d3-responsive-and-dynamic-visualizations-for-data-and-other-easy-recipes-68e37b451822?source=rss-3d24a95d9086------2</link>
            <guid isPermaLink="false">https://medium.com/p/68e37b451822</guid>
            <category><![CDATA[d3js]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Griffin Poole]]></dc:creator>
            <pubDate>Thu, 16 Jan 2020 20:32:19 GMT</pubDate>
            <atom:updated>2020-01-16T20:32:19.609Z</atom:updated>
            <content:encoded><![CDATA[<h4>Graphs made fun…or at least easier</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*ixL0IEhgl5HMEh90lXP23w.png" /></figure><h3>Introduction</h3><p>It’s no secret that a lot of people out there really, <em>really, </em>don’t like math. It was the truth as I was growing up, it’s the truth now, and I don’t believe it’ll ever stop being the truth. As a kid who grew up under the standard K-12 education system, one of my strongest subjects throughout the years was always mathematics. Memorization was less important than conceptualization and by understanding numbers and relationships, answers could always be constructed rather than regurgitated. I enjoyed it, and I was great at it. At the same time, I understood that not everyone felt the way I did about numbers.</p><p>I sympathize — numbers and math can be hard. As my education advanced into my undergraduate degree, I met people majoring in CS, math, and physics doing problems that made me want to pull my hair out. Math can be difficult, numbers can be extremely abstract, and not everyone enjoys spending hours staring at a piece of paper to tease out some niche relationship (I mean who would, right?)</p><p>That being said, as we move into an age of information dominated by technology and computation, handling and interpreting data becomes an increasingly necessary skill to have. Computers store and manipulate data in a way that’s generally unfriendly to human readability. Being able to extract and interpret this data is so important that there’s an entire data science field growing up around the topic.</p><p>What does any of this have to do with D3? It comes down to this: Data is numbers (or at least it can usually be digested to a numerical format), and numbers can be gross….</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*BhkG2qs9zGw610DNL9aLaQ.png" /></figure><p>But pictures…pictures can be much less gross. In an age where data and mass information interpretation is becoming more and more critical for performance in every field, visualization of data for ease of interpretation by <em>everyone </em>is an extremely powerful toolset. What better way to represent data visually, than with <em>graphs</em>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*877Wmk4Xy3On_dEQMqe1Gw.gif" /><figcaption>Most graphs are simpler than this one.</figcaption></figure><p>Well made data visualizations make large datasets accessible and interpretable for everyone. Libraries like D3 give us powerful pre-made toolkits for manipulation of data into visually representable formats. On that note, let’s move into more of the meat of D3 — what is it and what sort of tools does it provide for data visualization?</p><h3>What is D3?</h3><p>D3 is a JavaScript library that provides functionality for visually representing data through Html, SVG, and CSS. In other words, it’s a method library for drawing pictures based on data (although it can do much more than just pictures). Using it in your code is as simple as including it alongside any other JS script tags you run.</p><pre>&lt;script src=&quot;https://d3js.org/d3.v5.js&quot;&gt;&lt;/script&gt;</pre><p><a href="https://d3js.org/">D3</a></p><p>D3 provides a wide array of features, including external file parsing into JSON (.csv, .js, etc), HTML table generation from arrays, binding datasets (arrays) to DOM selections and dynamically rendering DOM elements. It also takes many processes involved in ‘vanilla’ DOM traversal and streamlines them into dry compact methods. These are all just the tip of the iceberg when it comes to D3’s functionalities, but for starting out with this library, I think the most important ones to grasp are the last three: binding data, dynamic elements, and streamlined DOM traversal.</p><h3>Binding Data</h3><p>First and foremost D3 allows you to bind data (arrays) to DOM element selections for later dynamic rendering. What does that mean exactly?</p><p>To start, you would make a selection of elements from the DOM. This is where some of D3’s streamlined traversal comes in handy. D3 provides an example of a messy traversal on their homepage, with code to change all p tag colors to blue:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1jyJkJWMqB6zbPMHxBRXlw.png" /></figure><p>This is not fun to write, so D3 provides us with a way to streamline this selection alongside the attribute change. The above has the same functionality as the following:</p><pre>d3.selectAll(&quot;p&quot;).style(&quot;color&quot;, &quot;blue&quot;);</pre><p>The selectAll method returns a selection of all relevant elements that are downstream of the provided node. If you provide d3.selectAll(), you get everything on the document. Running namedDivVariable.selectAll() selects everything downstream of the div in question. This makes DOM selection and attribute change much simpler — thanks D3! Now that we have ways to quickly retrieve “selections” of elements, we can bind our data to them. Let’s provide an example.</p><p>Say we have three arrays with different numbers of elements: three, two and one.</p><pre>let threeElement = [true, false, true]</pre><pre>let twoElements= [true, false]</pre><pre>let oneElement= [true]</pre><p>On the DOM we also have two circle SVGs. Don’t worry about the correct SVG syntax for now — just assume they’re proper elements. These circles also have a possible attribute: crossed; true — Ø, false — O. We can go ahead and select these and store them in a circles variable.</p><pre>circle1 = O, circle2 = O</pre><pre>let circles = d3.selectAll(“circle”)</pre><p>Now we can <em>bind</em> this array data to our selection of elements. What this means is that each value in our data array gets connected to each element in our selection by D3 magic. These data values can later be accessed functionally to dynamically render our elements. But we’ll get back to that later. The way that we bind data is through the .data() method. So we can bind our array of two values to our two circles like so: circles.data(twoElements). Now, the first circle is joined to our first data element ‘true’, and our second circle is bound to the second element ‘false’. This is called the <strong>update selection — </strong>when the number of data values matches the elements in the selection.</p><pre><strong>Update Selection:</strong></pre><pre>circles.data(twoElements)</pre><pre><strong>//Gives back all elements and matching data bound</strong></pre><pre>circles.data(twoElements).attr(‘crossed’, function(d){return d})</pre><pre><strong>Would result in something like // </strong>circle1 = Ø, circle2 = O</pre><p>In the code above, you’ll notice some mess in the .attr() method where I’m setting the circle to be crossed or not. Don’t worry about that for now. Just know that crossed is being set to the value of the data element that has been connected to each circle.</p><p>The update selection is all well and good when we have an element to match each data value, but what if the number of data values and elements in the selection don’t match? D3 also supports two other types of selections that incorporate overflow in either direction: .enter() and <em>.exit().</em> Here’s how they work.</p><p>With the .enter() selection, you receive any DATA values that did not have a matching element. These unmatched values can be used to render new elements corresponding to them with .append().</p><pre><strong>Enter Selection:</strong></pre><pre>circles.data(threeElements).enter()</pre><pre><strong>//Gives back all Data Values that don’t have a matching circle</strong></pre><pre><strong>//e.g. [true] will be unmatched</strong></pre><pre>circles.data(threeElements).enter().append(‘circle’).attr(‘crossed’, function(d){return d})</pre><pre><strong>Would result in something like // </strong>circle1 = O, circle2 = O, circle3 = Ø</pre><p>Here, we see that a three-element array is being bound to our two circles. Circle1 gets true, circle2 gets false, and our last true is floating. When we call .enter()<em>,</em> that floating element is now what we have selected. For each of these floating data points, we can .append() a new circle and set its crossed attribute to the now assigned data point true. By the end, we haven’t changed any of our original elements, we’ve only entered the floating data points in as new circles.</p><p>This concept was tricky for me to grasp at first, but once you understand the enter selection, you gain access to one of the most powerful tools of D3. Say, for example, I selected something I know to not exist. I then have an empty selection. I can bind any data array to it, then use .enter() to generate as many elements as I have data, all corresponding to a single data point. This is one of the most useful tools for D3 chart generation and a powerful one to master.</p><p>Finally, we have the .exit() selection. It is the opposite of the enter selection. The .exit() method will return all the <em>elements </em>that do not have matching data values.</p><pre><strong>Exit Selection:</strong></pre><pre>circles.data(oneElement).exit()</pre><pre><strong>//Gives back all elements without Data to bind to</strong></pre><pre><strong>//e.g. [circle2] will be unmatched</strong></pre><pre>circles.data(oneElement).exit().remove()</pre><pre><strong>Would result in something like //</strong> circle1= O</pre><p>What happens here is that the one element array of data is bound to our two element selection of circles. The first element gets bound to true, the second one does not get bound. Then, with .exit() we can access any elements that don’t have a data element bound to it (e.g. circle2). Calling .remove() on it will delete that circle from our DOM, leaving us with only an unchanged circle1.</p><p>This is a lot to consume at once, but these three functionalities are the meat of D3’s <strong>DOM traversal </strong>and <strong>data binding</strong> functionalities. They take some practice and some debugging, but once you’re used to them they’re remarkably flexible. Now that we are able to traverse the DOM and bind data to our selected elements, we reach the second critical component of D3: dynamic rendering.</p><h3>Dynamic Rendering</h3><p>This feature is possibly the hallmark of D3 and the primary reason you would use the library. D3 gives us the ability to generate and render HTML elements conditionally based on data arrays. We can access data values bound to HTML elements and derive the elements’ attribute values from the data functionally.</p><p>What this means is that if I have an array of numbers and select all rectangles (where none exist), then use .enter() and then .append(“rect”) to create a rectangle for each data value, I can functionally set each rectangle’s height to be equal to its corresponding data value. I can also set its location dependent on its index in the selection. Suddenly, I have a bar graph.</p><p>This:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b563ba809143e6a1d683fa42f5d6670b/href">https://medium.com/media/b563ba809143e6a1d683fa42f5d6670b/href</a></iframe><p>Becomes something like this (without the labels and dress-up of course):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IKpxxiD9M39elRkFft4PpA.png" /><figcaption>From a personal project of mine in progress: A graphing application that parses CSVs and generates graphs from selected columns/rows, written almost entirely in JS, Rails API backend, Implements D3.</figcaption></figure><p>Let’s break down what’s happening in this render:</p><ol><li>I retrieve an empty selection under canvas.</li><li>I append my four data values to the empty selection.</li><li>Using enter, I access all the floating data without elements.</li><li>For each, I append a rectangle and set its width to 10, and its y position to 100. <em>Note</em>: This is not how canvas actually works with respect to Y positioning, but just for the sake of explanation.</li><li>I set each rectangle’s height, the value of which is this rectangle’s data value.</li><li>Similarly, I can use the index as the second argument of the function in i and set the x position dependent on its index. First element at x=0, then x=60, then x=120 and so on</li></ol><p>This is where the unlimited possibilities of D3 start to open up to us. We’re able to generate any kind of element in response to collections of data and specify how they are rendered based on their bound information. This flexibility can be extended to any type of visualization. The best part is that D3 has hundreds of helper methods and functions to work out the more difficult aspects of graph generation for us.</p><p>Features like scaleLinear() allow us to translate a dataset from one spread to another in a linearly. For example, an array from 0 to 500 could be converted linearly and accurately to an array from 0 to 100 for percentage-based rendering.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/082043af55168531f67e3749ed7fa22a/href">https://medium.com/media/082043af55168531f67e3749ed7fa22a/href</a></iframe><p>Methods like transition() allow us to easily set up animation CSS for our elements as part of their rendering. The code below will render the circle at the top left corner, wait one second, then move it to the bottom right corner over two seconds.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43745c08e0e05193dc73c2ac001d7637/href">https://medium.com/media/43745c08e0e05193dc73c2ac001d7637/href</a></iframe><p>Using scales, D3’s axis functionality allows us to generate properly scaled axes to append to our charts for ease of interpretation. Using the same array and scale from above:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/587fc62f9b874e2363cf91783ca66c2a/href">https://medium.com/media/587fc62f9b874e2363cf91783ca66c2a/href</a></iframe><p>Once you practice them, every one of these methods works compactly, smoothly, and relatively intuitively when writing out the code. The size of our arrays becomes arbitrary, and everything is rendered conditionally and appropriately based on our data. This results in precise, accurate, and clean visualizations of data that are as dynamic as our code. Coupled with D3’s extensive styling and axis functions, we can interpret arbitrarily large datasets in ways that are much more digestible and appealing to the eyes. A large table of transactions, checking balances and savings over time can be converted into a much more appealing line graph:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eO6PlOQ0y1xryRqRwMEVEg.gif" /><figcaption>More from the same personal project displayed ealier, with axes removed.</figcaption></figure><h3>Conclusion</h3><p>D3 is a fairly meaty library to dig into, and while it does save a lot of work when it comes to rendering, scaling, and some mathematical computations, it is still largely up to the user to understand how the data needs to be handled and manipulated to render charts properly. That can be difficult to do and I had my fair share of fun debugging my mistakes with this library my first time learning it. I would, however, describe learning it as somewhat like riding a bike. Once you get going, the code is remarkably intuitive, and you’ll outpace every pedestrian charting in vanilla JS by miles. It is currently my go-to JS library for data visualization and one of my personal favorite tools to experiment with.</p><p>A quick glance at the <a href="https://github.com/d3/d3/wiki/Gallery">D3 Examples Gallery</a> will give you a better idea of what experts and teams are able to construct with this library. If that doesn’t make you fall in love with graphs, nothing will!</p><p>Graphing and data sifting are not always enjoyable but in an age of information science, they’re always useful. Plus, D3 is one of the more popular libraries out there for JS visualization, so there’s a large number of resources and tutorials available to help you learn all its features.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fvideoseries%3Flist%3DPL6il2r9i3BqH9PmbOf5wA5E1wOG3FT22p&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dn5NcCoa9dDU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fn5NcCoa9dDU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/57f21bcf0925299ce2eb0e7b7091ff62/href">https://medium.com/media/57f21bcf0925299ce2eb0e7b7091ff62/href</a></iframe><p>I personally recommend this youtube series by d3Vienno to start. It’s a great series that goes through the basics of rendering and data binding, and while some of the syntax he uses is changed in newer versions, the vast majority of it is consistent. It’s a great series for a first-start code-along.</p><p>For any of you who made it this far in reading, I hope I’ve been able to help you understand the basics of the D3 library. More importantly, I hope that I’ve inspired some of you to take up this library yourself and experiment with large dataset visualization. Math can be hard, numbers are gross, but pictures are pretty. With hard work and patience, unpleasant tables, numbers, and data collections can be turned into something beautiful to look at. Something readable, and enjoyable by all.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68e37b451822" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/d3-responsive-and-dynamic-visualizations-for-data-and-other-easy-recipes-68e37b451822">D3: Responsive and Dynamic Visualizations for Data and Other Easy Recipes</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Regular Expressions: Making Your Life Easier and Your Code Harder to Read!]]></title>
            <link>https://medium.com/analytics-vidhya/regular-expressions-making-your-life-easier-and-your-code-harder-to-read-e171cabe2997?source=rss-3d24a95d9086------2</link>
            <guid isPermaLink="false">https://medium.com/p/e171cabe2997</guid>
            <category><![CDATA[regex]]></category>
            <category><![CDATA[flatiron-school]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[codingbootcamp]]></category>
            <dc:creator><![CDATA[Griffin Poole]]></dc:creator>
            <pubDate>Thu, 12 Dec 2019 16:47:13 GMT</pubDate>
            <atom:updated>2020-01-31T16:52:13.126Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s an unfortunate truth that I have come across a less-than-agreeable part of my CS and coding knowledge through a less-than-traditional route. During my undergraduate term at Rutgers University I took a semester of intro to CS as an elective where I learned Java as my first language, then followed that up with a small foray into Data Structures before dropping it to concentrate on finishing up my major thesis. By that time however, I had made several friends who were CS majors, and it was at this point that I was introduced to my primary source for computer terminology and self-learning. My non-traditional source? Inside jokes, and comics…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*buu0O2c0hg8_cPO0Q_d-IQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*6O2fiWWJy7Ban_oF5AJejg.png" /><figcaption>Left: Something of that ilk webcomic: Right xkcd, a webcomic of romance, sarcasm, math and language <a href="https://xkcd.com/">https://xkcd.com/</a></figcaption></figure><p>Naturally as a complete amateur at the time, most coding jokes went right over my head (and still do), but surprisingly they turned out to be an excellent resource for finding terms and concepts to look up and research on my own. Dialoguing with people in the field of Computer Science and making an effort to educate myself to contribute in the circle is one of the major reasons that I continued to delve into the field and eventually find my love for its logic and languages.</p><p>Being able to <em>read about a concept or language</em> (humorous or otherwise) and <em>not understand a word of it</em>, but <em>understand</em> that these unknown concepts are now <em>no longer out of reach</em> was a unsuspected source of drive and education for me in my early periods of coding.</p><p>Now, on the note of encountering intimidating unknown material and back to the point of this article…</p><h3>Regular Expressions!</h3><p>If you have never encountered or heard of a regular expression before (regex for short), I’ve provided here a quick snippet of a regular expression example to help clarify their purpose.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/564/1*5Y2m699ZHfuyjBDe7sNZXw.png" /><figcaption>A phone number regular expression</figcaption></figure><p>Easy to follow right? No, it is not. Regular expressions at first glance are some of the most syntactically unfriendly looking code snippets I’ve ever encountered. They look like they belong in the realm of machine and assembly languages, but almost worse (<a href="https://upload.wikimedia.org/wikipedia/commons/f/f3/Motorola_6800_Assembly_Language.png">Assembly Language</a>). I first encountered regular expressions years ago in a similar fashion to my prior story—through a comic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*oGaYyDJNEZ6UL5nN-43YMg.png" /><figcaption>Another beloved xkcd comic, check it out! <a href="https://xkcd.com/">https://xkcd.com/</a></figcaption></figure><p>What I gathered from this bit the first time I read it was that regular expressions were probably a powerful tool for sorting through otherwise unwieldy data, and that it might be hard considering the self described heroism of being able to swoop in and parse through 200MB of emails with a few clicks.</p><p>How valorous of our wondrous regex experts to grace us with such a powerfully niche toolset, we are humbled truly! Such was my process at the time I looked up an example of a regex online. Then, I quickly closed the window and never went back to it until two years later when I began my term at Flatiron School. I finally knuckled down and decided to learn them. Having learned them now I can say with confidence:</p><blockquote>Regular expressions look much harder than they are</blockquote><blockquote>If you learn them, they will almost never be required over other tools</blockquote><blockquote>But if you know them, you will constantly find situations where they apply, and they will make many problems much, much easier</blockquote><p>So, how do they work?</p><h3>What They Do</h3><p>Regular expressions are a language/tool that excel at finding patterns in strings, and they are often used to search/parse through them. That is their primary purpose and really the only one we are likely to use them for. But they are very, very good at what they do. Here’s how they work:</p><p>Regex format will vary slightly between some languages and engines, but they are remarkably consistent across most coding languages they appear in (which is most of them).</p><p>They are typically flanked by /s. This is one of the most common ways a coding language identifies a series of characters to be interpreted as regex. For example:</p><blockquote>/insert your regex code here/</blockquote><p>Now, inside a regex expression, you define the details of a chunk of string that you are looking for (e.g. your specs for the regex to look for). so if I were to type:</p><blockquote>/a/</blockquote><p>This regex would look for any ‘a’ in the string I am comparing to. Regex are also read left-right, so:</p><blockquote>/ab/</blockquote><p>Would look for any ‘ab’ in my string. Easy peasy, but not very useful. So why are they so powerful? Regex has special characters that can generalize and change our search for us. So say for example I want to find exactly ten a’s consecutive. Well I can do:</p><blockquote>/aaaaaaaaaa/</blockquote><p>Or, more likely, I’d do this:</p><blockquote>/a{10}/</blockquote><p>Now we’re getting somewhere. So how do these interact with each other? Well at the end of the day a regex is still always read left-to-right. So say I only wanted ten a’s followed by three w’s and if it ends in a number, I want to include that number too optionally:</p><blockquote>/a{10}w{3}\d?/</blockquote><p>Boom. Now who knows why we would search this, but this means “find me substrings where there’s exactly ten a’s, immediately followed by exactly 3 w’s, then maybe a number (\d is all digits) but thats optional (the ? means one or none of the character before it)”</p><p>Lastly, and bear with me on this, what if I wanted to find all instances where a number was followed by a space, one or more words, then a comma and space, then more words, then a comma and space, then some combination of two capital letters, a space, then a number of exactly five digits. That was a mouthful but deep breathes…</p><blockquote>/\d+ (([a-zA-Z]+ )*[a-zA-Z]+, ){2}[A-Z]{2} \d{5}/</blockquote><blockquote>27 New Court Drive, Manchester, PA 90625</blockquote><p>Holy moly it’s gross, but what we’ve just done with our regular expression tools is create a line that will match with every United States address formatted like the one above. And that is incredibly powerful.</p><p>The secret to regex’s strength, and the intimidating side of it, comes from the wide array of special characters and expressions that you can chuck into a query to edit your search. However, for the most part they’re very intuitive once you start learning them, and they’re always available online to look up.</p><p>Some of the most frequent special characters that you’ll use include:</p><blockquote>\d =&gt;digits</blockquote><blockquote>\w =&gt;characters</blockquote><blockquote>\s =&gt;whitespaces</blockquote><blockquote>+ =&gt;one or more</blockquote><blockquote>* =&gt;none or more</blockquote><blockquote>? =&gt;once or none</blockquote><blockquote>{} =&gt;number of occurrences or range</blockquote><blockquote>. =&gt;any character except line break</blockquote><blockquote>[] =&gt;collection/range of characters</blockquote><blockquote>() =&gt;group of exclusive options</blockquote><blockquote>(?=something) =&gt;Positive lookahead</blockquote><blockquote>(?&lt;=something) =&gt;Positive lookbehind</blockquote><p>So maybe the power of regular expressions is starting to clarify, but some real world examples might help too. Below I’ve written out some examples of real-world application regex expressions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_1TmA-FKrLfVN25flbWBGA.png" /><figcaption>Phone #, English name Prefixes, Password Validation(not a good use of regex), Two space sentence breaks in a paragraph</figcaption></figure><blockquote>/^1?-?\d{3}-\d{3}-\d{4}$/ ~Phone number</blockquote><blockquote>/^M(rs|[rs]).?$/ ~Match English name prefixes</blockquote><blockquote>/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,}$/~Password Val</blockquote><blockquote>/(?&lt;=[.?!]) {2,}(?=[A-Z])/ ~Two space sentence break</blockquote><p>I won’t breakdown the logic of all of these, but the list above is a handful of the examples I’ve practiced within the first week of learning Regex: some from videos, some from articles, mostly from Codewars challenges and course assignments. Regex expressions can be used to check for existing matches. They can be passed into a split method to provide flexibility to large string splits, and they can be used to scare rubyists. Their power is substantial and real.</p><p>And the big secret is that you don’t have to be an expert to start using them quickly and easily; there are many online tutorials and references that can be accessed to learn their basic syntax and refer back to some ambiguous special character that you can’t remember.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SMdRtTlPCSmVdKnc6zZITw.png" /><figcaption>A subset of the regex expression tables at <a href="https://www.rexegg.com/regex-quickstart.html">https://www.rexegg.com/regex-quickstart.html</a>. One of my go-to sites for referencing regex syntax, definitely use them.</figcaption></figure><p>Regex expressions look gross, and they are gross, because they’re so useful :D. But seriously, I regret not jumping in and learning them sooner, because logically they are no more difficult than any coding language, and syntactically they end up being very intuitive and easy to look up. I highly recommend this youtube video by Corey Schafer if you want the quickest easiest route to picking up.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fsa-TUpSx1JA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dsa-TUpSx1JA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fsa-TUpSx1JA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1714f1da97a4135b8fa02e7f7b08b4f2/href">https://medium.com/media/1714f1da97a4135b8fa02e7f7b08b4f2/href</a></iframe><p>Don’t be afraid of regex and its intimidating syntax — be interested in its power and potential as another tool on your coding belt. Besides, despite the (sometimes questionable) origin of any of our coding knowledge-base, we all learned to write code and develop because we weren’t afraid to dive into something ugly and new that we didn’t understand. And with hard work and Google, maybe we can not understand difficult things a little bit less.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*0rMLSLfclFQ-SY_zq1FXaw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kT4WSpVMhllK0ez7Nii-nw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*KAuNlF0D4VEnqBGFcLFceA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e171cabe2997" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/regular-expressions-making-your-life-easier-and-your-code-harder-to-read-e171cabe2997">Regular Expressions: Making Your Life Easier and Your Code Harder to Read!</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Code-Switching: The Magic of Modularization and Communication in Programming]]></title>
            <link>https://medium.com/analytics-vidhya/code-switching-the-magic-of-modularization-and-communication-in-programming-2e8a927c38c4?source=rss-3d24a95d9086------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e8a927c38c4</guid>
            <category><![CDATA[polyglot-programming]]></category>
            <category><![CDATA[code-switching]]></category>
            <category><![CDATA[flatiron-school]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Griffin Poole]]></dc:creator>
            <pubDate>Thu, 21 Nov 2019 05:47:38 GMT</pubDate>
            <atom:updated>2019-11-27T08:57:38.755Z</atom:updated>
            <content:encoded><![CDATA[<h4>How linguistics made me a better coder.</h4><p>As a kid I grew up in a very multicultural household, with parents that remarried when I was around thirteen years old. Up until then I had been an only child to my parents and suddenly I was surrounded by three siblings from a wildly different ethnic background than my own. They were one of the best things to ever happen to me, but while the experience is something I’d love to share, I won’t be going deep into that in this blog. What I want to talk about is language.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qXyHxao5W2fj2Ag89K-2gA.png" /></figure><p>For anybody that has grown up in/experienced an environment where the people around you are multilingual you have probably seen an occurrence of a linguistics term known as code-switching. Code switching is defined as:</p><blockquote>“the practice of alternating between two or more languages or varieties of language in conversation.”</blockquote><p>And while there are several reasons that people engage in code-switch in conversation, there are two that I personally draw from when writing code(yes, this article is about code I promise).</p><ol><li>People may use code-switching to hide fluency or memory problems in the second language</li><li>Code-switching is used to mark switching from informal situations to formal situations</li></ol><p>Code switching can be used to accommodate for the skill and flexibility of the speaker, and it can also be used as a tool to deliver the most appropriate setting for your needs.</p><p>Now imagine instead of speaking, we’re talking about coding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P7i6ODBFr2ubdX_dd-9vHg.jpeg" /></figure><p>Coding languages at the end of the day are really just tools; tools built by other people to make our lives as developers easier. Anybody that’s coded in Java, LISP (or God-forbid <a href="https://en.wikipedia.org/wiki/Malbolge">Malbolge</a>) then switched over to Ruby/Python can attest that when iterating over an array, you probably wouldn’t pick any of the former. And each tool is going to have its ups, downs and specialties.</p><ol><li>Python is the leading language nowadays in terms of Data-Science and Analysis thanks to its massive open source libraries (Pandas, NumPy, SciPy, MatPlotLib etc.)</li><li>Ruby is one of the most widely preferred startup company web development languages due to its magical Rails web app framework</li><li>SQL is a widely used database query language heavily incorporated in many functions of higher level languages(including Ruby and Pandas)</li><li>Java is nice, because its everywhere</li></ol><p>A lot of these languages do similar things, some just do them better or maybe even easier. But we can utilize the same thought process behind code switching for our, well, code!</p><p>If you have a fluency or memory problem in a particular language that you haven’t used in years but know a one-line solution in another, wouldn’t it be nice to just get it easily and then tell your rusty code where to find it? Or say you’re working with legacy C++ code that looks like gibberish to you and your boss wants you to do a one-time transfer to it’s connected SQL database. Well nobody said you had to get the transfer done in C++, just do it in Ruby.</p><p>Lastly, maybe you’re trying to make a high-level database analysis webpage and it needs to be done fast. Well, wouldn’t it be nice if we could code our complicated data set systems for the backend in Python, interface it all to a SQL database, link it up to a Rails web application and still have some time to sleep?</p><p>The true power of coding languages are brought out when you are able to use them for what they’re best at(or for what you’re best at), and offload weaknesses to languages better suited for the job. Modularize files, and plan out exactly what a block needs put in, and exactly what you expect it to put out. When you learn how to make your languages talk to each other you open up a whole world(or playground) of possibilities for development. You also save yourself the trouble of learning how to full stack develop in a new language every time you switch jobs. You have become a polyglot programmer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g6qs6E0czU9m5-6Hozu7IA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KUMuaXIJvSisDeqkx0ADbw.jpeg" /><figcaption>Factorio, a game all about planning ahead, organizing, and optimizing. It’s fun if you like that sort of thing. (My go to game for having fun with modular design!)</figcaption></figure><h3>Closing Story</h3><p>I’d like to end with a description of a small implementation I put together this week that inspired this article. At the time of writing this I’m attending Flatiron School’s Web Development Immersive Bootcamp and am currently four weeks in on the game.</p><p>A little over a week ago, I suffered a coder’s worst nightmare, a hand injury in my dominant hand. Oh the horror. I went from being able to write line after line of code as fast as I could think to having to type with one hand, and during project week to boot. I decided that learning how to avoid excess typing would be a good investment now and got into bash aliasing. Aliases are just terminal inputs that you can pre-define in either your .<em>bash_profile</em> or your .<em>bashrc</em> file in your user folder on Mac.</p><blockquote>.<strong>bash_profile</strong> sets up a shell environment on login, .<strong>bashrc</strong> sets up an environment without the login requirement. The way OS X Terminal runs by default however makes this difference mean little for us.</blockquote><p>But they don’t do everything. Aliases can only be used for one-line terminal commands. More complicated behaviors wouldn’t work. Additionally, you can’t pass arguments to an alias.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tWTC4jD9hNHolF2vjKFiZA.png" /><figcaption>My desired one-line command</figcaption></figure><h4><strong>The Problem:</strong></h4><p>I wanted to write a one-line command that I could pass a github url that would clone the repo in a new folder at a specified directory, then automatically open up the directory in VSCode. This way I could type “clone <em>paste-url-here</em>” and it would make a new folder, clone the repo to the folder, and open it up for me all in one step. But:</p><ol><li>Aliases can’t take arguments</li><li>I know next to nothing about bash scripting</li><li>Functions accept arguments in bash, but I didn’t know how to pass a bash argument anywhere other than more bash script</li></ol><p>That being said, I did know how to do all of these steps in Ruby. I could generate the url’s and directory addresses I needed line by line in a Ruby executable, and run everything step by step. So the last part of my function would just execute that file. But I wanted to pass in a new url in the command line every time that would change how the ruby code operated, and I didn’t know how to pass a bash function argument into Ruby.</p><p>So instead, I used a file type I knew both bash and ruby can communicate with easily. And, more importantly, one that I already knew how to work with. A simple .txt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*yvLn5faCjfE0Tc0IzEaeRg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/1*rw36ghOwphKBglwvrkwlgg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ELwsaiTt1X_JGzKmUPSv0A.jpeg" /><figcaption>If you’re ever stressed out about coding problems know that there’s a community out there dedicated to expressing CS frustrations through fake O’Reilly Instructional Book Covers. They really do help.</figcaption></figure><h4><strong>The Solution:</strong> Improvise!</h4><p>I knew that in my bash function, I could accept the url and use echo to write it to a .txt file that both my function and my ruby code knew how to access.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*6FEkZCd_2XdNpUDjDusL2g.png" /></figure><blockquote>$1 means the first argument a bash function accepts.</blockquote><blockquote>echo “” &gt; filename will overwrite any text in that file and replace it with the supplied string</blockquote><blockquote>echo “” &gt;&gt; filename will append a supplied string to the end of a file, but we didn’t need that here</blockquote><p>After saving my url, I could handle all of my logic in an easy-to-code ruby file and just execute the file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a2N1eBtnYKbhM9uJPwvxcQ.png" /></figure><p>In the ruby file, I pre-define my particular username and the directory I want my labs cloned into. Then I read the url freshly echo’d to my .txt and use these three variables to parse out and join the strings I need to run my system commands. The first one to clone the repo, the second to open the new file.</p><blockquote>‘system’ commands in Ruby execute shell commands as though you typed them in the terminal. For more ways to execute shell commands in Ruby: <a href="https://redpanthers.co/different-ways-to-run-shell-commands-in-ruby/">https://redpanthers.co/different-ways-to-run-shell-commands-in-ruby/</a></blockquote><p>I was so excited when I finally ran this and it worked for the first time. It was one of the first times I used two separate tools talking to each other without some built in library handling all the specifics for me. In fact, I was so excited that I shared this cool little thing I made with the rest of my cohort.</p><p>Then one of our lovely instructors(and I do mean that sincerely) informed us of a tool built into our learn suite that did something very similar to what I had made. And when I say similar, I mean it did what mine did and then did more and did it better. As quickly as my shortcut had come about it was swept away into the world of wheels that have been re-invented except my wheel was square because I only had a steel square to work with.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*trhZRsaPvzjKoIbqLCoKWA.jpeg" /></figure><p>Regardless, this was a fun exercise and one that I personally take a lot of pride in despite its relative simplicity. Knowing how to cross-interface between the different tools at your disposal helps in productivity, creativity, and drives you towards pursuit of mastery.</p><p>It is a concept that shows practicality in linguistics, and in code, and in all approaches to problem solving. So I will continue to exercise it in practice, and I encourage anyone with a passion for building things to do so as well. Thank you, and good luck!</p><ul><li><a href="https://www.computerworld.com/article/2467812/polyglot-programming----development-in-multiple-languages.html">Polyglot programming -- development in multiple languages</a></li><li><a href="https://www.thoughtco.com/code-switching-language-1689858">What Is Code Switching?</a></li><li><a href="https://deanwampler.github.io/polyglotprogramming/papers/">Talks and Papers</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e8a927c38c4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/code-switching-the-magic-of-modularization-and-communication-in-programming-2e8a927c38c4">Code-Switching: The Magic of Modularization and Communication in Programming</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>