Week 04 2020

  • Python: One way to handle focusOut event by installing event filter on the widget
# Installing event filter
self.parent.txt_scratch_pad.installEventFilter(self.events)

# where self.events in a class with a QObject parent class - like ScratchPadEvents ๐Ÿ‘‡

# Then override eventFilter method in that class
class ScratchPadEvents(QObject):
    def __init__(self, parent, app):
        super().__init__(parent)

    def eventFilter(self, source: QObject, event: QEvent):
        if event.type() == QtCore.QEvent.FocusOut:
            # do something
            pass

        return super().eventFilter(source, event)
  • ImageMagick: Resize a bunch of images using ImageMagick
find ./static -name '*.png' | while read line; do convert -resize 1280x1280 $line $line; done
  • Projects: Add a guide on making contributions to DevRider project.

  • Java: Initialise a CharArray from a String

private String reservedCharacter = "!#$%&'()*+,/:;=?@[]";
private char[] chars = reservedCharacter.toCharArray();

We start with finding the ASCII value of : which is 58. See ASCII table

Convert 58 to binary using Short division by Two with Reminder. i.e. Divide the answer by 2 and keeping the reminder which will be the binary form.

58/2 = 29 => Reminder 0
29/2 = 14 => Reminder 1
14/2 = 7  => Reminder 0
7/2  = 3  => Reminder 1
3/2  = 1  => Reminder 1
1/2  = na => Reminder 1

So the binary form is 111010 if we line up all the reminders Padding the output will make it 00111010.

Convert the binary into two parts of 4 bits each.

0011 1010

Using the following Hex table conversion, we map 0011 => 3 and 1010 to A which becomes 3A.

Decimal Binary Hex
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F

The final part is to prefix it with % (which is the escape character) makes it %3A

  • Simple but very useful website to format Markdown tables -> http://markdowntable.com

  • Java/JUnit5 : Parameterized testing with JUnit 5

@ParameterizedTest(name = "{0} should be \"{1}\"")
    @CsvSource({
            "http://www.example.com, http%3A%2F%2Fwww.example.com",
            "?query=123, %3Fquery%3D123"
    })
    public void testPercentEncodeAsciiString(String source, String expected) {
        // when
        String result = PercentEncoding.encode(source);

        // then
        assertEquals(expected, result);
    }
  • Java: Generating a mvn wrapper
$ mvn -N io.takari:maven:0.7.7:wrapper
  • Python/PyQt5: TextEdit Widget Scroll to top
def scroll_to_top(txt_edit_widget):
    txt_cursor: QTextCursor = txt_edit_widget.textCursor()
    txt_cursor.movePosition(QTextCursor.Start)
    txt_edit_widget.setTextCursor(txt_cursor)
  • Python/PyQt5: Replacing Text in a PlainTextEdit without losing undo/redo history
def replace_text(self, txt_edit_widget, new_text):
    txt_cursor: QTextCursor = txt_edit_widget.textCursor()
    txt_cursor.select(QTextCursor.Document)
    txt_cursor.insertText(new_text)
  • Python/JsonPath: Filtering JSON using JsonPath.
pip install jsonpath-ng

from jsonpath_ng.ext import parse as jsonpath_parse

def filter_json_path(self, json_doc, path_query):
    if not json_doc:
        return

    parsed_query = jsonpath_parse(path_query)
    key_found = parsed_query.find(json_doc)
    if key_found:
        return key_found[0].value
    else:
        return None
  • Annoying: brew install unix2dos