Best Python code snippet using lisa_python
generators.py
Source:generators.py  
...145        self._set_sheet_name_to_module_or_form_mapping(rows[MODULES_AND_FORMS_SHEET_NAME])146        return rows147    def _set_sheet_name_to_module_or_form_mapping(self, all_module_and_form_details):148        # iterate the first sheet to get unique ids for forms/modules149        sheet_name_column_index = self._get_header_index(MODULES_AND_FORMS_SHEET_NAME, 'menu_or_form')150        unique_id_column_index = self._get_header_index(MODULES_AND_FORMS_SHEET_NAME, 'unique_id')151        type_column_index = self._get_header_index(MODULES_AND_FORMS_SHEET_NAME, 'Type')152        for row in all_module_and_form_details:153            self.sheet_name_to_module_or_form_type_and_id[row[sheet_name_column_index]] = Unique_ID(154                row[type_column_index],155                row[unique_id_column_index]156            )157    def _generate_module_sheet_name(self, module_index):158        """159        receive index of module and convert into name with module unique id160        :param module_index: index of module in the app161        :return: name like module_moduleUniqueId162        """163        _module = self.app.get_module(module_index)164        sheet_name = "_".join(["module", _module.unique_id])165        self.slug_to_name[_module.unique_id] = _module.name166        return sheet_name167    def _generate_form_sheet_name(self, module_index, form_index):168        """169        receive index of form and module and convert into name with form unique id170        :param module_index: index of form's module in the app171        :param form_index: index of form in the module172        :return: name like form_formUniqueId173        """174        _module = self.app.get_module(module_index)175        form = _module.get_form(form_index)176        sheet_name = "_".join(["form", form.unique_id])177        self.slug_to_name[form.unique_id][self.source_lang] = "%s > %s" % (178            _module.name.get(self.source_lang, _module.default_name()),179            form.name.get(self.source_lang, form.default_name())180        )181        return sheet_name182    def _update_sheet_name_with_unique_id(self, sheet_name):183        """184        update sheet name with HQ format like menu0 or menu1_form1 to185        a name with unique id of module or form instead186        :param sheet_name: name like menu0 or menu1_form1187        :return: name like module_moduleUniqueID or form_formUniqueId188        """189        if sheet_name == MODULES_AND_FORMS_SHEET_NAME:190            return sheet_name191        module_sheet_name_match = HQ_MODULE_SHEET_NAME.match(sheet_name)192        if module_sheet_name_match:193            module_index = int(module_sheet_name_match.groups()[0]) - 1194            return self._generate_module_sheet_name(module_index)195        form_sheet_name_match = HQ_FORM_SHEET_NAME.match(sheet_name)196        if form_sheet_name_match:197            indexes = form_sheet_name_match.groups()198            module_index, form_index = int(indexes[0]) - 1, int(indexes[1]) - 1199            return self._generate_form_sheet_name(module_index, form_index)200        raise Exception("Got unexpected sheet name %s" % sheet_name)201    def _get_filename(self, sheet_name):202        """203        receive sheet name in HQ format and return the name that should be used204        to upload on transifex along with module/form unique ID and version postfix205        :param sheet_name: name like menu0 or menu1_form1206        :return: name like module_moduleUniqueID or form_formUniqueId207        """208        sheet_name = self._update_sheet_name_with_unique_id(sheet_name)209        if self.version and self.use_version_postfix:210            return sheet_name + '_v' + str(self.version)211        else:212            return sheet_name213    def _get_header_index(self, sheet_name, column_name):214        for index, _column_name in enumerate(self.headers[sheet_name]):215            if _column_name == column_name:216                return index217        raise Exception("Column not found with name {}".format(column_name))218    def filter_invalid_rows_for_form(self, rows, form_id, label_index):219        """220        Remove translations from questions that have SKIP TRANSIFEX in the comment221        """222        labels_to_skip = self.checker.get_labels_to_skip()[form_id]223        valid_rows = []224        for i, row in enumerate(rows):225            question_label = row[label_index]226            if question_label not in labels_to_skip:227                valid_rows.append(row)228        return valid_rows229    @cached_property230    def _blacklisted_translations(self):231        return TransifexBlacklist.objects.filter(domain=self.domain, app_id=self.app_id).all()232    def filter_invalid_rows_for_module(self, rows, module_id, case_property_index,233                                       list_or_detail_index, default_lang_index):234        valid_rows = []235        for i, row in enumerate(rows):236            list_or_detail = row[list_or_detail_index]237            case_property = row[case_property_index]238            default_lang = row[default_lang_index]239            if not self.checker.is_blacklisted(module_id, list_or_detail, case_property, [default_lang]):240                valid_rows.append(row)241        return valid_rows242    def _get_translation_for_sheet(self, sheet_name, rows):243        occurrence = None244        # a dict mapping of a context to a Translation object with245        # multiple occurrences246        translations = OrderedDict()247        type_and_id = None248        key_lang_index = self._get_header_index(sheet_name, self.lang_prefix + self.key_lang)249        source_lang_index = self._get_header_index(sheet_name, self.lang_prefix + self.source_lang)250        default_lang_index = self._get_header_index(sheet_name, self.lang_prefix + self.app.default_language)251        if sheet_name == MODULES_AND_FORMS_SHEET_NAME:252            type_index = self._get_header_index(MODULES_AND_FORMS_SHEET_NAME, 'Type')253            sheet_name_index = self._get_header_index(MODULES_AND_FORMS_SHEET_NAME, 'menu_or_form')254            unique_id_index = self._get_header_index(MODULES_AND_FORMS_SHEET_NAME, 'unique_id')255            def occurrence(_row):256                # keep legacy notation to use module to avoid expiring translations already present257                # caused by changing the context on the translation which is populated by this method258                return ':'.join(259                    [_row[type_index].replace("Menu", "Module"),260                     _row[sheet_name_index].replace("menu", "module"),261                     _row[unique_id_index]])262        else:263            type_and_id = self.sheet_name_to_module_or_form_type_and_id[sheet_name]264            if type_and_id.type == "Menu":265                case_property_index = self._get_header_index(sheet_name, 'case_property')266                list_or_detail_index = self._get_header_index(sheet_name, 'list_or_detail')267                def occurrence(_row):268                    case_property = _row[case_property_index]269                    # limit case property length to avoid errors at Transifex where there is a limit of 1000270                    case_property = case_property[:950]271                    return ':'.join([case_property, _row[list_or_detail_index]])272            elif type_and_id.type == "Form":273                label_index = self._get_header_index(sheet_name, 'label')274                def occurrence(_row):275                    return _row[label_index]276        is_module = type_and_id and type_and_id.type == "Menu"277        for index, row in enumerate(rows, 1):278            source = row[key_lang_index]279            translation = row[source_lang_index]280            if self.exclude_if_default:281                if translation == row[default_lang_index]:282                    translation = ''283            occurrence_row = occurrence(row)284            occurrence_row_and_source = "%s %s" % (occurrence_row, source)285            if is_module:286                # if there is already a translation with the same context and source,287                # just add this occurrence..._manager.py
Source:_manager.py  
...7	from .model import GModel8class GModelManager(object):9	def __init__(self, model: "GModel"):10		self.model = model11	def _get_header_index(self):12		class_meta = getattr(self.model, "Meta")13		return getattr(class_meta, "header_index")14	def _filter_data_list(self, **kwargs):15		header_index = self._get_header_index()16		all_data: gspread.Worksheet = getattr(self.model, "_data")17		headers: list = getattr(self.model, "_headers")18		meta:dict[str, "Field"] = getattr(self.model, "_meta")19		filter_data_list = []20		for key, val in list(kwargs.items()):21			if "__" in key:22				field_key, operator = key.split("__")23			else:24				field_key, operator = key, "eq"25			filter_data_list = []26			field:Field = meta.get(field_key)27			key_name = getattr(field, "_meta", {}).get("name")28			local_index = headers.index(key_name) + 129			godm_column_values = all_data.col_values(local_index)...readme_updater.py
Source:readme_updater.py  
...4    with open("README.md", "r") as f:5        lines = f.readlines()6    header_line = f"## Solved problems (total {len(problems_solved)}):\n"7    problems_line = str(sorted(problems_solved)) + "\n"8    header_index = _get_header_index(lines, "## Solved problems")9    if header_index is not None:10        lines[header_index] = header_line11        if len(lines) == header_index + 1:12            # this is the last line, add a new one13            lines.append(problems_line)14        else:15            problems_index = None16            for i, line in enumerate(lines[header_index + 1:]):17                if line.strip() != "":18                    problems_index = i + header_index + 119            lines[problems_index] = problems_line20    else:  # if no record in file:21        lines.append(header_line)22        lines.append(problems_line)23    with open("README.md", "w") as f:24        f.writelines(lines)25def _get_header_index(lines: List[str], prefix: str):26    for i, line in enumerate(lines):27        if line.startswith(prefix):28            return i...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!!
