Skip to content

Commit 9c4ee50

Browse files
authored
Merge branch 'master' into python-in-operator
2 parents 0280666 + 81b8fb7 commit 9c4ee50

21 files changed

+396
-0
lines changed

python-get-current-time/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Get and Use the Current Time in Python
2+
3+
The code samples and supporting materials for the [corresponding tutorial](https://realpython.com/python-get-current-time/) on Real Python.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from datetime import datetime
2+
import time
3+
4+
5+
datetime_unixtime = datetime.now().timestamp()
6+
time_unixtime = time.time()
7+
timezone_aware_unixtime = datetime.now().astimezone().timestamp()
8+
9+
print(
10+
f"""
11+
{datetime_unixtime = }
12+
{time_unixtime = }
13+
{timezone_aware_unixtime = }
14+
"""
15+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from datetime import datetime, timezone
2+
3+
"""
4+
datetime.datetime.now() is preferred over:
5+
6+
- datetime.utc.now()
7+
- datetime.today()
8+
- time.time()
9+
"""
10+
11+
now = datetime.now()
12+
13+
print(
14+
f"""
15+
{now = }
16+
{now.time() = }
17+
{now.day = }
18+
{now.hour = }
19+
{now.minute = }
20+
{now.isoformat() = }
21+
{now.weekday() = }
22+
{now.isoweekday() = }
23+
{now.isocalendar() = }
24+
25+
"""
26+
)
27+
28+
# https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
29+
print(now.strftime("%A, %B %d %Z"))
30+
31+
32+
# UTC timezone aware object
33+
34+
now = datetime.now(timezone.utc)
35+
36+
print(
37+
f"""
38+
UTC timezone aware:
39+
{now = }
40+
{now.isoformat() = }
41+
"""
42+
)
43+
44+
print(now.strftime("%A, %B %d %Z"))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import time
2+
3+
4+
print(
5+
f"""
6+
{time.time() = }
7+
{time.ctime() = }
8+
{time.gmtime() = }
9+
{time.localtime() = }
10+
"""
11+
)

subprocess/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Python `subprocess` Examples
2+
3+
Here are supporting materials for the Real Python tutorial [The `subprocess` Module: Wrapping Programs With Python](https://realpython.com/python-subprocess/).
4+
5+
Be aware that some examples are designed for particular operating systems. The `basics_unix.py` file won't work on Windows, for instance.
6+
7+
You'll find quick descriptions of what the different scripts do in the docstring of each file.

subprocess/basics_unix.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
**Only works on Linux or macOS**
3+
4+
Demonstrates basic usage of `subprocess.run()`.
5+
"""
6+
7+
import subprocess
8+
9+
subprocess.run(["sh", "-c", "ls"])
10+
11+
subprocess.run(["ls"], shell=True)

subprocess/basics_win.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
**Only works on Windows**
3+
4+
Demonstrates basic usage of `subprocess.run()`.
5+
"""
6+
7+
import subprocess
8+
9+
# https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd
10+
subprocess.run("dir", shell=True) # COMSPEC env variable
11+
subprocess.run(["cmd.exe", "/c", "dir"])
12+
13+
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pwsh?view=powershell-7.2
14+
subprocess.run(["powershell", "-Command", "ls"])
15+
subprocess.run(["python", "helloworld.py"])

subprocess/create_project.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Uses `subprocess` to creates a Python project, complete with a virtual
3+
environment and initialized Git repository.
4+
5+
Must have Git installed on the system with the `git` command available.
6+
7+
If your Python command is `python3`, change the `PYTHON_COMMAND` variable.
8+
"""
9+
10+
from argparse import ArgumentParser
11+
from pathlib import Path
12+
import subprocess
13+
14+
PYTHON_COMMAND = "python"
15+
16+
17+
def create_new_project(name):
18+
project_folder = Path.cwd().absolute() / name
19+
project_folder.mkdir()
20+
(project_folder / "README.md").touch()
21+
with open(project_folder / ".gitignore", mode="w") as f:
22+
f.write("\n".join(["venv", "__pycache__"]))
23+
commands = [
24+
[
25+
PYTHON_COMMAND,
26+
"-m",
27+
"venv",
28+
f"{project_folder}/venv",
29+
],
30+
["git", "-C", project_folder, "init"],
31+
["git", "-C", project_folder, "add", "."],
32+
["git", "-C", project_folder, "commit", "-m", "Initial commit"],
33+
]
34+
for command in commands:
35+
try:
36+
subprocess.run(command, check=True, timeout=60)
37+
except FileNotFoundError as exc:
38+
print(
39+
f"Command {command} failed because the process "
40+
f"could not be found.\n{exc}"
41+
)
42+
except subprocess.CalledProcessError as exc:
43+
print(
44+
f"Command {command} failed because the process "
45+
f"did not return a successful return code.\n{exc}"
46+
)
47+
except subprocess.TimeoutExpired as exc:
48+
print(f"Command {command} timed out.\n {exc}")
49+
50+
51+
if __name__ == "__main__":
52+
parser = ArgumentParser()
53+
parser.add_argument("project_name", type=str)
54+
args = parser.parse_args()
55+
create_new_project(args.project_name)

subprocess/custom_exit.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Raises custom exit codes:
3+
4+
```shell
5+
$ python custom_exit.py 3
6+
```
7+
8+
This will exit with error code `3`. By default, will exit with code `0`.
9+
"""
10+
11+
import sys
12+
13+
print(sys.argv)
14+
15+
try:
16+
raise SystemExit(int(sys.argv[1]))
17+
except IndexError as e:
18+
raise SystemExit(0) from e
19+
except ValueError as e:
20+
print("Argument must be an integer")
21+
raise SystemExit() from e

subprocess/error_handling.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Demonstrates error handling with `subprocess.run()`, catching common errors
3+
that `subprocess.run()` can encounter. Try changing the commands in
4+
`subprocess.run()` to raise different errors.
5+
"""
6+
7+
import subprocess
8+
9+
10+
try:
11+
subprocess.run(["python", "timer.py", "10"], timeout=5, check=True)
12+
except FileNotFoundError as exc:
13+
print(f"Process failed because the executable could not be found.\n{exc}")
14+
except subprocess.CalledProcessError as exc:
15+
print(
16+
f"Process failed because did not return a successful return code. "
17+
f"Returned {exc.returncode}\n{exc}"
18+
)
19+
except subprocess.TimeoutExpired as exc:
20+
print(f"Process timed out.\n{exc}")

0 commit comments

Comments
 (0)