This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# --- | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 == []: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)], | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>']], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
class Chains: | |
def __or__(self, trans): | |
return trans.source(self) | |
class Source(Chains): | |
def __init__(self, i): | |
self.i = i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########################################################### | |
# 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 |