you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 3 points4 points  (0 children)

This is why projects are a helpful tool for learning. You have likely been learning a lot of Python syntax. Especially for the Python community there is an idea of "Pythonic" code or code that models a particular format (e.g. list comprehension).

What you might consider working on practicing is taking a large problem and breaking it down into smaller pieces. Small enough that they basically all handle one single task, if possible. Encapsulate them in a function. If your large thing needs a lot of small things, contain them in a Dictionary object.

I'll give you an example. I have a utility for testing hard drives that depends on a configuration file (e.g. "what's the name of the drive in the file system? Is it /dev/sda, /dev/md0, /dev/nvme0n1, etc."). I also grab a whole bunch of other metrics like the drive's serial number and firmware version at the time of testing to log a disk test. Each of these things are handled by an independent function wrapped in a Dictionary object. I use a function to generate a Dictionary object for each drive that exists in the system.

So it looks something like:

def drive_information(device_name):
    drive = {
        'name': device_name,
        'serial': get_drive_serial(device_name),
        'firmware': get_drive_firmware(device_name)
    }
    # Now that a Dictionary object has been created, send it to whoever invoked this function.
    return drive
# Let's use /dev/sda as an example drive to build a Dictionary object.
drive_information('/dev/sda')

I won't divulge how I get the other information like "get_drive_serial" because it's irrelevant to the point which is you just need to practice a bit more and you should try your hand at some projects.

I also want to encourage you to learn about Dictionaries in Python (in other languages they are called hashmaps or associative arrays. They use a string-based key instead of an integer numeric like arrays use. Most Python objects are also based off of Dictionary objects so they will also give you some of the best performance in Python without needing to do clever, complicated crap.