How to make friends python with win32 api
-
Upload
open-it -
Category
Technology
-
view
145 -
download
1
Transcript of How to make friends python with win32 api
![Page 1: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/1.jpg)
How to make friends of Python and Win32 API
Connecting the Python to a Win32 API provided by a stock broker
![Page 2: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/2.jpg)
What was expected
![Page 3: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/3.jpg)
Terms
![Page 4: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/4.jpg)
Real world
?
![Page 5: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/5.jpg)
Issues
● COM to pure Python object conversion● Flow (events, async methods) handling● Multithreading support● Extensibility
![Page 6: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/6.jpg)
Envisioned solution
![Page 7: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/7.jpg)
Or...
![Page 8: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/8.jpg)
What I’ve found
● pywin32 package on sourceforge.net by Mark Hammond● python interpreter builds: ActiveState, Anaconda, Canopy
with pywin32● Python Programming On Win32 by Mark Hammond and
Andy Robinson● no actual documentation on activestate.com
![Page 9: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/9.jpg)
Pywin32 workflow
Make Handle Events class
Make client object with DispatchWithEvents method
Use API methods on python object
![Page 10: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/10.jpg)
Python client structure
![Page 11: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/11.jpg)
Event class
class ClientEvents: def OnAddBar(self, row, nrows, symbol, interval, datetime, open, high, low, close, volume, open_int): pass # some logic …
![Page 12: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/12.jpg)
Client
clnt = client.DispatchWithEvents('COM.Server.1', ClientEvents)…clnt.GetBars('SBER', bar_interval, date_time_object, 10)
![Page 13: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/13.jpg)
Cool tool
Make folder python_folder\Lib\site-packages\win32com\gen_py
Run 'win32com\client\makepy.py' (eg, run it from the command window, or double-click on it) and a list will be presented. Select the Type Library.
It will generate python wrapper classes from COM Library with all available methods.
![Page 14: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/14.jpg)
Convert DateTime objects
def pytime_2_datetime(pythime): return dt(year=pythime.year, month=pythime.month, day=pythime.day, hour=pythime.hour, minute=pythime.minute, second=pythime.second)def datetime_2_pytime(datetime): return pywintypes.Time(time.mktime(datetime.timetuple()))
![Page 15: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/15.jpg)
Solution v. 0.1● COM to pure
Python object conversion
● Flow (events, async methods) handling
![Page 16: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/16.jpg)
Issues:● Multithreading support● Extensibility● Not stable (?)
![Page 17: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/17.jpg)
Manager
![Page 18: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/18.jpg)
Workflow
Add event queue
Create custom Manager
Make proxy to COM Object
Get pure python Events queue
Use proxy and queue from other threads
![Page 19: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/19.jpg)
Modify Event class, part 1
class ClientEvents: def __init__(self): self.event_queue = Manager().Queue() def get_event_queue(self): return self.event_queue …
![Page 20: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/20.jpg)
Modify Event class, part 2
class ClientEvents: … def OnAddBar(self, row, nrows, symbol, interval, datetime, open, high, low, close, volume, open_int): self.event_queue.put_nowait(('AddBar', row, nrows, symbol, interval, pytime_2_datetime(datetime), open, high, low, close, volume, open_int))
![Page 21: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/21.jpg)
Use multiprocessing.Manager
class ComManager(BaseManager): pass
![Page 22: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/22.jpg)
Add client method
def get_com_server(): CoInitializeEx(COINIT_MULTITHREADED) clnt = client.DispatchWithEvents('COM.Server.1', ClientEvents) CoUninitialize() return clnt
![Page 23: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/23.jpg)
Register methods
ComManager.register('get_com_server',callable=get_com_server,exposed=('CancelBidAsks', 'CancelOrder', 'GetBars', 'GetMoneyAccount', …, 'get_event_queue'))
![Page 24: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/24.jpg)
Fix DateTime bug, part 1
class ClientEvents: def GetBarsSer(self, symbol, interval, since, count): self.GetBars(symbol, interval, datetime_2_pytime(since), count)
![Page 25: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/25.jpg)
Fix DateTime bug, part 2
ComManager.register('get_com_server',callable=get_com_server,exposed=('CancelBidAsks', 'CancelOrder', 'GetBars', 'GetMoneyAccount', …, 'get_event_queue', 'GetBarsSer'))
![Page 26: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/26.jpg)
Connection server solution
![Page 27: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/27.jpg)
Use as server, server code
if __name__ == '__main__': freeze_support() m = ComManager(address=('127.0.0.1', port,authkey='authkey').get_server().serve_forever()
![Page 28: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/28.jpg)
Use as server, client code
…manager = ComManager(address='address', port, authkey='authkey')manager.connect()com_server =manager.get_com_server()com_event_queue = com_server.get_event_queue()
![Page 29: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/29.jpg)
Stand-alone
![Page 30: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/30.jpg)
In code use
from package_mame import ComManagerif __name__ == '__main__': freeze_support() manager = ComManager() manager.start() com_server = manager.get_com_server() com_event_queue = com_server.get_event_queue()
![Page 31: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/31.jpg)
Solutionv. 0.2
![Page 32: How to make friends python with win32 api](https://reader034.fdocuments.us/reader034/viewer/2022052213/58f1285c1a28ab16338b45dd/html5/thumbnails/32.jpg)
Contacts
Linkedin: https://linkedin.com/in/eugene-lopatkin
E-mail: [email protected]
Thank you! Any questions?