Help me pick an API design (OO vs functional)

Mitya Sirenef msirenef at
Tue Mar 26 23:01:08 CET 2013

On 03/26/2013 10:59 AM, Michael Herrmann wrote:
> On Tuesday, March 26, 2013  2:41:38 PM UTC+1, Mitya Sirenef wrote:
 >> ...
 >> At the __exit__, further commands are no longer routed to that window;
 >> if it was a nested context, window is switched to the outer context,
 >> WHEN there are commands in it (i.e. on the first command). This seems
 >> pretty intuitive to me:
 >> with notepad1:
 >>      ^S
 >>      with notepad2:
 >>          ^S
 >>      write('something')
 >> ...
 >>  > What I am most afraid of: that the window that's currently the
 >>  > context "disappears":
 >>  >     notepad = start("Notepad")
 >>  >     with notepad:
 >>  >         press(ALT + TAB)
 >>  >         write("Am I in Notepad now?")
 >> Alt-tab needs to be handled by a wrapper function that gives you the
 >> object of the window you've switched to:
 >> otherwin = alt_tab()
 >> with otherwin:
 >>      ...
 >> If window is changed within 'with' block, the rest of block should be
 >> ignored. Perhaps there could also be a way to switch this behaviour off,
 >> for the entire script or for current block only.
 >>  > What do you think of designs #3 and #4?
 >>  > ...
 >> These are ok, too, but I feel it's much easier to send commands to a
 >> wrong window vs. context managers. The same command in a different
 >> window can have vastly different and dangerous effect. In other python
 >> code that's generally not common at all, and would be bad style:
 >> lst = lst1
 >> lst.append('x')
 >> del lst[3]
 >> lst.insert(0, 'a')
 >> lst = lst2
 >> del lst[2]
 >> lst.append('y')
 >> lst = lst3
 >> lst.insert(0, 'x')
 >> lst += [1,2]
 >> I think current window should also be acquired explicitly:
 >> with get_current_window():
 >>      type("some kind of snippet")
 >> For usage when a command should apply to all types of windows.
 > I was skeptical of your suggestion at first but trying it out on an 
example script made me see its appeal:
 >     notepad_main = start("Notepad")
 >     with notepad_main:
 >         write("Hello World!")
 >         save_dialogue = press(CTRL + 's')
 >         with save_dialogue:
 >             write("test.txt", into="File name")
 >             click("Save")
 >         click("Close")
 > Forcing the library user to always use the "with ..." seems like 
overkill though. I think the gained precision does not justify this 
burden on the library user. Hm....

I don't see why that's a big deal, I've used AHK extensively and in my
experience you don't switch windows all that often. I think it's best to
optimize to have easy to type and read commands while you're working in
the same window.

I think you could argue that dialogs that belong to the main window
should be handled implicitly, though. I think for other windows it'd
definitely be good to use context managers, but for quick/simple dialogs
it's too much hassle, although for large, complex dialogs that have
inner tabs and require a lot of work, it again starts to make sense.

At the very least, for small dialogs it's sipmpler to do:

with press(CTRL + 's'):
     write("test.txt", into="File name")


Lark's Tongue Guide to Python:

Calamities are of two kinds: misfortunes to ourselves, and good fortune 
to others.
Ambrose Bierce, The Devil's Dictionary

More information about the Python-list mailing list