Skip to content

Instantly share code, notes, and snippets.

View vrthra's full-sized avatar

Rahul Gopinath vrthra

View GitHub Profile
@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
@vrthra
vrthra / ecearley.py
Last active February 27, 2021 06:39
error correcting earley
import random
import string
import itertools as I
grammar = {
'<start>': [['<expr>']],
'<expr>': [ ['<term>', '+', '<expr>'], ['<term>', '-', '<expr>'], ['<term>']],
'<term>': [ ['<fact>', '*', '<term>'], ['<fact>', '/', '<term>'], ['<fact>']],
'<fact>': [ ['<digits>'], ['(','<expr>',')']],
'<digits>': [ ['<digit>','<digits>'], ['<digit>']],
@vrthra
vrthra / pipes.py
Last active February 20, 2021 07:09
pipes in python
import time
class Chains:
def __or__(self, trans):
return trans.source(self)
class Source(Chains):
def __init__(self, i):
self.i = i
@vrthra
vrthra / donotuse3.py
Created September 18, 2020 07:11 — forked from MineRobber9000/donotuse3.py
How to NEVER use lambdas - Python 3 edition
###########################################################
# How to NEVER use lambdas. An inneficient and yet educa- #
# tonal [sic] guide to the proper misuse of the lambda #
# construct in Python 3.x. [DO NOT USE ANY OF THIS EVER] #
# original by (and apologies to): e000 (13/6/11) #
# now in Python 3 courtesy of: khuxkm (17/9/20) #
###########################################################
## Part 1. Basic LAMBDA Introduction ##
# If you're reading this, you've probably already read e000's