Pyglet, how to make the ESCAPE key not close the window? - pyglet

I am writing a small sample program and I would like to override the default pyglet's behavioyr of ESC closing the app. I have something to the extent of:
window = pyglet.window.Window()
#window.event
def on_key_press(symbol, modifiers):
if symbol == pyglet.window.key.ESCAPE:
pass
but that does not seem to work.

I know the question is old, but just in case. You've got to return pyglet.event.EVENT_HANDLED to prevent default behaviour. I didn't test it, but in theory this should work:
#window.event
def on_key_press(symbol, modifiers):
if symbol == pyglet.window.key.ESCAPE:
return pyglet.event.EVENT_HANDLED

Same for me. The question is old, but I've found out that you should use window handlers mechanisms, thus making the current event not to propagate further.
You can prevent the remaining event
handlers in the stack from receiving
the event by returning a true value.
The following event handler, when
pushed onto the window, will prevent
the escape key from exiting the
program:
def on_key_press(symbol, modifiers):
if symbol == key.ESCAPE:
return True
window.push_handlers(on_key_press)
Here is that link

On the Google group for pyglet-users it is suggest could overload the window.Window.on_key_press(), although there are no code example of it.

It's simple actually, subclass Window and overide the on_key_press, like this:
class MyWindow(pyglet.window.Window):
def on_key_press(self, symbol, modifiers):
if symbol == key.ESCAPE:
return pyglet.event.EVENT_HANDLED

Related

VsCode Extension custom CompletionItem disables built-in Intellisense

I am working on a VsCode extension in that I want to provide custom snippets for code completion.
I know about the option of using snippet json files directly, however those have the limitation of not being able to utilize the CompletionItemKind property that determines the icon next to the completion suggestion in the pop-up.
My issue:
If I implement a simple CompletionItemProvider like this:
context.subscriptions.push(
vscode.languages.registerCompletionItemProvider(
{scheme:"file",language:"MyLang"},
{
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position) {
let item = new vscode.CompletionItem('test');
item.documentation = 'my test function';
item.kind = vscode.CompletionItemKind.Function;
return [item];
}
}
)
)
then the original VsCode IntelliSense text suggestions are not shown anymore, only my own. Should I just return a kind of an empty response, like
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position) {
return [null|[]|undefined];
}
the suggestions appear again as they should. It seems to me that instead of merging the results of the built-in IntelliSense and my own provider, the built-in ones get simply overridden.
Question:
How can I keep the built-in IntelliSense suggestions while applying my own CompletionItems?
VsCode Version: v1.68.1 Ubuntu
I seem to have found the answer for my problem, so I will answer my question.
Multiple providers can be registered for a language. In that case providers are sorted
by their {#link languages.match score} and groups of equal score are sequentially asked for
completion items. The process stops when one or many providers of a group return a
result.
My provider seems to provide results that are just higher scored than those of IntelliSense.
Since I didn't provide any trigger characters, my CompletionItems were comteping directly with the words found by the built-in system by every single pressed key and won.My solution is to simply parse and register the words in my TextDocument myself and extend my provider results by them. I could probably just as well create and register a new CompletionItemProvider for them if I wanted to, however I decided to have a different structure for my project.

Unable to select an option from a drop down in Katalon studio

I am new to Katalon Studio and I am facing an issue regarding selection of the drop down.
Please find below the details:
This is the HTML :
I have tried using selectByIndex with the object xpath as:
//div[#class='paCriteriaContainer']//select[#class = 'pa-criteria-select a-select initialized']
It does not select any option and fails with an error stating "Unable to select option by index '2' of object"
Note:
I tried clicking on the input and then selecting the option, but that doesn't seem to work either.
Selecting by label and value don't work either
Please help me here.
Thank you
Try to capture an object and then use following methods :
WebUI.click(findTestObject(Your captured object))
WebUI.selectOptionByValue(findTestObject(Your captured object), 'TEST (2020)', false)
Did you done as I've described and it does not work ?
I tried clicking on the input and then selecting the option, but that doesn't seem to work either.
Are you sure you are clicking the right element in this case?
Try the following instead: create programmatically the element and select by value (note, value isn't the text contained, it is the value html attribute):
TestObject to = new TestObject().addProperty("xpath", ConditionType.EQUALS, "//div[#class='paCriteriaContainer']//select[#class = 'pa-criteria-select a-select initialized']")
WebUI.selectOptionByValue(to, '40696', false)
You have some options to do that, I reggardly suggest you that use always the xpath to reach all the elements that you want to use. The reasson is because the object reports usually fail and, in my opinion, this way is so much more complicated.
But obviously, the xpath will change if the web do, so take care with it.
The imports you need:
import static org.junit.Assert.*
import org.openqa.selenium.By
import org.openqa.selenium.Keys
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
def driver = DriverFactory.getWebDriver()
//If you want to click your input would be:
WebUI.click(WebUI.convertWebElementToTestObject(driver.findElement(By.xpath("(//input[#id='a-select-paCricteriaId_6908'])"))))
//**you just can click on "TestObject" type, and findElement returns "Element" type**
And if you want to select the option you need to know the whole path (I cannot get it with the given information).
An important tip for testing the xpath is to use this function in console mode (F12):
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
//And this code in the same console to test your xpath:
getElementByXpath("YOURTESTXPATH")
Furthermore, there are other ways to reach the same objetive with xpath, for example:
import com.kms.katalon.core.testobject.TestObject as TestObject
...
TestObject tobj = new TestObject('myTestObject')
String expr = '/some/valid/xpath/expression'
tobj.addProperty('xpath', ConditionType.EQUALS, expr)
WebUI.click(tobj)
You have a lot of information if you google "how to get elements by xpath katalon".
Here you can get official information about it:
https://docs.katalon.com/katalon-studio/tutorials/detect_elements_xpath.html#what-is-xpath
Test in the browser console
$x('//*[contains(#class, "pa-criteria-select a-select initialized")]')
if more than one result appear then you can access it like this
$x('(//*[contains(#class, "pa-criteria-select a-select initialized")])[1]')
then you can also access their children
$x('(//*[contains(#class, "pa-criteria-select a-select initialized")])[1]/option')
Use WebUI.selectOptionByIndex keyword but the object should point to the select tag instead of the div.
Update the object element and your code should work

How to use delta trigger in flink?

I want to use the deltatrigger in apache flink (flink 1.3) but I have some trouble with this code :
.trigger(DeltaTrigger.of(100, new DeltaFunction[uniqStruct] {
override def getDelta(oldFp: uniqStruct, newFp: uniqStruct): Double = newFp.time - oldFp.time
}, TypeInformation[uniqStruct]))
And I have this error:
error: object org.apache.flink.api.common.typeinfo.TypeInformation is not a value [ERROR] }, TypeInformation[uniqStruct]))
I don't understand why DeltaTrigger need TypeSerializer[T]
and I don't know what to do to remove this error.
Thanks a lot everyone.
I would read into this a bit https://ci.apache.org/projects/flink/flink-docs-release-1.2/dev/types_serialization.html sounds like you can create a serializer using typeInfo.createSerializer(config) on your type info. Note what you're passing in currently is a type itself and NOT the type info which is why you're getting the error you are.
You would need to do something more like
val uniqStructTypeInfo: TypeInformation[uniqStruct] = createTypeInformation[uniqStruct]
val uniqStrictTypeSerializer = typeInfo.createSerializer(config)
To quote the page above regarding the config param you need to pass to create serializer
The config parameter is of type ExecutionConfig and holds the
information about the program’s registered custom serializers. Where
ever possibly, try to pass the programs proper ExecutionConfig. You
can usually obtain it from DataStream or DataSet via calling
getExecutionConfig(). Inside functions (like MapFunction), you can get
it by making the function a Rich Function and calling
getRuntimeContext().getExecutionConfig().
DeltaTrigger needs a TypeSerializer because it uses Flink's managed state mechanism to store each element for later comparison with the next one (it just keeps one element, the last one, which is updated as new elements arrive).
You will find an example (in Java) here.
But if all you need is a window that triggers every 100msec, then it'll be easier to just use a TimeWindow, such as
input
.keyBy(<key selector>)
.timeWindow(Time.milliseconds(100)))
.apply(<window function>)
Updated:
To have hour-long windows that trigger every 100msec, you could use sliding windows. However, you would have 10 * 60 * 60 windows, and every event would be placed into each of these 36000 windows. So that's not a great idea.
If you use a GlobalWindow with a DeltaTrigger, then the window will be triggered only when events are more than 100msec apart, which isn't what you've said you want.
I suggest you look at ProcessFunction. It should be straightforward to get what you want that way.

Drag and Drop between two QListWidgets PyQt5

I have two QListWidgets. I want to be able to move items from widget 1 onto widget 2 and vice versa by dragging and dropping. It must work with MultiSelection mode. It must be a MoveAction NOT a copy action. A simple way to achieve this is to use:
self.listWidget_2.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
self.listWidget_2.setDefaultDropAction(QtCore.Qt.MoveAction)
self.listWidget_2.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
self.listWidget_2.setObjectName("listWidget_2")
self.listWidget_2.acceptDrops()
This achieves all the desired requirements with one caveat. Dragging and dropping the item onto the widget where it currently resides removes it from the widget. A definite no go. I Then tried to write my own QListWidget class to achieve my desired results to no avail, this is the class:
class dragLeaveList(QtWidgets.QListWidget):
def __init__(self, type, parent=None):
super(dragLeaveList, self).__init__(parent)
self.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
self.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
self.setAlternatingRowColors(True)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasText():
event.accept()
print("Drag Enter Event: IF CLAUSE")
else:
super(dragLeaveList, self).dragEnterEvent(event)
print("Drag Enter Event: ELSE CLAUSE")
def dragMoveEvent(self, event):
if event.mimeData().hasText():
event.setDropAction(QtCore.Qt.MoveAction)
event.accept()
else:
event.ignore()
super(dragLeaveList, self).dragMoveEvent(event)
def dropEvent(self, event):
print("Drop Event ", event)
if event.mimeData().hasText():
event.setDropAction(QtCore.Qt.MoveAction)
event.accept()
print("Drop Event - 1 ", event)
def startDrag(self, event):
print("In Start Drag")
item = self.currentItem()
itemText = self.currentItem().text()
itemData = QtCore.QByteArray()
dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.WriteOnly)
print(item, itemText)
mimeData = QtCore.QMimeData()
mimeData.setData(itemText, itemData)
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
if drag.exec_(QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction:
if self.currentItem() is not None:
self.takeItem(self.row(item))
I've read every tutorial online and all the related documentation on sourceforge and i just can't seem to make this work. I rarely end up not being able to figure something out but the documentation for this just seems to be pretty darn bad. I'm really looking for explanations along with the code. However, as i said I've read all the related documentation multiple times so I'm not completely ignorant. Seems to me in the above class that i have a lot more then what i need and it may have the right components just not the right implementation. Please give me some clarity on this subject, thanks a bunch.
Main things to clarify in answer code
The bare minimum/most effective DragDrop(the built in methods i.e. dragEnterEvent, dragMoveEvent et cetera) methods.
Is mimeData() absolutely required? Assume yes from the Docs.
Formatting the mimeData() into a QListWidgetItem.
Does the item have to be manually removed from the widget or is this
built in with the QtCore.Qt.MoveAction?
Actually making the QtCore.Qt.MoveAction move the list item.
So i have figured out one solution to get everything in working order. Keep in mind that this answer does not teach me what i wanted to know. However, this may be helpful to others wishing to do something the same or similar. I'm am still looking for a method the touches on the topics listed in " Main things to clarify code" list.
class QListDragAndDrop(QtWidgets.QListWidget):
def __init__(self):
super(QListDragAndDrop, self).__init__()
self.setFrameShape(QtWidgets.QFrame.WinPanel)
self.setFrameShadow(QtWidgets.QFrame.Raised)
self.setDragEnabled(True)
self.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
self.setDefaultDropAction(QtCore.Qt.MoveAction)
self.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
self.setMovement(QtWidgets.QListView.Snap)
self.setProperty("isWrapping", True)
self.setWordWrap(True)
self.setSortingEnabled(True)
self.setAcceptDrops(True)
Just make this QListWidget class with these parameters set and import to wherever you need it works like a charm but does not really teach you much. Hopefully, this helps certain people get by until someone answers the question properly.

Eclipse auto completion: How to show only those methods with a specific pattern/wildcard?

I often know only parts of a method, I would like to call, but not the whole name (especially not the beginning).
So I'd like to know, if there is a possibility to type in a pattern/wildcard while auto completion is active.
Thanks in advance!
// I'd like to call str.toUpperCase() but imagine I don't know that it starts with to
String str = "some string";
str.*upper // sth like that while auto completion is active
See also
Using Wildcards with Content Assist in Eclipse