Skip to content

Instantly share code, notes, and snippets.

What's happening

Armin Ronacher mitsuhiko

What's happening
View GitHub Profile
View gist:10734631
from importlib.machinery import ModuleSpec, SourceFileLoader
from _frozen_importlib import _SpecMethods
path = '/tmp/test.py'
loader = SourceFileLoader('plugin_base', path)
spec = ModuleSpec(name='plugin_base', loader=loader)
mod = _SpecMethods(spec).create()
View gist:10734180
>>> loader = machinery.SourceFileLoader('plugin_base', '/tmp/test.py')
>>> loader.create_module()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'SourceFileLoader' object has no attribute 'create_module'
>>> isinstance(loader, importlib.abc.Loader)
True
>>> importlib.abc.Loader.create_module
<function Loader.create_module at 0x10b84de18>
View gist:11330202
>>> import sys
>>> sys.stdin = sys.stdin.detach()
>>>
>>> input('foo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_io.BufferedReader' object has no attribute 'errors'
View wtf.py
$ python3 wtf.py
foo
bar
b''
View gist:11344831
>>> import io
>>> stdin = sys.stdin
>>> correct_stdin = io.TextIOWrapper(stdin.buffer, 'iso-8859-15')
>>> correct_stdin.readline()
foobar
'foobar\n'
>>> del correct_stdin
>>> stdin.readline()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
View gist:11344857
>>> import io
>>> sys.stdin.encoding
'ANSI_X3.4-1968'
>>> stdin = sys.stdin
>>> correct_stdin = io.TextIOWrapper(stdin.buffer, 'utf-8')
>>> correct_stdin.readline()
foobar
'foobar\n'
>>> del correct_stdin
>>> stdin.readline()
View gist:11395418
mitsuhiko at atherton in /tmp/test
$ python3
Python 3.4.0 (v3.4.0:04f714765c13, Apr 15 2014, 15:41:42)
[GCC 4.2.1 Compatible Clang 3.2 (tags/RELEASE_32/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> letter = '\xf6'
>>> letter
'
>>> open(letter, 'w').close()
View howtouse.txt
$ flask -a myapp
Usage: flask [OPTIONS] COMMAND [ARGS]...
This shell command acts as general utility script for Flask applications.
It loads the application configured (either through the FLASK_APP
environment variable or the --app parameter) and then provides commands
either provided by the application or Flask itself.
The most useful commands are the "run" and "shell" command.
View clickinout.py
import click
@click.command()
@click.argument('input', type=click.File('rb'), nargs=-1)
@click.argument('output', type=click.File('wb'))
def cli(input, output):
for f in input:
while True:
chunk = f.read(1024)
if not chunk:
View gist:09595f380eed3083b508
import os
debug = True
if os.environ.get('APP_NO_DEBUG'):
dеbug = False
You can’t perform that action at this time.