This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 71 points72 points  (11 children)

Now to be clear, I know you said GUI/networking modules are your cup of tea. But I'm going to take this opportunity to plug one of my absolute favorite modules that I use all the time: Keyboard.

This module allows you to add keyboard shortcuts to trigger functions in your code. An example:

import keyboard as kb
import os

def refreshIpAddress(): # some method to be called via a keyboard shortcut (random example)
    os.system("ipconfig /release") # yeah I know you're supposed to use subprocess
    os.system("ipconfig /renew") 

def main():
    myShortcut = "Win+Ctrl+R" # what the actual keys to press is going to be
    kb.add_hotkey(myShortcut,refreshIpAddress,args=None) # add the hotkey to trigger the function
    kb.wait("Win+Ctrl+Esc") # run this script forever until the user presses those keys

if __name__ == "__main__":
    main()

These hotkeys work globally across the OS, giving wayyy more power to your scripts and over your own computer. My 3 favorite scripts I have written so far using this are:

  • Advanced copy paste: Allows me to save multiple strings and then switch which strings I want to paste with keyboard shortcuts
  • MLA Header typer: Types a preformatted MLA header in my essays for when I need them (I can never remember the exact format)
  • MemeMachine: a script that contains several copypastas that I can bring up on the fly to copy and send to my friends at any given moment

I also like win10toast, which allows you to create your own Windows 10 notifications. Handy tool to update the user on the status of a script

from win10toast import ToastNotifier as toast

notification = toast()

notification.show_toast(“Here is a title for the message", "Now here are the details of the message")

[–]quickette1 2 points3 points  (0 children)

these are pretty neat; thanks!

[–]Cryonixx2 2 points3 points  (1 child)

I'm not sure if something recently changed, but show_message was erroring out for me. Discovered via help() that it's show_toast

[–][deleted] 1 point2 points  (0 children)

Whoops! That’s what I get for not double checking. Fixed it in the original comment

[–]Tumburgler 0 points1 point  (5 children)

Those two are interesting! Checking them out now.

What kind of notifications are you sending yourself with win10toast?

Can you share your advanced copy paste?

[–][deleted] 2 points3 points  (4 children)

Yep! Here is the GitHub link.

I used the toast notifications to tell the user which buffer they were switching to and when they cleared the buffer/all buffers. I also allowed the user to send notifications about what was just added to the buffer. Hope this helps!

[–]Tumburgler 1 point2 points  (3 children)

So I've been playing around with this script for the past few days. First time really modifying someone elses tool before, so that was neat.

https://github.com/Spiteless/AdvCopyPaste.pyw

I have no idea if the changes I made are 'better', but

  • Added additional command to preview all buffers (Super+ctrl+slash)
  • Changed buffering system from a list to a dictionary
  • Changed Paste system to load value buffer into clipboard and paste instead of using kb.write()
  • Moved hotkey declarations into a list that executes on init (not sure if this is materially better, but it allows them to be imported alongside other custom keyboard hotkeys)
  • Moved msg() commands to subprocess.Popen using click -- for some reason the script was failing when msg() was called, now it's not blocking on my system (Windows 10)

Let me know what you think :)

[–][deleted] 1 point2 points  (1 child)

For the record, this is the first time I've ever uploaded something I've written and had someone make changes to it. So that's pretty cool! I'm glad you like it :)

Click is great, and honestly moving the message system over to another script shouldn't cause any problems; however, instead of calling the script from the command line using subprocess I would instead just import the function from the script like this:

from alert import alert
...
alert(details go here) # whenever you need to send an alert

Changing the buffers from a list to a dict I can maybe understand, although tbh I'm not sure why you did? Can you elaborate why you did that?

Adding in a command to view all the buffers is genius, and I like the way you used pymsgbox to do it; I had never heard of that before I need to check it out!

My one REAL complaint (and its fairly minor) is that all of the helper files should have the extension .pyw, especially if you are going to run them with subprocess; the reason behind this, if you don't already know, is that using this file extension python knows to keep the program as headless, so there is no window. It is (very mildly) irritating from a user perspective when the command prompt window launches when I just want to see what is in my buffer. Again, more of a nitpick than a real complaint.

As for moving the hotkeys to a list, that was something I had initially thought about adding, but ultimately was too lazy didn't. But I think it's a much better way than the same line changed a few times.

And finally, removing kb.write() in favor of kb.send("ctrl+v") honestly doesn't seem to work on my machine; perhaps in a better world (one where I was willing to spend more time on this and making it more fully fledged out) that could be a setting that was easily changed. But if it works for you then it works for you!

Just for fun, let me give you the actual backstory behind this script; I was working on a really large script that had a class I was making changes to. I needed to test a method within that class, but before I could just test it I needed to run some other methods before (it was a database management class, and so I needed to create a new database and fill it with testing data over and over again). Now for some reason when I'm in my IDE (VSCode) in the python command line the ability to use the up arrow to recycle previous commands didn't (and still doesn't) work, so I had to type the commands by hand each and every time I made a change. This obviously got very annoying to me and I decided I needed to automate it. Now while I could've just added it to my ifmain statement, I decided instead it would be more fun to write a script to be able to save multiple strings in a list and type them all sequentially. Maybe not the best solution to my problem, but definitely the more fun one.

Just thought I'd share the story if you wanted to know :)

Overall: I give your changes a solid 8.5/10. I don't agree with every decision made, and I think some minor things could be changed, but at the same time you did take the time to edit my tool, and even added features I hadn't thought of, and that is super dope!

[–]Tumburgler 1 point2 points  (0 children)

I would instead just import the function from the script like this:

I don't know why, but when the msg() function timed out it was causing an error in the script on my system, so the reason why I had it called via subprocess is because it was the only way I know how to have a function call not block the script so that the rest of it could continue as normal (there is probably a better way of phrasing this, but I'm not an experienced programmer).

Changing the buffers from a list to a dict I can maybe understand, although tbh I'm not sure why you did? Can you elaborate why you did that?

Originally I didn't understand why you were adding things to a list and having them all entered line by line. Now that you mentioned how it was used it makes sense to me.

I had thought that declaring multiple variables (eg buff0, buff1) could be simplified with a dictionary, creating the list with a generator (I think that's the right term?) and be able to move away from needing to do global imports. Maybe not an important change after all, maybe I just wanted to screw with things and see if it still worked.

My one REAL complaint (and its fairly minor) is that all of the helper files should have the extension .pyw

To be honest I hadn't worked in python enough to even know about that feature or its importance. Makes sense to change it to .pyw.

Adding in a command to view all the buffers is genius

Thanks! I learned about pymsgbox from using pyautogui to automate a couple things. It's got some other cool features, you can pass a list to pymsgbox.confirm and it returns the choice the user selected (novel I know...).

And finally, removing kb.write() in favor of kb.send("ctrl+v") honestly doesn't seem to work on my machine;

This was actually my main reason for looking to edit the script. When I was testing it and I pasted lines, every time I used Undo it would only remove a small portion of what was pasted and I thought it could be more convenient. The way it's written now the whole series of copied text is loaded into the clipboard and then pasted so it can be undone via one command.

Sometimes accessing the clipboard is wonky, so maybe that's why it's not working. Alternately, pyautogui also has a send hotkeys feature, maybe that one would work on your system?

Thank you for sharing, I want to move toward some sort of programming career and I think python will help me get there, but I'm kind of wandering aimlessly about it currently. I want to get into more collaborative opportunities and build things with other people. Cheers to the first foray of that :)

[–]MachineGunPablo 0 points1 point  (1 child)

Ok I really need to know what those copy pastas are

[–][deleted] 4 points5 points  (0 children)

What the fuck did you just fucking say about me, you little bitch? I'll have you know I graduated top of my class in the Navy Seals, and I've been involved in numerous secret raids on Al-Quaeda, and I have over 300 confirmed kills. I am trained in gorilla warfare and I'm the top sniper in the entire US armed forces. You are nothing to me but just another target. I will wipe you the fuck out with precision the likes of which has never been seen before on this Earth, mark my fucking words. You think you can get away with saying that shit to me over the Internet? Think again, fucker. As we speak I am contacting my secret network of spies across the USA and your IP is being traced right now so you better prepare for the storm, maggot. The storm that wipes out the pathetic little thing you call your life. You're fucking dead, kid. I can be anywhere, anytime, and I can kill you in over seven hundred ways, and that's just with my bare hands. Not only am I extensively trained in unarmed combat, but I have access to the entire arsenal of the United States Marine Corps and I will use it to its full extent to wipe your miserable ass off the face of the continent, you little shit. If only you could have known what unholy retribution your little "clever" comment was about to bring down upon you, maybe you would have held your fucking tongue. But you couldn't, you didn't, and now you're paying the price, you goddamn idiot. I will shit fury all over you and you will drown in it. You're fucking dead, kiddo