Qtile: Make parse function for long texts like browsers - python

I have the following problem.
The widget "WindowName" displays very long Browser names (ie "Stackoverflow | Questions | Ask -- Mozilla Firefox)
Is there a way to shorten this? I am aware that qtile comes with a parse function but it's default function doesnt work

The following function solved this issue, however if someone has a better implementation I would greatly appreciate it.
Add this function to your config.py file
def longNameParse(text):
for string in ["Chromium", "Firefox"]: #Add any other apps that have long names here
if string in text:
text = string
else:
text = text
return text
And then when you call your Window Name widget, you use the function as an argument
widget.WindowName(parse_text=longNameParse)
That fixed it

Related

Replace words from an API in python

I am using an API I found online for one of my scripts, and I am wondering if I can change one word from the API to something else. My code is:
import requests
people = requests.get('https://insult.mattbas.org/api/insult')
print("Welcome to the insult machine!\nType somebody you want to insult!")
b = input()
print(people.replace("You", b))
Is replace not a command? If so, what plugin and/or commands would I need to do it? Thanks!
The value returned from requests.get isn’t a string, it’s a response object and that class has no replace method.
Have a look at the structure of that class. For example, you can do r = requests.get(...) and r.text.replace(...).
In other words, you need to operate on the text part of the response object.

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.

Why can't I write control identifiers to a file or assign them to a string or list

I'm using pywinauto to list the control identifiers of a particular application. I can do that just fine. However, I want to save those control identifiers to file, or better yet assign them to a string or list, but cannot write them or assign them .... Does anyone know a way to get these identifiers to a file or memory programmatically?
sample code:
import os
import time
from pywinauto import application
from SendKeys import SendKeys
app=application.Application()
app.start_(r"C:\Program Files\myapp.exe")
app.dlg.print_control_identifiers()
Control Identifiers:
Button - 'Exit' (L900, T649, R975, B672)
'Button' 'Button0' 'Button1' 'Exit' 'ExitButton'
Button - 'About' (L339, T646, R410, B672)
'About' 'AboutButton' 'Button2'
...
...
...
I tried the following:
my_App_ci = app.dlg.print_control_identifiers()
And:
my_App_ci = []
my_App_ci.append(app.dlg.print_control_identifiers())
to no avail ....
You can use print_control_identifiers(filename="path\to\your\desktop\file.txt")
print_control_identifiers prints to stdout instead of returning a string. I did a quick look at the source and I could not see any functions to get them as strings, which is pretty crappy design IMHO.
You could capture the information by reassigning sys.stdout to an StringIO object and get the string from that. Or read the source to see what print_control_identifiers does and make a version that returns a list of strings.

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())

Process string using sphinx

I was wondering whether it is possible to process a string in python, using sphinx. Basically, I would like to pass a string containing restructured text to sphinx, and then generate the corresponding HTML output. I meant something like this
import sphinx.something
s = 'some restructured text'
html_out = sphinx.something(s)
However, I could not find anything along these lines. So, is this possible, and if so, how would one do this?
The quickest solution I've seen is:
from docutils.examples import html_body
def rst(rstStr):
return html_body(rstStr, input_encoding='utf-8',
output_encoding='utf-8').strip()
I'd be interested myself in better solutions..

Categories