Skip to content

Instantly share code, notes, and snippets.

class TestForwardNullPagination(TestCase):
@classmethod
def setUpTestData(cls):
now = timezone.now()
cls.items = []
for i in range(2): # index 0-1
author = Author.objects.create(name='Name %s' % i, age=i+20, created=now - datetime.timedelta(hours=i))
cls.items.append(author)
for i in range(5): # index 2-6
author = Author.objects.create(name='NameNull %s' % (i + 2), age=None, created=now - datetime.timedelta(hours=i))
from base64 import b64decode, b64encode
from collections.abc import Sequence
from django.db.models import F, Q, TextField, Value
from django.utils.translation import gettext_lazy as _
class InvalidCursor(Exception):
pass
from django.db import transaction
class MyDjangoDBRoutner:
def db_for_read(self, model, **hints):
"""
When using multiple DB on Django meant for write and read, this condition will be necessary.
When inside a DB transaction, "replica" might not have un-commited data, so use "primary" for read.
"""
if transaction.get_connection().in_atomic_block:
# (my_datetime + pd.tseries.offsets.BusinessDay(n=my_days)).to_pydatetime() does not work.
# Try my_datetime = "2020-02-02 00:00", and my_days = 15, the expected time is "2020-02-24 00:00",
# but the result is "2020-02-21 00:00"
from datetime import datetime
import numpy as np
my_datetime = datetime.fromisoformat("2020-02-02 00:00")
datetime.combine(np.busday_offset(my_datetime.date(), 15, roll='forward').item(), my_datetime.time(), tzinfo=pytz.utc)
# => datetime.datetime(2020, 2, 24, 0, 0, tzinfo=<UTC>)
@untidy-hair
untidy-hair / asyncio_wait_exceptions.py
Created January 24, 2022 03:04
async.wait() Exception handling example
import asyncio
async def process_even(i):
return i
async def process_odd(i):
raise ValueError(f"Only even numbers are allowed! Given: {i}")

Keybase proof

I hereby claim:

  • I am untidy-hair on github.
  • I am untidyhair (https://keybase.io/untidyhair) on keybase.
  • I have a public key ASDw4gqUGGq-YdhpKWTwUW_fAa7g583wzNWf89a_noO0Xgo

To claim this, I am signing this object:

@untidy-hair
untidy-hair / Gemfile
Created January 20, 2014 20:11
Additional code for rails before start just for me
gem 'pry-rails'
group :development, :test do
gem 'rspec-rails'
gem 'shoulda-matchers'
gem 'factory_girl_rails'
end