SPB Git

Repository Public

hiver.py 7.6 KB 195 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project          : Lou·Ka9# File             : louka/hiver.py10# Module           : louka11# Repository       : https://git.spboucher.ai/lou-ka12# Created          : 2026-08-2413# Last Modified    : 2026-08-2414#15# =============================================================================16# AUTHOR & PROFESSIONAL AFFILIATION17# =============================================================================18#19# Author           : Simon-Pierre Boucher20# Position         : Professor of Finance21# Institution      : Université du Québec en Outaouais (UQO)22# Department       : Département des sciences administratives23# Organization     : SP Boucher24#25# Website          : https://spboucher.ai26# Git              : https://git.spboucher.ai27#28# =============================================================================29# OFFICIAL CONTACT DIRECTORY30# =============================================================================31#32# General          : info@spboucher.ai33# Professional     : contact@spboucher.ai34# Direct           : simon@spboucher.ai35# Research         : research@spboucher.ai36# Development      : dev@spboucher.ai37# Support          : support@spboucher.ai38# Security/Admin   : admin@spboucher.ai39# Notifications    : notifications@spboucher.ai40#41# Preferred Contact: contact@spboucher.ai42#43# =============================================================================44# COPYRIGHT & INTELLECTUAL PROPERTY45# =============================================================================46#47# Copyright (c) 202648# Simon-Pierre Boucher49#50# All rights reserved where applicable.51#52# This file is governed by the applicable project license,53# copyright notices, institutional agreements and relevant law.54#55# Unless expressly authorized by the applicable license or56# rights holder, copying, redistribution, sublicensing,57# publication and commercial exploitation are not permitted.58#59# Existing third-party rights and notices remain unaffected.60#61# =============================================================================62# CONFIDENTIALITY & SECURITY63# =============================================================================64#65# Classification   : Public66# Distribution     : Public repository; use is governed by the project license67# Security Contact : admin@spboucher.ai68#69# Access to this source does not independently grant permission70# to redistribute or disclose confidential materials.71#72# Do not embed credentials, private keys, access tokens,73# certificates or confidential personal information.74#75# =============================================================================76# LICENSE & MAINTENANCE77# =============================================================================78#79# License          : None specified (no license file in this repository)80# Maintainer       : Simon-Pierre Boucher81# Technical Contact: dev@spboucher.ai82#83# =============================================================================84# END OF SP BOUCHER SOURCE HEADER85# =============================================================================8687# -----------------------------------------------------------------------------88# Lou-Ka — Agrégateur de logements à louer (province de Québec)89# Auteur : Simon-Pierre Boucher — contact@spboucher.ai90# hiver.py : « Vie quotidienne en hiver » — indicateur 0-100 pensé pour le91#            Québec : peut-on vivre son quotidien À PIED quand il fait -20 °C92#            et que les trottoirs sont enneigés?93#94# Calcul déterministe et explicable à partir des données DÉJÀ collectées95# (poi_cache — plus proche commodité par catégorie, OpenStreetMap) :96#   épicerie, pharmacie, arrêt de bus/métro, dépanneur. Chaque sous-score est97#   une décroissance linéaire de la distance de marche (vol d'oiseau × 1,3).98# Pas de données de pente/exposition disponibles -> non pris en compte, et la99# méthodologie l'assume. Sans données POI : aucun score (jamais inventé).100# -----------------------------------------------------------------------------101from __future__ import annotations102103import json104105from . import db106107VERSION = "hiver-1.0"108DETOUR = 1.3          # vol d'oiseau -> distance de marche (même facteur que POI)109VITESSE_M_MIN = 80.0  # 4,8 km/h110111# (catégorie, libellé, poids, distance pleine note (m), distance note nulle (m))112CRITERES = [113    ("epicerie", "Épicerie", 0.35, 300, 1500),114    ("pharmacie", "Pharmacie", 0.20, 300, 1500),115    ("bus", "Arrêt de bus", 0.25, 150, 900),116    ("metro", "Métro", 0.05, 400, 1800),117    ("depanneur", "Dépanneur", 0.15, 200, 1000),118]119120CLASSES = [(80, "très pratique"), (60, "pratique"), (40, "exigeant")]121CLASSE_MIN = "difficile"122123124def _sous_score(dist_m: float, plein: float, nul: float) -> float:125    d = dist_m * DETOUR126    if d <= plein:127        return 100.0128    if d >= nul:129        return 0.0130    return 100.0 * (nul - d) / (nul - plein)131132133def score(lat: float | None, lng: float | None, con=None) -> dict | None:134    """Score hiver de l'immeuble (via poi_cache) — None si données absentes."""135    if lat is None or lng is None:136        return None137    own = con is None138    if own:139        con = db.connect()140    try:141        key = f"{round(lat, 4)},{round(lng, 4)}"142        row = con.execute("SELECT pois FROM poi_cache WHERE coord_key=?",143                          (key,)).fetchone()144        if row is None:145            return None146        try:147            pois = {p["cat"]: p for p in json.loads(row["pois"] or "[]")}148        except (ValueError, TypeError):149            return None150        if not pois:151            return None152153        total = 0.0154        poids_total = 0.0155        detail = []156        for cat, label, poids, plein, nul in CRITERES:157            p = pois.get(cat)158            if p is None:159                # métro absent = normal hors Montréal : critère simplement omis160                if cat == "metro":161                    continue162                s = 0.0163                item = {"critere": label, "score": 0, "distance_m": None,164                        "note": "aucun repéré à distance de marche"}165            else:166                s = _sous_score(p["dist_m"], plein, nul)167                item = {"critere": label, "score": round(s),168                        "distance_m": p["dist_m"],169                        "minutes": max(1, round(p["dist_m"] * DETOUR170                                                / VITESSE_M_MIN)),171                        "nom": p.get("name")}172            total += s * poids173            poids_total += poids174            detail.append(item)175        if poids_total == 0:176            return None177        note = round(total / poids_total)178        classe = CLASSE_MIN179        for seuil, nom in CLASSES:180            if note >= seuil:181                classe = nom182                break183        return {184            "score": note, "classe": classe, "detail": detail,185            "statut": "calculated", "version": VERSION,186            "methode": ("commodités essentielles à distance de marche "187                        "(épicerie, pharmacie, bus/métro, dépanneur — "188                        "OpenStreetMap), décroissance linéaire de la distance "189                        "de marche estimée (vol d'oiseau × 1,3). Pente et "190                        "exposition non disponibles : non prises en compte."),191        }192    finally:193        if own:194            con.close()195
Files main
Menu

Theme