texts:
In my case, I needed to give roles to users, but to specific departments, and I did not want to implement a new authorization mechanism which will require me to access the db everytime I want to check if the user is authorized for a specific role to a specific department.

So I thought I will just create an extension method IsInRole with two parameters (role, departmentId). Here's the exact code:

public static bool IsInRole(this IPrincipal principal, string role, int departmentId)
{
    var ident = principal?.Identity;
    if (ident == null)
        return false;

    if (departmentId <= 0)
        return false;

    var roleDeparments = ((ClaimsIdentity)ident).FindFirst($"{role} departments")?.Value;
    if (string.IsNullOrWhiteSpace(roleDeparments))
        return false;

    var inInRole = roleDeparments
        .Split(',')
        .Any(d => d == departmentId.ToString() || d == "all");

    return inInRole;
}

The user roles are saved in the db and are fetched once upon logging in, if the departmentId is set to NULL then it means this role is general to all departments. The Identity object is created at login time using the following method:

private ClaimsIdentity CreateIdentity(User user)
{
    var identity = new ClaimsIdentity(MyAuthentication.ApplicationCookie, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
    identity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "Active Directory"));
    identity.AddClaim(new Claim(ClaimTypes.Name, user.Id));
    identity.AddClaim(new Claim(ClaimTypes.GivenName, user.Name));
    identity.AddClaim(new Claim("DepartmentId", user.DepartmentId.ToString()));

    var roles = user.UserRoles
        .GroupBy(r => r.Role);

    foreach (var role in roles)
    {
        var departments = role
            .Select(r => r.DepartmentId)
            .Distinct()
            .ToList();

        bool isAll = departments.Any(d => d == null);

        string departmentIds = isAll
            ? "all"
            : string.Join(",", departments);

        identity.AddClaim(new Claim($"{role.Key.Name} departments", departmentIds));
    }

    return identity;
}

Now, I can simply do something like:

if (User.IsInRole("schedule viewer", 2))
{
   .....
}

Is this the right approach?


texts:
The following is my attempt at creating a Boggle board solver in Ruby. It's not especially fast. I would love to have input on how the solution can either be sped up, or whether a different algorithm might be applied that is faster.

The solution corresponds to my answer on a related question on the Math Stack Exchange site: https://math.stackexchange.com/a/2908479/30919

require 'set'
# List of words in a file, imported into sorted set for quick lookup:
WORDS = SortedSet.new(open('wordlist.txt').readlines.map(&:chomp))
# Possible directions from a space and their x/y deltas:
DIRS = { n: [0, -1], ne: [1, -1], e: [1, 0], se: [1, 1], s: [0, 1], sw: [-1, 1], w: [-1, 0], nw: [-1, -1] }.freeze

# Note on "Qu" -- "Qu" should require special handling in the

# Sample board we want to solve; we could create a random board with
# the known configuration of the cubes.
# For 'Qu' on the Boggle board, only 'Q' is entered in @board, because the traversion routine
# has special handling for 'Q' by adding a 'U' each time
@board = [
  %w[ N P E R ],
  %w[ P L R O ],
  %w[ I U N T ],
  %w[ J E G V ]
]

# This method determines the next position based on the currend position
# and direction. If the position is out of bounds, it returns nil
def next_pos(position, direction)
  npos = [
    position[0] + DIRS[direction][0],
    position[1] + DIRS[direction][1]
  ]
  return nil if (npos[0] < 0) || (npos[1] < 0) ||
                (npos[0] > 3) || (npos[1] > 3)
  npos
end

@stack = [] # Stack containing the positions traversed
# NB: **The letters have their own stack due to the special handling of "Qu"**
@letters = [] # Corresponding letters from the positions in stack
@found_words = SortedSet.new # Words found
def traverse(cpos)

    # Nested method that handles popping of stack with special 'Qu' handling (because it's used more than once)
    def pop_stack
        @stack.pop
        @letters.pop
        # Special handling for "Qu":
        @letters.pop if @letters.last == "Q"
    end

  #The longest words in my list are 15 letters - omit this check if you have more
  return if @stack.length == 15

  # Push current position and letters collected so far on the respective stacks
  @stack << cpos
  @letters << @board[cpos[0]][cpos[1]]
  # Special handling for Qu:
  @letters << "U" if @letters.last == "Q"

  # From 2 letters onwards, check whether this path is worth pursuing
  if @stack.length >= 2
    unless WORDS.grep(/^#{@letters.join}/).any?
      pop_stack
      return
    end
  end

  # From 3 letters onwards, check if the word is in the dictionary
  if @stack.length >= 3
    word = @letters.join
    if WORDS.include?(word)
      @found_words << word
    end
  end

  # Try each direction in succession from the current position
  DIRS.keys.each do |dir|
    npos = next_pos(cpos, dir) # Calculate next pos from current pos
    next unless npos # Check it is not out of bounds
    # Check whether the new position had been visited in current stack
    visited = false
    @stack.reverse_each do |pos|
      if pos == npos
        visited = true
        break
      end
    end
    next if visited # Skip the new position if it had been visited
    traverse(npos) # Start looking from next position
  end

  # Take the last position/letter off the stack before returning
  pop_stack

end

# Traverse from every square on the board
4.times.map do |x|
  4.times.map do |y|
    traverse([x, y])
  end
end

puts @found_words.sort


texts: Convert data (adding and sorting) with JavaScript

texts:
I have an array of data like this:

var data = [
    ['a', 2010, 12], ['a', 2010, 20], ['a', 2011, 43], ['a', 2012, 25], ['a', 2012, 15],
    ['b', 2010, 40], ['b', 2012, 65], ['b', 2013, 20], ['b', 2013, 10], ['b', 2013, 15],
    ['c', 2010, 13], ['c', 2010, 17], ['c', 2011, 22], ['c', 2011, 32], ['c', 2011, 10], ['c', 2012, 45], ['c', 2013, 10], ['c', 2013, 20]
]

and each array data is [name, year, value].

What I want to do here is to add values when name and year are the same and then sort data by year and change the order by name, so I want to convert data to:

var newData = [
    ['a', 2010, 32], ['b', 2010, 40], ['c', 2010, 30],
    ['a', 2011, 43], ['c', 2011, 64]
    ['a', 2012, 40], ['b', 2012, 65], ['c', 2012, 45],
    ['b', 2013, 45], ['c', 2013, 30]
]

I am doing this by taking multiple steps:

// find names
var sortByName = [];
var nameArr = [];
var namesFound = {};
for(var i = 0; i < data.length; i++) {
  if(namesFound[data[i][0]]) { continue; }
  nameArr.push(data[i][0]);
  namesFound[data[i][0]] = true;
}
// sort data by name by creating an array for each name (2D array to 3D array)
for (var i = 0; i < nameArr.length; i++) {
  var result = data.filter(function(arr) {
    return arr[0] == nameArr[i]
  })
  sortByName.push(result)
}
// add values with a same year
var newArr = sortByName.map(function(eachArr) {
  var sumBySameYear =  eachArr.reduce(function(acc, arr) {
    if (acc.year === arr[1]) {
      acc.result[acc.result.length - 1][2] += arr[2];
    } else {
      acc.result.push(arr);
      acc.year = arr[1]
    }
    return acc
  }, {result: [], year: null});
  return sumBySameYear.result
});
// 3D array to 2D array
var multiToSingleArr = [].concat.apply([], newArr);
// then sort new data by year and keep the order of name
var newData = multiToSingleArr.slice().sort(function(a, b) {
  return a[1] - b[1] || a[0].localeCompare(b[0]);
});

I definitely feel like I am taking unnecessary steps here, so how can I make this more concise?


texts: Verify if user has at least one of required roles to be able to update a record owned by a certain user

texts:
I have a Microsoft CRM 2016 plugin that checks if the user that triggers an update message has at least one of the required roles, when the updated record is owned by a specific user.

Here's the code:

public class CheckIfOwnedByPool : BasePlugin
{
    public CheckIfOwnedByPool(string unsecure, string secure)
        : base(unsecure, secure)
    {

    }

    public override void ExecutePluginLogic(XrmObjects xrmObjects)
    {

        if (xrmObjects.PluginContext.MessageName.ToLower() == "update" && xrmObjects.PluginContext.PreEntityImages.Contains("entity"))
        {

            Guid PoolId = Guid.Empty;
            List<Guid> allowedRoles = new List<Guid>();
            var entity = xrmObjects.PluginContext.PreEntityImages["entity"];

            //retrieve id of "POOL" team and allowed roles GUIDs from plugin step XML config. GUID varies from environment to environment...
            try
            {
                PoolId = new Guid(GetAllNodes("ownerconfig")[0].Attributes["value"].InnerText);
                GetAllNodes("roles")[0].Attributes["value"].InnerText.Split(';').ToList().ForEach(x => allowedRoles.Add(new Guid(x)));
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(string.Format("Error deserializing plugin step XML config: {0}", ex.Message));
            }

            if (PoolId == Guid.Empty || allowedRoles.Count == 0)
                return;

            //if entity is owned by team called "POOL"
            if (((EntityReference)entity["ownerid"]).Id == PoolId)
            {
                using (XrmServiceContext ctx = new XrmServiceContext(xrmObjects.Service))
                {
                    //retrieve current user roles
                    var userRoles = ctx.SystemUserRolesSet.Where(x => x.SystemUserId == xrmObjects.PluginContext.InitiatingUserId).ToList();

                    //check if current user has one of required roles
                    if (!userRoles.Any(x => allowedRoles.Any(y => y == x.RoleId.Value)))
                        throw new InvalidPluginExecutionException("You do not have access to modify this record. Please see with your supervisor");

                }

            }

        }
    }
}

This seems to work fine and is registered in pre-operation of Update of any entity. As observed, if one plugin in pre-operation throws an exception, all the plugin stack is stopped, meaning all other pre/post operation plugins won't execute, which is what I want.

Do you see anything that can go wrong? The only issue is that every update done on an entity owned by "POOL", there will be a retrieve to get the user's role. This could hurt performance, but by how much ?

BasePlugin is an abstract class implementing IPlugin and xrmObjects holds useful members such as the tracing service, the plugin exec. context, etc. GetAllNodes is a member of BasePlugin which allows for easy access of the XML plugin step config.


texts: Count characters, words, and lines in files (wc in rust)

texts:
I wrote a barebones version of wc in rust. wc is a program that counts the number of characters, words, and lines in a file and outputs those values to the command line. Here is an example of the output:
   9   25  246 Cargo.toml
  52  163 1284 src/main.rs
  61  188 1530 total

My version currently lacks the proper output alignment, and it doesn't print the total (it also lacks the command line options, and it panics when fed a directory). But I would like to get some feedback before I go any further.
use std::env;
use std::fs::read_to_string;

struct InputFile {
    words: u32,
    lines: u32,
    characters: u32,
    name: String,
}

impl InputFile {
    fn new(name: &String) -> Self {
        let content = read_to_string(name).unwrap();
        let (mut characters, mut words, mut lines) = (0, 0, 0);
        let mut spaced: bool = false;
        for c in content.chars() {
            if c as u8 != 0 {
                characters += 1;
            }
            if c != ' ' && c != '\n' {
                spaced = false
            }
            if c == '\n' {
                lines += 1;
                if !spaced {
                    words += 1;
                    spaced = true;
                }
            }
            if c == ' ' && !spaced {
                words += 1;
                spaced = true;
            }
        }
        Self { lines, words, characters, name: name.to_string() }
    }
}

impl std::fmt::Display for InputFile {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{} {} {} {}",
            self.lines, self.words, self.characters, self.name
        )
    }
}

fn main() {
    let files: Vec<String> = env::args().collect();
    for f in &files[1..] {
        println!("{}", InputFile::new(f));
    }
}


texts: In an set of integers, find three elements summing to zero (3-sum, leetcode variant)

texts:
I use a dummy solution to solve the leetcode 3Sum problem.

I employ the set data type to handle duplicates, then transform back to list.

  Given an array nums of n integers, are there elements a, b, c  in nums such that a + b + c = 0?  Find all unique triplets in the array which gives the sum of zero.

  Note:

  The solution set must not contain duplicate triplets.

  Example:

  Given array nums = [-1, 0, 1, 2, -1, -4],

  A solution set is:

[
  [-1, 0, 1],
  [-1, -1, 2]
]

My codes:

from typing import List
import logging
import unittest
import random
from collections import defaultdict,Counter

##logging.disable(level=#logging.CRITICAL)
##logging.basicConfig(level=#logging.DEBUG, format="%(levelname)s %(message)s")

    class Solution:
        def threeSum(self, nums, target: int=0) -> List[List[int]]:
            """
            :type nums: List[int]
            :type target: int
            """
            if len(nums) < 3: return []

            triplets = []
            if target == [0, 0, 0]:
                triplets.append([0, 0, 0])
                return triplets # finish fast

            lookup = {nums[i]:i for i in range(len(nums))} #overwrite from the high
            if len(lookup) == 1:#assert one identical element
                keys = [k for k in lookup.keys()]
                if keys[0] != 0:
                    return []
                else:
                    triplets.append([0,0,0])
                    return triplets

            triplets_set = set()

            for i in range(len(nums)):
                num_1 = nums[i]
                sub_target = target - num_1
                # #logging.debug(f"level_1_lookup: {lookup}")

                for j in range(i+1, len(nums)):
                    num_2 = nums[j]
                    num_3 = sub_target - num_2
                    k = lookup.get(num_3) #
                    if k not in {None, i, j}: #don't reproduce itself
                        result = [num_1, num_2, num_3]
                        result.sort()
                        result = tuple(result)
                        triplets_set.add(result)

            triplets = [list(t) for t in triplets_set]
            return triplets

My solution gets this result:

  Your runtime beats 28.86 % of python3 submissions.

Could anyone please give hints to improve?


texts: Listing an optional Python dependency in setup.py

texts:
I have a small project that requires a third-party module that's rarely updated (once every other year or so) and not maintained by any package management system. The context, here, is cognitive science & research, the third party module is for linking proprietary hardware to custom scripting, in the house project, potentially, may end up seeing wider (eventually open source) use within cog. psych so I want to plan for distribution.

Can anyone poke holes in or thumbs up this approach:

#!/usr/bin/env python

from distutils.core import setup

try:
    import third_party_package
    install_packages = ['in_house_package']
except ImportError:
    print "Warning: 'third_party_package' not found in PYTHON_PATH, installing version in_house_package v. <VERSION>"
    install_packages = ['in_house_package', 'in_house_package.third_party_package']

setup(
    name='InHousePackage',
    version = '0.1',
    description = 'A framework for building psychological experiments in Python',
    author = 'me',
    author_email = 'my_address',
    url = 'a local git',
    packages=install_packages,
    requires = ['numpy']
    )


texts: Construct and send an HTTP get request from scratch and print all the received data to the screen

texts:
My concerns:
Is the code pythonic?
Does the receive_all function make the code any cleaner or should that functionally be apart of the get function?

import socket
import urlparse

CONNECTION_TIMEOUT = 0.30
CHUNK_SIZE = 1024
socket.setdefaulttimeout = CONNECTION_TIMEOUT
CRLF = "\r\n\r\n"

def receive_all(sock, chunk_size=CHUNK_SIZE):
    '''
    Gather all the data from a request.
    '''
    chunks = []
    while True:
        chunk = sock.recv(chunk_size)
        if chunk:
            chunks.append(chunk)
        else:
            break

    return repr(''.join(chunks))

def get(url, timeout=CONNECTION_TIMEOUT, chunk_size=CHUNK_SIZE):
    url = urlparse.urlparse(url)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(timeout)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.connect((url.netloc, url.port or 80))
    sock.send('GET {0} HTTP/1.0 {1}'.format(url.path or '/', CRLF))
    data = receive_all(sock)
    sock.shutdown(1)
    sock.close()
    return data

print(get('http://www.google.com/robots.txt', chunk_size=65535))


texts:
How should I make this more Ruby-like or just "better"?

def password_format_is_valid?(submitted_password)

  #Gets regular expression for password format validation from settings and applies it
  regex = Regexp.new(Setting['global_admin.password_format_regex'])
  if submitted_password =~ regex then
    return true
  else
    self.errors.add(:password, Setting['global_admin.password_format_error_message'])
    return false
  end

end


texts: String to hash in Ruby without JSON.parse

texts:
Just for the sake of understanding. I know JSON.parse could do this pretty easily, but without using that, how could this be made cleaner or more efficient?

string = "ounces: 10; contains_toy: false"
result = string.split("; ")
result = result.map{|x| x = x.split(": "); Hash[x.first.to_sym, x.last] }
result = result.reduce(:merge)

Desired result:

{:ounces=>"10", :contains_toy=>"false"}


texts: Find and process duplicates in list of lists

texts:
I'm trying to merge counts for items (URLs) in the list:

[['foo',1], ['bar',3],['foo',4]]

I came up with a function, but it's slow when I run it with 50k entries. I'd appreciate if somebody could please review and suggest improvements.

def dedupe(data):
    ''' Finds duplicates in data and merges the counts '''
    result = []
    for row in data:
        url, count = row
        url_already_in_result = filter(lambda res_row: res_row[0] == url, result)
        if url_already_in_result:
            url_already_in_result[0][1] += count
        else:
            result.append(row)
    return result

def test_dedupe():
    data = [['foo',1], ['bar',3],['foo',4]]
    assert dedupe(data) == [['foo',5], ['bar',3]]


texts: PHP code structure for string manipulation tasks

texts:
I need to perform a bunch of string manipulation tasks on an original string, then output the final string. Best I could think of is extracting some functionality into functions. Any better way to structure it? Also, are the variable/function names too long?

strip out unicode chars from original string A, reverse them into string B
in the remaining string A, remove duplicate words
append: A + B

function extract_unicode_characters_from_string($str) {
    preg_match_all('/\p{Han}+/u', $str, $unicode_characters_only, PREG_SET_ORDER);
    $squashed = array_column($unicode_characters_only, 0);
    return $squashed;
}

function strip_unicode_characters_from_string($str) {
    return preg_replace('/\p{Han}+/u', '', $str);
}

function strip_duplicates($str){
    $exploded_str = explode(' ', $str);
    $exploded_str_no_empties = array_filter($exploded_str, function($item) { return !is_null($item) && $item !== ''; });
    return implode(' ', array_unique($exploded_str_no_empties));
}

$original_string = 'one two three 喞 喝 four 刷囿 two 跏正 吁';

$array_of_unicode_characters = extract_unicode_characters_from_string($original_string);
$reversed_unicode_characters = implode(' ', array_reverse($array_of_unicode_characters));

$string_without_unicode_characters = strip_unicode_characters_from_string($original_string);
$english_words_no_dupes = strip_duplicates($string_without_unicode_characters);

echo $english_words_no_dupes . ' ' . $reversed_unicode_characters;


texts: Queries to get unpaid and unpaid invoices

texts:
Here is my code:
add_hook('ClientAreaPageInvoices', 1, function($vars) {
    $user_id = $_SESSION["uid"];
    $today = Carbon::now()->format('Y-m-d');
    $query_1 = Capsule::table('tblinvoices')
        ->select(Capsule::raw('SUM(total) as total_aberto'))
        ->where(array('userid' => $user_id, 'status' => 'Unpaid'))
        ->get();
    $query_2 = Capsule::table('tblinvoices')
        ->select(Capsule::raw('SUM(total) as total_vencido'))
        ->where(array('userid' => $user_id, 'status' => 'Unpaid'))
        ->whereDate('duedate', '<=', $today)
        ->get();
    $query_3 = Capsule::table('tblinvoices')
        ->select(Capsule::raw('SUM(total) as total_pago'))
        ->where(array('userid' => $user_id, 'status' => 'Paid'))
        ->get();
    $array_1 = json_decode(json_encode($query_1), True);
    $array_2 = json_decode(json_encode($query_2), True);
    $array_3 = json_decode(json_encode($query_3), True);
    $totalAberto = $array_1[0]['total_aberto'];
    $totalVencido = $array_2[0]['total_vencido'];
    $totalPago = $array_3[0]['total_pago'];
    return array ('totalAberto' => $totalAberto,
                  'totalVencido' => $totalVencido,
                  'totalPago' => $totalPago);
});

Is there any way to simplify the entire code in general?
It works well - I just want to learn other practices to make this type of consultation in a more optimized, and simplified way.


texts:
I am dong a competitive programming problem I have passed all the tests.
but my program times out. I need help optimizing my code so it can run faster.

  Task:

  Finish method:   BingoCaller.GetNumber()
  Return all numbers in the range of 1 until 75 once and in random    order
  If there are no numbers left, return an empty string
  The numbers are returned one by one in Bingo style:

  "I27", "N40", "B5", "B12", "I28", "O69", "B1", ...
  These are the ranges that you must follow:

  A number within range 1 to    15 starts with a 'B'
  A number within range 16 to 30 starts with an    'I'
  A number within range 31 to 45 starts with an 'N'
  A number within range 46 to 60 starts with a 'G'
  A number within range 61 to 75 starts with an 'O'

  Use System.Random to generate your random numbers. Pass via the    constructor for testing purposes.

My code below

       using System;
       using System.Collections.Generic;

       public class BingoCaller
        {

            private Random random;

            static List<int> List = new List<int>();

            public BingoCaller(Random random)
            {
                this.random = random;

            }

            public string GetNumber()
            {

            Start:

             var item = random.Next(75);

                if (List.Contains(item))
                {
                  goto Start;
                }
                else
                {
                    List.Add(item);

                    if (item >= 61)
                    {
                        return "O" + item;
                    }
                    else if (item >= 46)
                    {
                        return "G" + item;
                    }
                    else if (item >= 31)
                    {
                        return "N" + item;
                    }
                    else if (item >= 16)
                    {
                        return "I" + item;
                    }
                    else
                    {
                        return "B" + item;
                    }

                }

            }
        }


texts: Process CSV files that follow the first nonempty one

texts:
I have a list of CSV files (all have the same fields), some of which are empty. My goal is to find the first nonempty file and use that as the "base" file for an aggregation. After finding the base file, the way I will consume the rest of the files changes, so I need to maintain the index of the file. Here is what I currently have:

def f(list_of_files):
    aggregated_file = ""
    nonempty_index = -1
    for index, file in enumerate(list_of_files):
        aggregated_file = extract_header_and_body(file)
        if aggregated_file:
            nonempty_index = index
            break

    if nonempty_index != -1:
        rest_of_files = list_of_files[nonempty_index:]
        for file in rest_of_files:
            aggregated_file += extract_body(file)

    return aggregated_file

Both extract_header_and_body and extract_body return string representations (with and without column names, respectively) of the CSV files -- if the file is empty, the empty string is returned. This seems like a clunky solution, especially for Python. Is there a more concise/readable way to accomplish this?


texts:
I have a Hidden Markov model class with basically a single method: getting the best parse of a sequence of input tokens based on Viterbi.

While I have no hardcore benchmarks, I'd love some pointers to make it even a bit faster, as it (expectedly) takes quite a long time when the number of states is over 2000.

from math import log10 as lg

class HMM(object):
    def __init__(self, states, start_probs, transition_probs, emission_probs):
        # set(str)
        self.states = states

        # map(str, float)
        self.start_probs = start_probs

        # map(str, map(str, float))
        self.transition_probs = transition_probs

        # map(str, map(str, float))
        self.emission_probs = emission_probs

    def best_parse(self, input_tokens):
        p, path = HMM.__viterbi(
            input_tokens, self.states, self.start_probs, self.transition_probs, self.emission_probs
        )
        return '{0} => {1}\t{2}'.format(' '.join(input_tokens), path, lg(p))

    @classmethod
    def __viterbi(cls, observations, states, start_probability, transitionable, emission_probability):
        trellis = [{}]

        path = {}

        for state in states:
            trellis[0][state] = \
                start_probability.get(state, 0.0)
            path[state] = [state]

        for i, e in enumerate(observations[0:-1], 1):
            trellis.append({})
            path_for_current_state = {}

            for state in states:
                (probability, candidate_state) = \
                    max((trellis[i - 1][s] *
                        transitionable.get(s, {}).get(state, 0.0) *
                        emission_probability.get(state, {}).get(observations[i], float('-inf')), s)
                    for s in states)

                trellis[i][state] = probability
                path_for_current_state[state] = path[candidate_state] + [state]

            path = path_for_current_state

        # trellis[n-1][state] is the most likely parse for state.
        (probability, state) = max(
            (trellis[len(observations) - 1][state], state)
            for state in states
        )

        return (probability, ' '.join(t for (t, _) in path[state]))


texts:
I know this is a pretty simple class for loading a config file into an object, and then accessing it's properties.  I think config options should be lightweight and a class like this seems to adds a lot of overhead IMO but I really like being able to access the config properties with $config->propertyName.  So do you see any room for improvement?  Mainly performance improvements?

<?php
// config.class.php
/*
example usage
$config = Config::getInstance(PATH TO FILE, FILE TYPE);
echo $config->ip;
echo $config->db['host'];

example array file
<?php
return array(
    'db' => array(
        'host' => 'localhost',
        'user' => 'user1',
        'pass' => 'mypassword'),

    'ip' => '123456',

)

*/

class Config
{
    private static $_instance = null;
    public $options = array();

    /**
     * Retrieves php array file, json file, or ini file and builds array
     * @param $filepath Full path to where the file is located
     * @param $type is the type of file.  can be "ARRAY" "JSON" "INI"
     */
    private function __construct($filepath, $type = 'ARRAY')
    {
        switch($type) {
            case 'ARRAY':
                $this->options = include $filepath;
                break;

            case 'INI';
                $this->options = parse_ini_file($filepath, true);
                break;

            case 'JSON':
                $this->options = json_decode(file_get_contents($filepath), true);
                break;

            //TO-DO add Database option for settings. Table = id, property, value
            case 'DATABASE':
                $this->options = json_decode(file_get_contents($filepath), true);
                break;
        }
    }

    private function __clone(){}

    public static function getInstance($filepath, $type = 'ARRAY')
    {
        if (!isset(self::$_instance)) {
            self::$_instance = new self($filepath, $type);
        }
        return self::$_instance;
    }

    /**
     * Retrieve value with constants being a higher priority
     * @param $key Array Key to get
     */
    public function __get($key)
    {
        if (isset($this->options[$key])) {
            return $this->options[$key];
        }else{
            trigger_error("Key $val does not exist", E_USER_NOTICE);
        }
    }

    /**
     * Set a new or update a key / value pair
     * @param $key Key to set
     * @param $value Value to set
     */
    public function __set($key, $value)
    {
        $this->options[$key] = $value;
    }

}
?>


texts: Passing a custom parameter through a logger

texts:
I have a custom logger which logs all messages at WARNING level and above to a Slack channel. Certain error messages occur so frequently that they make it hard to see actual errors in the channel, so I would like to be able to disable Slack logging for a specific message, using something like logger.error("This is a common error", slack=False).

The following approach works, but I was wondering if there is a nicer way to do it. One downside of this approach is that if any code that isn't using my custom logger uses the slack=False argument then it will cause an exception.

def get_slack_logger():
    logger = logging.getLogger("slack_logger")
    logger.setLevel(logging.DEBUG)

    # Add other handlers like stderr and log file.
    # ...

    # Add an error handler for Slack notifications.
    class SlackHandler(logging.Handler):
        def __init__(self):
            super().__init__(self)

        def emit(self, record):
            if not record.slack:
                return
            # Actually send the contents of `record.message` to a Slack channel.
            # ...

    slack_handler = SlackHandler()
    slack_handler.setLevel(logging.WARNING)
    logger.addHandler(slack_handler)

    # Wrap logger in an adapter that handles the `slack` argument.
    class SlackToggleableAdapter(logging.LoggerAdapter):
        def __init__(self, logger, extra={}):
            super().__init__(logger, extra)

        def process(self, msg, kwargs):
            extra = kwargs.get("extra", {})
            extra.update({"slack": kwargs.pop("slack", True)})
            kwargs["extra"] = extra
            return (msg, kwargs)

    logger = SlackToggleableAdapter(logger)
    return logger


texts:
Here's my bootstrap.php for my PHP/MySQL/Doctrine app.  It's my first PHP app so I'm interested in learning from the experience of others how this could be improved - security-wise, performance-wise, or otherwise.

//-------------------------------------------------------------------------
// Define global constants
define('ROOT_PATH', dirname(__FILE__) . '/');
define('URL_BASE', 'http://localhost/myapp/public/');
define('LIB_PATH', 'C:\\wamp\\www\\lib\\');
define('OPENID_PATH', 'C:\\wamp\www\\lib\\lightopenid.git\\openid.php');

//-------------------------------------------------------------------------
// Bootstrap Doctrine.php, register autoloader, specify
// configuration attributes and load models.
require_once(LIB_PATH . 'doctrine12/lib/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));
$manager = Doctrine_Manager::getInstance();

//-------------------------------------------------------------------------
// Define database connection
$dsn = 'mysql:dbname=mydb;host=127.0.0.1';
$user = 'xxx';
$password = 'yyy';
$dbh = new PDO($dsn, $user, $password);
$conn = Doctrine_Manager::connection($dbh);

//-------------------------------------------------------------------------
// Set defaults
$manager->setAttribute(Doctrine::ATTR_DEFAULT_COLUMN_OPTIONS,
  array('type' => 'string', 'length' => 255, 'notnull' => true));
$conn->setAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
// Don't load a model until it's needed (causes problems when this is on)
//$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_CONSERVATIVE);

//-------------------------------------------------------------------------
// Import model objects
Doctrine_Core::loadModels(ROOT_PATH . 'app/models/generated'); // have to load base classes first
Doctrine_Core::loadModels(ROOT_PATH . 'app/models');


texts:
I have an API gateway in a larger set of microservices, and I'm handling one of my routes. The endpoint ends up making an RPC call to another service, and then I just log the response and send it back to the user. I haven't really completed the error handling section, because was thinking that I could add some middleware later on to handle my errors.

routes.post("/submit", async (request, result) => {
  logger.info("Received call to /gapf/submit endpoint");
  const gapf = request.body;

  // convert relevant request strings into integers
  gapf.facultyId = parseInt(gapf.facultyId, 10);
  gapf.created = parseInt(gapf.created, 10);
  gapf.lastModified = parseInt(gapf.lastModified, 10);
  if (gapf.documents !== undefined) {
    gapf.documents = gapf.documents.map(doc => {
      return {
        name: doc.name,
        link: doc.link,
        attachedDate: parseInt(doc.attachedDate, 10)
      };
    });
  }

  try {
    logger.info("Calling to Ticket Service with gapf body: %j", gapf);
    const response = await tridentClient.SubmitGAPF(gapf);
    logger.info("Received response from Ticket Service: %j", response);
    logger.info("Exiting /gapf/submit endpoint");
    result.json(response);
  } catch (err) {
    logger.error(
      "Received error from submitting to Ticket Service: ",
      err.message
    );
    // TODO: should add middleware later on to handle errors
    result.status(500);
    result.json({});
  }
});

Right now the error.message seems to be some binary data instead of JSON, so that would also need to be fixed later on.


texts: Finding N partitions of number M with min and max partitions sizes

texts:
This code was created for a game project in which 10 houses need to be populated with 16 people. I wanted the distribution to be random, with no more than 3 and no less than 1 person per house and no remaining people or houses.

JSbin link

function getRandomPartitionsFromNumber(number, parts, min, max) {
  var result = [];

  for (var i = 0; i < parts; i++) {
    var part = randomIntFromInterval(min, max);
    result.push(part);
  }
  var sum = result.reduce(add, 0);

  // If the sum is greater than the number, we need to iterate through
  // the result and remove some numbers until it's equal
  while (sum > number) {
    for (i = 0; i < result.length; i++) {
      if (result[i] > min) {
        result[i]--;
        sum = result.reduce(add, 0);
        if (sum === number) {
          break;
        }
      }
    }
    sum = result.reduce(add, 0);
  }

  while (sum < number) {
    for (i = 0; i < result.length; i++) {
      if (result[i] < max) {
        result[i]++;
        sum = result.reduce(add, 0);
        if (sum === number) {
          break;
        }
      }
    }
  }

  return result;
}

function randomIntFromInterval(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

function randomFromArray(array) {
  return array[randomIntFromInterval(0, array.length - 1)];
}

function add(a, b) {
  return a + b;
}

console.log(getRandomPartitionsFromNumber(16, 10, 1, 3));


texts: A class that uses a few other classes to generate an email

texts:
I seem to find my self writing a lot of classes like the below where there seems to be some kind of send method that just calls a bunch of other methods.  Is this bad practice?  Is there a more object oriented way to do this?

The below class uses User, UserDetail (preference object), and a custom Mailchimp api client.  It basically goes through creates a campaign, adds the html body to that campaign and then sends it.

# Generate and send mailers
class Mail
  def initialize(user_id)
    @user = User.find(user_id)
    @detail = @user.details
    @client = @user.mailchimp
    @campaign_id
  end

  def send
    create_campaign
    create_html_content
    send_campaign
  end

  private

  def create_campaign
    params = @detail.create_campaign_params
    @campaign_id = @client.create_campaign(params)
  end

  def create_html_content
    params = @detail.create_content_params
    @client.create_campaign_content(@campaign_id, params)
  end

  def send_campaign
    @client.send_campaign(@campaign_id)
  end
end


texts:
A few days ago I wanted to build a tools to cut a long text into one hundred-letters strings with the number of the tweets and which part of the tweets the tweet belongs to.

  I tried to have a tweet with one-hundred characters long but I could not find anything relevant to [1/x]

This is my tool:

import tweepy

n = 100

with open('texte-tw.txt','r') as input_file:
    abc = []
    for lines in input_file:
        abc.extend([lines[i:i+n] for i in range(0,len(lines),n)])

abc = [i.replace('\n','') for i in abc]

abc = list(filter(bool, abc))

ckey = 'xxx'
csecret = 'yyy'
atoken = 'uuu'
asecret = 'zzz'

auth = tweepy.OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
api = tweepy.API(auth)

for i,val in enumerate(abc):
        api.update_status(status=(val+' ['+str(i+1)+'/'+str(len(abc))+']'))

This is an extract of my file:

While it should be clear that we need a means of representing and reasoningabout uncertainty, it is not immediately obvious that probability theory can provideall of the tools we want for artiﬁcial intelligence applications.

Probability theorywas originally developed to analyze the frequencies of events. It is easy to seehow probability theory can be used to study events like drawing a certain hand ofcards in a game of poker.

These kinds of events are often repeatable. When wesay that an outcome has a probabilitypof occurring, it means that if we repeatedthe experiment (e.g., draw a hand of cards) inﬁnitely many times, then proportionpof the repetitions would result in that outcome.

Questions are the following:

What can I do to improve my solution?
Is there a way to cut with words that will be around one-hundred characters (+/- 10 characters) long tweet?
Are there way to package everything and that the solution will run with just one click (using Tkinter or something similar)?


texts:
This is a Leetcode problem -

  Strings A and B are K-similar (for some non-negative integer
  K) if we can swap the positions of two letters in A exactly K
  times so that the resulting string equals B.

  Given two anagrams A and B, return the smallest K for which A
  and B are K-similar.

  Note -

  1 <= A.length == B.length <= 20
  A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}

Here is my solution to this challenge -

import collections

class Solution:

    def find_first_mismatch(self, A, B):
        for i in range(len(A)):
            if A[i] != B[i]:
                return i

    def find_candidates(self, A, B, mismatch):
        for i in range(mismatch + 1, len(A)):
            if A[i] != B[i] and A[i] == B[mismatch]:
                yield Solution.swap(A, mismatch, i)

    def swap(A, i, j):
        chars = list(A)
        chars[i], chars[j] = chars[j], chars[i]
        return "".join(chars)

    def k_similarity(self, A, B):
        """
        :type A: str
        :type B: str
        :rtype: int
        """
        q = collections.deque([A])
        distance = 0
        seen = set([A])
        while q:
            for _ in range(len(q)):
                current = q.pop()
                if current == B:
                    return distance
                mismatch = self.find_first_mismatch(current, B)
                for candidate in self.find_candidates(current, B, mismatch):
                    if candidate not in seen:
                        seen.add(candidate)
                        q.appendleft(candidate)
            distance += 1

Here is my idea of how to solve the challenge -

The problem can be modeled as a graph problem of finding the shortest path between A and B where intermediate nodes are anagrams that can be formed by swapping 2 characters in a node having an edge to it. In other words, there is an edge between node X to node Y if Y can be formed by swapping 2 characters in X and vice-versa.

Here are some example outputs -

Example 1 -

Input: A = "ab", B = "ba"
Output: 1

Example 2 -

Input: A = "abc", B = "bca"
Output: 2

Example 3 -

Input: A = "abac", B = "baca"
Output: 2

Example 4 -

Input: A = "aabc", B = "abca"
Output: 2

Here is my Leetcode result (54 test cases) -

So, I would like to know whether I could make this program shorter and more efficient.

Any help would be highly appreciated.


texts: Learning C: K&R 1-22: 'fold'. Is this solution very unwieldy?

texts:
This exercise gave me some trouble, but I think I have a solution here that fulfils the spec. However it's very long compared to what I'm looking at on the solutions page at clc-wiki.net.

In the course of solving the problem I loop over my buffered line three or four times. This seems like quite a lot, and the various edge cases I have to account for means my prog is over 100 lines long (including spacing).

If anyone is kind enough to have patience to read it, I'd really appreciate your thoughts. Could it be shorter? Are there any glaring redundancies?

#include <stdio.h>

#define FOLD 20

int getline( char s[], int max );
int does_line_have_blanks( char line[], int len );
int locate_last_space ( char line[], int len );
int find_split( char line[], int len, int offset);
int mark_split_and_clean_trailing_spaces( char line[], int len, int spacecount );
int print_with_split( char s[], int split_pos );

int main()
{
  int len, i, split_pos, offset;
  char line[FOLD+1];

  len = split_pos = 0;
  for ( i = 0; i < FOLD+1; ++i )
    line[i] = 0;
  offset = 0; /*keeps track of characters that overflow the split so next buffer may be shortened accordingly*/

  while ( ( len = getline(line, (FOLD+1) - offset) ) > 0 ) {

    split_pos = find_split ( line, len, offset );

    offset = print_with_split( line, split_pos );

    /* clean out buffer*/
    for ( i = 0; i < FOLD+1; ++i )
      line[i] = '\0';
  }
  return 0;
}

int getline ( char s[], int max ) /* doesn't add newlines as K&R's does */
{
  int i, c;

  for (i = 0 ; i < max-1 && (c = getchar()) != EOF && c != '\n'; i++ )
    s[i] = c;
  s[i] = '\0';

  return i;
}

int find_split( char line[], int len, int offset ) {

    int i, split_pos, blanks;

    i = split_pos = 0;

    /*make sure there are blanks - if not the line will be printed as-is*/
    blanks = does_line_have_blanks( line, len );

    if ( len < FOLD-offset || blanks != 1 ) {
      split_pos = len; /* hit a newline or EOF - split at end of input */
    } else {
      split_pos = locate_last_space( line, len );
    }
    return split_pos;
}

int does_line_have_blanks( char line[], int len ) {

  int i, blanks;

  i = blanks = 0;

  for ( i = 0; i < len; ++i )  { /*len is at null char in line*/
    if (line[i] == ' ' )
      blanks= 1;
  }
  return blanks;
}

int locate_last_space ( char line[], int len ) {

  int i, inspace, spacecount, split_pos;

  inspace = spacecount = split_pos = 0;

  for ( i = 0; i < len; ++i ) {
    if ( line[i] == ' ' ) {
      inspace = 1;
      ++spacecount;
    } else {
      inspace = 0;
      if ( spacecount > 0 ) { /*if we leave inspace state, split on the preceding char */
        spacecount = 0;
        split_pos = i -1;
      }
    }
  }

  if ( inspace == 1 ) { /* and if we left in a space... */
    split_pos = mark_split_and_clean_trailing_spaces( line, len, spacecount );
  }

  return split_pos;
}

int mark_split_and_clean_trailing_spaces( char line[], int len, int spacecount ) {

  int split_pos, i;

  split_pos = i = 0;

  split_pos = len - spacecount; /*... split where it started */
  for ( i = split_pos + 1; i < len; ++i ) { /*nullify trailing spaces as per instructions*/
    line[i] = '\0';
  }

  return split_pos;
}

int print_with_split( char line[], int split_pos ) {

   int i, offset;

   offset = 0;

   for ( i = 0; i < split_pos; ++i ) {
      putchar(line[i]);
    }
    putchar('\n');
    for ( i = (split_pos + 1 ); line[i] != '\0'; ++i) /*miss out the space we split on*/ {
      ++offset; /* extra chars on next line, so buffer will be smaller */
      putchar(line[i]);
    }

    return offset;
}

EDIT Added a return type for main()


texts: Shorter way to write multiple if-then-else-if

texts:
if($row['best']){
    $id = $row['best'];
} elseif($row['average']){
    $id = $row['average'];
} elseif($row['bad']){
    $id = $row['bad'];
}

If row['best'] is null, I need $id to equal the 2nd best.  If 2nd best is null, $id is equal to the next best.


texts: Implementation of Factory Method pattern for XML and Excel documents

texts:
I started learning factory pattern, and also implemented factory method pattern.

My factory-method pattern classes are as follows:

There are two major parts, an interface "Document.java" and and abstract class DocumentCreator.java which has an abstract method createDocument.
Classes XMLDocumentCreator and ExcelDocumentCreator are subclasses of DocumentCreator.
Classes XMLDoc and ExcelDoc both are implementing Document interface.

Interface Document.java

public interface Document {
  String parse(String parser);
}

Classes implementing above interface

public class XMLDoc implements Document{

  @Override
  public String parse(String parser) {
    return "This XML document can be parsed by \""+parser+"\" parser";
  }

}

public class ExcelDoc implements Document{

  @Override
  public String parse(String parser) {
    return "This Excel document can be parsed by \""+parser+"\" parser";
  }

}

Abstract class DocumentCreator.java is as follows:

public abstract class DocumentCreator {
  public abstract Document createDocument();
}

Classes extending the above class is:

public class ExcelDocumentCreator extends DocumentCreator{

  @Override
  public Document createDocument() {
    return new ExcelDoc();
  }

}

public class XMLDocumentCreator extends DocumentCreator{

  @Override
  public Document createDocument() {
    return new XMLDoc();
  }

}

This is my MainClass.java from where I run the application:

public class MainClass {
  public static void main(String [] args){
    DocumentCreator docCreator = new XMLDocumentCreator();
    System.out.println(docCreator.createDocument().parse("SAX"));
    System.out.println(docCreator.createDocument().parse("DOM"));

    docCreator = new ExcelDocumentCreator();
    System.out.println(docCreator.createDocument().parse("MSDoc"));
    System.out.println(docCreator.createDocument().parse("OpenOffice"));
  }
}

When I run the class, I get:

This XML document can be parsed by "SAX" parser
This XML document can be parsed by "DOM" parser
This Excel document can be parsed by "MSDoc" parser
This Excel document can be parsed by "OpenOffice" parser

Now, after looking at my class hierarchy, is the implementation a proper usage of factory-method pattern? Is the "DocumentCreator creates document" problem solved properly by the pattern? Can you please give me any suggestions like where the factory-method pattern can be applied?


texts:
I am new to learning Repository Pattern. I am developing a site in MVC 4 (I'm new to it as well).

The way I am using Repository pattern as follows:

I created IRepository<T> interface as follows:

public interface IRepository<T>
{
    void Insert(T entity);
    void Delete(T entity);
    IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate);
    IQueryable<T> GetAll();
    T GetById(int id);
}

Then I created a base class which implements the above interface as follows:

public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
    private readonly DbContext _dbContext;

    public BaseRepository(DbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public void Insert(TEntity entity)
    {
        _dbContext.Set<TEntity>().Add(entity);
        _dbContext.SaveChanges();
    }

    public void Delete(TEntity entity)
    {
        _dbContext.Set<TEntity>().Remove(entity);
        _dbContext.SaveChanges();
    }

    public IQueryable<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate)
    {
        return _dbContext.Set<TEntity>().Where(predicate);
    }

    public IQueryable<TEntity> GetAll()
    {
        return _dbContext.Set<TEntity>();
    }

    public TEntity GetById(int id)
    {
        return _dbContext.Set<TEntity>().Find(id);
    }
}

Now for each repository I want to create, I create another interface which contains methods specific to that class. E.g if I want to create a CustomersRepository, I create an interface ICustomersRepository as follows:

public interface ICustomersRepository: IRepository<Customer>
{
    IEnumerable<Customer> GetRecentCustomers();
    Customer GetByName(string customerName);
}

Now I create CustomersRepository class, derive it from BaseRepository and  implement ICustomersRepository. as follows:

public class CustomersRepository : BaseRepository<Customer>, ICustomersRepository
{
    private readonly DbContext _dbContext;

    public CustomersRepository(DbContext dbContext) : base(dbContext)
    {
        _dbContext = dbContext;
    }

    public IEnumerable<Customer> GetRecentCustomers()
    {
        // implementation
    }

    public Customer GetByName(string customerName)
    {
        //
    }
}

Now to create another Repository, I will have to create an interface for it, then create that repository class which will implement BaseRepository<TEntity> class, implement the specific repository interface.

Issues:

This is not the proper way. But I use it as I get a BaseRepository<TEntity> which gives me many functions implemented.
I have to pass DbContext for each repository class.
not satisfied

Can you please guide me have this code corrected?  I will be using the same approach in almost all of my future projects where I use an ORM.


texts:
I've written a solution to the following leetcode problem:

  Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

  Example 1:
  Input: nums = [1,1,1], k = 2
  Output: 2

  Note:
  The length of the array is in range [1, 20,000].
  The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].

Solution:

class Solution:
    def subarraySum(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        T = ['inf' for _ in range(len(nums))]
        count = 0
        for i in range(len(nums)):
            for j in range(i,len(nums)):
                if j == i:
                    T[i] = nums[i]
                    if T[i] == k:
                        count +=1
                else:
                    currSum = T[j-1] + nums[j]
                    T[j] = currSum
                    if currSum == k:
                        count +=1
        return count

The solution passes 58/80 test cases, but for some reason, the solution is returning a Time Limit Exceeded exception on an input array with hundreds of elements. I implemented a dynamic programming solution using a 1d array to memoize previous sums so the algorithm should be efficient. Is there any way to optimize this?


texts:
Here is a partial example of a text tokenizer. I'm looking for ways to improve one particular line in this code:

  implicit class textFile(val fileName: String)  {

    def toDict() = {
      io.Source.fromFile(fileName).getLines.flatMap(_.split("\\\\r?\\\\n")).toList
    }
  }

  def filterComments(m: List[String]) : List[String] = m match {
    case Nil => List()
    case x :: xs => x.takeWhile(c => c != '#').trim :: filterComments(xs)
  }

  def filterEmptyLines(m: List[String]) : List[String] = m match {

    case Nil => List()
    case x :: xs => if(x.isEmpty) filterEmptyLines(xs) else x :: filterEmptyLines(xs)
  }

  def splitParameters(m: List[String]) : List[String] = m match {

    case Nil => List()
    case x :: xs => x.split("\\s*(=>|,|\\s)\\s*").map(_.trim).toList ++ splitParameters(xs)

  }

  val dict = "test.txt".toDict()
  println(splitParameters(filterEmptyLines(filterComments(dict))))

The line I would like to break up is the last one:

println(splitParameters(filterEmptyLines(filterComments(dict))))

Is there any idiomatic (Scala) way to make this more readable but keeping it compact, on the same line preferable?


texts:
As an exercise, I've written quicksort algorithm in C using pointers. Please comment and help me find the cases where it breaks (if any).

void qsort(int *, int, int);
void swap(int *, int *);

void qsort(int *v, int left, int right)
{
    int i, *last;

    if (right <= left)
        return;
    last = v + left; //choosing the first element as the pivot
    for (i = left + 1; i <= right; i++)
        if (*(v + i) < *(v + left))
            swap(++last, v + i); //swapping the numbers < pivot
    swap(v + left, last);
    qsort(v, left, last - v - 1); //sub-array containing numbers < pivot
    qsort(v, last - v + 1, right); //sub-array containing numbers >= pivot
}

void swap(int *i, int *j)
{
    int temp;

    temp = *i;
    *i = *j;
    *j = temp;
}


texts: Validating email confirmation and sending different emails to admins

texts:
I wrote a method that will confirm email with token send to user emails. It will also send different email depending on the condition for the registered user. If anyone would write the same method, how would you write this code snippet?

[AllowAnonymous, Route("confirm-email", Name = AccountControllerRoute.GetConfirmEmail)]
public async Task<ActionResult> ConfirmEmail(int accountId, string token)
{
    if (accountId == 0 || token == null)
        return HttpNotFound();

    var account = await AccountManager.FindByIdAsync(accountId);

    if (account == null)
        return HttpNotFound();

    if (account.EmailConfirmed)
        return View("ConfirmEmail"); //return page that will say the token has expired

    var result = await AccountManager.ConfirmEmailAsync(accountId, token);

    var emailBody = string.Empty;
    if (result.Succeeded)
    {
        if(account.Institution == null)
        {
            // send message notifying admin that a new institution needs review and setup
            emailBody = $"{account.UserName}|{account.profile.contact}|{account.profile.institutionName}|{account.profile.institutionAddress}";
            await AccountManager.SendEmailAsync(account.Id, EmailNames.NewInstitutionRequest, "");

            return View("ConfirmEmail");
        }

         // Pass Name, Username
        emailBody = $"{account.Name}|{account.UserName}";

        if (account.Institution.IsClient)
        {
            if (account.Institution.Licence.AllowedAccounts == 0)
            {
                await AccountManager.SendEmailAsync(account.Id, EmailNames.NewUserRequest, emailBody);
                return View("ConfirmEmail"); // send a different message notifying admin about exceeding allow accounts
            }

            // send message notifying admin that a new user request for access
            await AccountManager.SendEmailAsync(account.Id, EmailNames.NewUserRequest, emailBody);
            return View("ConfirmEmail");
        }
        else
        {
            // send message notifying admin that a new user has registered
            await AccountManager.SendEmailAsync(account.Id, EmailNames.NewUserNotificationForSA, emailBody);

            return View("ConfirmEmail");
        }
    }

    return View("Error");
}

Also, could there be a better way to just pass an object rather than a string with a separator that I have to use to parse them, once passed to a method where it send an email to the user?

public class PostalEmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        if (message == null)
            throw new ArgumentNullException("message");

        EmailBase email;
        switch (message.Subject)
        {
            case EmailNames.ResetPassword:
                email = new ResetPasswordEmail {To = message.Destination, Link = message.Body};
                break;

            case EmailNames.NewUserRequest:
                string name = message.Body.Split('|')[0];
                string userName = message.Body.Split('|')[1];
                email = new NewUserRequestEmail { To = "dummy@gmail.com", Name = name, UserName = userName };
                break;
            default:
                throw new InvalidOperationException("Unknown email '{0}'.".FormatWith(message.Subject));
        }

        return email.SendAsync();
    }
}


texts: Scrape google apps page and store application details in database

texts:
Below is a python script which scrapes specific google apps url for example https://play.google.com/store/apps/details?id=com.mojang.minecraftpe and save the developer email,application name and price in mysql database with database name "app_database" and inside table named "app_table". There are lot of data type conversions and workarounds here and i think that might not come under best practices. How can I improve this.

from lxml import html
import requests
import MySQLdb
conn = MySQLdb.connect("localhost","root","pA$$W0rd","app_database")
cursor=conn.cursor()
class AppCrawler:

    def __init__(self,starting_url,depth):
        self.starting_url=starting_url
        self.depth=depth
        self.current_depth=0
        self.depth_links=[]
        self.apps=[]

    def crawl(self):
        app=self.get_app_from_link(self.starting_url)
        self.apps.append(app)
        self.depth_links.append(app.links)

        while self.current_depth < self.depth:      #0 < 0
            current_links=[]
            for link in self.depth_links[self.current_depth]:
                current_app = self.get_app_from_link(link)
                current_links.extend(current_app.links)
                self.apps.append(current_app)
            self.current_depth += 1
            self.depth_links.append(current_links)

        return

    def get_app_from_link(self,link):
        start_page=requests.get(link)
        #print start_page.text
        tree = html.fromstring(start_page.text)
        # the tree.xpath(...) gives an array
        name = tree.xpath('//div[@class="id-app-title"]/text()')[0]

        #developer=tree.xpath('//a[@class="dev-link"]//*/div/@href')
        temp_addr=tree.xpath('//a[contains(@href,"mailto") and @class="dev-link"]/@href')[0]
        developer=temp_addr.split(':')

        price=tree.xpath('//button[@class="price buy id-track-click id-track-impression"]/span[2]/text()')
        temp_price=price[0].split()
        price=str(temp_price[0].lstrip('$'))
        if price=="Install":
            price="0"
        templinks = tree.xpath("//a[@class='title']/@href")

        links=[]
        length=len(templinks)
        domain="https://play.google.com"
        for i in templinks:
            i="https://play.google.com"+i
            links.append(i)
#        print links
        #print len(testlinks)
        #links = [h for h in tree.xpath("//a[@class='title']/@href")]
        #print links
        #return name,developer[1],price,links
        app = App(name,developer[1],price,links)
        return app

class App:
    def __init__(self, name,developer,price,links):
        self.name=name
        self.developer=developer
        self.price = price
        self.links=links

    def __str__(self):
        return(self.name.encode('UTF-8')+"|"+
        self.developer.encode('UTF-8')+"|"+
        self.price.encode('UTF-8'))

crawler = AppCrawler('https://play.google.com/store/apps/details?id=com.mojang.minecraftpe',0)
crawler.crawl()
#print type(crawler.apps)

query="INSERT INTO app_table (name,developer,price) values('%s','%s','%d ')"
for app in crawler.apps:
    data=str(app).split('|',2)
    price=float(data[2])
    try:
        cursor.execute(query % (data[0],data[1],price))
        print "OK"
        conn.commit()
    #row=x.fetcha
    except:
        print "Failed"
        conn.rollback()
    print data[0],data[1],price


texts:
The program is separated in main.cpp, quicksort.cpp, partition.cpp and swap.cpp. There is also a header file to each .cpp file. The idea is from this site.

As you can see, I tried to be very modular. I think instead of having swap.cpp I could've simply used std::swap, right?

But the more important thing is, that I'd like to know how I can improve this quicksort algorithm or how to improve my program in general (not only the algorithm itself).

My three .hpp files are looking like this:

#ifndef __qs__name__
#define __qs__name__

#include <vector>

//protype

#endif

partition.cpp:

#include "partition.hpp"
#include "swap.hpp"

int partition(std::vector<int>& v, int ileft, int iright, int ipivot)
{
  int i = ileft, j = iright;

  while (i <= j)
  {
    while (v[i] < ipivot)
      i++;

    while (v[j] > ipivot)
      j--;

    if (i <= j)
    {
      swap(v, i, j);
      i++;
      j--;
    }
  }

  return j;
}

quicksort.cpp:

#include "quicksort.hpp"
#include "partition.hpp"

void quicksort(std::vector<int>& v, int ileft, int iright)
{
  int p, ipivot = v[iright]; // v[(ileft + iright) / 2];

  p = partition(v, ileft, iright, ipivot);

  if(ileft < p)
    quicksort(v, ileft, p);

  if((p + 1) < iright)
    quicksort(v, p + 1, iright);
}

swap.cpp:

#include "swap.hpp"

void swap(std::vector<int>& v, int i, int j)
{
  int temp = v[i];

  v[i] = v[j];
  v[j] = temp;
}

main.cpp:

#include <vector>

#include "partition.hpp"
#include "quicksort.hpp"
#include "swap.hpp"

int main()
{
  std::vector<int> v;
  v.push_back(7);
  v.push_back(10);
  v.push_back(11);
  v.push_back(1);
  /*v.push_back(-4543);
  v.push_back(88);
  v.push_back(5);
  v.push_back(3);
  v.push_back(31);
  v.push_back(34);
  v.push_back(7);*/

  for (int i = 0; i < v.size(); i++)
    std::cout << v[i] << std::endl;

  quicksort(v, 0, v.size()-1);
  std::cout << "\n";

  for (int i = 0; i < v.size(); i++)
    std::cout << v[i] << std::endl;

  return 0;
}


texts:
I'm learning F# and have a couple of routines much of whose functionality looks common so I am looking to refactor them together.

Here are the routines (which for the record I lifted from elsewhere:

let Prices time ID (polling:float) =
  let sync = System.Threading.SynchronizationContext.Current
  let obs = new Event<int>()
  let raiseEvent (value:int) = sync.Post((fun _ -> obs.Trigger(value)), null)
  let interval = TimeSpan.FromSeconds(polling)
  let rec loop nextTime= async {
    // Generate next value (on the GUI thread)
    do getPrices nextTime
      |> List.filter (fun price -> price.ID = ID)
      |> List.iter (fun price -> raiseEvent price.Price)
    // Wait some short time
    do! Async.Sleep(1000)
    // Continue looping
    do! loop (nextTime.Add(interval)) }
    loop time |> Async.Start
    obs.Publish

and

let Volumes time ID (polling:float) =
  let sync = System.Threading.SynchronizationContext.Current
  let obs = new Event<VolumeDTO>()
  let raiseEvent (value:VolumeDTO) = sync.Post((fun _ -> obs.Trigger(value)),  null)
  let interval = TimeSpan.FromSeconds(polling)
  let rec loop nextTime= async {
    // Generate next value (on the GUI thread)
    do getVolumes nextTime
      |> List.filter (fun volume -> volume.ID = ID)
      |> List.iter (fun volume -> raiseEvent volume)
    // Wait some short time
    do! Async.Sleep(1000)
    // Continue looping
    do! loop (nextTime.Add(interval)) }
    loop time |> Async.Start
    obs.Publish

The external function calls are hopefully reasonable self explanatory retrieving data from a db

let getPrices lastTime =
  createList<PriceDTO>((getPriceSql lastTime), priceReader)

let getVolumes lastTime =
  createList<VolumeDTO>((getVolumeSql lastTime), volumeReader)

The types are very simple, but I'm not sure I've got this 'right':

type IID =
  abstract member ID : int64

type PriceDTO(ID:int64, Price:int) =
  interface IID with
    member this.ID = ID
  member x.ID = ID
  member x.Price = Price

type VolumeDTO(ID:int64, Amount:decimal, Price:int32) =
  interface IID with
    member this.ID = ID
  member x.ID = ID
  member x.Amount = Amount
  member x.Price = Price

I've played around with something but I'm not at all there as yet:

let publish<'a> time ID (polling:float) dataRetriever =
  let sync = System.Threading.SynchronizationContext.Current
  let obs = new Event<'a>()
  let raiseEvent (value:'a) = sync.Post((fun _ -> obs.Trigger(value)), null)
  let interval = TimeSpan.FromSeconds(polling)
  let rec loop (nextTime:DateTime)= async {
    // Generate next value (on the GUI thread)
    do dataRetriever nextTime
      |> List.filter (fun item -> item.ID = ID)
      |> List.iter (fun item -> raiseEvent item)
    // Wait some short time
    do! Async.Sleep(1000)
    // Continue looping
    do! loop (nextTime.Add(interval)) }
    loop time |> Async.Start
    obs.Publish

I may well have not quite implemented this correctly. I'm obviously getting a list of prices or volumes filtering them and publishing them. In order to achieve the filtering I've created an interface, but I'm not really happy with this since I'd prefer to keep the DTOs as simple as possible. Intellisense is telling me that my dataRetriever function takes a DateTime and returns an 'a list, but I'm not sure I quite want that since I need to cast it to an IID interface for filtering and then back to the underlying DTO for publishing.

Is there a more 'functional' (or perhaps just better) way to do this?


texts: How to simplify nested conditional for loop in R?

texts:
I have this for loop for creating a list of variables:

vars <- c( 'beta.o', 'sd.y')
for (x in c('ghs', 'site', 'trt')) {
  if(model.parms[[x]] == 1) {
    data <- data[, which(names(data) != x)]
  } else {
    data <- data
    if(x!='ghs') {
      vars <- c(vars, paste('sd.', x, sep = ''))
    }
    m <- min(model.parms[[x]], 5)
    for (i in 1:m) {
      if(i == 1 && x == 'site') {
        vars <- c(vars, 'beta.site[1]')
      }
      if (i > 1) {
        vars <- c(vars, paste('beta.', x, '[', i, ']', sep=''))
      }
    }
  }
}

It has been bothering me terribly, and I have failed the last two times I have tried to replace it, although conceptually it should be able to be written in a few lines. Any advice?


texts: CPU friendly infinite text scroller - velocity

texts:
I've done some research today and read a lot about JavaScript performance. I learned that animating using the transform: translate makes a use of the GPU, hence reducing the CPU usage.

I've also read about velocity.js which seems to be a replacement for $.animate().
I've tried to combine these two and create an efficient infinite text-scroller.

My attempt: http://jsfiddle.net/umtzzmfh/

var PPS = 50;

// the function that animates the list.
var startAnimation = function(list) {
    var firstChild = list.children().first();

    var height = firstChild.outerHeight(true);
    console.log(height);
    var duration = figureNextIterationLength(height);

    list.velocity({
        translateY: [-height, 0]
    }, {
        duration: duration,
        easing: 'linear',
        complete: function() {
            prepareNextIteration(list);
        }
    });
};

// resets the list: removes first child and append to the end of the list
// returning the list back to it's start point
var prepareNextIteration = function(list) {
    // getting the list back to translateY: 0
    list.css({
        transform: 'translate(0, 0)'
    });

    // detaching and attaching the node again
    list.append(list.children().first().detach());

    startAnimation(list)
}

var figureNextIterationLength = function(height) {
    // returns the amount of pixels per MS based on PPS (PixelsPerSecond).
    return height / (PPS / 1000);
}

Few notes:

I have no clue why the other 2 lists are down the page, I didn't bother looking into it very much since it's not what I was testing. I was trying to test CPU usage.
The way the code "works" is by getting the amount of pixels it has to scroll, calculating how many seconds it should take (we want to keep a linear animation after all) and after animation removing the element that is outside the view and puts it at the end of the list (also reverts back to translate(0, 0)).

When viewing this code in Chrome it seems to be fine. I will need to actually view the code in some other weaker machines. However, when viewing it in IE11, the animated text becomes "wavy" it likes the text is moving in some wave style.

What is your opinion about this implementation?
Why does it look wavy-weird on IE11?
Do you have any other idea how I can implement an infinite text-scroller that is very CPU friendly?


texts:
I have the below code to check for a winner in a Tic Tac Toe game.  I'm wondering if this is a good approach, and if there is a better way of doing this (maybe by monitoring the state of the board).

public void CheckWinner()
{
    var board = GameBoard.Content;//stores data as dictionary
    for (int i = 0; i < 8; i++)
    {
        int a = winningIndex[i, 0], b = winningIndex[i, 1], c = winningIndex[i, 2];

        if (board.ContainsKey(a) && board.ContainsKey(b) && board.ContainsKey(c))
        {
            Func<KeyValuePair<int, string>, bool> containsO = input => (input.Key == a || input.Key == b || input.Key == c) && input.Value == "O";

            Func<KeyValuePair<int, string>, bool> containsX = input => (input.Key == a || input.Key == b || input.Key == c) && input.Value == "X";

            var oResult = board.Where(containsO).ToDictionary();
            if (oResult.Count == 3)
            {
                Winner = Player.Computer;
                WinnerIndex = oResult.Keys.ToArray();
                _gameOver = true;
            }

            var xResult = board.Where(containsX).ToDictionary();
            if (xResult.Count == 3)
            {
                Winner = Player.Human;
                WinnerIndex = xResult.Keys.ToArray();
                _gameOver = true;
            }
        }
    }
    if (board.Count == 9)
    {
        _gameOver = true;
        Winner = null;
    }
}


texts: Factory using Structuremap to inject concrete implementation of BaseTask

texts:
The problem statement that I am trying to solve is this;

We will retrieve a list of Tasks from an external repository. These tasks will have a type and for each type, there needs to be a specific implementation that defines the Model it needs it order to perform the Task and then the logic needed to Process the task, along with any dependencies.

At one point, the Factory was setup to use Generics, but reverted to this as it seemed simpler.

 public interface ITask
 {
     bool ProcessTask();
 }

 public interface ITask <TModel>
 {

 }

 public interface IModelBuilder <out TModel>
 {
     TModel Build(ITaskDefinition taskDefinition);
 }

 public abstract class BaseTask <TModel> : ITask<TModel> , ITask where TModel: IModel
 {
     public TModel Model {  get; set;}

     public ITaskDefinition TaskDefinition { get; set; }

     protected BaseTask(ITaskDefinition definition, IModelBuilder <TModel> builder)
     {
         Model = builder.Build(definition);
     }

     public abstract bool ProcessTask();
 }

 public interface ITaskFactory
 {
     ITask GetTask(ITaskDefinition taskDefinition, TaskType type);
 }

Concrete classes

public class TaskFactory: ITaskFactory
{
    private readonly IContainer _container;

    public TaskFactory(IContainer container)
    {
        _container = container;
    }

    public ITask GetTask(ITaskDefinition taskDefinition, TaskType type)
    {
        return _container
            .With(taskDefinition)
            .GetInstance < ITask > (type.ToString());
    }
}

public class DateTask: BaseTask < DateModel >
{
    private readonly ITaskManagementFacade _taskManagementFacade;

    public CaptureCompletionDateTask(ITaskDefinition taskDefinition, IModelBuilder < DateModel > builder, ITaskManagementFacade taskManagementFacade): base(taskDefinition, builder)
    {
        _taskManagementFacade = taskManagementFacade;
    }
    public override bool ProcessTask()
    {
        return _taskManagementFacade.CompleteTask(Model.SomeDate < DateTime.Now);
    }

}

public class DateModelBuilder: IModelBuilder < DateModel >
{
    private readonly ITaskManagementFacade _taskManagementFacade;

    public DateModelBuilder(ITaskManagementFacade taskManagementFacade)
    {
        _taskManagementFacade = taskManagementFacade;
    }

    public DateModel Build(ITaskDefinition taskDefinition)
    {
        var model = new DateModel
        {
            MyDate = DateTime.Now,
                Notes = _taskManagementFacade.GetTask((Guid.NewGuid()))
        };

        return model;
    }
}

public class DateModel: IModel
{
    public DateTime MyDate
    {
        get;
        set;
    }

    [Required]
    public string Notes
    {
        get;
        set;
    }
}

StructureMap container config

config.For<ITask>().Use<CaptureCompletionDateTask>().Named(TaskType.Task1.ToString());
config.For<IModelBuilder<DateModel>>().Use<DateModelBuilder>();

Code to get Task

var task = _taskFactory.GetTask(taskDefinition, TaskType.Task1);

What am I looking for? General comments, this is my first attempt at this kind of object construction. It would be great if I could get around the Named config in Structuremap and go back to using a Generic Factory to Load a Task based on TaskType.

Thanks


texts:
I'm working my way through a book on algorithms. One of the first challenges is the merge sort. I've seen this implemented before, but as the book suggested I coded one from just the method without looking at the code. When I looked it was very different from mine. Many online examples match the way the book does it.

I am asking about this because I want to learn the reasons this code is better, or if there are trade-offs, or things that are merely style choices, rather than just adopting the pattern of the example code.

Here's mine in Python:

A = [1,5,7,3,4,3,1,8,9,12,64,22,83,223,11]

def merge(A1,A2):
    B1 = []
    while(len(A1)>0 and len(A2) > 0):
        if(A1[0] < A2[0]):
            B1.append(A1.pop(0))
        else:
            B1.append(A2.pop(0))
    B1 = B1 + A1 + A2
    return B1

def mergesort(B):
    if len(B)>1:
        q = len(B)//2
        B1 = mergesort(B[:q])
        B2 = mergesort(B[q:])
        return merge(B1,B2)
    else:
        return (B)

print("mergesort(A) =",mergesort(A))

Here's an example a lot like in my book and in many websites, from GeeksForGeeks

  Python program for implementation of MergeSort

# Merges two subarrays of arr[].
# First subarray is arr[l..m]
# Second subarray is arr[m+1..r]
def merge(arr, l, m, r):
    n1 = m - l + 1
    n2 = r- m

    # create temp arrays
    L = [0] * (n1)
    R = [0] * (n2)

    # Copy data to temp arrays L[] and R[]
    for i in range(0 , n1):
        L[i] = arr[l + i]

    for j in range(0 , n2):
        R[j] = arr[m + 1 + j]

    # Merge the temp arrays back into arr[l..r]
    i = 0     # Initial index of first subarray
    j = 0     # Initial index of second subarray
    k = l     # Initial index of merged subarray

    while i < n1 and j < n2 :
        if L[i] <= R[j]:
            arr[k] = L[i]
            i += 1
        else:
            arr[k] = R[j]
            j += 1
        k += 1

    # Copy the remaining elements of L[], if there
    # are any
    while i < n1:
        arr[k] = L[i]
        i += 1
        k += 1

    # Copy the remaining elements of R[], if there
    # are any
    while j < n2:
        arr[k] = R[j]
        j += 1
        k += 1

# l is for left index and r is right index of the
# sub-array of arr to be sorted
def mergeSort(arr,l,r):
    if l < r:

        # Same as (l+r)/2, but avoids overflow for
        # large l and h
        m = (l+(r-1))/2

        # Sort first and second halves
        mergeSort(arr, l, m)
        mergeSort(arr, m+1, r)
        merge(arr, l, m, r)

# Driver code to test above
arr = [12, 11, 13, 5, 6, 7]
n = len(arr)
print ("Given array is")
for i in range(n):
    print ("%d" %arr[i]),

mergeSort(arr,0,n-1)
print ("\n\nSorted array is")
for i in range(n):
    print ("%d" %arr[i]),

# This code is contributed by Mohit Kumra

First, I notice this is manipulating the initial list in place by tracking indices, and I am creating several copies of the data.

I would suppose that I am using more memory, however the example posted lower does create temporary lists to perform the merge. But there they also use index tracking instead of destroying the two arrays with pop as they are consumed.

So again, I'm trying to learn why the differences matter. Which is more educational than just taking on this way of doing it.


texts: How to quickly work a linechart created with jfreechart

texts:
Currently I use jfreechart through swt.
The current problem is tens of thousands of files (currently between 10,000 and 20,000, up to 100,000 later)

I currently use DefaultXYDataset and XYLineAndShapeRenderer to load data after opening a file.

There are a few problems:

The chart on the screen is not immediate (within seconds). Work with setchart in the current file open listener.
When resizing the shell, there is a delay in drawing the chart. This causes the program to run slowly.
The jfreechart zoom function and mouse drag events are slow.

My questions:

Is there a quick way to draw when redrawing a new data chart in chartcomposite?
Essentially, is XYLineAndShapeRenderer not good at rendering tens of thousands of data? If so, what is the best method?
There is a slight speedup using fastscatterplot but I want the line to be a line rather than a dot. Is there a way to line fastscatterplot?

This is my test code. The screen works very slowly even with 100,000 datasets. Are there speed issues in your current code?

import java.awt.BasicStroke;
import java.awt.Color;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.experimental.chart.swt.ChartComposite;

public class TestGui {

private JFreeChart chart;
private ChartComposite frame;

public static void main(String[] args) {
try {
    TestGui window = new TestGui();
    window.open();
} catch (Exception e) {
    e.printStackTrace();
}
}

public void open() {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setSize(600, 400);
shell.setText("SWT Application");
shell.setLayout(new GridLayout(1, false));

// create
chart = createChart(createDataset(getSeries()));
    // using chartcomposite
frame = new ChartComposite(shell, SWT.NONE, chart, true);
frame.setDisplayToolTips(false);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.widthHint = 600;
data.heightHint = 366;
frame.setLayoutData(data);

shell.open();
shell.layout();
while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) {
        display.sleep();
    }
}
}

//making chart
private JFreeChart createChart(XYDataset dataset){
JFreeChart chart = ChartFactory.createXYLineChart(
        "", "x", "y", dataset,
        PlotOrientation.VERTICAL, false, false, false);

XYPlot plot = chart.getXYPlot();

XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false);
renderer.setSeriesPaint(0, Color.RED);

int seriesCount = plot.getSeriesCount();
  for (int i = 0; i < seriesCount; i++) {
    plot.getRenderer().setSeriesStroke(i, new BasicStroke(0));
  }

plot.setRenderer(renderer);
plot.setBackgroundPaint(Color.white);
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.BLACK);
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.BLACK);

return chart;
}

private XYDataset createDataset(double[] x) {
XYSeries series = new XYSeries("");
for(int i = 0; i<x.length; i++) {

    series.add(i,x[i],false);
}
    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series);
    return dataset;
}

private static double[] getSeries() {

double[] series = new double[104857];
for(int i = 0; i < series.length; i++) {
    series[i] = Math.sin(i * 33 * Math.PI / series.length) + Math.sin(i * 15 * Math.PI / series.length);
}
return series;
}

}


texts: Encryption/decryption by matrix multiplication in C

texts:
I need to encrypt and decrypt text, image and video with a program in C with a matrix.

My program is working but it's really slow, how can I improve its speed ?
specially for the decrypt part.

Each matrix have an identity part and a filling part.

For encryption, I only do a matrix multiplication of each hex of ascii I get from files.

There is my code for decryption:

In source_decode I have the name of the file I need to decrypt.

In matrice_identity I have an array that contain the order of the matrix identity hidden in the matrix.
For example for a matrix like:

10001111
11000111
10100100
10010010

The matrice_identity will contain {4,1,2,3}, because the column 4 (where columns are numbered starting from 0) is 1000, the column 1 is 0100, column 2 is 0010, and column 3 is 0001. Other columns are just here to fill.

void decode_it(char* source_decode ,int* matrice_identity){

    FILE* source_file = NULL;
    source_file = fopen(source_decode, "rb");

    // that's to make difference between the original one, and the decrypted one.

    char* exetention = "d";
    strcat(source_decode, exetention);

    FILE* decoded_file = NULL;
    decoded_file = fopen(source_decode, "wb");

    if(decoded_file != NULL){
    int carac;

        if(source_file != NULL){
            int i = 0;
            int j = 0;
            int octet1[8] = {0};
            int octet2[8] = {0};
            int result[8] = {0};

            //encrypted file are writed in acsii, so here, i get an ascii number.
            carac = fgetc(source_file);

            while(feof(source_file) == 0){

                 //this function convert an ascii number to a byte.
                 convert_hex_bin_v2(carac, octet1);
                 carac = fgetc(source_file);
                 convert_hex_bin_v2(carac, octet2);

                 //This reason of why i'm getting 2 carac is beacause that I need to decrypt 2 bytes to have the original ascii.

                //For example a letter 'a' encrypted give me 2 bytes.

            for(j=0; j<4; j++){
                for(i=0; i<8; i++){
                    if(i == matrice_identity[j]){
                        result[j] = octet1[i];
                        result[j+4] = octet2[i];
                    }
                }
            }

            int k = 0;
            int printFile = 0;
            for(i=7; i>=0; i--){
                printFile += result[i] * pow(2.,(double)k);
                k++;
            }
            fprintf(decoded_file, "%c", printFile);

            carac = fgetc(source_file);
        }
        fclose(decoded_file);

        }else{
        printf("read failed");

        }

        fclose(source_file);

    }else{
        printf("output creation failed");
    }
}

And there is the function that convert hex to binary:

void convert_hex_bin_v2(int carac, int* octet){

        int x;
          for (x = 0; x < 8; x++) {
            octet[x] = 0;
          }

    if(carac >= 128){

     octet[0]=1;
     carac -=128;
    }
    if(carac>=64){

     octet[1]=1;
     carac -=64;
    }
    if(carac>=32){
     octet[2]=1;
     carac -=32;
    }
    if(carac>=16){
     octet[3]=1;
     carac -=16;
    }
    if(carac>=8){
     octet[4]=1;
     carac -=8;
    }
    if(carac>=4){
     octet[5]=1;
     carac -=4;
    }
    if(carac>=2){
     octet[6]=1;
     carac -=2;
    }
    if(carac>=1){
     octet[7]=1;
     carac -=1;
    }
}


texts: Disruptor pattern in a lock-free producer-consumer queue

texts:
I'm implementing a quick-and-dirty lock-free MPMC queue in C++, based on the v3 Disruptor algorithm. The implementation is pretty simple:

Data to be shared between threads is stored in a ring buffer.
The ring buffer maintains a sequence number that points to the next available slot.
Consumer threads maintain their own sequence numbers indicating their current read position.
Producer threads claim a slot in the buffer by atomically incrementing its sequence number. To avoid overwriting data that is yet to be read, the producers block until every consumer has caught up (implementation omitted for brevity).
Once a producer has finished writing to its slot, it stores the current queue generation into a second ring buffer to indicate that the data has been published.
Consumers block until the published buffer contains the expected generation count, indicating that data is ready to be used.

I'm a bit fuzzy on memory barriers, and wanted to check that I'm using them appropriately. Writes to the data buffer must be completed before corresponding writes to the published buffer. I use a release barrier when writing to the published buffer, and an acquire barrier when reading from it. Is this correct?

template <class T, std::size_t n>
class mpmc_ring_buffer {
  static constexpr auto length = next_power_of_2(n);
  static constexpr auto mask = length - 1;
  static constexpr auto shift = log2(length);

public:
  ring_buffer () {
    for (std::size_t i = 0; i < length; ++i)
        published[i] = -1;
  }

  auto push (T && t) {
    auto seq = sequence.fetch_add(1, std::memory_order_relaxed);
    // TODO: Check for wraparound and spin while consumers catch up
    data[seq & mask] = std::forward<T &&>(t);
    published[seq & mask].store(seq >> shift, std::memory_order_release);
    return seq;
  }

  T peek (std::size_t seq) {
    while (published[seq & mask].load(std::memory_order_acquire) != static_cast<std::uint_fast8_t>(seq >> shift));
    return data[seq & mask];
  }

private:
  std::atomic_size_t sequence alignas(CACHE_LINE_SIZE) {};
  T data alignas(CACHE_LINE_SIZE) [length];
  std::atomic_uint_fast8_t published alignas(CACHE_LINE_SIZE) [length];
};


texts: Any better way to copy from Readable to OutputStream?

texts:
I neglected java.nio package in the past and now I am wondering if this is the best way to copy everything from a Readable to an OutputStream:

public static void copy(@Nonnull Readable src, @WillNotClose @Nonnull OutputStream dest, @Nullable Charset targetCharset) throws IOException {
    targetCharset = safeCharset(targetCharset);
    CharBuffer buf = CharBuffer.allocate(CHAR_BUF_SIZE);
    ByteBuffer byteBuffer = null;
    WritableByteChannel outChannel = Channels.newChannel(dest);

    while (src.read(buf) != -1) {
      buf.flip();
      byteBuffer = targetCharset.encode(buf);
      outChannel.write(byteBuffer);
      buf.clear();
    }
}

General

Is this the best way to do it? Am I missing something? What should I do to improve this code?

Closing the channel or not

I wonder if I must/should close the channel created by Channels.newChannel(dest)?
From the general contract of my class the caller of this method here is responsible for closing the provided OutputStream, because every resource should be closed where it was created. It's bad that closing the channel also closes the underlying stream, too.

On the other hand not closing the channel relies on undocumented implementation details of Channels$WritableByteChannelImpl where calling close() is just a delegate to the underlying stream and setting a close flag.

I do not want to break my general contract and I do not want to rely on undocumented implementation details.

The third option is most probably the best and addressing the source of the problem:
Write the content directly to the OutputStream without using a channel or any other wrapper around it which must be closed then. But I could not figure out how to achieve this.


texts: Combinable filters for Python Pandas dataframe structure

texts:
This is an extension of my other question here. I have applied suggestions from 200_success in real-life problem at hand that deals with modelling extensible and combinable filter on Python Pandas dataframe structure. Is the code below reasonable and extensible, and are there areas I could improve this code on?

import pandas as pd

# Create df
######################################################################
data = [['tom', 'M', 10, 160], ['nick', 'M', 15, 155], ['juli', 'F', 14, 123], ['tom', 'M', 13, 170],
    ['susan', 'F', 17, 99], ['trish', 'F', 45, 165], ['nash', 'M', 13, 213], ['nick', 'M', 15, 170],
    ['tom', 'M', 12, 188], ['trish', 'F', 16, 170]]

df = pd.DataFrame(data, columns=['Name', 'Sex', 'Age', 'Weight'])

''' Name Sex      Age  Weight
0    tom   M       10     160
1   nick   M       15     155
2   juli   F       14     123
3    tom   M       13     170
4  susan   F       17      99
5  trish   F       45     165
6   nash   M       13     213
7   nick   M       15     170
8    tom   M       12     188
9  trish   F       16     170'''

# Primitive filters
######################################################################
def age_at_least(var='Age', threshold=12):
    def execute(df):
        return df[df[var]>=threshold]
    return execute

def age_at_max(var='Age', threshold=30):
    def execute(df):
        return df[df[var]<=threshold]
    return execute

def remove_by_sex(var='Sex', threshold='M'):
    def execute (df):
        return df[df[var]!=threshold]
    return execute

def weight_at_least(var='Weight', threshold=50):
    def execute (df):
        df['Weight_kg'] = df[var].apply(lb_to_kg)
        return df[df['Weight_kg']>=threshold]
    return execute

def lb_to_kg(lb):
    return lb/2.20462

# Higher order function
######################################################################
def compose(*filters):
    def composed(df):
        for f in filters:
            if f is not None:
                df = f(df)
        return df
    return composed

def item_filtering(
        filter_age_at_least = age_at_least(threshold=15),
        filter_general = compose(age_at_max(), remove_by_sex(threshold='F')),
        filter_specific = weight_at_least(),
    ):
    return compose(filter_age_at_least, filter_general, filter_specific)

# Demonstration
######################################################################
ob = item_filtering()
print(ob(df))

# Output
'''    Name Sex  Age  Weight  Weight_kg
    1  nick   M   15     155  70.306901
    7  nick   M   15     170  77.110795
'''


texts:
Could someone suggest ways to simplify this code, or any other tips? All help is much appreciated. Moreover, should I comment code above or below the line of code (I have to comment as this is a requirement)?

#This imports the time and random modules
import time, random

#This defines a function called comment
def comment():
    #This makes the variable startage a global variable rather than a local variable
    global startage
    #This checks if choice is equal to 1 or equal to 2
    if choice == 1 or choice == 2:
        if startage < 25:
            print("You're a young cat or dog")
        elif startage > 25 and startage < 50:
            print("You're a middle aged cat or dog")
        elif startage > 50:
            print("You're an old cat or dog")
    elif choice == 3 or choice == 4:
        if newage < 25:
            print("You're a young cat or dog")
        elif newage > 25 and startage < 50:
            print("You're a middle aged cat or dog")
        elif newage > 50:
            print("You're an old cat or dog")
    else:
        pass

print("Welcome to the animal age calculator")
while True:
    time.sleep(1)
    choice = int(input("Which age would you like to convert?(Enter the corresponding number)\n1. Human ---> Dog\n2. Human ---> Cat\n3. Dog ---> Human\n4. Cat ---> Human\n5. Generate a random human age\n"))
    time.sleep(1)
    if choice >= 1 and choice <= 4:
        startage = int(input("What age would you like to start with?\n"))
    if choice == 1:
        print("You have chosen Human ---> Dog and the Human age is", startage)
        if startage < 3:
            newage = startage*11
        else:
            newage = 22+(startage-2)*4
    elif choice == 2:
        print("You have chosen Human ---> Cat and the Human age is", startage)
        if startage == 1:
            newage = 15
        else:
            newage = 25+(startage-2)*4
    elif choice == 3:
        print("You have chosen Dog ---> Human and the Dog age is", startage)
        if startage < 33:
            newage = startage/11
        else:
            newage = (((startage-22)/4)+2)
    elif choice == 4:
        print("You have chosen Cat ---> Human and the Cat age is", startage)
        if startage == 15:
            newage = 1
        else:
            newage = (((startage-22)/4)+2)
    elif choice == 5:
        print("You have chosen to generate a random human age.")
        time.sleep(1)
        randomage = random.randint(1,122)
        print(randomage)
    time.sleep(1)
    if choice >= 1 and choice <= 4:
        print("Your new age is:", newage)
    time.sleep(1)
    comment()
    restart = (input("Would you like to restart?(Yes or No)")).lower()
    while True:
        if restart == "yes":
            pass
        elif restart=="no":
            break


texts: Find all combinations of 4 elements whose sum equals a target in Python

texts:
I wrote a solution to find all combinations of 4 elements from a list whose sum of elements equals some target

  Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

If there is better way for me to do it? it seems like the performace for the following code is too bad.

def fourSum(self, nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: List[List[int]]
    """
    from itertools import combinations
    if len(nums) <= 3:return []
    res, col = [], []
    for i in combinations(nums, 4):
        check = set(i)
        if sum(i) == target and check not in col:
            res.append(list(i))
            col.append(check)
    return res


texts:
Not sure if this question will be considered "off topic". If it is, I'll remove it, but:
I hadn't see this yet so I wrote it and would like to know if this is a good approach to it. Would anyone care to offer improvements to it, or point me to an example of where someone else has already written it better?

function clwAjaxCall(path,method,data,asynch)
  {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    if(asynch)
      {
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            //alert(xmlhttp.responseText);
            //var newaction=xmlhttp.responseText;
            //alert('Action becomes '+newaction);
            return xmlhttp.responseText;
            }
          }
      }
    if(method=='GET'){path=path+"/?"+data;}
    xmlhttp.open(method,path,asynch);
    if(method=='GET'){xmlhttp.send();}else{xmlhttp.send(data);}
    if (!asynch){return xmlhttp.responseText;}

  }

I then called it like

Just Testing
<script type="text/javascript" src="/mypath/js/clwAjaxCall.js"></script>
<script type="text/javascript">
  document.write("<br>More Testing");
  document.write(clwAjaxCall("http://www.mysite.com",'GET',"var=val",false));
</script>

UPDATE

I don't know if it's any better to anyone else than it was before, but I like it. :-)

I have re-written it like this:

// I wrote this with help from StackOverflow and have twaeked it some since then.
// call it like this:
//
//        var holder={};
//        holder.text='';
//        watch(holder,function(){ //depends on watch.js. You might choose a different watch/observe method.
//          //do stuff with holder.text in here
//        });
//        var path='http://www.example.com/?key=value';
//        AjaxCall(path,"get",null,true,holder);

function AjaxCall(path,method,data,asynch,holder){
    // holder is expected to be an object. It should be watched with watch.js, Object.observe or a similar method as they become available.
    var xmlhttp;
    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }
    else{// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(asynch){
      xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
          holder.text=xmlhttp.responseText;
        }
      }
    }
    if(method=='GET'){path=path+"/?"+data;}
    xmlhttp.open(method,path,asynch);
    if(method=='GET'){xmlhttp.send();}else{xmlhttp.send(data);}
    if (!asynch){return xmlhttp.responseText;}
}