<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Dennis Farley </title>
    <description>The latest articles on Forem by Dennis Farley  (@dfarlz97).</description>
    <link>https://forem.com/dfarlz97</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1063156%2F6a42edc3-4e72-4d6a-a211-27748e765d08.png</url>
      <title>Forem: Dennis Farley </title>
      <link>https://forem.com/dfarlz97</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dfarlz97"/>
    <language>en</language>
    <item>
      <title>Using PyAutoGUI to Automate Your Python Scripts</title>
      <dc:creator>Dennis Farley </dc:creator>
      <pubDate>Sat, 20 May 2023 02:42:00 +0000</pubDate>
      <link>https://forem.com/dfarlz97/using-pyautogui-to-automate-your-python-scripts-1p6h</link>
      <guid>https://forem.com/dfarlz97/using-pyautogui-to-automate-your-python-scripts-1p6h</guid>
      <description>&lt;h2&gt;
  
  
  Project Day
&lt;/h2&gt;

&lt;p&gt;Hello! Today I completed phase 3 of 5 of my full-stack software engineering course here at Flatiron School.  As with every phase, we were tasked with submitting and presenting a final project.  This phase, we were tasked with building a CLI application using python and SQLAlchemy ORM.  Excited to put our skills to work, my partner and I brainstormed project ideas.  Ultimately, we settled on creating a application that mimics the functionality of a Command Line Interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;With python's built-in &lt;a href="https://docs.python.org/3/library/os.html"&gt;os module&lt;/a&gt;, allowing the user to input commands to navigate and interact with the files within our SQLite database was simple enough. &lt;/p&gt;

&lt;p&gt;For example, this is was our code to cd (change directory) based on user input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os 

#instantiating terminal while program is active 
terminal_active = True

while terminal_active == True: 
#check if user input is valid 
if x.split(" ")[0] == "cd":

#cd into user's specified directory 
    os.chdir(x.split(" ")[1])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy enough. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem: Opening &lt;em&gt;and&lt;/em&gt; Playing a Youtube Video
&lt;/h2&gt;

&lt;p&gt;While implementing the "open" command (i.e. open file), I decided to add an Easter egg: if the inputed file does not exist, a youtube video (&lt;a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"&gt;Rick Roll - &lt;em&gt;Never Gonna Give You Up&lt;/em&gt;&lt;/a&gt;) is opened and played in the browser.  By use of another python module &lt;a href="https://docs.python.org/3/library/webbrowser.html"&gt;webbrowser&lt;/a&gt;, opening a video in the browser was simple enough:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import webbrowser

webbrowser.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
## This will open the file in the user's default browser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, the tricky part was getting the video to then play after being opened. Sifting through dozens of outdated &lt;a href="https://stackoverflow.com/"&gt;stackoverflow&lt;/a&gt; posts yielded no promising results.  Many proposed solutions were outdated, and others proved too cumbersome to implement.  &lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution: Automate
&lt;/h2&gt;

&lt;p&gt;At last, I stumbled upon &lt;a href="https://pyautogui.readthedocs.io/en/latest"&gt;PyAutoGUI&lt;/a&gt; and a solution was in sight. PyAutoGUI is an installable package which allows you to use Python scripts to directly control the mouse and keyboard.  Thus, use of PyAutoGUI makes for seamless automation of application-to-application interactions.  &lt;/p&gt;

&lt;h2&gt;
  
  
  PyAutoGUI Functions
&lt;/h2&gt;

&lt;p&gt;For my particular problem, three of PyAutoGUI's functions came in handy: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;size()&lt;/li&gt;
&lt;li&gt;moveTo()&lt;/li&gt;
&lt;li&gt;click()&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The size function returns a named tuple with the resolution (width and height) of your screen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;width_height = pyautogui.size() 

&amp;gt;&amp;gt;&amp;gt; width_height

#return value
Size(width=1920, height=1080)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the size function, I was able to determine the x,y coordinates center-screen.  Next, it was only a matter of using moveTo() and click() to move the cursor to the center of the screen and click "play":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import webbrowser
import pyautogui
import time

webbrowser.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ")

#wait five seconds before moving cursor
time.sleep(5)

# moves cursor to center of (my) screen
pyautogui.moveTo(700, 450, 2)      

# clicks cursor after .05 seconds
time.sleep(.05)
pyautogui.click()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Success!
&lt;/h2&gt;

&lt;p&gt;Et voilà! With this code, my program successfully opened and played the youtube video.  While my use of PyAutoGUI's automation was quite simple, it nonetheless serves as an example of the enormous potential automation has in the world of software engineering.&lt;/p&gt;

&lt;h3&gt;
  
  
  Thanks for Reading!
&lt;/h3&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>automation</category>
    </item>
    <item>
      <title>Using 'Faker' to Generate Fake Data</title>
      <dc:creator>Dennis Farley </dc:creator>
      <pubDate>Tue, 11 Apr 2023 22:53:51 +0000</pubDate>
      <link>https://forem.com/dfarlz97/using-faker-to-generate-fake-data-39in</link>
      <guid>https://forem.com/dfarlz97/using-faker-to-generate-fake-data-39in</guid>
      <description>&lt;h2&gt;
  
  
  About Me
&lt;/h2&gt;

&lt;p&gt;Hello, my name is Dennis and this is my first blog post as a student at Flatiron School. Having just completed the first of five phases here, I am excited to share something new I have learned along the way.  Namely, how to use 'Faker' (and other similar gems), a library for generating fake data.  &lt;/p&gt;

&lt;p&gt;While working on my phase 1 project (a variation of the snake game), I came across the need for data, and a lot of it. I had a leaderboard that would keep track and display the user's score, but the board was empty-it needed players! &lt;/p&gt;

&lt;p&gt;I knew it would be too cumbersome to hard code data for each player object in my db.json file, and too time-consuming to play the game enough times to fill out the leaderboard. &lt;/p&gt;

&lt;p&gt;The solution: Faker! &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Faker?
&lt;/h2&gt;

&lt;p&gt;Faker is a library used to generate fake data which can then be used by a developer in their project.  &lt;/p&gt;

&lt;p&gt;For instance, here is a code snippet of the faker data I used in my db.json file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "players": [
    {
      "id": 1,
      "userName": "Tiffany.Koss",
      "score": 2029
    },
    {
      "id": 2,
      "userName": "Zoie63",
      "score": 625
    },
    {
      "id": 3,
      "userName": "Brandon_Beahan52",
      "score": 2013
    },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;No, "Tiffany Koss" is not a real person and no privacy violations are being committed here.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;While the data is fictitious, the developer can choose to make each value unique and eliminate the need to worry about instances of repeat-data. In order to do so, you can prefix your method call like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Faker::Name.unique.name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to Use
&lt;/h2&gt;

&lt;p&gt;While I am going to walk through some of the steps necessary to use Faker, you can navigate to &lt;a href="https://github.com/faker-ruby/faker"&gt;https://github.com/faker-ruby/faker&lt;/a&gt; and peruse the README.md for more in-depth information on installation and usage. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; After first making sure you are in your projects local directory, use a package manager (npm, yarn, pnpm) to install Faker as a Dev Dependency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @faker-js/faker --save-dev

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: This post is for the implementation of Fakerjs. The set-up process for Ruby, Java, and Python is slightly different.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Create a generateData.js file within your project directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dennis@Dennis's-MacBook-Pro my-project % touch generateData.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Within your generateData.js, retrieve the Faker data by providing the program with a route to '@faker-js/faker':&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { faker } from '@faker-js/faker'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; Create a function that generates your data.  Here is what my function looked like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { faker } = require('@faker-js/faker');

function generateData() {
    const players = [] //create an empty array for player data

    for (let i = 1; i &amp;lt; 201; i++) { // iterate through array
        const player = { // structure data within array
            id: i,
            userName: faker.internet.userName(),
            score: faker.datatype.number({ min: 10, max: 3000 })
        }
        players.push(player) 
          // fills empty players array with data
    }

    return {
        players // returns populated array 
    };
}

module.exports = generateData; // exports data to be used in your project (in this case it is exported from the module that houses my leaderboard) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the function generateData creates an empty array for player data, iterates through the array, structures the data within the array, and then exports it from the module.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; Now that you have retrieved your data and wrote a function to structure and export it, you are ready to implement the data within your program.  Doing so is simply a matter of writing a function(s) to render the data according to your needs.  &lt;/p&gt;

&lt;p&gt;For instance, here is how I was able to get my data (player names and scores) to display on my leaderboard:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getPlayers(){
    fetch(baseUrl)
    .then(res =&amp;gt; res.json())
    .then(players =&amp;gt; players.sort((playerA, playerB) =&amp;gt; {
        const playerAScore = Number(playerA.score)
        const playerBScore = Number(playerB.score)
        return playerBScore - playerAScore
    }))
    .then(renderRankings)
}


function renderRankings(players){
    for(let i = 0; i &amp;lt; players.length; i++){
        let ranking = i + 1
        renderUserRanking(ranking, players[i])
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code snippet (used to display player data retrieved from Faker) provides a simple demonstration of how one can make use of their fake data. &lt;/p&gt;

&lt;p&gt;Hopefully this blog post was helpful and you now have the knowledge necessary to make use of Faker in your own applications and projects.&lt;/p&gt;

&lt;p&gt;For more information on implementing Faker and it's different data types (animals, names, vehicles, etc.) refer to &lt;a href="https://fakerjs.dev/api/"&gt;https://fakerjs.dev/api/&lt;/a&gt;.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Thanks for reading!
&lt;/h3&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
