Computing the improper integral of several functions How can I optimize and make prettier the following code?

Main.cpp

#include "UtilitiesP.h"
#include "Simpson.h"

int main(void)
{
    double agm(1/functionsP::agm(1, 2));
    double integral( (2/constantsP::kPi) * Simpson(0, constantsP::kPi*0.5, constantsP::kEps, functionsP::eliptic));
    assert(fabs(agm-integral)<constantsP::kEps);

    double firstVal(constantsP::kPi*0.5);
    integral = Simpson(constantsP::kAlmostZero, 10000, constantsP::kEps, functionsP::Dirichlet);

    std::cout << firstVal << std::endl;
    std::cout << integral << std::endl;

    double secondVal(sqrt(constantsP::kPi)*0.5);
    integral = Simpson(constantsP::kAlmostZero, 12, constantsP::kEps, functionsP::Puasson);

    std::cout << secondVal << std::endl;
    std::cout << integral << std::endl;

    double thirdVal(constantsP::kPi/sin(constantsP::kNumberFromEuler*constantsP::kPi));
    integral = Simpson(constantsP::kAlmostZero, 13, constantsP::kEps, functionsP::Euler);

    std::cout << thirdVal << std::endl;
    std::cout << integral << std::endl;

    return 0;
}

Utilities.h

#include <stdexcept>
#include <iostream>

//#define NDEBUG
#include <cassert>

namespace constantsP
{
    const double kPi(3.1415926535897932);
    const double kEps(1.e-12);
    const double kAlmostZero(1.e-7);
    const double kBigNumber(30);
    const double kNumberFromEuler(0.4);
};

namespace functionsP
{
    double agm(const double&, const double&);
    double eliptic(const double&);
    double Dirichlet(const double&);
    double Puasson(const double&);
    double Euler(const double&);
};

Utilities.cpp

#include "UtilitiesP.h"

double functionsP::agm(const double& a, const double& b)
{
    if(a <= 0 || b <= 0)
    {throw std::invalid_argument("Arithmetic-geometric mean is defined only for 0 < a < b.");}

    double aPrev(a), bPrev(b), aCurrent(a), bCurrent(b);
    do
    {
        aPrev = aCurrent;
        bPrev = bCurrent;
        aCurrent = sqrt(aPrev*bPrev);
        bCurrent = (aPrev+bPrev)*0.5;
    }while((aPrev < aCurrent) && (bCurrent < bPrev) && (aPrev < bPrev));

    double res((aPrev <= aCurrent)?aPrev:bPrev);

    return res;
}

double functionsP::eliptic(const double& x)
{
    return 1/sqrt(pow(1*sin(x), 2) + pow(2*cos(x), 2));
}

double functionsP::Dirichlet(const double& x)
{
    return sin(2*x)/x;
}

double functionsP::Puasson(const double& x)
{
    return exp(-(x*x));
}

double functionsP::Euler(const double& x)
{
    return pow(x, constantsP::kNumberFromEuler-1)/(1+x);
}

Simpson.h

#include <cmath>

double Simpson (const double& a, const double& b, const double& eps, double(*const f)(const double&));

Simpson.cpp

#include "Simpson.h"

double Simpson (const double& a, const double& b, const double& eps, double(*const f)(const double&))
{
    double step((b-a)*0.5);
    double s1(step*(f(a)+f(b))), s2(0), s4(4*step*f(a+step));
    double previousSum(0), currentSum(s1+s4);
    int n(2);
    do
    {
        previousSum = currentSum;
        n += n;
        step *= 0.5;

        s1 *= 0.5;
        s2 = 0.5*s2+0.25*s4;

        s4 = 0;
        for(int i(1); i < n; i+=2)
        {s4 += f(a+i*step);}
        s4 = 4*step*s4;

        currentSum = s1+s2+s4;
    }
    while(eps<fabs(previousSum-currentSum));

    return currentSum/3;
}

After running the code you will see that it works well for the first three integrals. But it works very bad for the last one. It takes very much time to compute it.

What can I change in order to make it work faster?

What comments about the style do you have?

You are assuming the functions in <cmath> (e.g. fabs(), sin()) etc. are in the global namespace already. However, it is unspecified in the standard whether or not these functions exist in the global namespace. The easiest way to ensure they are in the global namespace is to add using namespace std; to the appropriate source file, but that's bad practice. A better practice would be to add using declarations (preferably in the most local scope possible), or simply add std:: to the beginning of each function call.

There is little point in passing a primitive like double by const reference rather than by value. The performance is about the same and it saves you typing to pass by value.

Areas of your code are difficult to read due to lack of spacing. For example:

while(eps<fabs(previousSum-currentSum));

The same line is easier to read like this:

while (eps < fabs(previousSum - currentSum));

Similarly, this:

for(int i(1); i < n; i+=2)
    {s4 += f(a+i*step);}

would be easier to read with the { and } on their own lines like this:

for (int i(1); i < n; i += 2)
{
  s4 += f(a + i*step);
}

If your compiler supports C++11 or later you may want to initialize with curly braces ({, }) to avoid the most vexing parse.


Java MongoDB client as a singleton Is below code a valid Singleton design pattern for Mongodb in Java?

Please suggest any improvements.

public class Mongo extends MongoClient {

    private static Mongo mongoClient = null;
    private static MongoDatabase mogoDB = null;

    private Mongo(ServerAddress server, List<MongoCredential> mongoCredentialList) {
        super(server, mongoCredentialList);
    }

    public static Mongo getMongoClient() {
        return getMongoClient("userName", "password", "127.0.0.1", 27017, "SampleDB");
    }

    public static Mongo getMongoClient(String userName, String pwd, String ip, int port, String dbName) {

        if (mongoClient == null) {

            logger.debug("******* Trying to connect to Mongo Server *******");

            ServerAddress server = new ServerAddress(ip, port);
            List<MongoCredential> mongoCredentialList = new ArrayList();
            char[] password = pwd.toCharArray();
            mongoCredentialList.add(MongoCredential.createCredential(userName, dbName, password));

            mongoClient = new Mongo(server, mongoCredentialList);

            logger.debug("******* Connected to the Mongo server Successfully *******");
        } else {
            logger.debug("Already connected to Mongo server...");
        }
        return mongoClient;
    }

    public static MongoDatabase getMongoDatabase() {

        if (mogoDB == null) {
            logger.debug("******* Trying to get Mongo DB - SampleDB - connection. *******");
            mogoDB = getMongoClient().getDatabase("SampleDB");
            logger.debug("******* Successfully Connected to - SampleDB - Mongo DB *******");
        }
        return mogoDB;

    }

    public static MongoDatabase getMongoDatabase(String dbName) {

        if (mogoDB == null) {
            logger.debug("******* Trying to get Mongo DB - " + dbName + " - connection. *******");
            mogoDB = getMongoClient().getDatabase(dbName);
            logger.debug("******* Successfully Connected to - " + dbName + " - Mongo DB *******");
        }
        return mogoDB;

    }
}

Simple answer: No. Unless if your application only ever runs on 1 thread.

The easiest way to lazy initialise a singleton thread safe would be with a holder pattern. Example from wikipedia:

public class Something {
    private Something() {}

    private static class LazyHolder {
        static final Something INSTANCE = new Something();
    }

    public static Something getInstance() {
        return LazyHolder.INSTANCE;
    }
}

This is thread safe because the java class loader ensures that the INSTANCE is initialised exactly once, when the LazyHolder class is used for the first time.

Since the database you initialise isn't known at compile time in this class you cannot use this pattern. What you can do is a pattern called double-checked locking:

public final class Singleton {
    private static volatile Singleton instance = null;

    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized(Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

Important here are the synchronized(...) and volatile keywords.
The synchronised part makes sure only 1 thread can execute the construction code. And the volatile ensures that all threads always see all changes to the variable.


Cross-platform command line tool for finding files #!/usr/bin/env python

''' Search for files using wildcards amd magic '''

import os
from fnmatch import fnmatch
import sys
import argparse
import magic

def cmd_args():
    ''' returns a argparse namespane '''
    parser = argparse.ArgumentParser()

    parser.add_argument(
        '--root', default='.',
        help='Starting point [ defaults to cwd ]')

    parser.add_argument(
        '--pattern', default='*.*',
        help='Pattern to use for matching file names [ Defaults to *.* ]')

    parser.add_argument(
        '--depth',
        default=2**32,
        type=int,
         help='''
        Depth is the number of slashes in the files path ''')

    parser.add_argument(
        '--links',
        default=True,
        type=bool,
        help='\nFollow links\n')

    parser.add_argument(
        '--magic',
        default=None,
        type=str,
        help='Filter file by a magic keyword link: image data png')

    parser.add_argument(
        '--cat',
        default=False,
        type=bool,
        help='Concatenate matched files')

    return  parser.parse_args()

CMD_ARGS = cmd_args()

def cat(filename):
    ''' cat(filename) -> file contents'''
    if CMD_ARGS.cat:
        try:
            with open(filename, 'r') as file_object:
                print file_object.read()
        except IOError:
            pass
    else:
        return filename

def depth_check(link):
    '''
    This is called on each files path to control depth cutoff.
    It works by counting the number of slashes in the links path.
    If DEPTH > MAX DEPTH {
        EXIT PROGRAM
    }
    Note: By default max depth is 2**32 aka big ass number
    '''

    if link.count('/') > CMD_ARGS.depth:
        sys.exit()
    else:
        return link

def aggragate_files(root, dirs, files):

    _files = [depth_check(os.path.join(root, filename))
              for filename in files if fnmatch(filename, CMD_ARGS.pattern)]

    _subfiles = [depth_check(os.path.join(root, filename))
                 for filename in dirs if fnmatch(filename, CMD_ARGS.pattern)]

    return set(_files + _subfiles)

def find():
    for root, dirs, files in os.walk(CMD_ARGS.root,
                                     followlinks=CMD_ARGS.links):
        handle_files(aggragate_files(root, dirs, files))

def handle_files(files):
    for filename in files:
        if CMD_ARGS.magic:
            meta = magic.from_file(filename)
            if meta and CMD_ARGS.magic.lower() in meta.lower():
                print cat(filename)
        else:
            print cat(filename)

if __name__ == '__main__':
    find()

My concerns with the the code above:

Is the code easy to read?
Does this code honor PEP8?
Have I made any mistakes in the control-flow?
Would this code be considered pythonic?
Are my naming conventions descriptive?

How can I structure this code to make it easy to add features?

For example, where and how should this be hooked in?

def cat(filename):
        ''' cat(filename) -> file contents'''
        if CMD_ARGS.cat:
            try:
                with open(filename, 'r') as file_object:
                    print file_object.read()
            except IOError:
                pass
        else:
            return filename

Let's first dispense with PEP8: by and large, as far as I can tell, your code respects PEP8. On the other hand, I've been writing Python professionally for almost 20 years and have never read PEP8 before today.

This code does a number of things that I wouldn't do, however. First, it uses

if __name__ == "__main__"

when there's no way for this code to be run any other way; it parses its command arguments from the arguments of the invocation of Python. This idiom is used to distinguish script behavior from module behavior, and your script behavior is all over the file. If you want handle_files to be useable as an imported function, you'll want to pass it all its arguments, and you'll want the option of not processing command line arguments. So really, all your command-line argument processing should be inside the conditional instead of outside - that is, assuming you want to be able to import this module as well as run it as a script. If you don't care about importing it, don't use the if __name__ == "__main__" idiom.

If you're interested in importing as an option, you also won't want to call sys.exit() in any of your module-level code (e.g., the depth_check function). Raise an error instead, and when you're running the module instead of importing, catch and exit.

I don't see the reason for the cmd_args() function. It's called once, and it creates module-level information; why should it be in a function?

On the other hand, I don't like the references to the globals all over the code - a special no-no if you're worrying about using this module by importing it. If you intend to import, my preferred organization would be:

class HandleFileError(Exception):
    pass

def depth_check(link, depth = ...):
    ... raise HandleFileError ...

def handle_files(files, cat = False, ....):
    ...

if __name__ == "__main__":
    ... process cmd args ...
    try:
        find(options.root, cat = options.cat, ...)
    except HandleFileError:
        ... report the error...
        sys.exit(1)

And you've got enough options that I'd probably create a Finder class and initialize it with the parameters, to hold the state.

On the other hand, if you just want this to be a script, I don't see the point of the find() function - it's called exactly once, after the command line arguments are parsed. I'd get rid of it.

Have you tested this code? As far as I can tell from reading it, the cat option will cause handle_files to print out None for each file that's concatenated. It's an odd mixture, either printing the contents of the files or the filenames; but one way or another, either the cat function should print the filename, or it should return the file contents, but not print in one case and return in another.

A couple small inefficiencies. First, you call CMD_ARGS.magic.lower() every time you check a file. You should call it once, when the finder is initializing, and never again. Second, in aggragate_files, you create two lists, then create a third list by concatenating the two lists, and then turn the third list into a set. At the very least, you can do this:

s = set(_files)
s.update(_subfiles)

But more globally, combining these two elements is wrong in general, because cat will always fail on the directory. Have aggragate_files return a pair, and only call cat on the files; for the dirs, just print them.

Finally, aggragate_files is misspelled; it's aggregate.


Scraping next page using BeautifulSoup I have created a script for article scraping - it finds title, subtitle, href-link, and the time of publication. Once retrieved, information is converted to a pandas dataframe, and the link for the next page is returned as well (so that it parses page after page).

Everything works as expected, though I feel there should be an easier -or more elegant- way of loading a subsequent page within main function.

import requests
import pandas as pd
from bs4 import BeautifulSoup
from time import sleep

def read_page(url):
    r = requests.get(url)
    return BeautifulSoup(r.content, "lxml")

def news_scraper(soup):
    BASE = "https://www.pravda.com.ua"
    container = []
    for i in soup.select("div.news.news_all > div"):
        container.append(
            [
                i.a.text,  # title
                i.find(class_="article__subtitle").text,  # subtitle
                i.div.text,  # time
                BASE + i.a["href"],  # link
            ]
        )

    dataframe = pd.DataFrame(container, columns=["title", "subtitle", "time", "link"])
    dataframe["date"] = (
        dataframe["link"]
        .str.extract("(\d{4}/\d{2}/\d{2})")[0]
        .str.cat(dataframe["time"], sep=" ")
    )
    next_page = soup.select_one("div.archive-navigation > a.button.button_next")["href"]

    return dataframe.drop("time", axis=1), BASE + next_page

def main(START_URL):
    print(START_URL)

    results = []

    soup = read_page(START_URL)
    df, next_page = news_scraper(soup)
    results.append(df)

    while next_page:
        print(next_page)

        try:
            soup = read_page(next_page)
            df, next_page = news_scraper(soup)
            results.append(df)
        except:
            next_page = False

        sleep(1)

    return pd.concat([r for r in results], ignore_index=True)

if __name__ == "__main__":
    df = main("https://www.pravda.com.ua/archives/date_24122019/")
    assert df.shape == (120, 4) # it's true as of today, 12.26.2019

Optimization and restructuring

Function's responsibility

The initial approach makes the read_page function depend on both requests and BeautifulSoup modules (though BeautifulSoup functionality/features is not actually used there). Then, a soup instance is passed to news_scraper(soup) function.To reduce dependencies let read_page function extract the remote webpage and just return its contents r.content. That will also uncouple news_scraper from soup instance arguments and allow to pass any markup content, making the function more unified.

Namings

BASE = "https://www.pravda.com.ua" within news_scraper function is essentially acting like a local variable. But considering it as a constant - it should be moved out at top level and renamed to a meaningful BASE_URL = "https://www.pravda.com.ua".

i is not a good variable name to reflect a document element in for i in soup.select("div.news.news_all > div"). Good names are node, el, atricle ...

The main function is better renamed to news_to_df to reflect the actual intention.main(START_URL) - don't give arguments uppercased names, it should be start_url.

Parsing news items and composing "date" value

As you parse webpages (html pages) - specifying html.parser or html5lib (not lxml) is preferable for creating BeautifulSoup instance.

Extracting an article publication time with generic i.div.text would be wrong as a parent node div.article could potentially contain another child div nodes with text content. Therefore, the search query should be more exact: news_time = el.find(class_='article__time').text.Instead of assigning, traversing and dropping "time" column and aggregating:

dataframe["date"] = (
        dataframe["link"]
        .str.extract("(\d{4}/\d{2}/\d{2})")[0]
        .str.cat(dataframe["time"], sep=" ")
    )

- that all can be eliminated and the date column can be calculated at once by combining the extracted date value (powered by precompiled regex pattern DATE_PAT = re.compile(r'\d{4}/\d{2}/\d{2}')) and news_time value.

Instead of collecting a list of lists - a more robust way is to collect a list of dictionaries like {'title': ..., 'subtitle': ..., 'date': ..., 'link': ...} as that will prevent confusing the order of values for strict list of column names.

Furthermore, instead of appending to list, a sequence of needed dictionaries can be efficiently collected with generator function. See the full implementation below.

The main function (new name: news_to_df)

The while next_page: turned to while True:.

except: - do not use bare except, at least catch basic Exception class: except Exception:.

The repeated blocks of read_page, news_scraper and results.append(df) statements can be reduced to a single block (see below).One subtle nuance is that the ultimate "next" page will have '/archives/' in its a.button.button_next.href path, signaling the end of paging. It's worth to handle that situation explicitly:

if next_page == '/archives/':
    break

The final optimized solution:

import requests
import pandas as pd
from bs4 import BeautifulSoup
from time import sleep
import re

BASE_URL = "https://www.pravda.com.ua"
DATE_PAT = re.compile(r'\d{4}/\d{2}/\d{2}')

def read_page(url):
    r = requests.get(url)
    return r.content

def _collect_newsitems_gen(articles):
    for el in articles:
        a_node = el.a
        news_time = el.find(class_='article__time').text
        yield {'title': a_node.text,
               'subtitle': el.find(class_="article__subtitle").text,
               'date': f'{DATE_PAT.search(a_node["href"]).group()} {news_time}',
               'link': f'{BASE_URL}{a_node["href"]}'}

def news_scraper(news_content):
    soup = BeautifulSoup(news_content, "html5lib")
    articles = soup.select("div.news.news_all > div")
    next_page_url = soup.select_one("div.archive-navigation > a.button.button_next")["href"]
    df = pd.DataFrame(list(_collect_newsitems_gen(articles)),
                      columns=["title", "subtitle", "date", "link"])

    return df, f'{BASE_URL}{next_page_url}'

def news_to_df(start_url):
    next_page = start_url
    results = []

    while True:
        print(next_page)
        try:
            content = read_page(next_page)
            df, next_page = news_scraper(content)
            results.append(df)
            if next_page == '/archives/':
                break
        except Exception:
            break

        sleep(1)

    return pd.concat([r for r in results], ignore_index=True)

if __name__ == "__main__":
    df = news_to_df("https://www.pravda.com.ua/archives/date_24122019/")
    assert df.shape == (120, 4)  # it's true as of today, 12.26.2019

If printing the final resulting df with print(df.to_string()) - the output would look like below (with cutted the middle part to make it a bit shorter):

https://www.pravda.com.ua/archives/date_24122019/
https://www.pravda.com.ua/archives/date_25122019/
https://www.pravda.com.ua/archives/
                                                 title                                           subtitle              date                                               link
0    Голова Закарпаття не зрозумів, за що його звіл...  Голова Закарпатської обласної державної адміні...  2019/12/24 23:36  https://www.pravda.com.ua/news/2019/12/24/7235...
1    Стало відомо коли відновлять будівництво об'єк...  На зустрічі представників керівництва ХК Київм...  2019/12/24 22:41  https://www.pravda.com.uahttps://www.epravda.c...
2          ВАКС продовжив арешт Гримчаку до 14 лютого   Вищий антикорупційний продовжив арешт для коли...  2019/12/24 22:25  https://www.pravda.com.ua/news/2019/12/24/7235...
3    Економічні новини 24 грудня: транзит газу, зни...  Про транзит газу, про зниження "платіжок", про...  2019/12/24 22:10  https://www.pravda.com.uahttps://www.epravda.c...
4    Трамп: США готові до будь-якого "різдвяного по...  Президент США Дональд Трамп на тлі побоювань щ...  2019/12/24 22:00  https://www.pravda.com.uahttps://www.eurointeg...
5    У податковій слідчі дії – електронні сервіси п...  Державна податкова служба попереджає, що елект...  2019/12/24 21:55  https://www.pravda.com.ua/news/2019/12/24/7235...
6     Мінфін знизив ставки за держборгом до 11% річних  Міністерство фінансів знизило середньозважену ...  2019/12/24 21:31  https://www.pravda.com.uahttps://www.epravda.c...
7    Україна викреслила зі списку на обмін ексберку...  Російський адвокат Валентин Рибін заявляє, що ...  2019/12/24 21:13  https://www.pravda.com.ua/news/2019/12/24/7235...
8    Посол: іспанський клуб покарають за образи укр...  Посол України в Іспанії Анатолій Щерба заявив,...  2019/12/24 20:45  https://www.pravda.com.uahttps://www.eurointeg...
9    Міністр енергетики: "Газпром" може "зістрибнут...  У Міністерстві енергетики не виключають, що "Г...  2019/12/24 20:03  https://www.pravda.com.uahttps://www.epravda.c...
10   Зеленський призначив Арахамію секретарем Націн...  Президент Володимир Зеленський затвердив персо...  2019/12/24 20:00  https://www.pravda.com.ua/news/2019/12/24/7235...
...
110  Уряд придумав, як захистити українців від шкод...  Кабінет міністрів схвалив законопроєкт, який з...  2019/12/25 06:54  https://www.pravda.com.ua/news/2019/12/25/7235...
111  Кіберполіція та YouControl домовилися про спів...  Кіберполіція та компанія YouControl підписали ...  2019/12/25 06:00  https://www.pravda.com.ua/news/2019/12/25/7235...
112  В окупованому Криму продають прикарпатські яли...  У центрі Сімферополя, на новорічному ярмарку п...  2019/12/25 05:11  https://www.pravda.com.ua/news/2019/12/25/7235...
113  У США схожий на Санту чоловік пограбував банк,...  У Сполучених Штатах чоловік з білою, як у Сант...  2019/12/25 04:00  https://www.pravda.com.ua/news/2019/12/25/7235...
114  У Росії за "дитячу порнографію" посадили блоге...  Верховний суд російської Чувашії засудив до тр...  2019/12/25 03:26  https://www.pravda.com.ua/news/2019/12/25/7235...
115  Уряд провів екстрене засідання через газові пе...  Кабінет міністрів у вівторок ввечері провів ек...  2019/12/25 02:31  https://www.pravda.com.ua/news/2019/12/25/7235...
116  Нова стратегія Мінспорту: розвиток інфраструкт...  Стратегія розвитку спорту і фізичної активност...  2019/12/25 02:14  https://www.pravda.com.ua/news/2019/12/25/7235...
117  Милованов розкритикував НБУ за курс гривні та ...  Міністр розвитку економіки Тимофій Милованов р...  2019/12/24 01:46  https://www.pravda.com.uahttps://www.epravda.c...
118  Російські літаки розбомбили школу в Сирії: заг...  Щонайменше 10 людей, в тому числі шестеро – ді...  2019/12/25 01:04  https://www.pravda.com.ua/news/2019/12/25/7235...
119  Ліквідація "майданчиків Яценка": Зеленський пі...  Президент Володимир Зеленський підписав закон,...  2019/12/25 00:27  https://www.pravda.com.ua/news/2019/12/25/7235...

P.S. From Ukraine with love ...


Python 2.7 Payroll Calculator program This is a calculator I'm working on. The ultimate goal is to calculate multiple payrolls before selecting to end the program, please review current version in that context. PSA: I'm a beginner.

def main():

    hours, rate = inputData()
    normal_hours, overtime_hours, overtime  = computePay (hours,rate)
    regular, total_pay  = preovertime_hours (hours, rate, overtime)
    displayPay  (rate, normal_hours, overtime_hours, regular, overtime, total_pay)

def inputData():

    name = raw_input('Name of employee: ')
    hours = float (input('Now enter the hours worked please:'))
    rate = float (input('Now enter the pay rate:'))

    return hours, rate

def computePay (hours,rate):

    if hours < 40:
        normal_hours = hours
        overtime_hours = 0
    else:
        normal_hours = 40
        overtime_hours = hours - 40

    if hours > 40:
        overtimerate = 1.5 * rate
        overtime = (hours-40) * overtimerate
    else:
        overtime = 0

    return normal_hours, overtime_hours, overtime

def preovertime_hours (hours, rate, overtime):

    regular = hours * rate
    total_pay = regular
    return regular, total_pay

def displayPay (rate, normal_hours, overtime_hours, regular, overtime, total_pay):

    print ("The amount to be paid to in this pay period is $" + format(total_pay, '.2f'))

main()

First off, there currently is no point in having name = raw_input('Name of employee: '),
as it is never used.

Generally it's safer to never use input. For example:

>>> hello, world = 'goodbye', 'logic'
>>> input()
hello + world
'goodbyelogic'

As you can see, input evaluates the input. Which is not very safe.
Lets say you have some special information that no-one should know about.
But you stored it in a variable.
We with our 'elite haxor skillz', we could find out that information.
for example:

>>> private_key = 'my super private key'
>>> input()
globals()['private_key']
'my super private key'

Bottom line input is bad, so bad that it got removed in Python3.
And so, I would recommend to always use raw_input, in Python2.

float (input('Now enter the hours worked please:'))

First as mentioned above, this should be raw_input,
but it is flimsy, say we input a.
float will error, and the program will exit.

Instead you can use a try except statement.

while True:
    try:
        float(input('Now enter the hours worked please:'))
        break
    except ValueError:
        pass

computePay is really good.
If you are ok with using turnery operators you can re-write it, but your way is probably best.
Except for overtimerate, that should be part of overtime.
If I were to re-write your code then it would be something like:

def computePay (hours,rate):
    normal_hours, overtime_hours = hours, 0 if hours < 40 else 40, hours - 40
    overtime = (hours - 40) * 1.5 * rate if hours > 40 else 0
    return normal_hours, overtime_hours, overtime

preovertime_hours can just be a return statement.
You could even opt for it not to be a function.
I'm more inclined for the latter as it just returns hours * rate, hours * rate.
Also you should add it to computePay as it would fit better there.

In displayPay you may want to use str.format rather than format.
They're very similar, so to change to str.format you would do:

"The amount to be paid to in this pay period is ${:.2f}".format(total_pay)

As for main.
Due to all the changes above I personally would recommend that you change it to 3 function calls.
They are inputData, computePay and displayPay.
Whilst you could merge it all into one function and keep everything simple,
keeping IO away from logic is always good.

Overall your structure is pretty good. There are a lot of nice small functions.

Just to note, you should always have functions declared and used as function().
Note there is no space between the name and the brackets!

Also as some languages allow - in names,
it's always a good idea to put a space either side of operators.

You could 'wrap' your main() in a if __name__ in '__main__':.
This prevents your code running if it's not the main file.
(E.g. if someone imports your file into there code.)

But otherwise your code is good.

Just so you know currently almost all your code is dead code (It does nothing).
And so you could re-write main into:

def main():
    hours, rate = inputData()
    displayPay('', '', '', '', '', hours * rate)

  The ultimate goal is to calculate multiple payrolls before selecting to end the program

If you want this you could make a while loop like the example above to fix float(input()).
A simple way to do this is to ask a question like 'Would you like to add another user? [y/n] '.
And exit on any input that is not 'y' or 'yes'.

As I'm not going to write it for you, the following is half the loop.

if input('Would you like to add another user? [y/n] ').lower() in ['y', 'yes']:
    main()


Number processing slower with asm.js I did a few times ago what I called a plasma in plain JavaScript:

jsFiddle

The idea is to use 2 oscillators (LFO) which have their frequency + phase driven by two oscillators each. Then we use those 2 main oscillator output to decide of the r, g, b of each point.

To test the quite new API asm.js, I've ported it to be fully 'typed' and in complete memory isolation.

The big issue I encountered is that it is way slower with asm.js than with plain JavaScript. Worse: When I comment 'use asm' to have the browser interpret the code, I get better results (30% faster) than when the compilation succeeds.

The memory I use is:

A byte array of the size of the canvas: this is the output that I will putImageData on the canvas.
Right after I consider I have a float array that stores the values for the LFOs (base / amplitude / frequency / phase ), for 6 LFO.

I've made two versions, one fp32 and one fp64, none of which is fast (<50 ms per frame, when the plain object version is around 20ms).

Be sure to use a Browser that understands asm.js Firefox is one.

jsFiddle

jsFiddle

After experimenting, a lot of time is spent evaluating the LFO value:

function oscillatorValueAt(heap_f64_Index, x) {
    heap_f64_Index = heap_f64_Index | 0;
    x = +x;
    var base = 0.0,
        amp = 0.0,
        freq = 0.0,
        phase = 0.0;

    heap_f64_Index = heap_f64_Index << 3;
    base = +heap_f64[(heap_f64_Index + 0) >> 3];
    amp = +heap_f64[(heap_f64_Index + 8) >> 3];
    freq = +heap_f64[(heap_f64_Index + 16) >> 3];
    phase = +heap_f64[(heap_f64_Index + 24) >> 3];
    return  +(base + amp * sin(freq * x + phase));
}

This is called several times per pixel in the tickPixels sub, with calls like:

vaty = +oscillatorValueAt(osc1, yD);

Could you help me to find why there isn't a speed boost?

Edit : When the compilation to asm.js is a success, you see in the console :

Successfully compiled asm.js code (total compilation time 0ms; not stored in cache)

On the other hand, if one does not follow asm.js convention, one gets a message like :

TypeError: asm.js type error: must be of the form +x, fround(x), simdType(x) or x|0

If you see this message, the code is interpreted as plain javascript

There could be a number of factors here. First, most of the computation happens in a tiny kernel - asm.js isn't necessary to make that fast, usually the normal JIT can do just as well. asm.js really shines on large applications with lots of hot code.

And asm.js might win or lose on such tiny kernels just because the register allocator might emit something a little different. Small kernels are very noisy that way.

But, the issue here looks like something else. Check out this, which modifies that code to add a little "counter" which adjusts the value sent into sin(). Actually, most of the time spent in this benchmark is in sin(), as shown by the profiler. And in Firefox, sin() is actually a little different in asm.js mode and not - when not, there is a cache which helps speed up simple code like you see in the SunSpider benchmark. All browsers implemented such caches because they sometimes really help. But so far Firefox doesn't have such a cache on asm.js. Anyhow, the counter I added makes it so the cache is less effective - a much wider range of values is sent into sin(), and with that change, asm.js is significantly faster. Or, to put it another way: asm.js is about as fast as before, but non-asm.js is much slower, due to the cache no longer helping.

What are the implications? If you need this code to be very fast, you might just implement your own sin() cache in asm.js. You can make it as low-precision as you want, so it could be even more efficient than the browser's general and precise cache.

Even better, you can implement sin() in asm.js as well, once more making it as low-precision as you can tolerate. Right now browsers call out to libc for sin(), which tends to be very precise, and a lower-precision asm.js one could beat that. It would also be more consistent between users, since no libc differences. (We are considering shipping sin() with emscripten's libc, for these reasons, but haven't so far.)


Singleton interface in Java I've created a singleton by means of an interface. I don't want to make a thing with getInstance() etc because I don't care about inheritance, and it's redundant and non-declarative.

Are there downsides of doing it this way? Any redundancy I can remove? Is there a cleaner/more consise way to do this? I like doing it this way because methods are first class because I can pass them to map etc (e.g: map(Dog.sleep, someListOfNumbers)) and it supports currying if you have functions like F<Pair<X,Pair<Y,Z>>>.

Note the F interface is declared elsewhere, I just inlined it here to have less files.

package a;
import static a.Dog.State.*;
public interface Dog {
    interface F<X,Y> {
        Y f(X x);
    }

    static final class State {
        protected static boolean dead;
        protected static String name = "leo";
        protected static Integer age = 0;
    }

    F<Void,String> getName = new F<Void,String>() {
        public String f(Void _) {
            return name;
        }
    };

    F<Void,Void> die = new F<Void,Void>() {
        public Void f(Void _) {
            System.out.println("X_X");
            dead = true;
            return null;
        }
    };

    F<Void,Void> woof = new F<Void,Void>() {
        public Void f(Void _) {
            if (dead)
                throw new IllegalStateException("cannot woof when dead");
                System.out.println("woof");
            return null;
        }
    };

    F<Integer,Void> sleep = new F<Integer,Void>() {
        public Void f(Integer years) {
            if (dead)
                throw new IllegalStateException("cannot sleep when dead");
            age += years;
            return null;
        }
    };

    F<Void,Integer> getAge = new F<Void,Integer>() {
        public Integer f(Void _) {
            return age;
        }
    };
}

A test:

import a.Dog;
class Main {
    public static void main(String[] args) {
      System.out.println(
          "The dog's name is " + Dog.getName.f(null) +
          " and is " + Dog.getAge.f(null) + " years old"
      );
      Dog.sleep.f(2);
      System.out.println(
          "The dog's name is " + Dog.getName.f(null) +
          " and is " + Dog.getAge.f(null) + " years old"
      );
      Dog.woof.f(null);
      Dog.die.f(null);
      Dog.woof.f(null);
    }
}

Your implementation fails to adhere to some important Object Oriented principles.

Encapsulation

The state of your singleton can be modified by others.

package a;

public class Cat implements Dog {

    static {
        State.age = 17;
    }

    public int age() {
        return Cat.getAge.f(null);
    }

    public void setAge(int age) {
        State.age = age;
    }

    public static void main(String[] args) {
        System.out.println(new Cat().age());
    }
}

Polymorphism

This is basically a problem that comes with the singleton pattern itself. However, a                             more conventional approach to implementing a singleton can deal with this issue :

package b;

public interface Dog {

    String getName();

    void die();

    void woof();
}

and the singleton implementation :

package b;

public enum SingleDog implements Dog {
    INSTANCE;

    private String name;
    private boolean dead;

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void die() {
        System.out.println("X_X");
        dead = true;
    }

    @Override
    public void woof() {
        if (dead) {
            throw new IllegalStateException("cannot woof when dead");
        }
        System.out.println("woof");
    }
}

This way clients can depend on the interface and not the singleton implementation. i.e. that there can only be one instance is now an implementation detail, and not an unchangeable design decision.

If you want to do functional programming, Java is probably not the language you should be using.


Building a simple singly-linked list in C++11 The implementation below works, but I'm wondering if there's a modern C++ alternative.

For example:

Should I replace the raw new with a braced initializer?
Should I replace the Node* with a shared_ptr?

Let me know if you see any other things I could improve. I know I could also use a class, but I'd like to understand how to do this with struct.

#include <iostream>

struct Node {
  int data;
  Node* next;
};

void insert(Node*& head, int data) {
  Node* new_node = new Node;
  new_node->data = data;
  new_node->next = head;
  head = new_node;
}

void print_list(Node* list) {
  Node* p {list};
  std::cout << "Printing list: " << std::endl;
  if (p) {
    std::cout << p->data;
    p = p->next;
    while(p) {
      std::cout << " -> " << p->data;
      p = p->next;
    }
    std::cout << std::endl;
  }
}

int main(int argc, char* argv[]) {
  Node* my_list {};
  insert(my_list, 10);
  insert(my_list, 20);
  print_list(my_list);
}
// Printing list:
// 20 -> 10

First, note that the only real difference between a struct and a class is the default accessibility: in a class everything is private by default, and in a struct everything is public by default. That's literally the only difference between the two.

In any case, some of your code can be simplified somewhat. For example, your insert can be reduced to just this:

void insert(Node*& head, int data) {
    head = new Node{data, head};
}

It's usually better to initialize rather than construct something uninitialized, and then assign a value anyway, so this is sort of a double-win.

I'd also prefer to use a for loop to print the list, and just a new-line where you want to print a new-line:

void print_list(Node* list) {
  Node* p {list};
  std::cout << "Printing list:\n";
  if (p)
  {
    std::cout << p->data;
    for (p=p->next; p != nullptr; p=p->next)
      std::cout << " -> " << p->data;
  }
  std::cout << "\n";
}

In this case, I'd restructure the code a bit to treat the list being non-null as a pre-condition to print_list and enforce that up-front:

void print_list(Node* list) {
  if (list == nullptr)
      return;
  std::cout << "Printing list:\n";
  std::cout << list->data;
  for (Node *p{list->next}; p != nullptr; p=p->next)
    std::cout << " -> " << p->data;
  std::cout << "\n";
}

The next steps I can see would involve the sort of re-vamping you don't seem to want right now--turning the linked list into a full-blown class of its own that supports iterators and such. This does have a tremendous advantage though: right now, your linked list only supports inserting items, then printing an entire linked list. If anybody wants to do anything else, they need to implement it entirely on their own, and know all about the internal details of the linked list to do it.

Along with that, you'd probably want to make it a generic container, so you can use it to store any data type, not just int.

This way, you can immediately support essentially all code that knows how to work with a collection that supports (in this case) forward iterators. You'd probably just eliminate print_list entirely, as it becomes just an application of std::copy from a list to (for example) an infix_iterator.

This is (IMO) much cleaner in general. In particular, it separates concerns much more cleanly: the linked list can concentrate solely on linked-list "things", and not worry about things like I/O as it does now.


OLED FeatherWing instruction page I have a Feather M0 basic paired with a OLED FeatherWing. I am making an instructions page but the code is quite repetitive. How can I make my code shorter?

import board
from busio import I2C
from adafruit_ssd1306 import SSD1306_I2C
from time import sleep
from digitalio import DigitalInOut, Direction, Pull

i2c = I2C(board.SCL, board.SDA)
oled = SSD1306_I2C(128, 32, i2c)

button_A = DigitalInOut(board.D9)
button_B = DigitalInOut(board.D6)
button_C = DigitalInOut(board.D5)

button_A.direction = Direction.INPUT
button_B.direction = Direction.INPUT
button_C.direction = Direction.INPUT

button_A.pull = Pull.UP
button_B.pull = Pull.UP
button_C.pull = Pull.UP

def check_buttons():
    if button_A.value is False:
        return 'A'
    elif button_B.value is False:
        return'B'
    elif button_C.value is False:
        return 'C'
    else:
        return None

def wait_for_A():
    sleep(0.5)
    while check_buttons() is not 'A':
        pass

oled.fill(0)
oled.text('Adafruit Feather', 0, 0, 1)
oled.text('Program selector', 0, 8, 1)
oled.text('Press A to continue', 0, 24, 1)
oled.show()

wait_for_A()

oled.fill(0)
oled.text('Instructions:', 0, 0, 1)
oled.text('Use A and C to move', 0, 8, 1)
oled.text('up and down.', 0, 16, 1)
oled.text('Press A to continue', 0, 24, 1)
oled.show()

wait_for_A()

oled.fill(0)
oled.text('Instructions (cont.):', 0, 0, 1)
oled.text('Press B to select a', 0, 8, 1)
oled.text('program.', 0, 16, 1)
oled.text('Press A to continue', 0, 24, 1)
oled.show()

wait_for_A()

oled.fill(0)
oled.text('Instructions (cont.):', 0, 0, 1)
oled.text('At anytime you can', 0, 8, 1)
oled.text('press reset to go...', 0, 16, 1)
oled.text('Press A to continue', 0, 24, 1)
oled.show()

wait_for_A()

oled.fill(0)
oled.text('Instructions (cont.):', 0, 0, 1)
oled.text('... back to the main', 0, 8, 1)
oled.text('screen.', 0, 16, 1)
oled.text('Press A to continue', 0, 24, 1)
oled.show()

wait_for_A()

oled.fill(0)
oled.text('Instructions (cont.):', 0, 0, 1)
oled.text('The programs will', 0, 8, 1)
oled.text('have there own...', 0, 16, 1)
oled.text('Press A to continue', 0, 24, 1)
oled.show()

wait_for_A()

oled.fill(0)
oled.text('Instructions (cont.):', 0, 0, 1)
oled.text('... instructions.', 0, 8, 1)
oled.text('Press A to continue', 0, 16, 1)
oled.text('to menu', 0, 24, 1)
oled.show()

wait_for_A()

oled.fill(0)
oled.show()

I think here a small class would help to keep track of all things related to a Button:

class Button(DigitalInOut):
    def __init__(self, name, pin, direction=Direction.INPUT, pull=Pull.UP):
        self.name = name
        super().__init__(pin)
        self.direction = direction
        self.pull = pull

    def wait_for_press(self):
        while self.value:
            pass

(Untested, since I obviously don't have your hardware lying around.)

With your initialization code can be shortened a bit:

names = ["A", "B", "C"]
pins = [board.D9, board.D6, board.D5]
buttons = {name: Button(name, pin) for name, pin in zip(names, pins)}

As for your printing to the oled, they always follow the same format, so just define a function for that:

def print_page(oled, strings):
    oled.fill(0)
    for i, s in enumerate(strings):
        oled.text(s, 0, i * 8, 1)
    oled.show()

After which your main code becomes:

messages = [
"""
Adafruit Feather
Program selector

Press A to continue
""",
"""
Instructions:
Use A and C to move
up and down
Press A to continue
""",
"""
Instructions (cont.):
Press B to select a
program.
Press A to continue
""",
...
]

for message in messages:
    print_page(oled, message.splitlines()[1:])
    buttons["A"].wait_for_press()
oled.fill(0)
oled.show()

Note that I used triple-quoted strings so you can just write your message in plaintext, line-breaks included. You might want to outsource that part to another file and just load / import from that.


Master engine class for a Pong game Question: Is having a master class bad practice? I am making a simple pong game, but I'm writing the code as if I were making for something that might be for something more complicated I want to create in the future. Also, am I not doing it in a good manner?

What I am doing: I have a class, in this case "engine", which basically holds everything and runs all of its referenced objects logic inside of its own functions. All of the other objects are then linked to each other by holding the data of the "world or engine" object and use it as a linker to other objects that it can interact with.

NOTE: The sfml media library was used with this project.
note: if you need the ball definitions i'll post them

#include <SFML/Graphics.hpp>

#include <vector>
#include <iostream>
#include <stdlib.h>

class paddle;
class ball;
class enemy;
class player;

class engine{
    ball *gameBall;
    enemy *enemyPaddle;
    player *playerPaddle;
    Clock gameClock;
    RenderWindow &refWindow;
    int gameSpeed;
    Event windowEvent;

    void eventDraw();
    void eventUpdate();
    void handleWindow();

public:
    engine( RenderWindow &rendy);
    ~engine();
    bool start();
    Vector2f getWindowSize();
    void drawPart(Drawable &art);
    Time getGameClock(){ return gameClock.getElapsedTime(); }
};

class ball{
private:
    CircleShape myShape;
    engine& world;
    int radius;
    Color color;
    Vector2f position, velocity;
    short signed int dirX, dirY;
    short signed int getDirection();
    void wallBounce();
public:
    ball( engine& gameEngine);
    void draw();
    CircleShape getCircle(){ return myShape; }
    void pop();
    void update();
};

int main()
{
    RenderWindow window(VideoMode(640, 480), "Pong");
    engine gameEngine( window );
    return (int)gameEngine.start();
}

// Class Prototyes
    // Engine
        engine::engine(RenderWindow &rendy):refWindow(rendy){
            gameBall = new ball(*this);
            enemyPaddle = new enemy;
            playerPaddle = new player;
            gameClock = Clock();
            gameSpeed = (1000/60.0);
            windowEvent = Event();
        }
        engine::~engine(){
            delete enemyPaddle;
            delete playerPaddle;
            delete gameBall;
        }

        void engine::handleWindow(){
            if ( refWindow.pollEvent( windowEvent ) )
            {
                 if ( windowEvent.type == Event::Closed )
                     refWindow.close();
            }
        }

        bool engine::start()
        {
            while( refWindow.isOpen() )
            {

                while( gameClock.getElapsedTime().asMilliseconds() > gameSpeed )
                {
                    eventUpdate();
                    eventDraw();

                    // Restart Clock
                        gameClock.restart();
                }

                handleWindow();
            }

            return true;
        }

        void engine::eventUpdate(){
            gameBall->update();

        }
        void engine::eventDraw(){
            refWindow.clear( Color::Black );

            gameBall->draw();

            refWindow.display();
        }

        Vector2f engine::getWindowSize(){
            Vector2f tempVec;
            tempVec.x = refWindow.getSize().x;
            tempVec.y = refWindow.getSize().y;
            return tempVec;
        }

        void engine::drawPart(Drawable &art){
            refWindow.draw( art );
        }

A "master class" is a very close relative to a singleton or a global.  It can easily be a design which violates the Dependency Injection principle, and leaves you with non-modular code.  In this case your engine is also violating the Single Responsibility principle, as it's doing a lot of unrelated tasks:  it's handling windows, providing the game's main driver, moving the ball, drawing the screen, and hosting all kinds of different data.

Try this task:  write a unit test that exercises just the engine::handleWindow() routine without needing to create a real ball, enemy, or player.  It's hard, because your constructor builds all of those objects.

You've already got a start.  For example, you can easily write a unit test that tests engine::eventDraw without creating a real RenderWindow, because you can pass a mock RenderWindow on the engine constructor.  You're using dependency injection.

As you're going to have a singleton in main (and every program does at some level), I recommend you try to keep that singleton as thin as possible.

It's not that your existing code isn't going to work, but it's going to be a problem to maintain, especially as you extend it to add functionality.  Where does scorekeeping come in?  Player preferences?  High score table?  Head-to-head play with two people?  The more you want to add to this, the more you're going to need to change engine, and the less confident you're going to be that you aren't breaking something else.


Backbone.Collection - Ensure that at most one model has property set to true I have a Backbone.Collection which has some models in it. Each model has a boolean property called 'special.' Only one model should be special at any given time.

I've got the following to enforce this, but I'm wondering if it could be clearer or if there is a more appropriate way to enforce such behavior:

//  Ensure that only 1 model is ever special.
this.on('change:special', function (changedModel, special) {

    if (special) {
        this.each(function (model) {
            if (model !== changedModel) {
                model.set('special', false);
            }
        });
    }

});

I don't like this because it seems overly verbose and also gives a period of time where two items are special.

That seems wrong on a few levels.

Slows down every update call
This is pretty much 'magic happens here'
I feel this should be enforced in the UI, is it not too late for this in Backbone?

I would modify the setter to accomplish what you need : https://stackoverflow.com/a/9415926/7602


Running an async function synchronously Sometimes I have the need to call an async function from a non-async function and get the result back synchronously. Therefore I wrote the following helper function to be able to do this in one line.

public static T RunSync<T>(Func<Task<T>> taskConstructor)
{
    var signal = new ManualResetEventSlim();
    T result;
    Exception ex = null;
    ThreadPool.QueueUserWorkItem(async () =>
    {
        try
        {
            var task = taskConstructor();
            result = await task.ConfigureAwait(false);
        }
        catch (Exception iex)
        {
            ex = iex;
        }
        finally
        {
            signal.Set();
        }

    });
    signal.Wait();
    if (ex != null)
        throw ex;
    return result;
}

Is this ok to do or will it behave unexpectedly in some edge cases? I know that writing this kind of async functions in can be tricky and can easily deadlock. That's why I ran it inside a ThreadPool thread which should prevent any kind of deadlock.

I think that this is probably not the most performant way to do it, but preventing deadlocks is more important in my case.

I know that re-throwing an exception is not ideal, but I think this is a compromise I have to make here.

public static class TaskExtensions
{
    private static readonly TaskFactory TaskFactory = new TaskFactory(
            CancellationToken.None,
            TaskCreationOptions.None,
            TaskContinuationOptions.None,
            TaskScheduler.Default);

    public static TResult RunSync<TResult>(this Func<Task<TResult>> asyncFunc)
        => TaskFactory
            .StartNew(asyncFunc)
            .Unwrap()
            .GetAwaiter()
            .GetResult();

    public static void RunSync(this Func<Task> asyncAction)
        => TaskFactory
            .StartNew(asyncAction)
            .Unwrap()
            .GetAwaiter()
            .GetResult();
}

There are several things that worth mentioning:

Async methods were designed to be used all the way. So, if you call an async I/O operation in the bottom layer then it should be be called in an async fashion till the top layer.
Async operations are sensitive for Exceptions. They can behave differently based on how you call them. (They can be swallowed, thrown as an AggregateException or thrown normally. Here the UnWrap + GetAwaiter do the magic for us to be able to handle the exception normally.
In order to avoid deadlocks the async operation is passed to an other Task, that is where TaskFactory.StartNew comes into play.


Filtering Gantt chart data involving hierarchical DTOs I have a bunch of code that deals with modifying a large nested set of IEnumerable<DTO's>

Basically, this is the structure of the DTO's

public class ProductLineDto
{
    ...
    public IEnumerable<ProjectTypeDto> ProjectType { get; set; }
}
public class ProjectTypeDto
{
    ...
    public IEnumerable<ProjectDto> Projects { get; set; }
}
public class ProjectDto
{
    ...
    public IEnumerable<SubProjectsDto> SubProjects { get; set; }
}

public class SubProjectDto
{
    ...
    public IEnumerable<SubProjectDto> Children { get; set; }
    public IEnumerable<ActivityDto> Activities { get; set; }
}

Here is what the code that uses them looks like.

I was wondering if there was a way to clean up the code. I don't like to have to make a copy of every nested IEnumerable to modify the ganttData variable. I was thinking of maybe making an extension method take care of that for me, that way I wouldn't have to throw those two lines of code in for every IEnuemerable
Are there any performance enhancements I can make? For instance, would it be faster to use Select() or Where() instead of RemoveAll()?

public IEnumerable<ProductLineDto> FilterGanttChartData(IEnumerable<ProductLineDto> ganttData, GanttFilterDto ganttFilterDataModel)
{
    ...

    ganttData = GetDataByEndDate(ganttData.ToList(), ganttFilterDataModel);

    return ganttData;
}

    private IEnumerable<ProductLineDto> GetDataByEndDate(List<ProductLineDto> ganttData, GanttFilterDto ganttFilterDataModel)
    {
        if (ganttFilterDataModel.EndDateFrom == null && ganttFilterDataModel.EndDateTo == null)
            return ganttData;

        var gdata = new List<ProductLineDto>();
        gdata.AddRange(ganttData);

        for (var i = 0; i < gdata.Count; i++)
        {
            var pType = new List<ProjectTypeDto>();
            pType.AddRange(gdata[i].ProjectType);

            for (var j = 0; j < pType.Count; j++)
            {
                var projects = new List<ProjectDto>();
                projects.AddRange(pType[j].Projects);

                for (var a = 0; a < projects.Count; a++)
                {
                    var subProjects = new List<SubProjectDto>();
                    subProjects.AddRange(projects[a].Children);
                    subProjects = GetActivitiesByEndDateRange(subProjects, ganttFilterDataModel.EndDateFrom, ganttFilterDataModel.EndDateTo).ToList();
                    projects[a].Children = subProjects;
                }
                pType[j].Projects = projects;
            }

            gdata[i].ProjectType = pType;
        }

        ganttData = gdata;
        return ganttData;
    }

Then the recursive function follows the same logic

    private IEnumerable<SubProjectDto> GetActivitiesByEndDateRange(List<SubProjectDto> children, DateTime? endDateFrom, DateTime? endDateTo)
    {
        var subProjects = new List<SubProjectDto>();
        subProjects.AddRange(children);

        for (var i = 0; i < subProjects.Count; i++)
        {
            var acts = new List<ActivityDto>();
            acts.AddRange(subProjects[i].Activities);

            if (endDateFrom != null)
                acts.RemoveAll(activity => activity.EndDate < endDateFrom);
            if (endDateTo != null)
                acts.RemoveAll(activity => activity.EndDate > endDateTo);
            subProjects[i].Activities = acts;

            if (subProjects[i].Children.Any())
                subProjects[i].Children = GetActivitiesByEndDateRange(subProjects[i].Children.ToList(), endDateFrom, endDateTo);
        }

        children = subProjects;
        return children;
    }

Direction

I tried to rewrite your original code without much luck. After making a little bit of analysis I came to the idea that in order to have a more generic project shaking algorithm, we need a more generic project representation (data structure). Here's what the code looks after my second attempt.

Code

Here's the project tree object that has arbitrary number of subproject children defined as the same type as project itself (see Composite Pattern). The type placeholder T is something that will need some time to design properly, and this we can address separately.

Also, see that we've got the Activities enumerable declared on this level. Which means that even the root of the project hierarchy may have some activities. This is good and bad at the same time: it's good because now there's no artificial limitation applied to the root level; but it's bad because it diverges from what you had originally and if the original limitation is a business rule, you will need an external mechanism to maintain the rule.

public interface IProjectTree<T>
    : IEnumerable<IProjectTree<T>> // !!! The implementation should rely on Children field
{
    IEnumerable<IProjectTree<T>> Children { get; set; }
    IEnumerable<ActivityDto> Activities { get; set; }

    T SubtreeSpecificData { get; set; }
}

Now we can build a Traverse class that implements a generic method ShakeOff which is generalized and can be applied to any part of the tree and with any thinkable predicate on activity object (passed as a parameter).

public static class Traverse
{
    public static IProjectTree<T> ShakeOff<T>(this IProjectTree<T> targetTree, Func<ActivityDto, bool> activityPredicate)
    {
        if (!targetTree.Any())
        {
            targetTree.Children = new List<IProjectTree<T>>();
        }
        else
        {
            targetTree.Children = targetTree
                .Children
                .Select(subTree => subTree.ShakeOff(activityPredicate))
                .ToList();
        }

        targetTree.Children = targetTree.Children.Where(subtree => subtree.ActivityPredicateApplies(activityPredicate));

        return targetTree;
    }

    public static bool ActivityPredicateApplies<T>(this IProjectTree<T> target, Func<ActivityDto, bool> activityPredicate)
    {
        target.Activities = target.Activities.Where(activityPredicate);

        var hasSubtreesAndUnfilteredActivities = target.Any() && target.Activities.Any();
        return hasSubtreesAndUnfilteredActivities;
    }
}

Consumption

The consumer will then look like this:

DateTime fromEndDate = ...;
DateTime toEndDate = ...;
Func<ActivityDto, bool> activityPredicate = activity =>
     activity.EndDate >= fromEndDate && activity.EndDate < toEndDate;

IProjectTree<...> projectTree = null;
projectTree = projectTree.ShakeOff(activityPredicate);

Side notes

But wait, I can't change my DTO classes into IProjectTree. I am pretty sure your DTO structure is shaped by the Database or the service and you want to keep them as they are. This is the hardest part, and you may not like the answer.

You will need to translate your DTOs into IProjectTree<T>. It means, all your DTOs stay unchanged; add the ProjectTree type; and write a class that translates one thing into another (and vice versa). This will require a bit of work. And you will need to find a proper way to define the <T> (the trivial solution would be an object, the SubtreeSpecificData could then point to the source DTO ;) ).

IMO, this would be a better design. Operating directly on DTOs is counter-intuitive to me. DTOs are not supposed to be used as a part of the domain logic, but rather as a dummy objects in client-server communication processes.

Disclaimer

Traverse.ShakeOff() method is not a pure function, it modifies the provided targetTree object in-place. This may be dangerous in some scenarios (but probably, not in your case).
The code above may or may not be easy to apply due to architectural changes -- it's your judgement call. If I were you, I would not limit myself to a rigid unmanageable data structure just because my database/service returns DTOs in this format. As mentioned earlier, generic representation enables generic algorithms which are used a LOT (meaning the effort pays off).
I didn't have much time to test the solution (in fact, I still don't have access to my compiler), so burden of testing is on you.

Update 1

I updated code as per t3chb0t's comment and own observations:

Added IEnumerable<IProjectTree<T>> to IProjectTree<T>.
Replaced .Count() > 0 with .Any() where applicable.
Using extension method subTree.ShakeOff(activityPredicate) instead of an explicit static method invocation.


Python percentage calculator I'm learning Python for 2 months now and I'm working on a little exercise app calculating percentages. I'd like to ask for a code review and comments. I'd like to make sure I'm following the right habits from the beginning.

# coding: utf-8

# In[9]:

def percA(perc, num):
    perc_a = perc/100 * num
    return perc_a

def percB(first, second):
    percB = first/second*100
    return percB

def percC(old, new):
    percC = (new - old)/old*100
    return percC

def percD(num, perc):
    perc = perc/100
    percD = num * (1 + perc)
    return percD

def percE(num, perc):
    perc = perc/100
    percD = num * (1 - perc)
    return percD

print("**************************************")
print("************** WELCOME ***************")
print("**************************************")
print("\n")
choose = input("Choose option:\nA. X% of Y\nB. X is what percent of Y\nC. By what percentage a number increased?\nD. Add percent to a number\nE.Substract percentage from a number\nYour answer: ")
if choose.isupper() == True:
    choose = choose.lower()
while choose == "a":
    if "a":
        usrperc = int(input("Provide percentage: "))
        usrnum = int(input("Provide number: "))
        result = percA(usrperc, usrnum)
        print("{0}% of {1} is {2}".format(usrperc, usrnum, result))
        break
    elif "b":
        usrperc = int(input("Provide percentage: "))
        usrnum = int(input("Provide number: "))
        result = percA(usrperc, usrnum)
        print("{0}% of {1} is {2}".format(usrperc, usrnum, result))
        break
else:
    print("Choose from range A - E")

You can call chose.lower without the if statement without any consequences as far as I know. I don’t have any experience with using something like while choose ==“a”, bit it looks to me like if choose ==“a” would work just the same way, and be more readable.


Show/hide function with multiple div ids I know close to nothing about jQuery, but I swear I'm trying to learn. The thing is, I've got some code that works, but I know it's repetitive and probably not kosher for a real programmer, which is why I've turned to you all.

What I want to do is show/hide (or toggle, whatever you think is best) some informational divs, or so you might call them, on this page.

The show/hide code that I have right now stands at this:

    $(document).ready(function(){

    $('#meet_growlab, #buddy_tv').hide();

    $('a#growlab').click(function(){
    $('#meet_growlab').show('slow');
});

    $('a#growlab_close').click(function(){
    $('#meet_growlab').hide('slow');
})

    $('a#buddytv').click(function(){
    $('#buddy_tv').show('slow');
});

    $('a#buddytv_close').click(function(){
    $('#buddy_tv').hide('slow');
})

});

With the HTML being:

BLAH BLAH BLAH

Close

BLAH BLAH BLAH

Close

Meet GrowLab - Canada’s Y-Combinator Arrives in Vancouver (June 24, 2011)

Building the Web's Best Entertainment-Based Community Site: Andy Liu, CEO and Founder of BuddyTV (April 1, 2011)

It works, but it's not pretty.

You could have the hide/show divs inside a container, and then on .ready hide all its immediate children;
You could then target all a.close inside this container, and on click hide the clicked anchor's grand-grandparent;
And finally target all anchors inside the items list, and on click show the div corresponding to the anchor's rel;
I would also make the anchor's href the same as its rel, so that if any user has JavaScript disabled the anchor still works.

So, HTML:

BLAH BLAH BLAH

Close

BLAH BLAH BLAH

Close

Meet GrowLab - Canada’s Y-Combinator Arrives in Vancouver (June 24, 2011)

Building the Web's Best Entertainment-Based Community Site: Andy Liu, CEO and Founder of BuddyTV (April 1, 2011)

and a more compact and DRY JQuery:

$(document).ready(function(){
  $('#eventDescriptions>div').hide();
  $('#eventTitles a').click(function(){
    var target = $(this).attr("rel");
    $(target).show('slow');
  });

  $('#eventDescriptions .close').click(function(){
    $(this).parent().parent().hide('slow');
  })
});

Here, a JSFiddle: http://jsfiddle.net/QYjLY/

Other things I quickly noticed:

Good job on using rel;
Instead of <img border=0 ... you could use CSS - a img { border: 0;};
#social is a list, so it could be a ul instead of a div.


Right way to handle, log and display exceptions I repost my question here because it has been closed on stack overflow

This question could sound stupid but I am new to the logging, displaying and handling exceptions.
Because before I was just doing a big try catch (Exception ex) to get exceptions (test softwares)

But now I am doing a "real" software, which will have a public release.
And I am asking what is the right way of doing those things.

I am using WPF with the .NET Framework 4.7.2, and i'm also using NLog

For now, I did a little code, and I am just asking like a review / advices.
I put that code at startup, so in the app.xaml.cs with a protected override OnStartup()

using System;
using System.IO;
using System.Windows;
using WPFTemplate.Classes;
using WPFTemplate.Classes.Core;

namespace WPFTemplate {

    public partial class App : Application {

        /// <summary>
        /// Logger
        /// </summary>
        private static readonly NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();

        /// <summary>
        /// Application starting point
        /// </summary>
        protected override void OnStartup(StartupEventArgs e) {

            // ---- Application

            // -- Directory
            try {

                // Create application main directory
                Directory.CreateDirectory(Reference.AppPath);

            } catch (Exception ex) {

                // Log
                Log.Error(ex);

                // The path isn't valid/found (ex: with network name)
                if (ex is DirectoryNotFoundException || ex is IOException || ex is NotSupportedException) {
                    MessageBox.Show(string.Format("The application path '{0}' isn't valid..\nPlease check if the path is valid", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
                }
                // The user hasn't the permission to write (then need to run the application as administrator)
                else if (ex is UnauthorizedAccessException) {
                    MessageBox.Show(string.Format("The application hasn't the right to write data in the application path '{0}'..\nPlease start the application as administrator", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
                }
                // Path is too long (max: 256 chars)
                else if (ex is PathTooLongException) {
                    MessageBox.Show(string.Format("The application path '{0}' is too long..\nPlease install the application in another place", Reference.AppPath), Reference.AppName, MessageBoxButton.OK, MessageBoxImage.Error);
                }

                // Shutdown
                Application.Current.Shutdown();

            }

        }

        protected override void OnExit(ExitEventArgs e) {

            // -- Logs

            Log.Info("Stopping the application");

            NLog.LogManager.Shutdown();

        }

    }

}

I did a custom message for every exceptions, and it's a little bit long

So i'm waiting for your reviews/advices and thanks a lot i'm still a beginner in that x)

The main reason for exceptions (from a developer point of view) is to detect malfunctioning code. Because once this malfunctioning code is executed, the result of an operation and therefore the state of an application can be considered as undefined, since unexpected/unpredictable side effects will transition the application to an unpredictable state.

Data might be in an unpredictable state i.e. data integrity is not guaranteed any more. Unmanaged resources might be initialized or reserved but not released, which may even affect other applications. So most of the time you want the application to stop execution to avoid unpredictable behavior.

Exceptions are designed to hold a program. They are controlled on OS level so that Operating System can stop the application and also provide a snapshot from the stack memory, that was occupied by the faulting application.

We can say that there are expected errors e.g., not enough disk space to save a file or wrong user credentials and there are unexpected errors e.g. a null reference exception.
So if the application could recover/rollback from an error, this error will be an expected error. You know in advance what can fail and you know how to handle this situation to keep the application operating reliably. You preferably would want to handle such errors "silently", without throwing exceptions, as exceptions have impact on application performance and therefore user experience.
You also don't want the exception to propagate through your complete application, which is expensive in terms of performance. Therefore you would like to handle it directly where the exception occurred. This implies that you better don't handle exceptions in a big catch block around your application. You always catch exceptions where they occur. This is a best practice for the reasons explained.

It's also best practice to use multiple catch blocks and catch specific exceptions only. This way your code becomes more readable. Specific exceptions are those and only those you can handle. And logging is not considered handling (if this exception is not relevant you should have avoided it in the first place). Every other exception must be thrown as they are unexpected or have heavy impact on the stability (predictability) of the application.

This means catching Exception, like it is happening in your code, is a very bad idea:

try
{
  // Create application main directory
  Directory.CreateDirectory(Reference.AppPath);
}
catch (Exception ex) // Bad: this will swallow every exception although unhandled by this catch block
{
  Log.Error(ex);

  if (ex is DirectoryNotFoundException || ex is IOException || ex is NotSupportedException)
  {
    MessageBox.Show("Error");
  }
}

Bad exception handling as all exceptions are swallowed. Logging an exception is not handling an exception.

What is happening here, is that all exceptions are swallowed. Especially those that are critical to the application. By catching Exception you basically handle every exception. Catching Exception at application level makes it even worse: a NullReferenceException will never see the light. A IndexOutOfRangeException will never get attention too, as you didn't even re-throw anything (re-throwing an exception is also generally considered as bad practice). A NullReferenceException must crash your application and requires the developer's full attention. If Reference.AppPath throws a NullReferenceException because Reference or AppPath is null we will never notice it without consulting any log file.

In real life, nobody is reading log files. Every developer avoids it. And for sure, nobody reads them daily to check if any of the hundreds of customers experienced a critical runtime error. Indeed, it's an uncomfortable situation when a customer experiences an application crash, but at least it will draw everybody's attention to look for an immediate fix. An application error reporter can be a very powerful enhancement to provide quick and proper bug support.
Nevertheless log files can be a very valuable support to reproduce and find bugs.

Also, exceptions of the same type might get thrown at different code segments in a totally different context. A global exception handler will have a hard time to get the context of the exception to find out if NotSupportedException was thrown in context of a invalid user input (recoverable error) or a wrong call to an API method (non-recoverable).

To solve the issue of swallowing exceptions, you have to get rid of the smelly switch statement, which already indicates that you are doing something "wrong". Type checking in a switch statement (or ugly and unreadable if-else constructs) is a classic code smell.

The goal is to handle exceptions that are non-critical i.e. the application can recover from and leave every other exception unhandled:

try
{
  // Create application main directory
  Directory.CreateDirectory(Reference.AppPath);
}
catch (DirectoryNotFoundException ex)
{
  // Log
  Log.Error(ex);

  MessageBox.Show("Error");
}
catch (UnauthorizedAccessException ex)
{
  // Log
  Log.Error(ex);

  MessageBox.Show("Error");
}

Now, any developer that comes across this code knows immediately what is going on here. The code has become more readable and the ugly and smelly switch was successfully eliminated. But more important, every unhandled exception will still crash the app, to indicate a critical bug. If Reference.AppPath throws a NullReferenceException we will now notice it first hand this time, without the need of consulting any log file.

Don't catch what you can't handle.

If you wish to log unhandled exceptions, instead subscribe to the Application.DispatcherUnhandled event:

public partial class App : Application
{
  public App()
  {
    this.DispatcherUnhandledException += (sender, args) => this.logger.Error(args.Exception.Message);
  }
}

Free Resources

As a note: it's best practice to take care of managed/unmanaged resources and use finalizers i.e. finally or using blocks, to make sure that these resources are released i.e. disposed properly under any circumstances before letting the application crash.

In case the following code throws an exception, the using statement will execute a finalizer before the forced exit:

using (var sqlConnection = new SqlConnection(args))
{
  sqlConnection.ExecuteQuery(queryArgs);
}

This is equal to:

try
{
  var sqlConnection = new SqlConnection(args);
  sqlConnection.ExecuteQuery(queryArgs);
}
finally
{
  sqlConnection.Dispose();
}

Both implementations won't handle the critical exception, but will release unmanaged resources before the method is put on hold and the exception routing is executed by the OS.

Re-throwing Exceptions

Although re-throwing exceptions is considered bad practice, it makes sense if you need to add data to the exception or like to throw a more specialized maybe custom exception type. But make sure to keep the stack trace intact.

Bad re-throw as it will break the stack trace:

try
{
  // Create application main directory
  Directory.CreateDirectory(Reference.AppPath);
}
catch (DirectoryNotFoundException directoryNotFoundException)
{
  Log.Error(directoryNotFoundException);

  // The previous stack trace of 'directoryNotFoundException' will be lost
  throw new UnknownUserInputException("Custom Message");
}

Good re-throw as it will preserve the full stack trace by setting
the original exception as inner exception of the wrapping exception type:

try
{
  // Create application main directory
  Directory.CreateDirectory(Reference.AppPath);
}
catch (DirectoryNotFoundException directoryNotFoundException)
{
  Log.Error(directoryNotFoundException);

  // Use constructor to set inner exception
  throw new UnknownUserInputException("Custom Message", directoryNotFoundException);
}

Avoiding Exceptions in the First Place

Microsoft best practices recommends to "Design classes so that exceptions can be avoided".
This is especially true for user input, since this are always errors we can recover from. In general, data input whether the source is a user or a client application, can be validated before it enters the application. The goal is to have the application to deal with valid data only. The logic should not process invalid data and throw an exception. The data persistence layers should never persist invalid data.

To prevent invalid data from being processed, you would typically use a form of data validation. In case of a WPF application, which is what you are implementing, I highly recommend to apply the MVVM pattern and implement the INotifyDataErrorInfo interface (example) which is part of the WPF framework and enables error propagation to the UI to provide a visual feedback. If the user enters an invalid file path you can easily make the UI draw e.g. a red border around the concerning input field, with messages or hints, forcing the user to correct the input.

In this special scenario of picking a file or folder, the best way is to use a file or folder picker dialog. This way, invalid filesystem path inputs are almost impossible.

Another example is file saving. Before saving a file, you can check whether the operation can be executed by checking the available disk space to avoid I/O exceptions.

Almost all expected exceptions can be avoided by checking the preconditions before executing a critical operation.

The bottom line is, that you should never catch Exception. Swallowing exceptions is dangerous as it will conceal serious bugs. To log an exception is not handling an exception. Logging an exception and done is equal to swallowing it, as you can't be sure somebody will ever read the log data or search it for random errors.
If you wish to log all unhandled exceptions by default, subscribe to the Application.DispatcherUnhandledException event, but never use a global catch block at application level.
Especially in your case where the errors or exceptions are triggered by invalid user input, you are advised to use data validation. This makes exception handling obsolete and is the preferred way. User input validation e.g., by implementing INotifyDataErrorInfo, includes a convenient way to give the user feedback and prompt him to correct the input.
In case the exception is not critical, in a away that it requires the application to halt, consider to roll it back to a defined state before continuing execution in order to prevent dirty data or undefined states.
Don't forget to free resources using a finally or using block to properly close/dispose those objects.


Matching entities in SQL Server to replace iterative Python script I have a SQL (MS SQL Server) database of ~22 million companies (called target_db in my code). For example:

+-----------------------+----------------+-----------+
|     CompanyName       |    ReFcode     | ID_number |
+-----------------------+----------------+-----------+
| Mercedes Benz Limited | Germany        |     12345 |
| Apple Corporation     | United States  |     67899 |
| Aunt Mary Butcher     | United Kingdom |     56789 |
+-----------------------+----------------+-----------+

Then, I have another list of companies (called input in my example) and I would like to assign ID_number based on approximate company name match. The size of my sample is 1000 companies but can be much more.

+--------------------+----------------+
|      name          |    ReFcode     |
+--------------------+----------------+
| Mercedes Benz Ltd. | Germany        |
| Apple Corp.        | United States  |
| Butcher Aunt Mary  | United Kingdom |
| Volkswagen Gmbh    | Germany        |
+--------------------+----------------+

I wrote a script in Python that does the following for each of the companies in my sample:

Connect to the database and filter the target_db to only get the names that start with the same 3 letters, have similar length (+- 7 characters) and Soundex DIFFERENCE is 4
Calculate levenshtein with all possible matches (using fuzzywuzzy library)
Choose the match with the highest similarity

This worked mostly fine and accomplished the task in around 6 minutes. I did some profiling and noticed that 5 minutes and 30 seconds was spent on connecting to the DB (in every iteration of the loop), running query and fetching the results. The rest was calculating the similarity. Which I found quite surprising.

I thought that I could save time and improve performance by doing the process entirely in SQL, so I started writing this query:

SELECT distinct input.name,
                target_db.CompanyName,
                input.Country,
                input.ReFcode,
                [dbo].[edit_distance](input.name, target_db.CompanyName) as levenshtein
FROM dbo.Sample as input
JOIN dbo.Company as target_db
on
    (input.ReFcode = target_db.Refcode and
    LEFT(input.name, 3) = LEFT(target_db.CompanyName, 3) and
    SOUNDEX(input.name)=SOUNDEX(target_db.CompanyName))
WHERE ABS( LEN(input.name) - LEN(target_db.CompanyName)) <= 7

ORDER BY levenshtein asc

Then I would take the match with the lowest EditDistance (Levenshtein) below a certain threshold. This works but the process now is taking almost 30 minutes. What am I doing wrong? Any suggestions on how to improve it?

Without knowing exactly what your old code was it is hard to pinpoint where the slowdown came from. That being said, here are some things you're doing in your query that are going to make it slower:

Using a UDF prohibits parallel plans. You could throw 1000 CPUs at this problem and it would still only use one of them. If computing this is too complex to do inline, then use an inline table-valued function instead, which enables parallelism.
JOINing on functions (UDF or otherwise) performs really poorly. SQL Server has no cardinality estimate for LEFT(input.name, 3), but it does for input.name. If at all possible find another way to do this join (a suggestion is below). Similarly, filtering in a WHERE clause has the same restrictions.

To address this, I would likely change your join to just be on RefCode - this gets you a good hash match (probably), but will increase your granularity. Then, make your edit_distance function table valued, like so

SELECT <<stuff>>
  FROM dbo.Sample input
    INNER JOIN dbo.Company target_db
      ON input.ReFcode = target_db.Refcode
    CROSS APPLY dbo.edit_distance( input.name, target_db.CompanyName ) levenshtein

Then, inside of dbo.edit_distance I would include the LEFT, SOUNDEX and LEN calculations (LEN calc would also be better if you did LEN( input.name ) - LEN( targetdb.CompanyName ) BETWEEN -7 AND 7).

If levenshtein returns some score, then you could do something like this to only get the best score per input name. Alternatively if you want them all, then have it return everything and do whatever calculations the consuming app needs.

SELECT name,
       CompanyName,
       Country,
       RefCode,
       levenshtein_score
  FROM ( SELECT input.name,
                target_db.CompanyName,
                input.Country,
                input.ReFcode,
                levenshtein.levenshtein_score,
                ROW_NUMBER() OVER ( PARTITION BY input.name
                                    ORDER BY levenshtein.levenshtein_score ASC ) RowNumber
           FROM dbo.Sample input
             INNER JOIN dbo.Company target_db
               ON input.ReFcode = target_db.Refcode
             CROSS APPLY dbo.edit_distance( input.name, target_db.CompanyName ) levenshtein ) ComputedLevenshteinScores
  WHERE ComputedLevenshteinScores.RowNumber = 1
  ORDER BY levenshtein ASC;


Generic getting single value from DB in C# Few months ago I posted my code Getting a single value from the DB. I implemented suggested changes and this is how it looks like right now:

public class DataBase : Page
{

    protected static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    protected static string ConnectionString;

    public DataBase()
    {
        ConnectionString = GetConnectionString();
    }

    public static String GetConnectionString()
    {
        return ConfigurationManager.ConnectionStrings["abc"].ConnectionString;
    }

    public static T GetValue<T>(string query)
        where T : IComparable, IConvertible, IEquatable<T>
    {
        Object value = GetValue(query);
        if (Convert.IsDBNull(value))
            return GetDefaultValue<T>();
        return (T)Convert.ChangeType(value, typeof(T));
    }

    public static T GetDefaultValue<T>()
        where T : IComparable, IConvertible, IEquatable<T>
    {
        if (typeof(T) == typeof(String))
            return (T)(object)String.Empty;
        return default(T);
    }

private static Object GetValue(string query)
    {
        try
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                connection.Open();
                return command.ExecuteScalar();
            }
        }
        catch (Exception e)
        {
            LogQueryError(query, e);
            return DBNull.Value;
        }
    }

protected static void LogQueryError(string query, Exception e)
    {
        log.Error(string.Format("Error while executing Query ({0}): {1}", query, e.Message));
    }
}

One explanation. The purpose of where T : IComparable, IConvertible, IEquatable<T> is to have single method for value types and strings. (inspired by C# Generic constraints to include value types AND strings

What do you think about this piece of code?

Something smells hinky having this class inherit from something called Page. Like it's mixing UI and data concerns where it shouldn't. What does Page give this class?
C# convention is to use language aliases object and string over the CLR types Object and String, respectively.
Also along the lines of convention, the word is "database", not "data base" and therefore the class should not be named DataBase, but rather Database.
You have two particular dependencies in this class: a connection string and a logger. I'd recommend inverting those dependencies and injecting them into the class at time of construction.
Better yet, there are other dependencies, SqlConnection and SqlCommand being created in the GetValue method. These may best be refactored into another class injected into this one.
Class member variables should always be private. If you need them exposed to the outside, or subclasses, use properties to control access.
Use var where possible.

So, here's a cut at that:

IDatabaseAdapter interface:

public interface IDatabaseAdapter
{
    IDbConnection GetConnection();

    IDbCommand GetCommand(IDbConnection connection, string query);
}

DatabaseAdapter implementation:

public class DatabaseAdapter : IDatabaseAdapter
{
    private readonly string _ConnectionString;

    public DatabaseAdapter(string connectionString)
    {
        this._ConnectionString = connectionString;
    }

    public IDbConnection GetConnection()
    {
        return new SqlConnection(this._ConnectionString);
    }

    public IDbCommand GetCommand(IDbConnection connection, string query)
    {
        var command = new SqlCommand(query, connection as SqlConnection);

        connection.Open();
        return command;
    }
}

Database class:

public class Database
{
    private readonly ILog _Log;

    private readonly IDatabaseAdapter _DatabaseAdapter;

    public Database(ILog log, IDatabaseAdapter databaseAdapter)
    {
        this._Log = log;
        this._DatabaseAdapter = databaseAdapter;
    }

    public string ConnectionString
    {
        get
        {
            return this.ConnectionString;
        }
    }

    protected ILog Log
    {
        get
        {
            return this._Log;
        }
    }

    public T GetValue<T>(string query)
        where T : IComparable, IConvertible, IEquatable<T>
    {
        var value = this.GetValue(query);

        return Convert.IsDBNull(value) ? GetDefaultValue<T>() : (T)Convert.ChangeType(value, typeof(T));
    }

    public static T GetDefaultValue<T>()
        where T : IComparable, IConvertible, IEquatable<T>
    {
        return typeof(T) == typeof(string) ? (T)(object)string.Empty : default(T);
    }

    private object GetValue(string query)
    {
        try
        {
            using (var connection = this._DatabaseAdapter.GetConnection())
            using (var command = this._DatabaseAdapter.GetCommand(connection, query))
            {
                return command.ExecuteScalar();
            }
        }
        catch (Exception e)
        {
            this.LogQueryError(query, e);
            return DBNull.Value;
        }
    }

    protected void LogQueryError(string query, Exception e)
    {
        this._Log.Error(string.Format("Error while executing Query ({0}): {1}", query, e.Message));
    }
}

Sample calling code:

internal static class Program
{
    private static readonly DataBase _Database = new DataBase(
        LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType),
        new DatabaseAdapter(ConfigurationManager.ConnectionStrings["abc"].ConnectionString));

    private static void Main()
    {

    }
}

As a final note, you'll likely want to check for null or empty strings in constructors and raise appropriate exceptions then to keep the state of the objects well-known.


ADT Stack with LinkedList I am currently preparing for my exam and I am trying to implement some abstract data types with linked lists as preparation for the exam. I found an older exam from my prof. and he wants us to implement an ADT stack with an inner class ListElement.

He has given us the interface, so it doesn't need to be implemented. He wants us to use his given methods, so we are not allowed to change them. This is what I did and I am not sure whether it is right or wrong.

public class Stack<T> implements ADTStack<T> {
    private ListElement<T> firstElement;
    int size = 0;

    public Stack() {
        firstElement = null;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }

    @Override
    public void push(T element) {
        if (empty()) {
            firstElement = new ListElement(element);
            size++;
        }
        else {
            firstElement.nextElement = firstElement;
            firstElement = new ListElement(element);
            size++;
        }
    }

    @Override
    public void pop() {
        if (empty()) {
            throw new RuntimeException("stack is empty");
        }
        else {
            T element = top();
            firstElement = firstElement.nextElement;
            size--;
        }
    }

    @Override
    public T top() {
        if (empty()) {
            return null;
        }
        else {
            return firstElement.element;
        }
    }

    @Override
    public boolean empty() {
        return (firstElement == null);
    }

    @Override
    public int size() {
        return size;
    }

    public class ListElement<T> {
        private T element = null;
        private ListElement<T> nextElement = null;

        public ListElement(T element) {
            this.element = element;
        }

        public T getElement() {
            return element;
        }

        public ListElement<T> getNextElement() {
            return nextElement;
        }

        public void setNextElement(ListElement<T> element) {
            this.nextElement = nextElement;
        }
    }
}

Don't use bare types

It seems you forgot to specify the type parameter here:

firstElement = new ListElement(element);

It should have been (as of Java 7):

firstElement = new ListElement<>(element);

Avoid duplicated logic

Notice the duplicated logic here:

if (empty()) {
    firstElement = new ListElement<>(element);
    size++;
} else {
    firstElement.nextElement = firstElement;
    firstElement = new ListElement<>(element);
    size++;
}

size++ appears at the end of both branches: it could be done after the conditional.

The assignment firstElement = new ListElement<>(element); is right before size++ at the end of both branches, so that too can be done after the conditional.

Without these duplications, the code becomes simpler:

if (!empty()) {
    firstElement.nextElement = firstElement;
}
firstElement = new ListElement<>(element);
size++;

Unnecessary statements

In pop, this statement is unnecessary, you can delete it:

T element = top();

And since the if branch throws an exception,
you can eliminate the else branch, like this:

public void pop() {
    if (empty()) {
        throw new RuntimeException("stack is empty");
    }
    firstElement = firstElement.nextElement;
    size--;
}

Follow good examples in the JDK

Take a look at the pop and peek methods of the Javadoc of Stack:
when the stack is empty, they both throw EmptyStackException.
I advise you do likewise.

Working with inner classes

The inner class ListElement holds a reference to the enclosing class
(as all non-static inner classes do).
In your example, this is unnecessary.
Make this inner class static, so that it becomes independent from the enclosing class.
This improves performance,
and also solves another problem that the type parameter T in ListElement<T> shadows the type parameter T of the enclosing Stack<T>.

Keep your toolbox clean

A Stack is a utility class,
potentially a useful element in your programming toolbox.
This method doesn't belong in it:

public static void main(String[] args) {
    // TODO Auto-generated method stub
}

The method does nothing, so it's pointless.
It has a default TODO comment,
which you should always delete when you're done working,
for example before asking for a code review.
If later you want to demonstrate the functionality of the stack by creating a runnable class with a main method,
do it in another class (say, StackDemo), not in this one.