public static string __format__(CodeContext /*!*/ context, object self, [NotNull] string /*!*/ formatSpec) { string text = PythonOps.ToString(context, self); StringFormatSpec spec = StringFormatSpec.FromString(formatSpec); if (spec.Type != null && spec.Type != 's') { throw PythonOps.ValueError("Unknown format code '{0}' for object of type 'str'", spec.Type.Value.ToString()); } else if (spec.Sign != null) { throw PythonOps.ValueError("Sign not allowed in string format specifier"); } else if (spec.Alignment == '=') { throw PythonOps.ValueError("'=' alignment not allowed in string format specifier"); } // apply precision to shorten the string first if (spec.Precision != null) { int precision = spec.Precision.Value; if (text.Length > precision) { text = text.Substring(0, precision); } } // then apply the minimum width & padding text = spec.AlignText(text); // finally return the text return(text); }
public static string __format__(CodeContext /*!*/ context, object self, [NotNull] string /*!*/ formatSpec) { if (formatSpec != string.Empty) { throw PythonOps.TypeError("unsupported format string passed to {0}.__format__", PythonTypeOps.GetName(self)); } return(PythonOps.ToString(context, self)); }
// support for EnumType(number) private static T CreateEnum <T>(object value) { if (value == null) { throw PythonOps.ValueError( $"None is not a valid {PythonOps.ToString(typeof(T))}" ); } try { return((T)Enum.ToObject(typeof(T), value)); } catch (ArgumentException) { throw PythonOps.ValueError( $"{PythonOps.ToString(value)} is not a valid {PythonOps.ToString(typeof(T))}" ); } }