HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Cross-platform performance and statistical information script

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
platformscriptcrossstatisticalperformanceandinformation

Problem

My usual disclaimer, I'm new to Python and scripting and I'm still studying the PEP8 guide, so please forgive any huge failures with respect to syntax, formatting and style. I'm open to any suggestions in regards to pretty much anything so let me have it!

That said, I've been building little games to learn so far, but decided it's time to try to create something useful that I might actually use one day and continue to build off of. Being a sysadmin I decided to try to build a cross platform (Windows and OSX) script that would run performance and system information gathering.

The version I'm about to paste is about 2 days old, and is in its infancy, but I wanted to get some feedback on a few things:

-
Are there any obvious failures in how I'm structuring it that I should fix now?

-
What are some suggestions for out of the box ways to gather the info and performance I'm getting with psutil? It's a non-standard module and I would like to make this script as standard as I can so it can just be run with a vanilla Python install. I'm considering just doing subprocess.call a lot, but figured that there has to be some stuff I'm not seeing when digging around the wild webs. Though it does look like they were considering adding it to the standard library back in October last year...

-
Is there anything that you see that would concern you to run on your own system?

I'm not sure how far I can get with this without it requiring some degree of admin rights, but that's a high priority for me, so I'll keep going until I hit a wall. I'm making a concerted effort to only call stuff that doesn't require admin rights.

As well if you think this just isn't useful at all and there are 50 different other ways to do this via python already and I'm recreating a wheel that's already much more elegant, then let me know. I've done a lot of looking around and haven't found anything that does this in particular (except psutil itself), at least at the depth and scope I would like t

Solution

Rather than having lot's of prints, you can do something like this:

print(
    "This will",
    "be printed",
    "on separate",
    "lines.",
    sep="\n"
)


I'm not a huge fan of your naming too. Some of the names, like runsys could really use some underscores, like this: run_sys.

I also don't like comments like these:

##########################################
# TRULY CROSS PLATFORM THINGS START HERE #
##########################################


While they do sometimes provide a shred of decent info, ones like these don't really serve a good purpose.

In addition, all the code under your MAIN CODE RUN comment should be encapsulated inside an if __name__ == "__main__": block. This is to ensure that it properly runs on windows. See this Stackoverflow question for more details.

I'm also noticing that you're mixing single quotes, '', and double quotes, "". I'd try to choose one or the other an be consistent.

You also are printing a blank line, or adding a newline to strings in some places. For example, in one place, you do print('Disk usage is:\n'), and in other places, you do things like print(''), or even worse, you do print('Disk usage is ', ) and use a comma. If you need to print with an extra newline on strings, just add the extra newline "\n" to the end of the string.

Finally, instead of having a string with many of the same characters, you can use string multiplication like this: print("#" * 30).

Code Snippets

print(
    "This will",
    "be printed",
    "on separate",
    "lines.",
    sep="\n"
)
##########################################
# TRULY CROSS PLATFORM THINGS START HERE #
##########################################

Context

StackExchange Code Review Q#97173, answer score: 2

Revisions (0)

No revisions yet.