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".
title: Lihaoyi PPrint Deep Tree
link: Lihaoyi PPrint Deep Tree
I have a "deep" tree of case classes that I want to pprint as "like Scala" source code.
... because I want to view, copy and paste them to make a UnitTest
I am using lihaoyi's pprint to perform the stringify of the values, BUT after a few levels, pprint stops printing.
I've checked in the tokenize thing and the instance is being emitted as-is; you can see that the strings aren't being excaped.
I can (manually) "tweak" my tests to test parts of the results and get my pretty source, but ... it feels wrong.
Original "Lack of deep" printout
Module(
Set(),
Set(
Material(
"testTextLayoutContainer",
List(
Attribute("Position", VecSingle(2)),
Attribute("UV", VecSingle(2)),
Attribute("Color", VecFixed(4))
),
List(
UniformInstance("ProjMtx", MatSingle(4, 4)),
UniformInstance("Texture", Texture(2, VecFixed(4)))
),
Set(
//
// this is the line that's not being pprinted
//
Program(ImGui,testTextLayoutContainer,Set(Attribute(Color,VecFixed(4)), Attribute(Position,VecSingle(2)), Attribute(UV,VecSingle(2))),Set(UniformInstance(Texture,Texture(2,VecFixed(4))), UniformInstance(ProjMtx,MatSingle(4,4))),Set(VaryingCopy(Attribute(Color,VecFixed(4))), VaryingCopy(Attribute(UV,VecSingle(2)))),List(Lookup(UniformInstance(ProjMtx,MatSingle(4,4))), Lookup(Attribute(Position,VecSingle(2))), Literal(0.0), Literal(1.0), Construct(VecSingle(4),List(Lookup(Attribute(Position,VecSingle(2))), Literal(0.0), Literal(1.0))), External(mat*vec,List(Lookup(UniformInstance(ProjMtx,MatSingle(4,4))), Construct(VecSingle(4),List(Lookup(Attribute(Position,VecSingle(2))), Literal(0.0), Literal(1.0)))),VecSingle(4)), Output(Vertex(ProductTransform(Lookup(UniformInstance(ProjMtx,MatSingle(4,4))),Construct(VecSingle(4),List(Lookup(Attribute(Position,VecSingle(2))), Literal(0.0), Literal(1.0))))),External(mat*vec,List(Lookup(UniformInstance(ProjMtx,MatSingle(4,4))), Construct(VecSingle(4),List(Lookup(Attribute(Position,VecSingle(2))), Literal(0.0), Literal(1.0)))),VecSingle(4))), Lookup(Attribute(Color,VecFixed(4))), Output(VaryingCopy(Attribute(Color,VecFixed(4))),Lookup(Attribute(Color,VecFixed(4)))), Lookup(Attribute(UV,VecSingle(2))), Output(VaryingCopy(Attribute(UV,VecSingle(2))),Lookup(Attribute(UV,VecSingle(2))))),List(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))), Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),x), Lookup(UniformInstance(Texture,Texture(2,VecFixed(4)))), Lookup(VaryingCopy(Attribute(UV,VecSingle(2)))), Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),x), External(*,List(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),x), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),x)),VecFixed(1)), Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),y), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),y), External(*,List(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),y), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),y)),VecFixed(1)), Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),z), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),z), External(*,List(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),z), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),z)),VecFixed(1)), Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),w), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),w), External(*,List(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),w), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),w)),VecFixed(1)), Construct(VecFixed(4),List(External(*,List(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),x), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),x)),VecFixed(1)), External(*,List(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),y), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),y)),VecFixed(1)), External(*,List(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),z), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),z)),VecFixed(1)), External(*,List(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),w), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),w)),VecFixed(1)))), Output(Fragment(Construct(VecFixed(4),List(ProductAtomic(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),x),Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),x)), ProductAtomic(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),y),Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),y)), ProductAtomic(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),z),Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),z)), ProductAtomic(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),w),Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),w))))),Construct(VecFixed(4),List(External(*,List(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),x), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),x)),VecFixed(1)), External(*,List(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),y), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),y)),VecFixed(1)), External(*,List(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),z), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),z)),VecFixed(1)), External(*,List(Access(Lookup(VaryingCopy(Attribute(Color,VecFixed(4)))),w), Access(Sample(UniformInstance(Texture,Texture(2,VecFixed(4))),Lookup(VaryingCopy(Attribute(UV,VecSingle(2))))),w)),VecFixed(1)))))))
)
)
)
)
You can provide a height and a width, so if you provide a big enough value it will show it all.
scala> pprint.pprintln(Seq(1, 2, 3), width = Int.MaxValue, height = Int.MaxValue)
so
# pprint.pprintln(1 to 10, 3, 10)
Range.Inclusive(
1,
2...
can be
# pprint.pprintln(1 to 10, Int.MaxValue, Int.MaxValue)
Range.Inclusive(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Edit:
as mentioned in the docs this is a feature to stop out of memory errors when printing really big data structures.
http://www.lihaoyi.com/upickle-pprint/pprint/#GettingStarted
I really wish I could be more specific here but I have read through related questions and none of them seem to relate to the issue that I am experiencing here and I have no understanding of the issue i am experiencing. This is for a homework assignment so I am hesitant to put up all my code for the program, here is a stripped down version. Compile this and you will see the issue.
import copy
class Ordering:
def __init__(self, tuples):
self.pairs = copy.deepcopy(tuples)
self.sorted = []
self.unsorted = []
for x in self.pairs:
self.addUnsorted(left(x))
self.addUnsorted(right(x))
def addUnsorted(self, item):
isPresent = False
for x in self.unsorted:
if x == item:
isPresent = True
if isPresent == False:
self.unsorted.append(left(item))
Here I have created a class, Ordering, that takes a list of the form [('A', 'B'), ('C', 'B'), ('D', 'A')] (where a must come before b, c must come before b, etc.) and is supposed to return it in partial ordered form. I am working on debugging my code to see if it works correctly but I have not been able to yet because of the error message I get back.
When I input the follwing in my terminal:
print Ordering[('A', 'B'), ('C', 'B'), ('D', 'A')]
I get back the following error message:
Traceback (most recent call last): File "<stdin>", line 1, in (module) Type Error: 'classobj' object has no attribute '__getitem__'
Why is this?!
To access an element of a list, use square brackets. To instantiate a class, use parens.
In other words, do not use:
print Ordering[('A', 'B'), ('C', 'B'), ('D', 'A')]
Use:
print Ordering((('A', 'B'), ('C', 'B'), ('D', 'A')))
This will generate another error from deeper in the code but, since this is a homework assignment, I will let you think about that one a bit.
How to use __getitem__:
As a minimal example, here is a class that returns squares via __getitem__:
class HasItems(object):
def __getitem__(self, key):
return key**2
In operation, it looks like this:
>>> a = HasItems()
>>> a[4]
16
Note the square brackets.
Answer to "Why is this?"
Your demo-code is not complete ( ref. comment above ), however the issue with .__getitem__ method is clearly related with a statement to print an object ( which due to other reasons did fail to respond to a request to answer to a called .__getitem__ method ) rather than the Class itself.
>>> aList = [ ('A','B'), ('C','D'), ('E','F')] # the stated format of input
>>> aList # validated to be a list
[('A', 'B'), ('C', 'D'), ('E', 'F')]
>>> type( aList ) # cross-validated
<type 'list'>
>>> for x in aList: # iterator over members
... print x, type( x ) # show value and type
... left( x ) # request as in demo-code
...
('A', 'B') <type 'tuple'>
Traceback (most recent call last): <<< demo-code does not have it
File "<stdin>", line 3, in <module>
NameError: name 'left' is not defined
>>> dir( Ordering ) # .__getitem__ method missing
[ '__doc__', '__init__', '__module__', 'addUnsorted']
>>> dir( aList[0] ) # .__getitem__ method present
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__',
'__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'count', 'index']