Python is my favorite language because it’s simple and fast. It has been 3 years since first time I learn python, I’ve wrote a lot of article about python inital in my first year coiding python, feel free to read it if you understand chinese well.
this article will focus on engineering practice of python and include an example about how to build a tool with python.
I believe that when you really embrace a new language, you have to spend a lot of time on tools and language ecology. The ecological community built by the huge community using group wisdom is full of all kinds of knowledge points. These knowledge points may be the traps of the previous people (Common Gotchas), based on the local consensus after the practice of the classic project. Idioms may also be a summary of the conceptual patterns, even aesthetics and Zen or Dao. These knowledge points work in the tool and language ecosystem, meaning you need to use the right tools and ecologically friendly gameplay to benefit from it.
There is no best tool in the world, just choice one you most familiar with.
If you don’t want to spend too much time on preperation, I suggest you use Pycharm. It’s out of box and has perfect function.
If you like to like to play with tools or Linux, like me, an editor may be your best choice. Python interpreter is not complex to setup, while you doing this, you may can have better understanding of python environment.
In my first year coding pyhon, I used Sublime Text3, it’s an old editor have very nice package management system, I have a sample configuration which is very old (for python 3.4 ) you can download and unarchive to your sublime user setting folder, then you can simply use do something like
F2-go to define,
F5-Run . For what I knoe after VS code become popular, most plugin are update so solow now.
I’m now using vim more and more, because you don’t have to install anything you can use it in almostly any kind of system.
For Viw if you just want use it as an editor to write some scripts, I think some text style configure would be enough for most people:
set textwidth=79 " lines longer than 79 columns will be broken
If you want a more smart editor with auto complete function, go to definition, refator function, I suggest you use a package for that: Pymode, and another plugin which is very popular for add directory named nerdtree, I use vim-plug to manage then , just run single command and put plugin you want you install in ~/.vimrc, you’ll get your python environment now.
Command to install vim-plug:
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
Because Python is very simple to install, let’s skip… However, heroes stay, you know that Python is actually just a language standard, it has more than one implementation, the official implementation is CPython, and Jython and IronPython. However, CPython’s use as the most widely used interpreter is certainly the first choice for development.
Repl-Oriented Programming is a more easy way to get fast feedback, I nromally use
ipython for that.
OSX default python version is python2.7, But I prefer use python3+ which are more Object-Oriented rather than script. If you like to use different version but don’t want to mess your environment just like me I suggest use pyenv to management your python version.
My aim is create a command line tool like
tree via python, let’s call it
After 3.3 version of python, there is a module named venv for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories. After 3.4, it also provide an independent pip via venv.
The way to use is:
python -m venv /path/to/new/virtual/environment # Create new virtual env
Now let’s start with create project directory:
You can entry python interactive mode and import pytest for verify your environment is ready.
Refer to Packaging Python Projects，create project directories and files like this:
You can distrubute your package via setuptool if you want:
pip install setuptools wheel twine
For writting test with pytest, the class has to start with
Test, and function has to start with
test_, we can also use setup.py to run pytest:
python setup.py pytest , setup tool will put
PATH. We cab also add an alias to pytest in setup.cfg：
then we can use
python setup.py test to run all the tests.
Use the TDD approach to implement the pytree core functionality(
core.py) and then consider how to turn it into a real command line program. The first problem to be solved is how to display which incoming parameters are needed in a user-friendly way. We expect pytree-h to provide some help information. In order not to re-create the wheel, it is easier to select the ready-made Option parsing library. Python’s built-in argparse is enough, but docopt is worth trying.
add dependence in setup.py:
python setup.py develop to install dependency.
then continues coding
Use pyinstaller to build an excutable commandline file(cross platform):
pyinstaller -F entry.py --clean
After push your code to github, we want to see the feedback for code quality and functional, so we always like to run all tests on a central server, github can integrate with travis CI do so.
Copy Images status to README.md
Now Source code is here: https://github.com/ADU-21/pytree
Some resources to learn: