[lxc-devel] [PATCH 2/3] python: Use builtin len() function for network interfaces

Serge Hallyn serge.hallyn at canonical.com
Mon Nov 26 17:15:29 UTC 2012


Quoting Stéphane Graber (stgraber at ubuntu.com):
> Use our own len() function for network interfaces as doing
> len(container.get_config_item("lxc.network")) will fail when the
> list is empty.
> 
> Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>

Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>

> ---
>  src/python-lxc/lxc/__init__.py | 14 +++++++++-----
>  1 file changed, 9 insertions(+), 5 deletions(-)
> 
> diff --git a/src/python-lxc/lxc/__init__.py b/src/python-lxc/lxc/__init__.py
> index c572e10..78852ec 100644
> --- a/src/python-lxc/lxc/__init__.py
> +++ b/src/python-lxc/lxc/__init__.py
> @@ -115,23 +115,27 @@ class ContainerNetworkList():
>          self.container = container
>  
>      def __getitem__(self, index):
> -        count = len(self.container.get_config_item("lxc.network"))
> -        if index >= count:
> +        if index >= len(self):
>              raise IndexError("list index out of range")
>  
>          return ContainerNetwork(self.container, index)
>  
>      def __len__(self):
> -        return len(self.container.get_config_item("lxc.network"))
> +        values = self.container.get_config_item("lxc.network")
> +
> +        if values:
> +            return len(values)
> +        else:
> +            return 0
>  
>      def add(self, network_type):
> -        index = len(self.container.get_config_item("lxc.network"))
> +        index = len(self)
>  
>          return self.container.set_config_item("lxc.network.%s.type" % index,
>                                                network_type)
>  
>      def remove(self, index):
> -        count = len(self.container.get_config_item("lxc.network"))
> +        count = len(self)
>          if index >= count:
>              raise IndexError("list index out of range")
>  
> -- 
> 1.8.0
> 
> 
> ------------------------------------------------------------------------------
> Monitor your physical, virtual and cloud infrastructure from a single
> web console. Get in-depth insight into apps, servers, databases, vmware,
> SAP, cloud infrastructure, etc. Download 30-day Free Trial.
> Pricing starts from $795 for 25 servers or applications!
> http://p.sf.net/sfu/zoho_dev2dev_nov
> _______________________________________________
> Lxc-devel mailing list
> Lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel




More information about the lxc-devel mailing list