Skip to content

Instantly share code, notes, and snippets.

View vrthra's full-sized avatar

Rahul Gopinath vrthra

View GitHub Profile
import json
import sys
def test(s):
print('test:', repr(s))
if not s: return True
try:
json.loads(s)
print('YES', s)
except:
@vrthra
vrthra / markdown-flavors.md
Created March 2, 2022 13:14 — forked from vimtaai/markdown-flavors.md
Comparison of features in various Markdown flavors

Comparison of syntax extensions in Markdown flavors

I created a crude comparison of the syntax of the various common Markdown extensions to have a better view on what are the most common extensions and what is the most widely accepted syntax for them. The list of Markdown flavors that I looked at was based on the list found on CommonMark's GitHub Wiki.

Flavor Superscript Subscript Deletion*
Strikethrough
Insertion* Highlight* Footnote Task list Table Abbr Deflist Smart typo TOC Math Math Block Mermaid
GFM
@vrthra
vrthra / capture.py
Created February 10, 2022 08:13 — forked from oinume/capture.py
Capture stdout/stderr in Python
import cStringIO
import sys
class IOCapture(object):
def __init__(self, stdout = True, stderr = True):
self.captured_stdout = None
self.captured_stderr = None
if stdout:
self.captured_stdout = cStringIO.StringIO()
if stderr:
@vrthra
vrthra / remove_epsilons.py
Last active September 29, 2021 16:54
remove_epsilons.py
import string
import itertools
class Grammar:
def __init__(self, g, s):
self.grammar = g
self.start = s
def remove_empty_rule_keys(self):
while True:
keys_to_delete = []
@vrthra
vrthra / grammar.py
Last active September 24, 2021 10:28
import random
# 5 20 26 33 35 37 62 74 95 98
#random.seed(98)
class NT:
def __init__(self):
self.children = []
self.depth = 0
def __str__(self):
@vrthra
vrthra / import_module.py
Last active September 23, 2021 09:11
Import Module.
# ---
# published: true
# title: Importing Python Modules
# layout: post
# comments: true
# tags: python
# categories: post
# ---
# In the [previous post](/post/2018/09/06/peg-parsing/) I discussed how to
@vrthra
vrthra / fix_params.py
Created September 5, 2021 10:02
Fixing Python wtf -- parameter defaults
import functools
import inspect
def fix_params(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
original_defaults = func.__defaults__
new_defaults = []
for val in func.__defaults__:
if val == []:
@vrthra
vrthra / nlen_random_access.py
Last active July 27, 2021 11:49
Count strings of length n
grammar = {
'<start>': [['<expr>']],
'<expr>': [ ['<term>', '+', '<expr>'], ['<term>', '-', '<expr>'], ['<term>']],
'<term>': [ ['<fact>', '*', '<term>'], ['<fact>', '/', '<term>'], ['<fact>']],
'<fact>': [ ['<digits>'], ['(','<expr>',')']],
'<digits>': [ ['<digit>','<digits>'], ['<digit>']],
'<digit>': [["%s" % str(i)] for i in range(10)],
}
@vrthra
vrthra / inheritance.py
Created March 29, 2021 08:05
Python explicit inheritance
2
>>>
| vim a.py
class X:
def __init__(self, a,b,c):
self.a, self.b, self.c = a,b,c
def get_a(self):
return self.a