How do you get directory listing sorted by date in Elixir? - date

How do you get directory listing sorted by date in Elixir?
File.ls/1 gives list sorted by filename only.
No other functions in File module seem relevant for this.

Maybe there's a built-in function I don't know about, but you can make your own by using File.stat!/2:
File.ls!("path/to/dir")
|> Enum.map(&{&1, File.stat!("path/to/dir" <> &1).ctime})
|> Enum.sort(fn {_, time1}, {_, time2} -> time1 <= time2 end)
Example output:
[
{"test", {{2019, 3, 9}, {23, 55, 48}}},
{"config", {{2019, 3, 9}, {23, 55, 48}}},
{"README.md", {{2019, 3, 9}, {23, 55, 48}}},
{"_build", {{2019, 3, 9}, {23, 59, 48}}},
{"test.xml", {{2019, 3, 23}, {22, 1, 28}}},
{"foo.ex", {{2019, 4, 20}, {4, 26, 5}}},
{"foo", {{2019, 4, 21}, {3, 59, 29}}},
{"mix.exs", {{2019, 7, 27}, {8, 45, 0}}},
{"mix.lock", {{2019, 7, 27}, {8, 45, 7}}},
{"deps", {{2019, 7, 27}, {8, 45, 7}}},
{"lib", {{2019, 7, 27}, {9, 5, 36}}}
]

Edit:
As pointed out in a comment, this assumes you're in the directory you want to see the output for. If this is not the case, you can specify it by adding the :cd option, like so:
System.cmd("ls", ["-lt"], cd: "path/to/dir")
You can also make use of System.cmd/3 to achieve this.
Particularly you want to use the "ls" command with the flag "-t" which will sort by modification date and maybe "-l" which will provide extra information.
Therefore you can use it like this:
# To simply get the filenames sorted by modification date
System.cmd("ls", ["-t"])
# Or with extra info
System.cmd("ls", ["-lt"])
This will return a tuple containing a String with the results and a number with the exit status.
So, if you just call it like that, it will produce something like:
iex> System.cmd("ls", ["-t"])
{"test_file2.txt\ntest_file1.txt\n", 0}
Having this, you can do lots of things, even pattern match over the exit code to process the output accordingly:
case System.cmd("ls", ["-t"]) do
{contents, 0} ->
# You can for instance retrieve a list with the filenames
String.split(contents, "\n")
{_contents, exit_code} ->
# Or provide an error message
{:error, "Directory contents could not be read. Exit code: #{exit_code}"
end
If you don't want to handle the exit code and just care about the contents you can simply run:
System.cmd("ls", ["-t"]) |> elem(0) |> String.split("\n")
Notice that this will however include an empty string at the end, because the output string ends with a newline "\n".

Related

How can I extrapolate the reversed hex string with Flutter?

through flutter I am reading a series of beacons with nfc_manager, the string I read is the one reported in the log below, how can I extrapolate the reversed hex string to be able to print it from the string represents in the log?
Flutter Code:
NfcManager.instance.startSession(onDiscovered: (NfcTag tag) async {
log(tag.data.toString());
});
Log print:
{nfca: {identifier: [4, 7, 255, 82, 190, 78, 129], atqa: [68, 0],
maxTransceiveLength: 253, sak: 0, timeout: 618},
mifareultralight: {identifier: [4, 7, 255, 82, 190, 78, 129],
maxTransceiveLength: 253, timeout: 618, type: 1}, ndefformatable: {identifier: [4, 7, 255, 82, 190, 78, 129]}}
Decode the json string like
var convert = json.decode(tag.data.toString());
Then you can use String.fromCharCodes like the following
List<int> charCodes = convert['nfca']['identifier'];
print(new String.fromCharCodes(charCodes));

PySNMP how to convert to dot oid

How to convert in python SNMPv2-SMI.enterprises.9.1.283 to dot oid?
SNMPv2-SMI.enterprises.9.1.283 -> 1.3.6.1.4.1.9.1.283
Thanks,
Alexey
You can get usufal data by:
print(o.getLabel())
print(o.getMibNode())
print(o.getMibSymbol())
print(o.getOid())
print(o.prettyPrint())
pysnmp implementation to resolve mib names to OID
from pysnmp.smi import builder, view, rfc1902, error
mibBuilder = builder.MibBuilder()
mibView = view.MibViewController(mibBuilder)
mibVar = rfc1902.ObjectIdentity('SNMPv2-SMI', 'enterprises', 9, 1, 283)
mibVar.resolveWithMib(mibView)
print(mibVar.prettyPrint()) # prints SNMPv2-SMI::enterprises.9.1.283
print(tuple(mibVar)) # prints (1, 3, 6, 1, 4, 1, 9, 1, 283)
print(str(mibVar)) # prints 1.3.6.1.4.1.9.1.283
Reference

Array prefix(while:) looping different number of times for same logic. (unable to comprehend)

Here is the shot of the playground where I am trying to print all the even numbers until you reach an odd one in 3 ways and I don't understand why there are different number of times it looped
From my understanding it should loop only 7 times because there are 6 even numbers in the starting and the 7th time there is an odd number which must terminate the loop but we have gotten 8 times in one case ! Why ?
Also quite literally the second and third way are same but still there is a difference in the number of times the loop was executed.
This, I don't understand.
===== EDIT =======
I was asked for text form of the code so here it is
import UIKit
// First way
var arr = [34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30].prefix { num in
return num.isMultiple(of: 2)
}
print(arr)
// Second way
var arr2 = [34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30].prefix(while: {$0.isMultiple(of: 2)})
print(arr2)
//Third way (almost second way but directly prinitng this time)
print([34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30]
.prefix(while: {$0.isMultiple(of: 2)}))
The message of "(x times)" isn't really saying how many times a line is executed in a loop.
Think of it like this, sometimes the playground want to output multiple messages on the same line, because two things are being evaluated on that line. This happen the most often in loops, where in each iteration a new thing is evaluated, and Xcode wants to display that on the same line, but can't, so can only display "(2 times)".
You can also see this happen in other places, like closures that are on the same line:
let x = Optional.some(1).map { $0 + 1 }.map { String($0) } // (3 times)
Here, the 3 things that are evaluated are:
x
$0 + 1
String($0)
Another example:
func f() { print("x");print("y") } // (2 times)
f()
Normally, each print statement will have some text displayed on the right, won't they? But since they are on the same line, Xcode can only show "(2 times)".
The same thing happens in this case:
var arr2 = [34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30].prefix(while: {$0.isMultiple(of: 2)})
There are 8 things to evaluate. The closure result is evaluated 7 times, plus once for the value of arr2.
If you put the variable declaration and the closure on separate lines, then Xcode can show the value of the variable on a separate line, which is why the number is one less in the two other cases.
To illustrate this graphically:
You are forcing Xcode to combine the two boxed messages into one single line. Xcode can't usefully fit all that text onto one single line, so the best it can do is "(8 times)".
I believe these are just artifacts of how playgrounds work. With a little re-formatting I get "7 times" in all three variations:
let data = [34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30]
let arr = data.prefix {
$0.isMultiple(of: 2)
}
print(arr)
let arr2 = data.prefix(while: {
$0.isMultiple(of: 2)
})
print(arr2)
print(data.prefix(while: {
$0.isMultiple(of: 2)
}))

How to use regex in CF template for conditions

I am using a condition in troposphere CF template, but unfortunately there are more than 10 conditions and AWS CF supports only 10 of them. The condition checks if the app name start with particular name. Is there a way to use regex in condition so I can write only one condition instead of 10, stating do something if the name start with appname*
I am adding the conditions for each role but since aws supports only 10, I cant add more than that.
conditions = {
"RoleEqualCollectors01" : Equals(
Ref(ThorRole),
"collectors01",
),
...,
...,
"RoleEqualCollectors22" : Equals(
Ref(ThorRole),
"collectors22",
),
"Collector" : Or(
Condition("RoleEqualCollectors01"),
...,
...,
Condition("RoleEqualCollectors22")
),
is there a way I can specify like this,
conditions = {
"RoleEqualCollectors" : Equals(
Ref(ThorRole),
"collectors*",
),
"Collector" : Or(
Condition("RoleEqualCollectors*"),
),
Just found out that AWS has a limit for Or condition, it needs minimum 2 and maximum 10 conditions, there is a work around, I did three separate Or conditions and then a Final_Or condition that combines all of them.
or1: Fn::Or condition for 1,2, 3, 4, 5, 6, 7, 8, 9, 10
or2: Fn::Or condition for 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
or3: Fn::Or condition for 21, 22
Final_Or: Fn::Or for or1, or2, or3

text file manipulation in MATLAB

I have a text file which contain a combination of string and numbers. for example like following:
*Preprint, echo=NO, model=NO, history=NO, contact=NO
*Element, type=S4R
1, 1, 9, 189, 84
2, 9, 10, 190, 189
3, 10, 11, 191, 190
4, 11, 12, 192, 191
*Surface, type=ELEMENT, name=Surf-1
I like to remove >> 2, 9, 10, 190, 189
I mean I want to have following in a new file:
*Preprint, echo=NO, model=NO, history=NO, contact=NO
*Element, type=S4R
1, 1, 9, 189, 84
3, 10, 11, 191, 190
4, 11, 12, 192, 191
*Surface, type=ELEMENT, name=Surf-1
I should notice that these data are changed, and the only thing that I know about them is its number, for above example line 4.
I have googled many times to write a text file from another one by skipping from special line that I just know its number without much success!
any suggestion will be appreciated.
The fgets function allows you to read an ASCII file line by line. When you encounter the line you want to skip (knowing its number as you said) just use the continue statement to skip the iteration :
clc;
close all;
clear all;
input = fopen('input.txt','r');
output = fopen('output.txt','w');
line = fgets(input);
fprintf(output,'%s',line);
cpt=0;
offset = 3;
while ischar(line)
cpt=cpt+1;
if (cpt==offset)
line = fgets(input);
continue;
end
line = fgets(input);
fprintf(output,'%s',line);
end
fclose(input);
fclose(output);
The first fgets allows you to copy the first line from your input file in memory.
The following fprintf writes it into your output file.
Next you define a counter initialized to zero and you also define the offset, that is the line you want to skip minus 1 because you have already read the first line.
Then you use a WHILE loop in which you read each line from the input file with the fgets function and directly writes it into your output file. When you reach the end of the file, fgets return -1 which is not a character and so the loop ends.
You notice that cpt is iterated and if it equals the offset then you must skip the line, so you read it but you do not write it and the continue statement allows you to jump to the next iteration of the loop skipping the remaining instructions.
You can use fgetl instead of fgets if you want to get rid of newline characters.
The input file :
*Preprint, echo=NO, model=NO, history=NO, contact=NO
*Element, type=S4R
1, 1, 9, 189, 84
2, 9, 10, 190, 189
3, 10, 11, 191, 190
4, 11, 12, 192, 191
*Surface, type=ELEMENT, name=Surf-1
The output file :
*Preprint, echo=NO, model=NO, history=NO, contact=NO
*Element, type=S4R
1, 1, 9, 189, 84
3, 10, 11, 191, 190
4, 11, 12, 192, 191
*Surface, type=ELEMENT, name=Surf-1