Skip to content

Commit 7c98c75

Browse files
committed
uploading draft examples
1 parent 49e7676 commit 7c98c75

14 files changed

+210
-0
lines changed

subprocess/basics_unix.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import subprocess
2+
3+
# LINUX examples
4+
5+
subprocess.run("ls")
6+
subprocess.run("bash ls")
7+
subprocess.run(["bash", "ls"])
8+
subprocess.run(["sh", "ls"])
9+
subprocess.run(["zsh", "ls"])
10+
subprocess.run(["ls"])
11+
12+
subprocess.run("python helloworld.py")

subprocess/basics_win.py

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

subprocess/choice_react.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from time import sleep, time
2+
from random import random
3+
from string import ascii_lowercase
4+
5+
6+
print(
7+
"A letter will appear on screen after a random amount of time,\n"
8+
+ "when it appears, type the letter as fast as possible and then press enter\n"
9+
)
10+
print("Press enter when you are ready")
11+
input()
12+
print("Ok, get ready!")
13+
sleep(random() * 5 + 2)
14+
target_letter = ascii_lowercase[int(random() * 26)]
15+
print(f"=====\n= {target_letter} =\n=====\n")
16+
17+
start = time()
18+
while True:
19+
if input() == target_letter:
20+
break
21+
else:
22+
print("Nope! Try again.")
23+
end = time()
24+
25+
print(f"You reacted in {end - start} seconds!\nGoodbye!")

subprocess/create_new_project.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import subprocess
2+
import os
3+
from pathlib import Path, PurePath
4+
import venv
5+
6+
7+
def create_new_project(name):
8+
9+
project_folder = PurePath.joinpath(Path.cwd(), name)
10+
project_folder.mkdir()
11+
os.chdir(
12+
project_folder
13+
) # Should change to call git with -C flag which would mean that this line would not be needed
14+
project_folder.joinpath("README.md").touch()
15+
project_folder.joinpath(".gitignore").touch()
16+
with open(".gitignore", mode="w") as f:
17+
f.write("\n".join(["venv", "__pycache__"]))
18+
venv.create("venv", system_site_packages=True, with_pip=True)
19+
subprocess.run(["git", "init"])
20+
subprocess.run(["git", "add", "."])
21+
subprocess.run(["git", "commit", "-m", "first commit"])
22+
23+
# Activating venv? Would need access to parent process of Python?
24+
25+
26+
if __name__ == "__main__":
27+
create_new_project("test")

subprocess/exiter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import sys
2+
3+
sys.exit(1)

subprocess/random_num_gen.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from random import random
2+
3+
print(int(random() * 1000))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import subprocess
2+
3+
print(subprocess.check_output(["python", "random_num_gen.py"]).strip())

subprocess/shlex_example.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import shlex
2+
3+
print(shlex.split("git commit -m 'first commit'"))

subprocess/simple_react.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from time import sleep, time
2+
from random import random
3+
4+
print("Press enter to play")
5+
input()
6+
print("Ok, get ready!")
7+
sleep(random() * 5 + 1)
8+
print("go!")
9+
start = time()
10+
input()
11+
end = time()
12+
print(f"You reacted in {end - start} seconds!\nGoodbye!")

subprocess/simple_react_perf.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from time import sleep, perf_counter
2+
from random import random
3+
4+
print("Press enter to play")
5+
input()
6+
print("Ok, get ready!")
7+
sleep(random() * 5 + 1)
8+
print("go!")
9+
start = perf_counter()
10+
input()
11+
end = perf_counter()
12+
print(f"You reacted in {end - start} seconds!\nGoodbye!")

0 commit comments

Comments
 (0)