Scraping lunar/solar eclipse data with BeautifulSoup I created a script to scrape this website and gather the eclipse data (date, type, and location):

class Eclipse():

    def __init__(self, eclipse_date, eclipse_type, eclipse_location):
        self._eclipse_date = eclipse_date
        self._eclipse_type = eclipse_type
        self._eclipse_location = eclipse_location

    @property
    def get_eclipse_date(self):
        return self._eclipse_date

    @property
    def get_eclipse_type(self):
        return self._eclipse_type

    @property
    def get_eclipse_location(self):
        return self._eclipse_location

    def __str__(self):
        return "Date: {0} Type {1} Location: {2}" .format(self._eclipse_date,self._eclipse_type,self._eclipse_location)

Main class:

import requests
from bs4 import BeautifulSoup
from Eclipse import Eclipse
from collections import namedtuple

def generate_eclipse_data(eclipse_info):

    eclipse_list = []

    for info in eclipse_info:
       eclipse_date_and_type = info.find('a').text
       retrieved_date_and_type = generate_date_and_type(eclipse_date_and_type)
       eclipse_location = info.find('p').text
       retrieved_location_data = generate_location(eclipse_location)
       eclipse_data = Eclipse(retrieved_date_and_type.date,retrieved_date_and_type.type,retrieved_location_data.location)
       eclipse_list.append(eclipse_data)
    return eclipse_list

def generate_location(info_to_clean):
   clean_location = namedtuple("Clean_Location",["location"])
   cleanup_location = info_to_clean.split(" New Features")[0]
   location_data = clean_location(cleanup_location)
   return location_data

def generate_date_and_type(info_to_split):
   date_and_type = namedtuple("Date_Type", ["date", "type"])
   eclipse_split_data = info_to_split.split("—")
   eclipse_date = eclipse_split_data[0]
   eclipse_type = eclipse_split_data[1].strip()
   eclipse_data = date_and_type(eclipse_date,eclipse_type)
   return eclipse_data

def write_data_to_file(filename, data):
    with open(filename, 'w') as eclipse_writer:
        for info in data:
            eclipse_writer.write(str(info))
            eclipse_writer.write("\n")

def main():

    eclipse_time_date = requests.get("https://www.timeanddate.com/eclipse/")
    soup = BeautifulSoup(eclipse_time_date.text, 'html.parser')
    eclipse_info = soup.find_all("div", class_= "six columns art__eclipse-txt")
    eclipse_filename = 'eclipse.txt'

    if (len(eclipse_info) > 0):
        eclipse_data =  generate_eclipse_data(eclipse_info)
        write_data_to_file(eclipse_filename, eclipse_data)
    else:
        print("No data available")

if __name__ == "__main__":
    main()

Output:

Date: July 13, 2018
Type: Partial Solar Eclipse
Location: South in Australia, Pacific, Indian Ocean

Date: July 27, 2018
Type: Total Lunar Eclipse
Location: Much of Europe, Much of Asia, Australia, Africa, South in North America, South America, Pacific, Atlantic, Indian Ocean, Antarctica

Questions:

I tried to be as descriptive as I can when naming my variables and functions. Are they clear enough?
Can I improve the script any further?

Regarding the Eclipse class:

Using "eclipse" in names is redundant. Beware though, that date and type are would not be good names since they both usually have a different meaning. A common pattern (but also unsatisfying) is to add an underscore after the name as in type_ to use special words as a variable or parameter name.
In languages like Java I would probably make the properties private, but doing so in Python is much more of a maintainability trade-off as you've probably noticed. Avoiding the @property methods and naming the properties the same as the constructor parameters simplifies the code quite a bit.
This class is only a container, with no logic and only a little bit of string formatting. The argument could be made that the code would be a fair bit simpler if it was replaced with a dict.
You don't actually use any of the get_eclipse_ methods.

Regarding the main file:

Many of the names, like eclipse_info, info_to_clean and data are vague, and don't encapsulate what they contain. In some cases you'll find that this is because they are rather arbitrary middle steps between two more cleanly defined things, in which case their processing might be improved by splitting differently, or that they really can be named more clearly in context. For example, the eclipse_data passed as data to write_data_to_file is actually a list of strings, or more accurately a list of Eclipses rather than an arbitrary binary blob.
In generate_date_and_type you can merge several lines into one:

eclipse_date, eclipse_type = info_to_split.strip().split("—")

Rather than writing the results to a file, consider simply printing them. This makes your tool easier (and faster) to interact with using other shell tools, and it's still trivial to write the results directly to a file using a redirect.

General:

This code could benefit from type hinting. This will also often emphasize names which should be changed.
If I were working on this I might use an IDE to squish the entire program into a single function and see if there are any obvious classes or methods which show themselves. When building a larger system there is a lot of value to splitting and abstracting, but now that you have a working program it looks possible to squeeze the whole thing into about 20 comprehensible lines.


Improve my Task Loops How can I improve up this code?
Is it ok to write code using Invoke and Action so liberally or is this bad?

Performance is not an issue as I'm not using Invoke 50,000x in a a row (a lot of other things will be done in-between, making the performance hit null).

The primary goal is to run my list of tasks either by Item first or task first. The secondary goal is to NOT write a massively long method where each method is written out in each way they can be completed. Doing so makes adding new tasks later much easier.

Code: (Updated with pseudo extras)

public class TaskObject
{
    public bool RunPerItem = true; //PLACEHOLDER, normally derived externally from view model
    public bool RunPerTask = false; //PLACEHOLDER, normally derived externally from view model

    public Dictionary<string, Item>() TaskItems = new Dictionary<string, Item>()
    {
        { "ItemA", new Item() {} },
        { "ItemB", new Item() {} },
        { "ItemC", new Item() {} },
    }

    private List<Action<TaskObject, Item>> Tasks = new List<Action<TaskObject, Item>>()
    {
        (TO, I) => { TO.TaskA(I); },
        (TO, I) => { TO.TaskB(I); },
        (TO, I) => { TO.TaskC(I); }
    };

    public void RunTasks()
    {
        //RunPerItem & RunPerTask are type `bool`
        if (this.RunPerItem)
        {
            foreach (var I in this.TaskItems)
            {
                foreach (var T in this.Tasks)
                {
                    T.Invoke(this, I.Value);
                }
            }
        }
        else if (this.RunPerTask)
        {
            foreach (var T in this.Tasks)
            {
                foreach (var I in this.TaskItems)
                {
                    T.Invoke(this, I.Value);
                }
            }
        }
    }

    public void TaskA(Item I) { /*...*/ }
    public void TaskB(Item I) { /*...*/ }
    public void TaskC(Item I) { /*...*/ }
}

public class Item
{
    public string ItemName { get; set; }
}

Thanks for any advice/help!!!

Edit/Clarification/Extra Details:

The above code describes 2 different loops with different orders of task completion.

Example Orders:
this.RunPerItem
ItemA
-TaskA
-TaskB
-TaskC

ItemB
-TaskA
-TaskB
-TaskC

this.RunPerTask
ItemA
-TaskA

ItemB
-TaskA

I don't know if you can, depending on your real world needs... but if you can convert the TaskItems Dictionary to a List of KeyValuePairs like this...

public List<KeyValuePair<string, Item>> TaskItems = new List<KeyValuePair<string, Item>>
{
    new KeyValuePair<string, Item>( "ItemA", new Item() {} ),
    new KeyValuePair<string, Item>( "ItemB", new Item() {} ),
    new KeyValuePair<string, Item>( "ItemC", new Item() {} ),
};

Then you can use the ForEach() exstension method to reduce the loop code to...

if (this.RunPerItem)
{
    TaskItems.ForEach(I => Tasks.ForEach(T => T(this, I.Value)));
}
else if (this.RunPerTask)
{
    Tasks.ForEach(T => TaskItems.ForEach(I => T(this, I.Value)));
}

If you couple that with @Steven Jueris' idea of splitting them into their own methods, your code will be very clean.


Advanced CSV-to-SQLite converter I just wrote an advanced CSV parser that translates CSV to SQL(SQLite Compatible) statements, which are then inserted into the database. It translates variable names in the CSV to values defined in the script.

Example CSV

0,1,An empty black tile,${ASSET_PATH}/BlackTile.png
1,1,Grassy Tile,${ASSET_PATH}/Grass.png
2,1,Lava Tile,${ASSET_PATH}/Lava.png
3,1,Stone Brick Texture,${ASSET_PATH}/StoneBrick.png

The code

# Variables that are used in the CSV files
ASSET_PATH=~/spacegame/assets

# $1 - File to read. Named same as table to insert to plus .csv
# $2 -
function parseCsvAdv2Db {
    local oldIFS=$IFS
    local table=$(echo $1 | cut -d'.' -f 1)
    local ins="INSERT INTO $table$2 VALUES"
    IFS='|'
    while read line
    do
        # Preprocess the line
        local data=$(eval echo "$line" | \
            awk 'BEGIN { FS=","; OFS="|"; } { $1=$1; print $0; }')
        local tmpdata=\(
        for field in $data
        do
            tmpdata+="'$field'"
        done
        tmpdata+=')'
        ins+="$tmpdata"
    done < $1

    ins=$(echo $ins | sed -e 's/)(/),(/g' -e "s/''/','/g")
    ins+=';'

    sqlite3 $dbfile "$ins"

    # Restore state
    IFS=$oldIFS
}

parseCsvAdv2Db test.csv '(id,type,descr,path)'

Your CSV parse is somewhat simplistic, it assumes the comma's are "simple" (no quoted commas, etc. This is not a bad thing, just an observation, but it leads on to the next thing... the cumbersome splitting and rejoining of the field values using awk, etc. I have a confession to make here, I dislike using awk. To clarify, there are often simple ways to do things, and then there are times the simple routes are not enough. You don't need awk for the simple stuff, and you need perl, or python for the complciated stuff. I like the concept of awk, I just don't like having to know three tools when two is enough.

So, I immediately look at your code and think "simplify" it.

If you are using a simple CSV parse, you may as well do it all at once, and use a simple regex and expand your sed use....

I assume you are running on a reasonably recent linux version.

the basename program has the suffix argument: basename -s .csv file/to/test.csv will return test (the base name of the file with the suffix .csv removed).
you are tagged as bash so I will recommend here-strings instead of the echo commands you use. Instead of $(echo $1 | cut -d'.' -f 1) do $(cut -d'.' -f 1 <<< "$1"). It saves an echo.
you should have a shebang line #!/bin/bash as the first line.

So, to strip out all the awk, and more, I would have:

function parseCsvAdv2Db {
    local table=$(basename -s .csv $1)
    local ins="INSERT INTO $table$2 VALUES"
    local data=""

    while read line
    do
        # expand variable names inside the line.
        line=$(eval echo $line)

        # add parenthesis and quotes to the data.
        # Internal quotes still missing.
        data+="('$line')"
    done < $1

    # Add internal quotes, and add comma between value sets.
    data=$(sed -e "s/,/','/g" -e 's/)(/),(/g' <<< "$data")

    ins+="$data;"

    sqlite3 $dbfile "$ins"
    #echo $ins

}

By sacrificing the per-field parsing from awk you instead gain the simplicity of bulk modifications of the comma values using sed, to expand them to quote their respective values. Combined with adding the surrounding ('...') structure for each line, it just works....


JQuery code to toggle an edit form I usually find the .js files to be quite messy and the truth is that mine are even worse than the average as, being soft, I'm not the sharpest tool neither at jQuery nor JavaScript.

I would really appreciate if someone could give me a heads up about improving the readability (and point me bad practices) of this code. I would try to have them into account for other .js files.

$(function () {
    $('form').on("click", "div.toggler", function () {
        $(this).next().slideToggle(300);
    });

$('a.edit').click(function (e) {
    var clickedElement = $(this).parent();
    var url = $('#editForm').data('amp-url');

    $.get(url, { id: parseInt($(this).attr('id')) }, function (result) {
        $("#editForm").html(result);

        // Display edit form just below the "item" clicked
        if ($("#editForm").is(":visible")) {
            $('#editForm').slideToggle(300, function() {
                $("#editForm").appendTo(clickedElement);
                $('#editForm').slideToggle(300);
            });
        } else {
            $("#editForm").appendTo(clickedElement);
            $('#editForm').slideToggle(300);
        }
    }, "html");
    e.preventDefault();
});

// Hide edit form when click outside it
$(document).mouseup(function (e) {
    if ($('#editForm').is(":visible")) {
        var container = $("#editForm");

        if (container.has(e.target).length === 0) {
            container.slideUp(300);
        }
    }
});

(function (a) {

    jQuery.fn.screencenter = function () {
        this.css("position", "absolute");
        this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
        this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
        return this;
    };
})(jQuery);
});

By the way, I have seen here that deferred objects seems cleaner (apart of other considerations). I couldn't make it work, though.

As Dagg said in his comment, try to cache things as much as possible - I haven't tested this code but you could do something like this:

(function ($, window) {
    // Do stuff when the DOM is ready.
    $(function () {

        $('form').on("click", "div.toggler", function () {
            $(this).next().slideToggle(300);
        });

        $('a.edit').click(function (e) {
            var clickedElement = $(this).parent(),
                editForm = $('#editForm'), // cache the edit form element.
                url = editForm.data('amp-url');

            $.get(url, {
                id: parseInt(this.id, 10) // Always specify the radix parameter
            }, function (result) {
                editForm.html(result);

                // Display edit form just below the "item" clicked
                if (editForm.is(":visible")) {
                    editForm.slideToggle(300, function () {
                        editForm.appendTo(clickedElement);
                        editForm.slideToggle(300);
                    });
                } else {
                    editForm.appendTo(clickedElement);
                    editForm.slideToggle(300);
                }
            },
                "html");
            e.preventDefault();
        });

        // Hide edit form when click outside it
        $(document).mouseup(function (e) {
            var container = $("#editForm");
            if (container.is(":visible")) {
                if (container.has(e.target).length === 0) {
                    container.slideUp(300);
                }
            }
        });
    });

    // Add the screen center function to jQuery.
    $.fn.screencenter = function () {
        var w = $(window);
        this.css("position", "absolute");
        this.css("top", ((w.height() - this.outerHeight()) / 2) + w.scrollTop() + "px");
        this.css("left", ((w.width() - this.outerWidth()) / 2) + w.scrollLeft() + "px");
        return this;
    };
}(jQuery, window));

This avoids having to hit the DOM each time, for example, you had $('window') four times... It's much better to get it once and store it in a variable.

I've also wrapped everything in an IIFE (Immediately Invoked Function Expression).

I'm not sure what you were trying to do here:

(function (a) {
    // code omitted.
})(jQuery);

But effectively you are passing the jQuery object into a function aliased as a. Then you are not using a but the jQuery object anyway - a bit pointless.

Edit:
Regarding the deferred object question:

var idToSend = 10;
var request = $.get(url, {
    id:  idToSend
});

// Assign handlers for events (can also chain):
request.done(function (result) {
    // Code for success.
}).fail(function (result) {
    // Code for fail
});


Retrieve Father and Child structure in a Database Table I have a table with this schema:

CREATE TABLE [td].[MyTable] (
     [ID] [int] NOT NULL
    ,[FatherID] [int] NULL
)

(Note: I have excluded all the columns not relevant to the discussion)

I receive an [ID] as input, and I need to collect the relative record and all of its fathers in one single output.
Every record has the id of the Father stored in the [FatherID] column. There is only one root element and it is recognized when we found the [FatherID] = 0.

This is the current full working query:

IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL DROP TABLE #MyTempTable

DECLARE @ID CHAR(11)

SET @ID = 13192

SELECT *
INTO #MyTempTable
FROM [MyTable]
WHERE [ID] = @ID

SELECT @ID = [FatherID]
FROM #MyTempTable
WHERE [ID] = @ID

WHILE @ID <> 0
BEGIN
    INSERT INTO #MyTempTable
    SELECT *
    FROM [MyTable]
    WHERE [ID] = @ID

    SELECT @ID = [FatherID]
    FROM #MyTempTable
    WHERE [ID] = @ID
END

SELECT *
FROM #MyTempTable
ORDER BY [ID]

The perfomance are fine, but I want improve the readability. Any suggestions?

Firstly, you use both the

SELECT [XYZ]
INTO [ABC]

and the

INSERT INTO [ABC]
SELECT [XYZ]

syntax in the same query, using one would make the query much more consistent and easier to read. Personally, I prefer using INSERT INTO [ABC] SELECT [XYZ], you do have to explicitly create the temporary table though, but that makes it usually clearer what is happening.

If you'd prefer, you can change

DECLARE @ID CHAR(11)
SET @ID = 13192

to

DECLARE @ID CHAR(11) = 13192

And you should really explicitly drop the #MyTempTable at the end of the query.

Lastly, the biggest change I would suggest is to completely remove the selects from outside of the while, they are completely redundant. If you remove those selects from outside the while loop, then the first iteration of the while does exactly the same as the selects on the outside would have done.

Here is what we end up with:

IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL DROP TABLE #MyTempTable

DECLARE @ID CHAR(11) = 13192

CREATE TABLE #MyTempTable(ID INT, FatherID INT)

WHILE @ID <> 0
BEGIN
    INSERT INTO #MyTempTable
    SELECT *
    FROM [MyTable]
    WHERE [ID] = @ID

    SELECT @ID = [FatherID]
    FROM #MyTempTable
    WHERE [ID] = @ID
END

SELECT *

FROM #MyTempTable
ORDER BY [ID]

DROP TABLE #MyTempTable

Here is an SQL Fiddle.


Extracting emails from log files I have huge number of text files, each several hundred MB in size. Unfortunately, they are not all fully standardized in any one format. Plus there is a lot of legacy in here, and a lot of junk and garbled text. I wish to check all of these files to find rows with a valid email ID, and if it exists then print it to a file named the first-char of the email ID. Hence, multiple text files get parsed and organized into files named a-z and 0-9. In case the email address starts with a special character, then it will get written into a file called "_" underscore. The script also trims the rows to remove whitespaces; and replaces single and double quotes (this is an application requirement)

My script works fine. There are no errors or bugs in it. But it is incredibly slow. My question: is there a more efficient way to achieve this? Parsing 30 GB logs takes me about 12 hrs - way too much! Will grep/cut/sed/another be any faster?

Sample txt File

!bar@foo.com,address
#john@foo.com;address
john@foo.com;address µÖ
email1@foo.com;username;address
email2@foo.com;username
  email3@foo.com,username;address   [spaces at the start of the row]
 email4@foo.com|username|address   [tabs at the start of the row]

My Code:

awk -F'[,|;: \t]+' '{
    gsub(/^[ \t]+|[ \t]+$/, "")
    if (NF>1 && $1 ~ /^[[:alnum:]_.+-]+@[[:alnum:]_.-]+\.[[:alnum:]]+$/)
    {
        gsub(/"/, "DQUOTES")
        gsub("\047", "SQUOTES")
        r=gensub("[,|;: \t]+",":",1,$0)
        a=tolower(substr(r,1,1))
        if (a ~ /^[[:alnum:]]/)
            print r > a
        else
            print r > "_"
    }
    else
        print $0 > "ErrorFile"
}' *.txt

Don't know if this is affecting your performance, but one thing to check is that you are using an up-to-date version of awk. In particular, the version of awk that ships on MacOS is very slow. You may get dramatic difference by installing the latest GNU gawk. I've measured order of magnitude performance improvements by upgrading. (On MacOS, Homebrew and MacPorts are two popular package managers that can install the latest version.)


Time-scrolling interface in matplotlib I've create a simple interface for viewing a time-series as it changes over time in matplotlib. You can pause, reset or click/drag on the time-line to change the view. Have I used the matplotlib API as effectively as possible?

import numpy as np
from collections import deque

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.widgets import Button, Slider

class AnalogPlot:

    def __init__(self, data, display_len):
        self.buff = deque(np.zeros(display_len))
        self.display_len = display_len
        self.data = data

        # set up the plot
        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111, xlim=(0, t_max), ylim=(-1, 1))
        self.ax.set_xticks((0, t_max))
        self.lines = self.ax.plot([], [])

        # setup the animation
        self.cur_frame = 0
        self.anim = animation.FuncAnimation(self.fig, self._update,
                                            interval=1.0)

        # setup the animation control
        self.anim_running = True

    def _add_to_buff(self, buf, val):
        if len(buf) < self.display_len:
            buf.appendLeft(val)
        else:
            buf.popleft()
            buf.append(val)

    def _update(self, frame):
        frame = self.cur_frame
        self._add_to_buff(self.buff, self.data[frame:frame+1])
        self.lines[0].set_data(range(self.display_len), self.buff)

        self.ax.set_xticklabels((str(frame), str(frame+self.display_len)))

        self.time_slider.eventson = False
        self.time_slider.set_val(frame)
        self.time_slider.eventson = True

        self.cur_frame += 1

        return self.lines

    def _pause(self, event):
        if self.anim_running:
            self.anim.event_source.stop()
            self.anim_running = False
        else:
            self.anim.event_source.start()
            self.anim_running = True

    def _reset(self, event):
        self._set_val(0)

    def _set_val(self, frame=0):
        frame = int(frame)
        self.cur_frame = frame
        new_start = frame - self.display_len
        if new_start >= 0:
            self.buff = deque(self.data[new_start:frame])
        else:
            self.buff = deque(np.concatenate((np.zeros(np.abs(new_start)),
                                              self.data[:frame])))

        self.anim.event_source.stop()
        self.anim = animation.FuncAnimation(self.fig, self._update,
                                            interval=1.0)
        self.anim_running = True

    def animate(self):
        pause_ax = self.fig.add_axes((0.7, 0.025, 0.1, 0.04))
        pause_button = Button(pause_ax, 'pause', hovercolor='0.975')
        pause_button.on_clicked(self._pause)

        reset_ax = self.fig.add_axes((0.8, 0.025, 0.1, 0.04))
        reset_button = Button(reset_ax, 'reset', hovercolor='0.975')
        reset_button.on_clicked(self._reset)

        slider_ax = self.fig.add_axes((0.1, 0.025, 0.5, 0.04))
        self.time_slider = Slider(slider_ax, label='Time',
                                  valmin=0, valmax=self.data.shape[0],
                                  valinit=0.0)

        self.time_slider.on_changed(self._set_val)

        plt.show()

t_max = 100
lin_sig = np.linspace(0, 1, 1000)
analog_plot = AnalogPlot(lin_sig, t_max)
analog_plot.animate()

Looks good to me.

Ship it.

(Consider using flake8, and burying the last four lines within if __name__ == '__main__':.)


Deleting pointers in Qt5 I am relatively new to the Qt framework and I was wondering if I should delete pointers in my program.  I know that, in C++, if memory is not return it could lead to memory leak, but I am not sure if the same applies to Qt.

#include "mainwindow.h"
#include <QApplication>
#include <QTextEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QIcon>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget *w = new  QWidget();
    w->setWindowTitle("Music Player");
    QIcon * mainwind_icon = new QIcon("MusicPlayer.png");
    w->setWindowIcon(*mainwind_icon);

    QPushButton * enter_button = new QPushButton();
    QTextEdit  * textbox = new QTextEdit();

    QHBoxLayout * vlayout = new QHBoxLayout;
    vlayout->addWidget(textbox);
    vlayout->addWidget(enter_button);

    w->setLayout(vlayout);

    w -> show();

    return a.exec();
}

In Qt, there is a concept of parents and a hierarchy.

In essence, it means that when a qobject owns another qobject (the other qobject's parent is the first) then the parent will take care of cleaning its children.

There are 2 ways an object becomes a child of another: when it is set in the constructor or when reparented. The qwidget family automatically reparents widgets which you add to it

Common use for the main function is to allocate the root statically and all the rest dynamically as child of the root:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w ();
    w.setWindowTitle("Music Player");
    QIcon * mainwind_icon = new QIcon("MusicPlayer.png");
    w.setWindowIcon(*mainwind_icon);

    QPushButton * enter_button = new QPushButton();
    QTextEdit  * textbox = new QTextEdit();

    QHBoxLayout * vlayout = new QHBoxLayout;
    vlayout->addWidget(textbox);
    vlayout->addWidget(enter_button);

    w.setLayout(vlayout);

    w.show();
    return a.exec();
}

The setlayout takes ownership of the vlayout and its children so the destructor of w will delete those as well

When you are inside an event loop then you can delete a qobject by calling deleteLater on it which will schedule the object (and its children) for deletion. You can also connect a signal to it if needed.


Write an anonymous function validating email address Problem statement

  Write an anonymous function which take email address as parameter and returns true/false after validating the input parameter.

  Objective

  This assignment will help you to master the following concepts:

  function
  closures

Solution

<!DOCTYPE html>
<html>
    <head>
        <title>Anomymous function</title>
        <meta charset="UTF-8">
        <script type="text/javascript">
            var result = (function(email_id){
                            var regpat = /^[a-zA-Z_\.\-]+@[a-zA-Z_\-\.]+?\.[a-zA-Z]{2,4}$/;
                            return regpat.test(email_id);
                        })('xyz@gmail.com');
            document.write(result);
        </script>
    </head>
    <body>

    </body>
</html>

1) Is this the corresponding solution?

2) Why document.body.innerHTML = result; does not work?

As I already noted in your other question, trying to validate email addresses correctly is an almost impossible task; there are a lot more characters allowed that you are assuming. Check out this correct regex to validate addresses, which is quite unpractical.

This means that we can either be too strict or too lenient with our regex. For front-end validation, being too strict is not the right choice as people with valid email addresses will not be able to use your service.

Personally, I would check if there is an @ in the string, and be done with it. It will catch common typos, there isn't much more you can do.

Later on, you could write two functions: requiredEmailValidation and optionalEmailValidation. The first just looks for an @, and doesn't allow a form to be submitted, the second checks for common typos (eg .con instead of .com, gmai.com instead of gmail.com, etc) and shows a warning.

Misc

js variable names generally can't contain -. I'm surprised it's even working for you, it didn't for me with firefox or chrome.
regpat isn't that great of a variable name; it's hard to read (regexPattern would be better), and contains few information (I can already see that it's a regex pattern). Something like emailPattern would be better (for such a short function, it's not that important, but if you write larger functions later on, naming becomes very important).


Implementation of Queue using Linkedlist in Java Can someone please review my code? Is it the correct implementation of a queue? If not, please give suggestions on how to implement it using a linked list.

class Queue
{
Node front,rear;

class Node
{
    int data;
    Node next;

    public Node(int d)
    {
        data=d;
        next=null;
    }
}

void enqueue(int new_data)
{
    Node new_node=new Node(new_data);

    if(front==null)
    {
        new_node.next=front;
        front=new_node;
        rear=front;
    }
    else if(rear.next==null)
    {
        rear.next=new_node;
        rear=rear.next;
    }
    System.out.println(rear.data+"\t Enqueued");
}

Node dequeue()
{
 if(front==null && rear==null)
 {
System.out.println("\t Queue is Empty");
  return null;
 }
    else    {
    Node temp=front;
    front=temp.next;
    temp.next=null;
    System.out.println(temp.data+"\t Dequeued");
if(front==null)
    { rear=front; }

    System.out.println(front +" "+ rear);
    return temp;
    }
}

public static void main(String[] args)
{
    Queue q=new Queue();
    q.enqueue(1);
    q.enqueue(2);
    q.enqueue(3);
    q.enqueue(4);

    q.dequeue();
    q.dequeue();

}
}

Formatting

Your indentation is all over the place.  Using a consistent form of indenting that reflects the scoping of statements makes your code much easier to read and makes bugs easier to spot.

Consistency

Be consistent with your interface.  You define enqueue in terms of the data item that is to be stored in the queue.  This is good, because it hides the implementation.  However, you define dequeue as returning a Node.  This is bad, Node is an implementation detail, there's no reason for the caller to need to know it exists.  dequeue should instead be returning an int (as this is the type stored in the queue).

Print statements

Your print statements don't belong in your queue.  Part of the reason that you originally submitted code that didn't allow access to the values stored in the queue is because you put the print statements within it.  Separate the concerns of your classes and test the boundaries.  If you had originally tried to write your print statements in your main is would have become clear that you needed dequeue to return something in order for you to be able to print it.

Mains don't belong in collections

Your main method doesn't belong in the Queue.  It's not part of the Queue, it's part of your test harness.  You should put it in a different class.  This will help to enforce the boundaries between what is the Queue's responsibilities and what is not the Queue's responsibilities.

Returns & Elses

Where possible you want to avoid nesting statements too far.  In your dequeue, you're doing this:

if(front==null && rear==null)
{
    System.out.println("\t Queue is Empty");
    return null;
}
else {

This creates a layer of nesting for the else statement.  You don't need the else.  When the if condition is matched, you return from the function.  The only way you get past the statement is if the condition didn't match, so the else is redundant.

Redundant checks

In enqueue you perform:

else if(rear.next==null)

You don't have an else condition for this check, and if this condition is triggered, it means that rear isn't pointing at the end of the queue and you've got something very wrong with your collection.  Either remove the if check, since it can't happen, or throw an exception when it does, don't just pretend everything is all right.


Find substring of a string in Swift 3 I am using this basic extension for a Swift project. It works well, but I want to make sure it is the absolute fastest way to get a substring.

extension String {
    func substring(start: Int, end: Int) -> String {
        if (start == end || self.strlen() == 0) {
            return ""
        }
        let startIndex = self.index(self.startIndex, offsetBy: start)
        let endIndex = self.index(self.startIndex, offsetBy: end)
        return self[startIndex..<endIndex]
    }

    func strlen() -> Int {
        return self.characters.count
    }
}

You can see I am checking for two situations that don't make since in the if then statement. Those are the only two things I need to check for. For example, I don't need to check for a situation where start is greater than end.

Should this condition be rewritten as a guard statement? Remember I'm not concerned about following best practices or writing pretty code, I just want to have an efficient substring function.

I see some things that might help:

index(_:, offsetBy:) is O(n) where n is the amount you're offsetting, so you can squeeze a bit out of calculating the endIndex as an offset from the startIndex especially if you're getting substrings from near the end of the string:

let startIndex = self.index(self.startIndex, offsetBy: start)
let endIndex = self.index(startIndex, offsetBy: end-start)

The other option could be to use the UTF16 view of the string instead of the character view.

The UTF16Index has an option to create directly from an Int (rather than) offsetting an index which will be faster (I assume O(1) but I can't find a reference).

let start16 = String.UTF16Index(start)
let end16   = String.UTF16Index(end)
return String(utf16[start16..<end16])!

The consideration around the UTF16 approach depends if you're going to be using text that has multi-width characters and how you're getting the actual start and end values to make the substring. Here's a post that I found useful getting my head around these options: https://oleb.net/blog/2016/08/swift-3-strings/

So it looks like there is an opportunity for a little bit of a performance increase there!

When you check to see if the string is empty you're checking if the character count is zero. self.characters.count == 0 is O(n) where n is the number of characters, you can get some performance increase here by using self.isEmpty which is O(1).

edit: added 4th option that returns String?

Finally, with the UTF16 option there's the need to cast to a String and force unwrap if you want to return the type String. An alternative could be to return nil instead as your early exit:

I ran a quick profile in Xcode comparing those four options:

1. Baseline approach (in question)
2. Alternative (use isEmpty and calculate endIndex from startIndex)
3. UTF16 (use isEmpty and create UTF16 index directly from Int)
4. UTF16 nil (use isEmpty, create UTF16 index from Int and return String?)

Benchmark substring using "hello tests".substring(1,10)

1. Baseline    -> 1.151s  (2% STDEV)
2. Alternative -> 0.633s  (1% STDEV)
3. UTF16       -> 0.408s  (2% STDEV)
4. UTF16 nil   -> 0.404s  (1% STDEV)

Benchmark early exit using "".substring(1,10)

1. Baseline    -> 0.074s  (4% STDEV)
2. Alternative -> 0.024s (12% STDEV)
3. UTF16       -> 0.024s (11% STDEV)
4. UTF16 nil   -> 0.019s (12% STDEV)

Here's a gist of the test I used for full transparency: https://gist.github.com/mathewsanders/c4c43915c5e1c13e8fe3b912bf4c27d1

So absolutely use isIndex instead of counting characters, and maybe consider using the UTF16 view if it's appropriate for the text you'll be making substrings from.

It also looks like returning nil instead of an empty string gives a bit of a boost, but you might loose that small advantage elsewhere in your code depending on how you handle the nil` return.


Getting timeslots using a single query I want to get latest time slots for 2 cases based on doorman true for false based on logged in user.

Cases are:

If doorman is true, the query will be the same
if doorman is false, then I need to add a parameter in the query for doorman that its is false so give me only records having none in them

So, it's the same query almost that will work for both cases with little modification.

def self.latest_pickup_date current_zone,doorman
    if doorman
      latest_timeslot = Timeslot.where(dropoff_slots: '-1', zone_id: current_zone).order(:slot_date).last
      // it is working fine
    else
      latest_timeslot = Timeslot.where(dropoff_slots: '-1', zone_id: current_zone, doorman_type: "none").order(:slot_date).last
      // it also does the same job but need to need check if doorman is none, so need refactoring my queries to use one query for both cases
    end
    latest_timeslot.nil? ? Date.current : latest_timeslot.slot_date
  end

I would like to refactor my code and the query in a way to use the approach of DRY.

I do not want to write these queries twice in both cases. I need a better solution using code practices. Or if I am doing this right way, you can advice as well. Plus, I need good professional code practices and code refactoring as well.

Some notes:

Without knowing the internals of the app, the use of some hardcoded values like dropoff_slits=-1 or doorman_type="none" look a bit suspicious.
Keep the maximum line length to 80 (100 at most) chars.
Write parens in method signature: def self.latest_pickup_date(current_zone, doorman).
latest_timeslot.nil? ? Date.current : latest_timeslot.slot_date. Use  Object#try instead: latest_timeslot.try(:slot_date) || Date.current.

There are many valid ways of DRYing this method. That's what I'd write:

def self.latest_pickup_date(current_zone, doorman)
  base_timeslots = Timeslot.where(dropoff_slots: '-1', zone_id: current_zone)
  timeslots = doorman ? base_timeslots : base_timeslots.where(doorman_type: "none")
  timeslots.order(:slot_date).last.try(:slot_date) || Date.current
end


Struct iteration with/without a pointer I've written the following sample program as a way to help improve my understanding of passing pointers to a function, and it currently works:
#include "stdio.h"

typedef struct Car {
    char*           name;
    unsigned int    price;
} Car;

void print_cars(Car* cars[]) {
    for (int i=0; cars[i] != NULL;i++) {
        printf("\n<Car: %s, Price: $%d>", car->name, car->price);
    }
}
void depreciate(Car* cars[]) {
    for (int i=0; cars[i] != NULL; i++) {
        cars[i]->price = cars[i]->price * 0.95;
    }
}

int main(int argc, char* argv[]) {

    Car chevy = {.name = "Chevy", .price = 45000};
    Car mazda = {.name = "Mazda", .price = 30000};
    Car ferrari = {.name = "Ferrari", .price = 200000};

    Car* my_cars[] = {&chevy, &mazda, &ferrari, NULL};
    print_cars(my_cars);
    printf("\n--------- Depreciating -----------");
    depreciate(my_cars); // <-- ignoring this
    print_cars(my_cars);

    return 1;

}

Ignoring the line that modifies the value of the struct, would it be possible to do the print_cars function without using a pointer? When I tried doing it I would receive the error:

new5.c:13:27: error: invalid operands to binary expression ('Car' (aka 'struct Car') and 'void *')

Which I think may be related to using the NULL in the my_cars array to signal the end of it, but wasn't positive. Additionally, is using NULL in a non-char array a common way to show that the end has been reached? Where else could this be improved?

While your code "works" for you, there are a lot of small mistakes in it. I'm listing them straight from top to bottom.
#include "stdio.h"

Since stdio.h is a header from the standard library, as opposed to a header you define yourself in your project, you should include it using #include <stdio.h> instead.
typedef struct Car {
    char*           name;
    unsigned int    price;
} Car;

The name of the car should be a const char * since later, you assign string literals like "Chevy" to that field, and string literals are read-only.
To prevent this possible bug in the future, let the C compiler warn you about issues like these. For GCC, pass the compile options -Wall -Wextra -Werror -O2. Since you didn't say which development environment you use, you have to find out how to add these options yourself.
void print_cars(Car* cars[]) {
    for (int i=0; cars[i] != NULL;i++) {
        printf("\n<Car: %s, Price: $%d>", car->name, car->price);
    }
}

Since print_cars does not modify the cars, its parameter should be const Car* cars[], just add the const.
There should be a single space around assignment operators. That is, write int i = 0 instead of int i=0. There should be a single space after the semicolon as well.
When a program produces output, the end-of-line marker \n should go to the end of the line, not to the beginning.
void depreciate(Car* cars[]) {
    for (int i=0; cars[i] != NULL; i++) {
        cars[i]->price = cars[i]->price * 0.95;
    }
}

Instead of writing cars[i]->price twice, you can make use of the combined assignment operator *= by writing: cars[i]->price *= 0.95.
The floating-point number 0.95 cannot be represented exactly in binary. You should experiment a little with a car price that would end up evenly. But even then you could end up with rounding errors. I wrote the following test snippet, which shows that for some prices, the actual result ends up lower than expected, because what you see as 0.95 is really 0.9499999999999999555910790149937383830547332763671875 for the computer.
#include <stdio.h>

int main(void)
{
    for (int i = 0; i < 1000; i++) {
        printf("%d: %.20f, %.20f, %d\n",
            i,
            i * 0.95,
            i * 95 / 100.0,
            i * 95 / 100);
    }
}

To keep the programming simple, you should stick to the last variant i * 95 / 100, until you get more experienced with floating point numbers.
int main(int argc, char* argv[]) {

    ...
    return 1;
}

The function main is special regarding its return value. Returning 1 means failure, returning 0 means success. Many other C functions return 0 for success (just like main) and -1 for failure. Others return non-zero for success and zero for failure. Things vary a lot, and you have to learn the meaning for each function individually. There are some patterns though, so it's not completely chaotic.
All in all, it's a well-written program. After modifying the few details I mentioned, it's perfect.


Faster/Better Processing for SQL While Loop I have a list of about 55k Accounts that I am cleaning up.  95% of them are from US/Canada which is what I'm most concerned with.  I am going through record by record and will perform various checks and fill in or correct City, State, Zip, Country.  I have a table with every City, State, and Zip for US and Canada that I check against.  For this part, maybe 20% will be processed after grabbing the info.

I am doing a kind of proof of concept right now, and it's taking forever.  I just hit the 1hr mark with 24,500 (Less than 7 records/sec)  .  I didn't expect this to take so long.  Next will be running a similar process on the Contacts, and there are over 400k contacts.  I'm afraid how long that will take!!

Is there a better way to do this to decrease the time taken?

Here is the code right now:

-- MS SQL Server 15.0.2070.41
ALTER PROCEDURE [dbo].[SetLocationInfo]
AS
BEGIN

DECLARE
    @maxRecords int = 0,
    @cnt        int = 1,
    @city       varchar(255),
    @state      varchar(255),
    @zip        varchar(255),
    @country    varchar(255)

SELECT TOP(1) @maxRecords = id2 FROM Account ORDER BY Id2 DESC

-- BEGIN LOOP
WHILE @cnt <= @maxRecords
BEGIN
    -- Resets Variables
    SET @city = NULL
    SET @state = NULL
    SET @zip = NULL
    SET @country = NULL

    -- Pulls BILLING Address Info from Record
    SELECT
        @city = [BillingCity],
        @state = [BillingState],
        @zip = [BillingPostalCode],
        @country = [BillingCountry]
      FROM [Account] WHERE Id2 = @cnt

    -- Searches for and Sets BILLING Country
    IF (@country IS NULL) OR ((@country != 'United States') AND (@country != 'Canada'))
    BEGIN
        SELECT TOP(1) @country = Country FROM [CityStateInfo]
          WHERE
          (City = @city AND State_abbr = @state) OR
          (City = @city AND Zip = LEFT(@zip,5)) OR
          (Zip = LEFT(@zip,5))

        IF @country IS NOT NULL
            UPDATE Account SET [BillingCountry] = @country WHERE Id2 = @cnt
    END

    -- Resets Variables
    SET @city = NULL
    SET @state = NULL
    SET @zip = NULL
    SET @country = NULL

    -- Pulls SHIPPING Address Info from Record
    SELECT
        @city = [ShippingCity],
        @state = [ShippingState],
        @zip = [ShippingPostalCode],
        @country = [ShippingCountry]
      FROM [Account] WHERE Id2 = @cnt

    -- Searches for and Sets SHIPPING Country
    IF (@country IS NULL) OR ((@country != 'United States') AND (@country != 'Canada'))
    BEGIN
        SELECT TOP(1) @country = Country FROM [CityStateInfo]
          WHERE
          (City = @city AND State_abbr = @state) OR
          (City = @city AND Zip = LEFT(@zip,5)) OR
          (Zip = LEFT(@zip,5))

        IF @country IS NOT NULL
        BEGIN
            PRINT CAST(@cnt AS varchar(10)) + ': ' + @country
            UPDATE Account SET [ShippingCountry] = @country WHERE Id2 = @cnt
        END
    END

    -- Increments Counter
    SET @cnt = @cnt + 1
END
-- END LOOP

END

Please update your questions with the relevant tags, although it is obvious you are using SQL Server here.

If you run an execution plan in your query window, you will be able to see which specific bits are taking a lot time and whether SQL Server is using any indexes etc.

It would be good if you gave a data sample and the table structure. Here are some ideas though.

Is there a reason why you are using a WHILE loop instead of a cursor ? The idea here is that you start with ID 1 and increment until you reach the max ID but there must probably be gaps in your table... the approach is possibly wasteful if you are trying to update rows that don't even exist.

Rather than work on the table Account directly (which may be in use or subject to locks), I would rather work on a partial copy using a temporary table.

Step 1: create a temporary table with all the records from Account you want to fix, matching some criteria eg country empty

SELECT *
INTO #Account
FROM Account
WHERE <add your criteria here>

Step 2: perform all your operations on that temporary table #Account

Step 3: update the original table based on the temporary table (UPDATE FROM):

UPDATE Account
SET
    Account.ShippingCountry = #Account.ShippingCountry,
    Account.BillingCountry = #Account.BillingCountry,
    ...
FROM #Account
WHERE
  #Account.id2 = Account.id2

Here I am assuming that id2 is indeed an identity column that uniquely identifies each record. If it isn't, but instead is some kind of unindexed integer value, then each UPDATE requires a full table scan and there is a performance hit here. Again, can't tell without knowing the table structure for sure.

If all this takes too much time, you can process the jobs in small chunks, for example pull 1000 records at a time using SELECT TOP 1000, and repeat until the table is cleaned.

In short, updating a large table can be very slow if you cannot rely on some index, using an appropriate WHERE clause.

Important remark regarding the temporary table approach: doing SELECT INTO creates a copy of the data but does not include the complete table structure, that is indexes, constraints. That mean the id column will be an ordinary integer and not a unique, indexed field. So I would add the index manually on that field to speed the UPDATE FROM.


Optimized pulling items out of arrays in nested objects I found a couple of similar questions, but they were mostly outdated.
Here's the problem: create a flat array from the values in several arrays contained in several nested objects.

const data = [
  { id: 1, items: ['one', 'two', 'three'] },
  { id: 2, items: ['two', 'one', 'four'] },
]

Expected result const result = ['one', 'two', 'three', 'four']

My current solution:

function() {
  const result = []
  data.forEach(item => data.items.forEach(item => result.push(item)))

  return _.uniq(result)
}

lodash is allowed

Any suggestion is more than welcome

Although at the time of writing, flatMap is not in every browser (according to MDN not in Edge and Samsung Internet);

data.flatMap(obj => obj.items).filter((e, i, ary) => ary.indexOf(e) == i)

Although, you could check the uniqueness from the result array in your code, and push only if not in there, to save one loop over the array.

Another option would be a reducer like

uniqItems = (acc, {items}) => acc.concat(items.filter(item => acc.indexOf(item) < 0))

to data.reduce(uniqItems, []).


Counting occurrences of a word in a file This gives me the output of how many times the word is in the file. (I will eventually want to enhance the program to output on what line of the file the word is.)

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.*;

public class CountLineWordsDuplicateWords {
     public FileReader fr = null;
     public BufferedReader br =null;

     public String [] stringArray;
     public int counLine = 0;
     public int arrayLength ;
      public String s="";
    public String stringLine="";
     public String filename ="";
     public String wordname ="";

public CountLineWordsDuplicateWords(){

    try{
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter the filename: ");
        filename = scan.nextLine();
        Scanner scan2 = new Scanner(System.in);
        System.out.println("Please enter a word: ");
        wordname = scan.nextLine();
        fr = new FileReader(filename);
        br = new BufferedReader(fr);
        while((s = br.readLine()) != null){
            stringLine = stringLine + s;
            //System.out.println(s);
            stringLine = stringLine + " ";
            counLine ++;
        }

        //System.out.println("Contents of file: " + stringLine);

        stringArray = stringLine.split(" ");
        arrayLength = stringArray.length;
                     //System.out.println("The total number of words in the text file is "+arrayLength);
        /*Duplicate String count code */
        for (int i = 0; i < arrayLength; i++) {
            int c = 1 ;
            for (int j = i+1; j < arrayLength; j++) {
                if(stringArray[i].equalsIgnoreCase(stringArray[j])){
                    c++;
                    for (int j2 = j; j2 < arrayLength; j2++) {
                        stringArray[j2] = stringArray[j2+1];
                        arrayLength = arrayLength - 1;
                    }
                    //System.out.println(wordname);
                       if (stringArray[i].equalsIgnoreCase(wordname)){
           System.out.println("The word "+wordname+" is present "+c+" times in the specified file.");
           }

            }//End of Inner for block
       //System.out.println("The word "+stringArray[i]+" is present "+c+" times in the specified file.");

        }
    }//End of Outer for block
        System.out.println("Total number of lines: "+counLine);
        //System.out.println();

        fr.close();
        br.close();
    }catch (Exception e) {
        e.printStackTrace();
    }
}
}

Your code is way more complicated that it needs to be. Let's look at it in detail

Scanner scan = new Scanner(System.in);
System.out.println("Please enter the filename: ");
filename = scan.nextLine();
Scanner scan2 = new Scanner(System.in);
System.out.println("Please enter a word: ");
wordname = scan.nextLine();

You're creating 2 Scanner object when you just need one to read both the filename and the word to search. You can reuse the Scanner you already have.

fr = new FileReader(filename);
br = new BufferedReader(fr);

Writing this generally means that something is wrong. You're opening a stream to a resource but, in case there is an exception, they won't be closed: the

fr.close();
br.close();

part of the code won't be reached. This creates a resource leak. Starting with Java 7, you can use a try-with-resources statement so that every opened resource is properly closed, regarless of the outcome (expection or not).

Then,

while((s = br.readLine()) != null){
    stringLine = stringLine + s;
    //System.out.println(s);
    stringLine = stringLine + " ";
    counLine ++;
}

concatenates Strings in a loop, which is a bad practice: Strings are immutable and every time you use + a new object is created each time. Instead, what you want to use is a StringBuilder. But in this case...

stringArray = stringLine.split(" ");
arrayLength = stringArray.length;

You're splitting in an array all the Strings you concatenated before, meaning you did all that work for nothing: you could have just created an array and populate it in the first place.

Now this is what the main loop is doing:

Start at the first word;
Look up a word equal, ignore case, to this word;
If one is found, move all the words after that to the left;
Continue with the second element, until the end of the array

So to put it another way, this is traversing the array as many times as there are elements, which can be a lot. Now imagine if the array is very long, this can take a very long time.

You don't even need to do this: you can just traverse the array a single time and determine if the current element matches your searched word.

This could be a proposed implementation: it uses the built-in LineNumberReader (to match your enhancement), wraps it inside a try-with-resources. Then each line read is split around a space and the words are compared against the word to search.

Sample code:

public static void main(String[] args) throws IOException {
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter the filename: ");
    String filename = scan.nextLine();
    System.out.println("Please enter a word: ");
    String wordname = scan.nextLine();

    int count = 0;
    try (LineNumberReader r = new LineNumberReader(new FileReader(filename))) {
        String line;
        while ((line = r.readLine()) != null) {
            for (String element : line.split(" ")) {
                if (element.equalsIgnoreCase(wordname)) {
                    count++;
                    System.out.println("Word found at line " + r.getLineNumber());
                }
            }
        }
    }
    System.out.println("The word " + wordname + " appears " + count + " times.");
}


Promise Chaining for a Firebase v3 Auth Subsystem - Password Based Users Firebase v3 Offers Authentication via 3rd party oAuth providers, facebook, github,google, and twitter.  They also offer password pased authentication.
Authenticated users have a firebase.User object with 4 key properties - displayName, email, photoURL.  A password is required to sign in and to register.
In oAuth, the email, displayName, and photoUrl are gotten from the oAuth provider.  Developers must enter this in the Firebase v3 Auth system via an .updateProfile method for Password based users.
So, for registering new users, I collect the 4 values from <inputs> and call the below homemade function to register the new user with all the values in one go.  Promise chaining was used.  Any missing arguments must be null (not undefined prior to invoking the firebase methods).
Is there a cleaner way to chain these or does this look fairly solid.  I know it works.  I'm just seeing if it can be smartened up a bit.
createUserWithEmailAndPassword
createUserWithEmailAndPassword(email, password) returns firebase.Promise containing non-null firebase.User.  Creates a new user account associated with the specified email address and password.
updateProfile
var profile = {displayName: nullable string, photoURL: nullable string};
updateProfile(profile) returns firebase.Promise containing void.  Updates a user's profile data.
function registerPasswordUser(email,displayName,password,photoURL){
  var user = null;
  //nullify empty arguments
  for (var i = 0; i < arguments.length; i++) {
    arguments[i] = arguments[i] ? arguments[i] : null;
  }

  firebase.auth().createUserWithEmailAndPassword(email, password)
  .then(function () {
    user = firebase.auth().currentUser;
    user.sendEmailVerification();
  })
  .then(function () {
    user.updateProfile({
      displayName: displayName,
      photoURL: photoURL
    });
  })
  .catch(function(error) {
    console.log(error.message);
  });
  console.log('Validation link was sent to ' + email + '.');
}

registerPasswordUser is a weird name. It's sort of like "register a user with a password" which all of registered users should have anyways. registerUser might be just fine.

One thing you can do instead of "nullifying" is to accept a user object that has the 4 fields. Then to "nullify", use Object.assign.

const userDefaults = {email: ..., displayName: ..., password: ..., photoURL: ... };

function registerUser(user){
  const finalUserConfig = Object.assign({}, defaults, user);
  ...
}

There is no way for the caller to actually know if the procedure succeeded or failed. This function is a one-off, and the only way to know is a visual console.log. Consider returning the promise, that way the caller knows it succeeded or failed.

function registerUser(user){
  ...
  return firebase.auth()
    .createUserWithEmailAndPassword(email, password)
    .then(...)
    .then(...)
    .catch(error => {
      console.log(error.message);

      // promise.catch recovers the chain.
      // We want to preserve the error for the caller.
      return Promise.reject(error);
    })
}

The two thens you have appear to have one-off calls, sendEmailVerification and updateProfile. They first then doesn't return a promise, which means next one doesn't appear to be waiting for it. They both will fire when createUserWithEmailAndPassword resolves and they may both be merged in one then.

However, since they don't return promises, the chain will not know about their presence. Should they fail, your catch will not capture.

Promise.resolve('test')
.then(function(v){
  Promise.reject('error')
})
.then(function(v){
  console.log('this gets called');
})
.catch(function(error){
  console.log('this never gets called');
});

Consider using Promise.all to handle both calls that return promises. This way, you know if they fail.

Here's how I would do it:

function registerPasswordUser(config){
  const defaults = {email: null,displayName: null,password: null,photoURL: null };
  const userConfig = Object.assign({}, defaults, userConfig);

  firebase.auth()
    .createUserWithEmailAndPassword(userConfig.email, userConfig.password)
    .then(() => {
      const user = firebase.auth().currentUser;

      // Do both in parallel while being able to capture errors in promise
      return Promise.all([
        user.sendEmailVerification(),
        user.updateProfile({ displayName, photoURL });
      ]);
    })
    .catch(error => {
      console.log(error.message);
    });
}


Moving a bot through a maze with Depth First Search This is supposed to be a depth first search. I just took the idea of what it does and made my own version of it. The bot works it's way through the maze, when it finds a dead end, it moves back to original spot, and continues in a different direction than it's already gone until it reaches E the end.

import java.util.Stack;
import java.lang.Comparable;
import java.util.*;

class Main {

  public static int[][] maze = {
    {9,9,9,9,9,9,9,9,9,9,9,9},
    {9,1,1,1,1,1,1,1,1,1,1,9},
    {9,1,0,1,0,1,1,0,0,0,5,9},
    {9,1,0,0,0,0,0,1,0,1,1,9},
    {9,1,1,0,1,1,0,1,0,0,1,9},
    {9,1,0,0,1,0,0,0,0,0,1,9},
    {9,1,1,0,1,1,1,1,1,0,1,9},
    {9,1,0,0,1,0,0,0,1,0,1,9},
    {9,1,1,0,1,0,1,0,1,1,1,9},
    {9,1,0,0,0,0,1,0,0,0,3,9},
    {9,1,1,1,1,1,1,1,1,1,1,9},
    {9,9,9,9,9,9,9,9,9,9,9,9}
    };

    public static Deque<Coordinate> deadEnd = new ArrayDeque<Coordinate>(10);
    public static Deque<Coordinate> deq = new ArrayDeque<Coordinate>(10);

  public static void main(String[] args) {

    printMaze();
    dfs(2,10);

  }

  public static boolean isDeadEnd(Coordinate coord){
    Iterator it = deadEnd.iterator();
    while(it.hasNext()){
      if(it.next().equals(coord)){
        return true;
      }
    }
    return false;
  }

  public static Coordinate setCoord(int i, int j){
    Coordinate coord = new Coordinate();
    coord.setCoord(i,j);
    return coord;
  }

  public static boolean checkCoord(Coordinate coord){
    return iterateCoord(coord);
  }

  private static boolean iterateCoord(Coordinate coord){
    Iterator it = deq.iterator();
    while(it.hasNext()){
      if(it.next().equals(coord)){
        return true;
      }
    }
    return false;
  }

  public static int dfs(int i, int j){
    if(i == 9 && j == 9){
      maze[9][10] =4;
      printMaze();
      System.out.println("You're Free!");
      return 0;
    }
    Coordinate coord = new Coordinate();

if(!checkCoord(coord) && !isDeadEnd(coord)){

    if(maze[i+1][j] == 0 || maze[i+1][j] == 3){
        coord = setCoord(i+1, j);
        deq.push(coord);
        maze[i+1][j] = 4;
        printMaze();
        return dfs(i+1,j);
    }
    else if(maze[i-1][j] == 0){
        coord = setCoord((i-1),j);
    deq.push(coord);
        maze[i-1][j] = 4;
        printMaze();
        return dfs(i-1,j);
    }
    else if(maze[i][j+1] == 0){
        coord = setCoord(i,(j+1));
        deq.push(coord);
        maze[i][j+1] = 4;
        printMaze();
        return dfs(i,j+1);
    }
    else if(maze[i][j-1] == 0){
        coord = setCoord(i,(j-1));
        deq.push(coord);
        maze[i][j-1] = 4;
        printMaze();
        return dfs(i,j-1);
    }
}

    deadEnd.push(deq.pop());
    int n = deq.peek().getX();
    int m = deq.peek().getY();
   // maze[m][n] = 0;
    return dfs(n,m);
  }

  public static void printMaze() {

    for(int i = 0; i < 12; i++){
      for(int j = 0; j < 12; j++){
        if(maze[i][j]==1){
         System.out.print(" X ");
        }
        if(maze[i][j]==0){
          System.out.print("   ");
        }
        if(maze[i][j]==2){
          System.out.print(" e ");
        }
        if(maze[i][j]==3){
          System.out.print(" E ");
        }
        if(maze[i][j]==4){
          System.out.print(" b ");
        }
        if(maze[i][j]==5){
          System.out.print(" b ");
          maze[i][j] = 1;
        }
      }
      System.out.println();
    }
  }
}

class Coordinate{
  Integer x = 0;
  Integer y = 0;

  public int getX(){
    return x;
  }
  public int getY(){
    return y;
  }
  public void setCoord(int i, int j){
    x = i;
    y = j;
  }
  @Override
  public boolean equals(Object obj) {

    if(obj == null){
      return false;
    }
    if(obj instanceof Coordinate){
      Coordinate test = (Coordinate) obj;
      if(test.x == this.x && test.y == this.y){
        return true;
      }
    }
    return (this == obj);
  }
  /*
  @Override
  public int hashCode(){
    return x + y * 31;
  }
    */
}

I cleaned up your code, but it appears like your code performs a depth-first search.  It's more common to use a graph to represent the different paths than a queue, but your code works, as best as I can tell.

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;

public class DFSMaze {

    public static int[][] maze = {
            { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
            { 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9 },
            { 9, 1, 0, 1, 0, 1, 1, 0, 0, 0, 5, 9 },
            { 9, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 9 },
            { 9, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 9 },
            { 9, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 9 },
            { 9, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 9 },
            { 9, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 9 },
            { 9, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 9 },
            { 9, 1, 0, 0, 0, 0, 1, 0, 0, 0, 3, 9 },
            { 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9 },
            { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }
            };

    public static Deque<Coordinate> deadEnd =
            new ArrayDeque<Coordinate>(10);
    public static Deque<Coordinate> deq =
            new ArrayDeque<Coordinate>(10);

    public static void main(String[] args) {
        printMaze();
        dfs(2, 10);
    }

    public static boolean isDeadEnd(Coordinate coord) {
        Iterator<Coordinate> it = deadEnd.iterator();
        while (it.hasNext()) {
            if (it.next().equals(coord)) {
                return true;
            }
        }
        return false;
    }

    public static Coordinate setCoord(int i, int j) {
        return new Coordinate(i, j);
    }

    public static boolean checkCoord(Coordinate coord) {
        Iterator<Coordinate> it = deq.iterator();
        while (it.hasNext()) {
            if (it.next().equals(coord)) {
                return true;
            }
        }
        return false;
    }

    public static int dfs(int i, int j) {
        if (i == 9 && j == 9) {
            maze[9][10] = 4;
            printMaze();
            System.out.println("You're Free!");
            return 0;
        }
        Coordinate coord = new Coordinate();

        if (!checkCoord(coord) && !isDeadEnd(coord)) {

            if (maze[i + 1][j] == 0 || maze[i + 1][j] == 3) {
                coord = setCoord((i + 1), j);
                deq.push(coord);
                maze[i + 1][j] = 4;
                printMaze();
                return dfs(i + 1, j);
            } else if (maze[i - 1][j] == 0) {
                coord = setCoord((i - 1), j);
                deq.push(coord);
                maze[i - 1][j] = 4;
                printMaze();
                return dfs(i - 1, j);
            } else if (maze[i][j + 1] == 0) {
                coord = setCoord(i, (j + 1));
                deq.push(coord);
                maze[i][j + 1] = 4;
                printMaze();
                return dfs(i, j + 1);
            } else if (maze[i][j - 1] == 0) {
                coord = setCoord(i, (j - 1));
                deq.push(coord);
                maze[i][j - 1] = 4;
                printMaze();
                return dfs(i, j - 1);
            }
        }

        deadEnd.push(deq.pop());
        int n = deq.peek().getX();
        int m = deq.peek().getY();
        // maze[m][n] = 0;
        return dfs(n, m);
    }

    public static void printMaze() {
        String chars = " XeEbb";
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 12; j++) {
                int k = maze[i][j];
                if (k < 9) {
                    System.out.print(" " + chars.charAt(k));
                    if (maze[i][j] == 5) {
                        maze[i][j] = 1;
                    }
                }
            }
            System.out.println();
        }
    }

}

class Coordinate {
    private int x;
    private int y;

    public Coordinate() {
        x = 0;
        y = 0;
    }

    public Coordinate(int i, int j) {
        x = i;
        y = j;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof Coordinate) {
            Coordinate test = (Coordinate) obj;
            if (test.x == this.x && test.y == this.y) {
                return true;
            }
        }
        return (this == obj);
    }

}


Powershell script for zipping up old files I have a script for zipping up old files. I know using Windows Compression isn't ideal, so I will make the script run using 7-Zip later on. For now though, I just want see how I can make my current script better. By better I mean, how could I make this code cleaner or neater? How could I make the script more effective?
Here is the code:

Write-Output("Beginning script....")

#File path of file to be cleaned
$File_Path = "C:\Users\Administrator\Downloads\Testing\*"

#Location of ZIP file
$Send_To = "C:\Users\Administrator\Documents\ARCHIVE2"

#Location of old files before being zipped
$Old_Files = "C:\Users\Administrator\Documents\OLD_FILES"

#Time frame for files
$Days = "-65"
$now = Get-Date
$last_Write = $now.AddDays($Days)

#Filtering files according to time parameters
$Filter = Get-ChildItem -Path $File_Path | Where-Object { $_.LastWriteTime -lt $last_Write }

#Moving old files to destination folder
if (!$Filter){
    Write-Host "Variable is null"
}
else{
    Move-Item $Filter -Destination $Old_Files
    Write-Output("Moving files....")
}

#Compressing destination folder if there is folders in $Old_Files folder
if (!(Test-Path $Old_Files)) {
    Write-Output("No old files found")
}
else{
    Compress-Archive -Path $Old_Files -DestinationPath $Send_To -Update
    Write-Output("Old files zipped!")
    Remove-Item -Path $Old_Files -Force
}

Write-Output("Script is finished")

I spent several weeks writing and tuning a script that moves old files to a timestamped Zip file. Here are a few guidelines I learned that may help:

1) Move variables into script parameters so they can be changed at runtime without editing the file:

Param($File_Path = "C:\Users\Administrator\Downloads\Testing\*", #File path of file to be cleaned
  $Send_To = "C:\Users\Administrator\Documents\ARCHIVE2", #Location of ZIP file
  $Old_Files = "C:\Users\Administrator\Documents\OLD_FILES", #Location of old files before being zipped
  $Days = "-65"
)

2) Filter usually means 'criteria for filtering a list'.  Calling the list of files $Filter is misleading. $FilesToZip might be a better variable name.

3) There are lots of reasons a file can't be moved or zipped (in use, doesn't exist, no read/write permission, etc.) You should have a try-catch block around Compress-Archive to account for this, and fail or proceed gracefully. Using ZipFileExtensions, an error created an unusable file until it was finalized. Compress-Archive may be more robust, but there are still chances for failure (try pulling your network cable while creating a large archive of files on your LAN).

4) There's no need for an intermediate folder, and it creates more opportunities for failure. You can just compress the file into the zip and delete on success.

5) The message 'No old files found' is misleading. Reading it, I would think that there were no files found matching the age criteria, when it really means the intermediate folder doesn't exist.

6) Don't you need a .Zip file name for the -DestinationPath?

7) Write-Output is usually used for return values rather than messages. Write-verbose, or Write-Host are more appropriate for status messages.


Improved manipulation of a 2D array for a game map I have a 2D array (or rather, an array of arrays) representing tile maps in game. The 'world' map array holds smaller arrays representing individual map parts which I am calling 'rooms':

var worldMap = [
    [room1, room2],
    [room3, room4]
];

I have some code which takes note of the 'current' room (which will be visible on screen to the player) and calculates what map should be loaded as the 'next' room. This will be triggered when the player walks off the edge of the screen. The key variable is currentMap - this variable will be passed to the function responsible for rendering the tile map that the user can see.

My code works and suits my purposes, but I'm wondering if there is a more efficient or elegant way to write it.

JSFiddle Code

function indexOf2d(arrayParam, valueParam) {
    var index = [-1, -1];
    if (!Array.isArray(arrayParam)) {
        return index;
    }
    arrayParam.some(function (sub, posX) {
        if (!Array.isArray(sub)) {
            return false;
        }
        var posY = sub.indexOf(valueParam);
        if (posY !== -1) {
            index[0] = posX;
            index[1] = posY;
            outputA = index[0];
            outputB = index[1];
            return true;
        }
        return false;
    });
    return index;
}

function goToNextRoomDown(map, valueA, valueB) {
    var newValue = valueA + 1;
    var result = map[newValue][valueB];
    return result;
}

Please note that although I am only showing 'goToNextRoomDown', I intend for the user to be able to access maps that are above, below, left and right of their current location. I'm intending to write a function that handles all of these possibilities, rather than having four separate functions, but before I do that I'm more interested in improving what I have presented so far. I'm not asking anyone to write the code for me, so I hope this question still qualifies as on-topic.

// get the index of the currentMap within the worldMap
console.log(indexOf2d(worldMap, currentMap));
// use the index value outputs to calculate the next room 'down' the worldMap array
console.log('next room down: ' + goToNextRoomDown(worldMap, outputA, outputB));
// assign the new room as the currentMap
currentMap = goToNextRoomDown(worldMap, outputA, outputB);

I like your question, games are one the best ways to get to know a language better.

From a once over:

Unless you have other 2d searching needs elsewhere, I would have called indexOf2d as findRoomLocation
I would have made findRoomLocation a part of worldMap
You are both returning the found location with index and setting the globals outputA  and outputB. Setting those globals is considered very bad practice.
If you can exit early from loops (you could exit once you found a room), then I would advise against use some and go for for instead
Also, indexOf is slower than rolling your own search so I would use 2 old school loops

worldMap.findRoom2 = function( room ){
  var notFound = [-1, -1],
  row;

  if (!Array.isArray(this)) {
    return notFound;
  }

  for( var x = 0 , xLength = this.length ; x < xLength ; x++ ) {
    row = this[x];
    if (!Array.isArray(row)) {
      continue;
    }
    for( var y = 0 , yLength = row.length ; y < yLength ; y++ ) {
      if( row[y] == room ){
        return [x,y];
      }
    }
  }
  return notFound;
};

If this was for a personal non production project I would settle for something like this

worldMap.findRoom = function( room )
{
  var x = -1, y;
  while( this[y=-1,++x] )
    while( this[x][++y] )
      if( this[x][y] == room )
        return [x,y];
  return [-1,-1]; //Not found
};

All in all, I would counter propose something like this:

// noprotect
var room1 = [
    [1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1]
];

var room2 = [
    [2, 2, 2, 2, 2, 2, 2],
    [2, 2, 2, 2, 2, 2, 2],
    [2, 2, 2, 2, 2, 2, 2]
];

var room3 = [
    [3, 3, 3, 3, 3, 3, 3],
    [3, 3, 3, 3, 3, 3, 3],
    [3, 3, 3, 3, 3, 3, 3]
];

var room4 = [
    [4, 4, 4, 4, 4, 4, 4],
    [4, 4, 4, 4, 4, 4, 4],
    [4, 4, 4, 4, 4, 4, 4]
];

var worldMap = [
    [room1, room2],
    [room3, room4]
];

var outputA;
var outputB;
var currentMap = room2;

worldMap.findRoom = function( room ){
  var notFound = [-1, -1],
      row;

  if (!Array.isArray(this)) {
        return notFound;
  }

  for( var x = 0 , xLength = this.length ; x < xLength ; x++ ) {
    row = this[x];
    if (!Array.isArray(row)) {
        continue;
    }
    for( var y = 0 , yLength = row.length ; y < yLength ; y++ ) {
      if( row[y] == room ){
        return [x,y];
      }
    }
  }
  return notFound;
};

worldMap.findRoomAlt = function( room )
{
  var x = -1, y = 0;
  while( this[y=-1,++x] )
    while( this[x][++y] )
      if( this[x][y] == room )
        return [x,y];
  return [-1,-1]; //Not found
};

worldMap.findNextRoom = function( directionName ){
  //dx & dy -> Delta on x and y axis
  var directions = {
    down : {  dx : 1 , dy : 0 }
  };
  var direction = directions[directionName];
  //Paranoia
  if(!direction){
    //Do something, I would actually throw an error
  }

  var location = this.findRoom(currentMap);
  location[0] += direction.dx;
  location[1] += direction.dy;
  return this[location[0]][location[1]];
};

// show the existing currentMap
console.log('existing currentMap = ' + currentMap);

// get the index of the currentMap within the worldMap
console.log(worldMap.findRoom(currentMap));

// use the index value outputs to calculate the next room 'down' the worldMap array

console.log('next room down: ' + worldMap.findNextRoom('down'));

currentMap = worldMap.findNextRoom('down');
console.log('new currentMap = ' + currentMap);


Row and Column input picker I've created a simple picker which is going to be used to select the number of rows and columns when constructing a table.

Here's the example: http://jsfiddle.net/tnn3qgvj/8/

HTML

<input type="text" value="1 x 1" class="grid-chooser"/>

JavaScript

$(function () {
    var rows = 5;
    var cols = 5;

    // create the grid
    var grid = '
';
    for (var i = 0; i < rows; i++) {
        grid += '
';
        for (var c = 0; c < cols; c++) {
            grid += '

';
        }
        grid += '
';
    }
    grid += '
';

    var $gridChooser = $('.grid-chooser');
    var $grid = $(grid)
        .insertAfter($gridChooser);

    var $allSquares = $('.square');

    $grid.on('mouseover', '.square', function () {
        var $this = $(this);
        var col = $this.index() + 1;
        var row = $this.parent().index() + 1;
        $allSquares.removeClass('highlight');
        // select all the rows and cols indexed lower than
        // the one under the mouse
        $('.row:nth-child(-n+'+row+') .square:nth-child(-n+'+col+')')
            .addClass('highlight');
        $gridChooser.val(col + ' x ' + row)
    })

});

CSS

html {
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit;
}

.grid-chooser + .grid {
    width: 120px;
    height: 120px;
    background: rgba(0,0,0,0.2);
    position: relative;
    padding: 2px;
    display: none;
}

.grid-chooser:focus + .grid {
   display: block;
}

.grid-chooser + .grid .row:first-child .square:first-child .inner {
   background: rgba(0,0,0,0.4);
}

.grid-chooser + .grid .square:first-child {
    margin-top: 0;
}

.grid-chooser + .grid .row {
    height: calc(100%/5);
}

.grid-chooser + .grid .square {
    height: 100%;
    width: calc(100%/5);
    display: inline-block;
    margin: -2px 0;
    padding: 1px;
}

.grid-chooser + .grid .square:hover .inner,
  .grid-chooser + .grid .square.highlight .inner {
    background: rgba(0,0,0,0.4)
}

.grid-chooser + .grid .inner {
    height: 100%;
    width: 100%;
    border-radius: 3px;
    background: rgba(0,0,0,0.2);
}

I'm interested to know if there's a more effective or efficient method of selecting the square elements that appear before the mouseover, or any other improvements!

You're using calc() when there's no need for it because you already know the size of the parent element and you're not mixing units.  You're also not providing a fallback for browsers that don't support calc(), which in turn makes your entire demo appear as though it doesn't work.

.grid-chooser + .grid .row {
    height: 23px;
}
.grid-chooser + .grid .square {
    height: 100%;
    width: 23px;
}

If you want to be able to change the size of your picker without having to recalculate the sizes of the descendant elements, you should be using variables via CSS preprocessors.  In addition to increasing your browser support, you'll also have a marginally smaller file.

You're using display: inline-block here when float: left would be a better choice.  Using negative margins to remove the space between inline elements is not reliable under any circumstance, ever.

.grid-chooser + .grid .square {
    float: left;
    padding: 1px;
}

You can eliminate need for the .inner element using this CSS (you'll need to modify your JS to match):

.grid-chooser + .grid {
    width: 120px;
    height: 120px;
    background: rgba(0, 0, 0, 0.2);
    position: relative;
    padding: 2px;
}
.grid-chooser + .grid .row:first-child .square:first-child {
    background: rgba(0, 0, 0, 0.4);
}
.grid-chooser + .grid .square {
    height: 22px;
    width: 22px;
    float: left;
    margin: 1px 0 0 1px;
    border-radius: 3px;
    background: rgba(0, 0, 0, 0.2);
}
.grid-chooser + .grid .square:hover,
.grid-chooser + .grid .square.highliger {
    background: rgba(0, 0, 0, 0.4)
}


Simple resizable array In some (lower-level) parts of my codebase I often have the need to allocate resizable array storage for certain objects, that usually are not default-constructible.

I created a simple class using std::aligned_storage and I was wondering if it's safe and if it can be improved.

The user of the class has to keep track of the current capacity.

template<typename T> class ResizableArray
{
    private:
        using TStorage = std::aligned_storage_t<sizeof(T), alignof(T)>;
        TStorage* data{nullptr};

    public:
        ~ResizableArray() { delete[] data; }

        void resize(std::size_t mSizeOld, std::size_t mSizeNew)
        {
            auto newData(new TStorage[mSizeNew]);
            for(auto i(0u); i < mSizeOld; ++i) newData[i] = std::move(data[i]);

            delete[] data;
            data = newData;
        }

    auto& operator[](std::size_t mIdx) noexcept
    { return reinterpret_cast<T&>(data[mIdx]); }

    const auto& operator[](std::size_t mIdx) const noexcept
    { return reinterpret_cast<const T&>(data[mIdx]); }
};

Example usage:

int main()
{
    ResizableArray<Thing> a;

    // Resize array capacity from 0 to 10
    a.resize(0, 10);

    for(auto i(0u); i < 10; ++i)
        new (&a[i]) Thing{/* something */};

    return 0;
}

I see a couple of problems:

If you ever shrink the array, the loop for copying the elements will overrun the bounds of newdata because the index runs from 0 to mSizeOld.
You don't store the size of the array in the class, so code like this will lose data:

ResizeableArray<Thing> a;
a.resize(0, 10);
// Populate the array
a.resize(0, 11);
// Now a is empty again because resize() uses the first argument.

This might be intentional — to allow you to start over with a fresh set of data &mdash but if so, resize() should be commented to say that.


MySQL class to add user/database I'm creating tool to add new virtualhost on UNIX-box.
One of tasks is to add a new user and database to MariaDB (aka MySQL) server.
In fact - this my first 'real' attempt to use OOP.

Here is class, which I created for it:

class mysqlUserDb:
        warnings.filterwarnings('error')
        def __init__(self):
                self.root = 'root'
                self.host = 'localhost'
                self.rootpw = 'p@ss'

                try:
                        print '\nChecking MySQL connection...'
                        self.db = MySQLdb.connect(self.host, self.root, self.rootpw)
                        self.cursor = self.db.cursor()
                        self.cursor.execute('select version()')
                        print 'Connection OK, proceeding.'
                except MySQLdb.Error as error:
                        print 'Error: %s ' %error + '\nStop.\n'
                        sys.exit()

        def createdb(self, db):
                print '\nCreating database...'
                try:
                        self.cursor.execute('create database if not exists ' + db)
                        self.cursor.execute('show databases like ' + '\'' + db + '\'')
                        dbs = self.cursor.fetchone()
                        print 'Database created: %s' %dbs
                except Warning as warn:
                        print 'Warning: %s ' %warn  + '\nStop.\n'
                        sys.exit()

        def grants(self, user, userpass, db):
                print '\nGrant privilegies... '
                try:
                        self.cursor.execute('grant all on ' + db + '.*' + 'to ' + '\'' + user + '\'' + "@'localhost'" + 'identified by ' + '\'' + userpass + '\'')
                        self.cursor.execute('select user, db from mysql.db where db=' + '\'' + db + '\'')
                        grs = self.cursor.fetchall()
                        print 'Access granted: %r' %grs
                except Warning as warn:
                        print 'Warning: %s ' %warn  + '\nStop.\n'
                        sys.exit()
                except MySQLdb.Error as error:
                        print 'Error: %s ' %error + '\nStop.\n'
                        sys.exit()

        def __del__(self):
                print '\nFinishing operations...'
                self.cursor.close()
                self.db.close()
                print 'Done.\n'

Next, in tool it call like:

import createvhostFunctsClass as fun

mysql = fun.mysqlUserDb()
mysql.createdb('db_2')
mysql.grants('user', 'pass', 'db_2')
del mysql

(it's just example, not included yet in tool).

And its works like:

$ ./myclass.py

Checking MySQL cconnection...
Connection OK, proceeding.

Creating database...
Database created: db_2

Grant privilegies...
Access granted: ('user', 'db_2')

Finishing operations...
Done.

I'm pretty sure, here is lot of stuff, which I missed or did incorrectly (or not the "Python way").

Also, I'm not sure - which way better:

create methods for each action (add database, add user) and just call this methods from script (currently done)
create one method, which will get needed action ('create database lalala') as argument and call this method several times in script

(This is in addition to what @200_success already said about exit codes and error handling.)

Your main questions

  I'm pretty sure, here is lot of stuff, which I missed or did incorrectly (or not the "Python way").

The convention is to use CamelCase for class names. So MysqlUserDb would be better for your class. See PEP8 for more details.

Don't mix string concatenation and formatting expressions like this:

print 'Error: %s ' % error + '\nStop.\n'

Stick to formatting expressions without concatenation:

print 'Error: %s\nStop.\n' % error

You do this a lot. Apply this technique everywhere.

Similarly, you're not using formatting expressions enough. For example in SQL queries like this one:

self.cursor.execute('show databases like ' + '\'' + db + '\'')

With all those quotations and escaping, it's easy to overlook a mistake. It becomes simpler if you use formatting expression, and if you change the outermost quotes to double quotes:

self.cursor.execute("show databases like '%s'" % db)

Your code appears to be for Python 2.x. To be a bit more ready for a possible future migration to Python 3.x, I recommend to start writing your print ... statements as print(...), for example:

print('Connection OK, proceeding.')

  Also, I'm not sure - which way better:

  create methods for each action (add database, add user) and just call this methods from script (currently done)
  create one method, which will get needed action ('create database lalala') as argument and call this method several times in script

Your current approach is good: use a separate method for each separate action.

Unused fields

In __init__ you are setting these fields:

self.root = 'root'
self.host = 'localhost'
self.rootpw = 'p@ss'

But then you're not using them anywhere else within the class. So it seems these should not be fields, but perhaps global constants declared at the top of the class. I would also name them a bit differently:

DBROOTUSER = 'root'
DBROOTPASS = 'p@ss'
DBHOST = 'localhost'

Security

Are you sure you want to store the db's root account password in this script? Make sure the file has restricted permissions (do a chmod 0600 on it).

Needless to say, but better make sure the method parameters are cleaned before reaching your class to avoid SQL injection attacks. Or implement validation inside your methods.

Naming

Instead of these parameter names:

def createdb(self, db):
    # ...

def grants(self, user, userpass, db):
    # ...

I recommend this:

def createdb(self, dbname):
    # ...

def grants(self, dbuser, dbpass, dbname):
    # ...

This may be a matter of taste though.


"Linked" Enum value attribute So I had this idea; what if I could decorate my Enum values with an attribute that cross-references a "default" value of another Enum that should be used when a variable of the current Enum type has this value?

The desired usage would look like:

public enum MyEnum
{
    Value1,
    Value2,
    Value3,
}

public enum ControllingEnum
{
    [LinkedValue(MyEnum.Value1)]
    ControllingValue1,
    [LinkedValue(MyEnum.Value2)]
    ControllingValue2,
    [LinkedValue(MyEnum.Value1)]
    ControllingValue3,
    [LinkedValue(MyEnum.Value3)]
    ControllingValue4,
}

...

var relatedValue = ControllingEnum.ControllingValue4.GetLinkedValue<MyEnum>();

I came up with the following:

public class LinkedValueAttribute:Attribute
{
    public LinkedValueAttribute(object value)
    {
        TypeOfValue = value.GetType();
        Value = (Enum)value;
    }

    public Type TypeOfValue { get; private set; }
    public Enum Value { get; private set; }
}

public static class AttributeHelper
{
    public static T GetLinkedValue<T>(this Enum enumValue) where T:struct
    {
        if (!typeof(Enum).IsAssignableFrom(typeof(T)))
            throw new InvalidOperationException("Generic type must be a System.Enum");

        //Look for LinkedValueAttributes on the enum value
        LinkedValueAttribute[] attr = enumValue.GetType().GetField(enumValue.ToString())
            .GetCustomAttributes(typeof(LinkedValueAttribute), false)
            .OfType<LinkedValueAttribute>()
            .Where(a=>a.TypeOfValue == typeof(T))
            .ToArray();

        if (attr.Length > 0) // a DescriptionAttribute exists; use it
            return (T)(object)attr[0].Value;
    }
}

It does the job, but the boxing and unboxing are generally to be avoided. The question is, how?

I would personally write the attribute class as such:

[AttributeUsage(AttributeTargets.Field)]
public sealed class LinkedValueAttribute : Attribute
{
    private readonly Type typeOfValue;

    private readonly Enum value;

    public LinkedValueAttribute(object value)
    {
        this.typeOfValue = value.GetType();
        this.value = (Enum)value;
    }

    public Type TypeOfValue
    {
        get
        {
            return this.typeOfValue;
        }
    }

    public Enum Value
    {
        get
        {
            return this.value;
        }
    }
}

Reasons:

1: the AttributeUsage attribute limits it to going on fields, which, in this case, is the same as an enum member. Without it, the attribute can go onto anything anywhere, and while harmless from the point of the extension method, it is just a bit weird. Signify intent and don't let your users shoot themselves in the foot if you have the means.

2: sealed class. If you see no more functionality being added by subclasses in the future, seal it up. Compiler and JIT can do some optimizations knowing there are no descendants.

3: readonly fields instead of automatically implemented properties. First, signifies intent. Those fields will not be modified after the constructor assigns them. Don't let any future methods in that class noodle them unknowingly. Second, the compiler and JIT may do some optimizations based on knowing the fields will be unmolested after construction.

Now, in your extension method, since you already use LINQ, there's a couple more extensions you can use:

public static class AttributeHelper
{
    public static T GetLinkedValue<T>(this Enum enumValue) where T : struct
    {
        if (!typeof(Enum).IsAssignableFrom(typeof(T)))
        {
            throw new InvalidOperationException("Generic type must be a System.Enum");
        }

        // Look for LinkedValueAttributes on the enum value.
        var attributes = enumValue.GetType()
            .GetField(enumValue.ToString())
            .GetCustomAttributes(typeof(LinkedValueAttribute), false)
            .OfType<LinkedValueAttribute>()
            .Where(a => a.TypeOfValue == typeof(T));

        // A DescriptionAttribute exists; use it.
        if (attributes.Any())
        {
            return (T)(object)attributes.Single().Value;
        }

        throw new InvalidOperationException("No cross-reference LinkedValueAttribute was found for the given value");
    }
}

1: Removed .ToArray() since the LINQ extensions I use don't need an array to work on.

2: Changed attributes.Length > 0 to attributes.Any(). This is primarily to signify intent. You want to know if there are any attributes returned, not particularly interested in the length of the array (that's no longer needed).

3: Changed attributes[0].Value to attributes.Single().Value. Because the default for AttributeUsage is Multiple = false and that's a good thing for the semantics of how it's used - we'll only get either zero or one back.

4: Added the exception throw I mentioned in my comment.

Now, as for the box/unbox... I'm still noodling on that one. I'll update this if/when I get something.

UPDATE I'm still not finding any way around the box/unbox issue. Reflection tricks and casting sorcery is still requiring the object bit.


Is it worthy to create a wrapper function like this? I have a few Python codes like this:

 workflow = []
 conf = {}
 result = []
 prepare_A(workflow, conf)
 prepare_B(workflow, conf)
 prepare_C(workflow, conf)
 result.append(prepare_D_result(workflow, conf))
 prepare_E(workflow, conf)
 #.... about 100 prepare functions has the same parameter list

I was wondering that whether I need to rewrite like this:

 workflow = []
 conf = {}
 result = []
 def prepare_wrapper(prepare_function):
         return prepare_function(workflow, conf)

 prepare_wrapper(prepare_A)
 prepare_wrapper(prepare_B)
 prepare_wrapper(prepare_C)
 result.append(prepare_wrapper(prepare_D_result))
 prepare_wrapper(prepare_E)
 #.... about 100 prepare functions has the same parameter list

Though it might reduce the burden of passing 2 parameters into the function each time, it might bring difficulties for those who read codes. Is there better ways to ameliorate the code quality in such situation?

Here's an idea, assuming your logic is really that straightforward:

workflow = []
conf = {}
result = []

steps = ((prepare_A, False),
         (prepare_B, False),
         (prepare_C, False),
         (prepare_D, True),
         (prepare_E, False))

for func, need_results in steps:
    res = func(workflow, conf)
    if need_results:
        result.append(res)

If you don't find that approach suitable, I'd suggest making your wrapper a class that encapsulates workflow and conf, and possibly results too.

class PrepareWrapper(object):
    def __init__(self):
        self.workflow = []
        self.conf = {}

    def __call__(self, prepare_function):
         return prepare_function(self.workflow, self.conf)

prepare_wrapper = PrepareWrapper()
prepare_wrapper(prepare_A)
prepare_wrapper(prepare_B)

Or, why not make all the prepare functions methods of this class?