def produce(self, obj, val, ctx=None):
        """ factory function to create primitives

        :param pyswagger.spec.v2_0.objects.Schema obj: spec to construct primitives
        :param val: value to construct primitives

        :return: the created primitive
        """
        val = obj.default if val == None else val
        if val == None:
            return None

        obj = deref(obj)
        ctx = {} if ctx == None else ctx
        if 'name' not in ctx and hasattr(obj, 'name'):
            ctx['name'] = obj.name
        if 'guard' not in ctx:
            ctx['guard'] = CycleGuard()
        if 'addp_schema' not in ctx:
            # Schema Object of additionalProperties
            ctx['addp_schema'] = None
        if 'addp' not in ctx:
            # additionalProperties
            ctx['addp'] = False
        if '2nd_pass' not in ctx:
            # 2nd pass processing function
            ctx['2nd_pass'] = None
        if 'factory' not in ctx:
            # primitive factory
            ctx['factory'] = self
        if 'read' not in ctx:
            # default is in 'read' context
            ctx['read'] = True

        # cycle guard
        ctx['guard'].update(obj)

        ret = None
        if obj.type:
            creater, _2nd = self.get(_type=obj.type, _format=obj.format)
            if not creater:
                raise ValueError('Can\'t resolve type from:(' + str(obj.type) + ', ' + str(obj.format) + ')')

            ret = creater(obj, val, ctx)
            if _2nd:
                val = _2nd(obj, ret, val, ctx)
                ctx['2nd_pass'] = _2nd
        elif len(obj.properties) or obj.additionalProperties:
            ret = Model()
            val = ret.apply_with(obj, val, ctx)

        if isinstance(ret, (Date, Datetime, Byte, File)):
            # it's meanless to handle allOf for these types.
            return ret

        def _apply(o, r, v, c):
            if hasattr(ret, 'apply_with'):
                v = r.apply_with(o, v, c)
            else:
                _2nd = c['2nd_pass']
                if _2nd == None:
                    _, _2nd = self.get(_type=o.type, _format=o.format)
                if _2nd:
                    _2nd(o, r, v, c)
                    # update it back to context
                    c['2nd_pass'] = _2nd
            return v

        # handle allOf for Schema Object
        allOf = getattr(obj, 'allOf', None)
        if allOf:
            not_applied = []
            for a in allOf:
                a = deref(a)
                if not ret:
                    # try to find right type for this primitive.
                    ret = self.produce(a, val, ctx)
                    is_member = hasattr(ret, 'apply_with')
                else:
                    val = _apply(a, ret, val, ctx)

                if not ret:
                    # if we still can't determine the type,
                    # keep this Schema object for later use.
                    not_applied.append(a)
            if ret:
                for a in not_applied:
                    val = _apply(a, ret, val, ctx)

        if ret != None and hasattr(ret, 'cleanup'):
            val = ret.cleanup(val, ctx)

        return ret
def produce ( self , obj , val , ctx = None ) : val = obj . default if val == None else val if val == None : return None obj = deref ( obj ) ctx = { } if ctx == None else ctx if 'name' not in ctx and hasattr ( obj , 'name' ) : ctx [ 'name' ] = obj . name if 'guard' not in ctx : ctx [ 'guard' ] = CycleGuard ( ) if 'addp_schema' not in ctx : ctx [ 'addp_schema' ] = None if 'addp' not in ctx : ctx [ 'addp' ] = False if '2nd_pass' not in ctx : ctx [ '2nd_pass' ] = None if 'factory' not in ctx : ctx [ 'factory' ] = self if 'read' not in ctx : ctx [ 'read' ] = True ctx [ 'guard' ] . update ( obj ) ret = None if obj . type : creater , _2nd = self . get ( _type = obj . type , _format = obj . format ) if not creater : raise ValueError ( 'Can\'t resolve type from:(' + str ( obj . type ) + ', ' + str ( obj . format ) + ')' ) ret = creater ( obj , val , ctx ) if _2nd : val = _2nd ( obj , ret , val , ctx ) ctx [ '2nd_pass' ] = _2nd elif len ( obj . properties ) or obj . additionalProperties : ret = Model ( ) val = ret . apply_with ( obj , val , ctx ) if isinstance ( ret , ( Date , Datetime , Byte , File ) ) : return ret def _apply ( o , r , v , c ) : if hasattr ( ret , 'apply_with' ) : v = r . apply_with ( o , v , c ) else : _2nd = c [ '2nd_pass' ] if _2nd == None : _ , _2nd = self . get ( _type = o . type , _format = o . format ) if _2nd : _2nd ( o , r , v , c ) c [ '2nd_pass' ] = _2nd return v allOf = getattr ( obj , 'allOf' , None ) if allOf : not_applied = [ ] for a in allOf : a = deref ( a ) if not ret : ret = self . produce ( a , val , ctx ) is_member = hasattr ( ret , 'apply_with' ) else : val = _apply ( a , ret , val , ctx ) if not ret : not_applied . append ( a ) if ret : for a in not_applied : val = _apply ( a , ret , val , ctx ) if ret != None and hasattr ( ret , 'cleanup' ) : val = ret . cleanup ( val , ctx ) return ret
factory function to create primitives

        :param pyswagger.spec.v2_0.objects.Schema obj: spec to construct primitives
        :param val: value to construct primitives

        :return: the created primitive