texts: Cleaning and extracting meaningful text from tweets

texts:
I have a dataset of around 200,000 tweets. I am running a classification task on them. Dataset has two columns - class label and the tweet text. In the preprocessing step I am passing the dataset through following cleaning step:

import re
from nltk.corpus import stopwords
import pandas as pd

def preprocess(raw_text):

    # keep only words
    letters_only_text = re.sub("[^a-zA-Z]", " ", raw_text)

    # convert to lower case and split
    words = letters_only_text.lower().split()

    # remove stopwords
    stopword_set = set(stopwords.words("english"))
    meaningful_words = [w for w in words if w not in stopword_set]

    # join the cleaned words in a list
    cleaned_word_list = " ".join(meaningful_words)

    return cleaned_word_list

def process_data(dataset):
    tweets_df = pd.read_csv(dataset,delimiter='|',header=None)

    num_tweets = tweets_df.shape[0]
    print("Total tweets: " + str(num_tweets))

    cleaned_tweets = []
    print("Beginning processing of tweets at: " + str(datetime.now()))

    for i in range(num_tweets):
        cleaned_tweet = preprocess(tweets_df.iloc[i][1])
        cleaned_tweets.append(cleaned_tweet)
        if(i % 10000 == 0):
            print(str(i) + " tweets processed")

    print("Finished processing of tweets at: " + str(datetime.now()))
    return cleaned_tweets

cleaned_data = process_data("tweets.csv)

And here is the relevant output:

Total tweets: 216041
Beginning processing of tweets at: 2017-05-16 13:45:47.183113
Finished processing of tweets at: 2017-05-16 13:47:01.436338

It's taking approx. 2 minutes to process the tweets. Although it looks relatively a small timeframe for current dataset I would like to improve it further especially when I use a dataset of much bigger size.

Can the steps/code in the preprocess(raw_text) method be improved in order to achieve faster execution?


texts:
I attempted a solution to a seemingly simple problem:

  Peak Finder

  Let input be a list. input[i] is a peak if input[i] > input[i + 1] and input[i] > input[i - 1].

  Implement a recursive algorithm of time complexity \$O(\log n)\$ to find a peak if it exists.

def peakFinder(input):
        n = len(input)
        middleIndex = n/2
        middleIsMax = input[middleIndex] > input[middleIndex + 1] and input[middleIndex] > input[middleIndex - 1]

        # base case
        if middleIsMax:
            return input[middleIndex]

        leftArray = input[:middleIndex]
        rightArray = input[middleIndex:]

        moveRight = input[middleIndex + 1] > input[middleIndex] and input[middleIndex + 1] > input[middleIndex - 1]

        moveLeft = input[middleIndex - 1] > input[middleIndex] and input[middleIndex - 1] > input[middleIndex + 1]

        # recursive case
        if moveRight:
            return peakFinder(rightArray)
        elif moveLeft:
            return peakFinder(leftArray)

    print peakFinder([2, 41, 17, 11, 13, 7])

Are there improvements to be made? Is the time complexity \$O(\log n)\$?


texts: Improving and optimizing Tic-Tac-Toe game written in Python 3

texts:
I've been teaching myself Python3 for about a week now and I decided to put my skills to the test by writing a simple Tic-Tac-Toe game. Here is what I came up with:

#!/usr/bin/env python3

import random

board = [0]*9

def resetBoard():
    for index, i in enumerate(board):
        board[index] = 0

def printBoard():
    counter = 0
    s = ""
    print("|-----|-----|-----|")
    for index, i in enumerate(board):
        s += "|  "
        if(board[index] == 0):
            s += str(index)
        if(board[index] == 1):
            s+= "x"
        if(board[index] == 2):
            s += "o"
        s += "  "
        counter += 1
        if(counter == 3):
            s += "|"
            print(s)
            print("|-----|-----|-----|")
            s = ""
            counter = 0

def getPlayers():
    return input("1 or 2 Players?")

def moveSquare(xOro, ai):
    square = 0
    flag = 0
    while(board[square] != 0 or flag == 0):
        if(ai == 0):
            if(xOro == 1):
                square = eval(input("Choose a square to place X: "))
            if(xOro == 2):
                square = eval(input("Choose a square to place O: "))
        if(ai == 1):
            square = random.randint(0, 8)
        flag = 1
    board[square] = xOro
    print("\n\n")

def checkForWin():
    if(board[0] == board[1] and board[1] == board[2] and board[2] != 0):
        return 1
    if(board[3] == board[4] and board[4] == board[5] and board[5] != 0):
        return 1
    if(board[6] == board[7] and board[7] == board[8] and board[8] != 0):
        return 1
    if(board[0] == board[3] and board[3] == board[6] and board[6] != 0):
        return 1
    if(board[1] == board[4] and board[4] == board[7] and board[7] != 0):
        return 1
    if(board[2] == board[5] and board[5] == board[8] and board[8] != 0):
        return 1
    if(board[0] == board[4] and board[4] == board[8] and board[8] != 0):
        return 1
    if(board[2] == board[4] and board[4] == board[6] and board[6] != 0):
        return 1
    for index, i in enumerate(board):
        if(i  == 0):
            break
        if(index == 8):
            return 2
    return 0

def gameOver():
    return eval(input("\nPress 0 and then ENTER to play again!"))

# Main
random.seed()
quit = 0
while(quit == 0):
    players = 0
    win = 0
    while(players != 1 and players != 2):
        players = eval(getPlayers())
    resetBoard()
    game = 0 #game = 1 when game is finished
    while(game == 0): #game loop
        printBoard()
        moveSquare(1, 0)
        game = checkForWin()
        if(game > 0):
            win = game
            break
        printBoard()
        if(players == 1):
            moveSquare(2, 1)
        if(players == 2):
            moveSquare(2, 0)
        game = checkForWin()
        if(game > 0):
            win = game + 1
            break
    if(win == 1):
        print("\nX Wins!")
    if(win == 2):
        print("\nO Wins!")
    if(win == 3):
        print("\n The game is a draw!")
    quit = gameOver()
exit()

I am interested in the following points about my code:

How can I make my code smaller and more optimized?
The game displays "O Wins" instead of "The game is a draw!" and I'm not sure why.
How can I sanitize input to prevent crashes if a string is entered instead of an integer (for example when choosing a square on the board)?
Any additional points to improve my code or general tips that will help me become a better Python programmer.


texts:
The following code detects if the system is idling. It approaches the problem by using the Robot class to take a screenshot, waits for a while, and then takes another screenshot. It then compares screenshot 1 with screenshot 2. If  a certain amount of change is detected, it presumes the system is idling, else the system is active.

Please suggest code changes or even a different solution!

package base;

import java.awt.AWTException;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

public class CheckIdle extends Thread {
    private Robot robot;
    private double threshHold = 0.05;
    private int activeTime;
    private int idleTime;
    private boolean idle;
    private Rectangle screenDimenstions;

    public CheckIdle(int activeTime, int idleTime) {
        this.activeTime = activeTime;
        this.idleTime = idleTime;

        // Get the screen dimensions
        // MultiMonitor support.
        int screenWidth = 0;
        int screenHeight = 0;

        GraphicsEnvironment graphicsEnv = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
        GraphicsDevice[] graphicsDevices = graphicsEnv.getScreenDevices();

        for (GraphicsDevice screens : graphicsDevices) {
            DisplayMode mode = screens.getDisplayMode();
            screenWidth += mode.getWidth();

            if (mode.getHeight() > screenHeight) {
                screenHeight = mode.getHeight();
            }
        }

        screenDimenstions = new Rectangle(0, 0, screenWidth, screenHeight);

        // setup the robot.
        robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e1) {
            e1.printStackTrace();
        }

        idle = false;
    }

    public void run() {
        while (true) {
            BufferedImage screenShot = robot
                    .createScreenCapture(screenDimenstions);

            try {
                Thread.sleep(idle ? idleTime : activeTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            BufferedImage screenShot2 = robot
                    .createScreenCapture(screenDimenstions);

            if (compareScreens(screenShot, screenShot2) < threshHold) {
                idle = true;
            } else {
                idle = false;
            }
        }
    }

    private double compareScreens(BufferedImage screen1, BufferedImage screen2) {
        int counter = 0;
        boolean changed = false;

        // Count the amount of change.
        for (int i = 0; i < screen1.getWidth() && !changed; i++) {
            for (int j = 0; j < screen1.getHeight(); j++) {
                if (screen1.getRGB(i, j) != screen2.getRGB(i, j)) {
                    counter++;
                }
            }
        }

        return (double) counter
                / (double) (screen1.getHeight() * screen1.getWidth()) * 100;
    }

    public static void main(String[] args) {
        CheckIdle idleChecker = new CheckIdle(20000, 1000);
        idleChecker.run();
    }
}


texts: Python retry logic based on return code and output

texts:
I am trying to write a function which execute the given command with given number of retry and delay. If the return code is 0 and output string have expected_output(partial) have to break the loop and return the return code and output. Is there any better to write this code?
cmd check_output_and_retry(cmd, expected_output, delay=1, retry=3):
    for i in range(retry):
        # Run the given command.
        # Return value will be return code with cmd output.
        ret, output = execute_cmd(cmd)
        if ret == 0 and expected_out in output:
            break
        else:
            time.sleep(delay)
    return ret, output


texts: VBA Loop through multiple sheets to output formulas in multiple columns

texts:
I write decently sloppy code and piecemeal together various things I find online to help me accomplish what needs to be done. This loop goes through 4 different worksheets to paste a formula in 3 columns. Based on some previous feedback from the site, I removed all of my .Select references and hopefully did things more cleanly. 2 questions that I have:

1) Can anyone think of a way I can further optimize this to have it run more quickly?

2) Is there a better way to define my RRSData!A2:O10000 formula.  I just chose 10,000 because there shouldn't be that many in the future, but its obviously working harder than it needs to now since there are only ~1000 rows currently.

Dim LastRow1 As Long, i1 As Long, InxW As Long
Dim wsNames As Variant

wsNames = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")

For InxW = LBound(wsNames) To UBound(wsNames)
   With Worksheets(wsNames(InxW))
        LastRow = Cells(Rows.Count, "B").End(xlUp).Row
        For i1 = 25 To LastRow
            With Range("KA1").Offset(i1 - 1, 0)
                .Formula = "=IF(SUMIF(RRSData!$A$2:$O$10000,OFFSET($KA$1,CELL(""row"",THIS),-285),RRSData!$M$2:$M$10000)>0,SUMIF(RRSData!$A$2:$O$10000,OFFSET($KA$1,CELL(""row"",THIS),-285),RRSData!$M$2:$M$10000),"""")"
                .NumberFormat = "0.00"
                .Value = .Value
            End With
            With Range("KB1").Offset(i1 - 1, 0)
                .FormulaR1C1 = "=IF(RC[-1]="""","""",If(RC[-1]>1.1,""High"",If(RC[-1]<0.8,""Low"",""Neutral"")))"
                .Value = .Value
            End With
            With Range("KC1").Offset(i1 - 1, 0)
                .FormulaR1C1 = "=IF(RC[-2]="""","""",IF(RC[-2]>1.1,""+5%"",IF(RC[-2]<0.8,""-5%"",""0%"")))"
                .Value = .Value
            End With
        Next i1
    End With
Next InxW

The code works, but it takes probably 2 minutes to loop through the four sheets, and the total number of rows across the four sheets is <500


texts: First attempt at writing Rock Paper Scissors game

texts:
I made this Rock Paper Scissors game and I would like for this to be critiqued.

import System.IO
import System.Random
import Control.Monad
import Control.Arrow

data Throw = Rock | Paper | Scissors
           deriving (Show, Read, Enum, Bounded)

instance Random Throw where
  random g = first toEnum $ randomR (a, b) g
    where a = fromEnum (minBound :: Throw)
          b = fromEnum (maxBound :: Throw)

  randomR (a, b) g = first toEnum $ randomR (a', b') g
    where a' = fromEnum a
          b' = fromEnum b

beats :: Throw -> Throw -> Bool
Rock     `beats` Scissors = True
Paper    `beats` Rock     = True
Scissors `beats` Paper    = True
_        `beats` _        = False

play :: Throw -> Throw -> String
play p1 p2
  | p1 `beats` p2 = "You win!"
  | p2 `beats` p1 = "You lose."
  | otherwise     = "Tie."

main :: IO ()
main = do
  putStr "Enter your move [Rock, Paper, or Scissors]: "
  hFlush stdout
  p1 <- getLine >>= readIO
  p2 <- liftM (fst . random) getStdGen
  putStrLn $ show p1 ++ " vs. " ++ show p2
  putStrLn $ play p1 p2


texts:
I'm having some questions on creating a design for simple approval workflow. Some arbitrary thing (ItemToApprove) needs approval process. Approval process begins with creating ApprovalRoute which contains several approval steps (ApprovalStep). ItemToApprove goes sequentially through all steps until gets status Approved. On each step there are several Approvers with equal priority. If one approver approves item, step becomes Approved.

public class ApprovalRoute
{
    private LinkedList<ApprovalStep> _approvalSteps;
    private ApprovalStep _currenStep;

    public bool? Approved { get; private set; }

    public void Approve(Approver approver)
    {
        // ... some cheks if already approved etc
        _currentStep.Approve(approver);

        if (_currentStep.Approved == true)
        {
            MoveToNextStep(); // changes _currentStep
        }

        if (AllStepsApproved()) {
           Approved = true;
        }
    }
}

public class ApprovalStep
{
    public bool? Approved { get; private set; }
    private List<Approver> _approvers;

    public ApprovalStep(List<Approver> approvers)
    {
        _approvers = approvers;
    }

    public void Approve(Approver approver)
    {
        if (_approvers.Contains(approver))
        {
            Approved = true;
        }
    }
}

public class ItemToApprove
{
    public bool? Approved { get; private set; }
    private ApprovalRoute _approvalRoute;

    public void StartApprovalWorkflow(ApprovalRoute route)
    {
        _approvalRoute = route;
    }

    public void Approve(Approver approver)
    {
        _approvalRoute.Approve(approver);
        if (_approvalRoute.Approved == true)
        {
            Approved = true;
        }
    }
}

public class Approver
{
    public string Name { get; private set; }
    public Approver(string name)
    {
        Name = name;
    }

    // ... ovveriding Equals etc
}

So some console client would be like this

      var itemToApprove = new ItemToApprove();
      var aprovalRoute = new ApprovalRoute(new ApprovalStep(new List<Approver>
      {
          new Approver("One"),
          new Approver("Neo"),
      });
      itemToApprove.StartApprovalWorkflow(aprovalRoute);

      while (itemToApprove.Approved == null)
      {
          Console.Write("Your name: ");

          string approverName = Console.ReadLine();
          itemToApprove.Approve(new Approver(approverName));

      }

I thik this design has some smell because calling Approve delegates further down to ApprovalStep. The second smell I feel more naturally call Approve on Approver passing ItemToApprove

// ....
var approver = new Approver();
approver.Approve(itemToApprove);

What do you think?


texts:
I rarely see this kind of navigating on the web.  But basically I load all the content in the background and then flip pages as needed while loading user specific data via ajax.

Are there any problems with this and in general?

/***********************************************************************************************************************
    *Page - flips pages in the app and also updates the Menu
    */
        var Page = $A.Class.create('public', {
            Name: 'Page',
            E: {

                // body elements
                body_splash:       '#body_splash',
                body_fave:         '#body_fave',
                body_feed:         '#body_feed',
                body_settings:     '#body_settings',
                body_fave_custom:  '#body_fave_custom',

                // menu elements
                nav_splash:         '#nav_splash',
                nav_fave:           '#nav_fave',
                nav_feed:           '#nav_feed',
                nav_settings:       '#nav_settings',
                nav_fave_custom:    '#nav_fave_custom'
            },
            P: {
                page_previous:   null,
                input_previous:  'splash'
            },

            // don't flip the page to fast or the inputs won't reset
            flip: function (input) {
                var self = this;
                input = input || 'splash';
                Backbone.trigger(input + '_flipped');
                Backbone.trigger(self.P.input_previous + '_flipped_from');

                // give page time to reset before flipping
                setTimeout(function() {
                  self.flipTimed(input)
                }, 100);
            },
            flipTimed : function (input) {
                var page_current,
                    nav_current;

                page_current =  this.E['body_' + input];
                nav_current =  this.E['nav_' + input];

                // check the timing on this
                Storage.set('page', input);

                // turn off previous page
                if (this.P.page_previous !== null && this.P.page_previous !== page_current) {
                    this.P.page_previous.style.opacity = 0;
                    this.P.page_previous.style.display = "none";
                }

                // turn on current page
                if (this.P.page_previous !== page_current) {
                    $A.animate(page_current);
                    page_current.style.display = "inline-block";
                }

                // update menu
                Menu.toggleMarker(nav_current);

                // update previous
                this.P.page_previous = page_current;
                this.P.input_previous = input;

            }
        });


texts:
I need to parse a JSON generated by iTunes Search API. I'm looking for all apps by a certain developer, and I usually get a response like this (simplified):

{
    "resultCount":4,
    "results": [
                   {
                       "wrapperType":"artist",
                       "artistType":"Software Artist",
                       "artistName":"Vladimir Kelin"
                   },
                   {
                       "wrapperType":"software",
                       "formattedPrice":"Free",
                       "trackId":933241656
                   },
                   {
                       "wrapperType":"software",
                       "formattedPrice":"$1.99",
                       "trackId":1175807581
                   }
                ]
}

As you can see, the results array is heterogenous, meaning it contains objects of 2 different types: artist and software.

I created a structure, implementing a new handy Codable protocol:

struct App: Codable {
    let trackId: Int
    let formattedPrice: String?

    /// - Parameter jsonDict: A dictionary produced by JSONSerialization
    init?(jsonDict: [String: Any]) {
        guard let trackId = jsonDict["trackId"] as? Int else { return nil }
        self.trackId = trackId

        formattedPrice = jsonDict["formattedPrice"] as? String
    }
}

I don't want to parse artist data, so I didn't created any structures for that.

It's relatively easy to parse such JSON using JSONSerialization:

do {
    let root = try unwrap(JSONSerialization.jsonObject(with: receivedData, options: []) as? [String: Any])
    let results = try unwrap(root["results"] as? [[String: Any]])
    var apps = [App]()
    for result in results {
        // Check "software" or "artist"
        guard result["wrapperType"] as? String == "software" else { continue }
        guard let app = App(jsonDict: result) else { continue }
        apps.append(app)
    }
    completion(.ok, apps)
} catch let e {
    let dataStr = String(bytes: receivedData, encoding: .utf8) ?? ""
    print("Exception: \"\(e)\", while parsing data: \"\(dataStr)\"")
    completion(.parseError, nil)
}

Do you see the unwrap() function for the first time?

The Problem. I'm looking for a way to parse the response using wonderful JSONDecoder class introduced in Swift 4. If the results array were homogenous I would be able to parse it in just few strings of code.

// Helper struct
struct TunesResponse: Codable {
    let resultCount: Int
    let results: [App]
}

// Parsing
let jsonDecoder = JSONDecoder()
if let parsedResponse = jsonDecoder.decode(TunesResponse.self, from: receivedData) {
    let apps = parsedResponse.results
    completion(.ok, apps)
} else {
    completion(.parseError, nil)
}

In practice I spend a lot of time but didn't found any way to apply JSONDecoder for such kind of response.


texts:
I was recently given a coding test by an employer. After the test, this was the employer's response:

  "I’d say that a lot of his solutions were pretty inefficient and took
  a very ‘brute force’ approach to a solution. For example, his solution
  for  question 1 took 30 lines (give or take for some spacing). This is
  normally done in about 8 lines. I did like that he documented all of
  his answers very well,  so that’s a positive, but yeah, just a lot of
  inefficiency throughout his answers  (processing inefficiency, object
  allocation inefficiency, etc)"

I know this question cannot be answered well without an example so to give an example of question 1:

  Write a method that takes a parameter x and then prints out an x by x multiplication table

This is what I wrote:

public void printMultTable(int x)
{
    int totalNumberOfPrints = x * x;
    int rowCounter = 0;
    int columnCounter = 1;
    int number = 0;

    for(int i = 0; i < totalNumberOfPrints; i++)
    {
        if(i < x)
        {
            number = i + 1;
            System.out.print(number + " ");
        }
        else if (i % x == 0)
        {
            columnCounter = columnCounter + 1;
            rowCounter = 2;
            System.out.println();
            System.out.print(columnCounter + " ");
        }
        else
        {
            number = columnCounter * rowCounter;
            System.out.print(number + " ");
            rowCounter = rowCounter + 1;
        }
    }
}

I am experienced, but I read and was told by previous employers that code that is easier to read is better, than complex code that takes only 3 lines. I could have written the code in less lines but I thought this was the way.

So my questions are:

What is processing inefficiency and how is that shown in my code?
What is object allocation inefficiency(I didn't use any static variables)
Is it better to use less lines (I thought it doesn't matter?)


texts:
I have a data table and have to validate every field in it. I have refactor this code to this below, but the complexity is 15(!!)  Should I make something like dictionary with type as Key and Func as Value?

private bool CheckField(DataRow dataRow, ValidationField validationField)
        {
            bool result = false;
            if (validationField.Requiered)
            {
                if (validationField.Type == typeof (int))
                {
                    result = this.CheckIntegerAndNotNull(dataRow[validationField.Name].ToString());
                }
                else if (validationField.Type == typeof (DateTime))
                {
                    result = this.CheckDateTimeAndNotNull(dataRow[validationField.Name].ToString());
                }
            }
            else
            {
                if (validationField.Type == typeof (int))
                {
                    result = this.CheckIntegerOrNull(dataRow[validationField.Name].ToString());
                }
                else if (validationField.Type == typeof(DateTime))
                {
                    result = this.CheckDateTimeOrNull(dataRow[validationField.Name].ToString());
                }
                else if (validationField.Type == typeof(string))
                {
                    result = this.CheckStringOrNull(dataRow[validationField.Name].ToString(),
                        validationField.MaxLength.Value);
                }
                else if (validationField.Type == typeof(decimal))
                {
                    result = this.CheckDecimalOrNull(dataRow[validationField.Name].ToString());
                }
            }
            return result;
        }

 public class ValidationField
    {
        public Type Type { get; set; }
        public string Name { get; set; }
        public bool Requiered  { get; set; }
        public int? MaxLength { get; set; }
    }


texts:
I'm not a Python developper, but I enjoy programming with it, and for a project I wanted to have generators that I can easily index. Using python's slice model is obviously the way to go, and here's the solution I've come up with.

class _SubscriptableGenerator():
    def __init__(self, generator, *args):
        self.gen = generator(*args)

    def __getitem__(self, key):
        try:
            if isinstance(key, int):
                self.ignore(key)
                yield next(self.gen)
            else:
                step = key.step if key.step else 1
                start = key.start if key.start else 0

                i = start
                self.ignore(start)

                while i < key.stop:
                    yield next(self.gen)
                    i = i + step
                    self.ignore(step-1)
        except Exception:
            self.raiseInvalidSlice(key)

    def raiseInvalidSlice(self, key):
        raise KeyError("{0} is not a valid key (only int and [x:y:z] slices are implemented.".format(key))

    def ignore(self, n):
        for i in range(n):
            next(self.gen)

It is not intended to be called by the user of the module, that's internal code. I for example define my generators like so

def _myGen(arg1, arg2):
    while 1:
        yield something

and provide them wrapped in my class

def myGen(*args):
    return _SubscriptableGenerator(_myGen, *args)

I'd like to know what a more pythonic solution would be, if there are things to fix, etc. I am not sure about the way to handle exceptions on the key.


texts: Internal method to calculate the distance between all nodes in Kruskal's Importance (Driver) Algorithm

texts:
I've been trying to reduce the indentation from a method in my code. The solution will probably be unrelated to the language, in this case python, although it may rely on some python specific libraries e.g. itertools.

The code below is part of this implementation of Kruskal's algorithm.

def generate_diff(self, ndarr, arr):
    """
    Internal method to calculate the difference between all points
    """
    l = ndarr.shape[1]
    pij = np.empty((l,l,)) * np.nan
    pijm = np.empty((l,l,l)) * np.nan
    for i in range(l):
        for j in range(l):
            if i != j:
                pij[i, j] = self.pcor_squared(np.array([ndarr[:,i], arr, ndarr[:,j]]))
                for m in range(l):
                    if m != i and m != j and m > j:
                        pijm[m, i, j] = self.pcor_squared(np.array([ndarr[:,i], arr, ndarr[:,j], ndarr[:, m]]))
    return (l, pij, pijm)

As you can see it's looping through the length of the shape 3 times! (in creating i, j, m).

ndarr and arr are just a two dimensional numpy array and a one dimensional numpy array. The specs in the file will enable you to hit the method very quickly if you add an assert False and a run py.test --pdb from the command.

I've tried changing the first line to for i, j in it.combinations(range(l), 2): and doing both sides of the square of the matrix simulataneously with:

pij[i, j] = self.pcor_squared(np.array([ndarr[:,i], arr, ndarr[:,j]]))
pij[j, i] = self.pcor_squared(np.array([ndarr[:,j], arr, ndarr[:,i]]))

but it's a bit off (i.e. gives wrong numbers in the spec).

In short I'm trying to reduce the complexity by making the code linear and hopefully making it more performant by removing the looping. A better understanding of matrices may be key to this.


texts: PowerShell - Loop Through Nested Objects

texts:
I'm writing a PowerShell script to loop through an organization's PowerBI workspaces and spit out the report IDs, names, and URLs for each workspace - so, a nested loop.

Here's my working script thus far:

Install-Module -Name MicrosoftPowerBIMgmt

$myArray = @()

Login-PowerBIServiceAccount
Get-PowerBIWorkspace -Scope Organization | ForEach-Object {
        $ws = $_
        Get-PowerBIReport -WorkspaceId $_.Id | ForEach-Object {
            $x = [pscustomobject]@{
                workspaceId = $ws.Id
                workspaceName = $ws.Name
                reportId = $_.id
                reportName = $_.Name
                reportUrl = $_.WebUrl
             }
        $myArray += $x
        }
}

$myArray | Export-Csv "PowerBIWorkspaceReports.csv"

Four questions:

1) Is there a better way to add the module dependency at the top? I can't find any info on how PowerBI looks for existing module dependencies.

2) Are the ForEach-Object calls redundant? I see in simple code people assume whatever command runs after the pipe runs just fine whether on a single object or a collection of objects.

3) Is there a better way to manage my objects? Since I need data from the first loop ( the Workspace name and ID) and the second loop, is there an easier way to join them (or export them as a flattened list) than looping?

4) Should I just append my rows directly to the CSV? There doesn't seem to be the equivalent of While(File.Open() ...) in PowerShell.

And any other advice appreciated.


texts: Reviewing the Dockerfile and docker-compose.yml files for a basic nodejs mysql project

texts:
I'm new to docker and after much research and stufy I've created a sample application and would like it to be reviewed in case if I'm missing any corner or if there's redundant information that can be simplified.

Dockerfile

FROM node

WORKDIR /src
COPY . .
RUN npm install

CMD ["npm", "start"]

docker-compose.yml

MySQL is getting initialized through schema.sql

version: '3'

services:
  database:
    image: "mysql:5.7"
    container_name: "mysql"
    ports:
      - "6603:3306"
    volumes:
      - ./schema.sql:/docker-entrypoint-initdb.d/init.sql
    environment:
      DATABASE_HOST: database
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: test
      MYSQL_USER: mysql
      MYSQL_PASSWORD: password

  service:
    build: .
    image: "node"
    container_name: "nodejs"
    ports:
      - "8080:8080"
    depends_on:
      - database
    links:
      - database
    environment:
      DATABASE_HOST: database
      MYSQL_PORT: 3306
      MYSQL_DATABASE: test
      MYSQL_USER: mysql
      MYSQL_PASSWORD: password
    restart: on-failure

index.js

It has some delay, that basically waits for the mysql to become fully alive.

setTimeout(function(){
    var mysql = require('mysql');

    var con = mysql.createConnection({
        host: process.env.DATABASE_HOST,
        user: process.env.MYSQL_USER,
        password: process.env.MYSQL_PASSWORD,
        database: process.env.MYSQL_DATABASE
    });

    con.connect(function(err) {
        if (err) throw err;
        console.log("Database Connected!");

        var http = require('http');

        //create a server object:
        http.createServer(function (req, res) {
            res.write('Hello World!'); //write a response to the client
            res.end(); //end the response
        }).listen(8080); //the server object listens on port 8080

        console.log("Listening at 8080");
    });
}, 10 * 1000);

I had one file in docker-compose.yml which I removed assuming it's not required.

#    volumes:
#      - .:/src


texts:
I have some code that performs (nearly) the opposite function of a pmovmskb.

It takes as input a 16-bit bitfield and expands it into 2 xmm registers.
(I'm limited to working with AVX1 code).

The code I've come up with is:

const SliverToSliceMask = 0x0001-0002-0004-0008-0010-0020-0040-0080

movdqu xmm14,[rip+SliverToSliceMask]    ;xmm14 = mask to isolate bits 0..7
movdqa xmm15,xmm14                      ;xmm15 = mask
pslldq xmm15,1                          ;convert mask from bit 0..7 to bit 8..15
and edx,r8d                             ;combine the two slivers
  //Now expand the sliver into a slice
movd xmm0, edx              ;xmm0 = sliver
pshuflw xmm0,xmm0,$00       ;Paste the sliver 4 times (expanding it to 8 bytes).
pshufd xmm0, xmm0, $50     ;Paste the sliver 2 more times (expanding it to 16 bytes)
movdqa xmm1,xmm0           ;make a copy,expanding it to 32 bytes (in 2 regs)
//now xmm0 and xmm1 are filled with 16 copies of the input sliver, mask out the bit
//that is intended per word
pand xmm0,xmm14             ;mask off the words for bit 0..7
pand xmm1,xmm15             ;mask off the words for bit 8..15
pcmpeqw xmm0,xmm14          ;set 1's to $FFFF and 0's to 0
pcmpeqw xmm1,xmm15          ;Thus expanding the sliver (1 bit per outer sliver)
                            ;to a slice (16 bits per sliver)

Is there a way to optimize this code. I would esp. like to get rid of the memory access to fetch the mask.


texts: Text file manipulation with Java 8 and Guava

texts:
I am trying to do some translation of text files. I have a translation .csv file and a file I need to translate (tab delimited). The translation is creating a file with an extra column of the translation.

I'd like to hear any suggestions on this.

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TextFileManipulationExample {

  public static void main(String[] args) {
    new TextFileManipulationExample().translate();
  }

  private void translate() {
    List<String> translationFileContent = readFile("dictionary.csv");
    Stream<List<String>> translationLines = translationFileContent.stream().map(
      line -> Splitter.on(",").splitToList(line));
    Map<List<String>, String> translationMap = translationLines.collect(Collectors.toMap(l -> Lists.newArrayList(l.subList(0, 2)), l -> l.get(2)));
    List<String> toTranslateLines = readFile("file_to_translate.txt");
    Stream<List<String>> listWithTranslation = toTranslateLines.stream().map(line -> {
      List<String> list = Lists.newArrayList(Splitter.on("\t").splitToList(line));
      if (translationMap.containsKey(list)) {
        list.add(translationMap.get(list));
      }
      return list;
    });
    String contentToWrite = listWithTranslation.map(lineList -> Joiner.on(",").join(lineList)).collect(
      Collectors.toList()).stream().reduce((t, u) -> t + "\n" + u).get();
    writeFile(contentToWrite, "translated.csv");
  }

  private void writeFile(String contentToWrite, String fileName) {
    try {
      Files.write(contentToWrite, new File(fileName), Charset.defaultCharset());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  private List<String> readFile(String fileName) {
    try {
      return Files.readLines(new File(fileName), Charset.defaultCharset());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}


texts:
I would like to display a 3...2...1 countdown on my iOS app (before to take a picture) but I didn't find an elegant solution. I don't want to make a Countdown app with an NSTimer. For me the important is to keep all code in the same method, use a system like Block.

self.infoLabel.text   = [NSString stringWithFormat:@"3"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

self.infoLabel.text = [NSString stringWithFormat:@"2"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    self.infoLabel.text = [NSString stringWithFormat:@"1"];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.infoLabel.hidden = YES;

            // DO WHAT I WANT AFTER 3 SECs
        });
    });
});
});

Do you have a better solution? t works okay, but it's not really elegant link up dispatch_after like I did.


texts: Simple C++ program to check win in TicTacToe

texts:
Given a string of size 9 that represents a tic tac toe board, determine if X or O has won, or if the board is in an invalid state. Any other character aside from x, X, o, O represents an empty spot on the board.

Input: 012345678

Board:

0 1 2
3 4 5
6 7 8

TicTacToe.cpp

#include "tictactoe.h"
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>

using std::string;
using std::vector;

const int kBoardSize = 9;
const int kBoardLength = std::sqrt(kBoardSize);

TicTacToeState CheckTicTacToeBoard(std::string board) {
  if (board.size() != kBoardSize) {
    return TicTacToeState::InvalidInput;
  }

  std::transform(board.begin(), board.end(), board.begin(), ::tolower);
  size_t numberOfO = std::count(board.begin(), board.end(), 'o');
  size_t numberOfX = std::count(board.begin(), board.end(), 'x');

  if (numberOfO > numberOfX || numberOfX > numberOfO + 1) {
    return TicTacToeState::UnreachableState;
  }

  bool xWon = winDetection(board, 'x');
  bool oWon = winDetection(board, 'o');

  if (xWon && oWon) {
    return TicTacToeState::UnreachableState;
  } else if (xWon) {
    return TicTacToeState::Xwins;
  } else if (oWon) {
    return TicTacToeState::Owins;
  }
  return TicTacToeState::NoWinner;
}

bool winDetection(string board, char marker) {
  bool rowWin = false, colWin = false, rightDiagWin = true, leftDiagWin = true;

  for (int i{0}, rightDiagIndex{0}, leftDiagIndex{kBoardLength - 1};
       i < kBoardLength; ++i, rightDiagIndex += (kBoardLength + 1),
                         leftDiagIndex += (kBoardLength - 1)) {
    bool row = true, col = true;

    int rowIndex = kBoardLength * i, colIndex = i;
    for (int j{0}; j < kBoardLength; ++j) {
      col &= (board[colIndex] == marker);
      row &= (board[rowIndex] == marker);

      rowIndex++;
      colIndex += kBoardLength;
    }

    colWin |= col;
    rowWin |= row;

    rightDiagWin &= board[rightDiagIndex] == marker;
    leftDiagWin &= board[leftDiagIndex] == marker;
  }

  return (rowWin || colWin || rightDiagWin || leftDiagWin);
}

TicTacToe.h

#pragma once

#include <string>

enum TicTacToeState { UnreachableState, Xwins, Owins, NoWinner, InvalidInput };

TicTacToeState CheckTicTacToeBoard(std::string board);

bool winDetection(std::string board, char marker);


texts:
So I have a program that visualizes data from files (csv,txt,etc.). These files are saved to database as individual tables. In my application I have DocumentExplorer which is just a module with a list of available documents(files). You can open and close any document you want, and you can have multiple documents opened at the same time.

This program also uses MEF to separate modules and exposes API module for users to create plugins. What I'm working on is some kind of manager to manage all opened documents in current session. Basically I need something that will be responsible for closing, opening, switching documents. I have 2 ideas how to deal with it, but I'm not sure whether they are any good or not.
This is what I came up with so far:

1. DocumentManager passed by IoC container with interface exposed inside API module.

   public class DocumentManager : IDocumentManager
    {
        private List<IDocument> ActiveDocuments { get; set; }

        public void Create(IDocument document)
        {
            if(!ActiveDocuments.Contains(document))
                ActiveDocuments.Add(document);
        }

        public void Close(IDocument document)
        {
            if (ActiveDocuments.Contains(document))
                ActiveDocuments.Remove(document);
        }

        public IDocument Get(Expression<Func<IDocument, bool>> predicate)
        {
            return ActiveDocuments.AsQueryable().FirstOrDefault(predicate);
        }
    }

2. DocumentManager as static class kept inside API module, so others can access it. (without IDocumentManager interface, obviously)

What are your thoughts? Are these good approaches? Do you know any good tricks with this kind of problem?


texts: Submit an array as an HTML form value using JavaScript

texts:
I'm sending data from a form with a variable number of fields via an Ajax call.  Some of the fields are grouped together so I have opted to send them as an array of JSON objects to keep the groupings, but there are a number of ways of doing this. For client-side, is it better to read the form as JavaScript objects and stringify them or just directly work with strings from the start?

Here's an example:

I want to group the data so I know which date goes with which member, so I have created a bunch of objects and added them to an array.  This seems to work but there are issues with using JSON.stringify because it's not supported in IE7.  I don't know if modifying the form is very efficient either. How would you manage this? Or would you submit the form as is and sort it out on the server?  What's the most widely accepted approach?

HTML:

<form id="form">

Group Name

    <input type="text" name="group_name">

Members

    <button type="button">New member</button>

NameDate

        <input type="text" name="name"><input type="text" name="date">

    <input type="submit">
</form>

JavaScript:

$(document).ready(function(){
    $('#form').submit(function(e){
        var memberArr = [];
        $('.member_data').each(function(){
            var thisMember = {};
            $(this).children().each(function(){
                thisMember[$(this).attr('name')] = $(this).val();
            });
            memberArr.push(thisMember);
            // Delete this input
            $(this).remove();
        });

        var input = $("<input>").attr("type", "hidden").attr("name", "members").val(JSON.stringify(memberArr));
        $('#dynamic_form').append($(input));
        e.preventDefault();

        $.post("http://geraintanderson.com/cgi-bin/ajax_array.py",
        $('#form').serialize(),
        function(data, status){
            $('#member_info').html(data);
            // Reset the form
            $('#dynamic_form').html('<input type="text" name="name"><input type="text" name="date">
');
        });
    });

    $('button').click(function(){
        // Add new form fields
        $('#dynamic_form').append('<input type="text" name="name"><input type="text" name="date">
');
    });
});

And here's a live example using a Python script to return some text.


texts: Web crawler that uses Task Parallel Library

texts:
This is the first time using the Task Parallel Library and it seems to be performing much slower than I was expecting. The application is simply a web crawler that returns all URLs for a given URL.

Here is my method that, depending on the level param, will either download single page links (getSinglePageLinks()) or it will make use of my threaded method (getManyPageLinks()):

public static IEnumerable<string> getLinks(string url, bool hostMatch=true, bool validatePages=true, int level=0)
    {
        string formattedUrl = urlFormatValidator(url);
        if (string.IsNullOrEmpty(formattedUrl)) return Enumerable.Empty<string>();
        //download root url's
        IEnumerable<string> rootUrls = getSinglePageLinks(formattedUrl, hostMatch, validatePages);
        //download url's for each level
        for (int i=0; i<level; i++)
        {
            rootUrls = rootUrls.Union(getManyPageLinks(rootUrls, hostMatch, validatePages));
        }
        return rootUrls;
    }

getSinglePageLinks() makes use of the HTML agility pack, and it simply downloads and parses the given URL:

private static IEnumerable<string> getSinglePageLinks(string formattedUrl, bool hostMatch = true, bool validatePages = true)
    {
        try
        {
            HtmlDocument doc = new HtmlWeb().Load(formattedUrl);
            var linkedPages = doc.DocumentNode.Descendants("a")
                                              .Select(a => a.GetAttributeValue("href", null))
                                              .Where(u => !String.IsNullOrEmpty(u))
                                              .Distinct();

             //hostMatch and validatePages left out
             return linkedPages;
        }catch(...){...}

And my getManyPageLinks():

   private static IEnumerable<string> getManyPageLinks(IEnumerable<string> rootUrls, bool hostMatch, bool validatePages)
    {
        List<Task> tasks = new List<Task>();
        List<List<string>> allLinks = new List<List<string>>();

        foreach (string rootUrl in rootUrls)
        {
            string rootUrlCopy = rootUrl; //required
            var task = Task.Factory.StartNew(() =>
            {
                IEnumerable<string> taskResult = getSinglePageLinks(rootUrlCopy, hostMatch, validatePages);
                return taskResult;
            });

            tasks.Add(task);
            allLinks.Add(task.Result.ToList());
        }

        Task.WaitAll(tasks.ToArray());
        return allLinks.SelectMany(x => x).Distinct();
    }

The app works OK if the level is set to 0, but if I set the level to 1 so it gets all links for all of the roots URL, the CPU/Network usage does not go above 1-3%. How can I improve performance?


texts:
Here is my attempt:

#include <stdio.h>

int main()
{
    int number = -1;
    printf("Enter a number in range [2,20]\n");
    scanf("%d", &number);
    while (number < 2 || 20 < number)
    {
        while (getchar() != '\n')
        {
        }
        printf("Try again\n");
        scanf("%d", &number);
    }
    printf("Your number was: %d", number);
    return 0;
}

But the solution from the professor is the following:

#include<stdio.h>

int main(){
    int n;
    while(scanf("%d",&n)==0 || n <2 || n >20){
        while ( getchar () != ’\n’);
        printf( "Wrong. Try again\n");
    }
    printf ( "%n\n",n);
    return 0;
}

Both approaches seem to work fine but is there a benefit to his method? Is it possible that scanf changes variable even though it couldn't read in a correct number. In that case checking the result of the scanf function would protect the program from accepting a wrong answer.


texts: Making a web service call based on a type of request

texts:
I have my code which makes a web service call based on a type of request:

public class Client
{
    IRequest request;

    public Client(string requestType)
    {
        request = new EnrolmentRequest();
        if (requestType == "Enrol")
        {
            request.DoEnrolment();
        }
        else if (requestType == "ReEnrol")
        {
            request.DoReEnrolment();
        }
        else if (requestType == "DeleteEnrolment")
        {
            request.DeleteEnrolment();
        }
        else if (requestType == "UpdateEnrolment")
        {
            request.UpdateEnrolment();
        }
    }

}

So as per open close principle, I can subclass like:

Class EnrolmentRequest:IRequest
{
    CallService();
}
Class ReEnrolmentRequest:IRequest
{
    CallService();
}
Class UpdateEnrolmentRequest:IRequest
{
    CallService();
}

Now my client class will look something like this:

public class Client
{
    public Client(string requestType)
    {
        IRequest request;

        if (requestType == "Enrol")
        {
            request = new EnrolmentRequest();
            request.CallService();
        }
        else if (requestType == "ReEnrol")
        {
            request = new REnrolmentRequest();
            request.CallService();
        }
        else if (requestType == "DeleteEnrolment")
        {
            request = new UpdateEnrolmentRequest();
            request.CallService();
        }
        else if (requestType == "UpdateEnrolment")
        {
            request = new UpdateEnrolmentRequest();
            request.CallService();
        }
    }

}

Now, I still have to use if and else, and will have to change my code if there are any new request type. So, it's definitely, not closed to modification.

Am I missing anything with respect to SOLID?


texts: Parsing a CSV file in node.js and express.js

texts:
I want to import a CSV file and do some operation on it.

The following is the code I'm using and it is working fine:

 return function (req, res) {
    var rl = require('readline').createInterface({
        input: fs.createReadStream(myFile.csv)
    });

    var lineNumber = 1;
    rl.on('line', function (line) {

        // Validate the first line
        if (lineNumber === 1) {
            if (firstLineIsInvalid()) {
                res.send({
                    status: 'error',
                    message: 'This file deos not have a proper header'
                });
                rl.close();
            }
            lineNumber++;
            return;
        }

        // Validate the 2nd line
        if (isSecondLineInvalid()) {
            res.send({
                status: 'error',
                message: '2nd line must be blank'
            });
            rl.close();
            lineNumber++;
            return;
        }

        // Skip 3rd line
        if (lineNumber === 3) {
            lineNumber++;
            return;
        }

        // Skip the last line, we do not need aggregations
        if (line.indexOf('Total') > -1) {
            lineNumber++;
            return;
        }

        // Start processing
        processLine(line);

        lineNumber++;
    });

    rl.on('close', function () {
        if (!res.headersSent) {
            fs.rename(myFile.csv, archivedFile.csv, function (err) {
                if (err) {
                    res.send({status: 'error',  message: 'Archiving failed, '+ JSON.stringify(err)});
                } else {
                    res.send({status: 'ok'});
                }
            })
        } else {
            fs.rename(myFile.csv, rejectedFile.csv)
        }
        res.end();
    });
}

But I am wondering if there is a better approach for:

Counting the line numbers and validating the lines based on their position in the file.
Reacting to the errors, especially those I get in the validation of the first and second lines.


texts: String permutation using iteration and a filter

texts:
I'm looking to improve my skills as a developer so I recently challenged myself to design a string permutation generator without recursion. To add to the algorithm I also decided that the string should have a filter to it. An example of a filter would be "lld" where "l" is any character a-z and "d" is any digit 0-9. The filter "lld" should produce all strings aa0-zz9. Would love feedback on how to improve this algo. Thanks.

Implementation in C++

#include <iostream>

#define CHAR_SET_LEN 26
#define INT_SET_LEN 10
#define FILTER_LEN 3

int main() {
    std::cout << "Hello, World!" << std::endl;

    char char_set[CHAR_SET_LEN] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    char int_set[INT_SET_LEN] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    bool doLoop = true;

    int filterStack[100];

    int filterLength = FILTER_LEN;

    char filter[FILTER_LEN+1] = "lld";

    for (int i=0; i < filterLength; i++) {
        filterStack[i] = 0;
    }

    while (doLoop) {
        char string[FILTER_LEN];

        for (int a = 0; a < filterLength; a++) {
            if (filter[a] == 'l') {
                string[a] = char_set[filterStack[a]];
            }
            else {
                string[a] = int_set[filterStack[a]];
            }

        }
//
        std::cout << string << std::endl;
//
        for (int z = (filterLength-1); z > -1; z--) {
            filterStack[z] = filterStack[z] + 1;

            int finalFilter = INT_SET_LEN;
            if (filter[z] == 'l') {
                finalFilter = CHAR_SET_LEN;
            }

            if (filterStack[z] < finalFilter) {
                break;
            }
            else {
                if (z == 0 && (filterStack[z] == finalFilter)) {
                    doLoop = false;
                    break;
                }

                filterStack[z] = 0;
            }
        }
    }

    return 0;
}


texts: "Heads or Tails?" guessing game in Python

texts:
I'm pretty new to coding and I want to get an opinion on my coding. I am learning Python and I made a really simple heads and tails guessing game.

import random
import time
import easygui
import sys
while True:
    rand = random.choice(["Heads", "Tails"])
    firstguess = raw_input("Guess Heads or Tails: ")
    if firstguess == rand:
        print "Wow you win"
    else:
        print "That is wrong you suck so bad lol."
    time.sleep(2)
    answer = easygui.buttonbox("Play again?", choices=["Yes","No"])
    if answer == "Yes":
        easygui.msgbox("Ok")
    else:
        break

easygui.msgbox("Ok, see you later!")
sys.exit(0)


texts:
I would like some advice on how I could shorten my function from my battleships game (below). Essentially what it does is it checks if a coordinate is a hit and if it is, then if checks adjacent coords and shoots the one that would be a hit (it cheats, basically, which is fine).

I would like to know what I could do to make it more effective/shorter/more neat.

(If you improve it with techniques that I am not currently using, then please explain said technique. I want to use something that I can understand.)

def ai_shoots(y_coord, x_coord, all_buttons, player_1_board, ai_score):
    # print("yes")
    if ai_score == 20:
        popupwindow("The computer has won.")
    if player_1_board[y_coord][x_coord] == ': ':
        ai_score += 1
        player_1_board[y_coord][x_coord] = 'X '
        all_buttons[y_coord - 1][x_coord - 1].configure(text="X", fg="black", bg="red3")
        if player_1_board[y_coord - 1][x_coord] == ': ':
            ai_shoots(y_coord - 1, x_coord, all_buttons, player_1_board, ai_score)
        elif player_1_board[y_coord + 1][x_coord] == ': ':
            ai_shoots(y_coord + 1, x_coord, all_buttons, player_1_board, ai_score)
        elif player_1_board[y_coord][x_coord - 1] == ': ':
            ai_shoots(y_coord, x_coord - 1, all_buttons, player_1_board, ai_score)
        elif player_1_board[y_coord][x_coord + 1] == ': ':
            ai_shoots(y_coord, x_coord + 1, all_buttons, player_1_board, ai_score)
        else:
            x = random.randint(1, 10)
            y = random.randint(1, 10)
            ai_shoots(y, x, all_buttons, player_1_board, ai_score)
    elif player_1_board[y_coord][x_coord] == 'X ' or player_1_board[y_coord][x_coord] == 'O ':
        x = random.randint(1, 10)
        y = random.randint(1, 10)
        ai_shoots(y, x, all_buttons, player_1_board, ai_score)
    else:
        player_1_board[y_coord][x_coord] = 'O '
        all_buttons[y_coord - 1][x_coord - 1].configure(text="O", fg="white")


texts: Find and return the earliest occurring word from a string which contains the most repeating letters

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

  [Letter Count II]
  Have the function LetterCount(str) take the str parameter being passed and
  return the first word with the greatest number of repeated letters. For
  example: "Today, is the greatest day ever!" should return greatest because it
  has 2 e's (and 2 t's) and it comes before ever which also has 2 e's. If
  there are no words with repeating letters return -1. Words will be separated
  by spaces.

import doctest
import logging
import timeit

from collections import Counter
from string import ascii_letters

def letter_count(s: str):
    """Return the word in s with the largest number of repeating letters

    >>> letter_count(s="Today, is the greatest day ever!")
    'greatest'
    >>> letter_count(s="!!!Today, is the >>>>greatest day ever!")
    'greatest'
    >>> letter_count(s="!!!Today, is the greatest >>>> day ever!")
    'greatest'
    >>> letter_count(s="!!!Today, is the greatest >>>> day ever xx!")
    'greatest'
    >>> letter_count(s="")
    -1
    >>> letter_count(s="abcd efg hijk")
    -1
    """
    if s == "":
        return -1

    word_counters = dict((word, Counter(word)) for word in s.split(" "))

    # tuple of (word(str), most common letter frequency(int))
    most_repeating_word = ("", 0)

    for word, counter in word_counters.items():

        # most_common format = (letter(str), freq(int))
        for most_common in counter.most_common():

            if most_common[0] not in ascii_letters:
                continue

            if most_common[1] > most_repeating_word[1]:
                most_repeating_word = (word, most_common[1])
                break

    if most_repeating_word[1] <= 1:
        return -1

    return "".join(l for l in most_repeating_word[0] if l in ascii_letters)

if __name__ == "__main__":
    doctest.testmod()
    print(timeit.timeit("letter_count(s='!!!Today, is the greatest >>>> day ever!')",
                  setup=("from __main__ import letter_count; from collections "
                         "import Counter; from string import ascii_letters"),
                  number=10000))


texts: Creating a singleton container class for intrer-class messaging

texts:
My question is for validation of my code (any loop holes / bugs) and guidance the best  methodology to implement for my requirement.

I am developing a Python application that will have many classes. We need to communicate in between each class - pass data. It would be very clumsy if across all classes I create a setter/getter method and refer them across different classes, hence I prefer to pass a container object in between all of them:

class One
  def __init__(self, container):

class Two
  def __init__(self, container):

Now obviously I want that this container object should be a Singleton object - only one instance to pass multiple data structures. Below is my code for same [class declaration for Container class]:

import abc

class Container(object):
    __metaclass__ = abc.ABCMeta
    __data1 = 0
    __data2 = 0
    @abc.abstractmethod
    def dummy():
        raise NotImplementedError()
    def __init__(self):
        self.__data1 = 0
        self.__data2 = 1
    def SetData(self,value):
        self.__data1 = value
    def GetData(self):
        return self.__data1

class Singleton(Container):
    _instances = {}
    def __new__(class_, *args, **kwargs):
        if class_ not in class_._instances:
                class_._instances[class_] = super(Singleton, class_).__new__(class_, *args, **kwargs)
        return class_._instances[class_]
    def dummy():
        pass

Now in my application, I would not be able to create an another separate instance of Container [abstract class] / Singleton class. So I can pass the object of Singleton like as mentioned below:

class One:
    def __init__(self, container)
        self.value = container.GetData()
        ................................
        container.SetData(9)
        Two(container)

class Two:
    def __init__(self, container)
        self.value = container.GetData()
        ................................
        container.SetData(19)
        ................................

class Three:
    def __init__(self, container)
        self.container = Singleton()
        ................................
        container.GetData() # will return value 19 only
        container.SetData(19)
        ................................

if __name__ == "__main__":
    container = Singleton()
    container.SetData(9)
    One(container)

Please comment on my approach and modifications required if any. This code will be pushed in production box - so I want to double check my implementation.


texts:
I posted a couple questions about a week ago, but have almost gotten it working. This program is supposed to simulate the following:

Ask the user how many balls to drop
Drop one ball at a time
Allow it to bounce ten times moving either once to the right or once to the left each bounce
Then print out the even numbers (since a ball could never land on an odd) -10 - 10 with a 'o' to represent each ball.

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

/* Prototype for drop_Balls, int parameter is number of balls being dropped */
void drop_balls(int);

/* Gets the number of balls that need to be dropped by the user */
int get_num_balls();

int main()
{
    drop_balls(get_num_balls());
}

int get_num_balls()
{
    int num_balls;
    printf("How many balls should be dropped? ");
    scanf("%d", &num_balls);
    /* Ensure that it is atleast one ball */
    while(num_balls <= 0)
    {
        printf("You have to drop at least one ball! \n ");
        printf("How many balls should be dropped? ");
        scanf("%d", &num_balls);
    }
    /* Return the number of balls that will be dropped */
    return num_balls;
}

void drop_balls(int num_balls)
{
    /* Keeps track of how many balls landed where */
    int ball_count[21];
    /* What number ball are we on */
    int ball_num = 0;
    /* Seed the generator */
    srand(time(NULL));
    /* Do the correct # of balls */
    for(ball_num = 0; ball_num < num_balls; ball_num++ )
    {
        /* Start at 10 since its the middle of 21 */
        int starting_point = 10;
        /* Bounce each ball 10 times */
        for(starting_point = 10; starting_point > 0; starting_point--)
        {
           int number;
           /* Create a random integer between 1-100 */
           number = rand() % 100;
            /* If its less than 50 bounce to the right once */
           if(number >= 50)
           {
               starting_point++;
           }
           /* If its greater than 50, bounce to the left once */
           else
           {
               starting_point--;
           }
        }
        /* Add one to simulate one ball landing there */
        ball_count[starting_point]++;
    }

    /* Start at the -10 spot */
    int ball_place = -10;
    int x;
    /* Go through the 21 spots, but skip the odd ones, since they never get balls */
    for(x = 0; x < 20; x+2)
    {
        printf("\n %d: ", ball_place);
        int l = 0;
        /* Print out an 'o' for each ball */
        for(l = 0; l < ball_count[x]; l++)
        {
            printf("o");
        }
        /* Increase the spot, taking it from -10 - 10 */
        ball_place = ball_place + 2;
    }
}


texts:
For practice, I've implemented my own smart pointer class. It's a 'unique pointer', meaning it doesn't allow copies of itself (when copying from a into b, a is 'emptied' after the copying, i.e. the raw pointer is set to 0), so there can never be more than one time where delete is called on the raw pointer.

It utilizes RAII to bound the memory deallocation to the object lifetime, so it is just a basic smart pointer.

#ifndef SMARTPOINTER_CPP
#define SMARTPOINTER_CPP
#include <iostream>

template <typename T>
class SmartPointer {
public:
    SmartPointer(T* pointer = NULL) : pointer(pointer) { }
    ~SmartPointer(){
        delete pointer;
    }
    SmartPointer(SmartPointer& other){
        *this = other;
    }
    SmartPointer& operator=(SmartPointer& other){
        this->pointer = other.pointer;
        other.pointer = NULL;

        return *this;
    }
    T& operator*() const {
        return *pointer;
    }
    T* operator->() const {
        return pointer;
    }
private:
    T* pointer;
    template <typename E>
    friend std::ostream& operator<<
        (std::ostream& stream, const SmartPointer<E>& smartPointer);
};
template <typename E>
std::ostream& operator<<(std::ostream& stream, const SmartPointer<E>& smartPointer){
    stream << smartPointer.pointer;
}
#endif


texts:
I currently have a singleton that holds the translation objects in my app. View controllers can subscribe to its delegate so they can update their views if new translation objects have been added. This is what the class looks like

class TranslationItems {
    var delegate: TranslationItemsDelegate?

    static let shared = TranslationItems()

    private var set = Set<Translation>()

    // Public API

    public func add(object: Translation) {
        set.insert(object)
        delegate?.newItemAdded()
    }

    public func remove(object: Translation) {
        set.remove(object)
        delegate?.someItemDeleted()
    }

    public func getAll() -> [Translation] {
        return Array(set)
    }
}

protocol TranslationItemsDelegate {
    func newItemAdded()
    func someItemDeleted()
}

View controllers can use it like this:

// Add or remove objects
TranslationItems.shared.add(object: translation)

// Subscribe for notifications
TranslationItems.shared.delegate = self

Does this singleton class look ok or could it be improved?


texts: Preventing invalid entity state that contains two related collections

texts:
I need to define an entity (CompositeContactList) that contains and manages two collections of identifiers
(IncludedLists and ExcludedLists) that are ids of some other entities (ContactList).

The business rules are the following:
1. The collections must contain only unique identifiers.
2. The same identifier cannot belong to both IncludedLists and ExcludedLists.
3. Identifiers can be added or removed (performing the checks above).

Note: Adding an identifier to the ExcludedLists is not opposite to removing it from the IncludedLists.
For instance, there is a list A that contains included lists B and C and excluded list D.
That means that A = (B + C) - D.
An attempt to add list C to the ExcludedLists leads to an error while C is in the IncludedLists.
If the list C is removed from the IncludedLists, the list A will be the following: A = B - D.

I introduced two IEnumerable<Guid> properties to get the collections and four methods to manage them.
There are few areas I'd like to hear some feedback about. Which variant is better?

Property naming

IncludedLists and ExcludedLists
IncludedListIds and ExcludedListIds

Method naming:

IncludeList(id)
RemoveFromIncludes(id)
ExcludeList(id)
RemoveFromExcludes(id)

AddIncludedList(id)
RemoveIncludedList(id)
AddExcludedList(id)
RemoveExcludedList(id)

AddIncludedListId(id)
RemoveIncludedListId(id)
AddExcludedListId(id)
RemoveExcludedListId(id)

Validation

Rule 1. When I add the same identifier twice, does it may sense to throw an exception saying that the id
cannot be added? Is there any harm in 'doing nothing' here?
Rule 2. Trying to add a list id to the included lists throws an error if it's already belongs to the excluded.
Does it make sense to introduce some helping methods like CanAddToIncludedLists or TryAddToIncludedLists?
Does it make sense to throw an ArgumentException if an empty Guid is provided?

Usability

Does it make sense in declaring 'Add' methods as void and 'Remove' methods returning bool?
(similar to .NET collections).
Does it make sense in adding other collection-specific methods like ClearIncludedLists or ClearAll?

Code

public class MyCompositeEntity
{
    private readonly ISet<Guid> _excludedLists = new HashSet<Guid>();
    private readonly ISet<Guid> _includedLists = new HashSet<Guid>();

    public MyCompositeEntity(Guid id)
    {
        if (id == Guid.Empty)
            throw new ArgumentException("The identifier cannot be empty.", nameof(id));

        Id = id;
    }

    public Guid Id { get; }

    public IEnumerable<Guid> IncludedLists => _includedLists;

    public IEnumerable<Guid> ExcludedLists => _excludedLists;

    public void IncludeList(Guid id)
    {
        if (id == Guid.Empty)
            throw new ArgumentException("The identifier cannot be empty.", nameof(id));

        if (ExcludedLists.Contains(id))
            throw new ArgumentException(
                "The identifier cannot be included because it is already excluded. " +
                "Please remove it from the excluded lists first.",
                nameof(id));

        _includedLists.Add(id);
    }

    public void ExcludeList(Guid id)
    {
        if (id == Guid.Empty)
            throw new ArgumentException("The identifier cannot be empty.", nameof(id));

        if (IncludedLists.Contains(id))
            throw new ArgumentException(
                "The identifier cannot be excluded because it is already included. " +
                "Please remove it from the included lists first.",
                nameof(id));

        _excludedLists.Add(id);
    }

    public bool RemoveFromIncludes(Guid id)
    {
        if (id == Guid.Empty)
            throw new ArgumentException("The identifier cannot be empty.", nameof(id));

        return _includedLists.Remove(id);
    }

    public bool RemoveFromExcludes(Guid id)
    {
        if (id == Guid.Empty)
            throw new ArgumentException("The identifier cannot be empty.", nameof(id));

        return _excludedLists.Remove(id);
    }
}


texts:
I have a small class of 2D drawing utilities, Draw2D which is the global entry point for anything related to drawing. Currently, the class looks like this:

struct Draw2D
{
    std::shared_ptr<D2Label> CreateLabel (...);
    std::shared_ptr<D2Plot> CreatePlot (...);

    void Draw (D2Element& e);

    std::vector<std::weak_ptr<D2Element>> elements_;
};

struct D2Element
{ ... };

struct D2Label : D2Element
{ ... };

So far so good. The key point here is that Draw2D needs to know all it's elements so it can update them and for drawing, each element has to call back to its parent Draw2D instance. I.e. you cannot move elements between instances as all elements share resources provided by the parent. The solution I have right now is weak_ptr which get checked on each update of the Draw2D instance and if stale, the object is removed (meaning the user has deleted it in the meantime.)

The problem is that after the Draw2D instance died, of course all still existing instances are dead, too. Even though they are technically valid objects, you cannot use them safely any more. Even queries could fail if they touch the shared resources, and keeping the shared resource alive via shared_ptr does not help either because that just postpones the problems until the drawing backend dies (in which case all resources are invalidated.)

Is there some way to rewrite this code to make it clearer to the clients? I tried something like this, as I definitely want clients to be able to release resources early (i.e. before Draw2D dies.)

struct D2Element
{
    ~D2Element ()
    {
        deleter_ (this);
    }

    std::function<void OnDelete (D2Element*)> deleter_;
}

struct Draw2D
{
    void Release (D2Element* element)
    {
        // Remove from list of registered elements
    }

    ~Draw2D ()
    {
        // delete all remaining children
    }

    D2Label* CreateLabel (...)
    {
        auto label = ...;
        label->deleter_ = std::bind (&Draw2D::Release, this, _1);
        return label;
    }
}

This is ok but now the clients have to wrap it into smart pointers on their own by default.


texts: Creating strings of desired length from a list of substrings

texts:
I have a method that generates strings from a list of substrings, and the desired length of all these strings is passed as an argument of the method. Here is my code:

import itertools as it

print make_string(5)

def make_string(wanted_length):
    available_strings = ['AB', 'CDE', 'FGHIJK', 'LMN', 'OP', 'QR', 'ST', 'U']
    minimum_length = 0
    i=2
    while minimum_length < wanted_length:
        products = it.product(*[available_strings]*i)
        str_products = [''.join(map(str,x)) for x in products]
        valid_products = [x for x in str_products if len(x)==wanted_length]
        if valid_products:
            return valid_products
        minimum_length = len(min(str_products, key=len))
        i += 1
    return None

As one can expect, it is incredibly slow and feels overall wrong even if it works. Do you have any input to help me make it better ?

EDIT

As Graipher pointed out in his comment, the above code stops when a valid string of desired length is found but doesn't return all valid strings of desired length. Here is the fixed code, which is obviously even slower than the previous one :)

def make_string(wanted_length):
    all_valid_products = []
    available_strings = ['AB', 'CDE', 'FGHIJK', 'LMN', 'OP', 'QR', 'ST', 'U']
    minimum_length = 0
    i=2
    while minimum_length < wanted_length:
        products = it.product(*[available_strings]*i)
        str_products = [''.join(map(str,x)) for x in products]
        valid_products = [x for x in str_products if len(x)==wanted_length]
        if valid_products:
            all_valid_products.extend(valid_products)
        minimum_length = len(min(str_products, key=len))
        i += 1
    return all_valid_products


texts:
Given:

data NonEmpty a = NonEmpty a (Array a)

I completed the following exercise from PureScript by Example:

  Write a Foldable instance for NonEmpty. Hint: reuse the Foldable instance for arrays.

instance foldableNonEmpty :: Foldable NonEmpty where
  foldr f acc (NonEmpty a as) = foldr f (f a acc) as
  foldl f acc nonEmpty        = foldr (flip f) acc nonEmpty
  foldMap f (NonEmpty a as)   = f a <> (foldMap f as)

Please critique my implementation. Can I do better?


texts:
I am about two months into programming with Python (no previous experience) and I tried to write a program which would generate a key and assign it to a student.

import random
import string

code_list = []
students = ["Aljoša", "Blažka", "Nana", "Kaja", "Alja", "Tjaša", "Ana",
           "Gal", "Danijela", "Alma", "Neja", "Žiga K.", "Patricija", "Aja",
           "Kristjan",  "Urban",  "Janja", "Lea", "Žana", "Aljaž", "Tilen",
           "Matic", "Marija", "Žiga T."]

#It generates the code and puts it in a list
size = 6
code = string.ascii_uppercase + string.digits
for i in range(24):
    code_list.append(str(''.join(random.choice(code) for _ in range(size))))

#Combines the two lists (students and codes) into one
students_plus_codes = [x + str(" - " + y) for y in students for x in
                      code_list]

#It prints only every 25th element from the combined list
new = [x for i, x in enumerate(students_plus_codes) if i % 25 == 0]

for i in new:
    print(i)`

My problem is that this code is not very elegant and just want to ask how I could write it more efficiently (especially the last part where I have to manually print out every 25th element of a list because every key gets assigned to every student).


texts: Multiply two columns of Census data and groupby

texts:
I have census data that looks like this

    State   County  TotalPop    Hispanic    White   Black   Native  Asian   Pacific
   Alabama  Autauga     1948    0.9         87.4    7.7     0.3     0.6     0.0
   Alabama  Autauga     2156    0.8         40.4    53.3    0.0     2.3     0.0
   Alabama  Autauga     2968    0.0         74.5    18.6    0.5     1.4     0.3
   ...

Two things to note, (1) there can be multiple rows for a County and (2) the racial data is given in percentages, but sometimes I want the actual size of the population.

Getting the total racial population translates to (in pseudo Pandas):

(census.TotalPop * census.Hispanic / 100).groupby("County").sum()

But, this gives an error: KeyError: 'State'.  As the product of TotalPop and Hispanic is a Pandas Series not the original dataframe.

As suggested by this Stack Overflow question, I can create a new column for each race...

census["HispanicPop"] = census.TotalPop * census.Hispanic / 100

This works, but feels messy, it adds 6 columns unnecessarily as I just need the data for one plot.  Here is the data (I'm using "acs2015_census_tract_data.csv") and here is my implementation:

Working Code

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
%matplotlib inline

census = pd.read_csv("data/acs2015_census_tract_data.csv")

races = ['Hispanic', 'White', 'Black', 'Native', 'Asian', 'Pacific']

# Creating a total population column for each race
# FIXME: this feels inefficient.  Does Pandas have another option?
for race in races:
    census[race + "_pop"] = (census[race] * census.TotalPop) / 100

# current racial population being plotted
race = races[0]

# Sum the populations in each state
race_pops = census.groupby("State")[race + "_pop"].sum().sort_values(ascending=False)

#### Plotting the results for each state

fig, axarr = plt.subplots(2, 2, figsize=(18, 12))
fig.suptitle("{} population in all 52 states".format(race), fontsize=18)

# Splitting the plot into 4 subplots so I can fit all 52 States
data = race_pops.head(13)
sns.barplot(x=data.values, y=data.index, ax=axarr[0][0])

data = race_pops.iloc[13:26]
sns.barplot(x=data.values, y=data.index, ax=axarr[0][1]).set(ylabel="")

data = race_pops.iloc[26:39]
sns.barplot(x=data.values, y=data.index, ax=axarr[1][0])

data = race_pops.tail(13)
_ = sns.barplot(x=data.values, y=data.index, ax=axarr[1][1]).set(ylabel="")


texts:
As my first Java program, I have written a number-guessing game. I would appreciate any suggestions on how I can clean it up or streamline it. I don't mean cut down on the general tasks executed within the program, but rather if there were better methods/classes/syntax I could have used to make it better. I took a few snippets from a tutorial.

import java.util.Scanner;
import java.util.Random;

class Game {

public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);
  //generate a random number
  //take user input
  //check the input act appropriately
  boolean running = true;
  boolean guessed = true;
  boolean realInt = true;

  int number = 0;
  int input = 0;

  String firstName=" ";

   System.out.print("want to play the game? Yes/No: ");
   firstName = sc.next();

  while (running) {

    if (guessed && (("yes".equals(firstName)) | ("cheater".equals(firstName)))){

        System.out.println("I have chosen a random value");
        System.out.println("Have a go at guessing it");

        Random rand = new Random();
        number = rand.nextInt(10) + 1;

        guessed = false;

    }

     if(("yes".equals(firstName)) | ("cheater".equals(firstName)))
     {

          if(sc.hasNextInt()) {
            realInt=true;
            input = sc.nextInt();
          }
          else{
            System.out.println("That is not an integer.");
            running = false;
          }

        }

     else
      running = false;

     if((((input != number) && realInt)&&(input != -1))){
           System.out.println("Plsease try again");
         }

    //below line sets up when to stop the program, when -1 is entered
         if (input == -1){
            running = false;
         }
         else{
           guessed = input == number;
         }

    //below lines are cheat codes

    if (input == -5){
        System.out.println("Answer: " + number);
    }

   if ("cheater".equals(firstName) && (input != number)){
            System.out.println("Answer: " + number);

      }

   }

 }

}


texts:
I managed to make a perfectly even cross with for loops.

I'm trying to make this cross easier to code.  I have to keep it with for loops, but it's too long to implement.

public class CroixDemoAmmeliore {
    public static void main(String args[]) {

        int e = 5;
        int i;
        int j;
        int k;
        int l;

        for (k = 1; k <= e; k++) {
            for (i = 1; i <= e; i++) {
                System.out.print(" ");
            }
            for (j = 1; j <= e; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (k = 1; k <= e; k++) {
            for (i = 1; i <= e * 3; i++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (k = 1; k <= e; k++) {
            for (i = 1; i <= e; i++) {
                System.out.print(" ");
            }
            for (j = 1; j <= e; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}


texts:
I wish to redirect users to the login page if they attempt to visit a page which requires them to be logged in. After logging in, however, I want to redirect the user back to their original destination. I've written a "redirect.php" script which is to be included on all such pages:

<?php
require "session.php";
if(!$user){
    header("Location: login.php?dest=".urlencode($_SERVER["REQUEST_URI"]));
    die();
}
?>

Then on my login page I have the following:

<?php
$dest = "./";
if(isset($_GET["dest"])){
    $dest = $_GET["dest"];
}
?>

with the following JavaScript:

var URL = "<?php echo $dest; ?>";
//...
//upon successful login (via AJAX):
window.location.replace(URL);

Everything here works as intended but where does this stand from a security standpoint?

One vulnerability that comes to mind is something like

  http://mysite.com/login.php?dest=http://phishingsite.com

How might I best prevent something like this? Would regex be suitable here?

Are there any other security concerns with this type of thing? Perhaps a standard way of doing this? Or better yet, a method which does not use GET variables at all?


texts:
I wanted to make a GUI-ish (a TUI) thing in the terminal as I have no experience with PyGame or the like. It works, but how can I improve this?

import os, colorama
from msvcrt import getch
from colorama import Fore, Back, Style

colorama.init()

def newGame():
    print("Sorry, currently not a feature")

def cont():
    print("We can't do that")

def highscores():
    print("Yep, again, no can do")

class comm:
    def __init__(self, comm):
        self.comm = comm

def getKeyInput():
    key = ord(getch())
    if key == 224:
        key = ord(getch())
    if key == 72: return 1 # Up arrow
    elif key == 80: return -1 # Down arrow
    elif key == 13: return 0 # Enter

class Menu:
    def __init__(self,
                 title,
                 options, # GUI Options (a list)
                 comms = []): # A list of actions corresponding to options
        self.options = options
        self.title = title
        self.comms = comms
        self.pointer = 0

    def show(self):
        os.system("cls")
        buffer = "-"*len(self.title)
        print("{}\n{}\n{}".format(buffer, self.title, buffer))

        for i in self.options:
            colour = Fore.WHITE + Back.BLACK
            if self.pointer == self.options.index(i): # This is why self.options cannot be a dictionary
                colour = Back.WHITE + Fore.BLACK
            print("{}{}".format(colour, i))
            colour = Fore.WHITE + Back.BLACK
            print(colour, end = '')

        key = getKeyInput()

        if key == 0:
            os.system("cls")
            if self.comms:
                self.comms[self.pointer].comm()
                return 0
            print('Picking {}'.format(self.options[self.pointer]))
            return 0

        self.pointer -= key
        self.pointer = max(min(self.pointer, len(self.options)-1), 0)
        self.show()

if __name__ == "__main__":
    m = Menu("Main Menu",

             ['Start New Game',
              'Continue',
              'Highscores',
              'Quit'],

             [comm(lambda : newGame()),
              comm(lambda : cont()),
              comm(lambda : highscores()),
              comm(lambda : quit())])
    m.show()


texts: Recursive/traversal search of items using EWS managed API in subfolders too

texts:
I would like to know if there's a more efficient way of querying items than this (that works, though):

Imports Microsoft.Exchange.WebServices

Public Class ExchangeTools
    Private Shared Function Items(
                                 target_folder As Data.Folder,
                                 Optional recurse As Boolean = False,
                                 Optional querystring As String = Nothing
                                 ) As IEnumerable(Of Data.EmailMessage)
        Dim retvar As IEnumerable(Of Data.EmailMessage)
        Dim iview = New Data.ItemView(Integer.MaxValue)
        If String.IsNullOrWhiteSpace(querystring) Then
            retvar = target_folder.FindItems(iview).OfType(Of Data.EmailMessage)
        Else
            retvar = target_folder.FindItems(querystring, iview).OfType(Of Data.EmailMessage)
        End If
        If recurse Then
            Dim fview = New Data.FolderView(Integer.MaxValue)
            retvar = retvar.Concat(
                target_folder.FindFolders(fview).SelectMany(
                    Function(sf) Items(sf, True, querystring)))
        End If
        Return retvar
    End Function
End Class


texts: Makefile for a x86 operating system in C++

texts:
I recently had submitted my bashscript for building an OS for review and got valuable feedback, so I rewrote the entire script for a makefile:

Makefile

# Make File for Bulding Ultron

# Sets up the Assembler
AS := nasm
ASFLAGS := -f elf
ASFILES := $(wildcard kernel/boot/*.s kernel/arch/*.s)
ASOBJECTS := $(ASFILES:.s=.s.o)

# Compile C++ Files
CC := g++
CCFLAGS := -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding -m32 -I./sdk/include
CCFILES := $(wildcard *.cc sdk/lib/*.cc)
CCOBJECTS := $(CCFILES:.cc=.o)

# Linker
LINKER := ld
LINKERFLAGS := -m elf_i386 -T link.ld

# The Final Product will be ultron.img in the build directory
all: ultron.img

# Ultron.img : It is the Final Product formed by linking all the object files together
ultron.img : $(ASOBJECTS) $(CCOBJECTS)
        $(LINKER) $(LINKERFLAGS) -o build/$@ $^

# Run the Kernel : First Make the Kernel and then run it
run: ultron.img
        qemu-system-i386 -kernel 'build/ultron.img'

# Assemble the Assembly Files into object Files
# This Assembles all the Files into thier respective object files
# $< : Names the first prerequiste
# $@ : Names the current Target
%.s.o: %.s
        $(AS) $(ASFLAGS) $< -o $@

# Compile the C++ Files into corresponding object Files and place it in same directory
%.o: %.cc
        $(CC) $(CCFLAGS) -o $@ $<

# Clean this Mess
clean: $(ASOBJECTS) $(CCOBJECTS)
        $(RM) $^

This is my first makefile and I have a couple of questions:

Should I make the makefile as verbose as it is now?
Is my coding style Ok?
What are the things that I should keep in mind while writing makefiles?


texts: Converting list of sequences into one-hot vectors

texts:
I have an array of dim 1000 x 150 where I have 1000 strings each of length 150. And the strings only contain ATCGN (these are DNA sequences).
I am converting the letters to one-hot vectors such that A is [1,0,0,0,0] and  N is [0,0,0,0,1].
I wrote the following code but it takes way too long when there are about 20 million strings.
def seqs2onehot(seqs, num_val):
    def one_hot_encode(seq):
        mapping = dict(zip("ACGTN", range(num_val)))
        seq = [mapping[i] for i in seq]
        return np.eye(num_val)[seq]
    onehotvecs = []
    for i in seqs:
        onehotvecs.append(np.array(one_hot_encode(i), dtype='bool'))
    return onehotvecs

To generate input:
num_inputs = 1000
inputs = []
for num in range(num_inputs):
    inputs.append(''.join(random.choice('ATCGN') for i in range(150)))

Outputs are of dimension 1000 x 150 x 5 x 1.
You can create the final outputs by doing this:
inputs = np.array(inputs)
inputs = np.expand_dims(np.array(seqs2onehot(inputs, 5)), -1)
inputs.shape

Is there a way to do this without iterating through the list and just using matrix operations?


texts: Design for class which accepts large array in constructor

texts:
Let's say we have the following class which performs interpolation on irregularly spaced data:

template<class Real>
class interpolator
{
public:
    interpolator(const Real* const x, const Real* const y, size_t length)
    {
        m_x.resize(length);
        m_y.resize(length);
        for (size_t i = 0; i < length; ++i)
        {
             m_x[i] = x[i];
             m_y[i] = y[i];
        }
    }

    Real interpolate_at(Real x)
    {
        // use m_x and m_y to produce an estimate of y(x):
        return something;
    }

private:
    std::vector<Real> m_x;
    std::vector<Real> m_y;
};

This design has a number of things going for it: For one it is easy to use:

std::vector<double> x;
std::vector<double> y;
// Initialize x and y ...
interpolator<double> interp(x.data(), y.data(), y.size());

Since the constructor accepts primitive types, it can also be wrapped in Python with moderate effort (you'd need a shared object and some template instantiations, but let's assume that can be done). You can also let x and y go out of scope and the interpolator will still work.

The tragic flaw is that it performs a huge memcpy, which is slow and not space efficient.

Is there a way to refactor this class such that

It is easy to wrap in Python (std::shared_ptr's in interfaces are a nightmare to wrap . . .)
It doesn't perform a memcpy
The input data can be allowed to go out of scope and the interpolator still work


texts: Merging two arrays of objects with different keys/properties

texts:
I have two different arrays:

var array1 = [
    {id: "foo", name: "Foo Smith"},
    {id: "caa", name: "CAA"},
    {id: "bar", name: "Bar Jones"},
    {id: "baz", name: "Baz Apple"},
    {id: "boo", name: "Boo Ghost"},
    {id: "dah", name: "DAH-123"}
];

var array2 = [
    {name: "bar", key1: false, key2: false, key3: "abcdef", id: "abc123"},
    {name: "foo", key1: true, key2: false, key3: "abcdef", id: "def456"},
    {name: "dah", key1: false, key2: true, key3: "abcdef", id: "jkl012"},
    {name: "doo", key1: false, key2: false, key3: "ghijkl", id: "mno345"},
    {name: "laa", key1: false, key2: true, key3: "ghijkl", id: "pqr678"},
    {name: "boo", key1: true, key2: true, key3: "ghijkl", id: "ghi789"},
    {name: "fib", key1: true, key2: false, key3: "abcdef", id: "stu901"}
];

And the following requirements:

Requirements:

Get the intersection of array1 and array2 based off of matching
array1['id'] to array2['name']
Output needs to look like array1 with the following requirements:

key1, key2 and key3 need to be merged into array1 for intersecting objects
id of array2 needs to be mapped to new_id in array1

If no intersecting object, then defaultOptions need to be applied to array1

So that the output should look like:

$scope.outputArray = [
    {id: "foo", name: "Foo Smith", key1: true, key2: false, key3: "abcdef", new_id: "def456"},
    {id: "caa", name: "CAA", key1: false, key2: false, key3: null},
    {id: "bar", name: "Bar Jones", key1: false, key2: false, key3: "abcdef", new_id: "abc123"},
    {id: "baz", name: "Baz Apple", key1: false, key2: false, key3: null},
    {id: "boo", name: "Boo Ghost", key1: true, key2: true, key3: "ghijkl", new_id: "ghi789"},
    {id: "dah", name: "DAH-123", key1: false, key2: true, key3: "abcdef", new_id: "jkl012"}
];

What works:

var defaultOptions = {
    key1: false,
    key2: false,
    key3: null
};

var intersection = _.intersection(_.pluck(array1, 'id'), _.pluck(array2, 'name'));
var intersectionArr1 = _.sortBy(_.filter(array1, function(person) {
    return intersection.indexOf(person.id) >= 0;
}), 'id');
var intersectionArr2 = _.sortBy(_.filter(array2, function(person) {
    return intersection.indexOf(person.name) >= 0;
}), 'name');

_.each(intersectionArr1, function(person, idx) {
    _.assign(person, _.pick(intersectionArr2[idx], ['key1', 'key2', 'key3']));
    _.defaults(person, { new_id: intersectionArr2[idx].id });
});

var diff = _.difference(_.pluck(array1, 'id'), _.pluck(array2, 'name'));
var leftoverArr1 = _.filter(array1, function(person) {
    return diff.indexOf(person.id) >=0;
});

_.each(leftoverArr1, function(person) {
    _.defaults(person, defaultOptions);
});

$scope.newOutputArray = array1;

and here is the CodePen.

While this works, I'm looking for speed improvements.  Both array will eventually be very large 50-100K.

Any help would be greatly appreciated.


texts: Using std::function to write a function that returns a function

texts:
Our C++ professor told us to practise the use of std::function, lambda and pointers to functions.
He asked us to:

Write the function
 std::function<bool(float)> interval(float low, float high). The function should accept two values low and high and should return a function.
The function returned will check if it's parameter is inside or outside the interval (low,high) and will return true or false accordingly.

I tackled the problem with this code.
The function declaration:
std::function<bool(float)> interval (float low,float high){
    return [low,high](float f){ return (f > low) && (f < high); };
}

How is it used in the main:
std::function<bool(float)> interval (float low,float high);
bool res = interval(10, 20)(150);
std::cout << res;

And it works.
But I am not sure how the solution is elegant, or "best practise".
In particular, this call bool res = interval(10, 20)(150); is what scares me.
It is really awful to call it like this.
How would you have done the excersize? Do you find my solution clear?


texts:
I was having problems setting my path the way I wanted, I turned to Apple's path_helper but found it appends system dirs to the beginning of the path. I found someone had made a start on a replacement and I forked it and made some updates.

I don't write a lot of shell scripts, and less so to replace system supplied programs. As such, I'm wondering 2 main things:

Does this follow best practices for a shell script?
Is there an obvious security flaw? I'm most reticent about the line that isn't in the script itself but that calls it

The suggestion is to put this in ~/.bash_profile or ~/.zshenv:

if [ -x /usr/local/libexec/path_helper ]; then
  eval `/usr/local/libexec/path_helper`
fi

Evals make me nervous. Is that okay in this context?

From https://github.com/yb66/path_helper

#!/bin/bash -

## A better path helper - don't put the standard bins first.

function path_helper {
  local -a path_dirs
  local envvar=$1
  local target=`echo $1 | /usr/bin/awk '{print tolower($1)}'`
  local etcpaths="/etc/${target}s"
  local etcpathsd="${etcpaths}.d/*"

  while read -r dir ; do
      if [ "${#path_dirs[@]}" == "0" ]; then
          path_dirs=${dir}
      else
          path_dirs+=":${dir}"
      fi
  done < <(/bin/cat ${etcpathsd} ${etcpaths})

  path_dirs=($(echo ${path_dirs[@]} | /usr/bin/tr [:space:] '\n' | /usr/bin/awk '!a[$0]++'))
  echo "${envvar}=\"\${${envvar}}:${path_dirs[@]}\"; export ${envvar};"
}

path_helper "PATH"

# Only handle MANPATH if the variable hasn't already been set.
if [ -z "$MANPATH" ]; then
  path_helper "MANPATH"
fi
# EOF


texts: Selecting which variables to read from file

texts:
I'm reading in data from an external file. The data comes in different types, both integers and strings and is shown here:

{
    "height":170,
    "weight":73,
    "country":"UK",
    "city":"Sheffield",
    "year":2017,
    "month":7,
    "day": 2
}

I'd like to decide manually which fields to read without having to recompile the program. Therefore, I do this by listing in a text file which fields I am interested in. I have a file for integer-variables and one for strings:

ints.txt

weight
year
month

strings.txt

country

Now, the data I read I store in an object of class Read_Data (see code below). I keep the data in maps, one for integers and one for strings. I'd like to know where/how can improve on my program and make it more professional. I have myself been thinking if it is possible to merge the integer- and string map such that I don't have to treat them individually as I do currently.

Here is the complete code (and a link to nlohmann's json reader):

main.cpp

#include <iostream>
#include "data.h"

int main()
{
    ///create object to store data
    Read_Data read_data;

    ///read data from file
    read_data.read();

    return 0;
}

data.h

#ifndef SFE_H
#define SFE_H

#include <fstream>
#include "json.hpp"

class Read_Data
{
private:

  ///maps of the read variables
  std::map<std::string, int> int_map;
  std::map<std::string, std::string> string_map;

  ///vectors of which variables to read
  std::vector<std::string> int_list;
  std::vector<std::string> string_list;

public:
  Read_Data();
  int read_list(std::string file_name, std::vector<std::string> &list);
  void read();
};

#endif

data.cpp

#include "data.h"

///which variables we want to read
Read_Data::Read_Data()
{
  read_list("ints.txt", int_list);
  read_list("strings.txt", string_list);
}

int Read_Data::read_list(std::string file_name, std::vector<std::string> &list)
{
  std::string value;

  std::ifstream file_strm(file_name);
  if (file_strm.is_open())
  {
    while (file_strm >> value)
      list.push_back(value);
    file_strm.close();
    return 0;
  }
  else
  {
    std::cout << "Error" << "\n";
    return 1;
  }
}

void Read_Data::read()
{
  ///load data
  std::ifstream strm("data.json");
  nlohmann::json j = nlohmann::json::parse(strm);

  ///read ints from data
  for (auto const &value : int_list)
    int_map[value] = j[value];

  ///read strings from data
  for (auto const &value : string_list)
    string_map[value] = j[value];
}