texts: BoltDB Lookup and Add methods and advice on unit testing

texts:
I am writing a Go package that handles communication with BoltDB as a part of some larger project. I have 2 methods for interacting with DB.
func Lookup(path string) (string, bool) {}
func RegisterUrl(path, url string) error {}

Lookup makes a request to database to retrieve corresponding url, whereas RegisterUrl writes path:url key-value pair to DB.
I would like to ask for a review of my implementation and some advice on how to implement unit tests.
Unit Testing
I've included the skeleton for unit tests that I would like to have for above 2 functions. However I have trouble figuring what is the best way to implement them.

I wouldn't like to test methods using the "prod" database/bucket, rather then that I would prefer to swap prod DB to test DB in the database_test.go file.
I thought about making dbName and bucketName public variables in database.go and then change them in _test file for test db, but this seems like I would be breaking the encapsulation.
I also considered passing test database/bucket to functions as functional options, but this seems like it will make code more complicated and I am not sure if this is the best approach.

I would be happy to hear your advice.
database.go
// Package database handles communication with BoltDB.
package database

import (
    "log"
    "time"

    "github.com/boltdb/bolt"
)

var dbOptions *bolt.Options = &bolt.Options{
    Timeout: 1 * time.Second,
}

const (
    dbName     = "urls.db"
    bucketName = "UrlFromPath"
)

func init() {
    // Make sure that the database exists.
    db, err := bolt.Open(dbName, 0600, dbOptions)
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
    // Make sure that UrlFromPath bucket exists.
    err = db.Update(func(tx *bolt.Tx) error {
        _, err := tx.CreateBucketIfNotExists([]byte(bucketName))
        if err != nil {
            return err
        }
        return nil
    })
    if err != nil {
        log.Fatal(err)
    }
}

// Lookup checks if `path` key exists in database and returns related `url` value if found. If not
// found second return value `ok` is set to false.
func Lookup(path string) (url string, ok bool) { // TODO: return
    // Open connection to DB.
    db, err := bolt.Open(dbName, 0600, dbOptions)
    if err != nil {
        log.Fatalf("Cannot connect to DB: %s", err)
        return "", false
    }
    defer db.Close()

    // Retreive url from DB.
    var url_bytes []byte
    err = db.View(func(tx *bolt.Tx) error {
        b := tx.Bucket([]byte(bucketName))
        url_bytes = b.Get([]byte(path))
        return nil
    })
    if err != nil {
        return "", false
    }
    if url_bytes != nil {
        return string(url_bytes), true
    } else {
        return "", false
    }

}

// RegisterUrl saves provided key:value (path:url) pair to database.
func RegisterUrl(path, url string) error {
    // Open connection to DB.
    db, err := bolt.Open(dbName, 0600, dbOptions)
    if err != nil {
        return err
    }
    defer db.Close()

    // Save path and url.
    err = db.Update(func(tx *bolt.Tx) error {
        b := tx.Bucket([]byte(bucketName))
        err := b.Put([]byte(path), []byte(url))
        return err
    })
    if err != nil {
        return err
    }

    log.Printf("Database: %s, %s registered.", path, url)
    return nil
}

database_test.go
package database

import "testing"

func setup() {
    // Init test db.
    // Init test bucket.
}

func cleanup() {
    // Remove test db.
}

func TestLookup(t *testing.T) {
    // T1. Path exists in db. Correct url is retrieved.

    // T2. Path doesn't exist in db. false is returned.

}

func TestRegisterUrl(t *testing.T) {
    // T1. Add path:url to db. Correct path:url pair has been added to db.
}
```


texts:
I've recently migrated away from floats and have started using flexbox to build my grids.

My navigation has 4 menu items. I'd like my logo to sit in the middle of these. To maintain a good page structure for SEO, I don't like the idea of putting my logo within a list item inside the nav.

Instead, I've built a solution that uses flexbox and a negative margin.

/*------------------------
Default grid
-------------------------*/
.flex {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-between;
}

.col {
  flex: 1;
}

/*------------------------
Columns
-------------------------*/
.has-2-columns .col {
  flex: none;
}

.has-2-columns .col {
  width: 49%;
}

/*------------------------
Logo
-------------------------*/
.site-title {
  position: relative; /* Enables z-index */
  z-index: 1; /* Positions logo above .main-navigation */
  width: 80px;
  margin: 0 auto;
  background: #ccc;
}

/*------------------------
Main navigation
-------------------------*/
.main-navigation {
  position: relative;
  margin-top: -24px; /* Brings nav inline with .site-branding */
  z-index: 0; /* Ensures nav is below .site-branding */
}

.main-navigation ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.main-navigation ul li {
  display: inline-block;
}

.left-menu {
  text-align: left;
}

.left-menu li {
  margin-right: 24px;
}

.right-menu {
  text-align: right;
}

.right-menu li {
  margin-left: 24px;
}

Demo

Home

About

Contact

Store

Is there a better way? Would this solution cause issues across different devices when it comes to clicking / tapping the menu items or logo to go back home?


texts: Replacing IUnitOfWork with a Factory Pattern

texts:
As a follow-up to this blog post and this question, I wanted to post my idea of replacing the IUnitOfWork with a factory pattern. It seems to me that in the case of this blog post the IUnitOfWork seems to be a type of sudo-factory pattern with each repository type and their accessors hardcoded. So my goal was to come up with a fully dynamic factory. I haven't had the chance to try this yet but wanted to post the idea to get some opinions.

IGenericRepository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace DataAccess
{
    public interface IGenericRepository<T>
    {
        IQueryable<T> AsQueryable();

        IEnumerable<T> GetAll();
        IEnumerable<T> Find(Expression<Func<T, bool>> predicate);
        T Single(Expression<Func<T, bool>> predicate);
        T SingleOrDefault(Expression<Func<T, bool>> predicate);
        T First(Expression<Func<T, bool>> predicate);
        T GetById(int id);

        void Add(T entity);
        void Delete(T entity);
        void Attach(T entity);
    }
}

EFGenericRepository

using System.Data.Entity;
using DataAccess;

namespace EFDataAccess
{
    public class EFGenericRepository<T> : IGenericRepository<T> where T : class
    {

        private readonly DbSet<T> _dbSet;

        public EFGenericRepository(DbSet<T> dbSet)
        {
            _dbSet = dbSet;
        }

        // Implement IGenericRepository Methods here...
    }
}

RepositoryFactory

using System;
using System.Data.Entity;
using DataAccess;

namespace EFDataAccess
{
    class RepositoryFactory
    {

        private readonly DbContext _context;

        public RepositoryFactory(DbContext context)
        {
            _context = context;
        }

        public IGenericRepository<T> Get<T>() where T : Type
        {
            return new EFGenericRepository<T>(_context.Set<T>());
        }
    }
}

Edit:
Created a github repo here


texts:
I'm tying to convert from this formatted string:

$filtersStr = "id:1||name:alex";

to:

['id' => 1,'name' => 'alex']

My solution for now:

foreach(explode('||', $filtersStr) as $filter){
            list($k, $v) = explode(':', $filter);
            $filters[ $k ] = $v;
        }

I'm looking for a more elegant way.


texts:
I have this string containing this block of code:

[General]
StartWithLastProfile=0
Profilist.notifications=true
Profilist.dev=true
Profilist.dev-builds=80k
Profilist.launch_on_create=true

[Profile0]
Name=Dev Profilist
IsRelative=1
Path=Profiles/j0a1zjle.Unnamed Profile 1
Profilist.tie=0

[Profile55]
Name=Main
IsRelative=1
Path=Profiles/qekfxcdm.Unnamed Profile 1

Currently I use regex to read this into an object like this:

var str = '[General]\nStartWithLastProfile=0\nProfilist.notifications=true\nProfilist.dev=true\nProfilist.dev-builds=80k\nProfilist.launch_on_create=true\n\n[Profile0]\nName=Dev Profilist\nIsRelative=1\nPath=Profiles/j0a1zjle.Unnamed Profile 1\nProfilist.tie=0\n\n[Profile55]\nName=Main\nIsRelative=1\nPath=Profiles/qekfxcdm.Unnamed Profile 1';
console.time('parse');
var linePatt = /^(?:\[(.*)\]|(.*?)=(.*))$/gm;
var match;
var objParsed = {};
var lastKey;
while(match = linePatt.exec(str)) {
  console.info(match);
  // Array [ "[General]", "General", undefined, undefined ]
  // Array [ "StartWithLastProfile=0", undefined, "StartWithLastProfile", "0" ]
  if (match[2] == undefined) {
    lastKey = match[1];
    objParsed[lastKey] = {};
  } else {
    objParsed[lastKey][match[2]] = match[3];
  }
}
console.timeEnd('parse');
console.log('objParsed:', JSON.stringify(objParsed));

This gives me an object like this:

{
    "General": {
        "StartWithLastProfile": "0",
        "Profilist.notifications": "true",
        "Profilist.dev": "true",
        "Profilist.dev-builds": "80k",
        "Profilist.launch_on_create": "true"
    },
    "Profile0": {
        "Name": "Dev Profilist",
        "IsRelative": "1",
        "Path": "Profiles/j0a1zjle.Unnamed Profile 1",
        "Profilist.tie": "0"
    },
    "Profile55": {
        "Name": "Main",
        "IsRelative": "1",
        "Path": "Profiles/qekfxcdm.Unnamed Profile 1"
    }
}

The console.time and console.timeEnd tell me this takes on average from 4-7ms. My application is for a much bigger file, though, and goes from Profile0 to ProfileN the blocks repeat. Is there any faster way you would do this?


texts:
I'm wondering what I could do to improve the function that handles initializing the UI. I think it looks disgusting. Any ideas on what I could do besides splitting the code up into multiple functions to make it look better?

def init_ui(window, root_dir):
    window.title('ACFA Save Editor')
    tk.Label(window, text='Pilot name: ').grid(row=0)
    tk.Label(window, text='AC name: ').grid(row=1)
    tk.Label(window, text='COAM (money): ').grid(row=2)
    tk.Label(window, text='FRS Memory: ').grid(row=3)
    e_p_name = tk.Entry(window)
    e_p_name.grid(row=0, column=1)
    e_ac_name = tk.Entry(window)
    e_ac_name.grid(row=1, column=1)
    e_coam = tk.Entry(window)
    e_coam.grid(row=2, column=1)
    e_frs_memory = tk.Entry(window)
    e_frs_memory.grid(row=3, column=1)
    messagebox.showinfo('Open Save Folder', 'Ex: GAMEDATXXXX')
    directory = askdirectory(initialdir=root_dir)
    APGD = directory + '/APGD.dat'
    GAMEDAT = directory + '/' + directory.split('/')[-1] + '_CONTENT'
    p, a, c, f = read_data(APGD)
    e_p_name.insert(0, p)
    e_ac_name.insert(0, a)
    e_coam.insert(0, c)
    e_frs_memory.insert(0, f)
    b_apply = tk.Button(window, text='Apply', width=10, command=lambda:
        apply(e_p_name.get(), e_ac_name.get(), e_coam.get(),
        e_frs_memory.get(),
        APGD, GAMEDAT)).grid(row=4, column=0)
    b_quit = tk.Button(window, text='Quit', width=10,
        command=window.destroy).grid(row=4, column=1)
    return window

The rest of the code can be found here.


texts:
I wrote my first animation in Java FX and would like a code review.

I created StackPane with Rectangle. Rectangle starts to move inside StackPane to right-bottom direction and changes direction after hitting the  StackPane bound.

Please review the code that moves Rectangle. Is it a correct implementation of a Java FX animation?

//...
new RectangleMover(pane, shape);
// ...

RectangleMover.java

public class RectangleMover extends Timer
{
   private final StackPane pane;
   private final Rectangle shape;
   private boolean moveRight = true;
   private boolean moveBottom = true;

   public RectangleMover(StackPane pane, Rectangle shape)
   {
      super(true);
      this.pane = pane;
      this.shape = shape;
      this.scheduleAtFixedRate(new Task(), 0, 10);
   }

   private static int boolToInt(boolean bool)
   {
      return bool ? 1 : -1;
   }

   private final class Task extends TimerTask
   {
      @Override
      public void run()
      {
         shape.setLayoutX(shape.getLayoutX() + boolToInt(moveRight));
         shape.setLayoutY(shape.getLayoutY() + boolToInt(moveBottom));

         if (shape.getLayoutX() >= pane.getWidth() - shape.getWidth())
         {
            moveRight = false;
         }
         if (shape.getLayoutY() >= pane.getHeight() - shape.getHeight())
         {
            moveBottom = false;
         }
         if (shape.getLayoutX() <= 0)
         {
            moveRight = true;
         }
         if (shape.getLayoutY() <= 0)
         {
            moveBottom = true;
         }
      }
   }
}

JFXTester.java (main class)

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class JFXTester extends Application
{

   StackPane root;

   @Override
   public void start(Stage primaryStage)
   {

      root = new StackPane();
      root.getChildren().add(createStartButton());

      Scene scene = new Scene(root, 300, 250);

      primaryStage.setTitle("Hello World!");
      primaryStage.setScene(scene);
      primaryStage.show();
   }

   private Rectangle createRectangle()
   {
      Rectangle r = new Rectangle();
      r.setX(0);
      r.setY(0);
      r.setWidth(30);
      r.setHeight(30);
      r.setFill(new Color(0.8, 0.7, 0.6, 0.5));

      return r;
   }

   private Button createStartButton()
   {
      final Button btn = new Button();
      btn.setText("START!");
      btn.setOnAction(new EventHandler<ActionEvent>()
      {
         @Override
         public void handle(ActionEvent event)
         {
            btn.setVisible(false);
            Rectangle r = createRectangle();
            root.getChildren().add(r);
            new RectangleMover(root, r);
         }
      });
      return btn;
   }

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

How to run application (java 1.7.0_51-b13):

D:\.temporary\java>dir /a-d /b /s
D:\.temporary\java\jfxtester\JFXTester.java
D:\.temporary\java\jfxtester\RectangleMover.java

D:\.temporary\java>%JAVA_HOME%\bin\javac -cp .;%JRE_HOME_7%\lib\jfxrt.jar ./jfxtester/JFXTester.java

D:\.temporary\java>%JRE_HOME_7%\bin\java -cp .;%JRE_HOME_7%\lib\jfxrt.jar jfxtester.JFXTester


texts: Extracting large tar.bz2 folders in parallel

texts:
The bash below utilizes parallel to extract tar.bz2 folders with 9 files in them. The total size of each is around 60GB. Below are some specs of my system. My question is the below the best/fastest (currently takes ~ 1 hour) way to extract these large tar.bz2 folders? I do not want to slow my system down or damage it so I wanted to ask more knowledgable users.

OS = Ubuntu 14.04, 64 bit
Memory = 128 GB
Processor = Intel® Xeon(R) CPU E5-2630 v3 @ 2.40GHz × 16

#!/bin/bash

# parallel untar of tar.bz2

maxproc=3 # Max number of threads.
i=0

# Count files
set -- /path/to/folders/*.tar.bz2
FILES="$#"

# Blank $1 $2 ...
set --

let i=1
for FILE in /path/to/folders/*.tar.bz2
do
    printf "(%2d/%2d)\tProcessing %s\n" "$i" "$FILES" "$FILE"
    let i=i+1

    tar -xvjf "$FILE" -C /path/to/folders >/dev/null &

    # Turn $1=pida $2=pidb $3=pidc $4=pidd, into
    #      $1=pida $2=pidb $3=pidc $4=pidd $5=pide
    set -- "$@" $!

    # Shift removes $1 and moves the rest down, so you get
    # $1=pidb $2=pidc $3=pidd $3=pide
    # $# is the number of arguments.
    if [ "$#" -ge $maxproc ]  ; then wait "$1" ; shift; fi
done

wait


texts: Comparing strings, ignoring case, punctuation, and whitespace

texts:
I have built a search function that will compare strings regardless of punctuation and whitespace. It is case insensitive.

Here is my current function:

var itemsToFilter = MPMediaQuery.songs().items!

func searchForItemsWithString(_ searchText: String) {
    if searchText != "" {
            let filteredItems = itemsToFilter.filter { item in
                let filterStr = item.title

                if filterStr == nil {
                    return false
                }

                let punctuation = CharacterSet.punctuationCharacters
                let whitespace = CharacterSet.whitespaces
                let unwanted = punctuation.union(whitespace)
                let filterStringContainsSearchText = filterStr!.components(separatedBy: unwanted).joined(separator: " ")

                return filterStringContainsSearchText.contains(searchText)
            }

    }
}

This works, but it is very slow. How can I optimise it?

What else can I improve?


texts: Optimising single-delimiter string tokenisation

texts:
I am trying to optimise my tokenizing of tab delimited strings:

static void split_line(string &line, input_record &rec)
{
    int col = 0;
    char *row = &line[0];
    char *token = strtok(row, "\t");
    while (token)
    {
        switch(++col)
        {
            case 2 : rec.sequence = token; break;
            case 4 : rec.content = token; break;
            case 9 : rec.position = atoi(token); return;
        }
        token = strtok(NULL, "\t");
    }
}

where

struct typedef
{
    string sequence;
    string content;
    unsigned int position;
} input_record

This function is called on the result from each getline from a parent, causing split_line to be called over 100 million times.

Each line has at least 15 columns (of which 2, 4 and 9 are useful), and I assign the relevant tokens to variables in a struct. The first 10 columns of a string contain roughly 150 characters (variable length) and I am handling millions of records. Currently, it takes ~0.8µs to process a string, and is a bottleneck in my code.

Does anyone have advice for squeezing more speed out of this?


texts:
I am generating a searchable and sortable data table using bootstrap. All works fine.

I am looking for the best approach to get it. Can anyone can please update/suggest the right way, if you find anything better?

jsfiddle

var tableMaker = function (columNames, parent, data) {
    var table = $('
', {
        id:"example",
        class:"table table-striped table-bordered"
    })
    .append($('

'))
    .append($('

'))
    .append($('

'));

    var thead = columNames.map(function (item) {
        return "" + item + "	";
    });

    var tbody = data.map(function (item, index) {
        var tr = $('
');
        columNames.map(function (label) {
            var label = label.replace(' ', '');
            var labelData = item[label];
            tr.append('' + labelData + '	')
        });
        return tr;
    });

    table
    .find('thead tr')
    .append(thead).end()
    .find('tfoot tr')
    .append(thead).end()
    .find('tbody')
    .append(tbody)
    parent.append(table); //appending to parent
    table.dataTable(); //starting tablar data works
};

var initTable = function () {
    var columNames = ['Organization Name', 'ZipCode', 'Telephone' ];
    var data = $.getJSON('https://tcs.firebaseio.com/d/DocPageDetails/d/Organizations.json');
    data.done(function (data) {
        tableMaker(columNames, $('#container'), data);
    });
}

initTable();


texts:
So I am trying to implement a menu in a program I am writing. My goal is to have zero errors when collecting user input. So I thought the best way to validate was to capture the input as a string and then test against it and prompt for input again until it is valid.

As far as I can tell, my code works as I want it to. BUT, is this the best way to go about this? Almost every example online uses char or int for the input, are there benefits to this?. I feel like I made it more complex than it needed to be. As a beginner, any suggestions would be appreciated.

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

string GetInput()
{
    string s = "";

    cout << ">> ";
    getline(cin, s);

    return s;
}
void mainMenu() {

    cout << "Select an option" << endl;
    cout << "1. Add Entry" << endl;
    cout << "2. Edit Entry" << endl;
    cout << "Q. Quit" << endl;

    string input = GetInput();

    while (!(input.find_first_not_of("12qQ") == string::npos) || input.empty())
    {
        input = GetInput();
    }

    if (input == "1") {
        cout << "Add Entry" << endl;
        //AddEntry();
    }

    else if (input == "2") {
        cout << "Edit Entry" << endl;
    }

    else {
        cout << "Quit" << endl;
    }

}

int main() {

    cout << "===================================================" << endl;
    cout << "|                 Secure Database                 |" << endl;
    cout << "===================================================" << endl << endl;

    mainMenu();

    return 0;
}


texts:
This database must be able to track multiple home owners, who may own many properties, have many phone numbers, and their mailing address may depend on what month we're in. Each property may have more than one owner.

Please point out all bad coding practices.

create table home_owner
(
    owner_id        SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    first_name      VARCHAR(10),
    last_name       VARCHAR(20)
);

create table email
(
    email_id        SMALLINT UNSIGNED PRIMARY KEY,
    email_address   VARCHAR(40)
);

create table owner_email
(
    owner_id        SMALLINT UNSIGNED,
    email_id        SMALLINT UNSIGNED,
    PRIMARY KEY emails_PK(owner_id, email_id),
    CONSTRAINT owner_email_FK FOREIGN KEY(owner_id) REFERENCES    home_owner(owner_id) ON DELETE CASCADE,
    CONSTRAINT email_owner_FK FOREIGN KEY(email_id) REFERENCES email(email_id) ON DELETE CASCADE
);

create table phone_number
(
    number_id       SMALLINT UNSIGNED PRIMARY KEY,
    phone_number    VARCHAR(10)
);

create table owner_number
(
    owner_id        SMALLINT UNSIGNED,
    number_id       SMALLINT UNSIGNED,
    PRIMARY KEY numbers_PK(owner_id, number_id),
    CONSTRAINT owner_number_FK FOREIGN KEY(owner_id) REFERENCES         home_owner(owner_id) ON DELETE CASCADE,
    CONSTRAINT number_owner_FK FOREIGN KEY(number_id) REFERENCES phone_number(number_id) ON DELETE CASCADE
);

create table seasonal_address
(
    seasonal_id     SMALLINT UNSIGNED PRIMARY KEY,
    address         VARCHAR(50),
    months          VARCHAR(27)
);

create table contact_info
(
    owner_id        SMALLINT UNSIGNED,
    seasonal_id     SMALLINT UNSIGNED,
    PRIMARY KEY contact_PK(owner_id, seasonal_id),
    CONSTRAINT owner_info_FK FOREIGN KEY(owner_id) REFERENCES home_owner(owner_id) ON DELETE CASCADE,
    CONSTRAINT seasonal_info_FK FOREIGN KEY(seasonal_id) REFERENCES seasonal_address(seasonal_id) ON DELETE CASCADE
);

create table property
(
    property_id     SMALLINT UNSIGNED PRIMARY KEY,
    address         VARCHAR(50),
    lot_number      SMALLINT UNSIGNED
);

create table owner_property
(
    owner_id        SMALLINT UNSIGNED,
    property_id     SMALLINT UNSIGNED,
    PRIMARY KEY properties_PK(owner_id, property_id),
    CONSTRAINT owner_property_FK FOREIGN KEY(owner_id) REFERENCES home_owner(owner_id) ON DELETE CASCADE,
    CONSTRAINT property_owner_FK FOREIGN KEY(property_id) REFERENCES property(property_id) ON DELETE CASCADE
);

insert into home_owner (first_name, last_name) values
("Angel", "Flop"),
("Bob", "Hoe"),
("Sue", "Hoe");

insert into email values
(1, "angel@hio.com"),
(2, "bob@ioj.com"),
(3, "sue@iojoiv.com");

insert into property values
(1, "123 Rainey", 123),
(2, "234 Bob", 1298),
(3, "697 Kolp", 782);

insert into seasonal_address values
(1, "7667 Noob", "1,2,3"),
(2, "2383 Fob", "4,5,6,7,8,9,10,11,12"),
(3, "7823 Flower", "1,2,3,4,5,6,7,8,9,10,11,12");

insert into phone_number values
(1, "5203601083"),
(2, "8039023093"),
(3, "2387784334"),
(4, "2377823782");

insert into owner_number values
(1, 1),
(1, 2),
(2, 3),
(3, 4);

insert into contact_info values
(1, 1),
(1, 2),
(2, 3),
(3, 3);

insert into owner_property values
(1, 1),
(1, 2),
(2, 3),
(3, 3);

insert into owner_email values
(1, 1),
(2, 2),
(3, 3);

select af.property_id
from property af
inner join owner_property op
on af.property_id = op.property_id
where op.owner_id = 1; //find all properties of owner_id = 1

select a.*
from home_owner a
inner join owner_property op
on a.owner_id = op.owner_id
where op.property_id = 3; //find all owners of property_id = 3

Table definitions here.


texts:
Let's assume I have an enum type

enum ComponentState {
    TURNED_OFF,
    TURNED_ON,
    SUSPENDED,
    TO_REPAIR;
}

This enum describes a state of some component. Now let's assume I want to export my component to as XML file. Then, of course, I'm going to import my component. So:

//Export:
componentElement.setAttribute("state", getState.name());  // results in <element state="TURNED_ON">

//Import
try {
   state = ComponentState.valueOf(element.getAttributeValue("state"));
} catch (IllegalArgumentException e) {
   //...
}

Everything is fine, but problems starts when I change the name of some enum constants, because it sounds better:

enum ComponentState {
    TURNED_OFF,
    TURNED_ON,
    SLEEPING,   // <- the same sense, but
    CRASHED;    // <- different name (PM's "I want to have")
}

Note, that doing this I'm loosing a backward compatibility.

Question:
How do you cope normally with such situations?

1

public String getXMLValue(){
    // ... implementation independent from name()
}

and

static ComponentState parseXMLString(String s) {
    // ... checking if s matches any name()
}

OR

2

My approach is OK - it should not be expected, that the name on enum will change just because it sounds better.


texts: Improve performance of Gaussian kernel function evaluation

texts:
I need to improve the performance of a function that calculates the integral of a two-dimensional kernel density estimate (obtained using the function stats.gaussian_kde) where the domain of integration is all points that evaluate below a given value.

I've found that a single line takes up pretty much 100% of the time used by the code:

insample = kernel(sample) < iso

(see the code below). This line stores in the list insample all the values in sample that evaluate in kernel to less than the float iso.

I made this question (slightly modified) 6 months ago over at Stack Overflow and the best answer involved parallelization. I'm hoping to avoid parallelization (gives way too much trouble as can be seen in the question linked above), but I'm open to any improvement in performance achieved by means of virtually any other way (including Cython and any package available).

Here's the minimum working example (MWE):

import numpy as np
from scipy import stats

# Define KDE integration function.
def kde_integration(m1, m2):

    # Perform a kernel density estimate (KDE) on the data.
    values = np.vstack([m1, m2])
    kernel = stats.gaussian_kde(values, bw_method=None)

    # This list will be returned at the end of this function.
    out_list = []

    # Iterate through all floats in m1, m2 lists and calculate for each one the
    # integral of the KDE for the domain of points located *below* the KDE
    # value of said float eveluated in the KDE.
    for indx, m1_p in enumerate(m1):

        # Compute the point below which to integrate.
        iso = kernel((m1_p, m2[indx]))

        # Sample KDE distribution
        sample = kernel.resample(size=100)

        # THIS TAKES ALMOST 100% OF THE COMPUTATION TIME.
        # Filter the sample.
        insample = kernel(sample) < iso

        # Monte Carlo Integral.
        integral = insample.sum() / float(insample.shape[0])
        # Avoid 'nan' and/or 'infinite' values down the line.
        integral = integral if integral > 0. else 0.000001

        # Append integral value for this point to list that will return.
        out_list.append(round(integral, 2))

    return out_list

# Generate some random two-dimensional data:
def measure(n):
    "Return two coupled measurements."
    m1 = np.random.normal(size=n)
    m2 = np.random.normal(scale=0.5, size=n)
    return m1+m2, m1-m2

# Random data.
m1, m2 = measure(100)
# Call KDE integration function.
kde_integration(m1, m2)

Any help/suggestions/ideas will be very much appreciated.


texts: Python program that checks connections based on URLLIB response

texts:
The main function tries to create a urllib connection, and if it fails/succeeds the label changes accordingly. However, I'm looking for ways to improve this code.

Some elements I'm already aware of:

The use of place really isn't ideal, I'm aware
Importing Urllib twice, but if I didn't do it like this the except: wasnt working correctly
Use of global

import urllib
import urllib.request as url
from tkinter import *

master = Tk()
master.geometry("280x36")
master.resizable(False, False)
master.config(bg="#4b4b4b")
master.overrideredirect(True)

master.wm_geometry("-0-40")
master.wm_attributes("-topmost", 1)

connectStatus = Label(bg="#e67e22", fg="#fff", width=29, height=1, text="Pending", font="Bahnscrift 10")
connectStatus.place(x=10,y=8)

closeButton = Button(width=2, bg="#706f6f", fg="#fff", borderwidth=0, text="x", command=master.destroy)
closeButton.place(x=253, y=8)

def displayCheck():
    colorFlash = "#38f789" if callbackAttempt == 1 else "#ff6656"
    connectStatus.config(bg=colorFlash)
    master.after(10, runRefresh)

def runRefresh():
    attemptConnect()
    master.after(2000, displayCheck)

def attemptConnect():
    global callbackAttempt

    try:
        callbackAttempt = 1
        callback = url.urlopen("https://stackoverflow.com", timeout=1)
        connectStatus.config(text="Connection Established", font="Bahnscrift 10", bg="#2ecc71")
        callback.close()

    except urllib.error.URLError:
        connectStatus.config(text="No Connection", bg="#e74c3c", font="Bahnscrift 10")
        callbackAttempt = 0

master.after(5, runRefresh)
master.mainloop()


texts: Binary searching the turning point of a function

texts:
The problem

You are given a list of \$ n \$ positive integers. Your task is to split the list into \$ k \$ subarrays so that the largest subarray sum is minimized.

The input

On the first line you have two integers n and k; the size of the list and the number of subarrays. On the second line you have n numbers: x1, x2, ..., xN.

The limits

\$ 1 \leq n \leq 10^5 \$

\$ 1 \leq k \leq n \$

\$ 1 \leq x \leq 10^9 \$

An example

Input

5 3

2 4 7 3 5

Output:

8 (the subarrays are [2,4], [7], [3,5])

My solution

Let's define a function isPossible, that returns true if it is possible to divide the list into \$ k \$ subarrays so that the maximum sum is the parameter sum. The function loops through the user-inputted array and pushes elements into a vector as long as the elements do not exceed the parameter sum. When the vector can't take the next element without exceeding sum, a new vector is created. All these created vectors are pushed into a vector and if the size of the vector holding all the vectors is less than or equal to \$ k \$, return true. Else return false.

The method vectorsum is a shortcut to getting the sum of the elements inside a vector.

After that I handle the input in main() and binary search the turning point of isPossible.

What went wrong?

I can handle the small test cases easily, but with larger numbers I get a "time limit exceeded"-error. However the time limit is 1 second and I don't think the test even took so long, so I suspect there might be something off with my data types. Any other improvements are accepted, too!

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <numeric>
#include <math.h>
using namespace std;

int arraysize;
int numberofsubarrays;
long long usernumber;
vector<long long> userArray; //Used in main()

vector<vector<long long>> subarrays; //Used in isPossible()

int vectorsum(vector<long long> userVector) {
    int ans = accumulate(userVector.begin(), userVector.end(), 0);
    return ans;
}

bool isPossible(long long n) {
    subarrays = {};
    subarrays.push_back({});
    for (auto u : userArray) {
        subarrays.back().push_back(u);
        if (vectorsum(subarrays.back()) <= n) {
            continue;
        }
        else {
            subarrays.back().pop_back();
            subarrays.push_back({ u });
        }
    }
    if ((long long)subarrays.size() > numberofsubarrays) {
        return false;
    }
    else {
        return true;
    }
}

int main()
{
    cin >> arraysize >> numberofsubarrays; //Input handling
    for (int u = 0; u < arraysize; u++) {
        cin >> usernumber;
        userArray.push_back(usernumber);
    }

    long long left = 1;
    long long middle;
    long long right = pow(10, 10);

    while (left + 1 != right) { //Binary search
        middle = ceil((left + right) / 2);
        /*cout << left << "  " << middle << "  " << right << endl;*/
        if (isPossible(middle)) {
            right = middle;
        }
        else {
            left = middle;
        }
    }
    cout << left + 1 << endl;
    return 0;
}


texts: Swapping dynamic page div's around on button click

texts:
The following working code basically swaps the content of two divs.  The divs are created dynamically, then the user checks which records they want to swap, and then clicks on a swap button that triggers a function to swap an inner div element.

The part that swaps the div's seems really messy to me. I'm only a beginner/intermediate with JavaScript, so I would really appreciate some help with this.

//dynamically create the div elements
$('#SwapBedsFields').html(options);
   for (var i = 1; i <= NumberOfBeds; i++) {
        var RowIndex = $.inArray(i.toString(), bedNumberColumn);
        var ptName = 'Empty';
        var UMRN = "";
        var UMRNstr = '';
        var pID = '';

       if (RowIndex != -1) {
           ptName = patientNameColumn[RowIndex];
           UMRN = patientUMRNColumn[RowIndex];
           pID = pIDColumn[RowIndex];
           UMRNstr = "(" + UMRN + ")";
       };
       options += '
<input type="checkbox" style="vertical-align:middle;" name="bed-' + i + '"';
       options += 'id="bed-' + i + '" />
&nbsp;Bed ' + i + '  -

' + ptName + '&nbsp;' + UMRNstr + '

';
};

//***THIS IS THE BIT I FIND PARTICULARLY MESSY!***
//then in another function - when the user clicks a button to swap the div elements...
var selected = new Array();
var patientname1;
var patientname2;
var b1html;
var b2html;
var pt1UMRN;
var pt2UMRN;

$('#SwapBedsFields input:checked').each(function () {
       selected.push($(this).attr('name'));
});

//get the html of the two bed divs
patientname1 = $('#bed-div-' + selected[0].substring(4)).html();
patientname2 = $('#bed-div-' + selected[1].substring(4)).html();
pt1UMRN = $('#bed-div-' + selected[0].substring(4)).attr('umrn');
pt2UMRN = $('#bed-div-' + selected[1].substring(4)).attr('umrn');

//swap the elements around
$('#bed-div-' + selected[0].substring(4)).html(patientname2);
$('#bed-div-' + selected[1].substring(4)).html(patientname1);

//update the umrn attribute
$('#bed-div-' + selected[0].substring(4)).attr({ umrn: pt2UMRN});
$('#bed-div-' + selected[1].substring(4)).attr({ umrn: pt1UMRN });

//message the user
$('#bed-topdiv-' + selected[0].substring(4)).effect("highlight", {}, 2000);
$('#bed-topdiv-' + selected[1].substring(4)).effect("highlight", {}, 2000);


texts: Excel VBA Sub Routine is extremely Slow deleting a Range

texts:
The following script is very slow and I'm not sure why.
The script works so if you copy and paste its a typo in the question.

Sub deleteData()
Dim wb as workbook
Dim ws as worksheet
Dim myCell as range
dim myRange as Range

with Application
 .Calculation = xlCalculationManual
 .ScreenUpdating = False
end with

Set wb = activeworkbook
set ws = wb.Sheets(1)
set myRange = ws.range("I15845:I64572")
set myCell = ws.range("I15845")

For each myCell in myRange
  myCell.Delete
next myCell

with Application
 .Calculation = xlCalculationAutomatic
 .ScreenUpdating = True
end with

MsgBox "Procedure Complete!"

end sub

I have also tried

Sub deleteData()
Dim wb as workbook
Dim ws as worksheet
Dim myCell as range
dim myRange as Range

with Application
 .Calculation = xlCalculationManual
 .ScreenUpdating = False
end with

Set wb = activeworkbook
set ws = wb.Sheets(1)
set myRange = ws.range("I15845:I64572")

myRange.Delete

with Application
 .Calculation = xlCalculationAutomatic
 .ScreenUpdating = True
end with

MsgBox "Procedure Complete!"

end sub

I know this is a large range but it should not take hours to complete.

The data in the column is an anchored countif.

=countif($A$1:A1,A1)

So each cell would be:

=countif($A$1:A1,A1)
=countif($A$1:A2,A2)
=countif($A$1:A3,A3)
.
.
.
=countif($A$1:A10000,A10000)


texts:
The case scenario:

A  2-D array is given:

1  3  6  10 15
2  5  9  14 19
4  8  13 18 22
7  12 17 21 24
11 16 20 23 25

and its size N is given as 5.

The program should output it as

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

The code I wrote for this problem:

#include <stdio.h>

void main()
{
    int N = 5;
    int pixel_array[5][5] = {
        {1, 3, 6, 10, 15}, {2, 5, 9, 14, 19}, {4, 8, 13, 18, 22}, {7, 12, 17, 21, 24}, {11, 16, 20, 23, 25}

    };

    for (int row = 0; row < N; row++)
    {
        if (row < N - 1)
        {
            for (int temp_row = row, col = 0; temp_row >= 0; temp_row--, col++)
                printf("%d ", pixel_array[temp_row][col]);
        }
        else
        {
            for (int col = 0; col < N; col++)
                for (int temp_col = col, temp_row = N - 1; temp_col < N; temp_col++, temp_row--)
                    printf("%d ", pixel_array[temp_row][temp_col]);
        }
    }
}

The above code works fine and outputs the required result.

Some specific questions:

Since there are many for loops here -

how can I decrease code length?
how can I improve the efficiency of this program?
Any other programming practices to be implemented?

Any advice would help.


texts: Average of multiple vectors with different length

texts:
I have several profiles that are in form of mappings (id,value) and I need to find average between that profiles (values). The issue is that not all profile consist of the same ids, so that the lengths are different, as here:

1 2 3 4 5 6
-----------
1 3 - 5 - 9
0 - 1 1 2 -

The first row denotes element_id and rows 2 and 3 denote the user profiles. '-' is the element with no value. The average for these 2 profiles is: {0.5, 3, 1, 3, 2, 9}.

I have implemented is as following:

private static void calcAverage(Map<Integer, Map<Integer, Integer>> profiles) {
        Map<Integer, Float> res = new HashMap<>();
        Map<Integer, Integer> elemcount = new HashMap<>();
        Map<Integer, Integer> elemsums = new HashMap<>();

        for (Map<Integer, Integer> profile : profiles.values()) {
            for (Map.Entry<Integer, Integer> element : profile.entrySet()) {
                if (!elemcount.containsKey(element.getKey())) {
                    elemcount.put(element.getKey(), 1);
                    elemsums.put(element.getKey(), element.getValue());
                } else {
                    int count = elemcount.get(element.getKey());
                    elemcount.put(element.getKey(), count + 1);

                    int sum = elemsums.get(element.getKey());
                    elemsums.put(element.getKey(), element.getValue() + sum);
                }
            }
        }

        for (Map.Entry<Integer, Integer> entry : elemcount.entrySet()) {
            System.out.println(entry.getKey() + " => " + (double)elemsums.get(entry.getKey())/(double)entry.getValue());
        }
    }

The source is working perfectly. It does what I want.

The only thing, I wanted to ask, if it is possible to optimize it somehow? Because when I have 1k profiles with 1k elements each, it will take really much time to work on.


texts: Fastest way to count combinations of values from two lists/arrays

texts:
I have a file with mostly 0 and 1. For each line I would like to compare it against all others (without the redundancy) and count the different combinations. It is slightly more complicated as there are also "N" in the matrix when there is missing data. For example if the two lines are:

line1 = "0 0 1 0 1"
line2 = "0 1 1 0 1"

Then I would like to create a two by two table like this:

    0   1
 0| 2 | 1 |
 1| 0 | 2 |

At the moment I am doing something like this:

line1_list = line1.split()
line2_list = line2.split()

To get the counts I have tried two options. The first if using a for loop:

tab_dict = defaultdict(int)
for i in xrange(0,len(line1_list)):
    key = line1_list[i]+"-"+line2_list[i]
    tab_dict[key] += 1

And the second is using a combination of map and zip:

tab_dict = Counter(map(lambda x:x[0]+"-"+x[1],zip(line1_list,line2_list)))

The first option seems to be the fastest but I was wondering if there is a faster way because the matrix I have is very big and it will take a long time to run. I have tried to look for libraries to do this but have not found anything yet.


texts:
Here I created a typed version of lodash  flow that offers more specific typing.

I'm curious if this exists in any other library, which optimized in tying any way — offering mainly it up to the world as a better version of flow.

The return value is set to the return in the last passed function
The input value is set to the input of the first given function
In between functions are checking the return types from the proceeding function match the inputs of the next.

Here's the code:

import * as _ from 'lodash';

namespace Compose {
    export type ReadFuncs = readonly ((x: any) => any)[]
    export type Tail<T extends ReadFuncs> = ((...a: T) => void) extends ((h: any, ...r: infer R) => void) ? R : never;
    export type Func = (...any) => any
    export type Funcs = Func[]
    export type LengthOfTuple<T extends any[]> = T extends { length: infer L } ? L : never;
    export type LastInTuple<T extends ReadFuncs> = T[LengthOfTuple<Tail<T>>];
    export type LastFnReturns<Fns extends ReadFuncs> = ReturnType<LastInTuple<Fns>>
    export type FirstFnParam<Fns extends ReadFuncs> = Parameters<Fns[0]>
    export type CheckFns<T extends readonly ((x: any) => any)[]> = { [K in keyof T]:
        K extends keyof Tail<T> ? (
            [T[K], Tail<T>[K]] extends [(x: infer A) => infer R, (x: infer S) => any] ? (
                [R] extends [S] ? T[K] : (x: A) => S
            ) : never
        ) : T[K]
    }
}

function compose<T extends Compose.ReadFuncs, R extends Compose.LastFnReturns<T>>(...t: Compose.CheckFns<T>) {
    return (...input: Compose.FirstFnParam<T>) => {
        return _.flow(t)(...input) as R
    }
}

const x = compose(
    (foo: string) => `wrapFoo1(${foo})`,
    (foo: string) => `wrapFoo2(${foo})`,
    (foo: string) => `wrapFoo3(${foo})`,
    (foo: string) => `wrapFoo4(${foo})`,
    (value: string): number => value.length,
)

const velvet = x('meow')

console.log(velvet)


texts:
I'm new to PHP and I would like to redirect the visitors of my website based on their operating system.
Below is my solution. Is there anything that needs to be optimized?

<?php
    // MOBILE
    $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
    $blackberry = strpos($_SERVER['HTTP_USER_AGENT'],"BB10");
    $ios = strpos($_SERVER['HTTP_USER_AGENT'],"iOS");
    // DESKTOP
    $windows = strpos($_SERVER['HTTP_USER_AGENT'],"Windows");
    $mac = strpos($_SERVER['HTTP_USER_AGENT'],"Mac");

    // REDIRECTS
    // MOBILE
    if ($android == true)
    {
    header('Location: http://www.example.com/android');
    }
    else if ($blackberry == true)
    {
    header('Location: http://www.example.com/blackberry');
    }
    else if ($ios == true)
    {
    header('Location: http://www.example.com/ios');
    }
    // DESKTOP
    else if ($windows == true)
    {
    header('Location: http://www.example.com/windows');
    }
    else if ($mac == true)
    {
    header('Location: http://www.example.com/mac');
    }
    ?>

Thanks. Patrick


texts: Encoding strings using periodic table abbreviations

texts:
I took a JavaScript challenge to finish a task related to logo of "Breaking  Bad" where letters in your first name and last name are spotted with elements of periodic table and its respective atomic number. I wrote the below code, any suggestions to improve performance or any best coding practices

  function Process() {
    var ellist = {
        "h": "1",
        "he": "2",
        "li": "3",
        "be": "4",
        "b": "5",
        "c": "6",
         .
         .
         .
        "Lv":"116",
    "Uus":"117",
    "Uuo":"118"
    };
    var fname = document.getElementById("firstname");
    var lname = document.getElementById("lastname");
    var splits = fname.split("");
    var value;

    for (var i = 0; i < splits.length; i++) {
        var onevalue = fname.indexOf(splits[i]);
        var singlev = fname.substring(onevalue, onevalue + 1);
        var doublev = fname.substring(onevalue, onevalue + 2);
        var triplev = fname.substring(onevalue, onevalue + 3);
        if (ellist[splits[i]] || ellist[doublev] || ellist[triplev]) {
            value = splits[i];

            if (ellist[doublev] || ellist[triplev]) {
                value = ellist[doublev];

                if (ellist[triplev]) {
                    value = ellist[triplev];
                         // some code here
                }
       // some code here
            }
    // some code here
        }
    }

Using the Process() function which contains the logic. The object ellist contains the list of elements of periodic table with its atomic number. First name is taken from textbox on webpage and stored in fname and similarly the last name in lname and in the for loop it contains the code which checks whether the firstname contains the string which matches the elemetns of periodic table. Any suggestions?


texts: Switch statement decreases adaptability of a function

texts:
Below you can see a class and a function. The class is just an dummy entity. The functon has a switch statement. That switch statement calculates the tax by to a predefined hardcoded string, and I don't like that. What's next, this switch statement violates the Open/Closed principle.
The class entity:
class Product {
    private int $id;
    private string $name;
    private int $price;
    private string $category;

    public function __construct(int $id, string $name, int $price, string $category) {
        $this->id = $id;
        $this->name = $name;
        $this->price = $price;
        $this->category = $category;
    }
    public function getId(): int
    {
        return $this->id;
    }
    public function getName(): string
    {
        return $this->name;
    }
    public function getPrice(): int
    {
        return $this->price;
    }
    public function getCategory(): string
    {
        return $this->category;
    }
}

The client code:
//$product = new Product(200, 'Bread', 200, 'Food'); // A different product category
$product = new Product(100, 'Chair', 5000, 'Furniture');

$taxForChair = calculateTax($product);
var_dump($taxForChair); // float 1050
exit;

function calculateTax(Product $product): float {
    // The switch statement below decreases adaptablity.
    // The function also violates the Open/Closed principle.
    // At this point, it has 2 cases.
    // When the Business Rule change by create a new product category, then this function needs to change too.
    switch($product->getCategory()) {
        case 'Furniture':
            $tax = $product->getPrice() / 100 * 21;
            break;
        case 'Food':
            $tax = $product->getPrice() / 100 * 9;
            break;
        default:
            $tax = 0;
            break;
    }

    return (float)$tax;
}

Would you be so kind and write a better calculateTax function?


texts:
I would like some eyes on this:

/* Little XHR
 * by: rlemon        http://github.com/rlemon/
 * see README for useage.
 * */
var xhr = {
    xmlhttp: (function() {
        var xmlhttp;
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
            } catch (er) {
                try {
                    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
                } catch (err) {
                    xmlhttp = false;
                }
            }
        }
        return xmlhttp;
    }()),
     /* https://github.com/Titani/SO-ChatBot/blob/ccf6cfe827aee2af7b2832e48720a8e24a8feeed/source/bot.js#L110 */
    urlstringify: (function() {
        var simplies = {
            'number': true,
            'string': true,
            'boolean': true
        };
        var singularStringify = function(thing) {
            if (typeof thing in simplies) {
                return encodeURIComponent(thing.toString());
            }
            return '';
        };
        var arrayStringify = function(array, keyName) {
            keyName = singularStringify(keyName);
            return array.map(function(thing) {
                return keyName + '=' + singularStringify(thing);
            });
        };
        return function(obj) {
            return Object.keys(obj).map(function(key) {
                var val = obj[key];
                if (Array.isArray(val)) {
                    return arrayStringify(val, key);
                } else {
                    return singularStringify(key) + '=' + singularStringify(val);
                }
            }).join('&');
        };
    }()),
    post: function(options) {
        this.request.apply(this, ['POST', options]);
    },
    get: function(options) {
        this.request.apply(this, ['GET', options]);
    },
    request: function(type, options) {
        if (this.xmlhttp && options && 'url' in options) {
            var xhr = this.xmlhttp,
                enctype = ('enctype' in options) ? options.enctype : 'application/x-www-form-urlencoded';
            xhr.open(type, options.url, true);
            xhr.setRequestHeader('Content-Type', enctype);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        if ('success' in options && options.success.apply) {
                            options.success.apply(this, [xhr]);
                        }
                    } else if (xhr.status && xhr.status != 200) {
                        if ('failure' in options && options.failure.apply) {
                            options.failure.apply(this, [xhr]);
                        }
                    }
                }
            };
            var data = null;
            if ('data' in options) {
                data = this.urlstringify.apply(this, [options.data]);
            }
            xhr.send(data);
        }
    }
};

For a barebones object; have I missed anything important? Should I change how any of this is organized or layed out? I have also tested in a limited number of browsers and so far so good! However I am unsure about older browsers and compatibility.

Any input would be great! thanks!


texts: Profiling script for gathering a lot of data from an API

texts:
I wrote a script which is supposed to be making requests to an API to gather a lot of data. There is a limit on requests that I can make: about 30 000 in 8 hours otherwise, I will get banned for a significant time. Each object I get from the API can be identified uniquely with a hash. Each API call returns me some data I need and also the hash of the next object I need to get. So I start with some hash I have, make a request, parse the result and obtain the hash of the next object. Repeat. I also log the time of each 20th request I make, recording the time to keep track of the number of the requests I made in last 8 hours.

Here is my code:

import ujson
import requests
import time
import os
import cProfile

# 'logs' has all the requests in the last 8 hours

# the snippet of code which does all of the hard work
for i in range(len(logs) * requests_per_log, maximum_requests):  # to stay in max requests range

    r = requests.get(url + current_hash)
    block = ujson.loads(r.text)  # use ujson because it is faster
    block_timestamp_str = format_timestamp(block['time'])
    block_index_str = str(block['block_index'])

    # only log each 20th request
    if i % 20 == 0:
        f_logs.write(str(time.time()) + '\n')  # log the time when the request was made
    f = open('data/' + block_index_str + "__" + block_timestamp_str, "w+")
    block_string = parse_transactions_in_block(block)
    current_hash = block['prev_block']
    f.write(block_string)
    f.close()
    # record the hash the script stopped at
    f_current_hash.write(current_hash)

# some of the functions it uses:

def parse_transactions_in_block(block):
    block_string = ''
    for transaction in block['tx']:
        block_string += str(transaction['time']) + ',' + str(transaction['tx_index']) \
                        + ',' + str(calc_total_input(transaction)) + '\n'
    return block_string

def calc_total_input(transaction):
    total_input = 0
    for input in transaction['out']:
        total_input += input['value']

# this is the time format I was asked to keep my data in

def format_timestamp(unix_time):
    return time.strftime('%Y-%m-%d-%H-%M-%S', time.gmtime(unix_time))

There is a lot of data to go through, so I want it to be as fast as possible and my previous iterations took a while to run. I am running it on Google Compute Engine using a Linux distribution. Any ideas how I can make this work faster? I don't have enough experience on concurrent computing in Python, so I am just looking for a way of optimizing what I have, without concurrency.


texts: Dealing with DB Through EF within Winforms

texts:
I have the following code which uses EntityFramework to reach out to a DB and retrieve data to populate into a Winform application gridview.

I am still quite new to C# and especially abstracting code out of code behind/writing loosely coupled code.

Could the following code be reviewed and feedback given on the methods I am using to construct the application in a methodical way?

MainScreenForm

public partial class MainScreenForm : MainScreenController
{
    private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public MainScreenForm()
    {
        InitializeComponent();
    }

    private void MainScreenForm_Load(object sender, EventArgs e)
    {
        log.Debug("Main Screen Launched");
        PopulateGrid();
        AdjustGridviewVisuals();
    }

    private void AdjustGridviewVisuals()
    {
        metroGrid1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        metroGrid1.AllowUserToAddRows = false;
        metroGrid1.RowHeadersVisible = false;

        this.metroGrid1.Columns["Id"].Visible = false;
        this.metroGrid1.Columns["ExamId"].Visible = false;
        this.metroGrid1.Columns["Comments"].Visible = false;
        this.metroGrid1.Columns["CreationDate"].Visible = false;
        this.metroGrid1.Columns["LastModified"].Visible = false;
        this.metroGrid1.Columns["PatientState"].Visible = false;
    }

    private void PopulateGrid()
    {
        metroGrid1.DataSource = GetListOfPatients();
    }
}

MainScreen Controller

public class MainScreenController : MetroForm
{
    private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    private TrollyPatientDbContext db = new TrollyPatientDbContext();

    public List<Patient> GetListOfPatients()
    {
        log.Debug("Attempting to retreve a list of patients from SQL");
        var patients = db.Patients;
        return patients.ToList();
    }
}

DbContext

public class TrollyPatientDbContext : DbContext
{
    public TrollyPatientDbContext() : base("PrimaryConnection")
    {
    }
    public DbSet<Exam> Exams { get; set; }
    public DbSet<Patient> Patients { get; set; }
}

The models are just standard "POCO" (I think that's what it is called).

Is the way I have extended the controller to allow access to methods within it a good way to do this work or should I being using static methods instead?

Is this considered a way of constructing to an MVP practice?


texts: Returning the results of four $resource calls as a JSON array in AngularJS

texts:
Title pretty much says it. I need a way to elegantly handle multiple $resource calls in AngularJS. Right now, I just calling each other call on the success function and calling a callback function to spit the data out when it's down.

Here's what I have so far.

// DataService.js
var resource = $resource('/app/data/event/:id/:ext', {id:'@id',ext:'@ext'}, {"getAll": {method: "GET", isArray: false}});
getAllEvents: function (successcb) {
    var combinedJSON = "[";
    // First Call
    var firstFile = resource.get({ id: 1, ext: '.json' });
    firstFile.$promise.then(function (data) {
        combinedJSON += JSON.stringify(data) + ",";
        // Second Call
        var secondFile = resource.get({ id: 2, ext: '.json' });
        secondFile.$promise.then(function (data) {
            combinedJSON += JSON.stringify(data) + ",";
            // Third Call
            var thirdFile = resource.get({ id: 3, ext: '.json' });
            thirdFile.$promise.then(function (data) {
                combinedJSON += JSON.stringify(data) + ",";
                // Fourth Call
                var fourthFile = resource.get({ id: 4, ext: '.json' });
                fourthFile.$promise.then(function (data) {
                    combinedJSON += JSON.stringify(data) + "]";
                    successcb(combinedJSON); // This is the success call back
                }, function (data) {
                    console.log(data);
                });
            }, function (data) {
                console.log(data);
            });
        }, function (data) {
            console.log(data);
        });
    }, function (data) {
        console.log(data);
    });
}

My callback function just spits out the data

// Controller.js
eventData.getAllEvents(function successcb(events) {
    $scope.foo = events;
});

Is there a better way to handle these types of situations?


texts: Concept for PHP Controlled, Privileged Execution (for a cPanel knockoff on Ubuntu Server)

texts:
I was thinking of building a F/OSS project as a cPanel for Ubuntu Server, since Ubuntu doesn't have this, and yet making it easy for a hosting company to use this, where users will find it just as intuitive as cPanel.

Trouble is, PHP web pages on Ubuntu run as www-data, and lots of senior PHP guys recommend not running a PHP page as root, and not shelling out and running a Bash script that may have a sticky bit applied on its permissions. They say that's a way to get hacked.

It then dawned on me how to get around this. Make a Bash script daemon in /etc/init.d that watches a named pipe connection, sitting in a loop. The loop will hang, but that's perfectly okay because it will be in the background, and in a sense it means that the script is dormant and not soaking up CPU cycles. That's cool! It's certainly better than a sleep loop, and means the process only wakes up when there's something that needs to be processed. The sleep loop technique, on the other hand, may be checking for PHP communication when there may not be any, and so the sleep loop soaks up unnecessary CPU. That's why I like the pipe-wait-loop strategy.

So, then all the PHP page has to do is do normal file I/O to like /tmp/pipe (my pipe, for instance) and the Bash script daemon in the background will wake up out of dormancy in that loop, and process a command as root. It can look in a database for new things it needs to do, and then do those tasks. When done, it can return values to the database and the PHP page can wait in a jQuery/AJAX loop until it receives a response back.

Because the PHP page doesn't actually compose the root commands it needs to run, and merely writes some records to a database table, and sends a string of "run" to the pipe, it safely passes on a request to the secure daemon in the background.

What do you think of this IPC technique? Good? Bad?

BTW, my demo /usr/sbin/foo command looks like this. Its point is to demonstrate that /tmp/foo-was-here was created by the root user account, triggered by a PHP page. It's just the start of something bigger.

#!/bin/bash

if [ ! -f /tmp/foo ]; then
    mkfifo /tmp/foo
fi

while true; do
    cat /tmp/foo;
    touch '/tmp/foo-was-here';
done


texts: back_inserter for several container arguments

texts:
Recently I wrote a back_inserter with the same interface as the std::back_inserter just for supporting several containers in the constructor and doing push_back for each. The solution needs C++17. Please give me some feedback! Thanks.

#include <iterator>
#include <tuple>

namespace stx {

template <class Container, class... Containers>
class back_insert_iterator
{
public:
    using value_type        = void;
    using difference_type   = void;
    using pointer           = void;
    using reference         = void;
    using iterator_category = std::output_iterator_tag;

    explicit back_insert_iterator(Container& container, Containers&... containers) : containers(container, containers...) {
    }

    back_insert_iterator& operator=(const typename Container::value_type& value) {
        std::apply([&](auto&... container) { ((container.push_back(value)), ...); }, containers);
        return *this;
    }

    back_insert_iterator& operator=(typename Container::value_type&& value) {
        std::apply([&](auto&... container) { ((container.push_back(std::move(value))), ...); }, containers);
        return *this;
    }

    back_insert_iterator& operator*() {
        return *this;
    }

    back_insert_iterator& operator++() {
        return *this;
    }

    back_insert_iterator& operator++(int) {
        return *this;
    }

private:
    std::tuple<Container&, Containers&...> containers;
};

template <class Container, class... Containers>
auto back_inserter(Container& container, Containers&... containers) {
    return back_insert_iterator<Container, Containers...>(container, containers...);
}

}

And you can use it like this:

#include <iostream>
#include <vector>

auto main() -> int {

    const auto numbers = std::vector<int>{ 1, 2, 3, 4, 5 };

    std::vector<int> copy1, copy2;

    std::copy(numbers.begin(), numbers.end(), stx::back_inserter(copy1, copy2));

    std::cout << "(numbers == copy1) = " << std::boolalpha << (numbers == copy1) << '\n';
    std::cout << "(numbers == copy2) = " << std::boolalpha << (numbers == copy2) << '\n';

};

A front_inserter would of course look similar, just calling push_front instead. Some specific additional questions:

does the operator= method with the r-value work as intended or do I need to pass the value differently to the lambda?
do I need an extra static_assert to check that each Container supports the same value_type or at least some that are convertible to avoid overloading the user with template errors in case they are different?
is there another solution where I could let the user provide and optional argument for execution policy, for example std::execution::par
the original std::back_inserter saves the container as pointer - is there any advantage versus my inserter which saves them per reference?


texts:
I have this Queue declared in my class:

static private Queue<SignerDocument> QUEUE = new Queue<SignerDocument>();

I fill this Queue with some items to process, and I want to process it using multithreading.

Did I need to use lock in this case, to ensure that QUEUE.Dequeue is thread-safe?

static protected void ThreadProcManager()
{
   lock (typeof(Queue<SignerDocument>))
   {
       if (QUEUE.Count > 0)
       {
           ThreadPool.QueueUserWorkItem(ThreadProc, QUEUE.Dequeue());
        }
    }
}

 static private void ThreadProc(Object stateInfo)
 {
     //Do the work...
 }


texts:
I've made the following implementation of counting sort. I don't think that I understand the algorithm fully but this implementation seems to be working correctly.

I've used the Counterdictionary from the collections module. It seems to be the best way to handle the counting part. But I haven't seen anyone using it to implement counting sort.

Is there any way to improve the code?

from random import randrange
from collections import Counter
from copy import deepcopy

def counting_sort(aList, length):
    """
    counting sort does not work for negative numbers
    counting sort assumes that each element is SMALL INTEGER

    running time is O(maxVal - minVal) ---> Linear

    (Useful only when the difference between maxVal and minVal is not large)
    """
    list_copy = deepcopy(aList)  # a copy so that I can return it and check it against the original list applied to sorted function
    c = Counter()

    for num in list_copy:
        c[num] += 1

    index = 0

    # for i in range(len(c)):
        # while 0 < c[i]:
            # list_copy[index] = i
            # c[i] -= 1
            # index += 1
    for key in c:
        while c[key] > 0:
            list_copy[index] = key
            c[key] -= 1
            index += 1

    return list_copy

aList = [randrange(100) for _ in range(1000)]
after = counting_sort(aList, len(aList))
print(after == sorted(aList))


texts: Only using randint, create a random list of unique numbers

texts:
There are many sorting algorithms, so how about unsorting algorithms?

But how could one go about creating a random list of unique elements while not using shuffle, NumPy or any other ready made tools to do it?

def get_un_sorted(limit):
    numbers = list(range(limit))
    shuffled = [0] * limit
    for i in range(limit):
        index = randint(0, len(numbers) - 1)
        shuffled[i] = numbers[index]
        numbers.pop(index)
    return shuffled

def main():
    not_sorted = get_un_sorted(2 * 10 ** 4)

if __name__ == '__main__':
    main()

It doesn't seem so nifty to me and it is very slow, but it gets the work done.


texts: Get mode of elements in a vector with rust - For loop borrowing and referencing

texts:
Before anyone marks this as duplicate, my question is only towards the median, and my approach is different from what the answer suggested. On getting the mode from a vector of values, I have the below:
fn get_mode(list_of_numbers: &[i32]) -> i32 {
    // most common occurrence of item from vector
    use std::collections::HashMap;

    //* using structs.More readable
    struct Mode {
        number: i32,
        frequency: u32,
    };

    let mut occurence = HashMap::new();

    let mut most_common: Mode = Mode {
        number: 0,
        frequency: 0,
    };

    for number in list_of_numbers {
        let count = occurence.entry(number).or_insert(0);
        *count += 1;
    }

    for (&key, val) in occurence {
        if val > most_common.frequency {
            most_common = Mode {
                number: key,
                frequency: val,
            };
        }
    }

    most_common.number
}

For readability, the function uses a struct. My question comes down to :
    for (&key, val) in occurence {

Why can't I borrow the value val from the HashMap given I might as well be passing the values and keys to another function in this loop? The error when I try:
for (&key, &val) in occurence {

would be : expected integer, found reference. How come?


texts: Inky pinky ponky game / Josephus problem

texts:
I have written below code for the famous Josephus problem, or inky pinky ponky game, or many other names. Does this looks good or is there any loophole if anyone can highlight. I shall be looking forward to optimize it now as much as possible.

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

typedef struct ll_core
{
    int c_ll;
    struct ll_core * next;
} node_d;

node_d * create_c_list(int ,int);
void printList(node_d *);
node_d * elimination(node_d *, int);

int main(void)
{
    node_d *c_list;
    int n_kids,num;

    printf("How many kids? ");
    scanf("%d",&n_kids);
    printf("How many no. of words? ");
    scanf("%d",&num);

    node_d *first_kid=create_c_list(n_kids,num);    // create the circular list and get the pointer to first node/tail.
    printList(first_kid);

    node_d *victor=elimination(first_kid,num);      //returns pointer to the winner/last kid left
    printList(victor);
}

node_d * create_c_list(int n_k,int n_w)
{
    node_d *first=malloc(sizeof(node_d));
    int dat=1;
    first=malloc(sizeof(node_d));
    first->c_ll=dat;
    first->next=first;

    int iter=0;
    while(iter< n_k-1)
    {
        node_d * new_p=malloc(sizeof(node_d));
        new_p->next=first->next;
        first->next=new_p;                   /*We are not changing the node pointed to by tail pointer anywhere*/
        new_p->c_ll=++dat;
        iter++;
    }

    return first;
}

node_d * elimination(node_d * fix, int cntr)
{
    node_d *it=fix, *prev=fix;
    int iter;

    while(1)
    {
         for(iter=1;iter<cntr;iter++)
    it=it->next;                            /*This pointer is always on the node to be deleted. Once node is deleted, it points to the node from where
                                            from where counting is to start next.*/

    while(prev->next != it)
    prev=prev->next;                        // This always points to the node just before the one pointed to by pointer 'it'.

    //if(prev->next == it)
    prev->next=it->next;
    printf("removed : %d\n",it->c_ll);
    node_d *temp=it;
    free(temp);
    it=prev->next;

    if(it==it->next)
    break;
    }

    return it;
}

void printList(node_d *list_tail)
{
    node_d *p = list_tail->next;

    if(p->next==p)
    {
        printf("Only one element at %p: Value : %d  next : %p\n",p,p->c_ll,p->next);
     return;
    }

    while(p != list_tail)
    {
        printf("Value : %d   next : %p\n",p->c_ll,p->next);
        p=p->next;
    }

    if(p == list_tail)
    {
        printf("tail.. value : %d next : %p\n",p->c_ll,p->next);
    }
}


texts:
I am working towards a script that will show a set small thumbnail images on one side of the page, that when clicked will show a larger version of the same image, plus related text content on the other side of the page. I am not very good with JavaScript yet and wonder if the

Show1

Show2

Show3

This is default content.

This is content 1.

This is content 2.

This is content 3.

Also, my overall goal is to use this with a page that shows, say 20-30 clickable thumbnails/small images, that when clicked will reveal a couple of text paragraphs and multiple (maybe 25) larger images that relate to the clicked thumbnail.  I believe I understand that all of that will be loaded/hidden when the page first loads.  Does that sound like too much?  Is it likely to be a slow page?

(I posted this exact same thing at phpbuilders...but it looks like a lot more JavaScript stuff goes on here.)


texts:
I was asked to implement this in an interview a while back. I didn't pass the interview, mostly likely because it took me far too long to do. However, I'm interested to know how well implemented the actual code is.

The task was to:

  Add one to a positive number represented as an array of integers.

The question is the same as this one. However, I have done it in JavaScript and my implementation is different (recursion rather than loops), so I hope they are sufficiently different.

var addToArray = function(n){
    var result = [];

    function carryOne(n) {
        if (n.length === 0) {
            result.push(1);
        }
        else {
            var length = n.length-1;
            if (n[length] < 9) {
                n[length] += 1;
                result.push(n);
            } else {
                result.push(0);
                carryOne(n.slice(0, length));
            }
        }
    }
    carryOne(n);
    result.reverse();
    var flatten = Array.prototype.concat.apply([], result);
    console.log(flatten);
}

I've just noticed I have missed the semicolons on the closing braces on the function definitions but I'm keeping it like that for honesty.

In general it works but I think the need to reverse and flatten the array at the end is quite inelegant, so I suspect there is a better solution.


texts:
What do you think of this code?

#ifndef COMPOSE_HPP
# define COMPOSE_HPP
# pragma once

#include <type_traits>

#include <utility>

namespace
{

template <typename R, typename ...A>
struct signature
{
};

template <typename R, typename ...A>
constexpr auto extract_signature(R (*const)(A...)) noexcept
{
  return signature<R, A...>();
}

template <typename C, typename R, typename ...A>
constexpr auto extract_signature(R (C::* const)(A...)) noexcept
{
  return signature<R, A...>();
}

template <typename C, typename R, typename ...A>
constexpr auto extract_signature(R (C::* const)(A...) const) noexcept
{
  return signature<R, A...>();
}

template <typename F>
constexpr auto extract_signature(F const&) noexcept ->
  decltype(&F::operator(), extract_signature(&F::operator()))
{
  return extract_signature(&F::operator());
}

template <typename F1, typename F2, bool A2Size, typename ...A1>
class composer;

template <typename F1, typename F2, typename ...A1>
class composer<F1, F2, true, A1...>
{
  F1 const f1_;
  F2 const f2_;

public:
  explicit composer(F1&& f1, F2&& f2) noexcept :
    f1_(::std::forward<F1>(f1)),
    f2_(::std::forward<F2>(f2))
  {
  }

  auto operator()(A1&& ...args) const noexcept(
    noexcept(f2_(f1_(::std::forward<A1>(args)...)))
  )
  {
    return f2_(f1_(::std::forward<A1>(args)...));
  }
};

template <typename F1, typename F2, typename ...A1>
class composer<F1, F2, false, A1...>
{
  F1 const f1_;
  F2 const f2_;

public:
  explicit composer(F1&& f1, F2&& f2) noexcept :
    f1_(::std::forward<F1>(f1)),
    f2_(::std::forward<F2>(f2))
  {
  }

  auto operator()(A1&& ...args) const noexcept(
    noexcept(f2_()) || noexcept(f1_(::std::forward<A1>(args)...))
  )
  {
    f1_(::std::forward<A1>(args)...);

    return f2_();
  }
};

template <
  typename F1, typename R1, typename ...A1,
  typename F2, typename R2, typename ...A2
>
inline auto compose(
  F1&& f1, signature<R1, A1...> const,
  F2&& f2, signature<R2, A2...> const) noexcept(
  noexcept(
    composer<F1, F2, sizeof...(A2), A1...>(::std::forward<F1>(f1),
      ::std::forward<F2>(f2)
    )
  )
)
{
  return composer<F1, F2, sizeof...(A2), A1...>(::std::forward<F1>(f1),
    ::std::forward<F2>(f2)
  );
}

}

template <typename F1, typename F2>
inline auto operator|(F1&& f1, F2&& f2) noexcept(
  noexcept(
    compose(
      ::std::forward<F1>(f1), extract_signature(f1),
      ::std::forward<F2>(f2), extract_signature(f2)
    )
  )
)
{
  return compose(
    ::std::forward<F1>(f1), extract_signature(f1),
    ::std::forward<F2>(f2), extract_signature(f2)
  );
}

#endif // COMPOSE_HPP

Example:

#include <iostream>

#include "compose.hpp"

int main(int, char*[])
{
  auto a([](int v) noexcept
    {
      ::std::cout << v << ::std::endl;

      return v + 1;
    }
  );

  (a | a | a | a | a | a)(0);

  return 0;
}


texts:
I'm working on a small project with dBase files from a 3rd party. Realizing that most of the code to fetch tables and form objects is the same, I made this function:

public static IEnumerable<T> OleDbFetch<T>(Func<OleDbDataReader, T> formator, string connectionString, string query)
{
    var conn = new OleDbConnection(connectionString);
    conn.Open();
    var cmd = new OleDbCommand(query, conn);
    var reader = cmd.ExecuteReader();
    while (reader.Read())
        yield return formator(reader);
    conn.Close();
}

That way I can use it with all sorts of classes (pre-made, imposed) that have mostly parameter-less constructors:

class Person
{
    public int ID { get; set; }
    public string Name { get; set; }

    public Person() { }
}

//...

var persons = OleDbFetch<Person>(
    r => new Person()
    {
        ID = Convert.ToInt32(r["ID"]),
        Name = r["Name"].ToString()
    },
    connString, "SELECT ID, Name FROM Person");

Am I missing something obvious, being reckless, or giving myself too much trouble?


texts:
This is a kind of follow up to my previous sort implementation review with specific questions to quick sort, and Modern C++ idioms.

template<typename BidirIt,
         typename Comparator = std::less<typename std::iterator_traits<BidirIt>::value_type>>
void partition(BidirIt first,
               BidirIt last,
               BidirIt & pivot,
               Comparator cmp = Comparator())
{
    // Last usually points to end, rather than the last element.
    last--;
    // Until both first and last point to the same element (always going to be the pivot).
    while (first != last)
    {
        auto pivot_val = *pivot;
        while (cmp(*first, pivot_val)) ++first;
        while (cmp(pivot_val, *last)) --last;
        if (first == pivot)
        {
            std::iter_swap(first, last);
            pivot = last;
        }
        else
        {
            if (last == pivot)
            {
                std::iter_swap(first, last);
                pivot = first;
            }
        }
    }
}

std::vector<int> vec{ 1, 4, 5, 3, 2 };

template<typename BidirIt,
         typename Comparator = std::less<typename std::iterator_traits<BidirIt>::value_type>>
void sort_quick(BidirIt first,
                BidirIt last,
                Comparator cmp = Comparator())
{
    typedef std::iterator_traits<BidirIt>::difference_type dt;
    dt size = std::distance(first, last);

    // Arrays of 0 or 1 elements are considered sorted.
    if (size > dt(1))
    {
        auto pivot = first + size / dt(2);
        ::partition(first, last, pivot);
        // Recursive call on each partititon.
        sort_quick(first, pivot);
        sort_quick(std::next(pivot), last);
    }
}

I am looking for any performance optimizations I may have missed in the algorithm itself, as well as Modern C++ constructs I may not be abiding by for this example.

In particular having to pass pivot by reference to partition and having to update the pivot reference if it gets swapped is pretty ugly, If anyone can think of a way around this I would love to know.

Also just realized this doesn't work with duplicates :|


texts:
I have written a program to compute mean and median. Please review my code and give suggestions.

I have a confusion about this temps[temps.size()/2] when temps.size() is an odd number. How does the "vector::operator[]" handle floating point numbers? From the behavior in my code, I guessed it rounds to the nearest integer.

Note: I am self learning programming by reading "Programming: Principles, and Practice" by Bjarne Stroustrup. Currently, I am on chapter 4.

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<double> temps;          // temperatures
    for (double temp; std::cin>>temp; ) // read into temp
        temps.push_back(temp);      // put temp into vector

    // Compute Mean temperature:
    double sum = 0;
    for (int x : temps) sum += x;
    std::cout << "Average temperature: " << sum/temps.size() << '\n';

    // sort temperatures
    std::sort(temps.begin(),temps.end());

    // Compute Median temperature
    if(temps.size()%2==1) //Number of elements are odd
    {
        std::cout << "Median temperature: " << temps[temps.size()/2] << '\n';
    }
    else // Number of elements are even
    {
        int index = temps.size()/2;
        std::cout<< "Median temperature: " << (temps[index-1] + temps[index])/2;
    }

}


texts:
I'm not that good when it comes to templates, especially when it comes to variadic templates. I want to show you some code where I use both - templates and variadic templates - to call a lua function. I'm proud that it works, and it works great, but I want to get some feedback on what I could've done better.

template<typename ... Args>
bool run_script(int id, Args && ... args) {
    const int params = sizeof...(args);
    _load(id);
    push(std::forward<Args>(args)...);
    return _run(params);
}

This is the "entry point". _load will just load the lua-function onto the stack and _run will run it with params arguments. The important part is the push function.

template<typename T>
void push(T && arg) {
    _push(std::forward<T>(arg));
}

template<typename T, typename ... Args>
void push(T && arg, Args && ... other) {
    _push(std::forward<T>(arg));
    push(std::forward<Args>(other)...);
}

void _push(int arg) {
    lua_pushinteger(L, arg);
}

void _push(const std::string& arg) {
    lua_pushstring(L, arg.c_str());
}

void _push(const char* arg) {
    lua_pushstring(L, arg);
}

I had a problem before, where I had all of them named push, but for some reason it couldn't push a std::string (std::string as argument choose the template instead of const std::string&)  but now with the templates called push and the functions which do the work _push it's working great. It's not much code, I know, but it's my the first time working with variadic templates and it took me a fair amount of time to get it working.


texts: Am I correctly testing that methods from Kernel have been called?

texts:
Any time I'm testing a Ruby class that calls a Kernel method such as sleep or exit, i define a singleton method on the class instance and test that it has been invoked:

it "must throttle requests" do
  sleep_was_called = false

  subject.define_singleton_method(:sleep) { |seconds|
    sleep_was_called = true
  }

  subject.request query
  subject.request query

  sleep_was_called.must_equal true
end

I have seen far more sophisticated method stub testing in RSpec, but it there a more fancy and less verbose way to do this using MiniTest?


texts: Django: View method that outputs large context data into an html dashboard template

texts:
I'm a beginner in Django3 and just wrote an overview function to pass on some data to the template in a context variable.
I am definitely passing a lot more than I should as I only need only a few fields from each of these variables. Also, it might not be the most Django-esque way of doing things as it isn't using GenericViews or any of the more advanced Django features.
please review, comment, and provide suggestions or modifications:
def overview_page(request):
    user = request.user
    org = Organization.objects.filter(user=user)
    projects = Project.objects.filter(organization=org[0])
    # get first is_active project, as this is only allowed once per
    #    Organization. The model will need to be fixed post MVP for better
    #    design
    active_project = Project.objects.filter(organization=org[0],
                                            is_active=True)[0]
    assets = Asset.objects.filter(project=active_project)
    functional_classes = FuncClass.objects.filter(project=active_project)
    conditions = Condition.objects.filter(project=active_project)
    det_curves = DetCurve.objects.filter(project=active_project)
    func_classes = FuncClass.objects.filter(project=active_project)
    model_settings = ModelSettings.objects.filter(project=active_project)
    scenarios = Scenario.objects.filter(project=active_project)
    surface_types = SurfaceType.objects.filter(project=active_project)
    treatments = Treatment.objects.filter(project=active_project)

    context = {'user': user,
               'my_org': org[0],
               'func_class': functional_classes,
               'projects': projects,
               'active_project': active_project,
               'assets': assets,
               'conditions': conditions,
               'det_curves': det_curves,
               'func_classes': func_classes,
               'model_settings': model_settings,
               'scenarios': scenarios,
               'surface_types': surface_types,
               'treatments': treatments,
               }

    return render(request, 'asset_manager/overview.html', context)


texts: Basic ul li elements in JavaScript listeners

texts:
I am learning JavaScript and I have written a code snippet. Can someone suggest some improvements?

Array.prototype.pushIfDoesntExist = function (item){
    if(this.indexOf(item) === -1) {
        this.push(item);
        return true;
    }
    return false;
}

var Testing = {
  clickedList: [],
  applyListeners: function() {
       var self = this;
          var nodes = document.getElementsByTagName('li');
        for (var i = 0; i < nodes.length; i++) {
           document.getElementsByTagName('li')[i].addEventListener('click', function() {
             //we only want to display an item once
             self.clickedList.pushIfDoesntExist(this.textContent)
              self.displayData();
           });
      }
    },
    displayData: function() {
      var textBox = document.getElementById('clickedElements');
        textBox.textContent = this.clickedList.join(', ');
    }
}

Testing.applyListeners();

Milk

Eggs

Bacon

Cheese


texts: Project Euler Problem 1 - Functional approach?

texts:
I'm trying to learn F# right now coming from C# and I'm finding it a great difficulty to "reconfigure" my mind to the functional programming mindset. So I'm going to attempt a few Project Euler problems to learn how to code functionally.

Here's my attempt at problem 1, now I'm wondering prorimarily, is this an idiomatic approach?

// Project Euler - Problem 1
// If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
// Find the sum of all the multiples of 3 or 5 below 1000.
#light

open System

let multiples = [for n in 1..1000 do if n % 3 = 0 || n % 5 = 0 then yield n]
let sum = List.sum multiples
printfn "The sum of all multiples are: %d" sum
ignore (Console.ReadKey ())


texts:
I use C# predominantly and just recently moved to C++/Linux, so I'm a bit of a newbie to Makefiles. After a few days of reading the GNU make docs and scouring StackOverflow questions, I was able to come up with my very first functional Makefile template. Here it is:

CXX := g++
CXXFLAGS := -Wall -std=c++11
LDFLAGS :=

DEBUG := false

# make DEBUG=true emits debug info
ifeq ($(DEBUG),true)
    CXXFLAGS := $(CXXFLAGS) -g
endif

# used for filename of output program
NAME := mk_practice

MKF_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MKF_DIR := $(patsubst %/,%,$(dir $(MKF_PATH))) # dir leaves a trailing /
MKF_DIR := $(strip $(MKF_DIR))

# absolute paths to dirs
BIN := $(MKF_DIR)/bin
OBJ := $(MKF_DIR)/obj
SRC := $(MKF_DIR)/src

HEADERS := non_trivial_class.h
H_PATHS := $(HEADERS:%=$(SRC)/%)

SOURCES := main.cpp non_trivial_class.cpp
S_PATHS := $(SOURCES:%=$(SRC)/%)

OBJECTS := $(SOURCES:.cpp=.o)
O_PATHS := $(OBJECTS:%=$(OBJ)/%)

# path to output lib/exe
OUTPUT := $(BIN)/$(NAME)

# General rules:
#
# - Whenever an .o file changes, the resulting executable/library should be updated.
# - Whenever a .cpp file changes, its corresponding .o file should be updated.
# - Whenever an .h file changes, all .o files should be updated.

.PHONY: all print-%

all: $(OUTPUT)

# useful for debugging
# usage: print-VARNAME
print-%:
    @echo $*=$($*) # $* matches the stem of a pattern rule

$(BIN):
    @mkdir -p $@

$(O_PATHS): $(OBJ)/%.o: $(SRC)/%.cpp $(H_PATHS) | $(OBJ)
    $(CXX) $(CXXFLAGS) -c -o $@ $<

$(OBJ):
    @mkdir -p $@

$(OUTPUT): $(O_PATHS) | $(BIN)
    $(CXX) $(LDFLAGS) -o $@ $(O_PATHS)

Since I wrote most of this from scratch, I would appreciate any critique you could give. For example, is there anything I could improve on, or any logic that I fuzzed up? Is there anything you would make more modular/group together?

For context, here is the GitHub repo that contains the Makefile as well as all of the sources it's supposed to build.


texts:
I have code that sorts two lists related with each other

"""
    list1 reperesents customer
    list2 reperesents amount of products they have brought
"""

list1=['a','c','e','i','o','u']
list2=[12,23,45,10,7,2]
list3=[]
for i in range(0,len(list1)):
        list3.append([list1[i],list2[i]])
print list3
"""
    [['a', 12], ['c', 23], ['e', 45], ['i', 10], ['o', 7], ['u', 2]]
"""
list3=sorted(list3,key=lambda x: list3[list3.index(x)][1])

print(list3)
"""
    [['u', 2], ['o', 7], ['i', 10], ['a', 12], ['c', 23], ['e', 45]]
"""
for i in range(0.len(list3)):
    list1[i]=list3[i][0]
    list2[i]=list3[i][1]

print list1
print list2

List1 is the customers' names
List2 is the number of products each customer bought

So:

'a' bought 12 products
'c' bought 23 products

And so on.

Is there a better way to do this?


texts:
As the question says....Can this be done any better? I could introduce more variables to cut down on function number, but does it make sense? So this is a simple cross-fade between two images :

var slide_number = 1;
var slide_number2 = 2;
var slide1 = new Image()
slide1.src = "img/top_slideshow/slideshow-1.jpg"
var slide2 = new Image()
slide2.src = "img/top_slideshow/slideshow-2.jpg"
var slide3 = new Image()
slide3.src = "img/top_slideshow/slideshow-3.jpg"
var slide4 = new Image()
slide4.src = "img/top_slideshow/slideshow-4.jpg"
var slide5 = new Image()
slide5.src = "img/top_slideshow/slideshow-5.jpg"
var slide6 = new Image()
slide6.src = "img/top_slideshow/slideshow-6.jpg"
var slide7 = new Image()
slide7.src = "img/top_slideshow/slideshow-7.jpg"
var slide8 = new Image()
slide8.src = "img/top_slideshow/slideshow-8.jpg"

function set_first_image() {
    document.images.slide.src = eval("slide"+slide_number+".src");
}
function set_second_image() {
    document.images.slide2.src = eval("slide"+slide_number2+".src");
}
function first_image_fade_in(){
    document.getElementsByClassName('slideshow')[0].setAttribute('style', 'opacity: 0;');
    slide_number += 2;
    if (slide_number >= 9){
        slide_number = 1;
    }
    setTimeout(function() {set_first_image()}, 2000);
}
function first_image_fade_out(){
    document.getElementsByClassName('slideshow')[0].setAttribute('style', 'opacity: 1;');
}
function second_image_fade_in(){
    document.getElementsByClassName('slideshow2')[0].setAttribute('style', 'opacity: 0;');
    slide_number2 += 2;
    if (slide_number2 >= 9){
        slide_number2 = 2;
    }
    setTimeout(function() {set_second_image()}, 2000);
}
function second_image_fade_out(){
    document.getElementsByClassName('slideshow2')[0].setAttribute('style', 'opacity: 1;');
}

function slideshow(){
    setTimeout(function() {first_image_fade_in()}, 20000);
    setTimeout(function() {second_image_fade_out()}, 20000);
    setTimeout(function() {first_image_fade_out()}, 40000);
    setTimeout(function() {second_image_fade_in()}, 40000);
    setTimeout(function() {slideshow()}, 40000);
}


texts:
I've taken this class and made it my own but I'm curious to know what other think of it? Are there things I'm missing? Anything you can recommend?

class Database {
    private $host = '';
    private $user = '';
    private $pass = '';
    private $dbname = '';

    private $dbh;
    private $error;
    private $stmt;

    public function __construct() {
        $dsn = 'mysql:dbname=' . $this -> dbname . ';host=' . $this -> host . '';
        // Set options
        $options = array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
        // Create a new PDO instanace
        try {
            $this -> dbh = new PDO($dsn, $this -> user, $this -> pass, $options);
        } catch(PDOException $e) {
            $this->error = $e -> getMessage();
        }
    }

    public function query($query) {
        $this -> stmt = $this -> dbh -> prepare($query);
    }

    public function bind($param, $value, $type = null) {
        if (is_null($type)) {
            switch (true) {
                case is_int($value) :
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value) :
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value) :
                    $type = PDO::PARAM_NULL;
                    break;
                default :
                    $type = PDO::PARAM_STR;
            }
        }
        $this -> stmt -> bindValue($param, $value, $type);
    }

    public function execute() {
        return $this -> stmt -> execute();
    }

    public function fetchAll() {
        $this -> execute();
        return $this -> stmt -> fetchAll(PDO::FETCH_ASSOC);
    }

    public function fetch() {
        $this -> execute();
        return $this -> stmt -> fetch(PDO::FETCH_ASSOC);
    }

    public function rowCount() {
        return $this -> stmt -> rowCount();
    }

    public function countAll($arr) {
        return count($arr);
    }

    public function lastInsertId() {
        return $this -> dbh -> lastInsertId();
    }
}


texts:
I receive a list of links via http and I want to open them all in a new Firefox (or default browser) window as tabs; that is my script must not touch existing windows.

This is the best solution I could come up with:

from subprocess import call

links = GetListViaHTTP()

if len(links) == 1:
    call(['firefox', '-new-window'] + links)
elif len(links) > 1:
    call(['firefox'] + links)
else:
    print('Nothing to do.')

This worked like a charm when I tested it, but putting something I received via http into subprocess.call does not look save to me at all. So the question is:

How can I improve this code to avoid being vulnerable to MITM attacks?

If the list was not tampered with by an attacker, all links it contains will point to Stack Overflow answers, so maybe somehow validating the links with urllib.parse is good enough?


texts:
As you can see here:

20% CPU in a 3 minutes and 20 seconds excessive 40 executions async/await at a time for 9700 times caused quite a bit of CPU usage.

Code:

public MemoryStream getMessageBody()
{
    #region Get MessageBody
    IEnumerable<BytesWraper> MessageBody = GetMessageBodySource();
    if (MessageBody == null) {
        return null;
    }
    #endregion
    #region Write MessageBody's Data to a MemoryStream so we can convert it later to a String
    MemoryStream memoryStream = new MemoryStream(ContentLength == -1 ? 0 : ContentLength);
    try {
        foreach (BytesWraper bytes in MessageBody) {
            memoryStream.Write(bytes.Value, 0, bytes.Length);
        }
    } catch (Exception) {
        return null;
    }
    #endregion
    if (ConnectionClosed()) {
        _request.Dispose();
    }
    MessageBodyLoaded = true;
    return memoryStream;
}

Is there a way here to reduce the amount of usage by this foreach call? Perhaps not need to do it at all?

All im then doing with the result is as follows:

return MessageBody != null ? CharacterSet.GetString(MessageBody.GetBuffer(), 0, (int)MessageBody.Length) : null

(where MessageBody is getMessageBody())


texts:
This is the dirty bastard of my current code block, and I'm trying to find a way to improve this code for increased readability, but also functionality.

The method checks if the store is currently open. Yet on our site we close the sites 30 minutes prior to their real closing time. Issues arise mostly because all of our stores are open past midnight.

So this function is supposed to figure out if a store is open, taking into account that we close it 30 minutes before it's actual close time, and the fact that if it closes 12:10AM the next day, we have to close it 11:40PM the prior day.

def open?(current_time: Time.now.change(sec: 0))
  current_business_hour = business_hours.where('week_day = ? AND open_at <= ? AND close_at >= ?', current_time.wday, current_time, current_time).first
  if current_business_hour
    safe_closing_hour = current_business_hour.close_at - 30.minutes
    if current_time.strftime('%H:%M') >= '23:29'
      next_opening_hour = business_hours.where(week_day: DateTime.now.tomorrow.wday).order(:open_at).first
      if next_opening_hour
        if next_opening_hour.open_at.strftime('%H:%M') == '00:00'
          if next_opening_hour.close_at.strftime('%H:%M') >= '00:29'
            true
          elsif (next_opening_hour.close_at - 30.minutes).strftime('%H:%M') < current_time.strftime('%H:%M')
            false
          else
            true
          end
        else
          false
        end
      else
        false
      end
    elsif safe_closing_hour.strftime('%H:%M') < current_time.strftime('%H:%M')
      false
    else
      true
    end
  else
    false
  end
end

It's an unappealing spiderweb of if else statements, I do hope that the code makes someone what sense when you read it, then it's just the puzzle of understanding which true and false belongs to which.

I also feel like this is awfully complicated, and I wish that there was an easier way to do this.

Each store has many business hours, which has the week_day and an open_at and close_at. The store can have several business hours in one week_day. F.ex a store can close at 1AM, but open at 11AM and go til 11:59PM.


texts:
I have the following bit of code the calls out to two different command line components (each wrapped in their own Task)

public async Task<FileInfo> RasterizeAllPdfs(IEnumerable<Uri> targetUris, FileInfo destination = null)
{
    // rasterize uris to files in the rasterizedOutput directory
    await rasterize(rasterizedOutput.Value, targetUris);
    // concatenate all pdfs in this directory
    return await concatenatePdfs(rasterizedOutput.Value.EnumerateFiles("*.pdf"), destination);
}

What I would like is for the method to return a task immediately and for the returned task to become resolved once the task returned by concatentatePdfs finishes running.

It seems to work correctly (as in it's returning correct output) but I'm not 100% sure that I'm using the async and await keywords properly


texts:
For positive integer x let define function F(x) = 1 * (1! + x) + 2 * (2! + x) + .. + x * (x! + x).
"k!" means factorial: k! = 1 * 2 * .. * k
Chef wants to calculate F(p1) + F(p2) + ... + F(pn).
As answer could be large, help him, calculate value modulo m.

Input

First line contains two integers n and m.
Next line contains n space separated integers pi.

Output

Output a single line containing one integer --- calculated value modulo m.

Constraints
1 ≤ n ≤ 10^5
1 ≤ pi ≤ 10^18
1 ≤ m ≤ 10^7

Subtasks
Subtask #1: 1 ≤ pi ≤ 6 (10 points)
Subtask #2: 1 ≤ pi ≤ 7 * 10^3 (25 points)
Subtask #3: m - prime number (25 points)
Subtask #4: original constraints (40 points)

Here is my code
#include <stdio.h>
#define M 10000000

long long int factorial(long long int n)
{
    long long int i,fact=1;
    for(i=1;i<=n;i++)
        fact=((fact%M)*(i%M))%M;
    return fact;
}
long long int func(long long int n)
{
    long long int a=((n%M)*(n%M)*((n+1)%M))%M/2;
    long long int sum=(a+factorial(n+1)-1)%M;
    return sum;
}

int main()
{
    long long int n,m;
    scanf("%lld%lld",&n,&m);
    long long int i,p[n];
    for(i=0;i<n;i++)
    {
        scanf("%lld",&p[i]);
    }
    long long int sum=0;
    for(i=0;i<n;i++)
    {
        sum+=func(p[i]);
    }
    printf("%lld\n",sum%m);
    return 0;
}

My code exceeds time limit on subtasks 2,3 and 4. How do I improve efficiency?