Skip to content

Instantly share code, notes, and snippets.

import os.path
from dataclasses import dataclass
from abc import ABC, abstractmethod
from typing import List
class Personne:
def __init__(self, nom, prenom):
self._nom = nom
self._prenom = prenom
@zulzeen
zulzeen / password_generator.py
Last active May 11, 2021 07:30
A random password generator
import math
import random
import string
import argparse
def generate_password(nb_of_char, min_special=0):
password = ''
for i in range(nb_of_char, 0, -1):
if min_special >= i:
@zulzeen
zulzeen / views.py
Last active November 20, 2020 08:40
Create in-memory zip file with multiple in-memory CSVs to serve with Django
from django.http import HttpResponse
import io
import csv
from zipfile import ZipFile
def get_zip(request):
# We generate rows for our different files
first_csv_rows = generate_rows_for_first_csv()
@zulzeen
zulzeen / create_requirement.py
Last active November 23, 2018 10:30
List all uninstalled import and add them to a requirement.txt file
import os
import importlib
all_imports = set()
for file in [filename for filename in os.listdir(os.path.curdir) if filename.endswith('.py')]:
with open(file, 'r') as current_file:
for line in current_file.readlines():
if line.startswith('import') or line.startswith('from'):
all_imports.add(line.split()[1])
@zulzeen
zulzeen / cache.py
Created October 25, 2018 14:37
A simple containerized redis controlled by Python
import argparse
import docker
def run_redis(client):
try:
container = client.containers.get('redis_cache')
except docker.errors.NotFound:
container = client.containers.run('redis', ports={'6379/tcp' : 6379}, detach = True, name='redis_cache')
else:
if container.status == 'exited':
@zulzeen
zulzeen / pdf_convert.py
Last active March 23, 2018 13:36
Convert PDF to text
# adapted from https://www.binpress.com/tutorial/manipulating-pdfs-with-python/167
# converts pdf, returns its text content as a string
# uses pdfminer.six as a library ; works with Python 3.6
from io import StringIO
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
def convert(fname, pages=None):

Keybase proof

I hereby claim:

  • I am zulzeen on github.
  • I am zulzeen (https://keybase.io/zulzeen) on keybase.
  • I have a public key whose fingerprint is 7788 8C2F C06B F73C 7F53 1357 6F17 FBEA 16A4 0042

To claim this, I am signing this object:

@zulzeen
zulzeen / gist:cbd5a09e3f4e5608db42
Last active March 19, 2016 22:54
List comprehension to generate a list from a dict containing each key its value times (python)
bucket = {"red_ball": 1, "blue_ball": 2, "green_ball": 3}
balls = [k for k in bucket.keys() for _ in range(bucket.get(k))]
# Returns ['blue_ball', 'blue_ball', 'green_ball', 'green_ball', 'green_ball', 'red_ball']
@zulzeen
zulzeen / Onename
Last active February 22, 2019 16:20
Verifying my Blockstack ID is secured with the address 1NBpELsNa3Q6toNRFcUamrPqjdq7tb6VE6 https://explorer.blockstack.org/address/1NBpELsNa3Q6toNRFcUamrPqjdq7tb6VE6