Skip to content

Commit

Permalink
feat: add support for python
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-gang committed Sep 5, 2024
1 parent 1547d03 commit 6fe3204
Show file tree
Hide file tree
Showing 15 changed files with 387 additions and 4 deletions.
1 change: 1 addition & 0 deletions compiled_starters/python/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
61 changes: 61 additions & 0 deletions compiled_starters/python/README.md
Original file line number Diff line number Diff line change
@@ -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 <module>
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`.
2 changes: 1 addition & 1 deletion compiled_starters/python/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
11 changes: 11 additions & 0 deletions compiled_starters/python/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions solutions/python/01-vi6/code/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions solutions/python/01-vi6/code/.codecrafters/run.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
1 change: 1 addition & 0 deletions solutions/python/01-vi6/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
153 changes: 153 additions & 0 deletions solutions/python/01-vi6/code/.gitignore
Original file line number Diff line number Diff line change
@@ -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/
61 changes: 61 additions & 0 deletions solutions/python/01-vi6/code/README.md
Original file line number Diff line number Diff line change
@@ -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 <module>
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`.
10 changes: 10 additions & 0 deletions solutions/python/01-vi6/code/app/main.py
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 11 additions & 0 deletions solutions/python/01-vi6/code/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions solutions/python/01-vi6/code/your_program.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
18 changes: 18 additions & 0 deletions solutions/python/01-vi6/diff/app/main.py.diff
Original file line number Diff line number Diff line change
@@ -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()
Loading

0 comments on commit 6fe3204

Please sign in to comment.