Skip to content

Instantly share code, notes, and snippets.

View icarovirtual's full-sized avatar
🐕
É o Bubu Show

Ícaro icarovirtual

🐕
É o Bubu Show
View GitHub Profile
@icarovirtual
icarovirtual / thumbfromvid.sh
Created May 31, 2019 18:47
Create a thumbnail from a frame of a video URL using ffmpeg
ffmpeg -itsoffset -4 -i "https://scontent.xx.fbcdn.net/v/t50.12441-16/61340994_435915640302058_n.mp4?_nc_cat=106&_nc_ht=scontent.xx&oh=19858915f178f4854a789438b3d9d24b&oe=5D5B3C92" -vcodec mjpeg -vframes 1 -an -f rawvideo test.jpg
@icarovirtual
icarovirtual / is_platypus.py
Last active July 7, 2019 14:33
guard clauses: bad example
# Sorry for the silly example
def is_platypus(self):
if self.is_mammal():
if self.has_fur():
if self.has_beak():
if self.has_tail():
if self.can_swim():
# It's a platypus!
return True
# Not a platypus
@icarovirtual
icarovirtual / is_platypus.py
Last active February 26, 2021 13:15
guard clauses: good example
def is_platypus(self):
# Not a platypus for everything below
if not self.is_mammal():
return False
if not self.has_fur():
return False
if not self.has_beak():
return False
if not self.has_tail():
return False
@icarovirtual
icarovirtual / guard_clauses.py
Last active August 11, 2019 18:01
guard clauses: longer example
def func_not_guarded(self, param):
if param == 'something':
self.counter += 1
if self.counter > 10:
self.reached_ten()
else:
if self.counter < 5:
self.has_not_reached_5()
else:
self.has_not_reached_5()
@icarovirtual
icarovirtual / copy_queryset_without_annotations.py
Last active October 19, 2019 13:38
copy_queryset_without_annotations: final investigation
def copy_queryset_without_annotations(original_qs):
# Create a new queryset based on the same model
optimized_qs = original_qs.model.objects.all()
# Copy the filters
optimized_qs.query.where = original_qs.query.where
optimized_qs.query.where_class = original_qs.query.where_class
return optimized_qs
@icarovirtual
icarovirtual / admin_mixins.py
Created October 19, 2019 13:04
copy_queryset_without_annotations: mixins
class PaginatorWithOptimizedCount(Paginator):
@cached_property
def count(self):
try:
# First usage of optimization function
return copy_queryset_without_annotations(self.object_list).count()
except (AttributeError, TypeError):
return len(self.object_list)
class Article(models.Model):
class Status(models.TextChoices):
DRAFT = '0', "Draft"
PUBLISHED = '1', "Published"
REMOVED = '2', "Removed"
status = models.CharField(choices=Status.choices, max_length=1)
class StatusFilter(admin.SimpleListFilter):
class StatusFilter(admin.SimpleListFilter):
# Change your lookups function and include the class method
def lookups(self, request, model_admin):
return self._lookups()
@classmethod
def _lookups(cls):
# Static choices that will be used in the forms
return Article.Status.choices
class BaseFilterForm(forms.Form):
def __init__(self, *args, **kwargs):
# Receive the filter instances from the view
filters = kwargs.pop('filters', [])
super(BaseFilterForm, self).__init__(*args, **kwargs)
for f in filters:
# Create dynamic form fields from the filter
self.fields[f.parameter_name] = \
forms.ChoiceField(label=f.title, choices=[('', "Select")] + f._lookups(), required=False)
class BaseFilterView:
# List of admin filters
filters = [StatusFilter]
def get_form_kwargs(self):
form_kwargs = super().get_form_kwargs()
# Pass the filters to the form
form_kwargs['filters'] = self.filters
return form_kwargs