Skip to content

Instantly share code, notes, and snippets.

View ojii's full-sized avatar

Jonas Obrist ojii

View GitHub Profile
In [1]: class Foo:
...: def __init__(self):
...: exec("self.__bar = 'hello'")
...: def bar(self):
...: print(self.__bar)
...:
In [2]: foo = Foo()
In [3]: foo.__bar
class A:
def a(self):
class __B:
def __b(self):
print("B")
_A__B()._B__b()
A().a()
@ojii
ojii / example-output.txt
Last active December 26, 2018 06:12
Make `python -c ...` print the result
$ ./python.exe -c '2+3'
5
from typing import *
T = TypeVar('T')
class NonTrueAwaitable(Generic[T]):
def __init__(self, inner: Awaitable[T]):
self.inner = inner
@ojii
ojii / with_moto.py
Created April 12, 2017 08:21
Run moto in a separate process so it works on py3
from contextlib import contextmanager
from multiprocessing import Process, Queue
from moto.server import DomainDispatcherApplication, create_backend_app
from werkzeug.serving import make_server
def server_runner(port_queue: Queue, service: str):
main_app = DomainDispatcherApplication(create_backend_app, service=service)
server = make_server(host='localhost', port=0, app=main_app, threaded=True)
import React from 'react';
export class MyComponent extends React.Component {
static propTypes = {
name: React.PropTypes.string.isRequired
};
render() {
return <div>{this.props.name}</div>;
}
@ojii
ojii / streamed_response.py
Created February 17, 2017 10:49
sanic streamed response
class StreamResponse(HTTPResponse):
def __init__(self, request, length, status=200, headers=None,
content_type='text/plain', ):
self.request = request
self.length = length
self._headers_sent = False
headers = headers or {}
headers['Content-Length'] = str(length)
super().__init__(None, status, headers, content_type)
@ojii
ojii / please-include-a-repro.md
Last active January 24, 2023 00:01 — forked from Rich-Harris/please-include-a-repro.md
Please include a repro

Please include a repro

You probably arrived here because of a curt message in response to an issue you filed on a repo that I contribute to. Sorry about that (particularly if you filed the issue long ago and have been waiting patiently for a response). Let me explain:

I work on a lot of different open source projects. I really do like building software that makes other people's lives easier, but it's crazy time-consuming. One of the most time-consuming parts is responding to issues. A lot of OSS maintainers will bend over backwards to try and understand your specific problem and diagnose it, to the point of setting up new test projects, fussing around with different Python versions, reading the documentation for build tools that we don't use, debugging problems in third party dependencies that appear to be involved in the problem... and so on. I've personally spent hundreds of hours of my free time doing these sorts of things to try and help people out, because I want to be a responsible maintainer and

@ojii
ojii / turing.py
Created November 24, 2016 05:42
Python 2 in Python 3
import subprocess
def run_in_py2(code):
return subprocess.check_output(['python2', '-c', code])
#!/usr/bin/env python3
import argparse
import subprocess
import sys
def scan(formula, keys):
info = subprocess.check_output(['brew', 'info', formula])
for line in map(str.strip, info.decode('utf-8').splitlines()):
for key, target in keys.items():