Skip to content

Commit ca60982

Browse files
authored
Merge pull request #2 from MightyHelper/copilot/fix-1
Implement STDERR output flag for interactive sorting
2 parents eb69bb8 + 8b5997c commit ca60982

3 files changed

Lines changed: 110 additions & 153 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
14+
python-version: ["3.10", "3.11", "3.12"]
1515

1616
steps:
1717
- uses: actions/checkout@v4
@@ -49,4 +49,4 @@ jobs:
4949
run: tox -e lint
5050

5151
- name: Check formatting with ruff
52-
run: tox -e format-check
52+
run: tox -e format-check

.gitignore

Lines changed: 78 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,93 @@
1-
# Byte-compiled / optimized / DLL files
2-
__pycache__/
3-
*.py[cod]
41
*$py.class
5-
6-
# C extensions
2+
*$py.class
3+
*.cover
4+
*.egg
5+
*.egg-info/
6+
*.log
7+
*.manifest
8+
*.manifest
9+
*.mo
10+
*.pot
11+
*.py,cover
12+
*.py[cod]
13+
*.py[cod]
14+
*.sage.py
715
*.so
8-
9-
# Distribution / packaging
16+
*.so
17+
*.spec
18+
*.spec
19+
*.swo
20+
*.swp
21+
*~
22+
.DS_Store
1023
.Python
24+
.cache
25+
.coverage
26+
.coverage.*
27+
.dmypy.json
28+
.eggs/
29+
.env
30+
.hypothesis/
31+
.hypothesis/
32+
.idea/
33+
.installed.cfg
34+
.ipynb_checkpoints
35+
.mypy_cache/
36+
.nox/
37+
.pdm.toml
38+
.pybuilder/
39+
.pyre/
40+
.pytest_cache/
41+
.pytest_cache/
42+
.pytype/
43+
.ropeproject
44+
.scrapy
45+
.spyderproject
46+
.spyproject
47+
.tox/
48+
.tox/
49+
.venv
50+
.vscode/
51+
.webassets-cache
52+
/site
53+
ENV/
54+
MANIFEST
55+
Thumbs.db
56+
__pycache__/
57+
__pycache__/
58+
__pypackages__/
1159
build/
60+
celerybeat-schedule
61+
celerybeat.pid
62+
cover/
63+
coverage.xml
64+
cython_debug/
65+
db.sqlite3
66+
db.sqlite3-journal
1267
develop-eggs/
1368
dist/
69+
dmypy.json
70+
docs/_build/
1471
downloads/
1572
eggs/
16-
.eggs/
73+
env.bak/
74+
env/
75+
htmlcov/
76+
htmlcov/
77+
instance/
78+
ipython_config.py
1779
lib/
1880
lib64/
81+
local_settings.py
82+
nosetests.xml
1983
parts/
84+
pip-delete-this-directory.txt
85+
pip-log.txt
86+
profile_default/
2087
sdist/
21-
var/
22-
wheels/
2388
share/python-wheels/
24-
*.egg-info/
25-
.installed.cfg
26-
*.egg
27-
MANIFEST
28-
29-
# PyInstaller
30-
# Usually these files are written by a python script from a template
31-
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32-
*.manifest
33-
*.spec
34-
35-
# Installer logs
36-
pip-log.txt
37-
pip-delete-this-directory.txt
38-
39-
# Unit test / coverage reports
40-
htmlcov/
41-
.tox/
42-
.nox/
43-
.coverage
44-
.coverage.*
45-
.cache
46-
nosetests.xml
47-
coverage.xml
48-
*.cover
49-
*.py,cover
50-
.hypothesis/
51-
.pytest_cache/
52-
cover/
53-
54-
# Translations
55-
*.mo
56-
*.pot
57-
58-
# Django stuff:
59-
*.log
60-
local_settings.py
61-
db.sqlite3
62-
db.sqlite3-journal
63-
64-
# Flask stuff:
65-
instance/
66-
.webassets-cache
67-
68-
# Scrapy stuff:
69-
.scrapy
70-
71-
# Sphinx documentation
72-
docs/_build/
73-
74-
# PyBuilder
75-
.pybuilder/
7689
target/
77-
78-
# Jupyter Notebook
79-
.ipynb_checkpoints
80-
81-
# IPython
82-
profile_default/
83-
ipython_config.py
84-
85-
# pyenv
86-
# For a library or package, you might want to ignore these files since the code is
87-
# intended to run in multiple environments; otherwise, check them in:
88-
# .python-version
89-
90-
# pipenv
91-
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92-
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93-
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94-
# install all needed dependencies.
95-
#Pipfile.lock
96-
97-
# poetry
98-
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99-
# This is especially recommended for binary packages to ensure reproducibility, and is more
100-
# commonly ignored for libraries.
101-
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102-
#poetry.lock
103-
104-
# pdm
105-
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106-
#pdm.lock
107-
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108-
# in version control.
109-
# https://pdm.fming.dev/#use-with-ide
110-
.pdm.toml
111-
112-
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113-
__pypackages__/
114-
115-
# Celery stuff
116-
celerybeat-schedule
117-
celerybeat.pid
118-
119-
# SageMath parsed files
120-
*.sage.py
121-
122-
# Environments
123-
.env
124-
.venv
125-
env/
126-
venv/
127-
ENV/
128-
env.bak/
90+
var/
12991
venv.bak/
130-
131-
# Spyder project settings
132-
.spyderproject
133-
.spyproject
134-
135-
# Rope project settings
136-
.ropeproject
137-
138-
# mkdocs documentation
139-
/site
140-
141-
# mypy
142-
.mypy_cache/
143-
.dmypy.json
144-
dmypy.json
145-
146-
# Pyre type checker
147-
.pyre/
148-
149-
# pytype static type analyzer
150-
.pytype/
151-
152-
# Cython debug symbols
153-
cython_debug/
154-
155-
# PyCharm
156-
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157-
# be added to the global gitignore or merged into this project gitignore. For a PyCharm
158-
# project, it is also recommended to add the following:
159-
# .idea/
92+
venv/
93+
wheels/

src/interactive_sort/main.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import bisect
44
import os
55
import re
6+
import sys
67
from functools import cmp_to_key
78

89

@@ -154,8 +155,12 @@ def key_func(x):
154155

155156
def display_sorted(self):
156157
"""Displays the current sorted data."""
157-
os.system("cls" if os.name == "nt" else "clear")
158-
print("--- Interactive Sorted Data ---")
158+
output_stream = sys.stderr if self.args.stderr else sys.stdout
159+
160+
if not self.args.stderr:
161+
os.system("cls" if os.name == "nt" else "clear")
162+
163+
print("--- Interactive Sorted Data ---", file=output_stream)
159164
items_to_display = self.data
160165
if self.args.unique:
161166
items_to_display = sorted(
@@ -165,21 +170,39 @@ def display_sorted(self):
165170
items_to_display = reversed(items_to_display)
166171

167172
for item in items_to_display:
168-
print(item)
169-
print("-----------------------------")
173+
print(item, file=output_stream)
174+
print("-----------------------------", file=output_stream)
175+
176+
def output_final_sorted(self):
177+
"""Outputs the final sorted data to stdout (used when stderr flag is set)."""
178+
items_to_display = self.data
179+
if self.args.unique:
180+
items_to_display = sorted(
181+
set(items_to_display), key=lambda x: self.data.index(x)
182+
) # Maintain original order of first occurrence
183+
if self.args.reverse:
184+
items_to_display = reversed(items_to_display)
185+
186+
for item in items_to_display:
187+
print(item, file=sys.stdout)
170188

171189
def run(self):
172190
"""Runs the interactive sort application."""
173-
print("Enter lines of text. Press Ctrl+D (or Ctrl+Z then Enter on Windows) to finish.")
191+
output_stream = sys.stderr if self.args.stderr else sys.stdout
192+
print("Enter lines of text. Press Ctrl+D (or Ctrl+Z then Enter on Windows) to finish.", file=output_stream)
174193
try:
175194
while True:
176195
line = input()
177196
self.add_item(line)
178197
self.display_sorted()
179198
except EOFError:
180-
print("\nEnd of input.")
199+
if self.args.stderr:
200+
# When using stderr flag, output final sorted result to stdout
201+
self.output_final_sorted()
202+
else:
203+
print("\nEnd of input.", file=output_stream)
181204
except KeyboardInterrupt:
182-
print("\nExiting.")
205+
print("\nExiting.", file=output_stream)
183206

184207

185208
def main():

0 commit comments

Comments
 (0)