Sunday, August 11, 2024

Demonstration Coding: Python sys.argv[]

Python3 sys.argv[]

      Hello there! Welcome to my blog, and good evening to all good people. Today, I want to share something exciting about Python: the sys.argv list. This list is incredibly useful for capturing command-line arguments passed to a Python script, allowing you to use them directly within your program.

This code is intended to showcase my Python coding abilities. It demonstrates my coding style and proficiency. Let's dive in!

 

What Is sys.argv[]?

sys.argv[] is a list in Python that contains the command-line arguments passed to a Python script. It is part of the sys module, so you must import sys before you can use it. Each element in the list is a string, and each string corresponds to a command-line argument. The first element of the list, sys.argv[0], is the name of the script being executed. It can be the script name alone or the full path to the script, depending on how you run it.

sys.argv[1] to sys.argv[n] are the actual arguments passed to the script. If no additional arguments are provided, sys.argv will be a list with a single element (the script name).


How Does It Work?

Let’s say I have a Python script called py.py that requires two arguments. After saving the script as py.py, I can run it from the command line like this:

Running the script

Everything is working as expected without any errors. Now, let's break down the code step-by-step...


Coding...

As the saying goes, practice makes perfect. To demonstrate this, I'll share some simple code. I prefer to organize my code into classes for better reusability. This way, I can easily import and use specific components when needed.

Classes in the script

Before we continue, for the requirements of this scrip is to import (install it with 'pip' if there is no module to import):

  1. import sys 
  2. import os 
  3. import pprint

Modules to import

 

Controller Class as a Data Flow Manager

'class Controller' is where I put all the control logic. The controller class is designed to manage the flow of data within application. It serves as a central hub for handling input and output operations.

Controller class

Essentially, this controller class sets up a foundation for managing data flow. The args attribute will be used to store command-line arguments, which will likely be processed or utilized in other methods within the class or in related classes.


Data_Control() Method

The Data_Controll() method is the heart of data validation within my Controller class. I use 'valid variable' with boolean value to be the return value if the check is valid or not, valid = True, not valid = False.

Method to ensure all the data is valid

In essence, the Data_Controll() method acts as a quality gatekeeper, ensuring my application has the necessary input file and can write to the specified output directory. The optional create_file argument allows for demonstration purposes by creating a missing file.


Handle Command-Line Arguments class Argv(Controller)

The Argv class is designed to specifically handle command-line arguments. It inherits from the Controller class to leverage its data management capabilities.

Initialization of Argv class, it inherits from Controller class

In essence, the Argv class acts as a specialized version of the Controller class, focusing solely on managing command-line arguments and performing necessary validations. By inheriting from Controller, it reuses the validation logic defined in the parent class.

 

Handling The Argv, Write, and Reading File

This method, within the Argv class, serves two primary purposes:

First, it checks the validation status (self.valid) inherited from the Controller class. If valid, it extracts information from the self.args list and prints it.

Second, It constructs the output file path by joining the output directory (self.args[2]) and the base filename (fileName). It opens the file in append mode ('a') using a 'with' statement. It writes a debug message to the file: "[DEBUG!!!] We're making progress...\n". It reads the entire file content (fileRead.read()) and uses pprint to print it in a formatted manner.

Handle command-line arguments

This method provides valuable insights into argument processing, file I/O, and how I'm leveraging inheritance to organize my code effectively.

 

Pyhton Idiom if __name__ == '__main__':

This block of code is a common Python idiom that determines whether a script is being run directly or imported as a module. It's a way to control the execution flow of the script.

Python idiom

When a Python script is run directly, __name__ is set to '__main__'. When a Python script is imported as a module, __name__ is set to the module's name.

In essence, this block ensures that the code within it only executes when the script is run directly. It's a common practice to keep the main execution logic within this block to separate it from functions and classes that might be reused in other parts of the application.

By placing the code within this block, I am preventing it from running unintentionally when the script is imported as a module.

This code will do nothing but display [ERROR!!!] because no arguments are provided. I originally designed this script just as a small demonstration, but later I changed my mind and added more classes, inheritance, and methods.

 

End...

I think that's all I can share to you this evening. As always, thank you for visiting my blog and I hope you have a great day!