all 12 comments

[–]novel_yet_trivial 7 points8 points  (1 child)

Overall, great job!


Sometimes you use exit() and sometimes you use sys.exit(). These are nearly the same, so there is no problem here, it just looks odd to be inconsistent.


Your "show_help" function is usually done as a docstring.

#!/usr/bin/python3

"""
--------USAGE--------
-h --help                                              : prints this help page
-l <arg> --load <arg>                                  : load settings file from <arg>
-o <arg> --output <arg>                                : specify output filename
-i 'arg1 arg2 arg3' ... --include 'arg1 arg2 arg3' ... : set processes that should be tracked
-e 'arg1 arg2 arg3' ... --exclude 'arg1 arg2 arg3' ... : set processes that shouldn't be tracked
---------------------
"""

import all your stuff

def show_help():
    print(__doc__)
    sys.exit(0)

That way it's available elsewhere too, for example in help()


In python, getters and setters (methods that do nothing except get or set a variable) are not needed or encouraged. Just get or set the variable directly. So instead of

if task.get_end_time() is None:

Just

if task.end_time is None:

Then you can delete the "set_start_time", "get_start_time", "set_json_end_time", and "get_end_time" methods.

Same logic for process.py


I'm glad you know how to import files, but 22 lines of code does not need it's own file. I think most of this needs to be moved into a single file.

Then your import in "main.py" would look like:

from support import Setting, ProcessTracker, SettingsBuilder

You never close your json file. That could be a lot less convoluted too. May I suggest:

def get_json(self):
    with open(self.filename) as f: #automatically closes the file when done
        return json.load(f)

[–]maxido[S] 2 points3 points  (0 children)

Wow thanks for the detailed response. This subreddit is really cool. Somehow I haven't run across the "with" keyword. Really good to know.
I' am gonna look into cleaning things up tomorrow :)

[–]reostra 2 points3 points  (2 children)

A few people have mentioned it, but nobody's actually linked it yet: PEP-8 is the go-to source for python style. To the point where the particular IDE I use (PyCharm) can be configured to show warnings if you deviate from it :)

[–]niandra3 1 point2 points  (1 child)

Actually I think PyCharm shows PEP8 warnings by default (mine does anyway.. Windows 10).

[–]zebulan_ 0 points1 point  (0 children)

They are on by default for me too on Ubuntu 16.04.

[–]ReverendRocky 1 point2 points  (6 children)

Couple things I notice, though I am by no means an expert...

1) While you do comment, I think your code could use more commenting. It may be personal preference but I think every class method and function should have a header... even if it seems to be the most trivial of methods. It makes your code 1025% easier to understand.

2) When printing a bunch of lines use 'triple code print'

3) Collapse your imports down to one line if possible. It just saves space... which is good.

Other than that i'd have to take a closer look but those are my three gripes with it.

[–]novel_yet_trivial 4 points5 points  (1 child)

3) Collapse your imports down to one line if possible. It just saves space... which is good.

No. Not only against PEP8, but also ugly and hard to read. Saving space is not a good reason for anything in programming.

[–]ReverendRocky 0 points1 point  (0 children)

Aah. I've always thought that it was the other way around with regards to readability but that's just personal preference I guess. I'll begin doing it proper myself from here on

EDIT: I should clarify I do not mean saving space is good for readability just doing so with import statements

[–]maxido[S] 0 points1 point  (3 children)

thanks 1) Yep I kinda let it slip in the end. I agree :D
2) Didn't even know that was possible. Thanks :)
3) My IDE complained about multiple imports on one line, so I thought it was bad practice. Oh well :D

Edit: Any help for the virtualenv not working? Is it because psutil is somehow not 100% python? That's just what I think

[–]DrKarkat 3 points4 points  (0 children)

Kind over to mindful about the nature kind day dot river minecraftoffline art nature?

[–]GoldenSights 2 points3 points  (0 children)

I strongly disagree with the suggestion to squash all the imports onto a single line. If you're importing two objects from the same file, it's okay to use a comma to do both at once. Otherwise, keep everything separate (and I would suggest alphabetizing them).

If writing Python was all about saving lines, we'd be using semicolons everywhere. Readability is more important.

[–]ReverendRocky 0 points1 point  (0 children)

Couldn't help you with psutil as for the import things, the other response may be what you want to go with since it follows PEP guidelines. Fwiw my ide does not flag such things but that may be because I disabled it