Skip to content

Instantly share code, notes, and snippets.

View lewoudar's full-sized avatar
🏠
Working from home

Kevin Tewouda lewoudar

🏠
Working from home
  • Paris
View GitHub Profile
@lewoudar
lewoudar / twitter_server_for_postponed_tweet_creation.py
Last active November 20, 2022 18:46
FastAPI server to to schedule tweet creation
# you will need to install the following libraries
# - FastAPI
# - tweepy (version 4.X)
# - twitter-text-parser
# - apscheduler (version 3.X)
import logging
import os
from datetime import datetime
from typing import Optional, List
@lewoudar
lewoudar / send_periodic_email_with_tweets.py
Created November 20, 2022 18:26
An example of how to schedule periodic emailing in python
import json
import os
import tempfile
from pathlib import Path
import emails
import tweepy
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
@lewoudar
lewoudar / click_params_2.py
Created March 14, 2022 21:09
Another example of click_params list type
import socket
import click
from click_params import DomainListParamType
@click.option(
'-d', '--domains',
prompt=True,
help='list of domain names separated by a space',
@lewoudar
lewoudar / click_params_1.py
Last active March 14, 2022 20:23
Example of click_param list parameter
import click
from click_params import StringListParamType
@click.option(
'-f', '--flavors',
prompt=True,
help='list of flavors for your ice cream',
type=StringListParamType()
)
@lewoudar
lewoudar / pydantic_func_argument_validation_3.py
Created March 10, 2022 22:23
A third example of pydantic argument validation using validate_arguments decorator and the Annotated typing feature
import math
from pydantic import validate_arguments, ValidationError, Field
from pydantic.typing import Annotated
@validate_arguments
def get_hypotenuse(
a: Annotated[float, Field(gt=0)],
b: Annotated[float, Field(gt=0)]
@lewoudar
lewoudar / pydantic_func_argument_validation_2.py
Last active March 10, 2022 22:23
A second example of pydantic argument validation using validate_arguments decorator
import math
from pydantic import validate_arguments, ValidationError, Field
@validate_arguments
def get_hypotenuse(
a: float = Field(..., gt=0),
b: float = Field(..., gt=0)
) -> float:
@lewoudar
lewoudar / non_pydantic_argument_validation.py
Created March 10, 2022 21:28
An example of function argument validation without using pydantic validate_arguments decorator
import math
from typing import Union
def get_hypotenuse(a: Union[int, float], b: Union[int, float]) -> float:
if not isinstance(a, (float, int)):
raise TypeError
if not isinstance(b, (float, int)):
raise TypeError
@lewoudar
lewoudar / pydantic_func_argument_validation_1.py
Last active March 11, 2022 20:46
An example of use of pydantic decorator validate_arguments
import math
from dataclasses import dataclass
from pydantic import validate_arguments, ValidationError
@dataclass
class Point:
x: int
y: int
@lewoudar
lewoudar / non_pydantic_validation_example.py
Last active March 11, 2022 20:41
An example of argument validation without using pydantic
import math
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
@lewoudar
lewoudar / ormar_json_querying.py
Last active November 11, 2021 11:20
A workaround to query ormar json field using sqlalchemy and databases
# this example is inspired by django docs: https://docs.djangoproject.com/en/3.2/topics/db/queries/#querying-jsonfield
from typing import Optional
import anyio
import databases
import ormar
import sqlalchemy
DATABASE_URL = 'sqlite:///db.sqlite'
database = databases.Database(DATABASE_URL)