2

I am having a bit of trouble converting this little class from Python to C#, could anyone help me please?

So far I have this:

public class objDict
{
    public objDict(Dictionary<object, object> obj)
    {
        foreach(KeyValuePair<object, object> kvp in obj)
        {

        }
    }
}

For the rest I have no idea what to do.. I know just a little bit about Python
Here's the class:

class objDict(object):

    def __init__(self, obj):
        for k, v in obj.iteritems():
            if isinstance(v, dict):
                setattr(self, _to_str(k).title(), objDict(v))
            else:
                setattr(self, k, v)

    def __getitem__(self, val):
        return self.__dict__[val]

    def __repr__(self):
        return '{%s}' % str(', '.join('%s : %s' % (k, repr(v)) for (k, v) in self.__dict__.iteritems()))

Thanks in advance!

6

2 Answers 2

2

It should be something like:

class objDict : Dictionary<object, object>
{
    // __init__
    public objDict(IDictionary obj = null)
    {
        if (obj == null)
        {
            return;
        }

        foreach (DictionaryEntry kv in obj)
        {
            if (kv.Value is IDictionary)
            {
                // Not sure if it should be CultureInfo.InvariantCulture or CultureInfo.CurrentCulture
                this.Add(CultureInfo.InvariantCulture.TextInfo.ToTitleCase(kv.Key.ToString()), new objDict((IDictionary)kv.Value));
            }
            else
            {
                this.Add(kv.Key, kv.Value);
            }
        }
    }

    // __getitem__
    public object this[object key]
    {
        get
        {
            return this[key];
        }
    }

    // __repr__
    public string ToString()
    {
        return string.Join(", ", this.Select(p => string.Format("{0} : {0}", p.Key, p.Value)));
    }
}

I'll say that I don't see anything "better" than a classical Dictionary<object, object> if we exclude that inner dictionaries will be shallow-cloned and that there is a useful .ToString().

Sign up to request clarification or add additional context in comments.

Comments

0

There is a free online tool to automatically convert Python to C#. The results are not perfect but take away a lot of the pain of porting control of flow etc.

https://pythoncsharp.com

namespace Namespace {
    
    using System;
    
    public static class Module {
        
        public class objDict
            : object {
            
            public objDict(object obj) {
                foreach (var _tup_1 in obj) {
                    var k = _tup_1.Item1;
                    var v = _tup_1.Item2;
                    if (v is dict) {
                        setattr(this, _to_str(k).title(), objDict(v));
                    } else {
                        setattr(this, k, v);
                    }
                }
            }
            
            public virtual object @__getitem__(object val) {
                return this.@__dict__[val];
            }
            
            public virtual object @__repr__() {
                return String.Format("{%s}", ", ".join(from _tup_1 in this.@__dict__.Chop((k,v) => (k, v))
                    let k = _tup_1.Item1
                    let v = _tup_1.Item2
                    select String.Format("%s : %s", k, repr(v))).ToString());
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.