Best of Adam Johnson2024

  1. 1
    Article
    Avatar of adamjAdam Johnson·2y

    Git: avoid reset --hard, use reset --keep instead

    When undoing commits in Git, it's recommended to use 'git reset --keep' instead of 'git reset --hard' to avoid losing uncommitted changes. 'reset --hard' is highly destructive as it discards unsaved work, making recovery difficult. In contrast, 'reset --keep' preserves uncommitted changes and fails if it jeopardizes unsaved work. The post provides practical examples and suggests setting an alias for convenience.

  2. 2
    Article
    Avatar of adamjAdam Johnson·2y

    Python: my new uv setup for development

    uv is a new, speedy Python packaging tool that now manages both Python versions and Python-powered tools. The author switched to uv for Python and tool management, replacing Mise, pipx, and Pip, to streamline their development workflow. They use Ansible to automate the setup, including installing and upgrading Python versions and tools, and cleaning up old cache entries. The post provides specific Ansible tasks for these operations.

  3. 3
    Article
    Avatar of adamjAdam Johnson·2y

    Django: build a Microsoft Teams bot

    Building a Microsoft Teams bot in Django is challenging due to issues such as convoluted documentation, a fragmented Bot Framework Python package, and limited helpful sample apps. Despite these obstacles, the bot can be configured and tested, with alternatives like the Teams Developer Portal simplifying some aspects. Key steps include handling incoming webhook messages, managing channel names, and navigating Adaptive Cards for message formatting. A single-file Django example and related tests are provided to get started.

  4. 4
    Article
    Avatar of adamjAdam Johnson·1y

    Git: undo a pull

    Learn how to undo a git pull by resetting your local branch to a previous commit. If you have access to the git pull output, you can identify the previous commit SHA directly. If the output is not available, use git reflog to find the necessary SHA. Use git reset with the --keep option to revert to the desired commit.

  5. 5
    Article
    Avatar of adamjAdam Johnson·2y

    Git: generate statistics with shortlog

    Learn how to generate detailed commit statistics in a Git repository using the 'git shortlog' command. The guide covers limiting commits by range, pathspec, and grouping by day or other fields formatted from the commit. It also touches on advanced configurations to optimize your repository analysis.

  6. 6
    Article
    Avatar of adamjAdam Johnson·2y

    Django: avoid “useless use of .all()”

    Using the `.all()` method in Django's ORM is often unnecessary and can lead to slightly more code and performance overhead. The manager `Digger.objects` already refers to all `Digger` objects, making `.all()` redundant in most cases. Appropriate use cases for `.all()` include creating an all-inclusive queryset for future operations and confirming deletions of entire tables.

  7. 7
    Article
    Avatar of adamjAdam Johnson·2y

    Django: a pattern for settings-configured API clients

    A common pattern in Django projects is to instantiate an API client as a module-level variable based on settings, which has drawbacks, especially during tests and with expensive clients. An alternative pattern involves using functools.cache to instantiate the client on first use and resetting the cache when settings change using Django's setting_changed signal. This approach improves efficiency and testability.

  8. 8
    Article
    Avatar of adamjAdam Johnson·2y

    PostgreSQL: Full text search with the “websearch” syntax

    PostgreSQL's powerful full text search feature supports the websearch syntax, which is forgiving and user-friendly. It allows individual word matching, double-quoted phrase matching, OR conditions, and negating matching. This post explains the syntax and how to use it in queries. It also mentions the integration of websearch in Django.

  9. 9
    Article
    Avatar of adamjAdam Johnson·2y

    Django: hoist repeated decorator definitions

    Learn how to hoist repeated decorator definitions in Django to reduce redundancy in your code. The technique involves calling a decorator function once and reusing its result to apply the decorator, which is beneficial for both function-based and class-based views. This approach also improves test readability by ensuring the same decorator is applied consistently across tests.