• Coding

    Python Learning: Automate Boring Stuff with Python | Chapter 8 : My Solution to Project Multiclipboard

    [sourcecode language=”python”] #mcb.pyw : Saves and loads pieces of text to the clipboard #Usage: py.exe mcb.pyw save &lt;keyword&gt; – Saves clipboard to keyword # py.exe mcb.pyw &lt;keyword&gt; – Loads keyword to clipboard # py.exe mcb.pyw list – Loads all keywords to clipboard # py.exe mcb.pyw read &lt;path text file&gt; &lt;keyword&gt; import shelve, pyperclip, sys, os,re <br data-mce-bogus="1"> mcbShelf = shelve.open("mcb") if len(sys.argv) == 4 and sys.argv[1].lower() == "read": #TODO: read file and save to shelf #check if path is valid path = sys.argv[2] if os.path.exists(path): #check if path is a text file if ".txt" in os.path.basename(path): #read file file = open(os.path.abspath(path)) content = file.read() #close file file.close() key = sys.argv[3]…

  • Coding

    Python Learning: Automate Boring Stuff with Python | Solution to Part II Chapter 7 Practice Project II

    [sourcecode language=”python”]</pre> import re #first argument – string to be stripped #second argument – char or default " " def customStrip(string: str, stripChar: str="\s")->str: ”’ Removes specified character from the beginning and end of a given string arguments: string: str stripChar: str returns: string ”’ if stripChar == "" or stripChar == " ": stripChar = "\s" patternString = r"({}+)".format(stripChar) actions = ("^" + patternString,patternString + "$") for p in actions: pattern = re.compile(p) string = pattern.sub("",string) return string texts = [ " aa dfgfhjgs, afktgrg aaa ", "aaaaaaaaaaaaaaaajfdgf fdfgjkerturt aaaaa" ] for text in texts: st = customStrip(text, "a") print("The text \n{}\n has {}characters.\nAfter strip \n{} \n====\n it now…

error: Content is protected !!