Given an array
$values = ['a', 'b', 'c']
How do I get the index of 'c'?
In puppet stdlib, it comes with a function name values_at. This does exactly the opposite of what you are looking for. You can reverse this process and create a function of your own quite easily by looking at e.g. /etc/puppet/modules/stdlib/lib/puppet/parser/functions/values_at.rb.
Related
In what I would have thought was relatively simple code, I can't figure out what the issue is with adding another string to my list of concatenations
Below is the code that I currently have and I get expected output
#concat('start ', if(equals(coalesce(pipeline().parameters.p_source_object.TYPE,''),'x'), 'a', 'b'))
However, I want to add more strings to the concatenation but when I add a comma between the two end brackets like so
#concat('start ', if(equals(coalesce(pipeline().parameters.p_source_object.TYPE,''),'x'), 'a', 'b'), )
I get an "Invalid" error with "Missing period" message. If I put a period before the comma, the error goes away (but obviously invalid syntax)
What is it expecting here?
On a related note, is there a better way to concatenate while also doing some functions that output strings? It's the most unintuitive interface imaginable (Microsoft do seem to pride themselves in the ridiculous!)
Hoping someone can find my sanity for me!
In the end, I completely reworked it avoiding the layers... however, I have discovered a resolution
#{concat('start ', if(equals(coalesce(pipeline().parameters.p_source_object.TYPE,''),'x'), 'a', 'b'), 'dd')}
While it doesn't stand out as to how... there's a space at the start of the line, this stops it being considered "Dynamic content" but instead uses string interpolation
i am trying to set up a basic tool which is supposed to do following:
1. Search for Musicans in Spotify
2. Print results in a way i can use them for stuff (e.g. show output in a window or something like that)
Unfortunatly i cant wrap my head around following Problem:
import spotipy
import sys
from spotipy.oauth2 import SpotifyClientCredentials
client_credentials_manager = SpotifyClientCredentials(client_id='',
client_secret='')
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
result = spotify.search(q='Korn', limit=5)
for i, t in enumerate(result['tracks']['items']): # way i dont understand
print (' ', i, t['name'])
print(result) #way i would like to do it
The code can print a list in this enumerate fashion but isnt capable of printing it normally. The thing is I dont understand the way enumerate does this.
Can someone please give me some insights how the search function and enumerate work and maybe show me an elegant solution so i can print this list?
I don't know about the spotipy module so I'll have to assume a few things, based on what you said works or doesn't work.
So... your variable named 'result' seems to behave like a dictionary. You can index it with keys ('tracks', 'items'), (as shown there: result['tracks']['items']), and this seems to give you back stuff that behaves like a list containing stuff that behaves like a dictionary, that among other things contains the key 'name' (as shown there: t['name']).
To illustrate how enumerate works, here's an example:
>>> hello = ['h', 'e', 'l', 'l', 'o']
>>> print(list(enumerate(hello)))
[(0, 'h'), (1, 'e'), (2, 'l'), (3, 'l'), (4, 'o')]
It essentially gives you back tuples of each index in the list, and what is at that index.
Also note that Python has a principle called 'duck-typing'. It says that “If it looks like a duck and quacks like a duck, it must be a duck.”. That's why I don't worry to much about what type an object is, what I care about is what it behaves like.
Now, when you say something like:
for index, element in enumerate(elements):
You're using what's called unpacking. It lets you separate the tuples that enumerate gives you into two variables.
Then if we wanted to simply do print(result), the class that let you instantiate result would need to implement either the __repr__ or __str__ methods. In it, you would have all the logic that's contained in your for loop, grabbing the data at the appropriate keys in the dictionary-like object and returning a correctly formatted string. But it looks like this class comes from an external library, so I'm not sure it would be a good idea to implement these yourself (I don't have experience doing that yet).
Hope that my explanations were at least a little bit helpful.
Newbie in nim here.
While experimenting in nim, I didn't find a proc to be able to find the first occurrence of an item in a sequence.
What I practically want to do is, given a seq, to remove the first instance of a known string, and return the same sequence, with the item removed. So I was thinking, to break it in a two-step work: first find the index and then remove the item at the specific index.
Of course I can write my own helper function for this, but I am surprised that I couldn't find in the system or sequtils modules any similar solution.
That would just be:
var s = #["a", "b", "c"]
s.del(s.find("b"))
The del function modifies the sequence in place. You would start with a copy of the sequence if you want a new value.
I'm working with firebase on pyhon3. I wanted to know if there's a way I can check if a substring is present in the firebase keys. For e.g. if I have a string 'he', and I have keys in my firebase for 'hell' and 'hello'. I can perform a get for 'he', but how can I get the others as well. And i dont want to get the entire parent and search on python as it is a fairly large database.
How can I go about to get them? I know this is not how firebase is designed. But remodeling will take a lot of time. So can it be done, and if so how?
Try using in like this:
>>> x = 'hello'
>>> y = 'll'
>>> y in x
True
So if you have a value you can assign, and you know what you want to check against, this is the easiest way to go about this in Python.
Is there an easy way to create a set of strings in Matlab?
I am going through a list of filepaths and want to get all names of folders at a specific level.
But since in some folders there are several files, I get these folders several times.
I know there would be the possibility to create a cell array and check every time if the current folder name is already in the array, and if not, add it.
Another option would be to use the java HashSet class.
But is there any easy inbuilt Matlab way to do something like that?
I can't use a Vector since it would create a vector of chars not strings.
Unfortunately there's nothing as efficient as Java Set implementations.
But you can use set operations. Either union when you add, or just call unique on your collection with duplicates.
You could use the rdir script... MATLAB file exchange to the rescue!
Use it like this:
listing = rdir(name);
The function returns a structure listing similar to the built-in dir command.
It should save you the headache of iterating through a directory tree yourself.
How about "unique":
x = {'dog', 'cat', 'cat', 'fish', 'horse', 'bird', 'rat', 'rat'};
x_set=unique(x)
x_set =
'bird' 'cat' 'dog' 'fish' 'horse' 'rat'