PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/IronPython/IronPython/Runtime/Operations/DictionaryOfTOps.cs

http://github.com/IronLanguages/main
C# | 62 lines | 42 code | 6 blank | 14 comment | 4 complexity | 8f066a8aed3310cbd84cf4a763f1b679 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.Text;
  18. using Microsoft.Scripting.Runtime;
  19. using IronPython.Runtime.Types;
  20. namespace IronPython.Runtime.Operations {
  21. public static class DictionaryOfTOps<K, V> {
  22. public static string __repr__(CodeContext/*!*/ context, Dictionary<K, V> self) {
  23. List<object> infinite = PythonOps.GetAndCheckInfinite(self);
  24. if (infinite == null) {
  25. return "{...}";
  26. }
  27. int index = infinite.Count;
  28. infinite.Add(self);
  29. try {
  30. StringBuilder res = new StringBuilder();
  31. res.Append("Dictionary[");
  32. res.Append(DynamicHelpers.GetPythonTypeFromType(typeof(K)).Name);
  33. res.Append(", ");
  34. res.Append(DynamicHelpers.GetPythonTypeFromType(typeof(V)).Name);
  35. res.Append("](");
  36. if (self.Count > 0) {
  37. res.Append("{");
  38. string comma = "";
  39. foreach (KeyValuePair<K, V> obj in self) {
  40. res.Append(comma);
  41. res.Append(PythonOps.Repr(context, obj.Key));
  42. res.Append(" : ");
  43. res.Append(PythonOps.Repr(context, obj.Value));
  44. comma = ", ";
  45. }
  46. res.Append("}");
  47. }
  48. res.Append(")");
  49. return res.ToString();
  50. } finally {
  51. Debug.Assert(index == infinite.Count - 1);
  52. infinite.RemoveAt(index);
  53. }
  54. }
  55. }
  56. }