-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
81 lines (69 loc) · 2.32 KB
/
models.py
File metadata and controls
81 lines (69 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import csv
import json
from dataclasses import dataclass, field, asdict
from typing import List, Optional
from datetime import datetime
from config import CSV_FIELDS
@dataclass
class RentalListing:
property_id: str
rental_id: str = ""
property_type: str = ""
address: str = ""
city: str = ""
state: str = ""
zip_code: str = ""
price_min: Optional[int] = None
price_max: Optional[int] = None
bedrooms_min: Optional[int] = None
bedrooms_max: Optional[int] = None
bathrooms_min: Optional[float] = None
bathrooms_max: Optional[float] = None
sqft_min: Optional[int] = None
sqft_max: Optional[int] = None
lat: Optional[float] = None
lon: Optional[float] = None
url: str = ""
description: str = ""
feed_source: str = ""
last_updated: str = ""
date_available: str = ""
num_available_units: Optional[int] = None
date_scraped: str = field(
default_factory=lambda: datetime.utcnow().isoformat() + "Z"
)
def to_dict(self) -> dict:
return asdict(self)
@dataclass
class ListingCollection:
items: List[RentalListing] = field(default_factory=list)
_seen_ids: set = field(default_factory=set, repr=False)
def add(self, listing: RentalListing) -> bool:
if listing.property_id and listing.property_id in self._seen_ids:
return False
if listing.property_id:
self._seen_ids.add(listing.property_id)
self.items.append(listing)
return True
def add_many(self, listings: List[RentalListing]) -> int:
return sum(1 for item in listings if self.add(item))
def __len__(self) -> int:
return len(self.items)
def __iter__(self):
return iter(self.items)
def to_csv(self, filepath: str) -> None:
if not self.items:
return
with open(filepath, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=CSV_FIELDS)
writer.writeheader()
for item in self.items:
writer.writerow(item.to_dict())
def to_json(self, filepath: str) -> None:
with open(filepath, "w", encoding="utf-8") as f:
json.dump(
[item.to_dict() for item in self.items],
f,
indent=2,
ensure_ascii=False,
)