Skip to content

Instantly share code, notes, and snippets.

@TohidN
TohidN / models.py
Created December 23, 2024 13:31
Dynamically remove element class of a django form fields.
for k, field in form.fields.items():
field.widget.attrs.update({"class": "special"})
if 'class' in field.widget.attrs:
classes = field.widget.attrs['class'].split()
classes = [cls for cls in classes if cls != 'is-valid']
field.widget.attrs['class'] = ' '.join(classes)
@TohidN
TohidN / models.py
Created July 25, 2024 14:25
Abstract Wagtail page model to allow HTMX partial page load
class HtmxPage(Page):
def get_context(self, request):
context = super().get_context(request)
context['base_template'] = "partial.html" if request.htmx else "base.html"
return context
class Meta:
abstract = True
@TohidN
TohidN / models.py
Last active July 19, 2024 07:36
Customize context for Wagtail page model
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
# Add extra variables and return the updated context. E.g.
#context['blog_entries'] = BlogPage.objects.child_of(self).live()
return context
@TohidN
TohidN / file_line_count.py
Created February 3, 2024 14:14
Get number of lines in file (comparing time of multiple functions)
import timeit
import csv
import pandas as pd
filename = '../datasets/data.tsv'
def talktime(filename, funcname, func):
print(f"# {funcname}")
t = timeit.timeit(f'{funcname}("{filename}")', setup=f'from __main__ import {funcname}', number = 100) / 100
@TohidN
TohidN / exception-descriptors.py
Last active February 16, 2023 08:53
Python: List of all default exception descriptors(methods, properties, meta detail) in python
# Python Exception - All exception object's default descriptions
try:
error
except Exception as e:
print("error: \t", e)
print("type: \t", type(e))
print("message: \t", repr(e))
print("error args: \t", e.args)
print("error class: \t", e.__class__)
print("class name: \t", e.__class__.__name__)
@TohidN
TohidN / dl.php
Last active February 16, 2023 08:53
PHP: Download tar(& tar.gz) file from web and extract it
<?php
/* Source file to be downloaded */
$source = "https://releases.wikimedia.org/mediawiki/1.30/mediawiki-1.30.0.tar.gz";
//$source = "http://www.question2answer.org/question2answer-latest.zip";
/* temporary zip file location for extraction */
$filename = 'file.tar.gz';
/* destination folder */
@TohidN
TohidN / import-imdb-dataset-pandas.py
Created April 10, 2017 15:19
Read IMDB's dataset files using Pandas as CSV files. this dataframes then can be merged using "movie" column.
import pandas as pd
dataset_path_movies = '/movies.list.gz'
dataset_path_keywords = '/keywords.list.gz'
dataset_path_genres = '/genres.list.gz'
dataset_path_ratings = '/ratings.list.gz'
print("Opening Keyword File.")
cols_keywords = ['movie', 'keyword']
@TohidN
TohidN / qa-external-users.php
Created January 7, 2017 22:09
Question2Answer-OSClass Single-Sign-On integration
<?php
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../');
exit;
}
require_once '../oc-load.php';
require_once '../oc-includes/osclass/helpers/hUsers.php';
@TohidN
TohidN / template.html
Created January 1, 2017 16:28
Django Flexible Pagination
{% if movies.has_other_pages %}
<div class="paginator-container">
<ul class="pagination">
{% if movies.has_previous %}
<li><a href="?page=1"><i class="fa fa-angle-double-left"></i></a></li>
<li><a href="?page={{ movies.previous_page_number }}"><i class="fa fa-angle-left"></i></a></li>
{% else %}
<li class="disabled"><a href="?page=1"><i class="fa fa-angle-double-left"></i></a></li>
<li class="disabled"><a href="?page=1"><i class="fa fa-angle-left"></i></a></li>
{% endif %}
@TohidN
TohidN / Django-user-pass
Last active January 1, 2017 16:32
Change Django's user password
# CMD
python manage.py shell
#SHELL
from django.contrib.auth.models import User
users = User.objects.all()
#get an id for user based on output
print(users)
user = users[0]