Redrawing gl2 isigraphs using event handler - j

I've been playing around with the table & gl2 demos, and have come across a "domain error: chkgl2" multiple times.
What I'm trying to do is to modify an isigraph after its initialized on program run, by selecting / un-selecting a checkbox.
I have the following code:
demo_run=: 3 : 0
require 'gl2'
coinsert 'jgl2'
wd 'pc test closeok escclose'
wd 'pn "Tester"'
wd 'cc gs isigraph'
wd 'cc cb checkbox; cn "Test GUI"'
wd 'set cb value 0'
wd 'pmove 400 10 400 200'
wd 'pshow'
)
demo_cb_button=: 3 : 0
demo_gs_paint''
)
demo_gs_paint=: 3 : 0
'w h'=: glqwh''
glfill 0 0 0 255
glrgb 255 223 0
glpen 2 1
if. cb do.
for_i. 250*i. >. h%250 do.
gllines 0,i,w,i
end.
end.
)
demo_run''
What I'm expecting to happen is the checkbox to update the isigraph. What I'm getting is a "domain error: chkgl2" on line "glfill 0 0 0 255".
Any help would be appreciated!

A couple of things that I spotted:
1) you are calling your parent 'test' but you are giving the verbs a 'demo' prefix. You can do this for the verb demo_run, but if you want the children of the form to react you will need to refer to them in the form of test_cb_button
2) cb in your test is actually the character '1' and not the integer 1, so in order for it to work as a conditional you need to change it to an integer type. I used Do (".) to make this change (". cb)
3) I am not sure that your code 250*i. >. h%250 is doing what you expect, so I replaced it with i. >.h%15 and then changed the gllines command to gllines 0,i,w,i*15 for a more interesting effect.
4) You are using isigraph which automatically triggers the painting of the screen and I wonder if the fact that you are clicking a button which triggers drawing of a screen to change the button image while also explicitly requiring the painting of the screen using the test_gs_paint verb is causing the glfill to be run twice. The second time it may be in a context that creates a domain error. To be honest, I don't know. What I do know is that when I use isidraw instead and trigger my own paint command using glpaint '' (which isidraw requires) that things seem to work.
5) I have added a second version that works with isigraph and it looks as if the issue is that by calling the paint verb directly with test_gs_paint that you were indeed calling it twice. By changing the action to glpaint '', the form does rerun the painting of the entire form once and this causes test_gs_paint to update as well. This time though it does it in a more coordinated fashion. My second version reflects the change. Other corrections I have left in place.
My isidraw version of the code follows:
demo_run=: 3 : 0
require 'gl2'
coinsert 'jgl2'
wd 'pc test closeok escclose'
wd 'pn "Tester"'
wd 'cc gs isidraw' NB. changed from isigraph
wd 'cc cb checkbox; cn "Test GUI"'
wd 'set cb value 0'
wd 'pmove 400 10 400 200'
wd 'pshow'
)
test_cb_button=: 3 : 0
test_gs_paint''
)
test_gs_paint=: 3 : 0
'w h'=: glqwh''
glfill 0 0 0 255
glrgb 255 223 0
glpen 2 1
if. (".cb) do. NB. needed to test on an integer and not a character value
for_i. i. >.h%15 do. NB. changed to give a more noticeable effect
gllines 0,i,w,i*15
end.
end.
glpaint'' NB. added to accommodate isidraw requirement of explicit painting
)
demo_run''
My isigraph version of the form:
demo_run=: 3 : 0
require 'gl2'
coinsert 'jgl2'
wd 'pc test closeok escclose'
wd 'pn "Tester"'
wd 'cc gs isigraph'
wd 'cc cb checkbox; cn "Test GUI"'
wd 'set cb value 0'
wd 'pmove 400 10 400 200'
wd 'pshow'
)
test_cb_button=: 3 : 0
glpaint '' NB. changed from test_gs_paint ''
)
test_gs_paint=: 3 : 0
'w h'=: glqwh''
glfill 0 0 0 255
glrgb 255 223 0
glpen 2 1
if. (".cb) do.
for_i. i. >.h%15 do.
gllines 0,i,w,i*15
end.
end.
)
demo_run''

Related

Watir Scroll For All Comments

I'm trying to scroll until there are no more contents on a Youtube video. I'm testing a counter versus the current number of comments displayed.
mycount = 0
1.upto(20) do
thiscount = browser.div(id: "contents").divs(id: "comment-content").size
puts "#{ mycount } : #{ thiscount }"
break if mycount == thiscount
mycount = thiscount
browser.driver.execute_script("window.scrollBy(0,1000)")
sleep 10
end
After the first pagedown, the count of comments should increase. It isn't. I've entered a sleep 10 for the comments to load and the count of those comments to also update. It doesn't update. I keep getting 20 : 20 so it breaks and leaves this iteration after a single iteration.
I'm not sure why that valuation isn't updating. How can I fix this so that it can get to the end of the comments?
I ran into 2 problems when running your script:
browser.div(id: "contents").divs(id: "comment-content") returned nothing. There were many "contents" divs, with the first one not including any comments. I removed this locator.
Scrolling by 1000 is not enough to get to the bottom of the page. Therefore the loading of additional comments does not get triggered.
From the following script:
scroll_by = 1000000000000
browser.goto('https://www.youtube.com/watch?v=u9DPBxiZZfo&ab_channel=America%27sTestKitchen')
browser.driver.execute_script("window.scrollBy(0,#{scroll_by})")
sleep 15
mycount = 0
1.upto(20) do
thiscount = browser.divs(id: "comment-content").size
puts "#{ mycount } : #{ thiscount }"
break if mycount == thiscount
mycount = thiscount
browser.driver.execute_script("window.scrollBy(0,#{scroll_by})")
sleep 10
end
You can see that having a larger scroll gives the expected results:
0 : 20
20 : 40
40 : 60
60 : 80
80 : 100
100 : 120
120 : 140
140 : 160
160 : 180
180 : 185
185 : 185
In contrast, setting the scroll_by to just "1000" did not trigger the comments. The output was just:
0 : 0
This works for me:
total = 0
while true
before = browser.divs(id: "comment-content").count
browser.div(id: "columns").scroll.to :bottom
sleep 10 # or sleep 5
total = browser.divs(id: "comment-content").count
puts "There are #{total} comments for now"
break if before == total
end
puts "There are #{total} comments in total"
instead while true you can use 20.times do but that would only count up to ~400 comments.

Console Screen Buffer Info shows incorrect X position

I recently found a great short code Why the irrelevant code made a difference? for obtaining console screen buffer info (which I include below) that replaces the huge code accompanying the standard 'CONSOLE_SCREEN_BUFFER_INFO()' method (which I won't include here!)
import ctypes
import struct
print("xxx",end="") # I added this to show what the problem is
hstd = ctypes.windll.kernel32.GetStdHandle(-11) # STD_OUTPUT_HANDLE = -11
csbi = ctypes.create_string_buffer(22)
res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(hstd, csbi)
width, height, curx, cury, wattr, left, top, right, bottom, maxx, maxy = struct.unpack("hhhhHhhhhhh", csbi.raw)
# The following two lines are also added
print() # To bring the cursor to next line for displaying infp
print(width, height, curx, cury, wattr, left, top, right, bottom, maxx, maxy) # Display what we got
Output:
80 250 0 7 7 0 0 79 24 80 43
This output is for Windows 10 MSDOS, with clearing the screen before running the code. However. 'curx' = 0 although it should be 3 (after printing "xxx"). The same phenomenon happens also with the 'CONSOLE_SCREEN_BUFFER_INFO()' method. Any idea what is the problem?
Also, any suggestion for a method of obtaining current cursor position -- besides 'curses' library -- will be welcome!
You need to flush the print buffer if you don't output a linefeed:
print("xxx",end="",flush=True)
Then I get the correct curx=3 with your code:
xxx
130 9999 3 0 14 0 0 129 75 130 76
BTW the original answer in the posted question is the "great" code. The "bitness" of HANDLE can break your code, and not defining .argtypes as a "shortcut" is usually the cause of most ctypes problems.

Setting the RNG seed from the time

When you open up a new J console and execute:
? 1000
You'll always see the same result, 689 (Try it online!).
This is because the RNG will always start with the same initial seed.
I'm looking for a way to change the seed semi-randomly. It doesn't have to be secure (eg, using milliseconds of time or some similar method is fine).
I assume I would do this using 9!:45 (documented on this page) but I'm unsure how to proceed.
I also don't care which of the 4 RNGs I use. I simply want a way to produce different random-ish results even in a fresh console.
The foreign that you need to set the random seed is 9!:1 (Random Seed).
You can use the randomize verb available from the general/misc addon to randomly set the random seed using guids, or current time if the guid script is not available. For example:
9!:0 '' NB. Query the random seed/link
16807
require 'numeric'
randomize '' NB. Set a new seed for the session
_1672920848 1179844600 923541917 _282857428
9!:0 '' NB. Query the random seed/link
_1672920848 1179844600 923541917 _282857428
The definition of randomize is:
randomize
3 : 0
try.
require 'guid'
tmp=. _2 (3!:4) , guids 1
catch. NB. general/misc/guid.ijs not available
tmp=. >:<.0.8*0 60 60 24 31#.0 0 0 0 _1+|.<.}.6!:0 ''
end.
([ 9!:1) {.^:(2~:9!:42'') tmp NB. set random initial random seed
)

How do I search and replace in vi to eliminate a random number of random characters preceding a known string?

I have text files which look like this:
0 298047498 /directory1/app/20170417/file1.blob 0 f191
e 6569844 /directory1/app/20170417/file2.blob 0 f191
344 /directory1/app/20170417/file3.blob 0
8946 /directory1/app/20170417/file4.blob 0
196496 /directory1/app/20170417/file5.blob 0
9 182340752 /directory1/app/20170417/file6.blob 0 f191
68802 /directory1/app/20170417/file7.blob 0
I want to remove everything prior to the first / and everything after the file extension.
Results should look like this:
/directory1/app/20170417/file1.blob
/directory1/app/20170417/file2.blob
/directory1/app/20170417/file3.blob
Is there a way to do this using vi search and replace?
This type of question may be better placed here: https://vi.stackexchange.com/
But for now:
Yout can e.g. use a simple vim-macro, in which you collect all the key-strokes you need to edit one line and repeat this macro as many times as you need it.
Here are simply the key-strokes for one line:
dt/WD
d = delete..
t = ..till the first "/"
W = [shift]+[w] jumps to the next Word (after the "file-location-string")
D = [shift]+[d] deletes till the end of the current line
If you want to record this as a macro, do the following, with the keystrokes from above, inbetween - like this:
qmdt/WD[home][down]q
qm = start the recording of a macro in buffer "m"
... key-strokes from above
[home][down] = key [home] followed by [arrow down]-key, to move into the next line (for convenince)
q = end up the macro-recording
Now execute that macro with:
#m
And if you added the [down] key, you can do something like:
7#m
with which you fire your macro 7 times, for all your 7 lines.

user defined feature in CRF++

I tried to add more feature to CRF++ template.
According to How can I tell CRF++ classifier that a word x is captilized or understanding punctuations?
training sample
The DT 0 1 0 1 B-MISC
Oxford NNP 0 1 0 1 I-MISC
Companion NNP 0 1 0 1 I-MISC
to TO 0 0 0 0 I-MISC
Philosophy NNP 0 1 0 1 I-MISC
feature template
# Unigram
U00:%x[-2,0]
U01:%x[-1,0]
U02:%x[0,0]
U03:%x[1,0]
U04:%x[2,0]
U05:%x[-1,0]/%x[0,0]
U06:%x[0,0]/%x[1,0]
U07:%x[-2,0]/%x[-1,0]/%x[0,0]
#shape feature
U08:%x[-2,2]
U09:%x[-1,2]
U10:%x[0,2]
U11:%x[1,2]
U12:%x[2,2]
B
The traing phase is ok. But I get no ouput with crf_test
tilney#ubuntu:/data/wikipedia/en$ crf_test -m validation_model test.data
tilney#ubuntu:/data/wikipedia/en$
Everything works fine if ignore the shape fearture above. where did I go wrong?
I figured this out. It's the problem with my test data. I thought that every feature should be taken from the trained model, so I only have two columns in my test data: word tag, which turns out that the test file should have the exact same format as the training data do!

Resources