MAP mieux que Object

 

MAP à fond la forme !

 

const films = [

  { titre: "Inception", genre: "SF", note: 9, annee: 2010 },
  { titre: "Titanic", genre: "Romance", note: 8, annee: 1997 },
  { titre: "Interstellar", genre: "SF", note: 10, annee: 2014 },
  { titre: "Le Roi Lion", genre: "Animation", note: 9, annee: 1994 },
  { titre: "Avatar", genre: "SF", note: 7, annee: 2009 },
  { titre: "Gladiator", genre: "Action", note: 8, annee: 2000 },
  { titre: "Matrix", genre: "SF", note: 9, annee: 1999 },
  { titre: "Blade Runner 2049", genre: "SF", note: 8, annee: 2017 },
  { titre: "La La Land", genre: "Romance", note: 8, annee: 2016 },
  { titre: "Shrek", genre: "Animation", note: 8, annee: 2001 },
  { titre: "Dune", genre: "SF", note: 8, annee: 2021 },
  { titre: "Forrest Gump", genre: "Romance", note: 10, annee: 1994 },
  { titre: "Mad Max: Fury Road", genre: "Action", note: 9, annee: 2015 },
  { titre: "Seven", genre: "Thriller", note: 9, annee: 1995 },
];

const categoriser = (rate) => {
  if (rate <= 0.072) return "VeryRare"; // 1 / 15
  if (rate <= 0.15) return "Rare"; // 2 / 15
  if (rate <= 0.25) return "Medium"; // 3–4 / 15
  if (rate <= 0.35) return "Frequent"; // 5 / 15
  return "Dominant"; // 6+ / 15
};

function analyzeByKey(items, key) {
  const total = items.length;

  const occur = Object.groupBy(items, (item) => item[key]);

  const map = new Map();

  for (const [value, list] of Object.entries(occur)) {
    const count = list.length;
    const rate = Number((count / total).toFixed(4));

    map.set(value, {
      value,
      count,
      rate,
      category: categoriser(rate),
      items: list,
    });
  }

  return [...map.values()];
}

const data = analyzeByKey(films, "genre");
console.log(data);

//
{ value: 'SF', count: 6, rate: 0.4286, category: 'Dominant', items: [ [Object], [Object], [Object], [Object], [Object], [Object] ] }, { value: 'Romance', count: 3, rate: 0.2143, category: 'Medium',
...

Anim the dom

 Code pour comprendre le recursif dans le parcours du DOM



En action  → https://dupontdenis.github.io/AnimDOM/


Code → https://github.com/dupontdenis/AnimDOM.git

Les générateurs ! un must

En L1, j'avais déjà utilisé les générateurs pour illustrer les algorithmes → comprendre les algo de base

Comment marche un générateur ? 

Les générateurs en action : → Learn

Go back to : todo list

 Learn by yourself : 📋Maitriser les listes de tâches



Django projet

Rappels : 

SQLite3
    Let's start ! 
Python : Learn by yourself
    Let's start !


Django : lien


Django model/ shell



Projet Airport : lien





Projets : Django

0° Blog


1°🛩️Airport


Objectif : intégrations des passagers aux vols.

------------------- no time left ! 

II partie


3° SPA



Python

Evaluez votre niveau en python ! 🥷🏼python !




Pour info : 

🪛 est équivalent à : 
🚀



Autre exemple

  1. import time

  2. def timer(f):
  3.     def wrapper(*args, **kwargs):
  4.         start = time.time()
  5.         result = f(*args, **kwargs)
  6.         end = time.time()
  7.         print(f"Temps : {end - start:.4f}s")
  8.         return result
  9.     return wrapper

  10. @timer
  11. def slow():
  12.     time.sleep(1)

  13. slow()
#Temps : 1.0006s


Learn by yourself



@debug def add(a, b): ... ↓ équivaut à ↓ def add(a, b): ... add = debug(add)
@debug def add(a, b): ... ↓ équivaut à ↓ def add(a, b): ... add = debug(add)@debug
def add(a, b): ... ↓ équivaut à ↓ def add(a, b): ... add = debug(add)