Programming & Scripting

How to use argparse to parse python scripts parameters

Objective Learn to use the argparse module to easily parse python scripts parameters Requirements Basic knowledge of python and object oriented concepts Difficulty EASY Conventions # - requires given command to be executed with root privileges either directly as a root user or by use of sudo command $ - given command to be executed as a regular non-privileged user Introduction In a previous article we have seen how to parse command line arguments using getopts in the context of bash scripts...
Read More...

How to Play Audio With VLC In Python

Objective Play audio with VLC in Python. Distributions This will work on any Linux distribution Requirements A working Linux install with Python and VLC. Difficulty Easy Conventions # - requires given command to be executed with root privileges either directly as a root user or by use of sudo command $ - given command to be executed as a regular non-privileged user Introduction There are plenty of ways to play audio files with Python. It really depends on your application, but the easiest way,...
Read More...

How to Use JSON API Data In Python

Objective Consume a JSON API in Python. Distributions This will work on any Linux distribution. Requirements A working Linux install with Python. Difficulty Easy Conventions # - requires given command to be executed with root privileges either directly as a root user or by use of sudo command $ - given command to be executed as a regular non-privileged user Introduction One of the main reasons that you'd like to work with JSON in Python is consuming APIs. There are hundreds of excellent public...
Read More...

How to Parse Data From JSON Into Python

Objective Parse JSON data in Python. Distributions This will work on any Linux distribution. Requirements A working Linux install with Python. Difficulty Easy Conventions # - requires given command to be executed with root privileges either directly as a root user or by use of sudo command $ - given command to be executed as a regular non-privileged user Introduction JSON is a favorite among developers for serializing data. It's used in most public APIs on the web, and it's a great way to pass...
Read More...

How to Encode Data From Python To JSON

Objective Encode Python data into JSON. Distributions This will work on any distribution with Python installed. Requirements A working Linux install with Python Difficulty Easy Conventions # - requires given command to be executed with root privileges either directly as a root user or by use of sudo command $ - given command to be executed as a regular non-privileged user Introduction JSON is the universal format for passing data between programs and/or languages, especially on the web. Python...
Read More...

How to modify scripts behavior on signals using bash traps

Objective The objective of this tutorial is to describe how to use the bash shell trap builtin to make our scripts able to perform certain actions when they receive a signal or in other specific situations. Requirements No special requirements Difficulty EASY Conventions # - requires given command to be executed with root privileges either directly as a root user or by use of sudo command $ - given command to be executed as a regular non-privileged user Introduction When writing scripts that...
Read More...

How to obtain Sunrise & Sunset time for any location from Linux command line

Objective The objective is to use command line and bash shell script to obtain Sunrise & Sunset time information for any given location. Operating System and Software Versions Operating System: - Linux distribution agnostic. Requirements Installed lynx tool and access to shell command line. Your location code obtained from https://weather.codes/search/ . Difficulty EASY Conventions
Read More...

How to use getopts to parse a script options

Objective Learn how to use the bash getopts builtin to parse a script options Operating System and Software Versions Operating System: - All Linux distributions Requirements No special requirements, just access to a bash shell Difficulty MEDIUM Conventions
Read More...

Children multiplication tables practice test with Bash script on Linux

Introduction The following post is a little bit different from my other posts as it does not solve any of your Linux configuration problems. Apologies there! This post is an attempt to kill multiple birds with one stone, hence expose children to a command line, avoid web flash and ads driven multiplication practice tests online and most importantly gain some multiplication skills along the way. My daughter reached the age where she is required to perform multiplication calculation at school....
Read More...

Learn the basics of the ZSH shell

Objective Install ZSH with Oh My ZSH and learn the basic features. Distributions ZSH is available in the repositories of nearly every distribution. Requirements A working Linux install with root privileges. Difficulty Easy Conventions # - requires given command to be executed with root privileges either directly as a root user or by use of sudo command $ - given command to be executed as a regular non-privileged user Introduction Bash isn't bad. It gets the job done just fine, but have you...
Read More...

Introduction to Bash shell redirections

Objective Learn to use redirections, pipes and tee in the Bash shell Operating System and Software Versions Operating System: - Linux distribution agnostic Requirements Access to a Bash shell Difficulty EASY Conventions
Read More...

Python Exception Handling

Introduction Python will let you know when you get your syntax wrong. It'll immediately fail and not allow your program to run. What about when your code has a different type of problem? Those are called exceptions, and they tend to be harder to catch. It's up to you to recognize situations where hey might come up and catch them to prevent your program from crashing altogether. Imagine a scenario where you need user input. Do you want your program to crash every time a user mistypes something...
Read More...

Python OS Module

Introduction Python is a powerful scripting language. So, why not use it to script Linux? The os module is Python's answer to handling many Linux file operations. The os module allows Python to perform many of the file and folder operations that you'd typically carry out in the Linux command line. It enable you to begin swapping out Bash for Python, which makes for a much cleaner and friendlier scripting experience. Loading OS The os module is a Python module like any other. In any script...
Read More...

Python Lambdas

Introduction You've worked with both functions and methods, but there's a different type of function, the anonymous function. Anonymous functions are just functions without names, and in Python, they are handled with the lambda operator. Anonymous functions allow you to use the behavior of a function without having to create a function. This is especially good when you only need to use a function once or twice. It might sound like an unusual occurrence, but it happens fairly frequently when a...
Read More...

Reading And Writing Files With Python

Introduction Jut about any program of a decent size needs to be able to read and write from files. At very least, it needs to be able to generate logs. Python is also tightly integrated into Linux system administration and scripting. Again, reading and writing are important for managing a system. Opening A File Python creates and object from the file that it opens. It can then manipulate that object. When it's done, it uses the object to save the any changes back to the file. Create a new text...
Read More...

Python Packages and Modules

Introduction Whenever you need some additional functionality in Python, you turn to the import keyword to pull in extras from Python modules. You've used common ones like the math module plenty of times. Now, you will learn how to create your own Python modules and packages to compartmentalize your code. Modules are sort of like classes in that they make your code modular. While classes make code modular within a program and serve as the blueprints for objects, modules make all of your code...
Read More...

Python Polymorphism

Introduction Polymorphism is yet another important aspect of Object Oriented Programming. No Warcraft fans, it doesn't have anything to do with turning people into sheep. That'd be much cooler. Instead, Polymorphism allows methods and functions to use classes with similar functionality in the same way. Polymorphism cuts down on the amount of code that you need to write by eliminating redundancy in a logical and meaningful way. It relies on you, the programmer, to be clever in your design and...
Read More...

Python Inheritance

Introduction Inheritance is yet another key concept in Object Oriented Programming, and it plays a vital role in building classes. It allows a class to be based off on an existing one. When you first started writing Python classes, you were told to just put "Object" in the parenthesis of the class definition and not think too much about it. Well, now's the time to start thinking about it. "Object" is actually the base class that all Python classes inherit from. It defines a basic set of...
Read More...

Python Encapsulation

Introduction Encapsulation is one of the fundamental aspects of Object Oriented Programming. It allows programmers better control of how data flows in their programs, and it protects that data. Encapsulation also makes objects into more self-sufficient and independently functioning pieces. The concept of encapsulation builds on what you did in the last two guides with classes and constructors. Constructors usually are usually used in close conjunction with encapsulation and actually aid in...
Read More...

Python Constructors

Introduction By now, you should be familiar with the way basic classes work in Python . If classes were just what you've seen, they'd be fairly rigid and not all that useful. Thankfully, classes are much more than just that. They are designed to be much more adaptable and can take in information to shape the way they look initially. Not every car starts off exactly the same, and neither should classes. After all, how awful would it be if every car was an orange 71' Ford Pinto? That's not a...
Read More...