Exploiter les classes

Principes d'ingénierie logicielle en Python

Adam Spannbauer

Machine Learning Engineer at Eastman

Étendre la classe Document

class Document:
    def __init__(self, text):
        self.text = text

Tokeniser un document

Principes d'ingénierie logicielle en Python

Classe Document actuelle

class Document:
    def __init__(self, text):
        self.text = text

Tokeniser : init de Document

Principes d'ingénierie logicielle en Python

Réviser __init__

class Document:
    def __init__(self, text):
        self.text = text
        self.tokens = self._tokenize()

doc = Document('test doc') print(doc.tokens)
['test', 'doc']
Principes d'ingénierie logicielle en Python

Ajouter la méthode _tokenize()

# Import function to perform tokenization
from .token_utils import tokenize

class Document: def __init__(self, text, token_regex=r'[a-zA-Z]+'): self.text = text self.tokens = self._tokenize()
def _tokenize(self): return tokenize(self.text)
Principes d'ingénierie logicielle en Python

Méthodes non publiques

Tokeniser : init de Document

Enseigne « Propriété privée »

Principes d'ingénierie logicielle en Python

Les risques des méthodes non publiques

  • Manque de documentation

  • Comportement imprévisible

Enseigne « Propriété privée »

Principes d'ingénierie logicielle en Python

Passons à la pratique !

Principes d'ingénierie logicielle en Python

Preparing Video For Download...