How to make friends python with win32 api

52 %
48 %
Information about How to make friends python with win32 api

Published on December 17, 2016

Author: EugeneLopatkin


1. How to make friends of Python and Win32 API Connecting the Python to a Win32 API provided by a stock broker

2. What was expected

3. Terms

4. Real world ?

5. Issues COM to pure Python object conversion Flow (events, async methods) handling Multithreading support Extensibility

6. Envisioned solution

7. Or...

8. What I’ve found pywin32 package on 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

9. Pywin32 workflow Make Handle Events class Make client object with DispatchWithEve nts method Use API methods on python object

10. Python client structure

11. Event class class ClientEvents: def OnAddBar(self, row, nrows, symbol, interval, datetime, open, high, low, close, volume, open_int): pass # some logic …

12. Client clnt = client.DispatchWithEvents('COM.Server.1', ClientEvents) … clnt.GetBars('SBER', bar_interval, date_time_object, 10)

13. Cool tool Make folder python_folderLibsite- packageswin32comgen_py Run '' (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.

14. Convert DateTime objects def pytime_2_datetime(pythime): return dt(year=pythime.year, month=pythime.month,, hour=pythime.hour, minute=pythime.minute, second=pythime.second) def datetime_2_pytime(datetime): return pywintypes.Time(time .mktime(datetime.timetuple()))

15. Solution v. 0.1 COM to pure Python object conversion Flow (events, async methods) handling

16. Issues: Multithreading support Extensibility Not stable (?)

17. Manager

18. Workflow Add event queue Create custom Manager Make proxy to COM Object Get pure python Events queue Use proxy and queue from other threads

19. Modify Event class, part 1 class ClientEvents: def __init__(self): self.event_queue = Manager().Queue() def get_event_queue(self): return self.event_queue …

20. 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))

21. Use multiprocessing.Manager class ComManager(BaseManager): pass

22. Add client method def get_com_server(): CoInitializeEx(COINIT_MULTITHREADED) clnt = client.DispatchWithEvents('COM.Server.1', ClientEvents) CoUninitialize() return clnt

23. Register methods ComManager.register('get_com_server', callable=get_com_server, exposed=('CancelBidAsks', 'CancelOrder', 'GetBars', 'GetMoneyAccount', …, 'get_event_queue'))

24. Fix DateTime bug, part 1 class ClientEvents: def GetBarsSer(self, symbol, interval, since, count): self.GetBars(symbol, interval, datetime_2_pytime(since), count)

25. Fix DateTime bug, part 2 ComManager.register('get_com_server', callable=get_com_server, exposed=('CancelBidAsks', 'CancelOrder', 'GetBars', 'GetMoneyAccount', …, 'get_event_queue', 'GetBarsSer'))

26. Connection server solution

27. Use as server, server code if __name__ == '__main__': freeze_support() m = ComManager(address=('', port, authkey='authkey').get_server() .serve_forever()

28. 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()

29. Stand-alone

30. In code use from package_mame import ComManager if __name__ == '__main__': freeze_support() manager = ComManager() manager.start() com_server = manager.get_com_server() com_event_queue = com_server.get_event_queue()

31. Solution v. 0.2

32. Contacts Linkedin: E-mail: Thank you! Any questions?

Add a comment

Related pages

winapi - How to use win32 API's with python? - Stack Overflow

How can I use win32 API in Python? What is the best and easiest way to do it? Can you please provide some examples?
Read more

winapi - Windows Printer Dialog using python win32 api ...

Windows Printer Dialog using python win32 api. ... Selection of the GUI window for sending keystrokes using win32 api in python 3 ... Taking up friends with a private ...
Read more

[python-win32] Child Windows - Grokbase

(6 replies) Hello, I am trying to make a program that sends text to notepad using win32api.SendMessage function. The thing is that I don't know how to get the notepad ...
Read more

[python-win32] Child Windows

... timr at > To: python-win32 at > Subject: Re: [python-win32] ... with the Win32 API, ... your mail contacts to join your friends list with ...
Read more

API Integration in Python - part 1 - Real Python

In this tutorial we'll detail how to develop a RESTful API with Python in a fun and easy manner. ... API Integration in Python ... How To Make Friends And Influence APIs.
Read more

Python for Windows Extensions download |

Download Python for Windows Extensions for free. Python extensions for Windows. None
Read more

Python for Windows Extensions - Browse Files at ...

Python for Windows Extensions. ... Note that there is one download package for each supported version of Python ... (Intel)] on win32 ...
Read more

Python Releases for Windows |

The official home of the Python Programming Language
Read more