From 0593aab35bb0b160c20774a93a61b951d90523b1 Mon Sep 17 00:00:00 2001 From: Bruce Payette Date: Wed, 3 Aug 2016 14:49:35 -0700 Subject: [PATCH] Checked in PowerShell python interop demo --- demos/python/class1.ps1 | 11 ++++++ demos/python/class1.py | 18 ++++++++++ demos/python/demo_script.ps1 | 61 ++++++++++++++++++++++++++++++++++ demos/python/inline_python.ps1 | 16 +++++++++ 4 files changed, 106 insertions(+) create mode 100644 demos/python/class1.ps1 create mode 100755 demos/python/class1.py create mode 100644 demos/python/demo_script.ps1 create mode 100644 demos/python/inline_python.ps1 diff --git a/demos/python/class1.ps1 b/demos/python/class1.ps1 new file mode 100644 index 000000000..63362fe50 --- /dev/null +++ b/demos/python/class1.ps1 @@ -0,0 +1,11 @@ +# +# Wrap python script in such a way to make it easy to +# consume from powershell +# +# The variable $PSScriptRoot points to the directory +# from which the script was executed. This allows +# picking up the python script from the same directory +# + +& $PSScriptRoot/class1.py | ConvertFrom-JSON + diff --git a/demos/python/class1.py b/demos/python/class1.py new file mode 100755 index 000000000..23209a920 --- /dev/null +++ b/demos/python/class1.py @@ -0,0 +1,18 @@ +#!/usr/bin/python3 + +import json + +# Define a class with a method that returns JSON +class returnsjson: + def method1(this): + return json.dumps(['foo', + { + 'bar': ('baz', None, 1.0, 2), + 'buz': ('foo1', 'foo2', 'foo3') + }, + 'alpha', + 1,2,3]) + +c = returnsjson() +print (c.method1()) + diff --git a/demos/python/demo_script.ps1 b/demos/python/demo_script.ps1 new file mode 100644 index 000000000..4e1bdd3b0 --- /dev/null +++ b/demos/python/demo_script.ps1 @@ -0,0 +1,61 @@ +# +# Demo simple interoperation between PowerShell and Python + +# Basic execution of a python script fragment +python -c "print('Hi!')" + +# Capture output in a variable +$data = python -c "print('Hi!')" + +# And show the data +$data + +# Use in expressions +5 + (python -c "print(2 + 3)") + 7 + +# Create a python script using a PowerShell here-string, no extension +@" +#!/usr/bin/python3 +print('Hi!') +"@ | out-file -encoding ascii hi + +# Make it executable +chmod +x hi + +# Run it - shows that PowerShell really is a shell +./hi + +# A mode complex script that outputs JSON +cat class1.py + +# Run the script +./class1.py + +# Capture the data as structured objects (arrays and hashtables) +$data = ./class1.py | ConvertFrom-JSON + +# look at the first element of the returned array +$data[0] + +# Look at the second +$data[1] + +# Get a specific element from the data +$data[1].buz[1] + +# Finally wrap it all up so it looks like a simple PowerShell command +cat class1.ps1 + +# And run it, treating the output as structured data. +(./class1)[1].buz[1] + +# Finally a PowerShell script with in-line python +cat inline_python.ps1 + +# and run it +./inline_python + + +#################################### +# cleanup +rm hi diff --git a/demos/python/inline_python.ps1 b/demos/python/inline_python.ps1 new file mode 100644 index 000000000..c9026304c --- /dev/null +++ b/demos/python/inline_python.ps1 @@ -0,0 +1,16 @@ +# +# An example showing inline Python code in a PowerShell script +# + +"Hello from PowerShell!" + +# Inline Python code in a "here string" which allows for a multi-line script +python3 -c @" +print(' Hello from Python!') +print(' Python and PowerShell get along great!') +"@ + +# Back to PowerShell... +"Back to PowerShell." +"Bye now!" +