Skip to content

Instantly share code, notes, and snippets.

@paulozullu
Last active April 16, 2021 12:19
Show Gist options
  • Save paulozullu/e46e20dc1b430146d7fd27edbd315f62 to your computer and use it in GitHub Desktop.
Save paulozullu/e46e20dc1b430146d7fd27edbd315f62 to your computer and use it in GitHub Desktop.
Python and Django tips

1 - Check if an element exists in a list of dictionnaires

my_list = [
    {'main_color': 'red', 'second_color':'blue'},
    {'main_color': 'yellow', 'second_color':'green'},
    {'main_color': 'yellow', 'second_color':'blue'},
]

if not any(my_dict['main_color'] == 'red' for my_dict in my_list):
    # does not exist

# The part in parentheses is a generator expression that returns True for each dictionary that has the key-value pair you are looking for, otherwise False.

# If the key could also be missing the above code can give you a KeyError. You can fix this by using get and providing a default value.

if not any(my_dict.get('main_color', None) == 'red' for my_dict in my_list):
    # does not exist

2 - OR in queryset filter

from django.db.models import Q
User.objects.filter((Q(income__gte=5000) | Q(income__isnull=True)),field1=1234)

3 - Case insensitive search in Django with MongoDB in a DictField type field

# ---- main.py ----
from models import Team

bahia = Team()
bahia.name = "E.C. Bahia"
bahia.others = {"title": "Ninguém nos Vence em Vibração!!!"}
bahia.save()

vicetoria = Team()
vicetoria.name = "E.C. Vicetoria"
vicetoria.others = {"title": "Vice de tudo!"}
vicetoria.save()

# Search (case insensitive), for all teams that have the field "others.title" like "vence".
teams = Team.objects.raw_query({"others.title": {"$regex" : "vence", "$options": "i"}})

# ---- models.py ----
from django.db import models
from djangotoolbox.fields import DictField

class Team(models.Model):
    name = models.CharField(max_length=50)
    others = DictField()

4 - Teste de tradução em python (translation)

# -*- coding: utf-8 -*-
import csv
from textblob import TextBlob
from pymongo import MongoClient

def start():
    
    lines = []
    count = 0
    
    with open("text_translate.csv") as csvfile:
        reader = csv.DictReader(csvfile, delimiter=';')
            
        for row in reader:
            st_text_parent = unicode(row['st_text_parent'],'utf-8')
            st_text = unicode(row['st_text'], 'utf-8')
            data = translate(st_text_parent, st_text)
            
            mongo['translate'].insert({'id': row['ID'], 'st_text_parent': st_text_parent.encode('utf-8','ignore'),
            'st_text_parent_trans': data['st_text_parent'], 'st_text': st_text.encode('utf-8','ignore'), 
            'st_text_trans': data['st_text']})
            
            count += 1
            print count
            
            if count==15:
                break

def translate(st_text_parent, st_text):
    
    try:
        blob = TextBlob(st_text_parent)
        st_text_parent_translated = str(blob.translate(to="en"))
    except:
        st_text_parent_translated = ''
        
    try:
        blob = TextBlob(st_text)
        st_text_translated = str(blob.translate(to="en"))
    except:
        st_text_translated = ''

    data = {'st_text_parent': st_text_parent_translated, 'st_text': st_text_translated}
    
    return data

''' Create a connection to MongoDB '''
def connect_mongo(ip, db):
    
    print "Conectando ao servidor %s..." %(ip)
    try:
        ip_mongo_server = ip
        client = MongoClient(ip, 27017)
        mongo = client[db]
        print "Conexão bem sucedida!"
    except:
        print "Não foi possível conectar!"
        exit(1)
        
    return mongo

# Main function
if __name__ == '__main__':
    mongo = connect_mongo('127.0.0.1', 'local')
    start()

5 - Get the biggest value of (not all) dictionary keys in Python and give the pair (key, value)

myDict = {"a":0,"b":5,"c":3,"x":4, "y":3, "w":2, "z": 1}
newDict = {key:myDict[key] for key in ["x","w","y","z"]}
key, value = max(newDict.iteritems(), key=lambda x:x[1])

# Result: key = x, value = 4 

6 - Get unique sorted values from list in Python

my_list = [2,2,2,2,2,2,1,1,10,5,7,7,4,4,4,4,4,3,3,0,99,5,4,3,2,6,5]
wanted_resut = sorted(set(my_list))

# Result: wanted_result = [0, 1, 2, 3, 4, 5, 6, 7, 10, 99]

7 - Export json to csv


import csv

def json_to_csv(json_, filename):
    keys = json_[0].keys()
    
    with open(filename, 'wb') as output_file:
        dict_writer = csv.DictWriter(output_file, keys)
        dict_writer.writeheader()
        dict_writer.writerows(json_)

if __name__ == "__main__":
    print "Executando..."
    words = [{"id":1, "name":"Paulo"}, {"id":2,"name":"zOiO"}]
    json_to_csv(words, "words.csv")
     
    print "Script Finalizado!"

8 - Debug Django Template (writes data to a text area for easy read)

<textarea onclick="this.focus();this.select()" style="width: 100%;"> {% filter force_escape %} {% debug %} {% endfilter %}</textarea>

9 - Distinct in Django with MongoDB

# ----models.py----
from django_mongodb_engine.contrib import MongoDBManager

class MyClass(models.Model):
    objects = MongoDBManager()
    field1 = models.IntegerField()
    field2 = models.CharField()

# ----views.py----
from models import MyClass

my_values = MyClass.objects.filter(field1=1).distinct('field2')

10 - Convert integer timestamp to datetime

import datetime
your_timestamp = 1331856000000
date = datetime.datetime.fromtimestamp(your_timestamp)

11 - Bypass psycopg2 error when getting invalid dates in the cursor

from django.utils import timezone
import pytz
from datetime import datetime, timedelta
import psycopg2, psycopg2.extras


conn_string = "host='my_host' dbname='my_dbname' user='user' password='pwd' port='1234'"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)

query = "SELECT to_char(field1,'dd/mm/yyyy-hh:mm') AS my_date FROM my_schema.my_table WHERE field2=%s"
args = [arg1,]
cursor.execute(query, args)
result = cursor.fetchone()

try:
    birthday = timezone.make_aware(datetime.strptime(result["my_date"], '%d/%m/%Y-%H:%M'), timezone=pytz.timezone('America/Bahia'))
except (pytz.NonExistentTimeError, ValueError):
    try:
        birthday = timezone.make_aware(datetime.strptime(result["my_date"], '%d/%m/%Y-%H:%M') + timedelta(hours=1), timezone=pytz.timezone('America/Bahia'))
    except ValueError:
        birthday = None

12 - Push(add) object to an array field

# The model must have objects = MongoDBManager()
id = 1234
name='my_name'
result = MyModel.objects.raw_update({'name': name, 'my_list_field.id': {'$ne': id}}, {'$push':{'my_list_field':my_dict_object}})

13 - Convert object to dict

my_obj = MyModel.objects.filter(id=1234).values()[0]

If object has another Objects in its fields, we need to convert them:

my_obj = MyModel.objects.filter(id=1234).values()[0]
my_obj["field"] = my_obj["field"][1]

14 - Getting random objects from a queryset in Django

import random
count = MyClass.objects.all().count()
slice = random.random() * (count - 10)
MyClass.objects.all()[slice: slice+10]

15 - Undo makemigration

You can revert by migrating to the previous migration.

For example, if your last two migrations are:

0010_previous_migration
0011_migration_to_revert

Then you would do:

./manage.py migrate my_app 0010_previous_migration 

You can then delete migration 0011_migration_to_revert.

If you're using Django 1.8+, you can show the names of all the migrations with

./manage.py showmigrations my_app

To reverse all migrations for an app, you can run:

./manage.py migrate my_app zero

16 - Django queryset where a field exact values are in a list

Make a query that returns all rows where a field values exist in a list. It should be case insensitive, but the values should be exact with the ones in the list:

my_list = ["Others", "Entertainment"]
results = MyObject.objects.filter(subject__iregex='^({})$'.format('|'.join(my_list)))

17 - Django queryset where a field values are in a list

Make a query that returns all rows where a field values exist in a list. It should be case insensitive.

my_list = ["Others", "Entertainment"]
results = MyObject.objects.filter(subject__iregex='({})'.format('|'.join(my_list)))

18 - Get distinct values of a key from a list of dicts

distinct_values = {my_dict["desired_value"] for my_dict in my_list}

19 - Count the numberof ocurrences of a value in a key in a list of dicts

count = sum(my_dict.get("my_key") == "value_to_find" for my_dict in my_list)

20 - Get from a list at most two dicts where a certain ket has a given value

from itertools import islice

my_list = [
           {'a': 1, 'b': 3, 'c': 0}, 
           {'a': 1, 'b': 5, 'c': -10}, 
           {'a': 1, 'b': 1, 'c': 1}, 
           {'a': 2, 'b': 0, 'c': 0}, 
           {'a': 1, 'b': 5, 'c': 0}
]

a, b = islice((d for d in my_list if d.get('a') == 1), 2)

21 - Date tips

21.1 - String to date

d1 = datetime.datetime.strptime('01/03/1979', '%m/%d/%Y').date()

21.2 - Today as date

today = datetime.date.today()

21.3 - Today as string

d1 = datetime.datetime.today().strftime('%m/%d/%Y') 

22 - Find a dict in a list of dictionaires

dicts = [
    {"name": "Tom", "age": 10},
    {"name": "Mark", "age": 5},
    {"name": "Pam", "age": 7}
]
match = next((item for item in dicts if item["name"] == "Pam"), None) 

23 - Convert string to unix format date

date_obj = strptime(date_str, '%Y-%m-%d %H:%M:%S.%f')

24 - Difference in seconds between two dates

import datetime as dt

a = dt.datetime(2013,12,30,23,59,59)
b = dt.datetime(2013,12,31,23,59,59)

(b-a).total_seconds()

25 - Fix from: can't read /var/mail/ in Python scripts

Add to the first line of the script

#! /usr/bin/python

26 -Error on import package from parent project

try:
    from package.file import function
except ImportError:
    from sys import path as pylib #im naming it as pylib so that we won't get confused between os.path and sys.path 
    import os
    pylib += [os.path.abspath('../../../'+os.curdir)] # path to the project
    from package.file import function

27 - Calculate the time of execution of a script or function

from timeit import default_timer as timer

start = timer()
# ...
end = timer()
print(end - start) # Time in seconds, e.g. 5.38091952400282

28 - Get datetime like timestamp

now = int(datetime.datetime.now().strftime("%s"))

29 - Print unicode list

msg = repr([x.encode(sys.stdout.encoding) for x in lst]).decode('string-escape')

30 - Send json from ajax to Django view via AJAX

// javascript
arr = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
var my_list = JSON.stringify(arr)

# python
json_data = request.GET.get("my_list")
python_list = _json.loads(json_data)

31 - Calculate age

from datetime import date

def calculate_age(born):
    today = date.today()
    return today.year - born.year - ((today.month, today.day) < (born.month, born.day))

32 - Force installation of not found module

pip install <module-name> --force-reinstall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment