Third-Party Python Modules
Cloudomation flows are written in Python 3.7. In addition to all standard Python modules several third-party modules are available.
Use Cases
The third-party Python modules serve different purposes. Please refer to the individual documentation pages which are linked below.
Available Third-Party Python Modules
The following third-party Python modules are available to be used in your flow scripts:
Module name | Description | Links |
---|---|---|
arrow | Better dates & times for Python | https://arrow.readthedocs.io/en/latest/ |
holidays | Generate and work with holidays in Python | https://github.com/dr-prodigy/python-holidays |
html5lib | Standards-compliant library for parsing and serializing HTML documents and fragments in Python | https://github.com/html5lib/html5lib-python |
lxml | The lxml XML toolkit for Python | https://github.com/lxml/lxml |
markdown | A Python implementation of John Gruber’s Markdown with Extension support. | https://github.com/Python-Markdown/markdown |
passlib | The Passlib Python Library | https://passlib.readthedocs.io/en/stable/ |
pdftotext | Simple PDF text extraction | https://github.com/jalan/pdftotext |
phonenumbers | Python port of Google's libphonenumber | https://github.com/daviddrysdale/python-phonenumbers |
pycryptodome | A self-contained cryptographic library for Python | https://github.com/Legrandin/pycryptodome |
pyjwt | JSON Web Token implementation in Python | https://github.com/jpadilla/pyjwt |
pyquery | A jquery-like library for python | https://github.com/gawel/pyquery |
pystache | Mustache in Python | https://github.com/defunkt/pystache |
python-dateutil | Useful extensions to the standard Python datetime features | https://github.com/dateutil/dateutil |
pytz | World Timezone Definitions for Python | https://pythonhosted.org/pytz/ |
pyyaml | PyYAML-based module to produce pretty and readable YAML-serialized data | https://github.com/mk-fg/pretty-yaml |
ujson | Ultra fast JSON decoder and encoder written in C with Python bindings | https://github.com/ultrajson/ultrajson |
Contact us at info@cloudomation.com to request additional Python modules.
Examples
Parse a PDF File
You can access the content of PDF files directly in your flow script.
Example
Access a date written in a PDF file
import reimport base64import ioimport pdftotextimport flow_apidef handler(system: flow_api.System, this: flow_api.Execution):pdf_name = 'example.pdf'# Open the Cloudomation file objectpdf_file = system.file(pdf_name)# Files contain binary content which is returned base64 encodedfile_base64 = pdf_file.get('content')file_bytes = base64.b64decode(file_base64)# Construct a `PDF` object from the bytespdf = pdftotext.PDF(io.BytesIO(file_bytes))# Access the text of the first page of the PDF filepdf_text = pdf[0]# Clear the PDF object, so that the file object is closedpdf = None# Use a Regex to find the datethe_date = re.search(r'Date:\s*(\d{4}-[01]\d-[0-3]\d)', pdf_text).group(1)this.log(the_date)return this.success('all done')
Work with CSV Files
You can use the csv
module to read CSV files into Python objects or write Python objects into CSV files.
Example
Create a CSV file and send it by email.
import base64import csvimport ioimport flow_apidef handler(system: flow_api.System, this: flow_api.Execution):fake_data_list = [{'name': 'spam', 'number': 1},{'name': 'eggs', 'number': 2},{'name': 'foo', 'number': 3},{'name': 'bar', 'number': 4},]result_file = io.StringIO()csv_writer = csv.DictWriter(result_file,fieldnames=['name', 'number'],)csv_writer.writeheader()for data in fake_data_list:csv_writer.writerow(data)csv_content = result_file.getvalue()result_file.close()result_file = Nonecsv_writer = Nonesystem.file('result.csv').save_text_content(csv_content)this.task('SMTP',inputs={'from': 'me@example.com','to': 'you@example.com','subject': 'Result','text': 'See the attached file','login': 'me@example.com','password': '****','smtp_host': 'SMTP.example.com','smtp_port': 587,'use_tls': True,'attachments': ['cloudomation:result.csv',],},)return this.success('all done')