From 0593aab35bb0b160c20774a93a61b951d90523b1 Mon Sep 17 00:00:00 2001 From: Bruce Payette Date: Wed, 3 Aug 2016 14:49:35 -0700 Subject: [PATCH 1/3] 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 0000000000..63362fe502 --- /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 0000000000..23209a9208 --- /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 0000000000..4e1bdd3b04 --- /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 0000000000..c9026304c9 --- /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!" + From c1ccbc09230293fac2fc6ab54a8ad24bd57af862 Mon Sep 17 00:00:00 2001 From: Bruce Payette Date: Wed, 3 Aug 2016 16:03:04 -0700 Subject: [PATCH 2/3] Added README.md to the demo. --- demos/python/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 demos/python/README.md diff --git a/demos/python/README.md b/demos/python/README.md new file mode 100644 index 0000000000..66ec4862d7 --- /dev/null +++ b/demos/python/README.md @@ -0,0 +1,8 @@ +## PowerShell/Python Interoperation Demo + +The "demo_script.ps1" file in this directory walks through a +demonstration of basic interoperation between PowerShell and Python +including how to use JSON to exchange structured objects between +Python and PowerShell. + +The other files in this directory are referenced by demo_script.ps1. From 8a6798fb4fb3a3ad6b74edce4fdb9571965d56a8 Mon Sep 17 00:00:00 2001 From: Bruce Payette Date: Thu, 4 Aug 2016 13:13:21 -0700 Subject: [PATCH 3/3] Updated to include Aleksandar's editorial changes; mostly python to Python in the comments. --- demos/python/class1.ps1 | 6 +++--- demos/python/demo_script.ps1 | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/demos/python/class1.ps1 b/demos/python/class1.ps1 index 63362fe502..16b426a91d 100644 --- a/demos/python/class1.ps1 +++ b/demos/python/class1.ps1 @@ -1,10 +1,10 @@ # -# Wrap python script in such a way to make it easy to -# consume from powershell +# 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 +# picking up the Python script from the same directory # & $PSScriptRoot/class1.py | ConvertFrom-JSON diff --git a/demos/python/demo_script.ps1 b/demos/python/demo_script.ps1 index 4e1bdd3b04..0544efe15e 100644 --- a/demos/python/demo_script.ps1 +++ b/demos/python/demo_script.ps1 @@ -1,7 +1,7 @@ # # Demo simple interoperation between PowerShell and Python -# Basic execution of a python script fragment +# Basic execution of a Python script fragment python -c "print('Hi!')" # Capture output in a variable @@ -13,7 +13,7 @@ $data # Use in expressions 5 + (python -c "print(2 + 3)") + 7 -# Create a python script using a PowerShell here-string, no extension +# Create a Python script using a PowerShell here-string, no extension @" #!/usr/bin/python3 print('Hi!') @@ -25,7 +25,7 @@ chmod +x hi # Run it - shows that PowerShell really is a shell ./hi -# A mode complex script that outputs JSON +# A more complex script that outputs JSON cat class1.py # Run the script @@ -49,7 +49,7 @@ cat class1.ps1 # And run it, treating the output as structured data. (./class1)[1].buz[1] -# Finally a PowerShell script with in-line python +# Finally a PowerShell script with in-line Python cat inline_python.ps1 # and run it