Skip to content

Commit 0593aab

Browse files
author
Bruce Payette
committed
Checked in PowerShell python interop demo
1 parent b1ea06a commit 0593aab

4 files changed

Lines changed: 106 additions & 0 deletions

File tree

demos/python/class1.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# Wrap python script in such a way to make it easy to
3+
# consume from powershell
4+
#
5+
# The variable $PSScriptRoot points to the directory
6+
# from which the script was executed. This allows
7+
# picking up the python script from the same directory
8+
#
9+
10+
& $PSScriptRoot/class1.py | ConvertFrom-JSON
11+

demos/python/class1.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/python3
2+
3+
import json
4+
5+
# Define a class with a method that returns JSON
6+
class returnsjson:
7+
def method1(this):
8+
return json.dumps(['foo',
9+
{
10+
'bar': ('baz', None, 1.0, 2),
11+
'buz': ('foo1', 'foo2', 'foo3')
12+
},
13+
'alpha',
14+
1,2,3])
15+
16+
c = returnsjson()
17+
print (c.method1())
18+

demos/python/demo_script.ps1

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#
2+
# Demo simple interoperation between PowerShell and Python
3+
4+
# Basic execution of a python script fragment
5+
python -c "print('Hi!')"
6+
7+
# Capture output in a variable
8+
$data = python -c "print('Hi!')"
9+
10+
# And show the data
11+
$data
12+
13+
# Use in expressions
14+
5 + (python -c "print(2 + 3)") + 7
15+
16+
# Create a python script using a PowerShell here-string, no extension
17+
@"
18+
#!/usr/bin/python3
19+
print('Hi!')
20+
"@ | out-file -encoding ascii hi
21+
22+
# Make it executable
23+
chmod +x hi
24+
25+
# Run it - shows that PowerShell really is a shell
26+
./hi
27+
28+
# A mode complex script that outputs JSON
29+
cat class1.py
30+
31+
# Run the script
32+
./class1.py
33+
34+
# Capture the data as structured objects (arrays and hashtables)
35+
$data = ./class1.py | ConvertFrom-JSON
36+
37+
# look at the first element of the returned array
38+
$data[0]
39+
40+
# Look at the second
41+
$data[1]
42+
43+
# Get a specific element from the data
44+
$data[1].buz[1]
45+
46+
# Finally wrap it all up so it looks like a simple PowerShell command
47+
cat class1.ps1
48+
49+
# And run it, treating the output as structured data.
50+
(./class1)[1].buz[1]
51+
52+
# Finally a PowerShell script with in-line python
53+
cat inline_python.ps1
54+
55+
# and run it
56+
./inline_python
57+
58+
59+
####################################
60+
# cleanup
61+
rm hi

demos/python/inline_python.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# An example showing inline Python code in a PowerShell script
3+
#
4+
5+
"Hello from PowerShell!"
6+
7+
# Inline Python code in a "here string" which allows for a multi-line script
8+
python3 -c @"
9+
print(' Hello from Python!')
10+
print(' Python and PowerShell get along great!')
11+
"@
12+
13+
# Back to PowerShell...
14+
"Back to PowerShell."
15+
"Bye now!"
16+

0 commit comments

Comments
 (0)