Skip to content

Instantly share code, notes, and snippets.

View opethe1st's full-sized avatar
🕶️
.

Ope opethe1st

🕶️
.
View GitHub Profile
@opethe1st
opethe1st / jinja_expression_statement.py
Created June 27, 2018 23:08
Describes using the expression statement
from jinja2 import FileSystemLoader
from jinja2 import Environment
from jinja2.ext import ExprStmtExtension
env = Environment(lstrip_blocks=True, loader=FileSystemLoader(searchpath='templates'), extensions=[ExprStmtExtension])
class Thing:
def __init__(self, a, b):
self.a = a
self.b = b
import string
from typing import List
graph = {
'1': ['2', '4'],
'2': ['1', '3', '5'],
'3': ['2', '6'],
'4': ['1', '5', '7'],
'5': ['2', '4', '6', '8'],
'6': ['3', '5', '9'],
@opethe1st
opethe1st / summation.py
Last active March 15, 2019 13:04
How to express summation
"""
How do we express the concept of summing values of a function over a set of values?
∑f(n) with n ∈ A
"""
array = range(29)
f = lambda x: x**2
# imperative
total = 0
from enum import Enum
from typing import Optional
from dataclasses import dataclass
class DIRECTION(Enum):
LEFT = 'L'
RIGHT = 'R'
@opethe1st
opethe1st / python_tweaks.md
Last active June 1, 2019 11:26
Tweaks to Python + other thoughts on programming

Tweaks to Python + other accompanying thoughts

This is based on my current (emphasis on current!) thoughts about what's best for code.

Dive in!

  • remove support for class and static methods. I believe it is neater for classes to be just prototypes or schemas for objects. So where would class and static methods go? They would go into the module :). For example,
# rectange.py - home of all things rectangular!

# This counts all the rectangles that have been created so far. This would have been a class variable
@opethe1st
opethe1st / underline.go
Created August 31, 2019 02:53
Underline a character - this is a snippet I found useful
package main
func underline(char rune) string {
return string([]rune{char, 204, 179})
}
func main(){
}
@opethe1st
opethe1st / range_int.go
Created November 21, 2019 12:44
range_int that works like range in Python
package main
import (
"fmt"
)
func range_int(start int, end int, step int) func() (int, bool) {
current := start
previous := 0
return func() (int, bool) {
@opethe1st
opethe1st / json_pointer.py
Created November 30, 2019 17:52
Implementation of JsonPointer
def dereference(value, pointer):
tokens = get_reference_tokens(pointer)
current_value = value
while tokens:
current = tokens.pop(0)
if current == "":
return current_value
elif isinstance(current_value, dict):
@opethe1st
opethe1st / idea.md
Last active February 16, 2020 13:13
A proposed DSL for data validation.

Data validation language

So I think it would be nice to have a DSL for just data validation (think jsonschema but just the bits that do with validation, no annotations and a simpler referencing system)

Why a DSL?

A DSL means that the language can be designed from the ground up for the best user experience and then compile to native languages ( the approach protobufs use for example). I don't propose any syntax or semantics that is radically different from what exists in standard programming languages today so I don't expect the learning curve to be high

Challenges with a DSL?

@opethe1st
opethe1st / context_manager.py
Created February 24, 2020 09:26
A context manager for managing contextvars
import contextlib
import contextvars
@contextlib.contextmanager
def context(contextvar: contextvars.ContextVar, value):
token = contextvar.set(value)
yield
contextvar.reset(token)