texts: Catching API changes for a service that doesn't version its API

texts:
I'm using the old Google Data API to access several spreadsheets and put their contents in a database using Flask and SQLAlchemy.  Each run deletes the contents of the database tables and repopulates them with results of the API call.

I'm trying to detect API changes and abort the operation to prevent leaving the tables empty or filling them with garbage.  I do this first by checking the status code of the API request.  If it's anything other than 200 I abort the operation.  To catch when the URL is valid but the JSON result has a new format, I catch KeyErrors when parsing the JSON.

Each time one of these conditions occurs I rollback the database session and exit the function with 1.

I'm worried that my detection coverage is spotty and I'm left feeling there should be a better way...

def get_google_content(key, db_table):
    if DEBUG:
        print 'Fetching content from Google Drive'
    spreadsheet_url = ('https://spreadsheets.google.com/feeds/worksheets/'
                       '{}/public/basic?alt=json'.format(key))
    spreadsheet_req = requests.get(spreadsheet_url)
    if spreadsheet_req.status_code != 200:
        print 'Failed to retrieve spreadsheet from {}'.format(spreadsheet_url)
        print 'Google Spreadsheet returned {}'.format(
                spreadsheet_req.status_code)
        db.session.rollback()
        return 1

    sheet_ids = list(OrderedDict.fromkeys(re.findall(r'/public/basic/(\w*)',
                     spreadsheet_req.text, flags=0)))

    # The only trivial way to catch removed spreadsheet data is to delete the
    # table and fill it all out again
    db.session.query(db_table).delete()

    for sheet_id in sheet_ids:
        worksheet_url = ('https://spreadsheets.google.com/feeds/list/{}/{}/'
                         'public/values?alt=json'.format(key, sheet_id))
        worksheet_req = requests.get(worksheet_url)
        if worksheet_req.status_code != 200:
            print 'Failed to retrieve spreadsheet from {}'.format(worksheet_url)
            print 'Google Spreadsheet returned {}'.format(
                    worksheet_req.status_code)
            db.session.rollback()
            return 1

        try:
            sheet_name = worksheet_req.json()['feed']['title']['$t']
            for row in worksheet_req.json()['feed']['entry']:
                db_row = db_table(column1=sheet_name,
                                  column1=row['gsx$location']['$t'],
                                  column2=row['gsx$caption']['$t'])
                db.session.add(db_row)
        except KeyError:
            print 'KeyError accessing Google Spreadsheet JSON from {}'.format(
                worksheet_url)
            print 'API changed?'
            db.session.rollback()
            return 1

        db.session.commit()
        return 0

Here's the relevant route:

@app.route('/admin/refresh_content')
def refresh_content():
    spreadsheet_key1 = 'key_1'
    spreadsheet_key2 = 'key_2'
    spreadsheet_key3 = 'key_3'

    sheet1_status = get_google_content(spreadsheet_key1, Table1)
    sheet2_status = get_google_content(spreadsheet_key2, Table2)
    sheet3_status = get_google_content(spreadsheet_key3, Table3)

    if sheet1_status + sheet2_status + sheet3_status > 0:
        return '
Failure

Unable to update spreadsheets'
    else:
        return '
Success

Updated spreadsheets'


texts: Extracting citations of court documents in multiple languages

texts:
I've got 20'000+ court documents I want to pull specific data points out of: date, document number, verdict. I am using Python and Regex to perform this.

The verdicts are in three languages (German, French and Italian) and some of them have slightly different formatting. I am trying to develop functions for the various data points that take this and the different languages into regards.

I'm finding my functions very clumsy. Has anybody got a more pythonic way to develop these functions?

def gericht(doc):
    Gericht = re.findall(
       r"Beschwerde gegen [a-z]+ [A-Z][a-züöä]+ ([^\n\n]*)", doc)
    Gericht1 = re.findall(
       r"Beschwerde nach [A-Za-z]. [0-9]+ [a-z]+. [A-Z]+ [a-z]+ [a-z]+[A-Za-z]+ [a-z]+ [0-9]+. [A-Za-z]+ [0-9]+ ([^\n\n]*)", doc)
    Gericht2 = re.findall(
       r"Revisionsgesuch gegen das Urteil ([^\n\n]*)", doc)
    Gericht3 = re.findall(
       r"Urteil des ([^\n\n]*)", doc)
    Gericht_it = re.findall(
       r"ricorso contro la sentenza emanata il [0-9]+ [a-z]+ [0-9]+ [a-z]+ ([^\n\n]*)", doc)
    Gericht_fr = re.findall(
       r"recours contre l'arrêt ([^\n\n]*)", doc)
    Gericht_fr_1 = re.findall(
       r"recours contre le jugement ([^\n\n]*)", doc)
    Gericht_fr_2 = re.findall(
       r"demande de révision de l'arrêt ([^\n\n]*)", doc)

    try:
       if Gericht != None:
          return Gericht[0]
    except:
         None

    try:
       if Gericht1 != None:
          return Gericht1[0]
    except:
        None

    try:
       if Gericht2 != None:
          return Gericht2[0]
    except:
       None

    try:
       if Gericht3 != None:
          return Gericht3[0]
    except:
       None

    try:
      if Gericht_it != None:
          return Gericht_it[0]
    except:
      None

    try:
     if Gericht_fr != None:
       Gericht_fr = Gericht_fr[0].replace('de la ', '').replace('du ', '')
         return Gericht_fr
    except:
      None

   try:
       if Gericht_fr_1 != None:
           Gericht_fr_1 = Gericht_fr_1[0].replace('de la ', '').replace('du ', '')
           return Gericht_fr_1
   except:
       None

   try:
      if Gericht_fr_2 != None:
         Gericht_fr_2 = Gericht_fr_2[0].replace('de la ', '').replace('du ', '')
         return Gericht_fr_2
   except:
       None


texts:
I'm currently learning Java along with the Stanford online "Programming Methodology" course and, while it is awesome, I do lack any kind of human feedback.

This exercise was to create a method that took a floating-point value x and an integer k and returned x to the power of k. The method needed to also be able to correctly calculate the result when k is negative using the relationship \$x^{-k} = \frac{1}{x^k}\$.

/*
 * File: RealPower.java
 * ---------------------
 * This Method takes a floating-point value 'x'
 * and an integer value 'k' and returns 'x to the power
 * of k'. Method is written to correctly calculate
 * even when 'k' is negative.
 * Program displays a table of values generated using
 * the raiseRealToPower method. where x = PI
 * and y is generated by 'i' variable in 'for' loop.
 * ---------------------
 * coded by Dartias
 */

import acm.program.*;

public class RealPower extends ConsoleProgram {

    public void run() {

        println("This program raises displays PI to the power of -4 through 4.");

/*
 * assigns 'x' the named constant value stored in PI
 */

        double x = PI;

/*
 * this loop uses the raiseToRealPower method to generate the
 * values for powers of -4 through 0.
 */

        for(int i = -4; i <= 0; i++){

            println(x + " to the power of "+ i + " is " + raiseRealToPower( x, i));
        }

/*
 * this loop uses the raiseToRealPower method to generate the
 * values for powers 1 through 4.
 */

        for(int i = 1; i <= 4; i++){

            println(x + " to the power of "+ i + " is " + raiseRealToPower( x, i));
        }

    }

/*
 * this method calculates x to the power of k.
 * in the event that 'k' is a negative number
 * the method uses the Math.abs method to convert it
 * to a positive number and then uses the formula
 * x(power of -k) = (1/x(power of k)) to ensure the
 * negative exponential powers generate correctly.
 */

private double raiseRealToPower(double x, int k){

    double total = x;

    if(k >=0){
        for(int i = 1; i < k; i++){
            total *= x;
        }} else {
            k = Math.abs(k);
            for(int i = 1; i < k; i++){
                total *= x;
            }
            total = (1/total);
        }
    return total;
    }

/*
 * defines the named constant PI and assigns it a value.
 */

private static final double PI = 3.14159265;

}


texts: SQL query with GROUP BY to show badges and points by user and course

texts:
I have the following database schema. My goal is to obtain a result set that lists the total badge points earned by each user. The badges might be earned in different courses. I want to include the courseid and the average score per course.

Users table:

| id | username |
|----|----------|
|  1 |    user1 |
|  2 |    user2 |
|  3 |    user3 |

Badges table:

| id | badgename | points | courseid |
|----|-----------|--------|----------|
|  1 |   badge a |     15 |        1 |
|  2 |   badge b |     10 |        1 |
|  3 |   badge c |     20 |        1 |
|  4 |   badge d |     15 |        2 |
|  5 |   badge e |     10 |        2 |
|  6 |   badge f |     25 |        2 |

BadgeAssignments table:

| userid | badgeid |
|--------|---------|
|      1 |       1 |
|      2 |       2 |
|      3 |       3 |
|      1 |       4 |
|      2 |       5 |
|      3 |       6 |
|      1 |       5 |
|      2 |       4 |
|      3 |       3 |

Courses table:

| courseid | coursename |
|----------|------------|
|        1 |   course 1 |
|        2 |   course 2 |

I have ended up with the following code, and I think it works fine:

SELECT userid, SUM(b.points), courseid,
   (SELECT AVG(points) FROM Badges WHERE courseid = b.courseid) as courseAVG
FROM
  Badges b INNER JOIN BadgeAssignments ba ON b.id = ba.badgeid
  INNER JOIN Users u ON ba.userid = u.id
GROUP BY userid, b.courseid

which brings me this result:

| userid | SUM(b.points) | courseid | courseAVG |
|--------|---------------|----------|-----------|
|      1 |            15 |        1 |        15 |
|      1 |            25 |        2 |   16.6667 |
|      2 |            10 |        1 |        15 |
|      2 |            25 |        2 |   16.6667 |
|      3 |            40 |        1 |        15 |
|      3 |            25 |        2 |   16.6667 |

The numbers seem to be correct. I wonder if my query makes sense? and if it needs to be revised?

Here is the SQL Fiddle.


texts:
I just wrote this C++ code in Visual Studio. The code is working fine but I wanted to ask whether my code is written in a safe way or not.

#include "stdafx.h"

#include <iostream>

int input()

{

    int x;
    std::cin >> x;

    return x % 2;

}

int main()

{

    using namespace std;

    cout << "Enter a digit" << endl;

    int x = input();

    if (input == 0)

        cout << "Even";

    else

        cout << "Odd";

    return 0;

}


texts: Join several columns with a single formula returning an array in Google Sheets

texts:
The title was taken from Join several columns with a single formula returning an array a Q&A hosted at http://webapps.stackexchange.com. The content is based on the original version of the answer posted by myself and posted to ARRAY_JOIN as a gist hosted at GitHub Gist

There could be differences as while I was writing this post I figured out some parts that should be corrected or enhanced (including grammar and spelling errors).

My main concerns are:

Performance of the custom function when large references or arrays are used as input.
The correct implementation as custom function
It's suitability for being included as part of a library or add-on.

filename: ARRAY_JOIN.md

ARRAY_JOIN

ARRAY_JOIN is a custom function that is part of a large personal project. It's purpose is to join the columns of one or several sets of columns, by sets. Accepts one or multiple ranges and/or arrays but they should have the same number of rows.

Demo spreadsheet

Sample usage

=ARRAY_JOIN("|",{A1:A3,B1:B3})
=ARRAY_JOIN("|",A1:B3,B1:C3)

Syntax

ARRAY_CONCATENATE([delimiter],reference_or_array1[,reference_or_array2, reference_or_array3])

[delimiter]: The character or string to place between each concatenated value..

reference_or_array1 [,reference_or_array2, reference_or_array3]: One or more references or arrays.

The custom function requires a helper function. Both are included in the gist as separate files.

Examples

Example 1

=ARRAY_JOIN("|",{A1:A3,B1:B3})

source

AA  Red
BB  Yellow
CC  Green

returns

AA|Red
BB|Yellow
CC|Green

Example 2

=ARRAY_JOIN("|",A1:B3,B1:C3)

source

AA    Red       Dog
BB    Yellow    Cat
CC    Green     Rabbit

returns

AA|Red        Red|Dog
BB|Yellow     Yellow|Cat
CC|Green      Green|Rabbit

TO DO:

Improve error catching/descriptions
i18n( internationalization) I doubt that it's possible that custom functions include support for multiple languages, so maybe I should to move from GitHub Gist to other code repository that allows to fork in order to have a version in English and other languages.

filename: Custom Funtions.gs

/**
 * Joins the columns in one or several sets, by sets. A set could be a reference
 * or an array.
 *
 * @param {"|"}   delimiter            Delimiter
 * @param {A1:B4} reference_or_array   REferences or array
 * @return                             Columns joined by set.
 * @customfunction
 */
function ARRAY_JOIN(delimiter,reference_or_array){
  var i;
  var effectiveDelimiter;
  if(typeof(delimiter) == 'string'){
    i = 1;
    effectiveDelimiter  =delimiter;
  } else if (Array.isArray(delimiter)) {
    i = 0;
    effectiveDelimiter  = '';
  } else {
    throw new Error('The first parameter should be a string or an array.')
  }
  for (i ; i < arguments.length; i++) {
    if(!Array.isArray(arguments[i])){
      throw new Error('The reference or array should be references or arrays. '
        + 'Also all of them should have the same number or rows');
    }
  }
    var result = new Array;
    for (var i = 0; i < arguments.length-1; i++) {
      result[i] = new Array;
      for (var j = 0; j < arguments[i+1].length; j++) {
        result[i].push(arguments[i+1][j].join(effectiveDelimiter));
      }
    }
    return transpose(result);
}

filename: Helper functions.gs

/**
 * Taken from http://stackoverflow.com/a/16705104/1595451
 * posted on May 23 of 2013 at 3:18
 * by [Mogsdad](http://stackoverflow.com/users/1677912/mogsdad)
 */
function transpose(a) {
  return Object.keys(a[0]).map(function (c) {
    return a.map(function (r) {
      return r[c];
    });
  });
}


texts:
Below code reverse a word  given by user input  example.

input : This is code Review

output : siht si edoc weiveR

steps

step 1: check for whitespace if found set the start and end position and
        pass to reverseword

step 2: check for null terminator  and set start and end position pass to reverseword

#include<stdio.h>
#include <ctype.h>
#define MAX 100
#define SPACE 1
#define NEXT 1
#define MIN_CHAR 1
void  reverseword(int start,int end,char *input)
{
        char temp;
        int i;
        while(end > start)
        {
                temp=input[start];
                input[start]=input[end];
                input[end]= temp;
                start++;
                end--;
        }
}

int main()
{
        char input[MAX];
        scanf("%[^\n]",input);
        int pos;
        int start=0,end=0;
        for(pos=0 ; input[pos] ; ++pos)
        {
                if(isspace(input[pos]) && pos>MIN_CHAR) // case when space is found
                {
                        end = pos-SPACE;
                        reverseword(start,end,input);
                        start= pos+SPACE;
                }
                if(input[pos+NEXT]=='\0' && pos>MIN_CHAR) // case for last world
                {
                        end =pos;
                        reverseword(start,end,input);
                }
        }
}


texts:
I have some code that is loading passwords from AWS SSM and then using them through the script. I am really concerned that some funky password character is going to be used that is going to get interpreted/escaped/munged by bash. The code in question is below. Are there any obvious problems here? Can I improve this so that no password can trip this up?

updateEnvFile() {
    local user=$1
    local pass=$2
    local host=$3

    cat "${bin}/../environments/development.properties" |
    sed "s;url=jdbc:mysql://localhost/mysql;url=jdbc:mysql://$host/mysql;" |
    sed "s/username=root/username=$user/" | sed "s/password=my-secret-pw/password=$pass/"
}

updateConfig() {
    local dbs=$1
    for db in ${dbs}; do
        local user=${params["/databases/migrate/${env}/${db}.user"]}
        local pass=${params["/databases/migrate/${env}/${db}.password"]}
        local host=${params["/databases/migrate/${env}/${db}.host"]}
        updateEnvFile "${user}" "${pass}" "${host}" > "${bin}/../environments/${db}.properties"
    done
}


texts:
Here is my revised code from my other review question Node JS delete multiple MySQL linked records and delete physical file
Just would like to hear some reviews if I have the right logic.
router.delete('/:id', (req, res)=>{

  function deleteCommentRecords(){
    db.beginTransaction(function(err) {
      if(err) return res.status(500).end(err.message);
      //Delete comment section records
      db.query("DELETE FROM commentSchema WHERE PostID = ?", req.params.id, (err, result)=>{
        if(err){
          db.rollback(()=>{
            return res.status(500).end(err.message);
          });
        }
        db.commit((err)=>{
          if(err){
            db.rollback(()=>{
              return res.status(500).end(err.message);
            });
          }
          console.log('Transaction Completed Successfully.');
        });
      });
    });
  }

  function deletePostSchemaReords(){
    // Delete PostSchema records
    db.query("delete from postschema where id = ?", req.params.id, (err, result)=>{
      if(err) {
        db.rollback(()=>{
          return res.status(500).end(err.message);
        });
      }
      db.commit((err)=>{
        if(err){
          db.rollback(()=>{
            return res.status(500).end(err.message);
          });
        }
        console.log('Transaction Completed Successfully.');
      });
    });
  }

  function loadData(){

    // Get filenames from Comments
    db.query("SELECT image FROM commentschema WHERE postID = ?", req.params.id, (error, comments_image_output)=>{
      if(error) return res.status(500).end(err.message);

      // If there is an image
      if(comments_image_output.length > 0){
        // Foreach image, delete one by one
        comments_image_output.forEach(function(row){
          try {
            console.log(row.image);
            fs.unlinkSync(uploadDir + row.image);
            console.log('Successfully deleted');
            // Query to remove commentSchema records
            deleteCommentRecords();
          } catch (err) {
          // handle the error
          }
        });
        // Redirect back to posts
        res.redirect(303, '/admin/posts');
      }
        deleteCommentRecords();
    })

    // Get filename from PostSchema
    db.query("SELECT filename FROM PostSchema WHERE id = ?", req.params.id, (err,post_image_output)=>{
      if(err) return res.status(500).end(err.message);
      if(post_image_output.length > 0){
        // Foreach image, delete one by one
        post_image_output.forEach(function(row){
          try {
            console.log(row.filename);
            fs.unlinkSync(uploadDir + row.filename);
            console.log('Successfully deleted files');
            deletePostSchemaReords();
          } catch (err) {
          // handle the error
          }
        });
      }
        deletePostSchemaReords();
    });
  }
  loadData();
});


texts: Moving average algorithm that preserves the integral and number of points

texts:
I need to smooth a noisy signal. I think that the moving average algorithm would be fine. However, I want the following conditions to be satisfied:

The total integral (the sum of all values) before and after the moving average filtering is kept unchanged
The number of points before and after the moving average filtering is kept unchanged

Implementation
The implementation is based on the answer to this post. The main question here is how to process the edges of a signal. The idea is to "reflect" the signal at the edges:
$$a_{-i} = a_{i-1},\, i > 0\quad \text{at the beginning of a signal},$$
$$a_{L+i} = a_{L-i-1},\, i\geq 0\quad \text{at the end of a signal},$$
where L denotes the length of the signal.
Note
To produce more or less appropriate results this algorithm should be used with the signals which start and end with relatively flat waveform (the length of "flatness" depends on the size of the window used).
Algorithm
#include <vector>
#include <iostream>

//Input :
//       in : the original signal
//       w  : the size of the window of the moving average
//Output :
//       out : the filtered signal
void MovingAverage( const std::vector<float>& in, std::vector<float>& out, int w )
{
    //****** PREPARATION ******/
    out.clear();

    //Make the window size odd
    w += (w % 2) ? 0 : 1;

    if( (w < 1)  or (in.size() < w) )
    {
        std::cerr << "Bad input!" << std::endl;
        return;
    }

    //****** ALGORITHM ******/
    int i = 0;  //the current position

    //Firstly, average the head
    for( ; i < w/2; i++ )
    {
        float averagedValue = 0.;
        for( int j = -w/2; j < (w/2 + 1); j++ )
        {
            averagedValue += (i + j >= 0) ? in[i + j] :
                                            in[-(i + j) - 1];//(i + j) is negative here
        }
        out.push_back( averagedValue / w );
    }

    //The middle is averaged normally
    for( ; i + w/2 < in.size(); i++ )
    {
        float averagedValue = 0.;
        for( int j = -w/2; j < (w/2 + 1); j++ )
        {
            averagedValue += in[i + j];
        }
        out.push_back( averagedValue / w );
    }

    //Finally, average the tail
    for( ; i < in.size(); i++ )
    {
        float averagedValue = 0.;
        for( int j = -w/2; j < (w/2 + 1); j++ )
        {
            //(i + j) is not negative here
            averagedValue += (i + j < in.size()) ? in[i + j] :
                                                   in[in.size() - (i + j - in.size()) - 1];//(i + j - in.size()) is not negative here
        }
        out.push_back( averagedValue / w );
    }
}

Test


texts: Golang: reading and writing files in parallel

texts:
I am trying to read a big file line by line, inspect and cleanse each row and write to either of two output files depending on the outcome. I am using channels for this. I am still quite new to this language so trying to build stuff by looking at examples.

I have kept a global count variable which I am printing at each 10k lines read to keep track of the progress.

package main

import (
    "fmt"
    "bufio"
    "time"
    "os"
    "log"
    "sync"
    "golang.org/x/net/publicsuffix"
)

func init() {
    // Change the device for logging to stdout.
    log.SetOutput(os.Stdout)
}

var count int

// read the file line by line
// push each line (domain name) onto a channel input
// write another function which reads from the input channel
// calls the func EffectiveTLDPlusOne and saves the results
// onto channel result if successful or rejected if unsuccessful
// write another function which reads from results channel and writes to cleansed file
// write another function which reads from rejected channel and writes to rejected file

func readInputFile(fileName string, domains chan string) {
    file, err := os.Open(fileName)

    if err != nil {
      log.Fatal(err)
    }

    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
      // fmt.Println(scanner.Text())
      count++
      domains <- scanner.Text()
    }

    if err := scanner.Err(); err != nil {
      log.Fatal(err)
    }
}

func cleansingRoutine(domains <-chan string, wg *sync.WaitGroup, results chan string, rejected chan string) {
    defer wg.Done()
    for domain := range domains {

      if(count % 10000 == 0){
        fmt.Println("count", count)
      }

      // get the domain name
      // if valid, push to result channel the result
      // if invalid, push to rejected channel the domain name as input
      domainName, err := publicsuffix.EffectiveTLDPlusOne(domain)
      if err != nil {
        rejected <- domain
      } else {
        results <- domainName
      }
    }
}

func main() {

  fmt.Println("start time", time.Now());

  concurrency := 500
  domains := make(chan string, 1000)
  results := make(chan string, 1000)
  rejected := make(chan string, 1000)

  fileName := os.Args[1]
  fmt.Println("fileName", fileName)
  go readInputFile(fileName, domains)

  wg := new(sync.WaitGroup)
  wg.Add(concurrency)

  for i := 0; i < concurrency; i++ {
      // parallel routine for cleansing
      go cleansingRoutine(domains, wg, results, rejected)
  }

  go writeToCleanFile(fileName, results)
  go writeToRejectedFile(fileName, rejected)

  wg.Wait()
  close(results)
  close(rejected)

  fmt.Println("end time", time.Now());
  return

}

func writeToCleanFile(fileName string, results chan string) {
  // write all lines to clean file
  resultFileName := fileName + ".cleansed"
  resultFile, err := os.OpenFile(resultFileName, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
  if err != nil {
    log.Fatal(err)
  }
  defer resultFile.Close()
  if err != nil {
    log.Fatal(err)
  }
  for r := range results {
    // fmt.Println("results", r)
    // write to file resultFileName
    _, err := resultFile.WriteString(r + "\n")
    if err != nil {
      log.Fatal(err)
    }
  }
}

func writeToRejectedFile(fileName string, rejected chan string) {
  // write all lines to clean file
  rejectedFileName := fileName + ".rejected"
  rejectedFile, err := os.OpenFile(rejectedFileName, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
  if err != nil {
    log.Fatal(err)
  }
  defer rejectedFile.Close()
  if err != nil {
    log.Fatal(err)
  }
  for r := range rejected {
    // fmt.Println("rejected", r)
    // write to file rejectedFileName
    _, err := rejectedFile.WriteString(r + "\n")
    if err != nil {
      log.Fatal(err)
    }
  }
}


texts: Function to test if the current time is within a time window

texts:
I have written the following code that tests if the current time is within a time window. It seems to work ok with my test cases, but I would be interested if anyone can see any possible problems or has any ideas for improvements?

Public Function IsCurrentTimeBetween(ByRef dteStartTime As DateTime, ByRef dteEndTime As DateTime) As Boolean

    Dim dteStart As DateTime, dteEnd As DateTime, dteCurrent As DateTime

    'strip off any date portion of the dates
    dteStart = Format(dteStartTime, "HH:mm:ss")
    dteEnd = Format(dteEndTime, "HH:mm:ss")
    dteCurrent = Format(DateTime.Now, "HH:mm:ss")

    If dteEnd < dteStart Then
        'the times span midnight
        If dteCurrent < dteStart And dteCurrent < dteEnd Then
            dteCurrent = dteCurrent.AddDays(1)
        End If
        dteEnd = dteEnd.AddDays(1)
    End If

    Return (dteStart <= dteCurrent AndAlso dteCurrent <= dteEnd)

End Function

Usage:

'Work out if we are between 5 to midnight and quarter past midnight
Debug.WriteLine("Is in midnight window? " + IsCurrentTimeBetween("23:55", "00:15").ToString)


texts: Convert flat object keys to hierarchical one

texts:
While responding to a SO question, I wrote the following method to convert a flat key structure to a hierarchical one as given below

In

{
   property1: 'value1',
   property2.property3: 'value2',
   property2.property7: 'value4',
   property4.property5.property6: 'value3',

}

Out

{
    property1: 'value1',
    property2: {
        property3: 'value2',
        property7: 'value4'
    },
    property4: {
        property5: {
            property6: 'value3'
        }
    }
}

Converter:

function convert(obj) {
    var res = {}, i, j, splits, ref, key;
    for (i in obj) {
        if (obj.hasOwnProperty(i)) {
            splits = i.split('.');
            ref = res;
            for (j = 0; j < splits.length; j++) {
                key = splits[j];
                if (j == splits.length - 1) {
                    ref[key] = obj[i];
                } else {
                    ref = ref[key] = ref[key] || {};
                }
            }
        };
    }
    return res;
}

Can this be improved?

Demo: Fiddle


texts:
I am new to Python and can't quite make my if-cases any shorter.  Any ideas on how to do that?

import csv
fileheader = csv.reader(open("test.csv"), delimiter=",")

# Defines the header of the file opened
header = fileheader.next()

# Loop into the file
for fields in fileheader:

# For each header of the file, it does the following :
# 1/ Detect if the header exists in the file
# 2/ If yes and if the cell contains datas, it's added to the payload that will be used for an HTTP POST request.
# 3/ If not, it doesn't add the cell to the payload.

    if 'url_slug' in header:
        url_slug_index = header.index('url_slug')
        if fields[url_slug_index] == "":
            print "url_slug not defined for %s" % fields[url_index]
        else:
            payload['seo_page[path]'] = fields[url_slug_index]

    if 'keyword' in header:
        keyword_index = header.index('keyword')
        if fields[keyword_index] == "":
            print "keyword not defined for %s" % fields[url_index]
        else:
            payload['seo_page[keyword]'] = fields[keyword_index]


texts:
The below code is a simple TCP echo server. The folks using it claim "it is going down every now and again". Any thoughts on how this could be improved would be really appreciated, particularly if they would improve the reliability.

import select, argparse, socket

def server_socket(ip, port):
    serv = socket.socket()
    try:
        serv.bind((ip, port))
    except:
        if (ip != ''):
            print("Failed to bind to " + ip + " on port " + str(port) + " exiting" )
        else:
            print("Failed to bind to all on port " + str(port) + " exiting" )
        exit()

    if (ip != ''):
        print("Bound to " + ip + " on port " + str(port) + " echoing tcp" )
    else:
        print("Bound to all on port " + str(port) + " echoing tcp" )

    serv.listen(1)
    inputlist = [serv,]

    while 1:
        inputready,_,_ = select.select(inputlist,[],[], 20)

        if not inputready:
            for i in inputlist:
                if i != serv:
                    i.close()
                    inputlist.remove(i)
                    print("connection timeout")

        for i in inputready:
            if i == serv:
                client, address = serv.accept()
                inputlist.append(client)
                print("connection open from " + str(address))
            else:
                try:
                    data = i.recv(1024)
                    print("received " + str(len(data)) + " bytes"),
                    print(" \""+ data +"\"")
                    if data:
                        i.send(data)
                    else:
                        i.close()
                        inputlist.remove(i)
                        print("connection closed")
                except:
                    i.close()
                    inputlist.remove(i)
                    print("connection closed")

if __name__ == '__main__':
    parser = argparse.ArgumentParser(prog = "TCP Echo server")

    parser.add_argument('-i', '--ip', type=str, default='', help = "IP to bind to")
    parser.add_argument('port', type=int, help = "Port to bind to")

    args = parser.parse_args()
    server_socket(args.ip, args.port)


texts: Remove duplicated functions inside different if within a function?

texts:
I have a function :
function __throwError(func) {
  if (func.length === 1) {
    function passNumber() {
      func(0);
    }
    function passString() {
      func("item");
    }
    function passEmptyArray() {
      func([]);
    }
    function passUndefinedOrNull() {
      func(undefined || null);
    }
    expect(passNumber).toThrowError("The parameter should be an array");
    expect(passString).toThrowError("The parameter should be an array");
    expect(passEmptyArray).toThrowError("The array is empty");
    expect(passUndefinedOrNull).toThrowError("The parameter is null or undefined");
  }
  if (func.length === 2) {
    function passNumber() {
      func(0, 1);
    }
    function passString() {
      func("item", 1);
    }
    function passEmptyArray() {
      func([], 1);
    }
    function passUndefinedOrNull() {
      func(undefined || null, 1);
    }
    expect(passNumber).toThrowError("The parameter should be an array");
    expect(passString).toThrowError("The parameter should be an array");
    expect(passEmptyArray).toThrowError("The array is empty");
    expect(passUndefinedOrNull).toThrowError("The parameter is null or undefined");
  }
}

You may notice that there are duplicated passNumber,passString,passEmpty inside different if statements and each function call different callback function func.
How do I remove duplicated functions: passNumber,passString,passEmpty and just have once call with different parameters ?


texts: Git script that fast forward merges and deletes the old branch

texts:
This script fast forward merges a branch and then deletes the old one.  I normally run it like this:

git m master feature

Here's a link to the gist.

#!/bin/bash -x
set -e

#by naming this git-m and putting it in your PATH, git will be able to run it when you type "git m ..."

if [ "$#" -ne 2 ]
then
  echo "Wrong number of arguments. Should be 2, was $#";
  exit 1;
fi

git checkout $1;
git merge --ff-only $2;
git branch -d $2;

Is there anything wrong with this?  My biggest fear is I could run this script with a typo and end up deleting a branch without merging first.


texts:
I am new to modular JavaScript code, and after reading an article on the Internet, I wrote a very basic calculator. This works fine, but due to some unknown reason, I feel that this code is not well written. I will appreciate it if someone could improve my code below so that it will be helpful with learning modular JavaScript.

$(function () {
    $('.button').on('click', function() {
        var operator = $(this).attr('name'),
        calc = new Calculator('output', 'valOne', 'valTwo', operator);

        calc.init();
    });
});

calculator.js

var Calculator = function(eq, valone, valtwo, operator) {
    var eqCtl = document.getElementById(eq),
    valone    = document.getElementById(valone),
    valtwo    = document.getElementById(valtwo),
    op        = operator,

    init = function() {
        op   = operator;
        val1 = parseInt($(valone).val());
        val2 = parseInt($(valtwo).val());

        calculation();
    },

    setVal = function(val) {
        eqCtl.innerHTML = val;
    },

    calculation = function() {
        if(op == 'add') {
            addition(val1, val2);
        }
        else if(op == 'sub') {
            subtract(val1, val2);
        }
        else if(op == 'mult') {
            multiply(val1, val2);
        }
        else {
            division(val1, val2);
        }
    },

    addition = function(x,y) {
        return setVal(x + y);
    },

    subtract = function(x,y) {
        return setVal(x - y);
    },

    multiply = function(x,y) {
        return setVal(x * y);
    },

    division = function(x,y) {
        if( y == 0 ) {
                return setVal('cannot divide by 0');
        } else {
                return setVal(x / y);
        }
    };

    return {
        init: init
    };
};


texts:
I am currently creating a website for an artist (my grandfather). My main concern with the code below is readability, as for the most part I have not worked with others on a (programming) project. I use a few external modules but hopefully their purpose is obvious/irrelevant.

I have been programming with javascript as a hobby for about two years.

Here is my code:

const express = require('express')
const app = express()
const fs = require('fs')
const gzip = require('express-gzip')
const handleRequest = require('./handleRequest')
const verify = require('../Admin/login').verify

app.use(gzip)
app.use(express.json())

app.get(['/bio', '/contact', '/login', '/works', '/404', '/bug', '/edit-bio', '/edit-works'], (req, res) => verify(req.query.token).then(result => fs.readFile((req.path === '/edit-bio' || req.path === '/edit-works' ? result : true) ? ((result && req.path === '/login') ? '../../Frontend/html/edit-works.html' : `../../Frontend/html${req.path}.html`) : '../../Frontend/html/login.html', (err, data) => err ? res.status(500).end() : res.end(data))))
app.get('/', (_req, res) => fs.readFile('../../Frontend/index.html', (err, data) => err ? res.status(500).end() : res.end(data)))
app.post(/.+/, (req, res) => handleRequest[req.body.type](req.body).then(result => result.err ? res.status(500).end(JSON.stringify(result.data)) : res.end(JSON.stringify(result.data))))

app.use('/html', (_req, res) => res.status(404).redirect('/404'))
app.use(express.static('../../Frontend'))
app.use((_req, res) => (res.status(404).redirect('/404'))).listen(8080)

This is a simple express server that answers get requests on: '/bio', '/contact', '/login', etc. If the request is to '/edit-bio' or '/edit-works' it verifies a jwt provided in the query string. If it receives a post request of any kind it runs a function corresponding to a type property in that request. It also has a simple 404 page. Finally it uses express.static on a Frontend folder containing the css, js, html, etc.


texts: Scraping different categories using different links through proxy

texts:
I've written a script in python to scrape name, review_star and review_count by going through thousands of links (to different products) stored in a csv file using reverse search. As those links are of amazon site so it is very natural to get the ip address banned for a short time while making use of few links only. However, to retain the continuation it is necessary to filter this process through proxy. This is what I tried to do here and it is running smoothly. For the record: as these proxies are collected from web, they may not last long. Anyways, this scraper is supposed to make requests using each links from the csv file, collect the product name, review_star and review_count from amazon site without being blocked. Considering the space, I only used three proxies in my scraper. I tried my level best to make it flawless and it is working without leaving any complaint at this moment. Any suggestion to make this better will be highly appreciated.

This is the script I've written:

import csv
import requests
from lxml.html import fromstring

def crawl():

    proxy={
        'https': 'https://188.166.154.140:8118',
        'https': 'https://198.52.44.130:53281',
        'https': 'https://46.101.2.115:8118'
        }

    with open("amazon.csv", 'r') as input_file:
        for entry in csv.DictReader(input_file):
            url = entry['Link']
            response = requests.get(url,headers={'User-Agent':'Mozilla/5.0'},proxies=proxy)
            root = fromstring(response.text)
            name = root.cssselect("#productTitle")[0].text.strip() if root.cssselect("#productTitle") else ""
            star = root.cssselect(".AverageCustomerReviews .a-icon-alt")[0].text if root.cssselect(".AverageCustomerReviews .a-icon-alt") else ""
            count = root.cssselect(".AverageCustomerReviews .totalReviewCount")[0].text if root.cssselect(".AverageCustomerReviews .totalReviewCount") else ""
            print("Name: {}\nStar: {}\nCount: {}".format(name,star,count))

if __name__ == '__main__':
    crawl()

These are the five links out of thousands which are supposed to store in a csv file named amazon.csv containing a header Link:

Link
https://www.amazon.com/dp/B013KBZ9RY
https://www.amazon.com/dp/B004AC6PQW
https://www.amazon.com/dp/B01JJBA06Y
https://www.amazon.com/dp/B001G9G6XY
https://www.amazon.com/dp/B000UCM0P6


texts:
This is my very first foray into Clojure (I'm normally a Python-pushing data-type). I'm trying to create a simple term-document matrix as a vector of vectors, out of a vector of strings.

For those who aren't into textmining, the term-document matrix is a dataset in matrix form where the column names represent every word in a set of documents, each row is a document, and each cell is the number of times a given word appears in a given document.

This is the very first step in what I hope to be a useful text data-cleaning library, as well as a clojure learning project.  After the basics are nailed down, I want to add functionality like n-grams, stemming, removing sparse terms, etc. etc.  My ultimate goals involve performance, so I want to optimize this beginning part within an inch of its life right from the start in order to build on it later.

I'm also trying to minimize dependencies (right now there are none), though I'm willing to use incanter or clojure.core.matrix if there are big performance gains to be gotten thereby.

So obviously I have a long way to go, but here are some questions on this initial step:

Is this "good clojure?"  I tried to stick to sort of basic functional programming practice, composing lots of short functions with discrete behavior and such.  But I'm not yet sure what the norms are otherwise.
How do I optimize this?  The particular parts of the existing code that smell funny to me, performance-wise, are:

terdocmmap: there's gotta be a more efficient way of handling the sorting here than building a bunch of sparse maps then sorting them all.  Ideally I'd like to build them in sorted form from the start somehow.
termdocmatrix: the maps -> sequences -> vectors conversion seems really wasteful; I'd like to come up with a more efficient way.

I'm not worrying about namespace and project structure at this stage.

(require '[clojure.string :as str])
(require '[clojure.walk :as walk])

(defn whitesplit
  "split a vector of string into vector of vectors of strings on whitespace"
  [docs]
  (map #(str/split % #" ") docs))

(defn stringcounts
  "count frequencies of strings in vector of vectors of strings"
  [stringvecs]
  (map frequencies stringvecs))

(defn liststrings
  "list all strings in doc set"
  [stringvecs]
  (distinct
    (apply concat stringvecs)))

(defn makezeroes
  [stringlist]
  (zipmap stringlist (repeat 0)))

(defn expandcounts
  "based on strings in all docs, fill counts with 0 for unused strings in each single doc"
  [zeroes counts]
  (map #(merge-with + % zeroes) counts))

(defn bigmap
  "split vector of docs by spaces then make zero-filled map of counts"
  [docs]
  (let [stringvecs (whitesplit docs)]
    (expandcounts
      (-> stringvecs liststrings makezeroes)
      (-> stringvecs stringcounts))))

(defn terdocmmap
  "make a sorted document-term-map of vector of docs with keywords"
  [docs]
  (walk/keywordize-keys  ; this is mainly for later flexibility
    (map #(into (sorted-map) %) (bigmap docs))))

(defn tdseqs
  "convert document-term-map into sequence of sequences"
  [tdmap]
  (cons
    (keys (first tdmap))
    (map vals tdmap)))

(defn nestvecify
  "sequence of sequences --> vector of vectors"
  [seqofseq]
  (into [] (map #(into [] %) seqofseq)))

(defn termdocmatrix
  "make document term matrix as vector of vectors from vector of docs"
  [docs]
  (-> docs terdocmmap tdseqs nestvecify))

Example input and output:

(termdocmatrix ["this is a cat" "this is a dog" "woof and a meow" "woof woof woof meow meow words"])

Produces:

[[:cat :is :this :words :dog :and :meow :woof :a] [1 1 1 0 0 0 0 0 1] [0 1 1 0 1 0 0 0 1] [0 0 0 0 0 1 1 1 1] [0 0 0 1 0 0 2 3 0]]


texts:
I'm trying to print out a list of files in a specific order, with files closer to the root directory sorted first. For example these files:

c:\users\steve\foo.txt
c:\users\bar.txt
c:\users\steve\desktop\file.txt
c:\users\john\bing.txt

Should be sorted as:

c:\users\bar.txt
c:\users\john\bing.txt
c:\users\steve\foo.txt
c:\users\steve\desktop\file.txt

Code:

namespace fs = std::experimental::filesystem;

std::vector<std::string> sortedRules;
std::sort(sortedRules.begin(), sortedRules.end(),
    [&](const string &str1, const string &str2)
{
    fs::path path1(str1);
    fs::path path2(str2);

    //if parent directories are the same, compare filenames
    if (path1.parent_path() == path2.parent_path())
        return path1 < path2;

    std::vector<std::string> path1Expnded;
    std::vector<std::string> path2Expnded;

    for (const auto &part : path1)
        path1Expnded.emplace_back(part.string());

    for (const auto &part : path2)
        path2Expnded.emplace_back(part.string());

    //if paths have same amount of stems, compare full paths
    if (path1Expnded.size() == path2Expnded.size())
        return path1 < path2;

    int minSize;
    path1Expnded.size() <= path2Expnded.size()
        ? minSize = path1Expnded.size()
        : minSize = path2Expnded.size();

    //sort by first differing stem
    for (int i = 0; i < minSize - 1; i++)
        if (path1Expnded[i] != path2Expnded[i])
            return path1Expnded[i] < path2Expnded[i];

    //if sizes differ and no stems are the same, sort by number of stems
    return path1Expnded.size() < path2Expnded.size();
});

I created a custom comparison lambda that overrides how std::sort compares, and it works most of the time. Sometimes items cannot be found after being sorted using this comparison lambda, and after being searched for using this same comparison lambda. So I'm guessing my algorithm is somehow incorrect.

Also even when the algorithm does run, it does so very slowly. It took me awhile to get the files to sort just the way I wanted, and while I tried to allow the function to return as soon as possible its performance is pretty poor. How can I fix the algorithm to ensure its correctness, and also make it more efficient?


texts: Opening social media links in a new window

texts:
I'm rather new to jQuery but managed to get some social links to open in a new controlled window instead of new tab. I have a few functions that do the same thing, and this works, but there is always a better method.

I did review the question and answer here and couldn't figure out which would apply to this case.

jQuery(document).ready(function ($) {
  $("#ssba_facebook_share").attr('title','opens in a new pop-up');
  $("#ssba_facebook_share").click(function(){
    w = parseInt((screen.width - 600)/2); h = parseInt((screen.height - 400)/2);
    cwin = window.open($(this).attr('href'), 'closewin',
   'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,height=400,width=600');
    cwin.moveTo(w,h);
    return false;
  });
  $("#ssba_twitter_share").attr('title','opens in a new pop-up');
  $("#ssba_twitter_share").click(function(){
    w = parseInt((screen.width - 600)/2); h = parseInt((screen.height - 400)/2);
    cwin = window.open($(this).attr('href'), 'closewin',
   'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,height=400,width=600');
    cwin.moveTo(w,h);
    return false;
  });
  $("#ssba_google_share").attr('title','opens in a new pop-up');
  $("#ssba_google_share").click(function(){
    w = parseInt((screen.width - 600)/2); h = parseInt((screen.height - 400)/2);
    cwin = window.open($(this).attr('href'), 'closewin',
   'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,height=400,width=600');
    cwin.moveTo(w,h);
    return false;
  });
});

Now, I've attempted a few things to no avail, and one method was this:

jQuery(document).ready(function ($) {
  $("#ssba_facebook_share","#ssba_twitter_share","#ssba_google_share").attr('title','opens in a new pop-up');
    $(this).click(function(){
      w = parseInt((screen.width - 600)/2); h = parseInt((screen.height - 400)/2);
      cwin = window.open($(this).attr('href'), 'closewin',
      'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,height=400,width=600');
      cwin.moveTo(w,h);
      return false;
    });
});

Does anyone have any pointers in this, if possible, with a description of where I went wrong? Or even post a link to a question that goes over this in laymen terms? (I've been looking around and found similar posts, which is how I arrived at where I am now, but still having trouble making complete sense of it.)


texts: Scalaz state monad exercise to model sampling of coloured balls

texts:
Here is a self imposed exercise and solution to learn Scalaz state monad.  How can I refactor it to take it to the next level?  So far I've only studied the state monad so would welcome suggestions on introducing other Scalaz features of relevance, as well as general Scala style/approach.

Exercise

  Use state monad to simulate the process of drawing 5 coloured balls
  from a bag of 30x Red, 20x Green, 10x Blue.  Rather than representing
  the bag and sample as collections of balls, use a map to track the
  number of each colour.

Solution

import scalaz._
import Scalaz._

object StateExercise extends App{
    trait Colour
    object Red extends Colour { override def toString = "Red"}
    object Green extends Colour { override def toString = "Green"}
    object Blue extends Colour { override def toString = "Blue"}

    type Bag = Map[Colour, Int]
    val empty: State[Bag, Bag] = state(Map[Colour, Int]())

    def drawOne(soFar: Bag): State[Bag, Bag] = for(
        ball <- State[Bag, Colour]{s =>
            val (colours, counts) = s.toIndexedSeq.unzip
            val ballIndex = (math.random * s.values.sum).toInt
            val selected = colours(
                counts.tail
                    .view
                    .scanLeft(counts(0))(_ + _)
                    .indexWhere(_ >= ballIndex)
            )
            (s.updated(selected, s(selected) - 1), selected)
        }
    ) yield soFar.updated(ball, soFar.getOrElse(ball, 0) + 1)

    def draw(n: Int): State[Bag, Bag] = for(
        balls <- get[Bag];
        selection <- {
            assert(balls.values.sum >= n)   //Check the bag has enough left
            (1 to n).foldLeft(empty)((accState, _) => accState.flatMap(drawOne))
        }
    ) yield selection

    val full: Bag = Map(Red -> 30, Green -> 20, Blue -> 10)
    val (remaining, selected) = draw(5)(full)
    println(" Selected: "+selected)
    println("Remaining: "+remaining)
}

Output

  Selected: Map(Red -> 3, Green -> 2)

  Remaining: Map(Red -> 27, Green -> 18, Blue -> 10)


texts: Change implementation from getting json from file to get it from API

texts:
I used to get a JSON file with some configuration settings from the file system, using this class:

public class ConfigurationFileService : IConfigurationService
{
    private readonly IConfigurationManager _configurationManager;

    public ConfigurationFileService(IConfigurationManager configurationManager)
    {
        _configurationManager = configurationManager;
    }

    public Configuration GetConfiguration(int agentId)
    {
        var path = _configurationManager.GetAppSetting("pathToConfigurationFile");

        var Configuration = JObject.Parse(File.ReadAllText(string.Format(path, agentId)));
        return Configuration.ToObject<Configuration>();
    }
}

Now I will be getting that from an API (it will return exactly the same JSON), so I need to change the implementation of the IConfigurationService, I have done this so far.

public class ConfigurationApiClient : IConfigurationService
{
    private readonly HttpClient _client;

    public ConfigurationApiClient()
    {
        _client = new HttpClient();
        _client.BaseAddress = new Uri(""); //TODO Add base url
        _client.DefaultRequestHeaders.Accept.Clear();
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    }

    public Configuration GetConfiguration(int agentId)
    {
        return GetConfigurationAsync(agentId).Result;
    }

    private async Task<Configuration> GetConfigurationAsync(int agentId)
    {
        try
        {
            var response = await _client.GetAsync(""); // TODO add the url
            response.EnsureSuccessStatusCode();
            var Configuration = await response.Content.ReadAsAsync<Configuration>();
            return Configuration;
        }
        catch(HttpRequestException ex)
        {
            throw new ConfigurationException(string.Format("Exception when getting  settings for agent: {0}", agentId), ex);
        }
    }

The interface is very simple:

public interface IConfigurationService
{
    Configuration GetConfiguration(int agentId);
}

I am not sure about a couple of things:

The way I reuse the httpclient. I have seen that the recommendation is not to dispose it after each request, so I guess putting it in the constructor is a good option as the class itself will be injected.
The usage of one public method with the signature of the interface, that blocks the result of the asynchronous method (given that's the correct way of getting the result asynchronously).


texts:
I am trying to find number of integer partitions of given n - number. If I have n == 4, the answer should be 5 because:

\$4 = 1+1+1+1\$
\$4 = 2+1+1\$
\$4 = 3+1\$
\$4 = 2+2\$
\$4 = 4\$

My code works properly but the matter is that it counts big numbers for a very long time. I have no idea how to optimize my code. Maybe you can help me to make it faster?

def get_answer(n):
    if n == 0:
        yield []
        return
    for p in get_answer(n-1):
        yield [1] + p
        if p and (len(p) < 2 or p[1] > p[0]):
            yield [p[0] + 1] + p[1:]
number_of_partitions=lambda n:sum(1 for _ in get_answer(n))


texts:
I made a slide show using array values, it works, but I strongly believe that the way I did is too long.

There should be an easier or more compact way to achieve this.

$(document).ready(function () {

  var num = 0;
  var myArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"];
  var batch = 1;
  var value = 0;
  var tdLength = 5;
  var flag = true;
  var cal = 0;

  var showArray = function (num) {

    var required = myArray.slice(num, (num+5));
    $('.info').empty();

    $.each(required, function (item, n) {

      $('.info').append(
         '
'+ n +'
'
        )

    })

    //value += 5;

  }

  function add(amount) {
    num = (num + tdLength - 1 + amount) % tdLength + 1

    cal = (num-1) * 5;
    //console.log(myArray.slice(cal, cal+5));
    showArray(cal);

  }

 $('#next-arrow').click(function(e){

   flag = true;

   if(cal >= (tdLength+5)) {
     $(e.target).css({ opacity:0.5 });
     return
   }
   $(e.target).css({ opacity:1});
   add(batch)

 });
 $('#prev-arrow').click(function(e){

   flag = false;

   if(cal <= 1) {
     $(e.target).css({ opacity:0.5 });
     return
   }

   $(e.target).css({ opacity:1});
   add(-batch)

 });

 $('#next-arrow').click();

})

HTML :

      Previous
      Next

Live Demo


texts:
This is a simple script that moves files to S3 if they are > 14 days old. It ignores files that are < 14 days old. If the file is successfully synced to AWS S3 then we can remove it from the server. If not, we leave the file where it is. The echo commands in the script are for debugging. I am aware that this could be a one liner, so interested to see what people suggest.

#!/bin/bash

fortnight=$(python -c "from datetime import datetime, timedelta; print (datetime.now()-timedelta(days=14)).strftime('%Y%m%d')")

for f in /var/mail/catchall-????????.gz
do
    if [[ "$f" < "/var/mail/catchall-$fortnight.gz" ]] ; then
        mv "$f" ./archive
        echo "$f"
        echo "$(basename $f)"
        cd ./archive
            if s3cmd sync "$(basename $f)" s3://vpcgwmail ; then
                echo "S3 sync successful, removing file"
                rm "$(basename $f)"
            else
                echo "s3 sync not succesful"
            fi
        fi
done


texts:
I recently needed a data structure with FIFO queue semantics that supported efficient bulk-insertion (often need to enqueue thousands of messages at once).  I took a look at Purely Functional Data Structures, and I didn't find an exact match, but it looked like the BatchedQueue could be easily enhanced to support this requirement.

This is my implementation.  Is this a reasonable solution, or should I be using a different approach?

type ImmutableQueue<'message> private (front: 'message list, rear: 'message list) =
    let enqueue message =
        match front, message::rear with
        | [], newRear -> ImmutableQueue(newRear |> List.rev, [])
        | _, newRear -> ImmutableQueue(front, newRear)

    let enqueueAll messages =
        let orderedMessages = messages |> List.rev
        match front, orderedMessages@rear with
        | [], newRear -> ImmutableQueue(newRear |> List.rev, [])
        | _, newRear -> ImmutableQueue(front, newRear)

    let dequeue () =
        match front with
        | message::tail ->
            message, (match tail with
                      | [] -> ImmutableQueue(rear |> List.rev, [])
                      | _ -> ImmutableQueue(tail, rear))
        | _ -> failwith "Cannot dequeue from empty queue!"

    let tryDequeue () =
        match front with
        | message::tail ->
            (message, (match tail with
                       | [] -> ImmutableQueue(rear |> List.rev, [])
                       | _ -> ImmutableQueue(tail, rear)))
            |> Some
        | _ -> None

    let reverse () =
        match front with
        | [] -> ImmutableQueue(rear |> List.rev, [])
        | _ -> ImmutableQueue(front, rear)

    let getEnumerator () =
        (seq {
            yield! front
            yield! rear |> List.rev
        }).GetEnumerator()

    static member Empty = ImmutableQueue<'message>([], [])
    static member From messages = ImmutableQueue<'message>(messages, [])

    member __.IsEmpty = front.IsEmpty && rear.IsEmpty
    member __.Length = front.Length + rear.Length
    member __.HasMessages = front.IsEmpty |> not
    member __.Enqueue message = enqueue message
    member __.EnqueueAll messages = enqueueAll messages
    member __.Dequeue () = dequeue ()
    member __.TryDequeue () = tryDequeue()
    member __.Reverse () = reverse()
    member __.GetEnumerator () = getEnumerator()

    interface IEnumerable<'message> with
        member this.GetEnumerator () = this.GetEnumerator()

    interface System.Collections.IEnumerable with
        member this.GetEnumerator () = this.GetEnumerator() :> System.Collections.IEnumerator

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module ImmutableQueue =
    let empty<'message> = ImmutableQueue<'message>.Empty

    let inline enqueue message (queue: ImmutableQueue<'message>) = queue.Enqueue message

    let inline enqueueAll messages (queue: ImmutableQueue<'message>) = queue.EnqueueAll messages

    let inline isEmpty (queue: ImmutableQueue<'message>) = queue.IsEmpty

    let inline length (queue: ImmutableQueue<'message>) = queue.Length

    let inline hasMessages (queue: ImmutableQueue<'message>) = queue.HasMessages

    let inline ofList messages = messages |> ImmutableQueue.From

    let inline ofSeq messages = messages |> Seq.toList |> ofList

    let inline dequeue (queue: ImmutableQueue<'message>) = queue.Dequeue()

    let inline tryDequeue (queue: ImmutableQueue<'message>) = queue.TryDequeue()

    let inline rev (queue: ImmutableQueue<'message>) = queue.Reverse()


texts:
this week I learned about QuickSort and used the technique to solve the Dutch National Flag problem. I would appreciate it if someone would review my code and give feedback.

  The Dutch national flag problem is a computer science programming problem proposed by Edsger Dijkstra. The flag of the Netherlands consists of three colors: red, white and blue. Given balls of these three colors arranged randomly in a line (the actual number of balls does not matter), the task is to arrange them such that all balls of the same color are together and their collective color groups are in the correct order.

Code:

pair<int, int> Partition(vector<int> &nums, int low, int high) {
  int pivotElement = nums.at(low), lt = low, gt = high, i = low + 1;
  while (i <= gt) {
    // case of left parition
    if(pivotElement > nums.at(i)) {
      std::swap(nums.at(i), nums.at(lt));
      ++lt;
    } // below if the case of right partition
    else if (pivotElement < nums.at(i)) {
      std::swap(nums.at(i), nums.at(gt));
      --gt;
    }
    ++i;
  }
  return std::make_pair(lt, gt);
}

void DutchFlag(vector<int> &nums, int low, int high) {
  pair<int, int> p = std::make_pair(0, 0);
  if(low < high) {
    p = Partition(nums, low, high);
    DutchFlag(nums, low, p.first);
    DutchFlag(nums, p.second, high);
  }
}

int main() {
  std::vector<int> nums = {0, 1, 2, 2, 2, 1, 0, 0};
  DutchFlag(nums, 0, nums.size() - 1);
  std::copy(nums.begin(), nums.end(), std::ostream_iterator<int>(std::cout, " "));
}


texts:
I do most of my programming in C/C++ and Perl, but I currently learning Fortran. I began by coding up a simple program, something that took me < 5 minutes in Perl (see this thread from BioStar). After working for several hours on a solution in Fortran 95 (teaching myself as I go), I came to this solution.

implicit none

! Variable definitions
integer                 :: argc
integer                 :: num_digits
integer                 :: i, j
integer                 :: iocode
integer                 :: line_length
integer                 :: value
character ( len=256 )   :: infile
character ( len=16 )    :: int_format
character ( len=16 )    :: int_string
character ( len=2048 )  :: line

! Verify and parse command line arguments
argc = iargc()
if( argc < 1 ) then
  write(*, '(A, I0, A)') "Error: please provide file name (argc=", argc, ")"
  stop
endif
call getarg(1, infile)

! Open input file, croak if there is an issue
open( unit=1, file=infile, action='read', iostat=iocode )
if( iocode > 0 ) then
  write(*, '(A, A, A)') "Error: unable to open input file '", trim(infile), "'"
  stop
endif

! Process the file, print in CSV format
do while(1 == 1)
  ! Read the line, skip if it is empty
  100 read( unit=1, fmt='(A)', end=200 ) line
  line_length = len_trim(line)
  if( line_length == 0 ) then
    goto 100
  endif

  ! The first value in the line is a string
  ! Find string boundaries and print it out
  i = 0
  do while( line(i:i) == ' ' )
    i = i+1
  end do
  j = i
  do while( line(j:j) /= ' ' )
    j = j+1
  end do
  write(*, '(A)', advance="no") line(i:j-1)

  ! Now grab the rest of the integer values
  ! on the line, multiply them by 3, and print
  i = j
  j = 0
  do while( i < line_length)
    do while( line(i:i) == ' ' )
      i = i+1
    end do
    j = i
    do while( j < line_length .and. line(j:j) /= ' ' )
      j = j+1
    end do
    int_string = line(i:j-1)
    read(int_string, '(I)') value
    value = value*3
    write(*, '(A, I0)', advance="no") ",", value
    i = j
    j = 0
  end do

  print *
end do
200 close( 1 )

end program

There is a lot that I don't like about how I've written this program, but it's hard to separate my inexperience with the new syntax from bad practice. One thing in particular that I don't like is my use of labels and the infamous goto statement. I'm interested in any feedback whatsoever, but I'm particularly interested in better ways to handle the control structure of the program (without using the goto statement and end parameter in the read function if possible).

The compilers I have access to only support features through f95.


texts: How could I have made this Powershell script that executes robocopy code better

texts:
I am trying to get better at Powershell and was wondering if I could get some advice on how to make this code more efficient/cleaner/anything.

This code is intended to launch three batch windows running robocopy at the same time with unique log names.

I looked into using a foreach loop but wasn't sure how I could do that with so many unique variables. Thinking back, I probably could have imported each source, destination and log name into a CSV though but this may have been cleaner/better because it's all in one file instead.

# sets the files to exclude
$defaultDirExclusionSet = '"$Recycle.Bin" "System Volume Information"'

# setting the filename perameter so that we have some timestamps.
$filedate = "$(get-date -format 'yyyy-MM-dd-HH-MM')"  # we can remove the HH and MM later... have this on now for testing.

#options for robocopy
$RoboOptions = "/MIR /COPY:DATSOU /ZB /R:1 /W:5 /XD $defaultDirExclusionSet /NP /TEE"

#main fileshares
$source = "\\server1\D$\Shares"
$destination = "\\server2\D$"
$logname = "fileshare"

#Citrix User Store Shares
$source1 = "\\server1\D$\CitrixUserStore"
$destination1 = "\\server2\E$\CitrixUserStore"
$logname1 = "ctxusrshare"

#Citrix User Redir Shares
$source2 = "\\server1\D$\CitrixUserRedir"
$destination2 = "\\server2\E$\CitrixUserRedir"
$logname2 = "ctxusrredir"

##### launch all at once.  fix the first line before using.

Start-Process robocopy.exe -ArgumentList "$($source) $($destination) $($RoboOptions.split(' ')) /log+:c:\robocopylogs\$($logname)_$($filedate).txt"
Start-Process robocopy.exe -ArgumentList "$($source1) $($destination1) $($RoboOptions.split(' ')) /log+:c:\robocopylogs\$($logname1)_$($filedate).txt"
Start-Process robocopy.exe -ArgumentList "$($source2) $($destination2) $($RoboOptions.split(' ')) /log+:c:\robocopylogs\$($logname2)_$($filedate).txt"


texts:
How's the following function to update an immutable Map's values?

scala> def updateMap[A](map: Map[A, Set[A]], item: A, toAdd: A) = {
    map.map(x => if(x._1 == item) (x._1, (x._2 + toAdd)) else (x._1, x._2))
}

scala> val map = Map(1 -> Set(2), 3 -> Set(4))

scala> updateMap(map, 1, 5)
res1 ...= Map(1 -> Set(2, 5), 3 -> Set(4))


texts:
I have a bunch of simple interfaces like this one (pretty enough formed, but not guarantee)

package com.example.sources;

import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.resources.client.ImageResource.ImageOptions;
import com.google.gwt.resources.client.ImageResource.RepeatStyle;

import com.example.sources.MainSceneResource;
import com.example.sources.Preloadable;

public interface MainSceneResources extends ClientBundle, Preloadable {

    @Source("com/example/sources/css/mainscene.css")
    public MainSceneResource mainSceneCss();

    @Source("com/example/sources/img/panorama.jpg")
    @ImageOptions(repeatStyle = RepeatStyle.None)
    public ImageResource mainSceneBackground();

}

I need to:

remove line with package;
change lines with import (delete the import keyword, whitespaces, semicolon at the end) and add stripped packages with the Factory's method addImport(line);
change Preloadable to Generated
copy other lines without changes with the SourceWriter's method sw.println(line)

Currently my code is like:

BufferedReader reader = new BufferedReader(new InputStreamReader(ris));

String line = null;
try {
    while ((line = reader.readLine()) != null) {
        if (line.contains("package") || line.isEmpty()) {
            continue;
            }
        int index = line.indexOf("import");
        if (index != -1) {
            line = line.substring(index + 6, line.lastIndexOf(";")).trim();
            factory.addImport(line);
            continue;
        }
        if (line.contains("Preloadable")) {
            line = line.replace("Preloadable", "Generated");
        }
        sw.println(line);
    }
} catch (IOException e) {
    throw new UnableToCompleteException();
}

The one more notice is that I can't use SourceWriter method sw.println(line) before Factrory factory.addImport(line);

My most worries are about calling line.contains("pattern") in while-loop. As I know "pattern" will be compiled to Java regex Pattern.class. I'm aware it's not good to compile the same Pattern over and over till the loop ends, but don't know how to do this better.
Also I doubt about this line (to strip import lines)

line = line.substring(index + 6, line.lastIndexOf(";")).trim();

Is there a better way to do this?


texts: C++ EventHandler class, invoking & arguments

texts:
This is my EventHandler class, which is extremely simple and can be used on lambdas and similar:

template<typename Signature, typename...Ax>
class Event
{
private:
    typedef std::function<Signature(Ax...)> Function;
    Function _func;

public:

    void CallFunc(Ax... ax)
    {
        if (_func)
        {
            (_func)(ax...);
        }
    }

    void operator+=(Function func)
    {
        _func = func;
    }

};

I'm looking to find improvements as it's basically as simple as it gets.

Example:

Event<void, int> myEvent;
myEvent += [](int arg) { printf("%i", arg); };

myEvent.CallFunc(15);

Would print out 15.


texts:
I want to write an Android ListAdapter to serve data from a database that look like this:

CREATE TABLE note (_id integer primary key autoincrement,
                   content text not null)
CREATE TABLE tag (_id integer primary key autoincrement,
                  name text not null);
CREATE TABLE relation (_id integer primary key autoincrement,
                       noteid integer not null,
                       tagid integer not null,
                       idx integer not null);

I use this database to store notes and their associated tags as an ordered list. One requirement of the ListAdapter is that it must be able to update the ListView contents if the underlying data changes. I can query all the notes in the database with this query:

SELECT note._id AS noteid,
       note.content,
       relation.idx AS tagidx,
       tag.name
FROM note
LEFT JOIN relation ON (relation.noteid = note._id)
LEFT JOIN tag ON (tag._id = relation.tagid)
ORDER BY noteid, tagidx;

Which will give me results looking like this (null values shown for clarity):

0|foo bar baz|0|x
1|hello world|null|null
2|one more nn|0|yy
2|one more nn|1|y

As you can see, a note with more than one tag will be located on more than one row in the result. This means that I can't look at the cursor size to determine the number of notes, I need to traverse the entire Cursor to get a count. I don't want to do that.

The solution I have come up with so far is to use two cursors: one with the query mentioned above and one with a query containing the number of rows in the notes table (select count(*) from notes). In the constructor I call intializeCursors():

private void initializeCursors() {
    notesCursor.moveToFirst();
    countCursor.moveToFirst();
    count = countCursor.getInt(0);
}

I have implemented  getItem() like this:

public Note getItem(int position) {
    // notes is a List of notes that we have already read.
    if (position < notes.size()) {
        return notes.get(position);
    }

    int cursorPosition = notes.size();
    while (cursorPosition <= position) {
        // Creates a note by reading the correct number of rows.
        Note note = NotesDb.noteFrom(notesCursor);
        notes.add(note);
        ++cursorPosition;
    }
    return notes.get(position);
}

The adapter assumes that the cursors are being managed by some Activity that has called startManagingCursor() on them.

So far so good, I guess. The problem is now how to handle the cursor being requeried. Since I have two cursors I need to register listeners for both of them and when I have received onChange() for both of them I can initializeCursors() and notify any listeners registered to my ListAdapter of a change in the its data.

The requery scenario is the actual reason that I needed two cursors in the first place. I have not figured out a way to count the number of notes in a required cursor other than asking the database using another cursor.

This is the best I have so far. I want to check the sanity of this approach with this group. :-) Is this two cursor approach too complicated? Perhaps I have missed some part of the API that solves this nicely for me?


texts:
When developing API methods to return JSON with details I often find myself creating the following code:

    $response = array('success' => false);

    $user = User::getSessionUser();
    if($user->id > 0){
        if(ItemSKU::stringIsValid($sku)){

            $itemSKU = ItemSKU::getFromString($sku);
            if($itemSKU->isAvailable()){
                if(Carts::add($user->id, $sku)){
                    $response['success'] = true;
                }
                else{
                    $response['error'] = "Item was already added";
                }
            }
            else{
                $response['error'] = "Unavailable Item";
            }
        }
        else{
            $response['error'] = "Invalid SKU";
        }
    }
    else{
        $response['error'] = "Invalid User";
    }

    return response()->json($response);

How can I avoid using this nested if-else chain?


texts:
Recently I learned HTML+CSS and a bit of jQuery and am building various web-things to stretch my skills and learn more about how to do things the right way.  I was a bit surprised at just how many different ways there are to do the same thing using just CSS and JavaScript.

Currently I am building a bit of a "landing page" style website bit by bit, and the component I am working on right now is an interactive discovery menu type thing.  Essentially there is an initial text, which incites the user to hover over it, and once hovering, various options appear, which are links.  My current code is below and functional, however I am not confident that I am executing this efficiently or properly.

A couple of specifics I want to learn are:

What HTML element should I use for the initial text?
Is the structure of my hover-menu proper?  Or is there a better way that is more standard for this sort of thing?
Would this be better done in CSS transitions rather than JS?  NOTE: I want to add a context box below the menu (or somewhere not directly attached to the individual links) which fills with contextual information about the currently hovered link, so I assume JS is the solution in the long run.
Not that important but: I had trouble attempting to classify what to call this sort of thing (so as to google it).  Is there a standard name for this sort of set-up?

Final Note:

I don't really know any general JavaScript outside of the jQuery I have learned.  I do however have programming experience, so I am not opposed to using any other external JS libraries which would be more suited to my goals.  I have simply not had much time to explore JavaScript and the many extensions it has.  Basically I'd love some directing from anyone who knows what I don't!

$(document).ready(function() {
  $('#menu').hover(
    function() { // mouseenter
      // hide & compress initial text
      $('#menu span').stop().animate({
        width: '0px',
        opacity: 0
      }, $('#menu span').hide);

      // show & decompress link options
      $('#menu a').stop().show().animate({
        width: '100px',
        opacity: 1
      });

    },
    function() { //mouseleave
      // hide & compress options
      $('#menu a').stop().animate({
        width: '0px',
        opacity: 0
      }, $('#menu a').hide);

      // show & decompress link options
      $('#menu span').stop().show().animate({
        width: '250px',
        opacity: 1
      });

    });
});
#menu {
  width: auto;
}
#menu * {
  overflow: hidden;
  white-space: nowrap;
  font-size: 30px;
  text-align: center;
}
#menu a {
  display: none;
  width: 0px;
  opacity: 0;
}
#menu span {
  display: inline-block;
  width: 250px;
}

    Hover for items!
    Item 1
    Item 2
    Item 3


texts: Conditionally copying cells from other worksheets

texts:
I wrote super redundant VBA code and it works and solve my problem. I am trying to simplify it but don't know where to start due to my limited knowledge in VBA.

Basically, this code works as a vlookup function and return all required data from other sheets in the same workbook. I have multiple columns to be filled so I repeat using the same codes. But I believe there is a much more neat version rather than mine.

Sub LoopTem()

Dim ws As Worksheet
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim ws4 As Worksheet
Dim ws5 As Worksheet
Dim ws6 As Worksheet
Dim ws7 As Worksheet

Dim i As Integer
Dim r As Integer
Dim s As Integer
Dim t As Integer
Dim u As Integer
Dim v As Integer
Dim w As Integer

Dim lastRow As Integer
Dim lastRow2 As Integer
Dim lastRow3 As Integer
Dim lastRow4 As Integer
Dim lastRow5 As Integer
Dim lastRow6 As Integer
Dim lastRow7 As Integer

Set ws = Worksheets("Proactive Template")
Set ws2 = Worksheets("To")
Set ws3 = Worksheets("Copy")
Set ws4 = Worksheets("Comment")
Set ws5 = Worksheets("PAM Consolidation")
Set ws6 = Worksheets("PSS Contact")
Set ws7 = Worksheets("Mapping")

lastRow = ws.Range("Q" & Rows.Count).End(xlUp).Row
lastRow2 = ws2.Range("Q" & Rows.Count).End(xlUp).Row
lastRow3 = ws3.Range("Q" & Rows.Count).End(xlUp).Row
lastRow4 = ws4.Range("A" & Rows.Count).End(xlUp).Row
lastRow5 = ws5.Range("A" & Rows.Count).End(xlUp).Row
lastRow6 = ws6.Range("A" & Rows.Count).End(xlUp).Row
lastRow7 = ws7.Range("L" & Rows.Count).End(xlUp).Row

For i = 2 To lastRow
For r = 2 To lastRow2
    If ws.Cells(i, 18) = ws2.Cells(r, 17) Then
       ws.Cells(i, 20) = ws2.Cells(r, 19)
    Exit For
    End If
Next r
Next i

For i = 2 To lastRow
For s = 2 To lastRow3

    If ws.Cells(i, 18) = ws3.Cells(s, 17) Then
       ws.Cells(i, 21) = ws3.Cells(s, 19)
    Exit For
    End If
Next s
Next i

For i = 2 To lastRow
For t = 2 To lastRow4
    If ws.Cells(i, 14) = ws4.Cells(t, 1) Then
       ws.Cells(i, 19) = ws4.Cells(t, 2)
    Exit For
    End If
Next t
Next i

For i = 2 To lastRow
For u = 2 To lastRow5
    If ws.Cells(i, 11) = ws5.Cells(u, 1) Then
       ws.Cells(i, 22) = ws5.Cells(u, 2)
    Exit For
    End If
Next u
Next i

   For i = 2 To lastRow
For v = 2 To lastRow6
   If ws.Cells(i, 11) = ws6.Cells(v, 1) Then
      ws.Cells(i, 23) = ws6.Cells(v, 3)
      ws.Cells(i, 24) = ws6.Cells(v, 2)
      ws.Cells(i, 25) = ws6.Cells(v, 4)

      Exit For
   End If
Next v
Next i

For i = 2 To lastRow
For w = 2 To lastRow6
   If ws.Cells(i, 10) = ws7.Cells(w, 12) Then
      ws.Cells(i, 26) = ws7.Cells(w, 13)
      Exit For
   End If
Next w
Next i

ws.Cells.WrapText = False

End Sub


texts: Complex dashboard using presenters and/or service objects

texts:
TL;DR

I need a way to refactor a complex user dashboard with several objects and some complex data to display a accounting chart. I have read about both presenters (also called decorators or view models) and service object, but I'm not sure if I should use one or the other, or both? And how to implement this? My model and controller are pretty messy, so if anyone have some hints or suggestions, would I really appreciate it.

Long version:

I'm currently working on an application that helps users to coordinate dinner clubs and all  related accounting. (A dinner club is where people in a group, take turns to cook for the rest and then you pay a small amount to participate. This is pretty normal in dorms and colleges where I'm from). When you login are you presented to a dashboard with all important information separated in three blocks: the next dinner and option to registrate, the next dinner where you have to cook, and accounting overview like current debt, spendings etc.

This gets pretty messy: a lot of instance variables in my controller, and a lot of methods to present this view in my models.

So now to the real question: can anyone tell me any good hints, design patterns or general advice to help me refactor this code? I have read about presenters, service objects, decorators etc. but I'm not sure which to use and how.

Here are some examples of how bad it looks right now (a kitchen is the group of people that have dinner together):

# app/controllers/dashboard_controller.rb
def index
  @user = current_user
  @kitchen = @user.kitchen

  @upcoming_dinner_clubs = @user.upcoming_dinner_clubs # The next dinner clubs where the current user have to cook
  @users_next_dinner_club = @user.next_dinner_club # The first of upcoming_dinner_clubs
  @unpriced_dinner_clubs = @user.unpriced_dinner_clubs # Old dinner clubs where the user haven't specified a price yet

  # The next dinner club in the kitchen
  @next_dinner_club = @kitchen.next_dinner_club if @kitchen.next_dinner_club
  @todays_dinner_club = @next_dinner_club if @next_dinner_club && @next_dinner_club.date.today?
end

This view shows some chart of the expenses and spendings of a user, rendered through JavaScript. My views are in HAML.

# app/views/dashboard/_expenses.html.haml
%h2 Dit forbrug
  %p
    = t '.usage_html', expenses: number_to_currency(@user.last_month_expenses), spendings: number_to_currency(@user.last_month_spendings), results: number_to_currency(@user.last_month_results)
    = content_tag :div, "", id: "revenue_chart", class: "chart dashboard-chart", data: { chart: @user.usage_chart_data }
    = t '.results_html', results: number_to_currency(@user.total_results)
    = content_tag :div, "", id: "result_chart", class: "chart dashboard-chart", data: { chart: @user.result_chart_data }

Don't want you to bore you with all the details, and how the methods work, but this is the methods i have, only for displaying the expenses and spendings data in the view:

# app/models/user.rb
def last_month_expenses
  expenses_for((1.month + 1.day).ago, 1.day.ago)
end

def last_month_spendings
  spendings_for((1.month + 1.day).ago, 1.day.ago)
end

def last_month_results
  results_for((1.month + 1.day).ago, 1.day.ago)
end

def spendings_for(start_date, end_date, kitchen)
end

def expenses_for(start_date, end_date, kitchen)
end

def fee_for(start_date, end_date, kitchen)
end

def accounting_query_conditions(start_date, end_date, kitchen)
  {date: start_date..end_date, kitchen_id: kitchen.id}
end

def results_for(start_date, end_date)
  spendings_for(start_date, end_date) - expenses_for(start_date, end_date)
end

def total_fee(date = Date.today, kitchen = primary_kitchen)
end

def total_spendings(date = Date.today, kitchen = primary_kitchen)
end

def total_used_on_dinner_clubs(date = Date.today, kitchen = primary_kitchen)
end

def total_expenses(date = Date.today, kitchen = primary_kitchen)
end

def total_results(date = Date.today, kitchen = primary_kitchen)
  total_expenses(date, kitchen) - total_spendings(date, kitchen)
end


texts:
I'm using Swift 4.0 and I'm using UserDefaults in my project for storing Strings like login or something. I have a class:

class Cache: UserDefaultsManager {

    static var currentProfileID: Int? {
        get {
            return value(forKey: Constants.Cache.CurrentProfileID) as? Int
        }
        set {
            set(newValue, forKey: Constants.Cache.CurrentProfileID)
        }
    }

    static var login: String? {
        get {
            return value(forKey: Constants.Cache.Login) as? String
        }
        set {
            set(newValue, forKey: Constants.Cache.Login)
        }
    }

    // ... and there are more of this kind

, where UserDefaultsManager looks like this:

class UserDefaultsManager {
    private static var uds: UserDefaults {
        return UserDefaults.standard
    }

    static func value(forKey: String) -> Any? {
        if let encoded = uds.object(forKey: forKey) as? Data {
            return NSKeyedUnarchiver.unarchiveObject(with: encoded)
        }
        return nil
    }

    static func set(_ data: Any?, forKey: String) {
        let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: data as AnyObject)
        uds.set(encodedData, forKey: forKey)

        if (!uds.synchronize()) {
            NSLog("Failed sync UserDefaults?")
        }
    }
}

Constants.Cache is a structure that holds my keys. My question is:

  What would be a good approach to make my Cache class lighter with
  less code?

I imagine the final product should look like:

class Cache {
    static var hash: UDSObject<String>?
    static var currentProfileID: UDSObject<Int>?
    static var login: UDSObject<String>?
    static var selectedWindowID: UDSObject<Int>?
}


texts:
I'm new to both Scala and Functional programming. I've been doing the excellent course Functional Programming Principles in Scala on Coursera and I want to test out what I've learned.

I've written a very simple program that integrates the lorenz attractor.

Here is the code:

object LorenzAttractor {

  def main(Arguments:Array[String]): Unit ={

    def func(y:List[Double], t:Double):List[Double] =
      List(10.0*(y(1)-y(0)), -y(0)*y(2)+28.0*y(0)-y(1), y(0)*y(1)-(8.0/3.0)*y(2))

    val w = null
    println(numerical.odeint(func, List(1.0, 1.0, 1.0), 0.0, 1000.0, 0.0001, w))
  }

}

object numerical {

  def odeint(f: (List[Double], Double) => List[Double], y:List[Double], tstart:Double, tend:Double, dt:Double, w:BufferedWriter): List[Double] = {

    def eulerMethod(): List[Double] =
      (y, f(y, tstart).map(x => x * dt)).zipped.map(_ + _)

    def getWriter(fileName:String):BufferedWriter =
      new BufferedWriter(new FileWriter(new File(fileName)))

    def writeData():Unit = {
      w.write(f"$tstart%.3f, ${y(0)}%.3f, ${y(1)}%.3f, ${y(2)}%.3f%n")
      w.flush()
    }

    def closeBuffer():Unit =
    {
      w.flush()
      w.close()
    }

    println(tstart)
    if ((w == null) && (tstart < tend)) odeint(f, eulerMethod(), tstart+dt, tend, dt, getWriter("lorenz.csv"))

    else if (tstart < tend) { writeData(); odeint(f, eulerMethod(), tstart+dt, tend, dt, w)}

    else y
  }
}

At the moment I don't like the way that I handle the writing output, but I can't really think of a better way.

Also eventually I'd like to add some more methods to odeint and make a driver function that checks integration is within a certain tolerance.


texts: Checking if two binary trees are identical

texts:
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

Tree is defined as :

    /**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode(int x) { val = x; }
 * }
 */

And my solution is

public boolean isSameTree(TreeNode p, TreeNode q) {

    if(null == p && null == q){
        return true;
    }

    if((null == p && null != q) || (null == q && null != p)){
        return false;
    }

    if(p.val == q.val){
       return isSameTree(p.left,q.left) ? isSameTree(p.right,q.right) :false;
    } else {
        return false;
    }

}

Please suggest any improvements.


texts: Reorganizing app.js to put related initialization steps together

texts:
This is my app.js:

'use strict';
global.config = require('./config/environment/config');
global.i18n = require('./config/locales/config');

var express = require('express');
var app = express();

app.locals = global.config;

var passport = require('passport');
var session = require('express-session');
var load = require('express-load');
var flash = require('express-flash');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

app.set('views', path.join(__dirname, 'views'))
    .set('view engine', global.config.site.html.engine)
    .set('port', process.env.PORT || 3000)
    .set('env', process.env.NODE_ENV || 'development')
    .set('ip', process.env.IP || "0.0.0.0")

app.use(favicon(__dirname + '/assets/favicon.ico'))
    .use(logger('dev'))
    .use(bodyParser.json())
    .use(bodyParser.urlencoded({ extended: false }))
    .use(flash())
    .use(cookieParser())
    .use(express.static(path.join(__dirname, 'assets')))
    .use(session({ secret: 'keyboard cat' }))
    .use(passport.initialize())
    .use(passport.session());

var middlewares = require('./middlewares/config')(app);

load('models')
    .then('controllers')
    .then('routes')
    .into(app);

var prototypes = require('./helpers/prototypes');

mongoose.connect('mongodb://' + global.config.db.credentials + '@' + global.config.db.url + '/' + global.config.db.name, function(req, res) {
    console.log('db ok');
});

module.exports = app;

One aspect that annoys me is that I would prefer to have all the require() calls together.  However, if I move middlewares and prototypes before app.set(), the code will break.  Is there a better way to organize this code?


texts:
Can you review the code?

#include <stdio.h>
#include <pthread.h>
#include <windows.h>

#include "bin/include/pthread.h"
#include "bin/include/sched.h"
#include "bin/include/semaphore.h"

#define TCOUNT 10
#define WATCH_COUNT 12

int count = 0;
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t count_threshold_cv = PTHREAD_COND_INITIALIZER;
int  thread_ids[2] = {1,2};

/* Function prototypes */
void watch_count(int *idp);
void inc_count(int *idp);
int getDirFileCount(void);
/* End of fuction prototypes */

int main(void)
{
    pthread_t threads[2];
    int retCode = 0;
    int initialFileCount = getDirFileCount();
    count = initialFileCount;
    printf("No of Files initially : %d \n",initialFileCount);
    if ((retCode = pthread_create(&threads[0],NULL,(void *)&inc_count, &thread_ids[0]))) {
        perror("Thread creation error : can't able to create thread");
    }
    if ((retCode = pthread_create(&threads[1],NULL,(void *)&watch_count, &thread_ids[1]))) {
        perror("Thread creation error : can't able to create thread");
    }

    pthread_exit(NULL);
    return 0;
}

void watch_count(int *idp)
{
    pthread_mutex_lock(&count_mutex);
    //int watchCount = getDirFileCount();
    while (1) {
        printf("waiting for changes made to the directory \n");
        pthread_cond_wait(&count_threshold_cv,
                          &count_mutex);
        printf("OK , now someone made chaneges to the directory\n");
    }
    pthread_mutex_unlock(&count_mutex);
}
void inc_count(int *idp)
{
    for (;;) {
        pthread_mutex_lock(&count_mutex);
        int countnow = getDirFileCount();
        printf("Number of files now: %d \n",countnow );
        if (count < countnow) {
            pthread_cond_signal(&count_threshold_cv);
            pthread_mutex_unlock(&count_mutex);
            return;
        }
        //pthread_cond_signal(&count_threshold_cv);
        pthread_mutex_unlock(&count_mutex);
        //pthread_exit(NULL)

    }
}

int getDirFileCount(void)
{
    int count = 0;
    WIN32_FIND_DATA fd;
    char *dirFilePath = "D:\\Test\\*";
    HANDLE h = FindFirstFile(dirFilePath, &fd);
    if (h != INVALID_HANDLE_VALUE) {
        do {
            count++;
        } while (FindNextFile(h, &fd));
        FindClose(h);
    }
    return count;
}


texts:
This is the first question for this language, and I decided I would do a FizzBuzz.

I used a template literal as a proof of concept, same goes for the pipe (|>). The code creates a function that recreates the traditional FizzBuzz in FreezeFlame:

var fizzBuzz = (max) -> {
    for(var i = 0; i < max + 1; i++) {
        (if i % 3 === 0 || i % 5 === 0 then "#{i % 3 == 0 ? 'Fizz' : ''}#{i % 5 === 0 ? 'Buzz' : ''}" else i) |> console.log
    }
}

This is equivalent to the following in JavaScript:

var fizzBuzz = (max) => {
    for(var i = 0; i < max + 1; i++){
        console.log((i % 3===0||i % 5===0 ? `${i % 3 == 0 ? 'Fizz' : ''}${i % 5 === 0 ? 'Buzz' : ''}` : i));
    };
}

The following features are used:

FreezeFlame ternary (if ... then ... else ...)
FreezeFlame template literals (#{...})
JS ternary (... ? ... : ...)
FreezeFlame pipe (... |> ...)
FreezeFlame function ((...) ->  { ... })

My main worries are the testing.

FreezeFlame GitHub


texts: Approach to a better flow for this context in terms of routes and controller methods? (avoid post request returning a view)

texts:
I have the context below, I dont know if the flow as it is its a correct approach in terms of controller methods and routes used because a post request is returning a view and it seems that its not a good approach.

Do you know how to properly improve the context below?

I have a page to show the congress details "http://project.test/congress/1/congress-test."

route for this page:

Route::get('/congress/{id}/{slug?}', [
    'uses' => 'FrontController@show',
    'as'   =>'congresses.show'
]);

In this page there is a form for the user select the quantities he want for each ticket type, so there is a form with this action:

<form method="post" action="{{route('congresses.registration', ['id' => $congress->id, 'slug' => $congress->slug])}}">

This above form thas the route:

Route::post('/congress/{id}/{slug?}/registration', [
    'uses' => 'RegistrationController@storeQuantity',
    'as'   =>'congresses.registration'
]);

So, when user submits the above form, the storeQuantity method returns the registration.blade.php, the url of this page is "http://project.test/congress/1/congress-test/registration.". StoreQuatity method:

 public function storeQuantity(Request $request, $id){
        $typeQuantities = $request->get('types');

        foreach($typeQuantities as $typeName => $quantity){

        $type = TicketType::where('name', $typeName)->firstOrFail();
        $price = $type->price;

        $selectedTypes[$type->name]['quantity'] = $quantity;
        $selectedTypes[$type->name]['price'] = $price;
        }
        return view('congresses.registration')
        ->with('selectedTypes', $selectedTypes);
    }

So there is the storeQuantity() that stores the selected quantities by  the user and returns the user to the registration.blade.php.


texts: Distinct nearest pixels for floating-point coordinates

texts:
dem_rows and dem_cols contain float values for a number of things i can identify in an image, but I need to get the nearest pixel for each of them, and than to make sure I only get the unique points, and no duplicates.

The problem is that this code is ugly and as far as I get it, as unpythonic as it gets. If there would be a pure-numpy-solution (without for-loops) that would be even better.

# next part is to make sure that we get the rounding done correctly, and than to get the integer part out of it
# without the annoying floatingpoint-error, and without duplicates
fielddic={}
for i in range(len(dem_rows)):
    # here comes the ugly part: abusing the fact that i overwrite dictionary keys if I get duplicates
    fielddic[int(round(dem_rows[i]) + 0.1), int(round(dem_cols[i]) + 0.1)] = None
# also very ugly: to make two arrays of integers out of the first and second part of the keys
field_rows = numpy.zeros((len(fielddic.keys())), int)
field_cols = numpy.zeros((len(fielddic.keys())), int)
for i, (r, c) in enumerate(fielddic.keys()):
    field_rows[i] = r
    field_cols[i] = c


texts:
This is function, that works correctly, but it seems to be too ugly.
panel argument is never used for anything but pushing into refreshGamePanel,
and it seems to be too much case statements. How can I cleanup this?

runGameLoop :: Game -> Panel -> Curses GameResult
runGameLoop game panel
  = if victory game then return Victory else
      refreshGamePanel game panel >> render >> getPanelWindow panel >>=
        flip getEvent (Just 1)
        >>=
        \ event' ->
          case event' of
              Nothing -> runGameLoop game panel
              Just event -> case event of
                EventSpecialKey (KeyFunction 2) -> return Restart
                EventSpecialKey (KeyFunction 10) -> return Quit
                EventSpecialKey key' -> case keyToDirection key' of
                  Nothing -> runGameLoop game panel
                  Just dir -> runGameLoop (makeMove game dir) panel
                _ -> runGameLoop game panel


texts: Function that reorders the columns of a matrix

texts:
The objective: Given an m by n matrix, I have written a function in Matlab that reorders the columns of the matrix to output linearly independent columns; other/redundant columns. Basically, it just pushes the redundant columns to the end of the matrix.

Here is my attempt at the code (which produces correct results):

function result = find_redundant_assets(A)

if (rank(A) == size(A, 2)) % if the rank is the same as the number of columns => full rank
    result = A;
else % there exists at least one linearly dependent column
    A_red = [];
    for n = 1:(size(A,2)-1) % fix a column and compare to all neighbors to the right
        for p = n+1:size(A,2) % loop through comparison columns

            if ( isequal( A(:,p)' * round(A(:, n)'/ A(:,p)') == A(:,n)', ones(1, size(A,1)))) % TRUE iff the nth column is a scalar multiple of the pth column
                A_red = [A_red, A(:,n), A(:,p)]; % add the linearly depedent rows
            end

            if (isequal(A(:,n)' * round(A(:, p)'/ A(:,n)') == A(:,p)', ones(1, size(A,1)))) % TRUE iff the pth column is a scalar multiple of the nth column
                A_red = [A_red, A(:,p), A(:,n)]; % add the linearly depedent row
            end

        end
    end
   A_red = unique(A_red.', 'rows', 'stable')';
   result = fliplr([A, A_red]);
   result = unique(result.', 'rows', 'stable')';
   result = fliplr(result);
end
end

This function gets pretty complicated, but it makes sense to me.

How can I improve my code? Does anyone know a better way to accomplish this task?

Also, I know this is not efficient code because I am constantly re-sizing the matrix.