Read a Range of Data - LIMIT and OFFSET - SQLModel
SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness.
sqlmodel.tiangolo.com [1]
Today I was running some sqlmodel queries through the sqlalchemy orm. Admittedly I’ve not done enough orm queries before, and I’ve done quite a bit of raw sql. I was trying to get objects from two separate models that had relationships setup.
session.query(User, Images).where(User.id == 3).all()
It is incredibly slow, and gives me the following warning.
SELECT statement has a cartesian product between FROM element(s)
What I learned from the SQLModel docs is that you should give it a join to correct this and go much faster.
session.query(User, Images).join(Images).where(User.id == 3).all()
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://sqlmodel.tiangolo.com/tutorial/limit-and-offset/
[2]: /thoughts/
Published
All published posts
2493 posts
latest post 2026-05-11
Publishing rhythm
So after months of fighting with gf not going to template files, I finally
decided to put in some effort to make it work.
This was the dumbest keybind in my config, that I copied from someone else
without understanding it.
What I am trying to do # [1]
I have jinja templates in a directory called templates. I want to bind gf to
open a template file, but it is trying to open a new file ./base.html
{% extends "base.html" %}
{% if request.state.user %}
{% block title %}Fokais - {{ request.state.user.full_name }} {% endblock %}
{% else %}
{% block title %}Fokais {% endblock %}
{% endif %}
{% block content %}
{% if request.state.user %}
<h1 id="title"
class="inline-block mx-auto text-5xl font-black leading-loose
text-transparent bg-clip-text bg-gradient-to-r from-red-600
via-pink-500 to-yellow-400 ring-red-500 text-shadow-xl
text-shadow-zinc-950 ring-5">
{{ request.state.user.full_name }}
</h1>
{% endif %}
{% include "me_partial.html" %}
{% endblock %}
What did not work # [2]
I tried all sorts of changes to my path, but it still didn’t work.
vim.api.nvim_command("set path+=templates/**")
What I found # [3]
after digging into my keymap I found that I had remaped gf ...
Template Designer Documentation — Jinja Documentation (3.1.x)
jinja.palletsprojects.com [1]
html [2] code generated by my jinja templates generally look half garbage because of indents and whitespace all over the place. I just learned about these pesky Whitespace Control characters that can get rid of the whitespace added from templating.
You can also strip whitespace in templates by hand. If you add a minus sign (-) to the start or end of a block (e.g. a For tag), a comment, or a variable expression, the whitespaces before or after that block will be removed:
{% for item in seq -%}
{{ item }}
{%- endfor %}
Note
This post is a thought [3]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://jinja.palletsprojects.com/en/3.0.x/templates/#whitespace-control
[2]: /html/
[3]: /thoughts/
Intro - Minecraft Server on Docker (Java Edition)
Documentation for Minecraft Server on Docker
docker-minecraft-server.readthedocs.io [1]
I just learned that if you can exec into the container running minecraft with the itzg/minecraft container you can run rcon-cli to get command access to the server. You need to set the RCON_PASSWORD if you want to access rcon remotely, but if you have not already done this and have access to the server you can just run rcon-cli when you are in.
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://docker-minecraft-server.readthedocs.io/en/latest/
[2]: /thoughts/
-
Theo’s response puts a lot of my feelings about unit testing into words. It’s crazy how cargo culty it becomes that the echo chamber of twitter can bring in beliefs that we think we believe, but have not experienced enough or put enough thought in to form our own opinion.
This video made me think so much that it turned into it’s own blog post
Thoughts on Unit Testing [1]
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://waylonwalker.com/thoughts-on-unit-tests
[2]: /thoughts/
thoughts on unit tests
[1]
Your browser does not support the audio element.
Theo’s response puts a lot of my feelings about unit testing into words. Many
of us have grown up in this world preaching unit testing. We often hear these
statements “Everything must be unit tested, tests make code more maintainable.”
In reality when we are not writing complex low level code unit tests are
probably the wrong approach.
[2]
Most of us are assemblers # [3]
So much of software engineering is assembling existing well tested code. Crud
applications, UI, Data Pipelines, building on top of battle tested code.
Manufacturing Analogy - Unit Testing # [4]
This kind of reminds me of Manufacturing. Individual components are QA tested
with tests that look more like unit test. Parts like bearings, pistons,
shafts, valves, they are all tested against sophisticated statistics of sample
measurements. This is quite similar to unit testing.
[5]
You see measuring the individual sizes of these components does not guarantee
...
Mastodon.py — Mastodon.py 2.2.1 documentation
mastodonpy.readthedocs.io [1]
Mastadon.py is a python api client for mastadon that makes it easy to cross post to mastadon.
from mastodon import Mastodon
Mastodon.create_app(
'pytooterapp',
api_base_url = 'https://mastodon.social',
to_file = 'pytooter_clientcred.secret'
)
from mastodon import Mastodon
mastodon = Mastodon(client_id = 'pytooter_clientcred.secret',)
mastodon.log_in(
'[email protected]',
'incrediblygoodpassword',
to_file = 'pytooter_usercred.secret'
)
mastodon.toot('Tooting from Python using #mastodonpy !')
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://mastodonpy.readthedocs.io/en/stable/
[2]: /thoughts/
bunny.net - The Global Edge Platform that truly Hops
Hop on bunny.net and speed up your web presence with the next-generation Content Delivery Service (CDN), Edge Storage, and Optimization Services at any scale.
bunny.net · bunny.net [1]
bunny.net looks like an interesting cloudflare alternative.
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://bunny.net/
[2]: /thoughts/
IndieWebify.Me - a guide to getting you on the IndieWeb
indiewebify.me [1]
This is a sick guided site to validate indieweb tags on your site. It makes it much easier than trying to do it yourself.
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://indiewebify.me/
[2]: /thoughts/
I came across nvim-macroni [1] from jesseleite [2], and it’s packed with great features and ideas.
🤌 Save your macros for future use
References:
[1]: https://github.com/jesseleite/nvim-macroni
[2]: https://github.com/jesseleite
jesseleite [1] has done a fantastic job with macroni.nvim [2]. Highly recommend taking a look.
Save your macros for future use 🤌
References:
[1]: https://github.com/jesseleite
[2]: https://github.com/jesseleite/macroni.nvim
ikalnytskyi [1] has done a fantastic job with httpie-auth-store [2]. Highly recommend taking a look.
Credential store plugin for HTTPie, attaches auth to ongoing request.
References:
[1]: https://github.com/ikalnytskyi
[2]: https://github.com/ikalnytskyi/httpie-auth-store
Authentication from cli tools can be a bit of a bear, and I have to look it up
every time. This is my reference guide for future me to remember how to easily
do it.
I set up a fastapi [1] server running on port 8000, it uses a basic auth with
waylonwalker as the username and asdf as the password. The server follows
along with what comes out of the docs. I have it setup to take basic auth,
form username and password, or a bearer token for authentication.
curl # [2]
The og [3] of command line url tools.
# basic auth
curl -u 'waylonwalker:asdf' -X POST localhost:8000/token
# basic auth with password prompt
curl -u 'waylonwalker' -X POST localhost:8000/token
# token
curl -H 'Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ3YXlsb253YWxrZXIiLCJleHAiOjE3MDI5NTI2MDJ9.GeYNt7DNal6LTiPoavJnqypaMt4vYeriXdq5lqu1ILg' -X POST localhost:8000/token
wget # [4]
My go to if I want the result to go into a file.
# basic auth
wget -q -O - --auth-no-challenge --http-user=waylonwalker --http-password=asdf --post-data '' localhost:8000/token
# token
wget -q -O - --header="Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ3YXlsb253YWxrZXIiLCJleHAiOjE3MDI5NT...
External Link
stackoverflow.com [1]
After struggling to get dependencies inside of middleware I learned that you can make global dependencies at the app level. I used this to set the user on every single route of the application without needing Depend on getting the user on each route.
from fastapi import Depends, FastAPI, Request
def get_db_session():
print("Calling 'get_db_session(...)'")
return "Some Value"
def get_current_user(session=Depends(get_db_session)):
print("Calling 'get_current_user(...)'")
return session
def recalculate_resources(request: Request, current_user=Depends(get_current_user)):
print("calling 'recalculate_resources(...)'")
request.state.foo = current_user
app = FastAPI(dependencies=[Depends(recalculate_resources)])
@app.get("/")
async def root(request: Request):
return {"foo_from_dependency": request.state.foo}
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://stackoverflow.com/questions/72243379/fastapi-dependency-inside-middleware#answer-72480781
[2]: /thoughts/
Handling Errors - FastAPI
FastAPI framework, high performance, easy to learn, fast to code, ready for production
fastapi.tiangolo.com [1]
This page shows how to customize your fastapi [2] errors. I found this very useful to setup common templates so that I can return the same 404’s both programatically and by default, so it all looks the same to the end user.
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
class UnicornException(Exception):
def __init__(self, name: str):
self.name = name
app = FastAPI()
@app.exception_handler(UnicornException)
async def unicorn_exception_handler(request: Request, exc: UnicornException):
return JSONResponse(
status_code=418,
content={"message": f"Oops! {exc.name} did something. There goes a rainbow..."},
)
@app.get("/unicorns/{name}")
async def read_unicorn(name: str):
if name == "yolo":
raise UnicornException(name=name)
return {"unicorn_name": name}
Note
This post is a thought [3]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://fastapi.tiangolo.com/tutorial/handling-errors/
[2]: /fastapi/
[3]: /thoughts/
External Link
github.com [1]
Setting an additional log handler to the uvicorn logger for access logs in fastapi [2] was not straightforward, but This post was very helpful.
@app.on_event("startup")
async def startup_event():
logger = logging.getLogger("uvicorn.access")
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
logger.addHandler(handler)
Note
This post is a thought [3]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://github.com/tiangolo/fastapi/issues/1508
[2]: /fastapi/
[3]: /thoughts/
External Link
stackoverflow.com [1]
Setting tags in your fastapi endpoints will group them in the docs. You can also set some metadata around the tags to get nice descriptions.
Here is a full example from the post.
from fastapi import FastAPI
tags_metadata = [
{"name": "Get Methods", "description": "One other way around"},
{"name": "Post Methods", "description": "Keep doing this"},
{"name": "Delete Methods", "description": "KILL 'EM ALL"},
{"name": "Put Methods", "description": "Boring"},
]
app = FastAPI(openapi_tags=tags_metadata)
@app.delete("/items", tags=["Delete Methods"])
@app.put("/items", tags=["Put Methods"])
@app.post("/items", tags=["Post Methods"])
@app.get("/items", tags=["Get Methods"])
async def handle_items():
return
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://stackoverflow.com/questions/63762387/how-to-group-fastapi-endpoints-in-swagger-ui#answer-63762765
[2]: /thoughts/
External Link
X (formerly Twitter) · twitter.com [1]
Most bloggers on my twitter blog right into a file that goes on git [2]. I kinda expected to have more database folk. I have my blog in markdown on git and the editing experience is top notch. I can just find files edit them in MY EDITOR, push them and I got a post. I am running thoughts in a sqlite database with a fastapi [3] backend, and holy crap the instant nature of posting feels so much better. Both sides have good points.
Note
This post is a thought [4]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://twitter.com/_WaylonWalker/status/1734387536716308693
[2]: /glossary/git/
[3]: /fastapi/
[4]: /thoughts/
Show some equivalent list comprehensions in filter examples · Issue #1068 · pallets/jinja
I'm willing to write a pull-request for this, but I just want to see what people think before I write it. So the issue is this. I'm very familiar with python. I'm new to Jinja2. Often I find myself...
GitHub · github.com [1]
I often want to reach for non existing list comprehensions in jinja 2, Here are a few nice equivalents.
a: {{ data | selectattr('x', 'gt', 5) | list }}
b: {{ data | map(attribute='c') | list }}
c: {{ data | selectattr('x', 'gt', 5) | map(attribute='c') | list }}
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://github.com/pallets/jinja/issues/1068
[2]: /thoughts/
External Link
vi.stackexchange.com [1]
I fixed my missing macro recording indicator that I lost and was never quite sure why. (because I forgot that I set cmdheight=0).
vim.cmd [[ autocmd RecordingEnter * set cmdheight=1 ]]
vim.cmd [[ autocmd RecordingLeave * set cmdheight=0 ]]
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://vi.stackexchange.com/questions/39947/nvim-vim-o-cmdheight-0-looses-the-recording-a-macro-messages
[2]: /thoughts/