Skip to content

Instantly share code, notes, and snippets.

Avatar

Vinay Sajip vsajip

View GitHub Profile
@vsajip
vsajip / custom_directive.py
Created Nov 27, 2019 — forked from mastbaum/custom_directive.py
Example of a custom ReST directive in Python docutils
View custom_directive.py
'''Example of a custom ReST directive in Python docutils'''
import docutils.core
from docutils.nodes import TextElement, Inline
from docutils.parsers.rst import Directive, directives
from docutils.writers.html4css1 import Writer, HTMLTranslator
class foo(Inline, TextElement):
'''This node class is a no-op -- just a fun way to define some parameters.
There are lots of base classes to choose from in `docutils.nodes`.
View qtlog.py
import datetime
import logging
import random
import sys
import time
# Deal with minor differences between PySide2 and PyQt5
try:
from PySide2 import QtCore, QtGui, QtWidgets
Signal = QtCore.Signal
View aiohttp_request_id_logging.py
# 05_aiohttp.py
from aiohttp import web
from aiohttp.web_log import AccessLogger
from asyncio import CancelledError
from contextvars import ContextVar
import asyncio
import logging
import secrets
@vsajip
vsajip / Args.ipynb
Created Feb 28, 2019 — forked from gbishop/Args.ipynb
Allow arguments to be passed to notebooks via URL or command line.
View Args.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vsajip
vsajip / playground_02.rs
Created Oct 18, 2018 — forked from rust-play/playground.rs
Code shared from the Rust Playground
View playground_02.rs
use std::io::{BufRead};
struct Processor<B: BufRead> {
reader: B
}
// If you uncomment p in ValidatorA, compilation fails because the size of
// ValidatorA can't be determined at compile time. Fair enough.
struct ValidatorA {
// p : Processor<BufRead>
@vsajip
vsajip / playground_01.rs
Last active Oct 17, 2018 — forked from rust-play/playground.rs
Code shared from the Rust Playground
View playground_01.rs
use std::io::{self,BufRead,BufReader};
enum Utf8DecoderError<'a> {
InvalidBytes(&'a [u8]),
Io(io::Error),
}
struct Utf8Decoder<B: BufRead> {
reader : B,
bytes_read: usize
@vsajip
vsajip / app.py
Created Oct 16, 2018
CLI starter template for Python logging cookbook
View app.py
import argparse
import importlib
import logging
import os
import sys
def main(args=None):
scriptname = os.path.basename(__file__)
parser = argparse.ArgumentParser(scriptname)
levels = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')
@vsajip
vsajip / fmt_test.py
Last active Oct 11, 2018
Logging format string validation
View fmt_test.py
import os
import re
import string
import sys
_sfmt = string.Formatter()
FIELD_SPEC = re.compile(r'^(\d+|\w+)(\.\w+|\[[^]]+\])*$')
FMT_SPEC = re.compile(r'^(.?[<>=^])?[+ -]?#?0?(\d+|{\w+})?[,_]?(\.(\d+|{\w+}))?[bcdefgnosx%]?$')
View ColumnAttributeTypeMapper.cs
namespace YourNamespace
{
/// <summary>
/// Uses the Name value of the <see cref="ColumnAttribute"/> specified to determine
/// the association between the name of the column in the query results and the member to
/// which it will be extracted. If no column mapping is present all members are mapped as
/// usual.
/// </summary>
/// <typeparam name="T">The type of the object that this association between the mapper applies to.</typeparam>
public class ColumnAttributeTypeMapper<T> : FallbackTypeMapper
View did-you-know-re.py
from sre_parse import Pattern, SubPattern, parse as sre_parse
from sre_compile import compile as sre_compile
from sre_constants import BRANCH, SUBPATTERN
class Scanner(object):
def __init__(self, tokens, flags=0):
subpatterns = []
pat = Pattern()