Skip to content

Instantly share code, notes, and snippets.

import asyncio
loop = asyncio.get_event_loop()
async def hello():
await asyncio.sleep(3)
print('Hello!')
if __name__ == '__main__':
loop.run_until_complete(hello())
@miguelgrinberg
miguelgrinberg / .vimrc
Last active April 4, 2024 19:06
My .vimrc configuration for working in Python with vim
" plugins
let need_to_install_plugins = 0
if empty(glob('~/.vim/autoload/plug.vim'))
silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
let need_to_install_plugins = 1
endif
call plug#begin()
Plug 'tpope/vim-sensible'
@miguelgrinberg
miguelgrinberg / rest-server.py
Last active March 29, 2024 09:05
The code from my article on building RESTful web services with Python and the Flask microframework. See the article here: http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask
#!flask/bin/python
from flask import Flask, jsonify, abort, request, make_response, url_for
from flask_httpauth import HTTPBasicAuth
app = Flask(__name__, static_url_path = "")
auth = HTTPBasicAuth()
@auth.get_password
def get_password(username):
if username == 'miguel':
@miguelgrinberg
miguelgrinberg / aioeventlet.py
Last active February 20, 2024 13:07
Eventlet running on top of asyncio proof-of-concept
# MIT License
#
# Copyright (c) 2023 Miguel Grinberg
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#!/bin/env python
# -------------------------------------------------------------------------------
# This is a basic implementation of comments with a flat structure,
# as described in my article:
# https://blog.miguelgrinberg.com/post/implementing-user-comments-with-sqlalchemy
# -------------------------------------------------------------------------------
from datetime import datetime
from flask import Flask
@miguelgrinberg
miguelgrinberg / undercurl
Created July 27, 2023 23:17
Tiny script to test if the undercurl control codes work in your terminal.
#!/bin/bash
printf 'This is \e[4:3mmono undercurl\e[0m,\n'
printf 'and this is \e[4:3m\e[58:2:206:64:51mcolor undercurl\e[0m.\n'
" plugins
set nocompatible
filetype off
let need_to_install_plugins=0
if empty(system("grep lazy_load ~/.vim/bundle/vundle/autoload/vundle.vim"))
echo "Installing Vundle..."
echo ""
silent !mkdir -p ~/.vim/bundle
silent !rm -rf ~/.vim/bundle/vundle
silent !git clone https://github.com/gmarik/vundle ~/.vim/bundle/vundle
@miguelgrinberg
miguelgrinberg / sqlalchemy-challenge.py
Last active October 18, 2022 06:51
A little SQLAlchemy challenge. See blog post at https://blog.miguelgrinberg.com/post/nested-queries-with-sqlalchemy-orm for details!
#!/usr/bin/env python
# Before you run this script make sure Flask-SQLAlchemy is installed in
# your virtual environment
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' # in-memory
@miguelgrinberg
miguelgrinberg / ujwt.py
Last active October 9, 2022 22:32
JWT support for MicroPython
import binascii
import hashlib
import hmac
import json
from time import time
def _to_b64url(data):
return (
binascii.b2a_base64(data)
@miguelgrinberg
miguelgrinberg / pycon-views.py
Last active September 18, 2022 05:41
Generate statistics about PyCon 2014 videos
import argparse
import re
from multiprocessing.pool import ThreadPool as Pool
import requests
import bs4
root_url = 'http://pyvideo.org'
index_url = root_url + '/category/50/pycon-us-2014'