Skip to content

Commit b93b2da

Browse files
committed
Code golf async lambdas
1 parent 4fa6020 commit b93b2da

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

chess/engine.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,6 +2696,10 @@ async def popen_xboard(command: Union[str, List[str]], *, setpgrp: bool = False,
26962696
return transport, protocol
26972697

26982698

2699+
async def _async(sync: Callable[[], T]) -> T:
2700+
return sync()
2701+
2702+
26992703
class SimpleEngine:
27002704
"""
27012705
Synchronous wrapper around a transport and engine protocol pair. Provides
@@ -2738,20 +2742,16 @@ def _not_shut_down(self) -> Generator[None, None, None]:
27382742

27392743
@property
27402744
def options(self) -> MutableMapping[str, Option]:
2741-
async def _get() -> MutableMapping[str, Option]:
2742-
return copy.copy(self.protocol.options)
2743-
27442745
with self._not_shut_down():
2745-
future = asyncio.run_coroutine_threadsafe(_get(), self.protocol.loop)
2746+
coro = _async(lambda: copy.copy(self.protocol.options))
2747+
future = asyncio.run_coroutine_threadsafe(coro, self.protocol.loop)
27462748
return future.result()
27472749

27482750
@property
27492751
def id(self) -> Mapping[str, str]:
2750-
async def _get() -> Mapping[str, str]:
2751-
return self.protocol.id.copy()
2752-
27532752
with self._not_shut_down():
2754-
future = asyncio.run_coroutine_threadsafe(_get(), self.protocol.loop)
2753+
coro = _async(lambda: self.protocol.id.copy())
2754+
future = asyncio.run_coroutine_threadsafe(coro, self.protocol.loop)
27552755
return future.result()
27562756

27572757
def communicate(self, command_factory: Callable[[Protocol], BaseCommand[Protocol, T]]) -> T:
@@ -2877,20 +2877,16 @@ def __init__(self, simple_engine: SimpleEngine, inner: AnalysisResult) -> None:
28772877

28782878
@property
28792879
def info(self) -> InfoDict:
2880-
async def _get() -> InfoDict:
2881-
return self.inner.info.copy()
2882-
28832880
with self.simple_engine._not_shut_down():
2884-
future = asyncio.run_coroutine_threadsafe(_get(), self.simple_engine.protocol.loop)
2881+
coro = _async(lambda: self.inner.info.copy())
2882+
future = asyncio.run_coroutine_threadsafe(coro, self.simple_engine.protocol.loop)
28852883
return future.result()
28862884

28872885
@property
28882886
def multipv(self) -> List[InfoDict]:
2889-
async def _get() -> List[InfoDict]:
2890-
return [info.copy() for info in self.inner.multipv]
2891-
28922887
with self.simple_engine._not_shut_down():
2893-
future = asyncio.run_coroutine_threadsafe(_get(), self.simple_engine.protocol.loop)
2888+
coro = _async(lambda: [info.copy() for info in self.inner.multipv])
2889+
future = asyncio.run_coroutine_threadsafe(coro, self.simple_engine.protocol.loop)
28942890
return future.result()
28952891

28962892
def stop(self) -> None:
@@ -2903,19 +2899,13 @@ def wait(self) -> BestMove:
29032899
return future.result()
29042900

29052901
def would_block(self) -> bool:
2906-
async def _would_block() -> bool:
2907-
return self.inner.would_block()
2908-
29092902
with self.simple_engine._not_shut_down():
2910-
future = asyncio.run_coroutine_threadsafe(_would_block(), self.simple_engine.protocol.loop)
2903+
future = asyncio.run_coroutine_threadsafe(_async(self.inner.would_block), self.simple_engine.protocol.loop)
29112904
return future.result()
29122905

29132906
def empty(self) -> bool:
2914-
async def _empty() -> bool:
2915-
return self.inner.empty()
2916-
29172907
with self.simple_engine._not_shut_down():
2918-
future = asyncio.run_coroutine_threadsafe(_empty(), self.simple_engine.protocol.loop)
2908+
future = asyncio.run_coroutine_threadsafe(_async(self.inner.empty), self.simple_engine.protocol.loop)
29192909
return future.result()
29202910

29212911
def get(self) -> InfoDict:

0 commit comments

Comments
 (0)