mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Merge pull request #1627 from PowerShell/brucepayette/python-demo
Checked in PowerShell python interop demo
This commit is contained in:
@@ -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.
|
||||
@@ -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
|
||||
|
||||
Executable
+18
@@ -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())
|
||||
|
||||
@@ -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 more 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
|
||||
@@ -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!"
|
||||
|
||||
Reference in New Issue
Block a user