def _ReferenceFromPairs(pairs, reference=None, app=None, namespace=None):
if reference is None:<EOL><INDENT>reference = entity_pb.Reference()<EOL><DEDENT>path = reference.mutable_path()<EOL>last = False<EOL>for kind, idorname in pairs:<EOL><INDENT>if last:<EOL><INDENT>raise datastore_errors.BadArgumentError(<EOL>'<STR_LIT>')<EOL><DEDENT>t = type(kind)<EOL>if t is str:<EOL><INDENT>pass<EOL><DEDENT>elif t is str:<EOL><INDENT>kind = kind.encode('<STR_LIT:utf8>')<EOL><DEDENT>else:<EOL><INDENT>if issubclass(t, type):<EOL><INDENT>from .model import Model<EOL>modelclass = kind<EOL>if not issubclass(modelclass, Model):<EOL><INDENT>raise TypeError('<STR_LIT>'<EOL>'<STR_LIT>' % modelclass)<EOL><DEDENT>kind = modelclass._get_kind()<EOL>t = type(kind)<EOL><DEDENT>if t is str:<EOL><INDENT>pass<EOL><DEDENT>elif t is str:<EOL><INDENT>kind = kind.encode('<STR_LIT:utf8>')<EOL><DEDENT>elif issubclass(t, str):<EOL><INDENT>pass<EOL><DEDENT>elif issubclass(t, str):<EOL><INDENT>kind = kind.encode('<STR_LIT:utf8>')<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>'<EOL>'<STR_LIT>' % kind)<EOL><DEDENT><DEDENT>if not (<NUM_LIT:1> <= len(kind) <= _MAX_KEYPART_BYTES):<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>' %<EOL>(_MAX_KEYPART_BYTES, kind))<EOL><DEDENT>elem = path.add_element()<EOL>elem.set_type(kind)<EOL>t = type(idorname)<EOL>if t is int or t is int:<EOL><INDENT>if not (<NUM_LIT:1> <= idorname < _MAX_LONG):<EOL><INDENT>raise ValueError('<STR_LIT>' % idorname)<EOL><DEDENT>elem.set_id(idorname)<EOL><DEDENT>elif t is str:<EOL><INDENT>if not (<NUM_LIT:1> <= len(idorname) <= _MAX_KEYPART_BYTES):<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>' %<EOL>(_MAX_KEYPART_BYTES, idorname))<EOL><DEDENT>elem.set_name(idorname)<EOL><DEDENT>elif t is str:<EOL><INDENT>idorname = idorname.encode('<STR_LIT:utf8>')<EOL>if not (<NUM_LIT:1> <= len(idorname) <= _MAX_KEYPART_BYTES):<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>' %<EOL>(_MAX_KEYPART_BYTES, idorname))<EOL><DEDENT>elem.set_name(idorname)<EOL><DEDENT>elif idorname is None:<EOL><INDENT>last = True<EOL><DEDENT>elif issubclass(t, (int, int)):<EOL><INDENT>if not (<NUM_LIT:1> <= idorname < _MAX_LONG):<EOL><INDENT>raise ValueError('<STR_LIT>' % idorname)<EOL><DEDENT>elem.set_id(idorname)<EOL><DEDENT>elif issubclass(t, str):<EOL><INDENT>if issubclass(t, str):<EOL><INDENT>idorname = idorname.encode('<STR_LIT:utf8>')<EOL><DEDENT>if not (<NUM_LIT:1> <= len(idorname) <= _MAX_KEYPART_BYTES):<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>' % (_MAX_KEYPART_BYTES, idorname))<EOL><DEDENT>elem.set_name(idorname)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>'<EOL>'<STR_LIT>' % idorname)<EOL><DEDENT><DEDENT>if not app:<EOL><INDENT>app = _DefaultAppId()<EOL><DEDENT>reference.set_app(app)<EOL>if namespace is None:<EOL><INDENT>namespace = _DefaultNamespace()<EOL><DEDENT>if namespace:<EOL><INDENT>reference.set_name_space(namespace)<EOL><DEDENT>return reference<EOL>
Construct a Reference from a list of pairs.

    If a Reference is passed in as the second argument, it is modified
    in place.  The app and namespace are set from the corresponding
    keyword arguments, with the customary defaults.