how to make friends python with win32 api

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

Upload: open-it

Post on 15-Apr-2017

139 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: How to make friends python with win32 api

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

What was expected

Page 3: How to make friends python with win32 api

Terms

Page 4: How to make friends python with win32 api

Real world

?

Page 5: How to make friends python with win32 api

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

Envisioned solution

Page 7: How to make friends python with win32 api

Or...

Page 8: How to make friends python with win32 api

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

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

Python client structure

Page 11: How to make friends python with win32 api

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

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

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

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

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

Issues:● Multithreading support● Extensibility● Not stable (?)

Page 17: How to make friends python with win32 api

Manager

Page 18: How to make friends python with win32 api

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

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

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

Use multiprocessing.Manager

class ComManager(BaseManager): pass

Page 22: How to make friends python with win32 api

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

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

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

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

Connection server solution

Page 27: How to make friends python with win32 api

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

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

Stand-alone

Page 30: How to make friends python with win32 api

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

Solutionv. 0.2

Page 32: How to make friends python with win32 api

Contacts

Linkedin: https://linkedin.com/in/eugene-lopatkin

E-mail: [email protected]

Thank you! Any questions?