Skip to content

Instantly share code, notes, and snippets.

View mahynski's full-sized avatar

Nathan A. Mahynski mahynski

View GitHub Profile
@mahynski
mahynski / notes_cython.md
Last active October 16, 2023 16:08
Using Cython to Accelerate Python Code #notes

tl;dr

An initial disclaimer: "Premature optimization is the root of all evil!" Never optimize your code before your profile it. If you determine your code is too slow, and learn why after profiling (where it is spending time), you can begin optimization.

This is very brief summary of Cython and how to use it to quickly accelerate pure python code. Cython is much more adaptable than what I discuss below; I have chosen to focus primarily on how you can use it to accelerate existing code, usually written during the course of prototyping, with minimal modification. This is my primary use case for Cython, and it allows you to use your code in production for research much more easily than trying to develop optimized C/C++ from scratch.

In fact, if you are writing scientific code and working with numerical computations, numba often allows to accelerate code even more easily. I recommend you check that out first. Cython is the ne

@mahynski
mahynski / notes_git_repos_with_python.md
Last active October 16, 2023 16:08
Writing Better Python Code Automatically #notes

tl;dr

This is a guide to setting up git repos for python code that take advantage of as many automatic code linting, testing, and continuous improvement tools (CI) as I have found useful. There are certainly more. I work in a linux-based environment, and the following was tested on Ubuntu 18.04 LTS assuming you have admin privileges.

Repository configuration

Most repos are built as packages and therefore should take advantage of python's use of __init__.py files to indicate structure. This makes writing documentation, tests, and use of the code itself (importing it externally) rational and readable. Python includes a nice discussion about this here with examples on how to configure your code as a package. To be brief, here is the basic layout by example:

my_repository/