Skip to content

Instantly share code, notes, and snippets.

View SahbiOuali13's full-sized avatar
🎯
Focusing

SahbiOuali SahbiOuali13

🎯
Focusing
  • Toulouse
View GitHub Profile
@SahbiOuali13
SahbiOuali13 / .bashrc
Last active February 26, 2023 17:56
* Specify the location of your PYTHONSTARTUP file * Write down your preferences into your .pythonstartup file * It will be taken into account everytime you load your REPL session. * pip install rich for colorization.#
# .bashrc
# ...
export PYTHONSTARTUP=~/.pythonstartup
@SahbiOuali13
SahbiOuali13 / project.md
Last active November 19, 2022 18:12
- It uses jinja2 templating - `pipx install cookiecutter` - Install from github audreyfeldroy/cookiecutter-pypackage - cookiecutter gh:audreyfeldroy/cookiecutter-pypackage - You can create your own project template and reproduce it using `cookiecutte
  1. Create your own project folder you want to reproduce, then cookiecutter.json

  • Create cookiecutter.json
{
    "project_name": "Mon super projet",
    "project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '_').replace('-', '_') }}",
 "project_decription": "Give a short description of your project.",
@SahbiOuali13
SahbiOuali13 / advanced_testing_with_code_excec.py
Last active November 1, 2022 08:46
# Sensitifity to types and letter_casing / whitespaces / characters ("" / '' / ...) - In practice, doctest is very strict when matching expected output with actual results. - For example, using integers instead of floating-point numbers will break t
# queue.py
from collections import deque
class Queue:
def __init__(self):
self._elements = deque()
def enqueue(self, element):
"""Add items to the right end of the queue.
@SahbiOuali13
SahbiOuali13 / mech_soup.py
Created October 22, 2022 08:45
Now break down the above example: 1. You create a Browser instance and use it to request the URL http://olympus.realpython.org/login. You assign the HTML content of the page to the login_html variable using the .soup property. 2. login_html.selec
import mechanicalsoup
# 1
browser = mechanicalsoup.Browser()
url = "http://olympus.realpython.org/login"
login_page = browser.get(url)
login_html = login_page.soup
# 2
form = login_html.select("form")[0]
from pathlib import Path
import multiprocessing
import sys
from PIL import Image, ImageFilter
from PIL import ImageEnhance
import PySimpleGUI as sg
CURRENT_DIR: Path = Path().cwd()
@SahbiOuali13
SahbiOuali13 / custom.py
Last active October 3, 2022 08:17
* How to use custom string formatter in f-strings. * Default used string formatters in f strings.
class Person:
def __init__(self, name) -> None:
self.name = name
def __format__(self, __format_spec):
if __format_spec == "scream":
return self.name.upper() + '!'
elif __format_spec == "repeat":
return self.name * 3
return self.name
  • This is a command-line tool that you can call to start a project. It’ll take care of creating a README.md file and a .gitignore file, and then it’ll run a few commands to create a virtual environment, initialize a git repository, and perform your first commit.
  • It’s even cross-platform, opting to use pathlib to create the files and folders, which abstracts away the operating system differences.
from pathlib import Path
from argparse import ArgumentParser
import subprocess


def create_new_project(name):
"""
Create a function that accepts a string. The function should return a string, with each character doubled.
E.g. MMaarrttiinn
"""
def double_characters(word):
if not word:
raise ValueError('You gave an empty list.')
double = ""
@SahbiOuali13
SahbiOuali13 / data_pipeline.py
Last active May 22, 2022 07:44
- This can be useful when working with big csv files for example. - You can work with chunks of the data and prevent a potential MemoryError due to the data not fitting into memory. - The impact on our script is processing one line of the entire file
file_name = "techcrunch.csv"
lines = (line for line in open(file_name))
list_line = (s.rstrip().split(",") for s in lines)
cols = next(list_line)
company_dicts = (dict(zip(cols, data)) for data in list_line)
funding = (
int(company_dict["raisedAmt"])
for company_dict in company_dicts
if company_dict["round"] == "a"
)
@SahbiOuali13
SahbiOuali13 / example.py
Last active May 20, 2022 20:23
These are some usecases: - Subclassing immutable Built-in types - Returning another class - Allowing a single instance in your class - Partially Emulating collections.namedtuple
class Person:
def __new__(cls, *args, **kwargs):
print('The instance is being constructed')
return super().__new__(cls)
def __init__(self, value):
print('The instance is being initialized')
self.value = value