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 json | |
import threading | |
import boto3 | |
from configurations.config_store import Config | |
from src.routes.frontapp.tasks import ( | |
add_tags_to_conversation, | |
reply_to_front_message, | |
) |
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 json | |
import openai | |
from pydantic import BaseModel, PrivateAttr | |
from src.common.config import Config | |
openai.api_key = Config.OPEN_AI_API_KEY | |
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 |
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
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
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
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
''' | |
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 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
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'} |
NewerOlder