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 / extract_text_and_images_from_pdf.py
Last active August 12, 2024 19:36
Example usage of pdfminer to extract text and images from a PDF
from pdfminer.high_level import extract_text_to_fp
from pdfminer.layout import LAParams
def extract_text_from_pdf(input_filename: str, output_filename: str, output_images_dir: str | None = None) -> None:
with open(input_filename, 'rb') as input_file, open(output_filename, 'w', encoding='utf-8') as output_file:
extract_text_to_fp(input_file, output_file, output_dir=output_images_dir, laparams=LAParams())
extract_text_from_pdf(
@lewoudar
lewoudar / transcribe.py
Created June 22, 2024 11:55
Playing with the ctranslate2 backend
import os
import time
from dataclasses import dataclass, field
from pathlib import Path
from typing import Literal
from faster_whisper import WhisperModel
model_size = "large-v3"
# workaround if we have already installed openai whisper stuff via transformers
@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