From 1e4ea4004212c0ed7668be40f71a2b32f3d181a2 Mon Sep 17 00:00:00 2001 From: Lucas Moura Date: Tue, 28 Jun 2016 01:14:44 -0300 Subject: [PATCH] Use defaultdict for _UID_PREFIXES The method get_uid on common.py first check if a prefix is in _UID_PREFIXED dict and if it is not, a variable is added to the dict. However, using a defaultdict, this check is no longer necessary. --- keras/backend/common.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/keras/backend/common.py b/keras/backend/common.py index 80a487a64672..deb177abfe70 100644 --- a/keras/backend/common.py +++ b/keras/backend/common.py @@ -1,9 +1,11 @@ import numpy as np +from collections import defaultdict + # the type of float to use throughout the session. _FLOATX = 'float32' _EPSILON = 10e-8 -_UID_PREFIXES = {} +_UID_PREFIXES = defaultdict(int) _IMAGE_DIM_ORDERING = 'th' @@ -60,9 +62,5 @@ def set_image_dim_ordering(dim_ordering): def get_uid(prefix=''): - if prefix not in _UID_PREFIXES: - _UID_PREFIXES[prefix] = 1 - return 1 - else: - _UID_PREFIXES[prefix] += 1 - return _UID_PREFIXES[prefix] + _UID_PREFIXES[prefix] += 1 + return _UID_PREFIXES[prefix]