Python3 solutions for codewars problems
About Codewars
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.
Difficulty
My profile on Codewars
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.
Dev Environment
- 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
Python Packages
Full list of dependencies see here.
Online Documentation
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 Report
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
Tech Issues and Problem Solving
Changing the project interpreter in the PyCharm project settings
1. In the **Settings/Preferences dialog** (Ctrl+Alt+S), select
**Project | Project Interpreter**.
2. Expand the list of the available interpreters and click the
**Show All** link.
3. Select the target interpreter. When PyCharm stops supporting
any of the outdated Python versions, the corresponding project
interpreter is marked as unsupported.
4. 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](https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html)
PyCharm - Choosing Your Testing Framework
1. Open the Settings/Preferences dialog, and under the node Tools,
click the page **Python Integrated Tools**.
2. On this page, click the **Default Test Runner** field.
3. Choose the desired test runner:
![choosing_test_runner](https://github.com/ikostan/SELENIUM_WEBDRIVER_WORKING_WITH_ELEMENTS/blob/master/testing_selenium_capabilities/img/py_choosing_test_runner.png)
For more info please see [Enable Pytest for you project](https://www.jetbrains.com/help/pycharm/pytest.html)
Setting up Python3 virtual environment on Windows machine
1. open CMD
2. navigate to project directory, for example:
```bash cd C:\Users\superadmin\Desktop\Python\CodinGame```
3. run following command:
```bash pip install virtualenv ```
4. run following command:
```bash virtualenv venv --python=python```
Setting up Python3 virtual environment on Linx (Ubuntu) machine
### How to install virtualenv
1. Install **pip** first:
```bash sudo apt-get install python3-pip```
2. Then install **virtualenv** using pip3:
```bash sudo pip3 install virtualenv```
3. Now create a virtual environment (>you can use any name instead of **venv**):
```bash virtualenv venv```
4. You can also use a Python interpreter of your choice:
```bash virtualenv -p /usr/bin/python2.7 venv```
5. Active your virtual environment:
```bash source venv/bin/activate```
6. Using fish shell:
```bash source venv/bin/activate.fish```
7. To deactivate:
```bash deactivate```
8. Create virtualenv using Python3:
```bash virtualenv -p python3 myenv```
9. Instead of using virtualenv you can use this command in Python3:
```bash python3 -m venv myenv```
[Source](https://gist.github.com/frfahim/73c0fad6350332cef7a653bcd762f08d)
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.
1. Open Terminal
2. Run: `\path\to\env\Scripts\activate`
[Source](https://pypi.org/project/virtualenv/1.8.2/)
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:
```bash
pip freeze > requirements.txt
```
If you save this in requirements.txt, you can follow this guide:
[PyCharm - Manage dependencies using requirements.txt](https://www.jetbrains.com/help/pycharm/managing-dependencies.html),
or you can:
```bash
pip install -r requirements.txt
```
[Source](https://www.idiotinside.com/2015/05/10/python-auto-generate-requirements-txt/)
error: RPC failed; curl 56 Recv failure: Connection was reset
1. Open Git Bash
2. Run: "git config --global http.postBuffer 157286400"
[Source](https://stackoverflow.com/questions/36940425/gitlab-push-failed-error)
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:
```bash
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:
1. Run tests from pytest using following arguments:
`-v --alluredir=allure-results`
2. Copy '.\allure-report\history\' folder into '.\allure-results\history\'
3. Run:
`allure generate .\allure-results\ -o .\allure-report\ --clean`
4. Following output should appear:
`Report successfully generated to .\allure-report`
5. Run:
`allure open .\allure-report\`
[Source](https://github.com/allure-framework/allure2/issues/813)
Sphinx Documentation Set Up
Step by step:
1. Create docs directory.
2. Open cmd > Go to docs directory.
3. cmd > Run: `sphinx-quickstart`. **Note:** run with default answers.
4. Go to `docs/conf.py`.
5. Uncomment following lines:
```python
import os
import sys
sys.path.insert(0, os.path.abspath('.'))
```
6. Update extensions list as following:
```python
extensions = ['sphinx.ext.todo', 'sphinx.ext.viewcode', 'sphinx.ext.autodoc']
```
7. Update template as following:
```python
html_theme = 'sphinx_rtd_theme'
```
8. Update sys.path.insert as following:
```python
sys.path.insert(0, os.path.abspath('..'))
```
9. Go to docs/index.rst > add modules, see example below:
```bash
.. toctree::
:maxdepth: 2
:caption: Contents:
modules
```
10. Open cmd > run:
```bash
sphinx-apidoc -F -o . ..
```
11. cmd > Run:
```bash
make html
```
12. Install html template:
```bash
pip install sphinx_rtd_theme
```
**More info:**
* [Video Tutorial](https://www.youtube.com/watch?v=b4iFyrLQQh4)
* [Sphinx Documentation](https://www.sphinx-doc.org/en/master/usage/quickstart.html)
* [More Info](https://stackoverflow.com/questions/13516404/sphinx-error-unknown-directive-type-automodule-or-autoclass)
Auto-Generated Python Documentation with Sphinx
Step by step:
1. Open CMD
2. Go to docs directory
3. Run: ```make clean```
4. Run: ```sphinx-apidoc -F -P -o . ..```
5. Add doc files name into relevant doc rst file
6. Run: ```make html```
[Source](https://www.youtube.com/watch?v=b4iFyrLQQh4)
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](https://github.com/readthedocs/readthedocs.org/issues/6554):
The fix is to wipe out the build environment as follows (this is taken
from [here](https://docs.readthedocs.io/en/stable/guides/wipe-environment.html)):
* 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).
[Source](https://stackoverflow.com/questions/59846065/read-the-docs-build-fails-with-cannot-import-name-packagefinder-from-pip-in)
How to use requirements.txt to install all dependencies in a python project
1. Run `pip install -r requirements.txt`
2. Run `pip freeze > requirements.txt`
[Source](https://intellipaat.com/community/31672/how-to-use-requirements-txt-to-install-all-dependencies-in-a-python-project)