SlideShare une entreprise Scribd logo
1
ANNOTATIONS JAVA
VS.
DÉCORATEURS PYTHON
8 Décembre 2016
Toulouse JUG
( )Didier Plaindoux @dplaindoux
2
Computer scientist Freelance
λ
3
4
ANNOTATIONS JAVA
Ajout de méta-données dans le code source Java
Intégration dans la version 1.5 (JSR-175) + APT
A la compilation depuis la version 1.6 (JSR-269)
5
ANNOTATIONS JAVA :: ELEMENTS CIBLE
6
ANNOTATIONS JAVA :: RÉTENTION
SOURCE
Eliminée à la
compilation
Traitement à la
compilation (*)
(*) JSR-269
CLASS
Eliminée à
l'exécution
Traitement du
code objet
RUNTIME
Préservée à
l'exécution
Traitement à
l'exécution
7
ANNOTATIONS JAVA :: EN RÉSUMÉ
Séparation Spéci cation & Traitement
Traitement à la Compilation & Exécution
8
9
CALLABLE PYTHON :: DÉFINITION
Fonction
Application
Classe
Application
Méthode
Application
Instance
Méthode __call__
__call__ évoque le apply de Scala !
10
DÉCORATEURS PYTHON :: ELEMENTS CIBLE
11
DÉCORATEURS PYTHON :: DÉFINITION
Callable qui prend pour argument un Callable
Décorateur :: Callable ⟶ Callable
Fonction d'ordre supérieur
Composition de Décorateurs et Monoïde Commutatif
12
DÉCORATEURS PYTHON :: FONCTION
def tracer(f):
print("Trace active pour " + nom_de(f)) # ⟨1⟩
return f
⟨1⟩ à l'initialisation
13
DÉCORATEURS PYTHON :: FONCTION
def tracer(f):
print("Trace active pour " + nom_de(f)) # ⟨1⟩
def manipule_f(*args, **kwargs): # Forme generale
print("Trace effective pour " + nom_de(f)) # ⟨2⟩
return f(*args, **kwargs)
return manipule_f
⟨1⟩ à l'initialisation et ⟨2⟩ à chaque exécution
14
DÉCORATEURS PYTHON :: A L'ACTION
def ma_fonction():
pass
ma_fonction = tracer(ma_fonction)
Un peu de sucre ?
@tracer
def ma_fonction():
pass
15
DÉCORATEURS PYTHON :: CLASSE CALLABLE
class Tracer:
def __init__(self, f):
self.f = f
print("Trace active pour " + nom_de(self.f)) # ⟨1⟩
⟨1⟩ à l'initialisation
16
DÉCORATEURS PYTHON :: CLASSE CALLABLE
class Tracer:
def __init__(self, f):
self.f = f
print("Trace active pour " + nom_de(self.f)) # ⟨1⟩
def __call__(f, *args, **kwargs):
print("Trace effective pour " + nom_de(self.f)) # ⟨2⟩
return self.f(*args, **kwargs)
⟨1⟩ à l'initialisation et ⟨2⟩ à chaque exécution
17
DÉCORATEURS PYTHON :: A L'ACTION
def ma_fonction():
pass
@Tracer
def ma_fonction():
pass
Décoration sans contexte i.e. paramètres
18
DÉCORATEURS PYTHON :: AVEC PARAMÈTRES
class Tracer:
def __init__(self, nom):
self.nom = nom
print("Trace active pour " + nom)
19
DÉCORATEURS PYTHON :: AVEC PARAMÈTRES
class Tracer:
def __init__(self, nom):
self.nom = nom
print("Trace active pour " + nom)
def __call__(self, f):
print("Trace active pour " + nom + "::" + nom_de(f))
return f
20
DÉCORATEURS PYTHON :: AVEC PARAMÈTRES
class Tracer:
def __init__(self, nom):
self.nom = nom
print("Trace active pour " + nom)
def __call__(self, f):
print("Trace active pour " + nom + "::" + nom_de(f))
def manipule_f(*args, **kwargs):
print("Trace effective pour " + nom + "::" + nom_de(f))
return self.f(*args, **kwargs)
return manipule_f
21
DÉCORATEURS PYTHON :: A L'ACTION
def ma_fonction():
pass
ma_fonction = Tracer("Ma Fonction")(ma_fonction)
Et rebelote avec le sucre !
@Tracer("Ma Fonction")
def ma_fonction():
pass
22
...
DÉCORATEURS PYTHON :: FRAMEWORKS
23
DÉCORATEURS PYTHON :: EXTENSION DU LANGAGE
def Maybe:
def __init__(self, value=None):
self.value = value
@staticmethod
def none(): # pas de self ... car statique
return Maybe()
# ...
24
DÉCORATEURS PYTHON :: EXTENSION DU LANGAGE
o.m ⇒ type(o).__dict__["m"].__get__(o, type(o))
class StaticMethod(object):
def __init__(self, f):
self.f = f
def __get__(self, obj, objtype=None):
return self.f # Appel glouton: mange l'objet associé à self
"Descriptor Protocol" pour aller plus loin ...
25
DÉCORATEURS ECMASCRIPT :: SÉQUELLE
A Decorator is an expression that evaluates to a function
applied to the target, name and decorator descriptor.
Optionally it returns a decorator descriptor to install on the
target object
26
27
APPROCHES DIFFÉRENTES
Java
Interface
Annotation
Traitement
Python
Callable
Décoration
Exécution
28
MERCI
QUESTIONS ?

Contenu connexe

Tendances (10)

PDF
Fondamentaux portée - contexte - function ms tech days
Jean-Pierre Vincent
 
PDF
Playing With PHP 5.3
Fabien Potencier
 
PPTX
Zend Framework 2
epixelic
 
PDF
BH Team - Mettez un python dans votre robot !
Yannick Jost
 
PDF
BackDay Xebia : Découvrez RxJava, le reactive programming
Publicis Sapient Engineering
 
PPTX
Php 5.3
epixelic
 
DOCX
Résumer sur les fichier et les enregistrement
borhen boukthir
 
PPTX
ALF 9 - Generation de code
Alexandru Radovici
 
PDF
Esupdays 21: Point sur le projet ESUP cas_toolbox
Ludovic A
 
Fondamentaux portée - contexte - function ms tech days
Jean-Pierre Vincent
 
Playing With PHP 5.3
Fabien Potencier
 
Zend Framework 2
epixelic
 
BH Team - Mettez un python dans votre robot !
Yannick Jost
 
BackDay Xebia : Découvrez RxJava, le reactive programming
Publicis Sapient Engineering
 
Php 5.3
epixelic
 
Résumer sur les fichier et les enregistrement
borhen boukthir
 
ALF 9 - Generation de code
Alexandru Radovici
 
Esupdays 21: Point sur le projet ESUP cas_toolbox
Ludovic A
 

Annotation Java vs. Decorator Python

  • 1. 1 ANNOTATIONS JAVA VS. DÉCORATEURS PYTHON 8 Décembre 2016 Toulouse JUG ( )Didier Plaindoux @dplaindoux
  • 3. 3
  • 4. 4 ANNOTATIONS JAVA Ajout de méta-données dans le code source Java Intégration dans la version 1.5 (JSR-175) + APT A la compilation depuis la version 1.6 (JSR-269)
  • 5. 5 ANNOTATIONS JAVA :: ELEMENTS CIBLE
  • 6. 6 ANNOTATIONS JAVA :: RÉTENTION SOURCE Eliminée à la compilation Traitement à la compilation (*) (*) JSR-269 CLASS Eliminée à l'exécution Traitement du code objet RUNTIME Préservée à l'exécution Traitement à l'exécution
  • 7. 7 ANNOTATIONS JAVA :: EN RÉSUMÉ Séparation Spéci cation & Traitement Traitement à la Compilation & Exécution
  • 8. 8
  • 9. 9 CALLABLE PYTHON :: DÉFINITION Fonction Application Classe Application Méthode Application Instance Méthode __call__ __call__ évoque le apply de Scala !
  • 10. 10 DÉCORATEURS PYTHON :: ELEMENTS CIBLE
  • 11. 11 DÉCORATEURS PYTHON :: DÉFINITION Callable qui prend pour argument un Callable Décorateur :: Callable ⟶ Callable Fonction d'ordre supérieur Composition de Décorateurs et Monoïde Commutatif
  • 12. 12 DÉCORATEURS PYTHON :: FONCTION def tracer(f): print("Trace active pour " + nom_de(f)) # ⟨1⟩ return f ⟨1⟩ à l'initialisation
  • 13. 13 DÉCORATEURS PYTHON :: FONCTION def tracer(f): print("Trace active pour " + nom_de(f)) # ⟨1⟩ def manipule_f(*args, **kwargs): # Forme generale print("Trace effective pour " + nom_de(f)) # ⟨2⟩ return f(*args, **kwargs) return manipule_f ⟨1⟩ à l'initialisation et ⟨2⟩ à chaque exécution
  • 14. 14 DÉCORATEURS PYTHON :: A L'ACTION def ma_fonction(): pass ma_fonction = tracer(ma_fonction) Un peu de sucre ? @tracer def ma_fonction(): pass
  • 15. 15 DÉCORATEURS PYTHON :: CLASSE CALLABLE class Tracer: def __init__(self, f): self.f = f print("Trace active pour " + nom_de(self.f)) # ⟨1⟩ ⟨1⟩ à l'initialisation
  • 16. 16 DÉCORATEURS PYTHON :: CLASSE CALLABLE class Tracer: def __init__(self, f): self.f = f print("Trace active pour " + nom_de(self.f)) # ⟨1⟩ def __call__(f, *args, **kwargs): print("Trace effective pour " + nom_de(self.f)) # ⟨2⟩ return self.f(*args, **kwargs) ⟨1⟩ à l'initialisation et ⟨2⟩ à chaque exécution
  • 17. 17 DÉCORATEURS PYTHON :: A L'ACTION def ma_fonction(): pass @Tracer def ma_fonction(): pass Décoration sans contexte i.e. paramètres
  • 18. 18 DÉCORATEURS PYTHON :: AVEC PARAMÈTRES class Tracer: def __init__(self, nom): self.nom = nom print("Trace active pour " + nom)
  • 19. 19 DÉCORATEURS PYTHON :: AVEC PARAMÈTRES class Tracer: def __init__(self, nom): self.nom = nom print("Trace active pour " + nom) def __call__(self, f): print("Trace active pour " + nom + "::" + nom_de(f)) return f
  • 20. 20 DÉCORATEURS PYTHON :: AVEC PARAMÈTRES class Tracer: def __init__(self, nom): self.nom = nom print("Trace active pour " + nom) def __call__(self, f): print("Trace active pour " + nom + "::" + nom_de(f)) def manipule_f(*args, **kwargs): print("Trace effective pour " + nom + "::" + nom_de(f)) return self.f(*args, **kwargs) return manipule_f
  • 21. 21 DÉCORATEURS PYTHON :: A L'ACTION def ma_fonction(): pass ma_fonction = Tracer("Ma Fonction")(ma_fonction) Et rebelote avec le sucre ! @Tracer("Ma Fonction") def ma_fonction(): pass
  • 23. 23 DÉCORATEURS PYTHON :: EXTENSION DU LANGAGE def Maybe: def __init__(self, value=None): self.value = value @staticmethod def none(): # pas de self ... car statique return Maybe() # ...
  • 24. 24 DÉCORATEURS PYTHON :: EXTENSION DU LANGAGE o.m ⇒ type(o).__dict__["m"].__get__(o, type(o)) class StaticMethod(object): def __init__(self, f): self.f = f def __get__(self, obj, objtype=None): return self.f # Appel glouton: mange l'objet associé à self "Descriptor Protocol" pour aller plus loin ...
  • 25. 25 DÉCORATEURS ECMASCRIPT :: SÉQUELLE A Decorator is an expression that evaluates to a function applied to the target, name and decorator descriptor. Optionally it returns a decorator descriptor to install on the target object
  • 26. 26