or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Python decorators, dependency management, & unit-testing
Week 18 Day 1
Part 1: Decorators
What's a decorator?
A decorator is syntactic sugar for a function that decorates another.
A decorator function takes in another function as a callback and returns a modified version of the callback function.
We can use decorators to add to or modify the behavior of regular functions.
Decorators
Let's create a decorator that could be used for timing function calls.
Decorators
Without the decorator syntax, we would have to define our function, then reassign our function to the return value from invoking the decorator function on our old function.
Decorators
Using the
@decorator_name
syntax, we can shorten this:To this:
Decorating a function definition (with the
@decorator
syntax) does the same thing as reassigning the function name to the return value of the decorator.Passing arguments through a decorator
What if I want to wrap functions that take arguments?
Passing arguments through a decorator
What if I want to wrap functions that take arguments… but I want to be flexible about what kind of arguments the function takes?
Part 2: The Import System
Python Modules
A module is code that is imported from a file or directory. A package is a collection of modules. A module/package can be:
To import code from a module, we use the
import
keyword. The import keyword will locate and initialize a module, and give you access to the specific names you have imported in the file.The
import
keywordThe Python standard library has a number of packages you can import without having to install them—they are included when you install Python (documentation).
Let's use the random package as an example (this would work the same with any package).
With aliasing
The
import
keywordYou can also import just specific functions from a package using the
from
keyword.Import Python code from a file
If I have two files at the same level, I can import one file using the filename (minus the
.py
).When I import the
other_code.py
file, all of the code in that file will run, even if I'm just importing one function.Import Python code from a subdirectory
To import code from inside a subfolder, use
import folder_name.file_name
.Quick note about
__init__.py
This file should go in any directory being imported. It will transform a plain old directory into a Python module/package.
Upon import from a module/package, its
__init__.py
file is implicitly executed, and all objects it defines are bound to the module's namespace (documentation).Why do we need
__init__.py
if we can import without it?Python 3.3+ creates an implicit namespace package if no
__init__.py
file exists for a directory. We want to avoid this most of the time!We need a
__init__.py
if we want to run the directory as a module, if we want to runpytest
on it, etc.This file can be completely empty (and often will be). It can also be the place where we initialize our applications!
JavaScript imports
Reminder: In Javascript, when we imported from other files, we used relative import statements.
The import path changes depending on what file we are in.
Python imports
In Python, absolute import statements are preferred when we are importing code from other files.
"Absolute" means that all imports are relative only to one location - the top-level file being executed.
Absolute imports are preferred because they are more explicit and straightforward.
However…
Python Imports
…that means that if I try to run a file directly, instead of from the intended entrypoint of my application, the file won't work correctly.
Python imports (takeaways)
__init__.py
file in any folder that has python code if you are going to be importing from that folder. The__init__.py
file can be completely empty (and often will be).Part 3: Dependency Management with Virtual Environments
Pip, Virtualenv, and Pipenv
.venv
folder)Pip, Virtualenv, and Pipenv
Using
pipenv
Create a virtual environment by running
pipenv install
. If there is a Pipfile present, this will install the dependencies in the Pipfile, otherwise it will create a new Pipfile along with a virtual environment.You can specify a particular version of Python to use in your virtual environment with
--python
flag.You can also pass in a path instead of a number .
Specifying a Python version (note for projects this week)
Many of the projects this week will specify a version of Python to use. If you try to use a version that you don't have installed, it will not work. Also, these projects expect you to be specifying the path instead of just a number.
If you see something like this
Run this instead:
If you aren't sure, you can check to see which version you have available with the command
pyenv versions
.Installing packages with
pipenv
Install a dependency:
Install a development-only dependency:
Uninstall a dependency:
More
pipenv
commandsActivate your virtual environment shell:
Remove a virtual environment:
Part 4: Unittest & Pytest
The
unittest
packageTo run tests with unittest:
All tests must be in a folder called
test
at the top level of the project. Thetest
folder must contain a__init__.py
Writing
unittest
testsInside a test file:
unittest
.unittest.TestCase
.Writing
unittest
testsTests are written as methods on the class.
test_
unittest.TestCase
class to make assertionsThe
pytest
packageCreate a virtual environment if you haven't yet, and install pytest.
Run tests at the command line by running
Make a directory called
test
at the top level of your project (be sure it contains a__init__.py
).Test files must be in
test
directory, and filenames must begin or end withtest
.Writing
pytest
testsDefine test functions directly—no need for classes. Function names must begin with
test
to be treated as a unit test.Use
assert
keyword, followed by the conditional you are trying to test.You can run
unittest
tests withpytest
, but not vice versa.Today's project
Test Driven Development (TDD) with Python