Simple Javascript Accordion I have put together a simple Javascript accordion, though the Javascript code I think could possibly be slimmed down. I would welcome any feedback anyone has on what I have done, and any advice to improve.

HTML

Accordion 1

Cras malesuada ultrices augue molestie risus.

Accordion 2

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Recusandae at placeat nesciunt nostrum accusamus debitis fuga similique quisquam, rerum temporibus, quod asperiores nulla, eveniet libero earum eaque harum inventore minima ipsum saepe omnis. Officia, est, maiores. Reprehenderit odio perspiciatis voluptates commodi ex at praesentium laborum deleniti libero, architecto sit optio repellat est molestiae beatae, magnam qui voluptatibus. Ducimus mollitia dignissimos minus sapiente quidem, animi adipisci laboriosam aliquam asperiores facere. Repellat recusandae doloribus incidunt voluptatibus quibusdam rem delectus inventore nisi, laudantium. Doloribus eum vero, consequuntur nisi enim quam non odio dignissimos praesentium nostrum magnam consequatur totam reprehenderit quaerat. Saepe, blanditiis fugit?

Accordion 3

Vivamus facilisisnibh scelerisque laoreet.

SCSS

div.accordion {
  .accordion-content {
    max-height: 0px;
    overflow: hidden;
    transition: max-height 400ms;
    &.active {
      overflow: auto;
    }
  }
}

JavaScript

document.addEventListener('DOMContentLoaded', function() {

  function toggle(){
    if (this.nextElementSibling.classList.contains('active')) {
      this.nextElementSibling.classList.remove('active');
      this.nextElementSibling.removeAttribute('style');
      this.classList.remove('active');
    }
    else {
      var elementHeight = this.nextElementSibling.scrollHeight;
      this.nextElementSibling.classList.add('active');
      this.nextElementSibling.style.maxHeight = elementHeight + 'px';
      this.classList.add('active');
    }
  }

  var accordion = document.querySelector('.js div.accordion');
  var header = accordion.getElementsByClassName('accordion-header');

  for (var i = 0; i < header.length; i++) {
    header[i].addEventListener('click', toggle);
  }

});

I don't see any issue with code other than below two things.

First, I'll suggest is to use classList.toggle instead of contains and then add/remove class. See browser compability and if target browser does not support toggle method, use polyfill from MDN.

function toggle() {
    if (this.nextElementSibling.classList.contains('active')) {
        this.nextElementSibling.removeAttribute('style');
    } else {
        var elementHeight = this.nextElementSibling.scrollHeight;
        this.nextElementSibling.style.maxHeight = elementHeight + 'px';
    }

    // Toggle `active` class
    this.nextElementSibling.classList.toggle('active');
    this.classList.toggle('active');
}

Second, caching the length of array when iterating using for will slightly improve the performance.

for (var i = 0, len = header.length; i < len; i++) {
              ^^^^^^^^^^^^^^^^^^^^^                  : Caching length

If running the code in latest environment where for...of is supported, you can use it to iterate over elements.

var headers = accordion.getElementsByClassName('accordion-header');
for (header of headers) {
    header.addEventListener('click', toggle);
}


Nested list performance tuning I need to make a function like these two, but at least an order of magnitude faster in python.
Any tips or tricks?

These functions inputs are nested lists, like: request_parameters = [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, [110, 111, 112, 113, 114]]

Their output should be: [[1, 2, 3, 4, 5, 110], [1, 2, 3, 4, 5, 111], [1, 2, 3, 4, 5, 112], [1, 2, 3, 4, 5, 113], [1, 2, 3, 4, 5, 114]]

OR
a numpy.ndarray:

[[ 1  2  3  4  5 110]
 [ 1  2  3  4  5 111]
 [ 1  2  3  4  5 112]
 [ 1  2  3  4  5 113]
 [ 1  2  3  4  5 114]]

A unit test is on the way...

input:

request_parameters = [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, [110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180]]

Output:

[[1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 110]
...
...
...
[1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 180]]

import numpy as np

request_parameters = [1, 2, 3, 4, 5, [10, 11, 12, 13, 14]]

def transform_nested_list(request_parameters: list) -> np.ndarray:
    """
    Generate np.ndarray from a nested list
    """
    abc_ids = np.asarray(request_parameters[-1])
    #print(abc_ids)

    all_except_id = np.asarray(request_parameters[0:-1])
    #print(all_except_id)

    all_subrequest = np.hstack([all_except_id, abc_ids[0]])
    for element in abc_ids[1:]:
        row = np.hstack([all_except_id, element])
        all_subrequest = np.vstack([all_subrequest, row])
    #print(all_subrequest)
    #print(type(all_subrequest))
    return (all_subrequest)

import copy

def old_transform_nested_list(request_parameters: list) -> list:
    abc_ids = np.asarray(request_parameters[-1])
    all_except_id = (request_parameters[0:-1])

    request_list = []
    subrequest = [all_except_id]
    for single_id in abc_ids:
        #print(single_id)
        subrequest = copy.deepcopy(all_except_id)

        subrequest.append(single_id.astype(int))
        request_list.append(subrequest)
    #print(request_list)
    #print(type(request_list))
    return request_list

import datetime

loop = 10000
start = datetime.datetime.now()
for i in range(loop):
    transform_nested_list(request_parameters)
end = datetime.datetime.now()
print((end-start).total_seconds())

start = datetime.datetime.now()
for i in range(loop):
    old_transform_nested_list(request_parameters)

end = datetime.datetime.now()
print((end-start).total_seconds())

  >
  1.111902,
  0.886634
  [Finished in 2.2s]

Got a bit better results, but still not good enough:

import itertools
def flatten(container):
    for i in container:
        if isinstance(i, (list,tuple)):
            for j in flatten(i):
                yield j
        else:
            yield i

def old_tuple_transform_nested_list(request_parameters: list) -> list:
    abc_ids = np.asarray(request_parameters[-1])

    all_except_id = (request_parameters[0:-1])

    request_list = []
    subrequest = tuple(all_except_id)
    for single_id in abc_ids:
        toupled_subrequest = tuple([subrequest, [single_id]])
        chain = list(itertools.chain(*toupled_subrequest))

        subrequest_list = list(flatten(chain))

        request_list.append(subrequest_list)
    #print(request_list)
    #print(type(request_list))
    return request_list

A better version for longer lists:

def longer_vector_transformrequest(requestparameters: list) -> list:
    ids = np.asarray(requestparameters[-1])
    allexceptid = np.asarray(requestparameters[0:-1])
    subrequest = np.append(allexceptid, 0)

    allsubrequest = np.empty((len(ids),len(requestparameters)))
    for i in range(len(ids)):
        subrequest[-1] = ids[i]
        allsubrequest[i] = subrequest
    return allsubrequest

First and foremost: Never use datetime to measure performance! There is timeit and profile/cProfile for that. With that out of the way, let's talk about NumPy.

Python in general does not like loops if you want to go fast. NumPy can help you here since a lot of the heavy lifting can be done by the C backend where loops are orders of magnitudes faster. But you have to allow it to play its strengths. Broadcasting and slicing to the rescue!

def slice_transformrequest(request_parameters: list):
    ids = request_parameters[-1]
    allexceptid = request_parameters[:-1]

    # create an empty array of the appropriate size
    transformed = np.empty((len(ids), len(allexceptid)+1))
    # fill all but the ID column
    transformed[:, :-1] = allexceptid
    # now fill the ID column
    transformed[:, -1] = ids

    return transformed

Now to the timing part:

import timeit

n_loops = 10000
request_parameters = [
    1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15,
    [
        110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
        123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
        136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148,
        149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161,
        162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174,
        175, 176, 177, 178, 179, 180
    ]
]

start = timeit.default_timer()
for i in range(n_loops):
    transform_nested_list(request_parameters)
end = timeit.default_timer()
print(f"transform_nested_list: {end-start:.3f}s")

start = timeit.default_timer()
for i in range(n_loops):
    old_transform_nested_list(request_parameters)
end = timeit.default_timer()
print(f"old_transform_nested_list: {end-start:.3f}s")

start = timeit.default_timer()
for i in range(n_loops):
    longer_vector_transformrequest(request_parameters)
end = timeit.default_timer()
print(f"longer_vector_transformrequest: {end-start:.3f}s")

start = timeit.default_timer()
for i in range(n_loops):
    slice_transformrequest(request_parameters)
end = timeit.default_timer()
print(f"slice_transformrequest: {end-start:.3f}s")

And the results:

transform_nested_list:            11.231s
old_transform_nested_list:        8.375s
longer_vector_transformrequest:   0.623s
slice_transformrequest:           0.140s

About 4x faster than your fastest solution and about 80x faster than transform_nested_list.


Filtering script for products by category I built a simple product filtering feature that works at least in the browsers and platforms that I have tried using it on, but I am unsure about a couple of aspects of it.  I built this on a Bigcommerce site but I think my questions could still apply broadly (some):

Is this a solid approach?  Conventional?  If not, why, and how could/should I approach it differently?
Should I expect to have any problems with browser compatibility?  If so, would they be different problems than any problems I might experience if I was using the stock Bigcommerce faceted search option, or even some other random search feature from another site?

Assume an out of the box faceted search option is not an available. jQuery is loaded twice in HTMLhead.html (head template) as 1.7.2 and 2.2.2

The following example is a simplified version of what I created.  In this example there are only 4 products and 2 categories present.  We can also assume that no products belong in more than one category:

HTML - (Two checkboxes, one for each category)

<fieldset>

<input id="example-category-1" type="checkbox" value="first-checkbox" />CHOOSE CATEGORY 1

<input id="example-category-2" type="checkbox" value="second-checkbox" />CHOOSE CATEGORY 2

</fieldset>

HTML - fiddle with expanded li showing the structure of a random example product.

JS:

Page loads, all products present, no checkboxes clicked. When a checkbox is clicked for the first time, all products are hidden (rather, their parent lis are)

    var test = true;
    $(".test-one").change(function() {
        if(test){
            $('div[data-product]').parents("li").hide();
            //hide every product after first click on any checkbox

            if( $('#example-category-1').is(':checked') ) {
            //check for checkbox activation and show products if true
                $('div[data-product="1802"]').parents("li").show();
                $('div[data-product="1781"]').parents("li").show();
            }
            if( $('#category-2').is(':checked') ) {
            //check for checkbox activation and show products if true
                $('div[data-product="1348"]').parents("li").show();
                $('div[data-product="1347"]').parents("li").show();
            }
            test =false;
            //set to false so we don't enter this if statement again
        }
    });

For the rest of the time, this is what is handling the filtering (would like to exclude the following code from the first click, but seems to work okay as is):

$('#example-category-1').click(function() {
        if( $(this).is(':checked') ) {
            $('div[data-product="1802"]').parents("li").show();
            $('div[data-product="1781"]').parents("li").show();
        } else {
            $('div[data-product="1802"]').parents("li").hide();
            $('div[data-product="1781"]').parents("li").hide();
        }
    });
    $('#example-category-2').click(function() {
        if( $(this).is(':checked') ) {
            $('div[data-product="1348"]').parents("li").show();
            $('div[data-product="1347"]').parents("li").show();
        } else {
            $('div[data-product="1348"]').parents("li").hide();
            $('div[data-product="1347"]').parents("li").hide();
        }
    });

Final note: I was told that I can manipulate the product object to achieve goals like this one - I kind of know in theory what the product object is, but I don't know how to access it / act on it - any input, test suggestions, or criticism would be greatly appreciated.

I don't see how this is scalable if you have to hard code product id's into your jQuery code and duplicate handlers for every category you may want to add.

I also see no need to have separate logic for "first time" execution vs. every other time.

I would consider putting a data- attribute on each checkbox that can indicate the target category the checkbox filter is to work against. And then put a data- attribute on each product to indicate what category it belongs to.

So let's assume your products are like this:

...

...

And your category checkboxes are like:

<input class="category_filter" data-category-target="1" type="checkbox" value="first-checkbox" />
<input class="category_filter" data-category-target="2" type="checkbox" value="second-checkbox" />

Then your jQuery might be simplified down to something like:

// cache references to DOM elements to eliminate need to requery
var $categoryFilters = $('.category_filter');
var $allProducts = $('.product');
// build out selectors for each category
var productsByCategory = {};
$categoryFilters.each(function()) {
     var category = $(this).data('category-target');
     productsByCategory[category] = $allProducts.filter('[data-category=' + category + ']');
});

// now attach change handler to filters
$categoryFilters.on('change', function() {
    $allProducts.hide();
    $categoryFilters.each(function() {
        var target = $(this).data('category-target');
        if($(this).is(':checked')) {
            productsByCategory[target].show();
        }
    }
});

Why are you loading two versions of jQuery?  If this is absolutely unavoidable,  you should be aliasing these versions appropriately (i.e. to something other than $) so that you can write code against whichever version is pertinent to that section of code.

Anytime you find yourself duplicating code for cases like you have with #example-category-*, this should be an immediate red flag to you that have established a coding antipattern.  For this specific example, what you really have is class level behavior (all of the category checkboxes exhibit similar behavior in this case).  That typically mean you should be writing code against the class of elements, not against individual unique id's.


Flask form validation technique This is currently the code that I'm using to validate and check for errors in a flask app, where the form fields are coming from a /registration html page:

@app.route("/register", methods=["GET", "POST"])
def register():
    if request.method == "POST":
        if request.form['register'] == 'Register':
            if request.form.get("eulacheck") != "on":
                return regError("Please read and agree to the Terms and Conditions and the License Terms.")
            if request.form['username'] == "":
                return regError("Please select a username.")
            if request.form['password1'] == "":
                return regError("Please enter a password.")
            if request.form['password2'] == "":
                return regError("Please confirm password.")
            if request.form['password1'] != request.form['password2']:
                return regError("Passwords did not match. Please enter passwords again.")
            username = request.form['username']
            password = request.form['password1']
            hash = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt())
            id = randint(1000,9999)
            if request.form['email1'] != "":
                if request.form['email2'] == "":
                    return regError("Please confirm email.")
                if request.form['email1'] != request.form['email2']:
                    return regError("Email addresses did not match. Please enter emails again.")
                email = request.form['email1']
                insert_db("INSERT INTO Users (ID, Username, Email, Hash) VALUES (?, ?, ?, ?)", (id, username, email, hash))
                flash("Account registration successfull! Please use your credentials to login below.")
                return render_template("login.html",pageType=['login'],flashType="info")
            insert_db("INSERT INTO Users (ID, Username, Hash) VALUES (?, ?, ?)", (id, username, hash))
            flash("Account registration successfull! Please use your credentials to login below.")
            return render_template("login.html",pageType=['login'],flashType="info")
    return render_template("register.html",pageType=['register'])

def regError(message):
    flash(message)
    return render_template("register.html",pageType=['register'],flashType="danger")

This code seems a bit messy and repetitive, is there a better way that I should be doing this?
  Thanks a lot!

Looking at register(), the format is as following:

def register():
    if request.method == "POST":
        if request.form['register'] == 'Register':
            stuff()
    return render_template("register.html",pageType=['register'])

This can easily be condensed using and:

def register():
    if request.method == "POST" and request.form['register'] == 'Register':
            stuff()
    return render_template("register.html", pageType=['register'])

Also, you are not inserting whitespaces between arguments.

stuff(3,3)  # <--Unconventional
stuff(3, 3) # <--More conventional

Another stylistic issue is some of your lines are too long. For all lines, line.length <= 79. You can read about the specific python conventions on the Python website.
For making it look less messy and repetitive, look at this answer to a Stack Overflow question. It is about implementing switch and case statements into Python, which seems perfect for your situation.


Merge sort implementation and improvements I would like your opinion on my Java implementation of the merge sort algorithm on an integer array, since I'm not really into algorithms I'd like to know from you if this is a correct implementation and if it can be improved somehow.

For example, it requires a lot of space for the sub arrays, but this should be normal for the merge sort as it is not space constant. I tested it and it seems to work.

private int[] mergeSort(int[] array) {

    if (array.length == 1) return array;

    int start = 0;
    int end = array.length;
    int middle = (end-start)/2;

    int[] left = Arrays.copyOfRange(array, start, middle);
    int[] right = Arrays.copyOfRange(array, middle, end);

    int[] sortedLeft = mergeSort(left);
    int[] sortedRight = mergeSort(right);

    return merge(sortedLeft, sortedRight);

}

private int[] merge(int[] first, int[] second) {

    int[] result = new int[first.length + second.length];

    int posFirst = 0;
    int posSecond = 0;
    int resultPos = 0;

    while (resultPos < result.length) {

        if (posFirst == first.length) {
            result[resultPos++] = second[posSecond++];
        }
        else if (posSecond == second.length) {
            result[resultPos++] = first[posFirst++];
        }
        else if (first[posFirst] > second[posSecond]) {
            result[resultPos++] = second[posSecond++];
        } else {
            result[resultPos++] = first[posFirst++];
        }

    }

    return result;

}

one of the things you can do is pass in the original array to merge and use that as result:

    return merge(sortedLeft, sortedRight, array);
}

and

private int[] merge(int[] first, int[] second, int[] result) {

    int posFirst = 0;
    int posSecond = 0;
    int resultPos = 0;

This reduces the space needed during the merge portion. Though at the cost of modifying the array the user put in.


A complex selection LINQ query I am stuck on recursive selection of the objects for TreeView.

I have some class IMenuDataSet, that stores objects, named 'I' in the List property:

public partial class IMenuDataSet
{
    private List<I> itemsField = new List<I>();

Each 'I' object has XDocument doc and public IT[] T properties:

public partial class I
{

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public XDocument doc;
    public IT[] T

And IT object is

public partial class IT
{

    private string nField;

    private string valueField;

IT object is a part of serialized XML, that could looks like

<?xml version="1.0" encoding="UTF-8"?>
<I c="PieMenuCategory" i="pie_menu_category" m="abc" n="someM" s="16312547127249792256">
  <T n="_collapsible">False</T>
  <T n="_display_name">0x3F70BCAA</T>
  <T n="_parent">12342647638224932948</T>
</I>

An important note: each XML file (each IT object) has s attribute, the value of this attribute is ID of the I tag (IT object).
ID of the parent element stored in T tag, that has attribute n="_parent" .
Each main (top/root) hasn't such <T n="_parent"> tag.

I need to build TreeView with LINQ query, something like an example in the How to use Linq to Xml to get tree data with recursive querying?

var dataSource = (from results in ids.Items.Descendants("nodeObject")
                                  select new nodeObject
                                  {
                                      nodeName = results.Element("nodeName").Value.ToString(),
                                      nodeChildren = this.GetChilden(results)
                                  });

Currently, my solution is

            List<I> xi = (from c in ids.Items
                          where !(
                                    from b in c.T.ToList()
                                    select b.n
                                  ).Contains("_parent")
                          select c
                 ).ToList<I>();

            foreach (I iobj in xi)
            {
                String nameId = iobj.T.ToList()
                    .Where(zxc => zxc.n.Equals("_display_name"))
                    .First().Value;

                uint elNameUint = Convert.ToUInt32(nameId, 16);
                String elName = translations[elNameUint];

                TreeNode treeNode = new TreeNode(elName);
                treeNode.Tag = iobj.rie.Instance;

                treeView1.Nodes.Add(treeNode);
            }

            xi = (from c in ids.Items
                  where (
                            from b in c.T.ToList()
                            select b.n
                          ).Contains("_parent")
                  select c

                ).ToList<I>();

            while (xi.Count > 0)
            {
                foreach (I iobj in xi)
                {
                    String nameId = iobj.T.ToList()
                        .Where(zxc => zxc.n.Equals("_display_name"))
                        .First().Value;

                    String parentId = iobj.T.ToList()
                       .Where(zxc => zxc.n.Equals("_parent"))
                       .First().Value;

                    uint elNameUint = Convert.ToUInt32(nameId, 16);
                    String elName = translations[elNameUint];

                    TreeNode treeNode = new TreeNode(elName);
                    treeNode.Tag = iobj.rie.Instance;

                    TreeNode ttmp = FindNode(treeView1.Nodes, parentId);
                    if (ttmp != null)
                    {
                        ttmp.Nodes.Add(treeNode);
                        xi.Remove(iobj);
                        break;
                    }
                }
            }

            treeView1.ExpandAll();

But I think, that my solution with breaks isn't a very  clean solution and I was hoping to figure out a more elegant solution using LINQ

Don't fetishise List<>

            List<I> xi = (from c in ids.Items
                          where !(
                                    from b in c.T.ToList()
                                    select b.n
                                  ).Contains("_parent")
                          select c
                 ).ToList<I>();

c.T is an array: what benefit is there to calling ToList() on it? There are two reasons for using ToList() on an enumerator: it's a lazy enumerator which you want to evaluate fully, or you need some specific method from List. Here neither of these applies.

It's not clear to me that xi needs to be eager either. I think that a first refactor could be to

            var xi = (from c in ids.Items
                          where !(
                                    from b in c.T
                                    select b.n
                                  ).Contains("_parent")
                          select c
                 );

Personally I'm not a fan of the SQL-like LINQ syntax. I'd find this more readable as

            var xi = ids.Items.Where(item => !item.T.Any(t => t.n == "_parent"));

There are various other instances of the same unnecessary or even counterproductive use of ToList().

Learn the LINQ overloads

                String nameId = iobj.T.ToList()
                    .Where(zxc => zxc.n.Equals("_display_name"))
                    .First().Value;

Instead of Where(...).First() you can just use First(...):

                String nameId = iobj.T.First(zxc => zxc.n.Equals("_display_name")).Value;

Don't do pointless work

            while (xi.Count > 0)
            {
                foreach (I iobj in xi)
                {
                    String nameId = ...;

                    String parentId = ...;

                    // Do a lot of work with nameId
                    ...

                    TreeNode ttmp = FindNode(treeView1.Nodes, parentId);
                    if (ttmp != null)
                    {
                        ttmp.Nodes.Add(treeNode);
                        xi.Remove(iobj);
                        break;
                    }
                }
            }

A given I instance might not find its parent until the 100th time round the outer loop, which means that 99 times it will construct a node and then hand it off to the garbage collector without it ever being needed. That's not only bad for performance: it's confusing for the maintenance programmer. Compare this rewrite:

            while (xi.Count > 0)
            {
                foreach (I iobj in xi)
                {
                    String parentId = ...;

                    TreeNode ttmp = FindNode(treeView1.Nodes, parentId);
                    if (ttmp != null)
                    {
                        String nameId = ...;

                        // Do a lot of work with nameId
                        ...

                        ttmp.Nodes.Add(treeNode);
                        xi.Remove(iobj);
                        break;
                    }
                }
            }

Avoid linear time insertions, deletions, and searches where possible

E.g.

                    String nameId = iobj.T.ToList()
                        .Where(zxc => zxc.n.Equals("_display_name"))
                        .First().Value;

                    String parentId = iobj.T.ToList()
                       .Where(zxc => zxc.n.Equals("_parent"))
                       .First().Value;

Even rewriting that to ditch to ToList() and use First(...), it's doing multiple linear searches through the same array. I would want to investigate whether I could refactor to replace IT[] T with IDictionary<string, string> T.

If I can't, I would strongly consider introducing a local struct or class to wrap iobj, nameId, and parentId so that the search can be done once.

Then in this main loop, xi has elements at random positions selected for removal. In that case it should under no means be a List<>. ISet is far more appropriate for random removals.

FindNode isn't supplied, but I suspect that it also does a linear search. There's nothing stopping you from introducing an IDictionary<string, TreeNode> to find the parents quickly.

Think in parallel

(Or: a lot of the preceding was because they're important lessons, but I'm now going to recommend throwing most of the code away and rewriting from scratch).

Why is it necessary to first find the roots and then build parents? And why is it necessary to break out of the inner loop in the second half when you've found a node whose parent is already in the tree? [NB That's a rhetorical question: I know that you're doing it to avoid a concurrent modification exception, but there are standard workarounds for that which are less inefficient].

In particular, is there any reason not to structure things like this?

IDictionary<string, Tuple<I, TreeNode>> nodes = ids.Items.ToDictionary(
    item => ...,
    item => ...);

foreach (var pair in nodes.Values)
{
    if (ItemHasParent(pair.Item1))
    {
        TreeNode parentNode = ...;
        parentNode.Nodes.Add(pair.Item2);
    }
    else
    {
        treeView1.Nodes.Add(pair.Item2);
    }
}


Recycling Internet Explorer Is there anything I can do to this code to optimize it to use less resources?

write-host (get-date -format s) " Beginning script..."
Add-Type -AssemblyName System.Windows.Forms
do{
    $len = [System.Windows.Forms.Screen]::AllScreens.Length;
    if($len-eq1){
        $ie = Get-Process -Name iexplore -ErrorAction SilentlyContinue
        if ($ie -ne $null){
            Write-Host (get-date -format s) "Stopping IE..."
            $ie | Stop-Process
        }
    }elseif($len-eq2){
        $ie = Get-Process -Name iexplore -ErrorAction SilentlyContinue
        if ($ie -eq $null){
            write-Host (get-date -format s) " Starting IE..."
            Start-Sleep -s 5 #5
            $ie = Open-InternetExplorer -Url http://localhost/ -Left 1366 -FullScreen -InForeground #This is an external function that works well.
            write-Host (get-date -format s) " IE Started"
        }
    }
    start-sleep -s 10
}while(1-eq1)

You should have included Open-InternetExplorer - without it, this code is incomplete and does not run.

A few of optimizations...

You will not need to Add-Type if all you need is the screen property. It might also make more sense to use Count rather than Length, afterall this code relies on the number of screens.
Unless the number of screens changes per iteration of the loop, getting this value could be taken outside the loop. Otherwise, consider a switch and remove $len altogether:

Switch ([System.Windows.Forms.Screen]::AllScreens.Count) {
   1 {
      ...
   }
   2 {
      ...
   }
}

The condition for the infinite loop while (1-eq1) can be simplified to avoid having to calculate a result. Use while ($true) or while (1).
When IE is started, the script does not use the return value so it isn't necessary to assign it to $ie:

$null = Open-InternetExplorer -Url http://...

The use of $ieis actually unnecessary in both cases.

The resulting script could look like:

write-host (get-date -format s) " Beginning script..."
do {
   Switch ([System.Windows.Forms.Screen]::AllScreens.Count) {
      1 {
         Get-Process -Name iexplore -ErrorAction SilentlyContinue | Stop-Process -Force
         If ($?) {
            Write-Host (get-date -format s) "Stopping IE..."
         }
      }
      2 {
         if ((Get-Process -Name iexplore -ErrorAction SilentlyContinue) -eq $null){
            write-Host (get-date -format s) " Starting IE..."
            Start-Sleep -s 5 #5
            $null = Open-InternetExplorer -Url http://localhost/ -Left 1366 -FullScreen -InForeground
            write-Host (get-date -format s) " IE Started"
         }
      }
   }
   start-sleep -s 10
} while(1)


Choose the right type for method GetById I designed my repository class and I need some advice from you. Now I have a method GetByID that returns a generic type:

 public T GetById(object id)
 {
    return this.Entities.Find(id);
 }

I'm going to use OData to filter data, and for that (when I'm using $expand for single row) I need to do something like this:

SingleResult.Create(repository.GetByID(id));

But of course I received an error because SingleResult needs a type IQueryable<T>.

So, I decided to change my GetByID to:

public IQueryable<T> GetById(object id)
{
    return this.Entities.FirstOrDefault(p => p.ID == (int)id);
}

Could you please tell me if is it the right way? What type should be returned from GetByID?

I prefer to use T GetById because it is more correct in my opinion, but I don't know.

If you're only going to make the minimal required change

Accommodate the IQueryable requirement in the layer where it is needed, not in your repository layer. That will probably be some object that takes in an entity, wraps it in an IEnumerable, and calls AsQueryable() on it. (Disclaimer: This may or may not work. I haven't tested it.) If you were to do otherwise and change your repo signature to use IQueryable, that would be coupling the repository layer to downstream requirements, which you are correct in wanting to avoid.

A side pet peeve

Please avoid using object in signatures if you're going to use generics in the same class and you don't have a reason to box (I believe DbSet has to use reflection to find the key so it has a boxing requirement, but your code does not require boxing). Use strong typing all the way until you absolutely must cast to object. You'll thank me later.  And make the parameter names expressive:

public TEntity GetById<TKey>(TKey id)
{
    return this.Entities.Find((object)id);
}

Digging deeper

It seems a bit wonky and counterintuitive to have to pass an IQueryable to SingleResult. But come! Join me on a journey in pursuit of greater understanding of this "IQueryable".

An IQueryable is a bit more than just a collection. It functions as a sort of gateway to the data you're after, letting you query that data without having to use its unique language (SQL, or OData queries, what have you). This is accomplished through an associated query provider, which translates the lambda expressions you provide it to the language of your data store.

More importantly, that query provider decides how to compose and manage queries, and that includes utilizing deferred evaluation. The fact that you can pass it a query from your EF context is its way of telling you "I got this bro. This IQueryable has all I need to pull the data from here when I need it." It will then pull the data it needs for the OData query it's building on demand.

If you instead return it an entity, you have eagerly loaded that entity from the DB yourself, when you may not have needed to. Or at the very least, before you needed to.

A Catch-22

Using the generic repository here directly pits the benefits of IQueryable (deferred evaluation, the need for less "intermediary" code) against the reusability of a library that abstracts data access away from queries (is this even being reused?). In order for these two to coexist, some compromises are going to have to be made, compromises that may result in an extremely mediocre "solution."

A simpler appeal

If you look at the way the OData provider functionality is designed, it's evident that it's meant to directly consume EF context queries including in your web app layer. If the DbContext was meant to be shut away inside a library, they would have designed things a lot differently.

To sum up

I would recommend reconsidering the purpose of your generic repository, why you need it, and what it's buying you.


Binary Search in C - Optimization I know that there is a function bsearch present in stdlib.h but still I want to implement this.

This is my code for binary search. Any and all reviews are welcome.

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

int compare (const void * a, const void * b)
{
    return (*(int*)a - *(int*)b);
}

int bin_search(int arr[], int min_index, int max_index, int element)
{
    /*
    Searches for an element in the array arr
    Returns fist index of element if present else returns -1
    */
    if (min_index > max_index){
        return -1;
    }
    else
    {
        //Don't change this assignment of mid_point. It avoids overflow
        int mid_point = min_index + (max_index - min_index)/2;

        if (arr[mid_point] > element){
            return bin_search(arr, min_index, mid_point - 1, element);
        }
        else if (arr[mid_point] < element){
            return bin_search(arr, mid_point + 1, max_index, element);
        }
        else{
            return mid_point;
        }
    }
}

int main()
{
    int length;
    while (1)
    {
        printf("Enter a positive length: ");
        scanf("%d", &length);
        if (length > 1){
            break;
        }
        else{
            printf("You entered length = %d\n\n", length);
        }
    }

    int *arr = malloc(sizeof(int) * length);
    if (arr == NULL)
    {
        perror("The following error occurred");
        exit(-1);
    }

    for (int i = 0; i < length; i++){
        scanf("%d", &arr[i]);
    }

    int element;
    printf("\nEnter the element to be searched: ");
    scanf("%d", &element);

    qsort(arr, length, sizeof(int), compare);
    int index = bin_search(arr, 0, length - 1, element);

    if (index == -1){
        printf("\nElement not in the array");
    }
    else{
        printf("\nIndex of element is %d", index);
    }

    free(arr);
    return 0;
}

The functions says it "Returns fist index of element if present".  If I give it the numbers 2, 2, 2 and ask it to find 2 it says the 1, but the first index with value 2 is clearly 0.

Some minor comments on the code:

the arr parameter to bin_search should be const.  This tells the compiler and the reader that the array is not changed by the function.  The compiler will then enforce this if you, by mistake, try to modify the array data.  The reader/user knows that her data is unchanged after the call.
the parameter names min_index and max_index could be shortened to min and max.  Giving names an appropriate size is a service to the reader (auto-completion by the IDE is a service to you).  In general, the shorter the names, the less dense the code and the easier it is to read.  This can be taken too far of course, once names become meaningless.
Note that it would be more normal to pass the start of the array and its size instead of the array plus two offsets.
functions could be static.  This is of no significance in a one-file program but becomes important with bigger programs.  Making functions and global variables static restricts their scope to the file which allows extra optimisation and reduces namespace pollution.
the output message needs a trailing \n
there is no prompt for the input values - ok that is trivial. Personally I find this sort of test better with values entered on the command line.
exit status is normally 1 (EXIT_FAILURE) on failure, not -1.  On success it is 0 (EXIT_SUCCESS).  These are UNIX conventions.

EDIT

You questioned the use of const. Its utility when used on parameters passed by
reference - pointers/array passed into functions - is, I think clear.

However, its use with parameters passed by value is not so clear.  It plays no
part in the interface seen by callers of a function, as it can have no
influence on the caller.  You can declare a function prototype in a public
header like this

int bin_search(const int arr[], int min, int max, int element);

and then define the function implementation like this:

int bin_search(const int arr[], const int min, const int max, const int element) {...}

And the compiler will be quite happy with the difference.

So it is purely an implementation issue.  Hence you should definitely not use const on pass-by-value parameters in public prototypes, only in the implementation (if at all).  Used in the implementation, const tells the reader and the compiler
that a parameter is not (and cannot be) changed.  This gives the reader some
extra information and of course the compiler will enforce this read-only
behaviour.

So shouldn't you use it on all parameters that are not changed (and some might say that parameters should never be changed)?  Good question!  I never use const on call parameters but would have no hesitation in adding const to a local variable

int cirle_area(double radius)
{
    const double pi = 3.14;
    return pi * radius * radius;
}

A lot of good programming style concerns consistency, so my inconsistency here
is troubling.  And my previous comment - that if you make the element
parameter const, then you should be consistent and do the same for min and
max uses consistency as an argument!

I'm afraid I can do no better than that.  I've programmed for 20 years in C
and have rarely seen const applied to parameters. But maybe it should be.


Auto-populating user info from database Auto populate user info from database when user inputs employee number.  The same applies when the user inputs name/extension/mobile/email/

Some code is duplicated.  Is there any way to make it shorter?

$(document).ready(function () {
    $("#empnum").change(function () {
        var fourdigitpatrn = /^[0-9]{4}$/;
        var idType = this.id;
        var myval = $.trim(this.value);
        if (myval == "" | myval.length < 3 ) return;
        if (fourdigitpatrn.exec(myval)) {
            myval ="EMP_"+myval;
            }
        if (myval.length) {
            $.ajax({
                contentType: "application/json; charset=utf-8",
                url: 'http://myserver.com/helpdesk/check_json.php',
                dataType: 'json',
                data: {type: 'empnum', term: myval},
                success: function (data) {
                    if(typeof data == "undefined" | data == null | data.length == 0){
                    alert("No Result");
                    document.getElementById("form1").reset();
                    return};
                    //console.log(data);
                    $("#empnum").val(data[0].empnum);
                    $("#ext").val(data[0].extension);
                    $("#omobile").val(data[0].omobile);
                    $("#pmobile").val(data[0].pmobile);
                    $("#ename").val(data[0].ename);
                    $("#name").val(data[0].empnum);
                    $("#email").val(data[0].email);
                },
                error: function (result) {
                    alert("Error");
                }
            });
        }
    });
});

$(document).ready(function () {
    $("#ext").change(function () {
        var extpatrn=/^[0-9]{2,11}$/;
        var idType = this.id;
        var myval = $.trim(this.value);
        if (!extpatrn.exec(myval))
        {
        alert ("2~11 digits needed");
        return;
        }
        if (myval == "" | myval.length < 2 ) return;
        if (myval.length) {
            $.ajax({
                contentType: "application/json; charset=utf-8",
                url: 'http://myserver.com/helpdesk/check_json.php',
                dataType: 'json',
                data: {type: 'ext', term: myval},
                success: function (data) {
                    if(typeof data == "undefined" | data == null | data.length == 0){
                    alert("No Result");
                    document.getElementById("form1").reset();
                    return};
                    //console.log(data);
                    $("#empnum").val(data[0].empnum);
                    $("#ext").val(data[0].extension);
                    $("#omobile").val(data[0].omobile);
                    $("#pmobile").val(data[0].pmobile);
                    $("#ename").val(data[0].ename);
                    $("#name").val(data[0].empnum);
                    $("#email").val(data[0].email);
                },
                error: function (result) {
                    alert("Error");
                }
            });
        }
    });
});

//similar script for auto populate on omobile/pmobile/email input
...
...
...

given that you duplicate nearly the whole $.ajax() call you can extract it into a function and add a parameter where it differs::

function doAjax(dataType, myval){
    $.ajax({
        contentType: "application/json; charset=utf-8",
        url: 'http://myserver.com/helpdesk/check_json.php',
        dataType: 'json',
        data: {type: dataType, term: myval},
        success: function (data) {
            if(typeof data == "undefined" | data == null | data.length == 0){
            alert("No Result");
            document.getElementById("form1").reset();
            return};
            //console.log(data);
            $("#empnum").val(data[0].empnum);
            $("#ext").val(data[0].extension);
            $("#omobile").val(data[0].omobile);
            $("#pmobile").val(data[0].pmobile);
            $("#ename").val(data[0].ename);
            $("#name").val(data[0].empnum);
            $("#email").val(data[0].email);
        },
        error: function (result) {
            alert("Error");
        }
    });
}


Filtering Pinboard API results by tags I've got a simple sinatra webapp that pulls data from Pinboard's RSS api, parses the info, and re-displays it. There are 4 tasks I need to perform with the data:

I need to remove all instances of a specific tag from all items (in this case, want)
I need to create a list of all unique tags across all the items returned
I need to dynamically remove certain tags from the list if they are already being used to filter the list
I need to add an attribute to each item that is the host of the url associated with that item (i.e. amazon.com for whatever long amazon url may have been added)
I need to return a list of unique locations across all the items returned

Here is the way I am currently handling this:

items = []
tags = []
locations = []
begin
  JSON.parse(data).each do |item|
    # Remove the tag want from the list of tags,
    # strip any extra whitespace, and add a location attribute
    item['t'].each { |t| t.strip! }
    item['t'].delete_if { |tag| tag == 'want' }

    # Try like hell to parse the url. Assign an error string as a last resort
    begin
      item['l'] = /https?:\/\/(?:[-\w\d]*\.)?([-\w\d]*\.[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?)/i.match(item['u'])[1]
    rescue
      item['l'] = "URL Parse error"
    end

    # Add the item's tags
    item['t'].each do |tag|
      tags << tag unless tags.include? tag or filter_tags.include? tag
    end

    locations << item['l'] unless locations.include? item['l']

    items << item
  end
rescue
end
return items, tags.sort, locations.sort

I really don't like this. It just doesn't feel clean to me, what with the creation of the empty arrays, and adding stuff in, etc. But my thought was that it's better to do a single iteration over the data returned than to do multiple loops. But now I am considering this instead:

items = JSON.parse(data)
# Remove the want tag from all items
items.each { |i| i['t'].delete_if { |t| t == 'want' }}

#generate a list of tags
tags = items.map { |i| i['t']}.flatten.uniq.sort
tags.delete_if { |t| filter_tags.include? t }

# Parse the url
re = /https?:\/\/(?:[-\w\d]*\.)?([-\w\d]*\.[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?)/i
items.each { |i| i['l'] = re.match(i['u'])[1] }

# Generate a list of locations
locations = items.map { |i| i['l']}.flatten.uniq.sort

return items, tags, locations

It feels cleaner, and seems like it's easier to read, but I'm not sure if the nested delete_if in the first bit is going too far. I'm also not sure how I feel about iterating over the items multiple times, or chaining flatten.uniq.sort for both tags and locations

The second version is undoubtedly better, more readable, even if you iterate multiple times over the data.

The problem is: both versions, specially the first, suffer from a common problem: imperative style ("do this, do that"). Are you familiar with the difference between imperative (focus on statements and state change) and functional programming (focus on expressions and immutable data)? it's easy, just try to code without in-place updates (no each, delete_if, bang! methods, ...) and you'll see that you'll end up writing less code, easier to test, to debug and to understand. You'll pay a bit of memory/speed performance, but it's usually worth it.

Now, using a functional approach and taking advantage of existing libraries (uri), that's what I'd write (some details may not be right, but you get the idea):

require 'uri'

def parse(data, filter_tags)
  items, nested_tags, nested_locations = JSON.parse(data).map do |item|
    location = URI.parse(item["l"]).host rescue "URL Parse error"
    tags = item["t"].map(&:strip) - filter_tags
    [item, tags, location]
  end.transpose

  [items, nested_tags.flatten(1).uniq.sort, nested_locations.flatten(1).uniq.sort]
end

More on functional programming with Ruby: http://code.google.com/p/tokland/wiki/RubyFunctionalProgramming


Checking the control of an entity I have a method which checks if a control of an entity's attribute is 'multiControl' or not:

private boolean entityHasMultiControl(IEntityMetadata entityMetadata) {

    for(IAttributeMetadata attr : entityMetadata.getAttributes()){
        for(IControl control : attr.getAttributeType().getControls()){
            if(control.getIsMultiControl() == true)
                return true;
        }

    }
    return false;
}

Can this be simplified further in Java 8?

Assuming that entityMetadata.getAttributes() and attr.getAttributeType().getControls() returns some kind of Collection, the answer is an absolute YES.

entityMetadata.getAttributes().stream()
    .flatMap(attr -> attr.getAttributeType().getControls().stream())
    .anyMatch(control -> control.getIsMultiControl());

Other notes:

Comparing with == true is not necessary, that part could be removed entirely.
Prefixing interfaces with I is not something commonly done in Java, it is not recommended in any naming conventions. It is, however, done in C#. C# is not Java.
getIsMultiControl is a very weird name. Should preferably be named isMultiControl.


Pull back records that include child records I am trying to pull back records that include child records for the View I'm putting together. Right now I have a working Linq statement, but it's big, and I think I could make it cleaner.

var reqMatsInc = (from rm in db.RequiredMaterial
                    join p in db.Project on rm.ProjectTemplateId equals p.ProjectTemplateId
                    where p.ProjectId == id
                    select new RequiredMaterialViewModel
                    {
                        RequiredMaterialId = rm.RequiredMaterialId,
                        ProjectTemplateId = rm.ProjectTemplateId,
                        RequiredMaterialName = rm.RequiredMaterialName,
                        RequiredMaterialDescription = rm.RequiredMaterialDescription,
                        CustomFormId = rm.CustomFormId,
                        AcceptFile = rm.AcceptFile,
                        RequiredMaterialRoles = (
                        from rmr in db.RequiredMaterialRole
                        where rmr.RequiredMaterialId == rm.RequiredMaterialId
                        select new RequiredMaterialRoleViewModel
                        {
                            RequiredMaterialRoleId = rmr.RequiredMaterialRoleId,
                            RequiredMaterialId = rmr.RequiredMaterialId,
                            RoleId = rmr.RoleId,
                            RoleName = rmr.Role.RoleName,
                            IsActive = rmr.IsActive
                        }
                        ).ToList()
                    }
);

Right now the ViewModel classes are pretty much mirror images of the Code First class they represent. db is my DbContext.

Here are some of the ViewModel classes:

RequiredMaterialViewModel

public class RequiredMaterialViewModel
{
    public Guid RequiredMaterialId { get; set; }

    public Guid ProjectTemplateId { get; set; }

    public ProjectTemplate ProjectTemplate { get; set; }

    [Display(Name = "Required Material Name")]
    public string RequiredMaterialName { get; set; }

    [Display(Name = "Required Material Description")]
    public string RequiredMaterialDescription { get; set; }

    public Guid? CustomFormId { get; set; }

    public CustomForm CustomForm { get; set; }

    public bool AcceptFile { get; set; }

    public ICollection<RequiredMaterialRoleViewModel> RequiredMaterialRoles { get; set; }

    public bool IsActive { get; set; }
}

RequiredMaterialRoleViewModel:

public class RequiredMaterialRoleViewModel
{
    public Guid RequiredMaterialRoleId { get; set; }

    public Guid RequiredMaterialId { get; set; }

    public int RoleId { get; set; }

    public string RoleName { get; set; }

    public bool IsActive { get; set; }
}

I can just get the RoleName for this View; that simplifies it some.

The fact that your ViewModel is almost an identical copy of your entity class is a code smell. ViewModel should represent data as it is to be shown on screen, that means that most properties should be simple types (string, int and bool mostly). Weed them out first, I don't believe you need all the IDs either!

Your repository doesn't do any real projection, so you can get rid of the entire select clause. Use eager loading (Include()) instead of joins (your domain and/or EF config might need to be modified):

var reqMatsInc = (from rm in db.RequiredMaterial.Include(rm => rm.RequiredMaterialRoles)
                  join p in db.Project on rm.ProjectTemplateId equals p.ProjectTemplateId
                  where p.ProjectId == id).

Then, have another method in another class (preferrably in your UI layer) map the result to  your ViewModels.

(may I suggest AutoMapper to handle this task for you, it was made for exactly these kind of things).


Tkinter application with multiple windows I'm creating an application that requires multiple tkinter windows. The code below works the way how I want it to work, but I'm not sure if I'm doing it in the proper way.

The first window (and in this case, the main window as well) contains three buttons as of now, 1 label which acts as a title, and a treeview.

The "Add Member" button is the only button that has functionality as of now. When clicked, it will pop a new window. That said window contains label.

To sum it up, I would like to humbly ask if I'm doing it properly, or there is a better and more accurate way of writing this code.

class Application(tk.Frame):
    def __init__(self,parent,*args,**kwargs):
        tk.Frame.__init__(self,parent)
        self.parent = parent
        self.parent.title("Senior Citizen Association")

        ttk.Label(text="SENIOR CITIZEN ASSOCIATION").grid(row=0,column=0,columnspan=5,sticky="n")
        ttk.Button(text="Add",command=self.add_member).grid(row=1,column=0,sticky="w")
        ttk.Button(text="Delete").grid(row=2,column=0,sticky="w")
        ttk.Button(text="Exit",command=lambda:self.parent.destroy()).grid(row=3,column=2,sticky="se")

        self.tree = ttk.Treeview(height=15,column=("First Name","Middle Initial","Last Name"))
        self.tree.heading("#0",text="First Name",anchor="w")
        self.tree.heading("#1",text="Middle Initial",anchor="w")
        self.tree.heading("#2",text="Last Name",anchor="w")
        self.tree.heading("#3",text="Status",anchor="w")
        self.tree.grid(row=1,column=1,rowspan=2,columnspan=2,sticky="nsew")

    def add_member(self):
        self.addMemberWindow = tk.Toplevel(self.parent)
        self.addClass = NewMember(self.parent,self.addMemberWindow)

class NewMember(tk.Frame):
    def __init__(self,parent,child,*args,**kwargs):
        tk.Frame.__init__(self,child)
        self.parent = parent
        self.child = child
        self.child.title("Add Member")

        ttk.Label(self.child,text="Add Member").grid(row=0,column=0,sticky="nsew")

if __name__ == "__main__":
    app = tk.Tk()
    Application(app)
    app.mainloop()

For me, it is a pretty piece of code. I learned much from it. Also, I'm not sure if it is a proper way. But, it works fine and is easy to read :)


Small String hashing and compare The idea is to convert first 8 character from the C string to uint64_t or uint32_t, then to convert to Big Endian.

The value can be stored and compared with same values and result will be compatible with memcmp or strcmp.

The only exception is if both "strings" are 8 bytes long or 4 bytes long for uint32_t and are equal, the function must return sentinel value, e.g. std::numeric_limits<int>::max(), so caller to know he will need to compare the real strings in order to find the result.

For simplicity, I assume code runs on Little Endian machine and convert to Big Endian is done by __builtin_bswap64 (available on gcc and clang).

Here is the code + some tests

#include <cstdint>
#include <cstring>
#include <limits>

namespace sshhash_implementation{
    constexpr auto bswap(uint64_t x){
        return __builtin_bswap64(x);
    }

    constexpr auto bswap(uint32_t x){
        return __builtin_bswap32(x);
    }

    template<typename T>
    T hash(const char *src){
        constexpr auto capacity = sizeof(T);

        union{
            char    s[capacity];
            T   u = 0;
        };

        strncpy(s, src, capacity);
        return bswap(u);
    }

    template<typename T>
    constexpr T mask(){
        return bswap(T{ 0xFF } << (sizeof(T) - 1) * 8);
    }

    template<typename T>
    int sscmp(T const a, T const b){
        if (a > b)
            return +1;

        if (a < b)
            return -1;

        // equal, check size

        constexpr auto m = mask<T>();

        if ( (a | b) & m )
            return std::numeric_limits<int>::max();

        return 0;
    }
}

template<typename T>
auto sshash(const char *s){
    return sshhash_implementation::hash<T>(s);
}

auto sshash8(const char *s){
    return sshash<uint64_t>(s);
}

auto sshash4(const char *s){
    return sshash<uint32_t>(s);
}

template<typename T>
auto sscmp(T a, T b){
    return sshhash_implementation::sscmp(a, b);
}

auto sscmp8(uint64_t a, uint64_t b){
    return sscmp(a, b);
}

auto sscmp4(uint32_t a, uint32_t b){
    return sscmp(a, b);
}

#include <cstdio>

template<typename T = uint32_t>
void testC(const char *sa, const char *sb, bool const overflow){
    auto const a = sshash<T>(sa);
    auto const b = sshash<T>(sb);

    int const  x = strncmp(sa, sb, sizeof(T));
    int const  y = sscmp(a, b);

    bool const ok = [=](){
        if (overflow)
            return x == 0 && y == std::numeric_limits<int>::max();
        else
            return x == y;
    }();

    printf(
        "%-8s | %-8s | %2d | %2d | %-8s\n",
        sa,
        sb,
        x,
        y,
        ok ? "OK" : "Error"
    );
}

template<typename T = uint32_t>
void test(const char *sa, const char *sb, bool const overflow = false){
    testC(sa, sb, overflow);
    testC(sb, sa, overflow);
}

int main(){
    test("aaa"  , "aaa"     );

    test("aaa"  , "b"       );
    test("aaaa" , "b"       );
    test("aaa"  , "bbbb"    );
    test("aaaa" , "bbbb"    );

    test("aaaa" , "aaaa", true  );
}

Since we assume GCC or Clang, we can easily make the code adapt to the target's endianness:

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
    constexpr auto hton(std::uint64_t x){
        return __builtin_bswap64(x);
    }

    constexpr auto hton(std::uint32_t x){
        return __builtin_bswap32(x);
    }
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
    constexpr auto hton(std::uint64_t x){
        return x;
    }

    constexpr auto hton(std::uint32_t x){
        return x;
    }
#else
#error Unsupported byte order
#endif

I've used the conventional "host to net" name for the function, to be clearer.

Some identifiers are misspelt: std::uint32_t, std::uint64_t, std::strncpy, std::strncmp, std::printf.

This function is unnecessary:

template<typename T>
auto sscmp(T a, T b){
    return sshhash_implementation::sscmp(a, b);
}

Much simpler to write

using sshhash_implementation::sscmp;

No need for the immediately-executed lambda in testC():

bool const ok = [=](){
    if (overflow)
        return x == 0 && y == std::numeric_limits<int>::max();
    else
        return x == y;
}();

That can be written as a simple expression:

bool const ok = overflow
    ? x == 0 && y == std::numeric_limits<int>::max()
    : x == y;

In mask(), I think the constant 8 was intended to be CHAR_BIT, since it's converting a count of char to number of bits.


Breaking a string into substrings I wrote some code which divides a line through the words of the text so that each substring is no longer than MaxWidth. It works well, but it's very slow.

Pattern pattern = Pattern.compile("(.{1," + symbols + "}(\\b|\\s))"); // symbols - MaxWidth
Pattern pattern2 = Pattern.compile("\\s.*");

while ((line = in.readLine()) != null) {  // reading file by lines
    String dopLine = "";
    if (pattern2.matcher(line).matches()) {
        // If a line begins with a space, it is the beginning of a paragraph and need to add \ n
        if(!tempDopLine.equals("")) { // tempDopLine - Some part of the previous line, which is not full screen
            tempStringBuffer.append(tempDopLine);
            tempStringBuffer.append("\n");
            lineCounter++;
            if (lineCounter == lines) {
                addPage(tempStringBuffer); // create page
                tempStringBuffer = new StringBuilder("");
                lineCounter = 0;
                numberOfPages++;
            }

        }
        tempDopLine = "";
        dopLine = line;
    } else {
        dopLine = tempDopLine + " " + line; // if this line
    }
    Matcher matcher = pattern.matcher(dopLine); // divide a string into a substrings

    HashMap<Integer, String> temp = new HashMap<Integer, String>();
    int i = 0;
    while (matcher.find()) {
        temp.put(i, matcher.group());
        i++;
    }
    for(i = 0; i < temp.size(); i++) {
        if (i<(temp.size()-1)) {
            String tempL = temp.get(i);

            tempStringBuffer.append(tempL);

            tempStringBuffer.append("\n");
            lineCounter++;
            if (lineCounter == lines) {
                addPage(tempStringBuffer);
                tempStringBuffer = new StringBuilder("");
                lineCounter = 0;
                numberOfPages++;
            }

        } else {
            tempDopLine = temp.get(i); // The last part of the string remember to display it along with the next line
        }
    }
}

Few thought,

You do a while loop  ( while( matcher.find() )  to find all matchs, then you do a for loop to deal with it. I think that can be done in only one loop.

in you last for loop, you could remove the first if :

   for(i = 0; i < (temp.size() -1); i++) {

     String tempL = temp.get(i);

     tempStringBuffer.append(tempL);

     tempStringBuffer.append("\n");
     lineCounter++;
     if (lineCounter == lines) {
            addPage(tempStringBuffer);
            tempStringBuffer = new StringBuilder("");
            lineCounter = 0;
            numberOfPages++;
     }

   } // End for loop

   tempDopLine = temp.get(temp.size()-1);

}

Other suggestion  the HashMap temp  got be only a array, because right now your code do a lot of autoboxing  ( from int to Integer) add and to retrieve information from your HashMap.

Edit: Here a quick example to merge your two loops:

String tempL = matcher.group();
while (matcher.find()) {

   tempStringBuffer.append(tempL);

   tempStringBuffer.append("\n");
   lineCounter++;
   if (lineCounter == lines) {
       addPage(tempStringBuffer);
       tempStringBuffer = new StringBuilder("");
       lineCounter = 0;
       numberOfPages++;
    }
    tempL =  matcher.group();

}

tempDopLine = tempL;


Automate html file editing with python - Change the author Hello I would like to use python to change the author of an html file, in order to do so I have written a few lines.

First I have used the HTMLParser class from the standard library to make a class that indicates which line of the html page needs to be changed.

from html.parser import HTMLParser

class AuthorFinder(HTMLParser):
    def __init__(self):
        super().__init__()
        self._edition_line = None

    def handle_starttag(self, tag, attrs):
    '''
    If the tag handled is the meta tag containing the author's name, then
    the value of _edition_line will be set to the current line (starting from 0)
    '''
    if tag.lower() == 'meta':
        for a_tuple in attrs:
            name, value = (field.lower() for field in a_tuple)
            if name == 'name' and value == 'author':
                # getpos() returns (line, column) starting from (1, 1)
                # whereas I want the first index to be 0.
                self._edition_line = self.getpos()[0] - 1

    @property
    def edition_line(self):
        return self._edition_line

I then use that class to get the line of the meta tag that contains the author. The following function changes the whole line and return and the new html page as a string.

def change_author(html_code, author):
    html_parser = AuthorFinder()
    html_parser.feed(html_code)
    author_line = html_parser.edition_line
    html_lines = html_code.splitlines()
    new_tag = '<meta name="author" content="{}">'.format(author)
    html_lines[author_line] = new_tag
    new_html = '\n'.join(html_lines)
    return new_html

From there you can easily change the author of several html files with something as simple as this:

# put the path to your html files in here
html_files = ['example1.html', 'example2.html']
new_author = 'Mario Luigi'
for filename in html_files:
    with open(filename, 'rt') as f:
        html_code = f.read()
    new_code = change_author(html_code, new_author)
    new_filename = '{}-NewAuthor.html'.format(filename.split('.')[0])
    with open(new_filename, 'w') as f:
        f.write(new_code)

Here is my first, quick attempt at automating file editing. I would like, in the future, to automate more changes but for now let's focus on this one. The script works fine but I'm pretty sure it can be improved in several ways as:

the class AuthorFinder will keep on parsing even though the desired meta tag has been found
if no meta tag with the author's name has been found, no author will be added
the previous indentation is ignored
...

I will try to find ways to improve this, and hope to get suggestions from here as well. Thank you for reading, and good day.

I'm going to try writing a few different versions of your program. I think it would make for a nice, basic comparison of the various alternatives.
Of course, this all hinges on the number of changes, and their complexity, you wish to make. In that regard, it would be nice to get some more information on those changes, even if you haven't written the code for it yet.

Pure lxml, .find() with a simple XPath expression.
from lxml import etree

parser = etree.HTMLParser()
tree = etree.parse('../resources/author_parser_test_1.html', parser=parser)

author_tag = tree.find("./head/meta[@name='author']")

if author_tag is None:
    print("Couldn't find author tag")
else:
    author_tag.set('content', 'New Author')
    tree.write('../out/author_parser_test_1_res.html', method='html', pretty_print=False)

BeautifulSoup using lxml as the parser
from bs4 import BeautifulSoup

with open('../resources/author_parser_test_1.html', 'r') as file_1:
    soup = BeautifulSoup(file_1, features='lxml')

author_tag = soup.find('meta', attrs={'name': 'author'})

if author_tag is None:
    print("Couldn't find author tag")
else:
    author_tag['content'] = 'New author'
    with open('../out/author_parser_test_1_res.html', 'w') as file_1:
        file_1.write(soup.prettify())

I will keep updating this post as I work.


Write the HTML markup for several of (profile picture + message heading + message) I was asked to write the HTML markup and CSS like the following:

User's profile picture on the left
his message heading
his message content

I was so concerned with how to correctly make it displayed (with JavaScript data and render it into HTML, and the HTML would result as:

heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

heading

content...

heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

with CSS

.comment-section { width: 400px; font: 13px Arial, sans-serif; }
.comment-section .comment { margin: 10px 0; }
.comment-section img, .comment-section .comment-text { display: inline-block; }
.comment-section img { width: 50px; height: 50px; margin-right: 5px; }
.comment-section .comment-text { vertical-align: top; width: 340px; }
.comment-section h4 { margin: 0 0 2px; }

Example at: https://jsfiddle.net/zayquc9q/2/

Should I actually have used
 and
:

heading

content...

Or
 and
 instead?

heading

content...

So although what I did looked pretty good in the real webpage, I don't know whether it was that it wasn't semantic markup that they disqualified me (for a JavaScript / React / Redux position), after this phone interview.

If it's all about semantic then let's see what HTML5 specs say (emphasis is mine):

  The article element represents a component of a page that consists of a self-contained composition in a document, page, application, or site and that is intended to be independently distributable or reusable, e.g. in syndication. This could be a forum post, a magazine or newspaper article, a blog entry, a user-submitted comment, an interactive widget or gadget, or any other independent item of content.

In this case it seems that
 is an appropriate choice for your comments. Is it
 an appropriate container? I don't think it is, specs says:

  The section element represents a generic document or application section…The section element is not a generic container element. When an element is needed only for styling purposes or as a convenience for scripting, authors are encouraged to use the div element instead. A general rule is that the section element is appropriate only if the element’s contents would be listed explicitly in the document’s outline.

I agree that interpretation can be little bit subjective here but I'd take it literally: you do not have a specific header for this section. In your case if
 is not to group a section of your document (for example an outer
 with its list of comments) then I'd use the good old
:

If, however, comments are just a part (section) of your main document which you may want to see in document outline and not simply a styling container (remember that it's important for screen-readers and all the others assistive tools) then don't refrain and use
.

Now let's talk about article content. You're using
 for styling purposes. I don't see whole page outline then I can't comment on this but keep in mind that heading elements should be used to denote an hypothetical structure, not for their styling. Imagine to replace your
Title
 with
Title
 where div.header is appropriately styled. Does the meaning of your document is lost? If answer is no then you're using heading elements because of styling and you shouldn't.

HTML5 introduced a new element  which may be appropriate in your case:

      ...

Note that even if you really want to keep  because of its meaning in document outline I'd wrap it inside an  section.

Title

What is it useful for? For example if tomorrow you will add subtitle to hold the date and the author of the comment...

Now let's think about . First of all it has to have an alt attribute but I'd also consider to move it into its own  element. It may help screen readers to keep "focus" on the content. About this...do not forget to include all the proper aria- attributes!


Depth and breadth first search I am practicing interview questions, I wrote this code to perform BFS and DFS on a graph in Python.

How can it be optimized, and how can it be made more readable?

class Graph:
    def __init__(self):
        self.graph = defaultdict(list)

    def add_edge(self, u, v):
        self.graph[u].append(v)

    def dfs(self, node):
        visited = [False for i in range(len(self.graph))]
        stack = []
        stack.append(node)
        visited[node] = True
        while stack:
            node = stack.pop()
            print node
            for i in self.graph[node]:
                if visited[i] == False:
                    visited[i] = True
                    stack.append(i)

     def Bfs(self, node):
        visited = [False for i in range(len(self.graph))]
        queue = []
        queue.append(node)
        visited[node] = True
        while queue:
            node = queue.pop(0)
            print node
            for i in self.graph[node]:
                if not visited[i]:
                    queue.append(i)
                    visited[i] = True

Small syntactical changes

Bfs should at least be bfs. Some would probably argue that you should spell out depth_first_search and breadth_first_search entirely. I personally like small names,  but regardless, the capital B should not be there.

You wrote this one time:

if visited[i] == False:

And used the correct idiom the second time:

if not visited[i]:

Performance

According to here, generating a list with multiplication is the fastest way, so change both occurrences of:

visited = [False for i in range(len(self.graph))]

to:

visited = [False] * len(self.graph)


Serializing tabular data in ruby -- is map, flatten, hash the correct approach? I wanted a hash which lets me reference Japanese syllables by their romanized names. In hindsight I could have searched for an existing one column table, but I wanted to improve my ruby by writing a function which serializes these multi-column tables I found on wikipedia:

 katakana:
 v_eng:   a i u e o
 v_jap:   ア イ ウ エ オ
 K:   カ キ ク ケ コ
 S:   サ シ ス セ ソ
 T:   タ チ ツ テ ト
 N:   ナ ニ ヌ ネ ノ
 H:   ハ ヒ フ ヘ ホ
 M:   マ ミ ム メ モ
 Y:   ヤ _ ユ _ ヨ
 R:   ラ リ ル レ ロ
 W:   ワ ヰ _ ヱ ヲ
hiragana:
 v_eng:   a i u e o
 v_jap:   あ い う え お
 k:   か き く け こ
 s:   さ し す せ そ
 t:   た ち つ て と
 n:   な に ぬ ね の
 h:   は ひ ふ へ ほ
 m:   ま み む め も
 y:   や _ ゆ _ よ
 r:   ら り る れ ろ
 w:   わ ゐ _ ゑ を
 nn:   ん _ _ _ _

I was able to create the serializing function, syllabarys():

#!/usr/bin/env ruby
require 'yaml'

def syllabarys
@syllabarys ||= lambda{
   raw_data = YAML.load_file 'japanese.dic'

   syllabary_names = ['katakana','hiragana']

   a = syllabary_names.map{|syllabary|

      syllabary_data = raw_data[syllabary]

      veng,vjap = syllabary_data['v_eng'].split, syllabary_data['v_jap'].split

      vowels = Hash[*veng.zip(vjap).flatten] #zipped flat array => splat

      #jp row strings by en consonants:
      jrsbec = syllabary_data.select{|con,row|con =~ /^[KSTNHMYRWkstnhmyrwN]$/}

      #jp row arrays by en consonants:
      jrabec = Hash[*jrsbec.map{|con,row|[con,row.split]}.flatten(1)]

      #en vowels with jp row arrays by en consonants:
      evwjrabec = Hash[jrabec.map{|con,row|[con,Hash[veng.zip(row)]]}] #array of hashes => no splat

      #jp syllables by en syllables:
         #outer map provides en consonant to inner map
         #inner map creates the dictionary we want in array form, e.g. [#K#[['Ka','カ'],..], #S..]
         #flatten(1) removes outer array created by outer map [['Ka','カ'],..] => no splat
      jp_by_en = Hash[evwjrabec.map{|con,row|row.map{|vowel,jp_syl| [con+vowel,jp_syl] }}.flatten(1)]

      #remove forgotten syllables:
      jp_by_en.select{|en_syl,jp_syl|jp_syl != '_'}
   }
   Hash[*syllabary_names.zip(a).flatten(1)]
}.call
end

And it returns the desired hash:

{"katakana"=>{"Ka"=>"カ", "Ki"=>"キ", "Ku"=>"ク", "Ke"=>"ケ", "Ko"=>"コ", "Sa"=>"サ", "Si"=>"シ", "Su"=>"ス", "Se"=>"セ", "So"=>"ソ", "Ta"=>"タ", "Ti"=>"チ", "Tu"=>"ツ", "Te"=>"テ", "To"=>"ト", "Na"=>"ナ", "Ni"=>"ニ", "Nu"=>"ヌ", "Ne"=>"ネ", "No"=>"ノ", "Ha"=>"ハ", "Hi"=>"ヒ", "Hu"=>"フ", "He"=>"ヘ", "Ho"=>"ホ", "Ma"=>"マ", "Mi"=>"ミ", "Mu"=>"ム", "Me"=>"メ", "Mo"=>"モ", "Ya"=>"ヤ", "Yu"=>"ユ", "Yo"=>"ヨ", "Ra"=>"ラ", "Ri"=>"リ", "Ru"=>"ル", "Re"=>"レ", "Ro"=>"ロ", "Wa"=>"ワ", "Wi"=>"ヰ", "We"=>"ヱ", "Wo"=>"ヲ"}, "hiragana"=>{"ka"=>"か", "ki"=>"き", "ku"=>"く", "ke"=>"け", "ko"=>"こ", "sa"=>"さ", "si"=>"し", "su"=>"す", "se"=>"せ", "so"=>"そ", "ta"=>"た", "ti"=>"ち", "tu"=>"つ", "te"=>"て", "to"=>"と", "na"=>"な", "ni"=>"に", "nu"=>"ぬ", "ne"=>"ね", "no"=>"の", "ha"=>"は", "hi"=>"ひ", "hu"=>"ふ", "he"=>"へ", "ho"=>"ほ", "ma"=>"ま", "mi"=>"み", "mu"=>"む", "me"=>"め", "mo"=>"も", "ya"=>"や", "yu"=>"ゆ", "yo"=>"よ", "ra"=>"ら", "ri"=>"り", "ru"=>"る", "re"=>"れ", "ro"=>"ろ", "wa"=>"わ", "wi"=>"ゐ", "we"=>"ゑ", "wo"=>"を"}}

This task was a good exercise in index-free coding, but I notice my code exhibits a recurring pattern of map|zip->flatten->Hash. I'd like to know if this is this a normal pattern in ruby or if there's a better way of serializing tabular data.

Feedback:

Instead of ||= lambda { ... }.call, you can use ||= begin ... end
Instead of Hash[*arr] you can use arr.to_h in Ruby 2.0+
You don't need to convert everything to a hash if you just want to use it for .map later -- [[1, 2], [3, 4]].map { |k, v| k + v } #=> [3, 7]
Instead of .map { ... }.flatten(1), you can use .flat_map { ... }
If you won't be using a variable in a block, you can use _ instead, like .map { |key, _| key }

I rewrote the code to be like what I'd code it today.

require 'yaml'

def do_it(raw)
  map = raw["v_eng"].split.zip(raw["v_jap"].split)

  raw.select do |k, _|
    k.size == 1
  end.flat_map do |pre, japs|
    map.zip(japs.split).map do |(post, _), jap|
      [pre + post, jap] unless jap == '_'
    end.compact
  end.to_h
end

want = {"katakana"=>{"Ka"=>"カ", "Ki"=>"キ", "Ku"=>"ク", "Ke"=>"ケ", "Ko"=>"コ", "Sa"=>"サ", "Si"=>"シ", "Su"=>"ス", "Se"=>"セ", "So"=>"ソ", "Ta"=>"タ", "Ti"=>"チ", "Tu"=>"ツ", "Te"=>"テ", "To"=>"ト", "Na"=>"ナ", "Ni"=>"ニ", "Nu"=>"ヌ", "Ne"=>"ネ", "No"=>"ノ", "Ha"=>"ハ", "Hi"=>"ヒ", "Hu"=>"フ", "He"=>"ヘ", "Ho"=>"ホ", "Ma"=>"マ", "Mi"=>"ミ", "Mu"=>"ム", "Me"=>"メ", "Mo"=>"モ", "Ya"=>"ヤ", "Yu"=>"ユ", "Yo"=>"ヨ", "Ra"=>"ラ", "Ri"=>"リ", "Ru"=>"ル", "Re"=>"レ", "Ro"=>"ロ", "Wa"=>"ワ", "Wi"=>"ヰ", "We"=>"ヱ", "Wo"=>"ヲ"}, "hiragana"=>{"ka"=>"か", "ki"=>"き", "ku"=>"く", "ke"=>"け", "ko"=>"こ", "sa"=>"さ", "si"=>"し", "su"=>"す", "se"=>"せ", "so"=>"そ", "ta"=>"た", "ti"=>"ち", "tu"=>"つ", "te"=>"て", "to"=>"と", "na"=>"な", "ni"=>"に", "nu"=>"ぬ", "ne"=>"ね", "no"=>"の", "ha"=>"は", "hi"=>"ひ", "hu"=>"ふ", "he"=>"へ", "ho"=>"ほ", "ma"=>"ま", "mi"=>"み", "mu"=>"む", "me"=>"め", "mo"=>"も", "ya"=>"や", "yu"=>"ゆ", "yo"=>"よ", "ra"=>"ら", "ri"=>"り", "ru"=>"る", "re"=>"れ", "ro"=>"ろ", "wa"=>"わ", "wi"=>"ゐ", "we"=>"ゑ", "wo"=>"を"}}

raw = YAML.load_file 'japanese.dic'

p do_it(raw["katakana"]) == want["katakana"] #=> true
p do_it(raw["hiragana"]) == want["hiragana"] #=> true

Hope that helps. Let me know if you want any other clarification(s) in a comment below.


An exception-safe wrapper for std::stoi std::stoi may throw exceptions so it needs to be surrounded by try/catch.

In applications where std::stoi may be used frequently, it could be useful to have a wrapper.

Is this good practice?

int _stoi(std::string str, int* p_value) {
    // wrapping std::stoi because it may throw an exception

    try {
        *p_value = std::stoi(str);
        return 0;
    }

    catch (const std::invalid_argument& ia) {
        //std::cerr << "Invalid argument: " << ia.what() << std::endl;
        return -1;
    }

    catch (const std::out_of_range& oor) {
        //std::cerr << "Out of Range error: " << oor.what() << std::endl;
        return -2;
    }

    catch (const std::exception& e)
    {
        //std::cerr << "Undefined error: " << e.what() << std::endl;
        return -3;
    }
}

This wrapper effectively removes some of the available functionality from std::stoi() because its signature is

int stoi(const std::string& str, std::size_t* pos = 0, int base = 10);

Because your wrapper does not allow a pos or base argument you cannot use it to give you the number of characters processed (with pos) nor to convert using a different base. std::stoi() provides default values for these arguments so you'd only have to provide them if you need the non-default behavior.

Also, you don't take the std::string argument by const reference like std::stoi() -- the string argument is probably not too expensive to copy in this context but why not accept the argument the same way std::stoi() does?

For completeness, I would also implement the overload of std::stoi() which accepts a std::wstring (and possibly std::stol() and std::stoll()).

I would also avoid the leading underscore, as identifiers with a leading underscore are reserved.

With these suggestions the wrapper would be implemented as

int stoi(const std::string& str, int* p_value, std::size_t* pos = 0, int base = 10) {
    // wrapping std::stoi because it may throw an exception

    try {
        *p_value = std::stoi(str, pos, base);
        return 0;
    }

    catch (const std::invalid_argument& ia) {
        //std::cerr << "Invalid argument: " << ia.what() << std::endl;
        return -1;
    }

    catch (const std::out_of_range& oor) {
        //std::cerr << "Out of Range error: " << oor.what() << std::endl;
        return -2;
    }

    catch (const std::exception& e)
    {
        //std::cerr << "Undefined error: " << e.what() << std::endl;
        return -3;
    }
}


Searching for files in a specified directory I am currently writing a program that allows me to search for files in a user-specified directory. My current code is as follows:

if os.path.exists(file_path)!= True:
    print('\n******* Path does not exist. *******\n')

else:
    while True:
        aa = '''\nWhich search characteristics would you like to use? \n 1. Search by name: N \n 2. Search by extension: E \n 3. Search by size: S
            \n    Please enter your choice: '''

        answer = input(aa).strip()

        if answer not in 'NnEeSs' or answer=='':
            print('\n***** Invalid choice. *****')

        elif answer in 'Nn':
            while True:
                try:
                    name = input ('\nEnter file name: ')
                    rr = search_by_name(name, file_path)
                    if not rr:
                        print('\n***** File not found *****\n')
                    else:
                        break

                except WindowsError:
                    print('\n***** Oops! Access denied.*****\n')
                    continue

        elif answer in 'Ee':
            while True:
                try:
                    ending = input ('\nEnter the file extension: ')
                    rr = search_by_extention(ending, file_path)
                    if not rr:
                        print('\n***** No File(s) found *****\n')
                    else:
                        break

                except WindowsError:
                    print('\n***** Oops! Access denied. *****\n')
                    continue

        elif answer in 'Ss':
            while True:
                try:
                    size = int(input('\nPlease enter file size: '))
                    rr = search_by_size(size, file_path)
                    if not rr:
                        print('\n***** No file(s) found *****\n')
                    else:
                        break

                except ValueError:
                    print('\n***** Enter an numeric value. *****\n')
                    continue

                except WindowsError:
                    print('\n***** Oops! Access denied. *****\n')
                    continue

Each of the search functions returns a list containing the result.

The try and except statements are concerning, and I feel they can be improved.

In each of these cases, or generally, how can the code be improved, or simplified?

To improve readability I would try and break the code down into more functions and redefine your pre-determined text values as globals, such that you can save space in the actual function. Responses can be represented by a dictionary value with boolean/text key value pairs (or exception/text). Also certain UI options can be stored in a dictionary, with links to their associated functions.

# using 3 quotes mean you don't need to specify newlines
dir_tools_menu = '''
Which search characteristics would you like to use?
1. Search by name: N
2. Search by extension: E
3. Search by size: S
\tPlease enter your choice: '''
dir_tools_options = {"n":ui_search_by_name,
                     "e":ui_search_by_extention,
                     "s":ui_search_by_size}
error_response = {WindowsError:"Oops! Access denied.",
                  ValueError:"Enter an numeric value."}
# add number of files for true
files_found_response = {False:"No file(s) found",
                        True:"{} file(s) found"}
# add filename
file_found_response = {False:"File {} not found",
                       True:"File {} found"}
file_exists_response ={ False:"Path does not exist.",
                         True:"Path Found"}
# add choice for true
valid_ui_response = {False:"Invalid choice.",
                      True:"You choose {}."}

Now that we have a collection of text values lets make a function to standardize print outputs, such that you don't have to write all those stars everywhere, and you can feel assured that they will produce even length lines.

line_length = 40
# or you can have a funky pattern like "`-._.-'-._...
line_pad = "*"*line_length

def my_print(text, *params):
    # len(params) must match the count of "{}" in the text so this will not crash
    # Perhaps add a check to assert this case
    message = text.format(*params)
    offset = line_length/2 - len(message)/2
    if offset > 0 and offset + len(message) < line_length:
        print(line_pad[:offset] + message + line_pad[offset+len(message):])
    # in the case that a line is larger than the formatting, you can just print the line.
    # its gonna look gross regardless
    else:
        print(message)

Lets also make a simple yes/no function.

yes = ("yes", "y","1")
no = ("no", "n", "0")
def ask_yes_no(question):
    answer = ''
    while not answer in yes + no:
        answer = input(question).lower()
        if not answer in yes + no:
            my_print(valid_ui_response[False])
        else:
            my_print(valid_ui_response[True], return_to_menu)
            # True - yes, False - no
            return answer in yes

Now your main method boils down to..

quit_question = "Quit Dir Tools? "

def dir_tools(file_path):
    if os.path.exists(file_path):
        my_print(file_exists_response[True] )
    else:
        my_print(file_exists_response[False])
        return
    quit_dir_tools = False
    while not quit_dir_tools:
        answer = input(dir_tools_menu).lower()
        if answer not in dir_tools_options:
            my_print(valid_ui_response[False])
        else:
            my_print(valid_ui_response[True],answer)
            dir_tools_options[answer](file_path)
        quit_dir_tools = ask_yes_no(quit_question)

Now for the actual important functions.. I'll just do one example to show the try catch responses,..

ui_sbn_question = 'Enter file name: '
return_question = "Return to main menu? "

def ui_search_by_name(file_path):
    return_to_menu = False
    while not return_to_menu:
        name = input(ui_sbn_question)
        try:
            sbn_response = search_by_name(name, file_path)
            if not sbn_response:
                my_print(file_found_response[False], name)
            else:
                my_print(file_found_response[True], name)
                # assuming that this isnt a boolean
                my_print(sbn_response)
        except WindowsError:
            my_print(error_response[WindowsError])
        return_to_menu = ask_yes_no(return_question)


Move values to back of an np.array() I'm trying to create the most efficient (not nicest/prettiest) way of moving particular values within an np.array() to the back of said array.

I currently have two different solutions:

import numpy as np
from numba import jit

@jit(nopython = True)
def move_to_back_a(a, value):
    new_a = []

    total_values = 0

    for v in a:
        if v == value:
            total_values += 1
        else:
            new_a.append(v)

    return new_a + [value] * total_values

@jit(nopython = True)
def move_to_back_b(a, value):
    total_values = np.count_nonzero(a == value)

    ind = a == value
    a = a[~ind]

    return np.append(a, [value] * total_values)

Which give the following output:

In [7]: move_to_back_a(np.array([2,3,24,24,24,1]), 24)
Out[7]: [2, 3, 1, 24, 24, 24]

In [8]: move_to_back_b(np.array([2,3,24,24,24,1]), 24)
Out[8]: array([ 2,  3,  1, 24, 24, 24], dtype=int64)

It doesn't really matter whether I get back my output as a list or as an array, though I expect that returning an array will be more helpful in my future code.

The current timing on these tests is as follows:

In [9]: %timeit move_to_back_a(np.array([2,3,24,24,24,1]), 24)
2.28 µs ± 20.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [10]: %timeit move_to_back_b(np.array([2,3,24,24,24,1]), 24)
3.1 µs ± 50.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Is there any way to make this code even faster?

Your second function is faster for me when simplifying it like this:

@jit(nopython=True)
def move_to_back_c(a, value):
    mask = a == value
    return np.append(a[~mask], a[mask])

In addition, Python's official style-guide, PEP8, recommends not surrounding a = with spaces if it is used for keyword arguments, like your nopython=True.

Since numba apparently recently gained generator support, this might also be worth checking out:

@jit(nopython=True)
def _move_to_back_d(a, value):
    count = 0
    for x in a:
        if x != value:
            yield x
        else:
            count += 1
    for _ in range(count):
        yield value

@jit(nopython=True)
def move_to_back_d(a, value):
    return list(_move_to_back_d(a, value))

The timings I get on my machine for the given testcase are:

move_to_back_a    1.63 µs ± 14.5 ns
move_to_back_b    2.33 µs ± 21 ns
move_to_back_c    1.92 µs ± 17.5 ns
move_to_back_d    1.66 µs ± 9.69 ns

What is in the end as least as important is the scaling behavior, though. Here are some timings using larger arrays:

np.random.seed(42)
x = [(np.random.randint(0, 100, n), 42) for n in np.logspace(1, 7, dtype=int)]

While slightly slower for small arrays, for larger the mask approach is consistently faster.


Check for correct number of elements in exploded string Let's say I've got this string:

$str = '1-2-3';

and I explode this like so:

$strSplit = explode('-', $str);

what would be the best way to check if I've got the correct number of elements and whether they are numeric (because the input can vary)?

Straightforward, I would say something like this:

if (isset($strSplit[0]) && is_numeric($strSplit[0]) && isset($strSplit[1]) && is_numeric($strSplit[1]) && isset($strSplit[2]) && is_numeric($strSplit[2]) {
    // some code
}

But you could also do:

if (count($strSplit) === 3 && is_numeric($strSplit[0]) && is_numeric($strSplit[1]) && is_numeric($strSplit[2])) {
    // some code
}

You can use some array functions to check for the is_numeric part, but that seems fussy, less readable and is probably slower. I could be wrong, though...

There are several options.

My favorite (based on readablity and simplicity) is using array_filter to remove everything that is not a number from the array and then comparing with the original to see if anything changed, then using count to make sure you have the correct number of elements:

$str = '1-foo-3';
$strSplit = explode('-', $str);

if(count($strSplit) == 3 && $strSplit == array_filter($strSplit, 'is_numeric')){
    // array is numeric // run code
} else {
    // array is non-numeric
}

Edit: did some speed testing and tried a few more options and validations.

I speedtested with the following inputs:

A: $strSplit = explode('-', '1-2-3');//3 elements, all numeric //evaluates true
B: $strSplit = explode('-', '1-foo-3-4');//4 elements, not all numeric //evaluates false

The results (after 1 million cycles) were as follows :

Static isset and is_numeric :

if (isset($strSplit[0]) && is_numeric($strSplit[0]) && isset($strSplit[1]) && is_numeric($strSplit[1]) && isset($strSplit[2]) && is_numeric($strSplit[2]) && !isset($strSplit[3])) {
    // some code
    // A : speed 0.68 mircoseconds
    // B : speed 0.48 mircoseconds
}

Looped isset and is_numeric :

for($i=0; $i<3;$i++) if(!isset($strSplit[$i]) || !is_numeric($strSplit[$i])) $nonNumeric = true;

if(!isset($strSplit[($i+1)]) && !isset($nonNumeric)){
    // some code
    // A : speed 0.96 mircoseconds
    // B : speed 1.00 mircoseconds
}

count and array_filter :

if(count($strSplit) == 3 && $strSplit == array_filter($strSplit, 'is_numeric')){
    // some code
    // A : speed 2.00 mircoseconds
    // B : speed 0.16 mircoseconds
}