How to filter and get sublist from a FileList in Rake? - rake

I don't know Ruby, but Rake looks so cool, so I am trying to use it.
When I select files with FileList["*.cpp"], sometimes I want to take filtering out sublist from it.
For source input file,
somefile.txt
a.cpp
MySome.cpp
Another.cpp
MySomeValue.cpp
newlist = FileList["*.cpp"].sublistByFiltering("*Value.cpp").sublistByFiltering("My*")
And newlist shows only MySomeValue.cpp.
How can I do this?
Update
The method sublistByFiltering is my imaginary one, and I am looking for the feature currently in Rake.

I'm not sure where you came up with this sublistByFiltering method, but I think what you're looking for is to do an intersection between the internal arrays of multiple FileList items.
a = FileList['*.cpp'].to_a
b = FileList['*Value.cpp'].to_a
a & b #=> files that are in both `a` and `b`
However, if you know all of the patterns up front, and you're only ever combining them, you can pass a regular expression to the FileList constructor:
FileList[/My.*Value\.cpp/]

Related

Check for list of substrings inside string column in PySpark

For checking if a single string is contained in rows of one column. (for example, "abc" is contained in "abcdef"), the following code is useful:
df_filtered = df.filter(df.columnName.contains('abc'))
The result would be for example "_wordabc","thisabce","2abc1".
How can I check for multiple strings (for example ['ab1','cd2','ef3']) at the same time?
I'm ideally searching for something like this:
df_filtered = df.filter(df.columnName.contains(['word1','word2','word3']))
The result would be for example "x_ab1","_cd2_","abef3".
Please, post scalable solutions (no for loops, for example) because the aim is to check a big list around 1000 elements.
All you need is isin
df_filtered = df.filter(df['columnName'].isin('word1','word2','word3')
Edit
You need rlike function to achieve your result
words="(aaa|bbb|ccc)"
df.filter(df['columnName'].rlike(words))

Defaultdict() the correct choice?

EDIT: mistake fixed
The idea is to read text from a file, clean it, and pair consecutive words (not permuations):
file = f.read()
words = [word.strip(string.punctuation).lower() for word in file.split()]
pairs = [(words[i]+" " + words[i+1]).split() for i in range(len(words)-1)]
Then, for each pair, create a list of all the possible individual words that can follow that pair throughout the text. The dict will look like
[ConsecWordPair]:[listOfFollowers]
Thus, referencing the dictionary for a given pair will return all of the words that can follow that pair. E.g.
wordsThatFollow[('she', 'was')]
>> ['alone', 'happy', 'not']
My algorithm to achieve this involves a defaultdict(list)...
wordsThatFollow = defaultdict(list)
for i in range(len(words)-1):
try:
# pairs overlap, want second word of next pair
# wordsThatFollow[tuple(pairs[i])] = pairs[i+1][1]
EDIT: wordsThatFollow[tuple(pairs[i])].update(pairs[i+1][1][0]
except Exception:
pass
I'm not so worried about the value error I have to circumvent with the 'try-except' (unless I should be). The problem is that the algorithm only successfully returns one of the followers:
wordsThatFollow[('she', 'was')]
>> ['not']
Sorry if this post is bad for the community I'm figuring things out as I go ^^
Your problem is that you are always overwriting the value, when you really want to extend it:
# Instead of this
wordsThatFollow[tuple(pairs[i])] = pairs[i+1][1]
# Do this
wordsThatFollow[tuple(pairs[i])].append(pairs[i+1][1])

Macro name expanded from another macro in makefile

I have a makefile with the following format. First I define what my outputs are;
EXEFILES = myexe1.exe myexe2.exe
Then I define what the dependencies are for those outputs;
myexe1.exe : myobj1.obj
myexe2.exe : myobj2.obj
Then I have some macros that define extra dependencies for linking;
DEP_myexe1 = lib1.lib lib2.lib
DEP_myexe2 = lib3.lib lib4.lib
Then I have the target for transforming .obj to .exe;
$(EXEFILES):
$(LINK) -OUT:"Exe\$#" -ADDOBJ:"Obj\$<" -IMPLIB:$($($(DEP_$*)):%=Lib\\%)
What I want to happen is (example for myexe1.exe)
DEP_$* -> DEP_myexe1
$(DEP_myexe1) -> lib1.lib lib2.lib
$(lib1.lib lib2.lib:%=Lib\\%) -> Lib\lib1.lib Lib\lib2.lib
Unfortunately this is not working. When I run make --just-print, the -IMPLIB: arguments are empty. However, if I run $(warning DEP_$*) I get
DEP_myexe1
And when I run $(warning $(DEP_myexe1)) I get
lib1.lib lib2.lib
So for some reason, make does not like the combination of $(DEP_$*). Perhaps it cannot resolve macro names dynamically like this. What can I do to get this to work? Is there an alternative?
Where does $(warning DEP_$*) give you DEP_myexe1 as output exactly? Because given your makefile above it shouldn't.
$* is the stem of the target pattern that matched. In your case, because you have explicit target names, you have no patten match and so no stem and so $* is always empty.
Additionally, you are attempting a few too many expansions. You are expanding $* to get myexe1 directly (assuming for the moment that variable works the way you intended). You then prefix that with DEP_ and used $(DEP_$*) to get the lib1.lib lib2.lib. You then expand that result $($(DEP_$*)) and then expand that (empty) result again (to do your substitution) $($($(DEP_$*)):%=Lib\\%).
You want to either use $(#:.exe=) instead of $* in your rule body or use %.exe as your target and then use $* to get myexe1/myexe2.
You then want to drop two levels of expansion from $($($(DEP_$*)):%=Lib\\%) and use $(DEP_$*:%=Lib\\%) instead.
So (assuming you use the pattern rule) you end up with:
%.exe:
$(LINK) -OUT:"Exe\$#" -ADDOBJ:"Obj\$<" -IMPLIB:$(DEP_$*:%=Lib\\%)
I managed to get it working without needing to resolve macros in the way described above. I modified the linking dependencies like this;
myexe1.exe : myobj1.obj lib1.lib lib2.lib
myexe2.exe : myobj2.obj lib3.lib lib4.lib
Then I need to filter these files by extension in the target recipe;
$(EXEFILES):
$(LINK) -OUT:"$(EXE_PATH)\$#" -ADDOBJ:$(patsubst %, Obj\\%, $(filter %.obj, $^)) -IMPLIB:$(patsubst %, Lib\\%, $(filter %.lib, $^))
The $(pathsubst ...) is used to prepend the path that the relevant files are in.
In the case of myexe1.exe, the link command expands to;
slink -OUT:"Exe\myexe1.exe" -ADDOBJ: Obj\myexe1.obj -IMPLIB: Lib\lib1.lib Lib\lib2.lib
Out of interest's sake, I would still like to know if it is possible to resolve macro names like in the question.

Exclude non-matching atoms from list

I would like to collect a list of atoms and pass a list to an object or abstraction that will pass through the matching atoms without modifying the order of the list, or removing duplicates.
(hello how are you)
|
[desiredobject how are you]
|
[print]
Ideally this would print (how are you). If I were to put in (how how how) I would get back the same message. But if I were to put in (jfj jfj jfj) I would get nothing.
[zl] is useful but I am looking for the inverse behaviour of [zl filter].
EDIT:
I came up with the following solution that works equally well to the solution #mattijs posted. My solution uses [uzi] to give indices of the symbols in a list. The indices output of [zl filter] is fed to [zl unique] in order to strip out undesired indices. This new list is fed to [zl lookup] in order to convert back to the symbols. The (fake) message is inserted in case [zl filter] filters everything and then [zl unique] would have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fUkOOQSswYn+YQS3r.M+Av6c
Rwkmz5yCGcgSyYY37fjW8FYXbzeLbTtrypS2+bwU8ZQAQmy9LXybgsOC24qF
KUGV97a7MX8rAKYrmlqtN8UNMCiOKbFLWfyl3.vdAfJvfeyplcmQSu2S8I+8
BNK39NVW5TH+pC7w.Q3RJb002OpWxpJRZfCczf.1RYJtjSnGC.5cmDXfm0Kt
mjlZGvQUL5Jqi3mJ1pSxD4RE4dDk9k45sRMSj45CaLcb1cVHy9STjmOR7jA4
fIZXbfjlyDaUPS17bCp2njm2Z02VPQ1MdvW8EX7B7qeO4S1hy7ABV+sSq0CG
5KyETvDk4vuehr+7QjWOUi4Me+j4P3GyGTnbWwwJ+MlO.g5sMGKOTWmXIdCv
fZ0AbJXf6rQu0inLhdalHyvoJyyGNcn+krF87PlibuDbdFsgyorI4nqEGD7q
klojgSI5Yb58hgPbJo6QbdAYaEWcrEyrI4il2DmOuLtfjInWgXjAenAcWFaK
JSm+LGaaO4rvxSfn7pR0OkzQx9SJb57xcOkb+gabjulvGMMj7TAWPsbz5n1e
.AqubW7HogzUDUd7gGA5eeUnBLxSFSPIpD3OJpr91fJ01J5eeY1J5HkprXFG
UbutnxPIzmL6l5ENbuf4r24RSOOn9Hiikld2H5wdbVFCLCbcnYGJ.xMbTygo
AMxlZGwLER0dEUMd0YkTnUHxF5PSV6lu6JncW8rilaxxy5oJOt2D4o0P0u7D
eyDG37Bcftelxix3tUh2VKJsR31VIaa2DscnjrULN+c4+C3uv.MC
-----------end_max5_patcher-----------
So basically you want to compare each item in list 1 to each item in list 2 and only pass through an item in list 1 if it is also in list 2?
It's possible this can be simplified btw, just a quick solution:
----------begin_max5_patcher----------
1198.3oc4Z1saihCEG+ZxSAhq6NBayWYuXk1mipQUFvIwcH1HvosyLpu6KXC
IlDRpIkkwilKBowewe94y4Xr64mqb7R4uQp8b+a2Gccb94JGGYQsE3z8aGu8
32xJv0xl4wHuxSe16AUUBxaBYwktYU755JxFREgkQ5avFNSTS+Aos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ry2siE85IlZmflyc
o0UXeFF4Uiegj+TysoY+tOgEhJZ5AgZin5o+z1BdJtnK2nNdvNGyHmQyjm+u
R+mOJ8ht1YOonf7pZ9yjjea3oedqS9bRI9ldtSocVCPUlb.Rfik3Ulmtbf.U
9xgF+7QMLc4fI2Hc4lQV97lmc69LmrLAoR0nOIKgAp+O7xQSw06gkwKCKasK
69LirDr1eVXIHL3DKStaV5Our7ZI3pLw9l.DAihC+OJqVA.z34Ki9y7B4Jti
TTvc+jA2lXV81QA+v60bvLzH65viYUQpyW3tiWW9BR5KCes0pG1nKVmd70nu
XxyXI4agZJ1B0ThoZBrXZBYh8Txm.SM+38U+GN+kvWC
-----------end_max5_patcher-----------
Hope that helps,
Mattijs

SWI-Prolog cgi_get_form(Arguments) saving and handling arguments web form

I'm looking for a way of saving and after handling the arguments of a web form in SWI-Prolog when I submit the form and I call the same program to generate another form and so on. Always calling the same prolog program from one form to the next one.
The CGI SWI-Prolog library saves these arguments as a list of Name(Value) terms, i.e [Name(Value)].
if I pass the arguments like a hidden argument inside the form (TotalArguments is a list):
format('"<"input type="hidden" id="nameofform1" name="nameofform1" value="~w" />~n', TotalArguments),
I need to get rid of the id or name that concatenates on my resultant list on TotalArguments when I append it. Any idea of how to do this so that the final list looks like [nameofform1(value1), nameofform2(value2),...]?
I could also write this list of arguments and append it into a file, and consult it every time the program is called again, but this will load them always and I only need to load the arguments needed in the specific step and form handled at the moment. Because otherwise this file could contain undesirable info after some executions. Any thoughts on how to do it this way?
Any other suggestions for this kind of problem?
Edit with my solution using hidden form
I've solved it by creating:
extract_value([],_).
extract_value([A0|__ ], Valor) :-
A0 =.. [_, Value],
Valor is Value.
and then doing:
extract_value(Arguments, Value),
and submiting the hidden value of the form like:
format('<"input type="hidden" id="nameofform1" name="nameofform1" value="~w"/>~n', [Value]),
and appending it in the next form so that it looks how I wanted:
[nameofform2(value2),nameofform1(value1)]
It's a bit unclear to me what exactly you need here, but to remove the first element of a list that unifies with a given element (especially if you know for certain that the list contains such an element), use selectkchk/3. For example:
selectchk(id(_), List0, List1),
selectchk(name(_), List1, List)
in order to obtain List, which is List0 without the elements id(_) and name(_). Kind of implicit in your question, as I understand it, seems to be how to create a term like "form1(Value)" given the terms name(form1) and Value. You can do this for example with =../2. You can create a term T with functor N and arguments Args with
T =.. [N|Args]
It does not seem necessary to write anything to files here, I would simply pass the info through forms just as you outline.