Best Python code snippet using slash
operators.py
Source:operators.py  
...44            if  intersection_count > best_var_count:45                best_var_count = intersection_count46                best_var_group_name = var.group_name47    return best_var_group_name48def get_current_variation(variations, obj):49    return next((var for var in variations if var.group_name == obj.dupli_group.name), None)50class OBJECT_OT_oa_convert(bpy.types.Operator):51    bl_description = bl_label = "Convert Models"52    bl_idname = "oa.convert"53    bl_options = {'INTERNAL'}54    55    # @classmethod56    # def poll(cls, context):57    #     settings = context.scene.OASettings58    #     return any((o.OAModel.marked for o in context.selected_objects)) and \59    #         all((bool(settings.file_valid),60    #              settings.oa_file == settings.loaded_oa_file,61    #              bool(settings.models.simps_impls)))62    63    def invoke(self, context, event):64        settings = context.scene.OASettings65        models = settings.models.simps_impls66        # real67        if settings.convert_real:68            bpy.ops.object.duplicates_make_real()69        70        # local71        if settings.convert_local_all:72            bpy.ops.object.make_local(type='ALL')73        else:74            if settings.convert_local_sel_obj:75                bpy.ops.object.make_local(type='SELECT_OBJECT')76            if settings.convert_local_sel_objdata:77                bpy.ops.object.make_local(type='SELECT_OBDATA')78            if settings.convert_local_sel_objdata_mat:79                bpy.ops.object.make_local(type='SELECT_OBDATA_MATERIAL')80        # single81        if settings.convert_single_obj_data_mat_tex:82            bpy.ops.object.make_single_user(object=True, obdata=True, material=True, texture=True, animation=False)83        else:84            if settings.convert_single_obj_data:85                bpy.ops.object.make_single_user(object=True, obdata=True, material=False, texture=False, animation=False)86            else:87                bpy.ops.object.make_single_user(object=True, obdata=False, material=False, texture=False, animation=False)88            if settings.convert_single_mat_tex:89                bpy.ops.object.make_single_user(object=False, obdata=False, material=True, texture=True, animation=False)90        if settings.convert_single_anim:91            bpy.ops.object.make_single_user(object=False, obdata=False, material=False, texture=False, animation=True)92        # rm sp93        if settings.convert_rm_sp:94            for obj in context.selected_objects:95                if obj.OASnapPoints.marked:96                    context.scene.objects.unlink(obj)97                obj.OAModel.marked = False98        return {'FINISHED'}99class OBJECT_OT_oa_random_model(bpy.types.Operator):100    bl_description = bl_label = "Random Model"101    bl_idname = "oa.random_model"102    bl_options = {'INTERNAL'}103    @classmethod104    def poll(cls, context):105        settings = context.scene.OASettings106        return any((o.OAModel.marked for o in context.selected_objects)) and \107            all((bool(settings.file_valid),108                 settings.oa_file == settings.loaded_oa_file,109                 bool(settings.models.simps_impls)))110    111    def invoke(self, context, event):112        settings = context.scene.OASettings113        models = settings.models.simps_impls114        all_variations_as_group_names = set()115        for model in models:116            for var in model.variations:117                all_variations_as_group_names.update({var.group_name})118        for obj in context.selected_objects:119            if not obj.OAModel.marked:120                continue121            122            variation = choice(tuple(all_variations_as_group_names))123            obj.dupli_group = bpy.data.groups.get((variation, settings.oa_file))124        125        return {'FINISHED'}126class OBJECT_OT_oa_select(bpy.types.Operator):127    bl_description = bl_label = "Select"128    bl_idname = "oa.select"129    bl_options = {'INTERNAL'}130    select_type = StringProperty(default="Select")131    132    @classmethod133    def poll(cls, context):134        settings = context.scene.OASettings135        return settings.models.simps_impls or settings.models.bases136    def invoke(self, context, event):137        def check_tags(oa_group):138            oa_group_tags = {tag.key:tag.value for tag in oa_group.tags}139            for key, value in select_tags.items():140                if key not in oa_group_tags:141                    return False142                else:143                    if select_tags[key] != '':144                        if oa_group_tags[key] != value:145                            return False146            return True147        def check_id(oa_group):148            if not settings.select_use_id:149                return True150            else:151                if tuple(settings.select_id) == tuple(oa_group.oa_id):152                    return True153            return False154        settings = context.scene.OASettings155        select_tags = {tag.key:tag.value for tag in settings.select_tags}156        157        oa_models = set()158        for obj in context.scene.objects:159            if not obj.OAModel.marked:160                continue161            if obj.dupli_group.library.filepath != settings.loaded_oa_file:162                continue163            oa_group = obj.dupli_group.OAGroup164            if settings.select_oa_type in ('SIMP', 'SIMP_IMPL'):165                if oa_group.oa_type == 'SIMP':166                    if check_id(oa_group) and check_tags(oa_group):167                        oa_models.update({obj})168            if settings.select_oa_type in ('IMPL', 'SIMP_IMPL'):169                if oa_group.oa_type == 'IMPL':170                    if check_id(oa_group) and check_tags(oa_group):171                        oa_models.update({obj})172        if self.select_type == 'Select':173            for obj in context.scene.objects:174                obj.select = False175        for obj in oa_models:176            if self.select_type in ('Select', 'Add to Selection'):177                obj.select = True178            else:179                obj.select = False180                181        return {'FINISHED'}182class OBJECT_OT_oa_select_add_tag(bpy.types.Operator):183    bl_description = bl_label = "Add Tag"184    bl_idname = "oa.select_add_tag"185    bl_options = {'INTERNAL'}186    @classmethod187    def poll(cls, context):188        settings = context.scene.OASettings189        return settings.models.simps_impls190    def invoke(self, context, event):191        settings = context.scene.OASettings192        settings.select_tags.add()193        return {'FINISHED'}194class OBJECT_OT_oa_select_remove_tag(bpy.types.Operator):195    bl_description = bl_label = "Remove Tag"196    bl_idname = "oa.select_remove_tag"197    bl_options = {'INTERNAL'}198    tag_idx = IntProperty(default=0, min=0)199    @classmethod200    def poll(cls, context):201        settings = context.scene.OASettings202        return settings.models.simps_impls203    def invoke(self, context, event):204        settings = context.scene.OASettings205        settings.select_tags.remove(self.tag_idx)206        return {'FINISHED'}207class OBJECT_OT_oa_order_add_tag(bpy.types.Operator):208    bl_description = bl_label = "Add Tag"209    bl_idname = "oa.order_add_tag"210    bl_options = {'INTERNAL'}211    @classmethod212    def poll(cls, context):213        settings = context.scene.OASettings214        return settings.models.simps_impls215    def invoke(self, context, event):216        settings = context.scene.OASettings217        settings.order_tags.add()218        return {'FINISHED'}219class OBJECT_OT_oa_order_remove_tag(bpy.types.Operator):220    bl_description = bl_label = "Remove Tag"221    bl_idname = "oa.order_remove_tag"222    bl_options = {'INTERNAL'}223    tag_idx = IntProperty(default=0, min=0)224    @classmethod225    def poll(cls, context):226        settings = context.scene.OASettings227        return settings.models.simps_impls228    def invoke(self, context, event):229        settings = context.scene.OASettings230        settings.order_tags.remove(self.tag_idx)231        return {'FINISHED'}232class OBJECT_OT_oa_order_models(bpy.types.Operator):233    bl_description = bl_label = "Order Models"234    bl_idname = "oa.order_models"235    bl_options = {'INTERNAL'}236    237    @classmethod238    def poll(cls, context):239        settings = context.scene.OASettings240        return all((settings.order_models_start != '', settings.order_models_end != ''))241    242    def invoke(self, context, event):243        settings = context.scene.OASettings244        models = settings.models.simps_impls245        246        if len(context.selected_objects) < 1:247            self.report({'ERROR'}, "Select at least one object")248            return {'CANCELLED'}249        objs_in_scene = [obj.name for obj in context.scene.objects]250        if not settings.order_models_start in objs_in_scene:251            self.report({'ERROR'}, "Start Object not found.")252            return {'CANCELLED'}253        if not settings.order_models_end in objs_in_scene:254            self.report({'ERROR'}, "End Object not found.")255            return {'CANCELLED'}256        257        start = context.scene.objects.get(settings.order_models_start).location.copy()258        end = context.scene.objects.get(settings.order_models_end).location.copy()259        distance = (start - end).length260        order_tags_count = len(settings.order_tags)        261        for obj in context.selected_objects:262            if not obj.OAModel.marked:263                continue264            if obj.dupli_group.library.filepath != settings.loaded_oa_file:265                continue266            # map between 0 to 1; 0 is at the start-objects and 1 is at the end-object267            obj_distance = (start - obj.location).length / distance268            269            # object outside of range270            if obj_distance > 1:271                continue272            273            model = next((model for model in models if tuple(model.oa_id) == tuple(obj.dupli_group.OAGroup.oa_id)), None)274            if not model:275                continue276            277            current_variation = get_current_variation(model.variations, obj)278            variations = [var for var in model.variations]279            for v in range(1, order_tags_count + 1):280                x = obj_distance281                if eval(settings.order_function) < (1/order_tags_count) * v:282                    key = settings.order_tags[v-1].key283                    value = settings.order_tags[v-1].value284                    if key == '' or value == '':285                        break286                    287                    best_var_group_name = get_best_match_inside_model(variations, current_variation, key, value, settings.tags)288                    obj.dupli_group = bpy.data.groups.get((best_var_group_name, settings.oa_file))289                    break290            291        return {'FINISHED'}292class OBJECT_OT_oa_random_tag_value(bpy.types.Operator):293    bl_description = bl_label = "Choose Random Tag Value"294    bl_idname = "oa.random_tag_value"295    bl_options = {'INTERNAL'}296    key = StringProperty(default="")297    298    @classmethod299    def poll(cls, context):300        return any((m.OAModel.marked for m in context.selected_objects))301    302    def invoke(self, context, event):303        settings = context.scene.OASettings304        models = settings.models.simps_impls305        306        for obj in context.selected_objects:307            if not obj.OAModel.marked:308                continue309            if obj.dupli_group.library.filepath != settings.loaded_oa_file:310                continue311            312            model = next((model for model in models if tuple(model.oa_id) == tuple(obj.dupli_group.OAGroup.oa_id)), None)313            if not model:314                continue315            # check whether the tag occures316            found = False317            for var in model.variations:318                if self.key in (tag.key for tag in var.tags):319                    found = True320                    break321            if not found:322                continue323            # choose random value324            values = set()325            for var in model.variations:326                for tag in var.tags:327                    if tag.key == self.key:328                        values.update({tag.value})329            random_value = choice(tuple(values))330            best_match = get_best_match_inside_model(331                model.variations,332                get_current_variation(model.variations, obj),333                self.key,334                random_value,335                settings.tags336                )337    338            obj.dupli_group = bpy.data.groups.get((best_match, settings.oa_file))339        return {'FINISHED'}340class OBJECT_OT_oa_random_variation(bpy.types.Operator):341    bl_description = bl_label = "Random Variation"342    bl_idname = "oa.random_variation"343    bl_options = {'INTERNAL'}344    @classmethod345    def poll(cls, context):346        return any((o.OAModel.marked for o in context.selected_objects))347    348    def invoke(self, context, event):349        settings = context.scene.OASettings350        models = settings.models.simps_impls351        for obj in context.selected_objects:352            if not obj.OAModel.marked:353                continue354            if obj.dupli_group.library.filepath != settings.loaded_oa_file:355                continue356            357            model = next((model for model in models if tuple(model.oa_id) == tuple(obj.dupli_group.OAGroup.oa_id)), None)358            if not model:359                continue360    361            variation = choice(model.variations)362            obj.dupli_group = bpy.data.groups.get((variation.group_name, settings.oa_file))363        364        return {'FINISHED'}365class OBJECT_OT_oa_change_variation(bpy.types.Operator):366    bl_description = bl_label = "Change Variation"367    bl_idname = "oa.change_variation"368    bl_options = {'INTERNAL'}369    370    key = StringProperty(default="")371    value = StringProperty(default="")372    373    def invoke(self, context, event):374        settings = context.scene.OASettings375        models = settings.models.simps_impls376        377        for obj in context.selected_objects:378            if not obj.OAModel.marked:379                continue380            if obj.dupli_group.library.filepath != settings.loaded_oa_file:381                continue382            model = next((model for model in models if tuple(model.oa_id) == tuple(obj.dupli_group.OAGroup.oa_id)), None)383            if not model:384                continue385            best_match = get_best_match_inside_model(386                model.variations,387                get_current_variation(model.variations, obj),388                self.key,389                self.value,390                settings.tags391                )392            obj.dupli_group = bpy.data.groups.get((best_match, settings.oa_file))393        394        return {'FINISHED'}395class OBJECT_OT_oa_change_default_variation(bpy.types.Operator):396    bl_description = bl_label = "Change Default Variation"397    bl_idname = "oa.change_default_variation"398    bl_options = {'INTERNAL'}399    simp_impl_idx = IntProperty(default=0)400    var_idx = IntProperty(default=0)401    ...parameters.py
Source:parameters.py  
...20            params = ParameterizationInfo(func)21            @wraps(func, preserve=['__slash_fixture__'])22            def new_func(*args, **kwargs):23                # for better debugging. _current_variation gets set to None on context exit24                variation = ctx.session.variations.get_current_variation()25                for name, param in params.iter_parametrization_fixtures():26                    value = variation.get_param_value(param)27                    if name not in kwargs:28                        kwargs[name] = value29                return func(*args, **kwargs)30            setattr(new_func, _PARAM_INFO_ATTR_NAME, params)31            returned = new_func32        else:33            returned = func34        params.add_options(parameter_name, values)35        return returned36    return decorator37def iterate(**kwargs):38    def decorator(func):39        for name, options in kwargs.items():40            func = parametrize(name, options)(func)41        return func42    return decorator43def toggle(param_name):44    """A shortcut for :func:`slash.parametrize(param_name, [True, False]) <slash.parametrize>`45    .. note:: Also available for import as slash.parameters.toggle46    """47    return parametrize(param_name, (True, False))48@contextmanager49def bound_parametrizations_context(variation, fixture_store, fixture_namespace):50    assert ctx.session.variations.get_current_variation() is None51    ctx.session.variations.set_current_variation(variation)52    try:53        fixture_store.activate_autouse_fixtures_in_namespace(fixture_namespace)54        yield55    finally:56        ctx.session.variations.set_current_variation(None)57def iter_parametrization_fixtures(func):58    if isinstance(func, FixtureBase):59        func = func.fixture_func60    param_info = getattr(func, _PARAM_INFO_ATTR_NAME, None)61    if param_info is None:62        return []63    return param_info.iter_parametrization_fixtures()64class ParameterizationInfo(object):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
