texts: Is there a better design pattern for a SDK?

texts:
At work I decided it would be better to reverse engineer a SDK for one of our pieces of hardware. Because it is work related I can't share the exact code.. but I can give enough of a gist of the code with the following question in mind. Is there a design pattern that better reflects what I want to do?

I'm going to describe what the endgame is, then lay the code out with how to use it first, how it is implemented, and what I was thinking about for a change.

The end game is that the device I have communicates via RS232. I write a byte array to it to have it do things for me, it responds back with a similar structure. (The response is not of concern though). So what I have done is created a rather large factory class (not exactly a factory pattern, but I couldn't think of a better name). The code in small part looks similar to this

public class Command
{
    public ushort DeviceId{get;private set;}
    public byte CommandId {get; private set;}
    public byte[] Data {get; private set;}

    public Command(ushort deviceId, byte commandId, params byte[] data)
    {
        //assign to properties
    }
}
public static class CommandAdapter
{
    public static byte[] ToArray(this Command command)
    {
        //build byte array to match format needed
        return buffer;
    }
}

public class Device
{
    private ushort deviceId;
    private SerialPort devicePort;
    public Device(string comPort)
    {
        devicePort = new SerialPort(comPort, 115200);
        byte[] response = TransceiveCommand(CommandFactory.GetDeviceId());
        deviceId = BitConverter.ToUInt16(response, 0);
    }
    public byte[] FooBar()
    {
        byte mode = 1;
        byte option = 0x52;
        Command cmd = CommandFactory.DoFooBar(deviceId, mode, option)
        return TransceiveCommand(cmd);
    }

    private byte[] TransceiveCommand(Command cmd)
    {
        byte[] buffer = cmd.ToArray();
        devicePort.Write(buffer, 0, buffer.Length);
        byte[] response = new DeviceResponseParser.GetResponse(devicePort);
        return response;
    }
}

and I think that this code isn't so bad. it's the factory that I don't care much for.

public static class CommandFactory
{
    public static Command GetDeviceId()
    {
        return new Command(0, GET_DEVICE_CMD_ID);
    }

    public static Command DoFooBar(ushort deviceId, byte mode, byte option)
    {
        return new Command(deviceId, FOOBAR_CMD_ID, mode, option);
    }

    public static Command Another(ushort deviceId, byte[] data)
    {
        return new Command(deviceId, ANOTHER_CMD_ID, data);
    }

    public static Command SetSerialNumber(ushort deviceId, string newSerial)
    {
        byte[] array = new byte[newSerial.Length + 2]; //1 for 0x12, 1 for length
        array[0] = 0x12; //not sure what the 0x12 is, but it is required
        array[1] = (byte)(newSerial.Length); //+1 for null-termination
        for (int i = 0; i < newSerial.Length; i++) { array[i + 2] = (byte)newSerial[i]; }
        return new Command(deviceId, SET_SERIAL_CMD_ID, array);
    }
    //..~30 more commands

    private const byte GET_DEVICE_CMD_ID = 0x01;
    private const byte FOOBAR_CMD_ID = 0x02;
    //.. ~30 more command id's
}

In the end the commands are not going to change, so I've considered just using a byte array instead of a whole class for command. I don't like the idea of making an extension method for a byte array which makes me second guess doing that. Every command except for GetDeviceId takes in as it's first parameter the device id. So I wrote down on paper an idea about writing something like CommandFactory.CreateCommandFor(deviceId).FooBar()

or I've also thought about making a non-static Command factory pass in the deviceId and have it send back commands. Then I wouldn't have to pass in the device Id each time. Are there any design patterns that would better match what I'm trying to acheive?


texts: More groovifed way of sublist with varying range

texts:
Student list of 50 students and a teacher list of 3 teachers.

Section XML as the output where each teacher is assigned to a section, and up to 20 students are assigned in that section.

Need to loop on each teacher element, and for each teacher element, loop on the student name from 1-20, than for 2nd teacher, students 21-40 etc.

The following is working code. However, feeling that there could be better groovified way to get 20 students for a teacher

def students = """<Students>
    <name> st name 1</name>
    <name> st name 2</name>
    <name> st name 3</name>
    <name> st name 4</name>
    <name> st name 5</name>
    <name> st name 6</name>
    <name> st name 7</name>
    <name> st name 8</name>
    <name> st name 9</name>
    <name> st name 10</name>
    <name> st name 11</name>
    <name> st name 12</name>
    <name> st name 13</name>
    <name> st name 14</name>
    <name> st name 15</name>
    <name> st name 16</name>
    <name> st name 17</name>
    <name> st name 18</name>
    <name> st name 19</name>
    <name> st name 20</name>
    <name> st name 21</name>
    <name> st name 22</name>
    <name> st name 23</name>
    <name> st name 24</name>
    <name> st name 25</name>
    <name> st name 26</name>
    <name> st name 27</name>
    <name> st name 28</name>
    <name> st name 29</name>
    <name> st name 30</name>
    <name> st name 31</name>
    <name> st name 32</name>
    <name> st name 33</name>
    <name> st name 34</name>
    <name> st name 35</name>
    <name> st name 36</name>
    <name> st name 37</name>
    <name> st name 38</name>
    <name> st name 39</name>
    <name> st name 40</name>
    <name> st name 41</name>
    <name> st name 42</name>
    <name> st name 43</name>
    <name> st name 44</name>
    <name> st name 45</name>
    <name> st name 46</name>
    <name> st name 47</name>
    <name> st name 48</name>
    <name> st name 49</name>
    <name> st name 50</name>
</Students>"""

def teachers = """<Teachers>
    <name> Teacher name 1</name>
    <name> Teacher name 2</name>
    <name> Teacher name 3</name>
</Teachers>"""

def getData  = { xml, elementName ->
    def parseXml = new XmlSlurper().parseText(xml)
    parseXml.'**'.findAll { it.name() == elementName }
}

def teacherNodes = getData( teachers, 'name')
def studentNodes = getData( students, 'name')

def sublist (data, start, end) {
    start == 0 ? data.take(end) : data.drop(start).take(end-start)
}

def incrementBy = 20
def startStudents = 0
def endStundents = incrementBy
def builder = new groovy.xml.StreamingMarkupBuilder()
builder.encoding = 'UTF-8'
def xml = builder.bind {
    mkp.xmlDeclaration()
    Assignments {
        teacherNodes.eachWithIndex { teacherNode, index ->
            Section {
                id (index+1)
                teacher (teacherNode.text())
                studentList {
                    sublist(studentNodes, startStudents, endStundents).each { studentNode ->
                        name (studentNode.text())
                    }
                }
            }
            startStudents = endStundents
            endStundents += incrementBy
        }
    }
}

println groovy.xml.XmlUtil.serialize(xml)


texts: A Get method for an API, with three modes

texts:
Is this good way to create a get API method with different service methods?
Based on the scope parameter, it decides what service method in the API to call.

Enum:

    enum scopeValue
    {
          None,
          GetAll,
          GetWithDetails
    };

API:

   public class WorkItemController : BaseController
    {
        private IWorkItemService workItemService;

        public WorkItemController(IWorkItemService workItemService)
        {
            this.workItemService = workItemService;
        }

        [HttpGet]
        public async Task<IEnumerable<WorkItemDto>> Get(int id, string scope = "None")
        {
            scopeValue choice;

            Enum.TryParse(scope, out choice);

            if (choice == scopeValue.None)
            {
                return await workItemService.Get(id);
            }
            else if (choice == scopeValue.GetAll)
            {
                return await workItemService.GetAll();
            }
            else if (choice == scopeValue.GetWithDetails)
            {
                return await workItemService.GetAllWithDetails();
            }
        }
    }


texts: C++ vector that uses expression templates technique to increase performance of mathematical expressions

texts:
I've been reading Item 6 from Scott Meyers' Effective Modern C++ and noticed that he mentioned a technique called expression templates. I've decided to give it a try and implemented a vector that supports addition and subtraction:

#include <iostream>
#include <vector>

template<typename T>
class Vec
{
public:
    std::vector<T> data;

    typedef typename std::vector<T>::size_type size_type;

    Vec(size_type size): data(size)
    {
    }

    Vec(const std::initializer_list<T>& elements): data(elements.size())
    {
        size_type i = 0;
        for (const auto& el: elements)
        {
            data[i++] = el;
        }
    }

    template<typename VecOperation>
    Vec(const VecOperation& vo): data(vo.t2.data.size())
    {
        for (size_type i = 0; i < data.size(); ++i)
        {
            data[i] = vo[i];
        }
    }

    T operator[](size_type i) const
    {
        return data[i];
    }
};

template<typename T1, typename T2>
struct VecSum
{
    const T1& t1;
    const T2& t2;
    auto operator[](typename T2::size_type i) const
    {
        return t1[i] + t2[i];
    }
};

template<typename T1, typename T2>
struct VecDiff
{
    const T1& t1;
    const T2& t2;
    auto operator[](typename T2::size_type i) const
    {
        return t1[i] - t2[i];
    }
};

template<typename T1, typename T2>
auto operator+(const T1& t1, const T2& t2)
{
    return VecSum<T1, T2>{t1, t2};
}

template<typename T1, typename T2>
auto operator-(const T1& t1, const T2& t2)
{
    return VecDiff<T1, T2>{t1, t2};
}

int main()
{
    Vec<int> v1{1, 2, 3, 4, 5};
    Vec<int> v2{6, 7, 8, 9, 11};
    Vec<int> v3{3, 5, 2, 0, 17};

    Vec<int> v4 = v1+v2-v3;

    for (const auto& x: v4.data)
    {
        std::cout << x << ", ";
    }
    std::cout << std::endl;

    return 0;
}

The main advantage of this solution is that in the line Vec<int> v4 = v1+v2-v3; no additional temporaries of type Vec are created, which increases performance.

I'd be grateful if someone could point potential drawbacks and possible improvements of this code.


texts: All integer sequences given amount of integers and length of sequence

texts:
Given two integers, X and Y, you must create all possible integer sequences of length Y using the numbers from 1 to X.

For certain reasons, the function must return a deque<deque<int> > representing such (the primary deque holds all my sequences - each of then is a deque of ints).

This is not a permutation generator.

Example Input

X = 3, Y = 3

This means that I require sequences of length 3 each one. To compose the sequences, I use the integers 1, 2, and 3.

Example Output

The previous input should produce:

1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3

My Idea

I'm not very good with this stuff. My approach is:

Create a start and end integer

Start is 1 repeated Y times. So in the previous example it is 111
End is X repeated Y times. So in the previous example it is 333

I count i from start to end.

If i contains an invalid digit (like 4, 5,...), abort this sequence.
If i is valid, then this sequence is valid, thus add it to my resulting deque.
When a sequence is valid, due to my needs, I split the integer and make a deque of each of its digits.

Why I need a review

I think it is pretty clear that this method doesn't sound very efficient. So many wasted iterations! Can you help me improve this code, and, perhaps, shorten it?

My Attempt

Note, you may notice that instead of "sequences" I call then "procedures".

#include <iostream>
#include <sstream>
#include <deque>

using namespace std;

// Converts a value to any type
template <typename T,typename S>
T convert(S original) {
    stringstream ss; ss << original;
    T result; ss >> result;
    return result;
}

// The actual function
deque<deque<int> > allPossibleProcedures(int values, int depth) {
    deque<deque<int> > result;
    // I create a start point and an end point for my counting
    string minString; for (int d = 0; d < depth; ++d) { minString.append("1"); }
    string limitString; for (int d = 0; d < depth; ++d) { limitString.append(convert<string>(values)); }
    int start = convert<int>(minString);
    int limit = convert<int>(limitString);
    // I begin counting
    for (int i = start; i <= limit; ++i) {
        deque<int> procedure;
        string text = convert<string>(i);
        bool ok = true;
        for (int c = 0; c < text.length() && ok; ++c) {
            int x = convert<int>(text.at(c));
            if (x <= values && x > 0) {
                // This is a valid character. Add to procedure.
                procedure.push_back(x);
            }else{
                // Not a valid character. Abort this procedure.
                ok = false;
                break;
            }
        }
        if (ok) {
            result.push_back(procedure);
        }
    }
    return result;
}

int main(int argc, const char * argv[]) {
    deque<deque<int> >procedures = allPossibleProcedures(3,3);
    for (int i = 0; i < procedures.size(); ++i) {
        for (int j = 0; j < procedures[i].size(); ++j) {
            cout << procedures[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}


texts:
I am developing a large .NET app.  The previous developer used Martin Fowlers Transaction script design pattern.  Please see the code below:

Public Class TypeStudent
    Private _Name As String
    Private _Type As String 'Undergraduate or postgraduate
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Public Property Type() As String
        Get
            Return _Type
        End Get
        Set(ByVal value As String)
            _Type = value
        End Set
    End Property
End Class

Public Class Students
    Public Sub Add(ByVal typeStudent As TypeStudent)

    End Sub

    Public Sub Remove(ByVal typeStudent As TypeStudent)

    End Sub

    Public Sub Enrol(ByVal typeStudent As TypeStudent)
        If typeStudent.Type = "U" Then
            'Undergraduate specific logic
        ElseIf typeStudent.Type = "P" Then
            'Postgraduate specific logic
        End If
    End Sub
End Class

Notice that there is one Student class and Student.Enrol has to check the type of student before executing the appropriate logic.  Now please see the refactored code below, which uses a domain model approach:

Public Class Student
    Private Name As String
    Private Type As String

    Public Sub Add()

    End Sub

    Public Sub Remove()

    End Sub

End Class

Public Class Undergraduate
    Inherits Student

    Public Sub Enrol()

    End Sub

End Class

Public Class Graduate
    Inherits Student

    Public Sub Enrol()

    End Sub

End Class

Notice that Undergraduate and PostGraduate has its own implementation of Enrol.  I want to refactor more code so that it is like sample B to make it more reuseable and maintainable.  My question is; is it a bad idea to mix and match domain logic patterns in the same application i.e. part of the app written using Transaction Script and another part using domain model?


texts: Activate submit button on form change and detach event listener

texts:
We have pages that contain 1-3 forms. Each form does have a submit button which is styled as inactive* as long as the user didn't change the form.

Now I need to:

remove the class from the button as soon as the form changes
remove the event listener from the form

I came up with this:

for (const form of Array.from(document.getElementsByClassName('form'))) {
    const formHandlerChange = event => {
        const submit = form.getElementsByClassName('disabled')[0];
        submit.classList.remove('disabled');
        form.removeEventListener('change', formHandlerChange);
    };

    form.addEventListener('change', formHandlerChange);
}

Is this a good solution? Is it a bad thing to create formHandlerChange for each form? Would it be better to get the form each time formHandlerChange is entered manually and have this function defined only once outside the loop?

* It's also disabled but I removed it for simplicity of this review.


texts: Executing query until there is nothing more left

texts:
I often query a database to get a batch of items to procees. I do this as long as the query returns some items. I use this pattern quite a lot so I thought I create a small helper so that I don't have to implement this logic again and again.
It's a small class that executes the query until there is nothing more left:
public static class Unfold
{
    public static async Task ForEachAsync<T>
    (
        Func<CancellationToken, Task<IList<T>>> query,
        Func<IList<T>, CancellationToken, Task> body,
        CancellationToken cancellationToken
    )
    {
        while (true)
        {
            var result = await query(cancellationToken);
            if (result.Any())
            {
                await body(result, cancellationToken);
            }
            else
            {
                break;
            }
        }
    }
}

The reason why I implemented it exactly this way is:

all my queries are async
they must always return IList<T> (if they return a collection of course)
I always process a batch at a time that I then mark as processed

Example
The typical use-case is like this:

get a batch of items from a repository
process this batch
repeat until the batch is empty

async Task Main()
{
    var numbers = new NumberRepository();
    await Unfold.ForEachAsync
    (
        query: async token => await numbers.GetNumbersAsync(token),
        body: ProcessBatch,
        CancellationToken.None
    );
}

A test repository:
public class NumberRepository
{
    private readonly IList<IList<int>> _numbers = new[] { new[] { 1, 2 }, new[] { 3, 4 }, new[] { 5 }, new int[0] };
    private int _batchIndex;

    public Task<IList<int>> GetNumbersAsync(CancellationToken cancellationToken) => Task.FromResult(_numbers[_batchIndex++]);
}

and the processing method:
private Task ProcessBatch<T>(T item, CancellationToken cancellationToken)
{
    item.Dump();
    return Task.CompletedTask;
}

What do you say? Is this a good or a bad solution? Is there anything missing (but null-checks)?


texts: Determining the existence of string Intersections

texts:
Problem:

  Given 2 strings, consider all the substrings within them
  of length len. Len will be 1 or more.
  Returns true if there are any such substrings which appear in both strings.
  Compute this in linear time using a HashSet.

Solution:

import java.util.HashSet;

public class Standford3 {
    public static void main(String[] args) {
        System.out.println("Test 1: " +
            (false  == stringIntersect("blahblah", "foralheh", 3))
        );
        System.out.println("Test 2: " +
            (true  == stringIntersect("checking", "deck", 2))
        );
        System.out.println("Test 3: " +
            (false  == stringIntersect("derping", "slurp", 3))
        );
        System.out.println("Test 4: " +
            (false  == stringIntersect("foo", "bar", 1))
        );
        System.out.println("Test 5: " +
            (true  == stringIntersect("nowai", "55&dcsnow", 3))
        );
    }

    public static boolean stringIntersect(String a, String b, int len) {
        if (a.length() == 0 || b.length() == 0) { return false; }

        HashSet<String> alpha = permutateString(a, len);
        HashSet<String> beta = permutateString(b, len);

        for (String s : alpha) {
            if (beta.contains(s)) { return true; }
        }

        return false;
    }

    public static HashSet<String> permutateString(String str, int i) {
        if (i > str.length()) {
            throw new IllegalArgumentException(
                "Substring length cannot be larger than provided string"
            );
        }

        HashSet<String> set = new HashSet<>();
        int count = i;

        for (int j = 0; j < str.length(); j++ ) {
            if (count > str.length()) { break; }

            set.add(str.substring(j, count));
            count++;
        }

        return set;
    }
}

Are these tests sufficient? Unlike the two challenges that preceded it, the tests are mine; is there anything I should be testing for that I missed?
I hope this isn't outside of codereview territory, but I'm wondering if something more was meant by "compute in linear time" or does this adequately encompass that requirement?
Although the use of HashSet was explicitly cited, I'm wondering if there are performance advantages in using another built in class like LinkedHashSet?


texts: Effective 3d array transposition with R/C++ (Rcpp)

texts:
Task is reshape 3d array from [row, col, slice] to [slice,row,col]. I tried implement base::aperm analog with Rcpp.

// [[Rcpp::export]]
Rcpp::NumericVector array_perm(const Rcpp::NumericVector& x) {
    if (Rf_isNull(x.attr("dim"))) {
        throw std::runtime_error("'x' does not have 'dim' attibute.");
    }
    Rcpp::Dimension d = x.attr("dim");
    if (d.size() != 3) {
        throw std::runtime_error("'x' must have 3 dimensions.");
    }
    std::size_t n = d[2];
    std::size_t n_vec = d[0] * d[1];
    std::size_t n_out = n_vec * n;
    Rcpp::NumericVector res = Rcpp::no_init(n_out);
    std::size_t ind_from = 0;
    for (std::size_t i = 0; i < n; ++i) {
        std::size_t ind_to = i;
        for (std::size_t j = 0; j < n_vec; ++j) {
            res[ind_to] = x[ind_from];
            ind_to += n;
            ind_from += 1;
        }
    }
    res.attr("dim") = Rcpp::Dimension(d[2], d[0], d[1]);
    return res;
}

How can I improve it?

For testing code:

# Observations
n <- 1000
# Dimension
d <- 100
# Array
a <- replicate(n, matrix(seq_len(d * d), nrow = d, ncol = d))
# Desired result
res <- aperm(a, c(3, 1, 2))
res

Small benchmark current variant of the code:

b <- bench::mark(aperm(a, c(3, 1, 2)), array_perm(a))
b[, c("expression", "min", "median", "max", "itr/sec")]
#>   expression                min   median      max `itr/sec`
#>   <chr>                <bch:tm> <bch:tm> <bch:tm>     <dbl>
#> 1 aperm(a, c(3, 1, 2))   84.9ms   85.1ms   85.5ms     11.7
#> 2 array_perm(a)         124.8ms  125.2ms  127.2ms      7.96

RcppArmadillo solution

// [[Rcpp::export]]
arma::Cube<double> array_perm2(const arma::Cube<double>& x) {
    std::size_t rows = x.n_rows;
    std::size_t cols = x.n_cols;
    std::size_t slices = x.n_slices;
    arma::Cube<double> res(slices, rows, cols);
    for(std::size_t i = 0; i < rows; ++i) {
        for(std::size_t j = 0; j < cols; ++j) {
            for(std::size_t k = 0; k < slices; ++k) {
                res(k, i, j) = x(i, j, k);
            }
        }
    }
    return res;
}

Benchmark:

  expression                min   median      max `itr/sec`
  <chr>                <bch:tm> <bch:tm> <bch:tm>     <dbl>
1 aperm(a, c(3, 1, 2))   85.8ms   86.4ms   87.7ms     11.6
2 array_perm(a)         116.1ms  127.3ms  129.6ms      8.08
3 array_perm2(a)        217.4ms  219.7ms  222.1ms      4.55


texts: TopCoder problem "Substitute" - SRM 160 (Division II Level One)

texts:
I have solved the problem "Substitute" at Topcoder.

  A simple, easy to remember system for encoding integer amounts can be very useful. For example, dealers at flea markets put the information about an item on a card that they let potential buyers see. They find it advantageous to encode the amount they originally paid for the item on the card.
  A good system is to use a substitution code, in which each digit is encoded by a letter. An easy to remember 10-letter word or phrase, the key, is chosen. Every '1' in the value is replaced by the first letter of the key, every '2' is replaced by the second letter of the key, and so on. Every '0' is replaced by the last letter of the key. Letters that do not appear in the key can be inserted anywhere without affecting the value represented by the code.. This helps to make the resulting code much harder to break (without knowing the key).
  Create a class Substitute that contains the method getValue that is given the Strings key and code as input and that returns the decoded value.

I started studying Big O notation and I think the complexity of my program is \$O(n^2)\$. How can I make my program more efficient and clean? Can I make it \$O(n)\$?

     public class Substitute {

    public int getValue(String key, String code) {
        String s = "";
        for (int i = 0; i < code.length(); i++) {
            for (int j = 0; j < key.length(); j++) {
                int x = key.indexOf(code.charAt(i)) + 1;
                if (code.charAt(i) == key.charAt(j)) {
                    if (x >= 10) {
                        x = 0;
                    }
                    s = s + x;
                }
            }
        }
        return Integer.parseInt(s);
    }
}


texts: Ignore nil values and unwrap optionals returning the value as the wrapped type

texts:
My primary concern is that this has been written as a global function.  I think it would be better if this was a function of the SignalProducer class but I am not sure that is even possible.  Is this thinking justified or is the use of global functions acceptable?

(The following code uses ReactiveCocoa 4 and is written in Swift.)

func ignoreNilValues <Value,Error> (producer: SignalProducer<Value?,Error>) -> SignalProducer<Value, Error> {
    return producer.filter { return $0 != nil }.map { $0! }
}


texts:
Problem

I want to receive advice on my code, which takes 0.012 seconds.

import numpy as np
import time
start_time = time.time()
raw_data= """
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
"""
def data_input():
    a = raw_data.split("\n")
    new_a = [x.split(" ") if True else None for x in a]
    del(new_a[0]) # delete first element in new_a
    del(new_a[-1])
    return(new_a) # [['75'], ['95', '64'], ['17', '47', '82'], ['18', '35', '87', '10'], ['20', '04', '82', '47', '65'],...

def make_memoi_table():
    #make a table from below to above (to the top)
    new_a = data_input() # new_a : original raw_input data
    size = len(new_a)
    memoi_table = np.zeros((size,size))
    memoi_table[size-1] = [int(x) for x in new_a[size-1]]
    for i in range(2,size + 1):
        for j in range(len(new_a[size-i])):
            memoi_table[size-i][j] = int(new_a[size-i][j]) + max(memoi_table[size-i+1][j],memoi_table[size-i+1][j+1])
    return(memoi_table[0][0],memoi_table)

print(make_memoi_table())

print("--- %s seconds ---" % (time.time() - start_time))


texts: Checking data on an incoming request in Go

texts:
I am trying to learn Go and as way of throwing myself in the deep-end I thought it would be cool to try building a web framework instead of using an already existing solution. I'm coming from the JavaScript world so all the type and interface stuff is new to me as well. I was wondering if this is an acceptable approach to doing something.
Essentially, I am trying to take a request that someone might send to a server and check some information about it to determine whether or not it should be allowed to move on. Specifically, in this example I am looking to see if the request method from a client is the same as the one the user of this framework intended an endpoint to be used with.
To do so, I am using middleware to access the req data, however I am unsure if this is ok... Here is my code, any feedback or suggestions would be much appreciated.
Running of the server:
var mux = http.NewServeMux()

func (r *Router) Run(c RunConfig) {
    log.Fatal(http.ListenAndServe(c.Port, mux))
}

Small portion of my router code:
func checkMethod(next http.Handler, method string) http.Handler {
    return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
        fmt.Printf("checkMethod\n \tMehtod: %v\n \tpath: %v\n", r.Method, r.URL.Path)
        if method == r.Method {
            next.ServeHTTP(rw, r)
        } else {
            fmt.Println("Wrong method")
        }
    })
}

// GET is a receiver function of Router and is used to create a route with a GET request.
// path {string}
// handler {http.HandlerFunc}
func (r *Router) GET(path string, handler http.HandlerFunc) {
    r.tree[path] = createNode(path, "GET", handler)
    mux.Handle(path, checkMethod(handler, "GET"))
}

It seems to work but is this an acceptable solution? Is there any reason not to do this? Would it scale?
Please let me know if you have questions or need to see more code (although this is more-or-less it right now).


texts: Multiple producer single consumer on the same thread

texts:
I implemented a multi-producer single consumer class, with an important restriction that once the consumer started working, it must continue its work on the same thread (the reasoning behind this is to allocate and free COM objects from a third party library, and these actions must be on the same thread according to that library).
Here's the code, followed by an example:
interface WorkerLogic<T>
{
    void Work(T item);
}

class ThreadedWorker<T> where T : class
{
    private readonly object locker = new object();
    private readonly Queue<T> queue = new Queue<T>();
    private readonly WorkerLogic<T> logic;
    private readonly Thread actualThread;
    private bool started = false;
    private readonly ManualResetEvent resetEvent = new ManualResetEvent(false);
    private volatile bool shouldWork = true;

    public ThreadedWorker(WorkerLogic<T> logic)
    {

        this.logic = logic;
        actualThread = new Thread(Spin);
    }

    private void Spin()
    {
        while (shouldWork)
        {
            resetEvent.WaitOne();
            while (shouldWork)
            {
                T item;
                lock (locker)
                {
                    if (queue.Count == 0)
                        break; // back to main loop and WaitOne
                    item = queue.Dequeue();
                    resetEvent.Reset();
                }
                try
                {
                    logic.Work(item);
                }
                catch (Exception ex)
                {
                    // log
                }
            }
        }
    }

    public void Stop()
    {
        Stop(TimeSpan.FromMilliseconds(500));
    }

    public void Stop(TimeSpan timeout)
    {
        shouldWork = false;
        lock (locker)
        {
            resetEvent.Set();
        }
        bool joined = actualThread.Join(timeout);
        if (!joined)
        {
            try
            {
                actualThread.Abort();
            }
            catch (ThreadStateException)
            {
                // swallow
            }
        }
    }

    public void Push(T item)
    {
        lock (locker)
        {
            queue.Enqueue(item);
            if (!started)
            {
                started = true;
                actualThread.Start();
            }
            resetEvent.Set();
        }
    }
}

Usage example for the code above:
public class UsageExample
{
    public class MyLogic : WorkerLogic<string>
    {
        public void Work(string s)
        {
            Console.WriteLine("working on '{0}', thread id = {1}", s, Thread.CurrentThread.ManagedThreadId);
        }
    }

    public static void Main()
    {
        ThreadedWorker<string> threadedWorker = new ThreadedWorker<string>(new MyLogic());
        threadedWorker.Push("My");
        threadedWorker.Push("name");
        threadedWorker.Push("is");
        threadedWorker.Push("Luca");
        threadedWorker.Stop();
    }

}

Example output:
working on 'My', thread id = 3
working on 'name', thread id = 3
working on 'is', thread id = 3
working on 'Luca', thread id = 3

I created a repl for this, feel free to play with it.
I'd like to have a peer review focused on correctness: is it thread safe? Am I missing an edge case?
Thanks!


texts:
This is in one of my AngularJS controllers:

websiteService.get('websites').then(function(data) {
    $scope.websites = data;
    websiteService.get('groups').then(function(data) {
        $scope.groups = data;
        websiteService.get('websites_groups').then(function(data) {
            $scope.websites_groups = data;
            // If everything goes well, my code will continue here - not really neat
        }, function(error) {
            $scope.errors.push(error);
        });
    }, function(error) {
        $scope.errors.push(error);
    });
}, function(error) {
    $scope.errors.push(error);
});

What would be a better, more elegant way to do this? Is there a best practice?

In my opinion, success functions are nested really nicely, in obvious and chronological and logical order, while error functions make everything messy.


texts:
I'm writing a method to save an input stream to an output stream. What's the optimal size for the buffer? Here's my method:

/**
* Saves the given InputStream to a file at the destination. Does not check whether the destination exists.
*
* @param inputStream
* @param destination
* @throws FileNotFoundException
* @throws IOException
*/
public static void saveInputStream(InputStream inputStream, File outputFile) throws FileNotFoundException, IOException {
  try (OutputStream out = new FileOutputStream(outputFile)) {
    byte[] buffer = new byte[2097152]; //This is set to two MB. But I have no idea whether this is optimal
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
      out.write(buffer, 0, length);
    }
    inputStream.close();
  }
}


texts: Sum of primes program is taking too long

texts:
I don't understand what is taking the program so long:

#!/bin/python

primes=[]
i=0
j=0
k=0

for i in range(2,2000000): #fill in the list
    primes.append(i)

i=0
while i<len(primes):
    j=primes[i]
    print(j)
    k=0
    while j*(j+k)<primes[len(primes)-1]: ##referred as 'line A'
        try:
            primes.remove(j*(j+k))
        except ValueError:
           k=k+1
           continue
        k=k+1
    i=i+1

sump=0
i=0
for i in range(len(primes)):
    sump=sump+primes[i]

print(sump)

I understand why the overall code is very inefficient, but line A takes 2 hours for j=2, and I don't understand why that is. Surely the list.remove(x) method is very inefficient?


texts:
I'm preparing for an interview soon, and I've written the following linked list implementation from scratch.

This is all self-taught, and I'm still working on my C. I feel that the implementation is correct (if someone catches a corner case though, that would be great!), and I'm looking for feedback on best C practices.

For example: Is the spacing excessive? Are 1-line control flow statements okay? Is the constructor implementation idiomatic?

Header file:

#ifndef Interview_Prep_SLL_h
#define Interview_Prep_SLL_h

typedef struct SLLNode
{
    int val;
    struct SLLNode* next;
} SLLNode;

SLLNode* new_SLLNode(int val);
SLLNode* SLLappend(SLLNode* head, int val);
SLLNode* SLLinsert(SLLNode* head, int val, int ind);
SLLNode* SLLdelete(SLLNode* head, int val);
SLLNode* SLLreverse(SLLNode* head);
void SLLprint(SLLNode* head);

#endif

Source file:

// constructor
SLLNode* new_SLLNode(int val)
{
    SLLNode* head = malloc(sizeof(SLLNode));
    head->val = val;
    head->next = NULL;
    return head;
}

// add node to end
SLLNode* SLLappend(SLLNode* head, int val)
{
    if (head == NULL) return new_SLLNode(val);

    SLLNode* curr = head;

    while (curr->next != NULL) curr = curr->next;

    curr->next = new_SLLNode(val);

    return head;
}

// insert node at index (position) ind
SLLNode* SLLinsert(SLLNode* head, int val, int ind)
{
    if (head == NULL && ind > 0) return head;
    if (head == NULL && ind == 0) return new_SLLNode(val);

    SLLNode* curr = head;
    int count = 0;

    while (curr != NULL && count < ind - 1)
    {
        curr = curr->next;
        count++;
    }

    if (curr == NULL) return head;

    SLLNode* temp = curr->next;
    SLLNode* new = new_SLLNode(val);

    curr->next = new;
    new->next = temp;

    return head;
}

SLLNode* SLLdelete(SLLNode* head, int val)
{
    if (head == NULL) return head;

    if (head->val == val) return head->next;

    SLLNode* curr = head;

    while (curr->next != NULL && curr->next->val != val) curr = curr->next;

    if (curr->next == NULL) return head;

    SLLNode* temp = curr->next;
    curr->next = curr->next->next;
    free(temp);

    return head;
}

SLLNode* SLLreverse(SLLNode* head)
{
    if (head == NULL) return head;
    if (head->next == NULL) return head;

    SLLNode* curr = head;
    SLLNode* next = curr->next;
    SLLNode* nextnext = next->next;

    while (true)
    {
        if (curr == head) curr->next = NULL;

        next->next = curr;
        curr = next;
        next = nextnext;

        if (nextnext == NULL) return curr;

        nextnext = nextnext->next;
    }
}

// print values of nodes separated by spaces
void SLLprint(SLLNode* head)
{
    while (head != NULL)
    {
        printf("%d ", head->val);
        head = head->next;
    }
    printf("\n");
}


texts: Implementation of glass filter for images

texts:
This is exercise 3.1.39. from the book Computer Science An Interdisciplinary Approach by Sedgewick & Wayne:

Write a program that takes the name of an image file as a command-line
argument and applies a glass filter: set each pixel p to the color of
a random neighboring pixel (whose pixel coordinates both differ from
p’s coordinates by at most 5).

Here is my program:
import java.awt.Color;

public class test
{
    public static int[] chooseRandomNeighbor(int i, int j)
    {
        int[] chosenNeighbor = new int[2];
        double r = Math.random();
        if      (r < 1.0/8.0)
        {
            chosenNeighbor[0] = i-1;
            chosenNeighbor[1] = j-1;
        }
        else if (r < 2.0/8.0)
        {
            chosenNeighbor[0] = i-1;
            chosenNeighbor[1] = j;
        }
        else if (r < 3.0/8.0)
        {
            chosenNeighbor[0] = i-1;
            chosenNeighbor[1] = j+1;
        }
        else if (r < 4.0/8.0)
        {
            chosenNeighbor[0] = i;
            chosenNeighbor[1] = j+1;
        }
        else if (r < 5.0/8.0)
        {
            chosenNeighbor[0] = i+1;
            chosenNeighbor[1] = j+1;
        }
        else if (r < 6.0/8.0)
        {
            chosenNeighbor[0] = i+1;
            chosenNeighbor[1] = j;
        }
        else if (r < 7.0/8.0)
        {
            chosenNeighbor[0] = i+1;
            chosenNeighbor[1] = j-1;
        }
        else if (r < 8.0/8.0)
        {
            chosenNeighbor[0] = i;
            chosenNeighbor[1] = j-1;
        }
        return chosenNeighbor;
    }
    public static Picture filter(Picture picture)
    {
        int width = picture.width();
        int height = picture.height();
        Picture filteredPicture = new Picture(width,height);
        // the following four for-loops make the dead frame
        for (int row = 0; row < height; row++)
        {
            Color color = picture.get(0,row);
            filteredPicture.set(0,row,color);
        }
        for (int row = 0; row < height; row++)
        {
            Color color = picture.get(width-1,row);
            filteredPicture.set(width-1,row,color);
        }
        for (int col = 0; col < width; col++)
        {
            Color color = picture.get(col,0);
            filteredPicture.set(col,0,color);
        }
        for (int col = 0; col < width; col++)
        {
            Color color = picture.get(col,height-1);
            filteredPicture.set(col,height-1,color);
        }
        // the real filtering takes place here
        for (int col = 1; col < width-1; col++)
        {
            for (int row = 1; row < height-1; row++)
            {
                int[] chosenNeighbor = chooseRandomNeighbor(row,col);
                Color color = picture.get(chosenNeighbor[1],chosenNeighbor[0]);
                filteredPicture.set(col,row,color);
            }
        }
        return filteredPicture;
    }
    public static void main(String[] args)
    {
        Picture picture = new Picture(args[0]);
        Picture filteredPicture = filter(filter(filter(filter(picture))));
        filteredPicture.show();
    }
}

Picture is a simple API written by the authors of the book. I checked my program and it works. Here is one instance of it:
Input (picture of James McAvoy taken from Wikipedia who played in the movie Glass):

Output:

Is there any way that I can improve my program?
Thanks for your attention.


texts: Constructing file paths for each row of a datatable in C#

texts:
I need to iterate through a DataTable and columns in each rows and do some data manipulation.

Below given is an extract from my code, I use c#.net. I expect maximum rows in datatable will be less than 25K.

How I can improve the code for better performance?

DataTable metadaDataTable = GetMetaDataTable();
foreach (DataRow row in metadaDataTable.Rows)
{
    string newfileName = string.Empty;
    foreach (DataColumn col in metadaDataTable.Columns)
    {
       if (col.ColumnName != "path_variable")
       {
           newfileName += row[col].ToString() + "_";
       }
     }
       newPath = CleanFileName(newfileName);
   }


texts: Macro for transforming 2 way matrix into 1 way matrix

texts:
I have written some code that transforms 2 way matrices into 1 way matrices. Although it's comfortable to use, it's very sluggish and slow in execution. Does anyone have any idea how to improve the performance? I'm aware that the same transformation can be done with standard formulas, but solely for learning reasons I would like to stick with vba.

How to use it - select whole matrix and execute the Sub.

Sub TwoToOneMatrix()
Set Mrange = Selection
colsX = Mrange.Columns.Count
rowsX = Mrange.Rows.Count
Set objects = Range(Mrange.Columns(1).Rows(2), Mrange.Columns(1).Rows(rowsX))
Set titleX = Range(Mrange.Columns(1).Rows(1), Mrange.Columns(1).Rows(1))
Set Rng = Application.InputBox("Select a range", "Obtain Range Object", Type:=8)

'Column 1
Rng.Value = titleX.Value
def = 1
For j = 2 To objects.Rows.Count + 1
    curCountry = Mrange.Columns(1).Rows(j)
    For i = 1 To colsX - 1
        Rng.Offset(def, 0).Value = curCountry
        def = def + 1
    Next i
Next j

'Column 2
def = 1
Rng.Offset(0, 1).Value = "Date"
For j = 1 To objects.Rows.Count
    For i = 2 To colsX
        curDate = Mrange.Columns(i).Rows(1)
        Rng.Offset(def, 1).Value = curDate
        def = def + 1
    Next i
Next j

'Column 3
Rng.Offset(0, 2).Value = "Values"
def = 1
For r = 2 To rowsX
    For c = 2 To colsX
        Rng.Offset(def, 2).Value = Mrange.Columns(c).Rows(r)
        def = def + 1
    Next c
Next r

End Sub


texts: Excel's SUMIFS implemented using PANDAS, the Python Data Analysis Library

texts:
I've implemented Excel's SUMIFS function in Pandas using the following code. Is there a better — more Pythonic — implementation?

from pandas import Series, DataFrame
import pandas as pd
df = pd.read_csv('data.csv')
# pandas equivalent of Excel's SUMIFS function
df.groupby('PROJECT').sum().ix['A001']

One concern I have with this implementation is that I'm not explicitly specifying the column to be summed.

Data File

Here's an example CSV data file (data.csv), although I'm displaying | instead of commas to improve the visual appearance.

DATE     | EMPLOYEE     | PROJECT | HOURS
02/01/14 | Smith, John  | A001    | 4.0
02/01/14 | Smith, John  | B002    | 4.0
02/01/14 | Doe, Jane    | A001    | 3.0
02/01/14 | Doe, Jane    | C003    | 5.0
02/02/14 | Smith, John  | B002    | 2.0
02/02/14 | Smith, John  | C003    | 6.0
02/02/14 | Doe, Jane    | A001    | 8.0

Equivalent Excel SUMIFS Function

If I were to open data.csv in Excel and wanted to determine how many hours were worked on project A001, I would use the SUMIFS formula as follows:

=SUMIFS($D2:$D8, $C2:$C8, "A001")

Where the SUMIFS function syntax is:

=SUMIFS(sum_range, criteria_range1, criteria1, [criteria_range2,
        criteria2], …)


texts: Porting Haskell list comprehensions to Standard ML

texts:
Out of curiosity, I'm porting the Haskell exercises of Programming in Haskell by Graham Hutton to Standard ML. At the beginning everything seemed pretty similar until I reached list comprehensions:

A triple (x,y,z) of positive integers is called pythagorean if x2 + y2 = z2. Using a list comprehension, define a function pyths ∷ Int → [(Int,Int,Int)] that maps an integer n to all such triples with components in [1..n].

The Haskell version is pretty simple:
pyths ∷ Int → [(Int, Int, Int)]
pyths n = [(x, y, z) |
  x ← [1..n],
  y ← [1..n],
  z ← [1..n],
  x^2 + y^2 == z^2]

My Standard ML attempt, not so much:
(* No list comprehensions in SML so we need a helper function *)
val generateIntsUpTo = fn n =>
  let val rec helper = fn current => fn xs =>
    if current <= n
    (* then current :: helper (current + 1) xs *)
    then helper (current + 1) (current :: xs)
    else xs
  in
    rev (helper 1 [])
  end

(* Now we can find the pyths *)
val pyths = fn n =>
  let
    val numbers = generateIntsUpTo n
  in
    foldr (fn (x, acc) =>
      foldr (fn (y, acc) =>
        foldr (fn (z, acc) =>
          if x * x + y * y = z * z
          then (x, y, z) :: acc
          else acc
        ) acc numbers
      ) acc numbers
    ) [] numbers
  end

The first thing that I don't like is the call to rev at the end of generateIntsUpTo but I did it to accomplish a tail recursive function. Commented out is the line that lets me avoid the reversion of the list but then it's not tail recursive.
Besides that, there's the three nested foldr calls. I have been thinking of writing my own recursive functions, but I wanted to do this with the minimum amount of custom code.


texts: Paper-scissors-stick-stone game in Python

texts:
I would like to condense the code under def game(). I was told through a response to my question on Stack Overflow that I could use dictionary which goes something like this:

mychoice = {1:"paper", 2:"scissors", 3:"stick", r: "stone"}

import random
import time
from getpass import getuser

def my_input():

    #validinput = False
    #while not validinput:
    while True:
        try:
            mychoice = int(input("Your choice? (1. paper, 2. scissor, 3. stick or 4. stone) "))

        except ValueError:
            print('You did not enter a valid input. Enter again.')
            continue

        else:
            if mychoice == 1:
                return 'paper'
            elif mychoice == 2:
                return 'scissor'
            elif mychoice == 3:
                return 'stick'
            elif mychoice == 4:
                return 'stone'
            else:
                print('Invalid choice. Enter again.')

def randomized():
    choice = ['paper', 'scissor', 'stick', 'stone']
    return random.choice(choice)

def game():
    user_continue = True
    while user_continue:
        myinput = my_input()
        print('YOU: ' + str(myinput))
        randomval = randomized()
        time.sleep(1)
        print('COMPUTER: ' + randomval)
        won = 'Congratulations ' + getuser() + '. You won!'
        lost = 'Sorry ' + getuser() + '. You lost.'
        draw = "It's a draw."
        if myinput == 'paper' and randomval == 'scissor':
            time.sleep(1)
            print(lost)
        elif myinput == 'scissor' and randomval == 'paper':
            time.sleep(1)
            print(won)
        elif myinput == 'paper' and randomval == 'stick':
            time.sleep(1)
            print(lost)
        elif myinput == 'stick' and randomval == 'paper':
            time.sleep(1)
            print(won)
        elif myinput == 'paper' and randomval == 'stone':
            time.sleep(1)
            print(won)
        elif myinput == 'stone' and randomval == 'paper':
            time.sleep(1)
            print(lost)
        elif myinput == 'scissor' and randomval == 'stick':
            time.sleep(1)
            print(lost)
        elif myinput == 'stick' and randomval == 'scissor':
            time.sleep(1)
            print(won)
        elif myinput == 'scissor' and randomval == 'stone':
            time.sleep(1)
            print(lost)
        elif myinput == 'stick' and randomval == 'stone':
            time.sleep(1)
            print(lost)
        elif myinput == 'stone' and randomval == 'stick':
            time.sleep(1)
            print(won)
        else:
            print(draw)

        #continue looping until user says no
        user_yn = str.upper(input('Do you want to continue? Press Enter or type any value other than N '))
        if user_yn != 'N':
            print('continuing...')
            continue
        else:
            print('ending...')
            break
game()


texts:
I have a class used to return a single integer, but think there is room for improvement. The class should only return a single row every time and if no row is found it might be a good idea to throw an error from the class rather than from the calling code which will break when the integer comes back as zero.

The function below is called from a sync application that is returning a primary key in order to make an update.

    public static int GetCommuteDataID(CommuteData commuteData)
    {
        int commuteDataID = 0;
        using (var db = new CarbonContext())
        {
            var returnCommuteData = from e in db.CommuteDatas
                              where e.id == commuteData.id &&
                                    e.SurveyID == commuteData.SurveyID &&
                                    e.SurveyYear == commuteData.SurveyYear
                              select e.CommuteDataID;

            foreach (var item in returnCommuteData)
                commuteDataID = Convert.ToInt32(item.ToString());
        }
        return commuteDataID;
    }

Here is the code that calls GetCommuteDataID:

//Make sure the record does not exist
if (!CommuteDataRepository.CommuteDataExists(localCommuteData))
    db.CommuteDatas.Add(localCommuteData);
else
{
    localCommuteData.CommuteDataID = CommuteDataRepository.GetCommuteDataID(localCommuteData);
    db.Entry(localCommuteData).State = System.Data.EntityState.Modified;
}

Here is the new function:

public static int GetCommuteDataID(int? id, int surveyID, int surveyYear)
{
    using (var db = new CarbonContext())
    {
        return db.CommuteDatas
                 .Where(cd => cd.id == id &&
                              cd.SurveyID == surveyID &&
                              cd.SurveyYear == surveyYear)
                 .Select(cd => (int)cd.CommuteDataID)
                 .SingleOrDefault();
    }
}


texts: Django view to display weather measurements for selected location and dates

texts:
I'm a crop scientist and am currently building a web app that collects weather data then generates and displays modeled agriculturally relevant output. My question centers around an increasingly complex Django view and a desire to learn about a potentially superior implementation if one exists.

I have a Django view that allows the user to select a location from a Google maps layer when the page in initially rendered. Once a selection is made the view will check for location in URL request and render the template for an individual location. Once the individual location page is rendered the user has further options, like selecting a date range for desired data. The selection is made and submitted at which point the view will check for selectstartdate and selectenddate in the URL request and, if present, will render the single location view with data for the desired range. As I plan on adding several other interactive features, I'm beginning to realize that this view is going to get complicated quickly.

Is an intricate, conditionally driven Django view a reasonable implementation or is there a better way to do something like this?

Code

import time
from django.shortcuts import render
from locations.models import Location
from metload.models import Obsset
from datetime import datetime
from ETo_py.eto import EToEstimator

def single_location(**kwargs):

    """

    Process a bunch of weather parameters and return modeled output.
    This function is not a view, just a helper function for processing data.
    I have left out the lengthy inner workings of this function.

    """

    return context

def index(request):

    print('hello')

    # Single location without date range selected.
    if 'location' in request.GET and \
            'selectstartdate' not in request.GET and \
            'selectenddate' not in request.GET:

        location = request.GET['location']
        selectstartdate = None
        selectenddate = None

        params = {
            'location': location,
            'selectstartdate': selectstartdate,
            'selectenddate': selectenddate,
        }

        context = single_location(**params)

        return render(request, 'locations/location.html', context)

    # Single location with a date range selected.
    elif 'location' in request.GET and  \
            'selectstartdate' in request.GET and \
            'selectenddate' in request.GET:

        location = request.GET['location']
        selectstartdate = request.GET['selectstartdate']
        selectenddate = request.GET['selectenddate']

        print(selectstartdate, selectenddate)

        params = {
            'location': location,
            'selectstartdate': selectstartdate,
            'selectenddate': selectenddate,
        }

        context = single_location(**params)

        return render(request, 'locations/location.html', context)

    # Initial page.
    else:
        # Get locations from db.
        locations = Location.objects.all()

        # Prepare locations dictionary for google map layer js.
        locs_ls = []
        for loc in locations:
            lat = loc.latitude
            lon = loc.longitude
            name = loc.name
            site_id = loc.site_id

            locs_ls.append({'site_id': site_id, 'name': name, 'lat': lat, 'lon': lon})

        context = {
            'locs_ls': locs_ls,
        }

        return render(request, 'locations/index.html', context)


texts: Copy and bit-invert ints in an std::vector

texts:
I want a second copy of an std::vector in which all elements are inverted, e.g. 0x01 in the first vector should be 0xFE in the second.

Is this a good solution?

std::vector<uint8_t> first_list = {0x01, 0x10, 0x32, 0x1A };
std::vector<uint8_t> second_list = first_list;
for(std::vector<uint8_t>::iterator it = second_list.begin(); it != second_list.end(); ++it)
    *it = ~*it;

The for loop sticks out to me as unnecessarily verbose.


texts: Perform a Caesar Cipher Shift on a given string

texts:
I'd like feedback on my solution to the outlined programming challenge (medium level). I've tried it fast in any way I know how to, but, what might be a more efficient and/or pythonic solution?

  [Caesar Cipher]
  Using the Python language, have the function CaesarCipher(str,num) take the
  str parameter and perform a Caesar Cipher shift on it using the num parameter
  as the shifting number. A Caesar Cipher works by shifting each letter in the
  string N places down in the alphabet (in this case N will be num).
  Punctuation, spaces, and capitalization should remain intact. For example if
  the string is "Caesar Cipher" and num is 2 the output should be
  "Ecguct Ekrjgt".

import doctest
import logging
import timeit

from string import ascii_lowercase, ascii_uppercase

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

ALPHABET_LENGTH = 26
# Wrap alphabet letters
LOWERCASE = ascii_lowercase + ascii_lowercase
UPPERCASE = ascii_uppercase + ascii_uppercase

def encode_string(s: str, n: int) -> str:
    """Perform a Caesar Cipher shift on s leaving punctuation, spaces intact

    :param s: the str on which to perform a Cipher shift
    :type s: str
    :param n: the number of places in the alphabet to shift the letter.
              Positive for a forward shift, negative for a backwards shift
    :type n: int
    >>> encode_string(s="Caesar Cipher", n=2)
    'Ecguct Ekrjgt'
    >>> encode_string(s="Caesar Cipher", n=54)
    'Ecguct Ekrjgt'
    >>> encode_string(s="Aa Bb", n=0)
    'Aa Bb'
    >>> encode_string(s="Cc Dd", n=-28)
    'Aa Bb'
    """
    if n ==0:
        return s

    # Incase of very large n, find lowest equivalent offset
    n = n % ALPHABET_LENGTH

    # value of n used to index constants, equivalent to -n steps in backwards direction
    if n < 0:
        n = ALPHABET_LENGTH - n

    new_str = ""

    for c in s:
        if c in LOWERCASE:
            i = LOWERCASE.index(c)
            encoded_char = LOWERCASE[i + n]
            new_str = new_str + encoded_char
        elif c in UPPERCASE:
            i = UPPERCASE.index(c)
            encoded_char = UPPERCASE[i + n]
            new_str = new_str + encoded_char
        else:
            new_str = new_str + c

    return new_str

if __name__ == "__main__":
    doctest.testmod()
    print(timeit.timeit(
        "encode_string(s='Caesar Cipher', n=2)",
        setup="from __main__ import encode_string, ALPHABET_LENGTH, LOWERCASE, UPPERCASE",
        number=100000)
    )


texts: PHP CouponGenerator class - preventing concurrent file access

texts:
I wrote this php class to generate one time random and unique coupon codes.

The Class generates coupon codes, storing it in a json file.

My question is regarding the write and get methods of this class.

In the write method, I use file_put_contents with LOCK_EX flag. I assume if the function fails to get a lock, it returns false.

When the Get method is called,

it pop a coupon code from the data array property
call the write method to update the file
if write returns true, it returns the coupon to the user and updates the database
if write returns false, it sleeps 10ms and makes a recursive call to itself.

Is this a correct way to prevent concurrent file access issues? For example, 2 users getting the same coupon code.

Any other suggestions or improvements would be much appreciated.

<?php
namespace Cart;
/**
* Generates random and unique coupon codes
*/
class CouponGenerator
{
  private $query;
  private $data;
  private $file;
  private $prefix;
  private $noOfCodesToGenerate = 10000;

  public function __construct(\QueryBuilder $query, $prefix = '')
  {
      switch ($prefix) {
          case 'FB':
              $this->file = 'fb.json';
              break;

          default:
              $this->file = 'coupons.json';
              break;
      }

      $this->query = $query;

      // if file doesn't exist or empty
      if (! file_exists($this->file) || empty($this->data = $this->read())) {
          // generate code and write to file
          $this->generateCodes();
          $this->write();
      }
  }

  // returns the coupon code
  public function get()
  {
      // Pop the last coupon
      $code = array_pop($this->data);

      // try updating the file
      if ($this->write()) {
          // if successful update the database and return code
          $this->updateTable($code);
          $data = [];
          return $code;
      }
      // else sleep 10ms and attempt again
      usleep(100000);
      $this->get();
  }

  private function updateTable($code)
  {
      // update database and return result
  }

  private function read()
  {
      return json_decode(file_get_contents($this->file));
  }

  private function write()
  {
      return false !== file_put_contents($this->file, json_encode($this->data), LOCK_EX);
  }

  private function generateCodes() {
      $chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
      $charsLen = strlen($chars) - 1;
      $code = $this->prefix;
      $maxLen = 8 - strlen($this->prefix);
      $result = [];

      while (count($result) < $this->noOfCodesToGenerate) {
          for ($i=0; $i < $maxLen; $i++) {
              $code .= $chars[mt_rand(0, $charsLen)];
          }

          if (! in_array($code, $result)) {
              $result[] = $code;
          }
          $code = $this->prefix;
      }
      $this->data = $result;
  }
}

EDIT
I took @freezePhoenix advice and rewrote the below function. Its not as random and unique as a UUID but i ran a test for 100000 codes and got just 3 collisions.

To eliminate duplicate codes, i implemented a try catch block to catch the PDOException and regenerate the code.

private function generateCode() {
      $randomCode = $this->prefix . bin2hex(openssl_random_pseudo_bytes(16));
      $ambigousChars = [0, 'o', 1, 'i', 'l'];
      $cleanString = str_replace($ambigousChars, '', $randomCode);
      return strtoupper(substr($cleanString, 0, 8));
}

public function get()
{
    try {
        $code = $this->generateCode();
        $this->updateTable($code);
        return $code;
    } catch (PDOException $e) {
        $this->get();
    }
}


texts: Functions to convert camelCase strings to snake_case

texts:
I have written two functions, one actual and one helper, to convert camelCase strings into snake_case strings (I call it joint-lower case). Any ideas for improvement are welcome.

def _cc2jl(string):
    """Camel case to joint-lower helper."""

    for index, current in enumerate(string):

        if is_upper(current):
            if index > 0:
                previous = string[index-1]

                if is_upper(previous):
                    try:
                        next = string[index+1]
                    except IndexError:
                        yield current.lower()
                        continue
                    else:
                        if is_upper(next):
                            yield current.lower()
                            continue

                yield '_' + current.lower()
                continue

            yield current.lower()
            continue

        yield current

def cc2jl(string):
    """Converts a camel case string to joint-lower."""

    return ''.join(_cc2jl(string))

Expected behaviour:

>>> cc2jl('m')
'm'
>>> cc2jl('AA')
'aa'
>>> cc2jl('MySQLDatabase')
'my_sql_database'
>>> cc2jl('GarbageCollection')
'garbage_collection'
>>> cc2jl('AAAAAAAAAAAAAAAAA')
'aaaaaaaaaaaaaaaaa'


texts: Insert all SQL Server table rows in a Google BigQuery Table

texts:
I want to know the best way (best performance and reaction time) to insert all rows of an SQL table into a Google BigQuery table. Actually, I am looping over a query result and insert the rows one by one into the BigQuery table.

The query GetShippingList returns all TShipping_ID of the table Shipping which has only one column:TShipping_ID. So, the SQL table has exactly the same schema of the BigQuery table.

<cfloop query="GetShippingList">
    <cfset DateTime=mid("#now()#",6,19)>
    <cfset TShipping_ID=TShipping_ID>

    <!--- instancier un objet de type (class) TableRow --->
    <cfobject action="create" type="java" class="com.google.api.services.bigquery.model.TableRow" name="row">
    <cfset row.set("DateTime",DateTime)>
    <cfset row.set("TShipping_ID", TShipping_ID)>

    <!--- instancier un objet de type (class) TableDataInsertAllRequest.Rows --->
    <cfobject action="create" type="java" class="com.google.api.services.bigquery.model.TableDataInsertAllRequest$Rows" name="rows">
    <cfdump var="#rows#">

    <!--- instancier un objet de type (class) TableDataInsertAllRequest --->
    <cfobject action="create" type="java" class="java.lang.System" name="sys">
    <cfdump var="#sys#">

    <cfset rows.setInsertId(sys.currentTimeMillis())>
    <cfset rows.setJson(row)>

    <!--- instancier un objet de type (class) ArrayList --->
    <cfobject action="create" type="java" class="java.util.ArrayList" name="rowList">
    <cfdump var="#rowList#">
    <cfset rowList.add(rows)>

    <!--- instancier un objet de type (class) TableDataInsertAllRequest --->
    <cfobject action="create" type="java" class="com.google.api.services.bigquery.model.TableDataInsertAllRequest" name="rqst">
    <cfdump var="#rqst#">
    <cfset content=rqst.setRows(rowList)>

    <!--- instancier un objet de type (class) TableDataInsertAllResponse --->
    <cfobject action="create" type="java" class="com.google.api.services.bigquery.model.TableDataInsertAllResponse" name="rsp">
    <cfdump var="#rsp#">
    <cfset response = bq.tabledata().insertAll("vigicolis", "wi_vigicolis", "TShipping", content).execute()>
</cfloop>


texts: Data lookup table with Redis and API call

texts:
This class' function is to change a given array's data into an ID.
The list is set by a configuration file. I have DI the outer array and objects into the class so that if there are any dependencies - it gets injected.

The protected $_api; is an external API class that contains some API call to an external address that will do a look up given type and value. It returns an ID and if there is a rare chance that it doesn't - it will return null.

redis is a redis object that communicates with redis (it's being instantiated with a known lib so the set/get that you will see in the code are known).

table is the list of tables being looked up.

2 steps:

Go find in redis and see if the hash value is there - if it is we get the ID and we move on.
If it is not there - go call the API and fetch it, and insert it into redis

My code used to pass the function getID and have that function do the look up / API call, however since I need several values - I decided to do mGET to get multiple values and only make one trip to redis.

The code works, however, I want to transform it to something better utilizing SOLID, and that's where the mGET got me and I'm stuck. I figure that after writing this code I can see it, but I can't which is what I need help on.

class LookUp
{
    protected $_api;
    protected $_redis;
    protected $_tables_switch_id;

    public function __construct(\Redis $redis, Api\Track $api, $tables = array())
    {
        $this->_api = $api;
        $this->_redis = $redis;
        $this->_tables_switch_id = $tables;
    }
    public function changeDataIntoId($data)
    {
        $hash_value = array();
        $index_array = array();
        foreach ($this->_tables_switch_id as $index => $table) {
            if (isset($data[$index])) {
                $hash_value[$index] = md5(serialize(array($index => strtolower($data[$index]))));
                $index_array[] = $index;
            }
        }

        $ids = $this->_redis->mGet($hash_value);
        $i = 0;
        foreach ($ids as $id) {
            $breakdown = $index_array[$i];
            if ($id !== false) {
                $data[$breakdown] = $id;
            } else {

                $api_id = $this->_api->getBreakdownID($breakdown, $data[$breakdown]);
                if ($api_id == null) {
                    $api_id = 0;
                }
                $this->_redis->set($hash_value[$breakdown], $api_id);
                $data[$breakdown] = $api_id;
            }
            $i++;
        }
        return $data;
    }
}

One observation that might confuse you: why do I have two arrays.

One is the hash data, and the other one is an index.  The entering data ($data) could be composed of numerous indexes. I'm only converting those who are from $table. This is the sole reason why I must retain the list and why I am also checking for isset.


texts: Multiple dispatch with variant and multi visitation in C++

texts:
The following program is a demonstration of variant and double visitation used to obtain double dispatch between the elements of two inhomogeneous containers. Most of the work is done at compile time. What do you think about it compared to the dynamic version in explained in Wikipedia?

boost::variant does not employ variadic templates and has a hand coded limit to 20 different types which can be easily extended to 50 but not beyond. Is it a crazy idea to use this pattern (with an appropriate variant implementation) with more than 50 types, say 100 spaceships and 10 different asteroids or so?

#include <iostream>
#include <vector>
#include "boost/variant.hpp"
using std::cout;
using std::endl;

////////// some basic objects

class SpaceShip {};
class ApolloSpacecraft : public SpaceShip {};

class Asteroid {
public:
  void CollideWith(SpaceShip&)
       { cout << "Asteroid hit a SpaceShip" << endl; }
  void CollideWith(ApolloSpacecraft&)
       { cout << "Asteroid hit an ApolloSpacecraft" << endl; }
};

class ExplodingAsteroid : public Asteroid {
public:
  void CollideWith(SpaceShip&)
       { cout << "ExplodingAsteroid hit a SpaceShip" << endl; }
  void CollideWith(ApolloSpacecraft&)
       { cout << "ExplodingAsteroid hit an ApolloSpacecraft" << endl; }
};
////////// visitor

struct CollideVisitor : public boost::static_visitor<void> {
  template <typename A, typename S>
  void operator()( A & a, S & s) const { a.CollideWith(s);}
};

////////// demo

int main() {
  std::vector<boost::variant<Asteroid,ExplodingAsteroid>> asteroids;
  asteroids.emplace_back(Asteroid());
  asteroids.emplace_back(ExplodingAsteroid());

  std::vector<boost::variant<SpaceShip,ApolloSpacecraft>> spaceships;
  spaceships.emplace_back(SpaceShip());
  spaceships.emplace_back(ApolloSpacecraft());

  for (auto & a : asteroids) {
    for (auto & s : spaceships) {
      boost::apply_visitor(CollideVisitor(), a, s);
    }
  }

  return 0;
}


texts: Multithreading algorithm for high performance parallel computing and Producer-Consumer queue using POSIX threads

texts:
The following is the producer-consumer algorithm for a piece of software that I'm upgrading to take advantage of multi-core processing. The intended platform is some flavor of Linux running on an EC2 HPC cc4x.large cluster, which feature 2 x Intel Xeon X5570, quad-core “Nehalem” architecture processors (2*4=8 cores). The software runs a genetic algorithm for optimizing artificial neural networks.

My dominating concern is performance. RAM and HD capacity are not an issue, but CPU time and anything else that delays processing is. Right now I've made a few (noted below), hopefully trivial, compromises to make the program portable to Mac OS X, which is on my home computer that I use for coding/debugging. I note a few other minor issues in the comments, most notably an uncertainty about thread-safeness in the consumer function. This is my first time working with threads. Advice/criticism of any kind is much appreciated.

#include <iostream>
#include <pthread.h>
#include <semaphore.h>
#include <vector>

#define NUM_THREADS 3     //will be >= to # of cores
#define N           30

//globals
sem_t* producer_gate;
sem_t* consumer_gate;
pthread_mutex_t access_queued =PTHREAD_MUTEX_INITIALIZER;
int queued;
int completed;

//a dummy class for testing thread-safeness
class the_class
{
public:

    void find_num()
    {
        //make sure completion is non-instant and variable
        double num = rand();
        for(double q=0; 1; q++)
            if(num == q)
                return;
    }
};

//the consumer function for handling the parallelizable code
void* Consumer(void* argument)
{

    std::vector <the_class>* p_v_work = (std::vector <the_class>*) argument ;

    while(1)
    {
        sem_wait(consumer_gate);
        pthread_mutex_lock (&access_queued);
        int index = queued;
        queued--;
        pthread_mutex_unlock (&access_queued);
        (*p_v_work)[index-1].find_num();
        completed++;                            //<-- thread safe??
        std::cout << "\n" << index;
        if(completed == N)
            sem_post(producer_gate);
    }

    return NULL;
}

int main ()
{
    srand(5);

    //holds data to be processed and the methods for processing it
    std::vector <the_class> work(N);
    std::vector <the_class>* p_work = &work;

    sem_unlink("producer_gate");
    sem_unlink("consumer_gate");
    //can't use `sem_init();` under Mac OS X so use sem_open instead
    producer_gate = sem_open("producer_gate", O_CREAT, 0700, 0);
    consumer_gate = sem_open("consumer_gate", O_CREAT, 0700, N);
    completed   = 0;
    queued      = N;

    pthread_attr_t attr;
    pthread_attr_init (&attr);
    pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); //no joining necessary

    pthread_t threads[NUM_THREADS];
    for(int q=0; q<NUM_THREADS; q++)
        //on OS X, a pthread_t must be provided or a bus error occurs. relevant to performance?
        pthread_create(&threads[q], &attr, Consumer, (void*) p_work );

    for( int q=0; q < 4; q++)
    {
        sem_wait(producer_gate);
        std::cout << "\nDONE\n";

        ////////////////////////////////////////////////////////
        //  Summate work done and layout work for next iteration
        ////////////////////////////////////////////////////////

        completed   = 0;
        queued      = N;
        for(int q=0;q<N;q++)
            sem_post(consumer_gate);  //some way to just set this instead of incrementing?
    }

    sem_wait(producer_gate);
    std::cout << "\n\nCompleted (:   !!\n";

}


texts:
I found it useful to execute multiple reads in parallel using a set of my extension methods for TaskFactory, which could be used as:

static async Task Main(string[] args)
{
    var (r1, r2, r3, r4) = await Task.Factory.StartNew(
        Read1Async, Read2Async, Read3Async, Read4Async);

    async Task<int> Read1Async() => /* cut as not relevant */;
    async Task<string> Read2Async() => /* cut as not relevant */;
    async Task<long> Read3Async() => /* cut as not relevant */;
    async Task<double[]> Read4Async() => /* cut as not relevant */;
}

The actual helper code is:

public static class TaskFactoryExtensions
{
    public async static Task<(T1, T2)> StartNew<T1, T2>(
        this TaskFactory taskFactory,
        Func<Task<T1>> action1,
        Func<Task<T2>> action2)
    {
        var task1 = Task.Run(action1);
        var task2 = Task.Run(action2);
        return (await task1, await task2);
    }

    public async static Task<(T1, T2, T3)> StartNew<T1, T2, T3>(
        this TaskFactory taskFactory,
        Func<Task<T1>> action1,
        Func<Task<T2>> action2,
        Func<Task<T3>> action3)
    {
        var task1 = Task.Run(action1);
        var task2 = Task.Run(action2);
        var task3 = Task.Run(action3);
        return (await task1, await task2, await task3);
    }

    public async static Task<(T1, T2, T3, T4)> StartNew<T1, T2, T3, T4>(
        this TaskFactory taskFactory,
        Func<Task<T1>> action1,
        Func<Task<T2>> action2,
        Func<Task<T3>> action3,
        Func<Task<T4>> action4)
    {
        var task1 = Task.Run(action1);
        var task2 = Task.Run(action2);
        var task3 = Task.Run(action3);
        var task4 = Task.Run(action4);
        return (await task1, await task2, await task3, await task4);
    }
}

It just saves on typing...


texts: Check if element is a member of all sets in Scheme

texts:
Here is a function that checks whether an alement a is a member of every set in list l-set.

I don't like the fact that it returns #t for an empty list of sets. What would be a clean fix to this problem?

(define memberall
  (lambda (a l-set)
    (cond
      ((null? l-set) #t)
      ((not (member a (car l-set))) #f)
      (else (memberall a (cdr l-set))))))


texts:
This is my Rock Paper Scissors code for Python 3 with score count. I want to make it more readable and concise if possible.

import random

def player_choice(user_choice):  # Register and shows the players choice
    if user_choice == 1:
        print("The player chose rock")
    elif user_choice == 2:
        print("The player chose paper")
    else:
        print("The player chose scissors")

def computer_choice(cpu_choice):  # Register and shows the cpus choice
    if cpu_choice == 1:
        print("The computer chose rock")
    elif cpu_choice == 2:
        print("The computer chose paper")
    else:
        print("The computer chose scissors")

def result(user_choice, cpu_choice, player_score, cpu_score):  # Define the game result
    if user_choice == cpu_choice:
        return [player_score + 0.5, cpu_score + 0.5]
    elif user_choice == 1 and cpu_choice == 3:
        return [player_score + 1, cpu_score]
    elif user_choice == 2 and cpu_choice == 1:
        return [player_score + 1, cpu_score]
    elif user_choice == 3 and cpu_choice == 2:
        return [player_score + 1, cpu_score]
    else:
        return [player_score, cpu_score + 1]

def print_score(p_score, c_score):  # Identifies the result and print the total score
    print("Score:""\nPlayer:", p_score, "\nComputer:", c_score)

def validation_input():  # Validates the input
    while True:
        try:
            user_input = int(input("Put your choice:"))
            if user_input not in range(1, 4):
                print("We only accept commands between 1 and 3, according to the table, type again")
                continue
            if type(user_input) == int:
                break
        except ValueError:
            print("We only accept exact numbers")
            continue
    return user_input

print('''1 - Rock
2 - Paper
3 - Scissors''')  # Printing the instructions
human_score = 0
computer_score = 0
while True:  # The condition is not important since the loop will stop on line 68 if the user wishes so
    user = validation_input()
    player_choice(user)
    ai = random.randint(1, 3)
    computer_choice(ai)
    human_score, computer_score = result(user, ai, human_score, computer_score)  # Accumulate the score
    print_score(human_score, computer_score)
    command = int(input("Type 0 to stop the program, type any another number to keep playing"))
    if command == 0:
        break


texts: Using Microsoft's Emotion API to detect emotions

texts:
I have not programmed a lot in JavaScript and I am hoping to improve myself. Thus, I am looking for a style review as well as to look for a better way to approach this problem (the current code works perfectly and as expected). What I am doing is requesting the Microsoft Emotion API to take an image and spit back the average happiness of all detected faces in the picture.

The code below is a small part of my project. Normally in other languages, I like to separate my code into as many small chunks as possible, but I am not so sure if I am doing it correctly here.

Would a class be suitable? Should I just write a function that takes two keys instead?
Should I have this all other the places?

Beside these questions above, I am sure that my code has many other problems. Also, I am building a Node server, so I could not use export default

const RapidAPI = new require('rapidapi-connect');

class ImageFaceEmotion {

  constructor(rapidKey, microsoftKey) {
    this._rapidKey = rapidKey;
    this._microsoftKey = microsoftKey;
    this.rapid = new RapidAPI('MLHPrimeMeme', rapidKey);
  }

  /* Returns a Promise with the overall happiness of the faces in the provided image.
  *
  * - url: link the the image
  */
  getEmotionFromImage(url) {
    return new Promise( (resolve, reject) => {

      this.rapid
        .call('MicrosoftEmotionAPI', 'getEmotionRecognition', {
          'image': url,
          'subscriptionKey': this._microsoftKey
        })
        .on('success', (payload) => {

          if (payload.length === 0) {
            reject('Cannot find any face within the picture');
          }

          let average = (payload.map(face => face.scores.happiness).reduce( (acc, cur) => acc + cur, 0)) / payload.length;
          resolve(average);
        })
        .on('error', (payload) => {
          reject(payload);
        });

    });
  }

}

var ifm = new ImageFaceEmotion('key1', 'key2');

ifm.getEmotionFromImage('link')
  .then((average) => {
    console.log(average);
  })
  .catch((error) => {
    console.log(error);
  });

exports.ImageFaceEmotion = ImageFaceEmotion;


texts:
I have set of values represented by a class. The class should be initialised with  a generator to limit memory use.

The class itself is the iterable, having __iter__, and the iterator, having __next__ (see below).

Since I want to be able to iterate over the set of values multiple times, I cache the generated values in a list. In __iter__, I check if I already have iterated over all values, and then return iter of the cached values, or self to continue with next, respectively.

Here's the code stripped down to the relevant parts:

class SetOfValues(object):
    def __init__(self, values):
        self._values = values
        self._values_generated = []
        self._done = False

    def __iter__(self):
        if self._done:
            return iter(self._values_generated)
        else:
            return self

    def __next__(self):
        try:
            value = next(self._values)
            self._values_generated.append(value)
            return value
        except StopIteration:
            self._done = True
            raise StopIteration("the end")

Then call it with:

x = SetOfValues((value for value in [1,2,3]))

for i in x:
    print(i)

Is this generally a good way to do it when you may need generated values more than once?
Might it be better if __iter__ yields the value?
In terms of usage of the class: might it be better to always rewind and let iter start with pos 0? Currently, iterating through the values, stopping early, then iterating again, would obviously just continue at the last index when not all values have been generated yet.

Thanks.


texts:
I have to read some data from xml file and then need to store the converted json in the database. The class DtoProcedureXml mapped with xml attributes.

public class ProcedureManager
{
    #region Declaration
    private DtoProcedureXml procedure;
    private ProcedureDataManager procedureDataManager;
    #endregion

    public ProcedureManager()
    {
        this.procedureDataManager = new ProcedureDataManager();
    }

    public bool SaveProcedureData(long procedureID)
    {
        bool isSuccess = false;
        try
        {
            SetProcedureXmlData();
            isSuccess = SaveProcedureSummary(procedureID);
        }
        catch (Exception ex)
        {
            CommonLogger.LogException("Failed to save procedure data");
        }
        return isSuccess;
    }

    private bool SaveProcedureSummary(long procedureID)
    {
        //saving the data to db
        return procedureDataManager.Save(new DtoProcedure { ProcedureID = procedureID, JsonData = Newtonsoft.Json.JsonConvert.SerializeObject(this.procedure), CreatedOn = DateTime.Now });
    }

    private void SetProcedureXmlData()
    {
        this.procedure = new DtoProcedureXml();
        XmlSerializer serializer = new XmlSerializer(typeof(DtoProcedureXml));
        using (XmlTextReader reader = new XmlTextReader(FileShareUrlBuilder.GetPath(Constants.Structures.PathType.XMLFILEPATH)))
        {
            object obj = serializer.Deserialize(reader);
            this.procedure = (DtoProcedureXml)obj;
            reader.Close();
        }
    }
}

I have some concerns with the function SetSReportXmlData() When I was debugging the XmlSerializer code takes few milliseconds, is it right to move that code in the using section?
Also please let me know if there is any efficient solutions than this logic.


texts:
I currently have this monstrosity:

next_period = (((current_month + tax_period_months)-1) % 12) + 1

I want to get the month that is tax_period_months after the current month.

E.g. if tax_period_months=3, and current_month=09 then next_period=12

This works, but has that horrible -1 +1 to stop month 12 becoming 0

Any alternatives?


texts:
I seem to have a memory leak on this method but can't figure it out. This method is called every 10sec by a timer which acts like a refresh.

    //Variables

    List<UserControls.userControlAlert> theDataPages = new List<UserControls.userControlAlert>();

    private OleDbConnection mycon;
    private OleDbCommand oleDbCmd;
    private OleDbDataReader dataReader;

    //This method looks for the Alerts in the Data Base and displays the info in a User Control
    private void ShowData()
    {
        try
        {

            mycon.Open();

            string queriesLabels = "SELECT *FROM MP where State = @stateAccepted OR State = @stateRejected";

            oleDbCmd = new OleDbCommand(queriesLabels, mycon);
            oleDbCmd.Parameters.Add("@stateAccepted", OleDbType.VarChar, 10).Value = "Accepted";
            oleDbCmd.Parameters.Add("@stateRejected", OleDbType.VarChar, 10).Value = "Rejected";

            dataReader = oleDbCmd.ExecuteReader();
                while (dataReader.Read())
                {

                    TabPage page = new TabPage("MP "
                                 + (DTL.TabPages.Count + 1).ToString());
                    UserControls.userControlAlert aForm = new UserControls.userControlAlert();
                    aForm.Parent = page;
                    aForm.showReaderRow(dataReader);
                    DTL.TabPages.Add(page);
                    theDataPages.Add(aForm);
                }
        }
        catch (Exception error)
        {
            MessageBox.Show(error.ToString());
        }
        finally
        {
            dataReader.Close();
            mycon.Close();
        }
    }

Timer (10sec):

 private void timer1_Tick(object sender, EventArgs e)
    {
        if (DTL.TabCount > 0)
        {
            theDataPages.Clear();
            DTL.TabPages.Clear();
            ShowData();
        }
        else
        {
            theDataPages.Clear();
            ShowData();
        }
    }

If I leave the application open, every 10sec I see an increment on the memory in that process, after some time windows closes the application.


texts:
this is a basic PHP auth class I've put together for use with simple sites I do from time to time which don't warrant using a framework. I'm wondering if i'm separating out responsibilities all that well, even at this early stage! For example, I'm not sure where I should be filtering user input with mysql_real_escape_string, and I don't know if I should be passing the DB into the constructor, but then I need it to be able to run my auth check.

Anyway, any pointers would be great thanks!

class Auth {

    protected $db;

    /**
     * Auth::__construct()
     *
     * @return
     */
    public function __construct($db = null)
    {
        $this->db = $db;
    }

    /**
     * Auth::login()
     *
     * @param mixed $username
     * @param mixed $password
     * @return
     */
    public function login($username, $password)
    {
        $query = $this->db->query("SELECT username, password
                                    FROM users
                                    WHERE username = '".mysql_real_escape_string($username)."'
                                    AND password = '".self::crypt_pass(mysql_real_escape_string($password))."'
                                  ");

        $result = $this->db->fetch_array($query);

        if (count($result) == 0)
        {
            return FALSE;
        }
        else
        {
            //set session variable
            Session::instance()->set('logged_in', TRUE);
            return TRUE;
        }
    }

    /**
     * Auth::logout()
     *
     * @return
     */
    public function logout()
    {
        //set session variable
        return Session::instance()->delete('logged_in');
    }

    private function crypt_pass($pass)
    {
        if (CRYPT_BLOWFISH == 1)
        {
            $salt = SHA1('acssalty457');
            $blowfish_salt = "\$2a\$07\$".substr($salt, 0, CRYPT_SALT_LENGTH);
            return crypt($pass, $blowfish_salt);
        }
    }
}


texts: .htaccess using public folder and SEO friendly URL

texts:
.htaccess is not my strong suit. Is there any there can optimize this code?

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(public)
RewriteRule (.*) /public/$1

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

ErrorDocument 500 "Sorry, our script crashed. Oh dear"
ErrorDocument 500 /cgi-bin/crash-recover
ErrorDocument 500 http://error.example.com/server_error.html
ErrorDocument 404 /errors/not_found.html
ErrorDocument 401 /subscription/how_to_subscribe.html

don't know if it necessary but here are an example of the folder system

app
public

index.php
contact.php
forum

last_news.php
users

user.php

ask.php

.htaccess


texts:
I'm trying to implement properly search functionality for database table. I tried this approach:

Controller:

    @GetMapping
    public Page<TransactionDTO> find(TransactionFilterDTO filter, Pageable page) {
        return searchRepository
                .findTransactionsByFilter(mapper.toFilter(filter), page)
                .map(mapper::toDTO);
    }

Filer DTO:

public class TransactionFilterDTO {

    private String name;

    private Integer id;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private LocalDateTime from;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private LocalDateTime to;
    ... // getters and setter
}

Search implementation:

@Repository
public class TransactionSearchRepositoryImpl implements TransactionSearchRepository {

    @Autowired
    private TransactionRepository transactionRepository;

    @Autowired
    private TransactionSpecification specification;

    @Override
    public Page<Transaction> findTransactionsByFilter(TransactionFilter filter, @Nullable Pageable page) {

        List<Transaction> transactions = transactionRepository
                .findAll(specification.getFilter(filter));

        int totalCount = transactions.size();

        if(page != null) {
           transactions = transactions
                   .stream()
                   .skip(page.getOffset())
                   .limit(page.getPageSize())
                   .collect(Collectors.toList());
        }

        return new PageImpl<>(transactions, page, totalCount);
    }
}

Repository:

public interface TransactionSearchRepository {

    Page<Transaction> findTransactionsByFilter(TransactionFilter filter, Pageable page);
}

Is there some better way to implement a search functionality? This solution is very ugly in my view.


texts:
I'm recently working on a project that involves animation in the background. It works well on desktop but performance drops drastically on mobile. I'm using Paper.js to import a svg and animate it. This is the demo.

paper.project.importSVG(svg, function(item) {
          var moveSpeed = 70;
          var movementRadius = 15;
          var boundingRec = item.bounds;
          var lines = [];
          var circles = [];
          /*
            Arrange lines and circles into different array
          */
          $.each(item.getItems({
            recursive: true,
          }), function(index, item) {
            if (item instanceof paper.Shape && item.type == 'circle') {
              item.data.connectedSegments = [];
              item.data.originalPosition = item.position;
              circles.push(item);
            }
            if (item instanceof paper.Path) {
              lines.push(item);
            }
          });

          /*
            Loop through all paths
            Checks if any segment points is within circles
            Anchors the point to the circle if within
          */
          $.each(lines, function(pathIndex, path) {
            $.each(path.segments, function(segmentIndex, segment) {
              $.each(circles, function(circleIndex, circle) {
                if (circle.contains(segment.point)) {
                  circle.data.connectedSegments.push( segment.point );
                  return false;
                }
              });
            });
          });

          /*
            Animate the circles
          */
          $.each(circles, function(circleIndex, circle) {
            var originalPosition = circle.data.originalPosition;
            var radius = circle.radius * movementRadius;

            var destination = originalPosition.add( paper.Point.random().multiply(radius) );

            circle.onFrame = function() {
              while (!destination.isInside(boundingRec)) {
                destination = originalPosition.add( paper.Point.random().multiply(radius) );
              }

              var vector = destination.subtract(circle.position);
              circle.position = circle.position.add(vector.divide(moveSpeed));
              // move connected segments based on circle

              for (var i = 0; i < circle.data.connectedSegments.length; i++) {
                circle.data.connectedSegments[i].set({
                    x: circle.position.x,
                    y: circle.position.y,
                });
              }

              if (vector.length < 5) {
                destination = originalPosition.add( paper.Point.random().multiply(radius) );
              }
            }
          });
        });

A single SVG animation alone is causing the CPU usage to go up to 90% on mid-tier mobile devices and it go to the point that the interface becomes unusable. Any advice or help is much appreciated.


texts:
This is a POC for a clock using moment.js, eventually I plan to recreate this in Raect.js. Any opimisations or a better way to do it?

<!DOCTYPE html>

    <title>Html clock</title>

    <link rel="stylesheet" type="text/css" href="stylesheet.css">

<select id='timezone_list'></select>


texts:
This is what my viewObject looks like:

{
    description: "GOOG AAPL TSLA"
    id: 42
    ticker_1: "GOOG"
    ticker_2: "AAPL"
    ticker_3: "TSLA"
    timespan: "1mo"
}

Here is the function use to check the object. In a very dirty fashion, it creates a string to be used to replace the URL with:

function createUrl(viewObj) {
    var urlString  = '',
        ticker1    = '',
        ticker2    = '',
        ticker3    = '',
        time       = '';

    if (viewObj.ticker_1) {
        ticker1 = '&ticker_1='+viewObj.ticker_1;
        ulrString = ulrString+ticker1;
    }
    if (viewObj.ticker_2) {
        ticker2 = '&ticker_2='+viewObj.ticker_2;
        ulrString = ulrString+ticker2;
    }
    if (viewObj.ticker_3) {
        ticker3 = '&ticker_3='+viewObj.ticker_3;
        ulrString = ulrString+ticker3;
    }

    if (viewObj.timespan) {
        time = '&timespan='+viewObj.timespan;
        ulrString = ulrString+time;
    }

    console.log('/dashboard'+ulrString);
    return '/dashboard'+ulrString;
}

I have multiple if/else statements and code that can be repeated. How would you accomplish this?


texts: Calling a FormatCurrency function on multiple textboses

texts:
I have a function called FormatCurrency() which I would like to call on each of the following elements:

 TextBoxA
 TextBoxB
 TextBoxC
 ...
 TextBoxN

My current function performs the following operation

 function formatALL()
 {
      //get the value into var v. call FormatCurrency(v) and store it back
      var v = document.getElementById("TextBoxA_TextBox").value;
  document.getElementById("TextBoxA_TextBox").value = FormatCurrency(v);

      v = document.getElementById("TextBoxB_TextBox").value;
  document.getElementById("TextBoxB_TextBox").value = FormatCurrency(v);

  v = document.getElementById("TextBoxC_TextBox").value;
  document.getElementById("TextBoxC_TextBox").value = FormatCurrency(v);

      //etc... all the way til my last TextBox
 }

Is it possible to optimize this code?  In other words, does anyone know any slick loops/operations/etc. to perform this same task?


texts: Error handling for a simple 'fetch some data, then save the data' Node function

texts:
This is a fairly simple bit of code:

async function fetchData() {
    console.log("fetching data");
    let json;
    try {
        const data = await fetch(endPoint);
        json = await data.json();
    }
    catch (error) {
        console.error(error);
        handleError(error);
    }

    try {
        await saveData(json);
    } catch (error) {
        console.error(error);
        handleError(error);
    }
}

console.info("start running");
setInterval(fetchData, config.newsapi.polling_interval);
console.info("application is running");

What we're doing is - every 15 minutes or so, we're polling an API endpoint, and then saving the results into a database.

So there's two main kinds of errors we might get here:

The API I fetched the data from might return some kind of error - it might be down, or my API key might be wrong, or I've gone over the rate limit or whatever.
The database might return some kind of error. For all the same reasons, but a completely different service.

Now in terms of handling these errors, for now all I'll be doing is logging the error, and send me an email.

But I can imagine, that in the future, I might want to handle these two distinct kinds of errors differently. As such, I've created two different try catch blocks, that gives me the flexibility to handle them differently.

But it does look a bit messier, in my opinion. The other way I could write this is:

async function fetchData() {
    console.log("fetching data");
    try {
        const data = await fetch(endPoint);
        const json = await data.json();
        await saveData(json);
    }
    catch (error) {
        console.error(error);
        handleError(error);
    }

}

console.info("start running");
setInterval(fetchData, config.newsapi.polling_interval);
console.info("application is running");

This looks so much tidier. But in which case - it seems like the error handling is a bit trickier.

I guess I could put a switch statement in the catch block handle different errors differently, but is that a bit messy? Any suggestions or guidelines for navigating this?