Replacing keystrokes using Python - python

I use a multilingual keyboard. Sometimes I write something in one language, forgetting to switch the keyboard layout (e.g. Alt+Shift in Windows) and getting gibberish, which I then erase, switch languauges and rewrite.
I thought of writing a script that replaces highlighted text automatically and assign a keyboard shortcut to it (in Linux). This is intended as a recreational project, and I'm a programming noob, so I don't yet know how to do most of it and I don't have code to share.
My concrete question is (assuming Python, but I haven't started yet so willing to consider alternatives):
Is there an efficient way to map keystrokes in one layout with keystrokes in another layout?
Right now my default idea is to create an explicit table of replacements, e.g. a<->ש, b<->נ etc.
But this would be neither pleasant nor elegant.
Any suggestions?
EDIT: I'll emphasize that I'm not asking about how to perform the actual replacement (e.g. via a dicitionary if using Python), but rather if there is a way to avoid writing an explicit table (dictionary or whatever) for each character in my keyboard.
For example, if there is a way to say "given the layout ENG (US), the letter 'a' corresponds to this phyiscal key right here on the left side of the keyboard, and this physical key, when using the HEB layout, corresponds to the letter ש".
If there's a way to do such a thing, I can write something like keyID = GetKeyID(char,layout) followed by altchar = GetChar(keyID,altlayout).
I hope this makes sense. Note this was only an example of what I might hope for, I'd be happy to hear other ideas!

Related

Adding dotted text placeholders within a textfield in Python?

So far I am using Tkinter to make textfields in Python.
My question is how do I make it so there are placeholders, preferably in the style of mathematica or something similar so that when a user starts a new line, a right and left place holder appear on that line and the user can only enter text in these placeholders? Eventually I would like to be able to make it so all the right placeholders are aligned as well, but that may be too complicated. I can't seem to find a way to do this in Tkinter. Is there possibly a better package for this?
I'm not sure how to generate and format "text placeholders"
Edit: I think this question is coming down to: how do I dynamically add text placeholders within already existing text fields based on certain key commands?
tkinter doesn't have anything built-in to support this. Tkinter likely has all of the fundamental building blocks in order to build it yourself, but it will require a lot of work on your part.

Under spf13-vim, how to jump to next item in the loop

I am new to spf13-vim configuration. I have a basic problem; for example, when I am writing a python script, if I type "for" in vim console, I could see:
1 for item in <`2:items`>:
2 <`0`>
Then I can type any variable on "item", but I couldn't figure out how to jump to the next item, "<2:items>". How could I do that?
The direct answer to your question is: press Tab.
This feature is most certainly not provided by spf13 itself: it is provided by a snippet-expansion plugin that comes with spf13. Maybe it's SnipMate? Maybe it's UltiSnips? Maybe another one? Who even knows? You could simply look up up the documentation of that plugin but you can't, because you don't know what plugins you have.
Well, you are supposed to know what the plugins you add to your config that you manage yourself do and do not. But you don't, because you gave away that responsibility to someone else and you end up with a black box that contains and does things you have no idea about.
And the best part is that you don't even ask for help to the author/maintainer of that crappy distribution. They lured you into installing their stuff and giving control up, they are the ones who should help their poor, misled, users.
If you are serious about using Vim, drop spf13 immediately and take care of your configuration yourself.
If you don't care about doing things the right way, use another editor.
Press Ctrl+k to jump to the next item.

Execute statements on multiple cursors (like ST's MiniPy)

In Sublime Text I used small and handy MiniPy plugin.
It evaluates each expression in multi-cursor selection and replaces the selection with the result.
For example, I have selected these lines in ST:
1+1
2+1
3+1
When I press <c-s-x> I get:
2
3
4
I use vim-multiple-cursors plugin, but can't see how to make this work.
So my question is how can I evaluate multi-cursor selections in vim, preferably through Python.
Please note that multi-cursor selection doesn't have to be on a separate lines.
I would go with bc, too, but what about a pure Vim command?
:1,3norm c$^R=^R"^M
^R is obtained with <C-v><C-r> and ^M with <C-v><CR>.
As for your question, this will depend on what that plugin does with the "selected" text.
Since Vim doesn't provide non-contiguous selection there's no ready made way to get the content of the multiple "selections" (they are not actual selections) made with that plugin.
You will need that plugin to export the selected text in one way or another and I'm afraid this goes a bit beyond the scope of SO.
I'd suggest you get in touch with the author of that plugin and see if he is able to help you.
That said, you might be interested by Pipe2Eval.

The Foundry Nuke – program a keystroke (backspace key)

So if you don't know The Foundry Nuke, I'm not sure if you can help me, so read on at the risk of your own time. If your still here, Awesome! Either you know it or think you can help anyway and are an awesome person.
Basically I'm using The Foundry Ocula inside Nuke and creating a Python script to automate some stuff for me. It goes ahead X frames, adds an analysis key, moves ahead frames, adds key, etc. What I want is to delete the error thresholded out key matches (which is usually done with the backspace key) but I can't find a script in Ocula to delete selected keys, nor can I find a way to Python script something like
nuke.keystroke('backspace')
to make Nuke react like someone just pressed the Backspace key in the GUI. That code above is just an example of what I want... of course it's never that easy.
Thanks in advance!
Try the following method but pay attention that after erasing all the keys in a range, in the midtones.gain knob there will be a curve value instead of default 1 (it's an abstract example):
nuke.animation("ColorCorrect1.midtones.gain", "erase", ("27", "53"))
Or for copying expression (generated for chosen keyframes) from handmade userKnob to multiply knob use this method:
nuke.animation("Grade9.multiply", "expression", ("Grade9.userKnob",))

scripting full-screen, curses-style, updating tabular output (a la top) on unixen

I am looking for some sort of packages / APIs in Linux that can display output in columns similar to how top does.
For example, keep cleaning and rewriting the output to a full screen at a given interval (I guess watch probably does this good enough. But I am hoping to have some APIs that wrap on top of it).
Sort by columns easily. Particularly if I sort by column A, then when next time I re-print everything, it remembers to sort by that column A every time the output is refreshed.
And of course, ideally it can handle the keyboard input for me as well.
All in all, I am looking for packages or APIs that can help me organize my output in a way "top" organizes it.
Just to clarity: What I display might be completely unrelated to the system statistics. I just like the way top organizes the content. For example, My output content might be (and it's constantly changing, which is why it needs to be cleaned and rewritten):
Time Col1 Col2
12 4 13
13 5 19
14 5 15
I can hit a key say "A" then it sorts by Time. If I hit a key B then it sorts by Col1. If I hit a key say C then it sorts by Col2, etc, etc.
And of course this output content can be entirely in memory, organized in any data structures.
If you wanted to do this in shell, watch + printf would be a quick and dirty place to start -- watch to rerun a script every few seconds, printf to do the formatting, roughly as so:
printf '%-20s %-20s\n' \
header1 header2 \
line1data1 line1data2 \
line2data1 line2data2 ...
...with the data presumably being constructed in and fed from an array. Simply looping within your script and emitting the appropriate control codes to clear each line as you're about to overwrite it (and moving to the top of the window at the beginning) is another reasonable approach.
That said, the right tool here will be a layer built on top of curses. There are a number of higher-level textual widget sets out there already -- but I don't know of anything with direct support for tables.
If you were willing to write C, one such wrapper around curses is GAP.Browse.
I found the curses library in Python handy for this sort of requirement. It still doesn't support column management, but it provides a decent solution with a sane language, a nice document and a manageable learning curve.
If anyone else has better suggestion, I would be happy to pick his as the best answer.
http://docs.python.org/howto/curses.html
There's CDK. I tried it, and it's all right, feels a bit like GTK. But a printf solution is far cleaner.
If you don't mind scripting languages, you could try rbcurse, which has more features. It has very bad documentation though.

Categories