27th Week of 2022
Projects⚑
-
New: Beancount forecast.
I'd like to see a forecast of the evolution of my accounts given an amount of time. Maybe by doing seasonality analysis and forecast in time series as stated here and here.
It will also be interesting to see for a given account the evolution of the subaccounts.
Coding⚑
-
New: Apply a style to a component given a condition.
if you use
:class
you can write javascript code in the value, for example:<b-form-radio class="user-retrieve-language p-2" :class="{'font-weight-bold': selected === language.key}" v-for="language in languages" v-model="selected" :id="language.key" :checked="selected === language.key" :value="language.key" >
-
New: Debug Jest tests.
If you're not developing in Visual code, running a debugger is not easy in the middle of the tests, so to debug one you can use
console.log()
statements and when you run them withyarn test:unit
you'll see the traces.
Python⚑
Type Hints⚑
-
New: Using
typing.cast
.Sometimes the type hints of your program don't work as you expect, if you've given up on fixing the issue you can
# type: ignore
it, but if you know what type you want to enforce, you can usetyping.cast()
explicitly or implicitly fromAny
with type hints. With casting we can force the type checker to treat a variable as a given type.The main case to reach for
cast()
are when the type hints for a module are either missing, incomplete, or incorrect. This may be the case for third party packages, or occasionally for things in the standard library.Take this example:
import datetime as dt from typing import cast from third_party import get_data data = get_data() last_import_time = cast(dt.datetime, data["last_import_time"])
Imagine
get_data()
has a return type ofdict[str, Any]
, rather than using stricter per-key types with aTypedDict
. From reading the documentation or source we might find that thelast_import_time
key always contains adatetime
object. Therefore, when we access it, we can wrap it in acast()
, to tell our type checker the real type rather than continuing withAny
.When we encounter missing, incomplete, or incorrect type hints, we can contribute back a fix. This may be in the package itself, its related stubs package, or separate stubs in Python’s typeshed. But until such a fix is released, we will need to use
cast()
to make our code pass type checking.
Python Snippets⚑
-
Interesting to test the accepted format of RSS dates.
>>> from email.utils import parsedate_to_datetime >>> datestr = 'Sun, 09 Mar 1997 13:45:00 -0500' >>> parsedate_to_datetime(datestr) datetime.datetime(1997, 3, 9, 13, 45, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))
-
New: Convert a datetime to RFC2822.
Interesting as it's the accepted format of RSS dates.
>>> import datetime >>> from email import utils >>> nowdt = datetime.datetime.now() >>> utils.format_datetime(nowdt) 'Tue, 10 Feb 2020 10:06:53 -0000'
-
New: Encode url.
import urllib.parse from pydantic import AnyHttpUrl def _normalize_url(url: str) -> AnyHttpUrl: """Encode url to make it compatible with AnyHttpUrl.""" return typing.cast( AnyHttpUrl, urllib.parse.quote(url, ":/"), )
The
:/
is needed when you try to parse urls that have the protocol, otherwisehttps://www.
gets transformed intohttps%3A//www.
.
Javascript⚑
-
New: Coalescent operator.
Is similar to the Logical
OR
operator (||
), except instead of relying on truthy/falsy values, it relies on "nullish" values (there are only 2 nullish values,null
andundefined
).This means it's safer to use when you treat falsy values like
0
as valid.Similar to Logical
OR
, it functions as a control-flow operator; it evaluates to the first not-nullish value.It was introduced in Chrome 80 / Firefox 72 / Safari 13.1. It has no IE support.
console.log(4 ?? 5); // 4, since neither value is nullish console.log(null ?? 10); // 10, since 'null' is nullish console.log(undefined ?? 0); // 0, since 'undefined' is nullish // Here's a case where it differs from // Logical OR (||): console.log(0 ?? 5); // 0 console.log(0 || 5); // 5
Javascript snippets⚑
-
New: Set variable if it's undefined.
var x = (x === undefined) ? your_default_value : x;
Operating Systems⚑
Linux⚑
Linux Snippets⚑
-
New: Git checkout to main with master as a fallback.
I usually use the alias
gcm
to change to the main branch of the repository, given the change from main to master now I have some repos that use one or the other, but I still wantgcm
to go to the correct one. The solution is to use:alias gcm='git checkout "$(git symbolic-ref refs/remotes/origin/HEAD | cut -d'/' -f4)"'
Science⚑
Data Analysis⚑
Recommender Systems⚑
-
Bookwyrm looks to be a promising source to build