Matlab Functions - How do I access structures with string-input? [duplicate] - string

This question already has answers here:
How do I access structure fields dynamically?
(6 answers)
Closed 9 years ago.
If I have a function like this:
function [ out ] = call(a)
out = s.a
end
How can I get it to access the structure s.hello with call('hello') or someting like this?
side question: Is it also possible to access a Variable "hello" with such a function?
Thanks in advance, you guys are awesome!

I would use dynamic structure access like so:
s.(a)
Learn more at the Mathworks website!
Also, if we look at your example function, I notice you're not passing in the structure as an argument, maybe it's global, but here an example of this technique using your function as a framework:
function out = call(s,a)
out = s.(a);
end
Then to use the function, I try:
>> s = struct('hello',42)
s =
hello: 42
>> call(s,'hello')
ans =
42
Works great with no recursion limit! If you're still getting a recursive function, try adding more of your code to the question, we'll get to the bottom of this!
HTH

Related

Python, what is this for loop doing? [duplicate]

This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 3 years ago.
I have scoured the internet and I cannot find any reference to this type of for loop:
variable = [(item["attribute1"], item["attribute2]") for item in piece_of_json_data]
I am using this to update wtform's SelecField choices:
form.SelectField.choices = variable
but I can only get it to work if I replace one of the attributes in parenthesis with a static number:
variable = [(1, item["attribute2"]) for item in piece_of_json_data]
but that sets the value of the option field to "1", when I need the option values to be one of the attributes as a string.
Does this create a dict? a tuple? is there some kind of terminology for this that I can use to find documentation?
Thanks to the comments, I now understand that I am using list comprehension to create a tuple. The tuple works fine with both string and integer values. My issue has to do with .choices not accepting a string.
I found that my only problem is that I had coerce set to int on my selectfield, so naturally it wanted an integer.

How to represent an anonymous function as a string

I want to represent a function like x->x^2 as a string - simply doing string(x->x^2) doesn't work, is there any way around this?
You could create a function from string using fun=eval(parse("x->x^2")) but as far as I know it's an irreversible process.
Just because something works, doesn't mean it is a solution... Suppose f is the anonymous function. With f = x->x^2. Then the following:
join(map(strip,match(r"line \d*:\n(.*)\n",string(f.code)).captures),"\n")
Gives:
return x ^ 2
More can be extracted using regular expressions from f.code. Also note this doesn't work for non-anonymous functions. And it isn't something one should rely on to work. Perhaps some other way to implement the functionality would be best.

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.

List to String to List Python 3

I need to convert a list into a string and then do the reverse process. Note that one script will convert List->String and another script will convert String->List, so store the list in a variable is not a solution. Use split(', ') or similar is not a solution either in all cases. So, as a challange I invite you to do the conversion in the following example:
l = ['ab,.cd\'ac"', b'\x80', '\r\nHi, !', b'\x01']
str_l = str(l)
I have tried one thing that worked: using exec() built-in function but people says is not a good practice, so I invite you to give me another alternative. Also I am having problems using exec() inside a function but that's another question that you can check -> Using exec() inside a function Python 3
This should work:
str_l = ("|").join(l)
Which gives you your first string. Then do:
l_2 = str_l.split("|")
Which gives you your second list.

PHP Strict Standards on line with variable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Annoying PHP error: “Strict Standards: Only variables should be passed by reference in”
I have this line of code,
$extension=end(explode(".", $srcName));
when I fun my function I get
PHP Strict Standards: Only variables should be passed by reference in
I am not sure how to solve this
The function end() requires a variable to be passed-by-reference and passing the return-value of a function doesn't acheive this. You'll need to use two lines to accomplish this:
$exploded = explode(".", $srcName);
$extension = end($exploded);
If you're simply trying to get a file-extension, you could also use substr() and strrpos() to do it in one line:
$extension = substr($srcName, strrpos($srcName, '.'));
Or, if you know the number of .'s that appear in the string, say it's only 1, you can use list() (but this won't work if there is a dynamic number of .'s:
list(,$extension) = explode('.', $srcName);

Resources