Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Mastering #Ansible Video #Course
Media is too big
VIEW IN TELEGRAM
#ansible part2 - Ansible requirements
In Django unittests you can use fixtures in order to create dummy data. To create fixtures you can use django dumpdata command to dump a specific data:

python3 manage.py dumpdata --indent 4 YOUR_APP.YOUR_TABLE > output_data.json


This is how you can dump data and use it as your data backend for unittests.

#django #python3 #dumpdata #unittest
Working Effectively with Legacy Code - Michael C. Feathers.epub
3.8 MB
Working effectively with legacy code
#ebook #pdf #epub
Is there a way to create ObjectID from an INT in MongoDB?

import bson

def object_id_from_int(n):
s = str(n)
s = '0' * (24 - len(s)) + s
return bson.ObjectId(s)

def int_from_object_id(obj):
return int(str(obj))

n = 12345
obj = object_id_from_int(n)
n = int_from_object_id(obj)
print(repr(obj)) # ObjectId('000000000000000000012345')
print(n) # 12345


#mongodb #objectid #pymongo #python #bson #int
To get an expiration time of a redis key you can use TTL like below:

TTL "YOUR_KEY_NAME"


To read more about setting expiration time and or other options:
- https://redis.io/commands/expire

#redis #expiration_time #expire #ttl
What does select_related do in Django?

select_related does a join in case needed on the DB side and reduce query counts. Let's look at an example:

# Hits the database.
e = Entry.objects.get(id=5)

# Hits the database again to get the related Blog object.
b = e.blog


In the above code 2 queries are issued in DB side. First it gets Entry record and then blog is fetched from DB when e.blog is called. And here’s select_related lookup:

# Hits the database.
e = Entry.objects.select_related('blog').get(id=5)

# Doesn't hit the database, because e.blog has been prepopulated
# in the previous query.
b = e.blog


You can follow foreign keys in a similar way to querying them. If you have the following models:

from django.db import models

class City(models.Model):
# ...
pass

class Person(models.Model):
# ...
hometown = models.ForeignKey(
City,
on_delete=models.SET_NULL,
blank=True,
null=True,
)

class Book(models.Model):
# ...
author = models.ForeignKey(Person, on_delete=models.CASCADE)


Then a call to Book.objects.select_related('author__hometown').get(id=4) will cache the related Person and the related City:

# Hits the database with joins to the author and hometown tables.
b = Book.objects.select_related('author__hometown').get(id=4)
p = b.author # Doesn't hit the database.
c = p.hometown # Doesn't hit the database.

# Without select_related()...
b = Book.objects.get(id=4) # Hits the database.
p = b.author # Hits the database.
c = p.hometown # Hits the database.


#python #django #select_related #join #database #models