Skip to content

Instantly share code, notes, and snippets.

@partner0
Last active May 8, 2024 12:27
Show Gist options
  • Save partner0/175eb8da3e80ac2cb271c2dcedf42154 to your computer and use it in GitHub Desktop.
Save partner0/175eb8da3e80ac2cb271c2dcedf42154 to your computer and use it in GitHub Desktop.
from datetime import date, datetime, timedelta
from tqdm import tqdm
from functools import reduce
DAYS_OUT = 180
def get_days_opened(clds: list[bool]) -> list[int]:
days_opened = [-1] * len(clds)
days_opened_cnt = 0
if len(clds) <= 1:
return
for cnt in range(len(clds) - 1, -1, -1):
if clds[cnt]:
days_opened_cnt += 1
days_opened[cnt] = days_opened_cnt
else:
days_opened_cnt = 0
days_opened[cnt] = -1
return days_opened
def get_prc_days_below_min(cl: ChannelListing) -> float:
days_below_min = 0
available_days = 0
clds = cl.dates.filter(date__gte=date.today(), date__lte=date.today() + timedelta(days=DAYS_OUT)).all().order_by('date')
clds_avail = list(map(lambda x: x.availability == 'available', clds))
clds_min_stays = list(map(lambda x: x.min_stay_scraped, clds))
clds_days_opened = get_days_opened(clds_avail)
for cnt in range(len(clds_days_opened)):
if clds_days_opened[cnt] == -1:
continue
available_days += 1
if not clds_min_stays[cnt]:
continue
if clds_days_opened[cnt] < clds_min_stays[cnt]:
days_below_min += 1
return days_below_min / available_days if available_days > 0 else 0
def count_instances(data: list[str]) -> dict[str, int]:
return reduce(lambda acc, x: {**acc, x: acc.get(x, 0) + 1}, data, {})
cls = ChannelListing.objects.non_deleted().enabled().filter(posted_prices_at__gte=date.today() - timedelta(days=2))\
.prefetch_related('master_listing')\
.defer(*ChannelListing.deferable_fields(), *Listing.deferable_fields(prefix='master_listing__'))\
.filter(master_listing__orphan_min_stay__isnull=True)\
.filter(master_listing__orphan_min_stay_buffer__isnull=True)
#cls = ChannelListing.objects.filter(title='Fidalsa Moon Dunes').defer(*ChannelListing.deferable_fields())
print(cls.query)
total_items = cls.count()
cls_selected = []
with tqdm(total=total_items) as pbar:
for cl in cls.in_batches(size=100):
prc_days_below_min = get_prc_days_below_min(cl)
if prc_days_below_min > 0.05:
cls_selected.append([cl.id, cl.master_listing_id, prc_days_below_min])
pbar.update(1)
with open(f'report_{datetime.now().strftime("%Y-%m-%d--%H-%M")}', 'w') as output:
output.write('Channel Listing ID, Master Listing ID, Prc Days Below Min\n')
for cl in cls_selected:
output.write(f'{cl[0]}, {cl[1]}, {cl[2]}\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment