python programming

3 minute read

**As you know I like to mess with programming, in this article I describe how I deal with new concepts and how I tend to work. **

The Introduction with Python

I really am not that handy with graphic programming, however I have done some work in C earlier and Python did stay the one language (aside of BASIC) that I keep falling back to out of ease, I have tried Golang before as well, but it didn’t stick. If I start writing a new project in Python I tend to do it as follows:



#!/usr/bin/env python3


def main():
    print("Hello, World!")
    
if __name__ == "__main__":
    main()


Which is just a Hello World program in Python.

How I deal with new concepts, and an example

For new concepts for projects, I first look at the docs then I decide what I want to do. Currently it’s something with networks, as recently I have written a script that did uname in python on my local machine, which ended up being the following code (which is a tad messy, but I will explain it):



#!/usr/bin/env python3
import platform
import array
import json


def main():

#    print("Hello, world!")

    os_name = platform.uname()
    array_uname_1 = ["System", "Node", "Release", "Version", "Machine",\
                    "Processor"]
    array_uname_2 = []
    
    for i in os_name:
        array_uname_2.append(i)
        
#    print(array_uname_1 + array_uname_2)

    dictionary_uname = dict(zip(array_uname_1,array_uname_2))
#    print(dictionary_uname)

    json_uname = json.dumps(dictionary_uname)
#    print(json_uname)

    with open('uname.json', 'a+') as file:
        file.write(f'{json_uname}')


if __name__ == "__main__":
    main()


Explaining the example

We start with the Shebang that tells the commandline interpreter what to use when we changed the mode of the file to also be executable with chomod +x test.py. The shebang is the following #!/usr/bin/env python3. Then we import packages necessary to be used. After which the main function main() gets defined. In this we have directly a commented out print statement, as made clear by the #. Then a variable (os_name) which calls platform.uname() is used. Then I made an array with all the parts of platform.uname(), since it didn’t fit 80 characters wide I used a \ with a line return and then continued on the next line, after that I defined a second empty array for the platform.uname() content, which is stored in os_name.

Then I loop over os_name and add it to array_uname_2 Then there’s a commented out debug print statement, and then I add it to a dictionary named dictionary_uname, after which a commented out print statement appears. Then I make it json data using json.dumps(dictionary_uname) and assign that to a variable named json_uname. Then I create or append a file for the time it needs to write using with open('uname.json', 'a+') as file: where a+ means append, or if the file json.uname doesn’t exist, create it, and assign this to a variable called file for the time you need to write. Then it writes to the file uname.json using a feature called format strings. This is the following part: file.write(f'{json_uname}') in format strings you can directly refer to variables.

Lastly we have the lines necessary to call the main function, namely:


if __name__ == "__main__":
    main()

I’m not sure why it’s necessary but it is, so I use it, haha.

The file is afterwards written as is visible below


the json file is written after the execution of the script.


The last image I want to share is the output of the script I’m currently working on:


Ip address validation on the terminal


I hope you enjoyed my journey into Python as much as I did, until next time.

Updated: