... | ... | @@ -155,20 +155,20 @@ Other rules come from the community... |
|
|
|
|
|
1. Always have a `if __name__ == "__main__"`.
|
|
|
2. Use f-strings over format where possible.
|
|
|
3. Use `with` closures
|
|
|
3. Use `with` closures.
|
|
|
4. Write pydoc style documentation.
|
|
|
5. Use functions and modules.
|
|
|
6. No global variables.
|
|
|
6. Avoid global variables.
|
|
|
7. Do not always use object-oriented design.
|
|
|
8. Do not forget about the [Python GIL](https://wiki.python.org/moin/GlobalInterpreterLock)
|
|
|
8. Do not forget about the [Python GIL](https://wiki.python.org/moin/GlobalInterpreterLock).
|
|
|
|
|
|
Other rules come from general software engineering practices:
|
|
|
|
|
|
1. Follow Test Driven Development (TDD)
|
|
|
2. Use top down design
|
|
|
3. Do not write monolithic functions
|
|
|
4. Use a code linter or style checker (e.g., pylint)
|
|
|
5. Use self-dcoumenting name
|
|
|
1. Follow Test Driven Development (TDD).
|
|
|
2. Use top down design.
|
|
|
3. Do not write monolithic functions.
|
|
|
4. Use a code linter or style checker (e.g., pylint).
|
|
|
5. Use self-documenting names.
|
|
|
6. Remember *S.O.L.I.D*.
|
|
|
7. Iterators are magic *(CS 330)*.
|
|
|
|
... | ... | @@ -176,7 +176,7 @@ Other rules come from general software engineering practices: |
|
|
# Data Structures
|
|
|
|
|
|
When I work in Python, I generally focus on three core (fundamental) data
|
|
|
structures.
|
|
|
structures (or specialized variations from the `collections` module).
|
|
|
|
|
|
- Lists: `prime_numbers = [1, 2, 3, 5, 7, 11, 13, 17, 19]`
|
|
|
- Dictionaries: `favourite_colors = {"Thomas": "Blue", "Jessica": "Purple"}`
|
... | ... | @@ -216,7 +216,6 @@ approach: |
|
|
> using std::string;
|
|
|
> using std::vector;
|
|
|
>
|
|
|
>
|
|
|
> int main(int argc, char** argv)
|
|
|
> {
|
|
|
> vector<string> some_terms {"Hello", "world", "with", "for", "while", "int"};
|
... | ... | @@ -248,7 +247,6 @@ and translate it into Python: |
|
|
> main()
|
|
|
> ```
|
|
|
|
|
|
|
|
|
The Python version can (and should) use a list comprehension.
|
|
|
|
|
|
> **Word Count - Fun Python Loop**
|
... | ... | @@ -264,7 +262,6 @@ The Python version can (and should) use a list comprehension. |
|
|
> main()
|
|
|
> ```
|
|
|
|
|
|
|
|
|
Depending on how many terms we have... a generator expression might be more
|
|
|
appropriate:
|
|
|
|
... | ... | @@ -313,7 +310,6 @@ to Python comprehensions and C++ `std::transform`. However, in Java, we would |
|
|
end up dealing with the `Integer` wrapper class if we wanted to use a non-array
|
|
|
data structure.
|
|
|
|
|
|
|
|
|
> **Word Count - Java Streams**
|
|
|
>
|
|
|
> ```java
|
... | ... | @@ -334,7 +330,6 @@ data structure. |
|
|
> }
|
|
|
> ```
|
|
|
|
|
|
|
|
|
**The Python implementation is the most succinct, approachable, and readable.**
|
|
|
|
|
|
|
... | ... | @@ -383,6 +378,7 @@ This also works for other types of files--including compressed files. |
|
|
> text_file.write(f"{number}\n")
|
|
|
> ```
|
|
|
|
|
|
|
|
|
# Python Includes Batteries
|
|
|
|
|
|
For many languages external libraries are usually required for *common
|
... | ... | @@ -390,8 +386,8 @@ operations*. [Python includes batteries](https://www.python.org/dev/peps/pep-020 |
|
|
|
|
|
| Operation | Built-in Python Module |
|
|
|
| :---------------------------------------- | :------------------ |
|
|
|
| Zip Files | `import zipfile` |
|
|
|
| GZipped Files | `import gzip` |
|
|
|
| Working with zip files | `import zipfile` |
|
|
|
| Working with gzipped files | `import gzip` |
|
|
|
| Reading, writing, or generating JSON | `import json` |
|
|
|
| Converting objects to JSON | `import json` |
|
|
|
| Serializing objects and data structures | `import pickle` |
|
... | ... | @@ -400,16 +396,17 @@ operations*. [Python includes batteries](https://www.python.org/dev/peps/pep-020 |
|
|
| Working with SQLite | `import sqlite3` |
|
|
|
| Building a calendar | `import calendar` |
|
|
|
| Generating log files | `import logfile` |
|
|
|
| Advanced command line arguments | `import argparse` |
|
|
|
| Using advanced command line arguments | `import argparse` |
|
|
|
|
|
|
|
|
|
## Third-Party Libraries & pip
|
|
|
## Third-Party (External) Libraries & pip
|
|
|
|
|
|
When external libraries are required, the Python `pip` utility and a
|
|
|
`requirements.txt` can be used for all dependency and configuration management.
|
|
|
|
|
|
In C/C++ we hope for a Linux environment (or Docker). In Java... Gradle is a
|
|
|
popular build and configuration management tool.
|
|
|
popular build and configuration management tool. In Rust... Cargo handles
|
|
|
dependency and configuration management.
|
|
|
|
|
|
|
|
|
# Examples & Case Studies
|
... | ... | |