texts: Generating string expression from data for filter

texts:
I have a set of parameters, and I need to get the expression:

So I got this parameters:

filters = {
  "type": 'human',
  "age": [{'min': 4, 'max': 8}, {'min': 15, 'max': 30}],
  "size": [{'min': 60}],
}

And my goal is to obtain this string from it:

type == 'human' and ((age >= 4 and age <= 8) or (age >= 15 and age <= 30)) and size >= 60

But when I am writing my script to do so, I keep having small details that bother me like the position of the 'or' or 'and' or the parenthesis.
And i think it really hit the readability here.
So I thought maybe there was a better way, but searching Google only shows me the inverse of what I want, that is transforming expression to expression tree.

I am trying to generate a string expression from a set of data (for a filter).

So if someone have a better idea, (or algo, or ...)

Here is my script:

#!/usr/bin/python3

# goal : type == 'human' and ((age >= 4 and age <= 8) or (age >= 15 and age <= 30)) and size >= 60
filters = {
  "type": 'human',
  "age": [{'min': 4, 'max': 8}, {'min': 15, 'max': 30}],
  "size": [{'min': 60}],
}

s = ''
type_first = True
for key, value in filters.items():
  if type_first:
    type_first = False
    s += "("
  else:
    s += " and ("
  value_first = True
  if(isinstance(value, str)):
    s += "%s == %s" % (key, value)
  elif(isinstance(value, list)):
    if value_first:
      value_first = False
    else:
      s += " and"
    dict_first = True
    for dict_ in value:
      if dict_first:
        dict_first = False
        s += "("
      else:
        s += " or ("
      if dict_.get('min', 0):
        s += "%s >= %s" % (key, dict_['min'])
        if dict_.get('max', 0):
          s += " and "
      if dict_.get('max', 0):
        s += "%s <= %s" % (key, dict_['max'])
      s += ")"
  s += ")"

print(s)


texts:
I'm learning from "Jumping into C++" by Alex Allain (sample chapter and TOC) and solved the first problem in Chapter 26 "Inheritance and Polymorphism".

I'm especially concerned about my use of dynamic_cast, as that isn't covered yet and I suspect there's a better way.

  Implement a sort function that takes a vector of pointers to an interface class, Comparable, that defines a method, compare(Comparable& other), and returns 0 if the objects are the same, 1 if the object is greater than other, and -1 if the object is less than other. Create a class that implements this interface, create several instances, and sort them. If you're looking for some inspiration for what to create-try a HighScoreElement class that has a name and a score, and sorts so that the top scores are first, but if two scores are the same, they are sorted next by name.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Comparable
{
public:
    virtual ~Comparable() { }
    virtual int compare(const Comparable& other) const = 0;
};

class HighScoreElement : public Comparable
{
public:
    HighScoreElement(string name, int score)
        : _name(name), _score(score)
    {
    }
    string getName() const {
        return _name;
    }
    int getScore() const {
        return _score;
    }
    virtual int compare(const Comparable& other) const {
        const HighScoreElement *other_hse = dynamic_cast<const HighScoreElement*>(&other);
        if (other_hse) {
            int this_score = getScore(),
                other_score = other_hse->getScore();
            if (other_score > this_score) {
                return -1;
            } else if (other_score < this_score) {
                return 1;
            } else {
                string this_name = getName(),
                       other_name = other_hse->getName();
                if (other_name > this_name) {
                    return -1;
                } else if (other_name < this_name) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }
        // an exception should be thrown here
        return 0;
    }
private:
    string _name;
    int _score;
};

void sortComparableVector(vector<Comparable*>& v) {
    for (int i = 0, e = v.size() - 1; i < e; i++) {
        int sorted_i = i;
        for (int j = i + 1; j < e + 1; j++) {
            if (v[sorted_i]->compare(*v[j]) < 0) {
                sorted_i = j;
            }
        }
        if (sorted_i != i) {
            Comparable *replacement = v[sorted_i];
            v[sorted_i] = v[i];
            v[i] = replacement;
        }
    }
}

int main()
{
    vector<Comparable*> v;

    v.push_back(new HighScoreElement("Kate Bush", 10));
    v.push_back(new HighScoreElement("Peter Gabriel", 20));
    v.push_back(new HighScoreElement("Robert Fripp", 5));
    v.push_back(new HighScoreElement("Phil Collins", 25));
    v.push_back(new HighScoreElement("Roger Waters", 25));
    v.push_back(new HighScoreElement("David Gilmour", 15));

    sortComparableVector(v);

    for (unsigned i = 0; i < v.size(); i++) {
        const HighScoreElement *el = dynamic_cast<const HighScoreElement*>(v[i]);
        cout << i << ": " << el->getScore() << '\t' << el->getName() << '\n';
    }

    return 0;
}


texts: Get count of digits of a number that divide the number

texts:
So, here's a program in python to get the count of digits that divide the number. If you give the program a list like [24,10245], you get an output of:

2
2

This is the code:

def count_div_digits(l):
    for number in l:
        count=0
        digits=str(number)
        for digit in digits:
            if digit=='0':
                continue
            else:
                if number%(int(digit))==0:
                    count+=1
        print count

The above code obviously works. How can I further optimize this, make it short or do it differently?


texts: Accelerate computing a Gram-like matrix with large files

texts:
I have N(>1000) .npy files, each has ~80MB, each contains a vector of high dimension (P).

I have to deal with these data with the code below:

G = np.zeros((N, N))
for i in range(N):
    vi = np.load(str(i)+'.npy')
    for j in range(i, N, 1):
        vj = np.load(str(i)+'.npy')
        G[i, j] = process(vi, vj)
        G[j, i] = G[i, j]

post_process(G)

It takes a lot of time depending on reading files and I'd like to accelerate it.

Is there any suggestion?


texts: Adding data from multiple Excel files to two lists

texts:
I have 5 nested for loops below, to add rows of data from multiple files to one of two lists.  Is there a more pythonic way of doing this?  I've come across an iterator-generator method named iteritems() -- could this be used to make this code more pythonic?

# 5 nested loops

for root,dirs,files in os.walk(src):
   files = [ _ for _ in files if _.endswith('.xlsx') ]
   for file in files:
     wb = xlrd.open_workbook(os.path.join(root,file))
     worksheets = wb.sheet_names()
     for worksheet_name in worksheets:
         if worksheet_name.rfind('7600') != -1 :
            sheet = wb.sheet_by_name(worksheet_name)
            keys = [sheet.cell(3, col_index).value for col_index in xrange(sheet.ncols)]
            for row_index in xrange(4, sheet.nrows):
               d = {keys[col_index]: sheet.cell(row_index, col_index).value
                  for col_index in xrange(sheet.ncols)}
               if file.rfind('oam') != -1 :
                  list_7600EoX.append(d)
               else:
                   list_7600EoX_OAM.append(d)


texts:
Pretty simple class but just want to make sure I've approached it correctly.

public class DynamicArray<T> : IEnumerable<T>, ICollection<T>
    {
        private T[] _items;
        private int _size;
        private const int _growFactor = 2;

        public DynamicArray()
        {
            _items = new T[4];
        }

        public int Count
        {
            get
            {
                return _size;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public void Add(T item)
        {
            if (_size + 1 > _items.Length)
            {
                var newArray = new T[_items.Length * _growFactor];
                _items.CopyTo(newArray, 0);
                _items = newArray;
            }
            _items[_size] = item;
            _size++;
        }

        public void Clear()
        {
            _items = new T[2];
            _size = 0;
        }

        public bool Contains(T item)
        {
            foreach(var value in _items)
            {
                if (value.Equals(item))
                {
                    return true;
                }
            }
            return false;
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            _items.CopyTo(array, arrayIndex);
        }

        public IEnumerator<T> GetEnumerator()
        {
            for(var i = 0; i < _size; i++)
            {
                yield return _items[i];
            }
        }

        public bool Remove(T item)
        {
            for(var i = 0; i < _items.Length; i++)
            {
                if (_items[i].Equals(item))
                {
                    _items[i] = default(T);
                    _size--;
                    return true;
                }
            }
            return false;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }


texts:
I've started writing a small CSV parser class as I needed a method to group a CSV file by a given column.

class CSVParser {

    private $FileName;
    private $FileHandle;
    private $HasHeaderRow = false;
    private $RowsToIgnore = array();

    public function __construct ($FileName, $Mode = "r") {
        $this->FileName = $FileName;

        if (($this->FileHandle = fopen($this->FileName, $Mode)) === FALSE)
            return false; // that's all your getting for now.
    }

    public function hasHeaderRow ($Flag = false) {
        $this->HasHeaderRow = (bool) $Flag;
    }

    public function ignoreRows (array $Rows) {
        $this->RowsToIgnore = $Rows;
    }

    public function groupByColumn ($GroupColumn = 0, $RowLength = 1000, $Seperator = ",") {

        $CurrentRow = -1;
        $Classes = array();

        while (($Data = fgetcsv($this->FileHandle, $RowLength, $Seperator)) !== FALSE) {
            $CurrentRow++;

            // does the csv contain a header row?
            if ($this->HasHeaderRow)
                if ($CurrentRow < 1) continue;

            // are there any rows to ignore?
            if (in_array($CurrentRow, $this->RowsToIgnore))
                continue;

            // line 45
            $Classes[$Data[$GroupColumn]][] = array_filter($Data, function ($var) use ($GroupColumn, $Data) {
                return $var !== $Data[$GroupColumn];
            });
        }

        return $Classes;
    }

    public function __destruct() {
        fclose($this->FileHandle);
    }
}

// usage
$csv = new CSVParser("mycsv.csv");
$csv->hasHeaderRow(true);

$classes = $csv->groupByColumn(0));

I'm a little unsure about the removal of the group by column from the array entries on line 45.

Can anyone see a better method of achieving this?


texts:
I decided to try writing up a port scanner to develop my understanding of Scapy. Here's a timed example of its use. The first two arguments are the address and range of ports to scan:
>>> timeit(lambda: print(port_scan("192.168.123.123", range(15000), timeout=5)), number=1)
[80, 11111]
32.23571190000001

I've made naïve scanners before that were basically just connecting sockets and seeing if you get errors, but those are "noisy" in that each attempt is more likely to get logged. This version sends a SYN to the server to initiate the TCP handshake, then looks at the response. If it's a SYN-ACK, something is accepting connections on the port, and if it's anything else, they aren't (or at least they aren't from us, right now).
There isn't a ton of code here, but this is my first time using Scapy, so I'd appreciate tips on it or anything else. It's painfully slow, so if the packet construction or sending could be sped up, I'd appreciate that. Normally I'd use a thread pool here, but I have no idea what all Scapy is doing behind the scenes, so I haven't tried that yet. I'm trying to used a "cached" ip_layer in new_scan_packets as well, but that doesn't seem to have helped.
from typing import Collection, List
from random import randint, shuffle
from scapy.layers.inet import IP, TCP
from scapy.sendrecv import sr

SEQ_MAX = 2**32 - 1
EPHEMERAL_RANGE = (2**14 + 2**15, 2**16 - 1)  # According to the IANA

SYN_FLAG = "S"
SYN_ACK_FLAG = SYN_FLAG + "A"
ALL_PORTS = range(EPHEMERAL_RANGE[1] + 1)
DEFAULT_TIMEOUT = 3

def new_scan_packets(address: str, ports: Collection[int]) -> List[TCP]:
    ip_layer = IP(dst=address)
    return [ip_layer / TCP(sport=randint(*EPHEMERAL_RANGE), dport=port, seq=randint(0, SEQ_MAX - 1), flags=SYN_FLAG)
            for port in ports]

def port_scan(address: str, ports: Collection[int], shuffled: bool = True, **kwargs) -> List[int]:
    """
    Scans the ports in the given collection and finds which are accepting connections.
    Returns a list of ports that were found to be open.
    kwargs are passed directly to sr to support options like "delay" and "timeout".
    """
    kwargs.setdefault("timeout", DEFAULT_TIMEOUT)  # Because the scan could hang indefinitely otherwise
    syns = new_scan_packets(address, ports)

    if shuffled:
        shuffle(syns)

    answered, _ = sr(syns, verbose=False, **kwargs)
    return sorted(stimulus[TCP].dport
                  for stimulus, response in answered
                  if response[TCP].flags.flagrepr() == SYN_ACK_FLAG)


texts:
I'm a total newbie at parsing, so I figured I'd start by writing instances of Read for HList, which does not currently have them. I'd appreciate some feedback on the following:

{-# LANGUAGE
  DataKinds
 ,KindSignatures
 ,FlexibleContexts
 ,FlexibleInstances
 ,TypeOperators
 ,GADTs
 ,ScopedTypeVariables
 #-}

module MyHList where
import GHC.Read
import Text.ParserCombinators.ReadPrec
import qualified Text.Read.Lex as L

-- This declaration is copied from `Data.HList.HList`.
data HList (l::[*]) where
  HNil  :: HList '[]
  HCons :: e -> HList l -> HList (e ': l)

infixr 2 `HCons`

instance Read (HList '[]) where
  readPrec = parens
    ( do
        expectP (L.Ident "H")
        expectP (L.Punc "[")
        expectP (L.Punc "]")
        return HNil
    )

instance forall e . Read e => Read (HList (e ': '[])) where
  readPrec = parens
    ( do
        expectP (L.Ident "H")
        expectP (L.Punc "[")
        e <- reset $ (readPrec::ReadPrec e)
        expectP (L.Punc "]")
        return (e `HCons` HNil)
    )

instance forall e f (l::[*]) . (Read e, ReadTl (HList (f ': l))) =>
   Read (HList (e ': f ': l)) where
  readPrec = parens
    ( do
        expectP (L.Ident "H")
        expectP (L.Punc "[")
        e <- reset $ (readPrec::ReadPrec e)
        expectP (L.Punc ",")
        rest <- readTl :: ReadPrec (HList (f ': l))
        return (e `HCons` rest)
    )

class ReadTl l where
  readTl :: ReadPrec l

instance forall e . Read e => ReadTl (HList (e ': '[])) where
  {-# INLINE readTl #-}
  readTl = do
    e <- reset $ (readPrec::ReadPrec e)
    expectP (L.Punc "]")
    return $ e `HCons` HNil

instance forall e f (l::[*]) . (Read e, ReadTl (HList (f ': l)))
             => ReadTl (HList (e ': f ': l)) where
  {-# INLINE readTl #-}
  readTl = do
    e <- reset $ (readPrec::ReadPrec e)
    expectP (L.Punc ",")
    rest <- readTl
    return (e `HCons` rest)


texts: How can I make my solution to "Climbing the Leaderboard" not time out?

texts:
I tried to solve the following challenge and optimize the solution but my code still times out. My code is below.

Problem statement

  Climbing the Leaderbord

  Alice is playing an arcade game and wants to climb to the top of the
  leaderboard and wants to track her ranking. The game uses Dense
  Ranking,
  so its leaderboard works like this:

  The player with the highest score is ranked number 1 on the leaderboard.
  Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.

  For example, the four players on the leaderboard have high scores of
  100, 90, 90, and 80. Those players will have ranks 1, 2, 2, and 3,
  respectively. If Alice's scores are 70, 80 and 105, her rankings after
  each game are 4th, 3rd and 1st.

  Function Description

  Complete the climbingLeaderboard function in the editor below. It
  should return an integer array where each element \$res[j]\$
  represents Alice's rank after the \$j^{th}\$ game.

  climbingLeaderboard has the following parameter(s):

  scores: an array of integers that represent leaderboard scores
  alice: an array of integers that represent Alice's scores

  Input Format

  The first line contains an integer \$n\$, the number of players on the
  leaderboard. The next line contains \$n\$ space-separated integers
  \$scores[i]\$, the leaderboard scores in decreasing order. The next
  line contains an integer, \$m\$, denoting the number games Alice
  plays. The last line contains \$m\$ space-separated integers
  \$alice[j]\$, the game scores.

  Constraints

  \$1 \le n \le 2 \times 10^5\$
  \$1 \le m \le 2 \times 10^5\$
  \$0 \le scores[i] \le 10^9\$ for \$0 \le i \lt n\$
  \$0 \le alice[j] \le 10^9\$ for \$0 \le j \lt m\$
  The existing leaderboard, \$scores\$, is in descending order.
  Alice's scores, \$alice\$, are in ascending order.

  Subtask

  For 60% of the maximum score:

  \$1 \le n \le 200\$
  \$1 \le m \le 200\$

  Output Format

  Print \$m\$ integers. The \$j^{th}\$ integer should indicate Alice's
  rank after playing the \$j^{th}\$ game.

  From Climbing the Leaderboard on HackerRank.com.

My code

The code simply starts with climbingLeaderboard method below which delete all similar values in the scores array,
after that it start with Alice score and using binary search it return the correct position.

int getPosition(vector<int> scores,int value,int start,int end){

    int middle = (start+end)/2;

    if(scores[start]<value){
        return start;
    }else if(scores[end]>value){
        return end+1;
    }

    else if(scores[middle]==value){
        return middle;
    }
    else if(start==end){
        if(scores[start]>value){
            return start+1;
        }else{
            return start;
        }
    }

    else if(scores[middle]>value){
        return getPosition(scores,value,middle+1,end);
    }else{
        return getPosition(scores,value,start,middle-1);
    }

}
/////
/////

vector<int> climbingLeaderboard(vector<int> scores, vector<int> alice) {

    int i=1;
    while(1){
        if(i == scores.size()){
            break;
        }
        if(scores[i]==scores[i-1]){
            scores.erase(scores.begin() + i);
            continue;
        }
        i++;
    }
    vector<int> results;
    int endValue = scores.size()-1;

    for(int j=0;j<alice.size();j++){
        endValue = getPosition(scores,alice[j],0,endValue);
        results.insert(results.end(),endValue+1);
    }

    return results;

    }

Any ideas? How can I make it not time out?


texts: Counting items that have a matching attribute

texts:
Here is my code which is going to through multiple for loops to get the matching product attribute.

I have single product with attributes trying to find out the matching product with same attribute value from a list of Product. I am having a multiple for loop. Can anyone suggest a way to optimize this solution?

// This is my single product which is empty with listOf attributes
// (Example : brand, size, color etc.)
Product singleProduct = new Product().setAttrs(
    new ArrayList<ProductAttribute>());

int productFound = 0;

// Get List of product from respose object
List<Product> products = response.getProductList();
for (Product product : products) {
    for (ProductAttribute proAttr : product.getAttrs()) {
        for (ProductAttribute singleProdAttr : singleProduct.getAttrs()) {
            if (proAttr.getValue().equals(singleProdAttr.getValue())) {
                productFound++;
            }
        }
    }
}


texts: Finding an element in an array that occurs a given number of times

texts:
I am trying to solve the below problem from an online coding site:

  Fredo is pretty good at dealing large numbers. So, once his friend
  Zeus gave him an array of N numbers, followed by Q queries which he
  has to answer. In each query, he defines the type of the query and the
  number f for which Fredo has to answer. Each query is of the following
  two types:

  Type 0: For this query, Fredo has to answer the first number in the array (starting from index 0) such that its frequency is at least
  equal to f.
  Type 1: For this query, Fredo has to answer the first number in the array such that frequency is exactly equal to f.

  Now, Fredo answers all his queries but now Zeus imagines how he should
  verify them. So, he asks you to write a code for the same.

  Note: If there is no number which is the answer to the query, output
  0. Use fast I/O.

  Input:

  The first line of the input contains N, the size of the array
  The next line contains N space separated integers
  The next line contains Q, denoting the number of queries

  Then follow Q lines, each line having two integers type and f,
  denoting the type of query and the frequency for which you have to
  answer the query.

  Output:

  You have to print the answer for each query in a separate line.

  Input constraints:

  \$1 \le N \le 10^6\$
  \$1 \le A[i] \le 10^{18}\$
  \$1 \le Q \le 10^6\$
  \$0 \le \text{type} \le 1\$
  \$1 \le f \le 10^{18}\$

  Sample input:

6
1 2 2 1 2 3
5
0 1
0 2
1 2
1 3
0 3

  Sample output:

1
1
1
2
2

Here is the solution that I have tried:

from collections import Counter
import sys
tokenizedInput = sys.stdin.read().split()
t = int(tokenizedInput[0])
a = []
for i in range(t):
    s = int(tokenizedInput[i+1])
    a.append(s)
collection = Counter(a)
key = collection.keys()
value = collection.values()
q = int(tokenizedInput[i+2])
k = i+2
for j in range(q):
    query = int(tokenizedInput[k+2*j+1])
    f = int(tokenizedInput[k+2*j+2])
    temp1 = list(key)
    temp2 = list(value)
    for w in range(t):
        try:
            index = temp1.index(a[w])
        except:
            continue
        if query == 0:
            if temp2[index] >= f:
                print a[w]
                break
            else:
                del temp1[index]
                del temp2[index]
        else:
            if temp2[index] == f:
                print a[w]
                break
            else:
                del temp1[index]
                del temp2[index]
        if len(temp1) == 0:
            print 0
            break
        if w == t-1:
            print 0

This code runs properly and gives the correct output for smaller test cases but crosses the time limit on larger test cases. Can someone please suggest what improvements can be made to this code to improve the speed.


texts:
I'm in the process of building a small application and obviously I need some sort of way to authenticate users. I'm not sure if I am way off or if this is close to what I should be thinking. Please let me know what you think...

<?php
class Authentication_controller {
    private __constructor() {
        include(Authentication_model.php);

        $auth_model = new Authentication_Model();
        $auth_model->database_connect();
    }

    public login($username, $password) {
        if($auth_model->username($username)) {
            //Username exists, now check password
            if($auth_model->password($username, $password)) {
                //User is OK to login, send to designated page
                include(Page_controller.php);

                $page_controller = new Page_controller();
                $page_controller->go('home');

                //Create session
                session_start();

                //Set session variables
                $_SESSION['logged_in'] = true;
                $_SESSION['id'] = $auth_model->id($username);
                $_SESSION['access_level'] = $auth_model->access_level($username);
            } else {
                //Password didn't match
                return $error = "Username or password incorrect."
            }
        } else {
            //Username didn't match
            return $error = "Username or password incorrect."
        }
    }

    public permission($task_access_level) {
        $user_access_level = $_SESSION['access_level'];

        if($user_access_level >= $task_access_level) {
            //User has access level that is high enough for task
            return true;
        } else {
            //User does not have access level high enough for task
            return false;
        }
    }

    public logout() {
        session_destroy();
    }
}

Is this typically how a class like this should work? I'm still very new to this stuff.


texts: Implementing a very simple memory block for quick allocation and memory alignment. C++

texts:
This is programmed in Visual Studio 2019 with the C++17 feature set.
The intent with this code is to be able to create memory blocks of the specified alignment and size, so as to have contiguous cache-friendly memory and to only do one malloc and free for the whole block. Is there something wrong or missing from this implementation so that it would effectively fulfill its purpose?
MemoryBlock.h
#pragma once
class MemoryBlock
{
private:
    void* p_rawMem;
    void* p_currentMemLocation;
    size_t p_currentMemLocationOffset;
    size_t p_totalMemSize;
    size_t p_remainingMemSize;
    size_t p_alignment;
    void *p_lastMemByteLocation;
    void p_addCurrentMemLocation(size_t delta);
public:
    MemoryBlock(size_t size, size_t alignment);
    void* getMemoryWith(size_t size, size_t alignemnt);
    ~MemoryBlock();
};

MemoryBlock.cpp
#include <memory>
#include <stdexcept>
#include <cmath>
#include <assert.h>
#include "MemoryBlock.h"

void MemoryBlock::p_addCurrentMemLocation(size_t delta)
{
    if (delta == 0) {
        return;
    }
    p_currentMemLocationOffset += delta;
    assert((p_currentMemLocationOffset) <= p_totalMemSize);
    p_remainingMemSize -= delta;
    if (p_remainingMemSize == 0) {
        p_currentMemLocation = p_lastMemByteLocation;
    }
    else {
        p_currentMemLocation = static_cast<void*>(static_cast<char*>(p_currentMemLocation) + delta);
    }

}

MemoryBlock::MemoryBlock(size_t size, size_t alignment):
    p_rawMem(_aligned_malloc(size, alignment)),
    p_currentMemLocation(p_rawMem),
    p_currentMemLocationOffset(0),
    p_totalMemSize(size),
    p_remainingMemSize(size),
    p_alignment(alignment),
    p_lastMemByteLocation(static_cast<void*>(static_cast<char*>(p_rawMem) + (size - 1)))
{
}

void* MemoryBlock::getMemoryWith(size_t size, size_t alignment)
{
    if (size > p_totalMemSize) {
        throw std::bad_alloc();
    }
    if (size == 0) {
        throw std::invalid_argument("size must be greater than 0");
    }
    if (alignment == 0 || (alignment &(alignment-1))!= 0) {//checks it alignment is power of 2
        throw std::invalid_argument("alignment should be a power of 2.");
    }
    if (alignment > p_alignment) {
        throw std::invalid_argument("alignment requirement is greater than the memory block alignment.");
    }
    if (size > p_remainingMemSize) {
        return nullptr;
    }
    if (p_totalMemSize == p_remainingMemSize) {
        p_addCurrentMemLocation(size);
        return p_rawMem;
    }
    p_addCurrentMemLocation((std::ceil(p_currentMemLocationOffset/(double)alignment)*alignment)-p_currentMemLocationOffset);
    void* retVal = p_currentMemLocation;
    p_addCurrentMemLocation(size);
    return retVal;
}

MemoryBlock::~MemoryBlock()
{
    _aligned_free(p_rawMem);
}

main.cpp
int main()
{
    MemoryBlock memBlock(1024,1024);
    int *memBlockIntPtr = new(memBlock.getMemoryWith(sizeof(int),alignof(int))) int(1);
    std::cout << *memBlockIntPtr << std::endl;
    constexpr size_t intArrSize = 4;
    int *memBlockIntArrPtr = static_cast<int*>(memBlock.getMemoryWith(sizeof(int)*intArrSize, alignof(int)));
    for (size_t i = 0; i < intArrSize; i++)
    {
        new (&memBlockIntArrPtr[i]) int(i);
    }
    for (size_t i = 0; i < intArrSize; i++)
    {
        std::cout << memBlockIntArrPtr[i] << std::endl;
    }
    return 0;
}

I also ask you if the usage of the code is correct in the main function, and if I'm handling the void pointers adequately in both single-variable and array cases. If not, of course, please correct me with an explanation. Say if I'm doing main, specifically, correctly or the whole thing- and if not, make it clear why. Also suggest if I should be using an underlying memory buffer of char* instead of void* (perhaps combined with static arrays).


texts:
I'm learning go. I wrote a simple logparser for SLF4J in Python some time ago and tried to port it to go as an exercise. The algorithm is identical, but the go-solution isn't quite as fast (the Python solution is about 1.5 times faster for large enough logfiles). Is there any way to get more speed out of it? It do not care, which one is the fastest, but I wanted to know if I could do better with go. Any inspiration is welcome.

A typical logline looks like

[INFO 05:00:07] CoolServiceImpl.executeReminderCC(601) | ==================Finish transaction with id  (REMINDER, 1394424006830)

Others include Stacktraces which span multiple lines.

My code:

package main

import (
    "bufio"
    "flag"
    "fmt"
    "io"
    "log"
    "os"
    "regexp"
    "strings"
)

func isStampRelevant(level, timestamp string) bool {
    return level == "ERROR" && timestamp > "10:00:00" && timestamp < "10:10:00"
}

func isMessageRelevant(lineBuffer string) bool {
    return strings.Contains(lineBuffer, "4020829010703")
}

func readFile(filename string) {
    lineStart := regexp.MustCompile("(TRACE|DEBUG|INFO|WARN|ERROR|FATAL).*?(\\d{2}:\\d{2}:\\d{2})")
    lineBuffer := ""
    fileHandle, err := os.Open(filename)
    if err != nil {
        log.Fatal(err)
    }

    buffer := bufio.NewReader(fileHandle)
    for {
        line, isPrefix, err := buffer.ReadLine()

        if err == io.EOF {
            break
        }
        if err != nil {
            log.Fatal(err)
        }
        if isPrefix {
            log.Fatal("Error: Unexpected long line reading", fileHandle.Name())
        }
        currentLine := string(line)

        matches := lineStart.FindStringSubmatch(currentLine)
        if len(matches) == 3 {
            level := matches[1]
            timestamp := matches[2]

            if len(lineBuffer) > 0 {
                if isMessageRelevant(lineBuffer) {
                    fmt.Println(lineBuffer)
                }
            }

            if isStampRelevant(level, timestamp) {
                lineBuffer = currentLine
            } else {
                lineBuffer = ""
            }
        } else if len(lineBuffer) > 0 {
            lineBuffer += currentLine + "\n"
        }
    }

    if len(lineBuffer) > 0 {
        if isMessageRelevant(lineBuffer) {
            fmt.Println(lineBuffer)
        }
    }
}

func printUsage() {
    fmt.Fprintf(os.Stderr, "usage: %s [inputfile]\n", os.Args[0])
    flag.PrintDefaults()
    os.Exit(2)
}

func main() {
    if len(os.Args) < 2 {
        printUsage()
    }
    filename := os.Args[1]
    readFile(filename)
}


texts:
I wanted to make something for the Community Challenge, so I made a simple permutation and combination calculator. I'd like input on:

The overall design - any suboptimal choices that could be improved?
Naming - This is hard, I know. But is it confusing to follow? Some names seem repetitive, others seem overly long.
Any general improvements I could make.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class PermutationCombinationCalculator extends Application {
    @Override
    public void start(Stage stage) {
        TextField entry = new TextField();
        entry.setPromptText("Enter a value to permutate/combine here");

        TextField limiter = new TextField();
        limiter.setPromptText("Enter S, where S is the number of selections possible");

        Label permutationResult = new Label("Result will be here!");
        Label combinationResult = new Label("Result will be here!");

        Button permutate = new Button("Permutate");
        permutate.setOnAction(e -> {
            permutationResult.setText(
                computePermutation(entry.getText(), limiter.getText())
            );
        });

        Button combine = new Button("Combine");
        combine.setOnAction(e -> {
            combinationResult.setText(
                computeCombination(entry.getText(), limiter.getText())
            );
        });

        HBox permutationLayout = new HBox();
        permutationLayout.getChildren().addAll(permutate, permutationResult);

        HBox combinationLayout = new HBox();
        combinationLayout.getChildren().addAll(combine, combinationResult);

        VBox layout = new VBox();
        layout.getChildren().addAll(entry, limiter, permutationLayout, combinationLayout);

        stage.setScene(new Scene(layout));
        stage.show();
    }

    private static String computePermutation(String entry, String limit) {
        int total;
        int select;

        if (limit.length() == 0) {
            try {
                total = Integer.parseInt(entry);
                return Integer.toString(permutate(total));
            } catch(Exception e) {
                return "Invalid input";
            }
        }

        try {
            total = Integer.parseInt(entry);
            select = Integer.parseInt(limit);
        } catch(Exception e) {
            return "Invalid input";
        }
        return Integer.toString(permutate(total, select));
    }

    private static String computeCombination(String entry, String limit) {
        int total;
        int select;

        try {
            total = Integer.parseInt(entry);
            select = Integer.parseInt(limit);
        } catch(Exception e) {
            return "Invalid input.";
        }
        return Integer.toString(combine(total, select));
    }

    private static int permutate(int p, int r) {
        int result = 1;
        for (int i = p; r > 0; r--, p--) {
            result *= p;
        }

        return result;
    }

    private static int permutate(int p) {
        return permutate(p, p);
    }

    private static int combine(int total, int select) {
        return permutate(total) / (permutate(select) * permutate(total - select));
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Download a runnable Jar here (Requires JRE 8)


texts: Project Euler #8 - "largest product in a series"

texts:
This code works when compiled on gcc 4.8.2.

I would appreciate any comments on my style and the readability of my code, and any improvements that I could make to make the code more readable and/or performant.

Problem:
Largest product in a series

Solution:

/*
 * file:    pe_008.cc
 * title:   Largest product in a grid
 * date:    October 2, 2014
 *
 * note:    The largest possible product of 13 consecutive digits
 *          in a decimal number is 9^13. log_2(9^13) = ~41.2, so we
 *          will need to use a 64-bit integer to represent the product.
 *
 * note 2:  Since some of the elements in the series are 0. The product
 *          of the digits in the 13-digit subseries will be 0 if any of
 *          the elements are 0. This means that if we see a 0, we can
 *          skip ahead by 13 digits, but we have to cache the nonzero
 *          digits that we skip over.
 */

#include <fstream>
#include <iostream>
using namespace std;

const int DIGITS_IN_PRODUCT = 13;

int main() {
  // read series.txt into a string without newline characters
  ifstream ifs("../series.txt");
  string series;
  while (ifs.good()) {
    string line;
    getline(ifs, line);
    series += line;
  }
  ifs.close();
  // take the product of the first 13 elements in the string
  uint64_t product = 1;
  uint64_t max_product;
  uint8_t product_digits[DIGITS_IN_PRODUCT];
  for (size_t i = 0; i < DIGITS_IN_PRODUCT; i++) {
    product_digits[i] = series[i] - '0';
    product *= product_digits[i];
  }
  max_product = product;
  int zero_for_next = 0;
  for (size_t i = DIGITS_IN_PRODUCT; i < series.length(); i++) {
    // use the modulo operator to minimize overwrites
    product_digits[i % DIGITS_IN_PRODUCT] = series[i] - '0';
    if (series[i] == '0') {
      zero_for_next = DIGITS_IN_PRODUCT;
      continue;
    } else if (zero_for_next < 1) {
      product = 1;
      // the order in which the product is taken is not important
      for (uint8_t digit : product_digits) {
        product *= digit;
      }
      if (product > max_product) {
        max_product = product;
      }
    }
    zero_for_next--;
  }
  cout << max_product << endl;
}


texts: Copying content from Excel into PowerPoint as a picture, with error handling

texts:
The code below works, but I want to ask if my solution is adequate or if there's something I should structure differently. If it's alright, I'll apply the same code in other subs.

My code copies stuff from Excel to PowerPoint and inserts it as a picture. In order to do this, I first have to copy data from one part of the sheet into a specific range. This range is where the summary and chart draw their input from. I then copy the summary and chart for each VW into PowerPoint. The one issue that persists is that the line mySlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile frequently gives me an error: "Error -2147188160 Shapes (unknown member): invalid request. The specified data type is unavailable." I had this issue in other files appear as well but was solved by using DoEvents and Application.CutCopyMode = False.

Neither helped in this case, so my next best idea is to let VBA retry copy-pasting until it works (up to 20, so it doesn't get stuck in an endless loop). I'm new to error handling and this is what I came up with (it's a sub called from the main procedure); I marked the 4 labels/GoTo statements:

Public PowerPointApp As PowerPoint.Application
Public myPresentation As PowerPoint.Presentation
Public myShape As PowerPoint.Shape
Public mySlide As PowerPoint.Slide

    'in main sub:
    'Set PowerPointApp = New PowerPoint.Application
    'Set myPresentation = PowerPointApp.Presentations.Open("C:\....")

    Sub SubSlide4(wsKAP As Worksheet, RangeArray As Variant)
        'RangeArray = = Array("Range1", "Range2", "Range3"...)
        Dim iSlide As Long
        Dim rngVW As Variant
        Dim fullNameVW As String
        Dim ErrorCount4 As Long

        iSlide = 4
        ErrorCount4 = 0
        For Each rngVW In RangeArray 'RangeArray has ~10 members

            'Paste correct data for each VW
            wsKAP.Range(rngVW).Copy
            wsKAP.Range("tab.StartHeader").PasteSpecial Paste:=xlPasteValues
            fullNameVW = "Test"
            wsKAP.Range("C73") = fullNameVW

            Set mySlide = myPresentation.Slides(iSlide)

            'Copying Summary for each VW
            Set rng = wsKAP.Range("C89:P97")
            rng.Copy
            DoEvents
            mySlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
            Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
            With myShape
                .Left = 20
                .Top = 71
                .Height = 92
            End With

            'Copying Charts
    RepeatOnError: '<-----------------------------------------------------
            Set rng = wsKAP.Range("A30:Y69")
            rng.Copy
            DoEvents
            On Error GoTo ErrorHandler '<-----------------------------------
                mySlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
                Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
                With myShape
                    .Left = 20
                    .Top = 187
                    .Width = 686
                End With
            iSlide = iSlide + 1
            Application.CutCopyMode = False
        Next rngVW

    ErrorHandler: '<----------------------------------------------------------
        If Err.Number = -2147188160 Then
            If ErrorCount4 > 20 Then
                MsgBox "Too many errors (-2147188160), canceling"
                End
            End If

            ErrorCount4 = ErrorCount4 + 1
            Debug.Print "ErrorCount4 is " & ErrorCount4
            Resume RepeatOnError '<-----------------------------------------
        End If
        On Error GoTo 0
    End Sub

So whenever error -2147188160 occurs, it should jump back to 'RepeatOnError' and try copy-pasting it once again. So far this has worked, but I wonder if this is the VBA way to go about error handling.

Someone on Stack Overflow suggested I need to use Exit Sub before the error handler— is that true?


texts:
I have a type that receives serial data over a pipe:

struct Packet : Base { // Base is POD, too
  int foo;
  char data[];
};

In the context, where I instantiate Packet, I already know how large this particular packet will be, so I write:

size_t const size = sizeof(Packet) + dataLength;
std::auto_ptr<Base> p(new((void*)(new char[size])) Packet());
// note that it will be deleted via 'delete'

Is this a sensible way of doing this, or is there a more idiomatic way?


texts:
Even though singletons are bad practice and often unnecessary, I still see many developers using this pattern over and over. Since implementation of this pattern usually requires some common code, I thought we could just make a base class to be extended.

I came up with this so far.

<?php
namespace wl;

/**
 * @dosc allows only one instance for each extending class
 * @example use it for database connection, config setup...
 * Be aware, the singleton pattern is consider to be an anti-pattern
 * because it can be hard to debug.
 * In most cases you do not need to use singleton pattern
 * so take a longer moment to think about it before you use it.
 */
class Singleton
{
    /**
     *  holds an single instance of a class
     *
     *  @var array of objects
     */
    protected static $instance = [];

    /**
     *  @desc provides a single slot to hold an instance interchanble between all child classes.
     *  @return object
     */
    public static function getInstance(){
        $class = get_called_class();
        if(!isset(self::$instance[$class]) || !self::$instance[$class] instanceof $class){
            self::$instance[$class] = new static(); // create and instance of child class which extends Singleton super class
            echo "new ". $class . PHP_EOL; // remove this line after testing
            return  self::$instance[$class]; // remove this line after testing
        }
        echo "old ". $class . PHP_EOL; // remove this line after testing
        return static::$instance[$class];
    }

    /**
     *  do not allow create new instance by new keyword
     *
     */
    protected function __construct(){}

    /**
     *  Do not clone the object
     */
    protected function __clone(){}

    /**
     *  Do not allow reserialization of this object
     */
    protected function __wakeup(){}

}

/**
 * ----------------------------------------------USE EXAMPLE---------------------------------------------------
 *  @docs example database class by extending singleton class implements singleton pattern
 */
class Database extends Singleton
{
    public function __construct(){

    }
}

/**
 *  @docs Config class by extending singleton class implements singleton pattern
 */
class Config extends Singleton
{
    public function __construct(){

    }
}

/**
 *  @example create new Database
 */
$bd1 = Database::getInstance(); // new
$bd2 = Database::getInstance(); // old

/**
 *  @example create new Config
 */
$bd1 = Config::getInstance(); // new
$bd2 = Config::getInstance(); // old

$bd3 = Config::getInstance(); // old
$bd4 = Database::getInstance(); // old

$bd5 = Database::getInstance(); // old
$bd6 = Config::getInstance(); // old

Any suggestions are very welcome.
github


texts: Python OOP Whac-A-Mole program using turtle

texts:
I am trying to create an object oriented program in Python for a whac-a-mole program.

I want to keep this simple where there are just a hammer and a mole and when the hammer turtle makes contact with the mole turtle.

I have created 4 classes, one for Game which is the super class, one for sprite which inherits from Turtle and one each for hammer and mole which both inherit from Sprite.

What I am asking is how to instantiate the mole and hammer turtles I have made use of inheritance but want to minimize the amount of methods I am using:

import random
import turtle
wn=turtle.Screen()
wn.addshape("moleSmall.gif")
wn.addshape("Mole.gif")
wn.addshape("Hammer.gif")
turtle.setup(800, 600)

    class Game():
        def __init__(self):
            self.score=0
            self.pointsDisplay=Points()

        def update_score(self,x,y):
            self.score = self.score + 2
            print("Score:", self.score)
            self.callDisplayScore()
        def decrease_score(self,x,y):
            self.score = self.score -1
            print("Score: ", self.score)
            self.callDisplayScore()
        def callDisplayScore(self):
            score = self.score
    class Sprite(turtle.Turtle):
        def __init__(self):
            turtle.Turtle.__init__(self)
            self.x=0
            self.y=0
            crash =False
            self.crash=crash
        def randomMove(self):
            leftbound = -self.wn.window_width()/2
            rightbound = self.wn.window_width()/2
            topbound = self.wn.window_height()/2
            bottombound = -self.wn.window_height()/2
            rand_x = random.randint(leftbound , rightbound)
            rand_y = random.randint(bottombound, topbound)
            self.mole.setpos(rand_x,rand_y)
        def collisionCheck(self,turtle1, turtle2):
            self.crash = False
            turtle1X = turtle1.xcor()
            turtle1Y = turtle1.ycor()
            turtle2X = turtle2.xcor()
            turtle2Y = turtle2.ycor()
            turtle1Pos = (int(turtle1X), int(turtle1Y))
            turtle2Pos = (int(turtle2X), int(turtle2Y))
            if turtle1Pos == turtle2Pos or turtle1X < turtle2X - 50 and \
                turtle1Y > turtle2Y - 50 and  turtle1X < turtle2X - 50 and\
                turtle1Y > turtle2Y + 50:
                self.crash = True
            return self.crash
    class Mole(Sprite):
        def __init__(self):
            Sprite.__init__(self)
            self.mole.pu()
            self.mole.shape("moleSmall.gif")
            self.x= self.mole.xcor()
            self.y = self.mole.ycor()
            self.randomMove()
    class Hammer(Sprite):
        def __init__(self):
            Sprite.__init__(self)
            self.counter = 0
            self.hammer.penup()
            self.hammer.shape("Hammer.gif")
            self.hammer.penup()
            leftbound = -self.wn.window_width()/2
            rightbound = self.wn.window_width()/2
            topbound = self.wn.window_height()/2
            bottombound = -self.wn.window_height()/2
            rand_x = random.randint(leftbound , rightbound)
            rand_y = random.randint(bottombound, topbound)
            self.hammer.setpos(rand_x,rand_y)
        def HammerMove(self):
            self.hammer.pu()
            self.hammer.clear()
            self.wn.onscreenclick(self.hammer.goto)
            while self.collisionCheck(self.mole, self.hammer) == False:
                self.randomMove()
                if self.collisionCheck(self.mole, self.hammer) == True:
                    self.score = self.score + 1
                    print(self.score)


texts:
I have been trying to parse results and make sure when I am doing so that I don't repeat the same data. I tried to use a few different options built into php with no luck. I did find an example of a recursive array search that seems to work but it's very intensive and adds a lot of time to the script.

What I'm needing: Does anyone know a better way to handle this without changing the array that I supply to it so something built-in like in_array or array_search?

Array example:

array (size=3)
  0 =>
    array (size=3)
      'author' => string 'Jim Beam' (length=8)
      'id' => string '1' (length=1)
      'md5' => string 'f2ebf4d4f333c31ef1491a377edf2cc4' (length=32)
  1 =>
    array (size=3)
      'author' => string 'Jack Daniels' (length=12)
      'id' => string '2' (length=1)
      'md5' => string 'd1839707c130497bfd569c77f97ccac7' (length=32)
  2 =>
    array (size=3)
      'author' => string 'Jose Cuervo' (length=11)
      'id' => string '3' (length=1)
      'md5' => string '64e989b4330cc03dea7fdf6bfe10dda1' (length=32)

Code example:

function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=>$value) {
        $current_key=$key;
        if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
            return $current_key;
        }
    }
    return false;
}

$agentArray = array(
    array('author'=>'Jim Beam','id'=>'1','md5'=>'f2ebf4d4f333c31ef1491a377edf2cc4'),
    array('author'=>'Jack Daniels','id'=>'2','md5'=>'d1839707c130497bfd569c77f97ccac7'),
    array('author'=>'Jose Cuervo','id'=>'3','md5'=>'64e989b4330cc03dea7fdf6bfe10dda1')
);

$fakeMD5 = '84d7dc19766c446f5e4084e8fce87f82'; //StackOverflow MD5
$realMD5 = 'd1839707c130497bfd569c77f97ccac7'; //Jack Daniels MD5

echo 'In_Array:
';

$faketest = in_array($fakeMD5,$agentArray);
$realtest = in_array($realMD5,$agentArray);

var_dump($faketest,$realtest);

echo 'Search_Array:
';

$faketest2 = array_search($fakeMD5,$agentArray);
$realtest2 = array_search($realMD5,$agentArray);

var_dump($faketest2,$realtest2);

echo 'Custom Recursive Array Seach Function:
';

$faketest3 = recursive_array_search($fakeMD5,$agentArray);
$realtest3 = recursive_array_search($realMD5,$agentArray);

var_dump($faketest3,$realtest3);

Results:

In_Array:
Fake: boolean false
Real: boolean false

Search_Array:
Fake: boolean false
Real: boolean false

Custom Recursive Array Seach Function:
Fake: boolean false
Real: int 1


texts: Replacing MIN and MAX macros with type-safe, recursive templates of variable arity

texts:
I'm currently reading Mastering Perl for Bioinformatics; I'm using the book for learning bioinformatics while implementing the code in C++. I came across a situation where I needed to use the minimum of three numbers, which the author did using a function call min3 which, as expected, returns the minimum of three numbers.

Rather than using the canonical C-style macro #define MIN(a,b) ..., I wanted to try and write more idiomatic C++, first by staying away from macros, and second by using templates to write type-safe, variadic Min and Max functions.

namespace Math
{
    template <typename T1, typename T2>
    constexpr inline auto Max(T1 a, T2 b) noexcept
    {
        return (a > b) ? a : b;
    }

    template <typename T1, typename T2, typename... Types>
    constexpr inline auto Max(T1 a, T2 b, Types... args) noexcept
    {
        return Max(a, Max(b, args...));
    }

    template <typename T1, typename T2>
    constexpr inline auto Min(T1 a, T2 b) noexcept
    {
        return (a < b) ? a : b;
    }

    template <typename T1, typename T2, typename... Types>
    constexpr inline auto Min(T1 a, T2 b, Types... args) noexcept
    {
        return Min(a, Min(b, args...));
    }
} // namespace Math

Example usage:

std::cout << Math::Max(1,2)       << std::cout.widen('\n');
std::cout << Math::Min(3,8,4,3,2) << std::cout.widen('\n');

Output:

2
2

I'm using std::cout.widen('\n') so as to not call std::fflush() on each call to std::endl.


texts: Making recurring requests to a web service using Task.Run

texts:
I am practicing with using Tasks and I put something together but am unsure if I used it properly.  I read through Stephen Cleary's blog and it seems that it is okay but at the same time I do some things that seem like there is a better way.  So what my program does is it has a start button and a stop button. When you press the start button it will start doing recurring requests to a webservice. At that time the start button is disabled and the stop button is enabled. When the requests finish up, I want the start button to be re-enabled and the stop button to become disabled.

This is what I have in my Form:

private async void btnSend_Click(object sender, EventArgs e)
{
      ...
      ...
      ...
            btnSend.Enabled = false;
            btnStop.Enabled = true;

            cts = new CancellationTokenSource();

            try
            {
                // wait for the task to finish before continuing to enable the buttons
                task t = requestThread.Execute(cts.Token);
                await t;
            }
            catch (Exception eRun)
            {
                MessageBox.Show(eRun.Message, "Application Error");
            }
            finally
            {
                cts.Dispose();
            }

            // re-enable btnSend
            btnSend.Enabled = true;
            btnStop.Enabled = false;

And then the Execute method:

public async Task Execute(CancelationToken ct)
    {
        UI.Log("-------------------------------------------------");
        UI.Log("Starting Test Execution");

        if (Recurring)
        {
            for (int i = 1; i <= NumberOfRequests; i++)
            {
                if (ct.IsCancellationRequested)
                    return;
                UI.Log(string.Format("Attempting {0} of {1}", i, numberOfRequests));
                await SendRequest(ct);
                if (NumberOfRequests > 1 && i != numberOfRequests)
                {
                    try
                    {
                        await Task.Delay(timeInterval * 1000, ct);
                    }
                    catch (TaskCanceledException e)
                    {
                        // do nothing it's okay
                    }
                }
            }
        }
        else
        {
            await SendRequest(ct);
        }

        UI.Log("Test Execution Complete");
    }

I would really appreciate it if someone with some more Task experience can give me some pointers on whether this looks okay or I am breaking etiquette.

Edit: I have done some improvements with the help of a discussion from StackOverflow. Particularly, I removed the Task.Run since neither the Execute nor the SendRequest methods are CPU-bound. I changed my request.GetResponse to await request.GetResponseAsync() in SendRequest. This way I'm not wasting a thread from the thread pool for no reason.


texts:
This is my implementation of CeWL, a ruby application that returns a list of words which can be used for password crackers.

My main concern is performance, as it takes a couple seconds to run. I presume it's mainly within the get_words function. Any and all feedback and criticism is appreciated and considered.

"""
Cewl Implementation (without page depth)

Usage: python3 cewl.py <max word length> <name of file to create> <valid website url>
"""

import string
import sys
import os
from typing import List

import bs4.element
from bs4 import BeautifulSoup
from bs4.element import Comment
import urllib.request

FILE_DIRECTORY = os.path.dirname(os.path.realpath(__file__))

def write_to_file(filename: str, words: List[str]) -> None:
    """
    Creates a file in the same directory as the file.

    :param str file_name: Filename of word file
    :param List[str] words: Words to write to file

    :return None:
    """
    with open(f"{FILE_DIRECTORY}/{filename}", "w") as file:
        for word in words:
            file.write(f"{word}\n")

def filter_words(words: List[str], max_length: int) -> List[str]:
    """
    Filters each of the words by the criteria passed by the user. Also
    ensures that the word is a "word" (no numbers or special characters).

    :param List[str] words: Words to filter
    :param int max_length: Max word length

    :return List[str]: Filtered words
    """
    return [word for word in words if len(word) <= max_length and word.isalpha()]

def visible_tag(element: bs4.element) -> bool:
    """
    Returns if the passed element is visible to scrape words.

    :param bs4.element element: Element to check

    :return bool: True is visible, False otherwise
    """
    return (
        not element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]'] and
        not isinstance(element, Comment)
    )

def get_words(url: str) -> List[str]:
    """
    Returns all the words in the website. Steps this function takes:

    1. Get all text from website
    2. Creates a list with words that have punctuation removed
    3. Removes newlines from each word in the list and lowers it
    4. Removes each "" in the list and joins to a string
    5. Returns a list of each word as an element of the list

    :param str url: Url of website

    :return List[str]: List of all text in website
    """
    body = urllib.request.urlopen(url).read()
    soup = BeautifulSoup(body, 'html.parser')
    texts = soup.findAll(text=True)

    # Steps begin #
    visible_texts = filter(visible_tag, texts)
    words = [str(word).translate(str.maketrans('', '', string.punctuation)) for word in visible_texts]
    words = [word.rstrip().lower() for word in words]
    words = " ".join(filter(None, words))
    return words.split()

def main() -> None:
    """
    Main Method for Cewl.

    :return None:
    """
    try:
        max_word_length = int(sys.argv[1])
        filename = sys.argv[2]
        website = sys.argv[3]
    except IndexError:
        sys.exit("Usage: python3 cewl.py <max word length> <name of file to create> <valid website url>")

    words = get_words(website)
    words = filter_words(words, max_word_length)
    words = list(set(words)) # Remove duplicate words and convert back to list #
    write_to_file(filename, words)

if __name__ == "__main__":
    main()


texts:
I have an array filled with 5 other arrays that consists of arrays of 2 values (first a date then a count):

[
  [
    [dt, cnt], [dt, cnt], ....
  ],
  [
    [dt, cnt], [dt, cnt], ....
  ],
  [
    [dt, cnt], [dt, cnt], ....
  ],
  [
    [dt, cnt], [dt, cnt], ....
  ],
  [
    [dt, cnt], [dt, cnt], ....
  ],
]

It concerns click statistics of different websites. This data needs to be converted to data to make a consistent chart with Google visualisations. So first a conversion is done in Python, then this result converted to JSON to pass to the Google libs.

My current code is this:

# determine min date
mindate = datetime.date.max
for dataSet in sets:
    if (dataSet[0][0] < mindate):
        mindate = dataSet[0][0];
# fill a dictionary with all dates involved
datedict = {}
for dat in daterange(mindate, today):
    datedict[dat] = [dat];
# fill dictionary with rest of data
arrlen = 2
for dataSet in sets:
    # first the values
    for value in dataSet:
        datedict[value[0]] = datedict[value[0]] + [value[1]];
    # don't forget the missing values (use 0)
    for dat in daterange(mindate, today):
        if len(datedict[dat]) < arrlen:
            datedict[dat] = datedict[dat] + [0]
    arrlen = arrlen + 1
# convert to an array
datearr = []
for dat in daterange(mindate, today):
    datearr = datearr + [datedict[dat]]
# convert to json
result = json.dumps(datearr, cls=DateTimeEncoder)

(The DateTimeEncoder just gives a JavaScript-friendly datetime in the JSON)

The output looks like this:

[
    ["new Date(2008, 7, 27)", 0, 5371, 1042, 69, 0],
    ["new Date(2008, 7, 28)", 0, 5665, 1100, 89, 0],
    ...
]

This is one of my first Python adventures, so I expect this piece of code can be improved upon easily. I want this to be shorter and more elegant. Show me the awesomeness of Python because I'm still a bit disappointed.

I'm using Django, by the way.


texts: Processing file inclusion directives using mutual recursion

texts:
I am preprocessing a set of files where one file is included in another file with an include tag like shown below:

file A

include file B
include file C
 contents of file A
include file D
 contents of file A

Here I need to replace the include tags with contents of each of the respective files, just like what a compiler does. I have two functions for that:

def parseContent(self, rdbFilePath, content):
    finalContent = self.removeComments(content)
    includeFileSearch = re.compile(r'(?P<tag>(\s)*include+(\s*)\"(\s*)(?P<file>[a-zA-Z0-9\.\_/]*)\")')
    for includes in includeFileSearch.finditer(finalContent):
        finalContent = re.sub(includes.group('tag'),self.parseIncludes(rdbFilePath, includes.group('file')), finalContent)
    return finalContent

def parseIncludes(self, rdbFilePath, file):
    path = rdbFilePath + "/" + file
    f = open(path)
    pathDir = os.path.dirname(path)
    includedFileContent = self.parseContent(pathDir, f.read())
    return includedFileContent

As you can see, functions parseContent and parseIncludes call each other recursively to replace all the include tags in every file. The logic works fine. But it takes a bit long time to execute. Is there any better way to do the same with lesser execution time?


texts: jQuery cycle content of static HTML page

texts:
I am making a simple jQuery code that changes the content of a static HTML page when a button (previous/next aka left/right) are pressed. The page is static, so the content that must be cycled (images setted as background, descriptions and a link) are saved in an array. Images must be shown two at a time, so the arrays must use two different counters (one for the images that goes every 2, and one for the links and descriptions that goes every element.)

var images = []; //array of images
var descriptions = []; //array of strings
var links = []; //array of urls

var i = 0;
  var j = 0;

  $(document).ready(function() {
    $(".view-before").css("background-image", 'url('+images[i]+')');
    $(".view-after").css("background-image", 'url('+images[i+1]+')');
    $(".gallery-title-h1").html(descriptions[j]);
    $("#link").attr("href", links[j]);
    animateTo(80);
  });

  $("#gallery_button_left").click(function() {
    if(i>0) {
      j=j-1;
      animateTo(80);
      $(".view-before").css("background-image", 'url('+images[i-2]+')');
      $(".view-after").css("background-image", 'url('+images[i-1]+')');
      $(".gallery-title-h1").html(descriptions[j]);
      $("#link").attr("href", links[j]);
      i=i-2;
    }
  });

  $("#gallery_button_right").click(function() {
    if(i<images.length-2) {
      i=i+2;
      j=j+1;
      animateTo(80);
      $(".view-before").css("background-image", 'url('+images[i]+')');
      $(".view-after").css("background-image", 'url('+images[i+1]+')');
      $(".gallery-title-h1").html(descriptions[j]);
      $("#link").attr("href", links[j]);
    }
  });

What I'd like to know is if performances could be improved or if it can be done in a "smarter" way than just cycling array elements.


texts: Freeing a binary tree data structure in C

texts:
I have this function that I hope to be a good alternative to the recursive (thus performance expensive) approach.

#define _free_all(node, yes_no) \
    do { \
        Tree *__i;
        for (__i = node; __i; __i = __i->yes_no); \
        free(; node != __i; __i = __i->parent) { \
            free(__i->value); \
            free(__i); \
        } \
    } while (0)

void Tree_destroy(tree)
    Tree *tree;
{ /* Destroys a Tree object - Non Recursive */
    if (!tree)
        return;
    _free_all(tree, yes);
    _free_all(tree, no);

    if (tree->parent) {
        if (tree->parent->no == info)
            tree->parent->no = NULL;
        else
            tree->parent->yes = NULL;
    }
    free(tree);
}

The two children of each parent are no and yes.

Are there any issues with this approach ?


texts:
I have a file, the content is in the format of

41545
0
0
1
2
3
3
3
...

Following is the code

X=[]
Y=[]
total=0
count=0
for line in open(file,'r'):
    count+=1
    line=line.strip()
    if(line.strip()):
        if(count==1):
            total=int(line)
        else:
            X.append(count-1)
            Y.append(int(line))

the output for the above code is

total=41545 #first line
X=[1,2,3,4,5,6,7] #count of second line onwards
Y=[0,0,1,2,3,3,3] #second line onwards information

Is there a shorter and hopefully more efficient code that can achieve the same purpose?


texts: Associate an object with a regular expression in Python

texts:
Is there a way I can link/associate an object (in this particular case a function) with a regular expression pattern?

Here's the function I'm working on:

def get_command(url):
    """
    Return command for downloading a particular url.

    >>> get_command('http://randomsite.com/XYZ')
    ['wget', '--content-disposition', '--continue', '--quiet', 'http://randomsite.com/XYZ']

    >>> get_command('http://examplesite.com/XYZ')
    ['/usr/bin/torify', 'wget', '--content-disposition', '--continue', '--quiet', '--referer=http://referersite.com', 'http://examplesite.com/XYZ']

    >>> get_command('https://mega.nz/%23!xd432Afds')
    ['megadl', '--no-progress', 'https://mega.nz/#!xd432Afds']

    >>> get_command('https://mega.nz/#!s2JHj1fds')
    ['megadl', '--no-progress', 'https://mega.nz/#!s2JHj1fds']

    >>> get_command('http://othersite.com/XYZ')
    ['downloadtool', 'http://othersite.com/XYZ']
    """
    import re

    base = ['wget', '--content-disposition', '--continue', '--quiet']

    # examplesite.com via torified wget and special referer
    if re.match(r'(https?://)?examplesite\.com/.+$', url):
        return ['/usr/bin/torify'] + base + \
            ['--referer=http://referersite.com', url]
    # MEGA via megadl of megatools
    elif re.match(r'https://mega\.nz/.+$', url):
        # fix url if necessary
        return ['megadl', '--no-progress', url.replace('%23', '#', 1)]
    # othersite.com via a dedicated download tool
    elif re.match(r'(https?://)?othersite\.com/.+$', url):
        return ['downloadtool', url]
    # default wget command
    else:
        return base + [url]

I think the aforementioned code is pretty straightforward, but it got me thinking whether there's a good way to refactor this. As the if-chain of re.match(pattern, url) gets longer, it starts to look more and more necessary.

Perhaps ideally I'd have a dictionary of patterns with their associated functions. Anyways here's what I ended up with:

def get_command(url):
    """Return command for downloading a particular url."""
    import re

    base = ['wget', '--content-disposition', '--continue', '--quiet']

    commands = [
        # examplesite.com via torified wget and special referer
        (r'(https?://)?examplesite\.com/.+$',
         lambda u: ['/usr/bin/torify'] + base +
         ['--referer=http://referersite.com', u]),
        # MEGA via megadl of megatools (fix url if necessary)
        (r'https://mega\.nz/.+$',
         lambda u: ['megadl', '--no-progress', u.replace('%23', '#', 1)]),
        # othersite.com via a dedicated download tool
        (r'(https?://)?othersite\.com/.+$',
         lambda u: ['downloadtool', u])
    ]

    return next(
        (f for p, f in commands if re.match(p, url)), lambda u: base + [u]
    )(url)

Doesn't seem like much of an improvement, potentially the opposite. Anyone have a good way to refactor this?


texts: Smart pointer mark II - my answer to C++ 11 shared_ptr

texts:
I am writing this smart pointer as a learning exercise.  Any feedback would be most appreciated.

Any flaws? Have I missed any test cases?

smart_pointer.hpp:

#ifndef SMARTPOINTER_HPP_
#define SMARTPOINTER_HPP_

#include <iostream>

template<typename T>
class sp {
public:
   sp(T* ptr) : ptr_(ptr), ref_cnt_(new unsigned(1)) {
      std::cout << "sp ctor. Address: " << ptr_ << ", ref_cnt_: " << *ref_cnt_ << '\n';
   }

   T& operator*() { return *ptr_; }
   const T& operator*() const { return *ptr_; }

   T* operator->() { return ptr_; }
   const T* operator->() const { return ptr_; }

   // access to raw ptr
   T* get() { return ptr_; }

   sp(const sp<T>& rhs) : ptr_(rhs.ptr_), ref_cnt_(rhs.ref_cnt_) {
      addref();
      std::cout << "sp copy ctor: " << ptr_ << ", ref_cnt_: " << *ref_cnt_ << '\n';
   }

   sp& operator=(const sp& rhs) {
      if(this != &rhs) {
         try_delete();
         ptr_ = rhs.ptr_;
         ref_cnt_ = rhs.ref_cnt_;
         addref();
         std::cout << "sp assignment: " << ptr_ << ", ref_cnt_: " << *ref_cnt_ << '\n';
      }
      return *this;
   }

   ~sp() {
        std::cout << "~sp. Address: " << ptr_ << ", ref count before remove_ref: " << *ref_cnt_ << '\n';
        try_delete();
   }

private:
   unsigned addref() {
      return ++*ref_cnt_;
   }
   unsigned remove_ref() {
      return --*ref_cnt_;
   }

   void try_delete() {
      if(remove_ref() == 0) {
         std::cout << "try_delete(): finally deleting ptr_ at address: " << ptr_ << ", ref count: " << *ref_cnt_ << '\n';
         delete ref_cnt_;
         delete ptr_;
      }
   }

   T* ptr_;                    // raw ptr
   unsigned* ref_cnt_;  // shared ownership
};

#endif //SMARTPOINTER_HPP_

Code to exercise:

#include <iostream>
#include <vector>

#include "smart_pointer.hpp"

// useless class just used to test with object more complex than int
class tester {
public:
   tester(const int id, const char* name) : id_(id), name_(name) {}

   const char* const get_name() const { return name_; }
   const int get_id() const { return id_; }

private:
   const int id_;
   const char* const name_;
};

// global vector to hold smart pointer
std::vector< sp<int> > gvs;

int main() {

   {
      std::cout << "Test with ints\n";
      sp<int> myptr1 = new int(3);
      *myptr1 = 4;
      std::cout << "number: " << *myptr1 << '\n';

      {
         // test usage in collection
         std::vector< sp<int> > vs;
         vs.push_back(myptr1);
      }

      // global collection
      gvs.push_back(myptr1);

      // copying
      sp<int> myp2(myptr1);
      sp<int> myp3(myptr1);
      sp<int> myp4 = myptr1;
   }

   {

      std::cout << "Test with more complex objects\n";
      sp<tester> spt = new tester(1, "Keira Knightley");

      tester* p = spt.get();
      std::cout << "Name: " << p->get_name() << ", id: " << p->get_id() << '\n';

      // copying
      sp<tester> spt2 = spt;
      sp<tester> spt3(spt);

      sp<tester> spt4(new tester(2, "Cate Blanchett"));

      // re-assign
      spt = new tester(3, "Natalie Portman");

   }

   return 0;
}


texts:
function    C = convolve_slow(A,B)
(file name is accordingly convolve_slow.m )
This routine performs convolution between an image A and a mask B.

Input:      A - a grayscale image (values in [0,255])
                    B - a grayscale image (values in [0,255]) serves as a mask in the convolution.
Output:     C - a grayscale image (values in [0,255]) - the output of the convolution.
                          C is the same size as A.

Method:  Convolve A with mask B using zero padding. Assume the origin of B is at
         floor(size(B)/2)+1.
         Do NOT use matlab convolution routines (conv,conv2,filter2 etc).
         Make the routine as efficient as possible: Restrict usage of for loops which
         are expensive (use matrix multiplications and matlab routines such as dot etc).

         To simplify and reduce ifs, you should pad the image with zeros before starting your convolution loop.
         Do not assume the size of A nor B (B might actually be larger than A sometimes).

here is my solution for this exercise. please elaborate on any change you have done or suggesting since i'm new to matlab and image processing.

function [ C ] = convolve_slow( A,B )

%This routine performs convolution between an image A and a mask B.
%
% Input:      A - a grayscale image (values in [0,255])
%             B - a grayscale image (values in [0,255]) serves as a mask in the convolution.
% Output:     C - a grayscale image (values in [0,255]) - the output of the    convolution.
%             C is the same size as A.
%
% Method:  Convolve A with mask B using zero padding. Assume the origin of B is at floor(size(B)/2)+1.

% init C to size A with zeros
C = zeros(size(A));

% make b xy-reflection and vector
vectB = reshape(flipdim(flipdim(B,1),2)' ,[] , 1);

% padding A with zeros
paddedA = padarray(A, [floor(size(B,1)/2) floor(size(B,2)/2)]);

% Loop over A matrix:
for i = 1:size(A,1)
    for j = 1:size(A,2)
        startAi = i;
        finishAi = i + size(B,1) - 1;
        startAj = j;
        finishAj = j + size(B,2) - 1;
        vectPaddedA = reshape(paddedA(startAi :finishAi,startAj:finishAj)',1,[]);
        C(i,j) = vectPaddedA* vectB;
    end
end

end


texts: Python script to execute a command using Paramiko SSH

texts:
I wrote this to learn exactly how paramiko worked in Py3. Also to keep as a code snippet for future work-related scripts. I know it can be tweaked and I'm hoping you experienced folks can help with that. I left the commented out lines for 'passwd' because not all hosts I connect to will use pubkeys.

import sys, paramiko, getpass

user = input("Username:") #Prompts for username
#passwd = getpass.getpass("Password for " + user + ":") #Prompts for password
key_file = input("Public key full path:") #Prompts for full path to key file
name = input("Target Hostname:") #Prompt for target hostname
command = input("Enter target command:") #Prompt for remote command

#Calling the paramiko ssh function
ssh = paramiko.SSHClient() #Define value 'ssh' as calling the paramiko.sshclient
print('calling paramiko')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #Must come before connect line to add hosts to known_hosts
#ssh.connect("name", username="user", password="passwd")
ssh.connect(hostname = name, username = user, key_filename = key_file) # key_filename means to specify the actual file, pkey on the
#other hand means to actually paste in the key text itself
print('trying to connect')
ssh.invoke_shell()
stdin, stdout, stderr = ssh.exec_command (command)
print(stdout.read())
#ssh.close()


texts: Spawning futures as an alternative to multithreading

texts:
I was trying to do a sample threading program, where the spawning of thread is in a while loop. I DO NOT want to generate multiple number of threads. The while loop should keep on running, irrespective of whether the thread completed execution or not. There is a call to the .get() function of the above thread within the while loop. Again, this has to be called only when the thread executed correctly.

My code is given below:

#include <iostream>
#include <future>
#include <thread>
#include <chrono>

int thread(int varJ)
{
    std::cout<<"\t\t"<<varJ<<std::endl;
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    return varJ;
}

int main()
{
    int varI = 0;
    std::future<int> future = std::async(std::launch::async, [](){return 0;});
    std::future_status status;
    bool flag = false;

    while(1)
    {
        status = future.wait_for(std::chrono::milliseconds(0));
        if(status == std::future_status::ready && !flag)
        {
            future = std::async(std::launch::async, thread, varI);
            flag = true;
        }
        status = future.wait_for(std::chrono::milliseconds(0));
        if(status == std::future_status::ready && flag)
        {
            std::cout << "\t result is " << future.get() << '\n';
            future = std::async(std::launch::async, [](){return 0;});
            flag = false;
        }
        varI++;
    }
}

The output obtained:

    0
 result is 0
    865
 result is 865
    1723
 result is 1723
    2632
 result is 2632
    3590
 result is 3590
    4551
 result is 4551
    5509
 result is 5509

The output is as I expected it to be. But is this the way to code or is there a better way?

I am not pleased to use a flag in between the code, calling future.wait_for a lot many times and especially using the dummy thread that returns 0.


texts:
I overloaded some C# operators to get a pseudo version of the Brainfuck symbols in the effort of understanding the flow of this language.

I got this version:

void Main()
{
    /*
    *   Write `Hello World!`
    *           using operators overloading for brainfuck symbols
    */
    var _ = new brainfuck();

    _ = + + + + +    + + + + +     _        ; // set cell #0 to 10

    while (_)
    {
        _ =     + + + + +    + +       (_ > 1)              ; // add 7 to #1
        _ =     + + + + +    + + + + + (_ > 1)              ; // add 10 to #2
        _ =     + + +                  (_ > 1)              ; // add 3 to #3
        _ =     +                      (_ > 1)              ; // add 1 to #4

        _ =     -                      (_ < 1 < 1 < 1 < 1)  ; // decrement 0
    }
    _ =     ~   + +                    (_ > 1)              ; // write 'H'
    _ =     ~   +                      (_ > 1)              ; // write 'e'
    _ =     ~   + + + + +    + +        _                   ; //write 'l'
    _ =     ~                           _                   ; //write 'l'
    _ =     ~   + + +                   _                   ; // write 'o'
    _ =     ~   + +                     (_ > 1)             ; // write ' '
    _ =     ~   + + + + +    + + + + +   + + + + +      (_ < 1 < 1)     ; // write 'W'
    _ =     ~                           (_ > 1)             ; // write 'o'
    _ =     ~   + + +                   _               ; // write 'r'
    _ =     ~   - - - - -    -          _               ; // write 'l'
    _ =     ~   - - - - -    - - -      _               ; // write 'd'
    _ =     ~   +                       (_ > 1)         ; // write '!'
    _ =     ~                           (_ > 1)         ; // write '\n'
}
public class brainfuck
{
    private List<int> cells;
    private int currentCell;
    public brainfuck()
    {
        cells = new List<int>{0};
    }

    public static brainfuck operator + (brainfuck bf)
    {
        bf.plus();
        return bf;
    }
    public static brainfuck operator - (brainfuck bf)
    {
        bf.minus();
        return bf;
    }
    public static brainfuck operator > (brainfuck bf, int pos = 1)
    {
        bf.right();
        return bf;
    }
    public static brainfuck operator < (brainfuck bf, int pos = 1)
    {
        bf.left();
        return bf;
    }

    public static brainfuck operator ~ (brainfuck bf)
    {
        bf.write();
        return bf;
    }
    public static brainfuck operator ! (brainfuck bf)
    {
        bf.read();
        return bf;
    }
    public static implicit operator bool (brainfuck bf)
    {
        return bf.has_data();
    }

    private int plus()
    {
        return ++cells[currentCell];
    }
    private int minus()
    {
        return --cells[currentCell];
    }
    private void right()
    {
        if (currentCell + 1 == cells.Count)
            cells.Add(0);
        currentCell++;
    }
    private void left()
    {
        if (currentCell > 0)
            currentCell--;
    }
    private int current()
    {
        return cells[currentCell];
    }
    private void write()
    {
        Console.Write((char)current());
    }
    private void read()
    {
        var input = Console.ReadLine();
        int value = Int32.TryParse(input, out value) ? value : 0;
        current(value);
    }
    private bool has_data()
    {
        return current() > 0;
    }
    private void current(int value)
    {
        cells[currentCell] = value;
    }

    public override string ToString()
    {
        return String.Join(",",cells);
    }
}

I had to cheat with the [] command pair using the while statement and the flow is very odd from the point of Brainfuck reader, since the <,> commands are before the +,- in the original Hello World! example, and with C# program are in the opposite way.

Do you think I can get a closer version to Brainfuck than the current one?


texts: Python Pandas Dataframe code takes too long to finish

texts:
I have a Pandas DataFrame with users subscription dates in the following format:

UserId, StartingDate, EndingDate

And I try to calculate the Churn Rate metric for every day.

What Churn Rate is:

  The churn rate, also known as the rate of attrition, is the percentage of subscribers to a service who discontinue their subscriptions to that service within a given time period.

So, for every day, I go back 1 month, I get a list of unique users that had an active subscription and check how many of those don't have it any more.

I wrote the code, but it takes ages to finish, so I am looking for any kind of performance issues that might have

import pandas as pd
from datetime import datetime
from datetime import timedelta

df = pd.read_csv("subscritpions.csv")

#make sure both columns are in datetime type
df['StartingDate'] =  pd.to_datetime(df['StartingDate'])
df['EndingDate'] =  pd.to_datetime(df['EndingDate'])

#get the first date of the dataframe to start the loop with it and set the stop date as today
start = pd.to_datetime(df.StartingDate.min())
minDate = start
stop = datetime.now()

def getUsersFromADate(df,date):
    return df.loc[(df['StartingDate'] <= date) & (df['EndingDate'] >= date)].UserId.unique()

churn = []
while start <= stop:
    # first 30 days doesn't have a churn rate. So just append a 0 value
    if start < minDate + pd.DateOffset(months=1):
        churn.append(0)
    else:
        usersBefore = getUsersFromADate(df, start - pd.DateOffset(months=1))
        usersNow = getUsersFromADate(df, start)
        lost = 0
        for u in usersBefore:
            if u not in usersNow:
                lost += 1
        churn.append(lost/len(usersBefore))
    start = start + timedelta(days=1)  # increase day one by one

Example of my data:

   UserId StartingDate  EndingDate
0      1   2013-05-09  2015-04-24
1      1   2015-04-29  2017-04-02
2      1   2017-04-05  2017-12-06
3      2   2014-02-13  2018-02-07
4      3   2013-04-25  2018-04-19


texts:
I will import given code for my main QMainWindow.(I only add import sys etc. to run it)
Is there a more compact way to code this lines.
Output is correct for my expectation.

from PyQt5.QtWidgets import *
import sys

class ButtonWidget(QWidget):

    def __init__(self):
        super(ButtonWidget, self).__init__()

        # Function
        button1 = QRadioButton("Sinus")
        button2 = QRadioButton("Cosines")
        # Color
        button3 = QRadioButton("Red")
        button4 = QRadioButton("Green")
        # Line
        button5 = QRadioButton("Solid")
        button6 = QRadioButton("Dashed")
        # Left Group
        left_group = QGroupBox("Left Group")
        left_group_layout = QVBoxLayout()
        left_group_layout.addWidget(button1)
        left_group_layout.addWidget(button2)
        left_group.setLayout(left_group_layout)
        # Middle Group
        middle_group = QGroupBox("Middle Group")
        middle_group_layout = QVBoxLayout()
        middle_group_layout.addWidget(button3)
        middle_group_layout.addWidget(button4)
        middle_group.setLayout(middle_group_layout)
        # Right Group
        right_group = QGroupBox("Right Group")
        right_group_layout = QVBoxLayout()
        right_group_layout.addWidget(button5)
        right_group_layout.addWidget(button6)
        right_group.setLayout(right_group_layout)
        # Main Group
        main_group = QGroupBox("Main Group")
        main_group_layout = QHBoxLayout()
        main_group_layout.addWidget(left_group)
        main_group_layout.addWidget(middle_group)
        main_group_layout.addWidget(right_group)
        main_group.setLayout(main_group_layout)
        # Widget
        main_widget = QWidget()
        main_widget_layout = QVBoxLayout()
        main_widget.setLayout(main_widget_layout)
        main_widget_layout.addWidget(main_group)
        # Layout Set
        self.setLayout(main_widget_layout)

        self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui = ButtonWidget()
    sys.exit(app.exec_())


texts: Optimize insertion from ArrayList to HashMap

texts:
I'm trying to insert data from ArrayList to HashMap<String, Language> optimally.
Many items may have the same languge_name (code below), so I need to group items having the same language in Language class and store languages in a HashMap with the name of the language as a Key.
Item
String name;
String language_name;

Language
String language_name;
int numberItems;
LinkedList<String> Items;

I solved this as follows:
        ArrayList<Item> items; // given array of items
        HashMap<String, Language> languages = new HashMap<String, Language>();

        items.forEach(item -> {
            /** case 1: language isn't specified */
            if (item.getLanguageName() == null) {
                item.setLanguageName("unknown");
            }
            /** case 2: language already added */
            if (languages.containsKey(item.getLanguageName())) {
                languages.get(item.getLanguageName()).getItems().add(item.getName());
                languages.get(item.getLanguageName())
                        .setNumberItems(languages.get(item.getLanguageName()).getNumberItems() + 1);
            } else {
                /** case 3: language isn't added yet */
                LinkedList<String> languageItems = new LinkedList<String>();
                languageItems.add(item.getName());
                Language language = new Language(item.getLanguageName(), 1, languageItems);
                languages.put(item.getLanguageName(), language);
            }
        });

Any help would be appreciated!


texts: Particles moving around a Perlin Noise field

texts:
I made this sketch in Processing (basically Java) that consists of a bunch of particles moving around a Perlin Noise field. Each particle is aware of the position of one other particle and this creates a rope like effect. The sketch is intended to be aesthetic, like a screensaver, and I in that way it might be difficult to judge the efficiency of the code.

Nevertheless, I'm hoping that there is something that I'm doing wrong stylistically that could be pointed out.

    int emergenceInterval = int(random(75, 100));
    float a, freq = 0.05;
    float alphDelta, alphAccel, alphSpringing = 0.0009, alphDamping = 0.98, alphX, maxAlph;
    int pAmt = int(random(2000, 4000));
    Particle p[] = new Particle[pAmt];

    void setup() {
      background(0);
      size(1000, 500);
      for (int i = 0; i < pAmt; i++) {
        p[i] = new Particle(random(width), random(height), i);
      }
    }

    void draw() {
      if(frameCount % emergenceInterval == 0) {
       if(maxAlph == .05) maxAlph = 1;
       else maxAlph = .05;
      }
      a += freq;
      fill(50, findAlpha() * 255);
      rect(0, 0, width, height);
      runParticles();
    }

    void runParticles() {
      for (Particle _p : p) {
        Particle lastP = _p;
        if (_p.index != 0) lastP = p[_p.index - 1];
        _p.update(450, lastP.pos, abs(pow(sin(radians(a)), 3)));
        point(_p.pos.x, _p.pos.y);
        _p.vel.x = 1;
      }
    }

    float findAlpha() {
      if (!(alphAccel < 0)) alphDelta = width - alphX;
      else alphDelta = 0 - alphX;
      alphDelta *= alphSpringing;
      alphAccel += alphDelta;
      alphAccel *= alphDamping;
      alphX += alphAccel;
      float alph = (alphX * (maxAlph * abs(pow(sin(radians(a)), 2))))/width;

      if(alphX > width) alphAccel *= -1;
      else if(alphX < 0) alphAccel *= -1;
      return alph;
    }

  class Particle {

  int index;
  float theta;
  float stride;
  float maxSpeed = 5;
  float maxChaos = 10;
  PVector accel, vel, pos;

  Particle(float startX, float startY, int index) {
    this.index = index;
    accel = new PVector(0, 0);
    vel = new PVector(0, 0);
    pos = new PVector(startX, startY);
  }

  void update(float stride, PVector target, float chaos) {
    this.stride = stride;
    stroke(50*(noise(pos.x)),0,50*(noise(pos.x)), noise(pos.x, pos.y) * 255);
    accel = noiseDir(chaos);
    accel.add(followDir(target));
    if (pos.x > width) pos.x = 0;
    if (pos.y > height) pos.y = 0;
    if (pos.y < 0) pos.y = height;
    vel.add(accel);
    float speed = noise(pos.x, pos.y) * maxSpeed;
    vel.limit(speed);
    pos.add(vel);
  }

  PVector noiseDir(float chaos) {
    theta = abs(noise(pos.x, pos.y)) * 180;
    float x = pos.x + stride * cos(theta);
    float y = pos.y + stride * sin(theta);
    PVector dir = new PVector(x - pos.x, y - pos.y);
    dir.normalize();
    dir.mult(chaos * maxChaos);
    return dir;
  }

  PVector followDir(PVector target) {
    PVector dir = new PVector(target.x - pos.x, target.y - pos.y);
    dir.normalize();
    dir.mult(5);
    return dir;
  }
}


texts:
Here is my insertion sort algorithm:

Note: Insertion sort works by starting off with the first item in the list and then inserting the next item in the original list into the correct place in the sorted list and so on until all of the items from the original list are in the sorted list.

def insertion_sort(alist):
    if len(alist) <= 1:
        return alist

    sorted_list = insertion_sort(alist[:-1])
    for i, item in enumerate(sorted_list):
        if alist[-1] < item:
            break
    sorted_list.insert(i, alist[-1])
    return sorted_list

from random import randint as r
alist = [r(1,1000) for x in range(20)]
print(insertion_sort(alist))

Is there any way I could make this code more efficient by reducing the number of steps taken to sort the list? Moreover, is using a recursive function the most efficient way to do this type of sort?


texts: Crypt-arithmetic solver (Brute force) in Python

texts:
I'm working on a crypt-arithmetic solver to add two or more numbers. I've used brute-force to find all the permutations. I'm looking forward to improve this algorithm. Kindly review for all the aspects of this code and provide me your comments.

import string
import itertools
inListNumsAsStringArray = [ ['BASE', 'BALL'],
                            ['SEND', 'MORE'] ]
inResultsArray = [ 'GAMES',
                'MONEY' ]
inPossibleNumsAsStr = '0123456789'

# Input: (string, dictionary)
#   string 'AB'
#   key-value pairs for characters and numbers. Ex: dictCharAndDigit = {'A': '1', 'B': '2'}
# Output:(Number)
#   12
def getNumberFromStringAndMappingInfo(inStr, inDictMapping):
    numAsStr = ''
    for ch in inStr:
        numAsStr = numAsStr + inDictMapping[ch]
    return int(numAsStr)

def solveCryptarithmeticBruteForce(inListNumsAsString, inResultStr, inPossibleNumsAsStr):
    nonZeroLetters = []
    strFromStrList = ''
    for numStr in inListNumsAsString:
        nonZeroLetters.append(numStr[0])
        strFromStrList = strFromStrList + numStr
    nonZeroLetters.append(inResultStr[0])
    strFromStrList = strFromStrList + inResultStr
    uniqueStrs = ''.join(set(strFromStrList))
    for tup in itertools.permutations(inPossibleNumsAsStr, len(uniqueStrs)):
        dictCharAndDigit = {}
        for i in range(len(uniqueStrs)):
            dictCharAndDigit[uniqueStrs[i]] = tup[i]
        nonZeroLetterIsZero = False
        for letter in nonZeroLetters:
            if(dictCharAndDigit[letter] == '0'):
                nonZeroLetterIsZero = True
                break
        if(nonZeroLetterIsZero == True):
            continue
        result = getNumberFromStringAndMappingInfo(inResultStr, dictCharAndDigit)
        testResult = 0
        for numStr in inListNumsAsString:
            testResult = testResult + getNumberFromStringAndMappingInfo(numStr, dictCharAndDigit)

        if(testResult == result):
            strToPrint = ''
            for numStr in inListNumsAsString:
                strToPrint = strToPrint + numStr + '(' + str(getNumberFromStringAndMappingInfo(numStr, dictCharAndDigit)) + ')' + ' + '
            strToPrint = strToPrint[:-3]
            strToPrint = strToPrint + ' = ' + inResultStr + '(' + str(result) + ')'
            print(strToPrint)
            break

for i in range(len(inResultsArray)):
    solveCryptarithmeticBruteForce(inListNumsAsStringArray[i], inResultsArray[i], inPossibleNumsAsStr)


texts: Generic Implementation for Paging Class in c#

texts:
I have created one generic Page class for store paging information with its data.

The class look like:

public class Page<T>
     where T : new()
{

    private List<T> _list = new List<T>();

    [JsonProperty(PropertyName = "total_count")]
    public long TotalCount { get; set; }

    [JsonProperty(PropertyName = "page_start")]
    public long PageStart { get; set; }

    [JsonProperty(PropertyName = "page_size")]
    public long PageSize { get; set; }

    [JsonProperty(PropertyName = "list")]
    public List<T> List
    {
        get { return _list; }
        set { _list = value; }
    }
}

For every table model of my application uses generic Page class in repository for return PageSize and its total records with its List.

My repository use like below:

public class StudentRepository
{
    private readonly CountingCloud_DevContext _dbContext;
    public StudentRepository(CountingCloud_DevContext dbContext)
    {
        _dbContext = dbContext;
    }
    public Page<StudentResponse> Get(Dictionary<string, object> filterParameters)
    {
        var resultfinal = new Page<StudentResponse>();
        int from = 0, size = 10;
        resultfinal = Paging(resultfinal, filterParameters, ref from, ref size);

        var query = _dbContext.Students.ToList();
        var result = query.Select(x => new StudentResponse
        {
            Id = x.Id,
            Name = x.Name
        });
        resultfinal.TotalCount = result.Count();
        query = result.Skip(from).Take(size);
        return resultfinal;
    }

    private static Page<StudentResponse> Paging(Page<StudentResponse> pagingResponse, Dictionary<string, object> parameters, ref int from, ref int size)
    {
        if (parameters != null)
        {
            if (parameters.ContainsKey("PageSize") && parameters.ContainsKey("PageStart"))
            {
                if (Convert.ToInt32(parameters["PageSize"]) != 0)
                {
                    size = Convert.ToInt32(parameters["PageSize"]);
                    from = (Convert.ToInt32(parameters["PageStart"]) - 1) * size;
                    pagingResponse.PageSize = size;
                    pagingResponse.PageStart = Convert.ToInt32(parameters["PageStart"]);
                }
            }
            else
            {
                pagingResponse.PageSize = size;
                pagingResponse.PageStart = from + 1;
            }
        }
        else
        {
            pagingResponse.PageSize = size;
            pagingResponse.PageStart = from + 1;
        }
        return pagingResponse;
    }
}

Currently Paging() method I have use for all table like my other Repository is for Employee then for that paging method is look like below:

private static Page<EmployeeResponse> Paging(Page<StudentResponse> pagingResponse, Dictionary<string, object> parameters, ref int from, ref int size)
{
    if (parameters != null)
    {
        if (parameters.ContainsKey("PageSize") && parameters.ContainsKey("PageStart"))
        {
            if (Convert.ToInt32(parameters["PageSize"]) != 0)
            {
                size = Convert.ToInt32(parameters["PageSize"]);
                from = (Convert.ToInt32(parameters["PageStart"]) - 1) * size;
                pagingResponse.PageSize = size;
                pagingResponse.PageStart = Convert.ToInt32(parameters["PageStart"]);
            }
        }
        else
        {
            pagingResponse.PageSize = size;
            pagingResponse.PageStart = from + 1;
        }
    }
    else
    {
      pagingResponse.PageSize = size;
        pagingResponse.PageStart = from + 1;
    }
    return pagingResponse;
}

I would like make this private method is generic so all repository use same,
please suggest me what I am implemented is correct or any better improvement for code any changes.


texts: OleDbContext to keep OleDbConnection alive

texts:
I'm writing some automated tests that read and write data from/to excel (so end-users can define their test cases themselves). I use dotTratce to optimize the algorithms and found that the worst bottleneck was writing to excel. It turns out that OleDb does not have connection pooling and opening/disposing it takes a lot of time. Even with only 200 writes it sums up to 80 seconds (opening 40sec and dispose 40sec). To fix this I wrote this simple OleDbContext that I pass to methods that write to excel so that they don't open/close a connection for each write.

public class OleDbContext : IDisposable
{
    private OleDbConnection _connection;

    public OleDbContext(string fileName)
    {
        FileName = fileName ?? throw new ArgumentNullException(nameof(fileName));
    }

    public string FileName { get; }

    public void Connect()
    {
        if (_connection == null)
        {
            _connection = new OleDbConnection(CreateConnectionString(FileName));
            _connection.Open();
        }
    }

    public int Execute(Func<OleDbConnection, int> query)
    {
        if (_connection == null) { throw new InvalidOperationException("There is no open connection yet. You need to connect first."); }
        return query(_connection);
    }

    private static string CreateConnectionString(string fileName)
    {
        return $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={fileName};Extended Properties='Excel 12.0 Xml;HDR=No;'";
    }

    public void Dispose()
    {
        if (_connection != null)
        {
            _connection.Dispose();
            _connection = null;
        }
    }
}

Usage example:

public int Write(string text, string columnLetter, int rowNumber, OleDbContext oleDbContext)
{
    return oleDbContext.Execute(conn =>
    {
        var range = $"{columnLetter}{rowNumber}";
        using (var update = new OleDbCommand($"UPDATE [{SheetName}${range}:{range}] SET [F1]='{text}'", conn))
        {
            return update.ExecuteNonQuery();
        }
    });
}

With this improvement the execution time dropped to under 2 seconds which is very good. I was just wondering if there's more I can improve - performance wise I guess I did what is technically possible so how about other things?