for loop in play framework 1.2.5 (groovy template) - groovy

I am using the following code to use the for loop with a list (I am familiar with the other syntax copied below as well). The first for loop is not working for me even though its the same list. Any ideas on how to go about troubleshooting this:
#{list items:0..items.size(), as:'item'}${item}#{/list} //Not Working - I have tried moving the items.size out of the loop as well but it has not worked for me
This is working for the same list:
#{list items, as:'item'}${item}#{/list}
Any thoughts/suggestions why? Thanks in advance.
Edit: The list is that of user defined instances (primarily holding Strings and Integers)

The syntax is incorrect, the items parameter need an iterable and you give it an integer, if you need an index you can use the item_index variable

Related

How do I avoid "'zip' object is not reversible" errors when migrating to Python 3?

In my recently migrated from 2 to 3 Python code I have
list(reversed(zip(*positions)))
which generates the error
TypeError: 'zip' object is not reversible
I can fix this by changing the problematic code to
list(reversed(list(zip(*positions))))
but this seems like the wrong way to go about it.
What is the correct way to revers a zip in Python 3?
reversed is used to iterate on the list. It doesn't create a list on purpose, because it's often used just to iterate backwards on elements, not to create lists.
That's why you have to use list on it to create a list. And it needs a sequence to be able to get to the last element directly so you have to do list(zip()) in python 3.
Maybe you could shorten
list(reversed(list(zip(*positions))))
to
list(zip(*positions))[::-1]
it creates a list directly without the need for reverse so it's probably slightly faster too.

Function getting "IndexError: list index of range" for writing to excel

Using python 2.7x:
I've searched through other questions but could not find anything similar to my functions. I'm writing to an excel spreadsheet. The variable a is not used anywhere else in the whole program. I have tried putting the global variable before the function, inside the function, adding into the function as shown below, changing from for loop to while loop, etc, almost everything as suggested by other posts, but they do not work unfortunately.
Could it be that since I'm using an array, it is affecting the code too?
I keep getting IndexError: list index out of range for this while loop and have no idea how to solve. Thanks so much for your help!
GPA is an array somewhere else in the program.
def function(a):
while (a <785):
sheet1.write(a+30,10,GPA[a][0])
sheet1.write(a+31,10,GPA[a][0])
sheet1.write(a+30,11,GPA[a][1])
sheet1.write(a+31,11,GPA[a][1])
sheet1.write(a+30,12,GPA[a][2])
sheet1.write(a+31,12,GPA[a][2])
a = a+2
function (1)
The codes are correct. The problem lies with the array which I've solved it.

Where can I find an overview of how the ec2.instancesCollection is built

In boto3 there's a function:
ec2.instances.filter()
The documentation:
http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#instance
Say it returns a list(ec2.Instance) I wish...
when I try printing the return I get this:
ec2.instancesCollection(ec2.ServiceResource(), ec2.Instance)
I've tried searching for any mention of an ec2.instanceCollection, but the only thing I found was something similar for ruby.
I'd like to iterate through this instanceCollection so I can see how big it is, what machines are present and things like that.
Problem is I have no idea how it works, and when it's empty iteration doesn't work at all(It throws an error)
The filter method does not return a list, it returns an iterable. This is basically a Python generator that will produce the desired results on demand in an efficient way.
You can use this iterator in a loop like this:
for instance in ec2.instances.filter():
# do something with instance
or if you really want a list you can turn the iterator into a list with:
instances = list(ec2.instances.filter())
I'm adding this answer because 5 years later I had the same question and went round in circles trying to find the answer.
First off, the return type in the documentation is wrong (still). As you say, it states that the return type is: list(ec2.Instance)
where it should be:ec2.instancesCollection.
At the time of writing there's an open issue in github covering this - https://github.com/boto/boto3/issues/2000.
When you call the filter method a ResourceCollection is created for the particular type of resource against which you called the method. In this case the resource type is instance which gives an instancesCollection. You can see the code for the ResourceCollection superclass of instancesCollection here:
https://github.com/boto/boto3/blob/develop/boto3/resources/collection.py
The documentation here gives an overview of the collections: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/collections.html
To get to how to use it and actually answer your question, what I did was to turn the iterator into a list and iterate over the list if the size is > 0.
testList = list(ec2.instances.filter(Filters=filters))
if len(testList) > 0;
for item in testList;
.
.
.
This may well not be the best way of doing it but it worked for me.

prolog recursive searching with contraints

I have a house with rooms that are defined with connections for when you can go from one room to another eg.
connection(garage,sidehall).
connection(sidehall,kitchen).
connection(kitchen,diningroom).
canget(X,Y):-connection(X,Y).
canget(X,Y):-connection(X,_),
write('player goes from '),write(X),write(' to '),write(Y),nl,
canget(_,Y).
Im trying to figure out how make it so the player can only get from one room to another when they have a specific item, such as you can only be in the kitchen when items = gloves.
canget(X,Y,Item):-connection(X,Y,Item),canbein(Y,Item).
canget(X,Y,Item):-connection(X,Somewhere,Item),canbein(Somewhere,Item),canget(Somewhere,Y,Item).
tried defining canbein with:
canbein(kitchen):- item(sword).
canbein(sidehall):- item(hat).
but that doesnt work!
Have defined my items as such, not sure if this is right either:
item(gloves,sword,helm,cheese).
Basically, have i declared my item values correctly?
How can i use the specific item value to make canget x to y false?
Thank you!
Well, I see a few problems with your code. Firstly, you call canbein with two arguments (from canget predicate). However, canbein is defined as single-argument predicate. Therefore, the call always fails as no canbein/2 predicate exists.
I suggest the following modification:
canbein(kitchen, sword).
canbein(sidehall, hat).
Than, the item definition is not required. Let's think about what happens during the unification of
canget(X,Y,Item) :- connection(X,Y,Item), canbein(Y,Item).
Let's assume the following setting X=sidehall, Y=kitchen, Item==sword. This predicate should be OK. Assuming the conection predicate is OK, prolog tries to find canbein(Y, Item) i.e. canbein(kitchen, sword) and it succeeds.
On the contrary, if the Item is different the unification fails, hence it works as expected.
The second problem is the item predicate. By your definition, it expects 4 arguments. That's nonsense, of course. You should declare it like
item(gloves).
item(sword).
item(helm).
item(cheese).
However, I don't think this predicate is necessary at all. Just to be clear, try to call item(X) and obtain all results (the four declared). Try it with the prior definition - what should you even ask for?
I hope it helps :)

How to save strings in matrixes in matlab

I want to have a matrix/cell, that has strings inside that I can access and use later as strings.
For instance, I have one variable (MyVar) and one cell (site) with names inside:
MyVar=-9999;
site={'New_York'; 'Lisbon'; 'Sydney'};
Then I want to do something like:
SitePosition=strcat(site{1},'_101'}
and then do this
save(sprintf('SitePosition%d',MyVar),);
This doesn't work at all! Is there a way to have strings in a matrix and access them in order to keep working with them if they were a string?
This:
MyVar=-9999; site={'New_York'; 'Lisbon'; 'Sydney'};
SitePosition = strcat(site{1},'_101');
save(sprintf('SitePosition%d',MyVar));
Works fine and yields SitePosition-9999.mat, note the syntax changes in lines 2 and 3.
Is there something else you're expecting?
EDIT: Based on your comment
Check out the documentation for save regarding saving specific variables
New example:
MyVar=-9999;
site={'New_York'; 'Lisbon'; 'Sydney'};
SitePosition = strcat(site{1},'_101');
save(SitePosition,'MyVar');
Creates New_York_101.mat with only the variable MyVar in it.

Resources