From ffd1ce42ba859370829e97a3b6377dbec0ab569d Mon Sep 17 00:00:00 2001 From: perro Date: Mon, 15 May 2023 11:25:14 -0700 Subject: [PATCH] Ejercicio 9a --- exercises/ex09/ex09a.py | 90 ++++++++++++++++++++++++++++++++++++++++ exercises/ex09/mcb | Bin 0 -> 16384 bytes 2 files changed, 90 insertions(+) create mode 100644 exercises/ex09/ex09a.py create mode 100644 exercises/ex09/mcb diff --git a/exercises/ex09/ex09a.py b/exercises/ex09/ex09a.py new file mode 100644 index 0000000..711310d --- /dev/null +++ b/exercises/ex09/ex09a.py @@ -0,0 +1,90 @@ +""" +Project: Extending the Multi-Clipboard +Page 229 +""" + +import sys +import shelve +import pyperclip +from pathlib import Path + + +def help(): + script = Path(__file__).name + print( + f""" + {script} saves and loads pieces of text to the clipboard. + Usage: python {script} save Saves clipboard to keyword + python {script} Loads keyword to clipboard + python {script} list Loads all keywords to clipboard + python {script} delete Deletes keyword + python {script} wipe Deletes all keywords + python {script} help Prints this help + """.strip() + ) + + +def save(key, data): + if key in data.keys(): + print(f"ERROR: '{key}' ya existe") + sys.exit(1) + else: + data[key] = pyperclip.paste() + + +def delete(key, data): + if key in data.keys(): + print(f"INFO: '{key}' se ha eliminado") + del data[key] + else: + print(f"ERROR: '{key}' no existe") + sys.exit(1) + + +def get(key, data): + if key in data.keys(): + pyperclip.copy(data[key]) + else: + print(f"ERROR: '{key}' no existe") + sys.exit(1) + + +def ls(data): + for key, val in data.items(): + txt = val.strip().split("\n")[0] + if len(txt) > 100: + txt = f"{txt[:97]}..." + print(f"{key} -> {txt}") + + +def wipe(data): + for key in data.keys(): + delete(key, data) + + +data = shelve.open("mcb") + +if len(sys.argv) == 1: + print("ERROR: al menos un argumento es necesario") + sys.exit(1) + +match sys.argv[1]: + case "save" | "delete": + if len(sys.argv) == 2: + print("ERROR: una palabra clave es necesaria") + sys.exit(1) + for key in sys.argv[2:]: + if sys.argv[1] == "save": + save(key, data) + else: + delete(key, data) + case "list": + ls(data) + case "wipe": + wipe(data) + case "help": + help() + case key: + get(key, data) + +data.close() diff --git a/exercises/ex09/mcb b/exercises/ex09/mcb new file mode 100644 index 0000000000000000000000000000000000000000..47cdf90107fcabd0c43fc2baea94441e63175d3f GIT binary patch literal 16384 zcmeI1&ubJh6vs!ZhzcTz7f-MC&>!q(wylB%k>WuRdJrurl%myH00ck)1V8`;KmY_l00ck)1V8`;{u=>` zmLUTOZ~zBS0C9jgKpY?sPzO*4PzO*4PzO*4PzO*4&R89|6tX&b9p$5Vf8X5wyEz;0 z@8>Us1@rN~za4*#>%U(M@8|9YIXe!sN%F(BR-cxqaem-7*!t()C-*pw`<&+3>gREv z(mCJ8uRpI>uAfH^Z46Ay<2We4Fy^`EW?VmRy;@L#01lI^z;~o&fxJLow6WkN;sNn+8sb5{zxZJoH*%yNXF1bCIxIkNgdTJHAUxY+L+JKEbzdCb)y zGi;pJ+en2Fwdifg_(NI2bZ?_WSwRmDv>N67kX%!ov*o2XeApo+tt5&Y8|nl8W7~cM z(}vgXH-;Ny(Q0FD$JyPjMdN8L`VP=7|1aCMm)D*0*X++Ye)U(LFT2@)?K`)YC*Du2 z{w@@1AswLYz$ZC+>NjghR!q9umqqrDl{;OlX;0^hD?>{%H<|`A&)pl9RXp(V!)Ccd zvQQ+cly>MMzznLa&!!eamL(TzX?@uP3hfT9*kNW!X*R{8onN7Dm(2;_U103LSxm=5Qb`T%{0w4eaAOHd&00JNY0%x7TPx7}k@c;k- literal 0 HcmV?d00001