Skip to content

Instantly share code, notes, and snippets.

@LeMeteore
Created December 23, 2021 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LeMeteore/278a9448a72d4222be862fccf4f01c30 to your computer and use it in GitHub Desktop.
Save LeMeteore/278a9448a72d4222be862fccf4f01c30 to your computer and use it in GitHub Desktop.
why Python data classes
import csv
from dataclasses import dataclass
"""
Creer une classe personne representant chaque ligne du fichier csv
"""
class Person2:
def __init__(self, list_name, name, label, agence, nom_prenom_cli, type_cli, anon):
self.list_name = list_name
self.name = name
self.label = label
self.agence = agence
self.nom_prenom_cli = nom_prenom_cli
self.type_cli = type_cli
self.anon = anon
# an example of what you may encounter in your career: a class with a lot of attributes
class Person3:
def __init__(self, list_name, name, label, agence, nom_prenom_cli, type_cli, anon, adress, location, email1, email2, phone1, phone2):
self.list_name = list_name
self.name = name
self.label = label
self.agence = agence
self.nom_prenom_cli = nom_prenom_cli
self.type_cli = type_cli
self.anon = anon
self.email1 = email1
self.email2 = email2
self.phone1 = phone1
self.phone2 = phone2
# data classes, are the way for you to easily or quickly define a class without writing too much code
# a __init__ will be generated for you
@dataclass
class Person4:
list_name: str # keep in mind, every attribute should have a type
name: str
label: str
agence: str
nom_prenom_cli: str
type_cli: str
anon: str
email1: str
email2: str
phone1: str
phone2: str
# a class to represetn a person
class OldPerson:
def __init__(self, lst):
# here we don't know what are the item inside the list
# so we are better put a verification
assert len(lst) == 7 # a tiny tiny verification
self.list_name = lst[0]
self.name = lst[1]
self.label = lst[2]
self.agence = lst[3]
self.nom_prenom_cli = lst[4]
self.type_cli = lst[5]
self.anon = lst[6]
def __eq__(self, other):
return self.name == other.name
# thanks to dataclasses
# you will have for free
# __init__
# __repr__ that can be reimplemented if you need to
# __eq__ that can be reimplemented if you need to
@dataclass
class Person:
list_name: str = "foo" # int, float, str, List, Dict, Tuple, Set
name: str = "bar"
label: str = "baz"
agence: str = "corge"
nom_prenom_cli: str = "qux"
type_cli: str = "quux"
anon: str = "warlow"
# you can add methods if you want
# but keep in mind most the time it is not what you'll need
def dire_bonjour(self):
return 42
# a list containing all the persons
results = []
# the csv file to open
monFichier = open('clients.csv','r')
data = csv.reader(monFichier, delimiter=',')
# a for loop to read each line of the csv file
for line in data:
results.append(Person())
p6 = results[6]
p10 = results[10]
print(p6)
print(p10)
print(p10.name)
print(p6.name)
print(p10 == p6)
# example of a method call that behaves as usual
print(p10.dire_bonjour())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment