Ejemplo n.º 1
0
        public static void ConstructExampleData()
        {
            FrameTable table = new FrameTable();

            table.AddColumn <int>("Id");
            table.AddColumn <string>("Name");
            table.AddColumn <string>("Sex");
            table.AddColumn <DateTime>("Birthdate");
            table.AddColumn <double>("Height");
            table.AddColumns <double?>("Weight");
            table.AddColumn <bool>("Result");

            Random rng = new Random(1000001);

            string[] maleNames = new string[] { "Alex", "Chris", "David", "Eric", "Frederic", "George", "Hans", "Igor", "John", "Kevin", "Luke", "Mark", "Oscar", "Peter", "Richard", "Stephan", "Thomas", "Vincent" };
            AddRows(table, maleNames, "M", 175.0, 12.0, 24.0, 3.0, 1, rng);

            string[] femaleNames = new string[] { "Anne", "Belle", "Dorothy", "Elizabeth", "Fiona", "Helen", "Julia", "Kate", "Louise", "Mary", "Natalie", "Olivia", "Ruth", "Sarah", "Theresa", "Viola" };
            AddRows(table, femaleNames, "F", 160.0, 10.0, 24.0, 3.0, 0, rng);

            // add rows with nulls
            table.AddRow(table.Rows.Count, null, "M", DateTime.Parse("1970-07-27"), 183.0, 74.0, false);
            table.AddRow(table.Rows.Count, "Zoey", "F", DateTime.Parse("2007-09-17"), 138.0, null, false);

            string path = @"example.csv";

            using (StreamWriter writer = new StreamWriter(File.OpenWrite(path))) {
                table.ToCsv(writer);
            }
            Console.WriteLine(File.Exists(path));

            string json = JsonConvert.SerializeObject(table.ToDictionaries(), Formatting.Indented);

            File.WriteAllText("example.json", json);
        }
Ejemplo n.º 2
0
        public void FrameTableCsvRoundtrip2()
        {
            // Let's exercise all our data adaptors
            FrameTable original = new FrameTable();

            original.AddColumn <string>("String");
            original.AddColumn <double?>("Double?");
            original.AddColumn <int>("Int");
            original.AddColumn <DateTime?>("DateTime?");
            original.AddColumn <TimeSpan>("TimeSpan");
            original.AddColumn <Boolean?>("Boolean?");

            original.AddRow("z", null, 1, DateTime.Today, TimeSpan.FromMinutes(5.0), true);
            original.AddRow("y", 4.3, 2, null, TimeSpan.FromHours(4.0), null);
            original.AddRow("x", 2.0, 3, DateTime.UtcNow.Date, TimeSpan.FromDays(3.0), false);

            TextWriter storage = new StringWriter();

            original.ToCsv(storage);

            FrameTable copy = FrameTable.FromCsv(new StringReader(storage.ToString()));

            for (int i = 0; i < original.Columns.Count; i++)
            {
                Assert.IsTrue(original.Columns[i].Name == copy.Columns[i].Name);
                Assert.IsTrue(original.Columns[i].StorageType == copy.Columns[i].StorageType);
            }

            for (int i = 0; i < original.Rows.Count; i++)
            {
                for (int j = 0; j < original.Columns.Count; j++)
                {
                    // This awkwardness is necessary because == resolves to a static method,
                    // so object == object does a reference check which will fail even if
                    // both sides are equal structures. Equals, on the other hand, is a
                    // virtual method, so it will do the appropriate comparison, but will
                    // fail if the instance is null.
                    if (original.Rows[i][j] == null)
                    {
                        Assert.IsTrue(original.Rows[i][j] == null);
                    }
                    else
                    {
                        Assert.IsTrue(original.Rows[i][j].Equals(copy.Rows[i][j]));
                    }
                }
            }
        }