'Sets a particular attribute of var if the provided value is
not None.
Checks if value is None. If not, set the attribute of the var
to be the default value. This is necessary for the special
required fields of the protocol buffer.
:param
var: the variable of which one of the attribute is being set.
attr: the attribute that is being set.
value: the value that is being checked
vartype: the expected type of the attr'

def set_attr_safely(self, var, attr, value, vartype):

    supported_types = [int, long, str]
    if (vartype in supported_types):
        if (value is None):
            value = vartype()
        else:
            assert isinstance(value, vartype), ('Unexpected    type    %s    for    attr    %s,    should    be    %s' % (type(value), attr, vartype))
        setattr(var, attr, value)
