Python version of Rust Result Sum Type

2025-01-19
I try to implement a Python version of the Result<T, E> sum type in rust which can be used with match/case pattern matching. The pattern matching with sub-patterns like case Err(e) could be implemented in Python with the __match_args__ dunder attribute. This is how Result[T, E], OK() and Err() defined: import typing T = typing.TypeVar('T') E = typing.TypeVar('E') class Result[T, E]: __match_args__ = ('_val',) def __init__(self, val): self._val = val class Ok[T](Result[T, . Continue reading

Run wasm built with wasm-pack with pythonmonkey

2025-01-03
I have been trying to run wasm code built with wasm-pack with pythonmonkey, a Mozilla SpiderMonkey JavaScript engine embedded into the Python Runtime. Consider a lib.rs as follow: use wasm_bindgen::prelude::*; #[wasm_bindgen] pub fn add(a: i32, b: i32) -> i32 { return a + b; } With wasm-pack, we can build the src code to wasm wasm-pack build --target nodejs --no-typescript We will get a .wasm file and a .js wrapper file. Continue reading

Combine mesop with natural abilities of flask

2024-12-23
mesop is popular with AI apps, since is can be used to build web apps with Python without frontend works. mesop is developed upon the popular frontend framework flask. How about combine the natural abilities of flask while developing with mesop? Let do a little research. Access flask request instance(including headers, cookies) In any mesop page, you may simply access flask.request, flask.request.cookies, flask.request.headers by importing flask. For example import flask import mesop as me @me. Continue reading

Create a partial model definition of a pydantic model

2024-12-09
Sometimes I want to create a partial model definition of a pydantic model. For example, if I want to expose a database model from API, some of the fields I would like to keep in secret, or I just want to expose very few fields in a summary API. I don’t want to write model definition twice. I would like to write something like ModelExposed = partial_model(ModelSource, 'ModelExposed', include=["title", "description"]) So I write a funciton as below to do it, including support of passing list of excluding fields, copying computed fields and validators. Continue reading

psql vs. mysql shell command cheatsheet

2024-12-05
MySQL Postgres list databases SHOW DATABASES \l change to a database USE <db_name> \c <db_name> list tables SHOW TABLES \dt describe a table DESC <table_name> \d <table_name> show the create table sql SHOW CREATE TABLE <table_name> pg_dump -st <table_name> <db_name> explain a query EXPLAIN <sql_statement> EXPLAIN <sql_statement> expanded(vertical) display \G \gxor use \x to switch get help ? <SQL STATEMENT>HELP <SQL STATEMENT> \h <SQL STATEMENT> get active processes SHOW [FULL] PROCESSLIST; SELECT pid, usename, state, query, query_start, application_name FROM pg_stat_activity WHERE state = ‘active’; kill a process KILL <pid>; SELECT pg_terminate_backend(<pid>); quit shell QUIT \q

Deploy next.js using pm2 cluster mode(and with docker)

2024-09-03
We can simply start the Next.js application using next start. However, when facing higher traffic demands in practical use, how can we deploy Next.js using a multi-process approach? We can achieve this using the cluster mode of pm2. The cluster mode is a powerful way of scaling Node.js applications, allowing you to start multiple processes, all listening on the same port. How do we do this? First, we can create a . Continue reading

Use python -m http.server in SSL

2024-09-02
python -m http.server is a very convenient command line tool to start an ad-hoc static web server. This tool is very useful when you want to develop small web applications or serve static files. But it does not support SSL. A lot of modern web applications as well as browser features require a secured connection. So I want to wrap http.server in SSL and try making it simple to use. Continue reading
Older posts