texts: Monitor, read and display data from a dynamic log file

texts:
I am developing a plugin for intellij that basically monitors a log file and displays the data in a tool window within the IDE in real-time. I have managed to get it working but I would like to know the areas where I can improve on. I'm specifically interested in the structure of my code and the log file monitoring method followed.

The createToolWindowContent method is called when the log panel is opened for the first time.

public class LogToolWindowLoader implements ToolWindowFactory {

private UIOperations ui;
private LogFileFunctions log_func;
private ToolWindow toolWindow;
@Override
public void createToolWindowContent(Project project, ToolWindow toolWindow) {

    this.toolWindow = toolWindow;
    //this generates the log viewer GRID within the IDE
    ui = new UIOperations(toolWindow);
    ui.createTable();

    //Create new thread to retrieve data from log file. this is a continuous process, hence the new Thread
    new Thread(){
        public void run(){

            log_func = new LogFileFunctions();

            log_func.connectToLog();

            while(true){
                String[] result = log_func.getData();
                if (result!=null) {
                    //GUI updated on the Event Dispatcher Thread
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            ui.updateTable(result);
                        }
                    });
                }
                else {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }.start();}

UI operations are handled in this class:

public class UIOperations {

private ToolWindow toolWindow;
private DefaultTableModel table_model;
private JBTable table;
private final String headers[]= {"header1","header2","header3","header4","header5","header6"};

public UIOperations(ToolWindow toolWindow){
    this.toolWindow = toolWindow;
}

public void createTable(){

    JComponent panel = new JPanel(new BorderLayout()); //new BorderLayout extends panel completely inside the tool Window
    panel.setBackground(JBColor.WHITE);
    toolWindow.getComponent().add(panel);

    //set column names
    table_model = new DefaultTableModel(headers,0);
    table = new JBTable(table_model);

    JScrollPane scrollPane = new JBScrollPane(table);
    panel.add(scrollPane);

    //table formatting
    table.setShowHorizontalLines(false);
    table.setShowVerticalLines(false);
    table.setFillsViewportHeight(true);//sets table background colour

    System.out.println("Log Viewer Table Generated..");

}

public void updateTable(String[] str){

    table_model.addRow(str);

}
}

Log file processing is handled in this class:

public class LogFileFunctions {

private BufferedReader in;

//reads the log file to retrieve data.
public String[] getData(){

    //System.out.println(System.getProperty("user.dir")); gives the executing directory
    String str; //holds string read from file
    String[] result=null; //holds tokenized string

    try {
        str = in.readLine();

        if (!(str == null)) {
            result = str.split("[|]");
        }

    }catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}

//connect FileReader to log file
public void connectToLog() {

    try {
        FileReader fileReader = new FileReader(getClass().getResource("log.txt").getPath());
        in = new BufferedReader(fileReader);

        System.out.println("Log File location: " + getClass().getResource("log.txt").getPath());
        } catch (FileNotFoundException e) {
            System.out.println("ERROR: log.txt file not found at " + getClass().getResource("log.txt").getPath() );
        }
    }
}

EDIT:  I would appreciate pointers on how to optimize my code


texts: Securing React SPA with Azure AD B2C Tenant using MSAL

texts:
I am securing my React Apps and I want to use the wrapper function below to insure that the app only gets rendered when there is a successful login. I am separating the Login from future MSAL token requests because I perceive Login to be a boolean in that you are logged in or you are not logged in. If a later MSAL request requires my user to "re-login" I will call this function to reload the app:
window.location.reload(true);

This code is for React Apps only. I am only securing the app not the endpoints. This code may be used in apps that only access public endpoints.
Sample Usage:
import { WrapWithMSAL } from './MSAL_Utils'

// Will handle Auth and then render App
WrapWithMSAL(() =>
  ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root'))
);

This is the Wrapper function:
    import { Configuration, UserAgentApplication, AuthenticationParameters,
    AuthResponse, Logger, LogLevel, } from 'msal'

    // Hold the render CBR while Authenticating
    let appRender:CallableFunction = ()=>{}

    // Wrapper function to secure the Rendering of the React App
    export const WrapWithMSAL = (callbackRtn:CallableFunction) => {
      appRender = callbackRtn;
      if (!MSALAuthAgent.getAccount())
      {
        // Set the MSAL CBR specifically to Login Handler
        MSALAuthAgent.handleRedirectCallback(msalLoginResultCallback);
        const msalAuthParams: AuthenticationParameters = {
          scopes: regScopes
        };
        // Popup AD B2C Login,
        MSALAuthAgent.loginRedirect(msalAuthParams);
      }
      else // Login was retrieved from Session Cache
      {
        appRender(); // Start the app
      }
    }
    // Callbacks passed to MSAL
    /**
    * Called when the user has successfully logged in
    * (the account object is created at the time of successful login)
    * or null when no state is found
    * @returns {@link Account} - the account object stored in MSAL
    */
    const msalLoginResultCallback = (error: any, response: any) => {
      if (error) {
        alert(error.toString());
      }
      else {
        appRender(); // Start the app
      }
    }

    // MSAL Specifications
    const configuration: Configuration = {
      auth: {
        clientId: "AZURE_APPLICATION_ID",
        authority:"AZURE_USER_FLOW",
        redirectUri: "https://myspa.mysite.com",
        validateAuthority: false,
      },
      cache: {
        cacheLocation: "sessionStorage",
        storeAuthStateInCookie: true,
      },
      system: {
        logger: new Logger((logLevel, message, containsPii) => {
          console.log("[MSAL]: ", message)
        }, {
          level: LogLevel.Verbose,
          piiLoggingEnabled: false
        })
     }
   }

   const regScopes : ['read.scope'];
   const MSALAuthAgent = new UserAgentApplication(configuration);
   export default MSALAuthAgent;

My question is does this approach secure my app?


texts: Help with refactoring my Tic Tac Toe game

texts:
I'm very new to coding and have been diving into the skill with Python as my first language. One of the first programs I wrote for a class was this Tic Tac Toe game. I re-wrote this game using classes and have just recently refactored it looking for different smells. The person who's been teaching the class suggested that I post it here for additional help optimizing it. Any help or suggestions for improvement is much appreciated! Is there anything that smells funky to you?

#Written in Python 3.3.0, adapted for Python 2.7.3
#Tic Tac Toe Part 2

class Board(object):
    """Represents tic tac toe board"""
    def __init__(self):
        self.move_names = '012345678'
        self.Free_move = ' '
        self.Player_X = 'x'
        self.Player_O = 'o'
        self.moves = [self.Free_move]*9

    def winner(self):
        """Wining combinations. Returns Player or None"""
        winning_rows = [[0,1,2],[3,4,5],[6,7,8], # up and down
                        [0,3,6],[1,4,7],[2,5,8], # across
                        [0,4,8],[2,4,6]]         # diagonal
        for row in winning_rows:
            if self.moves[row[0]] != self.Free_move and self.allEqual([self.moves[i] for i in row]):
                return self.moves[row[0]]

    def allEqual(self, list):
        """returns True if all the elements in a list are equal, or if the list is empty."""
        return not list or list == [list[0]] * len(list)

    def getValidMoves(self):
        """Returns list of valid moves"""
        return [pos for pos in range(9) if self.moves[pos] == self.Free_move]

    def gameOver(self):
        return bool(self.winner()) or not self.getValidMoves()
    def startover(self):
        """Clears board"""
        self.moves = [self.Free_move]*9

class Player(object):

    """ Represents player and his/her moves"""
    def __init__(self, moves):
        self.moves = moves
        self.whoseturn = 1

    def makeMove(self, move, player):
        """Plays a move. Note: this doesn't check if the move is legal!"""
        self.moves[move] = player

class PlayerX(Player):
    """ Player X's moves"""
    def __init__(self, moves):
    self.moves = moves

    def xmove(self):
        print("Move Player X")
        move = int(input())
        if -1 < move < 9:
            for i in self.moves[move]:
                if i == ' ':
                    super(PlayerX, self).makeMove(move, "X")
                    print(self.moves)
                    return 1
            else:
                print("That's not a valid move")
                return 0
        else:
            print("That's not a valid move")

class PlayerO(Player):
    """ Player O's Moves"""
    def __init__(self, moves):
        self.moves = moves

    def ymove(self):
        print("Move Player O")
        move = int(input())
        if -1 < move < 9:
            for i in self.moves[move]:
                if i == ' ':
                    super(PlayerO, self).makeMove(move, "O")
                    print(self.moves)
                    return 1
                else:
                    print("That's not a valid move")
                    return 0
        else:
            print("That's not a valid move")

def tictac():
    print(welcome_mes)
    b = Board()
    x = PlayerX(b.moves)
    y = PlayerO(b.moves)
    p = Player(b.moves)
    while b.gameOver() == False:
        if p.whoseturn % 2 == 0:
            print("Turn #" + str(p.whoseturn))
            if x.xmove() == 1:
                p.whoseturn += 1
        else:
            print("Turn #" + str(p.whoseturn))
            if y.ymove() == 1:
                p.whoseturn += 1

    print(b.winner() + " has won the game")
    print("would you like to play again?")
    Yes, yes, YES, yeah, Yeah = "yes","yes", "yes", "yes", "yes"
    answer = input()
    if str(answer) == "yes":
        b.startover()
        tictac()
    else:
        quit()

welcome_mes = """Welcome to Tic Tac Toe!

The Game board is as follows:

    _0_|_1_|_2_
    _3_|_4_|_5_
     6 | 7 | 8   ""


texts: best design pattern for refactoring a set of classes that do calculation base on many parameters

texts:
I'm refactoring a set of classes which does some price calculations.
the calculation is done based on many parameters.
The code is :

public interface IParcel {
    int SourceCode { get; set; }
    int DestinationCode { get; set; }
    int weight{get;set;}
    decimal CalculatePrice();
}

public abstract class GeneralParcel : IParcel {
    //implementation of inteface properties
    //these properties set with in SourceCode & DestinationCode
    //and are used in CalculatePrice() inside classes that inherit from GeneralParcel
    protected SourceProvinceCode{get; protected set;}
    protected DestinationProvinceCode{get;protected set;}

    //private variables we need for calculations
    private static ReadOnlyDictionary<int, List<int>> _States_neighboureness;
    private static ReadOnlyCollection<City> _Citieslist;
    private static ReadOnlyCollection<Province> _Provinceslist;

    protected ReadOnlyCollection<City> Citieslist {get { return _Citieslist; }}

    protected ReadOnlyCollection<Province> ProvincesList {get { return _Provinceslist; }}

    protected ReadOnlyDictionary<int, List<int>> StatesNeighboureness {get {return _States_neighboureness; }}

    //constructor code that initializes the static variables

    //implementation is in concrete classes
    public abstract decimal CalculatePrice();
}

public ExpressParcel : GeneralParcel {
    public decimal CalculatePrice() {
        //use of those three static variables in calculations
        // plus other properties & parameters
        // for calculating prices
    }
}

public SpecialParcel : GeneralParcel {
    public decimal CalculatePrice() {
        //use of those three static variables in calculations
        // plus other properties & parameters
        // for calculating prices
    }
}

Right now, the code uses "Strategy pattern" efficiently.

my question is that those three static properties, really are not part of parcel object, they are need only for price calculations, so which design pattern or wrapping(refactoring), is suggested?

Is having another interface as below necessary (& then wrap those static properties inside it?, even make static that class, because it is basically only some calculations), then how to connect it to IParcel?
Doing so, how best to implement CalculatePrice() in SpecialParcel & ExpressParcel classes?

public interface IPriceCalculator {
    decimal CalculatePrice();
}


texts: Reinterpreting a string, treating < as a backspace character

texts:
I've come up with a solution to this coding challenge using recursion. In summary, the input is a string where the < symbol represents a backspace (up to 1 million characters). The output should be a string where all characters that come before a backspace or series of backspaces are removed, as well as the backspaces themselves. E.g.:

"a<bc<"        --> "b"
"foss<<rritun" --> "forritun"

Here is my solution to the problem, in Java:

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String broken_str = scanner.nextLine();
        System.out.println(fix(broken_str));
    }

    public static String fix(String broken_str) {
        int brack_idx = broken_str.indexOf('<'); // index of the first occurrence of '<'
        if (brack_idx < 0) {
            // if '<' isn't found then the string is fixed
            return broken_str;
        }

        int num_brax = 0; // keeps track of the number of brackets in the current sequence
        while (brack_idx < broken_str.length() && broken_str.charAt(brack_idx) == '<') {
            ++num_brax; ++brack_idx;
        }

        return broken_str.substring(0, brack_idx - 2 * num_brax) // this substring ranges up to the first character that should be removed
                + fix(broken_str.substring(brack_idx)); // this substring is from the index after the last occurrence of '<' until the end of the string
    }

}

This algorithm passes for the first three test cases, but the fourth one results in a runtime error. I've tried to create test cases of my own, and when the string is really long, I get a stack overflow. I was wondering if the algorithm can be optimized.

By the way, I know that this can be done very easily iteratively, but that's kind of boring. If this can only be made efficient iteratively then let me know.


texts: Protect from people bruteforcing the PHPSESSID

texts:
The PHPSESSID is stored in the clients cookie so I don't consider it as secure. Someone might bruteforce it and perform some action (like a Facebook status post) whenever a session was successfully hijacked.

I was wondering how I can protect against attacks like that. Maybe by locking out suspect users? My solution I came up with is a bit different:

<?php

    $lifetime = 1 * 60 * 60; // seconds

    // Rename the PHPSESSID cookie for convenience and set the maximum lifetime
    ini_set('session.name', 'session_id');
    ini_set('session.gc_maxlifetime', $lifetime);
    session_set_cookie_params($lifetime);

    // Create a new session if necessary
    if ( ! isset($_COOKIE['session_id'])) {
        @session_start() or die(); // Don't output anything on invalid cookie forgery attempts

        // Generate a random lowercase alphanumeric string
        $sessionKey = substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyz', 5)), 0, 32);

        // Save it in the session and as a cookie
        setcookie('session_key', $sessionKey);
        $_SESSION['session_key'] = $sessionKey;

        $_SESSION['foo'] = 'bar'; // Just for testing – see the last echo statement in this script
    }
    else {
        @session_start() or die(); // Don't output anything on invalid cookie forgery attempts

        // Destroy the session completely (including client cookies) if the session keys don't match
        if ($_SESSION['session_key'] !== $_COOKIE['session_key']) {
            session_unset();
            session_destroy();
            setcookie('session_id', null, time() - 1);
            setcookie('session_key', null, time() - 1);
        }
    }

    echo isset($_SESSION['foo']) ? $_SESSION['foo'] : 'baz';

?>

What do you think about it? Can I consider this as secure or are any other steps required? Will it protect me successfully from the described attacks?


texts:
I have an application using c# and MVC5, with RazorEngine.

My application manages requests (orders from clients) and I need to show them in tables. To achieve this I have a Controller (HistoryController), a View (Index) and a Model (MaterialRequestModel) which takes several parameters in the constructor:

 public MaterialRequestModel(MaterialRequest order, Employee aConcernedEmployee, Employee anOrderedbyEmployee, Office anOffice)

In my controller, HistoryController I have the followin Index method, which gets all requests completed or canceled:

public ActionResult Index()
        {
            IQueryable<MaterialRequest> query= DB.MaterialRequest
                .Where(m => m.MaterialStatusId == MatStatus.A9Cancelled || m.MaterialStatusId == MatStatus.A8Complete);

            List<MaterialRequestModel> model= new List<MaterialRequestModel>();
            foreach (MaterialRequest req in query)
            {
                model.Add(new MaterialRequestModel(req, DB.Employees.Find(req.ConcernedEmployeeId), DB.Employees.Find(req.OrderedByEmployeeId), DB.Offices.Find(req.OfficeId)));
            }
            return View(model);
        }

Now, I could simply pass the query to the the View, but that is not a good practice and the community (this one!) strongly suggests I use Models to avoid placing logic into the view.

However, I can't help to think that the way I am building my model sucks terribly bad, because I am iterating over a large set of results.

How can I improve this code and make it decent without the loop?

Additionally, should I pass everything as a parameter to my model, or should I just pass the DB object into the Models contructor and have it do the quereies there?


texts: Find the smallest number in the first array that is not in the second one

texts:
I have solved the given assignment below is the question and getting correct answer. but my question is is there any way to reduce the complexity of my code or there is any other efficient way to write the code?

  Given two arrays of integers output the smallest number in the first
  array not present in the second one.

  Input Specification:

  The first line contains the size N1 of the first array.   Next line
  give the contents of the first array.   Next line contains the size N2
  of the second array.   Next line give the contents of the second
  array.

  Output Format:

  Output must be a single number which is the smallest number occurring
  in the first array that does not occur in the second. In case there is
  no such number, output NO.

  Variable Constraints:

  The sizes of the arrays are smaller than 20. Each array entry is an
  integer which fits an int data type.

  Example Input:

3
2 3 4
4
1 3 5 7

  Output: 2

  Example Input:

1
1
2
1 2

  Output: NO

Here is the code.

#include<stdio.h>
int main(void)
{
    int a[10],b[10];
    int small=0,l1,l2,i,flag=0;
    scanf("%d",&l1);
    for(i=0;i<l1;i++)
    {
        scanf("%d",&a[i]);
    }
    scanf("%d",&l2);
    for(i=0;i<l2;i++)
    {
        scanf("%d",&b[i]);
    }
    small=a[0];
    for(i=1;i<l1;i++)
    {
        if(small>a[i])
          small=a[i];
    }
    for(i=0;i<l2;i++)
    {
        if(small==b[i])
          flag=1;
    }
        if(flag==1)
        {
            printf("NO");
        }
        else
        {
            printf("%d",small);
        }
   return 0;
}


texts: Speed up a while loop that conditionally creates a new variable in R

texts:
I would like to speed up the following code in R. This is a loop to define new individuals (column newID) when the variable change is equal to 1. Any idea on how to improve the loop will be greatly appreciated.

Here is the code:

## Build the data frame
dat <- expand.grid(x = 1:1000, ID = as.character(seq(0, 4000, 1)))
dat$change <- 0
dat[which(dat$x == 1), c("change")] <- 1
dat[which(dat$x == 300), c("change")] <- 1
dat[which(dat$x == 700), c("change")] <- 1
dat[1, c("change")] <- 0

## Add a column "newID"
dat$newID <- NA
index <- c(1, which(dat$change == 1), nrow(dat))
j <- 1
i <- 1

system.time(while (j < length(index)){
  print(paste(j, "/", length(index), sep = " "))
  i <- ifelse((j > 1) && (dat[index[j], c("ID")] != dat[index[j - 1], c("ID")]), 1, i)
  ## print(i)
  if(j == length(index) - 1){
    dat[seq(index[j], index[j + 1], by = 1), c("newID")] <- paste("Ind ", dat[index[j], c("ID")], "|", i, sep="")
  } else{
    dat[seq(index[j], index[j + 1] - 1, by = 1), c("newID")] <- paste("Ind ", dat[index[j], c("ID")], "|", i, sep="")
  }
  j <- j + 1
  i <- i + 1
})
## summary(dat)

Here is an example:

The input data frame has 3 columns. In particular, ID is the ID number of each individual and change takes the value of 1 when the individual is renewed.

    x ID   change
1   1  0      0
2   2  0      0
3   3  0      0
4   4  0      0
5   5  0      1
6   6  0      0
7   7  0      1
8   8  0      0
9   9  0      0
10 10  0      0
11  1  1      1
12  2  1      0
13  3  1      0
14  4  1      0
15  5  1      1
16  6  1      0
17  7  1      1
18  8  1      0
19  9  1      0
20 10  1      0
21  1  2      1
22  2  2      0
23  3  2      0
24  4  2      0
25  5  2      1
26  6  2      0
27  7  2      1
28  8  2      0
29  9  2      0
30 10  2      0

The variable newID is created as follows:
When change is equal to 1, newID takes the old ID number and the increment value. Thus, in the example, the expected result is:

    x  ID  change newID
1   1  0      0 Ind 0|1
2   2  0      0 Ind 0|1
3   3  0      0 Ind 0|1
4   4  0      0 Ind 0|1
5   5  0      1 Ind 0|2
6   6  0      0 Ind 0|2
7   7  0      1 Ind 0|3
8   8  0      0 Ind 0|3
9   9  0      0 Ind 0|3
10 10  0      0 Ind 0|3
11  1  1      1 Ind 1|1
12  2  1      0 Ind 1|1
13  3  1      0 Ind 1|1
14  4  1      0 Ind 1|1
15  5  1      1 Ind 1|2
16  6  1      0 Ind 1|2
17  7  1      1 Ind 1|3
18  8  1      0 Ind 1|3
19  9  1      0 Ind 1|3
20 10  1      0 Ind 1|3
21  1  2      1 Ind 2|1
22  2  2      0 Ind 2|1
23  3  2      0 Ind 2|1
24  4  2      0 Ind 2|1
25  5  2      1 Ind 2|2
26  6  2      0 Ind 2|2
27  7  2      1 Ind 2|3
28  8  2      0 Ind 2|3
29  9  2      0 Ind 2|3
30 10  2      0 Ind 2|3


texts: SVG filtering an image to stepped alpha grayscale

texts:
I have a variety of images that will be used in a style intended to mimic a dot-matrix LCD.

In a previous version, I rendered these images using a program and essentially ended up with a duplicate of the entire image library (quite large!) whereas for this "rebooted" version I want to take advantage of newer technologies (in browsers at least) such as SVG filters.

Here is a sample:

<svg viewBox="0 0 32 32" width="128" height="128">
    <filter id="alphapixels">
        <feColorMatrix type="luminanceToAlpha" />
        <feComponentTransfer>
            <feFuncA type="table" tableValues="1 0" />
        </feComponentTransfer>
        <feComponentTransfer>
            <feFuncA type="discrete" tableValues="0 0.5 1" />
        </feComponentTransfer>
        <feComponentTransfer>
            <feFuncA type="linear" slope="0.67" />
        </feComponentTransfer>
        <feComposite operator="in" in2="SourceGraphic" />
    </filter>
    <image filter="url(#alphapixels)" image-rendering="pixelated" xlink:href="{SRC}" width="32" height="32" />
</svg>

My logic here is:

Convert to grayscale alpha mask with the handy luminanceToAlpha function
Invert the result (so dark regions become less transparent instead of more)
Snap the grayscale to the discrete values "off, medium, on"
Scale it down to "off, faint, dark"
Composite with original image so as to restore original alpha mask (otherwise the previously transparent background counts as solid darkness)

This is my first time using SVG filters so I have no idea if I'm doing it right, but it just feels wrong somehow to have multiple <feComponentTransfer> elements each with a single <feFuncA>. Is there perhaps some way to improve this?


texts:
Given any four-digit number that consists of at least two different digits, if you form a number by arranging its digits in descending order, and subtract the number formed by arranging its digits in ascending order, and repeat, you should eventually arrive at 6174, Kaprekar's Constant.

How can I make improvements to my code? Is my code readable and simple to understand?

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <functional>

//at least two digits must be distinct
bool isUnique(int num)
{
constexpr int numlength = 4;
std::vector<int> digits;

for(int i = 0; i < numlength ; ++i)
{
    int digit = num%10;
    digits.push_back(digit);
    num /= 10;
}

//If digits are all equal return false.
if ( std::adjacent_find( digits.begin(), digits.end(), std::not_equal_to<int>() ) == digits.end() )
{
    return false;
}else
{
    return true;
}

}

int KaprekarsConstant(int num)
{
constexpr int kaprekarsConst = 6174;
constexpr int lengthRequirement = 4;

static int iteration = 0;

if(std::to_string(num).length() == lengthRequirement && isUnique(num))
{
    int difference = num;
    std::string sNum;

        while(difference != kaprekarsConst)
        {
            sNum = std::to_string(difference);

            std::sort(sNum.begin(),sNum.end(),std::greater<int>());
            int ascendNum = stoi(sNum);

            std::sort(sNum.begin(),sNum.end(),std::less<int>());
            int descendNum = stoi(sNum);

            difference = ascendNum - descendNum;

            ++iteration;
        }
}
return iteration;
}


texts: Search a directory structure for tar files and report on their size

texts:
I have a complex directory structure on my web server that includes .tar files for download; I'm trying to create a report on the size distribution of these files (e.g. 10 are less than 1MB, two are 1-2MB, three are 2-3MB...)

Is there a better way to organize this code?  I started it as a console application, writing directly in the Main() function.  Now I'm taking a step back and thinking about improvements.

public class MyClass
{
    private static int counter = 0;
    private static Dictionary<int, int> sizeMap = new Dictionary<int, int>()
        {
              {0, 0}
            , {1, 0}
            , {2, 0}
            , {3, 0}
            , {4, 0}
            , {5, 0}
            , {6, 0}
            , {7, 0}
            , {8, 0}
            , {9, 0}
            , {10, 0}
            , {20, 0}
            , {30, 0}
            , {40, 0}
            , {50, 0}
            , {75, 0}
            , {100, 0}
            , {999, 0}
        };
    static void Main(string[] args)
    {
        DirectoryInfo cloud1 = new DirectoryInfo("C:\\brian");
        IEnumerable<DirectoryInfo> cloud1Dirs = cloud1.EnumerateDirectories();
        foreach (var dir in cloud1Dirs)
        {
            ParseDir(dir);
        }
        Console.WriteLine("==================================================");
        foreach (int i in sizeMap.Keys)
        {
            Console.WriteLine(i + "M\t" + sizeMap[i]);
        }
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }

    private static void ParseDir(DirectoryInfo currentDir)
    {
        if (counter++ > 1000)
        {
            return;
        }
        IEnumerable<FileInfo> tarFiles = currentDir.EnumerateFiles("*.tar");
        if (tarFiles.Count() > 0)
        {
            foreach (FileInfo tarFile in tarFiles)
            {
                int megLength = (int)(tarFile.Length / 1048576);
                int key = MakeDictionaryKey(megLength);
                sizeMap[key]++;
                Console.WriteLine(tarFile.Name + "\t" + megLength + "M");
            }
        }
        else
        {
            foreach (var dir in currentDir.EnumerateDirectories())
            {
                ParseDir(dir);
            }
        }
    }

    private static int MakeDictionaryKey(int n)
    {
        if (n <= 10) {
            return n;
        } else if (n <= 50) {
            return (int) Math.Ceiling(n / 10.0) * 10;
        } else if (n <= 75) {
            return 75;
        } else if (n <= 100) {
            return 100;
        } else {
            //Really large file!
            return 999;
        }
    }
}


texts: CSES standard solution for range query problem giving TLE

texts:
Problem statement is from CSES
This is a standard question where we are given a list of numbers and a number of queries. For each query, you have to give the sum of numbers in the given range.
My implementation in python:
def solve():
    n,q = [int(a) for a in input().split(' ')]
    arr = [int(a) for a in input().split(' ')]

    cumulativeArray = [0] # Initial entry added to make 1 based indexing
    sum = 0
    for i in range(len(arr)):
        sum += arr[i]
        cumulativeArray.append(sum)

    for i in range(q):
        x,y = [int(a) for a in input().split(' ')]
        print(cumulativeArray[y] - cumulativeArray[x-1])

solve()

This gives the required answers correctly for all my custom test cases. But gives TLE for huge test cases.
I believe this is happening due to split() method in my code.
Any leads are welcome.


texts: BIOS Video Service INT 10H (functions 9 and 13H) functionality

texts:
On the way to protected or long modes, real mode code needs to provide a lot of visual feedback. Video services INT 10H provide this functionality, but lack fluidity. This algorithm provides write character and attribute AH = 9 and write string AH = 13H functionality, reading NULL terminated strings.

ENTER

;    BH = Video page
;    BL = Attribute
;    CX = Repeat count or NULL to read string to EOS
;    DH = Row    (0 - 24)
;    DL = Column (0 - 79)
; DS:SI = Pointer to ASCIIZ string or character to be repeated CX times

LEAVE

;    CX = Number of characters displayed
; ES:SI = Points to the next position after null. Might be next string

To achieve a result like this:

The string would be defined as:

  Title:    db  'Proto_Sys 1.00.0', 0, 0xc4

and called by:

mov     si, Title
mov     dx, 0x11e       Line 2, Column 32 (values zero indexed)
mov     bx, 13          Light Magenta
xor     cx, cx
call    528H
mov     cl, 18          Repeat 18 times
mov     bl, 15          Bright White
add     dx, 255         Row 3, Column 30
call    528H

NOTE: As this is in a boot loader, there is a lot of code before this that initializes pertinent registers.

Begin with setting ES:DI to point to proper place in video memory B8000 - BFFFF:

528  31C0      xor  ax, ax
52A  88FC      mov  ah, bh
52C  80C4B8    add  ah, VIDEO_SEG
52F  06        push     es
530  8EC0      mov  es, ax
532  88D4      mov  ah, dl
534  C1E807    shr  ax, 7
537  57        push     di
538  89C7      mov  di, ax
53A  B0A0      mov  al, 0xa0
53C  52        push     dx
53D  C1EA08    shr  dx, 8
540  F7EA      imul     dx
542  5A        pop  dx
543  01C7      add  di, ax

Read the first character and then determine if this character is to be repeated CX times or the first of a NULL-terminated string:

547  AC        lodsb
548  09C9      or   cx, cx
54A  7407      jz   553H

Do repeat process and return to caller:

54C  51        push     cx
54D  F3AB      rep  stosw
54F  59        pop  cx

; see comments restored out of order
; -------------------------------
; 550  07        pop    es   [di] 5F
; 551  5F        pop    di   [es] 07
; -------------------------------

552  C3        ret

Keep reading until null and update CL as it returns the length of the string:

553  08C0      or     al, al
555  74F9      jz     550H
557  FEC1      inc    cl
559  AB        stosw
55A  AC        lodsb
55B  EBF6      jmp     553H


texts: Keeping Linq's readability (e.g. over goto statements) without sacrificing performance

texts:
I'm doing Project Euler problem 5 as a kata, focussing on TDD and code readability. The challenge is:

  What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

For reference, a "less-readable" solution that reeks of goto was something along these lines:

public int Solution_GotoVersion()
{
    int solution = 1;
    outer: while (solution++ < int.MaxValue)
    {
        for (int j = 1; j <= 20; j++)
        {
            if (solution % j != 0)
                goto outer;
        }
        return solution;
    }
    throw new Exception("Solution not found");
}

This takes about 4 seconds to run, relative to the other solutions.

Of course I was trying to avoid such code (in light of readability, for one), I wrote it specifically to compare it to my first solution, that uses an alternative along the lines of this SO answer. So, focussing on readability, here's something a little nicer:

public int Solution_LinqVersion()
{
    var dividers = Enumerable.Range(1, 20);

    int solution = Enumerable
                    .Range(1, int.MaxValue)
                    .FirstOrDefault(candidate
                      => dividers.All(divider => candidate.IsDivisibleBy(divider)));

    return solution;
}

This performs much worse, taking about 25 seconds, relatively. I don't mind loosing some performance when traded for readability, but a factor 8 is a bit much.

I've tried an intermediate solution, like this:

public int Solution_LittleOfBothWorlds()
{
    var dividers = Enumerable.Range(1, 20).ToArray();
    int solution = 1;
    while (solution++ < int.MaxValue)
    {
        if (dividers.All(divider => solution % divider == 0))
            return solution;
    }
    throw new Exception("Solution not found");
}

This is in the middle, performance-wise, taking about 14 seconds, relatively.

What can I do to keep relative performance close to the first example, with code that utilizes Linq's readability?

Note: I'm aware there are of  many other optimizations possible, amongst others in the algorithm itself. In this case however, I'm specifically interested in the performance of Linq queries relative to more "oldskool" constructs, and how to keep Linq's nice syntax without giving up too much performance (if possible).


texts: Return all pairs from a list of integers that contributes to a given sum

texts:
Given a list of integers return all the pairs that makes up to a given
  target sum.

  input: {2, 7, 11, 15, -2}  target sum: 9

  This should return all the pairs i.e {-2,11} and {7,2}.

  If there are duplicates that form the same pair, they should not be
  returned.

Here is the code for this problem - would be great to know how this can be refactored for improvement.

   public class TwoSum {
    static class Pair {
        int x, y;

        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (o == null) {
                return false;
            }

            if (o instanceof Pair) {
                Pair other = (Pair) o;
                if (other.x == this.x && other.y == this.y) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public String toString() {
            return new StringBuilder().append("{").append(this.x).append(",").append(this.y).append("}").toString();
        }
    }

    public static Set<Pair> findPairs(int inputs[], int targetSum) {
        Set<Pair> results = new HashSet<>();
        Map<Integer, Integer> map = new HashMap<>();

        for (int number : inputs) {
            int remainingSum = targetSum - number;

            if (map.containsKey(number)) {
                results.add(new Pair(number, map.get(number)));
            } else {
                map.put(remainingSum, number);
            }
        }
        return results;
    }

To invoke this code:

System.out.println(TwoSum.findPairs(new int[]{2, 7, 11, 15, -2}, 9));

This would print:

[{-2,11}, {7,2}]

Code here:

https://github.com/Ramblers-Code/CodeKata/blob/master/src/main/java/kata/array/TwoSum.java


texts: Check whether or not write permission is enabled in a remote server

texts:
I have written C++ code which will say whether write permission is enable on the server or not using the smbclient command.

Since the smbclient command is not returning a proper error code if the remote server is read-only, my logic is that I try to write in the remote server if successful, then I display write permission is enabled and if failure, it says disabled.

After writing, I deleted it back. Please help me do it better if possible.

    #include<iostream>
    #include<stdlib.h>
    #include<fstream>
    #define SMBCLIENT "smbclient "
    #define TEMPFILE "tempfile"
    #define search  "putting file"
    enum errcode
    {
    EMPTY,
    internalError

    }errCode;

    void getUserCreditentail(std::string &username,std::string &password,std::string &server,std::string &folder,std::string &subfolder)
    {
            std::cin>>server;
            std::cin>>folder;
            std::cin>>subfolder;
            std::cin>>username;
            std::cin>>password;

    }

    void MakeCommand(std::string &command,const std::string server,const std::string folder,const std::string subfolder,const std::string username,const std::string password)
    {
            if(server.empty() ||folder.empty() || username.empty() ||  password.empty())
            {
                    errCode=EMPTY;
                    return;
            }

            command= SMBCLIENT +server +"/"+ folder + " " + "-U"+ " " + username + "%" + password + " " + "--directory=" + subfolder + " " + "-c" + " " + "'put  testfile.txt'" + " " + ">" + TEMPFILE;
    }

    bool executeCommand(const std::string command)
    {

    if(!command.empty())
            {
                    if(system(command.c_str())<0)
                    {
                            std::cout<<"system call fails"<<std::endl;
                            errCode=internalError;
                            return false;
                    }

            }

    return true;

    }

    bool checkWritePermission()
    {

            std::ifstream fin(TEMPFILE);
            std::string line;

            if (fin.is_open())
            {
                    std::string line;

                    while(getline(fin,line))
                    {
                            if (line.find(search, 0) != std::string::npos)
                            {
                                    std::cout << "found: " << search << std::endl;
                                    return true;
                            }

                    }

            }

            return false;
    }

    int main()
    {
            std::string command;
            std::string username,password;
            std::string server,folder,subfolder;
            getUserCreditentail(username,password,server,folder,subfolder);
            std::cout<<server<<" "<<folder<<" "<<username<<" "<<password<<""<<subfolder<<std::endl;
            MakeCommand(command,server,folder,subfolder,username,password);
            std::cout<<command<<std::endl;
            if(executeCommand(command))
            {
                    if(checkWritePermission())
                      {
                            std::cout<<"Write permission is enable"
<<std::endl;
                            // removefile(); /*this will remove written file*/
}                    else
                            std::cout<<"write permission is disabled"<<std::endl;

            }

            return 0;
    }


texts: Use case of nesting an enum into a struct

texts:
At our office there is this piece of code (the original author isn't employed anymore, so I can't ask him).
Does anybody have an idea, what he did there? Is there any advantage of using this struct OrderDirection instead directly the enum Direction?
(for me, this is interesting and I just wanted to know why.)
public struct OrderDirection
{
    private enum Direction
    {
        Ascending = 0,
        Descending = 1
    }

    private OrderDirection(Direction direction)
    {
        _direction = direction;
    }

    private readonly Direction _direction;

    public override string ToString()
    {
        switch(_direction)
        {
            case Direction.Ascending: return "asc";
            case Direction.Descending: return "desc";
            default: throw new NotImplementedException(nameof(_direction));
        }
    }

    public static OrderDirection Ascending = new OrderDirection(Direction.Ascending);
    public static OrderDirection Descending = new OrderDirection(Direction.Descending);
}


texts: Munging data off the World Atlas of Language Structures website

texts:
I have created a script to munge data off this tab-delimited table.

from collections import defaultdict
from utils import sync_and_read, currentdirectory

class MiniWALS(dict):
  def __init__(self, toupdate=True):
    WALS_URL = "http://wals.info/languoid.tab?sEcho=1&iSortingCols=1"+\
            "&iSortCol_0=0&sSortDir_0=asc"
    WALS_TXT = currentdirectory()+"/data/wals/wals.txt"

    wals_tsv = sync_and_read(WALS_URL, WALS_TXT, toupdate=toupdate)
    headerline, _ , data = wals_tsv.partition('\n')

    for line in data.split('\n'):
      lang = line.split()[0]
      for key, value in zip(headerline.split('\t')[1:], line.split('\t')[1:]):
        self.setdefault(lang,{})[key] = value

    self.GENUS = defaultdict(list)
    for lang in self:
      self.GENUS[self[lang]['genus']].append(lang)

    self.LANGUAGEFAMILY = defaultdict(list)
    for lang in self:
      self.LANGUAGEFAMILY[self[lang]['family']].append(lang)

    self.RELATED_LANGS = defaultdict(list)
    for lang in self:
      self.RELATED_LANGS[lang] = self.GENUS[self[lang]['genus']] + \
                                self.LANGUAGEFAMILY[self[lang]['family']]

The usage of the script is to access the WALS as such (JSON-like):

# Accessing WALS information
>>> wals = miniwals.MiniWALS()
>>> print wals['eng']
{u'glottocode': u'stan1293', u'name': u'English', u'family': u'Indo-European', u'longitude': u'0.0', u'sample 200': u'True', u'latitude': u'52.0', u'genus': u'Germanic', u'macroarea': u'Eurasia', u'sample 100': u'True'}

But there are cases where it's a little verbose, e.g:

for key, value in zip(headerline.split('\t')[1:], line.split('\t')

How code this script be improved?


texts: JavaScript Tic Tac Toe Win Checker is Wet

texts:
New to coding/Javascript. Writing a basic Tic Tac Toe game.

I'm writing the game to be dynamic, so it won't always be a three-by-three grid, but rather any size grid larger than 1. I have a completely functioning game and win checker function, but the win checker is very, very wet. I've tried several way to dry it up, but can't really find a good pattern that doesn't cause bugs or issues.

The winChecker function below receives a 2D array that could resemble something like (for a 3x3 grid):

const squares = [
  [null, null, null],
  [null, null, null],
  [null, null, null]
];

The winChecker function looks like:

const winChecker = squares => {
  for (let column = 0; column < squares.length; column++) {
    const horizontalCase = squares[column][0];
    const verticalCase = squares[0][column];
    const negativeCase = squares[0][0];
    const positiveCase = squares[0][squares.length - 1];
    if (horizontalCase !== null) {
      for (let row = 0; row < squares.length; row++) {
        if (horizontalCase !== squares[column][row]) {
          break;
        } else {
          if (row === squares.length - 1) {
            return true;
          }
        }
      }
    }
    if (verticalCase !== null) {
      for (let row = 0; row < squares.length; row++) {
        if (verticalCase !== squares[row][column]) {
          break;
        } else {
          if (row === squares.length - 1) {
            return true;
          }
        }
      }
    }
    if (negativeCase !== null) {
      for (let row = 0; row < squares.length; row++) {
        if (negativeCase !== squares[row][row]) {
          break;
        } else {
          if (row === squares.length - 1) {
            return true;
          }
        }
      }
    }
    if (positiveCase !== null) {
      for (let row = 0; row < squares.length; row++) {
        if (positiveCase !== squares[row][squares.length - 1 - row]) {
          break;
        } else {
          if (row === squares.length - 1) {
            return true;
          }
        }
      }
    }
  }
  return false;

The function above actually works, it's just real wet. Any help in drying it up would be lovely.


texts:
Trying to get to grips with Haskell. The problem is taken from here.

{-
Given Credit Card number: 49927398716

• Reverse the digits: 61789372994
• Sum the odd digits: 6 + 7 + 9 + 7 + 9 + 4 = 42 = s1
• The even digits: 1, 8, 3, 2, 9
•  Two times each even digit: 2, 16, 6, 4, 18
•  Sum the digits of each multiplication: 2, 7, 6, 4, 9
•  Sum the last: 2 + 7 + 6 + 4 + 9 = 28 = s2
• s1 + s2 = 70
• which, as it ends in zero, means that 49927398716 passes the Luhn test
-}

import Data.Char

data Validity = Valid | Invalid deriving Eq
type CardNumber = String

luhnCheck :: CardNumber -> Validity
luhnCheck s =
    let r = revDigits s
        (os,es) = oddsAndEvens r
        s1 = sum os
        s2 = sum $ map sumDigits $ map (*2) es
    in if (s1 + s2) `mod` 10 == 0 then Valid
       else Invalid

revDigits :: CardNumber -> [Int]
revDigits s = map digitToInt (reverse s)

oddsAndEvens :: [Int] -> ([Int],[Int])
oddsAndEvens is =
    let digitsWithIdx = zip is (cycle [1,2])
        odds = map fst $ filter (\d -> (snd d) == 1) digitsWithIdx
        evens = map fst $ filter (\d -> (snd d) == 2) digitsWithIdx
    in (odds,evens)

sumDigits:: Int -> Int
sumDigits i = sum $ map digitToInt $ show i

test :: Bool
test = let testData = ["49927398716", "49927398717", "1234567812345678","1234567812345670"]
    in  (map luhnCheck testData) == [Valid,Invalid,Invalid,Valid]


texts: My custom implementation of MATLAB's imdilate

texts:
I have been assigned to implement the morphological dilation operation without using MATLAB's imdilate, and I came up with the following solution:

% -------------------------------------------------------------------------
% -> Morphological Operations
% -> Image Dilation
% -------------------------------------------------------------------------

% -------------------------------------------------------------------------
% - [Pre-Operation Code: Getting Everything Ready] -
% -------------------------------------------------------------------------
% Starting With A Clean (Workspace) And (Command Window)
clear;
clc;

% Creating A Matrix Consisting Of 0s And 1s Representing A Binary Image.
binaryImage = [ ...
    0 0 0 0 0 0
    0 0 1 1 0 0
    0 0 1 1 0 0
    0 0 0 0 0 0];

% Creating A Matrix Representing Our Structuring Element
structuringElement = [ ...
  0 1 0
  1 1 1
  0 1 0];

% Getting The Number Of Rows (Height) And The Number Of Columns (Width) Of The Binary Image
[imageRows, imageColumns] = size(binaryImage);

% Getting The Number Of Rows (Height) And The Number Of Columns (Width) Of The Structuring Element
[structuringRows, structuringColumns] = size(structuringElement);

% Creating An Empty Matrix That Will Be Used To Store The Final Processed Image
dilatedImage = zeros(imageRows, imageColumns);
ref = imdilate(binaryImage, structuringElement);
% -------------------------------------------------------------------------
% - [Dilation Operation] -
% -------------------------------------------------------------------------
% Going Over Each Row In The Binary Image
for i = 1:imageRows
  % Going Over Each Column In The Binary Image
  for j = 1:imageColumns
    % If The Current Pixel Is A Foreground Pixel (1)
    if (binaryImage(i, j) == 1)
      % Going Over Each Row In The Structuring Element
      for k = 1:structuringRows
        % Going Over Each Column In The Structuring Element
        for l = 1:structuringColumns
          % If The Current Pixel In The Structuring Element Is A Foreground Pixel (1)
          if (structuringElement(k, l) == 1)
            dilatedImage(i + k - 2, j + l - 2) = 1;
          end
        end
      end
    end
  end
end

subplot(1, 3, 1), imshow(binaryImage), title('Original Image');
subplot(1, 3, 2), imshow(dilatedImage), title('Dilated Image');
subplot(1, 3, 3), imshow(ref), title('MATLAB imdilate');

I am open to any suggestions! Thank you in advance.


texts:
I am trying to get familiar with OOP JavaScript, in particular working with the prototype pattern and would love some pointers/suggestions on how to improve my code.

I think I have understood the basics well, and have been able create class instances successfully.

I am however, a little unsure on how to work with events when it comes to representing these instances with a UI. Until now, I have been binding my events in the constructor before the element gets added to the DOM, but am sure this could be improved:

function Person(data) {
    var self = this;
    self.data = data;
    self.$element = $('
' +
                          '
' + data.name + '
' +
                          '
' + data.age + '
' +
                      '
');

    // bind events
    self.$element.find('.name').click(function () {
        self.speakName();
    });

    self.$element.find('.age').click(function () {
        self.speakAge();
    });

    // render element
    $('.person-container').append(self.$element);
}

Person.prototype.speakName = function () {
    alert(this.data.name);
};

Person.prototype.speakAge = function () {
    alert(this.data.age);
};

Ideally, I'd like to be able to build up a relatively complex UI of 'people', with various controls on each person to perform actions related to that person only. Apologies for the contrived example, I'm just trying to get the concept right in my head before doing anything more real-world.

I've also included a working jsfiddle incase this is easier to work with: http://jsfiddle.net/z59q2rab/.


texts: Is this good c++ code for a pin/socket for a node editor?

texts:
I'm trying to create a node editor "framework" (IDK what to call it, but essentially there is no GUI and should be implemented using these classes), similar to rete.js, blueprints(maybe an exception since there are events involved), imnodes, etc; and am creating a pin class/struct.

A pin, in this case, is the small things you actually connect (those circles on the sides of the actual nodes which can be inputs or outputs).

I'm using a single header, since almost everything I look into recommends using it for templated classes.

I think there are many problems in the code in general, but another thing that has been bugging me is the naming of classes, enums, members, etc., especially the naming of PinType, InputOutputCount (including the members itself), IOCount.

#pragma once

#include <string>
#include <vector>

enum PinType {//Whether or not the pin takes in values from other pins(input) or gives the value to other pins(output). TODO: Name change.
    INPUT,
    OUTPUT
};

enum InputOutputCount {//How many stuff the pin can take in or give out. TODO: Name change.
    NOTHING = 0,//Is this redundant?
    ONE = 1,
    MULTIPLE = -1
};

struct PinAbstract {//An abstract superclass for the actual Pin class, in order to store it in vectors, since Pin is a templated class.
    PinType Type;
    InputOutputCount IOCount = ONE;
    virtual ~PinAbstract() = 0;//Don't know much about destructors, but this was the only thing that worked for me.
};

template <class T>
struct Pin : public PinAbstract {//The actual pin class
    std::string Title = "Title";//The title of the pin
    T* Value = nullptr;//The value passed or being given.
    std::vector<Pin<T>*> Connections = {};//The other pins this pin is connected to. NOTE: same template type since two connected pins should be of same type. So a string input only takes in from a string output.
};

template <class T>
void ConnectPins(Pin<T> *pin1, Pin<T> *pin2) {//NOTE: same template type for the same reason above.
    if (((pin1->Type == OUTPUT && pin2->Type == INPUT) || (pin1->Type == INPUT && pin2->Type == OUTPUT)) && ((int)pin1->IOCount - pin1->Connections.size() != 0 && (int)pin2->IOCount - pin2->Connections.size() != 0)) {
/*Only connect input with output and vice-versa AND make sure that connection doesn't exceed max connections of any one of the pins*/
        pin1->Connections.push_back(pin2);
        pin2->Connections.push_back(pin1);
    }
}

Any sort of help is greatly appreciated. Including things that are not even code, including naming fixes, general strategies, etc.


texts:
This is a code I wrote to find all URL within a PDF file and replace the one that matches the ID that I passed as a parameter.

It works as intended, but as I am a beginner on Java, I am sure there are some best practices that I am not following.

I am using the PDFBox library.

Here's the code:

Main.java

public class Main {

public static void main(String[] args) {
    if (args.length < 4) {
        System.err.println("Parameter missing from PHP");

    } else {
        Helper.getURL(args[0], args[1], args[2], args[3]);
    }

}
}

Helper.java

public class Helper {

public static void getURL(String oldreportid, String newreportid, String oldpdf, String newpdf) {
    PDDocument doc = null;

    try {
        doc = PDDocument.load(oldpdf);
        List allPages = doc.getDocumentCatalog().getAllPages();
        for (int i = 0; i < allPages.size(); i++) {
            PDPage page = (PDPage) allPages.get(i);
            List annotations = page.getAnnotations();
            for (int j = 0; j < annotations.size(); j++) {
                PDAnnotation annot = (PDAnnotation) annotations.get(j);
                if (annot instanceof PDAnnotationLink) {
                    PDAnnotationLink link = (PDAnnotationLink) annot;
                    PDAction action = link.getAction();
                    if (action instanceof PDActionURI) {
                        PDActionURI uri = (PDActionURI) action;
                        String oldURL = uri.getURI();

                        String reportID = oldURL.substring(oldURL.lastIndexOf("=") + 1, oldURL.length());
                        if (oldreportid.equals(reportID)) {
                            String newURI = "http://www.test.com/test.php?T=MQ==&F=" + newreportid;
                            System.out.println("Page " + (i + 1) + ": Replacing " + oldURL + " with " + newURI);
                            uri.setURI(newURI);
                        }
                    }
                }
            }
        }

        doc.save(newpdf);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (COSVisitorException e) {
        e.printStackTrace();
    } finally {
        if (doc != null) {
            try {
                doc.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
}


texts:
This is a form that I want to use to enter users into a database. I used MySQLi with prepared statements. I want to make sure that I sanitized my data well.

<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once './db_con.php';
$id = '';
$firstname = trim(filter_input(INPUT_POST, 'firstname', FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$lastname = trim(filter_input(INPUT_POST, 'lastname', FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
//$val_email = trim(filter_var($email, FILTER_VALIDATE_EMAIL));
$password = trim(filter_input(INPUT_POST, 'password', FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$password2 = trim(filter_input(INPUT_POST, 'password2', FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$errs = array();
#firstname
    if ( empty($firstname) ) {
        $errs[] = "Please enter your first name.";
    }

#lastname
    if ( empty($lastname) ) {
        $errs[] = "Please enter your last name.";
    }

#email
    if ( empty($email) ) {
        $errs[] = "Please enter your E-mail.";
    }
#email validation
    if ( !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
        $errs[] = "Please enter a valid E-mail.";
    }
#check if email exist
    if ( email_exists($email,$connection) ) {
        $errs[] = "Your E-mail already exists.";
    }

#password
    if ( empty($password) ) {
        $errs[] = "Please enter your password.";
    } elseif (strlen($password) < 6) {
        $errs[] = "Your password must have  at least 6 characters.";
}
#confirm-password
    if ( empty($password2) ) {
        $errs[] = "Please confirm your password.";
    } else {
#matching-passwords
        if ( $password !== $password2 ) {
            $errs[] = "Password didn't match.";
        }
    }

if ( !empty( $errs ) )  {
    foreach ($errs as $err) {
        echo $err.'</br >';
    }
} else {

    $hashed_password = password_hash($password, PASSWORD_DEFAULT);

    $stmt = $connection->prepare("INSERT INTO `users` (`id`, `firstname`, `lastname`, `email`, `password`) VALUES (?, ?, ?, ?, ?)");
    $stmt->bind_param("sssss", $id, $firstname, $lastname, $email, $hashed_password);
    if ( !($stmt->execute()) ) {
        echo "There was a problem, Try again.";
    } else {
        echo "You are registered now.";
    }

    $stmt->close();
}
#functions
function email_exists($email,$connection) {

    $stmt = $connection->prepare("SELECT * FROM `users` WHERE `email` = ?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result();
    $rows_count = $result->num_rows;

    if ( $rows_count >= 1 ) {
        return TRUE;
    }

    $stmt->close();
}


texts: Generic solution to Hibernate's CriteriaQuery

texts:
I've created a little class which helps me to generalize many query cases. I have a DbService and couple methods in it. Can you please take a look at it and share some criticism? The usage of that tool is really easy to me and I do wanna know how to make it safer and optimized.
 public class DbServiceImpl implements DbService {
 private final EntityManager entityManager;

 @Inject
 DbServiceImpl(EntityManager entityManager) {
    this.entityManager = entityManager;
 }

 @Override
 public <T, V> List<V> createQuery(Query<T, V> query) {
    try {
        if (query != null && query.entityType.getAnnotation(Entity.class) != null) {
            CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

            CriteriaQuery<V> criteriaQuery = criteriaBuilder.createQuery(query.returnType);

            query.root = criteriaQuery.from(query.entityType);
            query.builder = criteriaBuilder;

            Selection<? extends V> selection = query.select(); //invokes on the call.
            Predicate predicate = query.where();

            if (predicate != null) {
                criteriaQuery.where(predicate); //null of a simple select without where clause.
            }

            criteriaQuery.select(selection); //null is allowed, it will just select * from the entity

            return entityManager.createQuery(criteriaQuery).getResultList();
        }
      } catch (Exception exception) {
        exception.printStackTrace();
      }
    return new ArrayList<>(); //a simple check of .isEmpty() covers that case.
   }

 @SuppressWarnings("unchecked")
 public abstract static class Query<T, V> {
    private final Class<T> entityType;
    private final Class<V> returnType;

    //accessible only by outer class's methods.
    private Root<T> root;
    private CriteriaBuilder builder;

    public Query() {
        this.entityType=(Class<T>) ((ParameterizedType) getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0];
        this.returnType=(Class<V>) ((ParameterizedType) getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0];
    }

    protected abstract Selection<? extends V> select();

    protected abstract Predicate where();

    protected Root<T> root() {
        return root;
    }

    protected CriteriaBuilder builder() {
        return builder;
    }
  }
}

And here are some use cases. Passed types are as follows - entity & return (User,Long)
  List<Long> data = dbService.createQuery(new DbServiceImpl.Query<User, Long>() {
        @Override
        protected Selection<? extends Long> select() {
            return builder().count(root());
        }
        @Override
        protected Predicate where() {
            return root();
        }
    });

This allows me to find the count of a table so understandable, without a line of pure HQL or any SQL code. One more example:
  List<User> users = dbService.createQuery(new DbServiceImpl.Query<User, User>() {
        @Override
        protected Selection<? extends User> select() {
            return root();
        }

        @Override
        protected Predicate where() {
            return builder().equal(root().get("username"), username);
        }
    });

One with a join:
    List<Recipe> recipeList= dbService.createQuery(new DbServiceImpl.Query<Recipe, Recipe>() {
        @Override
        protected Selection<? extends Recipe> select() {
            return null;
        }

        @Override
        protected Predicate where() {
            root().fetch("author", JoinType.LEFT);
            return builder().equal(root().get("author").get("id"),user.getId());
        }
    });

The code structure remains the same but the query is different. The access modifiers I used allowed me to encapsulate the behaviour on the use cases. What do you think? How broken or useful is that?


texts: Streamlining button creation and Handlers

texts:
The code below works but I can not help thinking it's to 'clunky' and could be so much tidier! It's the first time I've really used AddHandler so go gentle...

Create button(s) and handler(s)

The code below works but I can not help thinking it's to 'clunky' and could be so much tidier! It's the first time I've really used AddHandler so go gentle...

Create button(s) and handler(s)

 Public Sub AddChartRow(ByVal button As String, ByVal id As String, ByVal high As Integer, ByVal medium As Integer, ByVal low As Integer, ByVal na As Integer)

    ChartArea.Controls.Add(New LiteralControl("
"))

    Dim btnButton As New Button() With {.Text = button, .ID = id, .CssClass = vRiskActive}

            If id = "btn1" Then
                AddHandler btnButton.Click, AddressOf btnItem_1 ' here Buttons is your Handler
            End If
            If id = "btn2" Then
                AddHandler btnButton.Click, AddressOf btnItem_2 ' here Buttons is your Handler
            End If
            If id = "btn3" Then
                AddHandler btnButton.Click, AddressOf btnItem_3 ' here Buttons is your Handler
            End If
If id = "btn4" Then
                AddHandler btnButton.Click, AddressOf btnItem_4 ' here Buttons is your Handler
            End If
If id = "btn5" Then
                AddHandler btnButton.Click, AddressOf btnItem_5 ' here Buttons is your Handler
            End If
If id = "btn6" Then
                AddHandler btnButton.Click, AddressOf btnItem_6 ' here Buttons is your Handler
            End If
If id = "btn7" Then
                AddHandler btnButton.Click, AddressOf btnItem_7 ' here Buttons is your Handler
            End If
If id = "btn8" Then
                AddHandler btnButton.Click, AddressOf btnItem_8 ' here Buttons is your Handler
            End If

If id = "btn9" Then
                AddHandler btnButton.Click, AddressOf btnItem_9 ' here Buttons is your Handler
            End If
If id = "btn10" Then
                AddHandler btnButton.Click, AddressOf btnItem_10 ' here Buttons is your Handler
            End If

            If (oGetPercentages.SumTotal) <> "0" Then
                ChartArea.Controls.Add(btnButton)
                ChartArea.Controls.Add(New LiteralControl("
" & "
" & "
" & high & "
" & "
" & medium & "
" & "
" & low & "
" & "
" & na & "
" & "
" & "
" & "
"))
            Else
                ChartAreaGrey.Controls.Add(btnButton)
                ChartAreaGrey.Controls.Add(New LiteralControl("
" & "
" & "
" & "
" & "
"))
            End If

        End Sub

 Public Sub btnItem_1()
            Call PrcDash2("1")
        End Sub

        Public Sub btnItem_2()
            Call PrcDash2("2")
        End Sub

        Public Sub btnItem_3()
            Call PrcDash2("3")
        End Sub

 Public Sub btnItem_4()
            Call PrcDash2("4")
        End Sub

 Public Sub btnItem_5()
            Call PrcDash2("5")
        End Sub

 Public Sub btnItem_6()
            Call PrcDash2("6")
        End Sub

 Public Sub btnItem_7()
            Call PrcDash2("7")
        End Sub

 Public Sub btnItem_8()
            Call PrcDash2("8")
        End Sub

 Public Sub btnItem_9()
            Call PrcDash2("9")
        End Sub

 Public Sub btnItem_10()
            Call PrcDash2("10")
        End Sub


texts: Haskell Either Applicative where the Left is a Monoid

texts:
I'm currently learning Haskell and are starting to understand applicatives.

I wanted an Either Applicative where the left side is a monoid, so I created this as a learning exercise:

module EitherLeftMonoid where

data Error = InvalidName | UnderAge deriving (Eq, Show)

data Person = Person { name :: String, age :: Int } deriving (Eq, Ord, Show)

data EitherLeftMonoid a b = Lefty a
                          | Righty b
                          deriving (Eq, Show)

instance Monoid a => Functor (EitherLeftMonoid a) where
  fmap _ (Lefty a)  = Lefty a
  fmap f (Righty b) = Righty (f b)

instance Monoid a => Applicative (EitherLeftMonoid a) where
  pure                  = Righty
  Lefty a <*> Lefty a'  = Lefty (a `mappend` a')
  Lefty a <*> _         = Lefty a
  _ <*> (Lefty a)       = Lefty a
  Righty f <*> Righty x = Righty (f x)

mkName :: String -> EitherLeftMonoid [Error] String
mkName "" = Lefty [InvalidName]
mkName s  = Righty s

mkAge :: Int -> EitherLeftMonoid [Error] Int
mkAge n | n < 13    = Lefty [UnderAge]
        | otherwise = Righty n

mkPerson :: String -> Int -> EitherLeftMonoid [Error] Person
mkPerson n a = Person <$> mkName n <*> mkAge a

Output:

Prelude> mkPerson "" 12
Lefty [InvalidName,UnderAge]
Prelude> mkPerson "" 13
Lefty [InvalidName]
Prelude> mkPerson "Gerd" 13
Righty (Person {name = "Gerd", age = 13})

What could I have done different to make the code better?


texts: Deleting an item from a Set while iterating

texts:
I know that I'm not suppose to alter a Set as I iterate through it. But, in the deleteRow(String counter, String reading) method below, doing just that will make the code much more readable and clean.

Because there is so little going on in each iteration:

Is there risk?
Would a respectable coder look at that and consider it flawed?

public static class DbTable {
    public enum Columns { counter, reading, dfEng }
    public Set<Row> tableData = new HashSet<Row>();

    public void deleteRow(String counter, String reading) {
        Row deleteThis = null;
        for(Row r : tableData) {
            if((r.counter.equals(counter)) && (r.reading.equals(reading))) {
                // tableData.remove(r); <--- I want to do this, and
                // return; <--- then this to have cleaner code.
                deleteThis = r;
                break;
            }
        }
        if(deleteThis != null) { tableData.remove(deleteThis); }
    }
            public static class Row {
                public final String counter, reading, dfEng;
                public Row(String counter, String reading, String dfEng) {
                    this.counter = counter;
                    this.reading = reading;
                    this.dfEng = dfEng;
                }

                public String toString() {
                    String s = counter + String.valueOf((char) 9) + reading + String.valueOf((char) 9) + dfEng;
                    return s;
                }
            }
}


texts:
I'm looking for criticism on the following method used to determine if a collection of Integers forms an arithmetic sequence:

    public boolean isArithmetic(Integer[] nums)
{
    Set<Integer> result = new HashSet<Integer>();
    Collections.sort(Arrays.asList(nums));
    for(int i = 0; i < nums.length -1; i++)
    {
        result.add(nums[i+1] - nums[i]);
    }

    if(result.size() > 1)
        return false;
    return true;
}


texts:
How can I make this more functional?

#collects the first x + 1 items of a collection as long as predicate is true
#returns the a tuple containing a list of x+1 items and
# the rest of the collection
def collect(source, target \\ []) do
    [h|t] = source
    if predicate(h) do
        target = target ++ [h]
        collect(t, target)
    else
        if target != [] do
            target = target ++ [h]
            {target, t}
        else
            {[], source}
        end
    end
end


texts: Get an integer as input and display Pi rounded to that amount of decimal places

texts:
I've been writing a program that accepts an integer as input and displays the number Pi rounded to that number. The only issue I see is the fact that the Math.Round method can only round up to 15 spaces and there's no try-catch for that ArgumentOutOfRange exception. I'm also not sure how safe it is letting your flow of execution rely on a try-catch statement.

class Program
{
    public static int counter = 0;
    static void Main(string[] args)
    {
        Console.WriteLine("Welcome to the Pi-Rounder! Find Pi to up to 15 decimal places!");
        Console.WriteLine("Please enter the number of decimal places you'd like to round to.");
        int roundTo;
        do
        {
            string digitAsString = Console.ReadLine();
            roundTo = ConversionLoop(digitAsString);
        }
        while(roundTo == 0 && counter != 5);
        if(counter == 5)
        {
            throw new FormatException();
        }
        else
        {
            double piRounded = Math.Round(Math.PI, roundTo);
            Console.WriteLine(piRounded);
            Console.ReadLine();
        }
    }

    static int ConversionLoop(string digitString)
    {
        try
        {
            int digit = Convert.ToInt32(digitString);
            return digit;
        }
        catch(FormatException)
        {
            counter++;
            Console.WriteLine("That was not a valid number. Please try again.");
            return 0;
        }
    }
}


texts: SQL Query based on results of preceding query

texts:
I am bashing my head against how to solve this puzzle. Is there any super SQL expert out there who can lend some help

I have a database with the following structure.

adjlevel | scheme | holder | client | companyID | Rate

A rate can be held in this table based on either of the following keys

level  + companyId + scheme + holder + client

OR

level  + companyId + scheme + holder

OR

level  + companyId + scheme

OR

level  + companyId

There will always be one record per level. Therefore I need to write a query that looks at all the above criteria starting with the most filters first and eliminating one criteria at a time until I find the required record.

I have written a function to do the above but it is very slow.

ALTER FUNCTION [dbo].[Hourly_Rate]
(
-- Add the parameters for the function here
@ncompanyid numeric(2),
@client varchar(100),
@scheme varchar(100),
@holder varchar(100),
@nadjlevel numeric(3),
@date_done datetime
)
RETURNS Numeric(5)
AS
BEGIN
DECLARE @cmode varchar(1),
@level numeric(1),
@counter numeric(1),
@retval Numeric(5)

set @cmode = 'T'
set @level = 1  -- @level controls which filter to apply
set @counter = 1 -- @counter = loop counter

WHILE @counter <= 9
BEGIN

SELECT @retval = bhrlyrate FROM [MYDATABASE].dbo.inv_hrrate WHERE
adjlevel = @nadjlevel
and cmode = @cmode
and companyid = @ncompanyid
and client =  case when @level <= 3 THEN @client ELSE '' END
and scheme = case when @level <= 2  THEN @scheme ELSE '' END
and holder = case when @level <= 1 THEN @holder ELSE '' END
and dworkedfrom <= case when @date_done = '30 december 1899' then dworkedfrom ELSE    @date_done END
and dworkedto >= case when @date_done = '30 december 1899' then dworkedto ELSE @date_done END

IF @@rowcount > 0  -- Break if record found
BREAK;

IF @level = 4
-- T search mode unsuccessful change to A mode,
-- Reassign a few variables
--- and continue searching
BEGIN
SET @cmode = 'A'
SET @level = 0
set @date_done = '30 december 1899'
END

S ET @counter  = @counter + 1
SET @level = @level + 1

END

-- If record is not in hourly rate table then pick default rate from company

IF @counter = 8 and @retval = 0
SELECT @retval = CO.hrly_rate FROM company CO WHERE companyid =
@ncompanyid;

RETURN @retval

END


texts: Object Oriented programming in lua in a functional fashion

texts:
I've discovered (and written) a way to object oriented programming in lua in a functional fashion instead of the standard metatable method. It carries much more functionality (no pun intended) but I'm afraid it might dent performance. My goal is to experiment with servers written in lua and I wanted to use this OOP solution. Anyways here's my class.lua which holds the functions for creating new objects and subclasses.

local classes = setmetatable({}, {__mode = "k"}) -- Allow the GC to empty this as needed.

function class(f, super)
    classes[f] = {super = super}
end

function new(f, obj, ...)
    local fenv = getfenv(f)
    if type(obj) ~= "table" then
        error("bad argument: expected table, got " .. type(obj) , 2)
    end

    if classes[f] and classes[f].super then
        new(classes[f].super, obj, ...)
        local super = obj
        obj = setmetatable({}, { __index = super })
        obj.super = super
    else
        setmetatable(obj,{__index = fenv})
    end

    obj.this = obj
    setfenv(f, obj)
    f()
    setfenv(f, fenv)

    if obj.init then
        obj.init( ... )
    end

    return obj
end

Usage is fairly simple. Take the following example:

function Person()
    local privateVar = math.random()
    age, name, gender = nil, nil, nil

    function init(age, name, gender)
        this.age = age
        this.name = name
        this.gender = gender
    end

    function getPrivateVar()
        return privateVar
    end
end

And to create the object

obj = new(Person, {}, "John", 30, "male")

Subclassing is also simple

function Female()
    function init(name, age)
        super.init(name, age, "female")
    end
end

class(Female, Person)

Note that you only have to call a function to make something a class if you want to subclass.

When you call new, you pass in the class, object, and arguments. The object's metatable is set to have an index of the class's original environment. Then the super is determined and created. Then the class function is run to create all instance values. Next, init is called.

One advantage of this over metatables are that other code can't change the class and have it change all instances of that class that already exist and that will exist. Now the only way to do something like that is to swizzle and hack around in environments to make sure all references to the class refer to the hacker's dummy class.

On top of that, it supports private variables. Just declare them locally in your class function and you're good to go.

But I'm wondering if the fact that it creates all the instance methods once for every instantiation is going to cause performance problems. Will this eat memory? What can go wrong here?

Also, there's an issue. While this supports multiple inheritance, if you call a super method, and the super calls a method, it will call the method of the super class, not the instantiated class. Any ideas how to make it call from the instantiated class?


texts:
I am making a Pacman Player vs Player game in Java, and I am wondering if I am implementing movement in the game in a good manner. Anything that will reduce the code size or make the program faster is wanted.

Note that the game is not complete, and I have only worked on player one (Pacman). I have not started on player two (ghost) or the maze or pellets.

Movable Interface

public interface Movable {
void move();
}

Pacman / Player One Class

This class represents player one, or the Pacman character. It implements Movable.

@Override
public void move() {
    if (this.direction == Direction.UP) {
        y -= 5;
    } else if (this.direction == Direction.DOWN) {
        y += 5;
    } else if (this.direction == Direction.RIGHT) {
        x += 5;
    } else if (this.direction == Direction.LEFT) {
        x -= 5;
    }
    create();
}

Game Class

In this class, I make a global type of my Pacman class, and I instantiate it in the Game's initialize method.

Pacman pacman;

I also make a global array of my Movable interface with a size of one (for now) which will hold my pacman instance.

private Movable mover[] = new Movable[1];

Then:

@Override
public void actionPerformed(ActionEvent e) {

    if (running) {
        moveEntities();
    }

    this.repaint();
}

private void moveEntities() {

    initializeMovement();

    for (Movable m : mover) {
        m.move();
    }
}

private void initializeMovement() {
    mover[0] = pacman;
}


texts: Uploading captured video to Google Cloud Storage

texts:
My program intends to capture video streams (in mjpeg) with OpenCV and upload the captured frames into Google Cloud Storage for later processing. I am expecting to capture ~15-20 frames per second and expected to upload nearly all frames within a second.

The trick is, the script is running on Raspberry Pi 3, Model B, and together with Python, I found that it has some serious I/O limitation in terms of frames loading and uploading.

I use multiple threads to handle the loads and still find it is taking too long for uploading tasks. My current internet has a constant uplink speed of 30mbps. The received frame size is reduced to ~500KB.

I am thinking of instead of saving the frames locally, treat the loaded frames as an bytestring array and parse it accordingly in the server. But as of now, I would much prefer perhaps there is a much better solution which I am not awared of.

Below is the code I've written.

import os
import cv2
import sys
import time
import imutils
import datetime
from threading import Thread
from google.cloud import storage
from imutils.video import VideoStream

if sys.version_info < (3, 0):
    from Queue import Queue
else:
    from queue import Queue

BUCKET_NAME = "name"
RPID_FOLDER = "rpi1"
BY_DAY = datetime.datetime.now().strftime("%Y-%m-%d")

LOCAL_STREAM_MJPEG = "http://0.0.0.0:8080/stream/video.mjpeg"
REMOTE_STREAM_MJPEG = "http://address:8080/stream/video.mjpeg"

QUEUE_SIZE = 120
grabbedFrames = Queue(QUEUE_SIZE)
processFrames = Queue(QUEUE_SIZE)

class FrameGraber(Thread):

    def __init__(self):
        Thread.__init__(self)
        self.vs = VideoStream(src=REMOTE_STREAM_MJPEG).start()

    def run(self):
        global grabbedFrames

        while True:
            frame = self.vs.read()
            if frame is not None:
                grabbedFrames.put(frame)
                time.sleep(0.05)

class GCSUploader(Thread):

    def __init__(self):
        Thread.__init__(self)
        self.start()
        self.client = storage.Client()
        self.bucket = self.client.get_bucket(BUCKET_NAME)

    def run(self):
        global processFrames

        while True:
            if (not processFrames.empty()):
                fileName = processFrames.get()
                try:
                    blob = self.bucket.blob("{}/{}/{}".format(RPID_FOLDER, BY_DAY, fileName))
                    blob.upload_from_filename(os.path.abspath(fileName))
                    print("{} uploaded to {}/{}".format(fileName, RPID_FOLDER, BY_DAY))
                    os.remove(fileName)
                except Exception as e:
                    print(e)

class FrameProcessor(Thread):

    def __init__(self):
        Thread.__init__(self)
        self.secondCount = 0
        self.startSecond = datetime.datetime.now().second

    def run(self):
        global grabbedFrames, processFrames
        while True:

            if datetime.datetime.now().second != self.startSecond:
                self.startSecond = datetime.datetime.now().second
                self.secondCount = 0

            if (not grabbedFrames.empty()):
                frame = grabbedFrames.get()
                self.secondCount = self.secondCount + 1
                frame = imutils.resize(frame, 800)
                fileName = "{}-{}.png".format(datetime.datetime.now().strftime("%H-%M-%S"), self.secondCount)
                cv2.imwrite(fileName, frame)
                time.sleep(0.1)
                processFrames.put(fileName)

if __name__ == "__main__":

    uploader1 = GCSUploader()
    uploader2 = GCSUploader()
    uploader3 = GCSUploader()
    uploader4 = GCSUploader()
    uploader5 = GCSUploader()
    uploader6 = GCSUploader()
    uploader7 = GCSUploader()
    uploader8 = GCSUploader()

    frameGrabber = FrameGraber()
    frameProcessor = FrameProcessor()

    frameGrabber.start()
    frameProcessor.start()

    frameProcessor.join()
    frameGrabber.join()
    uploader1.join()
    uploader2.join()
    uploader3.join()
    uploader4.join()
    uploader5.join()
    uploader6.join()
    uploader7.join()
    uploader8.join()


texts:
I am using netty to connect to multiple servers, query for an item, and aggregate the results in my Client. The code below works, but I am unsure if it is the best way of achieving my goal. Also, I'm not sure how I should be closing the futures. Could anyone provide guidance? I am using Netty 4.0.25.Final.

   public void start() throws InterruptedException
   {
      EventLoopGroup group = new NioEventLoopGroup();
      try
      {
         ArrayList<ChannelFuture> channelFutures = null;
         for (MyServerOject server : servers)
         {
            Bootstrap b = new Bootstrap();
            b.group(group)
                     .channel(NioSocketChannel.class)
                     .option(ChannelOption.TCP_NODELAY, true)
                     .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception
                        {
                           ChannelPipeline pipeline = ch.pipeline();
                           pipeline.addLast(new myClientHandler(server));
                        }
                     });

            channelFutures = new ArrayList<>();
            ChannelFuture future = b.connect(server.getHost(), server.getPort()).sync();
            channelFutures.add(future);
         }

         for (ChannelFuture channelFuture : channelFutures)
         {
            channelFuture.channel().closeFuture().sync();
         }
      }
      finally
      {
         group.shutdownGracefully();
      }


texts:
Below are two ways to calculate an element of the Fibonacci series. Can someone please help me understand why fib1 is so much faster (over 60x) than fib2 ?

fib2 is just an assignment and a decrement whereas fib1 involves a dictionary and recursive calls.

Thanks.

import timeit

def fib1(n, lookup=dict()):
    if n == 0 or n == 1:
        lookup[n] = n

    elif n not in lookup:
        lookup[n] = fib1(n - 1, lookup) + fib1(n - 2, lookup)

    return lookup[n]

def fib2(n):
    n1, n2 = 0, 1
    while n > 1:
        n -= 1
        n1, n2 = n2, n1 + n2
    return n2

n = 1000
recursive = timeit.timeit(lambda: fib1(984), number=n)
iterative = timeit.timeit(lambda: fib2(984), number=n)

print("recursive:", recursive)
print("iterative:", iterative)

"""
output:

recursive: 0.0012962640030309558
iterative: 0.08570116000191774
"""


texts:
I am making a program that scores a scrabble game and this is the basic version of it.  Eventually I am going to add logic for double and triple words and letters but before I get too ahead of myself I would like to improve the basic code here.

All suggestions are welcome.

def main():
    letter_val = {" ": 0, "a": 1, "b": 3, "c": 3, "d": 2, "e": 1, "f": 4, "g": 2, "h": 4, "i": 1, "j": 8, "k": 5, "l": 1, "m": 3, "n": 1, "o": 1, "p": 3, "q": 10, "r": 1, "s": 1, "t": 1, "u": 1, "v": 4, "w": 4, "x": 8, "y": 4, "z": 10}
    players = []
    """ Function to add as many players as there are in the game (uses scrabble rules for limit) """
    def add_players():
        while True:
            while len(players) < 4:
                pla = input("Enter Player Names (can have up to 4) >>> ")
                if pla:
                    if pla not in (a for i in players for a in i):
                        if len(players) < 4:
                            players.append([pla, 0])
                            print("Player {} added".format(pla))
                        else:
                            return
                    else:
                        print("Name already in players' list")
                else:
                    return
            else:
                return

    def home(undo_ind=False):
        option = input('Would you like to [A]dd a score, [V]iew scores, [U]ndo the last change, or [End] the game? > ')

        class Score:
            def __init__(self):
                global temp_v, temp_p
                player = temp_p = input("Enter player to score>>> ")
                if player in (a for i in players for a in i):
                    try:
                        word = input("Enter word to score>>> ")
                        value = temp_v = sum(letter_val[i.lower()] for i in word)
                    except KeyError:
                        print("Word must consist of letters only.")
                        Score()
                    for i in players:
                        if i[0] == player:
                            i[1] += value
                else:
                    print("Player entered is not in player list.")
                home()

            @staticmethod
            def undo():
                try:
                    for i in players:
                        if i[0] == temp_p:
                            i[1] -= temp_v
                            home(True)
                except NameError:
                    print("No changes have been made.")
                    home()

            @staticmethod
            def view_scores():
                for i in players:
                    print("Player %s has a score of %d" % (i[0], i[1]))
                home(undo_ind)
        if option.lower() == "a":
            Score()
        elif option.lower() == "v":
            Score.view_scores()
        elif option.lower() == "u" and undo_ind is False:
            Score.undo()
        elif option.lower() == "u" and undo_ind is True:
            print("No changes have been made.")
            home(True)
        elif option.lower() == "end":
            print("Final scores are:")
            for i in players:
                print("Player {} has a final score of {}.".format(i[0], i[1]))
        else:
            print("That is not a valid option.")
            home(undo_ind)
    add_players()
    home()

if __name__ == "__main__":
    main()


texts: Proper Python type-annotation for variable parameters signature of inherited method overloads

texts:
I have a CLI lib to automate batch operations on GitHub with many use cases: create issue, merge PR, delete branch... Since they all operate in a similar fashing regarding execution and output, I created a parent class GhUseCase as follows:
class GhUseCase:
    # ...other methods
    def action(self, github_repo: str, *args: Any, **kwargs: Any) -> None:
        """Execute some action in a repo."""
        raise NotImplementedError  # pragma: no cover

    def execute(
        self, *args: Any, **kwargs: Any
    ) -> Union[res.ResponseFailure, res.ResponseSuccess]:
        """Execute GitHubUseCase."""
        if self.github_repo:
            self.action(self.github_repo, *args, **kwargs)
        else:
            for github_repo in self.config_manager.config.github_selected_repos:
                self.action(github_repo, *args, **kwargs)
        return self.generate_response()

Basically, each use cases inherits this class and implements action method. Eg. overload 1 (delete branch):
import git_portfolio.use_cases.gh as gh

class GhDeleteBranchUseCase(gh.GhUseCase):
    """Github delete branch use case."""

    def action(self, github_repo: str, branch: str) -> None:  # type: ignore[override]
        """Delete branches."""
        github_service_method = "delete_branch_from_repo"
        self.call_github_service(github_service_method, github_repo, branch)

Eg. overload 2 (create issue) with another signature:
import git_portfolio.domain.issue as i
import git_portfolio.use_cases.gh as gh

class GhCreateIssueUseCase(gh.GhUseCase):
    """Github create issue use case."""

    def action(  # type: ignore[override]
        self, github_repo: str, issue: i.Issue
    ) -> None:
        """Create issues."""
        github_service_method = "create_issue_from_repo"
        self.call_github_service(github_service_method, github_repo, issue)

Full use-cases code can be found in this branch (files with names stating in gh).
The problem is that each use case's action has a different list of parameters and I could not find a proper way to declare the type annotations without having type: ignore[override] errors on Mypy.
Any ideas of how to make this better and properly annotated?


texts: Return total balance from unsorted purchase items that may have specials

texts:
This is in reference to the following kata:

  Item   Unit      Special
         Price     Price
  --------------------------
    A     50       3 for 130
    B     30       2 for 45
    C     20
    D     15

I am still trying to learn F#.
Thus, I have the following code:

module Checkout

(*Types*)
type Type = A | B | C | D
type Total = { UnitPrice:int ; Qty:int }
             member x.Price() = x.UnitPrice * x.Qty

type Special =
    | None
    | ThreeForOneThirty
    | TwoForFourtyFive

type Item =
    { Type:Type
      Total:Total
      Special:Special }

      member x.Price() =
        match x.Special with
        | ThreeForOneThirty ->
            if x.Total.Qty / 3 > 0
            then (x.Total.Qty / 3) * 130
            else x.Total.Price()

        | TwoForFourtyFive ->
            if x.Total.Qty / 2 > 0
            then (x.Total.Qty / 2) * 45
            else x.Total.Price()

        | None -> x.Total.Price()

(*Private Functions*)
let private getTypeQty itemsOfType =
    itemsOfType |> Seq.sumBy(fun x -> x.Total.Qty)

let private consolidate group acc =

    let first = group |> Seq.head
    { Type    =  first.Type;
      Total   = { Qty=group |> getTypeQty; UnitPrice=first.Total.UnitPrice }
      Special = first.Special; } :: acc

(*Tests*)
open FsUnit
open NUnit.Framework

[<Test>]
let ``buying (2) A units, (1) B unit, (1) A unit = $160`` () =

    // Setup
    let a2 = { Type=A; Total={UnitPrice=50; Qty=2}; Special=ThreeForOneThirty }
    let b =  { Type=B; Total={UnitPrice=30; Qty=1}; Special=TwoForFourtyFive  }
    let a =  { Type=A; Total={UnitPrice=50; Qty=1}; Special=ThreeForOneThirty }

    seq [a2; b; a] |> Seq.groupBy (fun item -> item.Type)
                   |> Seq.map snd
                   |> Seq.fold(fun consolidated group -> consolidate group consolidated) []
                   |> Seq.sumBy (fun item -> item.Price())
                   |> should equal 160

I don't like the way I coupled the Specials DU to an item. Thus, my implementation enables an item to reference any special. It just feels wrong...


texts: Error checking in function that returns reference to static variable

texts:
This is a function I just wrote with the goal to only load the desired information once, and if an error occurs, save it for later so that it can always be reported to a calling function.

It does what I want but seems quite clumsy.

std::wstring const& GetMuiFilePath()
{
    static DWORD error = NO_ERROR;
    static std::wstring muiPath;
    static bool hasTriedLoadingPath = false;
    if (!hasTriedLoadingPath) {
        try {
             muiPath = Internal::zGetSpecialFolder(zCsidlSystem)
                     + L"\\" + Internal::zGetSystemLocale()
                     + L"\\" + L"wbadmin.exe.mui";
        }
        catch (AutoWinError const& e) {
            error = e.m_error;
        }
        hasTriedLoadingPath = true;
    }

    if (muiPath.empty()) {
        throw AutoWinError(error);
    }

    return muiPath;
}


texts:
Using an Atmega328P clocked externally @ 16 MHz, this procedure is analogous to delay routines that use instructions to consume clock cycles. The intent of this algorithm is not to advocate any particularly usefulness, but rather an alternative method of implementing delays in an application. The caveat would be a resolution of 10 ms and a range of 10 ms to 2.55 sec, with an accuracy of +/- 0.5%, but this is accomplished by using data that is only 8 bits wide.

The objective is to address the desired functionality based on a few simple criterion using the least amount of instructions.

        .def    TEMP        = R16
        .def    Iterations  = R17

        .equ       ZF = 1           ; Zero flag is bit 1 in SREG

; =============================================================================================
; Facilitates delays from 10 ms to 2.55 sec in 10 ms increments using TIMER0 on a Atmega328P
; running @ 16 mhz.

;   ENTER:  Iterations = Number of 10 ms intervals

;   LEAVE:  Iterations = 0 and TIMER0 is stopped.

;    NOTE: Actual delay/interation = 9.984 ms. (1 / (16e6 / 1024)) * 156
;                                                   F_CPU / Prescaler
; ---------------------------------------------------------------------------------------------

Delay:  push    TEMP

    ; Start TIMER0 on Arduino (Atmega328P)

        ; Table 14-9, page 110 of Datasheet 8161D-AVR-10/09

        ldi TEMP, (1 << CS02) | (1 << CS00)
        out TCCR0B, TEMP        ; Write to CS02:0

    ; Clear TCNT0 at beginning of each iteration. This also guarantees and residual value at
    ; first iteration will be overwitten.

        clr TEMP
        out TCNT0, TEMP     ; Reset counter

    ; Loop 156 times. 64 µs * 156 = 9.984 ms

Dly:    in  TEMP, TCNT0
        cpi TEMP, 156
        brlo    Dly         ; This works because TCNT0 is zero indexed.

        dec Iterations
        brne    Dly - 2         ; Back to top of outer loop if CY = 0

    ; Stop TIMER0

        out TCCR0B, Iterations
        pop TEMP
        ret

Code was tested with this short snippet on a Makerduino to strobe an LED for 20 ms every second for one minute. While code is running, onboard LED is on continuously and upon completion, both LEDs are off.

RESET:  ldi TEMP, high (RAMEND)
        out SPH, TEMP
        ldi TEMP, low (RAMEND)
        OUT SPL, TEMP

; ATmega328P pin 19 > Arduino pin 13 > PORTB pin 5 > ONBOARD LED.
;                18               12             4 > Red indicator LED

        ldi TEMP, (1 << PINB5) | (1 << PINB4)
        out DDRB, TEMP      ; Set pins as output
        sbi PINB, PINB5     ; Turn on onboard LED
        ldi TEMP, 60        ; Only for 60 times

Strobe: sbi PINB, PINB4
        ldi Iterations, 2
        rcall   Delay
        sbi PINB, PINB4
        ldi Iterations, 98
        rcall   Delay
        dec TEMP
        brne    Strobe

        sbi PINB, PINB5     ; Turn off onboard LED


texts:
Is it fine to retry a web service call like this in Python? Would there be any scenarios the exception might not work the way we expect it? I expect it to continue only if it's fine, otherwise just retry, and if that doesn't work, just throw the error out.

def getblob(html):
    num_of_retries = 2
    time_interval = 2
    while num_of_retries:
        try:
            request_id = str(uuid.uuid4())
            params = [('requestId', request_id)]
            url = 'service.com?'
            url = url + urllib.urlencode(params)
            request = urllib2.Request(url, data=html)
            request.add_header('Accept', 'application/pdf')
            request.add_header('Content-Type', 'text/html')
            handle = urllib2.urlopen(request)
            pdf_blob = handle.read()
            break
        except Exception:
            typ, val, tb = sys.exc_info()
            logger.error(traceback.format_exception(typ, val, tb))
            time.sleep(time_interval)
            num_of_retries -= 1
            # If there aren't any retries - propogate
            if not num_of_retries:
                    raise
    return pdb_blob

I wnated to review this because recently I bumped into an issue where I hit an Internal Server Exception on the server side and the retry gave me an HTTP 200 response with incomplete file (it was a PDF without EOF Marker).

Would this snippet cause anything like this? It does work and I get PDF back as well. However from time to time I hit an HTTP 500 (some unknown server issue) and then HTTP 200 with incomplete blob. The incomplete blob is what makes me investigate if this loop can cause that problem. This HTTP 500 HTTP 200 (Incomplete Blob) is very much a pattern. Before I added this retry, I used to get HTTP 500 but never an incomplete blob.