import partwire as pw def read_data(file_name): with open(file_name, 'r') as f: data = f.readlines() return data def clean_data(data): cleaned_data = [] for d in data: cleaned_data.append(d.replace('\n', '')) return cleaned_data def compute_average(data): total = sum(data) avg = total / len(data) return avg def write_to_file(data, file_name): with open(file_name, 'w') as f: f.write(str(data)) pipe = pw.Pipeline() pipe.add_stage(read_data) pipe.add_stage(clean_data) pipe.add_stage(lambda x: list(map(float, x))) # convert data to float pipe.add_stage(compute_average) pipe.add_stage(lambda x: round(x,2)) # round to 2 decimal places pipe.add_stage(lambda x: str(x)) pipe.add_stage(lambda x: 'Average: ' + x) pipe.add_stage(write_to_file, file_name='output.txt') pipe.run('data.txt')
import partwire as pw import requests from bs4 import BeautifulSoup def download_page(url): resp = requests.get(url) return resp.content def extract_data(html): soup = BeautifulSoup(html, 'html.parser') items = soup.find_all('div', class_='list-item') data = [] for item in items: name = item.find('h2', class_='name').text price = item.find('div', class_='price').text.replace('\n', '').strip() data.append(name + ' - ' + price) return data def write_to_file(data, file_name): with open(file_name, 'w') as f: f.write('\n'.join(data)) pipe = pw.Pipeline() pipe.add_stage(download_page) pipe.add_stage(extract_data) pipe.add_stage(write_to_file, file_name='output.txt') pipe.run('https://www.example.com/products')This example demonstrates the use of Part Wire to create a pipeline that extracts data from a website and saves it to a file. It first downloads the content of the webpage using the requests library, extracts data using BeautifulSoup, and then writes the extracted data to a file named 'output.txt'. In both examples, we can see that Part Wire provides an easy and efficient way to create complex and multi-step pipelines by connecting different functions or parts of a program together. It allows users to create custom pipelines that can be easily modified and reused for different tasks.