Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Commit f1ffd38

Browse files
authored
Merge pull request #4 from code-dot-org/molly/cleanup-code
Create a teardown function
2 parents 8f24084 + 349522e commit f1ffd38

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ This repository consists of packages for use in Python Lab. This can contain any
33
students or any patches we want to apply to student code. Here are the current packages:
44

55
### pythonlab_setup
6-
This package handles setup for Python Lab. Specifically, it patches libraries for use in Python Lab.
6+
This package handles setup and teardown for Python Lab. For setup, it patches libraries for use in Python Lab.
77
The current patches are:
88
- We patch `matplotlib` in order to display graphs correctly. The patch updates the `show` method to send
99
a base64 encoded string for display in Python Lab.
1010
- We patch `requests` in order to route requests through code.org's request proxy. This protects students
1111
by only allowing requests to an allow-list of urls.
1212

13-
All Python Lab programs are prefixed with `setup_pythonlab()`, the method this package exposes, which only applies
14-
the matplotlib patch for now.
13+
We run `setup_pythonlab()`, a method this package exposes, before each student run, which only applies
14+
the matplotlib patch for now. We also run `teardown_pythonlab()` after each run, which flushes stdout and
15+
changes directory to the home folder.
1516

1617
## unittest_runner
1718
This tests adds some customization to the output of unit tests, and has a function to either run validation tests

packages/pythonlab_setup/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "pythonlab_setup"
7-
version = "0.0.1"
7+
version = "0.1.0"
88
dependencies = ["matplotlib"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .setup_pythonlab import setup_pythonlab
2+
from .teardown_pythonlab import teardown_pythonlab
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
import sys
3+
4+
def teardown_pythonlab(home_folder):
5+
flush_sysout()
6+
go_home(home_folder)
7+
8+
# Ensure stdout is flushed so all of of a user's prints are visible to them.
9+
def flush_sysout():
10+
sys.stdout.flush()
11+
os.fsync(sys.stdout.fileno())
12+
13+
def go_home(home_folder):
14+
os.chdir(home_folder)

0 commit comments

Comments
 (0)