Created
April 16, 2024 15:33
-
-
Save panchishin/233d7bba7a9a8eb94b6af0694f525c76 to your computer and use it in GitHub Desktop.
Using the Pipe in python as an operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of using custom | operations | |
# Lets say we want to emulate LCEL, a language that uses | as a pipe operator | |
# For this example we will start with an input which is a dictionary | |
# and we want our operator object to read the 'input' key and add a new key 'output' | |
# Our starting data | |
input_data = {'input': "Hello World"} | |
# Our operator object that reverses the input and stores it in the 'output' key | |
class OurCustomOperator: | |
def __ror__(self, data): | |
data['output'] = data['input'][::-1] | |
return data | |
# Our pipe operator | |
custom_pipe = OurCustomOperator() | |
# Now we can use the | operator to apply the custom_pipe to the input_data | |
print("Starting data:", input_data) | |
print("Result:", input_data | custom_pipe) | |
# The result of running this code is the following | |
""" | |
Starting data: {'input': 'Hello World'} | |
Result: {'input': 'Hello World', 'output': 'dlroW olleH'} | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment