Skip to content

Commit

Permalink
update (componentization): step2 and step3 with Claude
Browse files Browse the repository at this point in the history
  • Loading branch information
santanche committed Sep 28, 2024
1 parent bdfc958 commit f94d8d3
Showing 1 changed file with 243 additions and 1 deletion.
244 changes: 243 additions & 1 deletion notebooks/componentization/claude.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

Consider the following code:


```
import pandas as pd
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -79,4 +78,247 @@ def main():
if __name__ == "__main__":
main()

~~~

## Passo 2 - Múltiplos Serviços

Consider the following code:

```
import matplotlib.pyplot as plt
# Create a scatter plot
plt.scatter(df['age'], df['intake_bw'], c=df['intake_bw'], cmap='viridis')
# Add labels to the plot
plt.xlabel('Age')
plt.ylabel('Intake BW')
plt.title('Age vs. Intake BW')
# Show the plot
plt.show()
```

How can I transform it in a Python class whose objects receive four parameters (df, xlabel, ylabel, and title) and plots a scatter plot?

<hr>

### Answer

~~~python

import matplotlib.pyplot as plt

class ScatterPlot:
def __init__(self, df, xlabel, ylabel, title):
self.df = df
self.xlabel = xlabel
self.ylabel = ylabel
self.title = title

def plot(self):
# Create a scatter plot
plt.figure(figsize=(10, 6))
scatter = plt.scatter(self.df['age'], self.df['intake_bw'], c=self.df['intake_bw'], cmap='viridis')

# Add labels to the plot
plt.xlabel(self.xlabel)
plt.ylabel(self.ylabel)
plt.title(self.title)

# Add a colorbar
plt.colorbar(scatter, label='Intake BW')

# Show the plot
plt.show()

# Example usage:
# scatter_plot = ScatterPlot(df, 'Age', 'Intake BW', 'Age vs. Intake BW')
# scatter_plot.plot()

~~~

<hr>

Consider this code:

```
import pandas as pd
# Read the CSV file
df = pd.read_csv('../../../data/food-intake/analysis/prepared/intake-person-demo(potato-chips).csv')
```

Can you transform it in a class whose object receives a data file path as a parameter and reads the file?
After that, provide an example of this class running together to the previous one.

<hr>

### Answer

~~~python
import pandas as pd

class DataLoader:
def __init__(self, file_path):
self.file_path = file_path
self.data = None

def load_data(self):
try:
self.data = pd.read_csv(self.file_path)
print(f"Data loaded successfully from {self.file_path}")
except FileNotFoundError:
print(f"Error: File not found at {self.file_path}")
except pd.errors.EmptyDataError:
print(f"Error: The file at {self.file_path} is empty")
except pd.errors.ParserError:
print(f"Error: Unable to parse the file at {self.file_path}")

def get_data(self):
if self.data is None:
print("Data has not been loaded. Call load_data() first.")
return None
return self.data
~~~

#### Updated ScatterPlot with Example

~~~python
import matplotlib.pyplot as plt

class ScatterPlot:
def __init__(self, df, xlabel, ylabel, title):
self.df = df
self.xlabel = xlabel
self.ylabel = ylabel
self.title = title

def plot(self):
# Create a scatter plot
plt.figure(figsize=(10, 6))
scatter = plt.scatter(self.df['age'], self.df['intake_bw'], c=self.df['intake_bw'], cmap='viridis')

# Add labels to the plot
plt.xlabel(self.xlabel)
plt.ylabel(self.ylabel)
plt.title(self.title)

# Add a colorbar
plt.colorbar(scatter, label='Intake BW')

# Show the plot
plt.show()

# Example usage of DataLoader and ScatterPlot classes together:

# Import the DataLoader class (assuming it's in a file named data_loader.py)
from data_loader import DataLoader

# Create a DataLoader instance
data_loader = DataLoader('../../../data/food-intake/analysis/prepared/intake-person-demo(potato-chips).csv')

# Load the data
data_loader.load_data()

# Get the loaded data
df = data_loader.get_data()

# Check if data was loaded successfully
if df is not None:
# Create a ScatterPlot instance
scatter_plot = ScatterPlot(df, 'Age', 'Intake BW', 'Age vs. Intake BW for Potato Chips')

# Generate the plot
scatter_plot.plot()
else:
print("Unable to create scatter plot due to data loading issues.")
~~~

## Passo 3 - Representando Relações

Can you modify The DataLoader and ScatterPlot classes (and the respective example) in such a way that the DataLoader has a connect method that receives the ScatterPlot as a parameter and keeps it. Whenever the DataLoader reads a new table it automatically sends it to the ScatterPlot.

<hr>

### Answer

~~~python
import pandas as pd

class DataLoader:
def __init__(self):
self.data = None
self.observers = []

def connect(self, observer):
self.observers.append(observer)

def notify_observers(self):
for observer in self.observers:
observer.update(self.data)

def load_data(self, file_path):
try:
self.data = pd.read_csv(file_path)
print(f"Data loaded successfully from {file_path}")
self.notify_observers()
except FileNotFoundError:
print(f"Error: File not found at {file_path}")
except pd.errors.EmptyDataError:
print(f"Error: The file at {file_path} is empty")
except pd.errors.ParserError:
print(f"Error: Unable to parse the file at {file_path}")

def get_data(self):
return self.data
~~~

#### Updated ScatterPlot with Example

~~~python
import matplotlib.pyplot as plt

class ScatterPlot:
def __init__(self, xlabel, ylabel, title):
self.df = None
self.xlabel = xlabel
self.ylabel = ylabel
self.title = title

def update(self, new_data):
self.df = new_data
self.plot()

def plot(self):
if self.df is None:
print("No data to plot.")
return

plt.figure(figsize=(10, 6))
scatter = plt.scatter(self.df['age'], self.df['intake_bw'], c=self.df['intake_bw'], cmap='viridis')

plt.xlabel(self.xlabel)
plt.ylabel(self.ylabel)
plt.title(self.title)

plt.colorbar(scatter, label='Intake BW')
plt.show()

# Example usage of the updated DataLoader and ScatterPlot classes:

# Assuming both classes are in the same file or properly imported

# Create instances
data_loader = DataLoader()
scatter_plot = ScatterPlot('Age', 'Intake BW', 'Age vs. Intake BW for Potato Chips')

# Connect the ScatterPlot to the DataLoader
data_loader.connect(scatter_plot)

# Load data - this will automatically update and plot the scatter plot
data_loader.load_data('../../../data/food-intake/analysis/prepared/intake-person-demo(potato-chips).csv')

# If you want to load another file, just call load_data again
# data_loader.load_data('path/to/another/file.csv')
~~~

0 comments on commit f94d8d3

Please sign in to comment.