Skip to content

Instantly share code, notes, and snippets.

@dvmn-tasks
dvmn-tasks / exception-bubbling.md
Last active November 2, 2023 16:20
Где перехватывать исключения

Исключения умеют всплывать

Исключения в Python — это особый объект, который умеет путешествовать между функциями и менять их поведение. Своим появлением исключение прерывает обычное исполнение программы — сверху вниз и вглубь — и переводит его в особый обратный режим — наверх до подходящего try except finally.

Для примера рассмотрим программу — информер с прогнозом погоды. Функция request_weather делает запрос к API сайта weather.com и возвращает прогноз погоды:

def request_weather():
    response = requests.get('https://weather.com/api/weather/moscow/')
    return response.json()
@emmanuellyautomated
emmanuellyautomated / coroutines.py
Created November 21, 2017 17:14
An example of using Python coroutines to create processing pipelines.
import json
import os
import requests
from copy import copy
# To run try this out, run --> `python coroutines.py`
import asyncio
loop = asyncio.get_event_loop()
async def hello():
await asyncio.sleep(3)
print('Hello!')
if __name__ == '__main__':
loop.run_until_complete(hello())
@pawl
pawl / gist:f6c0270d83241c31237a
Last active March 19, 2019 04:24
Example of an user selected page_size in flask-admin
from flask import Flask, session, request
from flask_sqlalchemy import SQLAlchemy
import flask_admin as admin
from flask_admin.contrib import sqla
from flask_admin import expose
# Create application
app = Flask(__name__)
@skyuplam
skyuplam / gist:ffb1b5f12d7ad787f6e4
Created October 13, 2014 07:50
Flask-Security and Flask-Admin example by Steve Saporata
# Example of combining Flask-Security and Flask-Admin.
# by Steve Saporta
# April 15, 2014
#
# Uses Flask-Security to control access to the application, with "admin" and "end-user" roles.
# Uses Flask-Admin to provide an admin UI for the lists of users and roles.
# SQLAlchemy ORM, Flask-Mail and WTForms are used in supporting roles, as well.
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
*.pyc
*.jpe
*.png
# code to take a PDF and scrape address information. Note that this particular script will
# only work using the specific PDF formatting my PDF had so you can use as a guide but
# it will definitely not work on your PDF!
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import HTMLConverter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
<snippet>
<content><![CDATA[
<!-- begin $1 -->
<div class="$1">
$2
</div>
<!-- end $1 -->
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>di</tabTrigger>
@rubillionaire
rubillionaire / gist:7775655
Created December 3, 2013 19:11
Python using virtualenv and pip.

Python using virtualenv and pip

With every project you write in python, you will have different libraries that you lean on. Some projects might use the same library, but different versions. For this reason, the Python library virtualenv was created. To enable a developer to quickly create new environments.

Every virtualenv instance comes with pip installed, which is a package manager for python. This allows you to do install packages like so.

pip install django

Installing Virtualenv

@sloria
sloria / bobp-python.md
Last active November 14, 2024 15:01
A "Best of the Best Practices" (BOBP) guide to developing in Python.

The Best of the Best Practices (BOBP) Guide for Python

A "Best of the Best Practices" (BOBP) guide to developing in Python.

In General

Values

  • "Build tools for others that you want to be built for you." - Kenneth Reitz
  • "Simplicity is alway better than functionality." - Pieter Hintjens