Skip to main content

Automation

Check Disk Usage

import shutil

def check_disk_usage(disk):
du = shutil.disk_usage(disk)
free = du.free / du.total * 100
return free > 20
check_disk_usage('/')

Check CPU Usage

import psutil

def check_cpu_usage():
usage = psutil.cpu_percent(1)
return usage < 75

Check localhost

import socket

def check_localhost():
localhost = socket.gethostbyname('localhost')
return localhost == "127.0.0.1"

Check connectivity

import requests

def check_connectivity():
request = requests.get("http://www.google.com")
return request.status_code == 200

Reading and writing files

Link

Remove file

import os

os.remove("file.txt")

Rename file

os.rename("original.txt", "new.txt")

Check if file exists

os.path.exists("file.txt")

Get size of a file

os.path.getsize("file.txt")

Last modfied

import datetime
timestamp = os.path.getmtime("file.txt")
datetime.datetime.fromtimestamp(timestamp)

Get absolut path

os.path.abspath("file.txt")

Directories

Get current working directory

os.cwd()

Change current working directory

os.chdir("new_dir")

Create dir

os.mkdir("my_dir")

Remove dir

os.rmdir("my_dir")

Get all files in dir

os.listdir("my_dir")

CSV

Read

import csv
with open("csv_file.txt") as f:
csv_f = csv.reader(f)
for row in csv_f:
name, phone, role = row

Write

with open("csv_file.txt", "w") as f:
writer = csv.writer(f)
writer.writerows([["bla", "123"], ["blubb", "456"]])

Read from dict

with open("csv_file.txt") as f:
reader = csv.DictReader(f)
for row in reader:
row["name"]

Writing from dict

with open("csv_file.txt", "w") as f:
keys = ["name", "department"]
writer = csv.DictWriter(f, fieldnames=keys)
writer.writeheader()
writer.writerows([{"name": "bob", "department": "dep1"}])

Regex

grep, sed, awk

import re
result = re.search(r"aza", "plaza")
result = re.search(r"p.eng", "Pangaea", re.IGNORECASE)
result = re.search(r"[^a-zA-Z]", "This is a sentence with spaces.")
result = re.findall(r"cat|dog", "I like both dogs and cats.")
re.search(r"Py.*n", "Pygmalion")
  • Match everything that begins with A and ends with a:
re.search(r"^A.*a$", "Azerbaijan")
  • Split on sentence:
re.split(r"[.?!]", "One sentence. Another one? And the last one!")
  • Split on sentence but add chars we split on:
re.split(r"([.?!])", "One sentence. Another one? And the last one!")
  • Substitute
re.sub(r"[\w.%+-]+@[\w.-]+", "[REDACTED]", "Received an email for go_nuts95@my.example.com")

Better save than sorry

  • Reorder matches
re.sub(r"^([\w .-]*), ([\w .-]*)$", r"\2 \1", "Lovelace, Ada")

Grep

grep  thon /usr/share/dict/words
  • Ignore case
grep -i python /usr/share/dict/words
  • Beginswith
grep ^fruit /usr/share/dict/words
  • Endswith
grep cat$ /usr/share/dict/words

Processes

  • Access arguments
sys.argv
  • Access environment
os.environ.get("PATH", "")
  • Get exit value of the last command:
echo $?
import subprocess
result = subprocess.run(["ls", "this_file_does_not_exist"])
result.returncode
# 2

Obtain output of system command

result = subprocess.run(["host", "8.8.8.8"], capture_output=True)
result.returncode
# 0
result.stdout.decode("utf-8")
# '8.8.8.8.in-addr.arpa domain name pointer dns.google.\n'

result = subprocess.run(["rm", "doesnt_exist"], capture_output=True)
result.returncode
# 1
result.stderr.decode("utf-8")
# "rm: cannot remove 'doesnt_exist': No such file or directory\n"
  • Pass modified environment
my_env = os.environ.copy()
my_env["PATH"] = os.pathsep.join("/opt/myapp/", my_env["PATH"])
result = subprocess.run(["myapp"], env=my_env)

# result = subprocess.run(["myapp"], env=my_env, timeout=..., cwd=...)

Testing

#!/usr/bin/env python3
import unittest

from emails import find_email

class EmailsTest(unittest.TestCase):

def test_basic(self):
testcase = [None, "Bree", "Campbell"]
expected = "breee@abc.edu"
self.assertEqual(find_email(testcase), expected)

def test_one_name(self):
testcase = [None, "John"]
expected = "Missing parameters"
self.assertEqual(find_email(testcase), expected)

def test_two_name(self):
testcase = [None, "Roy","Cooper"]
expected = "No email address found"
self.assertEqual(find_email(testcase), expected)

if __name__ == '__main__':
unittest.main()

Exceptions

Source