[IronPython] Dict convertion question

JoeSox joesox at gmail.com
Thu Jul 20 22:41:56 CEST 2006


On 7/20/06, J. Merrill <jvm_cop at spamcop.net> wrote:
> At 01:05 AM 7/20/2006, JoeSox wrote (in part)
> >File.WriteAllText(Application.StartupPath + "\\mydict.txt", myDict.ToCodeString());
> >(This results in a file size of 1,540,096 bytes which was created in about a second.)
>
> I don't know much about ToCodeString, but from the name, it should produce a string that can be treated as Python code.  If you were to write something like
>    mydict =
> into the file, name the file mydict.py, and run that file -- wouldn't you end up with your dictionary back in a Python variable?
>

I have tried this in IDLE and I couldn't get a response back or
extremely slow.  I haven't had time to try it in IP yet.

I have written this method below (rather quickly) and since it changes
the strings into ints using IP it slows down the conversion.

===
        public static Dict Convert_StringtoDict(string dictcodestring)
        {
            Dict myDict = new Dict();
            int i = 0;
            string nextKey = "";

            StringCollection tempSC = new StringCollection();
            foreach (string kv in dictcodestring.Split(':'))
            {
                string tempstr = "";
                string cur = "";
                cur = kv;
                cur = cur.Trim();

                //**if begining of Dict, remove the {
                if (cur.StartsWith("{") && i.Equals(0))
                {
                    tempstr = cur.Replace("{", "");
                    tempstr = tempstr.Trim();

                    //Add the first Key
                    tempSC.Add(tempstr);
                }

                //**if '[' found then get its value [x,x...], may have
some at end
                if (cur.StartsWith("["))
                {

                    string Value = "";

                    tempstr = cur.Replace("[", "");
                    tempstr = tempstr.Trim();
                    if (tempstr.Contains("],"))
                    {
                        int index = 0;
                        index = tempstr.IndexOf("],");
                        Value = tempstr.Substring(0, index);
                        //Turn Value into ints
                        CNDB.cn_pe.Globals["Value"] = Value;
                        CNDB.cn_pe.ExecuteToConsole("v={}");
                        CNDB.cn_pe.ExecuteToConsole("v=Value");

                        nextKey = tempstr.Substring(index,
tempstr.Length - Value.Length);
                        nextKey = nextKey.Replace("],", "").Trim();

                        //Add the next Key to SC
                        tempSC.Add(nextKey);
                    }
                    else
                        nextKey = "";

                    //Add the Key,Value...
                    if (!i.Equals(0))
                    {
                        //Add this Value to the last Key
                        myDict.Add(Convert.ToInt32(tempSC[i - 1]),
CNDB.cn_pe.Globals["Value"]);
                    }
                }

                i = i + 1;
            }


            //IEnumerator myEnumerator = inList.GetEnumerator();
            //while (myEnumerator.MoveNext())
            //    stringList += myEnumerator.Current.ToString() + "\r\n";

            return myDict;
        }



More information about the Ironpython-users mailing list