Did you know that you can apply
This is a simple class that sets a fake lesson in your class and insert a new lesson.
As you can see above we have used
#python #class #builtin #len #__len__
len
python function on your classes? Let's say you have a Lessons class
and when a use use len()
function on your class it should return number of lessons inside of your class. For our sample let's say we have the below class:class Lessons(object):
def __init__(self, lessons=0):
self.lessons = lessons
def insert(self, data):
# let's assume we have save data in database
self.lessons += 1
def __len__(self):
return self.lessons
This is a simple class that sets a fake lesson in your class and insert a new lesson.
std_lessons = Lessons(1)
print len(std_lessons) # output: 1
std_lessons.insert({})
print len(std_lessons) # output: 2
As you can see above we have used
len
on our class itself. In case you don't implemet __len__
on your class and use len()
on it, there will be an AttributeError
:len(std_no_len)
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: Lessons instance has no attribute '__len__'
__len__
is built-in class and can be implemented in different scenarios. There are tons of built-in functions that you can master and use to improve code quality and prove that you are professional.#python #class #builtin #len #__len__
One of the benefits of
One of the ways that executes the query is using iteration:
In the below ways your query will be evaluated (executed):
- repr
- len
- list (e.g.: `list(Entry.objects.all())`)
- bool
#python #django #querySet #query_set #evaluation
Django`'s `QuerySet
is that you can apply filters on it, slice it and move it around your classes without actually hitting the database until you do so.One of the ways that executes the query is using iteration:
for c in Course.objects.all():
print(c.title)
NOTE:
the first time you iterate over the query, it hits the database and get the result NOT in each iteration.In the below ways your query will be evaluated (executed):
- repr
- len
- list (e.g.: `list(Entry.objects.all())`)
- bool
#python #django #querySet #query_set #evaluation
Tech C**P
Usually when you buy something, you're asked whether your credit card number, phone number or answer to your most secret question is still correct. However, since someone could look over your shoulder, you don't want that shown on your screen. Instead, we…
Tech C**P
Count the number of Duplicates Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both…
Tech C**P
A Narcissistic Number is a number which is the sum of its own digits, each raised to the power of the number of digits in a given base. Here we will restrict ourselves to decimal (base 10). For example, take 153 (3 digits): 1^3 + 5^3 + 3^3 = 1 + 125…
Tech C**P
from math import pow def narcissistic(value): digits = map(int, list(str(value))) power = len(digits) final_res = 0 for digit in digits: final_res += pow(digit, power) return int(value) == int(final_res) #python #solution
Is there a way to create ObjectID from an INT in
#mongodb #objectid #pymongo #python #bson #int
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