From 6fe32042be4a78982ab95406c1131201ad7561f8 Mon Sep 17 00:00:00 2001 From: Ryan Gang Date: Thu, 5 Sep 2024 21:37:37 +0530 Subject: [PATCH] feat: add support for python --- compiled_starters/python/.gitattributes | 1 + compiled_starters/python/README.md | 61 +++++++ compiled_starters/python/app/main.py | 2 +- compiled_starters/python/codecrafters.yml | 11 ++ .../01-vi6/code/.codecrafters/compile.sh | 11 ++ .../python/01-vi6/code/.codecrafters/run.sh | 11 ++ solutions/python/01-vi6/code/.gitattributes | 1 + solutions/python/01-vi6/code/.gitignore | 153 ++++++++++++++++++ solutions/python/01-vi6/code/README.md | 61 +++++++ solutions/python/01-vi6/code/app/main.py | 10 ++ solutions/python/01-vi6/code/codecrafters.yml | 11 ++ solutions/python/01-vi6/code/your_program.sh | 15 ++ solutions/python/01-vi6/diff/app/main.py.diff | 18 +++ solutions/python/01-vi6/explanation.md | 18 +++ starter_templates/python/code/app/main.py | 7 +- 15 files changed, 387 insertions(+), 4 deletions(-) create mode 100644 compiled_starters/python/.gitattributes create mode 100644 compiled_starters/python/README.md create mode 100644 compiled_starters/python/codecrafters.yml create mode 100755 solutions/python/01-vi6/code/.codecrafters/compile.sh create mode 100755 solutions/python/01-vi6/code/.codecrafters/run.sh create mode 100644 solutions/python/01-vi6/code/.gitattributes create mode 100644 solutions/python/01-vi6/code/.gitignore create mode 100644 solutions/python/01-vi6/code/README.md create mode 100644 solutions/python/01-vi6/code/app/main.py create mode 100644 solutions/python/01-vi6/code/codecrafters.yml create mode 100755 solutions/python/01-vi6/code/your_program.sh create mode 100644 solutions/python/01-vi6/diff/app/main.py.diff create mode 100644 solutions/python/01-vi6/explanation.md diff --git a/compiled_starters/python/.gitattributes b/compiled_starters/python/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/compiled_starters/python/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/compiled_starters/python/README.md b/compiled_starters/python/README.md new file mode 100644 index 0000000..95573b7 --- /dev/null +++ b/compiled_starters/python/README.md @@ -0,0 +1,61 @@ +![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/kafka.png) + +This is a starting point for Python solutions to the +["Build Your Own Kafka" Challenge](https://codecrafters.io/challenges/kafka). + +In this challenge, you'll build a toy Kafka clone that's capable of accepting +and responding to APIVersions & Fetch API requests. You'll also learn about +encoding and decoding messages using the Kafka wire protocol. You'll also learn +about handling the network protocol, event loops, TCP sockets and more. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your Kafka implementation is in `app/main.py`. Study and +uncomment the relevant code, and push your changes to pass the first stage: + +```sh +git commit -am "pass 1st stage" # any msg +git push origin master +``` + +That's all! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `python (3.x)` installed locally +1. Run `./your_program.sh` to run your Kafka server, which is implemented in + `app/main.py`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. + +# Troubleshooting + +## module `socket` has no attribute `create_server` + +When running your server locally, you might see an error like this: + +``` +Traceback (most recent call last): + File "/.../python3.7/runpy.py", line 193, in _run_module_as_main + "__main__", mod_spec) + File "/.../python3.7/runpy.py", line 85, in _run_code + exec(code, run_globals) + File "/app/app/main.py", line 11, in + main() + File "/app/app/main.py", line 6, in main + s = socket.create_server(("localhost", 6379), reuse_port=True) +AttributeError: module 'socket' has no attribute 'create_server' +``` + +This is because `socket.create_server` was introduced in Python 3.8, and you +might be running an older version. + +You can fix this by installing Python 3.8 locally and using that. + +If you'd like to use a different version of Python, change the `language_pack` +value in `codecrafters.yml`. diff --git a/compiled_starters/python/app/main.py b/compiled_starters/python/app/main.py index 8761b2c..7b9c1a4 100644 --- a/compiled_starters/python/app/main.py +++ b/compiled_starters/python/app/main.py @@ -7,7 +7,7 @@ def main(): # Uncomment this to pass the first stage # - # server_socket = socket.create_server(("localhost", 6379), reuse_port=True) + # server_socket = socket.create_server(("localhost", 9092), reuse_port=True) # server_socket.accept() # wait for client diff --git a/compiled_starters/python/codecrafters.yml b/compiled_starters/python/codecrafters.yml new file mode 100644 index 0000000..ec2956e --- /dev/null +++ b/compiled_starters/python/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the Python version used to run your code +# on Codecrafters. +# +# Available versions: python-3.12 +language_pack: python-3.12 diff --git a/solutions/python/01-vi6/code/.codecrafters/compile.sh b/solutions/python/01-vi6/code/.codecrafters/compile.sh new file mode 100755 index 0000000..c6e646f --- /dev/null +++ b/solutions/python/01-vi6/code/.codecrafters/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to compile your program on CodeCrafters +# +# This runs before .codecrafters/run.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +# (This file is empty since Python programs don't use a compile step) diff --git a/solutions/python/01-vi6/code/.codecrafters/run.sh b/solutions/python/01-vi6/code/.codecrafters/run.sh new file mode 100755 index 0000000..af13576 --- /dev/null +++ b/solutions/python/01-vi6/code/.codecrafters/run.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to run your program on CodeCrafters +# +# This runs after .codecrafters/compile.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +exec python3 -m app.main "$@" diff --git a/solutions/python/01-vi6/code/.gitattributes b/solutions/python/01-vi6/code/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/solutions/python/01-vi6/code/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/solutions/python/01-vi6/code/.gitignore b/solutions/python/01-vi6/code/.gitignore new file mode 100644 index 0000000..06109db --- /dev/null +++ b/solutions/python/01-vi6/code/.gitignore @@ -0,0 +1,153 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +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/ +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/ diff --git a/solutions/python/01-vi6/code/README.md b/solutions/python/01-vi6/code/README.md new file mode 100644 index 0000000..95573b7 --- /dev/null +++ b/solutions/python/01-vi6/code/README.md @@ -0,0 +1,61 @@ +![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/kafka.png) + +This is a starting point for Python solutions to the +["Build Your Own Kafka" Challenge](https://codecrafters.io/challenges/kafka). + +In this challenge, you'll build a toy Kafka clone that's capable of accepting +and responding to APIVersions & Fetch API requests. You'll also learn about +encoding and decoding messages using the Kafka wire protocol. You'll also learn +about handling the network protocol, event loops, TCP sockets and more. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your Kafka implementation is in `app/main.py`. Study and +uncomment the relevant code, and push your changes to pass the first stage: + +```sh +git commit -am "pass 1st stage" # any msg +git push origin master +``` + +That's all! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `python (3.x)` installed locally +1. Run `./your_program.sh` to run your Kafka server, which is implemented in + `app/main.py`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. + +# Troubleshooting + +## module `socket` has no attribute `create_server` + +When running your server locally, you might see an error like this: + +``` +Traceback (most recent call last): + File "/.../python3.7/runpy.py", line 193, in _run_module_as_main + "__main__", mod_spec) + File "/.../python3.7/runpy.py", line 85, in _run_code + exec(code, run_globals) + File "/app/app/main.py", line 11, in + main() + File "/app/app/main.py", line 6, in main + s = socket.create_server(("localhost", 6379), reuse_port=True) +AttributeError: module 'socket' has no attribute 'create_server' +``` + +This is because `socket.create_server` was introduced in Python 3.8, and you +might be running an older version. + +You can fix this by installing Python 3.8 locally and using that. + +If you'd like to use a different version of Python, change the `language_pack` +value in `codecrafters.yml`. diff --git a/solutions/python/01-vi6/code/app/main.py b/solutions/python/01-vi6/code/app/main.py new file mode 100644 index 0000000..5d6bb67 --- /dev/null +++ b/solutions/python/01-vi6/code/app/main.py @@ -0,0 +1,10 @@ +import socket # noqa: F401 + + +def main(): + server_socket = socket.create_server(("localhost", 9092), reuse_port=True) + server_socket.accept() # wait for client + + +if __name__ == "__main__": + main() diff --git a/solutions/python/01-vi6/code/codecrafters.yml b/solutions/python/01-vi6/code/codecrafters.yml new file mode 100644 index 0000000..ec2956e --- /dev/null +++ b/solutions/python/01-vi6/code/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the Python version used to run your code +# on Codecrafters. +# +# Available versions: python-3.12 +language_pack: python-3.12 diff --git a/solutions/python/01-vi6/code/your_program.sh b/solutions/python/01-vi6/code/your_program.sh new file mode 100755 index 0000000..016e98e --- /dev/null +++ b/solutions/python/01-vi6/code/your_program.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# +# Use this script to run your program LOCALLY. +# +# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit early if any commands fail + +# Copied from .codecrafters/run.sh +# +# - Edit this to change how your program runs locally +# - Edit .codecrafters/run.sh to change how your program runs remotely +exec python3 -m app.main "$@" diff --git a/solutions/python/01-vi6/diff/app/main.py.diff b/solutions/python/01-vi6/diff/app/main.py.diff new file mode 100644 index 0000000..f176cb7 --- /dev/null +++ b/solutions/python/01-vi6/diff/app/main.py.diff @@ -0,0 +1,18 @@ +@@ -1,15 +1,10 @@ + import socket # noqa: F401 + + + def main(): +- # You can use print statements as follows for debugging, they'll be visible when running tests. +- print("Logs from your program will appear here!") +- +- # Uncomment this to pass the first stage +- # +- # server_socket = socket.create_server(("localhost", 9092), reuse_port=True) +- # server_socket.accept() # wait for client ++ server_socket = socket.create_server(("localhost", 9092), reuse_port=True) ++ server_socket.accept() # wait for client + + + if __name__ == "__main__": + main() diff --git a/solutions/python/01-vi6/explanation.md b/solutions/python/01-vi6/explanation.md new file mode 100644 index 0000000..54b8baf --- /dev/null +++ b/solutions/python/01-vi6/explanation.md @@ -0,0 +1,18 @@ +The entry point for your Kafka implementation is in `app/main.py`. + +Study and uncomment the relevant code: + +```python +# Uncomment this to pass the first stage + +server_socket = socket.create_server(("localhost", 9092), reuse_port=True) +server_socket.accept() # wait for client +``` + +Push your changes to pass the first stage: + +``` +git add . +git commit -m "pass 1st stage" # any msg +git push origin master +``` diff --git a/starter_templates/python/code/app/main.py b/starter_templates/python/code/app/main.py index 8761b2c..f34f389 100644 --- a/starter_templates/python/code/app/main.py +++ b/starter_templates/python/code/app/main.py @@ -2,13 +2,14 @@ def main(): - # You can use print statements as follows for debugging, they'll be visible when running tests. + # You can use print statements as follows for debugging, + # they'll be visible when running tests. print("Logs from your program will appear here!") # Uncomment this to pass the first stage # - # server_socket = socket.create_server(("localhost", 6379), reuse_port=True) - # server_socket.accept() # wait for client + # server = socket.create_server(("localhost", 9092), reuse_port=True) + # server.accept() # wait for client if __name__ == "__main__":