text:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.PythonTools.Analysis.Values;
using Microsoft.PythonTools.Infrastructure;
using Microsoft.PythonTools.Intellisense;
using Microsoft.PythonTools.Interpreter;
using Microsoft.PythonTools.Parsing.Ast;

namespace Microsoft.PythonTools.Analysis {
    class SaveAnalysis {
        private List<string> _errors = new List<string>();
        private Dictionary<AnalysisValue, string> _classNames = new Dictionary<AnalysisValue, string>();
        private List<AnalysisValue> _path = new List<AnalysisValue>();
        private Dictionary<string, Dictionary<string, object>> _typeNames = new Dictionary<string, Dictionary<string, object>>();
        private Dictionary<string, string> _MemoizedStrings = new Dictionary<string, string>();
        private Dictionary<string, object[]> _moduleNames = new Dictionary<string, object[]>();
        private static readonly List<object> _EmptyMro = new List<object>();
        private PythonAnalyzer _curAnalyzer;
        private ModuleInfo _curModule;

        public void Save(PythonAnalyzer state, string outDir) {
            _curAnalyzer = state;
            foreach (var modKeyValue in state.Modules) {
                string name = modKeyValue.Key;

                ModuleInfo moduleInfo;
                if ((moduleInfo = modKeyValue.Value.Module as ModuleInfo) != null) {
                    _curModule = moduleInfo;
                    var info = SerializeModule(moduleInfo);
                    WriteModule(outDir, name, info, moduleInfo.Scope.AllVariables.Keys());
                }
            }

            foreach (var error in _errors) {
                Console.WriteLine(error);
            }
        }

        private void WriteModule(string outDir, string name, Dictionary<string, object> info, IEnumerable<string> globals) {
            for (int i = 0; i < 10; i++) {
                try {
                    using (var writer = new FileStream(Path.Combine(outDir, name + ".idb"), FileMode.Create, FileAccess.ReadWrite)) {
                        new Pickler(writer).Dump(info);
                    }
                    break;
                } catch (IOException ex) {
                    // race with a reader, retry... http://pytools.codeplex.com/workitem/570
                    Console.WriteLine("Could not access {0} {1}", name + ".idb", ex);
                    Thread.Sleep(1000);
                }
            }

            for (int i = 0; i < 10; i++) {
                try {
                    using (var writer = new StreamWriter(new FileStream(Path.Combine(outDir, name + ".idb.$memlist"), FileMode.Create, FileAccess.ReadWrite))) {
                        object membersObj;
                        Dictionary<string, object> members;
                        if (!info.TryGetValue("members", out membersObj) ||
                            (members = membersObj as Dictionary<string, object>) == null) {
                            if (globals != null) {
                                members = globals.ToDictionary(k => k, _ => (object)null);
                            } else {
                                members = new Dictionary<string, object>();
                            }
                        }
                        foreach (var memberName in members.Keys) {
                            writer.WriteLine(memberName);
                        }
                    }
                    break;
                } catch (IOException ex) {
                    // race with a reader, retry... http://pytools.codeplex.com/workitem/570
                    Console.WriteLine("Could not access {0} {1}", name + ".idb.$memlist", ex);
                    Thread.Sleep(1000);
                }
            }
        }

        private Dictionary<string, object> SerializeModule(ModuleInfo moduleInfo) {
            var children = GenerateChildModules(moduleInfo);
            return new Dictionary<string, object>() {
                { "members", GenerateMembers(moduleInfo, children) },
                { "doc", MemoizeString(moduleInfo.Documentation) },
                { "children", children },
                { "filename", moduleInfo.ProjectEntry.FilePath }
            };
        }

        private List<object> GenerateChildModules(ModuleInfo moduleInfo) {
            var res = new HashSet<object>(moduleInfo.GetChildrenPackages(null).Select(kv => kv.Key));

            // Add any child built-in modules as well. This will include the modules that are part of the package,
            // but which do not participate in analysis (e.g. native modules).
            foreach (var child in
                from moduleName in moduleInfo.ProjectEntry.ProjectState.Interpreter.GetModuleNames()
                let lastDot = moduleName.LastIndexOf('.')
                where lastDot >= 0
                let packageName = moduleName.Substring(0, lastDot)
                where packageName == moduleInfo.Name
                select moduleName.Substring(lastDot + 1)
            ) {
                // Only include the child if it is actually importable. As a
                // side-effect, we really import the child, which means that
                // GenerateMembers will not exclude it.
                ModuleReference modTableRef;
                if (moduleInfo.ProjectEntry.ProjectState.Modules.TryImport(moduleInfo.Name + "." + child, out modTableRef)) {
                    res.Add(child);
                }
            }

            return res.ToList();
        }

        private Dictionary<string, object> GenerateMembers(ModuleInfo moduleInfo, IEnumerable<object> children) {
            Dictionary<string, object> res = new Dictionary<string, object>();
            foreach (var keyValue in moduleInfo.Scope.AllVariables) {
                if (keyValue.Value.IsEphemeral) {
                    // Never got a value, so leave it out.
                    continue;
                }
                res[keyValue.Key] = GenerateMember(keyValue.Value, moduleInfo);
            }
            foreach (var child in children.OfType<string>()) {
                object modRef = null;
                ModuleReference modTableRef;
                if (moduleInfo.ProjectEntry.ProjectState.Modules.TryGetImportedModule(moduleInfo.Name + "." + child, out modTableRef) &&
                    modTableRef != null &&
                    modTableRef.Module != null
                ) {
                    MultipleMemberInfo mmi;
                    if (modTableRef.Module is ModuleInfo || modTableRef.Module is BuiltinModule) {
                        modRef = new Dictionary<string, object> {
                            { "kind", "moduleref" },
                            { "value", GetModuleName(moduleInfo.Name + "." + child) }
                        };
                    } else if ((mmi = modTableRef.Module as MultipleMemberInfo) != null) {
                        modRef = new Dictionary<string, object> {
                            { "kind", "multiple" },
                            { "value", new Dictionary<string, object> {
                                {
                                    "members",
                                    mmi.Members
                                        .Select(m => m.Name)
                                        .Where(n => !string.IsNullOrEmpty(n))
                                        .Select(GetModuleName)
                                        .ToArray<object>()
                                }
                            } }
                        };
                    }
                }

                object existing;
                if (res.TryGetValue(child, out existing) && IsValidMember(existing)) {
                    var members1 = GetMultipleMembersOrDefault(existing) ?? new object[] { existing };
                    var members2 = GetMultipleMembersOrDefault(modRef) ?? new object[] { modRef };
                    var members = members1.Concat(members2).Distinct(ModuleReferenceComparer.Instance).ToArray();

                    if (members.Length > 1) {
                        res[child] = new Dictionary<string, object> {
                            { "kind", "multiple" },
                            { "value", new Dictionary<string, object> { { "members", members } } }
                        };
                    } else if (members.Length == 1) {
                        res[child] = members[0];
                    }
                } else {
                    res[child] = modRef;
                }
            }
            return res;
        }

        private class ModuleReferenceComparer : IEqualityComparer<object> {
            public static readonly IEqualityComparer<object> Instance = new ModuleReferenceComparer();

            private ModuleReferenceComparer() { }

            public new bool Equals(object x, object y) {
                if (object.ReferenceEquals(x, y)) {
                    return true;
                }

                var asDict1 = x as Dictionary<string, object>;
                var asDict2 = y as Dictionary<string, object>;
                if (asDict1 == null || asDict2 == null) {
                    return false;
                }

                object obj;
                if (!asDict1.TryGetValue("kind", out obj) || (obj as string) != "moduleref") {
                    return false;
                }
                if (!asDict2.TryGetValue("kind", out obj) || (obj as string) != "moduleref") {
                    return false;
                }

                object[] modref1, modref2;
                if (!asDict1.TryGetValue("value", out obj) || (modref1 = obj as object[]) == null) {
                    return false;
                }
                if (!asDict2.TryGetValue("value", out obj) || (modref2 = obj as object[]) == null) {
                    return false;
                }

                return modref1.SequenceEqual(modref2);
            }

            public int GetHashCode(object obj) {
                var asDict = obj as Dictionary<string, object>;
                if (asDict == null) {
                    return 0;
                }

                object o;
                if (!asDict.TryGetValue("kind", out o) || (o as string) != "moduleref") {
                    return 0;
                }

                object[] modref;
                if (!asDict.TryGetValue("value", out o) || (modref = o as object[]) == null) {
                    return 0;
                }

                return modref.Aggregate(48527, (a, n) => a ^ (n != null ? n.GetHashCode() : 0));
            }
        }

        private static bool IsValidMember(object info) {
            var asDict = info as Dictionary<string, object>;
            if (asDict == null) {
                return false;
            }

            object obj;
            string kind;
            if (!asDict.TryGetValue("kind", out obj) || (kind = obj as string) == null) {
                return false;
            }

            if (!asDict.TryGetValue("value", out obj)) {
                return false;
            }

            if (kind == "data" && (asDict = obj as Dictionary<string, object>) != null) {
                // { 'data': { 'type': None } } is invalid
                return asDict.TryGetValue("type", out obj) && obj != null;
            }

            return true;
        }

        private static object[] GetMultipleMembersOrDefault(object info) {
            var asDict = info as Dictionary<string, object>;
            if (asDict == null) {
                return null;
            }

            object obj;
            string kind;
            if (!asDict.TryGetValue("kind", out obj) || (kind = obj as string) == null || kind != "multiple") {
                return null;
            }

            if (!asDict.TryGetValue("value", out obj) || (asDict = obj as Dictionary<string, object>) == null) {
                return null;
            }

            if (!asDict.TryGetValue("members", out obj)) {
                return null;
            }

            return obj as object[];
        }

        private object GenerateMember(VariableDef variableDef, ModuleInfo declModule, bool isRef = false) {
            Dictionary<string, object> memberEntry = new Dictionary<string, object>() {
                { "kind", GetMemberKind(variableDef, declModule, isRef) },
                { "value", GetMemberValue(variableDef, declModule, isRef) }
            };

            return memberEntry;
        }

        private object GetMemberValues(VariableDef[] variableDefs, ModuleInfo declModule, bool isRef) {
            List<object> res = new List<object>();
            foreach (var variableDef in variableDefs) {
                res.Add(GetMemberValue(variableDef, declModule, isRef));
            }
            return res;
        }

        private object GetMemberValue(VariableDef variableDef, ModuleInfo declModule, bool isRef) {
            return GetMemberValue(variableDef.TypesNoCopy, declModule, isRef);
        }

        private object GetMemberValue(IAnalysisSet types, ModuleInfo declModule, bool isRef) {
            if (types.Count == 1) {
                var type = types.First();
                var res = GetMemberValueInternal(type, declModule, isRef);
                if (res == null) {
                    _errors.Add(String.Format("Cannot save single member: {0}", types.First()));
                }
                return res;
            } else if (types.Count == 0) {
                return new Dictionary<string, object>() {
                    { "type", null }
                };
            } else {
                List<object> res = new List<object>();
                foreach (var type in types) {
                    res.Add(
                        new Dictionary<string, object>() {
                            { "kind", GetMemberKind(type, declModule, isRef) },
                            { "value", GetMemberValueInternal(type, declModule, isRef) }
                        }
                    );
                }
                return new Dictionary<string, object>() {
                    { "members", res.ToArray() }
                };
            }
        }

        private object GetMemberValueInternal(AnalysisValue type, ModuleInfo declModule, bool isRef) {
            SpecializedNamespace specialCallable = type as SpecializedNamespace;
            if (specialCallable != null) {
                if (specialCallable.Original == null) {
                    return null;
                }
                return GetMemberValueInternal(specialCallable.Original, declModule, isRef);
            }

            switch (type.MemberType) {
                case PythonMemberType.Function:
                    FunctionInfo fi = type as FunctionInfo;
                    if (fi != null) {
                        if (fi.DeclaringModule.GetModuleInfo() != declModule) {
                            return GenerateFuncRef(fi);
                        } else {
                            return GenerateFunction(fi);
                        }
                    }

                    BuiltinFunctionInfo bfi = type as BuiltinFunctionInfo;
                    if (bfi != null) {
                        return GenerateFuncRef(bfi);
                    }

                    return "function";
                case PythonMemberType.Method:
                    BoundMethodInfo mi = type as BoundMethodInfo;
                    if (mi != null) {
                        return GenerateMethod(mi);
                    }
                    return "method";
                case PythonMemberType.Property:
                    FunctionInfo prop = type as FunctionInfo;
                    if (prop != null) {
                        return GenerateProperty(prop);
                    }
                    break;
                case PythonMemberType.Class:
                    ClassInfo ci = type as ClassInfo;
                    if (ci != null) {
                        if (isRef || ci.DeclaringModule.GetModuleInfo() != declModule) {
                            // TODO: Save qualified name so that classes defined in classes/function can be resolved
                            return GetTypeRef(ci.DeclaringModule.ModuleName, ci.Name);
                        } else {
                            return GenerateClass(ci, declModule);
                        }
                    }

                    BuiltinClassInfo bci = type as BuiltinClassInfo;
                    if (bci != null) {
                        return GetTypeRef(bci.PythonType.DeclaringModule.Name, bci.PythonType.Name);
                    }
                    return "type";
                case PythonMemberType.Constant:
                    ConstantInfo constantInfo = type as ConstantInfo;
                    if (constantInfo != null) {
                        return GenerateConstant(constantInfo);
                    }
                    break;
                case PythonMemberType.Module:
                    if (type is ModuleInfo) {
                        return GetModuleName(((ModuleInfo)type).Name);
                    } else if (type is BuiltinModule) {
                        return GetModuleName(((BuiltinModule)type).Name);
                    }
                    break;
                case PythonMemberType.Instance:
                    return new Dictionary<string, object> {
                        { "type", GenerateTypeName(type, true) }
                    };
                default:
                    return new Dictionary<string, object>() {
                        { "type", GenerateTypeName(type.PythonType) }
                    };
            }
            return null;
        }

        private object GenerateFuncRef(FunctionInfo fi) {
            string name = ".";
            for (var cd = fi.FunctionDefinition.Parent as ClassDefinition;
                cd != null;
                cd = cd.Parent as ClassDefinition) {
                name = "." + cd.Name + name;
            }
            name = fi.DeclaringModule.ModuleName + name + fi.Name;

            return new Dictionary<string, object>() {
                { "func_name", MemoizeString(name) }
            };
        }

        private object GenerateFuncRef(BuiltinFunctionInfo bfi) {
            string name = bfi.Function.DeclaringModule.Name;
            if (bfi.Function.DeclaringType != null) {
                name += "." + bfi.Function.DeclaringType.Name;
            }

            name += "." + bfi.Function.Name;

            return new Dictionary<string, object>() {
                { "func_name", MemoizeString(name) }
            };
        }

        private string GetMemberKind(VariableDef variableDef, ModuleInfo declModule, bool isRef) {
            if (variableDef.Types.Count == 1) {
                return GetMemberKind(variableDef.Types.First(), declModule, isRef);
            } else if (variableDef.Types.Count == 0) {
                // typed to object
                return "data";
            } else {
                return "multiple";
            }
        }

        private static string GetMemberKind(AnalysisValue type, ModuleInfo declModule, bool isRef) {
            SpecializedNamespace specialCallable = type as SpecializedNamespace;
            if (specialCallable != null) {
                if (specialCallable.Original == null) {
                    return "data";
                }
                return GetMemberKind(specialCallable.Original, declModule, isRef);
            }

            switch (type.MemberType) {
                case PythonMemberType.Function:
                    if (type is BuiltinFunctionInfo || type.DeclaringModule != declModule.ProjectEntry) {
                        return "funcref";
                    }
                    return "function";
                case PythonMemberType.Method: return "method";
                case PythonMemberType.Property: return "property";
                case PythonMemberType.Class:
                    if (isRef || type is BuiltinClassInfo || (type is ClassInfo && type.DeclaringModule.GetModuleInfo() != declModule)) {
                        return "typeref";
                    }
                    return "type";
                case PythonMemberType.Module:
                    return "moduleref";
                case PythonMemberType.Instance:
                default:
                    return "data";
            }
        }

        private object GenerateProperty(FunctionInfo prop) {
            return new Dictionary<string, object>() {
                { "doc", MemoizeString(prop.Documentation) },
                { "type", GenerateTypeName(GetFunctionReturnTypes(prop), true) },
                { "location", GenerateLocation(prop.Locations) }
            };
        }

        private static IAnalysisSet GetFunctionReturnTypes(FunctionInfo func) {
            return func.GetReturnValue();
        }

        private Dictionary<string, object> GenerateConstant(ConstantInfo constantInfo) {
            return new Dictionary<string, object>() {
                { "type", GenerateTypeName(constantInfo.PythonType) }
            };
        }

        private Dictionary<string, object> GenerateClass(ClassInfo ci, ModuleInfo declModule) {
            return new Dictionary<string, object>() {
                { "mro", GetClassMro(ci) },
                { "bases", GetClassBases(ci) },
                { "members", GetClassMembers(ci, declModule) },
                { "doc", MemoizeString(ci.Documentation) },
                { "builtin", false },
                { "location", GenerateLocation(ci.Locations) }
            };
        }

        private object GetTypeRef(string moduleName, string className) {
            return new List<object> { GetTypeName(moduleName, className) };
        }

        private object GetTypeName(string moduleName, string className) {
            // memoize types names for a more efficient on disk representation.
            object typeName;
            Dictionary<string, object> typeNames;
            if (!_typeNames.TryGetValue(moduleName, out typeNames)) {
                _typeNames[moduleName] = typeNames = new Dictionary<string, object>();
            }

            if (!typeNames.TryGetValue(className, out typeName)) {
                typeNames[className] = typeName = new object[] {
                    MemoizeString(moduleName),
                    MemoizeString(className)
                };
            }
            return typeName;
        }

        private object[] GetModuleName(string moduleName) {
            // memoize types names for a more efficient on disk representation.
            object[] name;
            if (!_moduleNames.TryGetValue(moduleName, out name)) {
                _moduleNames[moduleName] = name = new object[] { MemoizeString(moduleName), string.Empty };
            }
            return name;
        }

        private object GetClassMembers(ClassInfo ci, ModuleInfo declModule) {
            Dictionary<string, object> memTable = new Dictionary<string, object>();
            foreach (var keyValue in ci.Scope.AllVariables) {
                if (keyValue.Value.IsEphemeral) {
                    continue;
                }
                memTable[keyValue.Key] = GenerateMember(keyValue.Value, declModule, true);
            }
            if (ci.Instance.InstanceAttributes != null) {
                foreach (var keyValue in ci.Instance.InstanceAttributes) {
                    if (keyValue.Value.IsEphemeral) {
                        continue;
                    }
                    memTable[keyValue.Key] = GenerateMember(keyValue.Value, declModule, true);
                }
            }

            return memTable;
        }

        private List<object> GetClassBases(ClassInfo ci) {
            List<object> res = new List<object>();
            foreach (var baseClassSet in ci.Bases) {
                foreach (var baseClass in baseClassSet) {
                    var typeName = GenerateTypeName(baseClass, true);
                    if (typeName != null) {
                        res.Add(typeName);
                    }
                }
            }
            return res;
        }

        private object GenerateTypeName(IAnalysisSet name, bool isRef) {
            if (name.Count == 0) {
                return null;
            } else if (name.Count == 1) {
                return GenerateTypeName(name.First(), isRef);
            }

            return name.Select(ns => GenerateTypeName(ns, isRef)).Distinct().ToList<object>();
        }

        private object GenerateTypeName(AnalysisValue baseClass, bool isRef) {
            ClassInfo ci = baseClass as ClassInfo;
            if (ci != null) {
                return GetTypeName(((ProjectEntry)ci.DeclaringModule).GetModuleInfo().Name, ci.ClassDefinition.Name);
            }

            BuiltinClassInfo bci = baseClass as BuiltinClassInfo;
            if (bci != null) {
                return GenerateTypeName(bci._type);
            }

            IterableValue iteri = baseClass as IterableValue;
            if (iteri != null) {
                return GenerateTypeName(iteri.PythonType, isRef, iteri.IndexTypes);
            }

            InstanceInfo ii = baseClass as InstanceInfo;
            if (ii != null) {
                return GenerateTypeName(ii.ClassInfo, isRef);
            }

            BuiltinInstanceInfo bii = baseClass as BuiltinInstanceInfo;
            if (bii != null) {
                return GenerateTypeName(bii.ClassInfo, isRef);
            }

            return GenerateTypeName(baseClass.PythonType);
        }

        private object GenerateTypeName(IPythonType type) {
            if (type != null) {
                return GetTypeName(type.DeclaringModule.Name, type.Name);
            }
            return null;
        }

        private object GenerateTypeName(IPythonType type, bool isRef, VariableDef[] indexTypes) {
            if (type != null) {
                if (indexTypes == null || indexTypes.Length == 0) {
                    return GetTypeName(type.DeclaringModule.Name, type.Name);
                }

                var moduleName = type.DeclaringModule.Name;
                var className = type.Name + "`" + string.Join(",", indexTypes.Select(t => t.TypesNoCopy.ToString()));
                object typeName;
                Dictionary<string, object> typeNames;
                if (!_typeNames.TryGetValue(moduleName, out typeNames)) {
                    _typeNames[moduleName] = typeNames = new Dictionary<string, object>();
                }

                if (!typeNames.TryGetValue(className, out typeName)) {
                    var mModuleName = MemoizeString(moduleName);
                    var mTypeName = MemoizeString(type.Name);
                    // Create a type without the typename that will be used if
                    // the index types recurse.
                    typeNames[className] = typeName = new object[] { mModuleName, mTypeName };

                    typeNames[className] = typeName = new object[] {
                        mModuleName,
                        mTypeName,
                        indexTypes.Select(vd => GenerateTypeName(vd.TypesNoCopy, isRef)).ToList<object>(),
                    };
                }

                return typeName;
            }
            return null;
        }

        private object GetClassMro(ClassInfo ci) {
            List<object> res = new List<object>();
            foreach (var mroClassSet in ci.Mro) {
                foreach (var mroClass in mroClassSet) {
                    var typeName = GenerateTypeName(mroClass, true);
                    if (typeName != null) {
                        res.Add(typeName);
                    }
                }
            }
            return res;
        }

        private Dictionary<string, object> GenerateFunction(FunctionInfo fi) {
            // TODO: Include inner classes/functions
            var res = new Dictionary<string, object>() {
                { "doc", fi.Documentation },
                { "builtin", false },
                { "location", GenerateLocation(fi.Locations) },
                { "overloads", GenerateOverloads(fi) }
            };
            if (fi.IsStatic) {
                res["static"] = fi.IsStatic;
            }
            if (fi.IsClassMethod) {
                res["classmethod"] = true;
            }
            return res;
        }

        private Dictionary<string, object> GenerateMethod(BoundMethodInfo bmi) {
            var res = GenerateFunction(bmi.Function);
            if (bmi.Instance != null && bmi.Instance.TypeId != BuiltinTypeId.NoneType) {
                res["bound"] = true;
            }
            return res;
        }

        private static object[] GenerateLocation(LocationInfo location) {
            return new object[] { location.StartLine, location.StartColumn };
        }

        private static object[] GenerateLocation(IEnumerable<LocationInfo> locations) {
            // TODO: Support saving and loading multiple locations for a single definition
            Debug.Assert(locations.Count() == 1);
            var location = locations.FirstOrDefault();
            if (location != null) {
                return GenerateLocation(location);
            }
            return new object[0];
        }

        private List<object> GenerateOverloads(FunctionInfo fi) {
            var res = new List<object>();

            // TODO: Store distinct calls as separate overloads
            res.Add(new Dictionary<string, object> {
                { "args", GenerateArgInfo(fi, fi.GetParameterTypes()) },
                { "ret_type", GenerateTypeName(fi.GetReturnValue(), true) }
            });

            return res;
        }

        private object[] GenerateArgInfo(FunctionInfo fi, IAnalysisSet[] parameters) {
            var res = new object[Math.Min(fi.FunctionDefinition.Parameters.Count, parameters.Length)];
            for (int i = 0; i < res.Length; i++) {
                res[i] = GenerateParameter(fi.FunctionDefinition.Parameters[i], parameters[i]);
            }
            return res;
        }

        private string MemoizeString(string input) {
            if (input == null) {
                return null;
            }
            string res;
            if (!_MemoizedStrings.TryGetValue(input, out res)) {
                _MemoizedStrings[input] = res = input;
            }
            return res;
        }

        private object GenerateParameter(Parameter param, IAnalysisSet typeInfo) {
            Dictionary<string, object> res = new Dictionary<string, object>();
            if (param.Kind == ParameterKind.Dictionary) {
                res["arg_format"] = "**";
            } else if (param.Kind == ParameterKind.List) {
                res["arg_format"] = "*";
            }
            res["name"] = MemoizeString(param.Name);
            res["type"] = GenerateTypeName(typeInfo, true);
            var defaultValue = FunctionInfo.GetDefaultValue(_curAnalyzer, param, _curModule.ProjectEntry.Tree);
            if (defaultValue != null) {
                res["default_value"] = defaultValue;
            }
            return res;
        }

    }
}


text:
// Copyright © 2017-2019 The OpenEBS Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package server

import (
	"bytes"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/ugorji/go/codec"
)

/*var (
	RequestDuration = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Name:    "latest_openebs_volume_request_duration_seconds",
			Help:    "Request response time of the /latest/volumes.",
			Buckets: []float64{0.005, 0.01, 0.025, 0.05, 0.075, 0.1, .5, 1, 2.5, 5, 10},
		},
		// code is http code and method is http method returned by
		// endpoint "/latest/volumes"
		[]string{"code", "method"},
	)
	// latestOpenEBSVolumeRequestCounter Count the no of request Since a
	// request has been made on /latest/volumes
	RequestCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "latest_openebs_volume_requests_total",
			Help: "Total number of /latest/volumes requests.",
		},
		[]string{"code", "method"},
	)
)
*/
func TestInvalidReqMetaData(t *testing.T) {
	s := makeHTTPTestServer(t, nil)
	defer s.Cleanup()

	resp := httptest.NewRecorder()
	// valid uri is meta-data & not metadata
	req, _ := http.NewRequest("GET", "/metadata/instance-id", nil)

	out, err := s.Server.MetaSpecificRequest(resp, req)

	if err == nil {
		t.Fatalf("ERR: expected: %v, got: %v", ErrInvalidMethod, err)
	}

	if err.Error() != ErrInvalidPath {
		t.Fatalf("ERR: expected: %v, got: %v", ErrInvalidPath, err.Error())
	}

	if out != nil {
		t.Fatalf("Service must not return any value, for invalid request")
	}
}

func TestMetaInstanceID(t *testing.T) {
	s := makeHTTPTestServer(t, nil)
	defer s.Cleanup()

	resp := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/latest/meta-data/instance-id", nil)

	out, err := s.Server.MetaSpecificRequest(resp, req)

	if err != nil {
		t.Fatalf("ERR: %v", err)
	}

	if out == "" || out == nil {
		t.Fatalf("Service must return a non empty instance")
	}
}

func TestInvalidReqMetaInstanceID(t *testing.T) {
	s := makeHTTPTestServer(t, nil)
	defer s.Cleanup()

	resp := httptest.NewRecorder()
	req, _ := http.NewRequest("POST", "/latest/meta-data/instance-id", nil)

	out, err := s.Server.MetaSpecificRequest(resp, req)

	if err == nil {
		t.Fatalf("ERR: expected: %v, got: %v", CodedError(405, ErrInvalidMethod), err)
	}

	if err.Error() != ErrInvalidMethod {
		t.Fatalf("ERR: expected: %v, got: %v", ErrInvalidMethod, err.Error())
	}

	if out != nil {
		t.Fatalf("Service must not return any value, for invalid request")
	}
}

func TestMetaAvailZone(t *testing.T) {
	s := makeHTTPTestServer(t, nil)
	defer s.Cleanup()

	resp := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/latest/meta-data/placement/availability-zone", nil)

	out, err := s.Server.MetaSpecificRequest(resp, req)

	if err != nil {
		t.Fatalf("ERR: %v", err)
	}

	if out == "" || out == nil {
		t.Fatalf("Service must return a non empty instance")
	}
}

func TestInvalidReqMetaAvailZone(t *testing.T) {
	s := makeHTTPTestServer(t, nil)
	defer s.Cleanup()

	resp := httptest.NewRecorder()
	req, _ := http.NewRequest("POST", "/latest/meta-data/placement/availability-zone", nil)

	out, err := s.Server.MetaSpecificRequest(resp, req)

	if err == nil {
		t.Fatalf("ERR: expected: %v, got: %v", CodedError(405, ErrInvalidMethod), err)
	}

	if err.Error() != ErrInvalidMethod {
		t.Fatalf("ERR: expected: %v, got: %v", ErrInvalidMethod, err.Error())
	}

	if out != nil {
		t.Fatalf("Service must not return any value, for invalid request")
	}
}

func TestInvalidReqPathMetaViaWrap1(t *testing.T) {

	s := makeHTTPTestServer(t, nil)
	defer s.Cleanup()

	resp := httptest.NewRecorder()

	// An invalid URL path
	req, err := http.NewRequest("GET",
		"/oddy/meta-data/placement/availability-zone", nil)

	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// The handler i.e. `s.Server.MetaSpecificRequest` is curried via `wrap`
	// function & returned. The returned func is immediately invoked by
	// passing the respective arguments i.e. `resp` & `req`.
	// Learn more by understanding -
	// `Immediately Invoked Function Expression (IIFE)`.
	s.Server.wrap(RequestCounter, RequestDuration, s.Server.MetaSpecificRequest)(resp, req)

	contentType := resp.Header().Get("Content-Type")

	if len(contentType) != 0 {
		t.Fatalf("err content type, expected: nil, got: %s", contentType)
	}

	// This should be an invalid path error
	if resp.Code != 421 {
		t.Fatalf("err http resp code, expected: 421, got: %v", resp.Code)
	}

	// actuals
	actual, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatalf("err reading response: %v", err)
	}

	// compare expectations with actuals
	if !bytes.Equal([]byte(ErrInvalidPath), actual) {
		t.Fatalf("bad:\nexpected:\t%q\n\nactual:\t\t%q", ErrInvalidPath, string(actual))
	}
}

func TestInvalidReqPathMetaViaWrap2(t *testing.T) {

	s := makeHTTPTestServer(t, nil)
	defer s.Cleanup()

	resp := httptest.NewRecorder()

	// An invalid URL path
	// NOTE: `v1` is invalid here
	req, err := http.NewRequest("GET",
		"/latest/meta-data/v1/placement/availability-zone", nil)

	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// The handler i.e. `s.Server.MetaSpecificRequest` is curried via `wrap`
	// function & returned. The returned func is immediately invoked by
	// passing the respective arguments i.e. `resp` & `req`.

	// Learn more by understanding -
	// `Immediately Invoked Function Expression (IIFE)`.
	s.Server.wrap(RequestCounter, RequestDuration, s.Server.MetaSpecificRequest)(resp, req)

	contentType := resp.Header().Get("Content-Type")

	if len(contentType) != 0 {
		t.Fatalf("err content type, expected: nil, got: %s", contentType)
	}

	// This should be an invalid path error
	if resp.Code != 421 {
		t.Fatalf("err http resp code, expected: 421, got: %v", resp.Code)
	}

	// actuals
	actual, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatalf("err reading response: %v", err)
	}

	// compare expectations with actuals
	if !bytes.Equal([]byte(ErrInvalidPath), actual) {
		t.Fatalf("bad:\nexpected:\t%q\n\nactual:\t\t%q", ErrInvalidPath, string(actual))
	}
}

func TestMetaAvailZoneViaWrap(t *testing.T) {

	s := makeHTTPTestServer(t, nil)
	defer s.Cleanup()

	resp := httptest.NewRecorder()

	req, err := http.NewRequest("GET",
		"/latest/meta-data/placement/availability-zone", nil)

	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// The handler i.e. `s.Server.MetaSpecificRequest` is curried via `wrap`
	// function & returned. The returned func is immediately invoked by
	// passing the respective arguments i.e. `resp` & `req`.
	// Learn more by understanding -
	// `Immediately Invoked Function Expression (IIFE)`.
	s.Server.wrap(RequestCounter, RequestDuration, s.Server.MetaSpecificRequest)(resp, req)

	contentType := resp.Header().Get("Content-Type")

	if contentType != "application/json" {
		t.Fatalf("Content-Type header was not 'application/json'")
	}

	// expectations
	var expected bytes.Buffer
	enc := codec.NewEncoder(&expected, jsonHandle)
	err = enc.Encode(AnyZone)

	if err != nil {
		t.Fatalf("err while encoding: %v", err)
	}

	// actuals
	actual, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatalf("err reading response: %v", err)
	}

	// compare
	if !bytes.Equal(expected.Bytes(), actual) {
		t.Fatalf("bad:\nexpected:\t%q\n\nactual:\t\t%q", expected.String(), string(actual))
	}
}

func TestInvalidReqMetaAvailZoneViaWrap(t *testing.T) {

	s := makeHTTPTestServer(t, nil)
	defer s.Cleanup()

	resp := httptest.NewRecorder()

	// NOTE: `POST` req is invalid
	req, err := http.NewRequest("POST",
		"/latest/meta-data/placement/availability-zone", nil)

	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// The handler i.e. `s.Server.MetaSpecificRequest` is curried via `wrap`
	// function & returned. The returned func is immediately invoked by
	// passing the respective arguments i.e. `resp` & `req`.
	// Learn more by understanding -
	// `Immediately Invoked Function Expression (IIFE)`.
	s.Server.wrap(RequestCounter, RequestDuration, s.Server.MetaSpecificRequest)(resp, req)

	contentType := resp.Header().Get("Content-Type")

	if len(contentType) != 0 {
		t.Fatalf("err content type, expected: nil, got: %s", contentType)
	}

	// This should be an invalid path/method error
	if resp.Code != 405 {
		t.Fatalf("err http resp code, expected: 405, got: %v", resp.Code)
	}

	// actuals
	actual, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatalf("err reading response: %v", err)
	}

	// compare expectations with actuals
	if !bytes.Equal([]byte(ErrInvalidMethod), actual) {
		t.Fatalf("bad:\nexpected:\t%q\n\nactual:\t\t%q", ErrInvalidMethod, string(actual))
	}
}

func TestMetaInstanceIDViaWrap(t *testing.T) {

	s := makeHTTPTestServer(t, nil)
	defer s.Cleanup()

	resp := httptest.NewRecorder()

	req, err := http.NewRequest("GET",
		"/latest/meta-data/instance-id", nil)

	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// The handler i.e. `s.Server.MetaSpecificRequest` is curried via `wrap`
	// function & returned. The returned func is immediately invoked by
	// passing the respective arguments i.e. `resp` & `req`.
	// Learn more by understanding -
	// `Immediately Invoked Function Expression (IIFE)`.
	s.Server.wrap(RequestCounter, RequestDuration, s.Server.MetaSpecificRequest)(resp, req)

	contentType := resp.Header().Get("Content-Type")

	if contentType != "application/json" {
		t.Fatalf("Content-Type header was not 'application/json'")
	}

	// expectations
	var expected bytes.Buffer
	enc := codec.NewEncoder(&expected, jsonHandle)
	err = enc.Encode(AnyInstance)

	if err != nil {
		t.Fatalf("err while encoding: %v", err)
	}

	// actuals
	actual, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatalf("err reading response: %v", err)
	}

	// compare
	if !bytes.Equal(expected.Bytes(), actual) {
		t.Fatalf("bad:\nexpected:\t%q\n\nactual:\t\t%q", expected.String(), string(actual))
	}
}

func TestInvalidReqMetaInstanceIDViaWrap(t *testing.T) {

	s := makeHTTPTestServer(t, nil)
	defer s.Cleanup()

	resp := httptest.NewRecorder()

	// NOTE: `POST` req is invalid
	req, err := http.NewRequest("POST",
		"/latest/meta-data/instance-id", nil)

	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// The handler i.e. `s.Server.MetaSpecificRequest` is curried via `wrap`
	// function & returned. The returned func is immediately invoked by
	// passing the respective arguments i.e. `resp` & `req`.
	// Learn more by understanding -
	// `Immediately Invoked Function Expression (IIFE)`.
	s.Server.wrap(RequestCounter, RequestDuration, s.Server.MetaSpecificRequest)(resp, req)

	contentType := resp.Header().Get("Content-Type")

	if len(contentType) != 0 {
		t.Fatalf("err content type, expected: nil, got: %s", contentType)
	}

	// This should be an invalid path/method error
	if resp.Code != 405 {
		t.Fatalf("err http resp code, expected: 405, got: %v", resp.Code)
	}

	// actuals
	actual, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatalf("err reading response: %v", err)
	}

	// compare expectations with actuals
	if !bytes.Equal([]byte(ErrInvalidMethod), actual) {
		t.Fatalf("bad:\nexpected:\t%q\n\nactual:\t\t%q", ErrInvalidMethod, string(actual))
	}
}

redpajama_set_name: RedPajamaGithub


text:
#import <Foundation/NSFormatter.h>

@class NSDecimalNumberHandler;

typedef int NSNumberFormatterBehavior;
typedef int NSNumberFormatterStyle;
typedef int NSNumberFormatterPadPosition;
typedef int NSNumberFormatterRoundingMode;

@interface NSNumberFormatter : NSFormatter {
    NSString     *_positiveFormat;
    NSString     *_negativeFormat;
    NSDictionary *_positiveAttributes;
    NSDictionary *_negativeAttributes;
    NSString     *_thousandSeparator;
    NSString     *_decimalSeparator;

    NSAttributedString *_attributedStringForNil;
    NSAttributedString *_attributedStringForNotANumber;
    NSAttributedString *_attributedStringForZero;

    BOOL _allowsFloats;
    BOOL _localizesFormat;
    BOOL _hasThousandSeparators;
}

+(NSNumberFormatterBehavior)defaultFormatterBehavior;
+(void)setDefaultFormatterBehavior:(NSNumberFormatterBehavior)value;

-(NSNumberFormatterBehavior)formatterBehavior;
-(NSNumberFormatterStyle)numberStyle;

-(NSString *)format;
-(NSUInteger)formatWidth;
-(NSLocale *)locale;
-(NSNumber *)multiplier;

-(BOOL)allowsFloats;
-(BOOL)localizesFormat;
-(BOOL)hasThousandSeparators;
-(BOOL)alwaysShowsDecimalSeparator;
-(BOOL)isLenient;
-(BOOL)isPartialStringValidationEnabled;
-(BOOL)generatesDecimalNumbers;
-(BOOL)usesGroupingSeparator;
-(BOOL)usesSignificantDigits;

-(NSNumber *)minimum;
-(NSUInteger)minimumIntegerDigits;
-(NSUInteger)minimumFractionDigits;
-(NSUInteger)minimumSignificantDigits;

-(NSNumber *)maximum;
-(NSUInteger)maximumIntegerDigits;
-(NSUInteger)maximumFractionDigits;
-(NSUInteger)maximumSignificantDigits;

-(NSString *)nilSymbol;
-(NSString *)notANumberSymbol;
-(NSString *)zeroSymbol;
-(NSString *)plusSign;
-(NSString *)minusSign;
-(NSString *)negativePrefix;
-(NSString *)negativeSuffix;
-(NSString *)positivePrefix;
-(NSString *)positiveSuffix;
-(NSString *)negativeInfinitySymbol;
-(NSString *)positiveInfinitySymbol;
-(NSString *)thousandSeparator;
-(NSString *)decimalSeparator;
-(NSString *)exponentSymbol;
-(NSString *)currencyCode;
-(NSString *)currencySymbol;
-(NSString *)internationalCurrencySymbol;
-(NSString *)currencyDecimalSeparator;
-(NSString *)currencyGroupingSeparator;
-(NSString *)groupingSeparator;
-(NSUInteger)groupingSize;
-(NSUInteger)secondaryGroupingSize;
-(NSString *)paddingCharacter;
-(NSNumberFormatterPadPosition)paddingPosition;
-(NSString *)percentSymbol;
-(NSString *)perMillSymbol;
-(NSDecimalNumberHandler *)roundingBehavior;
-(NSNumber *)roundingIncrement;
-(NSNumberFormatterRoundingMode)roundingMode;

-(NSString *)positiveFormat;
-(NSString *)negativeFormat;
-(NSDictionary *)textAttributesForPositiveValues;
-(NSDictionary *)textAttributesForNegativeValues;
-(NSAttributedString *)attributedStringForNil;
-(NSAttributedString *)attributedStringForNotANumber;
-(NSAttributedString *)attributedStringForZero;
-(NSDictionary *)textAttributesForNegativeInfinity;
-(NSDictionary *)textAttributesForNil;
-(NSDictionary *)textAttributesForNotANumber;
-(NSDictionary *)textAttributesForPositiveInfinity;
-(NSDictionary *)textAttributesForZero;

-(void)setFormat:(NSString *)format;
-(void)setAllowsFloats:(BOOL)flag;
-(void)setLocalizesFormat:(BOOL)flag;

-(void)setCurrencyCode:(NSString *)value;
-(void)setCurrencyDecimalSeparator:(NSString *)value;
-(void)setCurrencyGroupingSeparator:(NSString *)value;
-(void)setCurrencySymbol:(NSString *)value;
-(void)setDecimalSeparator:(NSString *)value;
-(void)setExponentSymbol:(NSString *)value;
-(void)setFormatterBehavior:(NSNumberFormatterBehavior)value;
-(void)setFormatWidth:(NSUInteger)value;
-(void)setGeneratesDecimalNumbers:(BOOL)value;
-(void)setGroupingSeparator:(NSString *)value;
-(void)setGroupingSize:(NSUInteger)value;
-(void)setInternationalCurrencySymbol:(NSString *)value;
-(void)setLenient:(BOOL)value;
-(void)setLocale:(NSLocale *)value;
-(void)setMaximum:(NSNumber *)value;
-(void)setMaximumFractionDigits:(NSUInteger)value;
-(void)setMaximumIntegerDigits:(NSUInteger)value;
-(void)setMaximumSignificantDigits:(NSUInteger)value;
-(void)setMinimum:(NSNumber *)value;
-(void)setMinimumFractionDigits:(NSUInteger)value;
-(void)setMinimumIntegerDigits:(NSUInteger)value;
-(void)setMinimumSignificantDigits:(NSUInteger)value;
-(void)setMinusSign:(NSString *)value;
-(void)setMultiplier:(NSNumber *)value;
-(void)setNegativeInfinitySymbol:(NSString *)value;
-(void)setNegativePrefix:(NSString *)value;
-(void)setNegativeSuffix:(NSString *)value;
-(void)setNilSymbol:(NSString *)value;
-(void)setNotANumberSymbol:(NSString *)value;
-(void)setNumberStyle:(NSNumberFormatterStyle)value;
-(void)setPaddingCharacter:(NSString *)value;
-(void)setPaddingPosition:(NSNumberFormatterPadPosition)value;
-(void)setPartialStringValidationEnabled:(BOOL)value;
-(void)setPercentSymbol:(NSString *)value;
-(void)setPerMillSymbol:(NSString *)value;
-(void)setPlusSign:(NSString *)value;
-(void)setPositiveInfinitySymbol:(NSString *)value;
-(void)setPositivePrefix:(NSString *)value;
-(void)setPositiveSuffix:(NSString *)value;
-(void)setRoundingBehavior:(NSDecimalNumberHandler *)value;
-(void)setRoundingIncrement:(NSNumber *)value;
-(void)setRoundingMode:(NSNumberFormatterRoundingMode)value;
-(void)setSecondaryGroupingSize:(NSUInteger)value;
-(void)setTextAttributesForNegativeInfinity:(NSDictionary *)value;
-(void)setTextAttributesForNil:(NSDictionary *)value;
-(void)setTextAttributesForNotANumber:(NSDictionary *)value;
-(void)setTextAttributesForPositiveInfinity:(NSDictionary *)value;
-(void)setTextAttributesForZero:(NSDictionary *)value;
-(void)setThousandSeparator:(NSString *)value;
-(void)setUsesGroupingSeparator:(BOOL)value;
-(void)setUsesSignificantDigits:(BOOL)value;
-(void)setZeroSymbol:(NSString *)value;

-(void)setHasThousandSeparators:(BOOL)value;
-(void)setAlwaysShowsDecimalSeparator:(BOOL)value;

-(void)setPositiveFormat:(NSString *)format;
-(void)setNegativeFormat:(NSString *)format;

-(void)setTextAttributesForNegativeValues:(NSDictionary *)attributes;
-(void)setTextAttributesForPositiveValues:(NSDictionary *)attributes;

-(void)setAttributedStringForNil:(NSAttributedString *)attributedString;
-(void)setAttributedStringForNotANumber:(NSAttributedString *)attributedString;
-(void)setAttributedStringForZero:(NSAttributedString *)attributedString;

-(NSString *)stringFromNumber:(NSNumber *)number;
-(NSNumber *)numberFromString:(NSString *)string;

-(BOOL)getObjectValue:(id *)valuep forString:(NSString *)string range:(NSRange *)rangep error:(NSError **)errorp;

@end


text:
import React from 'react';
import Layout from './layout';
import Navbar from './navbar';

const Introduction = () => (

    Depcheck looks at your project files and scans your code to find any unused dependencies. Get rid of useless dependencies, and ship clean package.

);

const Logo = () => (

);

const Login = () => (

    Login with GitHub Account

);

const Welcome = ({ login, repoListUrl }) => (

    Logged in as {login.provider}/{login.user}

);

export default React.createClass({
  render() {
    return (
      <Layout>

Depcheck

            <Introduction />

              {this.props.login ? <Welcome {...this.props} /> : <Login />}

            <Navbar {...this.props} />

        <Logo />
      </Layout>
    );
  },
});


text:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;

namespace Limo.Plugins
{

    /// <summary>
    /// Manages plugins of specified base type
    /// </summary>
    /// <typeparam name="T">Base type of plugins tu manage</typeparam>
    public class PluginsManager<T>
    {

        #region Public Properties

        /// <summary>
        /// The list of collected plugins.
        /// </summary>
        public IReadOnlyCollection<T> Plugins { get; private set; } = new List<T>();

        #endregion Public Properties

        #region Public Constructors

        /// <summary>
        /// Constructor. Mostly likely it will be called from DI framework.
        /// </summary>
        /// <param name="logger">logger where information must be printed</param>
        /// <param name="configuration"></param>
        /// <param name="serviceProvider">This service provider will be used to find services required by service plygins</param>
        /// <param name="pluginsFinder">Plugins finder to use to find available plugins</param>
        /// <param name="order">Comparision object used to sort plugin list</param>
        public PluginsManager(ILogger<PluginsManager<T>> logger, IConfiguration configuration, IServiceProvider serviceProvider,
          IPluginsFinder pluginsFinder, Comparison<T> order = null)
        {
            this.logger = logger;
            this.serviceProvider = serviceProvider;
            this.configuration = configuration;
            this.pluginsFinder = pluginsFinder;

            plugins = CreatePlugins();
            Configure(order);

            Plugins = new ReadOnlyCollection<T>(plugins);
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Configure PluginsManager
        /// </summary>
        /// <param name="order">Comparision object used to sort plugins</param>
        public void Configure(Comparison<T> order)
        {
            this.order = order;
            if (order != null)
            {
                plugins.Sort(order);
            }
        }

        #endregion Public Methods

        #region Private Methods

        private List<T> CreatePlugins()
        {
            List<T> instances = new List<T>();

            foreach (Type type in pluginsFinder.FindPlugins<T>())
            {
                if (!type.GetTypeInfo().IsAbstract)
                {
                    T instance = (T)ActivatorUtilities.CreateInstance(serviceProvider, type, new object[] { });

                    instances.Add(instance);
                }
            }

            return instances;
        }

        #endregion Private Methods

        #region Private Fields

        private readonly IServiceProvider serviceProvider;
        private IConfiguration configuration;
        private ILogger<PluginsManager<T>> logger;
        Comparison<T> order;
        private List<T> plugins;
        IPluginsFinder pluginsFinder;

        #endregion Private Fields
    }
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace node_log_admin.Models
{
    public class ColumnConfiguration
    {
        public string name { get; set; }
        public bool isHidden { get; set; }
        public EnumFilterType filterType { get; set; }

        public ColumnConfiguration()
        {

        }
    }
}


text:
layout: post
title: "Useful Figures in Deep Learning"
date: 2021-06-04 17:29:00 +0800
update: 2022-03-17
categories: 深度学习
mathjax: true
figure: /images/2021/06/figures-polylr.png
author: Jarvis
meta: Post
---

* content
{:toc}

{% include card.html type="info" content="本文代码见 [Github: Jarvis73/CodeForBlog](https://github.com/Jarvis73/CodeForBlog/blob/main/python/20210604-figures-activations.py)" %}

## Activation Functions

* 激活函数

{% include image.html class="polaroid" url="2021/06/figures-activations.png" title="Activation functions." %}

## Softmax 的温度系数

{% include image.html class="polaroid-small" url="2021/06/figures-softmax.png" title="Temperature of Softmax." %}

## Exponential Moving Average (EMA/ema)

{% include image.html class="polaroid" url="2021/06/moving_average.png" title="Exponential Moving Average with different momentum values." %}

## Learning Rate Decay Policy

* Polynomial

```python
def polylr(it, init_lr, max_iter, eta_min=0, gamma=0.9):
    factor = eta_min + (init_lr - eta_min) * (1 - (it - 1) / (max_iter - 1)) ** gamma
    return factor
```

{% include image.html class="polaroid" url="2021/06/figures-polylr.png" title="Polynomial learning rate" %}

* Cosine

```python
def cosinelr(it, init_lr, T_max, eta_min=0):
    factor = eta_min + 0.5 * (init_lr - eta_min) * (1 + np.cos(((it - 1) % T_max) / (T_max - 1) * np.pi))
    return factor
```

{% include image.html class="polaroid" url="2021/06/figures-cosinelr.png" title="Cosine learning rate" %}


text:
package router

import (
	"log"
	"net/http"
)

type Router struct {
	// Configurable Handler to be used when no route matches.
	NotFoundHandler http.Handler
	// Configurable Handler to be used to recover panic in routes.
	PanicHandler http.Handler
	// Routes to be matched, in order.
	routes []*Route
}

// If a panic handler was defined it calls
func (this *Router) handlePanic(w http.ResponseWriter, r *http.Request) {
	if this.PanicHandler != nil {
		err := recover()
		if err != nil {
			log.Println(err)
			this.PanicHandler.ServeHTTP(w, r)
		}
	}
}

func (this *Router) parseRequestParams(r *http.Request, route *Route, matches []string) {
	r.ParseForm()
	if len(matches) > 0 && matches[0] == r.URL.Path {
		for i, name := range route.pathRegexp.SubexpNames() {
			if len(name) > 0 {
				r.Form.Add(name, matches[i])
			}
		}
	}
}

// NewRouter returns a new router instance.
func NewRouter() *Router {
	return &Router{}
}

// NewRoute registers an empty route.
func (this *Router) NewRoute() *Route {
	r := &Route{}
	r.method = "GET"
	this.routes = append(this.routes, r)
	return r
}

func (this *Router) Handle(path string, handler http.Handler) *Route {
	return this.NewRoute().Path(path).Handler(handler)
}

func (this *Router) HandleFunc(path string, f func(http.ResponseWriter,
	*http.Request)) *Route {
	return this.NewRoute().Path(path).HandlerFunc(f)
}

func (this *Router) FindMatchingRoute(r *http.Request) *Route {
	for _, route := range this.routes {
		if route.pathRegexp != nil && route.method == r.Method {
			if matches := route.pathRegexp.FindStringSubmatch(r.URL.Path); matches != nil {
				this.parseRequestParams(r, route, matches)
				return route
			}
		}
	}
	return nil
}

func (this *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	defer this.handlePanic(w, r)

	var handler http.Handler
	var matchingRoute *Route

	if matchingRoute = this.FindMatchingRoute(r); matchingRoute != nil {
		handler = matchingRoute.handler
	}

	if handler == nil {
		handler = this.NotFoundHandler
		if handler == nil {
			handler = http.NotFoundHandler()
		}
	}

	handler.ServeHTTP(w, r)
}


text:
package milo.utils.auth;

import javax.persistence.Column;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.PrePersist;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.xml.bind.DatatypeConverter;
import java.io.Serializable;
import java.security.MessageDigest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

@MappedSuperclass
@NamedQueries({
		@NamedQuery(
				name = AuthUser.FIND_BY_EMAIL,
				query = "SELECT entity FROM User entity WHERE entity.email = :email"
		),
		@NamedQuery(
				name = AuthUser.FIND_BY_CONFIRMATION_DATA,
				query = "SELECT entity FROM User entity WHERE entity.confirmationData = :confirmationData"
		),
})
public abstract class AuthUser implements Serializable {

	protected static Logger LOG = Logger.getLogger(AuthUser.class.getName());
	protected static final long serialVersionUID = 1L;

	public static final String FIND_BY_EMAIL = "User.findByEmail";
	public static final String FIND_BY_CONFIRMATION_DATA = "User.findByConfirmationData";

	@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
	protected Long id;
	@NotNull @NotEmpty @Email
	@Column(unique = true)
	protected String email;

	@Enumerated(EnumType.STRING)
	protected ConfirmationStatusType confirmationStatus;
	protected String confirmationData;
	protected String password;
	protected String passwordRecovery;

	protected LocalDateTime created;
	protected LocalDateTime subscribed;

	public AuthUser() {
	}

	public AuthUser(Long id) {
		this.id = id;
	}

	public AuthUser(String email) {
		setEmail(email);
	}

	@PrePersist
	protected void prePersist() {
		created = LocalDateTime.now();
	}

	protected abstract String passwordHashSalt();

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setCreated(LocalDateTime created) {
		this.created = created;
	}

	public LocalDateTime getCreated() {
		return created;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setPlainPasswordOnPersistedUser(String plainPassword) {
		this.password = countPasswordHash(plainPassword);
	}

	public void setPlainRecoveryPassword(String plainPassword) {
		this.passwordRecovery = plainPassword == null ? null : countPasswordHash(plainPassword);
	}

	public boolean hasRecoveryPassword() {
		return this.passwordRecovery != null && !this.passwordRecovery.isBlank();
	}

	public static String generateRandomText() {
		String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
				+ "0123456789"
				+ "abcdefghijklmnopqrstuvxyz";
		int length = 17 + new Random().nextInt(6);
		StringBuilder sb = new StringBuilder(length);
		for (int i = 0; i < length; i++) {
			int index = (int)(AlphaNumericString.length() * Math.random());
			sb.append(AlphaNumericString.charAt(index));
		}
		return sb.toString();
	}

	public ConfirmationStatusType getConfirmationStatus() {
		return confirmationStatus;
	}

	public void setConfirmationStatus(ConfirmationStatusType confirmationStatus) {
		this.confirmationStatus = confirmationStatus;
	}

	public String getConfirmationData() {
		return confirmationData;
	}

	public void setConfirmationData(String confirmationData) {
		this.confirmationData = confirmationData;
	}

	public boolean isHisPassword(String password) {
		String passwordHashInput = countPasswordHash(password);
		return passwordHashInput.equals(this.password) || passwordHashInput.equals(this.passwordRecovery);
	}

	public void setSubscribed(LocalDateTime subscribed) {
		this.subscribed = subscribed;
	}

	public LocalDateTime getSubscribed() {
		return subscribed;
	}

	private String countPasswordHash(String text) {
		try {
			String dynamicSalt = this.getId().toString();
			String saltedText = dynamicSalt + passwordHashSalt() + text;
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.update(saltedText.getBytes());
			byte[] digest = messageDigest.digest();
			return DatatypeConverter.printHexBinary(digest);
		} catch (Exception ex) {
			LOG.log(Level.SEVERE, "caught ex when hashing text ", ex);
			return LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + "errordigest" + text;
		}

	}

	public enum ConfirmationStatusType {
		SENT,
		SENDING_FAILED,
		EMAIL_VERIFICATION_FAILED,
		CONFIRMED
	}

	@Override
	public String toString() {
		return "User{" +
				"id=" + id +
				", email='" + email + '\'' +
				", confirmationStatus=" + confirmationStatus +
				", created=" + created +
				", subscribed=" + subscribed +
				'}';
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (!(o instanceof AuthUser)) return false;
		AuthUser user = (AuthUser) o;
		return getId().equals(user.getId()) && getEmail().equals(user.getEmail()) && password.equals(user.password);
	}

	@Override
	public int hashCode() {
		return Objects.hash(getId(), getEmail(), password);
	}

}


text:
<?php
abstract class SGPBExtension {

	const SGPB_ADDON_TABLE_NAME = 'sg_popup_addons';
	const SGPB_ADDON_POPUP_CONNECTION_TABLE_NAME = 'sg_popup_addons_connection';

	private $params = array();
	private $popupId;
	private $extensionContent;
	private $extensionOptions;
	private $postData;

	/**
	 * Php magic call method using for setter and getter
	 *
	 * @since 1.0.0
	 *
	 * @param $name setter or getter name
	 * @param  array $args setter params
	 *
	 * @return void
	 *
	 */
	public function __call($name, $args) {

		$methodPrefix = substr($name, 0, 3);
		$methodProperty = lcfirst(substr($name,3));

		if ($methodPrefix=='get') {
			return $this->$methodProperty;
		}
		else if ($methodPrefix=='set') {
			$this->$methodProperty = $args[0];
		}
	}

	/**
	 * Create Extension tables
	 *
	 * @since 2.4.6
	 *
	 * @param int $blogsId
	 *
	 * @return void
	 *
	 */
	public static function createExtensionTables($blogsId) {

		global $wpdb;

		$sgPopupAddon = "CREATE TABLE IF NOT EXISTS ". $wpdb->prefix.$blogsId.SGPBExtension::SGPB_ADDON_TABLE_NAME." (
			`id` int(11) NOT NULL AUTO_INCREMENT,
			`name` varchar(255) NOT NULL UNIQUE,
			`paths` TEXT NOT NULL,
			`type` varchar(255) NOT NULL,
			`options` TEXT NOT NULL,
			`isEvent` TINYINT UNSIGNED NOT NULL,
			PRIMARY KEY (id)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8; ";

		$extensionDataTable = "CREATE TABLE IF NOT EXISTS ". $wpdb->prefix.$blogsId.SGPBExtension::SGPB_ADDON_POPUP_CONNECTION_TABLE_NAME." (
			`id` int(11) NOT NULL AUTO_INCREMENT,
			`popupId` int(11) NOT NULL,
			`extensionKey` TEXT NOT NULL,
			`content` TEXT NOT NULL,
			`extensionType` varchar(255) NOT NULL,
			`options` TEXT NOT NULL,
			PRIMARY KEY (id)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8; ";

		$columInfo = $wpdb->query("SHOW COLUMNS FROM ".$wpdb->prefix.$blogsId.SGPBExtension::SGPB_ADDON_TABLE_NAME." LIKE 'isEvent'");

		if(!$columInfo) {
			$alterQuery = "ALTER TABLE ".$wpdb->prefix.$blogsId.SGPBExtension::SGPB_ADDON_TABLE_NAME." ADD isEvent TINYINT UNSIGNED NOT NULL";
			$wpdb->query($alterQuery);
		}

		$wpdb->query($sgPopupAddon);
		$wpdb->query($extensionDataTable);
	}

	/**
	 * Popup extensions Saved option
	 *
	 * @since 2.4.6
	 *
	 * @param int $popupId
	 * @param string $extensionKey
	 *
	 * @return bool if does not exists data and Object when have saved data
	 *
	 */
	public static function getSavedOptions($popupId, $extensionKey) {

		global $wpdb;

		$prepareSql = $wpdb->prepare("SELECT options FROM ".$wpdb->prefix.SGPBExtension::SGPB_ADDON_POPUP_CONNECTION_TABLE_NAME." WHERE popupId = %d AND extensionKey = %s", $popupId, $extensionKey);
		$savedData = $wpdb->get_results($prepareSql, OBJECT);

		/*When does not have saved data*/
		if(empty($savedData)) {
			return false;
		}

		return $savedData;
	}

	/**
	 * Exists extension options
	 *
	 * @since 2.4.6
	 *
	 * @param int $popupId
	 * @param string $extensionKey
	 *
	 * @return bool if doens not exists data and Object when have saved data
	 *
	 */
	private function isExistOptionData($popupId, $extensionKey) {

		global $wpdb;

		$prepareSql = $wpdb->prepare("SELECT COUNT(*) as count FROM ".$wpdb->prefix.SGPBExtension::SGPB_ADDON_POPUP_CONNECTION_TABLE_NAME." WHERE popupId = %d AND extensionKey=%s", $popupId, $extensionKey);
		$arr = $wpdb->get_row($prepareSql, ARRAY_A);

		return $arr['count'];
	}

	public function save() {

		$popupId = $this->getPopupId();
		$popupContent = $this->getExtensionContent();
		$extensionOptions = $this->getExtensionOptions();
		if(!isset($extensionOptions)) {
			$extensionOptions = '';
		}
		$extensionOptions = @json_encode($extensionOptions, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
		$extensionConnectionTable = SGPBExtension::SGPB_ADDON_POPUP_CONNECTION_TABLE_NAME;
		$extensionKey = static::SGPB_EXTENSION_KEY;
		$extensionType = static::SGPB_EXTENSION_TYPE;
		$saveMode = $this->isExistOptionData($popupId, $extensionKey);

		global $wpdb;

		if($saveMode == 0) {
			$data = array(
				'popupId'=> $popupId,
				'extensionKey'=> $extensionKey,
				'content'=> $popupContent,
				'extensionType'=> $extensionType,
				'options'=> $extensionOptions
			);
			$formats = array('%d', '%s', '%s', '%s', '%s');
			$wpdb->insert($wpdb->prefix.$extensionConnectionTable, $data, $formats);

		}
		else {
			$data = array(
				'content'=> $popupContent,
				'options'=> $extensionOptions
			);
			$formats = array('%s', '%s');
			$whereFormat = array('%d', '%s');
			$where = array(
				'popupId'=> $popupId,
				'extensionKey'=> $extensionKey
			);

			$wpdb->update($wpdb->prefix.$extensionConnectionTable, $data, $where, $formats, $whereFormat);
		}

		return $data;
	}

	public function deleteOption() {

		global $wpdb;

		$popupId = $this->getPopupId();
		$extensionConnectionTable = SGPBExtension::SGPB_ADDON_POPUP_CONNECTION_TABLE_NAME;
		$extensionKey = static::SGPB_EXTENSION_KEY;

		$deleteCondition = array('popupId' => $popupId, 'extensionKey' => $extensionKey);

		$wpdb->delete($wpdb->prefix.$extensionConnectionTable, $deleteCondition, array('%d', '%s'));
	}

	public static function deletePopupFromConnectionById($popupId) {

		global $wpdb;

		$extensionConnectionTable = SGPBExtension::SGPB_ADDON_POPUP_CONNECTION_TABLE_NAME;
		$deleteCondition = array('popupId' => $popupId);
		$wpdb->delete($wpdb->prefix.$extensionConnectionTable, $deleteCondition, array('%d'));
	}

	public static function getExtensionsOptions($popupId, $type = 'option') {

		global $wpdb;
		$extensionData = array();

		$prepareSql = $wpdb->prepare("SELECT extensionKey, options FROM ".$wpdb->prefix.SGPBExtension::SGPB_ADDON_POPUP_CONNECTION_TABLE_NAME." WHERE popupId = %d AND extensionType=%s", $popupId, $type);
		$extensionOptions = $wpdb->get_results($prepareSql, ARRAY_A);

		if(empty($extensionOptions)) {
			return $extensionData;
		}

		foreach($extensionOptions as $extension) {
			$extensionData[$extension['extensionKey']] = json_decode($extension['options'], true);
		}

		return $extensionData;
	}

	public static function getPopupSavedExtensionsKeys($popupId) {

		global $wpdb;

		$prepareSql = $wpdb->prepare("SELECT extensionKey, paths FROM ".$wpdb->prefix."sg_popup_addons LEFT JOIN ".$wpdb->prefix.SGPBExtension::SGPB_ADDON_POPUP_CONNECTION_TABLE_NAME ." ON extensionKey = name  and popupId = %d", $popupId);
		$extensionOptions = $wpdb->get_results($prepareSql, ARRAY_A);

		return $extensionOptions;
	}

	public static function hasPopupEvent($popupId) {

		global $wpdb;

		$prepareSql = $wpdb->prepare("SELECT count(id) as count FROM ".$wpdb->prefix.static::SGPB_ADDON_TABLE_NAME ." WHERE isEvent = 1 AND name in (SELECT extensionKey FROM ".$wpdb->prefix.SGPBExtension::SGPB_ADDON_POPUP_CONNECTION_TABLE_NAME." where popupId = %d)", $popupId);
		$countEvents = $wpdb->get_row($prepareSql, ARRAY_A);
		$count = $countEvents['count'];

		return $count;
	}

}


text:
<?php

namespace DmTest\ServiceManager;

use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\ServiceLocatorInterface;

class ServiceLocatorDummy implements ServiceLocatorInterface
{
    const CREATE_NOTHING = 0;
    const CREATE_CLASS = 1;
    const CREATE_ABSTRACT_CLASS = 2;
    const CREATE_INTERFACE = 3;
    const CREATE_TRAIT = 4;

    /** @var array */
    private $values = [];

    /** @var \PHPUnit_Framework_TestCase */
    private $testCase;

    /**
     * @param \PHPUnit_Framework_TestCase $testCase
     */
    public function __construct(\PHPUnit_Framework_TestCase $testCase)
    {
        $this->testCase = $testCase;
    }

    /**
     * Retrieve a registered instance
     *
     * @param string $name
     * @param mixed  $value
     * @param int    $create
     *
     * @throws ServiceNotFoundException
     * @return mixed
     */
    public function set($name, $value = null, $create = self::CREATE_CLASS)
    {
        if (null === $value && $create != self::CREATE_NOTHING) {
            $mockBuilder = new \PHPUnit_Framework_MockObject_MockBuilder($this->testCase, $name);

            $mockBuilder->disableOriginalConstructor();

            switch ($create) {
                case self::CREATE_ABSTRACT_CLASS:
                    $value = $mockBuilder->getMockForAbstractClass();
                    break;
                case self::CREATE_TRAIT:
                    $value = $mockBuilder->getMockForTrait();
                    break;
                default:
                    $value = $mockBuilder->getMock();
            }
        }

        $this->values[$name] = $value;

        return $this;
    }

    /**
     * Retrieve a registered instance
     *
     * @param string $name
     *
     * @throws ServiceNotFoundException
     * @return mixed
     */
    public function get($name, $create = self::CREATE_CLASS)
    {
        if (!array_key_exists($name, $this->values)) {
            if ($create != self::CREATE_NOTHING) {
                $this->set($name, null, $create);
            } else {
                throw new ServiceNotFoundException();
            }
        }

        return $this->values[$name];
    }

    /**
     * Check for a registered instance
     *
     * @param string|array $name
     *
     * @return bool
     */
    public function has($name)
    {
        return array_key_exists($name, $this->values);
    }
}


text:
{% extends "base.html" %}
{% load markup %}
{% load core_tags %}
{% load pagination_tags %}
{% load thumbnail %}
{% load hitcount_tags %}
{% load i18n %}
{% load author_tags %}

{% block title %}{{ thread.title }}{% endblock %}

{% block extrajs %}
{{ block.super }}

{% include "comments/_ajax_comments.html" %}
{% endblock %}

{% block author_header %}{% with thread.created_by as author %}{% include "_author_header.html" %}{% endwith %}{% endblock %}

{% block about %}

        {{ thread.comment|markdown }}

{% endblock %}

{% block content %}
{% include "comments/_discussion.html" %}
{% endblock %}

{% block sidebar_right %}

Actions

{% if thread.url %}
 {% trans "Go to Homepage" %}
{% endif %}

 {% trans "Print this page" %}

Share this thread

{% trans "Tags" %}

    {% with thread.tags.all as tags %}

            {% include "_tags.html" %}

    {% endwith %}

Related Applications

    {% for result in morelikethis %}
    {% with result.object as mlt_thread %}
    {% if mlt_thread %}

{{ mlt_thread }} {{ mlt_thread.tagline }}

    {% endif %}
    {% endwith %}
    {% empty %}

None.

    {% endfor %}

{% trans "Meta" %}

 Added by {{ thread.created_by.get_full_name }}

{% if thread.edited_by %}

 Latest edit by {{ thread.edited_by.get_full_name }}

{% endif %}
 Updated {{ thread.updated_at|brieftimesince }} ago

{% get_hit_count for thread as hit_count %}
 Viewed {{ hit_count }} time{{ hit_count|pluralize }}

{% endblock %}

{% block modals %}

Share this thread.

  {% with thread as object %}{% include "_social.html" %}{% endwith %}

{% endblock %}