Example #1
0
		private static List<User> ReadUsers(FileInfo dataDirectory, List<NetflixMovie> movies)
		{
			Dictionary<int, List<Preference>> userIDPrefMap =
				new Dictionary<int, List<Preference>>(104395301, 1.0f);
				//new HashMap<Integer, List<Preference>>(15485867, 1.0f);

			int counter = 0;
			FilenameFilter filenameFilter = new FilenameFilter(
                    delegate(File dir, String filename) 
                    {
					    return filename.startsWith("mv_");
					    //return filename.startsWith("mv_000");
				    }
                );
			
			foreach (FileInfo movieFile in new File(dataDirectory, "training_set").ListFiles(filenameFilter)) 
            {
				BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(movieFile)));
				String line = reader.readLine();
				if (line == null) {
					throw new IOException("Can't read first line of file " + movieFile);
				}
				int movieID = int.parse(line.substring(0, line.length() - 1));
				NetflixMovie movie = movies.get(movieID - 1);
				if (movie == null) 
                {
					throw new ArgumentException("No such movie: " + movieID);
				}
				while ((line = reader.readLine()) != null) 
                {
					counter++;
					if (counter % 100000 == 0) 
                    {
						log.Info("Processed " + counter + " prefs");
					}
					int firstComma = line.indexOf((int) ',');
					Int32 userID = Int32.Parse(line.Substring(0, firstComma));
					int secondComma = line.IndexOf((int) ',', firstComma + 1);
					double rating = Double.Parse(line.Substring(firstComma + 1, secondComma));
					List<Preference> userPrefs = userIDPrefMap.get(userID);
					if (userPrefs == null) 
                    {
						userPrefs = new List<Preference>();
						userIDPrefMap.Add(userID, userPrefs);
					}
					userPrefs.Add(new GenericPreference(null, movie, rating));
				}
				IOUtils.quietClose(reader);
			}

			List<User> users = new List<User>(userIDPrefMap.Count);
			foreach (KeyValuePair<int, List<Preference>> entry in userIDPrefMap) 
            {
				users.Add(new GenericUser<int>(entry.Key, entry.Value));
			}
			return users;
		}
Example #2
0
        public Application(TupleList<string, CreateRequestHandler, Dictionary<string, object>> handlers_ = null, string default_host_ = "", List<Func<HTTPRequest, OutputTransform>> transforms_ = null,
                 bool wsgi=false, Dictionary<string, object> settings_=null)
        {
            if (settings_ == null) 
                settings_ = new Dictionary<string, object>();

            if (transforms_ == null)
            {
                transforms = new List<Func<HTTPRequest, OutputTransform>>();
                if (settings_.get("gzip") != null)
                    transforms.Add(r => new GZipContentEncoding(r));
                transforms.Add(r => new ChunkedTransferEncoding(r));
            }
            else
                transforms = transforms_;
            handlers = new TupleList<Regex,List<URLSpec>>();
            named_handlers = new Dictionary<string, object>();
            default_host = default_host_;
            settings = settings_;
            //todo implement
            /*self.ui_modules = {'linkify': _linkify,
                               'xsrf_form_html': _xsrf_form_html,
                               'Template': TemplateModule,
                               }
            self.ui_methods = {}*/
            _wsgi = wsgi;
            /*self._load_ui_modules(settings.get("ui_modules", {}))
            self._load_ui_methods(settings.get("ui_methods", {}))*/
            if (settings.get("static_path") != null)
            {
                var path = settings["static_path"] as string;
                handlers_ = handlers_ ?? new TupleList<string, CreateRequestHandler, Dictionary<string, object>>();
                var static_url_prefix = settings.get("static_url_prefix", "/static/");
                var static_handler_class = settings.get<CreateRequestHandler>("static_handler_class", (app, rq, args) => new StaticFileHandler(app, rq, args));
                var static_handler_args = settings.get("static_handler_args", new Dictionary<string, object>());
                static_handler_args["path"] = path;

                foreach (var pattern in new string[] {Regex.Escape(static_url_prefix) + @"(.*)", 
                                                     @"/(favicon\.ico)", @"/(robots\.txt)"}) 
                {
                    handlers_.Insert(0, Tuple.Create(pattern, static_handler_class, static_handler_args));
                }
            }
            if (handlers_ != null)
                add_handlers(".*$", handlers_);

      
            // Automatically reload modified modules
            if (settings.get("debug") != null && !wsgi)
            {
                //todo implement
                //from tornado import autoreload
                //autoreload.start()
            }
        }
Example #3
0
 public void TestParseLabelsInNestedTree()
 {
     TreeWizard wiz = new TreeWizard( adaptor, tokens );
     CommonTree t = (CommonTree)wiz.Create( "(A (B C) (D E))" );
     var labels = new Dictionary<string, object>();
     bool valid = wiz.Parse( t, "(%a:A (%b:B %c:C) (%d:D %e:E) )", labels );
     Assert.IsTrue( valid );
     Assert.AreEqual( "A", labels.get( "a" ).ToString() );
     Assert.AreEqual( "B", labels.get( "b" ).ToString() );
     Assert.AreEqual( "C", labels.get( "c" ).ToString() );
     Assert.AreEqual( "D", labels.get( "d" ).ToString() );
     Assert.AreEqual( "E", labels.get( "e" ).ToString() );
 }
Example #4
0
 public void TestParseWithWildcardLabels()
 {
     TreeWizard wiz = new TreeWizard( adaptor, tokens );
     CommonTree t = (CommonTree)wiz.Create( "(A B C)" );
     var labels = new Dictionary<string, object>();
     bool valid = wiz.Parse( t, "(A %b:. %c:.)", labels );
     Assert.IsTrue( valid );
     Assert.AreEqual( "B", labels.get( "b" ).ToString() );
     Assert.AreEqual( "C", labels.get( "c" ).ToString() );
 }
Example #5
0
 public User(Dictionary<String, String> dict)
 {
     this.info = dict.;
     this.password = dict.get("password");
     this.nick = dict.get("nick");
 }
Example #6
0
 public void TestParseLabelsAndTestText()
 {
     TreeWizard wiz = new TreeWizard( adaptor, tokens );
     CommonTree t = (CommonTree)wiz.Create( "(A B[foo] C)" );
     var labels = new Dictionary<string, object>();
     bool valid = wiz.Parse( t, "(%a:A %b:B[foo] %c:C)", labels );
     Assert.IsTrue( valid );
     Assert.AreEqual( "A", labels.get( "a" ).ToString() );
     Assert.AreEqual( "foo", labels.get( "b" ).ToString() );
     Assert.AreEqual( "C", labels.get( "c" ).ToString() );
 }
Example #7
0
        public Dictionary<int, int> poll(double timeout)
        {
            var readable = read_fds.ToList();
            var writeable = write_fds.ToList();
            var errors = error_fds.ToList();
          
            Socket.Select(readable, writeable, errors, (int)(timeout * 1000000)); // select in microseconds

            var events = new Dictionary<int, int>();
            foreach(var fd in readable.Select(r => r.fileno()))
                events[fd] = events.get(fd, 0) | IOLoop.READ;
            foreach (var fd in writeable.Select(w => w.fileno()))
                events[fd] = events.get(fd, 0) | IOLoop.WRITE;
            foreach (var fd in errors.Select(e => e.fileno()))
                events[fd] = events.get(fd, 0) | IOLoop.ERROR;
            return events;

        }
 /// <summary>
 /// This <c>write</c> method will write the key value pairs
 /// within the provided map to the specified XML node. This will
 /// write each entry type must contain a key and value so that
 /// the entry can be deserialized in to the map as a pair. If the
 /// key or value object is composite it is read as a root object
 /// so its <c>Root</c> annotation must be present.
 /// </summary>
 /// <param name="node">
 /// this is the node the map is to be written to
 /// </param>
 /// <param name="map">
 /// this is the source map that is to be written
 /// </param>
 /// <param name="mode">
 /// this is the mode that has been inherited
 /// </param>
 public void Write(OutputNode node, Dictionary map, Mode mode) {
    String root = entry.Entry;
    String name = style.GetElement(root);
    for(Object index : map.keySet()) {
       OutputNode next = node.getChild(name);
       Object item = map.get(index);
       next.setMode(mode);
       key.Write(next, index);
       value.Write(next, item);
    }
 }
Example #9
0
        private Dictionary<string, Host> parse(Stream stream)
        {
            var m = new Dictionary<string, Host>();
            var sr = new StreamReader(stream);
            var current = new List<Host>(4);
            string line;

            while ((line = sr.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.Length == 0 || line.StartsWith("#"))
                    continue;

                var regex = new Regex("[ \t]*[= \t]");

                string[] parts = regex.Split(line, 2);
                string keyword = parts[0].Trim();
                string argValue = parts[1].Trim();

                var regex2 = new Regex("[ \t]");
                if (StringUtils.equalsIgnoreCase("Host", keyword))
                {
                    current.Clear();
                    foreach (string pattern in regex2.Split(argValue))
                    {
                        string name = dequote(pattern);
                        Host c = m.get(name);
                        if (c == null)
                        {
                            c = new Host();
                            m.put(name, c);
                        }
                        current.Add(c);
                    }
                    continue;
                }

                if (current.isEmpty())
                {
                    // We received an option outside of a Host block. We
                    // don't know who this should match against, so skip.
                    //
                    continue;
                }

                if (StringUtils.equalsIgnoreCase("HostName", keyword))
                {
                    foreach (Host c in current)
                        if (c.hostName == null)
                            c.hostName = dequote(argValue);
                }
                else if (StringUtils.equalsIgnoreCase("User", keyword))
                {
                    foreach (Host c in current)
                        if (c.user == null)
                            c.user = dequote(argValue);
                }
                else if (StringUtils.equalsIgnoreCase("Port", keyword))
                {
                    try
                    {
                        int port = int.Parse(dequote(argValue));
                        foreach (Host c in current)
                            if (c.port == 0)
                                c.port = port;
                    }
                    catch (FormatException)
                    {
                        // Bad port number. Don't set it.
                    }
                }
                else if (StringUtils.equalsIgnoreCase("IdentityFile", keyword))
                {
                    foreach (Host c in current)
                        if (c.identityFile == null)
                            c.identityFile = toFile(dequote(argValue));
                }
                else if (StringUtils.equalsIgnoreCase("PreferredAuthentications", keyword))
                {
                    foreach (Host c in current)
                        if (c.preferredAuthentications == null)
                            c.preferredAuthentications = nows(dequote(argValue));
                }
                else if (StringUtils.equalsIgnoreCase("BatchMode", keyword))
                {
                    foreach (Host c in current)
                        if (c.batchMode == null)
                            c.batchMode = yesno(dequote(argValue));
                }
                else if (StringUtils.equalsIgnoreCase("StrictHostKeyChecking", keyword))
                {
                    string value = dequote(argValue);
                    foreach (Host c in current)
                        if (c.strictHostKeyChecking == null)
                            c.strictHostKeyChecking = value;
                }
            }

            return m;
        }
Example #10
0
        private void readAdvertisedRefsImpl()
        {
            var avail = new Dictionary<string, Ref>();
            while (true)
            {
                string line;

                try
                {
                    line = pckIn.ReadString();
                }
                catch (EndOfStreamException)
                {
                    if (avail.Count == 0)
                    {
                        throw noRepository();
                    }

                    throw;
                }

                if (line == PacketLineIn.END)
                    break;

                if (avail.Count == 0)
                {
                    int nul = line.IndexOf('\0');
                    if (nul >= 0)
                    {
                        // The first line (if any) may contain "hidden"
                        // capability values after a NUL byte.
                        foreach (string c in line.Substring(nul + 1).Split(' '))
                            remoteCapabilies.Add(c);
                        line = line.Slice(0, nul);
                    }
                }

                string name = line.Slice(41, line.Length);
                if (avail.Count == 0 && name.Equals("capabilities^{}"))
                {
                    // special line from git-receive-pack to show
                    // capabilities when there are no refs to advertise
                    continue;
                }

                ObjectId id = ObjectId.FromString(line.Slice(0, 40));
                if (name.Equals(".have"))
                    additionalHaves.Add(id);
                else if (name.EndsWith("^{}"))
                {
                    name = name.Slice(0, name.Length - 3);
                    Ref prior = avail.get(name);
                    if (prior == null)
                        throw new PackProtocolException(uri, "advertisement of " + name + "^{} came before " + name);

                    if (prior.PeeledObjectId != null)
                        throw duplicateAdvertisement(name + "^{}");

                    avail.put(name, new PeeledTag(Storage.Network, name, prior.ObjectId, id));
                }
                else
                {
                    Ref prior = avail.put(name, new PeeledNonTag(Storage.Network, name, id));
                    if (prior != null)
                        throw duplicateAdvertisement(name);

                }
            }
            available(avail);
        }
Example #11
0
        protected virtual void IssueRecursionWarnings()
        {
            // RECURSION OVERFLOW
            ICollection<int> dfaStatesWithRecursionProblems =
                _stateToRecursionOverflowConfigurationsMap.Keys;
            // now walk truly unique (unaliased) list of dfa states with inf recur
            // Goal: create a map from alt to map<target,IList<callsites>>
            // Map<Map<String target, IList<NFAState call sites>>
            IDictionary<int, IDictionary<string, ICollection<NFAState>>> altToTargetToCallSitesMap =
                new Dictionary<int, IDictionary<string, ICollection<NFAState>>>();
            // track a single problem DFA state for each alt
            var altToDFAState = new Dictionary<int, DFAState>();
            ComputeAltToProblemMaps( dfaStatesWithRecursionProblems,
                                    _stateToRecursionOverflowConfigurationsMap,
                                    altToTargetToCallSitesMap, // output param
                                    altToDFAState );            // output param

            // walk each alt with recursion overflow problems and generate error
            ICollection<int> alts = altToTargetToCallSitesMap.Keys;
            List<int> sortedAlts = new List<int>( alts );
            sortedAlts.Sort();
            //Collections.sort( sortedAlts );
            foreach ( int altI in sortedAlts )
            {
                var targetToCallSiteMap =
                    altToTargetToCallSitesMap.get( altI );
                var targetRules = targetToCallSiteMap.Keys;
                var callSiteStates = targetToCallSiteMap.Values;
                DFAState sampleBadState = (DFAState)altToDFAState.get( altI );
                ErrorManager.RecursionOverflow( this,
                                               sampleBadState,
                                               altI,
                                               targetRules,
                                               callSiteStates );
            }
        }
Example #12
0
 public void TestParseLabels()
 {
     TreeWizard wiz = new TreeWizard( adaptor, tokens );
     CommonTree t = (CommonTree)wiz.Create( "(A B C)" );
     IDictionary<string, object> labels = new Dictionary<string, object>();
     bool valid = wiz.Parse( t, "(%a:A %b:B %c:C)", labels );
     assertTrue( valid );
     assertEquals( "A", labels.get( "a" ).ToString() );
     assertEquals( "B", labels.get( "b" ).ToString() );
     assertEquals( "C", labels.get( "c" ).ToString() );
 }
Example #13
0
            public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
            {
                TLog.d(TAG, "Upgrading database from version {0} to {1}",
                        oldVersion, newVersion);
                ICursor notesCursor;
                List<Dictionary<string, string>> db_list = new List<Dictionary<string, string>>();
                notesCursor = db.Query(DB_TABLE_NOTES, COLUMNS_VERSION[oldVersion - 1], null, null, null, null, null);
                notesCursor.MoveToFirst();

                if (oldVersion == 1) {
                    // GUID and NOTE_CONTENT are not saved.
                    TLog.d(TAG, "Database version {0} is not supported to update, all old datas will be destroyed", oldVersion);
                    db.ExecSQL("DROP TABLE IF Exists notes");
                    onCreate(db);
                    return;
                }

                // Get old datas from the SQL
                while(!notesCursor.IsAfterLast) {
                    Dictionary<string, string> row = new Dictionary<string, string>();
                    for(int i = 0; i < COLUMNS_VERSION[oldVersion - 1].Length; i++) {
                        row.Add (COLUMNS_VERSION[oldVersion - 1][i], notesCursor.GetString(i));
                    }

                    // create new columns
                    if (oldVersion <= 2) {
                        row.Add(Note.TAGS, "");
                    }
                    if (oldVersion <= 3) {
                        row.Add(Note.NOTE_CONTENT_PLAIN, stringConverter.encode(Html.FromHtml(row.get(Note.TITLE) + "\n" + row.get(Note.NOTE_CONTENT)).ToString()));
                    }

                    db_list.Add(row);
                    notesCursor.MoveToNext();
                }

                db.ExecSQL("DROP TABLE IF Exists notes");
                onCreate(db);

                // put rows to the database
                row = new ContentValues();
                for(int i = 0; i < db_list.Count; i++) {
                    for(int j = 0; j < COLUMNS_VERSION[newVersion - 1].Length; j++) {
                        row.put(COLUMNS_VERSION[newVersion - 1][j], db_list.get(i).get(COLUMNS_VERSION[newVersion - 1][j]));
                    }
                    db.Insert(DB_TABLE_NOTES, null, row);
                }
            }