Python3 solutions for codewars problems
Codewars is a collective effort by its users. They are creators - authoring kata to teach various techniques, solving kata with solutions that enlighten others, and commenting with constructive feedback. The leaders among them moderate the content and community.
8 - 7 kyu │ Beginner
6 - 5 kyu │ Novice
4 - 3 kyu │ Competent
2 - 1 kyu │ Proficient
1 - 5 dan │ Expert
5 - 8 dan │ Master
Note: For each completed kata, there is a corresponding unittest file.
- Python 3.12.3
- PyTest
- Allure Framework
- Win 10 (64 bit)
- PyCharm 2024.1.1 (Community Edition)
- GitHub Desktop
- GIT 2.39.1.windows.1
Full list of dependencies see here.
Python3 solutions for codewars problems’s documentation is built with Sphinx using a theme provided by Read the Docs.
Sphinx is a tool that makes it easy to create intelligent and beautiful documentation, written by Georg Brandl and licensed under the BSD license.
Online version of the latest tech documentation
Allure is based on standard xUnit results output but adds some supplementary data. Any report is generated in two steps. During test execution (first step), a small library called adapter attached to the testing framework saves information about executed tests to XML files.
During report generation (second step), the XML files are transformed to a HTML report. This can be done with a command line tool, a plugin for CI or a build tool.
Online version of the latest Allure report
Changing the project interpreter in the PyCharm project settings
-
In the Settings/Preferences dialog (Ctrl+Alt+S), select Project | Project Interpreter.
-
Expand the list of the available interpreters and click the Show All link.
-
Select the target interpreter. When PyCharm stops supporting any of the outdated Python versions, the corresponding project interpreter is marked as unsupported.
-
The Python interpreter name specified in the Name field, becomes visible in the list of available interpreters. Click OK to apply the changes.
For more info please check here
PyCharm - Choosing Your Testing Framework
-
Open the Settings/Preferences dialog, and under the node Tools, click the page Python Integrated Tools.
-
On this page, click the Default Test Runner field.
-
Choose the desired test runner:
For more info please see Enable Pytest for you project
Setting up Python3 virtual environment on Windows machine
- open CMD
- navigate to project directory, for example:
bash cd C:\Users\superadmin\Desktop\Python\CodinGame
- run following command:
bash pip install virtualenv
- run following command:
bash virtualenv venv --python=python
Setting up Python3 virtual environment on Linx (Ubuntu) machine
- Install pip first:
bash sudo apt-get install python3-pip
- Then install virtualenv using pip3:
bash sudo pip3 install virtualenv
- Now create a virtual environment (>you can use any name instead of venv):
bash virtualenv venv
- You can also use a Python interpreter of your choice:
bash virtualenv -p /usr/bin/python2.7 venv
- Active your virtual environment:
bash source venv/bin/activate
- Using fish shell:
bash source venv/bin/activate.fish
- To deactivate:
bash deactivate
- Create virtualenv using Python3:
bash virtualenv -p python3 myenv
- Instead of using virtualenv you can use this command in Python3:
bash python3 -m venv myenv
Activate Virtual Environment
In a newly created virtualenv there will be a bin/activate shell script. For Windows systems, activation scripts are provided for CMD.exe and Powershell.
- Open Terminal
- Run:
\path\to\env\Scripts\activate
Auto generate requirements.txt
Any application typically has a set of dependencies that are required for that application to work. The requirements file is a way to specify and install specific set of package dependencies at once.
Use pip’s freeze command to generate a requirements.txt file for your project:
pip freeze > requirements.txt
If you save this in requirements.txt, you can follow this guide: PyCharm - Manage dependencies using requirements.txt, or you can:
pip install -r requirements.txt
error: RPC failed; curl 56 Recv failure: Connection was reset
- Open Git Bash
- Run: "git config --global http.postBuffer 157286400"
How to fix in case .gitignore is ignored by Git
Even if you haven't tracked the files so far, Git seems to be able to "know" about them even after you add them to .gitignore
NOTE:
- First commit your current changes, or you will lose them.
- Then run the following commands from the top folder of your Git repository:
git rm -r --cached .
git add .
git commit -m "fixed untracked files"
How to generate Allure report with history trends (Windows OS)
Step by step:
- Run tests from pytest using following arguments:
-v --alluredir=allure-results
- Copy '.\allure-report\history' folder into '.\allure-results\history'
- Run:
allure generate .\allure-results\ -o .\allure-report\ --clean
- Following output should appear:
Report successfully generated to .\allure-report
- Run:
allure open .\allure-report\
Sphinx Documentation Set Up
Step by step:
- Create docs directory.
- Open cmd > Go to docs directory.
- cmd > Run:
sphinx-quickstart
. Note: run with default answers. - Go to
docs/conf.py
. - Uncomment following lines:
import os
import sys
sys.path.insert(0, os.path.abspath('.'))
- Update extensions list as following:
extensions = ['sphinx.ext.todo', 'sphinx.ext.viewcode', 'sphinx.ext.autodoc']
- Update template as following:
html_theme = 'sphinx_rtd_theme'
- Update sys.path.insert as following:
sys.path.insert(0, os.path.abspath('..'))
- Go to docs/index.rst > add modules, see example below:
.. toctree::
:maxdepth: 2
:caption: Contents:
modules
- Open cmd > run:
sphinx-apidoc -F -o . ..
- cmd > Run:
make html
- Install html template:
pip install sphinx_rtd_theme
More info:
Auto-Generated Python Documentation with Sphinx
Step by step:
- Open CMD
- Go to docs directory
- Run:
make clean
- Run:
sphinx-apidoc -F -P -o . ..
- Add doc files name into relevant doc rst file
- Run:
make html
Read-the-docs build fails with “cannot import name 'PackageFinder' from 'pip._internal.index'
The issue and the fix are described in read-the-docs issue #6554:
The fix is to wipe out the build environment as follows (this is taken from here):
- Log in to read-the-docs
- Go to Versions
- Click on the Edit button of the version you want to wipe on the right side of the page
- Go to the bottom of the page and click the wipe link, next to the “Save” button
- Now you can re-build the version with a fresh build environment!
This fix worked for me (but as of 26-Jan-2020 you have to wipe out the environment for every build, see comment from Grimmy below).
How to use requirements.txt to install all dependencies in a python project
- Run
pip install -r requirements.txt
- Run
pip freeze > requirements.txt