Skip to content

Instantly share code, notes, and snippets.

View mitsuhiko's full-sized avatar
🖥️
What's happening

Armin Ronacher mitsuhiko

🖥️
What's happening
View GitHub Profile
>>> 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>
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()
>>> 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'
>>> 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>
>>> 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()
$ python3 wtf.py
foo
bar
b''
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()
$ 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.
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:
import os
debug = True
if os.environ.get('APP_NO_DEBUG'):
dеbug = False