소스 검색

Examples, parsing CIM port data, defer examples.

Steve Thielemann 5 년 전
부모
커밋
655c17a181
3개의 변경된 파일151개의 추가작업 그리고 0개의 파일을 삭제
  1. 54 0
      defer_alreadycallederror.py
  2. 67 0
      defer_errback.py
  3. 30 0
      try.py

+ 54 - 0
defer_alreadycallederror.py

@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+
+
+# https://twistedmatrix.com/documents/16.2.0/core/howto/defer.html
+
+from twisted.internet import reactor, defer
+from pprint import pprint
+
+def getData(inputData):
+    print('getData({0})'.format(inputData))
+    deferred = defer.Deferred()
+    # simulate a delayed result by asking reactor
+    # to fire in 2 seconds with inputData * 3 as result
+    print("return result (x*3) in 2 sec...")
+    print(deferred)
+    reactor.callLater(2, deferred.callback, inputData * 3)
+    print(deferred)
+    reactor.callLater(3, deferred.callback, inputData * 3)
+    print(deferred)
+    return deferred
+
+def cbPrint(result):
+    print("cbPrint({0})".format(result))
+    # doing eferred, or the return result + 1 works
+    print("return result (x+1) in 1 sec...")
+    deferred = defer.Deferred()
+    reactor.callLater(1, deferred.callback, result + 1)
+    return deferred
+    # return result + 1
+
+def cbPrint2(result):
+    print("cbPrint2({0})".format(result))
+    print("return result (x+2) ...")
+    # print("(this isn't a defer)")    # it is now!  :P
+    # return a Deferred object already called back with the value of result
+    return defer.succeed(result + 2)
+    # return result + 2
+
+d = getData(3)
+# Ok, chaining callbacks is ... different.
+
+d.addCallback(cbPrint)
+# Before return result, this was printing None.  
+d.addCallback(cbPrint2)
+d.addCallback(cbPrint)
+d.addCallback(print)
+
+print("Stop the reactor in 7 seconds...")
+reactor.callLater(7, reactor.stop)
+print("starting the reactor")
+reactor.run()
+
+
+print("Reactor has been safely shutdown.  :P")

+ 67 - 0
defer_errback.py

@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+
+
+# https://twistedmatrix.com/documents/16.2.0/core/howto/defer.html
+
+from twisted.internet import reactor, defer
+from twisted.python.failure import Failure
+from pprint import pprint
+
+def getData(inputData):
+    print('getData({0})'.format(inputData))
+    deferred = defer.Deferred()
+    # simulate a delayed result by asking reactor
+    # to fire in 2 seconds with inputData * 3 as result
+    print("return result in 1 sec...")
+    reactor.callLater(1, deferred.callback, inputData+1)
+    return deferred
+
+def cbPrint(result):
+    print("cbPrint({0})".format(result))
+    # doing eferred, or the return result + 1 works
+    print("return result (x+1) in 1 sec...")
+    deferred = defer.Deferred()
+    if result % 2 == 0:
+        reactor.callLater(1, deferred.callback, result + 1)
+    else:
+        reactor.callLater(2, deferred.errback, ValueError("You used an odd number!"))
+    return deferred
+    # return result + 1
+
+def ePrint(result):
+    """ Errback message received here.
+    
+    However, I get little to no control over this.
+    I don't get anything (other then the error itself),
+    so I can't do much with the deferred.
+
+    I'm not sure how this is useful.
+    """
+
+    print("ERROR:", result)
+    # return twisted.python.failure.Failure
+    # return Failure
+    # return ValueError("Errback handlying sucks...")
+
+d = getData(1)
+# Ok, chaining callbacks is ... different.
+
+d.addCallback(cbPrint)
+# d.addErrback(ePrint)
+
+d.addCallback(cbPrint)
+# d.addErrback(ePrint)
+
+d.addCallback(cbPrint)
+# d.addErrback(ePrint)
+
+d.addCallback(cbPrint)
+d.addErrback(ePrint)
+
+print("Stop the reactor in 15 seconds...")
+reactor.callLater(15, reactor.stop)
+print("starting the reactor")
+reactor.run()
+
+
+print("Reactor has been safely shutdown.  :P")

+ 30 - 0
try.py

@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+
+import re
+from pprint import pprint
+
+for line in [
+'    2   1950 100% - 1050 100%   2780 100%',
+'    5   2800 100% - 2330 100% - 1230 100%',
+'    8   2890 100%   1530 100% - 2310 100%',
+'    9 - 2160 100%   2730 100% - 2120 100%',
+'  324 - 2800 100%   2650 100% - 2490 100%',
+'  492    990 100%    900 100%   1660 100%',
+'  890   1920 100% - 2140 100%   1480 100%',
+' 1229 - 2870 100% - 1266  90%    728  68%',
+' 1643 - 3000 100% - 3000 100% - 3000 100%',
+' 1683 - 1021  97%   1460 100% - 2620 100%',
+' 1898 - 1600 100% - 1940 100% - 1860 100%',
+' 2186   1220 100% -  900 100% - 1840 100%',
+' 2194   2030 100% - 1460 100% - 1080 100%',
+' 2577   2810 100% - 1550 100% - 2350 100%',
+' 2629   2570 100% - 2270 100% - 1430 100%',
+' 3659 - 1720 100%   1240 100% - 2760 100%',
+' 3978 -  920 100%   2560 100% - 2590 100%',
+' 4302    348  25% - 2530 100% -  316  23%',
+' 4516 - 1231  60% - 1839  75%      7   0%']:
+
+    work = line.replace('%', '')
+    parts = re.split(r"(?<=\d)\s", work)
+    print(work)
+    pprint(parts)