Patchwork [1,of,3] templater: implement __len__ for _hybrid

login
register
mail settings
Submitter Anton Shestakov
Date Sept. 8, 2014, 12:03 p.m.
Message ID <3295511410177818@web19g.yandex.ru>
Download mbox | patch
Permalink /patch/5724/
State Accepted
Headers show

Comments

Anton Shestakov - Sept. 8, 2014, 12:03 p.m.
# HG changeset patch
# User Anton Shestakov <engored@ya.ru>
# Date 1410174778 -32400
#      Mon Sep 08 20:12:58 2014 +0900
# Node ID 9bb0ab35e90ac9ebf6adbd6e503f3412d9f2adb7
# Parent  5c153c69fdb28ff5b1bf6578e5f07c50bf25833c
templater: implement __len__ for _hybrid

Objects of class _hybrid are returned by such template keywords as children,
bookmarks, tags and others, and also by revset() template function. They are
representing "list of strings" (as hg help template says) for use in templates.
So it would be logical to implement a handy way to count the number of strings
in such list, and that's what __len__ method does.
Pierre-Yves David - Sept. 9, 2014, 9:15 a.m.
On 09/08/2014 01:03 PM, Anton Shestakov wrote:
> # HG changeset patch
> # User Anton Shestakov <engored@ya.ru>
> # Date 1410174778 -32400
> #      Mon Sep 08 20:12:58 2014 +0900
> # Node ID 9bb0ab35e90ac9ebf6adbd6e503f3412d9f2adb7
> # Parent  5c153c69fdb28ff5b1bf6578e5f07c50bf25833c
> templater: implement __len__ for _hybrid

Patch 1 is pushed to the clowncopter. I'm eager to see a V2 of the rest 
of this series with mads comment applied.

Patch

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -26,6 +26,8 @@  class _hybrid(object):
     def __call__(self):
         for x in self.values:
             yield x
+    def __len__(self):
+        return len(self.values)
 
 def showlist(name, values, plural=None, element=None, **args):
     if not element: