SteamPy breakdown : Part 1

python, steam

In this post I am going to write about SteamPy, an SCM investment profit tracker for CS:GO items. Well, for now it can track only CS:GO items. I might add support for other games later.

If you want to know what does it do you should go through the documentation. Here I will be describing the ‘how does it do’ part only.

So, there are four files that run the program: main.py, get_price.py, operation.py and our beloved investment.ini file which just act as a config file.

This program is so small it could have been done within a single python file but I did use multiple files to better understand the concepts like importing, modulesm packages etc.

main.py file breakdown:

In the main.py file we have the import section at the top which holds all the necessary modules and packages.

from operation import *
import os
import configparser
import argparse

For the simplicity of understanding I will go to the main() function first and then we will go through the other functions as they are needed during the lifetime of our program.

So, in the main() function we have:

def main():
    option = get_arguments()
    operations(option)

Here, we are calling get_arguments() function to understand what arguments the user has given us. If we look into the function get_arguments() we can see we have two flags -s or --short and -l or --long for two different types of functionality. ArgumentParser() can store command line input string as objects and this information can be used by calling parse_args().

def get_arguments():
    parser = argparse.ArgumentParser(description="A python script to track your SCM investment profits easily.To make this programm give an efficient output, be as accurate as you can when filling up the investment.ini file. It is recommended that you put the name exactly as they are on SCM.",
    epilog="N.B. profit/loss is calculated after deducting the 15% steam tax.")
    parser.add_argument("-s", "--short",action='store_true', help="generate a short report: name and net profit/loss")
    parser.add_argument("-l","--long",action='store_true', help="generate a full detailed report of your investments")
    return parser.parse_args()

To get into more details about parsing arguments in python check out this.

After the user choice is received we pass it to the function operation() as a parameter. Now lets look at operation().

def operations(choice):
    if choice.long and choice.short:
        print("Use either -s or -l to generate a report, not both.")
    elif choice.long:
        details()
    elif choice.short:
        shortlist()
    else:
        print("Please use -s or -l with the command to generate a report. For more help, use -h")

Here, the code is a simple if else block which is checking the validity of the received argument. If it is not valid, the program shows some help texts, otherwise for option -l or --long, it calls details() and for -s or --short, it calls the function shortlist().

With this, breakdown of our main.py ends here.

Click here to continue to the next part.