liveBook · Manning

Decorator

As explained in Software Engineering for Data Scientists

A decorator is a callable Python construct that wraps a function or class to extend or alter its behavior without changing its source definition.

A decorator is a callable Python construct placed above a function definition with @. It wraps the function to extend or alter its behavior without changing the function’s source code. This makes a repeated behavior—such as a timeout or profiling hook—portable across functions. section 5.3.4

Limit model-training time

The stopit package provides a timeoutable decorator. Applying @timeoutable() to train_model lets the call supply a timeout alongside the function’s normal arguments: train_model(timeout=180, features=train_features, labels=train_labels). If training does not finish, the function returns None, allowing the code to raise an error. section 5.3.4

Listing 5.14 This listing’s code uses a timeout limit whenever the train_model function is called. Try the code out for yourself in the ch5/stopit_with_decorator.py file.
from stopit import threading_timeoutable as timeoutable #1
from sklearn.ensemble import RandomForestClassifier #1
from ch4.dataset_class_final import DataSet #1

@timeoutable() #2
def train_model(features, labels): #2

    forest_model = RandomForestClassifier( #2
        n_estimators = 500).\ #2
        fit(features, labels) #2

    return forest_model #2

customer_obj = DataSet( #3
    feature_list = [ #3
    "total_day_minutes", #3
    "total_day_calls", #3
    "number_customer_service_calls"], #3
    file_name = "data/customer_churn_data.csv", #3
    label_col = "churn", #3
    pos_category = "yes" #3
    )

forest_model = train_model(timeout = 180, #4
    features = train_features, #4
    labels = train_labels) #4


# Did code finish running in under
# 180 seconds (3 minutes)?
if forest_model: #5
    print("FINISHED TRAINING MODEL...") #5

# Did code timeout?
else: #6
    raise AssertionError("""DID NOT FINISH
        MODEL TRAINING WITHIN TIME LIMIT""") #6

# [Additional code block...] #7

See the complete decorator example, including imports, a decorated train_model function, model training, and a timeout-enabled call. section 5.3.4

Decorators support practical data-science workflows:

  • Add execution time limits to model training or other functions without placing the main code inside a with block. section 5.3.4
  • Apply @profile to a function so line-profiler can measure compute time line by line through terminal commands. section 6.1.2

How Software Engineering for Data Scientists covers it

How deep it goes
Defines @ syntax and demonstrates wrapping functions, but deliberately provides only a highlighted introduction rather than decorator internals.
Examples
Uses stopit around random-forest training and line-profiler around an artist-filtering function.
What it assumes
Assumes familiarity with defining and calling Python functions, while introducing decorator syntax through concrete package examples.
What you can do after
Apply it via stopit or line-profiler to impose function timeouts or inspect line-by-line execution time.
How it connects
Connects decorators with functions, timeouts, model training, performance profiling, and command-line tools.

Also explained in

Compare how 9 books explain decorator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Engineering for Data Scientists. The section links point to the parts of the book it draws on.