diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b3db618..18f358a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + python-version: ["3.10", "3.11", "3.12"] steps: - uses: actions/checkout@v4 @@ -49,4 +49,4 @@ jobs: run: tox -e lint - name: Check formatting with ruff - run: tox -e format-check \ No newline at end of file + run: tox -e format-check diff --git a/.gitignore b/.gitignore index 90e69c2..c2505c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,159 +1,93 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] *$py.class - -# C extensions +*$py.class +*.cover +*.egg +*.egg-info/ +*.log +*.manifest +*.manifest +*.mo +*.pot +*.py,cover +*.py[cod] +*.py[cod] +*.sage.py *.so - -# Distribution / packaging +*.so +*.spec +*.spec +*.swo +*.swp +*~ +.DS_Store .Python +.cache +.coverage +.coverage.* +.dmypy.json +.eggs/ +.env +.hypothesis/ +.hypothesis/ +.idea/ +.installed.cfg +.ipynb_checkpoints +.mypy_cache/ +.nox/ +.pdm.toml +.pybuilder/ +.pyre/ +.pytest_cache/ +.pytest_cache/ +.pytype/ +.ropeproject +.scrapy +.spyderproject +.spyproject +.tox/ +.tox/ +.venv +.vscode/ +.webassets-cache +/site +ENV/ +MANIFEST +Thumbs.db +__pycache__/ +__pycache__/ +__pypackages__/ build/ +celerybeat-schedule +celerybeat.pid +cover/ +coverage.xml +cython_debug/ +db.sqlite3 +db.sqlite3-journal develop-eggs/ dist/ +dmypy.json +docs/_build/ downloads/ eggs/ -.eggs/ +env.bak/ +env/ +htmlcov/ +htmlcov/ +instance/ +ipython_config.py lib/ lib64/ +local_settings.py +nosetests.xml parts/ +pip-delete-this-directory.txt +pip-log.txt +profile_default/ sdist/ -var/ -wheels/ share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ -cover/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -.pybuilder/ target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -# For a library or package, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: -# .python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# poetry -# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. -# This is especially recommended for binary packages to ensure reproducibility, and is more -# commonly ignored for libraries. -# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control -#poetry.lock - -# pdm -# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. -#pdm.lock -# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it -# in version control. -# https://pdm.fming.dev/#use-with-ide -.pdm.toml - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ +var/ venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - -# pytype static type analyzer -.pytype/ - -# Cython debug symbols -cython_debug/ - -# PyCharm -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be added to the global gitignore or merged into this project gitignore. For a PyCharm -# project, it is also recommended to add the following: -# .idea/ \ No newline at end of file +venv/ +wheels/ diff --git a/src/interactive_sort/main.py b/src/interactive_sort/main.py index 8f3111b..a805b28 100755 --- a/src/interactive_sort/main.py +++ b/src/interactive_sort/main.py @@ -3,6 +3,7 @@ import bisect import os import re +import sys from functools import cmp_to_key @@ -154,8 +155,12 @@ def key_func(x): def display_sorted(self): """Displays the current sorted data.""" - os.system("cls" if os.name == "nt" else "clear") - print("--- Interactive Sorted Data ---") + output_stream = sys.stderr if self.args.stderr else sys.stdout + + if not self.args.stderr: + os.system("cls" if os.name == "nt" else "clear") + + print("--- Interactive Sorted Data ---", file=output_stream) items_to_display = self.data if self.args.unique: items_to_display = sorted( @@ -165,21 +170,39 @@ def display_sorted(self): items_to_display = reversed(items_to_display) for item in items_to_display: - print(item) - print("-----------------------------") + print(item, file=output_stream) + print("-----------------------------", file=output_stream) + + def output_final_sorted(self): + """Outputs the final sorted data to stdout (used when stderr flag is set).""" + items_to_display = self.data + if self.args.unique: + items_to_display = sorted( + set(items_to_display), key=lambda x: self.data.index(x) + ) # Maintain original order of first occurrence + if self.args.reverse: + items_to_display = reversed(items_to_display) + + for item in items_to_display: + print(item, file=sys.stdout) def run(self): """Runs the interactive sort application.""" - print("Enter lines of text. Press Ctrl+D (or Ctrl+Z then Enter on Windows) to finish.") + output_stream = sys.stderr if self.args.stderr else sys.stdout + print("Enter lines of text. Press Ctrl+D (or Ctrl+Z then Enter on Windows) to finish.", file=output_stream) try: while True: line = input() self.add_item(line) self.display_sorted() except EOFError: - print("\nEnd of input.") + if self.args.stderr: + # When using stderr flag, output final sorted result to stdout + self.output_final_sorted() + else: + print("\nEnd of input.", file=output_stream) except KeyboardInterrupt: - print("\nExiting.") + print("\nExiting.", file=output_stream) def main():