How to make pywinauto faster (keyboard.send_keys)? - python

pwa.keyboard.send_keys ("StackOverflow")
with this code, the text is printed 1 letter at a time (long)
how to make it so that the text is entered completely at once? (works with 1000+ variables)

You should be able to change the optional parameter pause to zero like this:
pwa.keyboard.SendKeys("StackOverflow", pause=0)
There are more details on this here

Related

Python function not working with seemingly correct string path

I have the following code (it changes the string/filepath, replacing the numbers at the end of the filename + the file extension, and replaces that with "#.exr")
I was doing it this way because the name can be typed in all kinds of ways, for example:
r_frame.003.exr (but also)
r_12_frame.03.exr
etc.
import pyseq
import re
#create render sequence list
selected_file = 'H:/test/r_frame1.exr'
without_extention = selected_file.replace(".exr", "")
my_regex_pattern = r"\d+\b"
sequence_name_with_replaced_number = re.sub(my_regex_pattern, "#.exr" ,without_extention)
mijn_sequences = fileseq.findSequencesOnDisk(sequence_name_with_replaced_number)
If I print the "sequence_name_with_replaced_number" value, this results in the console in:
'H:/test/r_frame#.exr'
When I use that variable inside that function like this:
mijn_sequences = fileseq.findSequencesOnDisk(sequence_name_with_replaced_number)
Then it does not work.
But when I manually replace that last line into:
mijn_sequences = fileseq.findSequencesOnDisk('H:/test/r_frame#.exr')
Then it works fine. (it's the seems like same value/string)
But this is not an viable option, the whole point of the code if to have the computer do this for thousands of frames.
Anybody any idea what might be the cause of this?
After this I will do simple for loop going trough al the files in that sequence. The reason I'm doing this workflow is to delete the numbers before the .exr file extensions and replace them with # signs. (but ognoring all the bumbers that are not at the end of the filename, hence that regex above. Again, the "sequence_name_with_replaced_number" variable seems ok in the console. It spits out: 'H:/test/r_frame#.exr' (that's what I need it to be)
I fixed it. the problem as stated was correct, every time I did a cut and past from the variable value in the console and treated it as manual input it worked.
Then I did a len() of both values, and there was a difference by 2! What happend? The console added the ''
But in the generated variable it had those baked in as extra letters. i fixed it by adding cleaned_sequence = sequence_name_with_replaced_number[1:-1] so 'H:/test/r_frame1.exr' (as the console showed me) was not the same as 'H:/test/r_frame1.exr' (what I inserted manually, because I added these marks, in the console there are showed automatically)

How can I get an input between two print() functions without going to the next line?

I am trying to build this receipt kind of structure.
This is my code:
print('--------------------------------')
print('|\tGEOMETRICAL FIGURE\t|')
print('|\t CALCULATIONS\t|')
print('--------------------------------')
print('|\tFigure:\t\t\t|')
print('|\t1. Circle\t\t|')
print('|\t2. Triangle\t\t|')
print('|\t3. Ellipse\t\t|')
print('|\t4. Trapezium\t\t|')
print('--------------------------------')
print('|\tType number desired\t|')
print('|\t\t',end = '');num = int(input());print('\t\t|')
print('--------------------------------')
I am getting this as the output (5 is the user input I gave):
How can I get those into one single line?
I don't think you can. The new line is what the user types, it doesn't come from your program.
To have more fine control over what's on the terminal you need to use curses, that's much more complicated to use, but I think it's the way to go, so you can handle all cases, including errors.
Another way is to turn off the echo in the terminal, so that the typed stuff will not appear on screen. However you'll have to read digit by digit instead of using input and display it or the user won't see anything.
Try to use one print instead.
print(f"|\t\t{int(input())}\t\t|")

Is there a way to remove items from various lists based on conditional statements?

I am writing a program that will act as a photography idea-generator for New York photographers. The way it works now is quite simple, the code is utilizing the random.choice function to randomly pull items from lists, then the code prints them out in a way that forms a sentence in English as an end result.
My issue is I need to add some logic to this, as some results would not make sense for a photographer to do (at least in my opinion). In this example I am trying to remove 'Bracketed (HDR)' from the technique_list, IF "Portrait" happens to be randomly chosen when python chooses the theme item.
I have a feeling I am mis-using the .remove function within the conditional if statement. Is there a better way to do this? I have attached the pertinent parts of the code for examination.
I have tried technique_list.remove('Bracketed (HDR)') , as well as
del technique_list[0] , both as the response part of the if statement.
import random
print ("You should try taking a...")
#pool of items that the program will randomly choose..
theme_list = ['Cityscape','Peoplescape','Port-Scape', 'Portrait']
technique_list = ['Bracketed (HDR)','Wide Angle', 'Zoom','Long
Exposure','Fast Shutter','Daytime Long Expo','Timelapse']
#what we need here are conditional IF statements, that manipulate items
from various lists
#this bit of code determines the theme of a photo idea
theme_var = random.choice(theme_list)
for theme in theme_var:
if theme == 'Portrait':
technique_list.remove('Bracketed (HDR)')
print("",theme_var)
#this bit of code determines the technique of a photo idea
technique_var = random.choice(technique_list)
print("", technique_var)
print("picture, from")
#this line of code determines the location of a photo idea
location_var = random.choice(location_list)
print("", location_var)
This still remains one of the possible results of the code:
You should try taking a...
Portrait
Bracketed (HDR)
picture, from
34th Street
during
Sunrise
and then give it a
Black & White
edit in Lightroom!
[Finished in 0.2
As I said earlier, Portrait and Bracketed (HDR) should never be part of the same result, it doesn't make sense for this situation.
The issue (I think) is because you are iterating over the randomly chosen result not the list itself, you don't need the for loop that is.
theme_var = random.choice(theme_list)
if theme_var == 'Portrait':
technique_list.remove('Bracketed (HDR)')
print("",theme_var)
#this bit of code determines the technique of a photo idea
technique_var = random.choice(technique_list)
print("", technique_var)
print("picture, from")
#rest of the code
Should do it
I'd go with a dictionary of inappropriate techniques, a list comprehension, and top it off with an f-string:
import random
#pool of items that the program will randomly choose..
theme_list = ['Cityscape','Peoplescape','Port-Scape', 'Portrait']
technique_list = ['Bracketed (HDR)','Wide Angle', 'Zoom','Long Exposure','Fast
Shutter','Daytime Long Expo','Timelapse']
location_list = ['34th Street']
# dictionary of inappropriate techniques for given theme
d_inappropes = {'Cityscape': [],
'Port-Scape': [],
'Portrait': ['Bracketed (HDR)'],
'Peoplescape': ['Long Exposure', 'Timelapse', 'Daytime Long Expo']}
#this bit of code determines the theme of a photo idea
theme_var = random.choice(theme_list)
#this bit of code determines the technique of a photo idea
# list comprehension generates a new list with the inappropriate techniques removed,
# without affecting the original list
technique_var = random.choice([ti for ti in technique_list if ti not in d_inappropes[theme_var]])
#this line of code determines the location of a photo idea
location_var = random.choice(location_list)
# use f-stirngs to put the whole output in a single line to keep it managable
print(f"You should try taking a \n {theme_var} {technique_var} picture, \n from
{location_var}.")
if I may add and give more explanation to the answers
You want to delete "bracked (HDR)" IF potrait is selected. Don't use .remove as it will delete "bracked (HDR)" permanently and prevent other theme to use that technique. you can use dictionary of inappropriate technique as kingfischer suggested for that
random.choice outputted a single value from your list. you should not use for-loop with it as for-loop will iterate over the character/alphabets in the value outputted by random.choice
if I may give a feedback, the indentations in your code snippet are quite jumbled. Some lines that should have indentation, don't have it. I don't know.. maybe it is unintended and the problem is with my browser. if it was so, sorry!

In SikuliX, type(variable) always types the variable then presses enter automatically. How do I avoid pressing enter?

Sorry if this is really basic, I cannot find a workaround. I have a variable called doc that stores the number 510 that was copied from an excel cell.
I need to type it in a field, but I need to continue typing in another field on the same page afterwards.
My code has:
type(doc)
The log shows:
[log] TYPE "510#ENTER."
The full code looks like this:
type(doc)
wait(1)
type(Key.DOWN)
type(Key.BACKSPACE+Key.BACKSPACE+Key.BACKSPACE+Key.BACKSPACE)
wait(1)
type(code)
However, I can't get to the type(code) because it switches page before I get there...
Using paste() maybe solved your issue here but this is not the right way to do that as Sikuli does not automatically presses any buttons.
Your problem is probably with the doc variable itself. In your case, you probably just copied the new line character with your variable from excel and that's why Sikuli is hitting Enter. To avoid that, try stripping the new line from your variable prior to typing it, like this:
doc.rstrip()
Then do your usual type(doc) and it should be fine.
Another thing that works is: doc.strip()
It turns out sikuli writes /n after strings, so strip removes that /n.

wxPython message dialog won't work with function

I'm new to Python and just started writing a basic GUI program with wxPython. I have a series of text boxes where the user enters data and then they click a submit button.
The submit button triggers a getvalue method for each box (it looks like a=self.textbox1.GetValue()).
The there is a function that simply reads answer=a+b+c+d+e+f+g+h.
Then finally there's the wx.MessageDialog(self, answer, Title, wx.OK | wx.ICON_EXCLAMATION) that prints the answer in a msg dialog.
But instead of printing the sum of the numbers, it just prints them in a series.
I was messing around and replaced the variables in the answer function with actual integers and it gives me an error that says:
String or Unicode type required
I can't really think of any way to fix it since I only have like two days experience with Python.
How can I fix this?
GetValue() gives you the string value of what was typed.
You want to convert the strings to integers before trying to sum them.
a = int(self.textbox1.GetValue())

Categories