This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FormatDict(dict): | |
| def __missing__(self, key): | |
| return '{{{}}}'.format(key) | |
| def format_string(string, context: dict) -> str: | |
| """ | |
| Format string without KeyError if key is not present in dict. | |
| >>> format_string('Oh, hi {first_name} {last_name}', {'first_name': 'Mark'}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_phones_from_text(text: str) -> Set[str]: | |
| phone_matches_iterator = re.finditer(r'(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}', text) | |
| return set((r.group(0) for r in phone_matches_iterator)) | |
| def get_emails_from_text(text: str) -> Set[str]: | |
| email_matches_iterator = re.finditer(r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+', text) | |
| return set((r.group(0) for r in email_matches_iterator)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import collections | |
| def flatten(d: dict, parent_key='', sep='__', flat_lists=False) -> dict: | |
| """ | |
| >>> flatten({'name': 'Ivan', 'mother': {'name': 'Alisha'}}) | |
| {'name': 'Ivan', 'mother__name': 'Alisha'} | |
| >>> flatten({'name': 'Ivan', 'brothers': [{'name': 'Jack'}]}, flat_lists=True) | |
| {'name': 'Ivan', 'brothers_0__name': 'Jack'} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from datetime import datetime | |
| def seconds_to_iso8601_duration(seconds: int) -> str: | |
| return datetime.utcfromtimestamp(seconds).strftime('PT%HH%MM%SS') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Example of usage: | |
| from googleapiclient.discovery import build | |
| from httplib2 import Http | |
| from oauth2client import file | |
| # Create google client first, for example | |
| storage = file.Storage('path_to_google_oauth_credentials') | |
| creds = storage.get() | |
| http = creds.authorize(Http()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.db import models | |
| from django.contrib import admin | |
| from django.contrib.admin.options import csrf_protect_m | |
| class SingletonModel(models.Model): | |
| class Meta: | |
| abstract = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def tabl(model_or_qs, field_names: str = '', limit: int = 1000): | |
| """ | |
| Examples: | |
| >>> tabl(UserStatus) | |
| or | |
| >>> tabl(User, 'first_name,last_name') | |
| or | |
| >>> tabl(User.objects.filter(is_staff=True), 'first_name,last_name', limit=20) | |
| """ | |
| qs = model_or_qs.objects.all() if inspect.isclass(model_or_qs) else model_or_qs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sqlparse | |
| from pygments import highlight | |
| from pygments.formatters.terminal import TerminalFormatter | |
| from pygments.lexers import SqlLexer # to highlight SQL statements | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import Query | |
| engine = create_engine("sqlite+pysqlite:///db.sqlite", echo=True, future=True) | |
| def format_sql(query: Query): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import asyncio | |
| import aiohttp | |
| from multidict import CIMultiDictProxy | |
| @dataclass | |
| class AsyncToSyncResponse: | |
| status: int | |
| content: dict | |
| headers: CIMultiDictProxy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BatchSendToSQS: | |
| """ | |
| >>> with BatchSendToSQS(someQueue, batch_size=10) as sqs_batcher: | |
| >>> sqs_batcher.add_msg({'data': 'booba'}) | |
| >>> ... | |
| >>> sqs_batcher.add_msg({'data': 'piu piu'}) | |
| """ | |
| def __init__(self, queue: "boto3.sqs.Queue", batch_size: int): | |
| self.queue = queue |