Convert csv to switch commands Python The script should be a single python file that takes an excel file (or csv).  Each row represents one switch.  For each switch, there will be one or more interfaces.  The objective is to output a script of commands to execute to configure each switch, as specified.

For example, Row 1 is below:

switch1    Eth1    trunk    10,20,30    Eth2    Access    10    Y

The script should see that there are two interfaces.  For each trunk interface, the script should output the command “swithport mode trunk”, then “switchport trunk allow vlan …” with the list of allowed VLANs.  For each access interface, it’s the same, but with the word “access” instead of “trunk”.

The sample output for the above row is as below;

switch1

hostname switch1
!
interface Eth1
 switchport mode trunk
 switchport trunk allow vlan 10,20,30
!
interface Eth2
 switchport mode access
 switchport access vlan 10

Each switch’s output should be written as a separate column in a new csv or excel file.

My submitted code as below, please review and guide me for improvements;

#!/usr/bin/python
import csv
import sys
import re
import argparse

def parseCSV(path):
    '''
    Parses a CSV file and return the rows as a list
    '''
    rows = []
    try:
        f = path
    except IOError:
        print "Cannot open specified file: %s" % path
    else:
        reader = csv.reader(f)
        for row in reader:
            rows.append(row)
        f.close()
    return rows

def outputCSV(destpath, rows):
    '''
    Process the input rows and provide switch command output.
    Output then processed to destination CSV file.
    '''
    try:
        f = destpath
    except IOError:
        print "Cannot open specified file: %s" % destpath
    else:
        writer = csv.writer(f)
    for i in range(len(rows)):
        new_list = []
        final_string = ''
        if re.match(r'^switch\d$', rows[i][0]):
            new_list.append(rows[i][0])
            new_list.append('')
            new_list.append('hostname '+rows[i][0])
            new_list.append('!')
            if rows[i][1] is not None:
                new_list.append('interface '+rows[i][1].lower())
                new_list.append(' switchport mode '+rows[i][2])
                new_list.append(' switchport '+rows[i][2]+' allow vlan '+rows[i][3])
            new_list.append('!')
            if rows[i][4] is not None:
                new_list.append('interface '+rows[i][4].lower())
                new_list.append(' switchport mode '+rows[i][5].lower())
                new_list.append(' switchport '+rows[i][5].lower()+' vlan '+rows[i][6])
            final_string = '\n'.join(new_list)
            writer.writerow([final_string])
    f.close()

def main(args):
    '''
    Parses the command line arguments
    Arguments are:
        arg1: Input CSV file to process
        arg2: Output CSV file

    Sample program execution: python myfile.py arg1 arg2
    '''
    parser = argparse.ArgumentParser(description="Reads CSV input and provide switch commands in CSV output file")
    parser.add_argument('path', help="CSV file to process", type=argparse.FileType('rt'))
    parser.add_argument('destpath', help="Output CSV file", type=argparse.FileType('at'))
    try:
        args = parser.parse_args()
    except IOError, msg:
        parser.error(str(msg))
        return
    rows = parseCSV(args.path)
    outputCSV(args.destpath, rows)

if __name__ == '__main__':
    args = sys.argv[1:]
    main(args)

First, your exception wrappers are useless:

try:
    f = destpath
except IOError:
    print "Cannot open specified file: %s" % destpath

destpath is already a file object. If there were an opening problem, then argparse would have told you with an exception at this level.

  parseCSV function can be rewritten in one line by turning the csv reader object to a list by iterating on it when building the list object:

def parseCSV(path):
    '''
    Parses a CSV file and return the rows as a list
    '''
    return list(csv.reader(path))

(I suggest that you close the file outside the parseCSV method as it wasn't its responsibility to open the file in the first place)

  outputCSV function:

don't use the indices here, iterate on the elements:

for i in range(len(rows)):

becomes:

for row in rows:  # "rows[i]" becomes "row"

now all those append statements can be simplified/shortened by passing lists to extend.

Also note that if rows[i][1] is not None is always True: csv module returns str objects, never None.

    new_list = []
    if re.match(r'switch\d$', rows[i][0]):  # no need for ^: it's match not search
        new_list.extend([row[0],'','hostname '+row[0],'!'])
        # skipping the "always true" condition, you may need to adapt this
        new_list.extend(['interface '+row[1].lower(),' switchport mode '+row[2],' switchport '+row[2]+' allow vlan '+row[3],'!'])
        # skipping the "always true" condition, you may need to adapt this
        new_list.extend(['interface '+row[4].lower,' switchport mode '+row[5].lower(),' switchport '+row[5].lower()+' vlan '+rows[i][6]])
        writer.writerow(['\n'.join(new_list)])

Last but not least, when using csv & text mode in Windows you stumble on the

  infamous blank-line-every-two-line bug ¹ ² ³

So for python 2 (seems to be the case), you may want to change the argparse for output to:

parser.add_argument('destpath', help="Output CSV file", type=argparse.FileType('ab'))  # open as binary

and for python 3:

parser.add_argument('destpath', help="Output CSV file", type=argparse.FileType('at',newline="")) # don't issue extra CR chars, well, some magic like that :)


Accessing a String value from multiple threads without synchronization I'm looking for some input here.  I have a class that contains a value which is updated every few seconds by a method within that class.  Right now, access to this value across multiple threads is done via synchronization, which I would like to eliminate.  Would this make sense?

class DataSegment {

    private MetricsUpdater metrics = new MetricsUpdater.getInstance();

    public String printValues() {
        StringBuilder sb = new StringBuilder();
        sb.append(value1);
        sb.append(morevalues);
        sb.append(metrics.myValue); // this is the value that's currently synchronized
        return sb.toString();
    }
}

class MetricsUpdater {

    private String myValueSynchronized;
    public String myValue;

    public static MetricsUpdater getInstance() {
        if (theInstance == null) {
            theInstance = new MetricsUpdater();
        }
        return theInstance;
    }

    // this runs on a timer but to keep it simple I'll just define the method...
    private void updateMetrics() {

        synchronized(myValue) {
            // also, to keep things simple, I've replaced all of the actual logic with a method called someMethodToUpdateMyValue()
            myValueSynchronized = someMethodToUpdateMyValue();
            myValue = myValueSynchronized;
        }
    }
}

There can be many instances of DataSegment all reading from myValue, but the metrics class is a singleton.  myValue only updates every 5 seconds or so and only MetricsUpdater is allowed to write to it.  Does that make sense?

Does it even need to be synchronized at all if all of the other threads are only allowed to read it?

Your code is not synchronized at all at the moment.  There are two major mistakes:

You are only synchronizing in the thread(s) which are writing the value, but are never synchronizing on the threads reading the value.  You must always synchronized on all threads that read/write a value which is shared between thread.  Absolutely nothing is synchronized if you only synchronize on read or only on write.
You use synchronize(myValue), but you keep changing myValue, so you are synchronizing on different objects over time, which basically means no synchronization.

By the way, it is possible that you made some tests and everything seemed synchronized.  However, you should be aware that those tests would fail on other cpu's, or could fail on your machine if you tried them enough time.

More general comments:

Your whole class MetricsUpdater can be dropped for a volatile String myValueSynchronized.  Note however that this only work with references to immutable Objects (such as String), or raw types.
To answer your question "Does it even need to be synchronized at all if all of the other threads are only allowed to read it?"
NO!
You could forgo synchronization however if the value was final and unmodifiable.


Persist values for a known set of keys in Python Use case: I sometimes write programs that want to store a little state between runs, e.g., "What was the last file selected last time this program ran?" I wanted a convenient way to track this state. Python's built-in shelve module is pretty good but I wanted a way to restrict the possible set of keys in the shelf so that I wouldn't (due to a bag) store something under some unexpected key that would never be read. While I was at it I made it so the you access values using foo.a instead of foo['a'], though I paid a price for this.

Questions I have about this code are:

Is there an easier way I could have accomplished this using built-in stuff or widely used libraries?
Did I choose the right design?

I could have tried to inherit from one of the existing shelf classes, though the shelve module decides at runtime what sort of shelf you should get back from shelve.open.
I could have tried to use descriptors to create my own pseudo-properties, sort of like what's done here, but I don't see a need to decide which fields to include at the class, rather than instance, level

Is there any less gross for me to use __getattr__ and __setattr__ (without having to store a list of fields to be handled as special)?
Did I handle the nested context managers (shelf within RestrictedShelf) correctly? Do I need a bunch of try/except logic in __exit__, as in the fourth example in this answer?
Should I be defining my own exception classes, identical with Exception beyond the class name, as I did?
Is making _check_special_keys a class method, rather than a static method, the correct approach?

import shelve
import os
import sys

class UnexpectedKeyError(Exception):
    pass
class BadSpecialKey(Exception):
    pass

class RestrictedShelf(object):
    _SPECIAL_KEYS = ["_filepath", "_keys", "_shelf"]

    @classmethod
    def _check_special_keys(cls):
        for key in cls._SPECIAL_KEYS:
            if not key.startswith("_"):
                raise BadSpecialKey("Special key does not start with underscore: '{}'".format(key))

    def __init__(self, filepath, keys):
        self._filepath = filepath
        self._keys = list(keys) # Make a copy in case someone modifies the original list

        for key in keys:
            if not isinstance(key, str):
                raise TypeError("String object expected for key, {} found".format(key.__class__.__name__))
            # The keys in _SPECIAL_KEYS can't be used, but let's just rule out all keys that start with underscore for good measure
            if key.startswith("_"):
                raise ValueError("Key illegally starts with underscore: '{}'".format(key))

    def __enter__(self):
        self._shelf = shelve.open(self._filepath)
        for key in self._shelf.keys():
            if key not in self._keys:
                # This is not quite the same thing as a regular KeyError
                raise UnexpectedKeyError(key, "Shelf at '{}' contains unexpected key '{}'".format(self._filepath, key))
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if sys.version_info[0] <= 2:
            self._shelf.close()
        else:
            self._shelf.__exit__(exc_type, exc_val, exc_tb)

    def __getattr__(self, key):
        self.check_key(key)
        return self._shelf.get(key)

    def __setattr__(self, key, value):
        if key in self._SPECIAL_KEYS:
            # https://stackoverflow.com/a/7042247/2829764
            super(RestrictedShelf, self).__setattr__(key, value)
        else:
            self.check_key(key)
            self._shelf[key] = value

    def check_key(self, key):
        if key not in self._keys:
            raise KeyError(key)

RestrictedShelf._check_special_keys()

if __name__ == "__main__":
    with RestrictedShelf(os.path.expanduser("~/tmp/test.shelf"), "a b c".split()) as rs:
        print(rs.a) # `None` the first time you run this; `10` thereafter
        rs.a = 10
        print(rs.a) # 10
        print(rs.b) # None

1. Separation of concerns

There are three main features in this code:

Restricting the keys that can be stored in a dictionary.
Presenting a dictionary as a namespace.
Adding context manager behaviour to shelves in older versions of Python.

The principle of separation of concerns indicates that these should be implemented separately. See §3 below for one way to do this. This makes the code more understandable, testable, and reuseable.

2. Other review points

There are no docstrings. What does this code do? How do I use it?
The code in __exit__ assumes that Shelf objects have context manager behaviour in Python 3.0 or later. But the documentation says that this behaviour was only added in Python 3.4.
Storing _keys as a list means that checking to see if a key is valid takes time proportional to the number of keys. It would be better to store the keys in a set so that checking validity takes constant time.
There are implementations of __getattr__ and __setattr__ but not __delattr__.
A caller who tried to store a key check_key in the shelve would end up overwriting the method of that name.

3. Revised code

Restricting the keys that can be stored in a dictionary.

import collections.abc

class RestrictedMapping(collections.abc.MutableMapping):
    """A proxy for a mapping that restricts the set of allowable keys.

    >>> d = RestrictedMapping({0:0, 1:1}, range(3))
    >>> del d[1]
    >>> d[2] = 2
    >>> d[3] = 3
    Traceback (most recent call last):
        ...
    KeyError: 3
    >>> sorted(d.items())
    [(0, 0), (2, 2)]

    """
    def __init__(self, mapping, keys):
        keys = set(keys)
        for key in mapping:
            if key not in keys:
                raise ValueError('invalid key {}'.format(key))
        self.keys = keys
        self.mapping = mapping
        super(RestrictedMapping, self).__init__()

    def _check_key(self, key):
        if key not in self.keys:
            raise KeyError(key)

    def __getitem__(self, key):
        self._check_key(key)
        return self.mapping[key]

    def __setitem__(self, key, value):
        self._check_key(key)
        self.mapping[key] = value

    def __delitem__(self, key):
        self._check_key(key)
        del self.mapping[key]

    def __iter__(self):
        return iter(self.mapping)

    def __len__(self):
        return len(self.mapping)

Presenting a dictionary as a namespace.

class Namespace(object):
    """A proxy for a mapping that provides access to items via attribute
    lookup.

    >>> d = dict(a=1, b=2, c=3)
    >>> n = Namespace(d)        # n is now a proxy for d
    >>> n.d = 4
    >>> n.a, n.b, n.c, n.d
    (1, 2, 3, 4)
    >>> del n.b
    >>> sorted(d.items())
    [('a', 1), ('c', 3), ('d', 4)]

    """
    def __init__(self, mapping):
        super(Namespace, self).__setattr__('_mapping', mapping)

    def __getattr__(self, attr):
        return self._mapping[attr]

    def __setattr__(self, attr, value):
        self._mapping[attr] = value

    def __delattr__(self, attr):
        del self._mapping[attr]

Combining restriction on keys, presentation as namespace, and addition of context manager behaviour.

from contextlib import contextmanager
import shelve

@contextmanager
def restricted_shelve(filename, keys):
    shelf = shelve.open(filename)
    try:
        yield Namespace(RestrictedMapping(shelf, keys))
    finally:
        shelf.close()

4. Namespace antipattern

Presenting a mapping as a namespace instead of a dictionary is superficially attractive because n.name is three characters shorter than d['name'], but it's an antipattern in Python. (The only place this pattern is used in the standard library is in the argparse module.)

The problems are:

Attribute names need to be syntactically valid as Python identifiers, so you have to be very sure you're never going to need an attribute called class or return or 1 or whatever.
There will be collisions between the data attributes and the implementation attributes. In my version in §3.2 above you can't have attributes named _mapping or __init__ or __dict__ or __slots__ or ...
There's no easy way to get at the list of attributes.
There's not much you can do with a namespace-like object: the Python standard library usually expects you to use mappings instead. For example, you can't pass a namespace to itertools.ChainMap, or convert it to JSON using json.dumps, or output it as CSV using csv.DictWriter, and so on.

5. Answers to questions

As far as I know, there's no easy way to implement features 1 (restriction on keys) and 2 (presentation as namespace). Feature 3 (adding context manager behaviour) is more easily done using contextlib.contextmanager as shown in §3.3 above.
Your design puts all features into one class. This technique is known as the "big ball of mud".
Since your code knows exactly when it is assigning a special attribute, it can call super().__setattr__ itself, thus avoiding the need for special handling in the __setattr__ method. See §3.2 above.
Your code looks OK to me, but writing __exit__ methods is complicated and so (if you can) it's better to use contextlib.contextmanager which takes care of this complexity for you.
It's sensible to define your own exception classes, but it's good practice to inherit directly from Exception only if there is no close match in the exception hierarchy. Here UnexpectedKeyError is a runtime problem caused by an invalid value of the correct type, so it makes sense to inherit from ValueError.

The BadSpecialKey exception indicates a static programming error, not runtime exceptional behaviour, so I would use an assertion here.
It would be better to avoid having special keys altogther: you'll see in §3 above that I didn't need them. But if you must, then @classmethod makes slightly more sense: in theory you might have a subclass with a different value for _SPECIAL_KEYS.


Should I use CDATA in javascript? What difference does this make:

<script type="text/javascript">
//<![CDATA[ ...code

Compared to this

<script type="text/javascript"> ...code

You should not use XHTML 1.0 anymore ( which is where you would use CDATA ). What is even more important, you should no have javascript inside your html files. Instead you should include external files via <script src="/my/js/file.js"></script>.


Find the largest element in a sorted array with increasing consecutive differences, smaller than x, in O( log(logmax_value) I want to find the largest element in a sorted array, where the differences between the consecutive elements  of the array are increasing, that is smaller than x.
For example:

[1, 3, 10, 17, 50, 1000] is a valid input , because the consecutive differences (2,7,7,33,950) are in increasing order.
[1, 3, 4, 10, 18] is not a valid input, as the consecutive differences (2, 1, 6, 8) are not in increasing order.

The optimum solution is O(log(log max_value)), so I am looking for better solutions than upperbound/ binary search. Actually ,I am looking to optimize binary search from O(log N) to O(log(log max_value).
#include <bits/stdc++.h>

using namespace std;

const int n=50000001;
int arr[n];

int binarysearch_lowerbound(int begin, int end, int x){
    int ans=end;
    while(begin<end){
    int mid=(begin+end)/2;
        if(arr[mid] >= x){
            ans = mid;
            end = mid;
        } else {
            begin = mid + 1;
        }
    }return ans;
}
int main() {
    int N;
    cin>>N;
    for (int i=0; i<N; i++){
        cin>> arr[i];
    }
    int q;
    cin>>q;
    for (int i=0; i<q; i++){
        int x;
        cin>>x;
        int k;
        int begin = x/(arr[N-1]-arr[N-2]);
        int end = x/(arr[1]-arr[0]);
        if(end>N-1){
            end=N;
        }
        k=binarysearch_lowerbound(begin,end,x);
        cout<<arr[k-1]<<endl;
    }
    return 0;
}

#include <bits/stdc++.h>

Not a good start.  This is not a standard C++ header; it's an artefact of your compiler's implementation, so non-portable, and subject to change without notice.  Use the Standard-specified headers to define the library identifiers you need (any good reference tells you which to include).

using namespace std;

Please don't bring all of the Standard Library identifiers into the global namespace.  It makes it much harder to reason about your code, and in extreme cases, can cause future versions of C++ to silently change the meaning of the program (when a new library function becomes a better match than one you define, for example).

const int n=50000001;

Where does this magic constant come from?  What does it mean? n is not very descriptive for a global constant.

int arr[n];

Another global variable with an unhelpful name.  What's it for?  Why does it need to be global?  Why are we using C-style arrays rather than std::array or std::vector?

int binarysearch_lowerbound(int begin, int end, int x){

This function isn't very reusable, as it depends on the global arr.  If we passed pointers instead of integers for begin and end, then we would be able to find a value in any array we like.  And why are we passing signed integers?  Why would a negative value ever be useful?

    int ans=end;

We don't need this variable, since at the end of the loop ans==begin==end.  So just return end when we get there.

    while(begin<end){

Nothing wrong with this line (although I would like the operators to have more "room to breathe").

      int mid=(begin+end)/2;

There's a problem here that's frequently found in binary search algorithms.  The sum could overflow (which in signed integers, is Undefined Behaviour).  A better way, given that we know begin is less than end, is
        int mid = begin + (end - begin) / 2;

This also has an advantage of being legal when we replace the indices with pointers.
That said, what we have here is plain binary search.  We haven't used the information about the distribution (namely that the curve is concave, so all values are no higher than the straight line joining begin and end).  We're only going to scale as O(log n).

int main() {
    int N;
    cin>>N;

Again, an utterly unhelpful variable name.  I had to read the rest of main() to determine that this is supposed to be the array size to read.
After reading from a stream, we must confirm that the stream is not in error before using the value we read (e.g. if (!std::cin) { return EXIT_FAILURE; }).

    for (int i=0; i<N; i++){
        cin>> arr[i];
    }

If we'd used a std::vector for this, we could replace that loop with a simple standard copy:
std::copy_n(std::istream_iterator<int>(std::cin), N, std::back_inserter(arr));

Again, we need to check the error state after reading.

    int q;
    cin>>q;

What's q?  I'm guessing a query into the array we have read.

    for (int i=0; i<q; i++){
        int x;
        cin>>x;

No, q must be the number of queries, and x is the actual query.  These unhelpful names are really making the code harder to follow!

        int k;

Why declare k here?  It's best to declare and initialise variables together, to reduce the risk of using it before it's initialised (though any decent compiler will warn if you do that).

        int begin = x/(arr[N-1]-arr[N-2]);
        int end = x/(arr[1]-arr[0]);

Here, we're using gradient to estimate the bounds for our search, but I'm not convinced the calculation is correct (shouldn't begin be subtracted from N?).  When it has been corrected, I think we should be doing this inside binarysearch_lowerbound() at every iteration, since each subarray obeys the constraint on its shape.

        if(end>N-1){
            end=N;
        }
        k=binarysearch_lowerbound(begin,end,x);
        cout<<arr[k-1]<<endl;

There's no need to flush output using std::endl.  Just output an ordinary return "\n" and let the stream do its buffering.

    }
    return 0;

In C++, we're allowed to omit return 0; from the end of main() (but only main(), not any other function).  Success is assumed if we run off the end of the main function.

Modified code
Fixing the faults (other than the algorithm to reduce the search space, which is your own responsibility), I get:
/* Constraints (TODO: express using C++20 Concepts):
 *  * Iter must be a random-access iterator
 *  * Its value type must be an arithmetic type
 */
template<typename Iter, typename T>
Iter binarysearch_lowerbound(Iter begin, Iter end, const T x)
{
    --end;                      // make an inclusive range
    while (begin < end) {
        // TODO: improve algorithm to use curve constraint
    auto mid = begin + (end - begin + 1) / 2;
        if (x <= *mid) {
            end = mid - 1;
        } else {
            begin = mid;
        }
    }
    return begin;
}

#ifdef TEST

#include <array>
#include <cassert>
#include <iterator>

int main() {
    auto check = [](auto collection, auto value)
                 { using std::begin; using std::end; // for argument-dependent lookup
                     return *binarysearch_lowerbound(begin(collection), end(collection), value) ; };
    assert(check(std::array{ 0, 1, 1, 5, 10 }, 11) == 10);
    assert(check(std::array{ 0, 1, 1, 5, 10 },  8) == 5);
    assert(check(std::array{ 0, 1, 1, 5, 10 },  5) == 1);
    assert(check(std::array{ 0, 1, 1, 5, 10 },  -1) == 0);
}

#else  // not test

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

int main()
{
    using value_type = int;     // would work for doubles, rationals, etc

    std::size_t array_size;
    if (!(std::cin >> array_size)) {
        return EXIT_FAILURE;
    }
    std::vector<value_type> values;
    values.reserve(array_size);
    std::copy_n(std::istream_iterator<int>(std::cin), array_size,
                std::back_inserter(values));
    if (!std::cin) {
        return EXIT_FAILURE;
    }

    std::size_t query_count;
    if (!(std::cin >> query_count)) {
        return EXIT_FAILURE;
    }

    while (query_count-->0) {
        value_type query;
        if (!(std::cin >> query)) {
            return EXIT_FAILURE;
        }
        std::cout << *binarysearch_lowerbound(values.begin(), values.end(), query);
    }
}

#endif


Mouse control of a camera in an OpenGL program Introduction

I'm doing an OpenGL program in C. As of now I'm working on the 3D camera system and got the control right. Now I'm working on the mouse control.
It works, but I have used two different ways to do it.

The problem

The two different ways are using a global variable and callback, and just using a local variable and function.

I don't know which one is better. One seems to loop over itself every time and the other only when the mouse moves, but uses a global variable.

For all the following code you will see that the Camera struct is defined as:

Camera camera = {
        .view = GLM_MAT4_IDENTITY_INIT,
        .pos = { 0.f,0.f,3.f },
        .target = { 0.f, 0.f, 0.f },
        .upAxe = { 0.0f, 1.0f, 0.0f },
        .front = { 0.0f, 0.0f, -1.0 },

        .yaw = -90.f,
        .pitch = 0.f,

        .lastX = SCR_HEIGHT / 2,
        .lastY = SCR_WIDTH / 2,

        .lastFrame = 0.0f,
        .deltaTime = 0.0f
};

The code

First the "global" way:

I have only shown the intended part of the code; other code is not pertinent.

main.c:

void mouseCallBack(GLFWwindow * window, double xpos, double ypos);

Camera camera = {
    .view = GLM_MAT4_IDENTITY_INIT,
    .pos = { 0.f,0.f,3.f },
    .target = { 0.f, 0.f, 0.f },
    .upAxe = { 0.0f, 1.0f, 0.0f },
    .front = { 0.0f, 0.0f, -1.0 },

    .yaw = -90.f,
    .pitch = 0.f,

    .lastX = SCR_HEIGHT / 2,
    .lastY = SCR_WIDTH / 2,

    .lastFrame = 0.0f,
    .deltaTime = 0.0f
};

int main()
{
    glfwSetCursorPosCallback(window, mouseCallBack);
    while (!glfwWindowShouldClose(window))
    {
    }
}

void mouseCallBack(GLFWwindow * window, double xpos, double ypos)
{

    float xoffset = xpos - camera.lastX;
    float yoffset = camera.lastY - ypos;

    camera.lastX = xpos;
    camera.lastY = ypos;

    float sensivity = 0.05f;
    xoffset *= sensivity;
    yoffset *= sensivity;

    camera.yaw += xoffset;
    camera.pitch += yoffset;

    if (camera.pitch > 89.f) camera.pitch = 89.f;
    if (camera.pitch < -89.f) camera.pitch = -89.f;

    vec3 front;
    front[0] = cos(glm_rad(camera.yaw)) * cos(glm_rad(camera.pitch));
    front[1] = sin(glm_rad(camera.pitch));
    front[2] = sin(glm_rad(camera.yaw)) * cos(glm_rad(camera.pitch));

    glm_normalize_to(front, camera.front);

}

The function and local way:

int main()
{
    Camera camera = {
            .view = GLM_MAT4_IDENTITY_INIT,
            .pos = { 0.f,0.f,3.f },
            .target = { 0.f, 0.f, 0.f },
            .upAxe = { 0.0f, 1.0f, 0.0f },
            .front = { 0.0f, 0.0f, -1.0 }
    };

    camera.yaw = 0.f;
    camera.pitch = 0.f;

    camera.lastX = SCR_HEIGHT / 2;
    camera.lastY = SCR_WIDTH / 2;

    camera.lastFrame = 0.0f;
    camera.deltaTime = 0.0f;

    while (!glfwWindowShouldClose(window))
    {
        //input
        processMouse(window, &camera);
    }
}

and in GLFWfunction.c:

void processMouse(GLFWwindow * window, Camera * camera)
{
    double xpos;
    double ypos;

    glfwGetCursorPos(window, &xpos, &ypos);

    if (xpos != camera->lastX || ypos != camera->lastY)
    {
        float xoffset = xpos - camera->lastX;
        float yoffset = camera->lastY - ypos;

        float sensivity = 0.1f;

        xoffset *= sensivity;
        yoffset *= sensivity;

        camera->yaw += xoffset;
        camera->pitch += yoffset;
        if (camera->pitch > 89.0f) camera->pitch = 89.f;
        if (camera->pitch < -89.0f) camera->pitch = -89.f;

        vec3 front;
        front[0] = cos(glm_rad(camera->pitch)) * cos(glm_rad(camera->yaw));
        front[1] = sin(glm_rad(camera->pitch));
        front[2] = sin(glm_rad(camera->yaw)) * cos(glm_rad(camera->pitch));
        glm_normalize_to(front, camera->front);

        camera->lastX = xpos;
        camera->lastY = ypos;
    }
}

What would you choose? My eyes tell me that the global alternative is good and clean, but contradict a bit of the C logic whereas the function one is good but is poorly written.

Is the camera a global entity?  Will the only ever be just one?

Perhaps, but perhaps not. You can have two or more viewpoints shown in multiple viewports.  You might use shadow mapping, which positions a camera at a light source for rendering a shadow map.

If you are thinking of a generic “mouse controller” for a camera, which you can reuse in multiple applications, I’d shy away from the global camera.

If you are working on a one of a kind application, have no intention of reusing the code, will only ever use one point-of-view, and needs the minuscule speed gain and reduction of complexity of not passing a pointer to the camera around to functions as required, a global camera is fine.


Carpet Calculator Program Final Edit I asked a previous question of this same program. I would like more opinions before I submit this assignment since I edited the program after receiving feedback last time.

public class Room_Dimension
{
    //Data fields
    private double length;
    private double width;

    //Constructor
    public Room_Dimension(double length, double width)
    {
        super();
        this.length = length;
        this.width = width;
    }

    //Copy constructor
    public Room_Dimension(Room_Dimension rd)
    {
        this.length = rd.getLength();
        this.width  = rd.getWidth();
    }

    //Accessors
    public double getLength()
    {
        return length;
    }

    public double getWidth()
    {
        return width;
    }

    //Mutators
    public void setLength(double length)
    {
           this.length = length;
    }

    public void setWidth(double width)
    {
           this.width = width;
    }

    //Function to calculate the area
    public double getArea()
    {
        return length * width;
    }

    //Function to display information
    public String toString()
    {
        return "Dimensions of room: (length = " + length + ", width = " + width + ")";
    }

}

public class Room_Carpet
{
    //Data fields
    private Room_Dimension rd;
    private double carpetCost;

    //Constructor
    public Room_Carpet(Room_Dimension rd, double carpetCost)
    {
        super();
        this.rd = rd;
        this.carpetCost = carpetCost;
    }

    //Copy constructor
    public Room_Carpet(Room_Carpet rc)
    {
         this.rd             = rc.rd;
         this.carpetCost     = rc.getCarpetCost();
    }

    //Function to determine total cost of carpet that will fill area
    public double getTotalCost()
    {
        return carpetCost * rd.getArea();
    }

    //Function to get size
    public Room_Dimension getSize()
    {
        return rd;
    }

    //Function to get carpet cost
    public double getCarpetCost()
    {
        return carpetCost;
    }

    //Function to display information
    public String toString()
    {
        return "" + rd + ", Carpet cost per square feet = $" + carpetCost + ", " + "Total cost = $" + getTotalCost() + "";
    }

}

public class Carpet_Calculator
{
    public static void main(String[] args)
    {
        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);

        // Display information about program.
        System.out.println("This program will display the carpet cost of a room.");
        System.out.println("Please enter the following information.");

        // Get the length of the room.
        System.out.print("Enter the length of room: ");
        double length = keyboard.nextDouble();

        // Get the width of the room.
        System.out.print("Enter the width of room: ");
        double width = keyboard.nextDouble();

        // Get the cost of price per square feet of the desired carpeting.
        System.out.print("Enter the price per square feet for the desired carpeting: ");
        double carpetCost = keyboard.nextDouble();

        //Close keyboard
        keyboard.close();

        // Create Room_Dimension and Room_Carpet objects.
        Room_Dimension dimension = new Room_Dimension(length, width);
        Room_Carpet roomCarpet = new Room_Carpet(dimension, carpetCost);

        // Print the object calling the toString
        System.out.println(roomCarpet);
    }
}

Your code is near perfection, if you want to "get OCD" about it you can skip all the empty lines, comments on self-explanatory methods and fields and open code blocks on the same line, for example:

 public Room_Dimension(double length, double width)
{
    super();
    this.length = length;
    this.width = width;
}

and all the rest, could be written as:

 public Room_Dimension(double length, double width){
    super();
    this.length = length;
    this.width = width;
}

to save space, so that the class is visible within the screen with no need of scrolling. Additionally, search for program layout and class interface best practices, to get insight on the subject.


Ghostscript check, sends e-mail if one occurs I've created a program that will search our work server where Ghostscripts occur. How it accomplishes this is by first sshing to the server specified, running a bash command to search the server for Ghostscripts caused by our guy that uses them, most frequently. If it finds one, it e-mails somebody on my team. It will also e-mail if it doesn't find one.

I'm curious to know if there are other ways I can do this specific task. Better syntax, etc..

#!/local/usr/bin/ruby

require 'rubygems'
require '<mailgem>'
require 'etc'
require 'net/ssh'

class GhostScript

  attr_accessor :host, :usr, :pass

  def initialize(host, usr, pass)
    @host = host
    @usr  = usr
    @pass = pass
  end

  def script_search
    ssh = Net::SSH.start(host, usr, :password => pass)
    res = ssh.exec!('ps -u <user>|grep gs')
    if res == nil
      MailGem::Mail.new do |m|
        m.to      = '<email-address>'
        m.subject = 'GhostScript not found'
        m.body    = "GS not found on server: #{host}"
      end.send
      else
      alert(res)
    end
  end

  def alert(res)
    MailGem::Mail.new do |m|
      m.to      = '<email-address>'
      m.subject = 'GhostScript found'
      m.body    = "GS script found on server: #{host}",
                  "Results of search: #{res}"
    end.send
  end
end

check = GhostScript.new('<server>', Etc.getlogin, nil)
check.script_search

Looks pretty good, just some minor details:

if res == nil -> if !res
DRY the sending of the email by creating a new method: send_email(to, subject, body)
Subjective: I'd not include a blank lines when there is a change of indentation level (between class and attr_accesor)


Printing the days of the week for every day this year @chux pointed out that my attempted solution contained several problems.  Since I screwed that up so badly, I figure that I should put my revised solution up for review.

#include <stdio.h>
#include <time.h>

/**
 * The current year (number of years since 1900)
 */
static int this_year(void) {
    time_t now = time(NULL);
    struct tm thetime;
    localtime_r(&now, &thetime);
    return thetime.tm_year;
}

static struct tm jan1(int yy) {
    struct tm t = { 0 };
    t.tm_year = yy;
    t.tm_mday = 1;
    t.tm_isdst = -1;
    return t;
}

int main(void) {
    int yy = this_year();

    for (struct tm t = jan1(yy); mktime(&t), t.tm_year == yy; t.tm_mday++) {
        char buf[80];
        if (!strftime(buf, sizeof buf, "%m/%d/%Y is a %A", &t)) {
            return 1;   // Unexpected failure
        }
        puts(buf);
    }
}

Concerns include standards compliance, portability, and correctness in all possible time zones.  If possible, a more succinct way to obtain the current year and January 1 of the current year would be appreciated.

The only thing I have to say is that the use of strftime and %A means that the output will depend on the current locale setting.

This may be a bug or a feature depending on what your original intention was.

Also, the use of char buf[80] seems rather small for today's processors. The only way that strftime is going to fail is if it can't fit all of the output in the buffer. Since memory is cheap, you might as well make it char buf[256] or buf[1024]. This kinda of ties in with with the question of whether you expect to use this with custom locales.

Alternatives:

use strftime_l to use a specific locale
create your own array of weekday names instead of %A and use the tm_wday field to index into that array


Removing stray brackets from in between shortcodes I was wondering if someone could point me in the right direction for cleaning up this code.

WordPress uses shortcodes in the form [shortcode] to add extended functionality to posts and pages.

What I am trying to do is to return a string with all of the 'stray' brackets, ones not being used by any of shortcodes, as html entities.

What I have to work with are these variables via WordPress:

$shortcode_tags: an array of all shortcodes as strings (without brackets)
get_shortcode_regex(): combines all registered shortcode tags into a single regular expression. (I am not using this in the code below)

function find_stray_brackets ($content) {
// this code repalces all occurences of every shortcode with special brackets.
// It then replaces remaining 'stray' brackets with the HTML character entitiy.

        global shortcode_tags;
        foreach($shortcode_tags as $k=>$item)
        {

            $pattern='/\[('.$k.'.*?(?=\]))\]/';
            $content=preg_replace($pattern,'*{*$1*}*',$content);
            $pattern='/\[\/('.$k.'.*?(?=\]))\]/';
            $content=preg_replace($pattern,'*{*/$1*}*',$content);

        }

        $content=str_replace('[','&#91;',$content);
        $content=str_replace(']','&#93;',$content);
        $content=str_replace('*{*','[',$content);
        $content=str_replace('*}*',']',$content);

    return($content);
}

The approach is to replace all of the brackets that surround shortcodes with special characters-> replace stray brackets with html entities -> re-insert original brackets

My end goal is to put all of the text between short codes (including before the first and after the last shortcode) into an array. I couldn't get the regex that Wordpress produces to do what I wanted, so wrote my own regex that relies on brackets - this is why I need to deal with the strays.

Here is the regex that WordPress outputs and some sample text with nested shortcodes.

You can simplify and improve this part:

$pattern = '/\[('.$k.'.*?(?=\]))\]/';
$content = preg_replace($pattern,'*{*$1*}*',$content);
$pattern = '/\[\/('.$k.'.*?(?=\]))\]/';
$content = preg_replace($pattern,'*{*/$1*}*',$content);

Instead of using two replacements for [k] and [/k],
you can add the /? inside the capture group and use a single replacement:

$pattern = '/\[(\/?'.$k.'.*?(?=\]))\]/';
$content = preg_replace($pattern,'*{*$1*}*',$content);

This regex can be still improved. Is the positive look-ahead really necessary?
It seems to me that this should do the job just as well:

$pattern = '/\[(\/?'.$k.'.*?)\]/';

On the other hand, this is not very strict. For example,
if you have shortcode gallery, then [galleryzzz] will also match and not be treated as "stray brackets". Maybe that's fine with you, but in any case,
it's easy to prevent that by adding a zero-width word boundary check:

$pattern = '/\[(\/?'.$k.'\b.*?)\]/';


Finding the difference in time between two events for a specific ID Details about code:

Dataframe (x) shape: (20494463, 31)
In this Dataframe, each id shows up multiple times with different events and the event dates. What I'm looking for is the time between the first time event A occurs and the first time event B occurs for each id
feature_final.index.values is another Dataframe whose indexes are the id's I need to work with

def feature(x):
   times = []
   for j in feature_final.index.values:

     pat = x[x.id == j]
     min_dates_j = pat.groupby(["event_description"]).min()

     try:
         time = ((min_dates_j.loc["A","event_description"] -  min_dates_j.loc["B","event_description"]).days)
     except:
         #RX or DX not present
         time = np.NaN()

     times.append(time)

     return times

Some general suggestions:

Run the code through pycodestyle to get several suggestions for writing idiomatic Python:

  indentation is not a multiple of four

  missing whitespace after ','

  multiple spaces after operator

  line too long

  do not use bare except' [sic]

  block comment should start with '# '

Don't use single letter variables - it makes it very hard to maintain the code.
Don't shadow common names like time - if you ever need the time module in the code you'll have to rename the import or your variable.
You have unnecessary outer parens in the time = … line.
Indexing by a non-trivial expression is confusing. Assign x.id == j to a variable first so the maintainer knows what it means.
feature_final is accessed like a global. You should instead pass it in.
If you're processing a huge list you might want to try returning an iterable instead of a list - that way other code can start processing the output before it's complete.


(Codewars) Linked Lists-Sorted Insert (2) Original Question.
So I rewrote the code for the problem described in the original question, but someone had already answered that question, and my rewrite would have invalidated their answer, so I just posted a new question.
Updated Solution
def sorted_insert(head, data):
    tmp = Node(None)
    tmp.next = head
    node = Node(data)
    while tmp.next:
        if tmp.next.data > data:
            break
        tmp = tmp.next
    node.next = tmp.next
    tmp.next = node
    return head if tmp.data else node

To me it seems the purpose of this rewrite is to mitigate the special treatment for replacing the head. Like the previous solution, it also suffers from lack of separation of distinct logical elements. In this example, tmp is used for two purposes: traverse the list, and act as the dummy to check if head was replaced.

This is one step away from a clear dummy node to prefix the list to eliminate the special treatment of the head, and I think this clean separation is simpler and easier to understand:

def sorted_insert(head, data):
    # a dummy node inserted in front of head
    dummy = Node(None)
    dummy.next = head

    # whatever happens in the rest, the new head will be dummy.next

    # loop until the insertion point
    node = dummy
    while node.next:
        if node.next.data > data:
            break
        node = node.next

    # create the new node and insert it
    new_node = Node(data)
    new_node.next = node.next
    node.next = new_node

    # return the head. it may or may not have been replaced
    return dummy.next


A javascript equivalent for toDictionary (c#) I made a function that should ease the making of new objects out of objects or arrays.

I extended underscore so thats why the _.

        _.dictionary = function (obj, keyIteratee, valueIteratee) {
            var newObj = new Object();
            if (Array.isArray(obj)) {
                var length = obj.length;
                for (var index = 0; index < length; index++) {
                    newObj[keyIteratee(obj[index], index)] = valueIteratee(obj[index], index);
                }
            } else { //assume its an object
                var keys = Object.keys(obj);
                var length = keys.length;
                for (var index = 0; index < length; index++) {
                    newObj[keyIteratee(obj[keys[index]], keys[index])] = valueIteratee(obj[keys[index]], keys[index]);
                }
            }
            return newObj;
        };

usage:

_.dictionary(inputArray, function(s) { return s.Name; }, function(s) { return s.Value; });
_.dictionary(inputObject, function(s,i) { return i; }, function(s) { return s.Inner.Value; });

Is this a good way to tackle such a thing? am i missing something?

When providing an object, I don't think it makes sense to pass the index into the callbacks. Rather, pass in the current key.

Also, I think reduce reads much clearer here:

_.dictionary = function (source, keyGenerator, valueGenerator) {
    if (Array.isArray(source)) {
        return source.reduce(function (output, value, index) {
            output[keyGenerator(value, index)] = valueGenerator(value, index);
            return output;
        }, {});
    }

    return Object.keys(source).reduce(function (output, key) {
        output[keyGenerator(source[key], key)] = valueGenerator(source[key], key);
        return output;
    }, {});
};

See it here in action: https://jsfiddle.net/e4g4p7wy/


A small app-crawler created using python I've written some code using python having tried to comply with OOP design for the purpose of collecting name of the app, price and developer name from i-tune site by going deeper until there is a limit [which can be toggled within my script] to stop as it has millions of links to go. It is working impeccably at this moment. I tried my best to make this scraper pythonic. However, suggestions to bring about any change to make this crawler more robust will be highly appreciable. Thanks in advance.

from lxml import html
import requests

class app_scraper:

    start_url = "https://itunes.apple.com/us/app/candy-crush-saga/id553834731?mt=8"

    def __init__(self):

        self.links = [self.start_url]
        self.vault = []

    def crawler(self):

        for url in self.links:
            self.get_app(url)

    def get_app(self, url):

        print("Scraping now "+ url)
        page = requests.get(url)
        tree = html.fromstring(page.text)

        name = tree.xpath('//h1[@itemprop="name"]/text()')[0]
        developer = tree.xpath('//div[@class="left"]/h2/text()')[0]
        price = tree.xpath('//div[@itemprop="price"]/text()')[0]

        item = processor(name, developer, price)
        self.vault.append(item)

        p_links = tree.xpath('//div[@class="lockup-info"]//li/a[@class="name"]/@href')
        for p_link in p_links:
            if not len(self.links)>=5:  # It'll stop crawling until it meets the limit whatever it is
                self.links += [p_link]

class processor:

    def __init__(self, name, developer, price):

        self.name = name
        self.developer = developer
        self.price = price

    def __str__(self):
        return (self.name+" "+self.price+" "+self.developer)

crawl = app_scraper()
crawl.crawler()

for info in crawl.vault:
    print(info)

Good job improving the modularity of your code under reviews.

We can improve the following:

follow Python's naming convention and use camel-case style names for your classes
let's as usual reuse a Session instance
let's also use findtext() to get element texts
I would either use namedtuple for your "processor"s or use __slots__

Improved code:

from lxml import html
import requests

class AppScraper:
    start_url = "https://itunes.apple.com/us/app/candy-crush-saga/id553834731?mt=8"

    def __init__(self):
        self.session = requests.Session()

        self.links = [self.start_url]
        self.vault = []

    def crawler(self):

        for url in self.links:
            self.get_app(url)

    def get_app(self, url):
        print("Scraping now "+ url)

        page = self.session.get(url)
        tree = html.fromstring(page.text)

        name = tree.findtext('.//h1[@itemprop="name"]')
        developer = tree.findtext('.//div[@class="left"]/h2')
        price = tree.findtext('.//div[@itemprop="price"]')

        item = Processor(name, developer, price)
        self.vault.append(item)

        p_links = tree.xpath('//div[@class="lockup-info"]//li/a[@class="name"]/@href')
        for p_link in p_links:
            if len(self.links) < 5:  # It'll stop crawling until it meets the limit whatever it is
                self.links += [p_link]

class Processor:
    __slots__ = ['name', 'developer', 'price']

    def __init__(self, name, developer, price):
        self.name = name
        self.developer = developer
        self.price = price

    def __str__(self):
        return self.name + " " + self.price + " " + self.developer

if __name__ == '__main__':
    crawl = AppScraper()
    crawl.crawler()

    for info in crawl.vault:
        print(info)


Print a character frequencies as a vertical graph I've been solving K&R 1-14.

Exercise 1-14. Write a program to print a histogram of the frequencies of different characters in its input.

After several edits, it looks good to me now. But it's my first time to write a long code(it's long to me). I wonder if there is more simple and shorter code with the same logic or I wrote a bad style code or not. I also wonder whether my vertical graph looks good and well written.  Any suggestion will be so appreciated!
#include <stdio.h>

/*  print a graph of the frequencies of different characters */

#define GRAPH_HEIGHT 20

main()
{
    int af[26] = {0};
    int max = 1;
    int c;
    int i, j;

    /* get characters */
    while ((c = getchar()) != EOF) {
        if (c >= 'a' && c <= 'z')
            if (++af[c - 'a'] > max) {
                ++max;
                continue;
            }
        if (c >= 'A' && c <= 'Z')
            if (++af[c - 'A'] > max)
                ++max;
    }

    /* calculate a relative frequencies */
    for (i = 0; i < 26; ++i) {
        af[i] = GRAPH_HEIGHT * af[i] / max;
    }

    /* print characters on a graph */
    for (i = GRAPH_HEIGHT; i > 0; --i) {
        printf("%2d |", i);
        for (j = 'a'; j <= 'z'; ++j) {
            if (af[j - 'a'] == i) {
                printf(" | ");
                --af[j - 'a'];
            }
            else
                printf("   ");
        }
        printf("\n");
    }

    printf("   " "*" "---------------" "---------------" "---------------" "---------------" "---------------" "---"  "\n");
    printf("   ");
    for (i = 'a'; i <= 'z'; ++i) {
        printf("%3c", i);
    }

    printf("\n");
}

This is a pretty good start.  You should be warned that K&R describes a very early version of C, and the language has evolved considerably since the 1980s.
For example, we normally always write function declarations as prototypes, i.e. specifying the argument types, and we don't depend on the implicit int.  So:
int main(void)

Also, we don't need to declare all variables at the start of their scope any more; instead, we can declare them where they are initialised:
for (int i = 0;  i < 26;  ++i)

Although the problem statement says that you should chart the frequency of different characters, we have implemented something different here, because we're only counting the characters between a and z and between A and Z.
While looking at that, there's a non-portable assumption in this code that 'Z'-'A' and 'z'-'a' are both 25.  That is true on ASCII systems, but definitely not true on EBCDIC systems, for example, where this code will index af outside its bounds.
You could solve this by declaring af differently:
#define AZ_COUNT ('z' - 'a' + 1)
unsigned int af[AZ_COUNT] = {0};

I think I would store counts of all different characters, and filter to just the alphabetic ones later (and use isalpha() for that, from <ctype.h>):
#include <limits.h>
unsigned int af[UCHAR_MAX+1] = {0};

That can make it easier to adapt the printing part (e.g. to show digits and punctuation characters) later.  (Note the use of unsigned int, too - a negative count makes no sense).
I think the printing part would be better if it didn't modify the counts.  We can ensure that if we split the counting and printing into separate functions:
void count_chars(FILE *in, unsigned int *frequencies);
void print_graph(FILE *out, const unsigned int *frequencies);

That const helps us write code that's more reusable.

With my changes, the code looks more like this (not perfect, but I hope it has some items to learn from):
#include <ctype.h>
#include <limits.h>
#include <stdio.h>

/*  print a graph of the frequencies of different characters */

#define GRAPH_HEIGHT 20

static void count_chars(FILE *in, unsigned int *frequencies)
{
    int c;
    while ((c = fgetc(in)) != EOF) {
        ++frequencies[(unsigned char)c];
    }
}

static void print_graph(FILE *out, const unsigned int *frequencies)
{
    /* start at 1 to avoid division by 0 later */
    unsigned int max = 1;
    for (unsigned int i = 0;  i <= UCHAR_MAX;  ++i) {
        if (frequencies[i] > max) {
            max = frequencies[i];
        }
    }

    /* print characters on a graph */
    for (unsigned int i = GRAPH_HEIGHT;  i > 0;  --i) {
        fprintf(out, "%2d |", i);
        for (int c = 0;  c <= UCHAR_MAX;  ++c) {
            if (!isalnum(c)) { continue; }
            char bar = frequencies[c] >= i * max / GRAPH_HEIGHT ? '|' : ' ';
            fprintf(out, " %c ", bar);
        }
        fputs("\n", out);
    }

    /* print the base line */
    fputs("   +", out);
    for (int c = 0;  c <= UCHAR_MAX;  ++c) {
        if (!isalnum(c)) { continue; }
        fputs("---", out);
    }
    fputs("\n", out);
    for (int c = 0;  c <= UCHAR_MAX;  ++c) {
        if (!isalnum(c)) { continue; }
        fprintf(stdout, "%3c", c);
    }
    fputs("\n", out);
}

int main(void)
{
    unsigned int af[UCHAR_MAX+1] = {0};
    count_chars(stdin, af);
    print_graph(stdout, af);
}


Pythonic type coercion from input My context is a very simple filter-like program that streams lines from an input file into an output file, filtering out lines the user (well, me) doesn't want. The filter is rather easy, simply requiring a particular value for some 'column' in the input file. Options are easily expressed with argparse:

parser.add_argument('-e', '--example', type=int, metavar='EXAMPLE',
    help='require a value of %(metavar)s for column "example"')

There's a few of these, all typed. As the actual filter gets a line at some point, the question whether to include such a line is simple: split the line and check all the required filters:

c1, c2, c3, *_ = line.split('\t')
c1, c2, c3 = int(c1), int(c2), int(c3) # ← this line bugs me

if args.c1 and args.c1 != c1:
    return False

The second line of which bugs me a bit: as the values are initially strings and I need them to be something else, the types need to be changed. Although short, I'm not entirely convinced this is the best solution. Some other options:

create a separate function to hide the thing that bugs me;
remove the type= declarations from the options (also removes automatic user input validation);
coerce the arguments back to strs and do the comparison with strs (would lead to about the same as what I've got).

Which of the options available would be the 'best', 'most pythonic', 'prettiest'? Or am I just overthinking this...

1. Possible bugs

I can't be certain that these are bugs, but they look dodgy to me:

You can't filter on the number 0, because if args.c1 is 0, then it will test false and so you'll never compare it to c1. Is this a problem? If it is, you ought to compare args.c1 explicitly against None.
If the string c1 cannot be converted to an integer, then int(c1) will raise ValueError. Is this what you want? Would it be better to treat this case as "not matching the filter"?
If line has fewer than three fields, the assignment to c1, c2, c3, *_ will raise ValueError. Is this what you want? Would it be better to treat this case as "not matching the filter"?

2. Suggested improvement

Fixing the possible bugs listed above:

def filter_field(filter, field):
    """
    Return True if the string `field` passes `filter`, which is
    either None (meaning no filter), or an int (meaning that
    `int(field)` must equal `filter`).
    """
    try:
        return filter is None or filter == int(field)
    except ValueError:
        return False

and then:

filters = [args.c1, args.c2, args.c3]
fields = line.split('\t')
if len(fields) < len(filters):
    return False
if not all(filter_field(a, b) for a, b in zip(filters, fields)):
    return False


Database module for node.js This will get quite clumsy if I continue down the road with this design, so I'm asking you, enlightened ones, is there a way to make this prettier and easier to understand? Primarily the run-function (Especially in the else if where self.query and self.find are set), also, I want it to be chain-able, with .run being the last function.

db = function(dir){
    if(dir[dir.length - 1] == "/") this.dir = dir.slice(0, -1);
    else this.dir = dir;
    return this;
}

db.prototype.table = function(table){
    this.table = table;
    return this;
}

db.prototype.put = function(obj){
    this.put = obj;
    return this;
}

db.prototype.find = function(query){
    this.query = query;
    this.find = "all";
    return this;
}

db.prototype.pluck = function(){
    this.pluck = arguments;
    return this;
}

db.prototype.run = function(callback){
    var self = JSON.parse(JSON.stringify(this));
    for(var key in this){
        if(typeof this[key] != "function" && key != "dir" && key != "table") delete this[key];
    }
    helper.mkdb(self, function(err, created){
        helper.mktbl(self, function(err, created){
            var docs = [];
            if(self.put){
                helper.put(self, function(err, docs){
                    callback(err, docs);
                })
            }else if(self.query && self.find){
                helper.find(self, function(err, docs){
                    if(err) callback(err);
                    else{
                        self.docs = docs;
                        if(self.pluck){
                            helper.pluck(self, function(err, docs){
                                callback(err, docs);
                            })
                        }else{
                            callback(err, docs);
                        }
                    }
                })
            }else if(self.pluck){
                helper.pluck(self, function(err, docs){
                    callback(err, docs);
                })
            }else callback(new Error("Cannot execute \".run()\" without any functions before it"));
        })
    });
}

I've found a way, it's not the best though. You can use nimble (Or async, it's basically the same but both larger in size and contain more functions) to do a series of functions, and then check from within the helper-class to see if a certain value has been set.

This is my new and improved version of the run-function:

db.prototype.run = function(callback){
    var self = JSON.parse(JSON.stringify(this));
    for(var key in this){
        if(typeof this[key] != "functions" && key != "dir" && key != "table") delete this[key];
    }
    helper.mkdb(self, function(err, created){
        helper.mktbl(self, function(err, created){
            self.docs = [];
            nimble.series([
                function(done){
                    if(!self.put) done(err, []);
                    else{
                        helper.put(self, function(err, docs){
                            self.docs = docs;
                            done(err, docs);
                        })
                    }
                },
                function(done){
                    if(!self.query && !self.find) done(err, self.docs || []);
                    else{
                        helper.find(self, function(err, docs){
                            self.docs = docs;
                            done(err, docs);
                        })
                    }
                },
                function(done){
                    if(!self.pluck) done(err, self.docs || []);
                    else{
                        helper.pluck(self, function(err, docs){
                            self.docs = docs;
                            done(err, docs);
                        })
                    }
                }
            ], function(err, res){
                callback(err, helper.lastData(res));
            })
        })
    });
}


Function to return the closest Date to Today (inclusive) from Ordered list The way I've written this feels clunky. Particularly, it feels like I shouldn't need to have both a 'dateToCheck' and 'selectedDate' variables.

The objective is to find the Date in a list that is the closest to today (inclusive), with a preference for dates in the future.

The list is guaranteed to be passed in, in descending order.

The list can contain dates in the future...all the way to anytime in the past. Or just dates in the future. Or just today. Or just dates in the past. Anything...

Here is my working version:

Public Shared Function GetClosetDateToToday(lstOrderedDates As List(Of Date)) As Date

    ' Select the closest Delivery date to TODAY (inclusive) from the list of Ordered dates
    If lstOrderedDates.Count > 1 Then
        Dim dateToCheck = lstOrderedDates.ElementAt(1)
        Dim selectedDate = lstOrderedDates.ElementAt(0)
        For i = 1 To lstOrderedDates.Count - 1

            If dateToCheck < Date.Now.Date Then
                Exit For
            End If

            If dateToCheck >= Date.Now.Date Then
                selectedDate = dateToCheck
            End If

            If i + 1 < lstOrderedDates.Count Then
                dateToCheck = lstOrderedDates.ElementAt(i + 1)
            End If
        Next i
        Return selectedDate
    ElseIf lstOrderedDates.Count = 1 Then
        Return lstOrderedDates.First()
    End If

    Return Nothing
End Function

Is there a better way?

Because this method is Public you should check if lstOrderedDates is Nothing and you should't believe that the passed List really is ordered.

Doing the check on lstOrderedDates.Count for 0 and 1 first will save one indentation for the meat of the code.

Placing Dim dateToCheck = lstOrderedDates.ElementAt(1) inside the loop will remove the need for the  third if inside the loop.

The second if isn't needed at all because of the first if. You can just set selectedDate = dateToCheck.

Would lead to:

Public Shared Function GetClosetDateToToday(lstOrderedDates As List(Of Date)) As Date

    ' Select the closest Delivery date to TODAY (inclusive) from the list of Ordered dates
    If lstOrderedDates is Nothing OrElse lstOrderedDates.Count = 0 Then
        Return Nothing
    ElseIf lstOrderedDates.Count = 1
        Return lstOrderedDates.ElementAt(0)
    End If
    Dim ordered as List(Of DateTime) = lstOrderedDates.OrderByDescending(function(d) d).ToList()
    Dim selectedDate = ordered.ElementAt(0)
    For i = 1 To ordered.Count - 1

        Dim dateToCheck = ordered.ElementAt(i)

        If dateToCheck < Date.Now.Date Then
            Exit For
        End If

        selectedDate = dateToCheck

    Next i
    Return selectedDate

End Function

In C# I would do it using some Linq magic like so

    public static DateTime GetClosetDateToToday(List<DateTime> ordered)
    {
        if (ordered == null || ordered.Count == 0) { return default(DateTime); }

        var result = ordered
                     .OrderByDescending(item => item)
                     .LastOrDefault(item => item >= DateTime.Now);

        if (result == default(DateTime))
        {
            return ordered[0];
        }
        return result;
    }

which should be equal to the VB.NET version but without guarantee

Public Shared Function GetClosetDateToToday(lstOrderedDates As List(Of Date)) As Date

    ' Select the closest Delivery date to TODAY (inclusive) from the list of Ordered dates
    If lstOrderedDates is Nothing OrElse lstOrderedDates.Count = 0 Then
        Return Nothing
    End If

    Dim found as DateTime = lstOrderedDates
                            .OrderByDescending(function(d) d)
                            .LastOrDefault(function(d) d >= DateTime.Now)
    If found is Nothing then
        found = lstOrderedDates.ElementAt(0)
    End If

    Return found

End Function


Create UIView programmatically in Swift I am generating View and setting constraints programmatically in UIViewController

import UIKit
import SnapKit

class LoginViewController: UIViewController {
    lazy var topImageView = UIImageView()
    lazy var centerStackView = UIStackView()
    lazy var phoneTextField = UITextField()
    lazy var sendOTPButton = UIButton()
    lazy var bottomStackView = UIStackView()
    lazy var separatorLine = UILabel()
    lazy var signUpButton = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()
        makeUI()
    }

    private func makeUI() {
        self.view.backgroundColor = .white
        self.view.addSubview(topImageView)
        topImageView.backgroundColor = UIColor.magenta.withAlphaComponent(0.4)
        topImageView.snp.makeConstraints {(make) -> Void in
            make.width.equalToSuperview()
            make.height.equalTo(225)
            make.topMargin.equalToSuperview()
            make.centerX.equalToSuperview()
        }

        centerStackView.translatesAutoresizingMaskIntoConstraints = false
        centerStackView.axis = .vertical
        centerStackView.distribution = .fillEqually
        self.view.addSubview(phoneTextField)
        self.view.addSubview(sendOTPButton)
        self.view.addSubview(centerStackView)
        centerStackView.addArrangedSubview(phoneTextField)
        centerStackView.addArrangedSubview(sendOTPButton)
        phoneTextField.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2)
        phoneTextField.delegate = self
        sendOTPButton.setTitle("Send OTP", for: .normal)
        sendOTPButton.addTarget(self, action: #selector(generateAccessToken), for: .touchUpInside)
        sendOTPButton.backgroundColor = .blue

        centerStackView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(100)
        }

        bottomStackView.translatesAutoresizingMaskIntoConstraints = false
        bottomStackView.axis = .vertical
        bottomStackView.distribution = .fillProportionally
        self.view.addSubview(separatorLine)
        self.view.addSubview(signUpButton)
        self.view.addSubview(bottomStackView)
        bottomStackView.addArrangedSubview(separatorLine)
        bottomStackView.addArrangedSubview(signUpButton)
        separatorLine.backgroundColor = .white
        signUpButton.backgroundColor = .orange
        bottomStackView.snp.makeConstraints { (make) in
            make.bottomMargin.equalTo(additionalSafeAreaInsets)
            make.width.equalToSuperview()
            make.height.equalTo(80)
        }
    }

The makeUI functions essentially creates the views, adds them to UIController's view (as a subview) and set constraints on them (AutoLayout).
But the ViewController become bulky if more UIViews are added.

My question is:

Should I move the UI code to another file (say LoginView.swift)? Or is it recommended to keep the UI code in UIViewController as they are tightly coupled?

You can move setting properties to lazy var declarations eg.

lazy var centerStackView: UIStackView = {
  let stackView = UIStackView()
  stackView.translatesAutoresizingMaskIntoConstraints = false
  stackView.axis = .vertical
  stackView.distribution = .fillEqually
  return stackView
}()

This will divide your code nicely. In my opinion, extracting views to separate files has a sense only if they have a more complex structure and you want to reuse them in other places. What is important is keeping functions short and on point. Also, try to use more empty lines to visually divide logic blocks, eg. adding subview and setting constraints to it from another subview.


String parsing with AVR-GCC I'm working on interfacing some older positioning equipment with an arduino, but having done the majority of my work with higher level language I'm having trouble adjusting to the limitations of AVR-GCC.

I basically have two questions: Is this a good way to parse out the strings? What can I do to optimize this code for an embedded platform?

// this is in the global scope of my program. normally accepted as bad code,
// but it seems to make sense in the context of an embedded platform.
// "global" cache
int data[10];

void getPosition(){
    // position message is always less than 25 bytes with null termination characters
    byte bufferIndex = 0;
    char buffer[25];

    // read the position command from the serial port
    // should look like:
    //      D20BIX+00733Y+00080S99\r\n
    //
    // and we need the X+00000 and Y+00000 parts
    //
    if (Serial.available() > 0){
        while (Serial.available() > 0 && bufferIndex < 25){
            buffer[bufferIndex] = Serial.read();

            if (buffer[bufferIndex++] == '\n'){
                buffer[bufferIndex] = '\0';
                break;
            }
        }

        Serial.flush();

        // check to see if we have good orientation on the buffer by
        // checking for lines starting with model identifier 'D'
        String input = String(buffer);

        if (buffer[0] == 'D' && bufferIndex <=24){
            int x_result = data[1];
            int y_result = data[4];
            String check;
            char token_buffer[8] = {'0', '0', '0', '0', '0', '0', '0', '0' };

            // scan for x, target token is X+00000
            String x_token = input.substring(5, 11);
            check = x_token.substring(2, 6);
            check.toCharArray(token_buffer, 8);

            x_result = atoi(token_buffer);
            if (x_token[1] == '-'){
                x_result *= -1;
            }

            // scan for y, target token is Y+00000
            String y_token = input.substring(12, 18);
            check = y_token.substring(2, 6);
            check.toCharArray(token_buffer, 8);

            y_result = atoi(token_buffer);
            if (y_token[1] == '-'){
                y_result *= -1;
            }

            // finalize results
            data[1] = x_result;
            data[4] = y_result;
        }
    }
}

IMHO string parsing is not a good idea for embedded applications. If you really care for speed, it would be better to use some binary format w/o dynamic length numbers etc.

I suggest you cannot change protocol due to legacy code/hardware. Then you could try turning your parser into finite automate. You just make a table (first index for current state, second index for a class of read char, value is new state and function to call) and then (state, f) = transition[state][class_of[*s++]]; f(); Files, generated with YACC for some simple grammars, could be helpful.

If you need just some minor modifications to your code, here you are.
First you read data into your buffer. All you have to do then is just some calculations with Horner scheme:

x_result = ((buffer[5]-'0')*10+buffer[6]-'0')*10+... ;
if (buffer[4] == '-') x_result =- x_result;

String constructor, substring method and other string operations are quite costly.


Implementing [ ] operator I have implemented the "[ ]" operator in a class that I have created to return values associated with a key (the same concept of a dictionary, except that it's possible to have the same key more than one time). However, looking at my code I don't think it is as efficient as it could be.

public class MyClass
{
    public object[][] Values;

    public MyClass()
    {
        Values = new object[4][];
    }

    public IEnumerable<object> this[string key]
    {
        get
        {
            var results = new List<object>();

            foreach (var value in this.Values)
            {
                if (value[0].ToString() == key)
                {
                     results.Add(value[1]);
                }
             }

            return results;
        }
    }
}

Values can contain a bunch of 2 values arrays.

A small example:

MyClass class = new MyClass();

class.Values[0] = new object[] { "key1", 25 };
class.Values[1] = new object[] { "key2", 3 };
class.Values[2] = new object[] { "key2", 789 };
class.Values[3] = new object[] { "key4", 12 };

and when I do class["key2"], it returns a collection containing 3 and 789.

What do you think of the way I implemented the this[string key] method? Do you think there is a way to do this without iterating on all the objects in the Values array?

If you look for performance, you'd better rely on the framework dictionary. You can use the Dictionary<string, List<object>> to do what you need. When you add a new key and value, look if the key already exists. If so, add the value to the array and don't add the key.


Bash script to manage multiple Java Development Kits installations I'm writing a bash script to manage multiple JDKs, the script is very simple, you have to choose a directory when you store all the jdk's, and the  script maintain a symlink to the current jdk'. TheJAVA_HOMEis always pointing to thesymlink`.

In order to use the script you have to source the jdk-manager.bash in .bash_profile or .profile

The script is used by calling jdk function from the command line.

Questions:

is there any security issues in the script ?
is there any way to implement the same functionality without sourcing the shell file in a dotfile ?

#! /usr/bin/env bash

#set -u
#set -e

#to modify
JDK_ROOT="$HOME/local" # change it to the directory that contain the jdk's

JDK_PATTERN="[0-9]\.[0-9]\.+([0-9])?(_+([0-9]))"
CURRENT_JDK="$JDK_ROOT/.current_jdk"
SESSION=0;

# colors
textred=$(tput setaf 1)
texttyellow=$(tput setaf 3)
textreset=$(tput sgr0)
textgreen=$(tput setaf 2)

########################################################################""
# functions
########################################################################"

# ascii art
_print_java_ascii() {
printf ${texttyellow}"
   _     _ _
  (_) __| | | __     _ __ ___   __ _ _ __   __ _  __ _  ___ _ __
  | |/ _  | |/ /____|  _   _ \ / _  |  _ \ / _  |/ _  |/ _ \  __|
  | | (_| |   <_____| | | | | | (_| | | | | (_| | (_| |  __/ |
 _/ |\__ _|_|\_\    |_| |_| |_|\__ _|_| |_|\__ _|\__  |\___|_|
|__/                                             |___/

"${textreset}
}

_is_jdk() {
  return `[[ "$1" == jdk${JDK_PATTERN} ]]`
}

# init the current symlink
_init_current() {
  if [[ ! -L $CURRENT_JDK ]];
  then
    ln -s /dev/null $CURRENT_JDK
  fi
}

_get_current_jdk() {
  basename `readlink -f $CURRENT_JDK`
}
# list all available jdks in the JDK_ROOT directory
_ls_jdks() {
  for file in $(ls $JDK_ROOT)
  do
    if `_is_jdk "$file"`
     then
      if [[ $file == `_get_current_jdk` ]]
       then
    printf "${textgreen} > $file\n"
      else
    printf "${textreset}   $file\n"
      fi
    fi
  done
}

_set_current_jdk() {
  jdk="jdk$1"
  if `_is_jdk $jdk` && [[ -d "$JDK_ROOT/$jdk" ]]
  then
    rm "$CURRENT_JDK"
    ln -sf "$JDK_ROOT/$jdk" "$CURRENT_JDK"
    printf "${textgreen} INFO: ${textreset}Updating the \$JAVA_HOME to $JDK_ROOT/${textgreen}`_get_current_jdk`${textreset}  .\n\n";
    _ls_jdks;
  else
    printf "${textred} ERROR: ${textreset} there is no $jdk in $JDK_ROOT \n"
  fi
}

########################################################################""
# main
########################################################################"

export JAVA_HOME=$CURRENT_JDK;
export PATH="$JAVA_HOME/bin:$PATH"

jdk() {
 if [[ $SESSION -eq 0 ]]
  then
  _print_java_ascii;
  SESSION=1;
fi

_init_current;

if [[ $# -eq 0 ]]
then
  _ls_jdks;
else
  _set_current_jdk $1
fi
}

Let's see:

There's no need to use return in the last statement of a function. By default, the return code of the last command is returned anyway.
Using backticks for evaluation is deprecated - you should use $(command args) instead. It is also more readable.
Use More Quotes™. The current script does not handle even whitespace in file names, much less newlines or - at the beginning.
Indent consistently, otherwise it's difficult to follow the code.
Are you sure you need to initialize the symlink?
Don't parse ls output.


HTML for Wikipedia infoboxes Wikipedia uses Infoboxes for many of its articles. They contain basic information in a structured form about the topic. Take for example the articles about Stack Overflow and donkeys.

Their structure is something like:

Title
Icon or Image
One or more sections

Each containing on or more key value pairs
or a text

Wikipedia uses for Infoboxes Tables.

My approach in HTML5 is:

Donkey

Conservation status

    Domesticated

Scientific classification

Kingdom

Animalia

Phylum

Chordata

Class

Mammalia

Order

Perissodactyla

Family

Equidae

Genus

Equus

Subgenus

Asinus

Species

E. africanus

ubspecies

E. a. asinus

Trinomial name

    quus africanus asinus
    Linnaeus, 1758

Is this HTML correct and useful? Are there better ways to write it?

This question is not only about Wikipedia but on how to write such constructs like infoboxes, that are used on other sites as well.

First of all use one h1 on the page (it's not stated anywhere but it's generally good practice), which means you cannot use in sidebar. I would start with h3 and do subheadings as h4 in your case.

Using aside (http://html5doctor.com/aside-revisited/):
When used in article tag it will be good structure, however when outside of it, it will mean that it's a sidebar(not connected to article itself).

You might as well use figure and fiugurecaption element with images and their caption (it's nice to provide image probably there).

One more thing. If your infobox is separate content why not use article instead of aside? Let's say you are writing text about peanuts. If you want to put that peanuts are produced in kongo  in infobox it qualify as separate content in my opinion.

EDIT:

About wikipedia. Wikipedia is using tables as the data inside is table alike. In your case too. dl doesn't really make sense in this case as it shows that is dictionary definition list, where it actually is table data. It will be better visible when you add for example english name next to latin you will see even more that data is table suitable


The stocks problem - find the biggest profit that can be made The Stocks Problem: Given an array of n integers representing the
  price of a stock over the course of n days, determine the maximum
  profit you can make if you can buy and sell exactly 1 stock over these
  n days.

My solution is using the same approach as the merge sort algorithm. It is implemented using Java. I am looking for harsh feedback including advice on function names, layout, formating etc..

public static void main (String[] args) {
    // A few basic tests
    System.out.println(stocks(new Integer[]{1, 2, 3, 8, 5}) + " - should be 7");
    System.out.println(stocks(new Integer[]{100, -1000, 3, 8, -1, 9, -2000}) + " - should be 1009");
    System.out.println(stocks(new Integer[]{-3000, -1000, 3, 8, -1, 9, 2000}) + " - should be 5000");
    System.out.println(stocks(new Integer[]{-3000, 1001, 5001, -2000, 2000,  5000}) + " - should be 8001");
    System.out.println(stocks(new Integer[]{-3000, 1001, 5000, -2000, 2000,  5001}) + " - should be 8001");
    System.out.println(stocks(new Integer[]{-3000, 1001, 4000, -2000, 2000,  5000}) + " - should be 8000");
}

public static int stocks(Integer[] stocks) {
    // Calculate max and minimum value
    Integer[] maxmin = splitAndMerge(stocks);

    // Biggest stock is difference between max and min value (considering time)
    return maxmin[1] - maxmin[0];
}

public static  Integer[] merge(Integer[] leftMinMax, Integer[] rightMinMax) {
    // Finding the biggest min max combination between the 4 values.
    if (rightMinMax[1] - Math.min(rightMinMax[0], leftMinMax[0])
            > Math.max(rightMinMax[1], leftMinMax[1]) - leftMinMax[0]) {
        return new Integer[] {Math.min(rightMinMax[0], leftMinMax[0]), rightMinMax[1]};
    } else {
        return new Integer[] {leftMinMax[0], Math.max(rightMinMax[1], leftMinMax[1])};
    }
}

public static Integer[] splitAndMerge(Integer[] arr) {
    // Split array into two parts
    int split = arr.length / 2;
    Integer[] arrLeft = Arrays.copyOfRange(arr, 0, split);
    Integer[] arrRight = Arrays.copyOfRange(arr, split, arr.length);

    // Compute left and right - then return merged solution
    return  merge(left(arrLeft), right(arrRight));
}

public static Integer[] left(Integer[] left) {
    if (left.length == 1) {
        return new Integer[]{left[0], left[0]};
    } else if (left.length == 2) {
        return new Integer[]{Math.min(left[0], left[1]), left[1]};
    } else {
        return splitAndMerge(left);
    }
}

public static Integer[] right(Integer[] right) {
    if (right.length == 1) {
        return new Integer[]{right[0], right[0]};
    } else if (right.length == 2) {
            return new Integer[]{right[0], Math.max(right[1], right[0])};
    } else {
        return splitAndMerge(right);
    }
}

Also it should be noted that Math.max() - Math.min() cannot be used since the the min value could have occurred before the max value, which doesn't make sense given the scenario of selling stocks.

This implementation is very inefficient due to creating a lot of arrays. Even worse is that the arrays are of type Integer[] instead of int[] which would be lighter.

A much simpler algorithm is possible:

iterate over the prices
keep track of the max difference seen so far, and the local minimum and maximum
if the current price is bigger than the local max

update the local max
if the difference from the local minimum is bigger than the max difference seen so far, then update the max difference = the best time to sell so far

if the current price is less than the local minimum then reset the local min and max
the maximum profit is the maximum difference at the end of the iteration

It's nice that you added some test cases, but it would be a lot better to turn those into proper junit test cases.

You missed some interesting corner cases:

decreasing sequence, for example: 5, 1
degenerate input: empty array or single element

Negative values as stock prices in the test cases seem strange and a bit confusing.

"stocks" is a poor name for a function that returns an integer. Plural names imply some sort of collection. The number returned is the maximum profit you can make. So for example calculateMaxProfits would be more appropriate.

Suggested implementation

Applying the suggestions above,
here's a solution that's \$O(n)\$ time and \$O(1)\$ space.

public int maxProfit(int[] prices) {
    if (prices.length < 1) {
        return 0;
    }

    int maxDiff = 0;
    int localMin = prices[0];
    int localMax = localMin;

    for (int price : prices) {
        if (price > localMax) {
            localMax = price;
            int localDiff = localMax - localMin;
            if (localDiff > maxDiff) {
                maxDiff = localDiff;
            }
        } else if (price < localMin) {
            localMin = localMax = price;
        }
    }

    return maxDiff;
}


Produce the sum of all white space separated integers in a given file How's my code?  Works perfectly for what I'm trying to do :)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<fstream>
#include<assert.h>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }

int main() {
    int sum{ 0 }, number;
    vector<int> numbers;
    string word;
    fstream file;
    file.open("file1.txt", fstream::in);

    while (true) {
        file >> number;
        if (file.eof()) {
            numbers.push_back(number);
            break;
        }
        else if (file.fail()) {
            file.clear();
            file >> word;
        }
        else if (file.bad()) exit(1);
        else numbers.push_back(number);
    }

    for (int x : numbers) sum += x;

    cout << sum << "\n";

}

Here's the file I'm reading from:
words 32 jd: 5 alkj 3 12 fjkl: 23 / 32
hey 332 2 jk 23 k 23 ksdl 3
32 kjd 3 klj 332 c 32

You collect all numbers but then use the collection only for the sum. Better just update the sum. And I find the whole trying int and if fails use word a bit complicated. How about just always reading words and then reading numbers from them? (The >> writes zero if there's no number.)
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
    int sum = 0;
    ifstream file("file1.txt");
    string word;
    while (file >> word) {
        int number;
        istringstream(word) >> number;
        sum += number;
    }
    cout << sum << endl;
}


How can a code that takes dict as input and validates it based on multiple conditions be further optimized? I have a piece of code that takes a dictionary as input and validates it based on the following rules:

The dictionary has a key named items and its value must be a list
Now, the list from point 1 must be a nested list with the length of each list should be 2
The first element ([0]) of every nested list must be of type int and the second element ([1]) must be float

The input to validate:
json_to_test = {
    "items": [
        [
            1577837700,
            63.2
        ],
        [
            1577838600,
            61
        ],
        [
            1577839500,
            58.9
        ],
        [
            1577840400,
            57
        ],
        [
            1577841300,
            55.3
        ]
    ]
}

My code
def get_element(lst, num):
    return [item[num] for item in lst]

json_input = json_to_test['items']
if any(isinstance(i, list) for i in json_input):
    if all(len(i) == 2 for i in json_input):
        if all(isinstance(j, int) for j in get_element(json_input, 0)) and all(isinstance(j, float) for j in get_element(json_input, 1)):
            print('Data ready to be saved to DB')
            return json_input
        else:
            abort(400, 'Check data type')
    else:
        abort(400, 'Nested list but not lenghth 2')
else:
    abort(400, 'Not list')

The abort in the else statements is being imported from flask: https://flask.palletsprojects.com/en/1.1.x/api/#flask.abort
Is there a way the above code of mine be more efficient or further improved?

Keyerror
Did you test this code with some invalid input?
The code will fail with a keyError if the items key does not exist, since your code just assumes it exists.
You could do a try-except with the json_to_test['items'] so that you can handle the keyError.
Unspecificness
if any(isinstance(i, list) for i in json_input)

You're testing that json_input contains at least one element that is a list, but that could be something elese than json_to_test['items'] which you clearly said has to be a list. Why aren't you testing that particular object?
Nested code
Your code is triple-nested which makes it hard to read and to maintain.
It is better if you check each error condition separately without nesting and return only if they all pass. That also makes it much easier to add or remove another validation check without re-indenting the whole code and figuring out where it goes.
Something like this:
if fail_condition_1:
    abort("error_message_1")

if fail_condition_2:
    abort("error_message_2")

if fail_condition_3:
    abort("error_message_3")

print('Data ready to be saved to DB')
return json_input
```


Optimizing SQLite SELECT Performance I need to read Databases which contain 44-50 Tables, with around 5 Million entries in Total (~ 100k entries per Table).

The Data consists of Positional Tracking Data in Sports (Players,Refs and the Ball) and Match-Events (Shots, Plays,Tackles,...):
Match-Events are negligible regarding performance.

Table: PlayerXYZ or Ball
-------------------------------------------
|id (int Primary Key)| x | y | z | timekey |
--------------------------------------------

Right now, it takes 86 seconds to read the Database and assign the content to a DataTable Dictionary.
That's a "Speed" of 57000 entries per second.

private async void ProcessLoadMatch()
{
    var window = Application.Current.MainWindow as MetroWindow;
    var controller = await window.ShowProgressAsync("Please wait...", "Process message", false, new MetroDialogSettings());

    controller.SetTitle("Loading Match-Data...");
    await Task.Run(() => HandleLoadMatch(controller));
    await controller.CloseAsync();

}

static bool HandleLoadMatch(ProgressDialogController ProgCtrl)
{
    string DataBasePath = @"W:\data\sqlite";
    string DataBaseName = "db";
    string dbpath = @DataBasePath + @"\" + @DataBaseName + ".sqlite";

    SQLiteConnection con = new SQLiteConnection("Data Source=" + dbpath + ";Version=3;");
    con.Open();

    DataTable tables = con.GetSchema("Tables");
    double currentTable = 0;
    double Percentage = 0;
    foreach (DataRow row in tables.Rows)
    {
        currentTable++;
        Percentage = (100 / tables.Rows.Count) * currentTable;
        string tablename = (string)row[2];
        ProgCtrl.SetMessage("Loading Data\nCurrent Table ("+currentTable+" of "+tables.Rows.Count+"): " + tablename + " ...");
        ProgCtrl.SetProgress(Percentage / 100);

        string CmdString = "SELECT * FROM " + tablename;
        SQLiteCommand cmd = new SQLiteCommand(CmdString, con);
        SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
        DataTable MatchDt = new DataTable();
        sda.Fill(MatchDt);

        CurrentDataSet.CurrentMatch.Data.Add(tablename, MatchDt);
    }
    con.Close();
    return true;

}

CurrentDataSet.CurrentMatch.Data:

class CurrentMatch
{
    public static Dictionary<string, DataTable> Data = new Dictionary<string, DataTable>();
}

My System: Mac Mini (Late 2012)
i5-3210m clone..
16gb ram,
256GB SSD

My Question: Is there any performance potential in my code left?
I load different databases on a regular basis, so any significant performance-gains would be appreciated.

If I open my Database with a Program like SQlite Browser, the tables are being read within a second..

Improving the performance depends on where exactly the bottleneck is - with a database it's typically either IO or CPU. A simple way to see if IO is the main problem is to load the entire database into memory and then selecting from it. However once you have loaded the database into memory you might just want to consider loading your data on demand when you need to display it rather than pulling it into a DataTable.

Once you have the database in-memory you could also try a Parallel.ForEach to load the data from all tables in parallel. Of course you can try this without loading the data into in-memory. I'm not sure how well this works with a physical file but it's worth a shot.

A few additional remarks:

Nitpick: It's a database and not a data base hence your variable names should read Database and not DataBase.
The general C# naming convention for method parameters and local variables is camelCase - Percentage and ProgCtl both violate this.
Don't abbreviate variable and parameter names needlessly, e.g. ProgCtl could be easily progressController - much more readable.
For more complex string formatting I'd prefer string.Format over concatenating manually:

ProgCtrl.SetMessage(string.Format("Loading Data\nCurrent Table ({0} of {1}): {2} ...",
                                   currentTable, tables.Rows.Count, tablename));

Makes the structure of the resulting string much clearer.
SQLiteConnection, SQLiteDataAdapter and SQLiteCommand are all  IDisposable and should be wrapped into using statements to make sure they get cleaned up properly.
DatabasePath and DatabaseName should be const.
Use Path.Combine to combine paths:

string dbpath = Path.Combine(DataBasePath, DataBaseName + ".sqlite");


Hacker rank - Left rotation - PHP code feedback Here is the problem description - https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem

  A left rotation operation on an array of size \$n\$ shifts each of the array's elements 1 unit to the left. For example, if 2 left rotations are performed on array \$[1,2,3,4,5]\$, then the array would become \$[3,4,5,1,2]\$.

  Given an array of \$n\$ integers and a number, \$d\$, perform \$d\$ left rotations on the array. Then print the updated array as a single line of space-separated integers.

I wanted to get some feedback on my solution for this problem, it's a task in the Hackerrank Cracking the Coding Interview series.
I am pretty sure the code is working fine, as it passes 7 of the 9 test cases, but is getting timed-out for the last two cases, since these cases are unknown, I wanted to get some expert feedback, as how can I optimize this code.

<?php
    //$handle = fopen ("input_leftR.txt","r");
    $handle = fopen ("php://stdin","r");
    fscanf($handle,"%d %d",$n,$k); //n and k values are available from here
    $a_temp = fgets($handle);
    $a = explode(" ",$a_temp);
    array_walk($a,'intval');

    //My Code from below
    $i; $j; $temp;
    if($n<0 || $n>1000000 || $k<0|| $k>1000000 || count($a) <=0)
        exit;
    for($i=0; $i<$k%$n; $i++){
    //for($i=0; $i<$k; $i++){
        for($j=0; $j<$n-1; $j++){
            $temp = $a[$j];
            $a[$j] = $a[$j+1];
            $a[$j+1] = $temp;
        }
    }
    //array_walk($a,'intval');
    //print implode(" ", $a);
    /*foreach($a as $key=>$value){
        printf("%d", $value);
        if($key<$n-1)
            printf(" ");
    }*/

    for($i=0;$i<$n;$i++){
    printf("%d", $a[$i]);

    if($i<$n-1)
        printf(" ");
    }
?>

Performance

Shifting the content of an array is a very expensive operation.
Lot's of assignments.

Realize that you don't really need to shift the content.
You can shift an offset index,
and use that shifted offset to print the desired output:

for ($i = 0; $i < $n; $i++) {
    $shifted = ($i + $k) % $n;
    printf("%d", $a[$shifted]);

    if ($i < $n-1) printf(" ");
}

Unnecessary statements

This is unnecessary, it serves no purpose at all, you can safely delete it:

$i; $j; $temp;

Input validation

Although input validation is a good practice,
in an online challenge you can trust that the input will be in the described format, and all values will be within the described ranges.
I haven't seen an exception to that yet.
So you don't need this kind of code:

if($n<0 || $n>1000000 || $k<0|| $k>1000000 || count($a) <=0)
    exit;

Formatting

The code is very poorly formatted:

Indentation is inconsistent
No spaces between operators make it look extremely compact and hard to read

Try to follow the writing style of the snippet in my first point.