-
Notifications
You must be signed in to change notification settings - Fork 42
📦 NEW: get cpu process #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
get_cpu_process/cpu
Outdated
from get_cpu_process import get_pid | ||
from get_cpu_process import get_process_run_time | ||
from get_cpu_process import is_running |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to immediately import all the necessary entities in one request
from get_cpu_process import get_pid, get_process_run_time, is_running
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍
get_cpu_process/cpu
Outdated
def help(): | ||
print("") | ||
print("Simple CPU related script written by Zac the Wise utilising psutil") | ||
print("") | ||
print("Basic usage: cpu \"<process-name>\"") | ||
print("") | ||
print("Example: cpu \"amethyst\"") | ||
print("Returns 'running' or 'not running'") | ||
print("") | ||
print("") | ||
print("Advanced usage: cpu <option> <arguement>") | ||
print("") | ||
print(" --get-pid \"<process-name>\" returns the process id") | ||
print("") | ||
print(" --run-time \"<process-name>\"> returns the process running-time") | ||
print("") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use multilingual strings, then it would be possible to beautifully describe the entire text and would not have to escape quotes
However, if you use https://docs.python.org/3/library/argparse.html#example then this is not needed - it generates help text
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍. First time learning and using argparse so pls let me know if I can improve anything
get_cpu_process/get_cpu_process.py
Outdated
import psutil | ||
# https://thispointer.com/python-check-if-a-process-is-running-by-name-and-find-its-process-id-pid/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm used to doing it this way :)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SOURCE: https://thispointer.com/python-check-if-a-process-is-running-by-name-and-find-its-process-id-pid/
# pip install psutil
import psutil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍
get_cpu_process/get_cpu_process.py
Outdated
def is_running(provided_process_name: str) -> bool: | ||
""" | ||
Takes the name of a process and | ||
returns True if it is running, | ||
False if it isn't""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that docstring looks more beautiful - the text is separated from the literal
def is_running(provided_process_name: str) -> bool:
"""
Takes the name of a process and
returns True if it is running,
False if it isn't
"""
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍
get_cpu_process/get_cpu_process.py
Outdated
""" | ||
Takes the name of a process and | ||
returns the process runtime""" | ||
from datetime import datetime |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is better to import at the beginning of the file.
And for datetime
I use import datetime as DT
(DT.datetime
, DT.date
, DT.timedelta
) - a handy module shorthand
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍
In this PR I've added a directory called
get_cpu_process
It includes:
get_cpu_process.py
which contains three functions relating to getting the information on a process.cpu
a CLI interface for theget_cpu_process
functions which has 3 commands./cpu <process>
- returns True if the process is running./cpu --get-pid <process>
- returns the process id of the specified process./cpu --run-time <process>
- returns how long the specified process has been runningLet me know what you think 👍