texts:
I'm using io.js. I have to read some files as a stream, process them with the same function, and join them back into a stream. I decided to create my own solution with Transform streams and PassThrough streams.

  "use strict";

  let fs=require('fs');
  let filePaths=['./tmp/h.txt','./tmp/s.txt'];

  let Stream = require('stream');

   class StreamProcessor  {

        constructor() {
       this.process_streams = [];
       }

       push (source_stream) {
         // Create a new Transform Stream
        let transform = new StreamTransformer();
        // Register the finish event and then pipe
        transform.processed = transform.wait.call(transform);
          source_stream.pipe(transform);
       // push the stream to the internal array
         this.process_streams.push(transform);
     }

       done (callback) {
         let streams = this.process_streams;
       // Wait for all Transform streams to finish processing
        Promise.all(
          streams.map(function(s) {return s.processed;}  )
         )
       .then ( function() {
          let combined_stream=new Stream.PassThrough();
         streams.forEach(function (stream) {
         stream.pipe(combined_stream);
       });

       // Call the callback with stream or error
       callback(null,combined_stream);
       })
        .catch(function (err) {
        callback(err);
        });
       }

     }

      class StreamTransformer extends Stream.Transform {

          constructor () {
           // call super
          super();
         }

         _transform(chunk,enc, transformed) {
           // process files here
              let data=chunk.toString();
               data=data.substring(0,data.length-2);
                this.push(data);
                 transformed();
        }

          _flush(flushed) {
              // for additonal at end
              this.push('\n');
               flushed();
             }

       wait() {
          // returns a promise that resolves, when all the data is processed;
           let stream = this;

           return new Promise(function(resolve,reject)  {
       stream.on('finish', function() {
       resolve(true); });
      stream.on('error', function(err) {
         reject(err);  });
      });

       }

    }

         ///  Now simply

          let process_stream = new StreamProcessor();

           filePaths.forEach(function (fpath) {
               let fstream = fs.createReadStream(fpath);
             process_stream.push(fstream);
         });

           process_stream.done(  function
          (err,combined_stream) {
          // Consume the combines stream
          combined_stream.pipe(process.stdout);
           });

Test files contain 'hello' and 'stream, and outputs:

     hell
     stream

I think this can be improved further.


texts: Compare hostnames and leases between CSV and DHCP info

texts:
I have 3 nested loops and they do the following:

Get scopes from DHCP
Get hostnames from csv file
Compares hostnames and leases, matches are added to a hash table

There are 5 DHCP servers, 100's of scopes and thousands of leases.
What would be a good method to speed this up?
$DHServers = Get-DhcpServerInDC #get DHCP info
$hashtable = @{} #create hash table

foreach ($server in $DHServers){
$scopes = Get-DHCPServerv4Scope -ComputerName $server.dnsname #get all scopes in DHCP

    foreach ($_ in (Import-Csv C:\script\Asset_List.csv | Select-Object -ExpandProperty asset)){ #get hostnames from list

        foreach ($scope in $scopes){
            if($scope | Get-DhcpServerV4Lease -ComputerName $server.dnsname | Where-Object HostName -like "$_*" ){ #compares the hostname to find which lease it is in
                $scopename=$scope.name #matches give scope name
                $hashtable[$_] = $scopename #when a match is found, add keys and values to table
            }
        }
    }
}


texts: Limiting function invocations using closures

texts:
Problem statement:

  We have a number of functions in our web application that are getting called too frequently. We'd like you to create a function that, given a function and a time interval, returns a new function that we can call as often as we like, but ensures that the original function is never called more than once per interval.

  Example usage:

limitedDoSomething = rateLimit(doSomething, 500);

  Now calls to limitedDoSomething will simply call doSomething, but never more than once every 500 milliseconds.

Test Cases:

function logHello(){ // just a sample function
  console.log('Hello!');
}

limitedDoSomething = rateLimit(logHello, 500);

limitedDoSomething(); // should log “Hello!”

window.setTimeout(function(){
  limitedDoSomething();  // should not log “Hello!"
}, 400);

window.setTimeout(function(){
  limitedDoSomething();  // should log “Hello!"
}, 500);

My solution:

function rateLimit(func, limit){
  var lastInvokedTimestamp;

  return function(){
    if(typeof lastInvokedTimestamp === 'undefined' || Date.now() - lastInvokedTimestamp >= limit){
      lastInvokedTimestamp = Date.now();
      console.log('go ahead!');
      return func();
    } else {
      console.log('too early!');
      return;
    }
  };

}

// run test cases
function logHello(){ // just a sample function
  console.log('Hello!');
}

limitedDoSomething = rateLimit(logHello, 500);

limitedDoSomething(); // should log “Hello!”

window.setTimeout(function(){
  limitedDoSomething();  // should not log “Hello!"
}, 400);

window.setTimeout(function(){
  limitedDoSomething();  // should log “Hello!"
}, 500);

Equivalent jsBin

I would love to know if there is a way to improve my solution.


texts: (Project Euler #1) Find the sum of all the multiples of 3 or 5 below 1000

texts:
I have decent experience in programming before (mostly C++), but I am very, very, very new to Python, and I decided to try out Project Euler as an exercise.  Here's the description of Problem 1:

  If we list all the natural numbers below 10 that are multiples of 3 or
  5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

  Find the sum of all the multiples of 3 or 5 below 1000.

Here's my solution:

sum = 0
for n in range(0, 1000):
    if n % 3 == 0 or n % 5 == 0:
        sum += n
print(sum)

The program works fine.  What I want to get from a code review:

whether I did anything non-Pythonic;
whether I did anything outdated;
whether I did anything against the PEP 8;
whether there's a better way of doing this;
...


texts: Getting the index (x,y) of a char in a "2D" list in Haskell

texts:
I have this working function circuitFind that I really don't find nice because I feel there is a better and simpler way to accomplish what it does.

circuitFind :: Circuit -> Tile -> Maybe Position
circuitFind c t = unpack . find (\ p -> fst p /= Nothing) . enumerate . map findTile $ c
    where findTile = findIndex (==t)
          enumerate = flip zip [0..]
          unpack a = case a of
             Just (Just x, y) -> Just (x,y)
             _ -> Nothing

Basically, the function works like this: (ie: we search for the char '3')

["1stline", "2ndline", "3rdline"]
              |
        map findTile '3'
              |
              v

[Nothing, Nothing, Just 0]
              |
          enumerate
              |
              v

[(Nothing, 0), (Nothing, 1), (Just 0, 2)]
              |
       find (\ p -> fst p /= Nothing)
              |
              v

      Just (Just 0, 2)
              |
           unpack
              |
              v

     ––  Just (0,2) ––

Is there a better way to make this function more readable/shorter/... ?

All the relevant code here:

import Data.List

type Vec2D = (Int, Int)
type Position = Vec2D
type Circuit = [[Tile]]
type Tile = Char

basicCircuit :: Circuit
basicCircuit = ["------------",
                "-b        e-",
                "------------"]

circuitFind :: Circuit -> Tile -> Maybe Position
circuitFind c t = unpack . find (\ p -> fst p /= Nothing) . enumerate . map findTile $ c
    where findTile = findIndex (==t)
          enumerate = flip zip [0..]
          unpack a = case a of
             Just (Just x, y) -> Just (x,y)
             _ -> Nothing

Some tests to illustrate the usage:

Main> circuitFind basicCircuit 'e'
Just (10,1)
Main> circuitFind basicCircuit 'o'
Nothing


texts: Generic Repository without Entity Framework

texts:
Almost all of generic repository pattern tutorials were making use of EF, so I haven't got much sources to compare my code with. Here is what I have done. Could you please suggest me to fix my wrongs if I have or improve the code?

Note: I am using Dapper.Net as ORM, and I am aware of Dapper.Contrib dll, I just want to implement my own methods.

Generic Repository class

public interface IRepository<T> where T : BaseEntity
{
    IDbConnection Connection { get; set; }
    void Insert(T entity);
    void Delete(T entity);
    T GetById(int id);
    IQueryable<T> GetAll();
}

A Repository Class

public interface ICustomerRepository : IRepository<Customer>
{

}

public class CustomerRepository : ICustomerRepository
{
    public IDbConnection Connection { get; set; }

    public CustomerRepository()
    {
        Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["default"].ConnectionString);
    }

    public void Delete(Customer entity)
    {
        try
        {
            using (Connection)
            {
                Connection.Execute("DELETE FROM dbo.Customer Where Id = @Id", new { entity.Id });
            }
        }
        catch (Exception ex)
        {

        }
    }

    public IQueryable<Customer> GetAll()
    {
        IQueryable<Customer> list = new List<Customer>().AsQueryable();

        try
        {
            using (Connection)
            {
                list = Connection.Query<Customer>("SELECT * FROM dbo.Customer").AsQueryable();
            }
        }
        catch (Exception ex)
        {

        }
        return list;
    }

    public Customer GetById(int id)
    {
        Customer customer = new Customer();
        try
        {
            using (Connection)
            {
                customer = Connection.Query<Customer>("SELECT * FROM dbo.Customer WHERE Id = @Id", new { Id = id }).SingleOrDefault();
            }
        }
        catch (Exception ex)
        {

        }
        return customer;
    }

    public void Insert(Customer entity)
    {
        try
        {
            using (Connection)
            {
                Connection.Execute("INSERT INTO dbo.Customer VALUES(@FullName, @TcNo, @BirthDate)",
                    new
                    {
                        entity.FullName,
                        entity.Tcno,
                        entity.BirthDate
                    });
            }
        }
        catch (Exception ex)
        {

        }
    }
}

An Api Controller

public class CustomerController : ApiController
{
    private IRepository<Customer> _customerRepo;

    public CustomerController()
    {
        _customerRepo = new CustomerRepository(new SqlConnection(ConfigurationManager.ConnectionStrings["default"].ConnectionString));
    }
    [HttpGet]
    [Route("")]
    public IHttpActionResult Get()
    {
        return Json(_customerRepo.GetAll());
    }
}


texts:
Some background before the actual code:

My team needs to develop an optimization library for a specific problem. In order to optimize (pun intended) the projects life-cycle I am trying to write some interfaces that my peers (who are data scientist with small interest and experience in programming) can easily implement. For this reason the only requirement I am looking to fulfill is the readability and extensibility of the code, as it will never go into production.

The optimizer interface:

class ProtonOptimizer(object):
"""
An abstract class defining a common interface to be used by all our future models.
Make sure you comply to the API meaning that your concrete class should:
1) Be constructed by providing the BED values as a 2D np array and the capacity
2) It should return its result by implementing the get_optimum() method
"""
  def __init__(self, BED, capacity = 100, model_name = 'abstract_therapy'):
    raise NotImplementedError("You cannot construct an abstract class")

  def get_optimum():
    """Returns a dictionary (int -> int) from patient ID to fractions"""
    return {}

  def pretty_print():
    """Override to print your models output in a meaningful way for debugging purposes"""
    print("The optimum is " + str(self.get_optimum()))

The sample implementation I provided:

class LPOptimizer(ProtonOptimizer):
"""Concrete implementation of the ProtonOptimizer interface"""
  def __init__(self, BED, capacity = 100, model_name = 'proton_therapy'):
    self._BED = BED
    num_patients, max_fractions_per_patient = BED.shape
    self.patients = [i for i in range(num_patients)]
    self.fractions = [j for j in range(max_fractions_per_patient)]
    self.m = Model(model_name)

    # Set binary decision variables
    self.x = self.m.addVars(num_patients, max_fractions_per_patient, vtype = GRB.BINARY)

    # We can only perform so many proton therapies per week
    self.m.addConstr(quicksum(
        quicksum(self.x[i,j] * self.fractions[j] for j in self.fractions)
        for i in self.patients) <= capacity)
    self.m.update()

    self.optimum = {}

  def _solve(self, debug = False):
    # Set objective
    self.m.setObjective(quicksum(
        self.x[i,j] * self._BED[i,j] for i in self.patients for j in self.fractions),
                        GRB.MAXIMIZE)

    self.m.setParam('OutputFlag', debug)
    self.m.update()
    self.m.optimize()
    if self.m.status == GRB.Status.OPTIMAL:
        solution = self.m.getAttr("x", self.x)
        for i in self.patients:
            for j in self.fractions:
                if(solution[i,j] == 1):
                    self.optimum[i] = j
                    break
    else:
        print("Infeasible model")

  def pretty_print(self):
    solution = self.get_optimum()
    for patient, fractions in solution.items():
        print(("Patient " + str(patient) + " should receive " + str(fractions) + " fractions"))

  def get_optimum(self):
    if not self.optimum:
        self._solve()
    return self.optimum

The actual question:
The goal here is to enable my peers to write other implementations of the ProtonOptimizer but be able to test all of them uniformly later (this is why i demand that every implementation has a get_optimum() function that returns a dict). Is this design actually helping them achieve it? How can I make it as easy for them as possible to contribute?


texts:
I am a newcomer to jQuery and am working on an app that allows the user to paint images. To do that, I've created a color palette, and if a user clicks on a color, the paint brush successfully changes to that color. However, my code looks really ugly. I really want to clean it up.

$(document).ready(function() {
        $('#color-one').click(function() {
          brush.setColor('#990000');
        });
        $('#color-two').click(function() {
        brush.setColor('#CC0000');
    });
        $('#color-three').click(function() {
        brush.setColor('#CC3300');
    });
        $('#color-four').click(function() {
        brush.setColor('#FFCC00');
    });
        $('#color-five').click(function() {
        brush.setColor('#FFFF00');
    });
        $('#color-six').click(function() {
        brush.setColor('#CCFF00');
    });
        $('#color-seven').click(function() {
          brush.setColor('#66FF00');
        });
        $('#color-eight').click(function() {
        brush.setColor('#003300');
    });
        $('#color-nine').click(function() {
        brush.setColor('#33FFCC');
    });
        $('#color-ten').click(function() {
        brush.setColor('#3300CC');
    });
        $('#color-eleven').click(function() {
        brush.setColor('#660033');
    });
        $('#color-twelve').click(function() {
        brush.setColor('#660066');
    });
        $('#color-thirteen').click(function() {
        brush.setColor('#000000');
    });
        $('#color-fourteen').click(function() {
        brush.setColor('#ffffff');
    });
    });
</script>

HTML

I was thinking that the refactored version might look something along the lines of this:

$('#color-one, #color-two, ... color-fourteen).click(function() {
  brush.setColor(this.val);
});

However, I would like to see what this community has to think.


texts:
This is designed to be a simple example of a RPG battle, for a talk I hope to give. The rules:

Alice and Bob both have 5 hit points and inflict 2 damage.
Each turn, Alice attacks Bob, and Bob attacks Alice.
3/4 of attacks hit their target.
1/2 of the attacks that hit their target are blocked, inflicting half damage.
When one character is dead, the game is over.

Important ideas:

Sending the attack and receiving the attack are handled separately.
It's a re-usable API for characters, so game.rb is a single use case. There could be others.

character.rb

class Character

  attr_accessor :name, :hit_points

  def initialize(name, hit_points, hit_chance, block_chance, damage)
    @name = name
    @hit_points = hit_points
    @hit_chance = hit_chance
    @block_chance = block_chance
    @damage = damage
  end

  def attack(other_character)
    puts "#{name} attacks #{other_character.name}"
    if roll(@hit_chance)
      puts "  The attack hits"
      other_character.be_attacked(@damage)
    else
      puts "  The attack missed"
    end
  end

  def be_attacked(damage)
    if roll(@block_chance)
      puts "  It was blocked, half damage done."
      take_damage(damage / 2)
    else
      take_damage(damage)
    end
  end

  def take_damage(damage)
    @hit_points -= damage
    puts "  -#{damage} hit points"
    puts "  #{name} has been defeated" if dead?
  end

  def dead?
    @hit_points <= 0
  end

  private

  def roll(chance)
    rand(100) < chance
  end

end

game.rb

class Game

  def initialize
    @alice = Character.new('Alice', 5, 75, 50, 2)
    @bob = Character.new('Bob', 5, 75, 50, 2)
  end

  def turn
    @alice.attack(@bob)
    @bob.attack(@alice)
    report_status
    sleep 1
  end

  def over?
    (@alice.dead? or @bob.dead?)
  end

  def report_status
    puts ''
    if over?
      puts "The game is over!"
    else
      puts "Alice: #{@alice.hit_points} - Bob: #{@bob.hit_points}"
      puts ''
    end
  end

end

script.rb (executable file)

require_relative 'character.rb'
require_relative 'game.rb'

game = Game.new
game.turn until game.over?

Example output:

Alice attacks Bob
  The attack missed
Bob attacks Alice
  The attack hits
  -2 hit points

Alice: 3 - Bob: 5

Alice attacks Bob
  The attack hits
  -2 hit points
Bob attacks Alice
  The attack missed

Alice: 3 - Bob: 3

Alice attacks Bob
  The attack hits
  -2 hit points
Bob attacks Alice
  The attack hits
  -2 hit points

Alice: 1 - Bob: 1

Alice attacks Bob
  The attack hits
  It was blocked, half damage done.
  -1 hit points
  Bob has been defeated
Bob attacks Alice
  The attack hits
  -2 hit points
  Alice has been defeated

The game is over!


texts: Combining four functions for toggling gallery displays

texts:
Here is my HTML:

            Ken
            Kina

This is some jQuery I set up to have the divs fadeIn/FadeOut. Is there a way to combine all of these together or a more elegant way of writing it? I feel I'm repeating things too much.

    $('.ken-gallery .gallery-home').click(function( e ){
        e.preventDefault();
        $('.ken-gallery').fadeOut(function() {
            $('.artists-home').fadeIn('slow');
        });
    });

    $('.kina-gallery .gallery-home').click(function( e ){
        e.preventDefault();
        $('.kina-gallery').fadeOut(function() {
            $('.artists-home').fadeIn('slow');
        });
    });

    $('.artist-ken').click(function( e ){
        e.preventDefault();
        $('.artists-home').fadeOut(function() {
            $('.ken-gallery').fadeIn('slow');
        });
    });

    $('.artist-kina').click(function( e ){
        e.preventDefault();
        $('.artists-home').fadeOut(function() {
            $('.kina-gallery').fadeIn('slow');
        });
    });


texts:
I am working on a new MVC web application, so the project is in early stages. I have created 4 projects.

Let me explain what they do:

Common --> contains all my DTOs, enums helpers, ViewModels etc.

Web --> simple MVC application

Business --> This contains providers which have an interface and implementation for dependency injection:

DAL --> Contains edmx file and DB Entities

DAL is similar to Business project. It has an IUsersDAL and then UsersDAL implementation.

Do I really need the DAL classes to access the database, or can I just access them from the Business class?
Just for an example I am adding a sample code from UsersProvider.cs:
This what my BusinessProvider is doing:
    private readonly IXPTSecurity _security;
    private readonly IUsersDAL _usersDAL;

    public UsersProvider(IXPTSecurity xptSecurity, IUsersDAL usersDAL)
    {
        _security = xptSecurity;
        _usersDAL = usersDAL;
    }

    public UserSessionModel AuthenticateUser(string userName, string password, ValidationContainer validationContainer)
    {
        var userInfo = _usersDAL.GetUserByUserNamePassword(userName, _security.Encrypt(password));

        if (userInfo == null)
        {
            validationContainer.AddMessage(MessageType.Error, "Login Failed. User name or password is incorrect.");
            return null;
        }

}

And then this is what is happening in the _usersDal.GetUserByUserNamePassword():
// So this is this is what UsersDAL is doing
    public User GetUserByUserNamePassword(string userName, string password)
    {
        return _managerEntities.Users.FirstOrDefault(s => s.sUserName == userName && s.sPassword == password);
    }

As you can see, all I am doing in my DAL classes is a simple LINQ type read from the database. If I do this in the Providers it saves me an extra layer.
It would be nice to get someone else opinion on this before I start coding.
By the way I need a separate Common and Business because this project requires another web application and another 3,4 Windows services, which will be later added using the same Business and Common. But I am not sure on the DAL.


texts:
For a homework assignment for an online class (pretty much learning everything from the internet anyway) we were asked to build an appointment program. This is just a start, however I feel I am definitely over-complicating things and going about this the complete wrong way.
These were the instructions we were given:

Implement a superclass Appointment and sub classes OneTime, Day and Month. An appointment has a description (for example, "Root Canal"), and dates information (you can use Date object or Int Year, Int Month, Int Day). Fill an array of Appointment objects with a mixture of appointments.
Write a method OccursOn inside each of the sub classes that checks whether the appointment occurs on that date (OneTime), day (Day) or month (Month). Ask the user to enter a date to check (for example, 2006 10 5), and ask to user if they want to check against OneTime, Day or Month appointment. Based on what the user selected, OccursOn inside each of the sub class should run and display any matching appointment and associated descriptions.

require 'date'

class Appointment
    attr_accessor :info, :date, :year, :day, :month
    def initialize(info, date)
        @info = info
        @date = Date.parse(date)
    end

    def get_info
        @info
    end

    def get_date
        @date
    end

    def get_year
        @year = date.year
    end

    def get_month
        @month = date.mon
    end

end

class OneTime < Appointment

    def OccursOn(year, month, day)
        puts "The year you entered is #{year}"
        puts "The month you entered is #{month}"
        puts "The day you entered is #{day}"
        appointments.each do |x|
            puts "Month #{x.get_month}"
            if x.get_month == month
                puts x.get_date
                puts x.get_info
            end
        end
    end

end

class Day < Appointment

    def OccursOn(day)
        puts day
    end

end

class Month < Appointment

    def OccursOn(month)
        puts month
    end
end

appointments = []
appointment1 = Appointment.new("Root Canal", "31/10/2017")
appointment2 = Appointment.new("Cavity Filling", "31/12/2017")
appointments << appointment1 << appointment2
puts appointment1.get_date
puts appointment2.get_date
puts appointment1.get_info

onetime1 = OneTime.new("Root Canal", "31-10-2017")

puts "Enter a date to check (for example, 31-10-2017)"
checkdate = gets.chomp
checkdate = Date.parse(checkdate)

puts "Would you like to check against OneTime, Day or Month appointment?"
checkagainst = gets.chomp

if checkagainst == "OneTime" or checkagainst == "onetime"
    onetime1.OccursOn(checkdate.year, checkdate.month, checkdate.day)
elsif checkagainst == "Day" or checkagainst == "day"
    puts "Day" # run day check function
elsif checkagainst == "Month" or checkagainst == "Month"
    puts "Month" # run month check function
else
    puts "Invalid option."
end


texts:
I created a simple script that implements log rotation. It checks the log file size (say, out.log) if it exceeds 5MB limit. If it does then the script copies it to out1.log and clears out.log. But it also copies out1.log to out2.log, etc. So log files are kind of 'shifted' and contents of the last one is discarded. In this implementation I keep only 5 files (initial out.log and out[1-4].log).

I am not a Python programmer, although I find this language extremely useful for such kinds of tasks. I would like my code to be as much idiomatic as possible, so what can I change/improve in my script for this?

from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser

SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5

class LogRotator(object):
    def __init__(self, prefix, suffix):
        self.prefix = prefix
        self.suffix = suffix

    def __str__(self):
        return  "{}[x].{}".format(self.suffix, self.prefix)

    def __touch(self, file_name):
        open(file_name, 'w').close()

    def rotate(self):
        files = ["{}{}.{}".format(self.prefix, x + 1, self.suffix) for x in range(MAX_LOG_FILES_COUNT)]

        [self.__touch(f) for f in files if not exists(f)]

        current_log = "{}.{}".format(self.prefix, self.suffix)
        if (getsize(current_log) < SIZE_5MB):
            return

        files.insert(0, current_log)

        for i in range(len(files) - 1, 0, -1):
            copyfile(files[i-1], files[i])

        self.__touch(files[0])

if __name__ == '__main__':
    parser = ArgumentParser(description="Rotate log files")
    parser.add_argument("-prefix", help="log file prefix")
    parser.add_argument("-suffix", help="log file suffix")

    args = parser.parse_args()

    logRotator = LogRotator(args.prefix, args.suffix)
    logRotator.rotate()


texts: Find and remove duplicate files in one or more directories

texts:
I needed this to clean up a couple of similar music libraries, which also contained some empty files and duplicate files with different names.

# dupFinder.py
import os, sys, shutil, datetime
import hashlib

def findDup(parentFolder):
    # Dups in format {hash:[names]}
    dups = {}
    for dirName, subdirs, fileList in os.walk(parentFolder):
        print('Scanning %s...' % dirName)
        for filename in fileList:
            # Get the path to the file
            path = os.path.join(dirName, filename)
            print(path)
            # Calculate hash
            file_hash = hashfile(path)
            # Add or append the file path
            if file_hash in dups:
                dups[file_hash].append(path)
            else:
                dups[file_hash] = [path]
    return dups

# Joins two dictionaries
def joinDicts(dict1, dict2):
    for key in dict2.keys():
        if key in dict1:
            dict1[key] = dict1[key] + dict2[key]
        else:
            dict1[key] = dict2[key]

def hashfile(path, blocksize = 65536):
    print(path)
    afile = open(path, 'rb')
    hasher = hashlib.md5()
    buf = afile.read(blocksize)
    while len(buf) > 0:
        hasher.update(buf)
        buf = afile.read(blocksize)
    afile.close()
    return hasher.hexdigest()

def handleResults(dict1):
    results = list(filter(lambda x: len(x) > 1, dict1.values()))
    if not 'testrun' in globals():
        backupdir = 'removed_' + str(datetime.datetime.now())
        if not os.path.exists(backupdir):
            os.makedirs(backupdir)
    else:
        backupdir = 'backup_dir_date_now'
    if len(results) > 0:
        print('Duplicates Found:')
        print('The following files are identical. The name could differ, but the content is identical')
        print('^^^^^^^^^^^^^^^^^')
        count = 0
        for result in results:
            for subresult in result:
                if count == 0:
                    print('KEEPING ONE COPY: %s.' % subresult)
                    count += 1
            elif 'testrun' in globals():
                print('\t\t %s WILL BE REMOVED TO: %s' % (subresult, backupdir))
            else:
                print('\t\tREMOVING %s TO %s' % (subresult, backupdir))
                try:
                    shutil.move(subresult, backupdir)
                except shutil.Error:
                    print ('\t\t EXISTS. DELETING.')
                    os.remove(subresult)
                print('___________________')

    else:
        print('No duplicate files found.')

if __name__ == '__main__':
    if len(sys.argv) > 1:
        dups = {}
        if sys.argv[1] == '-t':
            testrun = 1
            folders = sys.argv[2:]
        else: folders = sys.argv[1:]
        for i in folders:
            # Iterate the folders given
            if os.path.exists(i):
                # Find the duplicated files and append them to the dups
                joinDicts(dups, findDup(i))
            else:
                print('%s is not a valid path, please verify' % i)
                sys.exit()
        handleResults(dups)
    else:
        print('Usage: python dupFinder.py [-t] folder or python [-t] dupFinder.py folder1 folder2 folder3')

It can be run in test mode with -t flag.

Wow - awesome advice here sure transformed the code: GitHub Repo


texts:
When the application connects to the database it keeps the connection open until the user closes the file using File->Close.

Is this bad practice?

Should I be opening and closing the connection every time I need to use it?

using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Windows.Forms;

namespace timelord
{
    /// <summary>
    /// Manages the tasks database
    /// </summary>
    class Timesheet
    {
        private string filePath;
        private SQLiteConnection sqlite;
        private SQLiteDataAdapter adapter;
        private SQLiteCommandBuilder builder;
        private DataTable datatable;

        /// <summary>
        /// Creates a timesheet object that determines if the filepath exists.
        /// If the filepath exists then it opens a database.
        /// If it does not exist it creates a database.
        /// </summary>
        /// <param name="filePath">The path to open or create a database file at.</param>
        public Timesheet(string filePath)
        {
            this.filePath = filePath;

            if (File.Exists(filePath))
            {
                openDatabase();

            }else
            {
                createDatabase();

                createSchema();
            }

            prepareQueries();

            this.datatable = new DataTable();

            this.adapter.Fill(this.datatable);
        }

        /// <summary>
        /// Calls the methods that create the database file, connection, and schema
        /// </summary>
        private void createDatabase()
        {
            SQLiteConnection.CreateFile(filePath);

            openDatabase();
        }

        /// <summary>
        /// Creates the database connection
        /// </summary>
        private void openDatabase()
        {
            sqlite = new SQLiteConnection("Data Source=" + this.filePath + ";Version=3;");
            try
            {
                sqlite.Open();
            }
            catch(SQLiteException e)
            {
                MessageBox.Show(e.Message);
            }
        }

        /// <summary>
        /// Creates the schema for all tables nessicary
        /// </summary>
        private void createSchema()
        {
            string query = "CREATE TABLE task (id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT, begindate TEXT, enddate TEXT, status INTEGER default 0)";

            SQLiteCommand cmd = new SQLiteCommand(query, sqlite);

            cmd.ExecuteNonQuery();

        }

        /// <summary>
        /// Prepares the queries used to manipulate the timesheet
        /// </summary>
        private void prepareQueries()
        {
            adapter = new SQLiteDataAdapter("select id,description,begindate,enddate,status from task", sqlite);

            builder = new SQLiteCommandBuilder(adapter);

            adapter.UpdateCommand = builder.GetUpdateCommand();
            adapter.DeleteCommand = builder.GetDeleteCommand();
            adapter.InsertCommand = builder.GetInsertCommand();
        }

        /// <summary>
        /// Return the task table
        /// </summary>
        /// <returns></returns>
        public DataTable Tasks()
        {
            return this.datatable;
        }

        public void Update()
        {
            adapter.Update(datatable);

            datatable.Clear();

            adapter.Fill(datatable);
        }

        /// <summary>
        /// Closes the database connection
        /// </summary>
        public void close()
        {
            sqlite.Close();
        }
    }
}


texts:
I want to write my own version of HttpUtility.ParseQueryString, because it is not included in the .NET Framework Client Profile. My code is pretty straightforward and simple, but I'm looking at the code of HttpUtility.ParseQueryString with Reflector, and is about 200 lines including the code of the called methods.

I don't want to just steal the code specially because there are parts that I have no idea what they are for. Is my code OK or am I missing something (I don't care about performance here)?

Dictionary<string, string> GetParams(string str)
{
    int questionMarkIndex = str.IndexOf('?');
    if (questionMarkIndex != -1)
    {
        string paramsString = str.Substring(questionMarkIndex + 1);
        string[] keyValues = paramsString.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
        Dictionary<string, string> result = new Dictionary<string, string>();

        foreach (string keyValue in keyValues)
        {
            string[] splitKeyValue = keyValue.Split(new char[] { '=' });
            if (splitKeyValue.Length == 2 && !string.IsNullOrWhiteSpace(splitKeyValue[0]))
            {
                string unencodedKey = Uri.UnescapeDataString(splitKeyValue[0]);
                string unencodedValue = Uri.UnescapeDataString(splitKeyValue[1]);
                result[unencodedKey] = unencodedValue;
            }
        }

        return result;
    }
    else return new Dictionary<string, string>();
}


texts: Alien Name Generator (using blocks of consonants and vowels)

texts:
This is my first script in Python, and I am looking for tips and constructive criticism about code style, whether there are more efficient ways of doing things, etc. (not as interested in tips about the method, more so the actual code).

Method:

Choose random no. blocks (between 2 & 6)
Decide whether to start vowel or consonant (random).
Generate all blocks of vowels (random vowels, vowel number between 1 & 3)
Generate all blocks of consonants (as above).
Merge together to create 'name'.

## Alien Name Generator 1.0
## 19/7/15
## Build names using alternating blocks of all consonants or vowels

import math
import random
random.seed(a=None, version=2)
print("Alien name generator:")

# Useful parameters:
NUM_NAMES = 100 # number of names to generate
mx_name_blocks = 5 # max number of blocks that can be used
vbl = 3 # max length of a single vowel block
cbl = 2 # max length of a single consonant block

filenametosave = "namelist5.txt"

## NOTE: Alphabet characters are 97 (a) - 122 (z)
# vowels are (96 +): 1,5,9,15,21
vowel_numbers = [97, 101, 105, 111, 117]
# consonant numbers:
consonant_list = list(range(97,123)) # start with full alphabet
for count2 in vowel_numbers: # remove vowels from alphabet
    consonant_list.remove(count2) # list of consonants

namelist = list() # store names from the main loop

### Main Loop (generates a single word each time):

for count in range (0,NUM_NAMES):

    # Random number of blocks to use:
    n_vcs = random.randint(2,mx_name_blocks)

    # Randomly start vowel or consonant:
    vc_start = int(round(random.random(),0)) # v = 0, c = 1

    # Work out number of vowel or consonant blocks:
    n_v = math.ceil(n_vcs/2) - vc_start*(n_vcs%2) # no. vowels
    n_c = n_vcs-n_v # no. consonants

    ### Generate vowel block list:

    vowel_block_list = list() # store all vowel blocks

    for count0 in range(1,1+n_v): # loop for each block

        vlength = random.randint(1,vbl) # random length of vowel block

        ### Generate single vowel blocks:

        v_block = list() # Store single vowel block

        for count1 in range(1,1+vlength): # loop for each individual vowel
            v1_val = chr(vowel_numbers[random.randint(0,4)]) # random vowel
            v_block.append(v1_val) # create list of vowels in single block

        string_block = ''.join(v_block) # join to make string

        vowel_block_list.append(string_block) # create list of all vowel blocks

    ### Generate consonant block list:

    const_block_list = list() # store all consonant blocks

    for count3 in range(1,1+n_c):
        clength = random.randint(1,cbl) # random length of block

        ### Generate single blocks:

        c_block = list() # store single block

        for count4 in range(1,1+clength):
            c1_val = chr(consonant_list[random.randint(0,20)]) # random character
            c_block.append(c1_val)

        cstring_block = ''.join(c_block)

        const_block_list.append(cstring_block) # list of consonant blocks

    ### Combine vowel and consonant blocks, alternating:

    if n_v > n_c:
        mixed = []
        for i in range(len(vowel_block_list)):
             mixed.append(vowel_block_list[i])
             if i < len(const_block_list):
                mixed.append(const_block_list[i])
    else:
        mixed = []
        for i in range(len(const_block_list)):
             mixed.append(const_block_list[i])
             if i < len(vowel_block_list):
                mixed.append(vowel_block_list[i])

    aName = ''.join(mixed).title()
    namelist.append(aName)

print("\n".join(namelist))

# WRITE TO FILE:
f = open(filenametosave,'w')
f.write("Alien name generator names: \n"+"\n".join(namelist))
f.close()

Example output:

Gque
Iiolyeeejuui
Hacbiieq
Bxau
Jyuu


texts: Show furnitures in a different way considering django principle

texts:
I want in my homepage to show the furnitures items in a following way:

  Newly Arrived Furnitures(upto 10)

  Featured Furnitures

  Free shipping Furnitures (upto 10)

For this I tried to go with the principle of Fat Models, Thin View. Here is my code

class FurnitureQuerySet(models.QuerySet):
    def active(self):
        return self.filter(is_active=True)

    def featured(self):
        return self.active().filter(is_featured=True)

    def new_arrival(self, max_furnitures):
        return self.active().order_by('-id')[:max_furnitures]

    def free_shipping(self, max_furnitures):
        return self.active().filter(is_free_shipping=True)[:max_furnitures]

class FurnitureManager(models.Manager):
    def get_queryset(self):
        return FurnitureQuerySet(self.model, using=self._db)

    def all(self):
        return self.get_queryset().active()

    def featured(self):
        return self.get_queryset().featured()

    def new_arrival(self, max_furnitures):
        return self.get_queryset().new_arrival(max_furnitures)

    def free_shipping(self, max_furnitures):
        return self.get_queryset().free_shipping(max_furnitures)

    def get_by_id(self, slug):
        qs = self.get_queryset().filter(slug=slug)
        if qs.count() == 1:
            return qs.first()
        return None

class Furniture(models.Model):
    name = models.CharField(max_length=100, blank=True, null=True)
    manufacturer = models.ForeignKey(Manufacturer, blank=True, null=True)
    slug = models.SlugField(max_length=200, unique=True)
    is_active = models.BooleanField(
    default=True, help_text='Furniture is available for listing and sale')
    is_featured = models.BooleanField(
        default=False, help_text='Promote this furniture on main pages')
    is_free_shipping = models.BooleanField(
        default=False, help_text='No shipping charges')
    timestamp       = models.DateTimeField(auto_now_add=True)
    objects         = FurnitureManager()

def home(request):
    categories = Category.objects.root_nodes()
    furnitures = Furniture.objects.new_arrival(10)
    featured_furnitures = Furniture.objects.featured()
    shipping_free_furnitures = Furniture.objects.free_shipping(10)
    context = {
        'categories': categories,
        'furnitures': furnitures,
        'featured': featured_furnitures,
        'shipping_free': shipping_free_furnitures,
        }
    return render(request, 'home.html', context)

The template will be:

Newly Arrival

{% for new_furniture in furnitures %}
  {{ new_furniture.name }}
{% endfor %}

Featured

{% for featured_furniture in featured %}
  {{ featured_furniture.name }}
{% endfor %}


texts:
I'd like this code to be improved

Input string: codereview is awesome

Output string: awesome is codereview

My approach:

Reverse the entire string
Reverse each word in a string

package com.arun.reversewords;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReverseWords {

public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter the string");
    String s = br.readLine();

    String rev = StringRev(s);

    System.out.println(rev);

}

private static String StringRev(String s) {

    char[] modString = new char[s.length()];

    for (int i = 0; i < s.length(); i++) {
        modString[i] = s.charAt(s.length() - 1 - i);
    }

    s = s.copyValueOf(modString);
    String reverseWord = "";
    String eachWord;
    for (String part : s.split(" ")) {
        eachWord = new StringBuilder(part).reverse().toString();
        reverseWord = reverseWord + eachWord + " ";
    }

    return reverseWord;

  }

}


texts:
I thought of writing a utility class where I can implement patterns that are useful for day-to-day parsing of strings, without having to to import java.util.regex everywhere. So I wrote this simple class as a model.

This was inspired by a (now deleted) Stack Overflow question:

  For ex: String str="asd14sd67fgh007"; I want the output like: 1467007asdsdfgh

Right now I only added two patterns, with the intention of adding more as the need calls for it. I used regex for these, but other pattern-matching methods could [potentially] also be used, if the need calls for it.

I would be interested to know if there is a smarter way of doing this that I have not thought of, and in general, how can this be improved, to make adding more patterns and methods as easy as possible, keep the code clear and concise, and in general make it better for Mr. or Mrs. Maintainer.

Note: I wrote this using repl.it for the time being, and not actually added to a code base yet, hence the class not being declared as public. That will be rectified once it's added to a code base.

PatternMatcher class

import java.util.regex.*;

class PatternMatcher {

    public String inputString;

   /**
    * Constructor.
    */
    public PatternMatcher(String inputString) {
        this.inputString = inputString;
    }
    /**
     * Get only upper and lower case letters from inputString.
     * @return  String containing the upper and lower case letters from inputString
     */
    public String getLetters() {
        final Pattern LETTERS = Pattern.compile("[A-Za-z]");
        StringBuilder outputBuilder = new StringBuilder();
        Matcher letterMatcher = LETTERS.matcher(inputString);
        while (letterMatcher.find()) {
            outputBuilder.append(letterMatcher.group());
        }
        return outputBuilder.toString();
    }
    /**
     * Get only numbers/digits from inputString.
     * @return  String containing the numbers/digits from inputString
     */
    public String getNumbers() {
        final Pattern NUMBERS = Pattern.compile("[0-9]");
        StringBuilder outputBuilder = new StringBuilder();
        Matcher letterMatcher = NUMBERS.matcher(inputString);
        while (letterMatcher.find()) {
            outputBuilder.append(letterMatcher.group());
        }
        return outputBuilder.toString();
    }
}

Main class (for testing)

class Main {
      public static void main(String[] args) {
          String input = "asd14sd67fgh007";
          String expectedOutput = "1467007asdsdfgh";
          PatternMatcher inputMatcher = new PatternMatcher(input);
          StringBuilder actualOutputBuilder = new StringBuilder();
          actualOutputBuilder.append(inputMatcher.getNumbers());
          actualOutputBuilder.append(inputMatcher.getLetters());
          String actualOutput = actualOutputBuilder.toString();
          if (actualOutput.equals(expectedOutput)) {
              System.out.println("Pattern match succeeded");
          } else {
              System.out.println("Pattern match failed");
          }
      }
}


texts: Logging Entity Framework Changes - improve performance

texts:
I have the following code to log changes using Entity Framework 6.

At the moment it only logs changes to a particular table - but I intend to expand it to cope with any table.  I'm not sure of the best way to extract the table name.

I call this code before Db.SaveChanges.  Db is the DBContext.

private void LogHistory()
    {
        var entries = this.Db.ChangeTracker.Entries()
            .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted)
            .ToList();
        foreach (var entry in entries)
        {
            foreach (string o in entry.CurrentValues.PropertyNames)
            {
                var property = entry.Property(o);
                var currentVal = property.CurrentValue == null ? "" : property.CurrentValue.ToString();
                var originalVal = property.OriginalValue == null ? "" : property.OriginalValue.ToString();

                if (currentVal != originalVal)
                {
                    if (entry.Entity.GetType().Name.Contains("PropetyPair"))
                    {
                        //  make and add log record
                    }
                }
            }
        }
    }


texts:
In use cases where web applications utilize ng-repeat heavily and also rely on event triggers for each item in the collection, such as ng-click and the like, watchers become too numerous and may adversely affect performance when the number of items grow large.

To remedy this problem, an event delegation directive could handle the work. It is unfortunate that AngularJS does not support this as a built-in feature, but perhaps they might include it in a future release. Please review my code example for such a directive and point out any areas for improvement or if there's a better implementation.

Note: This example assumes that the view model (not shown) contains an array of items and a function called doSomething.

Update: Here is a working example in JSBin to illustrate its use.

Markup

{{item.name}}

Code

(function () {
  angular.module('myModule').directive('evtDelegate', ['$parse', delegateDirective]);

  function delegateDirective($parse) {
    return {
      restrict: 'A',
      scope: {
        selector: '='
      },
      link: link
    };

    function link(scope, element, attributes) {
      var selector = attributes.evtSelector,
          expression = attributes.evtDelegate,
          expressionHandler = $parse(expression);

      element.on('click', function (e) {
        var targetNode, targetScope;

        targetNode = getTargetNode(element[0], e.target, selector);

        if (targetNode) {
          targetScope = angular.element(targetNode).scope();

          targetScope.$apply(function () {
            expressionHandler(targetScope);
          });
        }
      });
    }

    function getTargetNode(parent, child, selector) {
      if (!parent || !child) {
        return null;
      }

      if (!selector) {
        return child;
      }

      if (parent === child) {
        // we traveresed all the way up to the parent node, so return
        // null since the selector does not exist as a child
        return null;
      }

      // we cannot trust that child.nodeName is uppercase,
      // so we make sure it is before doing the comparison
      if (child.nodeName.toUpperCase() === selector.toUpperCase()) {
        return child;
      }

      return getTargetNode(parent, child.parentNode, selector);
    }
  }
})();


texts: Converting Confluence markup to a simpler XML format

texts:
I have a code that converts one xml structure to another. I am removing unnecessary tags and replacing some temporarily. I think I have way too many replace operations and wondering if there is a way to optimize this. Can someone pour in suggestions?

Note: I am not using regex to convert xml to plain text. All I am doing using regex is to do away with certain unwanted tags not supported by target xml format, and temporarily replace a few inline elements and replace them back after converting (so that I dont need to parse the inline elements). After sanitizing the string, I am parsing it using jquery xml parser.

Here's the code snippet:

  str = str.replace(/\r?\n|\r/g, ''); //Remove all new line characters
  // Replace <strong> tags to retain and convert them in the end
  str = str.replace(/<strong>/g, '(strong)');
  str = str.replace(/<\/strong>/g, '(/strong)'); //
  // Replace <code> tags to retain and convert them in the end
  str = str.replace(/<code>/g, '(code)');
  str = str.replace(/<\/code>/g, '(/code)');
  // Remove these tags as they arent required
  str = str.replace(/<ac:rich-text-body>/g, '');
  str = str.replace(/<\/ac:rich-text-body>/g, '');
  // Remove 'ac:' from macros elements
  str = str.replace(/ac:/g, '');
  str = str.replace(/<\/*span.*?>/g, ''); //Remove all span tags
  str = str.replace(/<\/*div.*?>/g, ''); //Remove all div tags
  str = str.replace(/<br.*?>/g, ''); //Remove br tags
  str = str.replace(/&nbsp;/g, ''); //Remove non-breaking white spaces
  str = str.replace(/<\/*a.*?>/g, ''); //Remove a tags
  str = str.replace(/<\/*u>/g, ''); //Remove u tags
  str = str.replace(/<\/*em>/g, ''); //Remove em tags


texts:
I am solving another problem in leetcode.

  Given a set, generate all the subsets of the set.

  Input: [1,2,3]
  Output: [ [3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[]]

The solution is accepted but I would like to improve on my C coding style. I have created a node_t type to encapsulate the data related each subset. There could be less verbose ways to do this. Two popular methods of solving this problem seem to be:

backtracking
a subset equals to binary number ≤ n where n is element count.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node_s {
    int *array;
    int  size;
    struct node_s *next;
} node_t;

node_t* alloc_node()
{
    return (node_t*)malloc(sizeof(node_t));
}

void free_node(node_t *node)
{
    free(node);
}
#define SIZE 3

void print_one(int *one)
{
    printf("%d ", *one);
}
void print_1d_array(int *array, int size)
{
    for(int i = 0; i < size; i++) {
        print_one(&array[i]);
    }
    printf("\n");
}
void print_2d_array(int **array, int *col_sizes, int size)
{
    for (int i = 0; i < size; i++) {
        print_1d_array(array[i], col_sizes[i]);
    }
}
int** create_solution_array(node_t *head, int ans_size, int **column_sizes)
{
    int **ans = (int**)malloc(sizeof(int*)*ans_size);

    node_t *iter;
    iter = head->next;
    int idx = 0;
    while(iter) {
        ans[idx] = iter->array;
        (*column_sizes)[idx] = iter->size;
        idx++;
        iter = iter->next;
    }
    return ans;
}

void gen_powerset(int *num, int num_size, int idx, int cur_size, int *cur, node_t **iter_node, int *ans_size) {
    if (idx == num_size) {
        node_t *new_node = alloc_node();
        if (cur_size) {
            new_node->array = (int *) malloc(sizeof(int) * cur_size);
            new_node->size = cur_size ;
            memcpy(new_node->array, cur, cur_size*sizeof(int));
        }
        (*iter_node)->next = new_node;
        *iter_node = new_node;
        (*ans_size)++;
        return;
    }

    gen_powerset(num, num_size, idx + 1, cur_size, cur, iter_node, ans_size);
    *(cur + cur_size) = num[idx];
    gen_powerset(num, num_size, idx + 1, cur_size + 1, cur, iter_node, ans_size);
}

int** subsets(int* nums, int numsSize, int** columnSizes, int* returnSize) {
    node_t  *head = alloc_node();
    node_t  *iter = head;
    int     *cur = (int*)malloc(sizeof(int)*numsSize);
    gen_powerset(nums, numsSize, 0, 0, cur, &iter, returnSize);
    return create_solution_array(head, *returnSize, columnSizes);
}

int main ()
{
    int *nums = (int*)malloc(sizeof(int)*SIZE);
    for (int i = 0; i < SIZE; i++) {
        nums[i] = i+1;
    }
    int *col_sizes = (int*)malloc(sizeof(int)*SIZE);
    int ans_size;
    int ** ans = subsets(nums, SIZE, &col_sizes, &ans_size);
    print_2d_array(ans, col_sizes, ans_size);
    return 0;
}


texts: Search through JavaScript object using a whitelist/filterlist

texts:
I am trying to build a small search function where I can search through some JavaScript objects even if there are arrays in it. I got it working but I would like to ask if someone can find a better approach than the one I made. Also, I assume I have some cases I did not think of.

My current search function looks as follows:

function searchFor(needle, haystack, filter, inside){
    if (inside == undefined) inside = 1;
    if (filter){
        for (var c = 0; c < filter.length; c++) {
            var splittedFilter = filter[c].split('.');
            for (var s = 0; s < splittedFilter.length; s++) {
                var obj = haystack[splittedFilter[s]];
                if (obj){
                    if (Array.isArray(obj) && obj.length > 0){
                        for (var i = 0; i < obj.length; i++) {
                            var result = searchFor(needle, obj[i], [splittedFilter[inside]], inside+1);
                            if (result) return true;
                        }
                    } else if( typeof obj == "object"){
                        var result = searchFor(needle, obj, [splittedFilter[inside]], inside+1);
                        if (result) return true;
                    } else {
                        if (obj.toLowerCase().indexOf(needle.toLowerCase()) != -1){
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false
}

The filterlist:

var filter = ['company', 'friends.name'];

The haystack:

var hay =  [
  {
    "id": "57db96f4acfaec3218383063",
    "name": {
      "first": "Nora",
      "last": "Cooke"
    },
    "company": "MITROC",
    "email": "nora.cooke@mitroc.com",
    "friends": [
      {
        "id": 0,
        "name": "West Duke"
      },
      {
        "id": 1,
        "name": "Williams Kelley"
      },
      {
        "id": 2,
        "name": "Amelia Kirk"
      }
    ]
  },
  {
    "id": "57db96f4d5cae409054d3a5b",
    "name": {
      "first": "Dickson",
      "last": "Moses"
    },
    "company": "VISUALIX",
    "email": "dickson.moses@visualix.org",
    "friends": [
      {
        "id": 0,
        "name": "Patty Carr"
      },
      {
        "id": 1,
        "name": "Bowers Wilkerson"
      },
      {
        "id": 2,
        "name": "Fox Kidd"
      }
    ]
  }
];

The execution of the search function:

hay.forEach(function(element) {
    if (searchFor('Duke', element, filter)) {
        document.getElementById('result').innerHTML = document.getElementById('result').innerHTML + '<br />' + JSON.stringify(element);
    }
}, this);

needle is the string to search for
haystack is one object
filter is an array of filters, like: var filter = ['company'];
inside is to determine how far in the recursive loop the function is

I made a live example here.


texts:
For the given below exercise:

  Here is one method to check if a number is prime:

def is_prime(n):
    k = 2
    while k < n:
        if n % k == 0:
            return False
        k += 1
    return True

  How does this function work?

  This is a decent way of testing if a number is prime, but looping k all the way to n
  might be a bit cumbersome. As a little bonus question, can you think of a better place
  to stop?

  Using the is_prime function, fill in the following procedure, which generates the nth
  prime number. For example, the 2nd prime number is 3, the 5th prime number is 11,
  and so on.

def nth_prime(n):

Below is the solution:

def is_prime(n):
    def f(n, k):
        if k > (n // 2):
            return True
        elif n % k == 0:
            return False
        else:
            return f(n, k + 1)
    return f(n, 2)

def nth_prime(n):
    def traverse_for_nth_prime(count, k):
        if count == n:
           return k-1
        elif is_prime(k):
            return traverse_for_nth_prime(count + 1, k + 1)
        else:
            return traverse_for_nth_prime(count, k + 1)
    return traverse_for_nth_prime(0, 2)

print(nth_prime(20))

Above code is written in functional paradigm. Can it be improved, especially in relation to being optimised from recursion depth limit perspective, by strictly following functional paradigm?


texts: Better performance in read/write data structure + byte

texts:
I am writing a program to compress an image using Huffman encoding and I need to write the data structure and the byte file. After that, I have to read/decode it. I realize that my read and write is very slow.

I think in writetree() I need to write either width, height, compress bits and the tree data structure. I am not sure we can combine it or not.

And another part, I think I use too many for loop so it is very slow in a very long string

from PIL import Image
import numpy as np
import json
import sys, string

trim = ('0', ('127', '255'))
width = 4
height = 4
longstring = "1100100111001101011110010011010110101111001001101011010111100100110101101011"

def decode (tree, str) :
    output = ""
    list = []
    p = tree
    count = 0
    for bit in str :
        if bit == '0' : p = p[0]     # Head up the left branch
        else          : p = p[1]     # or up the right branch
        if type(p) == type("") :
            output += p              # found a character. Add to output
            list.append(int(p))
            p = tree                 # and restart for next character

    return list

def writetree(tree,height, width,compress):
    with open('structure.txt', 'w') as outfile:
        json.dump(trim, outfile)
        outfile.close()
    f = open("info.txt", "w")
    f.write(str(height)+"\n")
    f.write(str(width)+"\n")
    f.write(str(compress)+"\n")
    f.close()

def readtree():
    with open('structure.txt') as json_file:
        data = json.load(json_file)

    k = open("info.txt", "r")
    heightread = k.readline().strip("\n")
    widthread = k.readline().strip("\n")
    compressread = k.readline().strip("\n")
    json_file.close()
    k.close()
    return tuple(data), int(heightread), int(widthread), int(compressread)

def writefile():
    print("Write file")
    with open('file', 'wb') as f:
        bit_strings = [longstring[i:i + 8] for i in range(0, len(longstring), 8)]
        byte_list = [int(b, 2) for b in bit_strings]
        print(byte_list)
        realsize = len(bytearray(byte_list))
        print('Compress number of bits: ', len(longstring))
        writetree(trim,height,width,len(longstring))
        f.write(bytearray(byte_list))
        f.close()
def readfile():
    print("Read file")
    byte_list = []
    longbin = ""
    with open('file', 'rb') as f:
        value = f.read(1)
        while value != b'':
            byte_list.append(ord(value))
            value = f.read(1)
    print(byte_list)

    for a in byte_list[:-1]:
        longbin = longbin + '{0:08b}'.format(a)
    trim_read, height_read, width_read , compress_read = readtree()
    sodu = compress_read%8

    '''
    because the this string is split 8 bits at the time, and the current compress_read is 76
    so the sodu is 4. I have to convert the last byte_list into 4bits not 8 bits
    '''

    if sodu == 0:
        longbin = longbin + '{0:08b}'.format(byte_list[-1])
    elif sodu == 1:
        longbin = longbin + '{0:01b}'.format(byte_list[-1])
    elif sodu == 2:
        longbin = longbin + '{0:02b}'.format(byte_list[-1])
    elif sodu == 3:
        longbin = longbin + '{0:03b}'.format(byte_list[-1])
    elif sodu == 4:
        longbin = longbin + '{0:04b}'.format(byte_list[-1])
    elif sodu == 5:
        longbin = longbin + '{0:05b}'.format(byte_list[-1])
    elif sodu == 6:
        longbin = longbin + '{0:06b}'.format(byte_list[-1])
    elif sodu == 7:
        longbin = longbin + '{0:07b}'.format(byte_list[-1])

    print(longbin)
    print("Decode/ show image:")
    pixels = decode(trim_read, longbin)
    it = iter(pixels)
    pixels = list(zip(it,it,it))
    #print(pixels)

    image_out = Image.new("RGB", (width_read, height_read))
    image_out.putdata(pixels)
    #image_out.show()

writefile()
readfile()


texts: Project Euler # 95 in Haskell - kind-of finding longest cycle in a graph

texts:
I am trying to solve Project Euler #95 in Haskell, though I have already solved it in Java (see). I found that writing imperatively, we can easily do the task this way:

Pseudocode

Define cache as an int array.
Find all perfect numbers and store them in a HashSet.
For each number create a new HashSet for terms (named terms) that have already occured while iterating.
If the number is prime or perfect skip it (and set cache value to -1)
Now iterate using sum of proper divisors, if we stumble upon a number that is out of bounds or has cache value -1 skip this too and set this number's cache value as -1 too. Iterate until we reach 1 or a perfect number or the number itself; keep adding to terms.
If we reach the number, get the size of terms, otherwise (1 or perfect number) set cache value to -1.
Keep record of number with maximum terms size.

Now thinking of functionally writing this solution I find it quite difficult to maintain something as cache, I have to iterate for each number. Also, my mind was quite stuck to this imperative solution so the Haskell equivalent was not quite different or unique. How can I improve it? (It is also quite slower - 32s)

95.hs

import           Control.Arrow
import qualified Data.IntSet   as Set
import           Data.List
import           Data.Ord
import           Divisors

lim :: Int
lim = 1000000

main :: IO ()
main = print . fst . maximumBy (comparing snd) . map (id &&& amicableChainLength) $ [2..lim]
    where
        amicableChainLength x
            | sumOfProperDivisors x == x = 1 -- perfect numbers
            | otherwise = (\((_, list), _) ->
                case elemIndices (last list) list of
                    -- Two occurences
                    (a:b:_) -> b - a
                    -- Cannot occur but anyways
                    _       -> 0
                )
                -- Take first element where include stopped
                . head . dropWhile snd . scanl (include x)
                -- ((Set of numbers observed, Ordered list of them), Continue?)
                ((Set.empty, []), True)
                $ iterate sumOfProperDivisors x
        -- tries to include number into set/list and also tell when to stop
        include x ((set, list),_) n
            -- number is member of observed numbers
            | Set.member n set =
                if x==n
                    -- chain should start at that number
                    then ((set, list ++ [n]), False)
                    -- otherwise no meaning
                    else ((Set.empty, []), False)
            -- Out of bounds or an number earlier encountered, this cannot be answer
            -- since we are required to find minimum element of that chain
            | n > lim || n < 2 || n < x = ((Set.empty, []), False)
            -- New number, add it & continue
            | otherwise = ((Set.insert n set, list ++ [n]), True)

Divisors.hs

module Divisors where

sumOfProperDivisors :: Int -> Int
sumOfProperDivisors n = subtract n . (\s -> if isPerfectSquare n then s - root else s)
    . sum . map (\x -> x + n `div` x) . filter (\x -> n `mod` x == 0)
    . takeWhile (\p -> p*p <= n) $ [1..]
    where
        root = round (sqrt . fromIntegral $ n :: Double)
        isPerfectSquare x = root * root == x


texts:
What troubles me are the empty /**/ brackets. Is there a cleaner way of handling this logic?

if (opcode == OP_0) { /* continue */}
    else if (opcode == OP_1NEGATE) { /* continue */}
    else if (opcode >= OP_1 && opcode <= OP_16) { /* continue */}
    else { throw new Exception("decodeFromOpN called on non OP_N opcode"); }


texts: Repository pattern with plain old PHP object

texts:
I've been reading a lot about repository patterns these days. At first, the pattern seemed easy.

Most of the examples I read over the web use an ORM like below.

interface MemberRepositoryInterface {
    public function find($id);
}

class MemberRepository implements MemberRepositoryInterface {

    // here we're passing framework sepific ORM implementation
    public function __construct(Model_Member $model)
    {
        $this->model = $model;
    }

    public function find($id)
    {
        return $this->model->find($id);
    }

}

// In a controller

class Test_Controller {

    public function __construct(MemberRepositoryInterface $memberRepository)
    {
        $this->repository = $memberRepository;
    }

    public function show()
    {
        $member = $this->repository->find(1);

        $this->template->content = View::factory("testView")->set('member', $member)->render();
    }

}

It looks fine up to this point. Now..when you pass $member variable to a view then you can access all the framework specific methods of the ORM such as...delete(), save(), and so on.

what's the best way to prevent people from using framework specific ORM methods?

I thought about it..then the answer is to use a plain old PHP object.

// Modified Repository

class MemberModel {

    public $id;
    public $firstName;
    public $lastName;

    public function __construct(array $data)
    {
        foreach($data as $key=>$value)
        {
            if(isset($this->$key))
            {
                $this->$key = $value;
            }
        }
    }
}

class ContactModel {
    public $id;
    public $firstName;
    public $lastName;

    public function __construct(array $data)
    {
        foreach($data as $key=>$value)
        {
            if(isset($this->$key))
            {
                $this->$key = $value;
            }
        }
    }
}

class MemberRepository implements MemberRepositoryInterface {

    // here we're passing framework sepific ORM implementation
    public function __construct(Model_Member $model)
    {
        $this->model = $model;
    }

    public function find($id)
    {
        $record =  $this->model->find($id)->as_array();
        return new MemberModel($record);
    }

    public function find_with_contacts($id)
    {
        //find a member and contacts then use MemberModel and ContactModel
        // then return the data
    }

}

Is this  good enough? or are there other best practices?


texts: Script for saving/sorting/sending emails based on their subject

texts:
I've spent a couple days making this script in Outlook that checks my emails and saves/sorts/sends a reply based on the subject. This also happens to be my first experience in working with VBA. Now that I have it working, I was wondering how I could simplify it or "make it prettier" (I always got docked points in my C++ class for having "ugly code").

DOC and PDF are unrelated to .doc and .pdf. They're just my stupid renaming of what are actually two different types of names for .pdf files.

Public Sub saveattachtodisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim saveName As String
Dim whatIs As String
Dim emailAttach As String
Dim attName As String
saveName = itm.Subject
whatIs = retArray(saveName)
saveFolder = "C:\Users\USER\Desktop\Staging"
For Each objAtt In itm.Attachments
    If whatIs = "PDF" Then
        objAtt.SaveAsFile saveFolder & "\" & UCase(saveName) & ".pdf"
ElseIf whatIs = "DOC" Then
    objAtt.SaveAsFile saveFolder & "\" & "DOC 14-" & stripNums(saveName) & ".pdf"
    emailAttach = saveFolder + "\" + "DOC 14-" + stripNums(saveName) + ".pdf"
    attName = stripNums(saveName)
    sendDOC emailAttach, attName
ElseIf whatIs = "other" Then
    objAtt.SaveAsFile saveFolder & "\" & saveName & ".pdf"
End If
objAtt = Nothing
Next
End Sub

'Pulls numbers from DOC type document
Function stripNums(s As String) As String
Dim saveName As String
Dim i As Integer
For i = 1 To Len(s)
    If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then
        saveName = saveName + Mid(s, i, 1)
    End If
Next
stripNums = saveName
End Function

'Checks to see if document is PDF type docement
Function isPDF(s As String) As Boolean
Dim retVal As Boolean
retVal = False
For i = 1 To Len(s)
If Mid(s, i, 2) = "1f" Then
    retVal = True
End If
Next
isPDF = retVal
End Function

'Checks to see if document is of type PDF, DOC, or other
Function retArray(s As String) As String
Dim retVal As String
Dim isItPDF As Boolean
retVal = "other"
arr = Array("t 1", "t 2", "t 3", "t 4", "t 5", "t 6", "t 7", "t 8", "t 9", "t 0")
isItPDF = isPDF(s)
If isItPDF = False Then
For j = 0 To 9
    If InStr(1, s, arr(j), 1) = 1 Then
        retVal = "DOC"
    End If
Next
ElseIf isItPDF = True Then
    retVal = "PDF"
End If
    retArray = retVal
End Function

'Automatically creates and sends an email for DOC type documents
Function sendDOC(s As String, n As String)
Dim objMsg As MailItem
Set objMsg = Application.CreateItem(olMailItem)
objMsg.Recipients.Add ("SEND.TO@EMAIL.COM")
objMsg.Recipients.Add ("DOUBLE.CHECK@EMAIL.COM")
objMsg.Subject = "DOC 14-" & n
objMsg.Body = "Please see attached." & vbCrLf & vbCrLf & vbCrLf & "SIGNATURE BLOCK"
objMsg.Attachments.Add (s)
objMsg.Send
End Function


texts:
Please review this code, considering I put emphasis on constraining space complexity at the expense of execution speed (accepting additional branching).

Can you see cases that prove the algorithm incorrect?
Could you improve its execution speed without increasing space complexity?
Could you make it more readable?

Method signature from Codility (i.e. strange capitalised parameter name).

Definition of "magnitude pole": available online (e.g. https://stackoverflow.com/questions/15397637/interview-find-magnitude-pole-in-an-array)

import java.util.Arrays;
public class MagnitudePole {
  public int solution(int[] A) {
    final int notFound = -1;
    final int minimumArrayLength = 2;
    if (A.length < minimumArrayLength) {
      return notFound;
    }
    int magnitudePole = 0;
    boolean isPoleValid = true;
    int highestSeen = A[magnitudePole];
    for (int i = 1; i < A.length; i++) {
      final int currentValue = A[i];
      if (currentValue < A[magnitudePole]) {
        isPoleValid = false;
      } else if (currentValue > A[magnitudePole]) {
        if (currentValue > highestSeen && !isPoleValid) {
          isPoleValid = true;
          magnitudePole = i;
        }
      } else {
        if (currentValue < highestSeen) {
          isPoleValid = false;
        } else {
          isPoleValid = true;
          magnitudePole = i;
        }
      }
      highestSeen = Math.max(currentValue, highestSeen);
    }
    return isPoleValid ? magnitudePole : notFound;
  }
}


texts:
I would like a quick review on my method to delete a node from a singly linked list. I'm fairly certain that this is not near optimally done, and would love to get feedback.

It uses a SinglyLinkedNode element as the node:

public class SinglyLinkedNode {
    private SinglyLinkedNode next = null;
    int data;
    public SinglyLinkedNode(int data){
        this.data=data;
    }
    public int getData(){
        return this.data;
    }
    public void setNext(SinglyLinkedNode next){
        this.next = next;
    }
    public SinglyLinkedNode getNext(){
        return this.next;
    }
}

The element definitions and the delete function are as so:

import data_structures.LinkedLists.SinglyLinkedNode;

public class SinglyLinkedList{
    private SinglyLinkedNode head = null;
    private SinglyLinkedNode tail = null;
    private int length = 0;
    public SinglyLinkedList(int data){
        this.head = new SinglyLinkedNode(data);
        this.tail = this.head;
        this.length = 1;
    }

    public void delete(int data){
        SinglyLinkedNode n = this.head;
        if (n==null){
            return;
        }
        if (n.getData()==data){//If the head is the data we want
            if (n.getNext()==null){//If it's the only node, null the head and tail
                this.tail = null;
                this.head = null;
                this.length = 0;
            }
            else {//Or move the head to the next node
                this.head = n.getNext();
                this.length--;
            }
            return;
        }
        while (n.getNext()!=null && n.getNext().getData()!=data){//Get the data node or the last node at n.next
            n=n.getNext();
        }
        if (n.getNext() == null){//If n is the last element in the list
            if (n.getData()!=data){//If data wasn't in array then we're done
                return;
            }
            //If we're deleting the only remaining element
            if (this.length <=1){
                this.tail = null;
                this.head = null;
                this.length=0;
            }
            else {//If we're just moving the tail
                n.setNext(null);
                this.tail = n;
                this.length--;
            }
        }
        else {//If n is not the last element
            if (n.getNext().getNext()==null){//If n.next is the last element
                n.setNext(null);
                this.tail = n;
                this.length--;
            }
            else {//If n.next is not the last element
                n.setNext(n.getNext().getNext());
                this.length--;
            }
        }
    }
}

As you can see, this is quite a lot of lines of code - 46 to be exact. In particular, the section below the while loop seems redundant - but I can't figure out how to replace any of it.


texts:
This is designed for a high performance complex log analyzer. Very simple idea: read a file line-by-line as fast as possible.

I would appreciate any hints what should/could be improved in this code.

GitHub

FastLineReader.h

/* Copyright (c) 2015 Simon Toth kontakt@simontoth.cz
 * Lincensed under the MIT license: http://opensource.org/licenses/MIT
 */

#ifndef FASTLINEREADER_H
#define FASTLINEREADER_H

// STD C++
#include <iosfwd>

/** Quick line-by-line parser of text files for POSIX/Linux
 *
 *  This function provides a fast line parser with a callback model.
 *
 * @param filename file to be parsed
 * @param callback function that will be called for each line
 * @returns 0 on success, -1 if file could not be opened
 **/
int fastLineParser(const char * const filename, void (*callback)(const char * const, const char * const));

#endif // FASTLINEREADER_H

FastLineReader.cpp

#include "FastLineReader.h"

// POSIX
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

// C++ STD
#include <iostream>
#include <cstring>
using namespace std;

int fastLineParser(const char * const filename, void (*callback)(const char * const, const char * const))
{
    int fd = open(filename, O_RDONLY); // open file
    if (fd == -1)
    {
        cerr << "Could not open \"" << filename << "\" for reading (" << strerror(errno) << ")." << endl;
        return -1;
    }

    struct stat fs;
    if (fstat(fd, &fs) == -1)
    {
        cerr << "Could not stat \"" << filename << "\" for reading (" << strerror(errno) << ")." << endl;
        close(fd);
        return -1;
    }

    posix_fadvise(fd,0,0,1); // announce the desire to sequentialy read this file
    // silent error handling - weak error

    char *buf = static_cast<char*>(mmap(0, static_cast<size_t>(fs.st_size), PROT_READ, MAP_SHARED, fd, 0));
    if (buf == MAP_FAILED)
    {
        cerr << "Could not memory map file \"" << filename << "\" (" << strerror(errno) << ")." << endl;
        close(fd);
        return -1;
    }

    char *buff_end = buf + fs.st_size;
    char *begin = buf, *end = NULL;

    // search for newline in the remainder in the file
    while ((end = static_cast<char*>(memchr(begin,'\n',static_cast<size_t>(buff_end-begin)))) != NULL)
    {
        callback(begin,end);

        if (end != buff_end)
            begin = end+1;
        else
            break;
    }

    // enable if you are working with malformed text files, proper text file needs to end with a newline
#ifdef MALFORMED_TEXFILE
    callback(begin,buff_end);
#endif

    munmap(buf, static_cast<size_t>(fs.st_size));
    // silent error handling - weak error

    close(fd);
    return 0;
}


texts: Optimizing this 'print up to the nth prime number' script

texts:
I've been seeking to optimize this algorithm, perhaps by eliminating one of the loops, or with a better test to check for prime numbers.

I'm trying to calculate and display 100000 prime numbers has the script pausing for about 6 seconds as it populates the list with primes before the primes list is returned to the console as output.

I've been experimenting with using

print odd,

to simply print every found prime number, which is faster for smaller inputs like n = 1000, but for n = 1000000 the list itself prints much faster (both in the Python shell and in the console).

Perhaps the entire code/algorithm should be revamped, but the script should remain essentially the same: The user types in the number of prime numbers to be printed (n) and the script returns all prime numbers up to the nth prime number.

from time import time
odd = 1
primes = [2]
n = input("Number of prime numbers to print: ")
clock = time()
def isPrime(number):
    global primes
    for i in primes:
        if i*i > number:
            return True
        if number%i is 0:
            return False
while len(primes) < n:
    odd += 2
    if isPrime(odd):
        primes += [odd]
print primes
clock -= time()
print "\n", -clock
raw_input()


texts:
I'm trying it out just to see if I could solve it. It would be great if I could receive tips on how to improve it.

//input
var decimal = prompt("Please enter a decimal number");
if (decimal >= 1 && decimal < 1000000000) {
    //convert to binary
    var binConvert = parseInt(decimal, 10).toString(2);

    //reverse binary number
    var makeString = binConvert.toString();
    var srj = makeString.split("").reverse().join("");
    var makeNumber = Number(srj);

    //convert back to decimal
    alert(parseInt(makeNumber, 2));

} else {
    alert("Input number should be: 1 <= N < 1 000 000 000. Please try again.");
}

jsFiddle


texts:
I implemented an ExpireLRUCache class, which will clear the data when it times out. There are two ways to achieve this:

Use a timer to clear the expire data
Call the clean function in Add and Get

If I use a struct in class, and it use the template typename, how to deal with this?
I put the declaration of Node at the top, because it will be used when it is used. Is it reasonable?
template <typename K, typename V>
class ExpireLRUCache {
 private:
  using Timestamp = std::chrono::time_point<std::chrono::system_clock>;
  struct Node {
    K key;
    V value;
    Timestamp timestamp;
  };

 public:
  using NodePtr = std::shared_ptr<Node>;
  using NodeIter = typename std::list<NodePtr>::iterator;
  using ExpiredCallBack = std::function<void(K, V)>;

  // Default timeout is 3000ms.
  ExpireLRUCache(size_t max_size)
     : max_size_(max_size), time_out_(3000), expired_callback_(nullptr) {}

  ExpireLRUCache(size_t max_size, uint32_t time_out, ExpiredCallBack call_back)
     : max_size_(max_size), time_out_(time_out), expired_callback_(call_back) {}

  void Add(K key, V value);
  V Get(K key);

  size_t Size() const;

 private:
  void Expired();

  mutable std::mutex mutex_;
  std::list<NodePtr> list_;
  std::unordered_map<K, NodeIter> map_;

  size_t max_size_;
  // ms
  uint32_t time_out_;

  ExpiredCallBack expired_callback_;
};

template <typename K, typename V>
void ExpireLRUCache<K, V>::Add(K key, V value) {
  std::lock_guard<std::mutex> lock(mutex_);
  // if full, delete oldest
  if (list_.size() >= max_size_) {
    auto oldest = list_.back();
    list_.pop_back();
    map_.erase(oldest->key);
  }

  // if exist, delete it in the list, and then add to the front
  // then overwrite in map.
  if (map_.find(key) != map_.end()) {
    NodeIter iter = map_[key];
    list_.erase(iter);
  }

  auto timestamp = std::chrono::system_clock::now();
  NodePtr node = std::make_shared<Node>(Node{key, value, timestamp});
  list_.push_front(node);
  map_[key] = list_.begin();
}

template <typename K, typename V>
V ExpireLRUCache<K, V>::Get(K key) {
  std::lock_guard<std::mutex> lock(mutex_);

  // Todo(zero): how to call
  Expired();

  if (map_.find(key) != map_.end()) {
    return (*map_[key])->value;
  } else {
    return V{};
  }
}

template <typename K, typename V>
void ExpireLRUCache<K, V>::Expired() {
  auto time_now = std::chrono::system_clock::now();

  while( !list_.empty() ) {
    auto oldest = list_.back();
    auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(
                    time_now - oldest->timestamp);
    if (diff.count() > time_out_) {
      list_.pop_back();
      map_.erase(oldest->key);
      expired_callback_(oldest->key, oldest->value);
    } else {
      break;
    }
  }
}

template <typename K, typename V>
size_t ExpireLRUCache<K, V>::Size() const {
  std::lock_guard<std::mutex> lock(mutex_);
  return map_.size();
}
```


texts:
I have known about unit testing for a while now, but I am just now finally understanding how to implement it.  I think that my initial implementation is a little rough so I could use some feedback.

I have implemented these tests in order to verify that a new feature of my strategy game is working properly. Previously, I would have output these values to the screen somewhere and simply watched to make sure that the expected behavior was happening. With these tests, I have concrete proof that the feature is working.  This is much better!

However there are some problems. I needed to implement a Testing category for the class that I am testing because some of the methods I needed to run the tests are private. By creating this category at the top of the testing class, I can use private methods from the class without having to permanently expose them.  This works, but I have heard that I should only be unit testing public methods, so I am wondering if what I have done is acceptable. I should note that I also had to add a couple getter methods to the class to get some information that was not public.

DTDwarfTests.m

#import <XCTest/XCTest.h>
#import "DTDwarf.h"
#import "DTJobUnit.h"

@interface DTDwarf (Testing)
    -(void) setMoodForTesting:(int)moodAmount;
    -(void) startJobCountdown:(JobType)jobType;
    -(BOOL) doJobCountdown;
    -(void) calculateMoodState;
    -(void) calculateMoodAmountForJob;
    -(JobType) getFavoriteJob;
    -(JobType) getHatedJob;
@end

@interface DTDwarfTests : XCTestCase
@end

@implementation DTDwarfTests

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

-(void) testMoodInitialization {
    DTDwarf *dwarf = [[DTDwarf alloc]initWithWorldSize:CGSizeZero];
    XCTAssert(dwarf.moodAmount > 0);
}
-(void) testFavoriteAndHatedJobsValues {
    DTDwarf *dwarf = [[DTDwarf alloc]initWithWorldSize:CGSizeZero];
    XCTAssert([dwarf getFavoriteJob] != JobTypeNumJobTypes);
    XCTAssert([dwarf getHatedJob] != JobTypeNumJobTypes);
}
-(void) testFavoriteJobMoodInfluence {
    DTDwarf *dwarf = [[DTDwarf alloc]initWithWorldSize:CGSizeZero];
    [dwarf setMoodForTesting:0];
    dwarf.dwarfJobUnit = [[DTJobUnit alloc]initWithJobType:[dwarf getFavoriteJob]];
    [dwarf calculateMoodAmountForJob];
    XCTAssert(dwarf.moodAmount > 0);
}
-(void) testHatedJobMoodInfluence {
    DTDwarf *dwarf = [[DTDwarf alloc]initWithWorldSize:CGSizeZero];
    [dwarf setMoodForTesting:1000];
    dwarf.dwarfJobUnit = [[DTJobUnit alloc]initWithJobType:[dwarf getHatedJob]];
    [dwarf calculateMoodAmountForJob];
    XCTAssert(dwarf.moodAmount < 1000);
}
-(void) testBadMoodIncreasesCountdown {

    DTDwarf *happyDwarf = [[DTDwarf alloc]initWithWorldSize:CGSizeZero];
    [happyDwarf setMoodForTesting:1000];
    [happyDwarf calculateMoodState];
    [happyDwarf startJobCountdown:JobTypeMining];
    int happyDwarfCountdown = 0;
    while (![happyDwarf doJobCountdown]) {
        happyDwarfCountdown++;
    }

    DTDwarf *sadDwarf = [[DTDwarf alloc]initWithWorldSize:CGSizeZero];
    [sadDwarf setMoodForTesting:0];
    [sadDwarf calculateMoodState];
    [sadDwarf startJobCountdown:JobTypeMining];
    int sadDwarfCountdown = 0;
    while (![sadDwarf doJobCountdown]) {
        sadDwarfCountdown++;
    }

    XCTAssert(happyDwarfCountdown < sadDwarfCountdown);
}

@end


texts: Inline conversion/cast from Expression> to LambdaExpression

texts:
I have the following code (I need the LambdaExpression as not all Func<>'s are T, int):

var orderDelegates = new Dictionary<string, LambdaExpression>();

Expression<Func<Image, int>> id = i => i.Id;
orderDelegates.Add(ContentItem.ORDER_BY_ID, id);

Is it possible to make this shorter?

Best something like:

var orderDelegates = new Dictionary<string, LambdaExpression>()
{
    { ContentItem.ORDER_BY_ID, ????? },
    ...
};

I already tried the following, which did not work:

orderDelegates.Add(ContentItem.ORDER_BY_ID, (Expression<Func<Image, int>>)i => i.id);
//or
orderDelegates.Add(ContentItem.ORDER_BY_ID, i => i.id as Expression<Func<Image, int>>);

I "improved" it a little to the following already, but "full inlining" would be the desired result:

public static Dictionary<string, LambdaExpression> GetOrderDelegates<T>() where T : ContentItem
{
    Expression<Func<T, int>> id = i => i.Id;
    Expression<Func<T, bool>> enabled = i => i.IsEnabled;
    Expression<Func<T, IComparable>> author = i => i.Creator.UserName;
    //...

    return new Dictionary<string, LambdaExpression>()
        {
            { ContentItem.ORDER_BY_ID, id },
            { ContentItem.ORDER_BY_ENABLED, enabled },
            { ContentItem.ORDER_BY_AUTHOR, author },
            //...
        };
}


texts:
I am building an Administration web site for the Office details in my database. Right now, I am just trying to display the office details.

public class OfficeRepository
{
    public static Office GetOffice(int syncID)
    {
        List<Office> results = new List<Office>();

        var sql = @"Select so.SyncID, so.title From Offices o Left Outer Join SyncOffices so On so.id = o.SyncID Where o.SyncID = @syncID";

        using (var connection = new SqlConnection(Settings.ConnectionString))
        using (var command = new SqlCommand(sql, connection))
        {
            command.CommandType = CommandType.Text;
            command.Parameters.AddWithValue("@syncID", syncID);
            connection.Open();

            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var office = new Office();
                    office.SyncID = reader.GetInt32(0);
                    office.OfficeName = reader.GetString(1);
                    results.Add(office);
                }
            }
        }

        Office returnOffice = new Office();
        returnOffice.SyncID = results.FirstOrDefault().SyncID;
        returnOffice.OfficeName = results.FirstOrDefault().OfficeName;
        return returnOffice;
    }
}

public class HomeController : Controller
{
    public ActionResult Index(int id = 0)
    {
        Office currentOffice = OfficeRepository.GetOffice(id);
        var officeMgrViewModel = new OfficeMgrViewModel();
        officeMgrViewModel.Office = currentOffice;
        return View(officeMgrViewModel);
    }

}

public class OfficeMgrViewModel
{
    public Office Office { get; set; }
}

public class Office
{
    public Int32 SyncID { get; set; }
    public string OfficeName { get; set; }
}


texts: Constructors - lots of paramters or none and use of set methods

texts:
In your opinion what is the best way to construct an object given the two examples below:

Option 1: Lots of parameters

private string firstname;
private string surname;
private Address homeAddress;
private PhoneNumber homeNumber;

public Person(string firstname, string surname, Address homeAddress,
              PhoneNumber homeNumber)
{
    _firstname = firstname;
    _surname = surname;
    _homeAddress = homeAddress;
    _homeNumber = homeNumber;
}

public string getFirstname()
{
    return firstname;
}

public string getSurname()
{
    return surname;
}

public Address getAddress()
{
    return address;
}

public PhoneNumber getHomeNumber()
{
    return _homeNumber;
}

Option 2: no parameters and use of set methods

private string firstname;
private string surname;
private Address homeAddress;
private PhoneNumber homeNumber;

public Person()
{
}

public string getFirstname()
{
    return firstname;
}
public void setFirstname(String value)
{
    firstname = value;
}

public string getSurname()
{
    return surname;
}
public void setSurname(String value)
{
    surname = value;
}

public Address getAddress()
{
    return address;
}
public void setAddress(Address value)
{
    address = value;
}

public PhoneNumber getHomeNumber()
{
    return _homeNumber;
}
public void setHomeNumber(PhoneNumber value)
{
    _homeNumber = value;
}

Which method do you prefer?  In my mind, option 1 is better because you know that the state of your object cannot be changed.  I believe this is called immutability and is something that should be desired?  On the other hand, it can quickly get out of hand and ugly if you have more than a handful of parameters.

I am aware that some languages such as C# have nice features that would probably make option 1 more appealing such as named and optional parameters however this question was prompted while dealing with Java code.

Update: Thank you for all of your suggestions.  I will comment on each answer individually but a few answers suggested using a factory method instead of having the constructor do all of the work.  This was actually the pattern I was using because it is a standard at work but I've never really seen the advantage of it as opposed to using the constructor to do the work.  This is more how the code looked:

private string firstname;
private string surname;
private Address homeAddress;
private PhoneNumber homeNumber;

private Person()
{
}

public Person factoryCreate(string firstname, string surname, Address homeAddress,
                            PhoneNumber homeNumber)
{
    Person person = new Person();
    person.setFirstname(firstname);
    person.setSurname(surname);
    person.setHomeAddress(homeAddress);
    person.setHomeNumber(homeNumber);
    return person;
}

public string getFirstname()
{
    return firstname;
}
public void setFirstname(String value)
{
    firstname = value;
}

public string getSurname()
{
    return surname;
}
public void setSurname(String value)
{
    surname = value;
}

public Address getAddress()
{
    return address;
}
public void setAddress(Address value)
{
    address = value;
}

public PhoneNumber getHomeNumber()
{
    return _homeNumber;
}
public void setHomeNumber(PhoneNumber value)
{
    _homeNumber = value;
}

From the discussion about having constructors do little work and using builders, I can see why having the factory method is a good one.  My understanding of factory methods was that they were used to create a class given a few options.  For example, if you have an Employee class and that is sub-classed by FullTimeEmployee and PartTimeEmployee, you would have a factory() method on Employee to deal with creating the more concrete class.


texts: Safely Initialising a static variable to a file

texts:
This might appear to be a trivial task, but I think it's not the case. My code originally looked like this:

class DataSetToPdf
{
  static byte [] F1 = Util.GetFile( "C:\PdfFiles\FreeSans.ttf" );
  public static void Go( Data.DataSet ds, int ix, System.IO.Stream output )
  {
    // Code which uses F1
  }
}

Util.GetFile simply reads the file and returns an array of bytes. For completeness, here it is:

class Util
{
  public static byte[] GetFile( String path )
  {
    IO.MemoryStream ms = new IO.MemoryStream();
    using( IO.FileStream f = IO.File.OpenRead( path) )
    {
      f.CopyTo( ms );
    }
    return ms.ToArray();
  }
}

However, when using DataSetToPdf in a multi-threaded environment (IIS), I was sometimes ( but not always ) getting mysterious null reference exceptions, which I could not easily debug. However, what I believe what was happening is that the static method could be called before the initialisation is complete. Instead what appears to be is necessary is this:

class DataSetToPdf
{

  static System.Object Locker = new System.Object();

  static byte [] F1;

  public static void Go( Data.DataSet ds, int ix, System.IO.Stream output )
  {
    lock( Locker )
    {
      if ( F1 == null )
      {
         F1 = Util.GetFile( "C:\PdfFiles\FreeSans.ttf" );
      }
    }
    // Code which uses F1
  }
}

Please review my updated code, was it wrong before, is it right now?


texts: Parsing and extracting all IDs from a document (using regex with capture)

texts:
I'm interested in parsing and extracting the IDs found in a document.  The IDs are found throughout the document in the this format:

  IDStart=1FG3392MQ9&IDEnd

The "1FG3392MQ9" is an example of such an ID (it will always appear between the IDStart and IDEnd tokens as in the above example). There will be text before and possibly after each occurrence and they could appear once on a given line or many times on a line.

Here's a utility function I have to parse and extract them:

Public Shared Function ParseALLDocumentIDs(text As String) As List(Of String)

   Dim allParsedIDs As New List(Of String)

   Dim IDregex As String = "IDStart=(.+?)&IDEnd"

   Dim matches As MatchCollection = Regex.Matches(text, IDregex, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
   For Each m As Match In matches
       If m.Success Then
           Dim parsedID = m.GetFristCapture()
           If Not String.IsNullOrWhiteSpace(parsedID) Then
               allParsedIDs.Add(parsedID)
           End If
       End If
   Next

   Return allParsedIDs

 End Function

Upon each match, the following extension method is used (defined in another module) to get the first group capture of a match:

<Extension()>
    Public Function GetFristCapture(m As Match) As String

        ' Gets the first group capture "(...)" in a regex match.
        ' Returns empty string if not a match or there is no group capture found

        Dim capturedValue As String = ""

        If m IsNot Nothing AndAlso m.Success AndAlso m.Groups.Count > 1 Then
            ' Note: The first group is the entire match itself,
            ' so Group(1) is what we want, not Group(0)
            If m.Groups(1).Success Then

                capturedValue = m.Groups(1).Value.Trim

            End If
        End If

        Return capturedValue

    End Function

Questions:

How does the Regex look? Am I missing anything?  The idea was to capture all text between any occurrences of those two tokens, and do it non-greedily so that it doesn't capture across multiple tokens.
How does overall approach look with getting these IDs?  Is there a cleaner way?