Skip to content

Instantly share code, notes, and snippets.

@dgroft
dgroft / csvtoxml.py
Last active December 18, 2015 05:38
Converts CSV to XML in Python.
import argparse
import csv
parser = argparse.ArgumentParser(description="Converts a CSV file to an XML file")
parser.add_argument("csv", help="the path to the .CSV file")
parser.add_argument("xml", help="the path to the .XML file")
parser.add_argument("--root", help="root tag name", default="root")
parser.add_argument("--row", help="row tag name", default="row")
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true")
args = parser.parse_args()
@dgroft
dgroft / CSVtoXML.cs
Last active December 3, 2020 14:46
Convert CSV to XML in C#
string csvPath = "...";
string xmlPath = "...";
using (StreamReader streamReader = new StreamReader(new FileStream(csvPath, FileMode.Open)))
{
// snag headers
string[] headers = streamReader.ReadLine().Split(',');
// col0="{0}" col1="{1}" coln="{n}"
string attributesFormat = string.Join(" ", headers.Select((colStr, colIdx) => string.Format("{0}=\"{{{1}}}\"", colStr, colIdx)));
@dgroft
dgroft / ImmutableDataObject.cs
Created November 14, 2012 18:53
Immutable object base class
using System.Linq;
public abstract class DataObject
{
public override bool Equals(object obj)
{
var dataObj = obj as DataObject;
if (dataObj == null) return false;
@dgroft
dgroft / MyImmutableObject.cs
Created November 8, 2012 20:59
A constructor for immutable types that takes a dynamic list of parameters
protected MyImmutableObject(IDictionary<string, object> parameters)
{
foreach (var field in GetType().GetFields().Where(x => parameters.Keys.Contains(x.Name)))
{
if (parameters[field.Name] is Dictionary<string, object>)
{
var ctor = field.FieldType.GetConstructor(new[] { typeof(Dictionary<string, object>) });
var instance = ctor.Invoke(new object[] { parameters[field.Name] });
field.SetValue(this, instance);
}
@dgroft
dgroft / Math.cs
Created November 6, 2012 14:11
Determine if two floats are "equal"
using System.Linq;
public static class Math
{
/// <summary>
/// Floating-point epsilon value.
/// </summary>
public const float FLT_EPSILON = 1.192092896e-07f;
/// <summary>