I'm trying to switch over to using python exclusively. Something that I have used pretty extensively in C# is LINQ. In this exercise the goal is to get a collection of key value pairs, the keys being each month and the value a count of the number of messages in that month, how can I do something like this with python or perhaps what would be a better way to do this?
class MainClass
{
public static void Main (string[] args)
{
string[] months = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
var log = LineReader ();
Dictionary<string, int> cumulativeMonths = new Dictionary<string, int> ();
months.ToList ()
.ForEach (f => {
cumulativeMonths.Add(f, log.GroupBy(g => g.Split(' ').First().ToLower())
.Where(w => w.Key == f).ToList().Count());
});
}
public static IEnumerable<string> LineReader()
{
Console.WriteLine ("Hello World!");
using (StreamReader sr = new StreamReader (File.OpenRead ("/var/log/messages"))) {
while (!sr.EndOfStream) {
yield return sr.ReadLine ();
}
}
}
}
Test Input:
Feb 18 02:51:36 laptop rsyslogd: [origin software="rsyslogd" swVersion="8.4.2" x-pid="2952" x-info="http://www.rsyslog.com"] start
Feb 18 02:51:36 laptop kernel: Adaptec aacraid driver 1.2-0[30300]-ms
Feb 18 02:51:36 laptop kernel: megaraid cmm: 2.20.2.7 (Release Date: Sun Jul 16 00:01:03 EST 2006)
Feb 18 02:51:36 laptop kernel: megaraid: 2.20.5.1 (Release Date: Thu Nov 16 15:32:35 EST 2006)
Feb 18 02:51:36 laptop kernel: megasas: 06.805.06.00-rc1 Thu. Sep. 4 17:00:00 PDT 2014
Feb 18 02:51:36 laptop kernel: qla2xxx [0000:00:00.0]-0005: : QLogic Fibre Channel HBA Driver: 8.07.00.16-k.
Feb 18 02:51:36 laptop kernel: Emulex LightPulse Fibre Channel SCSI driver 10.4.8000.0.
Feb 18 02:51:36 laptop kernel: Copyright(c) 2004-2014 Emulex. All rights reserved.
Feb 18 02:51:36 laptop kernel: aic94xx: Adaptec aic94xx SAS/SATA driver version 1.0.3 loaded
Feb 18 02:51:36 laptop kernel: ACPI: bus type USB registered
Test Output would be a dictionary: {Jan: 64562, Feb: 38762} ....