Best Python code snippet using lisa_python
glamour.py
Source:glamour.py  
...59        if os.path.exists(working_directory):60            shutil.rmtree(working_directory)61    def write_release_log_and_upload_to_sftp(self):62        versions_data = self.get_versions_data()63        release_log_temp_file = open(self.get_working_path("releases.yml"), 'w')64        65        release_log_temp_file.write(yaml.dump(yaml.load(str(versions_data)), default_flow_style=False))66        release_log_temp_file.close()67        self.save_file_to_sftp_appcast_directory(self.get_working_path("releases.yml"))68    def write_release_notes_and_upload_to_sftp(self):69        versions_data = self.get_versions_data()70        release_notes_temp_file = open(self.get_working_path("release_notes.html"), 'w')71        release_notes_template_file = open(self.get_support_files_path("release_notes.template.html"), 'rU')72        release_notes_template_data = ""73        for line in release_notes_template_file.readlines():74            release_notes_template_data += line75        76        release_notes_versions_data = ""77        for version_number in sorted(versions_data.iterkeys(), reverse = True):78            release_notes_versions_data += self.get_partial_html_from_version_hash(self.get_version_hash_from_version_number(version_number), self.get_version_hash_from_version_number(str(int(version_number)-1)))79        release_notes_template_data = release_notes_template_data.replace("$VERSIONS", release_notes_versions_data)80        release_notes_temp_file.write(release_notes_template_data)81        release_notes_temp_file.close()82        self.save_file_to_sftp_appcast_directory(self.get_working_path("release_notes.html"))83    def write_appcast_and_upload_to_sftp(self):84        appcast_file = open(self.get_working_path("appcast.xml"), 'w')85        appcast_file.write(self.get_appcast_data())86        appcast_file.close()87        self.save_file_to_sftp_appcast_directory(self.get_working_path("appcast.xml"))88    def get_appcast_data(self):89        appcast_template_file = open(self.get_support_files_path("appcast.template.xml"), "rU")90        contents = ""91        for line in appcast_template_file.readlines():92            contents += line93        appcast_template_file.close()94        contents = contents.replace("$TITLE", "Version " + str(self.get_current_app_version()))95        contents = contents.replace("$APP_NAME", glamour_settings["app_name"])96        contents = contents.replace("$APPCAST_LINK", self.get_https_url("appcast.xml"))97        contents = contents.replace("$RELEASE_NOTES_LINK", self.get_https_url("release_notes.html"))98        contents = contents.replace("$DESCRIPTION", "Updated to version " + str(self.get_current_app_version()))99        contents = contents.replace("$PUBLISH_DATE", str(datetime.datetime.now()))100        contents = contents.replace("$VERSION", str(self.get_current_app_version()))101        contents = contents.replace("$URL", self.get_https_url(self.get_zipfile_name()))102        return contents103        104    def zip_app_and_upload_to_sftp(self):105        print("Compressing " + glamour_settings["app_name"]+".app ... ", end = '')106        107        self.zip_folder(glamour_settings["built_app_path"], os.path.splitext(self.get_zipfile_name())[0], working_directory)108        109        print("Compressed to " + str(round(float(os.path.getsize(self.get_working_path(self.get_zipfile_name())))/float(1000**2), 2))+ " MB")110       111        self.save_file_to_sftp_appcast_directory(self.get_working_path(self.get_zipfile_name()))112        113    def save_file_to_sftp_appcast_directory(self, file_path):114        connected = False115        if self.sftp_password == None:116            self.sftp_password = getpass.getpass("Please enter your SFTP password: ")117        while not connected:118            try:119                transport = paramiko.Transport((glamour_settings["sftp_host"], glamour_settings["sftp_port"]))120                transport.connect(username = glamour_settings["sftp_username"], password = self.sftp_password)121                connected = True122                break123            except paramiko.AuthenticationException:124                self.sftp_password = getpass.getpass("Please enter your SFTP password: ")125        sftp = paramiko.SFTPClient.from_transport(transport)126        print("Uploading " +os.path.basename(file_path)+" to sftp." + glamour_settings["sftp_host"] + " ... ", end = '')127        then = time.time()128        sftp.put(localpath=file_path, remotepath = self.get_sftp_directory(os.path.basename(file_path)) )129        now = time.time()130 131        #let's do a little speed calculation for the kids back home132        Kb_per_second = 8*(float(os.path.getsize(file_path))/float(1000))/float(now-then)133        print("Finished uploading in " + str(round(now - then, 1)) + " seconds (" + str(int(round(Kb_per_second, 0))) + " Kb/s)")134    135        136    def get_partial_html_from_version_hash(self, version_hash, previous_version_hash):137        138        repo = self.get_git_repo()139        this_partial = ""140        partial_reference = open(self.get_support_files_path("release_notes.version.template.html"), 'r')141        for line in partial_reference.readlines():142            this_partial += line143        this_partial = this_partial.replace("$APPNAME", glamour_settings["app_name"])144        this_partial = this_partial.replace("$VERSION", version_hash["human_version"])145        this_partial = this_partial.replace("$HEAD", version_hash["head"])146        this_partial = this_partial.replace("$DATE", str(version_hash["date"]))147        148        if previous_version_hash is None:149            commits_between_previous_verson_and_current_version = self.get_commits_between(None, version_hash["head"])150        else:151            commits_between_previous_verson_and_current_version = self.get_commits_between(previous_version_hash["head"], version_hash["head"])152        153        commit_messages = ""154        for commit in commits_between_previous_verson_and_current_version:155          commit_messages += "<li>"+commit.message.rstrip()+"</li>\n"156        if len(commit_messages) < 1:157          commit_messages += "<li><em>Unknown changes.</em></li>"158    159        this_partial = this_partial.replace("$FEATURELIST", commit_messages)160        return this_partial161    def zip_folder(self, source_folder, zipfile_name, destination_folder):162        os.chdir(source_folder)163        return shutil.make_archive(destination_folder+"/"+zipfile_name, "zip", source_folder+"/..", glamour_settings["app_name"]+".app")164    def get_versions_data(self):165        try:166            versions_data = yaml.load(urllib2.urlopen(self.get_https_url("releases.yml")))167        except urllib2.HTTPError:168            return {str(self.get_current_app_version()): {"head": self.get_current_head_id(), "date": str(datetime.datetime.now()), "human_version": self.get_human_version(self.get_current_app_version())}}169        versions_data[str(self.get_current_app_version())] = {"head": self.get_current_head_id(), "date": str(datetime.datetime.now()), "human_version": self.get_human_version(self.get_current_app_version())}170        return versions_data171    def get_commits_between(self, commit_1, commit_2_inclusive):172        repo = self.get_git_repo()173        if commit_1 is None:174            return repo.iter_commits(commit_2_inclusive)175        else:176            return repo.iter_commits(commit_1 + ".." + commit_2_inclusive)177    def get_git_repo(self):178        repo = Repo(glamour_settings["git_repo_path"])179        if repo.bare:180            raise Error181        return repo182    def get_current_head_id(self):183        return self.get_git_repo().commit('master').name_rev.rsplit()[0]184    def get_version_hash_from_version_number(self, version_number):185        versions_data = self.get_versions_data()186        try:187            return versions_data[str(version_number)]188        except KeyError:189            return None190    def get_human_version(self, version):191        return "b"+str(version)192    def get_support_files_path(self, file_path):193        return script_directory + "/support_files/" + file_path194    def get_working_path(self, file_path):195        return working_directory + "/" + file_path196    def get_https_url(self, file_name):197        return glamour_settings["https_base_url"] + "/" +file_name198    def get_sftp_directory(self, file_name):199        return glamour_settings["sftp_appcast_directory"] + "/" +file_name 200    def get_current_app_version(self):201        plist = plistlib.readPlist(glamour_settings["built_app_path"]+"/Contents/Info.plist")202        return int(plist["CFBundleVersion"])203    def get_zipfile_name(self):204        return glamour_settings["app_name"]+"_"+str(self.get_current_app_version())+".app.zip"205        206glamour = glamour()207try:208    glamour.release(make_clean = False)...datalake_management.py
Source:datalake_management.py  
...4def move_to_working_dir(source, file_path, load_id, delimiter=',', remove_breaks=False, escape='\\',5                        dates_from_julian_to_gregorian=None):6    res = []7    file_path = get_normal_path(file_path)8    target_path = get_working_path(file_path, source, load_id, False)9    print(file_path, target_path)10    mkdir(target_path)11    mkdir(target_path + '_orig')12    for f in os.listdir(file_path):13        move_file(file_path + "/" + f, target_path + "_orig/" + f)14        if remove_breaks is False and is_nan(dates_from_julian_to_gregorian):15            copy_file(target_path + "_orig/" + f, target_path + "/" + f)16        else:17            df = read_csv(target_path + "_orig/" + f, delimiter, escape=escape, remove_breaks=remove_breaks,18                          dates_from_julian_to_gregorian=dates_from_julian_to_gregorian)19            df.to_csv(target_path + '/' + f, sep=delimiter, header=True, index=False,20                      escapechar=escape,21                      doublequote=False)22        res.append(file_path + "/" + f)23    return target_path24def move_to_history_dir(source, file_path, load_id, cdc=None):25    file_path_working = get_working_path(file_path, source, load_id, False)26    target_path = get_history_path(file_path, source, load_id, False)27    print("target_path: ", target_path)28    if os.path.isdir(file_path_working):29        mkdir(target_path)30        for f in os.listdir(file_path_working):31            if cdc:32                move_file(file_path_working + "/" + f, target_path + "/" + f)33            else:34                move_file(file_path_working + "_orig/" + f, target_path + "/" + f)...aud_img_database.py
Source:aud_img_database.py  
...2627    def get_file_title(self,f):28        return os.path.basename(f).split(".")[0]2930    def get_working_path(self,f,fldr):31        return os.path.join(fldr,f)3233    def init_pair_list(self):34        """Loops through the wav and img folders, if files have same name pairs them"""35        for aud_f in os.listdir(self.WAV_FLDR):36            # need title to check, and need to get the path of the file37            aud_title = self.get_file_title(aud_f)3839            working_aud_path = self.get_working_path(aud_f,self.WAV_FLDR)4041            for img_f in os.listdir(self.IMG_FLDR):42                img_title = self.get_file_title(img_f)43                working_img_path = self.get_working_path(img_f,self.IMG_FLDR)44                if(aud_title == img_title):45                    # get a match, make a pairing46                    self.PAIRINGS.append(Pair(aud_title,working_aud_path,working_img_path))47    48    def print_pairings(self):49        for pair in self.PAIRINGS:50            print("Name: {} -- First: {} -- Second: {}".format(pair.NAME,pair.FIRST,pair.SECOND))5152# Examples53#test = AudImgDataBase("wav_refs","wav_img_refs")
...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!!
