class MyList(list):
RAISE = object()
def intersect(self, dct, default=RAISE):
for item in self:
if all(item[k] == v for k, v in dct.items()):
return item
if default is self.RAISE:
raise ValueError("Couldn't find")
else:
return default
In [6]: l = MyList([{'city': 'moscow', 'name': 'Roman'}])
In [7]: l.intersect({'city': 'moscow'})
Out[7]: {'city': 'moscow', 'name': 'Roman'}
In [8]: l.intersect({'city': 'tomsk'})
ValueError: Couldn't find
from django.db import connection
print(connection.queries)
чтобы иметь рабочий сайт и не парится, что в нём есть дыры
через какое-то время добавить в него постджр БД или JSON.
общим названиям
fax = soup.find(id="ctl0_left_fax")
if fax:
another_element = fax.find(class_='some_class')
if another_element:
another_one = another_element.find(class_='some_another_class')
if another_one:
do_something()
fax = soup.find(id="ctl0_left_fax")
another_element = fax and fax.find(class_='some_class')
another_one = another_element and another_element.find(class_='some_another_class')
if another_one:
do_something()
letters = 'абвгд'
Model.objects.filter(reduce(
lambda x,y: (isinstance(x, Q) and x or Q(name__istartswith=x)) | Q(name__istartswith=y),
letters
))
# Или
from operator import or_
Model.objects.filter(
reduce(or_, (Q(name__istartswith=l) for l in letters))
)
class Meta:
managed = False
def save(self, *args, **kwargs):
raise ValueError('Read-only database')
def delete(self, *args, **kwargs):
raise ValueError('Read-only database')
class CardserverQuerySet(models.QuerySet):
def create(self, *args, **kwargs):
raise ValueError('Read-only database')
def update(self, *args, **kwargs):
raise ValueError('Read-only database')
def delete(self, *args, **kwargs):
raise ValueError('Read-only database')
def bulk_create(self, *args, **kwargs):
raise ValueError('Read-only database')
def get_or_create(self, *args, **kwargs):
raise ValueError('Read-only database')
def update_or_create(self, *args, **kwargs):
raise ValueError('Read-only database')
class CardserverManager(models.Manager):
def get_queryset(self):
return CardserverQuerySet(self.model, using='remote')