Python’s rich library – a tutorial

Python’s rich library – a tutorial

Python, System Administration
The Python rich library is a package for having clearer, styled, and colored output in the terminal. rich works across multiple operating systems - including Windows, Linux, and macOS. In this post, we'll give an introduction to what it can do for you. You can get started with rich by installing it with pip. [code] pip install rich [/code] Once you have it installed, open up the command line and type in python. In order to get the additional functionality from rich, you'll need to do one more step, which you can see below. Running this snippet will allow you to have styled / formatted code interactively. You'll only need to do this once. [code lang="python"] from rich import pretty pretty.install() [/code] Here's a couple examples of automatic coloring for…
Read More
3 ways to do RPA with Python

3 ways to do RPA with Python

Python, System Administration
In this post we'll cover a few packages for doing robotic process automation with Python. Robotic process automation, or RPA, is the process of automating mouse clicks and keyboard presses - i.e. simulating what a human user would do. RPA is used in a variety of applications, including data entry, accounting, finance, and more. We'll be covering pynput, pyautogui, and pywinauto. Each of these three packages can be used as a starting point for building your own RPA application, as well as building UI testing apps. pynput The first package we'll discuss is pynput. One of the advantages of pynput is that is works on both Windows and macOS. Another nice feature is that it has functionality to monitor keyboard and mouse input. Let's get started with pynput by installing…
Read More
How to solve Sudoku with R

How to solve Sudoku with R

R
In this post we discuss how to write an R script to solve any Sudoku puzzle. There are some R packages to handle this, but in our case, we'll write our own solution. For our purposes, we'll assume the input Sudoku is a 9x9 grid. At the end result, each row, column, and 3x3 box needs to contain exactly one of each integer 1 through 9. Learn more about data science by checking out the great curriculum at 365 Data Science! Step 0) Define a sample board Let's define a sample Sudoku board for testing. Empty cells will be represented as zeroes. [code lang="R"] board <- matrix( c(0,0,0,0,0,6,0,0,0, 0,9,5,7,0,0,3,0,0, 4,0,0,0,9,2,0,0,5, 7,6,4,0,0,0,0,0,3, 0,0,0,0,0,0,0,0,0, 2,0,0,0,0,0,9,7,1, 5,0,0,2,1,0,0,0,9, 0,0,7,0,0,5,4,8,0, 0,0,0,8,0,0,0,0,0), byrow = T, ncol = 9 ) [/code] Step 1) Find the empty cells…
Read More
How to schedule a Python script on a Mac

How to schedule a Python script on a Mac

Python, System Administration
In a previous article, we talked about how to run Python from the Windows Task Scheduler. This post will cover how to schedule Python tasks on a Mac operating system as well as give an overview of the schedule package. Using crontab on Mac Python tasks can be scheduled on Mac using crontab. To do that, first, open up the Terminal. Then, we need to modify the crontab file. We can do that by typing crontab -e. This will open crontab in the default editor, which is typically vim. You can change the editor by adding the editor name in front of our command - for example, to modify the crontab file using nano, we can run nano crontab -e (followed by enter). Next, we need to add in a…
Read More
Why you should use vapply in R

Why you should use vapply in R

R
In this post we'll cover the vapply function in R. vapply is generally lesser known than the more popular sapply, lapply, and apply functions. However, it is very useful when you know what data type you're expecting to apply a function to as it helps to prevent silent errors. Because of this, it can be more advisable to use vapply rather than sapply or lapply. See more R articles by clicking here Examples Let's take the following example. Here, we have a list of numeric vectors and we want to get the max value of each vector. That's simple enough - we can just use sapply and apply the max function for each vector. [code lang="R"] test <- list(a = c(1, 3, 5), b = c(2,4,6), c = c(9,8,7)) sapply(test,…
Read More
How to create a progress bar in Python

How to create a progress bar in Python

Python
Creating a progress bar is really useful if you have a long-running task and you'd like to know how far along you are. In this post we'll talk about two packages for creating progress bars in Python: tqdm and progressbar2. Creating a progress bar with Python's tqdm package Let's get started by installing tqdm. We can do that using pip: [code] pip install tqdm [/code] Once we have tqdm installed, it's straightforward to use. Essentially, we just need to wrap whatever object we're looping over (i.e. any object that is iterable) inside tqdm. Then, when the code runs, Python will print out a progress bar so that you can know how far along you are. [code lang="python"] from tqdm import tqdm result = 0 for i in tqdm(range(10000000)): result +=…
Read More
How to create an API for your R code

How to create an API for your R code

R
In the video linked below we discuss how to convert your R code into an API using the awesome plumber package! Learn more by clicking here or by following the links below. The plumber package allows you to convert R functions into API calls. For example, rather than launching R and executing a function, you can use plumber to turn the function into an API request that can be called from other software (e.g. Python, cURL, etc.). This is enormously useful in a lot of applications e.g. building a web application with Python that can make API calls to R model objects, retrieving R plots into other applications, and more. Check out the video below to learn more! Link to full video: https://www.youtube.com/watch?v=Z2Aofr4UIFY To skip to specific parts of the…
Read More
How to scrape news articles with Python

How to scrape news articles with Python

Python, Web Scraping
In this post we're going to discuss how to scrape news articles with Python. This can be done using the handy newspaper package. Introduction to Python's newspaper package The newspaper package can be installed using pip: [code] pip install newspaper [/code] Once its installed, we can get started. newspaper can work by either scraping a single article from a given URL, or by finding the links on a webpage to other news articles. Let's start with handling a single article. First, we need to import the Article class. Next, we use this class to download the content from the URL to our news article. Then, we use the parse method to parse the HTML. Lastly, we can print out the text of the article using .text. Scraping a single article…
Read More
How to create PowerPoint reports with R

How to create PowerPoint reports with R

File Manipulation, R
In my last post, we discussed how to create and read Word files with R's officer package. This article will expand on officer by showing how we can use it to create PowerPoint reports. Getting started Let's get started by loading officer. [code lang="R"] library(officer) [/code] Next, we'll create a PowerPoint object in R using the read_pptx function. [code lang="R"] pres <- read_pptx() [/code] To add a slide, we use the add_slide function. The first slide we'll create is the title slide. We specify the type of slide in the layout parameter. There's several other possibilities here including "Title and Content", "Blank", "Title Only", "Comparison", "Two Content", and "Section Header". Secondly, we use ph_with to add the title text. [code lang="R"] # add title slide pres <- add_slide(pres, layout =…
Read More
How to read and create Word Documents in R

How to read and create Word Documents in R

File Manipulation, R
Reading and creating word documents in R In this post we'll talk about how to use R to read and create word files. We'll primarily be using R's officer package. For reading data from Word Documents with Python, click here. Creating Word reports with the officer package The first thing we need to do is to install the officer package. [code lang="R"] install.packages("officer") [/code] We'll also be using the dplyr package, so you'll need to install that the same way if you don't have it already. Next, let's load each of these packages. [code lang="R"] library(officer) library(dplyr) [/code] Now, we'll get started creating a report! First, we will use the read_docx function to create an empty Word file. [code lang="R"] # create empty Word file sample_doc <- read_docx() [/code] Adding…
Read More