texts: Setup ordering in priority blocking queue

texts:
I have a PriorityBlockingQueue in which I am adding SocketHolder. And let's say current datacenter where this code is running is abc, we have three datacenters abc, def and pqr.

private static final PriorityBlockingQueue<SocketHolder> liveQueue = new PriorityBlockingQueue<>();

for (SocketHolder socket : listOfSockets) {
    liveQueue.add(socket);
}

Now what I need to do is:

I have to setup some sort of ordering on liveQueue so that all SocketHolder whose datacenter is abc (which is current datacenter where the code is running), they should be at the top so which means when we retrieve it from this liveQueue all SocketHolder with abc datacenter should come out first and then later on all other SocketHolder with other dc's. Similarly for cases where current datacenter is either pqr or def.

This is my SocketHolder class that implements Comparable interface and what I need to do in my compareTo method so that at the top of liveQueue (after adding elements to it) we have all SocketHolder whose datacenter are current datacenter and then followed by other datacenter in which I have no priority.

Here is how I did: The live datacenter will be assigned priority 1, and the other datacenters priority 2. The queue will process the lowest priority first.

public final class SocketHolder implements Comparable<SocketHolder> {
   private final Socket socket;
   private final Datacenter dc;
   private final Context context;
   private final int priority;

   public SocketHolder(Socket socket, Datacenter dc, Context context) {
      super();
      this.socket = socket;
      this.dc = dc;
      this.context = context;
      this.priority = this.dc.name().equalsIgnoreCase(Utils.CURRENT_DATACENTER.get().name()) ? 1 : 2;
   }

   @Override
   public int compareTo(SocketHolder o) {
       return Integer.compare(this.priority, o.priority);
   }
}

I wanted to see if this is the right way to do it or is there is any better way.


texts: Efficient numpy cosine distance calculation

texts:
I want to calculate the nearest cosine neighbors of a vector using the rows of a matrix, and have been testing the performance of a few Python functions for doing this.

def cos_loop_spatial(matrix, vector):
    """
    Calculating pairwise cosine distance using a common for loop with the numpy cosine function.
    """
    neighbors = []
    for row in range(matrix.shape[0]):
        neighbors.append(scipy.spatial.distance.cosine(vector, matrix[row,:]))
    return neighbors

def cos_loop(matrix, vector):
    """
    Calculating pairwise cosine distance using a common for loop with manually calculated cosine value.
    """
    neighbors = []
    for row in range(matrix.shape[0]):
        vector_norm = np.linalg.norm(vector)
        row_norm = np.linalg.norm(matrix[row,:])
        cos_val = vector.dot(matrix[row,:]) / (vector_norm * row_norm)
        neighbors.append(cos_val)
    return neighbors

def cos_matrix_multiplication(matrix, vector):
    """
    Calculating pairwise cosine distance using matrix vector multiplication.
    """
    dotted = matrix.dot(vector)
    matrix_norms = np.linalg.norm(matrix, axis=1)
    vector_norm = np.linalg.norm(vector)
    matrix_vector_norms = np.multiply(matrix_norms, vector_norm)
    neighbors = np.divide(dotted, matrix_vector_norms)
    return neighbors

cos_functions = [cos_loop_spatial, cos_loop, cos_matrix_multiplication]

# Test performance and plot the best results of each function
mat = np.random.randn(1000,1000)
vec = np.random.randn(1000)
cos_performance = {}
for func in cos_functions:
    func_performance = %timeit -o func(mat, vec)
    cos_performance[func.__name__] = func_performance.best

pd.Series(cos_performance).plot(kind='bar')

The cos_matrix_multiplication function is clearly the fastest of these, but I'm wondering if you have suggestions of further efficiency improvements for matrix vector cosine distance calculations.


texts:
A follow-on, rags-to-riches implementation of The most efficient way to merge two lists in Java

The original requirements are to:

  Identify the distinct values from two input files, and output the
  distinct values to an output file. There is no specification for the
  order of the output, only that each line should be unique in the
  results.

  Special consideration should be made for efficiency.

I have implemented a more general specification:

merge multiple input files (at least one) to an output file
each line is treated as a line, not necessarily a "word". If the input files have just one word per line, then the output would be the same as the original specification.
take the input files from the commandline (the first file is the output file).

In my answer to the linked post I suggested that a Java 8 Streams implementation would be "nice". I have implemented that solution here. I am looking for suggestions on how to better utilize the new Java functionality, and any other suggestions you may have.

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

@SuppressWarnings("javadoc")
public class Linemerge {

    /* Wrap the IOException in order to make convenient Stream usage. */
    private static final void writeWord(BufferedWriter writer, String word) {
        try {
            writer.write(word);
            writer.newLine();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    private static void merge(Path source, Set<String> seen, BufferedWriter writer) throws IOException {
        try (Stream<String> words = Files.lines(source)) {
            words.filter(seen::add).forEach(word -> writeWord(writer, word));
        }
    }

    public static void main(String[] args) {
        if (args.length < 2) {
            throw new IllegalArgumentException("Need at least two file arguments: Destination Source {Source {Source {...}}}");
        }
        Path dest = Paths.get(args[0]);
        try (BufferedWriter writer  = Files.newBufferedWriter(dest)) {
            Set<String> seen = new HashSet<>();
            for (int i = 1; i < args.length; i++) {
               Path source = Paths.get(args[i]);
               if (Files.isRegularFile(source) && Files.isReadable(source)) {
                   System.out.println("Merging " + source);
                   merge(source, seen, writer);
               } else {
                   System.out.println("Unable to read (and ignoring) " + source);
               }
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

}


texts:
I am an amateur programmer and have just written a simple Capacitor Identifier mostly to practice. I have not had many people review my coding and thought this would be a good time to start. It is completed and works. Is there a better way I should have went about it? What should I look at in what and how I am writing that might improve my approach?

#!/usr/bin/env python3

import sys

# Get the arguments list
cmdargs = sys.argv

cmdargs = cmdargs[1:]

tolerances = {'B':'.10%','C':'.25%','D':'.5%','E':'.5%','F':'1%','G':'2%','H':'3%','J':'5%','K':'10%','M':'20%','N':'.05%','P':'+100% to 0%','Z':'+80% to -20%'}

# Get the total number of args passed
total = len(cmdargs)

# Templete String for output
capTemp = '{} = {}'

# output container
result = []

# cycle through passed arguments and decode each capacitor
for arg in cmdargs:
    case = len(arg)

    if(case == 2):
        temp = arg + ' pf, ' + str(float(arg) / 1000000) + 'uf'
        result.append(capTemp.format(arg, temp))
    elif(case == 3):
        cap = int(arg[:2]) * 10**int(arg[2:])
        temp = str(cap) + ' pf, ' + str(float(cap) / 1000000) + 'uf'
        result.append(capTemp.format(arg, temp))

    elif(case == 4):
        cap = int(arg[:2]) * 10**int(arg[2:3])
        temp = str(cap) + ' pf, ' + str(float(cap) / 1000000) + 'uf, ' + tolerances[arg[3:].upper()] + ' tolerance'
        result.append(capTemp.format(arg, temp))

# print the decoded capacitor values
for i in result:
    print(i)


texts: StringBuilder in C v2: Not a charged dance

texts:
A follow-up on this. To quote by copy/paste:

  Lots of languages have something that lets you build up a dynamically-sized string with minimal overhead. C doesn't, and I found myself using code that did that manually in a couple of places, so I packaged it into a class. Note that I've only implemented functionality I'm using.

stringbuilder.h

It's the same as last time.

#ifndef CONCATEN_STRINGBUILDER_H
#define CONCATEN_STRINGBUILDER_H
#include <stddef.h>
#include <stdbool.h>

struct StringBuilder;
typedef struct StringBuilder *StringBuilder;
StringBuilder sb_new(size_t);
bool sb_append(StringBuilder, char);
char *sb_as_string(StringBuilder);
char *sb_free_copy(StringBuilder);
size_t sb_size(StringBuilder);
void sb_free(StringBuilder);

#endif //CONCATEN_STRINGBUILDER_H

stringbuilder.c

#include <stdlib.h>
#include <string.h>
#include "stringbuilder.h"

struct StringBuilder {
    char *mem;
    size_t count;
    size_t cap;
};
StringBuilder sb_new(size_t init_cap) {
    if (init_cap == 0) {
        return NULL;
    }
    StringBuilder ret = malloc(sizeof(struct StringBuilder));
    if (!ret) return NULL;
    ret->mem = calloc(init_cap, sizeof(char));
    if (!ret->mem) {
        free(ret);
        return NULL;
    }
    ret->cap = init_cap;
    ret->count = 0;
    return ret;
}
#define LOAD_FACTOR 2
bool sb_append(StringBuilder to, char c) {
    if (to->count + 1 == to->cap) {
        char *new_mem = realloc(to->mem, to->cap * LOAD_FACTOR);
        if (!new_mem) {
            return false;
        }
        memset(new_mem + to->cap, 0, to->cap);
        to->mem = new_mem;
        to->cap *= LOAD_FACTOR;
    }
    ++to->count;
    to->mem[to->count - 1] = c;
    return true;
}
char *sb_as_string(StringBuilder sb) {
    return sb->mem;
}
char *sb_free_copy(StringBuilder sb) {
    char *ret = malloc((sb->count + 1) * sizeof(char));
    if (!ret) {
        return NULL;
    }
    strcpy(ret, sb->mem);
    ret[sb->count] = 0;
    sb_free(sb);
    return ret;
}
size_t sb_size(StringBuilder sb) {
    return sb->count;
}
void sb_free(StringBuilder sb) {
    if (sb) {
        if (sb->mem) free(sb->mem);
        free(sb);
    }
}

This time around, I'm concerned most about... exactly the same things:

  Performance. This code gets called a lot. I want it to be as fast as possible.
  Memory safety. While I'm fairly sure that this doesn't leak memory (assuming it's used properly), I'm not confident, and I'm not
  sure how to check.
  Edge cases. It works, as far as I can tell, but that doesn't mean that it's bug-free.

Also,

Portability. Will this code be compileable on any (C11-conforming and/or major) compiler? I want the code that uses this to be as simple to deploy on as many platforms as possible.

Note that I specifically am not asking for advice on functions that would be nice to have. I'm only spending the time to implementing things I'm going to use; this isn't part of a general-purpose utility library, so I really don't care about supporting all the features when I use a tiny fraction of them.


texts: What standard should I use for the naming of my MVC controller services?

texts:
I have the following:

   public class ContentsController : BaseController {
        private IContentService _content;
        private IPageService pageService;
        private IReferenceService _reference;
        private ISequenceService seqService;

        public ContentsController(
            IContentService contentService,
            IPageService pageService,
            IReferenceService referenceService,
            ISequenceService sequenceService) {
            this._content = contentService;
            this.pageService = pageService;
            this._reference = referenceService;
            this.seqService = sequenceService;
        }

Note that it's completely mixed up with different naming standards.

Is there anything recommended for the naming of services that are used by my controller?


texts:
I've knocked together a few lines of python to read in stats for a service (haproxy), and store them in an array (to do some analysis on later). It basically takes a multiline output, and splits it into multiple subarrays. This is how I've done it - can anyone offer improvements for me?

def build_array():
        services=[]
        for line in data.split('\n'):           # split out each line of raw input
                holding=[]                      # start a temp array
                for var in line.split(','):     # for each value append it to the temp array
                        holding.append(var)
                services.append(holding)        # append the temp array to the services array
        return services

The raw data is in the format:

data="""web,FRONTEND,,,0,0,4096,0,0,0,0,0,0,,,,,OPEN,,,,,,,,,1,2,0,,,,0,0,0,0,,,,0,0,0,0,0,0,,0,0,0,,,
    mysql,FRONTEND,,,0,0,4096,0,0,0,0,0,0,,,,,OPEN,,,,,,,,,1,3,0,,,,0,0,0,0,,,,,,,,,,,0,0,0,,,
    web-https,FRONTEND,,,0,0,4096,0,0,0,0,0,0,,,,,OPEN,,,,,,,,,1,4,0,,,,0,0,0,0,,,,,,,,,,,0,0,0,,,
    web,web2-NEW,0,0,0,0,,0,0,0,,0,,0,0,0,0,UP,1,0,1,0,0,19,0,,1,5,1,,0,,2,0,,0,L4OK,,0,0,0,0,0,0,0,0,,,,0,0,
    web,web1-OLD,0,0,0,0,,0,0,0,,0,,0,0,0,0,UP,1,1,0,0,0,19,0,,1,5,2,,0,,2,0,,0,L4OK,,0,0,0,0,0,0,0,0,,,,0,0,
    web,BACKEND,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,UP,1,1,1,,0,19,0,,1,5,0,,0,,1,0,,0,,,,0,0,0,0,0,0,,,,,0,0,"""


texts:
The challenge:

From: URI Online Judge

  Implement a program Combiner that takes two Strings as parameters and combines them, alternating letters, starting with the first letter of the first String, followed by the first letter of the second String, then second letter of first String, etc. The remaining letters of the longer String are then appended to the end of the combination String and this combination String is returned.

  Input

  The input contains several test cases. The first line of input contain an integer N that indicates the number of test cases. Each test case is composed of a line containing two strings and each string contains between 1 and 50 characters, inclusive.

  Output

  Combine the two input strings, as shown in the example below and print the resulting string.

Code

static void Main(string[] args)
{
    int n = int.Parse(Console.ReadLine());

    while(n-- > 0)
    {
        args = Console.ReadLine().Split(' ');

        for (int j = 0; j < args[0].Length || j < args[1].Length; j++)
        {
            if (j < args[0].Length)
            {
                Console.Write(args[0][j]);
            }
            if (j < args[1].Length)
            {
                Console.Write(args[1][j]);
            }
        }

        Console.WriteLine();
    }
}

Question is

How can this code be faster?

Apparently, my code took 0.384 second to run with URI's test cases.

The faster sample that solved the problem using C# took 0.072 second, which is a significant difference.

An image with all the execution times from this problem in C# can be found here.


texts: First Unique Character in Java by frequency

texts:
I have completed an implementation for finding the first unique character.
Is there any way I can improve it?
package com.datastructures;

public class FirstUniqueCharacter {

    public static void main(String args[]){

        String string = "neslihan";
        System.out.println(firstUniqueChar(string));
    }

    private static int firstUniqueChar(String string) {

        int freq[] = new int[26];

        for(int i=0; i< string.length() ;i++){
            freq[string.charAt(i) - 'a'] ++;
        }
        for (int j =0;j<string.length();j++){
            ;
            System.out.println(string.charAt(j)-0);
            System.out.println('a'-0);
            if(freq[string.charAt(j) - 'a'] == 1) {
                return j;
            }
        }
        return -1;
    }
}


texts: Form validation for user account creation

texts:
What would be a cleaner way to write the following?

// Check if add user was attempted
if ($params['submit']) {
    // Verify all fields are filled in
    if (Utilities::checkAllFieldsNotEmpty($params)) {
        // Make sure username is not taken
        if (Admin::checkUsername($params['username'])) {
            // Check if directory is created
            if (Admin::createUsersDirectory($params['username'])) {
                if (Admin::createNewUser($params['name'], $params['username'], $params['password'], $params['admin'])) {
                    Utilities::setMessage("Excellent!", "User was created successfully.", "admin_modal");
                }
            } else {
                Utilities::setMessage("Whoa!", "Directory creation failed.", "admin_modal");
            }
        } else {
            Utilities::setMessage("Whoa!", "Username in use.", "admin_modal");
        }
    } else {
        Utilities::setMessage("Whoa!", "Please fill in all fields", "admin_modal");
    }

    // Return admin view with message
    return $this->view->render($response, 'admin/admin.twig', [
        'name' => $user['name'],
        'message' => $_SESSION['message'],
        'form' => [
            'name' => $params['name'],
            'username' => $params['username'],
            'admin' => $params['admin']
        ]
    ]);
}


texts:
I am looking for a more efficient or shorter way to achieve the following output using the following code:

timeval curTime;
gettimeofday(&curTime, NULL);
int milli = curTime.tv_usec / 1000;

time_t rawtime;
struct tm * timeinfo;
char buffer [80];

time(&rawtime);
timeinfo = localtime(&rawtime);

strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", timeinfo);

char currentTime[84] = "";
sprintf(currentTime, "%s:%d", buffer, milli);
printf("current time: %s \n", currentTime);

Sample output:

  current time: 2012-05-16 13:36:56:396

I would prefer not to use any third-party library like Boost.


texts:
I've been working on learning Java and this was a challenge as part of a chapter on data types:

  Create a program that computes how far away, in feet, a listener is
  from a sound. Sound travels approximately 1,100 feet per second
  through air.

  You can also compute the distance to a large object, such as a rock
  wall, by timing the echo. For example, if you clap your hands and time
  how long it takes for you to hear the echo, then you know the total
  round-trip time.

  Java: A Beginner's Guide, Sixth Edition - by Herbert Schildt · McGraw-Hill Education (Chapter 2)

I'm super-new to OOP and am trying to nip bad habits in the bud, so-to-speak. I've divided the logic in one class as much as I think is possible (at my current skill level).

SoundSpeed.java

public class SoundSpeed {
    private final double SOUND_TRAVEL_SPEED = 1100;
    public double timeInSeconds;
    public boolean isEcho;
    private int distanceDivisor;

    double getResult() {
        if(isEcho) {
            distanceDivisor = 2;
        } else {
            distanceDivisor = 1;
        }
        return (timeInSeconds * SOUND_TRAVEL_SPEED) / distanceDivisor;
    }
}

SoundSpeedCalc.java

class SoundSpeedCalc {
    public static void main(String args[]) {
        SoundSpeed soundSpeed = new SoundSpeed();
        soundSpeed.timeInSeconds = 35.079;
        soundSpeed.isEcho = true;
        String verb;

        if(soundSpeed.isEcho) {
            verb = "echo";
        } else {
            verb = "travel";
        }
        System.out.println("The sound took " + soundSpeed.timeInSeconds + " seconds to "
        + verb + " and thus the distance is " + soundSpeed.getResult() + " feet away.");
    }
}

Example output (using above parameters in main):

  The sound took 35.079 seconds to echo and thus the distance is 19293.45 feet away.

Example output if I set soundSpeed.isEcho to false:

  The sound took 35.079 seconds to travel and thus the distance is 38586.9 feet away.


texts:
I'm learning React and I made this country guessing game. What improvements/suggestions do you have for it?
Preview it here
import React, {Component} from 'react';
import './Country.css';

class Country extends Component {
    constructor(props) {
        super(props);
        this.state = {
            countries: [],
            randomCountry: {},
            randomOptions: [],
            userIsWin: '',
            disableFieldset: false,
            goodGuess: 0,
            bgColor: {backgroundColor: 'white'}
        }
        this.getRandomCountry = this.getRandomCountry.bind(this);
        this.checkWin = this.checkWin.bind(this);
    }

componentDidMount() {
    const apiUrl = "https://restcountries.eu/rest/v2/all";
    fetch(apiUrl)
    .then(data => data.json())
    .then(countries => this.setState({countries}))
    .then(this.getRandomCountry)
}

getRandomCountry() {
    const random = this.state.countries[Math.floor(Math.random()*this.state.countries.length)];
    const randomOpt1 = this.state.countries[Math.floor(Math.random()*this.state.countries.length)];
    const randomOpt2 = this.state.countries[Math.floor(Math.random()*this.state.countries.length)];
    const randomOpt3 = this.state.countries[Math.floor(Math.random()*this.state.countries.length)];
    const randomOptions = [random.name, randomOpt1.name, randomOpt2.name, randomOpt3.name];
    randomOptions.sort(() => { return 0.5 - Math.random() });
    this.setState({
        randomCountry: random,
        randomOptions: randomOptions,
        userIsWin: '',
        disableFieldset: false
    })
}

checkWin(e) {
    this.setState({
        disableFieldset: true
    })
    const winCountry = this.state.randomCountry.name;
    const userGuess = e.target.value;
    if (winCountry === userGuess) {
        this.setState({
            userIsWin: 'Win',
            goodGuess: this.state.goodGuess + 1,
            bgColor: {backgroundColor: '#81C784'}
        })
    } else {
        this.setState({
            userIsWin: 'Lose',
            bgColor: {backgroundColor: '#FF8A65'}
        })
    }
    setTimeout(()=>{
        this.getRandomCountry();
        this.setState({
            userIsWin: '',
            disableFieldset: false,
            bgColor: {backgroundColor: 'white'}
        })
        console.log(e.target)
    }, 2000)

}

render() {
    return (

Country Guessing Game

                <button className="rnd mui-btn mui-btn--raised" onClick={this.getRandomCountry}>Random</button>

{this.state.userIsWin === 'Win' ? 'You guess right! ' : ''}
                    {this.state.userIsWin === 'Lose' ? 'You guess wrong. ' : ''}
                    Score:{this.state.goodGuess}

            <fieldset disabled={this.state.disableFieldset}>
                <form onClick={e => this.checkWin(e)}>
                    <button className="mui-btn mui-btn--raised" value={this.state.randomOptions[0]}>{this.state.randomOptions[0]}</button>
                    <button className="mui-btn mui-btn--raised" value={this.state.randomOptions[1]}>{this.state.randomOptions[1]}</button>
                    <button className="mui-btn mui-btn--raised" value={this.state.randomOptions[2]}>{this.state.randomOptions[2]}</button>
                    <button className="mui-btn mui-btn--raised" value={this.state.randomOptions[3]}>{this.state.randomOptions[3]}</button>
                </form>
            </fieldset>

    )
}
}

export default Country;


texts: Could this depth system for a game be improved?

texts:
I am still new to C++ and don't have a great insight on my coding yet, so I would be very grateful to anyone and everyone that gives advice.

Also, this code is meant to: keep all of my objects in an ordered fashion based on depth. I have a couple functions that allow for easy management and I made object friends with the depth manager because I only want the depthManager to have control over each objects idDepth and depth which are two different things.

The reason I need a depth system is because I need to have objects execute there code in an orderly fashion, and I also need to have control of what objects are drawn first to last.

This class has been tested and works as expected.

Stay up to date with this project on GitHub.com

Abstract object class:

class object{
    // Placement Data
        unsigned int depth, idDepth, idObject, idMain;

    // Friends
        friend class depthManager;
        friend class objectManager;
protected:
    unsigned int getDepthId(){ return idDepth; }
public:
    virtual void update() = 0;
    virtual void draw() = 0;
    unsigned int getDepth() { return this->depth; }
};

note: I have each object handle its own update and draw events for easier design. I'm mainly asking on approval for the depth system.

depthManager class

class depthManager{
private:
    std::map<unsigned, std::vector<object*>* > objectMap;

    void changeListPlacement( unsigned int depth, unsigned int position, int change);
public:
    void objectAdd( unsigned int depth, object* obj);
    void objectRemove( object* obj );
    void objectMove( unsigned int depth, object* obj );
};

depthManagers Functions:

void depthManager::objectAdd(unsigned int depth, object *obj)
{
    obj->depth = depth;
        // Check if depth key existant
        if ( objectMap.find( depth ) != objectMap.end() )
        {
            std::vector< object* >* &refVec = objectMap[ depth ];
            refVec->push_back( obj );
            obj->idDepth = (unsigned)(int)refVec->size() - 1;
        }
        else // Add new Key
        {
            objectMap[ depth ] = new std::vector< object* >;
            std::vector< object* >* &refVec = objectMap[ depth ];
            refVec->push_back( obj );
            obj->idDepth = (unsigned)(int)refVec->size() - 1;
        }
}

void depthManager::changeListPlacement(unsigned int depth, unsigned int position, int change = -1)
{
    if ( objectMap.find( depth ) == objectMap.end() )
    {
        return;
    }

    std::vector<object*>* &refVec =  objectMap[ depth ];
    for( unsigned int i = refVec->size() - 1; i > position; i -- )
    {
        object* &pObj =  refVec->at( i );
        pObj->idDepth += change;
    }
}

void depthManager::objectRemove(object *obj)
{
    if (  objectMap.find( obj->depth ) == objectMap.end() )
    {
        std::cout << "ERROR DEPTH NOT FOUND \n" << std::flush ;
        return;
    }
    std::vector<object*>* &refVec =  objectMap[ obj->depth ];
    changeListPlacement( obj->depth, obj->idDepth);
    refVec->erase( refVec->begin() + obj->idDepth );
}

void depthManager::objectMove(unsigned int depth, object *obj)
{
    this->objectRemove( obj );
    this->objectAdd( depth, obj );
}

previous verions

Depth Manager Source Code 1


texts: Using a DependencyProperty ChangedCallback as a validation measure

texts:
I have an UserControl where I declare two DependencyProperties

int? Level;
CardBase SelectedCardBase;

A non-null CardBase object will always have 2 read-only integers: MinLevel and MaxLevel.

The UserControl allows the user to change as he desires both DPs, and the internal logic must ensure that both DPs have valid values following these rules:

If SelectedCardBase is null, then Level must be null.
If SelectedCardBase is not null, then Level must be between MinLevel and MaxLevel.
When SelectedCardBase changes to anything but null, Level is set to MaxLevel.

This is the code:

    public CardBase SelectedCardBase
    {
        get { return (CardBase)GetValue(SelectedCardBaseProperty); }
        set { SetValue(SelectedCardBaseProperty, value); }
    }

    public static readonly DependencyProperty SelectedCardBaseProperty =
         DependencyProperty.Register("SelectedCardBase", typeof(CardBase), typeof(MyUserControl),
         new PropertyMetadata(null, new PropertyChangedCallback(SelectedCardBaseChanged)));

    private static void SelectedCardBaseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue == null)
            d.SetValue(LevelProperty, null);
        else
            d.SetValue(LevelProperty, ((CardBase)e.NewValue).MaxLevel);
    }

    public int? Level
    {
        get { return (int?)GetValue(LevelProperty); }
        set { SetValue(LevelProperty, value); }
    }
    public static readonly DependencyProperty LevelProperty =
        DependencyProperty.Register("Level", typeof(int?), typeof(MyUserControl),
        new PropertyMetadata(null, new PropertyChangedCallback(LevelChanged)));
    private static void LevelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        CardBase card = (CardBase)d.GetValue(SelectedCardBaseProperty);

        if (card == null)
        {
            if (e.NewValue != null)
                d.SetValue(LevelProperty, null);
        }
        else if (e.NewValue == null)
        {
            d.SetValue(LevelProperty, e.OldValue);
        }
        else
        {
            int level = (int)e.NewValue;

            if (level < card.MinLevel)
                d.SetValue(LevelProperty, card.MinLevel);
            else if (level > card.MaxLevel)
                d.SetValue(LevelProperty, card.MaxLevel);
        }
    }

I don't fully get how CoerceValueCallbacks work, and I've read (or misunderstood) that both Coerce and Changed callbacks can be overwritten because they are metadata, so my questions are:

What problems (if they exist) will I have when something subscribes to the OnValueChanged of one of those DP?
Should I use CoerceValueCallbacks instead of this?
If so, what is the correct way of doing this using CoerceValueCallbacks?
And of course, with the given rules at the beginning, there is any mistake on the  code that would cause strange behaviour, or anything that could be improved?


texts:
Please consider the following three tables that I am playing with in the image below:

I want to come up with a SQL Query which shows first_name, last_name, and the total amount of all orders for customer_id 12345

Here is what I have come up with:

SELECT C.first_name , C.last_name,
       SUM(O.total_price) AS TotalAmount
FROM   Customer C , Order O
WHERE  C.customer_id = O.customer_id
AND    C.customer_id = 12345;

Does my query look good? If yes, then should I be using Inner Join instead of Cross Join from an efficiency point of view?


texts:
I just started to learn C, and for practicing, I tried to implement a simple stack data structure, and I want to know if I'm making any mistakes, or how could I improve it:

My stack.h:

#ifndef STACK_H
#define STACK_H
#define STACK_MAX_SIZE 125

typedef struct
{
    int max_size;
    int items[STACK_MAX_SIZE];
    int position;
} Stack;

void stack_push(Stack *stack, int number);
int stack_pop(Stack *stack);
bool stack_is_empty(Stack *stack);

#endif

Stack.c:

#include <stdio.h>
#include <stdbool.h>
#include "stack.h"

void stack_push(Stack *stack, int number)
{
    if(stack->position < 0){
        stack->position = 1;
    }
    stack->position++;

    stack->items[stack->position] = number;
}

int stack_pop(Stack *stack)
{
    if(stack->position == 0){
        fprintf(stderr, "Stack is empty");
        return -1;
    }
    int number = stack->items[stack->position];

    stack->position--;

    return number;
}

bool stack_is_empty(Stack *stack)
{
    return stack->position < 1;
}

And the test:

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdio.h>
#include <stdbool.h>
#include "../src/stack.h"

static void stack_test() {
    Stack s, *stack = &s;

    for(int i = 0; i <= 5; i++){
        stack_push(stack, i);
    }

    assert_int_equal(stack_pop(stack), 5);

    while(false == stack_is_empty(stack)){
        stack_pop(stack);
    }

    assert_true(stack_is_empty(stack));
}

int main(void) {
    const struct CMUnitTest tests[] = {
        cmocka_unit_test(stack_test),
    };
    return cmocka_run_group_tests(tests, NULL, NULL);
}


texts:
I have the following routine that I would like to simplify:

public void SetUniform(RenderContext ctx, string uName, object value)
{
    if (ctx == null)
        throw new ArgumentNullException("ctx");
    if (value == null)
        throw new ArgumentNullException("value");

    if (value is int)
        SetUniform(ctx, uName, (int)value);
    else if (value is Vertex2f)
        SetUniform(ctx, uName, (Vertex2f)value);
    else if (value is Vertex3f)
        SetUniform(ctx, uName, (Vertex3f)value);
    else if (value is Vertex4f)
        SetUniform(ctx, uName, (Vertex4f)value);
    else if (value is Matrix4x4)
        SetUniform(ctx, uName, (Matrix4x4)value);
    else
        throw new NotSupportedException(value.GetType() + " is not supporteds");
}

As you can read, the above routine is essentially the generic version of all SetUniform. I need this routine becuase I don't know the value type at compile time, but in this way I need to check its type at runtime each time I need it.

How can I reduce this routine is a more simple and performant-wise way?

Due the comments, I need to clarify some question that seems not enought clear.

The "Uniform" state belongs to a specific class ShaderProgram, not to a wide set of classes (the ones listed on the snippet above are only a subset). Each SetUniform implementation access to the internal state of ShaderProgram, which I would not like expose, even using an internal modifier.
The SetUniform overload with object parameter is required because I get the value  using reflection, indeed I have no idea at compile time what is the value type. Indeed I have defined an additional overload which recall specific implementation.
Each SetUniform overload have a different implementation. Specifically, they call the one of the routines specified here.


texts: Get time slots based on multiple workshops

texts:
I have a list of Workshops, each having open days (Monday, Tuesday etc.) and open time and close time (which will be same for each day). Now, Based on the current time, I need to find out next 7 days available slots. A slot is of 2 hour and is available if any of the workshop is open in that 2 hour time. The first slot of each day will start at the open_time (nearest to hour time for eg. if open_time is 09:23:54 then first slot start time will be 10:00:00) of workshop opens first on that day.

My solution so far is to have a Workshop table which has off_days, open_at, close_at, name columns. There is another table which is WorkshopHolidays which has holiday_date, workshop_id.

class Workshop(models.Model):
    DAY_CHOICES = (
        (0,'Monday'),
        (1,'Tuesday'),
        (2,'Wednesday'),
        (3,'Thursday'),
        (4,'Friday'),
        (5,'Saturday'),
        (6,'Sunday')
    )
    name = models.CharField(max_length=255,null=True)
    address = models.CharField(max_length=1024)
    open_at = models.TimeField(null=True, blank=True)
    close_at = models.TimeField(null=True, blank=True)
    off_days = MultiSelectField(choices=DAY_CHOICES,null=True,blank=True)
    active = models.BooleanField(default=True)

class WorkshopHoliday(CreatedAtAbstractBase):
    workshop = models.ForeignKey(Workshop)
    holiday_date = models.DateField()
    active = models.BooleanField(default=True)

Now given a datetime, I need to find what are the two hour slots available for next seven days. If any one store is open in a slot, that slot should be part of the output.

The way I am approaching the problem now is following:

def get_slots():
    current_time = timezone.now()
    workshops = Workshop.objects.all()
    slots = {}
    for i in range(1,7):
        curdate = current_time.date()
        day = current_time.weekday()
        curdate_dict = {'start_time':None, 'end_time':None}
        for workshop in workshops:
            off_days = workshop.off_days if workshop.off_days else ''
            if day in off_days:
                continue
            wh = WorkshopHoliday.objects.filter(holiday_date=curdate,active=True)
            if wh:
                continue
            start_time = curdate_dict.get('start_time')
            if start_time:
                if start_time > current_time.time() and start_time > workshop.open_at and workshop.open_at > current_time.time():
                    start_time = workshop.open_at
            elif workshop.open_at > current_time.time():
                    start_time = workshop.open_at
            curdate_dict['start_time'] = start_time
            end_time = curdate_dict.get('end_time')
            if end_time:
                if end_time > current_time.time() and end_time < workshop.close_at and workshop.close_at > current_time.time():
                    end_time = workshop.open_at
            elif workshop.open_at > current_time.time():
                    end_time = workshop.open_at
            curdate_dict['end_time'] = end_time
        slots[curdate] = curdate_dict
        current_time.replace(hour=0,minute=0,second=0)
        current_time = current_time + timezone.timedelta(days=1)

Please suggest if there could be a better way to approach this problem (may be a better SQL query or algorithm). Also, please point out if there is some scenario in which this will give incorrect result.


texts:
I have a CSV holding a "flat" table of a tree's edges (NOT binary, but a node cannot have two parents), ~1M edges:

node_id parent_id
1       0
2       1
3       1
4       2
...

The nodes are sorted in a way that a parent_id must always come before any of its children, so a parent_id will always be lower than node_id.

I wish, for each node_id, to get the set of all ancestor nodes (including itself, propagated until root which is node 0 here), and a set of all descendant nodes (including itself, propagated until leaves), and speed is crucial.

Currently what I do at high level:

Read the CSV in pandas, call it nodes_df
Iterate once through nodes_df to get node_ancestors, a {node_id: set(ancestors)} dict adding for each node's ancestors itself and its parent's ancestors (which I know I have seen all by then)
Iterate through nodes_df again in reverse order to get node_descendants, a {node_id: set(ancestors)} dict adding for each node's descendants itself and its child's descendants (which I know I have seen all by then)

import pandas as pd
from collections import defaultdict

# phase 1
nodes_df = pd.read_csv('input.csv')

# phase 2
node_ancestors = defaultdict(set)
node_ancestors[0] = set([0])
for id, ndata in nodes_df1.iterrows():
    node_ancestors[ndata['node_id']].add(ndata['node_id'])
    node_ancestors[ndata['node_id']].update(node_ancestors[ndata['parent_id']])

# phase 3
node_descendants = defaultdict(set)
node_descendants[0] = set([0])
for id, ndata in nodes_df1[::-1].iterrows():
    node_descendants[ndata['node_id']].add(ndata['node_id'])
    node_descendants[ndata['parent_id']].\
      update(node_descendants[ndata['node_id']])

So, this takes dozens of seconds on my laptop, which is ages for my application. How do I improve?

Plausible directions:

Can I use pandas better? Can I get node_ancestors and/or node_descendants by some clever join which is out of my league?
Can I use a python graph library like Networkx or igraph (which in my experience is faster on large graphs)? E.g. in both libraries I have a get_all_shortest_paths methods, which returns something like a {node_id: dist} dictionary, from which I could select the keys, but... I need this for every node, so again a long long loop
Parallelizing - no idea how to do this


texts: Conditionally perform asynchronous actions with RxJava

texts:
This seems to work fine however I'm sure there must be a neater way. The outcome should be:
If isHidden == true then call canHideAccount. canHideAccount returns a Single.
If isHidden != true then there is no need to call canHideAccount as it is a long asynchronous operation.
If canHideAccount returns false then the user should be shown an error dialog via getView().render().
If canHideAccount is true or isHidden was false then updateExternalAccount should be called. This is a network request that returns a Single.
If the request completes successfully then update the UI, if there is an error then notify the user.

Kicking off the chain with the Single.just(isHidden) seems a bit odd, but I cant see how to kick it off with the condition that it should only call canHideAccount if isHidden == true.

All comments and criticism are welcome.

public void updateAccount(String accountId, boolean isHidden, String type) {

    UpdatePostData accountUpdatePost = new UpdatePostData();
    accountUpdatePost.accountId = accountId;
    accountUpdatePost.isHidden = isHidden;
    accountUpdatePost.type = type;

    postAccountUpdate = Single.just(isHidden)
            .subscribeOn(Schedulers.io())
            .flatMap(hideAccount -> {
                if (hideAccount) {
                    return canHideAccount(accountId);
                } else {
                    return Single.just(true);
                }
            }).flatMap(canHideAccount -> {
                if (canHideAccount) {
                    return getApi().updateAccount(accountUpdatePost);
                } else {
                    return Single.error(new IllegalStateException());
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(response -> {
                if (isViewAttached()) {
                    getView().render(State.accountUpdated);
                }
            }, error -> {
                if (isViewAttached()) {
                    if (error instanceof IllegalStateException) {
                        getView().render(State.cannotHideAccount);
                    } else if (isAuthenticationError(error)) {
                        getView().render(State.reauthenticate);
                    } else {
                        getView().render(error);
                    }
                }
                GA.instance.error(error);
            });
}


texts: Inserting 50k rows with Spring Data and JPA

texts:
I am using Spring Data JPA with Spring boot application. My requirement is to insert 50K rows in JPA entity table in one hour or less.

I have 3 entities A, B and C.  Entity A has a one to many association with entity B and a one to one association with entity C.

@Entity
public class A {
    @OneToMany(mappedBy = "chipJobItem", cascade = {
            CascadeType.MERGE,
            CascadeType.PERSIST,
            CascadeType.REFRESH,
            CascadeType.DETACH }, orphanRemoval = true, fetch = FetchType.LAZY)
    private List<B> values;
}

@Entity
public class B {
    @ManyToOne(fetch = FetchType.LAZY, cascade = {
        CascadeType.MERGE,
        CascadeType.PERSIST,
        CascadeType.REFRESH,
        CascadeType.DETACH })
    @JoinColumn(name = "fk_product_instance_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "DATAITEMS_FK"))
    private A job;
}

@Entity
public class C {
    @ManyToOne(fetch = FetchType.LAZY, cascade = {
        CascadeType.MERGE,
        CascadeType.PERSIST,
        CascadeType.REFRESH,
        CascadeType.DETACH })
    @JoinColumn(name = "fk_product_instance_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "DATAITEMS_FK"))
    private A job;
}

I have also set the JDBC batch size = 100, it improves the performance a little.

Need help improving the performance of the JPA insert.


texts:
I request data from a table in a database and each line comes as a dictionary like this one :
{
    "timestamp" : 1234657890,
    "prices" : {
                   "AAA" : 111,
                   "BBB" : 222,
                    ...
                   "ZZZ" : 999
               }
}

From all those lines i wanted to create a dataframe like this:
Timestamp    AAA    BBB    ...   ZZZ
1234657890   111    222    ...   999
1234567891   110    223    ...   997
   ...
1324657899   123    208    ...  1024

So i did :
rawData = database_request()
listPrices = []
for row in rawData
    tmp = {'timestamp': row['timestamp']}
    tmp.update({name : price for name,price in row['prices'].items()})
    listPrices.append(tmp)
df = pd.DataFrame(listePrices)

So i was wondering if there were a more pythonic way to do this ?


texts:
Is better to use $id_name ( Class A Method b() ) OR $id_name_2 ( Class A Method c())?

<?php

Class A {

    function a(){
        $params = array(
            'id'=> 1,
            'name' => 'b',
        );

        $id_name = $this->b($params);
        $id_name_2 = $this->c($params);
    }

    function b($params){
        $id = $params['id'];
        $name = $params['name'];
        /*******

            Some code performing operations with $name and $id

        ********/

        $str = $id . $name;
        return $str;
    }

    function c($params){

        /*******

            Some code performing operations with $params['name'] and $params['id']

        ********/

        $str = $params['name'] . $params['id'];

        return $str;
    }

}


texts:
I have written a function/script to simulate data for sample size estimation. The following code samples from a vector of generated values with varying numbers of sample sizes and then concatenates means and standard deviations for a number of simulations.

I am using nested loops but would like to vectorize the code for efficiency and am wondering if anyone could suggest some optimizations.

library(MCMCglmm)
library(tidyverse)

Est <- function(n, mean, sd, lower, upper, samp_min, samp_max, samp_int, nsim){

        Data <- round(rtnorm(n, mean, sd, lower, upper), digits = 0) # Create a vector to sample from

        Samp_size <- seq(samp_min, samp_max, samp_int) # Create vector of sample sizes

        # Set up enpty results data frames
        Results_samp <- data.frame()
        Results <- data.frame()

        for(i in 1:nsim){ ## Loop through number of simulations

                for (j in seq_along(Samp_size)) { # Loop through sample sizes
                     SUS_Score <- sample(Data, Samp_size[j], replace = TRUE)
                     Nsubj <- Samp_size[j]
                     Mean <- mean(SUS_Score, na.rm = TRUE)
                     SD <- sd(SUS_Score, na.rm = TRUE)
                     Results_samp <- rbind(Results_samp,
                                      data.frame(
                                              Nsubj,
                                              Mean,
                                              SD))
                }
             Results <- rbind(Results, Results_samp)
             Results_samp <- data.frame()
        }

       Results %>%
               arrange(Nsubj)
}

Sims <- Est(n = 1000, mean = 55, sd = 37, lower = 0, upper = 100, samp_min = 5, samp_max = 35, samp_int = 5, nsim = 1000)

Any suggestions would be greatly appreciated!


texts: Showing a message when the column is true

texts:
I'm making an app using Rails. I have 4 Boolean columns in a table like below, and I want to show a message if that column is true.

class CreatePosts < ActiveRecord::Migration[5.2]
  def change
    create_table :posts do |t|
      t.integer :user_id, null: false, default: 0
      t.string :title, null: false, default: ''
      t.text :description, null: true
      t.boolean :tag_1, null: false, default: false
      t.boolean :tag_2, null: false, default: false
      t.boolean :tag_3, null: false, default: false
      t.boolean :tag_4, null: false, default: false

      t.timestamps
    end
  end

posts_controller.rb

def index
  @posts = Post.all
end

index.html.erb

<% @posts.each do |post| %>

    <% if post.tag_1 %>

<%= 'tag_name_A' %>

    <% end %>
    <% if post.tag_2 %>

<%= 'tag_name_B' %>

    <% end %>
    <% if post.tag_3 %>

<%= 'tag_name_C' %>

    <% end %>
    <% if post.tag_4 %>

<%= 'tag_name_D' %>

    <% end %>
    <% if !post.tag_1 && !post.tag_2 && !post.tag_3 && !post.tag_4 %>

none

    <% end %>

<% end %>

tag_name_A will be shown if tag_1 is the only true column, and there'll be 'tag_name_B' & 'tag_name_D' shown if tag_2 and tag_4 are both true. (I hope I'm making myself clear.)

This code is working fine and I'm already getting what I want, but I just don't like how they are written. It looks messy and obviously not smart. How would you optimize it?


texts: Flask blueprints and context_processor - Best approch?

texts:
I am working on a Flask application (side-project) to send text-messages to customers using Twilio. There is two different list of customers, 1 for testing and  for production (stored in MongoDB). Current functionalities:

user login/ logout
Add, remove or search a user in both test and production collections
Send a text message to a given list (using Twilio API)
Get the current credit amount left on Twilio
(to-do): get an overview of the past messages sent / the cost of the campaign and the date

I used to have everything under main.py but I recently refactored most of my code to make it more readable. I am now using blueprints and initializing my app using the factory design pattern. So far, I only have a single blueprint (user) but I am trying to split logic between user blueprint (login/logout) and messaging (send messages).
On my navbar, I display the amount left of Twilio (81 euros in the image). I am using the Flask context_processor to inject the value on the navbar. However, the point of having a blueprint is that it encapsulates everything. And if I have multiple blueprints, I would need to duplicate that context_processor.
web_messaging/templates/layouts/base.html (where the credit amount is injected)
<!DOCTYPE html>

    ...

            {% if current_user.is_authenticated %}

              {{credit}} €

                Déconnection

            {% else %}

            Connection

            {% endif %}

  {% block body %}{% endblock %}

web_messaging/blueprints/user/routes.py (where the credit is generated)
user = Blueprint('user', __name__, template_folder='templates')

...

def get_current_credits():
    balance_data = client.api.v2010.balance.fetch()
    balance = float(balance_data.balance)
    currency = balance_data.currency
    return balance, currency

@user.context_processor
def inject_credit():
    credit, currency = get_current_credits()
    if currency != "EUR":
        credit = c.convert(credit, currency, 'EUR')
    return dict(credit=(round(credit,2)))

What I am thinking about is to create a web_messaging/context_manager.py file to manage all contexts. The issue I see here is I will have to import Twilio library on context.py
def get_current_credits():
    balance_data = client.api.v2010.balance.fetch()
    balance = float(balance_data.balance)
    currency = balance_data.currency
    return balance, currency

def inject_credit():
    credit, currency = get_current_credits()
    if currency != "EUR":
        credit = c.convert(credit, currency, 'EUR')
    return dict(credit=(round(credit,2)))

web_messaging/app.py
from context import inject_credit

def create_app(settings_override=None):
    app = Flask(__name__, instance_relative_config=True)

    app.config.from_object('config.settings')
    app.config.from_pyfile('settings.py', silent=True)
    app.register_blueprint(user)
    app.register_blueprint(texting)
    extensions(app)
    configure_context_processors(app) # here
    return app

def configure_context_processors(app):
    processors = [inject_credit]
    for processor in processors:
        app.context_processor(processor)


texts:
I need to generate semi-complex throw-able passwords (just one use) in my application. I want it to be:

at least 8 characters long;
contains at least 1 digit, 1 [a-z] char, 1 [A-Z] char;

I generate a few passwords a day, and I don't mind about speed. I'm more looking for suggestions about code being maintainable (or not) and possible huge security flaws. Here's the code:

public static string GenerateSimplePassword(int length = 8)
{
    length = Math.Max(length, 8);

    var allowedLower = new[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
    var allowedDigits = new[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    var allowedUpper = allowedLower.Select(x => char.ToUpper(x)).ToArray();

    var r = new Random();
    var generatedCode = "";
    generatedCode += allowedLower[r.Next(0, allowedLower.Length)];

    generatedCode += allowedDigits[r.Next(0, allowedDigits.Length)];

    generatedCode += allowedUpper[r.Next(0, allowedUpper.Length)];

    var arrays = new[] { allowedLower, allowedUpper, allowedDigits };
    for (var i = 0; i < length - 3; i++)
    {
        var array = arrays[r.Next(0, arrays.Length)];
        generatedCode += array[r.Next(0, array.Length)];
    }

    return String.Join("", generatedCode.OrderBy(x => r.Next()));
}


texts: Speeding up Python date conversion function currently using list comprehension and datetime

texts:
I am reading a large data file where the time is given in number of days since some epoch. I am currently converting this to Python's datetime format using this function:

import datetime as dt
def days2dt(days_since_epoch):
    epoch = dt.datetime(1980, 1, 6)
    datelist = [epoch + dt.timedelta(days=x) for x in days_since_epoch]
    return datelist

# run with sample data (might be larger in real life, in worst case multiply
# the list by 40 instead of 6)
import numpy as np
sample = list(np.arange(0, 3/24., 1/24./3600./50.))*6
dates = days2dt(sample)

Running this function takes 5x longer than reading the entire file using pandas.read_csv() (perhaps because the listcomp performs an addition for each element). The returned list is used immediately as the index of the pandas DataFrame, though interestingly, using a generator expression instead of a listcomp as above improves performance by ~35% (why?).

Aside from using a generator expression, can the performance of this function be improved in any way, e.g. by not performing this date conversion per-element or by using some NumPy feature I'm not aware of?


texts:
I am using Python threads to download images. I have a JSON file that contains URLs to the images in the following structure:

images = {'images: [{'url': 'https://contestimg.wish.com/api/webimage/5468f1c0d96b290ff8e5c805-large',
   'imageId': '1'},....]}

There are over 1,000,000 images.  After using 20 threads, I only collected ~80,000 of these images, along with ~1000 exceptions due to 500 status codes from the URL.  I have a feeling that my code is incorrect.  Can someone please check my code?

import requests
import threading
import json
import pickle

train_data = json.load(open("train.json", "rb"))

images = train_data["images"]

threads = []

def save_image(images_part):
    errors = []
    for i in images_part:
        Picture_request = requests.get(i['url'])
        if Picture_request.status_code == 200:
            with open(f"train/{i['imageId']}.jpeg", "wb") as f:
                f.write(Picture_request.content)
        else:
            errors.append((i, Picture_request.status_code))
            print(f"error in {i['imageId']} with {Picture_request.status_code}")
    return errors

for i in range(0, 20):
    start = i*50727
    end = (i+1)*50727
    if i == 19:
        end = None
    t = threading.Thread(target=save_image, args=(images[start:end],))
    threads.append(t)
    t.start()
    print(f"initiating {i}th thread")


texts: BackgroundWorker in WPF executing multiple methods

texts:
I've implemented a BackgroundWorker in my WPF app. My code is pretty ugly though. I've been told to write as little as redundant code as possible. IS there some generic Method I could employ to reduce the amount of code?

    //BackgroundWorker Databases
    private void worker_updateDatabases(object sender, RunWorkerCompletedEventArgs e)
    {
        this.Databases = ModelSource.Databases;
        this.IsLoading = false;
    }

    private void worker_reportDatabaseProgress(object sender, ProgressChangedEventArgs e)
    {
        this.IsLoading = true;
    }

    private void worker_FetchDatabases(object sender, DoWorkEventArgs e)
    {
        ModelSource.getDatabasesAsync(this._currentConfig);
        (sender as BackgroundWorker).ReportProgress(ModelSource.Databases.Count);
    }

    //BackgroundWorker Tables
    private void worker_updateTables(object sender, RunWorkerCompletedEventArgs e)
    {
        this.Tables = ModelSource.Tables;
        this.IsLoading = false;
    }

    private void worker_reportTableProgress(object sender, ProgressChangedEventArgs e)
    {
        this.IsLoading = true;
    }

    private void worker_FetchTables(object sender, DoWorkEventArgs e)
    {

        ModelSource.getTablesAsync(SelectedDatabase.Name);
        (sender as BackgroundWorker).ReportProgress(ModelSource.Tables.Count);
    }
    #endregion


texts: Harnessing hashCode as JAXB @XmlID in order to use it with @XmlIDREF

texts:
I have a class Fighter in a software for planning boxing events.  I implemented its hashCode() and equals() since I collect them in various data structures in many places.

Now I want to persist specific planned boxing events which themselves contain the same Fighters  in different lists.  I am using JAXB for this purpose.

In order not to have duplication in the XML nodes I made use of @XmlID and @XmlIDREF such that only once the whole Fighter data is stored – but referenced in other places. The code for the id is simple and it works as expected:

@XmlID
@XmlAttribute
public String getId() {
    return Integer.toString(hashCode());
}

(I was also searching for an auto-mechanism to generate those ids by JAXB but could not find anything which would be simpler as the solution above. Please tell me, if there is such a solution!)

The hashCode method looks as follows:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result =
        prime * result + ((firstNameProperty == null) ? 0 : firstNameProperty.get().hashCode());
    result = prime * result + ((lastNameProperty == null) ? 0 : lastNameProperty.get().hashCode());
    result = prime * result + ((genderProperty == null) ? 0 : genderProperty.get().hashCode());
    result = prime * result + ((birthdayProperty == null) ? 0 : birthdayProperty.get().hashCode());
    result = prime * result + ((clubProperty == null) ? 0 : clubProperty.get().hashCode());
    long temp;
    temp = Double.doubleToLongBits((weightProperty == null) ? 0 : weightProperty.get());
    result = prime * result + (int) (temp ^ (temp >>> 32));
    result = prime * result + ((winsProperty == null) ? 0 : winsProperty.get());
    result = prime * result + ((lossesProperty == null) ? 0 : lossesProperty.get());
    result = prime * result + ((drawsProperty == null) ? 0 : drawsProperty.get());
    return result;
}

And the class member variables which are used here:

private StringProperty firstNameProperty;
private StringProperty lastNameProperty;
private ObjectProperty<Gender> genderProperty;
private StringProperty clubProperty;
private ObjectProperty<LocalDate> birthdayProperty;
private DoubleProperty weightProperty;
private IntegerProperty winsProperty;
private IntegerProperty lossesProperty;
private IntegerProperty drawsProperty;

My rationale was that hashCode() would give me a unique id for every fighter and is already there.

What do you think about that? Are there any drawbacks I just don't see? Is it common to do it this way? Is there a far better solution?


texts: Calculator for scoring dives in a competition

texts:
I was looking for a light weight coding challenge and found this:

  In the sport of diving, seven judges award a score between 0 and 10, where each score may be a floating-point value. The highest and lowest scores are thrown out and the remaining scores are added together. The sum is then multiplied by the degree of difficulty for that dive. The degree of difficulty ranges from 1.2 to 3.8 points. The total is then multiplied by 0.6 to determine the diver’s score.
  Write a computer program that inputs a degree of difficulty and seven judges’ scores, and outputs the overall score for that dive. The program should ensure that all inputs are within the allowable data ranges.

So, I tried to write some modern c++, and I am looking for suggestions to modernize it or is it so basic that this isn't a concept.

#include "pch.h"
#include <iostream>
#include <sstream>
#include <string>

constexpr auto JUDGES = 7;
constexpr auto FUDGE_FACTOR = 0.6f;
constexpr auto MIN_DIFFICULTY = 1.2f;
constexpr auto MAX_DIFFICULTY = 3.8f;
constexpr auto MIN_SCORE = 0.f;
constexpr auto MAX_SCORE = 10.f;

using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::stringstream;

float getInput(float low, float high)
{
    string line;
    float ret;
    while (std::getline(cin, line))
    {
        stringstream ss(line);
        if (ss >> ret && ss.eof()) {
            if (ret>=low && ret<=high) return ret;
        }
        cout << "invalid input, try again: ";
    }
    return -1.0;
}

int main()
{
    float difficulty;
    float total{ 0. };
    float min{ MAX_SCORE };
    float max{ MIN_SCORE };

    cout << "please enter degree of difficulty for the dive: ";
    difficulty = getInput(MIN_DIFFICULTY, MAX_DIFFICULTY);
    for (int i = 0; i < JUDGES; ++i) {
        cout << "please enter the judge #"<<i+1<<"'s score: ";
        auto score = getInput(MIN_SCORE, MAX_SCORE);
        if (score < min) min = score;
        if (score > max) max = score;
        total += score;
    }
    total -= (min + max);
    total *= difficulty * FUDGE_FACTOR;
    cout << "The dive scored: " << total << endl;
}

Any thoughts/suggestions will be welcome.


texts: Better way to create objects from strings

texts:
I'm looking for a better way to create objects based off of a value of a string.

Consider the following:

Input file:

//each Vehicle type has a list of different options
//whose length varies, I provided a simple case
van*2006*dodge*grand caravan*green~
car*2010*toyota*corolla*red~
truck*2005*toyota*tundra*black~

Classes:

abstract class Vehicle: IVehicle{
    public int Year;
    public string Make;
    public string Model;

    ...
}

class Van : Vehicle
{
    ...
}

class Car : Vehicle
{
    ...
}

class Truck : Vehicle
{
    ...
}

Program:

static void Main(string[] args)
{
    char delimiter = '*';
    string fileToRead = "inventory.csv";
    string currentLine = string.Empty;
    List<IVehicle> inventory = new List<IVehicle>();

    //Open file
    using (StreamReader reader = new StreamReader(fileToRead))
    {
        //while you read each line
        while ((currentLine = reader.ReadLine()) != null)
        {
            IVehicle temp;

            //Tokenize the line
            string[] parts = currentLine.Split(delimiter);

            switch(parts[0])
            {
                case "van":
                    temp = new Van();
                    ...
                break;
                case "car"
                    temp = new Car();
                    ...
                break;
                case "truck"
                    temp = new Truck();
                    ...
                break;
            }

            inventory.add(temp);
        }
    }

    ...
}

My problem with the code is the switch statement. It feels messy and I'm looking for a better way.


texts: Invert rows and columns in an Excel file

texts:
Annother little excercise with openpyxl:

Say you have a Excel file like this:

The Goal is to make make it look like this:

My code to acomplish this:

 inversion.py

"""
Inverts the content of table row and columns
"""

from copy import copy
import openpyxl
from openpyxl.utils import get_column_letter

def save_workbook_excel_file(workbook, filename: str):
    """Tries to save created data to excel file"""
    try:
        workbook.save(filename)
    except PermissionError:
        print("Error: No permission to save file.")

def invert_row_column(filename: str):
    """
    Main loop to invert rows and column
    """
    workbook = openpyxl.load_workbook(filename)
    sheet_names = workbook.sheetnames
    sheet = workbook[sheet_names[0]]
    workbook.create_sheet(index=0, title='tmp_sheet')
    tmp_sheet = workbook['tmp_sheet']

    data = []
    for row in sheet:
        cells = []
        for cell in row:
            cells.append(cell)
        data.append(cells)

    for x in range(0, len(data)):
        for y in range(0, len(data[x])):
            column = get_column_letter(x + 1)
            row = str(y + 1)
            tmp_sheet[column + row] = copy(data[x][y].value)

    sheet_name = sheet.title
    del workbook[sheet_name]
    tmp_sheet.title = sheet_name
    save_workbook_excel_file(workbook, 'updated_' + filename)

invert_row_column("test.xlsx")

I wonder how this can be improved?
Can the naming be better?
Is there a better / or shorter solution?


texts:
I recently started doing some competitive programming in C and one of my first requirements was a high-speed token reader (analogous to the java Scanner class' next() function). A few examples of input I am most likely to read are:
5
ccadd
bddcc

5 4 1
1 2 5
2 3 7
3 4 8
4 5 2
2 3

The integer/float inputs will be handled using atoi() and atof(), so all I need to develop is a function that will read words from stdin. Here's the first prototype:
#define BUF_SIZE (1 << 10) // approx 2 KiB or 1024 chars

char* next_token() {
    char* buf = malloc(BUF_SIZE * sizeof(char));
    char cc;
    // consume leading whitespaces
    while (isspace(cc=getchar())) ;
    buf[0] = cc;
    int i=1;
    int nofs = 1;
    while (!isspace(cc=getchar())) {
        if (i >= BUF_SIZE*nofs) {
            // gracefully extend buffer size
            nofs++;
            buf = realloc(buf, BUF_SIZE*nofs*sizeof(char));
        }
        buf[i] = cc;
        i++;
    }
    // trim buffer
    buf = realloc(buf, (i+1)*sizeof(char));
    buf[i] = '\0';
    return buf;
}

int main() {
    int T = atoi(next_token());
    while (T-- > 0) {
        char* word = next_token();
        // more logic here
    }
}

Two questions I had with this code are:

Is this fast enough? I think the major bottleneck lies with the realloc at the end, where I trim the length. If it's not fast enough, please suggest some optimizations.
Is this compliant with how C is generally written? I'm coming from Java and have little  experience with C code. I write some embedded C but that's closer to assembly than it is to this type of code.

Any further improvements are welcome.


texts: Making events not trigger so often on a datatable with dc.js

texts:
I used this answer to make a table with datatables.net and dc.js.  However, I detected a performance issue in this loop:

function RefreshTable() {
    dc.events.trigger(function () {
        datatable.api()
        .clear()
        .rows.add( YearDimension.top(Infinity) )
        .draw() ;
    });
}

count = dc.chartRegistry.list().length
for (var i=count; i--;) {
    dc.chartRegistry.list()[i].on("filtered", RefreshTable);
}
RefreshTable() ;

Because when resetting all the filters with

dc.filterAll();dc.redrawAll();

RefreshTable() is executed once for every dc chart (or dimension?). In my case it's 5 loops at ~500ms each, thus more than 2s for 4 charts and 10k records.

Has someone any idea on how to make it faster? The table should be refreshed only once.


texts:
Please review my implementation of mergesort:

mergesort' :: (Ord a) => [a] -> [a]
mergesort' []     = []
mergesort' xs = merge' (sort' fst', sort' snd')
   where fst' = take half xs
         snd' = drop half xs
         half = len `div` 2
         len = length xs

merge' :: (Ord a) => ([a], [a]) -> [a]
merge' ([], [])      = []
merge' ([], ys)      = ys
merge' (xs, [])      = xs
merge' (x:xs, y:ys) = if x < y then x : merge' (xs, y:ys) else y : merge'(x:xs, ys)

sort' :: (Ord a) => [a] -> [a]
sort' []     = []
sort' (x:xs) = m : sort' rest
  where m = foldl (\acc x -> if x < acc then x else acc) x xs
        rest = filterOne' (/= m) (x:xs)

filterOne' :: (a -> Bool) -> [a] -> [a]
filterOne' _ []     = []
filterOne' f (x:xs) = if not $ f x then xs else x : filterOne' f xs

If it doesn't adhere to the mergesort algorithm, please let me know.

Also, is there a Haskell library function for my filterOne'?


texts:
I'm writing a Telephone class and it has a method called getDigits which takes in a String parameter and returns the number that would appear on your phone if you typed in those letters the old fashioned way.

Example: Typing in "CAT" would return 228.

I wrote the following code and it works but I wanted to know if anyone else out there had better ideas to do it/ more sophisticated ones. I'm learning about data abstraction/enums and maybe that's a clue to how my professor wanted me to approach this but I'm stumped.

public int getDigits(String test){
        String result = "";
        String param = test.toUpperCase();
        for(int i = 0; i<param.length(); i++){
            String s = Character.toString(param.charAt(i));
            if(s.equals("A") || s.equals("B") || s.equals("C")){
                result += 2;
            }

            else if(s.equals("D") || s.equals("E") || s.equals("F")){
                result += 3;
            }

            else if(s.equals("G") || s.equals("H") || s.equals("I")){
                result += 4;
            }

            else if(s.equals("J") || s.equals("K") || s.equals("L")){
                result += 5;
            }

            else if(s.equals("M") || s.equals("N") || s.equals("O")){
                result += 6;
            }

            else if(s.equals("P") || s.equals("Q") || s.equals("R") || s.equals("S")){
                result += 7;
            }
            else if(s.equals("T") || s.equals("U") || s.equals("V")){
                result += 8;
            }

            else if(s.equals("W") || s.equals("X") || s.equals("Y") || s.equals("Z")){
                result += 9;
            }
        }
        return Integer.parseInt(result);
    }


texts: Web scraper extracts zip files and publishes them to redis

texts:
I wrote this program for a job interview a while back. I would like general feedback on style, readability, maintainability, or any obvious flaws:

 #!/usr/bin/ruby
require 'httparty'
require 'nokogiri'
require 'fileutils'
require 'zip'
require 'redis'

Zip.on_exists_proc = true ## When extracting zip files, old files will be overwritten.

class Scraper
  attr_accessor :uri
  attr_accessor :xml_dir
  attr_accessor :zip_dir
  attr_accessor :redis_key

  def initialize(uri: "http://feed.omgili.com/5Rh5AMTrc4Pv/mainstream/posts/", zip_dir: "tmp", xml_dir: "xml", redis_key: "NEWS_XML", limit: nil)
    ## Set some vars
    @uri = uri
    @zip_dir = zip_dir
    @xml_dir = xml_dir
    @redis_key = redis_key
    @limit = limit

    ## Get the filenames
    begin
    page_obj = Nokogiri::HTML(HTTParty.get(uri))
    rescue HTTParty::Error
      puts "Error connecting to #{uri}. It may be that your connection is down, or that the address is bad."
      puts "Exiting..."
      exit
    rescue StandardError
      puts "Error connecting to #{uri}. It may be that your connection is down, or that the address is bad."
      puts "Exiting..."
      exit
    end

    filenames = page_obj.css('td a').map {|node| node['href'] }.select {|attr| attr.include? ".zip"} ## Exclude anything that doesn't end with '.zip'
    filenames = filenames[0..limit-1] if limit.is_a?(Integer) && limit-1 >= 0
    zip_uris = filenames.map {|filename| uri + filename}
    filenames_to_uris = [filenames, zip_uris].transpose.to_h

    ## Download the files
    Dir.mkdir(zip_dir) if !Dir.exist? zip_dir
    filenames_to_uris.each do |name, uri|
      zip_data = HTTParty.get(uri).body
      File.write("#{zip_dir}/#{name}", zip_data)
    end
  end

  def extract_zip()
    Dir.mkdir(self.xml_dir) if !Dir.exists? self.xml_dir
    Dir.foreach(self.zip_dir).drop(2).each do |filename| ## .drop(2) because the first two elements are . and ..
      file_path = File.join(self.zip_dir, filename)
      Zip::File.open(file_path) do |zipped_files|
        zipped_files.each do |file|
          file.extract(File.join(self.xml_dir, file.name))
        end
      end
    end
  end

  def push_xml_to_redis()
    redis = Redis.new()
    file_list = Dir.foreach("#{self.xml_dir}").drop(2) ## .drop(2) because the first two elements are . and ..
    file_list.each do |filename|
      file_data = File.read(File.join(self.xml_dir, filename))
      redis.hset(self.redis_key, filename, file_data)
    end
  end
end

Where an example of usage would be:

s = Scraper.new(limit: 1)
s.extract_zip()
s.push_xml_to_redis()

The full project can be found on github.


texts:
This is an function that downloads a single file from an FTP server and saves it to disk
This is my first time doing anything with FTP, please let me know if i have missed anything.

public void DownloadFile(string fileloc, string saveLoc)
{
    try
    {
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(fileloc);
        request.Method = WebRequestMethods.Ftp.DownloadFile;

        request.Credentials = new NetworkCredential(Username, Password);

        using (var response = (FtpWebResponse)request.GetResponse())
        {
            using (var fileStream = File.Create(saveLoc))
            {
                response.GetResponseStream().CopyTo(fileStream);
            }
        }
    }
    catch (Exception EX)
    {
        throw EX;
    }
}


texts: Handle static objects using custom adapter

texts:
I have read some articles discussing about why static is bad, some of them refering static as not-testable, non-mockable, has state (non-stateless) and everything else.

However I have encountered some cases where I am forced to use static implementation, such as:

During using HttpContext.Current in Asp.Net, as well as Session
The HttpContext and Session sometimes contains configurations, which is needed to do some validations
Accessing some static objects in framework or third-party component
Maybe there are other situation where I don't yet encountered

Up until now I have handle those static methods using custom adapters (correct me if I misuse the term adapter here, or maybe it is better for provider), such as:

public class ConfigurationAdapter : IConfigurationAdapter
{
    public string CurrentUsername
    {
        get
        {
            return HttpContext.Current.User.Identity.Name;
        }
    }
}

Then the consumer will use the adapter like:

public class ConfigurationConsumer : IConfigurationConsumer
{
    public ConfigurationConsumer(IConfigurationAdapter adapter)
    {
        _adapter = adapter;
    }
    private readonly IConfigurationAdapter _adapter;

    public void DoSomething()
    {
        string userName = _adapter.CurrentUsername;
    }
}

I believe that this way I can get the benefits:

The adapter can be replaced by other implementation
The adapter can be mocked
The consumer does not directly dependent on HttpContext

However due to inexperienced in design, I am still wondering about:

Does this kind of implementation can really decouple the consumer with the static object?
Will it provide any problem in the future?
Is there any better design or pattern than this?

In this case I want to make sure that my application is testable, maintainable dan extendable. I believe performance is not effecting too much here, so it can be ignored.

Any kind of thoughts will be appreciated


texts: Translating code numbers to item descriptions in PostgreSQL query

texts:
For each itemId I need to select a value in my PostgreSQL query. I am doing it like this:

select itemId,
 CASE
-- Revenue
    WHEN itemId in (339,28,29)
    THEN 'Revenues'
    -- Rev CHG calculated on the back-end
--Expenditures
    WHEN itemId = 102
    THEN 'SG'||CHR(38)||'A'
    -- SGA CHG calculated on the back-end
    WHEN itemId = 4375
    THEN 'SG'||CHR(38)||'A MARGIN'
    WHEN itemId = 100
    THEN 'R'||CHR(38)||'D EXPENSE'
    -- RD CHG calculated on the back-end
    WHEN itemId  in (2021, 2114)
    THEN 'CAPEX'
    --CAPEX CHG calculated on the back-end
    WHEN itemId = 34
    THEN 'COGS'
--Income
    WHEN itemId = 379
    THEN 'Net Income'
    WHEN itemId = 7
    THEN 'Earnings from Cont. Ops'
    WHEN itemId = 83
    THEN 'Minority Interest'
    WHEN itemId = 280
    THEN 'Preferred Stock Dividend'
    -- NI CHG calculated on the back-end
    WHEN itemId = 400
    THEN 'EBIT'
    WHEN itemId = 4197
    THEN 'EBIT CHG 1 YR'
    WHEN itemId = 4210
    THEN 'EBIT CHG 2 YR'
    WHEN itemId = 4223
    THEN 'EBIT CHG 3 YR'
    WHEN itemId = 4236
    THEN 'EBIT CHG 5 YR'
    WHEN itemId = 4249
    THEN 'EBIT CHG 7 YR'
    WHEN itemId = 4262
    THEN 'EBIT CHG 10 YR'
    WHEN itemId = 4051
    THEN 'EBITDA'
    WHEN itemId = 4196
    THEN 'EBITDA CHG 1 YR'
    WHEN itemId = 4209
    THEN 'EBITDA CHG 2 YR'
    WHEN itemId = 4222
    THEN 'EBITDA CHG 3 YR'
    WHEN itemId = 4235
    THEN 'EBITDA CHG 5 YR'
    WHEN itemId = 4248
    THEN 'EBITDA CHG 7 YR'
    WHEN itemId = 4261
    THEN 'EBITDA CHG 10 YR'
    WHEN itemId = 4047
    THEN 'EBITDA MARGIN'
    WHEN itemId = 3064
    THEN 'EPS-BASIC'
    --EPS-BASIC CHG calculated on the back-end
    WHEN itemId = 142
    THEN 'EPS-DIL'
    --EPS-DIL CHG calculated on the back-end
    WHEN itemId = 10
    THEN 'GROSS PROFIT'
    WHEN itemId = 4195
    THEN 'GP CHG 1 YR'
    WHEN itemId = 4208
    THEN 'GP CHG 2 YR'
    WHEN itemId = 4221
    THEN 'GP CHG 3 YR'
    WHEN itemId = 4234
    THEN 'GP CHG 5 YR'
    WHEN itemId = 4247
    THEN 'GP CHG 7 YR'
    WHEN itemId = 4260
    THEN 'GP CHG 10 YR'
    WHEN itemId = 4074
    THEN 'GP MARGIN'
    --GP MARGIN CHG calculated on the back-end
    WHEN itemId in (21,5,356,22)
    THEN 'OP PROFIT'
    WHEN itemId = 373
    THEN 'Total Operating Exp'
    --OP PROFIT CHG calculated on the back-end
--Cash Flow
    WHEN itemId = 2207
    THEN 'Cash Flow'
    --CF CHG calculated on the back-end
    WHEN itemId = 4423
    THEN 'UNLEVERED FREE CASH FLOW'
    WHEN itemId = 4430
    THEN 'UCF CHG 1 YR'
    WHEN itemId = 4431
    THEN 'UCF CHG 2 YR'
    WHEN itemId = 4432
    THEN 'UCF CHG 3 YR'
    WHEN itemId = 4433
    THEN 'UCF CHG 5 YR'
    WHEN itemId = 4434
    THEN 'UCF CHG 7 YR'
    WHEN itemId = 4435
    THEN 'UCF CHG 10 YR'
    WHEN itemId = 2006
    THEN 'CASH OPS'
    WHEN itemId = 2081
    THEN 'Net Cash From Discont. Ops'
    --Cash Ops CHG calculated on the back-end
--Return
    WHEN itemId = 4178
    THEN 'ROA'
    WHEN itemId = 4363
    THEN 'ROC'
    WHEN itemId = 4128
    THEN 'ROE'
--Working Capital
    WHEN itemId = 4030
    THEN 'CURRENT RATIO'
    WHEN itemId = 4121
    THEN 'QUICK RATIO'
    WHEN itemId = 4177
    THEN 'ASSET TURNOVER'
    WHEN itemId = 4082
    THEN 'INVENTORY TURNOVER'
  END itemname in
  from item i
  join itemcode ic on i.itemcode = ic.itemcode
  WHERE ic.dataItemId IN (.................);

Can I do it in a better way?


texts: Simplify series of string manipulations in Ruby

texts:
I have a series of string manipulation, I realize this with the following code. I want it to be more elegant and readable such as str.sub_heading().sub_codeblock()..., how could I rewrite my code?

def sub_heading(str)
  str.gsub!(/^(#+)\w+/) {|m|  m.gsub("#", "=")}
  str.gsub!(/^(=+)(.+)/, '\1 \2 \1')
end

def sub_codeblock(str)
  str.gsub!(/```(.+\n)((.|\n)+)```/,  '{{{#!text_pygments \1\2}}}')
end

def sub_inlinecode(str)
  str.gsub!(/`(.+)`/, '{{{\1}}}')
end

def sub_bold(str)
  str.gsub!(/([^\w]|^)\*\*\w[\w\s\d\-_'"]*\*\*([^\w]|$)/) {|m|  m.gsub("**", "'''")}
end

def sub_italic(str)
  str.gsub!(/([^*]|^)\*\w[\w\s\d\-_'"]*\*([^*]|$)/) {|m|  m.gsub("*", "''")}
end

def sub_link(str)
  str.gsub!(/\[\[(.+)\|(.+)\]\]/, '[[\2|\1]]')
end

sub_heading(inputdata)
sub_codeblock(inputdata)
sub_inlinecode(inputdata)
sub_bold(inputdata)
sub_italic(inputdata)
sub_link(inputdata)


texts:
Given an array, print the Next Greater Element (NGE) for every element. The Next Greater Element for an element x is the first greater element on the right side of x in array. Elements for which no greater element exist, consider next greater element as -1.

Examples:

For any array, rightmost element always has next greater element as -1.
For an array which is sorted in decreasing order, all elements have next greater element as -1.
For the input array [4, 5, 2, 25}, the next greater elements for each element are as follows:

    Element       NGE
       4      -->   5
       5      -->   25
       2      -->   25
       25     -->   -1

For the input array [13, 7, 6, 12}, the next greater elements for each element are as follows.

    Element          NGE
      13      -->     -1
      7       -->     12
      6       -->     12
      12      -->     -1

final class DataSet {

    private final int x;
    private final int nextX;

    public DataSet(int x, int nextX) {
        this.x  = x;
        this.nextX = nextX;
    }

    public int getX() {
        return x;
    }

    public int getNextX() {
        return nextX;
    }

    @Override
    public String toString() {
        return "x: " + x + " nextX: " + nextX;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + nextX;
        result = prime * result + x;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DataSet other = (DataSet) obj;
        if (nextX != other.nextX)
            return false;
        if (x != other.x)
            return false;
        return true;
    }
}

public final class NextGreatestElement {

    private NextGreatestElement() {}

    /**
     * Computes list of dataset, where dataset is a tuple of
     * (currentInt, nextGreatestelement)
     *
     * @param a     the array
     * @return      list of dataset
     */
    public static List<DataSet> getNextGreatestElement(int[] a) {

        final List<DataSet> dataSetList = new ArrayList<DataSet>();
        final Stack<Integer> stack = new Stack<Integer>();

        for (int i = 0; i < a.length; i++) {

            while (!stack.isEmpty() && stack.peek() < a[i]) {
                dataSetList.add(new DataSet(stack.pop(), a[i]));
            }

            stack.push(a[i]);
        }

        while (!stack.isEmpty()) {
            dataSetList.add(new DataSet(stack.pop(), -1));
        }

        return dataSetList;
    }
}

public class NextGreatestElementTest {

    @Test
    public void test1() {
        int[] a1 = {2, 3, 4, 5};
        List<DataSet> ds1 = new ArrayList<DataSet>();
        ds1.add(new DataSet(2, 3));
        ds1.add(new DataSet(3, 4));
        ds1.add(new DataSet(4, 5));
        ds1.add(new DataSet(5, -1));
        assertEquals(ds1,  NextGreatestElement.getNextGreatestElement(a1));
    }

    @Test
    public void test2() {
        int[] a2 = {3, 2, 1};
        List<DataSet> ds2 = new ArrayList<DataSet>();
        ds2.add(new DataSet(1, -1));
        ds2.add(new DataSet(2, -1));
        ds2.add(new DataSet(3, -1));
        assertEquals(ds2, NextGreatestElement.getNextGreatestElement(a2));
    }

}


texts:
For decoding binary data (in my case, delivered by a Bluetooth device), I've written this struct:

public struct ConsumableByteArray {
    private let bytes: [UInt8]
    private var idx = 0

    enum Error: Swift.Error {
        case notEnoughBytes
    }

    init(data: Data) {
        bytes = [UInt8](data)
    }

    init(bytes: [UInt8]) {
        self.bytes = bytes
    }

    mutating func consume() throws -> UInt8 {
        guard idx < bytes.count else {
            throw Error.notEnoughBytes
        }
        defer { idx += 1}
        return bytes[idx]
    }

    mutating func consume() throws -> UInt16 {
        guard idx+1 < bytes.count else {
            throw Error.notEnoughBytes
        }
        defer { idx += 2 }
        return UInt16(bytes[idx+1]) << 8 + UInt16(bytes[idx])
    }

    mutating func consume() throws -> Int16 {
        guard idx+1 < bytes.count else {
            throw Error.notEnoughBytes
        }
        defer { idx += 2 }
        return Int16(bytes[idx+1]) << 8 + Int16(bytes[idx])
    }

    mutating func consume() throws -> UInt32 {
        guard idx+3 < bytes.count else {
            throw Error.notEnoughBytes
        }
        defer { idx += 4 }
        // Swift compiler insists on splitting this expression up
        let b3 = UInt32(bytes[idx+3]) << 24
        let b2 = UInt32(bytes[idx+2]) << 16
        let b1 = UInt32(bytes[idx+1]) << 8
        let b0 = UInt32(bytes[idx+0]) << 0

        return b3 + b2 + b1 + b0
    }
}

Given some data buffer, likely containing ints of varying widths packed together, it allows those fields to be read out:

let buffer = ConsumableByteArray(data: someData)
let header:UInt8 = try buffer.consume()
let word1:UInt16 = try buffer.consume()
let word2:UInt16 = try buffer.consume()
let crc32:UInt32 = try buffer.consume()

Values in the early data may alter the structure of the later data (e.g. whether a feature is supported or not), hence the need for the flexibility to extract data progressively.

Would you write the implementation any differently, or change the API?


texts:
This isn't so much as a specific code review, but a concept review when converting a class diagram to code and then eventually a database, in Python. I know how to add things in to a database, but I have only ever done this directly with SQL from forms and not from classes, (which I believe is called ORM)

I have the following class diagram

I have created the following classes to match the diagram

class Member:

  def __init__(self):
    self.memid = 0
    self.memname = ""

  def addMember(self, memid, memname):
    self.memid = memid
    self.memname = memname

  def searchbyid(memid):
    return 0

  def searchbyname(memname):
    return 0

  def newcust(self, custid, custname):
    self.custid = custid
    self.custname = custname

class Rental:

  def __init__(self):
    self.title = ""
    self.sartdate = ""
    self.enddate = ""
    self.cost = 0

  def addrental(self, title, startdate, enddate, cost):
    self.title = title
    self.startdate = startdate
    self.enddate = enddate
    self.cost = 0

class Movie:

  def __init__(self):
    self.movieID = 0
    self.title = ""
    self.genre = ""
    self.runtime = 0.0

  def newMovie(self, movieID, title, genre, runtime):
    self.movieID = movieID
    self.title = title
    self.genre = genre
    self.runtime = runtime

Forgetting the specific syntax (for example, data types, best practice etc) My question is, how do I get these to work together to model the real world scenario I have? I know I can create a member like this:

member = Member()
member.addMember(1, "John Sampson")

And I know I can create a movie like this:

movie = Movie()
movie.newMovie(47, "Die Hard", "Action", 94.27)

But when joining these things together to create a rental, would I do it like this?:

member.rental = Rental()
member.rental.addrental(movie.title, "07/02/2018", "18/02/2018", 10)

My further questions are:

How do I specifically link together classes as I have in my diagram (1 member having many rentals etc.) or would the link only be created when the data is passed in to a database?

How would I create a member search function? Would this only be able to work once I had a database up and running?

In my example above, I add Die Hard as the movie they rented because it's the only one I have an object for. However in the real world, they could choose any movie, so I assume I would have to search for the movie they entered and then link it from there. However, how would I search for a movie when objects have been created and before they have been added to the database? Would I add each object to a list as they are added?

I'm getting very stuck conceptually between the action of creating simple classes, and the method of making them work together so that we know a member has rented this specific video and we know the rental was for this movie etc.


texts: How to write this code without using 11 if-statements?

texts:
if (progressBar1.Value == 1) { progressLabel.Text = "1%"; }
if (progressBar1.Value == 10) { progressLabel.Text = "10%"; }
if (progressBar1.Value == 20) { progressLabel.Text = "20%"; }
if (progressBar1.Value == 30) { progressLabel.Text = "30%"; }
if (progressBar1.Value == 40) { progressLabel.Text = "40%"; }
if (progressBar1.Value == 50) { progressLabel.Text = "50%"; }
if (progressBar1.Value == 60) { progressLabel.Text = "60%"; }
if (progressBar1.Value == 70) { progressLabel.Text = "70%"; }
if (progressBar1.Value == 80) { progressLabel.Text = "80%"; }
if (progressBar1.Value == 90) { progressLabel.Text = "90%"; }
if (progressBar1.Value == 100) { progressLabel.Text = "100%"; }


texts: Filtering undefined elements out of an array

texts:
I have some code that filters an array with type (Suggestion|undefined)[] into an array with type Suggestion[], but I feel it does so very inelegantly:

const optionsProvider: AutocompletionProvider = () => {
    const results: (Suggestion|undefined)[] = flagDescriptions.map(descriptions => {
        let shortFlagWithArgument = descriptions[0].match(/^ *-(\w) (\w*)$/);
        let shortFlagWithoutArgument = descriptions[0].match(/^ *-(\w) *(.*)$/);
        if (shortFlagWithArgument) {
            const flag = shortFlagWithArgument[1];
            const argument = shortFlagWithArgument[2];
            const description = combineManPageLines(descriptions.slice(1));

            return new Suggestion({
                value: `-${flag}`,
                style: styles.option,
                description,
                displayValue: `-${flag} ${argument}`,
            });
        } else if (shortFlagWithoutArgument) {
            const flag = shortFlagWithoutArgument[1];
            const description = combineManPageLines([shortFlagWithoutArgument[2], ...descriptions.slice(1)]);

            return new Suggestion({
                value: `-${flag}`,
                style: styles.option,
                description,
            });
        }
    });
    const filteredResults: Suggestion[] = [];
    results.forEach(result => {
        if (result) {
            filteredResults.push(result);
        }
    });
    return filteredResults;
};

In straight JS, this would just be array.filter(element => element !== undefined) but the best working solution I could come up with in TypeScript was to push the defined elements into a separate array within a forEach.

For context:

type AutocompletionProvider = (context: AutocompletionContext) => Suggestion[] | Promise<Suggestion[]>;

Original PR: https://github.com/shockone/black-screen/pull/623/files


texts:
I wrote this method that adds two integers in the form of strings together. This way I can add large integers without overflowing.

How efficient is the code that I wrote? I am by no means a great coder, but I would consider myself somewhat decent. Is there anyway that would make it better?

I am a self taught coder and have only been doing it for about a year, so please be easy on me.

static string addString(string num1, string num2) {
        string retVal = null, cNum1 = null, cNum2 = null;
        int bf1 = 0, bf2 = 0;
        if (num1.Length > num2.Length) {
            for (int i = 0; i < (num1.Length - num2.Length); i++) {
                cNum2 += "0";
            }
        } else if (num1.Length < num2.Length) {
            for (int i = 0; i < (num2.Length - num1.Length); i++) {
                cNum1 += "0";
            }
        }
        cNum1 += num1;
        cNum2 += num2;
        char[] cAN1 = cNum1.ToCharArray();
        Array.Reverse(cAN1);
        cNum1 = new string(cAN1);
        char[] cAN2 = cNum2.ToCharArray();
        Array.Reverse(cAN2);
        cNum2 = new string(cAN2);
        for (int i = 0; i < cNum1.Length; i++) {
            bf2 = bf1 + int.Parse(cNum1.Substring(i, 1)) + int.Parse(cNum2.Substring(i, 1));
            if (bf2 > 9) {
                retVal += bf2.ToString().Substring(bf2.ToString().Length - 1, 1);
                bf1 = int.Parse(bf2.ToString().Substring(0, bf2.ToString().Length - 1));
            } else {
                retVal += bf2.ToString();
                bf1 = 0;
            }
        }
        if (bf1 != 0) {
            retVal += bf1.ToString();
        }
        char[] ret1 = retVal.ToCharArray();
        Array.Reverse(ret1);
        retVal = new string(ret1);
        return retVal;
    }


texts:
I'm making a project that needs a simple cross-platform command-line parser. I made this. Hope you enjoy!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

#define __NUM_OF_ELEMENTS(ARR) (sizeof(ARR)/sizeof(ARR[0]))
#define __HAS_ARG(FLAGS, ARG) (FLAGS & (1 << ARG))

void panic(const char *fmt, ...){
    fprintf(stderr, "error: ");

    va_list arglist;
    va_start( arglist, fmt );
    vfprintf(stderr, fmt, arglist);
    va_end( arglist );

    exit(EXIT_FAILURE);
}

void help(const char * progName){
    char * whoami = 0;
    (whoami = strrchr(progName, '/')) ? ++whoami : (whoami = progName);
    printf(

           "%s is intended to do blah\n"
           "Options:\n"
           " --help                Display This message\n"
           " --file [file name]    Input file to use\n",

    whoami);
    exit(EXIT_SUCCESS);
}

int main(int argc, char *argv[])
{
    if(argc == 1){
        help(argv[0]);
    }

    const char * args[] = {"--help", "--file", "--2cool4scool"};
    enum                  {HELP_ARG, FILE_ARG, TOO_COOL_FOR_SCHOOL_ARG};
    unsigned int flags = 0;

    //skip prog name
    for(unsigned int arg = 1, i = 0; arg < argc; ++arg){
        for(i = 0; i < (unsigned int)__NUM_OF_ELEMENTS(args); ++i){
            if(!strcmp(argv[arg], args[i])){
                flags |= 1 << i;
                goto CMD_ARGUMENT_FOUND;
            }
        }

        //argument is not found / unkown arg
        panic("Argument '%s' not found\n", argv[arg]);

        CMD_ARGUMENT_FOUND:;
    }

    if(__HAS_ARG(flags, HELP_ARG)){
        help(argv[0]);
    }
    if(__HAS_ARG(flags, TOO_COOL_FOR_SCHOOL_ARG)){
        puts("Hell YA!");
    }

    return 0;
}


texts:
I was successfully able to write a small script using PySpark to retrieve and organize data from a large .xml file. Being new to using PySpark, I am wondering if there is any better way to write the following code:

import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.functions import monotonically_increasing_id

### xml file from https://wit3.fbk.eu/
sc = SparkSession.builder.getOrCreate()
df = sc.read.format("com.databricks.spark.xml").option("rowTag","transcription").load('ted_en-20160408.xml')
df_values = df.select("seekvideo._VALUE")
df_id = df.select("seekvideo._id")
df_values = df_values.withColumn("id", monotonically_increasing_id())
df_id = df_id.withColumn("id", monotonically_increasing_id())
result = df_values.join(df_id, "id", "outer").drop("id")
answer = result.toPandas()

transcription = dict()
for talk in range(len(ted)):
    if not answer._id.iloc[talk]:
        continue
    transcription[talk] = zip(answer._id.iloc[talk], answer._VALUE.iloc[talk])

where df is of the form:

DataFrame[_corrupt_record: string, seekvideo: array<struct<_VALUE:string,_id:bigint>>]

and transcription is a dictionary of the transcriptions of each TED Talk keyed by position. For example, transcription[0] is of the form:

[(800, u'When I moved to Harare in 1985,'),
 (4120,
  u"social justice was at the core of Zimbabwe's national health policy."),
 (8920, u'The new government emerged from a long war of independence'),
 (12640, u'and immediately proclaimed a socialist agenda:'),
 (15480, u'health care services, primary education'),
...
]


texts: JavaScript function to calculate the average of an array

texts:
I have this function that calculates average. I don't want it to look like this. I am hoping for a less cliche average function because I see that most people's look like this.

function mean(array) {
    var num=0;
    for (var i=1;i<=array.length;i++) num=num+array[i];
    var divide=num/array.length;
    alert(divide);
}

Can anyone change this up to be better than before? It doesn't have to be more efficient but just look better.


texts:
This checks whether it's valid to make a processing. Processing can occur if:

there are files and spreadsheets (spreadsheet is the spec attribute)
there are any files or spreadsheets (it never occurs if there is at least one of the two)

AllFiles is empty only if one has sent a zip, but it wasn't unzipped and has been deleted. If unzipped and deleted, allFilesIsEmpty is false because one usually receives files in addition to the formats allowed that the program doesn't delete.

It should be at least one .swf, .gif or .jpg processing to occur. If not, it's not an allowed file format.

I described the best in the code but I have cyclomatic complexity. Any idea on how I can reduce?

private boolean directoryIsEmpty(OldValidacao validacao) {
        Map<String, String> allFiles = validacaoService.getFilenameMap(validacao.getArquivoPath());
        if (allFiles.isEmpty() && validacao.getSpec() == null) {
            return true;
        } else if (validacao.getSpec() != null) {
            return false;
        } else if (!allFiles.isEmpty()) {
            String files = allFiles.toString();
            if (files.contains("swf") || files.contains("jpg") || files.contains("gif")) {
                return false;
            }
        }
        return true;
    }