Skip to content

Instantly share code, notes, and snippets.

@maxbellec
maxbellec / update_create_mixin.py
Created March 30, 2023 13:42
Add a Django Rest Framework mixin for a GenericViewSet to replace create with an update_or_create method
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import status
from rest_framework.mixins import CreateModelMixin
from rest_framework.response import Response
class UpdateOrCreateModelMixin(CreateModelMixin):
"""
Update or create a model instance.
"""
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from gensim.corpora.wikicorpus import WikiCorpus
from gensim.models import TfidfModel
wiki = WikiCorpus.load('wiki.corpus')
tfidf = TfidfModel.load("wiki.gensim.tfidfmodel")
# transform sentence in bow
sentence = "hi my name is"
sentence = wiki.dictionary.doc2bow(sentence.lower().split()) # [(662762, 1), (1271346, 1), (1756375, 1), (1770642, 1)]
# tfidf for that sentence
@maxbellec
maxbellec / word2vec_tf_idf_from_wikipeida.py
Last active June 22, 2022 14:36
Create Word2Vec from wikipedia with gensim
import multiprocessing
from gensim.corpora.wikicorpus import WikiCorpus
from gensim.models.word2vec import Word2Vec
from gensim.models import TfidfModel
# logging is important to get the state of the functions
import logging
logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s')
logging.root.setLevel(level=logging.INFO)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@maxbellec
maxbellec / get_lat_lon_exif_pil.py
Last active December 5, 2023 12:39 — forked from erans/get_lat_lon_exif_pil.py
Get Latitude and Longitude from EXIF using PIL
import PIL.Image
get_float = lambda x: float(x[0]) / float(x[1])
def convert_to_degrees(value):
d = get_float(value[0])
m = get_float(value[1])
s = get_float(value[2])
return d + (m / 60.0) + (s / 3600.0)
def get_lat_lon(info):