Typescript design pattern for rendering different objects with similarities It's a web project. The code is working. The question is a design pattern question to write code that's more elegant, you know.
I've created a Codepen of this question.
I created a css grid WITHOUT using 'grid' but using float and clear.
I know such project works better written in React but it's just a try.
The grid I'm creating is entirely recreated when calling the render(world) method.
I'm not using any HTML outside of javascript (typescript).
I'm only using 4 CSS classes:
.block-first, .block-lasts {
  width: 20px;
  height: 20px;
  border-radius: 20px;
  float: left;
}

.block-first {
  clear: both;
}

.image-in-block {
  width: 20px;
  height: 20px;
}

.container {

}

In the grid are square blocks of equal size being 1- a uniformly colored block or 2- a block containing an image.
So I first fill up a matrix 20x20 of instances of classes that implements a IBlock interface containing a getProperty() method. Those classes are 1- ColorBlock or 2- ImageBlock. Below are these classes and the interface:
interface IBlock {
  getProperty() : string;
}
class ImageBlock implements IBlock{
  url: string;
  constructor(url: string) {
    this.url = url;
  }
  getProperty() : string {
    return this.url;
  }
}
class ColorBlock implements IBlock{
  color: string;
  constructor(hexColor: string) {
    this.color = hexColor;
  }
  getProperty() : string {
    return this.color;
  }
}

My problem is that I need to use different code to render each. For the ColorBlock I'm going with a colored div with background: '#ff00ff'; But for the ImageBlock I'm going with a nested img tag instead of a background-image: url('https://...');.
For distinguishing between the different classes, I'm using object_instance.constructor.name, I think that's the whole problem.
Now the rest of my code:
Matrix prepping:
const world = new Array(20);
for(let i=0; i<20; i++) {
  world[i] = new Array(20);
  world[i].fill(new ColorBlock('#ff00ff'));
}

The function rendering each block as ColorBlock or ImageBlock:
function html_process_image_or_color_block(dom, block_instance) {
  const html_block = dom;
  const block = block_instance;
  const block_property = block.getProperty();
  switch (block.constructor.name) {
    case 'ImageBlock':
      const image_tag = document.createElement('img');
      image_tag.src = block_property;
      image_tag.classList.add('image-in-block');
      html_block.appendChild(image_tag);
      break;
    case 'ColorBlock':
      html_block.setAttribute('style', `background: ${block_property};`)
      break;
    default: break;
  }
  // return html_block;
}

And finally the render(world) function that uses the 20x20 Matrix and the above function:
function render(world) {
  const html_container = document.createElement('div');
  html_container.classList.add('container');
  for(let i=0; i<20; i++) {

    //code for rendering first left most block
    const html_first_block = document.createElement('div');
    html_first_block.classList.add('block-first');
    const first_block = world[i][0];
    html_process_image_or_color_block(html_first_block, first_block);
    html_container.appendChild(html_first_block);

    //code for rendering all other blocks on its right
    for(let j=1; j<20; j++) {
      const html_block = document.createElement('div');
      html_block.classList.add('block-lasts');
      const block = world[i][j];
      html_process_image_or_color_block(html_block, block);
      html_container.appendChild(html_block);
    }
  }
  document.body.innerHTML = '';
  document.body.appendChild(html_container);
}

render(world);

In my opinion the code is terrible :) and I appreciate all your time and effort!
Codepen

So I know there gotta be a better way but I found a satisfying one:
interface IBlock {
  getProperty() : string;
  render() : Object;
}

class ColorBlock {
  color: string;
  constructor(color: string) {
    this.color = color;
  }
  getProperty() : string {
    return this.color;
  }
  render(isFirstBlock: boolean) : Object/*Well it's an HTML dom object*/ {
    const tag = document.createElement('div');
    isFirstBlock? tag.classList.add('block-first') : tag.classList.add('block-lasts');
    tag.setAttribute('style', `background: ${this.color};`);
    return tag;
  }
}

class ImageBlock {
  url: string;
  constructor(url: string) {
    this.url = url;
  }
  getProperty() : string {
    return this.url;
  }
  render(isFirstBlock: boolean) : Object/*Well it's an HTML dom object*/ {
    const tag = document.createElement('div');
    isFirstBlock? tag.classList.add('block-first') : tag.classList.add('block-lasts');
    const image_tag = document.createElement('img');
    image_tag.src = this.url;
    image_tag.classList.add('image-block');
    tag.appendChild(image_tag);
    return tag;
  }
}

const global_x = 10;
const global_y = 10;

const world = new Array<Array<IBlock>>(global_x);

const default_ColorBlock = new ColorBlock('#ff00ff');

for(let i=0; i<global_x; i++) {
  world[i] = new Array<IBlock>(global_y);
  world[i].fill(default_ColorBlock);
}

function render(world: Array<Array<IBlock>>) {
  const html_container = document.createElement('div');
  for(let i=0; i<global_x; i++) {
    html_container.appendChild(world[i][0].render(true));
    for(let j=0; j<global_y; j++) {
      html_container.appendChild(world[i][j].render(false));
    }
  }
  document.body.innerHTML = '';
  document.body.appendChild(html_container);
}

render(world);

CSS being:
:root {
  --global-width: 20px;
  --global-height: 20px;
}

.block-first, .block-lasts {
  width: var(--global-width);
  height: var(--global-height);
  border-radius: 50%;
  float: left;
}

.block-first {
  clear: both;
}

.image-block {
  width: var(--global-width);
  height: var(--global-height);
}

And no HTML outside of javascript (typescript) .


Getting a table from an Excel worksheet I need to get a table from an Excel worksheet.  Sometimes I know the worksheet that contains the table, other times I might know it's going to be in one of a handful of worksheets, and still other times I might not even know what workbook it's in.

To me, this seemed like a good place to use an overloads function. What I did was create the following overloaded function which recursively calls itself to dig down into each "level" of the input object's hierarchy so that it allows me to look for the table using a variety of input objects.

I'd like to get some feedback on this solution; is it acceptable practice? Is this a good use of overloading?  Is there a better (more practical, more efficient, etc.) way to do it?

Note 1: I've omitted class members not related to this question.

Note 2:  I haven't gotten deep enough into debugging to add any specific error handling to the try/catch structure, but I do plan to add more specific "catches" eventually.

Imports Microsoft.Office
Imports Microsoft.Office.Interop.Excel
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class MyExcel
    Public Overloads Function GetTable(ByVal SearchName As String, ByVal SearchSheets() As Worksheet, Optional ByVal ExactMatch As Boolean = False) As ListObject
        If Not ExactMatch Then SearchName = "*" & SearchName & "*"
        Try
            For Each ws As Worksheet In SearchSheets
                For Each lo As ListObject In ws.ListObjects
                    If lo.Name Like SearchName Then Return lo
                Next
            Next
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return Nothing
    End Function
    Public Overloads Function GetTable(ByVal SearchName As String, ByVal SearchBooks() As Workbook, Optional ByVal ExactMatch As Boolean = False) As ListObject
        Dim loFound As ListObject
        Dim wsList As New List(Of Worksheet)
        Try
            For Each wb In SearchBooks
                For Each ws In wb.Worksheets
                    wsList.Add(ws)
                Next
                loFound = Me.GetTable(SearchName:=SearchName, SearchSheets:=wsList.ToArray, ExactMatch:=ExactMatch)
                If Not loFound Is Nothing Then Return loFound
            Next
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return Nothing
    End Function
    Public Overloads Function GetTable(ByVal SearchName As String, ByVal SearchApps() As Microsoft.Office.Interop.Excel.Application, Optional ByVal ExactMatch As Boolean = False) As ListObject
        Dim loFound As ListObject
        Dim wbList As New List(Of Workbook)
        Try
            For Each app In SearchApps
                For Each wb In app.Workbooks
                    wbList.Add(wb)
                Next
                loFound = Me.GetTable(SearchName:=SearchName, SearchBooks:=wbList.ToArray, ExactMatch:=ExactMatch)
                If Not loFound Is Nothing Then Return loFound
            Next
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return Nothing
    End Function
End Class

Basically, this isn't recursion but just overloading methods being called from each other, which is a valid approach targeting the problem.

But nevertheless, you could do better.

Naming

Based on the naming guidelines method parameters should be nameed using camelCase casing.
If you have own naming guidelines you for sure should stick to them.

Abbreviations for naming variables like loFound doesn't tell anything at all. You should name your variable as meaningful as possible. In that way Sam the maintainer won't have a hard time evaluating the purpose of the variable. That being said, I would have expected a name like foundTable inside a method named GetTable().
Scope

You should declare your variables as near as possible to their usage. It does not help readability if you declare them at the top of the method if you use them way down the method.
Using the right type

It doesn't make sense to use a List<T> to collect items and then calling ToArray() because the method to be called takes an array as parameter, at least if you can change it.

I would like to suggest changing the parameter to IEnumerable<T> because you are only iterating over the array which can be done in the same way with an IEnumerable<T> and much better, if you want to pass an array, a List<T> or a ICollection<T> you can still do it.

You should always try to code against interfaces and not against a concrete implementation.
Spacing

By adding vertical space to logicaly related code your code will improve readability.

So let us take a look at this

Public Overloads Function GetTable(ByVal SearchName As String, ByVal SearchBooks() As Workbook, Optional ByVal ExactMatch As Boolean = False) As ListObject
   Dim loFound As ListObject
   Dim wsList As New List(Of Worksheet)
   Try
       For Each wb In SearchBooks
           For Each ws In wb.Worksheets
               wsList.Add(ws)
           Next
           loFound = Me.GetTable(SearchName:=SearchName, SearchSheets:=wsList.ToArray, ExactMatch:=ExactMatch)
           If Not loFound Is Nothing Then Return loFound
       Next
   Catch ex As Exception
       MessageBox.Show(ex.ToString)
   End Try
   Return Nothing
End Function

The List<T> class has a method AddRange() to add a range of objects. This would improve the method regarding performance.

But you would gain lot more performance if you would declare the List<T> just inside the outer loop. Why do you add all of the WorkSheets but never clear the List ? You will iterate over the whole list each time the searched item isn't found.

So let us do this right. Because the Sheets interface implements IEnumerable we can use the overloaded constructor of List<T> and pass the IEnumerable like so

Public Overloads Function GetTable(ByVal searchName As String, ByVal searchBooks As IEnumerable(Of Workbook), Optional ByVal exactMatch As Boolean = False) As ListObject()

    Try
        For Each wb In searchBooks
            Dim sheets As New List(Of Worksheet)(wb.Sheets.OfType(Of Worksheet))

            Dim foundTable As ListObject = Me.GetTable(searchName:=searchName, searchSheets:=sheets, exactMatch:=exactMatch)
            If Not foundTable Is Nothing Then Return foundTable

        Next
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
    Return Nothing
End Function


Loading message with Pthreads I want to print a nice loading message with these three fading dots, while the main thread does some heavy IO stuff. This is why I implemented this:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdbool.h>

static bool            loadingStop;
static pthread_mutex_t loadingMutex;

void *loadingDots(void *message);

int main() {
  loadingStop = false;

  // MUTEX
  pthread_mutex_init(&loadingMutex, NULL);

  // ATTRIBUTE
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

  // THREAD
  pthread_t loadingThread;
  pthread_create(&loadingThread, &attr, loadingDots, (void *)"Loading (main, does something long)");

  pthread_attr_destroy(&attr);

  // SOMETHING TIME CONSUMING
  usleep(8000000);

  // PLEASE 'LoadingDots' TO STOP
  pthread_mutex_lock(&loadingMutex);
  loadingStop = true;
  pthread_mutex_unlock(&loadingMutex);

  // WAIT FOR IT TO FINISH
  pthread_join(loadingThread, NULL);
  pthread_mutex_destroy(&loadingMutex);

  printf("This is printed after 'LoadingDots' savely quit.\n");

  return 0;
}

void *loadingDots(void *message)
{
  int c = 0;

  while (1) {
    switch (c) {
    case 0:
      printf("\r%s   \r%s", (char *)message, (char*)message);
      break;
    default:
      putchar('.');
      break;
    }

    fflush(stdout);
    usleep(1000000);

    c = (c+1) % 4;

    pthread_mutex_lock(&loadingMutex);
    if (loadingStop) { break; };
    pthread_mutex_unlock(&loadingMutex);
  }

  putchar('\n');
  pthread_exit(NULL);
}

Mostly minor stuff

Good use of fflush(stdout); to insure output.  Something often missed.
Minor: int main() { is not explicitly allowed per the standard.  Better to use int main(void) {
Minor: Naked physical quantity.  Values like height, weight, time, should have units made clear  feet vs meter $327.6 million mistake

// usleep(8000000);
#define SOME_us_TIME_CONSUMPTION (8000000 /* us */)
usleep(SOME_us_TIME_CONSUMPTION);

Minor: Avoid printf() when fputs() will do.  Avoid % issue.. BTW did you mean "safely" vs "savely"?

// printf("This is printed after 'LoadingDots' savely quit.\n");
fputs("This is printed after 'LoadingDots' savely quit.\n", stdout);
// or
puts("This is printed after 'LoadingDots' savely quit.");

Long lines: Respect the width for the target audience - in this case code review.

// pthread_create(&loadingThread, &attr, loadingDots, (void *)"Loading (main, does something long)");

vs.

pthread_create(&loadingThread, &attr, loadingDots,
    (void *)"Loading (main, does something long)");

Consider auto format.  The space between bool            loadingStop will likely shrink with auto formatting.  Insure code is presentable even after auto formatting.

// looks OK
static bool loadingStop;
static pthread_mutex_t loadingMutex;

Minor: Not a fan of {} one liners

// if (loadingStop) { break; };
if (loadingStop) {
  break;
};


I made basketball program using Java that runs on console I made a simple console basketball game using Java.
/*
 * Java Basketball
 * by Clint
 * 2020.07.08 | 3:59AM
 * This Java program let's the player play basketball in console.
 */

import java.util.Random;
import java.util.Scanner;

public class Basketball {

    static String[] shoot = {"1 POINT!", "2 POINTS!", "3 POINTS!", "MISSED!"};
    static String player;

    public static void main(String[] args) {
        System.out.println("WELCOME TO JAVA CONSOLE BASKETBALL\n" +
                "how to play:\n" +
                "press \"s\" or type \"shoot\" to shoot the basketball.\n");

        while (true) {
            shootBall();
        }
    }

    public static void shootBall() {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);

        int shot = random.nextInt(shoot.length);
        System.out.print("SHOOT: ");
        player = scanner.nextLine();

        switch (player) {
            case "s", "S", "shoot", "Shoot", "SHOOT" -> System.out.println(shoot[shot] + "\n");
            default -> System.out.println("SHOOT THE BALL PROPERLY.\n");
        }
    }
}

ps: ik that there's while (true) in my code, but it's the thing that I know so far that keeps the programme running. I'm open for suggestions on how to improve my coding.

Descriptive variable names
You have this:
player = scanner.nextLine();

Does the input reflect a player?
I think a more descriptive name would be playerInput. That makes the switch more readable as well.
Make methods do one thing (single responsibility principle)
Your shootBall method does three things: asking for input, calculating the result, and printing the result. Ideally this would be split up:

private String getInput()
private String shootBall(String input)
private String printResult(String result)

This makes it easier to change the implementation of a responsibility and easier to read the logic of your program.
The main while loop would become
while(true) {
    String input = getInput();
    String result = shootBall(input);
    printResult(result);
}

This makes it easier for coding some exit value as well;
while (true) {
    String input = getInput();
    if (input.equals("QUIT"))
      break;
    String result = shootBall(input);
    printResult(result);
}

Or, if you don't like the while-true-break
String input;
while (!(input = getInput()).equals("QUIT")) {
    printResult(shootBall(input));
}


Automatic Property Upgrading I am creating an application just for practicing c# as well as new programming ways. So i came across the idea that i create an application that stores information about "soldiers" ex. Names, ranks, titles, etc... but the trick here is that i want the soldier title to be automatically upgraded when it's rank increases.

I have created a small application to demonstrate the idea.

Soldier Class

public class Soldier
{
    public string Name { get; private set; }
    public int Rank { get; private set; }
    public string Title { get; private set; }

    public Soldier(string name)
    {
        Name = name;
        RefreshTitle();
    }

    public void Rankup(int rank = -1)
    {
        if (-1 != rank)
            Rank = rank;
        else
            Rank++;

        RefreshTitle();
    }

    public void RefreshTitle()
    {
        Title = SoldierRankLibrary.GetSoldierTitle(Rank);
    }
}

Soldier Rank Library

public class SoldierRankLibrary
{
    private static readonly Dictionary<int, string> RankDictionary = new Dictionary<int, string>
                                                          {
                                                              {0, "Novice"},
                                                              {1, "Second Lieutenant"},
                                                              {2, "First Lieutenant"},
                                                              {3, "Captain"},
                                                              {4, "Major"},
                                                              {5, "Lieutenant Colonel"},
                                                              {6, "Colonel"}
                                                              //etc...
                                                          };

    public static string GetSoldierTitle(int requiredRank)
    {
        return RankDictionary.FirstOrDefault(rank => rank.Key == requiredRank).Value;
    }
}

Using the code

static void Main()
{
    //Create new soldier...
    var soldier = new Soldier("Daniel");

    //Print soldier info...
    PrintSoldierInfo(soldier);

    //Upgrade soldier...
    soldier.Rankup();

    //Print soldier info...
    PrintSoldierInfo(soldier);

    Console.Read();
}

static void PrintSoldierInfo(Soldier soldier)
{
    Console.WriteLine("****************************************");
    Console.WriteLine("Name: {0}", soldier.Name);
    Console.Write("Rank: ");
    switch (soldier.Rank)
    {
        case 0:
            Console.WriteLine("novice");
            break;
        case 1:
            Console.WriteLine("*");
            break;
        case 2:
            Console.WriteLine("**");
            break;
        case 3:
            Console.WriteLine("***");
            break;
        case 4:
            Console.WriteLine("****");
            break;
        case 5:
            Console.WriteLine("*****");
            break;
        case 6:
            Console.WriteLine("******");
            break;
        case 7:
            Console.WriteLine("*******");
            break;
    }
    Console.WriteLine("Title: {0}", soldier.Title);
    Console.WriteLine("****************************************");
    Console.WriteLine();
}

Conclusion

I am really waiting for your answers and i hope you give me detailed information about what i miss, what i can do better, how could i improve this code.

Get rid of SoldierRankLibrary

GetSoldierRank() belongs in the Soldier class. That's what Rank { get; set; } is for.
the Dictionary will be replaced by an enum (see below). An enum does not need be inside of any class. You can use boolean operators on enums ("<", "==", ...)

Use Enum instead of Dictionary

Your dictionary is mimicking an Enum. In other words and Enum
inherently associates a number with a name. And in general using Enum
is preferred over strings.
Make separate OfficerRank and EnlistedRank enums if that works
better.
I like @Nik's answer with "unknown". But I'd make it "0" instead of
"-1". The natural default value of an enum variable is zero.
If the underlying int values matter elsewhere in code just make sure you put the ranks in the desired order.

Soldier Class

Rank and Title appear to be the same thing. Do you really need to refer to the soldier's rank by its underlying (integer) value?
Explicitly set variables in the constructor. We all know default values for .NET types, but you want to convey intent and meaning.
Exhaustively set the entire state in the constructor. Again, intent and meaning.
Rankup() - change name to Promote(). Don't pass in an integer, pass in something that has meaning. We're going to utilize Rank enum so this fills the bill.
RefreshTitle() - obsolete. Rank/Title is the same thing. The Rank enum keeps us from having to deal with a string and an int.
Property mis-use. It makes no sense to have a private Title set and
a public RefreshTitle(). Think of a Property as a method. A lot of existing code goes away simply by using the public get properties.
Override ToString(). Now PrintSoldierInfo() disappears.

New Code

public enum Rank {undefined, BuckPrivate, Sergeant, Lieutenant, Captain}

public class Soldier {
    public string   Name { get; protected set; }
    public Rank     Rank { get; protected set; }

    public Soldier (string name) {
        Name = name ?? "No Name";       // null coalescing.
        Rank = Rank.undefined;   // maybe private if enlisted or lieutenant if officer
    }

    public void Promote (Rank newRank) {
        if ((int)this.Rank < (int)newRank ) this.Rank = newRank;
        // we don't allow Demotion in this method.
    }

   public override string ToString() {
       Stringbuilder me = new Stringbuilder();  // need to import System.Text to use Stringbuilder

// C# string formatting avoids clumsy string concatination; clearer and less error prone
       me.AppendLine("Name: {0}  Rank: {1}", Name,Rank);

       return me.ToString();  // Stringbuilder must be converted to a string.
   }
}

Using Soldier

Soldier grunt = new Soldier("Sad Sack");
grunt.Promote(Rank.Sergeant);
Console.WriteLine (grunt); // ToString() is automagically called.


A toy example of the concurrent stack via atomic variables and CAS pattern I've written a toy example of the concurrent stack which has only three functions push(), peek(), and length(). I've used atomic variables for synchronization. Is there anything incorrect in terms of synchronization? Could you please review this code?
#include <iostream>
#include <thread>
#include <atomic>
#include <vector>

template <typename T>
struct Node {
    Node<T>* next{};
    T val{};
    Node(const T& v) : val(v) {};
};

template <typename T>
class Stack {
    std::atomic<Node<T>*> top{};
    std::atomic<size_t> size{};
    public:
    void push(const T& data) {
        Node<T>* node = new Node<T>(data);
        size_t length = size.load(std::memory_order_relaxed);
        node->next = top.load(std::memory_order_relaxed);
        while(!std::atomic_compare_exchange_weak_explicit(&top, &node->next, node, std::memory_order_release, std::memory_order_relaxed));
        while(!std::atomic_compare_exchange_weak_explicit(&size, &length, size + 1, std::memory_order_release, std::memory_order_relaxed));
    }
    Node<T>* peek() {
        return top.load();
    }
    size_t length() const {
        return size;
    }
};

template <typename T>
class Runner {
    int cnt{};
    Stack<T>* ss{};
    public:
        Runner(const int n, Stack<T>* s) : cnt(n), ss(s) {}
        void operator()() {
            for(int i = 0; i < cnt; ++i) {
                size_t size = ss->length();
                if(size >= cnt) {
                    return;
                }
                ss->push(i);
            }
        }
        Stack<T>* get_stack() const {
            return ss;
        }
};

int main() {
    std::vector<std::thread> threads{};
    Stack<int>* s = new Stack<int>();
    int n = 10;
    Runner<int> r(n, s);
    for(int i = 0; i < 3; ++i) {
        threads.push_back(std::move(std::thread(r)));
    }
    for(int i = 0; i < 3; ++i) {
        threads[i].join();
    }
    Stack<int>* top = r.get_stack();
    Node<int>* head = top->peek();
    while(head) {
        std::cout << head->val << ", ";
        head = head->next;
    }
    size_t size = top->length();
    return 0;
}

Naming
My immediate reaction is that without a pop (or something equivalent) it's not really a stack. Especially given the item below about exposing the implementation details, from a client's viewpoint it's really a linked list that supports only a couple of operations: adding nodes to the beginning, getting the length, and (by knowing the implementation) traversing the list.
Exposing Internal Details
As it stands now, it seems to rely on client code to realize that it's implemented as a linked-list, so access to elements other than the top require client code to use peek to get a pointer to the top element, and then traverse the nodes of the linked list to get to the rest of the elements.
Avoiding Race Conditions
I'd also question (to at least some degree) inclusion of length in the interface. In a multi-threaded environment, almost all possible uses of length are practically guaranteed to introduce race conditions--by the time you actually try to use a length you've read, the length may have changed, so you're using obsolete information.
It can (at least sort of) make some sense to use a length for things like logging, simply to give the user at least some notion of whether the stack is (for example) growing constantly (but in this case, that's pretty much a given, since you've omitted a pop from its interface).
Side Note
It's not immediately apparent whether you're thinking of Runner as part of the Stack itself, or an ancillary class you included to demonstrate/test the actual Stack. At least for now, I've treated it as ancillary, so it shows how the Stack is used, but I haven't really tried to review it in itself.


Parsing the lsblk output I am a Python beginner learning Python 3. I have written two small functions that parse the lsblk output and return Linux physical and logical disks. Here is the first function:

from subprocess import run, PIPE

def physical_drives():
    """
    Gets all physical drive names.

    Gets all physical drive names on a Linux system,
    parsing the lsblk utility output.

    Parameters
    ----------

    Returns
    -------
    list
        A list of strings representing drive names.

    """

    command = ['lsblk -d -o name -n']
    output = run(command, shell=True, stdout=PIPE)

    output_string = output.stdout.decode('utf-8')
    output_string = output_string.strip()

    results = output_string.split('\n')
    return results

def main():
    print(physical_drives())

if __name__ == '__main__':
    main()

The second function:

from subprocess import run, PIPE

def partitions(disk):
    """
    Gets all partitions for a given physical disk.

    Gets all partitions present on a physical disk
    on a Linux system.
    The function parses the lsblk utility output.

    Parameters
    ----------
    disk : string
        A string containing a disk name such as 'sda'

    Returns
    -------
    list
        A list of strings representing partitions.

    """

    command = ['lsblk -o name -n -s -l']
    output = run(command, shell=True, stdout=PIPE)

    output_string = output.stdout.decode('utf-8')
    output_string = output_string.strip()

    results = list()
    results.extend(output_string.split('\n'))
    results = [x for x in results if x != disk and disk in x]

    return results

def main():

    from disks import physical_drives

    for drive in physical_drives():

        print(drive)
        parts = partitions(drive)

        for partition in parts:
            print('\t' + partition)

if __name__ == '__main__':
    main()

The functions are in two different files in the same directory. I would appreciate a quick review on anything that is not idiomatic/pythonic code. I also have one specific question. I have coded the functions in Ninja-Ide with lint and PEP8 suggestions turned on. In both files the IDE suggests that my print() statements:

print('\t' + partition)
print(physical_drives())

should be written with doubled parentheses:

print(('\t' + partition))
print((physical_drives()))

For Python 3 support, I have checked the print() documentation, but have been unable to find a reference for including function calls and string concatenation in double parentheses when calling print().

lsblk

The -s option to lsblk was introduced to util-linux rather recently, in release 2.22.  You may experience compatibility issues on slightly older GNU/Linux installations.

But I don't see why you would want the -s option at all — it just gives you an inverted device tree.  For example, on my machine:

$ lsblk -o name -n -s -l
sda1
sda
sda2
sda
sr0
vg-root
sda3
sda
vg-var
sda3
sda
vg-data
sda3
sda

In the output, sda appears multiple times.  To understand the output, you need to drop the -l flag so that the list appears in tree form:

$ lsblk -o name -n -s
sda1
└─sda
sda2
└─sda
sr0
vg-root
└─sda3
  └─sda
vg-var
└─sda3
  └─sda
vg-data
└─sda3
  └─sda

Now, it's more apparent that the -s option isn't helpful.  If you drop it, then the output makes more sense:

$ lsblk -o name -n
sda
├─sda1
├─sda2
└─sda3
  ├─vg-root
  ├─vg-var
  └─vg-data
sr0
$ lsblk -o name -n -l
sda
sda1
sda2
sda3
vg-root
vg-var
vg-data
sr0

To list the devices on sda, it would be better to run lsblk -o name -n -l /dev/sda — that would immediately drop sr0 from consideration, for example.  Note that LVM volumes (such as vg-root above) would still appear in the output.  I don't think that doing a substring search (if x != disk and disk in x in your code) is a reliable filter.  It could be fooled if there are more than 26 physical disks: the 27th disk would be named sdaa.  It might also be fooled by exceptionally tricky naming of LVM volumes.

Subprocess execution

Whenever practical, I recommend avoiding the shell when executing subprocesses.  The shell introduces a set of potential security vulnerabilities — for example, shenanigans with the PATH environment variable.  Best practice would be to run the command with a specific executable and pre-parsed command-line options:

run('/bin/lsblk -o name -n -s -l'.split(), stdout=PIPE)

Alternative solution

I actually wouldn't bother with parsing the output of lsblk at all.  After all, lsblk is just a way to report the contents of the sysfs filesystem.  You would be better off inspecting /sys directly.

from glob import glob
from os.path import basename, dirname

def physical_drives():
    drive_glob = '/sys/block/*/device'
    return [basename(dirname(d)) for d in glob(drive_glob)]

def partitions(disk):
    if disk.startswith('.') or '/' in disk:
        raise ValueError('Invalid disk name {0}'.format(disk))
    partition_glob = '/sys/block/{0}/*/start'.format(disk)
    return [basename(dirname(p)) for p in glob(partition_glob)]


Tabulate text with JavaScript Pretty printer in ABAP does not take care of all my aligning need, so I wrote a small JS script that tabulates/aligns with a custom string. It contains HTML, CSS and script in 1 file because this is a small tool.

Link : https://github.com/konijn/aligner

<!DOCTYPE html>

        <meta charset="utf-8">
        <title>Aligner</title>

Aligner

      Editor:

      <textarea id="editor">
  DATA : wa_ekpo TYPE ekpo.
  DATA : wa_eket TYPE eket.
  DATA : wa_decision TYPE zrms_performance_decision.
  DATA : wa_receipt_item TYPE zrmt_rcpt_items.
  DATA : wa_container TYPE zrmt_rcpt_hdrs_c.
      </textarea>

      Align at
      <input type="text" id="token" class="emo" value="TYPE">
      <button type="button" id= "button">Align</button>

      Output:

      <textarea id="output"></textarea>

Please review for style and maintainability.

I'm just commenting on your HTML/CSS.

I commented out some br tags, because they were not necessary
I splitted the HTML in three parts with div's
Slightly adjusted the CSS rules

<!DOCTYPE html>

        <meta charset="utf-8">
        <title>Aligner</title>

Aligner

        Editor:
        <textarea id="editor">
  DATA : wa_ekpo TYPE ekpo.
  DATA : wa_eket TYPE eket.
  DATA : wa_decision TYPE zrms_performance_decision.
  DATA : wa_receipt_item TYPE zrmt_rcpt_items.
  DATA : wa_container TYPE zrmt_rcpt_hdrs_c.
        </textarea>

        Align at
        <input type="text" id="token" class="emo" value="TYPE">
        <button type="button" id= "button">Align</button>

        Output:
        <textarea id="output"></textarea>


Writing an SQL SELECT statement I'd like to Select columns from Table 1 with a Row Filter, Select columns from Table 2 and join.

This works but it's a bit verbose. I was wondering if there is a cleaner way to write this.

SELECT * FROM
  ((SELECT JOB_NUM, ITEM_NUMBER, DATE_COMPLETED, QTY_COMPLETED, STD_USAGE, ACTUAL_USAGE FROM JOB_ROUTERS WHERE RESOURCE_CODE == "TDWELD") AS JOBS
 LEFT JOIN
(SELECT ITEM, DESCRIPTION FROM ITEM_ATTRIBUTE) as item
  ON JOBS.ITEM_NUMBER == ITEM.ITEM)

There is no need to do the deep abstraction for the Select *. You can simply reference the columns you want and include them in the result...

SELECT JOB_NUM,
       ITEM_NUMBER,
       DATE_COMPLETED,
       QTY_COMPLETED,
       STD_USAGE,
       ACTUAL_USAGE,
       ITEM,
       DESCRIPTION
FROM JOB_ROUTERS
LEFT JOIN ITEM_ATTRIBUTE
       ON JOBS.ITEM_NUMBER == ITEM_ATTRIBUTE.ITEM
WHERE RESOURCE_CODE == "TDWELD"


How to correctly get a IDisposeable when you need to lock the factory? If I need to create a IDisposeable object from a factory, but if the factory object is not thread safe and requires me to lock on it is this the correct pattern to use?

public void DisposeExample(FactoryClass factoryClass)
{
    DispObject dispObject = null;

    lock(factoryClass)
    {
        dispObject = factoryClass.GetDispObject();
    }

    using(dispObject)
    {
        dispObject.DoWork();
    }
}

Yes, assuming the factory is shared between threads and the created object doesn't contain any resource that is shared with other objects from the same factory.

But as was already pointed out in your previous question, a better solution might be to have a separate factory for each thread. Factories usually don't use much resources, so it should be fine to have more of them.


Print total number of vowel and consonant in an entered sentence After asking my first question got a nice review on that. So, this is my solution for a different question checking edge cases and incorporating ideas given in that review.

  Question: Write a program to count total number of vowel and consonant characters present in an user entered sentence?

  Expected Output: If user inputs,

  1CHELSEA. 1FOOTBALL. 1CLUB. -> Vowel: 7, Consonant: 12
  a1?3e5i6!@o7^&*8)u -> Vowel: 5, Consonant: 0
  q1?32536!@47^&*8)p -> Vowel: 0, Consonant: 2

My solution works for all expected cases and from previous review suggestion I tried docstring, doctest, etc. So from this review I would like to know whether I am using those correctly or not, and if there's easier and better way to do this.

My solution:

from sys import exit

def counter(sentence):
    """Removes whitespaces, punctuation and capitalize every words.

    >>> counter("1CHELSEA. 1FOOTBALL. 1CLUB.")
    Vowel: 7, Consonant: 12

    >>> counter("a1?3e5i6!@o7^&*8)u")
    Vowel: 5, Consonant: 0

    >>> counter("q1?32536!@47^&*8)p")
    Vowel: 0, Consonant: 2

    """
    count_sentence = "".join(ch for ch in sentence if ch.isalpha()).upper()  # removes punctuation and numbers

    def find_total(count):
        """Check if character is present in vowel_list if present ++ in total"""
        vowel_list = ["A", "E", "I", "O", "U"]
        total = 0
        for character in count:
            if character in vowel_list:
                total += 1
        return total

    find_total(count_sentence)
    vowel_total = find_total(count_sentence)
    consonant_total = len(count_sentence) - vowel_total
    print("Vowel: {0}, Consonant: {1}".format(vowel_total, consonant_total))
    return

def main():
    """Checks for blank input."""
    entered_sentence = input("Please enter a sentence?\n>")
    if entered_sentence.strip() == "":
        exit("Program Terminated. (No input)")
    else:
        counter(entered_sentence)
    return

if __name__ == '__main__':
    import doctest
    doctest.testmod(main())

Lastly, please help me on returning from a function and separating printing part not been able to understand that.

Some thoughts:

choose good names

counter is IMO a bit "generic". Perhaps something more expressive like count_vowels_consonants?

Moreover, the inner function's parameter is named count, but it's actually not some count (number) but a text string.

Furthermore you're using the term "sentence", but you're not actually requiring sentences, but rather character sequences / strings.

don't call the function twice

find_total(count_sentence)
vowel_total = find_total(count_sentence)

Here you call find_total, discard its result (return value) and then call it again, this time assigning the result to the local variable.

separate calculation and output

Instead of

print("Vowel: {0}, Consonant: {1}".format(vowel_total, consonant_total))
return

leave out the print and return the values in a tuple

return (vowel_total, consonant_total)

On the caller side, you can then either use the returned tuple as it is

t = count ("foo bar")

or for example use tuple unpacking to obtain the individual counts

vowels, consonants = count ("foo bar")

only iterate once over the characters

Instead of first going one time over the characters to filter out non alphabetic characters and adjust the case of the alphabetic ones and then a second time to count, do it in one loop:

def count_vowels_consonants(text):
  vowels_list = ['A', 'E', 'I', 'O', 'U']
  consonants = 0
  vowels = 0
  for character in text:
    if character.isalpha():
      if character.upper() in vowels_list:
        vowels += 1
      else:
        consonants += 1
  return (vowels, consonants)

test corner cases

Does your function work well with an empty string? A string with only vowels or consonants? A string with only punctuation?


Making user login system secure with $_SESSION I am hoping that someone can help me out here. I am creating a PHP web app that requires a user to login to view their data in the MySQL database.

I just want to know if this is a good approach and is it secure enough. I know that nothing is secure in PHP scripting but I need this fairly secure.

Here is what I am doing step by step. If you could point me in the right direction and tell me what is wrong that would be great!

User logs in with user and password.
PHP checks user and password against the database.
If OK, I store the $SESSION["username"] (their username) to the session.

On every request/page load I have a function (below) to check the session:

function check_session_valid(){ if(!isset($_SESSION['username'])||$_SESSION['username'] == ""){ header("Location: /login.php"); } }

Now is this safe or am I doing this all completely wrong?

Here is what I am thinking that the user could do to "hack" the app:

On the registration page, when the user enters there username it will let them know if the username is available.

Could the "hacker" not just find a username that is not available and then write a PHP script to set the $_SESSION["username"] in their browser, then navigate to the .index.php page and be logged into some other users account?

I am also using session_start() before I check_session_valid() on every request. Is that wrong?

One long line that is hard to read.

The technique looks legitimate, but I caution against reusing 'username' as the key for storing the login state.  Who knows, someday you might want to be able to persist a username even when logged out.  Or maybe a multi-page user registration form would set $_SESSION['username'] between pages.  I recommend choosing something more explicit, such as 'login_time' — and you could also use it later to help with automatic logouts after a certain time.

Note that calling header(…) does not halt the regular program flow.  Whoever calls check_session_valid() will still need to terminate further processing if the session is invalid, so your check_session_valid() should return a TRUE/FALSE value as well.  You could also throw an exception to ensure that regular program flow is impossible.


Optimizing large WP Query I have WP Query like this:
      $map_args = array(
        'post_type'       => 'spots',
        'post_status'     => 'publish',
        'posts_per_page'  => -1
      );
      $markers_query = new WP_Query($map_args); ?>
      <?php if ($markers_query->have_posts()) : ?>
        <?php while ($markers_query->have_posts()) : $markers_query->the_post();
          $terms = get_the_terms( $post->ID , 'spots_categories' );
          $map_marker = get_field( 'map_marker', $terms[0]->taxonomy . '_' . $terms[0]->term_id );
          $tooltip_photo = get_the_post_thumbnail($post->ID, 'thumbnail', array('class' => 'marker-pop__photo'));
          $marker_url = esc_url(get_the_permalink());
          if ( $spot_location = get_field( 'spot_location' ) ) :
            $markersArray[] = array($spot_location['lat'], $spot_location['lng'], get_the_title(), 'http://localhost/icamp/app/wp-content/themes/iCamp/images/logo-black.svg', $tooltip_photo, $marker_url );
          endif; ?>
        <?php endwhile; ?>
      <?php endif; ?>
      <?php wp_reset_query(); ?>
      <?php wp_reset_postdata(); ?>

'spots' is my custom post type, and 'spots_categories' is my custom taxonomy. I have over 10.000 CPT in spots right now, so that query is really slow. I need all my posts (can't use pagination here), because I need to list that locations on leaflet map (pins with title, thumbnail and custom marker). Is there any better and faster way to list my custom pins with location to leaflet map? That Query is really slow.

The first thing I would consider is saving the resulting $markersArray into a transient - I'm not sure how much data you can fit into one transient, so you might need to split the data into couple of transients. More about transients, https://developer.wordpress.org/apis/handbook/transients/
The basic idea is to run the costly query once, store the result into one value in the wp_options table, and then use that value on any subsequent requests, skipping the query alltogether.
But with transient you need to decide when and how you invalidate and rebuild them. Do you give them some lifespan (minutes, hours, days...) and let them recreate themselves whenever the lifespan expires. Or is the transient only recreated whenever you add a new post or update an existing post. Or you could conjure a scheduled action (WP Cron, replaced with system cron for accuracy) that runs the query and updates the transient at a low-traffic time of the day, which can cause delay between post publish/update and the data actually showing on the public view.
Another option could be to skip the query on the initial php load and move it to an ajax request. So you would first show an empty map, which would get populated by a looping ajax request either to admin-ajax.php or to a custom WP REST Api endpoint. For example first fetch the first 1000 posts, when they're ready the next 1000 (i.e. page 2), and so on until all of the posts / map pins have been fetched. The pin json could then be stored to the user's browser, if needed.
I'm not a database expert, but I'd imagine you could also add a custom db table to serve as a aggregate table, which would contain appropriate columns for the pins. This table would get populated / updated whenever you would add or update a post - i.e. the save_post_{$post->post_type} action, https://developer.wordpress.org/reference/hooks/save_post_post-post_type/. Then you'd query this aggregate table with $wpdb and get all of the pin data on one go.
Or instead of an aggragate table or a transient, turn the query result into json and store it into a file, which you could then include and have the map script consume.
And if not all map pins are needed at the very beginning, get only the pins for the currently visible area with an ajax request when you know the map boundaries. Then fetch more pins when the boundaries change.


Combine Filter to include and Exclude I have some data which I want to filter. I have a collection of things to include and a second one of things to exclude.

Currently I'm looping my data and filter once for the including and a second time for the excluding.

Example coding:

#[derive(Debug)]
pub struct Dataset {
    pub data: Vec<String>
}

fn main() {
    //setup test data
    let mut ds1_data : Vec<String> = Vec::with_capacity(2);
    ds1_data.push("a".to_string());
    ds1_data.push("b".to_string());
    let ds1 = Dataset{data: ds1_data};

    let mut ds2_data : Vec<String> = Vec::with_capacity(2);
    ds2_data.push("a".to_string());
    ds2_data.push("c".to_string());
    let ds2 = Dataset{data: ds2_data};

    let mut datasets : Vec<Dataset> = Vec::with_capacity(2);
    datasets.push(ds1);
    datasets.push(ds2);

    let mut include : Vec<String> = Vec::with_capacity(2);
    include.push("a".to_string());

    let mut exclude : Vec<String> = Vec::with_capacity(1);
    exclude.push("b".to_string());

    //filter datasets
    for ds in datasets {
        if ds.data.iter().find(|d| include.contains(d)).is_none() {
            //skip not included
            continue;
        }
        if ds.data.iter().find(|d| exclude.contains(d)).is_some() {
            //skip excluded
            continue;
        }

        println!("{:?}", ds);
    }
}

Also available in the rust playground:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=29c13f2b7ab3a50fbcd12b88b59f4b74

Is there way to combine the filter statements so I don't need to iterate ds.data two times?

To clarify:
The above coding is working as expected. I get datasets containing at least one element of include but no element of exclude

The vec! macro

Before we have a look at including and excluding, we should note that

let mut myvec: Vec<T> = Vec::with_capacity(2);
myvec.push(a);
myvec.push(b);
myvec.push(c);

can be written with the vec! macro as

let myvec: Vec<T> = vec![a,b,c];

This has the nice side-effect that myvec doesn't need to be mut anymore. If we apply this too all your code, we end up with

#[derive(Debug)]
pub struct Dataset {
    pub data: Vec<String>,
}
fn main() {
    //setup test data
    let ds1_data = vec!["a".to_string(), "b".to_string()];
    let ds1 = Dataset { data: ds1_data };

    let ds2_data = vec!["a".to_string(), "c".to_string()];
    let ds2 = Dataset { data: ds2_data };

    let datasets = vec![ds1, ds2];

    let include = vec!["a".to_string()];
    let exclude = vec!["b".to_string()];

    //filter datasets
    for ds in datasets {
        if ds.data.iter().find(|d| include.contains(d)).is_none() {
            continue;
        }
        if ds.data.iter().find(|d| exclude.contains(d)).is_some() {
            continue;
        }

        println!("{:?}", ds);
    }
}

That's a lot less noise, perfect to concentrate on the original problem.

Including and excluding

First of all, instead of find(…).is_some(), we can use any, and instead of find(…).is_none() too. We could use that to simplify the expressions, but we would still traverse the set twice.

for ds in datasets {
    if !ds.data.iter().any(|d| include.contains(d)) {
        //skip not included
        continue;
    }
    if ds.data.iter().any(|d| exclude.contains(d)) {
        //skip excluded
        continue;
    }

    println!("{:?}", ds);
}

Instead, let's think about the elements we have to look at. The answer is all, as exclude might match the last element in our dataset, even if include matched the first.

As we have to look at all elements either way, we might as well traverse them by hand:

    for ds in datasets {
        let mut included = false;
        let mut excluded = false;
        for d in &ds.data {
            if included == false && include.contains(d) {
                included = true;
            }
            if exclude.contains(d) {
                excluded = true;
                break;
            }
        }
        if included == false || excluded == true {
            continue;
        }

        println!("{:?}", ds);
    }

Now that we have this as a blueprint, we can check the iterators for a fitting method. We need to

check that at least one include matches
check that no exclude matches
stop as soon as soon as any exclude matches.

Unfortunately it's not possible to quick exit from a closure, so the last requirement cannot be done with fold or similar. However, we can use inspect and all to get something like our previous loop:

    for ds in datasets {
        let mut included = false;

        if !ds
            .data
            .iter()
            .inspect(|d| {
                if included == false || include.contains(d) {
                    included = true
                }
            })
            .all(|d| !exclude.contains(d))
            || !included
        {
            continue;
        }
        println!("{:?}", ds);
    }

But that's a lot less readable than our previous loop, which did the same. We could increase readablity if we introduced a function, but that's left as an exercise. In the end, we would need a fold-like function that supports an early exit, but such a function does not exist.


Read text and return lists of different parts of speech This function reads through text and returns lists of all the different parts of speech (nouns, verbs, proper nouns, etc.). My sense is that there is a more elegant and probably more efficient way to accomplish the same thing. The code feels really repetitive so I'm hoping there is a better way.

import nltk

def find_pos(tokens):

    '''This function accepts tokens as an input and returns a list of all
    the parts of speech.
    Note that some words are return twice:
    -Nouns are separated into common and proper as well as grouped together
    -Modals are added to verbs are well as returned separately'''

    tagged = nltk.pos_tag(tokens)

    # Now we devide them into groups
    # Note that IN can be either a preposition or a conjunction, for now we're going to list it with the prepositions
    common_noun_pos = ['NN', 'NNS']
    common_nouns = []
    verb_pos = ['VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ']
    verbs = []
    adjective_pos = ['JJ', 'JJR', 'JJS']
    adjectives = []
    pronoun_pos = ['PRP', 'PRP$', 'WP', 'WP$']
    pronouns = []
    adverb_pos = ['RB', 'RBR', 'RBS', 'WRB']
    adverbs = []
    proper_noun_pos = ['NNP', 'NNPS']
    proper_nouns = []
    conjunction_pos = ['CC']
    conjunctions = []
    preposition_pos = ['IN', 'TO']
    prepositions = []
    interjection_pos = ['UH']
    interjections = []
    modal_pos = ['MD']  # But these are also verbs, include them
    modals = []
    tagged_other_pos = ['CD', 'DT', 'EX', 'FW', 'LS', 'PDT', 'POS', 'RP', 'SYM', 'WDT']
    tagged_others = []
    other = []

    for idx, token in enumerate(tagged):
        if token[1] in common_noun_pos:
            common_nouns.append(token)
        elif token[1] in verb_pos:
            verbs.append(token)
        elif token[1] in adjective_pos:
            adjectives.append(token)
        elif token[1] in pronoun_pos:
            pronouns.append(token)
        elif token[1] in adverb_pos:
            adverbs.append(token)
        elif token[1] in proper_noun_pos:
            proper_nouns.append(token)
        elif token[1] in conjunction_pos:
            conjunctions.append(token)
        elif token[1] in preposition_pos:
            prepositions.append(token)
        elif token[1] in interjection_pos:
            interjections.append(token)
        elif token[1] in modal_pos:
            modals.append(token)
        elif token[1] in tagged_other_pos:
            tagged_others.append(token)
        else:
            other.append(token)

    verbs.append(modals)
    nouns = common_nouns + proper_nouns
    parts_of_speech = [nouns, common_nouns, verbs, adjectives, pronouns, adverbs, proper_nouns, conjunctions,
                       prepositions, interjections, modals]
    return parts_of_speech

Here some code to test it:

text = 'It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in ' \
       'want of a wife. However little known the feelings or views of such a man may be on his first entering a ' \
       'neighbourhood, this truth is so well fixed in the minds of the surrounding families, that he is ' \
       'considered the rightful property of some one or other of their daughters. '
tokens = nltk.word_tokenize(text)
find_pos(tokens)

For some more typing up front, you can make the parsing slightly easier.

This uses the fact that two variables can point to the same (even mutable) object:

def find_pos(tokens):
    tagged = nltk.pos_tag(tokens)
    # Define all lists (just like before), here with tuple unpacking:
    common_nouns, verbs, adjectives, pronouns, adverbs, proper_nouns, conjunctions, prepositions, interjections, modals, tagged_others, other = [
        [] for _ in range(12)]

    # Define a dictionary mapping from the positions to the lists:
    d = {'NN': common_nouns, 'NNS': common_nouns,
         'VB': verbs, 'VBD': verbs, 'VBG': verbs, 'VBN': verbs, 'VBP': verbs, 'VBZ': verbs,
         ...}

    # Use it like this:
    for token in tagged:
        d[token[1]].append(token)
    ...

Alternatively, do the calculations afterwards:

from collections import defaultdict

def find_pos(tokens):
    tagged = nltk.pos_tag(tokens)

    d = defaultdict(list)
    for token in tagged:
        d[token[1]].append(token)

    def t(*pos):
        return sum([d[x] for x in pos], [])

    return [t('NN', 'NNS', 'NNP', 'NNPS'),
            t('NN', 'NNS'),
            t('VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ'),
            ...]


Having a class return a single database instance or list of all database instances I'd like the following to happen:

var person = new Person(); // Create instance of new person with Id 0
var person1 = new Person(1); // Creates instance of existing person from database
var list = new Person().List(); // Creates instance of all persons from database

I'm sure there's a better way than what I'm currently doing:

using System.Collections.Generic;
using System.Data;

public class Person
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int GenderId { get; set; }

    public Person()
        : this(0)
    {
    }

    public Person(int id)
    {
        if (id == 0)
        {
            Id = id;
        }
        else
        {
            LoadFromDataBase(id);
        }
    }

    public List<Person> List()
    {
        return LoadFromDataBase(0);
    }

    private List<Person> LoadFromDataBase(int personId)
    {
        var dt = new DataTable();
        // Assume Data acquired from database using personId.
        // A Value of 0 returns all rows

        var list = new List<Person>();

        foreach (DataRow dr in dt.Rows)
        {
            var person = new Person();
            Id = person.Id = (int)dr["Id"];
            Name = person.Name = (string)dr["Name"];
            GenderId = person.GenderId = (int)dr["GenderId"];

            list.Add(person);
        }

        return list;
    }
}

First, I don't like the concept of mixing "Models" with "Repository".

One principle that, from my point of view, should be followed, is the Single Responsibility (the S in SOLID). It means that one class should have one responsibility, and only one. Therefore yours would be against this principle, simply because you are holding your data, and loading data from the database.

If you want to follow this Principle, you would have one class "PersonRepository", that handles getting/updating data to the database, and a second class "Person", which is the person itself.

PersonRepository personRepository = new PersonRepository();
Person person = personRepository.Find(1); // Would return a person
List<Person> persons = personRepository.FindAll(); // Would return all records

Anyway, regarding your implementation, the point that are bad :

Constructors --> Never do any database related/IO operation in constructor. How would you handle the exception ? You would have an object created (as you cannot destruct them), but actually a "not working" object. Bad practice. And as said by Nick, it will not be clear for the user of the class that this load the user from database. Imagine he wants to create a Person with a predefined ID, he may think using the "ID" for the constructor would be the way to go
As said before, Load and List should be static.
Why is the "LoadFromDatabase" id mandatory ? Use optional parameters, in example :

... LoadFromDataBase(int personId = 0);

Same for the constructor
Imagine now you have a webpage/grid in an application that should display some filtered value, or paginated. How would you handle this ? You would create tons of method within your person class to "LoadFromDatabaseFilterByName(String name)", "LoadFromDatabasePage(2)",... And this would mess up with real method of the class (see first paragraph.
As said by Nick, the LoadFromDataBase is really... strange.


Compare same filenames with different extensions I wrote this piece of code below, but I'm sure it can be done in a more Pythonic way. Only, my knowledge is not that much to be able to figure out a more efficient way.

The DOCX and PDF files are in the same folder.

def compare_text_docx_pdf(path):
    for i in range(0, len(os.listdir(path))):
        if os.listdir(path)[i].upper().endswith('.PDF') and '.' not in os.listdir(path)[i][:7]:
            for j in range(0, len(os.listdir(path))):
                if os.listdir(path)[j].upper().endswith('.DOCX'):
                    if os.listdir(path)[i][:7] == os.listdir(path)[j][:7]:
                        try:
                            print('PDF:', os.listdir(path)[i][:7])
                            print('DOCX:', os.listdir(path)[j][:7])
                        except Exception as e:
                            print(e)

If your directory is only containing two types of files, I would do the following way, using splitext function and Counter:

import os
from os.path import splitext
from collections import Counter

def compare_text_docx_pdf(path):
    #List containing all file names + their extension in path directory
    myDir = os.listdir(path)
    #List containing all file names without their extension (see splitext doc)
    l = [splitext(filename)[0] for filename in myDir]
    #Count occurences
    a = dict(Counter(l))
    #Print files name that have same name and different extension
    for k,v in a.items():
        if v > 1:
            print(k)


Recursive vs. non-recursive implementations of Fibonacci sequence I have 2 functions to get the n-th fibonacci number. The 1st one uses recursive calls to calculate the power(M, n), while the 2nd function uses iterative approach for power(M, n). Theoretically (at least what I think), they should have the same speed O(log n), but why when I run both, the 2nd one is much slower than the 1st one?

def fib_1(n):
    from numpy import matrix
    def power(F, n):
        if n == 0 or n == 1: return matrix('1 1; 1 0', object)

        F = power(F, n >> 1) # n // 2
        F = F * F
        if n % 2 == 0:
            return F
        if n % 2 != 0:
            return F * matrix('1 1; 1 0', object)

    F = matrix('1 1; 1 0', object)
    F = power(F, abs(n)-1)
    return F[0,0] if n > 0 else int((-1)**(n+1)) * F[0,0]

def fib_2(n):
    from numpy import matrix
    def power(F, n):
        M = matrix('1 1; 1 0', object)
        while n > 0:
            if n & 1 == 1:
                M = F * M
            n = n >> 1 # n = n // 2
            F = F * F
        return M

    F = matrix('1 1; 1 0', object)
    F = power(F, abs(n)-2)
    return F[0,0] if n > 0 else int((-1)**(n+1)) * F[0,0]

Before we got any further, a word of warning. The complexity is only valid in a particular computational model.

The complexity of these algorithms is \$O(\log n)\$ only if the addition takes constant time. For large \$n\$ it is not the case.

Fibonacci numbers grow exponentially with n. It means that the number of bits grows linearly. Now we are in the Turing machine realm. The last addition itself takes \$O(n)\$, which is way more than \$\log n\$.

The overall complexity should be \$O(n\log{n})\$ in both cases. Why the two exhibit different performance?

The likely reason is that there is a subtle difference between them. fib_1 maintains just one matrix, F. fib_2 maintains two of them, F, M, and must do roughly twice as many reallocations as fib_1 does. The matrices contain huge numbers, and I expect that the memory management time dominates. Yet another factor the computational model shall now consider.


One method for changing one of the properties I have a few resources set in my class – coins, diamonds, hearts, enum of these resources names, and method for increasing specified resource. I want to make single "Increase" method for all resources that wouldn't have switch statement. Sort of single-line not overloaded "generic" method independently on which property to increase. Below is the best what I achieved so far. Please advice on best approach to be taken for the described Increase method.

public enum ResourceType { Coins, Diamond, Hearts };

private static int Coins { get { return resourceBox[ResourceType.Coins]; } }
private static int Diamonds { get { return resourceBox[ResourceType.Diamonds]; } }
private static int Hearts { get { return resourceBox[ResourceType.Hearts]; } }
private static Dictionary<ResourceType, int> resourceBox = new Dictionary<ResourceType, int>();

private void Init()
{
    resourceBox.Add(ResourceType.Coins, 0);
    resourceBox.Add(ResourceType.Diamond, 0);
    resourceBox.Add(ResourceType.Hearts, 0);
}

public static void Increase(ResourceType _res, int _income)
{
resourceBox[_res] += _income;
}

However this leads to situation where Dictionary is a primary resource storage, which may not be desired in future.
I wonder what could be another (not obligatory dictionary) way to create Increase method as described above.

I find there is nothing wrong with using a dictionary here. What I'm worried about are those properties:

private static int Coins { get { return resourceBox[ResourceType.Coins]; } }
private static int Diamonds { get { return resourceBox[ResourceType.Diamonds]; } }
private static int Hearts { get { return resourceBox[ResourceType.Hearts]; } }

Why do you need them? They are not only static but also private. If you cannot access them from the outside how are you going to use them?

You didn't include the complete class definition but just a small snippet so it's hard to give you more feedback.


Outputting scatter plots I have written a python function that outputs scatter plots using Matplotlib after processing the data a little. It works but it's painfully slow. I was wondering if anybody had any suggestions as to how I can improve the efficiency of this function or whether its slow just because it's processing a lot of data (a 30x43 element Pandas data frame).

def scatter_x_yi(self,filtered_data,x_parameter):
    '''
   takes some prefiltered data (len(x) by len(Y)) and the name of a
    data column (x_parameter), plots it against every other column (yi)
    '''
    PE=Parameter_Estimation_Tools() #custom class
    number_of_full_subplots=len(filtered_data)/16
    remainder=len(filtered_data)-(number_of_full_subplots*16)-1
    os.path.dirname(str(filtered_data))
    '''
    some code for isolating the x parameter column
    and all the y's (yi)
    '''
    yi=[]
    for i in range(len(filtered_data)):
        if filtered_data[i].name!=x_parameter:
            yi.append(filtered_data[i])
    '''
    code for plotting the scatter plots (43 in total)
    using matplotlib in 'units' of 4 by 4, then plotting
    the remainder in a last figure

    '''
    try:
        for i in range(len(filtered_data)):
            for j in range(number_of_full_subplots+1):
                fig=plt.figure(j)
                txt = fig.suptitle('{} , {} of {}'.format(str(x_parameter),str(j),str(number_of_full_subplots)),fontsize='18')
                txt.set_text('{} , {} of {}'.format(str(x_parameter),str(j),str(number_of_full_subplots)))
                for k in range(16):
                    plt.subplot(4,4,k)
                    PE.scatter_x_y(filtered_data,x_parameter,yi[16*j+k].name)
                    plt.subplots_adjust(wspace=0.2, hspace=0.25,top=0.9)
            mng=plt.get_current_fig_manager()
            mng.window.showMaximized()
            plt.ioff()
            fig.savefig(str(j)+'.png',bbox_inches='tight',orientation='landscape')
            plt.close(fig)
            plt.ion()
    except IndexError:
        for i in range(len(filtered_data)-remainder,len(filtered_data)-1):
            PE.scatter_x_y(filtered_data,x_parameter,yi[i].name)
            plt.subplots_adjust(wspace=0.2, hspace=0.25,top=0.9)
            mng=plt.get_current_fig_manager()
            mng.window.showMaximized()
            plt.ioff()
            fig.savefig('2.png',bbox_inches='tight',orientation='landscape')
            plt.close(fig)
            plt.ion()
        print '{} has been plotted against all other parameters'.format(x_parameter)

Based on the code provided, here are a few suggestions for where decreasing the time it takes to run it:

Too many ranges, iterators

The first thing that pops out as an area for improvement is the number of basic iterators being used, like for x in range()... and when you are using the Pandas library, it's likely there's a speed improvement there.

The problem with using for x in range() is that the script has to go through every single item in the range, one at a time. Pandas has a number of more efficient approaches to handling this, namely df.groupby(), df[colname].apply(), and in the worst case scenario for index, row in df.iterrows() that will very likely cut down your time.

Here's one example:

yi = []
for i in range(len(filtered_data)):
    if filtered_data[i].name != x_parameter:
        yi.append(filtered_data[i])

It appears you are simply trying to make a subset of filtered_data where the column name doesn't match the x_parameter. Assuming that filtered_data is a Pandas dataframe object, you can replace the code block above with this and it should be faster:

yi = filtered_data.loc[filtered_data.name != x_parameter]

Parallelize the chart generation

Instead of having the code blocked from processing data while it waits for matplotlib to create and save the next chart, making this into a separate function and process that can be run concurrently may save you some time. Breaking up the different steps in your pipeline allows you to run the slow loops separately, and also makes testing easier. For a specific example of how to use multiprocessing to run your charting code concurrently, check out this answer on Stackoverflow.

Use ipython magic functions

It is very helpful to use ipython and the magic functions it contains to debug time issues in code. For instance, if you have broken up your one big script into smaller functions, you can use %timeit and to find out what is taking too long.

Lose your self

This isn't really a speed optimization, but definitely helps with readability.

self is the first argument to this function, however it doesn't seem this variable is used elsewhere. My guess is that the function being asked about is actually a method within a class. One quick and easy optimization would be to remove self from the argument list, since it doesn't appear that you need it, and then add @staticmethod to the just before your function. So this will look like:

@staticmethod
def scatter_x_yi(filtered_data, x_parameter):
    [... rest of the method...]

More Possibilities

There may be some additional ways to optimize this code hidden in the Parameter_Estimation_Tools(). What is that process doing? Take a look if that is taking too much time.
It appears this isn't a standalone function but a method contained in a class. What if this function were broken out to run in isolation?


Path traversal prevention in PHP As with other inputs, file paths should be validated before thay could be passed to operating system interfaces. Since chroot(2) has a bit more than some portability issues, oftentimes it is desireable to use a path-validating function.

The general idea, is that no path component should be .., and that if we chdir(2) to the path in question, we must make sure it doesn't begin with a <slash> ("/").

Also, for some application, files or directories with certain prefix (say <dot> (".")) should be outright rejected.

Here's my example of PHP code that does path-validation. I'd like some expertized opinion from the members of the site.

<?php
 function antitraversal($s)
 {
   if( !is_string($s) ) die("Invalid argument: $s");

   $components = explode("/", $s);
   foreach($components as $component) {
     if( $component[0] === "." ) die("Invalid argument: $s");
   }

   if( $s[0] === "/" ) { return ".$s"; } else { return $s; };
 }

For simplicity, we'll assume the script runs in Unix-like environments.

Security

This can be bypassed by /\...

You are currently checking if / is followed by . and reject the input if that is the case. But as you can see, it is possible to traverse with different sequences (null bytes would be another candidate for bypasses).

It would be a bit more secure if you checked for ... To really prevent directory traversal, you should check if the resolved path is inside the desired directory.

Misc

dieing inside a function is always a bad idea, as you have no more control over the code. The calling function can't recover, the function can't be reused, etc.
You are vulnerable to XSS.
I would put each statement on it's own line (e.g. if/else on the same line is a bit difficult to read).


Check if a string ends with a given suffix, without using .endswith() I figured out how to do this assignment, but now that I have, I'd like to know if there is a cleaner way to do it. Below is what was required of the assignment, below that is my working function that got me the correct answer and further below that is the all the statements that it had to and did pass.

How can I make it better?

  Check if a string (first argument, str) ends with the given target
  string (second argument, target).

  This challenge cannot be solved with the .endsWith()

My Working Code:

function confirmEnding(str, target) {
  var targetLength = target.length;
  var anyString = str.substring(str.length - targetLength);
  return anyString == target;
}

Tests in needs to pass:

console.log(confirmEnding("Bastian", "n")); //should return true.
console.log(confirmEnding("Connor", "n")); //should return false.
console.log(confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification")); //should return false.
console.log(confirmEnding("He has to give me a new name", "name")); //should return true.
console.log(confirmEnding("Open sesame", "same")); //should return true.
console.log(confirmEnding("Open sesame", "pen")); //should return false.
console.log(confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain")); //should return false.

You could use substr and pass it a negative number. Doing so is the equivalent of str.substring(str.length - X). That means you just have to pass -target.length.

function confirmEnding(str, target) {
  return str.substr(-target.length) === target;
}

function test(str, target, expected) {
  let testName = `"${str}" ${expected ? 'ends' : 'does not end'} with "${target}"`;
  if (confirmEnding(str, target) === expected) {
    console.log(`PASS: ${testName}`);
  } else {
    console.log(`FAIL: ${testName}`);
  }
}

test("Bastian", "n", true);
test("Connor", "n", false);
test("Walking on water and developing software from a specification are easy if both are frozen", "specification", false);
test("He has to give me a new name", "name", true);
test("Open sesame", "same", true);
test("Open sesame", "pen", false);
test("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain", false);


Flatmap implementation I have an implementation of flatmap for std::vector. However, I find the repetition of the inferred return type to be ugly. And, in general, I suspect that the implementation could be cleaner or more general.

template<typename T, typename FN>
static auto flatmap(const std::vector<T> &vec, FN fn) -> std::vector<typename std::remove_reference<decltype(fn(T())[0])>::type> {
    std::vector<typename std::remove_reference<decltype(fn(T())[0])>::type> result;
    for(auto x : vec) {
        auto y = fn(x);
        for( auto v : y ) {
            result.push_back(v);
        }
    }
    return result;
};

It can be used like this:

TEST_F(MarkdownUtilTests, testFlatmap) {
    std::vector<int> v = { 1, 2, 3};
    std::vector<int> result = flatmap(v, [](int x) { return std::vector<int> { x, x*2, x*3 }; } );
    assertThat(result, is(std::vector<int> {1,2,3,2,4,6,3,6,9} ));
}

There's a reason the algorithms in the standard library use iterators instead of dealing with containers directly. One is that it makes code like this quite a bit simpler. For example, using iterators, we could write Flatmap something like this:

template <typename InIter, typename OutIter, typename FN>
void Flatmap(InIter begin, InIter end, OutIter out, FN fn) {
    for (; begin != end; ++begin) {
        auto y = fn(*begin);
        std::copy(std::begin(y), std::end(y), out);
    }
}

This is somewhat more versatile, since it can take input from/write output to essentially any iterator, rather than necessarily reading from one vector and producing another vector. Likewise, what's produced by fn doesn't have to be a vector either--it can be essentially any container (anything for which we can use std::begin() and std::end(), to be more exact). While vector is certainly common, there are reasons for using other containers--and sometimes even a reason to write to something that isn't exactly a container either. For example:

std::vector<int> inputs { 1, 2, 3 };

Flatmap(inputs.begin(), inputs.end(),
        std::ostream_iterator<int>(std::cout, "\t"),
        [](auto x) { return std::vector<decltype(x)> { x, x * 2, x * 3 }; });

Note that I've used auto as the parameter type in the lambda, so this test code requires C++14, but Flatmap itself is fine with C++11.

If you wanted to be a bit more forward thinking, you could write this to work with ranges instead of iterators. Eric Neibler's range library is fairly nice, and seems well on its way toward being included in an upcoming C++ standard.

Along with the code being simpler and more versatile, the other obvious advantage would be that this follows the same style as standard library code--for example, its signature and usage is essentially identical to std::transform.


Algorithms for traversing unordered tree I have been doing some programming 'exercises'.  I'm trying to come up with the most efficient algorithm for tree traversal.

Consider the following function signature:

CNode * CNode::find(int number) const

where CNode structure is not organized as a binary search tree (otherwise obviously a faster algorithm would have been used - not involving the entire tree traversal), so all the nodes have to be inspected.

I have implemented 3 algorithms so far:

Stack-based search

CNode * CNode::findStackBased(int number)
{
  Node *tmp = this;
  stack<CNode*> onStack;
  while (tmp != NULL || !onStack.empty())
  {
    if (tmp == NULL)
    {
      tmp = onStack.top();
      onStack.pop();
    }

    if (tmp->number == number)
    {
      return tmp;
    }

    if (tmp->right)
    {
      onStack.push(tmp->right);
    }

    tmp = tmp->left;
  }

  return NULL;
}

Recursive search

CNode * CNode::findRecursion(int number)
{
  if (this->number == number)
  {
    return this;
  }

  Node * result = NULL;
  if (this->left != NULL && (result = left->findRecursion(data)))
  {
    return result;
  }

  if (this->right != NULL)
  {
    return this->right->findRecursion(data);
  }

  return NULL;
}

Walking along the edge

CNode * CNode::findWalkAlongTheEdge(int number)
{
  Node *tmp = this;
  while (tmp != NULL)
  {
    if (tmp->number == number)
    {
      return tmp;
    }

    if (tmp->left != NULL)
    {
      tmp = tmp->left;
      continue;
    }
    else if (tmp->right != NULL)
    {
        tmp = tmp->right;
        continue;
    }
    else
    {
      while (1)
      {
        if (tmp->parent->left == tmp)
        {
          if (tmp->parent->right != NULL)
          {
            tmp = tmp->parent->right;
            break;
          }
        }
        else
        {
          if (tmp->parent == this)
          {
            return NULL;
          }

          tmp = tmp->parent;
        }
      }
    }
  }
  return NULL;
}

Each of the above have have some advantages/drawbacks, but performance wise 'walking along the edge is the winner so far(out of the 3 given above).

My question is:

Do you see any way to optimize it further (recursive implementation already benefits from the tail recursion optimization, at least gcc seems to optimize it this way)?

Do you see any way to optimize it further

I don't see why you have (i.e. don't think you should have) while (tmp != NULL) at the top:

On entry you know that this != NULL
You verify that the result won't be NULL before you execute the statement before each continue or break

Removing that would make it slightly faster.

Also I don't understand ...

    if (tmp->parent->left == tmp)
    {
      if (tmp->parent->right != NULL)
      {
        tmp = tmp->parent->right;
        break;
      }
    }

... because if (tmp->parent->left == tmp) and (tmp->parent->right == NULL) then you do nothing and therefore stay inside while(1) forever. So perhaps either your code is wrong, or one of those if conditions is unecessary.

Apart from the two items above, the existing code looks simple? Multi-threading, using a more-optimizing compiler, or trying to write in assembly are possibilities but not clever. Good optimizations sometimes come from changing the data layout.

For example, one faster algorithm might be to change the way in which the tree is contained in memory: use a custom allocator or similar, to ensure that nodes are physically contained in a contiguous array or vector.

Walking the whole tree would then mean, simply, iterating through the contiguous array: which is pretty quick.

If you do that, it would be easier to change your left, right, and parent pointers to indexes (into the array): because then you wouldn't need to change them if you reallocate (move) the array.

Alternatively keep the nodes in random heap-allocated memory but store the numbers themselves in a contiguous array. Instead of a number member, nodes would have an index member which says which slot in the array contains their number. Making the numbers contiguous (without extraneous tree-node pointers) might be the fastest way to iterate them.