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 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
How to schedule R scripts

How to schedule R scripts

R, System Administration
Running R with taskscheduleR and cronR In a previous post, we talked about how to run R from the Windows Task Scheduler. This article will talk about two additional approaches to schedule R scripts, including using the taskscheduleR package on Windows and the cronR package for Linux. For scheduling Python code, check out this post. Schedule R scripts with taskscheduleR Let's install taskscheduleR using the install.packages command. [code lang="R"] install.packages("taskscheduleR") [/code] Next, we just need to load the package to get started. [code lang="R"] library(taskscheduleR) [/code] Creating a sample R script to run automatically Before we do any scheduling, we need to first create a script. We'll save the code below in a file called "create_file.txt". This script will randomly generate a collection of integers and write them out to…
Read More
How to hide a password in a Python script

How to hide a password in a Python script

Python, System Administration
Background Python is widely used for web scraping and APIs, dealing with databases, sending emails, and other tasks that may involve credentials, like a username or password. Luckily, there are several ways to hide a password in a Python script, as well as to store multiple passwords for many users if needed. In this post, we'll talk about three such packages - keyring, passlib, and cryptography, with a focus on the first two. How to hide a password in a Python script with keyring keyring is an easy-to-use package that works by allowing you to store a password in your operating system's credential store. It's great in that it can work across multiple different operating systems. To get started, just use pip to install: [code] pip install keyring [/code] Next,…
Read More
10 R functions for Linux commands and vice-versa

10 R functions for Linux commands and vice-versa

File Manipulation, R, System Administration
This post will go through 10 different Linux commands and their R alternatives. If you're interested in learning more R functions for working with files like some of those below, also check out this post. How to list all the files in a directory Linux R What does it do? ls list.files() Lists all the files in a directory ls -R list.files(recursive = TRUE) Recursively lists all the files in a directory and all sub-directories ls | grep "something" list.files(pattern = "something") Lists all the files in a directory containing the regex "something" R [code lang="R"] list.files("/path/to/directory") list.files("/path/to/do/directory", recursive = TRUE) # search for files containing "something" in their name list.files("/path/to/do/directory", pattern = "something") # search for all CSV files list.files("/path/to/do/directory", pattern = ".csv") [/code] Linux [code lang="bash"] ls /path/to/directory…
Read More
How to run R from the Task Scheduler

How to run R from the Task Scheduler

R, System Administration
In a prior post, we covered how to run Python from the Task Scheduler on Windows. This article is similar, but it'll show how to run R from the Task Scheduler, instead. Similar to before, let's first cover how to R from the command line, as knowing this is useful for running it from the Task Scheduler. Running R from the Command Line To open up the command prompt, just press the windows key and search for cmd. When R is installed, it comes with a utility called Rscript. This allows you to run R commands from the command line. If Rscript is in your PATH, then typing Rscript into the command line, and pressing enter, will not result in an error. Otherwise, you might get a message saying "'Rscript'…
Read More
R: How to create, delete, move, and more with files

R: How to create, delete, move, and more with files

File Manipulation, R, System Administration
Though Python is usually thought of over R for doing system administration tasks, R is actually quite useful in this regard. In this post we're going to talk about using R to create, delete, move, and obtain information on files. How to get and change the current working directory Before working with files, it's usually a good idea to first know what directory you're working in. The working directory is the folder that any files you create or refer to without explicitly spelling out the full path fall within. In R, you can figure this out with the getwd function. To change this directory, you can use the aptly named setwd function. [code lang="R"] # get current working directory getwd() # set working directory setwd("C:/Users") [/code] Creating Files and Directories…
Read More
Running Python from the Task Scheduler

Running Python from the Task Scheduler

Python, System Administration
Background Running Python from the Windows Task Scheduler is a really useful capability. It allows you to run Python in production on a Windows system, and can save countless hours of work. For instance, running code like extracting data from a database on an automated, regular basis is a common need at many companies. How to run Python from the command line Before we go into how to schedule a Python script to run, you need to understand how to run Python from the command line. To open the command prompt (command line), press the windows key and type cmd into the search box. Next, suppose your python script is called cool_python_script.py, and is saved under C:\Users. You can run this script from the command prompt by typing the below…
Read More
File Manipulation with Python

File Manipulation with Python

File Manipulation, Python, System Administration
Getting started Python is great for automating file creation, deletion, and other types of file manipulations.  Two of the primary packages used to perform these types of tasks are os and shutil.  We'll be covering a few useful highlights from each of these. [code lang="python"] import os import shutil [/code] How to get and change your current working directory You can get your current working directory using os.getcwd: [code lang="python"] os.getcwd() [/code] Any actions you take without specifying a directory will be assumed to be associated with your current working directory i.e. if you create or search for a file without specifying a directory, Python will assume you're in the value of os.getcwd(). To change your working directory, use os.chdir: [code lang="python"] os.chdir("C:/path/to/new/directory") [/code] How to merge a directory name…
Read More