loop/repeat in sikuli - sikuli

switchApp("Safari.app")
t = find(img)
t1= capture(t.getX(), t.getY() - 25, t.getW(), t.getH(),)
click(t1)
sleep(1)
type("text")
sleep(2)
type(Key.ENTER)
type(Key.PAGE_DOWN)
I have the above working once.. however, how would i repeat this to do the same for all matches on a web page? I'm not sure the code for looping this action / repeating.
Thanks

See example usage of the findAll function here: http://sikuli.org/trac/wiki/reference-0.10#IteratingMatches
You might do it like this:
with findAll(img) as tt:
while tt.hasNext():
t = tt.next()
t1 = capture(t.getX(), t.getY() - 25, t.getW(), t.getH())
# etc.

Related

Weird KeyError (Python)

So, I have to work with this JSON (from URL):
{'player': {'racing': 25260.154000000017, 'player': 259114.57700000296}, 'farming': {'fishing': 33783.390999999414, 'mining': 29048.60500000002, 'farming': 25334.504000000023}, 'piloting': {'piloting': 25570.18800000001, 'cargos': 3080.713000000036, 'heli': 10433.977000000004}, 'physical': {'strength': 198358.86700000675}, 'business': {'business': 50922.88500000005}, 'trucking': {'mechanic': 2724.5620000000004, 'garbage': 755.642999999997, 'trucking': 223784.99700000713, 'postop': 1411.4190000000006}, 'train': {'bus': 669.1940000000001, 'train': 1363.805999999999}, 'ems': {'fire': 25449.43400000001, 'ems': 13844.628000000012}, 'hunting': {'skill': 4179.033000000316}, 'casino': {'casino': 18545.526000000027}}
It is indeed one line. I am trying to make it so that for example, I can get racing, which is the first one you see. For this, you need go into Player first, and then you can get to Racing. How do I do this?
My current code:
def allthethings():
# Grab all the skills
geturl = ("http://server.tycoon.community:30120/status/data/" + str(setting_playerid))
print(geturl)
a = requests.get(geturl,headers={"X-Tycoon-Key":setting_apikeyTT}).json()
jsonconverted = (a["data"]["gaptitudes_v"])
print(jsonconverted)
# Convert JSON into many, many variables
Raw_RACR = jsonconverted['player.racing']
print(Raw_RACR)
I believe this is all the code that is needed.
Also, this is the error:
KeyError: 'player.racing'

I couldn't show article on view rails

I'm beginner of programming and doing the project on rails.
I'm having a problem that I can't show the data on view.
The codes are listed bellow.
#routes.rb
scope module: :mobile do
scope module: :home do
get "/", action: :index
-
#index.html.slim
- if #pickup_links.present?
.user-posts-area
.inner-headline
h2 Pickup Link
h3 ピックアップリンク
.top-user-posts
- pl = #pickup_links
a.post href=pl.page_path
img.lazy data-original=pl.picture
.post-descs
h3 = pl.title_or_notitle
h4 = pl.name_or_no_name
.date-area
.right-date = pl.created_at.to_s(:md_dot_en)
-
#home_controller.rb
def index
#pickup_links = PickupLink.limit(1)
end
I tested "#pickup_links = PickupLink.limit(1)" on terminal and could get the data from the database.
Please someone give me a hand.
I am not familiar with "slim" but it looks like "HAML". So my guess is that your line
- pl = #pickup_links
is not a block, so all following line should not be nested.
Another matter (I know this is only a test project but) why don't you do
# why link**s**
#pickup_links = PickupLink.first
then you would only test like this
- if #pickup_links
and you would not need to set
-pl = #pickup_links
but just use #pickup_links. "pl" btw is still a relation of PickupLink and has none of the methods you are calling

Java progressbar for matlab, how do you feed the values to it?

I came across an interesting alternative to matlab waitbar which is from java, it follows as below.
jBarHandle = javax.swing.JProgressBar(0, 103);
jBarHandle.setStringPainted(true);
jBarHandle.setIndeterminate(false);
posPanel = getpixelposition(handles.slider1,true);
javacomponent(jBarHandle,posPanel,handles.fig);
jBarHandle.setValue(fix(0));
calling the above code under output function of GUIDE will update the slider with the java enabled one. But how to pass the variable_data into setValue ?
anyone tried this? or have any idea? please help.
You can pass the variable directly to the setValue method of your JProgressBar object. Here I have used javaMethodEDT to ensure that the method is called from the Event Dispatch Thread (EDT). You could also just call jBarHandle.setValue(k) directly.
jBarHandle = javax.swing.JProgressBar(0, 103);
jBarHandle.setStringPainted(true);
jBarHandle.setIndeterminate(false);
fig = figure('Position', [0 0 200 20]);
[jhandle, hhandle] = javacomponent(jBarHandle, [0 0 1 1], fig);
set(hhandle, 'parent', fig, 'Units', 'norm', 'Position', [0 0 1 1])
for k = 1:103
javaMethodEDT('setValue', jBarHandle, k);
pause(0.1);
end
The value that you pass to the progress bar should just be a variable within your local scope. I'm not completely sure why you're overlaying a slider with this progress bar, but you could link the value of these two by simply doing the following
listener = addlistener(handles.slider1, 'Value', 'PostSet', #(s,e)jBarHandle.setValue(e.NewValue));
Then whenever the slider value changes (either programmatically or through the GUI), the progress bar gets updated as well.
More info in the EDT here provided by Yair Altman.
If you want to save your java handle within your GUI you can use the following
setappdata(handles.fig, 'ProgressBar', jBarHandle);
Then from the external function you can retrieve it the following way
figure_handle = findall(0, 'type', 'figure', 'tag', 'fig');
jBarHandle = getappdata(figure_handle, 'ProgressBar');
javaMethodEDT('setValue', jBarHandle, value)

Ruby on Rails optimalization of some code

I have some simple code that uses the minmax algoritm to locate birds. Everything works but I find my programming not good and I believe there is a better solution. I'm not that experienced in RoR but if somebody knows a better way to achieve the same solution then I'm greatful ;).
There are two parts I hate, the 4 lists I had to create to determine the max or min value for the different combinations (the core of the min-max algorithm) and the very ugly SQL hack.
Thanks!
def index
# fetch all our birds
#birds = Bird.all
# Loop over the birds
#birds.each do |bird|
#fixed = Node.where("d7type = 'f'")
xminmax = []
xmaxmin = []
yminmax = []
ymaxmin = []
#fixed.each do |fixed|
rss = Log.find_by_sql("SELECT logs.fixed_mac, AVG(logs.blinker_rss) AS avg_rss FROM logs
WHERE logs.blinker_mac = '#{bird.d7_mac}' AND logs.fixed_mac = '#{fixed.d7_mac}' ORDER BY logs.id DESC LIMIT 30")
converted_rss = calculate_distance_rss(rss[0].attributes["avg_rss"])
xminmax.push(fixed.xpos + converted_rss)
xmaxmin.push(fixed.xpos - converted_rss)
yminmax.push(fixed.ypos + converted_rss)
ymaxmin.push(fixed.ypos - converted_rss)
end
pos = {x: (xminmax.min + xmaxmin.max) / 2, y: (yminmax.min + ymaxmin.max) / 2}
puts pos
end
end
2 things you could do to start with is (assuming Birds could be a large table) Change Bird.all to
Bird.find_each do |bird|
... code ...
end
It's a more efficient way to loop over many table records.
2nd: take #fixed = Node.where("d7type = 'f'") out of the each loop since it doesn't need any variables for its query. Put it above the loop so it doesn't execute each time.
3rd (Not so much of an optimization but just safer code): Your Log.find_by_sql looks simple enough to use active_record, you can change it to:
Log.select('fixed_mac, AVG(logs.blinker_rss) AS avg_rss, blinker_mac').
where(blinker_mac: bird.d7_mac, fixed_mac: fixed.d7_mac).
order('id DESC').limit(30)
converted_rss = calculate_distance_rss(rss.first.avg_rss)
Everything else looks fine.

How do I set a function to a variable in MATLAB

As a homework assignment, I'm writing a code that uses the bisection method to calculate the root of a function with one variable within a range. I created a user function that does the calculations, but one of the inputs of the function is supposed to be "fun" which is supposed to be set equal to the function.
Here is my code, before I go on:
function [ Ts ] = BisectionRoot( fun,a,b,TolMax )
%This function finds the value of Ts by finding the root of a given function within a given range to a given
%tolerance, using the Bisection Method.
Fa = fun(a);
Fb = fun(b);
if Fa * Fb > 0
disp('Error: The function has no roots in between the given bounds')
else
xNS = (a + b)/2;
toli = abs((b-a)/2);
FxNS = fun(xns);
if FxNS == 0
Ts = xNS;
break
end
if toli , TolMax
Ts = xNS;
break
end
if fun(a) * FxNS < 0
b = xNS;
else
a = xNS;
end
end
Ts
end
The input arguments are defined by our teacher, so I can't mess with them. We're supposed to set those variables in the command window before running the function. That way, we can use the program later on for other things. (Even though I think fzero() can be used to do this)
My problem is that I'm not sure how to set fun to something, and then use that in a way that I can do fun(a) or fun(b). In our book they do something they call defining f(x) as an anonymous function. They do this for an example problem:
F = # (x) 8-4.5*(x-sin(x))
But when I try doing that, I get the error, Error: Unexpected MATLAB operator.
If you guys want to try running the program to test your solutions before posting (hopefully my program works!) you can use these variables from an example in the book:
fun = 8 - 4.5*(x - sin(x))
a = 2
b = 3
TolMax = .001
The answer the get in the book for using those is 2.430664.
I'm sure the answer to this is incredibly easy and straightforward, but for some reason, I can't find a way to do it! Thank you for your help.
To get you going, it looks like your example is missing some syntax. Instead of either of these (from your question):
fun = 8 - 4.5*(x - sin(x)) % Missing function handle declaration symbol "#"
F = # (x) 8-4.5*(x-sin9(x)) %Unless you have defined it, there is no function "sin9"
Use
fun = #(x) 8 - 4.5*(x - sin(x))
Then you would call your function like this:
fun = #(x) 8 - 4.5*(x - sin(x));
a = 2;
b = 3;
TolMax = .001;
root = BisectionRoot( fun,a,b,TolMax );
To debug (which you will need to do), use the debugger.
The command dbstop if error stops execution and opens the file at the point of the problem, letting you examine the variable values and function stack.
Clicking on the "-" marks in the editor creates a break point, forcing the function to pause execution at that point, again so that you can examine the contents. Note that you can step through the code line by line using the debug buttons at the top of the editor.
dbquit quits debug mode
dbclear all clears all break points