Python monkeypatch an API and search through iterable object tree General information

I am using an acceptance testing framework (robot framework) and it has an API which allows you to 'visit' suites and tests before execution.

Specific goal

The framework does not save the original suites name (a.k.a. tests.my_test). If you wanted to run that test twice with different names it will throw out the tests component and become new_name.my_test. I wanted to be able to preserve that information so that while viewing it can have descriptive names, but while searching the suite trees I could still find the original test names and rerun.

Questions to people unfamiliar with the framework

I wanted to piggyback off of their work to stay as compatible with the framework as possible. The selection of rerunned tests is here. It includes a function gather_failed_tests which uses class GatherFailedTests. Since, that native feature of rerunfailed and my feature of rerunrenamedtests can't coexist I chose to simply monkeypatch the GatherFailedTests class to inherit from my class, which fixes the names before it would gather the tests.

Is that a dangerous idea anyway? Should I copy all the source for the function gather_failed_tests and rename them in their entirety? It's a maintainability vs stability problem. I either can monkey patch a minimal component and hope I get any fixes added to the function or surrounding framework or I can have to manually update the whole chain.

I feel like there is a more efficient way to go through the iterable suites. suite_obj.suites returns an iterable of suites. However the initial top level suite itself also needs to be searched. Is there a way to collapse config_suites and config_all_suites simply?

It is ultimately a tree of suites and I need to handle the first element in each branch that has the originallongname in metadata.

Questions for anyone who knows robot

I'm unhappy with wiping out the parent when I am configuring a non-root suite test.parent.parent = None. I'm uncertain if why that method doesn't just configure the parent's criticality instead of failing, but regardless of how it functions is there a better way to alter the longname property without destroying info?

from robot.api import SuiteVisitor
from robot.conf import gatherfailed

class rename(SuiteVisitor):
    """Store the original longname so that rerunning can happen even with virtually reorganized tests."""

    def __init__(self, new_name):
        self.new_name = new_name

    def start_suite(self, suite):
        originallongname = suite.longname
        suite.metadata.setdefault('originallongname', originallongname)
        suite.configure(name=self.new_name)

class resetname(SuiteVisitor):
    def config_test(self, suite):
        originallongname = suite.metadata['originallongname']
        try:
            suite.configure(name=originallongname)
        # Critically setting from non-root error
        except TypeError:
            for test in suite.tests:
                test.parent.name = originallongname
                test.parent.parent = None

    def config_all_suites(self, suite):
        for suite in suite.suites:
            try:
                self.config_test(suite)
            except KeyError:
                self.config_suites(suite)

    def config_suites(self, suite):
        try:
            self.config_test(suite)
        except KeyError:
            self.config_all_suites(suite)

    def start_suite(self, suite):
        self.config_suites(suite)

class RenameThenGatherFailedTests(resetname):

    def __init__(self):
        self.tests = []

    def visit_test(self, test):
        if not test.passed:
            self.tests.append(test.longname)

    def visit_keyword(self, kw):
        pass

gatherfailed.GatherFailedTests = RenameThenGatherFailedTests

class rerunrenamedtests(SuiteVisitor):
    def __init__(self, output):
        self.output = output

    def start_suite(self, suite):
        tests = gatherfailed.gather_failed_tests(self.output)
        suite.filter(included_tests=tests)

If it would help to see how this would be used you could see here.

I think it is more futureproof to use multi-inheritance to resetname and use GatherFailedTests together rather than copy and pasting the code and only having it have single inheritance with copy pasted source.

class RenameThenGatherFailedTests(resetname, gatherfailed.GatherFailedTests):
    pass

gatherfailed.GatherFailedTests = RenameThenGatherFailedTests

I think this is simply better than the copy some code and don't copy other hybrid in the original question. However, I'm still uncertain if simply copying and altering the entire source would be a better practice.


Split string into parts of varying lengths I need to split a string into parts of varying predefined lengths, given as a sequence of integers. After a first imperative attempt using a sequence expression and a reference cell, I have now come up with this implementation that uses Seq.scan and doesn't need mutability:

let segmentString lengths (input : string) =
    let folder (start, parts) lengthHere =
        let partHere = input.Substring(start, lengthHere)
        start + lengthHere, partHere :: parts

    let results = lengths |> Seq.scan folder (0, [])

    let _, resultParts = Seq.last results
    List.rev resultParts

This works and is probably "good enough", but I wonder whether there are things that could be improved or more idiomatic (other than error handling and allowing more generic inputs than strings; I left those out because they are not a concern right now). What especially bugs me is the need to reverse the results list at the end, but obviously, I could only add the new part as the head of the aggregate list in the folder function.

This is a pretty good solution but like you say, the reverse is annoying, so let's see if we can fix that.

We want a sequence of tuples (start, length) that we can pass to Substring. We already have the lengths, so we just need to figure out the starts. The skeleton of our solution looks like this:

let segmentString lengths (input : string) =
    let segments = Seq.zip ??? lengths
    Seq.map (input.Substring : int * int -> string) segments

So where does a segment start? It starts at the sum of the previous lengths. We can use scan (+) 0 to calculate the partial sums of the lengths.

This gives us the final solution

let segmentString lengths (input : string) =
    let segments = Seq.zip (Seq.scan (+) 0 lengths) lengths
    Seq.map (input.Substring : int * int -> string) segments


Unit tests for os.scandir/scantree a directory I wrote my first series of test for this simple function:

def map_directory(self, path='', method='recursive'):
    """
        scan the given path
            method -> recursive (all levels) or single (first level only)
    :param method: string
    :param path: string
    :return: generator
    """
    if not path:
        path = self.path

    if method == 'recursive':
        f = self.scan_tree
    elif method == 'single':
        f = os.scandir
    else:
        raise BullShitError('please enter a valid scanning method : {} is BS'
                            .format(method))
    return f(path)

def scan_tree(self, path=''):
        """
            recursively yield DirEntry objects for given directory.
        :return: generator
        """
        if not path:
            path = self.path

        for entry in (d for d in os.scandir(path) if FileSystemManager.is_readable(d.path) is True):
            try:
                if entry.is_dir(follow_symlinks=False):
                    yield entry
                    yield from self.scan_tree(entry.path)
                else:
                    yield entry
            except PermissionError:
                logger.warning('this entry raised an error : {}'.format(entry.path))
                continue

Tests :

import os
from django.test import TestCase

from packages.system.filesystem import FileSystemManager
from packages.system.local_dirs import user_home

class FileSystemManagerTests(TestCase):

    @classmethod
    def setUpClass(cls):
        cls.test_dir = os.path.join(user_home, 'test/a/b/c')

        os.makedirs(cls.test_dir, 0o755, exist_ok=True)
        cls.fl = FileSystemManager(cls.test_dir)

    @classmethod
    def tearDownClass(cls):
        pass

    def test_map_directory_returns_only_direntry_objects(self):
        """
            map_directory() returns only DirEntry objects in the returned generator
        """
        mapping = self.fl.map_directory()
        for obj in mapping:
            self.assertIsInstance(obj, os.DirEntry)

    def test_map_directory_returns_generator(self):
        """
            map_directory() returns a generator
        """
        from typing import Generator
        self.assertIsInstance(self.fl.map_directory(), Generator)

    def test_map_directory_recursive_method(self):
        """
            map_directory() recursive returns a, b, c
        """
        mapping = self.fl.map_directory(os.path.join(user_home, 'test'))
        self.assertEqual(len(tuple(mapping)), 3)

    def test_map_directory_single_method(self):
        """
            map_directory() single method returns a
        """
        mapping = self.fl.map_directory(os.path.join(user_home, 'test'),
                                        method='single')
        self.assertEqual(len(tuple(mapping)), 1)

I then have 3 main questions:

Is this "over tested"?
Does testing for only the length of the generator suffice for a proper test?
Should I ignore this function which is straightforward enough and center the focus of my tests on the self.scan_tree method?

Any other comments are welcomed, thanks.

General

You use what seems to be a non-standard docstring style. That's fine, but it's actually quite close to the RST / Sphinx syntax. An overview of the various styles can be found here. It's a good idea to pick one you like and stick to it, or to simply enforce your own style, but mixing styles will certainly result in confusion. 'Why is this RST not being interpreted correctly?!' or 'Why does this perfectly readable docstring have random pieces of RST syntax thrown in?'
These two lines:

if not path:
    path = self.path

Should be a red flag. Let's get this straight. There's a path associated with self, and a custom path can be passed as a parameter to the method. This means that map_directory() should either be @staticmethod'ed, or self.path should not exist. Without context, there's no way of telling which is the right solution, but this is clearly wrong.
When you raise the exception (by the way, BullShitError, really?) you tell the developer what went wrong (good) but don't give them a list of valid options (bad). Not everyone is going to read through the documentation immediately when they get an exception. To a reasonable extent, exception messages should tell the developer exactly what to do to prevent the exception in the future.

Questions

Barely. The only thing I don't think is worth testing is whether the return type is a generator. The other tests look sane and useful, so keep them.
In a controlled testing environment, you might as well have a tuple of the entries and check if the method call matches the expected results.
That's impossible to tell if you don't show us what self.scan_tree() actually does.


Validate array elements in PHP $elements is an array, which may contain 'x' or 'y' keys, I want to check if they exist, otherwise assign a 0. I'm looking for a clean way to validate it. I'm new to PHP, so I want to make sure my code is created according to PHP best practices.

Input array:

Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
            [x] => 123
        )

    [2] => Array
        (
            [x] => 123
            [y] => 456
        )

    [3] => Array
        (
            [y] => 456
        )

)

Code:

foreach ($elements as $element) {
    $element_x = $element_y = 0;
    if (isset($element['x']))
        $element_x = $element['x'];
    if (isset($element['y']))
        $element_y = $element['y'];
    $results[] = sprintf('(%d,%d)', $element_x, $element_y);
}

Is there a cleaner way to do it?

First: Upgrade PHP!!! PHP 5.6 is losing security support on December 31st this year. Ref. All other PHP 5 versions are completely unsupported. You should be running PHP 7 today.

Once you've done that, you can use PHP 7's new null coalescing operator ?? to clean up the loop.

foreach ($elements as $element) {
    $results[] = sprintf('(%d,%d)', $element['x'] ?? 0, $element['y'] ?? 0);
}
unset($element);

Note that I've added unset($element) after the loop. You should always do this to avoid confusing behavior when using the same name in multiple loops. Take a look at the warning on PHP's documentation page for more info. You should also unset $element_x, and $element_y.

The PSR-2 style guide requires braces on control statements, style is a personal(/team) choice, but most people do agree with this rule:

  The body of each structure MUST be enclosed by braces. This standardizes how the structures look, and reduces the likelihood of introducing errors as new lines get added to the body. - PSR-2 Coding Style Guide

Other than this, I believe your code is about as clean as you can get. Good work! I'd probably go for $x instead of $element_x, but there's nothing wrong with your choice.


Removing Asterisks and neighbors from a string I am going through the CodingBat exercises for Java. I got to this problem:

  Return a version of the given string, where for every star (*) in the string the star and the chars immediately to its left and right (if any) are gone. So "ad", "ab*cd", "ab**cd", "*ead", and "ade*" all yield "ad".

I decided to solve this using regular expressions. Here is my code:

public String starOut(String str){

    String s = " " + str + " "; //Avoiding OOB exceptions.
    String n = ""; //Used for replacements of s.

    if (s.contains("***")) {
        n = s.replaceAll(".[*][*][*].", "");
        s = n;
    }

    if (s.contains("**")) {
        n = s.replaceAll(".[*][*].", "");
        s = n;
    }

    if (s.contains("*")) {
        n = s.replaceAll(".[*].", "");
    }

    String theOne = n.replaceAll("\\s", ""); //Remove whitespace created by s declaration.
    return theOne;
}

My code is inefficient and repetitious, and does not account for situations of a string containing more than three * adjacent to each other. I can't help feeling like I'm missing something obvious of regex that would be a beautifully logical solution.

What would be a good solution to ensure my code utilises regex in an efficient and sensical way? Would it be more appropriate to solve this by looping through characters in the original string?

Regular expressions are the right tool for this sort of problem, and your suspicions that your code is not great, is about right.... there's the + operator in regular expressions which will do what you want much more concisely. + matches 1-or-more times.

Consider the simple expression:

String compact = raw.replaceAll(".?\\*+.?", "");

That replaces all something-stars-something patterns with nothing.

Note, this pattern will have the following results:

ab*cd     ad
ab***cd   ad
a**b
ab****    a

etc.

The way the expression works is as follows:

Key features of the regex are:

\\*+ - * is normally a special character. We have to escape it with \\ to make it a normal *. The + is a 1-or-many match. What does this mean? It means that the expression \\*+ will match at least one * character, perhaps many of them in a row.
.? this is a non-greedy 0-or-1 match of any character. This requires some explaining. This will match at most 1 character, but, if the overall pattern will fail to match something, then the pattern can be tried again but matching nothing. What it means, is: "if possible, match 1 character - any character (including *)"

Putting them together, you get an expression that matches any character, if there is one, before an asterisk, as well as the asterisk, and any other asterisks that follow it, and finally any other character, if there is one.

See this in an ideone here: http://ideone.com/6yMSvx


Google Translate CLI A Translator in the Terminal using Golang. It uses Google's API. Currently, it only translates en (English) to es (Spanish).

package main

import (
    "bufio"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "os"
)

func (item *Result) UnmarshalJSON(data []byte) error {
    var v []interface{}
    if err := json.Unmarshal(data, &v); err != nil {
        fmt.Println(err)
        return err
    }
    item.Data = v[0].(interface{}).([]interface{})[0].([]interface{})[0].(string)
    return nil
}

type Result struct {
    Data interface{}
}

func main() {
    fmt.Printf("\nEnter text\t: ")
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    query := scanner.Text()
    response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    } else {
        defer response.Body.Close()
        bytes, err := ioutil.ReadAll(response.Body)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
        var result Result
        if err := json.Unmarshal(bytes, &result); err != nil {
            fmt.Println(err)
        }
        value := result.Data
        fmt.Printf("Translation\t: %s\n", value)
    }
}

In several places you use fmt.Println followed by os.Exit.
You may create your own Exit function to write error message to stderr and terminate.

Note: it is common practice to write error messages to stderr.
stdout is for program output - translated text in your case.

item.Data = v[0].(interface{}).([]interface{})[0].([]interface{})[0].(string)

This statement will trigger a panic someday. I bet you need some recover.

if err := json.Unmarshal(bytes, &result); err != nil {

Hm, you've shaded previous err. Do you need one more error variable?
When you use := operator, it will declare all missing variables on the left that doesnt present in current scope. Example:

str := "What??"
if str != "" {
    str := ""
    fmt.Printf("str is '%s'\n", str)
}
if str != "" { panic(str) }

Other notes:

I see you've used bufio.Scanner to get first line from input. You may consider reading all via ioutil.ReadAll. This way one may pipe some text to your program: cat ./text.txt | go-translate
No need for else branch in main function
value variable is used only once. You may pass result.Data directly to fmt.Printf call.

Example:

func Exit(err interface{}) {
    fmt.Fprint(os.Stderr, err)
    os.Exit(1)
}

func main() {
    fmt.Fprint(os.Stderr, "\nEnter text\t: ")

    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    query := scanner.Text()

    response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
    if err != nil {
       Exit(err)
    }
    defer response.Body.Close()

    bytes, err := ioutil.ReadAll(response.Body)
    if err != nil {
        Exit(err)
    }

    var result Result
    if err = json.Unmarshal(bytes, &result); err != nil {
         Exit(err)
    }

    fmt.Printf("Translation\t: %s\n", result.Data)
}


Implementation of Logistic Regression Is this kind of vectorized operations the most efficient way to do this in matlab? Any critics about my code? Am I doing something wrong (i tested several times, I think it works). Notice that I use J to store the history of the cost function so I can see how well it is converging (by plotting a graph for instance).

main function

function [theta, J_history] = logRegGradientDescent(X, y, theta, alpha, num_iters)
% Given a matrix X where the columns are features and a matrix Y of targets
% we apply the gradientDescent to minimize the cost function and find its
% local optimum. Alpha is the learning rate on which we look for a local
% minimum and num_iters is the amount of times we repeat the learning step.

J_history = zeros(num_iters);

for iter = 1:num_iters

    % Derivative of the cost function used, the square error in that case.
    dLogisticCostFunction = (1/m) * X' * (logisticFunction(X,theta) - y);

    % Learning step
    theta = theta - alpha * dLogisticCostFunction;

    % Save the cost function for convergence analysis
    J_history(iter) = logRegCostFunction(X,y,theta);
end
end

logistic function

function h = logisticFunction(X,theta)
% Compute the logistic function.
% If X is a matrix such as:
%
%    x1_ x2_ x3_ .. xn_;
%  [ x11 x12 x13 .. x1n;
%    x21 x22 x23 .. x2n;
%    ..  ..  ..  .. .. ;
%    xn1 xn2 xn3 .. xnn; ]
%
% and thetha' is a vector:
%  [ t0, t1, t3 .. tn ]
%
% We calculate the logistic function:
% 1/ ( 1 + e^(-sum(x*theta)))

h = 1 ./ ( 1  + exp(-X*theta) );
end

logistic cost function

function J = logRegCostFunction(X,y,theta)
% Compute a convex cost function to the Logistical Regression where
% if y = 1 and the logistic function predicts y = 0, cost -> inf
% and if y = 0 and the logistic fucntion predicts y = 1, cost -> inf

% Calculates number of Features
m = length(y);

% Calculates the case where if y = 1, Cost = -log(h(x))
ify1 =  log(logisticFunction(X,theta)).*y;

% Calculates the case where if y = 0, Cost = -log(1-h(x))
ify0 =  log(1 - logisticFunction(X,theta)).*(y-1);

% Calculates the cost function
J = - (ify1 + ify0) / m;
end

Your code looks fine and is vectorized! You could've written a single line logistic cost function, but I believe your approach is more readable. Good job!

I don't think there is much more optimizations that you can do related to the basic form of logistic regression.

A possible addition however is to add regularization. This helps for the scenario of overfitting, where a high order polynomial function tries to fit perfectly according to the dataset in such a way that it doesn't make sense anymore.

An example:

regularization tries to prevent this by reducing the magnitude of the parameters θ (penalizing the parameters).  You will end up with a smoother curve which fits the data and gives a better hypothesis.

To accomplish this you will have to introduce another parameter, λ (regularization parameter).

I suggest you to look further into this yourself, perhaps here is a good starting point.

I hope this helps a bit.


Reading Database, scraping Barchart and uploading prices to DB This is my first real excursion into Python, coming from C#. I wanted to see if my code is well-formatted and efficient.

The following will read the needed codes from a Database Table, then submit the codes to an API, store them in individual objects, and then dump them back into another table in the Database.

import ondemand
import pyodbc
import datetime

#Code purposes
#1.) Get the desired barchart codes for the year
#2.) Load quotes into an array from Barchart API
#3.) Submit quotes to database for today's date

#Qute Class - Stores the quotes as objects
class Quote():
    def __init__(self, seq_dimDate, symbol, month, year, price, crop, ficalYear):
        self.seq_dimDate = seq_dimDate
        self.symbol = symbol
        self.month = month
        self.year = year
        self.price = price
        self.crop = crop
        self.fiscalYear = ficalYear

#Establish a connection to the database
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=MyComputer\\SQLEXPRESS;DATABASE=MarketDB')
cursor = cnxn.cursor()

#get the Date and Fiscal Year
now = datetime.datetime.now().strftime('%m/%d/%Y')
dateRow = cursor.execute('SELECT * from dbo.dimDate WHERE [Date] = ?', now).fetchone()
seqDimDate = dateRow.Sequence
fiscalYear = dateRow.FiscalYear
print('Sequence for %s is %s with a fiscal year of %s' % (now, seqDimDate, fiscalYear))

#Query the Barchart Codes Table
cursor.execute('SELECT * FROM dbo.Codes WHERE [Year] = %s' % (fiscalYear))

#Generate a list of codes to send to API
quotesToQuery = ''

for row in cursor.fetchall():
    for x in range(3,len(row)):
        quotesToQuery = quotesToQuery + ',' + row[x]

#Open connection to the API
od = ondemand.OnDemandClient(api_key='123', end_point='https://marketdata.websol.barchart.com/')

#Get quotes
quotes = od.quote(quotesToQuery)['results']
lQuotes = []

#load the quotes into our array of objects
for q in quotes:
    #Get the Symbol, month, year and price of the quote
    symbol = q['symbol']
    dt = datetime.datetime.strptime(q['tradeTimestamp'], '%Y-%m-%dT00:00:00-%H:%M')
    month = dt.month
    year = dt.year
    price = q['lastPrice']

    #Get the Sequence for the Crop
    if q['name'] == 'Corn':
        crop = 1
        price = price / 100
    elif q['name'] == 'Soybean Meal':
        crop = 2
    else:
        crop = 3

    #create a new Quote object and add it to the collection
    nq = Quote(seqDimDate, symbol, month, year, price, crop, fiscalYear)
    lQuotes.append(nq)

#Double Check of Quotes
print('%s total Quotes Collected' % (len(lQuotes)))

#Delete any Prices in the Database for this date sequence
cursor.execute('DELETE FROM dbo.MarketPrice WHERE Seq_dimDate = ?', seqDimDate)
cursor.commit()
print('MarketPrice table cleared of records with Date Sequence %s...' % (seqDimDate)) #confirm records were deleted

#Submit the Quotes to the Database
for n in lQuotes:
    cursor.execute('INSERT INTO dbo.MarketPrice (Seq_dimDate, Seq_Crop, [Month], FiscalYear, Price, Comments) VALUES(?,?,?,?,?,?)', n.seq_dimDate, n.crop, n.month, n.fiscalYear, n.price, n.symbol)
    cursor.commit()
    print('%s Uploaded to MarketPrice Table in Hedging2' % (nq.symbol)) #confirm update

I submit myself to your critique!

Python review

This could make better use of docstrings and functions; for example, your comment about the Quote class should be a well-formatted docstring instead. Additionally, each of your different comment blocks would be best suited as a function, and then piece them together inside of a __main__ block.

I would also do my best to remove any of your magic values, and instead use constants. This makes it easier if they come from somewhere else (e.g. a config file, or somewhere on the database) to adjust.

When formatting, prefer str.format over % formatting. If you're on a high-enough Python version, f-strings are even nicer.

This line is very magic: for x in range(3,len(row)). I would strongly prefer that you document what is going on here using non-magic numbers (and maybe a code comment). I think that what you're doing is getting the value from every column after the first 4; if that is what you're doing, then

Improving the SQL query is good to do here (see #1 in the SQL review below)
There are better variable names to use here

Generators are a good thing to use, as are list comprehensions. You can also use tuple-unpacking to make trivial assignments easier.

Instead of iteratively building a string, which performs poorly, use str.join. Similarly, instead of repeatedly list.appending, use list.extend or a list comprehension.

Instead of cascading your if/elif/else, use a dictionary and a default value.

SQL review

In general, your SQL is pretty straightforward so there isn't a ton to say. Overall, you should:

When SELECTing something, only get what you need. SELECT * should always be considered suspect.
Avoid embedding raw SQL in your source code; if the desired behavior ever changes, now you're needing to update this code for no reason. A stored procedure (or table-valued function) is likely going to be a better option.
Always use your language/libraries tools for parameterizing SQL; you seem to switch back and forth between this and using string formatting. Always remember that no input is trusted, even if it comes from your own database.
Instead of inserting VALUES row-by-row, investigate if there is a bulk INSERT option for your library. If not, consider other options (as row-by-row is going to perform poorly)
Consider deleting and inserting in a single commit so as to avoid leaving the database in an unusual state in-between operations.

Updated code

I came up with the following updates (that assume you have created the appropriate stored procedures, and I didn't bother with fully making all literals constants). I think there are a few more places for improvement here; for example, some of the formatting you do before instantiating the class could be done in the class itself (for example, a property that returns the formatted value, or a setter on that property that sets it in a formatted way). I also think you could likely break this up into a few more classes if you really wanted to make it OOP, but that is probably overkill for your use-case.
    """
    Gets barchart codes for a given year, then loads them into an array
    using BarChart's API. From there, the quotes are pushed to the database
    using the current date
    """

import datetime

import ondemand
import pyodbc

CONNECTION_STRING = "DRIVER={SQL Server};SERVER=MyComputer\\SQLEXPRESS;DATABASE=MarketDB"
API_KEY = "123"
END_POINT = "https://marketdata.websol.barchart.com/"
NAME_TO_CROP_MAPPING = {
    "Corn": (1, lambda price: price / 100),
    "Soybean Meal": (2, lambda price: price)
}
DEFAULT_CROP_MAPPING = (3, lambda price: price)

class Quote():
    """Stores the quotes as objects"""

    def __init__(self, seq_dimDate, symbol, month, year, price, crop, ficalYear):
        self.seq_dimDate = seq_dimDate
        self.symbol = symbol
        self.month = month
        self.year = year
        self.price = price
        self.crop = crop
        self.fiscalYear = ficalYear

def get_today_formatted():
    return datetime.datetime.now().strftime("%m/%d/%Y")

def get_sequence_date(cursor):
    now = get_today_formatted()
    row = cursor.execute("EXECUTE dbo.RetrieveDateAndFiscalYear @dateFilter = ?", now).fetchone()
    return (now, dateRow.Sequence, dateRow.FiscalYear)

def get_codes(cursor, fiscal_year):
    FIRST_CODE_INDEX = 3

    all_codes = cursor.execute("EXECUTE dbo.RetrieveBarchartCodesByYear = ?", fiscal_year)
    for row in all_codes.fetchall():
        for code in range(FIRST_CODE_INDEX, len(row)):
            yield row[code]

def get_date_from_timestamp(timestamp):
    date = datetime.datetime.strptime(timestamp, '%Y-%m-%dT00:00:00-%H:%M')
    return (date.month, date.year)

def get_quotes(od_connection, seq_dim_date, fiscal_year, quotes_to_query):
    raw_quotes = od_connection.quote(quotes_to_query)["results"]

    for raw_quote in raw_quotes:
        symbol = raw_quote["symbol"]
        month, year = get_date_from_timestamp(quote["tradeTimestamp"])
        base_price = q["lastPrice"]

        crop_id, adjusted_price = get_crop_sequence(q["name"], base_price)

        yield Quote(seq_dim_date, symbol, month, year, adjusted_price, crop_id, fiscal_year)

def get_crop_sequence(crop, base_price):
    crop_id, price_adjustor = NAME_TO_CROP_MAPPING.get(crop, DEFAULT_CROP_MAPPING)
    return (crop_id, price_adjustor(base_price))

def update_invalid_prices(cursor, seq_dim_date, quotes):
    cursor.execute("EXECUTE dbo.DeleteHistoricalPricesByDate @Date = ?", seq_dim_date)

    for quote in quotes:
        cursor.execute("EXECUTE dbo.UpdatePricesForQuote <<Fill in your parameters here>>", <<Fill in your parameters here>>)

    cursor.commit()

    print(f"MarketPrice table cleared of records with Date Sequence {seq_dim_date}...")
    print("\n".join([f"{q.symbol} Uploaded to MarketPrice Table in Hedging2" for q in quotes]))

if __name__ == "__main__":
    sql_connection = pyodbc.connect(CONNECTION_STRING)
    cursor = sql_connection.cursor()

    now, seq_dim_date, fiscal_year = get_sequnce_date(cursor)
    print(f"Sequence for {now} is {seq_dim_date} with a fiscal year of {fiscal_year}"))

    quotes_to_query = ",".join(get_codes(cursor, fiscal_year))
    ondemand_connection = ondemand.OnDemandClient(api_key=API_KEY, end_point=API_END_POINT)
    all_quotes = list(get_quotes(ondemand_connection, seq_dim_date, fiscal_year, quotes_to_query))
    print(f"{len(all_quotes)} total Quotes Collected")

    update_invalid_prices(cursor, seq_dim_date, all_quotes)


Finding a word ladder using Breadth First Search This is a LeetCode problem called Word Ladder:

  Given two words (start and end), and a dictionary, find the length of
  shortest transformation sequence from start to end, where only one letter can be
  changed at a time. Each intermediate word must
  exist in the dictionary For example,

  Given: start = "hit" end = "cog" dict = ["hot","dot","dog","lot","log"]

  As one shortest transformation is
  "hit" -> "hot" -> "dot" -> "dog" -> "cog", return its length 5.

  Note:

  Return 0 if there is no such transformation sequence. All words
  have the same length. All words contain only lowercase alphabetic
  characters.

My solution:

#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

class Solution
{
    //! O(n)
    bool is_changeable(const string& lhs, const string& rhs)
    {
        size_t count = 0;
        for(auto l = lhs.cbegin(), r = rhs.cbegin();    l != lhs.cend();    ++l,++r)
            if(*l != *r)    ++count;
        return count == 1;
    }

public:
    using Level = vector<string>;

    int ladderLength(string start, string end, unordered_set<string> &dict)
    {
        if(dict.empty())    return 0;

        auto d = dict;
        d.erase(start);
        d.erase(end);
        if(d.empty())       return 2;

        //! lambda to make next level
        auto next_level = [&](const Level& curr) -> Level
        {
            Level ret;
            for(const auto& s : curr)
                for(const auto& attempt : d)
                    if(is_changeable(s,attempt))    ret.push_back(attempt);
            return ret;
        };

        //! lambda to check if end reached
        auto if_found = [&](const Level& lvl) -> bool
        {
            for(const auto& s : lvl)
                if(is_changeable(s,end))    return true;
            return false;
        };

        /**
         * @brief top abstraction layer
         */
        size_t count = 0;
        for(auto curr = Level{start}; !if_found(curr);   curr = next_level(curr))
        {
            if(curr.empty())    return 0;
            for(const auto& s : curr)   d.erase(s);
            ++count;
        }

        return count + 2;
    }
};

This solution caused "Time Limit Exceeded" when tested with a reasonably big dict. It's so big that I'm not not allowed to paste here, so get it here.

How can I optimize this code to AC? Am I doing BFS right? What is the bottleneck?

If you worry about the performance, the first thing to try is to hook up the profiler. Besides that, constructing another Ladder at each level seems wasteful.

is_changeable is named misleadingly; my first impression was that you are testing a mutability of something. It turned out that the method test for a Hamming distance to be 1. I recommend to name it hamming_distance, make it return int (or size_t or whatever you find appropriate) ant let the client test the result for 1.
Use of lambdas borders to abuse. If you feel a need to comment what lambda is doing, it is a strong indication that the lambda wants to have a name, i.e. be a normal function.


Multi-User Roles in Laravel I am very new to Laravel. I am building a website where multiple users can have one or more roles. I have a users table and a user_roles table. Each user_roles column has a user_id and an enum field where they can be a "referee", "coach", or "player". Whenever I want to check if the User is a certain member of a role I call a referee, coach, or player function in my model from the controller. Is my structure and code efficient, or is there any way to clean it up?

User.php

public function roles()
{
    return $this->hasMany('App\Role');
}

public function getRoles()
{
    $roles_collection = $this->roles;
    $roles = [];
    foreach ($roles_collection as $role) {
        $roles[] = $role['role'];
    }
    return $roles;
}

public function referee()
{
    if (in_array('referee', $this->getRoles())) {
        return true;
    } else {
        return false;
    }
}

public function player()
{
    if (in_array('player', $this->getRoles())) {
        return true;
    } else {
        return false;
    }
}

public function coach()
{
    if (in_array('coach', $this->getRoles())) {
        return true;
    } else {
        return false;
    }
}

Example of checking the User role, called from the controller:

if (Auth::user()->referee()) {
       echo 'referee';
}

I do not have much experience with Laravel, so I will comment on your code as it is presented, but keep in mind that I may miss something which is already handled by the framework.

The first thing which strikes my mind is the getRoles() method. To me it seems you are doing the work of the native function array_column(). This means the method can be reduced to:

public function getRoles() {

    return array_column($this->roles, 'role');

}

I even think you could extract only the role names into a property. I am not familiar with how Laravel deals with databases, but I am sure you can extract only the role names associated with the user directly from the database. If you then extract the roles during instantiation of the class, you can remove the getRoles() method altogether.

I would also remove the methods player, coach and referee and replace them with a generic method. This is to make it easier in the future if you add/remove new roles. Consider the following method.

public function hasRole($role) {

    return in_array($roles, $this->getRoles());

}

You would then check if a user by simply writing the role name.

if(Auth::user()->hasRole('referee')) {
    // Do awesome stuff here
}

This would reduce the number of methods inside your class and make it so you wouldn't have to edit your exiting code just to add a new role. This follows the open-closed principle which states that a class should be open for extension, but closed for modification.

Now you can also pass the role name as variable (if you ever need to) without the fear of an undefined method error as the method would simply return false.

Happy coding!


Boilerplate singleton / lazy initialiser I'm working with an existing code base here and while I don't have the time/budget to re-do the entire thing (ultimate solution), I do get a bit of leeway to enhance the code base, as well as maintaining it.

We're using EntityFramework and the SynchronizedBindingList is essentially just that; it keeps all the data in memory, but also ensures that it's as in-sync as possible with the database. The idea being that memory is cheap and we'd rather use the end-users' (internal staff) memory, than have a few hundred users frequently querying the database. The concept works well and we haven't really had any issues with the setup, I'm just trying to make the code more maintainable.

At present, we have a lot of Manager classes, essentially one for each table, that has the following code:

public class LessonsLeartManager
{
    private static readonly Lazy<LessonsLearntManager> Lazy =
            new Lazy<LessonsLearntManager>(() => new LessonsLearntManager());
    public SynchronizedBindingList<LessonsLearnt, DbContext> LessonsLearntList { get; private set; }

    public static LessonsLearntManager Instance
    {
        get { return Lazy.Value; }
    }

    private LessonsLearntManager()
    {
        LessonsLearntList = new SynchronizedBindingList<LessonsLearnt, DbContext>();
        LessonsLearntList.RefreshSync();
    }

    // Manager specific code goes here.
}

Obviously this is highly repetitive and difficult to maintain if you want to change something as it would have to be changed across multiple files.

I have been fiddling with this idea today and would like to get other opinions on the quality/idea of this code:

public class DatabaseTable<TManager, TTable> where TManager : class, new()
        where TTable : class, IDbPk<TTable>, IAutoIncludeEntity<TTable>, new()
{
    private static readonly Lazy<TManager> Lazy = new Lazy<TManager>(() => new TManager());
    public SynchronizedBindingList<TTable, DbContext> List { get; private set; }

    internal DatabaseTable()
    {
        List = new SynchronizedBindingList<TTable, DbContext>();
        List.RefreshSync();
    }

    public static TManager Instance
    {
        get { return Lazy.Value; }
    }
}

Obviously the previous LessonsLearntManager code then becomes:

public class LessonsLearntManager : DatabaseTable<LessonsLearntManager, LessonsLearnt>{//Manager specific code}

and can be used as such:

MyGridView.DataSource = LessonsLearntManager.Instance.List;

which is a far bit better than previously (the class declaration anyway, the usage doesn't change much).

Based on the naming guidelines variables should be named using camelCase casing, at least if you don't have any internal naming guidelines.
So Lazy should become lazy or much better lazyManager to make it clear what's the lazy is about.

Otherwise your code looks good.
What I would like to suggest is to rename the class to better reflect its usage and makes it clear from the name what it is about. Maybe DatatableManager would be better.


Place Ajax request on completion of Ajax Request I have an Ajax request which in turn on completion make another Ajax request:

$.ajax({
          url: '<?php echo BASE_URL.'Dashboard/GetMyTrackList' ?>',
          type: 'GET',
          success: function (response) {

              var list = JSON.parse(response);
              $.each(list, function (k, v) {
                  MarkLocation(parseFloat(v["Longitude"]),parseFloat(v["Latitude"]), v["UserId"]);
              });
          },
          complete: function(){
              $(".icon").click(function(evt){
                  debugger;
                  $(container).css("display","block");
                  var value = $(this).attr("alt");
                  $.ajax({
                      url:'<?php echo BASE_URL.'Dashboard/GetProfile' ?>',
                      data:'id='+value,
                      type:'GET',
                      success:function(response){
                          var user = JSON.parse(response);
                          $(container).find("#content").find("#username").text(user["Username"]);
                          $(container).find("#content").find("#email").text(user["Email"]);

                          map.addOverlay(new ol.Overlay({
                              position: ol.proj.transform(
                                  [parseFloat(user["Longitude"]),parseFloat(user["Latitude"])],
                                  'EPSG:4326',
                                  'EPSG:3857'
                              ),
                              element: container
                          }));

                      }
                  });

              });
          }
      });

Element with an icon class is created in the MarkLocation function:

 function MarkLocation(lon, lat, alt) {
          $userid = '<?php echo $_SESSION["UserID"];?>';
          if(alt==$userid)
              var marker = $('<img class="icon" src="<?php echo BASE_URL.'includes/images/location/mylocation.png'  ?>">');
          else
              var marker = $('<img class="icon" src="<?php echo BASE_URL.'includes/images/location/marker.png'  ?>">');
          map.addOverlay(new ol.Overlay({
              position: ol.proj.transform(
                  [lon, lat],
                  'EPSG:4326',
                  'EPSG:3857'
              ),
              element: marker
          }));
          $(marker).attr("alt", alt);
      }

Previously, I had an icon click event placed outside (i.e. not in the complete function of ajax). In that case, an element with an icon class was not clickable, maybe because of a binding issue. Then I placed the click event in the complete function and it works. Is this a good way? Or what approach would you suggest?

From your code it seems like that you are binding a click event on $(".icon") when the ajax completes.

However, you are not making another call after completion of first, but instead making a call on $(".icon").

if you dont want to put ajax call here then you can bind icon with on function with parent selector like below:

$( "#parentDiv " ).on( "click", ".icon", function() {
   //make ajax call
});

more info on this here

you can get help for this from
You should improve your code's clarity to reflect its purpose.


Separate construction phase from constructed phase I am sure you can find many weak spots, please let me know about them.
My main question is if this is a sensible approach to separate the construction phase from the constructed phase.

Basically I have a set of objects that needs to be transformed into similar (but different) objects. Once transformed I want to be able to query these later.

So I have a PartitionedContainersBuilder which deals with the transformation part and I have a PartitionedContainer which represents the transformed query-able.

What I do like with this is that there is no invalid state anywhere and little risk of doing mistakes with member variables. Had Java been able to return two values then I would probably have solved it differently. However with that said, I know for example that calling it builder and build may not be the best thing (it is not the builder pattern).

I would prefer having the constructor in PartitionedContainer do the work rather than a static build - would that be better? My gut feeling says yes, but is there a tangible advantage?

public class PartitionedContainers {

static class PartitionedContainersBuilder
{
    private LogicalExcelAreaInterface buildLogicalExcelArea(ContainerNodeInterface pContainer, PapyrusInterface pPapyrus)
    {
        // TODO: introduce LeafItemContainer to remove if cases
        if (pContainer.getItem() != null)
        {
            return pContainer.getItem().getLogicalExcelArea(pPapyrus);
        } else {
            return new MutableLogicalExcelArea(1, 1)
                        .withCellWidth(0, 0, pPapyrus.getWidth())
                        .withCellHeight(0, 0, pPapyrus.getHeight());
        }
    }

    public PartitionedContainers build(final ContainerNodeInterface pRootContainer, final Map<ContainerInterface, PapyrusInterface> pPapyrusFromContainer)
    {
        final Map<PartitionedContainerInterface, PapyrusInterface> lPapyrusFromContainer = new HashMap<PartitionedContainerInterface, PapyrusInterface>();
        final Map<ContainerNodeInterface, MutablePartitionedContainer> lPartitionedContainerFromContainer = new HashMap<ContainerNodeInterface, MutablePartitionedContainer>();

        for (final ContainerNodeInterface lNode : new NodeCollections<ContainerNodeInterface>().getAllSortedTopDown(pRootContainer))
        {
            lPartitionedContainerFromContainer.put(
                    lNode,
                    new MutablePartitionedContainer(lNode.getElement(), buildLogicalExcelArea(lNode, pPapyrusFromContainer.get(lNode)))
            );
            if (!lNode.isRoot()) {
                lPartitionedContainerFromContainer.get(lNode).withParent(lPartitionedContainerFromContainer.get(lNode.getParent()));
                lPartitionedContainerFromContainer.get(lNode.getParent()).withAdditionalChild(lPartitionedContainerFromContainer.get(lNode));
            }
            lPapyrusFromContainer.put(lPartitionedContainerFromContainer.get(lNode), pPapyrusFromContainer.get(lNode));
        }
        return new PartitionedContainers(lPartitionedContainerFromContainer.get(pRootContainer), lPapyrusFromContainer);
    }

}

PartitionedContainerInterface mRootContainer;
Map<PartitionedContainerInterface, PapyrusInterface> mPapyrusFromContainer;

private PartitionedContainers(PartitionedContainerInterface pRootContainer, Map<PartitionedContainerInterface, PapyrusInterface> pPapyrusFromContainer)
{
    mRootContainer = pRootContainer;
    mPapyrusFromContainer = pPapyrusFromContainer;
}

public PartitionedContainerInterface getRoot()
{
    return mRootContainer;
}

public static PartitionedContainers build(ContainerNodeInterface pRootContainer, final Map<ContainerInterface, PapyrusInterface> pPapyrusFromContainer)
{
    return new PartitionedContainersBuilder().build(pRootContainer, pPapyrusFromContainer);
}

public PapyrusInterface getPapyrusFromContainer(PartitionedContainerInterface pContainer)
{
    return mPapyrusFromContainer.get(pContainer);
}
}

First of all, you are right, what you have there is not a builder class, but more like a factory class. Which is roughly equivalent to a static factory method.

Now, the question of using constructors vs static factory methods is one over which there has been a lot of talk, and for the most part no conclusive, universally-applicable answers. (Just search for "c# constructor vs static factory method" and you will see.) However, specifically for your case, where both the constructed class and the code constructing it are private within another class, the advantages of "discoverability" and "recognizability" that the new keyword has are not applicable, so I'd definitely go with a factory method or class.

With regards to whether you should be using a factory class or a static factory method, I think that the factory class is fine. The overhead for the generation of an additional object which gets immediately discarded is negligible, (especially compared to the amount of work that the object will do,) and might even be optimized away by the compiler. On the other hand, having a factory class instead of a static factory method could turn out to be useful if in the future you decide to introduce some state into your builder.


Very slow graph walking My code is very very slow. Could you give me hints on how I can make it much faster?

for (stdext::hash_map<unsigned __int32, CVertex*>::iterator it1 = graph.begin(); it1 != graph.end(); it1++)
{
    for (std::vector<CVertex*>::iterator it2 = it1->second->getNeighbors().begin(); it2 != it1->second->getNeighbors().end(); it2++)
    {
        if ((*it2)->isEqualTo(it1->second)) continue;
        for (std::vector<CVertex*>::iterator it3 = (*it2)->getNeighbors().begin(); it3 != (*it2)->getNeighbors().end(); it3++)
        {
            if ((*it3)->isEqualTo(*it2) || (*it3)->isEqualTo(it1->second)) continue;
            for (std::vector<CVertex*>::iterator it4 = (*it3)->getNeighbors().begin(); it4 != (*it3)->getNeighbors().end(); it4++)
            {
                if ((*it4)->isEqualTo(*it3) || (*it4)->isEqualTo(*it2) || (*it4)->isEqualTo(it1->second)) continue;
                for (std::vector<CVertex*>::iterator it5 = (*it4)->getNeighbors().begin(); it5 != (*it4)->getNeighbors().end(); it5++)
                {
                     if ((*it5)->isEqualTo(*it4) || (*it5)->isEqualTo(*it3) || (*it5)->isEqualTo(*it2) || (*it5)->isEqualTo(it1->second)) continue;
                     for (std::vector<CVertex*>::iterator it6 = (*it5)->getNeighbors().begin(); it6 != (*it5)->getNeighbors().end(); it6++)
                     {
                         if (it1->second->isEqualTo(*it6))
                         {
                             unsigned __int32 *circle = new unsigned __int32[5];

                             circle[0] = it1->second->getWord();
                             circle[1] = (*it2)->getWord();
                             circle[2] = (*it3)->getWord();
                             circle[3] = (*it4)->getWord();
                             circle[4] = (*it5)->getWord();

                             m_results.push_back(circle);
                         }
                     }
                }
            }
        }
    }
}

I think this question is not subjective because I've found many good things on the Internet yet:

I use only vectors which is much faster (the not reserved too) because of the cache.
You cannot see here, but I use only inline function in vertex class.
I tried to reduce the numbers into uint 32 bit.

But I still miss good trick because its time is lifetime. For example, do you think I can make better compiler settings?

Or it's also very useful if you say I cannot make it much faster because in that case I don't waste more energy into finding a better solution.

Since you've only posted partial code, all we can do is offer some suggestions:

Google suggests that the performance of stdext::hash_map might be improved by adding #define _SECURE_SCL 0. See this MSDN article for background.
Your if's look like they might become victim to branch misprediction. This StackOverflow answer explains that better than I ever could.
If you haven't done so already, take care to allocate your vertices (and their internal data) contiguously. The simplest way to do that is usually to shove everything into a vector.


Genetic Drift Simulator - Follow Up This question is a follow-up of a previous question of mine: Genetic Drift Simulator.

Following the advice given in the last question, and some more general improvements, the code has been slimmed down a bit. The major one was implementing Underscore.js, which slimmed down a few of the loops into functions.

The config has identical properties to the last question, however, they've been renamed.

numGenerations: the number of generations to iterate over. Can be number input or infinite to go until one population remains.
population: an object containing the names of the populations and the amount of each.
drawAmount: the amount of population to be surveyed.

var GeneticDriftSimulator = function (user_config) {
    this.config = _.defaults(user_config, {
        numGenerations: '5',
        population: {
            red: 50,
            blue: 50
        },
        drawAmount: 10
    });
};

GeneticDriftSimulator.prototype.run = function () {
    var ratioPopulation = this.config.population;
    var isIndefinite = (this.config.numGenerations == 'infinite');
    for (var i = 0; isIndefinite || i < this.config.numGenerations; i++) {
        if (Object.keys(ratioPopulation).length == 1) {
            console.log('Generation ' + i + ': One trait left: ' + Object.keys(ratioPopulation)[0]);
            break;
        }
        var population = [];
        for (var populationName in ratioPopulation) {
            for (var k = 0; k < ratioPopulation[populationName]; k++) {
                population.push(populationName);
            }
        }
        ratioPopulation = this.ProcessGeneration(population, config.drawAmount);

        var print_result = "Generation " + i + ": ";
        Object.keys(ratioPopulation).forEach(function (key) {
            print_result += key + ':' + ratioPopulation[key] + ' '
        });
        console.log(print_result);
    }
};

GeneticDriftSimulator.prototype.ProcessGeneration = function (population, drawAmount) {
    var temp_drawn = [];
    if (config.drawAmount >= population.length) {
        throw 'SimulationError: The DrawAmount is more than the Population.';
    }
    if (population.length % drawAmount != 0) {
        throw 'SimulationError: The DrawAmount cannot be evenly divided by Population';
    }

    temp_drawn = _.sample(population, drawAmount);
    var multiplier = population.length / drawAmount;
    return this.RatioCount(multiplier, temp_drawn);
};

GeneticDriftSimulator.prototype.RatioCount = function (multiplier, temp_drawn) {
    var final = {};
    _.each(temp_drawn, function (population) {
        for (var i = 0; i < multiplier; i++) {
            if (!(population in final)) {
                final[population] = 0;
            }
            final[population]++;
        }
    });
    return final;
}

with usage like the following:

var config = {
    numGenerations: 'infinite',
    population: {
        cats: 3e4,
        dogs: 7e4,
    },
    drawAmount: 5e4
};
var simulation = new GeneticDriftSimulator(config);
simulation.run();

A few things to point out in retrospect:

There's inconsistent quotation marks everywhere
Lines like the following are confusing and hard to read: print_result += key + ':' + ratioPopulation[key] + ' '
These (throw) should be throw new Error as it includes a stack trace.
Don't use snake case (temp_drawn) use camel case (tempDrawn)
Be careful about using truthy value checks like (population.length % drawAmount != 0) as it can cause issues relating to booleans, for example false == 0 will return true, but false === 0 will return false. See Truthy and Falsy: When All is Not Equal in JavaScript for more information and examples.
run doesn't match the case of the other functions, it should be Run.
k < ratioPopulation[populationName];: declare the length as a variable within the for loop as it gets rid of the constant length checking which can strain when you have a really large array.
You should consider using an array here instead var final = {}; as it may reduce logic on the loops.


Elegant wp-query markup for repetitive post-type category loops For WordPress sites, I always end up building custom post types. They usually have categories, but I don't use much of the 'archive' abilities. This site, for example, is for a film director and so it has "project" resources and then have the standard 'category' taxonomy. I find myself copying and pasting this code in blocks to list each category.

It seems like there are 2 ways to go with PHP. You can keep the markup clear and work the PHP into it, or stay mostly in PHP land and echo markup. I think PHP is a fine language - but simply the syntax has lead me to JS frameworks in its place:

{{#each things as |thing|}}

{{thing.name}}

{{/each}}

Regarding custom fields, I am far more accustomed to ACF vs CMB2, but recently worked on a project with CMB2 and I kinda saw some light in creating the template with more PHP... but both ways still seem less than awesome.

Here's the block of code I find myself using the most. I do it so much that I have a version with xxxxx where project is - that I use to just find and replace with the resource name.

The markup isn't always exactly the same - so I paste it rather than build a function that just takes 1 parameter of resource - but I could have conditionals

What say you - brave code reviewers? How can I up my game with this? Simplify? Change my view-point? Just relax? build a function? Best practices?

Should I be using some archive template for repeating post-type categories instead?

  <?php
  // =========================================
  // Define the rules/arguments
  $project_args = array(
    'post_type' => 'project',
    'showposts' => '-1',
    'category_name' => 'documentary',
  );

  // The Query
  $project_query = new WP_Query($project_args);

  // The Loop
  if ( $project_query->have_posts() ) { ?>

Documentary

    <?php
      while ( $project_query->have_posts() ) {
        $project_query->the_post();

        $image = get_field('project_poster');
    ?>

            <a class='link' href='<?php the_permalink(); ?>'>

                <?php
                  if ( $image ) {
                    $poster = $image['sizes']['medium'];
                  } else {
                    $poster = 'https://placehold.it/1600x900';
                  }
                ?>
                <img src='<?php echo $poster; ?>' alt='<?php echo get_the_title(); ?> poster image' />

<?php the_field("project_title"); ?>

        <?php } ?>

  <?php } else { ?>

    <?php // what happens if there are no posts? ?>

  <? } ?>

  <?php wp_reset_postdata(); /* restore original post data */ ?>

<?php // repeat many blocks like the 'section' above ?>

First off, Wordpress is an awful platform when it comes to separation of business logic from display logic.  If you expect to continue working in it, you can expect to continue to feel the pain that you are feeling.

The thing that concerns me the most about your comments, is that you say you cut/paste this code repeatedly.  That immediately screamw out for putting this code into a widget or function so that it is reusable.  Think if you had a function like:

display_category_list($post_type, $category_name, $show_posts = -1)
{
    // your code
}

This would simplify your main pages to have calls like:

display_category_list('project', 'documentary');
display_category_list('foo', 'bar');

So that could solve the copy/paste problem.

Now let's get into the problem of display templating.  Even working with basic PHP output mechanisms, you can greatly clean up your code, simply by separating out the logic from the HTML in a more reasonable fashion.  You could structure your code like this:

display_category_list($post_type, $category_name, $show_posts = -1)
{
    // Define main section template.
    // You could just as easily include this to keep template
    // in a separate file
    $section_template = <<<'EOT'

{{list}}

EOT;

    // list template, again could be included
    $list_template = <<<'EOT'

{{category_name}}

    {{items}}

EOT;

    // item template
    $item_template = <<<'EOT'

{{title}}

EOT;

    // Actual function logic
    $args = array(
        'post_type' => $post_type,
        'showposts' => $show_posts,
        'category_name' => $category_name,
    );
    $query = new WP_Query($args);

    // put default "no post" content here (this could come from template as well)
    $list_content = '';
    if($query->have_posts()) {
        $list_content = str_replace('{{category_name}}', $category_name, $list_template);
        $items = '';
        while ($project_query->have_posts()) {
            $post->the_post();
            $link = the_permalink();
            $poster = (get_field('project_poster')) ?
                $image['sizes']['medium'] : 'https://placehold.it/1600x900';
            $poster_alt_text = get_the_title();
            $title = the_field('project_title');
            $template_data = array(
                '{{link}}' => $link,
                '{{poster}}' => $poster,
                '{{poster_alt_text}}' => $poster_alt_text,
                '{{title}}' => $title
            );
            $items .= str_replace(
                array_keys($template_data),
                array_values($template_data),
                $item_template
            );
            $items .= PHP_EOL;
        }
        $list_content = str_replace('{{items}}', $items, $list_content);
    }
    echo str_replace('{{list}}', $list_content, $section_template);
    wp_reset_postdata();
}

Note how I have moved the HTML into templates defined within nowdoc blocks.  This approach can allow you to easily pull this content out of the function itself into separate template files that are easier to manage.  For example, the function could be rewritten with template-to-variable assignments like this:

$section_template = include('/path/to/section_template.php');

With the template file looking like:

return <<<'EOT'

{{list}}

EOT;

This means you could even go so far as to pass template file paths into the function on the fly to switch out templates to be used (or you could have some config settings for template paths that the function uses).  This could totally get you out of having any HTML within the function itself.


Finding max amount of integers in list that add up to target Introduction
1) Problem description:

Suppose you are managing files. You are given a text file, where in the first line are storage's volume of the server and the amount of users in total that is in the text file. Each line from the second to the end shows volume of individual user's storage. Based on the server's volume limit you need to find the maximum amount of users that can be fit in the database and the maximum volume among them.

2) Sample.txt
100 4
80
30
50
40

3) Sample output: 2 50
4) Sample output's explanation: The maximum amount of users that can be fit in the database is 2 (more than 2 will be more than 100 in total). All possible pairs, which meet volume's requirements, are 30 40 30 50 50 40 (each makes less than 100). The maximum number among these pairs is 50.

Actual problem
1) db.txt
9691 1894
89
24
33
11
...

2) My solution:
fName = open('../root_files/26_7791633.txt')
param = fName.readline().split()
data = [int(el) for el in fName.readlines()]
fName.close()

TARGET = int(param[0])

data.sort()

target_disposable = TARGET
max_num = 0
quantity = 0

# finding maximum amount
while target_disposable - data[quantity] >= 0:
    target_disposable -= data[quantity]
    quantity += 1

new_list = data[0:quantity]

element_in_the_end = new_list.pop()
remainder = TARGET - sum(new_list)

# finding the very maximum number based on quantity
for i in reversed(range(remainder + 1)):
    if i > element_in_the_end:
        max_num = i
        break

print(quantity, max_num)

3) Output:
34 601
Solution accepted
Runtime: 55ms

4) My question: Is there a way to make this code more concise, maybe even improving its runtime? Thank you in advance.

Some suggestions:

foo[:bar] is syntactic sugar for foo[0:bar].

The idiomatic way to ensure that a file is closed when the code is finished with it is to use a context manager:
with open(…) as fName:
    [any code which reads from fName]

data is a literally meaningless name: anything stored in a variable is data. In this case you might want to use a name like user_counts or users.

The return value of open is not a file name, but rather a file descriptor.

Unless this is a one-off piece of code I would wrap the different bits (the input-handling code (reading the file), the central algorithm and the output code) into functions.

By returning the result rather than printing it this code could be reused by other code.

Since the code naturally only takes a single input stream, I would read the values from standard input (per the Unix philosophy). That way the code can be called with any file trivially, as in ./max_sum_inputs.py < ../root_files/26_7791633.txt

A linter like flake8 or pylint will tell you about other non-idiomatic code, such as how variable names should all be snake_case.


How to improve my JS code? Avoid repeated element I'm trying to learning JS good practices. With jQuery, everything was easier and organized. With JS, I really don't know if I doing this right.

I must repeat the element I am manipulating all the time? For example:

elementHere.className = "";
elementHere.classList.add(test);
elementHere.addEventListener(etc, function() {
}, true);

Is it a a good code, for example? Is it DRY? I really need to repeat the element name all the time? How to improve?

I would like tips to improve and make it DRY! I did not find any tutorial on the internet.

I think it is also not good for maintenance.

This code is perfectly fine (aside from the fact that it's weird to clean up className and then add one item to classList—might as well set className directly).

DRY is about repetition of logic (which is dangerous because you may forget to change it in one place when you change it in the other place). Repetition of variable name is totally fine.

In fact, sometimes some verbosity is preferred to excessive succinctness.

// Bad

sb.className = "";
sb.classList.add(test);
sb.dataset.kind = 'web';
sb.addEventListener('click', function() {
}, true);

// Good

searchWebButton.className = "";
searchWebButton.classList.add('search');
searchWebButton.dataset.kind = 'web';
searchWebButton.addEventListener('click', function() {
}, true);

You're not repeating yourself—these are several distinct actions, and they are obvious.
There is nothing to simplify here.

As noted by Greg, if you did this sequence of calls for several elements, you could have extracted them into a function:

// Hm-kay

searchWebButton.className = "";
searchWebButton.dataset.kind = 'web';
searchWebButton.classList.add('search');
searchWebButton.addEventListener('click', function() {
}, true);

searchHistoryButton.className = "";
searchHistoryButton.dataset.kind = 'history';
searchHistoryButton.classList.add('search');
searchHistoryButton.addEventListener('click', function() {
}, true);

// Good

function setupSearchButton(btn, kind) {
  btn.className = "";
  btn.classList.add('search');
  btn.dataset.kind = kind;
  btn.addEventListener('click', function() {
  }, true);
}

setupSearchButton(searchWebButton, 'web');
setupSearchButton(searchHistoryButton, 'history');

Note that btn by itself is short but not descriptive, and it would make a bad name in outer scope, but as a parameter to setupSearchButton it's obvious so we can keep it short.

It also might be a good idea to extract initialisation code if the initialization is long and you have several other elements to initialize (and maybe something else) in one function:

// Hm-kay

function initialize() {
  searchField.className = "";
  searchField.classList.add('search');
  searchField.addEventListener('change', function() {
    search();
  }, true);
  searchField.addEventListener('focus blur', function() {
    refreshSearchPlaceholder();
  }, true);

  searchWebButton.className = "";
  searchWebButton.dataset.kind = 'web';
  searchWebButton.classList.add('search');
  searchWebButton.addEventListener('click', function() {
  }, true);

  doSomethingElseForInitialization();
}

// Good

function initialize() {
  initializeSearchField(searchField);
  initializeSearchButton(searchWebButton);
  doSomethingElseForInitialization();
}

function initializeSearchField(field) {
  field.className = "";
  field.classList.add('search');
  field.addEventListener('change', function() {
    search();
  }, true);
  field.addEventListener('focus blur', function() {
    refreshSearchPlaceholder();
  }, true);
}

function initializeSearchButton(btn) {
  btn.className = "";
  btn.classList.add('button');
  btn.dataset.kind = 'web';
  btn.addEventListener('click', function (e) {
    e.preventDefault();
    search();
  }, true);
}

For some ideas about easy-to-maintain JS code, read Airbnb Javascript Guide (or any other popular guide, for that matter).


Random gradient header generator I made a header generator that creates a linear gradient of two random colors from its top left to its bottom right.

Please tell me about everything I could do better, but especially how to calculate and not hardcode the randomly shifted second array secondColor, that has the minColorValue, maxColorValue and the random value on other positions.

const settings = {
  width: 500,
  height: 180,
  minColorValue: 70, // min rgb value
  maxColorValue: 185, // max rgb value
};
let container = document.getElementsByClassName("container")[0];
let firstColor = [0, 0, 0];
let secondColor = [0, 0, 0];

let create = function(width, height) {
  let canvas = document.createElement("canvas");
  canvas.width = width;
  canvas.height = height;
  container.appendChild(canvas);
};

let colorPicker = function() {
  // possible configurations of which of the rgb values is min, which is max, and which is inbetween
  let configurations = [
    [0, 1, 2],
    [0, 2, 1],
    [1, 0, 2],
    [1, 2, 0],
    [2, 0, 1],
    [2, 1, 0],
  ];
  let randomConfig = configurations[random(0, 5)];
  firstColor[randomConfig[0]] = settings.minColorValue;
  firstColor[randomConfig[1]] = settings.maxColorValue;
  firstColor[randomConfig[2]] = random(settings.minColorValue, settings.maxColorValue);

  // shift configuration by one or two to never get a different second color
  let shiftAmount = random(1, 2);
  let secondConfig = [0, 0, 0];
  for (let i = 0; i < 3; i++) {

    if (i + shiftAmount < 3) {
      secondConfig[i] = randomConfig[i + shiftAmount];
    } else {
      secondConfig[i] = randomConfig[i - 3 + shiftAmount];
    }
  }
  secondColor[secondConfig[0]] = settings.minColorValue;
  secondColor[secondConfig[1]] = settings.maxColorValue;
  secondColor[secondConfig[2]] = random(settings.minColorValue, settings.maxColorValue);

};

let fill = function() {
  let canvas = document.getElementsByTagName("canvas")[0];
  let context = canvas.getContext("2d");
  let gradient = context.createLinearGradient(0, 0, settings.width, settings.height);
  gradient.addColorStop(0, `rgb(${firstColor[0]},${firstColor[1]},${firstColor[2]})`);
  gradient.addColorStop(1, `rgb(${secondColor[0]},${secondColor[1]},${secondColor[2]})`);
  context.fillStyle = gradient;
  context.fillRect(0, 0, settings.width, settings.height);
};
let start = function() {
  create(settings.width, settings.height);
  colorPicker();
  fill();
};

// help function
function random(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
html {
  background-color: #1d1f21;
}

canvas {
  display: inline-block;
}

.container {
  text-align: center;
}

.textbox {
  text-align: center;
  color: white;
  font-family: monospace;
}

  <title>random gradient header</title>
  <meta charset="UTF-8">
  <link href="style.css" rel="stylesheet">

Reload to get a different header gradient.

Please tell me about everything I could do better, but especially how to calculate and not hardcode the randomly shifted second array [...].

I suggest to get a random permutation of [0, 1, 2] by introducing a simple but unbiased shuffle function.

A circular shift of that random permutation can then be found by mapping index i to index (i + shift) % length:

// Return two shifted colors with min, max and random component:
function randomGradientColors(min, max) {
  let permutation = shuffle([0, 1, 2]);
  let shift = random(1, 2);
  return [
    permutation.map(i => [min, max, random(min, max)][i]),
    permutation.map(i => [min, max, random(min, max)][(i + shift) % 3])
  ];
}

Since you asked for everything that you could do better, I would like to suggest using a CSS background gradient instead of a canvas element. If compatibility is not a concern, you can even leverage fancy CSS variables:

.gradient {
  background: linear-gradient(90deg, var(--from, black), var(--to, black));
}

Your markup then would provide the min and max color components via data attributes:

And your script would simply query all .gradient elements and compute their CSS from and to variables from the provided data attributes:

let gradients = document.querySelectorAll(".gradient");
for (let gradient of gradients) {
  let [from, to] = randomGradientColors(
    gradient.dataset.min,
    gradient.dataset.max
  );
  gradient.style.setProperty("--start", "rgb(" + from + ")");
  gradient.style.setProperty("--to", "rgb(" + to + ")");
}

This approach is pretty flexible and allows you to adapt the styling to your needs without touching the script.

Here is the complete code:

// Return random integer within interval [min, max]:
function random(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// Shuffle array in-place:
function shuffle(array) {
  for (let i = array.length - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array;
}

// Return two shifted colors with min, max and random component:
function randomGradientColors(min, max) {
  let permutation = shuffle([0, 1, 2]);
  let shift = random(1, 2);
  return [
    permutation.map(i => [min, max, random(min, max)][i]),
    permutation.map(i => [min, max, random(min, max)][(i + shift) % 3])
  ];
}

// Compute CSS 'from' and 'to' variables for all gradients:
let gradients = document.querySelectorAll(".gradient");
for (let gradient of gradients) {
  let [from, to] = randomGradientColors(
    gradient.dataset.min,
    gradient.dataset.max
  );
  gradient.style.setProperty("--from", "rgb(" + from + ")");
  gradient.style.setProperty("--to", "rgb(" + to + ")");
}
html {
  background-color: #1d1f21;
}

.gradient {
  display: inline-block;
  width: 500px;
  height: 180px;
  background: linear-gradient(90deg, var(--from, black), var(--to, black));
}

.container {
  text-align: center;
}

.textbox {
  text-align: center;
  color: white;
  font-family: monospace;
}

Reload to get a different header gradient.

If you wish to support older browsers, remove the CSS variables and use

gradient.style.background =
  "linear-gradient(90deg, rgb(" + from + "), rgb(" + to + "))";


Binary search in C++ I was wondering if I have any hidden bugs or traps in my code I wrote for an exercise.

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<algorithm>
using namespace std;

# define MAX 1000

int binSearch(int *arr, int arrSize, int num)
{
    int leftPos = 0, rightPos = arrSize - 1;

    while(mid != leftPos)
    {
        int mid = (rightPos + leftPos)/2;
        if(arr[mid] == num)
            return mid;
        else if(arr[mid] > num)
            rightPos = mid;
        else
            leftPos = mid;
    }

    return -1;
}

int main(){

    /*testing program */
    int arr[MAX];
    for(int i = 0; i < MAX; i++)
        arr[i]=rand()%100;

    sort(arr, arr+MAX);

    cout<<binSearch(arr, MAX, 4);

}

Advice 1

while(mid != leftPos)

Above, mid is not defined, so my compiler does not compile that.

Advice 2

#include<stdio.h>

I don't see your code using anything in stdio.h; why not remove it?

Advice 3

It seems that you forgot to seed the random number generator. Did you mean to

srand(time(NULL));

before creating the integer array?

Advice 4

int mid = (rightPos + leftPos)/2;

If rightPos + leftPos overflow, mid will end up negative. You can "extend the range" by writing

mid = leftPos + (rightPos - leftPos) / 2;

instead.

Putting pieces together

After a minor rewrite, your implementation may look like this:

int binSearch(int *arr, int arrSize, int num)
{
    int leftPos = 0, rightPos = arrSize - 1;

    // 'mid' not yet declared:
    int mid;

    while(leftPos < rightPos)
    {
        // Avoids overflowing '(leftPos + rightPos) / 2':
        mid = leftPos + (rightPos - leftPos) / 2;

        if(arr[mid] == num)
            return mid;
        else if(arr[mid] > num)
            rightPos = mid - 1;
        else
            leftPos = mid + 1;
    }

    return -1;
}

Alternative implementation

It is not hard to write the binary search as a template:

template<typename RandomIt, typename T>
RandomIt binarySearch(RandomIt begin, RandomIt end, const T& value)
{
    RandomIt save_end = end;

    while (begin != end)
    {
        auto range_length = std::distance(begin, end);
        RandomIt middle = begin;
        std::advance(middle, range_length / 2);

        if (*middle == value)
        {
            return middle;
        }

        if (value < *middle)
        {
            end = middle;
            std::advance(end, - 1);
        }
        else
        {
            begin = middle;
            std::advance(begin, +1);
        }
    }

    return save_end;
}

Hope that helps.


performance test the code for finding time taken to fill the disk space I have written this module that takes input of configuration of multiple lines with first item as available storage and remaining are the process at different rate using sys.stdin .

#! /usr/bin/python

""" this script calculates the time remaining for available storage to get filled up.
"""

import sys

def time_to_meltdown(proc_r, available_storage):
    """
    this functions caclulates the time it will take to full the available disk space
    by n number of proceses by the rate of 1 byte after every d(p_i) seconds.

    :param list proc_r: number of process with constant but different rate memmory consumption
    :param int available_storage: total amount of free storage available on the farm.

    :return: time in seconds taken by n process to fill up the storage
    :rtype: int
    """
    mem = 0 # Initial consumption
    timeTaken = 0
    while mem < available_storage:
        timeTaken += 1 # increament each second by 1
        mem = 0
        for v in proc_r:
            mem += timeTaken // v
    return timeTaken

def main():
    """ this function builds data from the input and pass it
        to function that calculate time it will take to for
        available storage to get filled.
    """
    input_data = sys.stdin.read()
    config_lines = input_data.split("\n")
    for line in config_lines:
        if line:
            data = [int(data) for data in line.split() if data]
            available_storage = data[0]
            pr_i = data[1:]
            print "{} processes will take {} seconds to fill storage".format(len(pr_i), time_to_meltdown(pr_i, available_storage))

if __name__ == "__main__":
    main()

Please review the code for optimal performance.

When I run this code:

        while mem < available_storage:
        timeTaken += 1 # increament each second by 1
        mem = 0
        for v in proc_r:
            mem += timeTaken // v
    return timeTaken

above lines take quite substantial amount of time for long list of processes.

You may be able to speed up the body of the loop using the sum function & list comprehension:

mem = sum( timeTaken // v for v in proc_r )

But the real speed up won’t come until you realize you can compute a lower limit for timeTaken, and begin looping starting from that value, instead of from zero.


PDO Insert function using PHP This code inserts data into the database successfully. I was concerned about whether it is a considered good practise to go about inserting data into the database in this way. Is there something I am not considering?

function insertTest(){

    global $db;

    $field_names    = array("id", "first_name", "last_name", "email");
    $alias          = array(":id", ":fn", ":ln", ":em");
    $assignment     = array(":id" => "2k39dk38dk2k39dk38dk2k39dk38dk2k39dk38dk",
                            ":fn" => "Chris",
                            ":ln" => "Moore",
                            ":em" => "test@gmail.com");

    $array = array();
    $array['table'] = 'users';
    $array['field_names'] = $field_names;
    $array['alias'] = $alias;
    $array['assignment'] = $assignment;

    $q = "INSERT INTO `%s` (%s) VALUES (%s)";
    $q = sprintf($q, $array['table'], implode(", ",$array['field_names']), implode(", ",$array['alias']));
    $q = $db->prepare($q);
    $r = $q->execute($assignment);

    print_r($r);

    return $r;

}

Is it good practice to insert data into a table this way?
If by that you mean: is it considered good practice to use prepared statements, and a bind array, then yes. It is. If you mean: "can I consider my code to be good practice", then the answer is no. It's not. Here's why:
function insertTest()
{//needs to go on the next line, PSR standards
    global $db;

The use of functions + global keyword is bad practice. For starters: you have no real way of ensuring that $db will be an instance of PDO. And seeing as you are using a function, why not require the caller to pass a valid DB connection to your function:
function insertTest(PDO $db)
{
}

Now you know what $db will be, with the added benefit of the type-hint, any decent IDE will know, too. So your code is self-explanatory.
Next we have this:
$field_names    = array("id", "first_name", "last_name", "email");
$alias          = array(":id", ":fn", ":ln", ":em");
$assignment     = array(":id" => "2k39dk38dk2k39dk38dk2k39dk38dk2k39dk38dk",
                        ":fn" => "Chris",
                        ":ln" => "Moore",
                        ":em" => "test@gmail.com");

That looks a tad messy, doesn't it. For starters, it's pretty clear to me that $field_names and $alias belong together: for each field in your INSERT, there needs to be a placeholder. Having 2 separate arrays means that someone can add a value to the first, and simply forget about adding a placeholder in the other array. That would break your code. Just write:
$map = [
    'id'         => ':id',
    'first_name' => ':fn',
    'last_name'  => ':ln',
    'email'      => ':email',
];

That addresses that issue. Now you can easily extract the fields you're using (array_keys($map)) and the parameters (array_values($map)). As for the $assignment variable: that really should be an argument that the user passes to your function, so change its signature to:
function inserTest(PDO $db, array $assignment)
{}

In the long run, you'll probably want to turn this function into a method, and define $map as a property, which you can then use to validate $assignment (check if the keys in the array exist in $this->map and throw exceptions if something doesn't quite add up)
Prepared statements are reusable
Another drawback of the way you're using prepared statements in your function is that you're missing out on one of their biggest advantages: prepared statements can be re-used several times:
$stmt = $pdo->prepare('INSERT INTO foo (field1, field2) VALUES (:foo, :bar)');
$stmt->execute([':foo' => 'value 1', ':bar' => 1]);
$stmt->execute([':foo' => 'value 2', ':bar' => 2);

The code above will add 2 rows to the foo table, using the same prepared statement. Creating such a statement in a function, using it and then returning a boolean will GC (garbage collect) the prepared statement, only to create it a second time after when you call the function again. This isn't ideal, obviously.
It might be more useful to harness the power of the PDOStatement instance that PDO::prepare returns.
This can be especially useful if you're using SELECT queries: returning the PDOStatement instance allows the user to fetch the data in a format he sees fit:
$stmt = $obj->getSelectByIdStmt();
$data = [];
foreach ($ids as $id) {
    $stmt->execute([':id' => $id]);
    $objects = [];
    //or PDO::FETCH_ASSOC, PDO::FETCH_CLASS, ...
    while ($row = $stmt->fetch(PDO::FETCH_OBJ) {
        $objects[] = $row;
    }
    $data[$id] = $objects;
}

This is something that is quite hard to abstract into one, comprehensive function/method...
Other niggles:
Your usage of this (badly) named $array variable doesn't really make sense:
$array = array();
$array['table'] = 'users';
$array['field_names'] = $field_names;
$array['alias'] = $alias;
$array['assignment'] = $assignment;

For starters, initializing a variable to an empty array, and then adding keys one by one is a lot more work (to type) than a simple:
$array = [
    'table'       => 'users',
    'field_names' => $field_names,
    'alias'       => $alias,
    'assignment'  => $assignment,
];

Now if you were to pre-format/stringify array values (like $field_names), you could use vsprintf to create your query, but that's something others who work on the same code will hate you for (they really will, trust me, I've been there).
I've already hinted at the possibility of creating a class, which could be used to store things that are, essentially, immutable (like the table name and the field map). But that's a different matter, for now: just start by leaving out the $array variable. There's also no need to assign the format to $q if you're reassigning $q the next statement. Just write:
$q = sprintf(
    'INSERT INTO `%s` (%s) VALUES (%s)',
    'users',
    implode(', ', $field_names),
    implode(', ', $alias)
);

The backticks around the table name imply you're using MySQL, you might want to add the same backticks around your field names (in case some mug decides to call fields "transaction" or "set" - again, I've been there, it happens):
$q = sprintf(
    'INSERT INTO `%s` (`%s`) VALUES (%s)',
    'users',
    implode('`, `', $field_names),
    implode(', ', $alias)
);


C: struct timespec library I'm trying to write a minor lib that would enable me to measure average time measured when using struct timespec. Since C doesn't allow operator overloading, I'm forced to build functions. I am aware that checks like end_time < start_time are not done, but I've left them out to achieve some kind of shortness in the code. The accuracy of the calculations is primary goal.
enum { NS_PER_SECOND = 1000000000L };
enum { MAX_NS = 999999999 };

// t1 --> start time
// t2 --> end time
// set t1 and t2 by:
//        clock_gettime(CLOCK_REALTIME, &t1);
struct timespec get_timespec_diff(const struct timespec t1, const struct timespec t2)
{
    struct timespec t;

    t.tv_nsec = t2.tv_nsec - t1.tv_nsec;
    t.tv_sec  = t2.tv_sec - t1.tv_sec;

    if (t.tv_sec > 0 && t.tv_nsec < 0) {
        t.tv_nsec += NS_PER_SECOND;
        t.tv_sec--;
    } else if (t.tv_sec < 0 && t.tv_nsec > 0) {
        t.tv_nsec -= NS_PER_SECOND;
        t.tv_sec++;
    }

    return t;
}

struct timespec timespec_add(const struct timespec t1, const struct timespec t2) {

    unsigned int total_ns   = t1.tv_nsec + t2.tv_nsec;
    unsigned int total_s    = t1.tv_sec  + t2.tv_sec;

    while(total_ns > MAX_NS) {
        total_ns -= (unsigned)(MAX_NS + 1);
        total_s++;
    }

    struct timespec ret;
    ret.tv_sec  = total_s;
    ret.tv_nsec = total_ns;

    return ret;
}

struct timespec timespec_avg(const struct timespec t, const unsigned n) {

    struct timespec ret;
    ret.tv_sec  = 0;
    ret.tv_nsec = 0;

    unsigned int total_ns = t.tv_nsec + (t.tv_sec * (MAX_NS+1));
    unsigned int avg_ns = total_ns/n;

    while(avg_ns > MAX_NS) {
        ret.tv_sec++;
        avg_ns /= 10;
    }
    ret.tv_nsec = avg_ns;

    return ret;
}

You need to #include <time.h> for struct timespec to be defined.

Why are NS_PER_SECOND and MAX_NS enum members?  There's no advantage compared to plain old const long.  I'm not sure why we need two constants anyway; the code that uses MAX_NS is simplified if we use NS_PER_SECOND instead:
long total_ns   = t1.tv_nsec + t2.tv_nsec;
time_t total_s  = t1.tv_sec  + t2.tv_sec;

while (total_ns >= NS_PER_SECOND) {
    total_ns -= NS_PER_SECOND;
    ++total_s;
}

(Note the use of the correct types in this version).

I think this logic is wrong:
if (t.tv_sec > 0 && t.tv_nsec < 0) {
    t.tv_nsec += NS_PER_SECOND;
    t.tv_sec--;
} else if (t.tv_sec < 0 && t.tv_nsec > 0) {
    t.tv_nsec -= NS_PER_SECOND;
    t.tv_sec++;
}

I'm reasonably sure that tv_nsec should always be positive, even when tv_sec is negative.  So we can simplify that to just:
if (t.tv_nsec < 0) {
    t.tv_nsec += NS_PER_SECOND;
    t.tv_sec--;
}

Note that the logic in timespec_add() already assumes positive tv_nsec.

This is highly susceptible to overflow:
unsigned int total_ns = t.tv_nsec + (t.tv_sec * (MAX_NS+1));

The whole reason we have struct timespec is that we might need to represent values outside the range of the integer types.  Probably better to use divmod to divide tv_nsec by n, and add the remainder to nsec before dividing - we need to be very careful here to avoid overflow.  Again, an unsigned type is inconsistent with other code.
Modified code
Here's my version of these functions:
#include <time.h>

const long NS_PER_SECOND = 1000000000L;

struct timespec timespec_sub(const struct timespec t1, const struct timespec t2)
{
    struct timespec t;

    t.tv_nsec = t2.tv_nsec - t1.tv_nsec;
    t.tv_sec  = t2.tv_sec - t1.tv_sec;

    if (t.tv_nsec < 0) {
        t.tv_nsec += NS_PER_SECOND;
        t.tv_sec--;
    }
    return t;
}

struct timespec timespec_add(const struct timespec t1, const struct timespec t2)
{
    struct timespec t = { t1.tv_sec + t2.tv_sec, t1.tv_nsec + t2.tv_nsec };
    if (t.tv_nsec >= NS_PER_SECOND) {
        t.tv_nsec -= NS_PER_SECOND;
        t.tv_sec++;
    }
    return t;
}

struct timespec timespec_divide(struct timespec t, const int n)
{
    time_t remainder_secs = t.tv_sec % n;
    t.tv_sec /= n;
    t.tv_nsec /= n;
    t.tv_nsec +=
        remainder_secs * (NS_PER_SECOND / n) +
        remainder_secs * (NS_PER_SECOND % n) / n;

    while (t.tv_nsec >= NS_PER_SECOND) {
        t.tv_nsec -= NS_PER_SECOND;
        t.tv_sec++;
    }
    return t;
}

And a primitive unit-test:
int main(void)
{
    const struct timespec a = { 1, 905234817 };
    struct timespec a_2 = timespec_add(a, a);
    struct timespec a_4 = timespec_add(a_2, a_2);
    struct timespec a_5 = timespec_add(a_4, a);
    struct timespec z = timespec_sub(a, timespec_divide(a_5, 5));
    return z.tv_sec || z.tv_nsec;
}

You should expand on the testing, to prove correctness for the tricky cases where overflow could happen.

Further simplification
We can separate out the code to normalise out-of-range nanoseconds into its own function:
struct timespec timespec_normalise(struct timespec t)
{
    t.tv_sec += t.tv_nsec / NS_PER_SECOND;
    if ((t.tv_nsec %= NS_PER_SECOND) < 0) {
        /* division rounds towards zero, since C99 */
        t.tv_nsec += NS_PER_SECOND;
        --t.tv_sec;
    }
    return t;
}

Then the functions can use that, to make them shorter and simpler:
struct timespec timespec_sub(const struct timespec t1, const struct timespec t2)
{
    struct timespec t = { t2.tv_nsec - t1.tv_nsec, t2.tv_sec - t1.tv_sec };
    return timespec_normalise(t);
}

struct timespec timespec_add(const struct timespec t1, const struct timespec t2)
{
    struct timespec t = { t1.tv_sec + t2.tv_sec, t1.tv_nsec + t2.tv_nsec };
    return timespec_normalise(t);
}

struct timespec timespec_divide(struct timespec t, const int n)
{
    time_t remainder_secs = t.tv_sec % n;
    t.tv_sec /= n;
    t.tv_nsec /= n;
    t.tv_nsec +=
        remainder_secs * (NS_PER_SECOND / n) +
        remainder_secs * (NS_PER_SECOND % n) / n;
    return timespec_normalise(t);
}

Because we have the unit tests, we have high confidence that we haven't affected the functionality here.


Plot a piecewise-defined function I would like to plot the following function:

$$
\begin{align}
\Lambda(\delta\tau) &\equiv\ \chi(\delta\tau, 0)
= \frac{1}{T_i} \int_0^{T_i} a(t_0+t')a(t_0+t'+\delta\tau) dt' \\
&= \begin{cases}
   1 - \frac{\left|\delta\tau\right|}{\tau_c}, &\left|\delta\tau\right| \le \tau_c(1+\frac{\tau_c}{T_i}) \\
   -\frac{\tau_c}{T_i}, &\left|\delta\tau\right| \gt \tau_c(1+\frac{\tau_c}{T_i}) \\
   \end{cases}
\end{align}
$$
which represents a simple triangular shape.
There is a conditional statement. My implementation uses a for loop as follows:

def waf_delay(delay_increment):
    for d in delay_increment:
        if np.abs(d) <= delay_chip*(1+delay_chip/integration_time):
            yield 1 - np.abs(d)/delay_chip
        else:
            yield -delay_chip/integration_time;

integration_time = 1e-3 # seconds
delay_chip =  1/1.023e6 # seconds

x=np.arange(-5.0, 5.0, 0.1)
y=list(waf_delay(x))
plt.plot(x, y)
plt.show()

Is there a more correct way to transform an array based on a condition rather than just looping through it? Instead of having something like this:

def f(x_array):
    for x in x_array:
        if np.abs(x) <= 3:
            yield 1 - x/3
        else:
            yield 0

x=np.arange(-5.0, 5.0, 0.1)
y=list(f(x))
plt.plot(x, y)
plt.show()

I would like to write something like this:

def f(x):
        if np.abs(x) <= 3:
            yield 1 - x/3
        else:
            yield 0

x=np.arange(-5.0, 5.0, 0.1)
plt.plot(x, f(x))
plt.show()

that could take an array.

There are two ways to solve this problem. The first one is numpy.where, which can take two arrays and it will choose from one wherever a condition is true and from the other wherever it is false. This only works if your piecewise function has only two possible states (as is the case here):

def waf_delay(delays):
    return np.where(np.abs(delays) <= delay_chip*(1+delay_chip/integration_time),
                    1 - np.abs(delays)/delay_chip,
                    -delay_chip/integration_time)

Another more general possibility is to use numpy.piecewise, but that is probably overkill here:

def f1(x):
    return 1 - np.abs(x)/delay_chip

def f2(x):
    return -delay_chip/integration_time

cut_off = delay_chip*(1+delay_chip/integration_time)
y = np.piecewise(x, [np.abs(x) <= cut_off, np.abs(x) > cut_off], [f1, f2])

Note that in both cases no for d in delays is needed, because all functions used are vecotorized (the basic arithmetic operations for numpy arrays are and so is numpy.abs).