Skip to content

Commit cfd5f84

Browse files
authoredJan 17, 2022
Merge pull request #1025 from LukasZahradnik/master
User Guide documentation typos fixes
2 parents 7e12ac6 + 38fedf7 commit cfd5f84

File tree

1 file changed

+90
-90
lines changed

1 file changed

+90
-90
lines changed
 

‎doc/userguide.rst

+90-90
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ set the class path, start the JVM, remove all the type declarations, and you are
9292
9393
# Copy in the patterns from the guide to replace the example code
9494
db = Database("our_records")
95-
with db.connect() as DatabaseConnection:
96-
c.runQuery()
97-
while c.hasRecords():
98-
record = db.nextRecord()
99-
...
95+
with db.connect() as DatabaseConnection:
96+
c.runQuery()
97+
while c.hasRecords():
98+
record = db.nextRecord()
99+
...
100100
101101
Launch it in the interactive window. You can get back to programming in Python
102102
once you get a good night sleep.
@@ -136,11 +136,11 @@ in your serialized object.
136136
from java.nio.file import Files, Paths
137137
from java.io import ObjectInputStream
138138
139-
with Files.newInputStream(Paths.get("myobject.ser") as stream:
140-
ois = new ObjectInputStream(stream)
141-
obj = ois.readObject()
139+
with Files.newInputStream(Paths.get("myobject.ser")) as stream:
140+
ois = ObjectInputStream(stream)
141+
obj = ois.readObject()
142142
143-
print(obj) # prints org.bigstuff.MyObject@7382f612
143+
print(obj) # prints org.bigstuff.MyObject@7382f612
144144
145145
It appears that the structure is loaded. The problematic structure requires you
146146
call the getData method with the correct index.
@@ -172,7 +172,7 @@ example and watch what happens.
172172

173173
.. code-block:: python
174174
175-
import matplot.pyplot as plt
175+
import matplotlib.pyplot as plt
176176
plt.plot(d)
177177
plt.show()
178178
@@ -227,13 +227,13 @@ Based on the previous examples, you start by defining a monitor class
227227
228228
@JImplements(Monitor)
229229
class HeartMonitor:
230-
def __init__(self):
231-
self.readings = []
232-
@JOverride
233-
def onMeasurement(self, measurement):
234-
self.readings.append([measurement.getTime(), measurement.getHeartRate()])
235-
def getResults(self):
236-
return np.array(self.readings)
230+
def __init__(self):
231+
self.readings = []
232+
@JOverride
233+
def onMeasurement(self, measurement):
234+
self.readings.append([measurement.getTime(), measurement.getHeartRate()])
235+
def getResults(self):
236+
return np.array(self.readings)
237237
238238
There is a bit to unpack here. You have implemented a Java class from within Python.
239239
The Java implementation is simply an ordinary Python class which has be
@@ -762,7 +762,7 @@ entirely the domain of whatever JPype has defined including user defined casts.
762762
As ``JObject`` syntax is long and does not look much like Java syntax, the
763763
Python matmul operator is overloaded on JPype types such that one can use the
764764
``@`` operator to cast to a specific Java type. In Java, one would write
765-
``(Type)@object`` to cast the variable ``object`` to ``Type``. In Python, this
765+
``(Type)object`` to cast the variable ``object`` to ``Type``. In Python, this
766766
would be written as ``Type@object``. This can also be applied to array types
767767
``JLong[:]@[1,2,3]``, collection types ``Iterable@[1,2,3]`` or Java functors
768768
``DoubleUnaryOperator@(lambda x:x*2)``. The result of the casting operator
@@ -1656,28 +1656,28 @@ Here is an example:
16561656
.. code-block:: python
16571657
16581658
try :
1659-
# Code that throws a java.lang.RuntimeException
1659+
# Code that throws a java.lang.RuntimeException
16601660
except java.lang.RuntimeException as ex:
1661-
print("Caught the runtime exception : ", str(ex))
1662-
print(ex.stacktrace())
1661+
print("Caught the runtime exception : ", str(ex))
1662+
print(ex.stacktrace())
16631663
16641664
Multiple java exceptions can be caught together or separately:
16651665
16661666
.. code-block:: python
16671667
16681668
try:
1669-
# ...
1669+
# ...
16701670
except (java.lang.ClassCastException, java.lang.NullPointerException) as ex:
1671-
print("Caught multiple exceptions : ", str(ex))
1672-
print(ex.stacktrace())
1671+
print("Caught multiple exceptions : ", str(ex))
1672+
print(ex.stacktrace())
16731673
except java.lang.RuntimeException as ex:
1674-
print("Caught runtime exception : ", str(ex))
1675-
print(ex.stacktrace())
1676-
except jpype.JException:
1677-
print("Caught base exception : ", str(ex))
1678-
print(ex.stacktrace())
1674+
print("Caught runtime exception : ", str(ex))
1675+
print(ex.stacktrace())
1676+
except jpype.JException as ex:
1677+
print("Caught base exception : ", str(ex))
1678+
print(ex.stacktrace())
16791679
except Exception as ex:
1680-
print("Caught python exception :", str(ex))
1680+
print("Caught python exception :", str(ex))
16811681
16821682
Exceptions can be raised in proxies to throw an exception back to Java.
16831683
@@ -1903,7 +1903,7 @@ should be attached to the Java Runtime object. The following pattern is used:
19031903
class MyShutdownHook:
19041904
@JOverride
19051905
def run(self):
1906-
# perform any required shutdown activities
1906+
# perform any required shutdown activities
19071907
19081908
java.lang.Runtime.getRuntime().addShutdownHook(Thread(MyShutdownHook()))
19091909
@@ -1975,16 +1975,16 @@ Example taken from JPype ``java.util.Map`` customizer:
19751975
@_jcustomizer.JImplementationFor('java.util.Map')
19761976
class _JMap:
19771977
def __jclass_init__(self):
1978-
Mapping.register(self)
1978+
Mapping.register(self)
19791979
19801980
def __len__(self):
1981-
return self.size()
1981+
return self.size()
19821982
19831983
def __iter__(self):
1984-
return self.keySet().iterator()
1984+
return self.keySet().iterator()
19851985
19861986
def __delitem__(self, i):
1987-
return self.remove(i)
1987+
return self.remove(i)
19881988
19891989
19901990
The name of the class does not matter for the purposes of customizer though it
@@ -2355,19 +2355,19 @@ dispatch:
23552355
23562356
@JImplements(JavaInterface)
23572357
class MyImpl:
2358-
@JOverride
2359-
def callOverloaded(self, *args):
2360-
# always use the wild card args when implementing a dispatch
2361-
if len(args)==2:
2362-
return self.callMethod1(*args)
2363-
if len(args)==1 and isinstance(args[0], JString):
2364-
return self.callMethod2(*args)
2365-
raise RuntimeError("Incorrect arguments")
2358+
@JOverride
2359+
def callOverloaded(self, *args):
2360+
# always use the wild card args when implementing a dispatch
2361+
if len(args)==2:
2362+
return self.callMethod1(*args)
2363+
if len(args)==1 and isinstance(args[0], JString):
2364+
return self.callMethod2(*args)
2365+
raise RuntimeError("Incorrect arguments")
23662366
23672367
def callMethod1(self, a1, a2):
2368-
# ...
2368+
# ...
23692369
def callMethod2(self, jstr):
2370-
# ...
2370+
# ...
23712371
23722372
Multiple interfaces
23732373
-------------------
@@ -2388,7 +2388,7 @@ achieve this, specify the interface using a string and add the keyword argument
23882388
23892389
@JImplements("org.foo.JavaInterface", deferred=True)
23902390
class MyImpl:
2391-
# ...
2391+
# ...
23922392
23932393
23942394
Deferred proxies are not checked at declaration time, but instead at the time
@@ -2444,27 +2444,27 @@ First, with an object:
24442444
24452445
.. code-block:: python
24462446
2447-
class C :
2448-
def testMethod(self) :
2449-
return 42
2447+
class C:
2448+
def testMethod(self):
2449+
return 42
24502450
2451-
def testMethod2(self) :
2452-
return "Bar"
2451+
def testMethod2(self):
2452+
return "Bar"
24532453
24542454
c = C() # create an instance
2455-
proxy = JProxy("ITestInterface2", inst=c) # Convert it into a proxy
2455+
proxy = JProxy("ITestInterface2", inst=c) # Convert it into a proxy
24562456
24572457
or you can use a dictionary.
24582458
24592459
.. code-block:: python
24602460
2461-
def _testMethod() :
2462-
return 32
2461+
def _testMethod():
2462+
return 32
24632463
2464-
def _testMethod2() :
2465-
return "Fooo!"
2464+
def _testMethod2():
2465+
return "Fooo!"
24662466
2467-
d = { 'testMethod' : _testMethod, 'testMethod2' : _testMethod2, }
2467+
d = { 'testMethod': _testMethod, 'testMethod2': _testMethod2, }
24682468
proxy = JProxy("ITestInterface2", dict=d)
24692469
24702470
@@ -2548,19 +2548,19 @@ launches it from within Python.
25482548
from javax.swing import *
25492549
25502550
def createAndShowGUI():
2551-
frame = JFrame("HelloWorldSwing")
2552-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
2553-
label = JLabel("Hello World")
2554-
frame.getContentPane().add(label)
2555-
frame.pack()
2556-
frame.setVisible(True)
2551+
frame = JFrame("HelloWorldSwing")
2552+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
2553+
label = JLabel("Hello World")
2554+
frame.getContentPane().add(label)
2555+
frame.pack()
2556+
frame.setVisible(True)
25572557
25582558
# Start an event loop thread to handling gui events
25592559
@jpype.JImplements(java.lang.Runnable)
25602560
class Launch:
2561-
@jpype.JOverride
2562-
def run(self):
2563-
createAndShowGUI()
2561+
@jpype.JOverride
2562+
def run(self):
2563+
createAndShowGUI()
25642564
javax.swing.SwingUtilities.invokeLater(Launch())
25652565
25662566
@@ -2658,12 +2658,12 @@ keep the synchronization on as long as the object is kept alive. For example,
26582658
mySharedList = java.util.ArrayList()
26592659
26602660
# Give the list to another thread that will be adding items
2661-
otherThread,setList(mySharedList)
2661+
otherThread.setList(mySharedList)
26622662
26632663
# Lock the list so that we can access it without interference
26642664
with synchronized(mySharedList):
2665-
if not mySharedList.isEmpty():
2666-
... # process elements
2665+
if not mySharedList.isEmpty():
2666+
... # process elements
26672667
# Resource is unlocked once we leave the block
26682668
26692669
The Python ``with`` statement is used to control the scope. Do not
@@ -2695,26 +2695,26 @@ results. For example:
26952695
26962696
.. code-block:: python
26972697
2698-
def limit(method, timeout):
2699-
""" Convert a Java method to asynchronous call with a specified timeout. """
2700-
def f(*args):
2701-
@jpype.JImplements(java.util.concurrent.Callable)
2702-
class g:
2703-
@jpype.JOverride
2704-
def call(self):
2705-
return method(*args)
2706-
future = java.util.concurrent.FutureTask(g())
2707-
java.lang.Thread(future).start()
2708-
try:
2709-
timeunit = java.util.concurrent.TimeUnit.MILLISECONDS
2710-
return future.get(int(timeout*1000), timeunit)
2711-
except java.util.concurrent.TimeoutException as ex:
2712-
future.cancel(True)
2713-
raise RuntimeError("canceled", ex)
2714-
return f
2715-
2716-
print(limit(java.lang.Thread.sleep, timeout=1)(200))
2717-
print(limit(java.lang.Thread.sleep, timeout=1)(20000))
2698+
def limit(method, timeout):
2699+
""" Convert a Java method to asynchronous call with a specified timeout. """
2700+
def f(*args):
2701+
@jpype.JImplements(java.util.concurrent.Callable)
2702+
class g:
2703+
@jpype.JOverride
2704+
def call(self):
2705+
return method(*args)
2706+
future = java.util.concurrent.FutureTask(g())
2707+
java.lang.Thread(future).start()
2708+
try:
2709+
timeunit = java.util.concurrent.TimeUnit.MILLISECONDS
2710+
return future.get(int(timeout*1000), timeunit)
2711+
except java.util.concurrent.TimeoutException as ex:
2712+
future.cancel(True)
2713+
raise RuntimeError("canceled", ex)
2714+
return f
2715+
2716+
print(limit(java.lang.Thread.sleep, timeout=1)(200))
2717+
print(limit(java.lang.Thread.sleep, timeout=1)(20000))
27182718
27192719
Here we have limited the execution time of a Java call.
27202720
@@ -3268,7 +3268,7 @@ this block as a fixture at the start of the test suite.
32683268
faulthandler.enable()
32693269
faulthandler.disable()
32703270
except:
3271-
pass
3271+
pass
32723272
32733273
This code enables fault handling and then returns the default handlers which
32743274
will point back to those set by Java.

0 commit comments

Comments
 (0)
Please sign in to comment.