Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components): Added a Python script sample #5203

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Components - Added a Python script sample
  • Loading branch information
Ark-kun committed Feb 28, 2021
commit 9dec99020e4da3fa13c2841755d75b322092cb67
43 changes: 43 additions & 0 deletions components/sample/Python_script/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Filter text
inputs:
- {name: Text}
- {name: Pattern, default: '.*'}
outputs:
- {name: Filtered text}
metadata:
annotations:
author: Alexey Volkov <alexey.volkov@ark-kun.com>
implementation:
container:
image: python:3.8
command:
- sh
- -ec
- |
# This is how additional packages can be installed dynamically
python3 -m pip install pip six
# Run the rest of the command after installing the packages.
"$0" "$@"
- python3
- -u # Auto-flush. We want the logs to appear in the console immediately.
- -c # Inline scripts are easy, but have size limitaions and the error traces do not show source lines.
- |
import os
import re
import sys

text_path = sys.argv[1]
pattern = sys.argv[2]
filtered_text_path = sys.argv[3]

regex = re.compile(pattern)

os.makedirs(os.path.dirname(filtered_text_path), exist_ok=True)
with open(text_path, 'r') as reader:
with open(filtered_text_path, 'w') as writer:
for line in reader:
if regex.search(line):
writer.write(line)
- {inputPath: Text}
- {inputValue: Pattern}
- {outputPath: Filtered text}