Help me pick an API design (OO vs functional)

Mitya Sirenef msirenef at lightbird.net
Tue Mar 26 14:41:38 CET 2013


On 03/26/2013 05:38 AM, Michael Herrmann wrote:
> On Tuesday, March 26, 2013  12:40:45 AM UTC+1, Mitya Sirenef wrote:
 >> ...
 >>
 >> I think I would prefer context managers. I don't think it's a big
 >> problem for
 >> win users because this behaviour would be one of the first things 
documented
 >> in the start guide and would be all over example scripts, so a new user
 >> missing
 >> or forgetting it is not a realistic scenario.
 >>
 >> The advantages are that it's explicit, blocks are indented and it's
 >> impossible to
 >> miss which window is the action applied to, and at the same time 
actions are
 >> short and easy to type and read.
 >
 > Thank you for your reply. What do you think of Chris Angelico's points?


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')

>
 > He wrote:
 >> What happens at the __exit__ of the context manager? What happens if
 >> context managers are nested? I'd be inclined to the simpler option of
 >> an explicit switch (since focus doesn't really "stack" and it'd feel
 >> weird for focus to *sometimes* switch away when you're done working
 >> with one window), though the context manager syntax does have its
 >> advantages too.
 >
 > 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?
 >
 >         notepad_1 = start("Notepad")
 >         notepad_2 = start("Notepad")
 >         switch_to(notepad_1)
 >         write("Hello World!")
 >         press(CTRL + 'a', CTRL + 'c')
 >         switch_to(notepad_2)
 >         press(CTRL + 'v')
 >
 >         notepad_1 = start("Notepad")
 >         notepad_2 = start("Notepad")
 >         notepad_1.activate()
 >         write("Hello World!")
 >         press(CTRL + 'a', CTRL + 'c')
 >         notepad_2.activate()
 >         press(CTRL + 'v')
 >
 > I somehow prefer "activate" over "focus" as in my feeling, you'd 
normally say that you focus *on* something, so it should be called 
"focus_on" or "give_focus[_to]". Can you say, in everyday English, that 
you "focus a window"? I'm not a native speaker so maybe my feeling is 
misguided.


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.

HTH, -m



-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/

Food is an important part of a balanced diet.
Fran Lebowitz




More information about the Python-list mailing list