Skip to content

Instantly share code, notes, and snippets.

View xtornasol512's full-sized avatar

Jesús Alvarado Garzón xtornasol512

View GitHub Profile
@xtornasol512
xtornasol512 / converting_files.py
Created April 12, 2024 01:54
Converting utf files to certain encode format this case cp1252
import csv
import unicodedata
import sys
def normalize_and_convert(input_filepath, output_filepath):
""" The function manage to open a file with utf and converting safely into cp1252,
make adjustments to accept more formats """
# Open the input file with UTF-8 encoding
with open(input_filepath, mode='r', encoding='utf-8', newline='') as infile:
@xtornasol512
xtornasol512 / create_file.sh
Created December 6, 2023 00:06
Create files in console
# Copy this lines on <BASE_PROJECT_PATH>
echo 'Some lines' > some_file.some_extension
echo 'some other lines' >> some_file.some_extension
@xtornasol512
xtornasol512 / gist:ebc8c677f7a55fc8c263cc3a0fb71f13
Created January 13, 2023 19:03
For public images purpose
HEllo
@xtornasol512
xtornasol512 / mailgun_wrapper.py
Last active October 20, 2022 03:55
A useful mailgun wrapper to send emails via api
import requests
from os import getenv
class MailgunWrapper:
""" Principal mailgun app """
PRIV_KEY = getenv("MAILGUN_PRIV_KEY", "")
FROM_EMAIL = getenv("MAILGUN_FROM_EMAIL", "")
def __init__(self, *args, **kwargs):
@xtornasol512
xtornasol512 / pdf_with_latexmk.py
Last active October 20, 2022 03:40
A useful function to call latexmk on python
from tempfile import TemporaryDirectory as TempD
import subprocess # nosec
from os import path
import base64
import shlex
def generate_tex_pdf(tex_string):
""" Generic Function to convert a template string to pdf """
@xtornasol512
xtornasol512 / scape_functions.py
Created October 20, 2022 03:17
Scape functions for latex, compatible only with python 3+
import re
import unicodedata
def escape_text(s):
# Convert from python escape text to latex format.
"""^*¨ÑLIOPL:_:(/&%&()=(/&WQ·W$ERTY +´^*Ñ´plqj"""
# This block is omited
s = s.replace("·", "")
s = s.replace("¨", "")
@xtornasol512
xtornasol512 / add_permisions.py
Created February 21, 2022 17:09
Add permissions to a group, using Django
""" Little snippet for looking for permissions and add them to a group """
from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission
from <your-model-folder>.models import <MODEL>, <MODEL-N>
gp = Group.objects.get(name="<GROUP_NAME>")
qs = Permission.objects.filter(codename__icontains="<FOLDER-NAME>")
@xtornasol512
xtornasol512 / utils.md
Created October 27, 2021 17:19
Drop tables using django and sql commands

DROP A TABLE django

python manage.py dbshell

\dt drop table

python manage.py migrate --fake <app_name> zero

find . -path "/migrations/.py" -not -name "init.py" -delete

@xtornasol512
xtornasol512 / adapter_example.py
Created May 23, 2019 01:38
Mas alla de las guias de estilo | Talk 22 May , 2019
"""
Pythonic means "coding beautifully in harmony with the language to get the
maximum benefits from python"
Learn to recognize non-pythonic APIs and to recognize good code.
Dont get distracted by PEP8. Focus first on Pythonic vs NonPythonic(P vs NP)
When needed, write an adapter class to convert from the former to the latter
==========================================================
"""
# Send a file by email with S3
message = EmailMessage(subject, body, from_email, bcc=recipient_list)
message.attach(FILENAME, mymodel.myfilefield.read())