Rebol Function to Categorize Items in a Block I have a function that iterates through a block, tests each value against a given function and is placed in a block associated with the product of that function.

CATEGORIZE items test

An example:

categorize [
    "Aardvark" "Bison"
    "Antelope" "Baboon"
    "Anaconda" "Basking Shark"
] [first item]

Results in:

[
    #"A" [
        "Aardvark"
        "Antelope"
        "Anaconda"
    ]
    #"B" [
        "Bison"
        "Baboon"
        "Basking Shark"
    ]
]

For its test argument, a function! or a block! is accepted—a block! is transformed into a function with a single argument item.

NB. applying new-line before returning the block neatens the resultant block—for aesthetic purposes only.

Review questions I have are:

Is there a more efficient way to evaluate each iteration step?
Are there any potential optimization possibilities for common comparisons? e.g. by first letter, etc.
This is supposed to be a general-purpose function—are there any glaring cases this function fails? Any enhancement suggestions?

The Code

Rebol [
    Title: "Categorize"
    Date: 11-Mar-2014
    Author: "Christopher Ross-Gill"
    Type: 'module
    Exports: [categorize]
]

categorize: func [items [block!] test [any-function! block!] /local out value target][
    out: copy []
    if block? :test [test: func [item] :test]
    foreach item items [
        value: test item
        unless target: select out value [
            repend out [value target: copy []]
        ]
        append/only target item
    ]
    foreach [value items] out [new-line/all items true]
    new-line/all/skip out true 2
]

It's a useful function, and exists in underscore.js as groupBy.  So I'd probably call it group-by.  It might be useful to go function by function in underscore and use their names, possibly even putting them in the namespace _ if requested...although that's a little bit ugly.

The one thing I'll say about the interface in general, is that it's a bit too bad that what's coming back here is a block! and not a map!.  I feel like if you're reorganizing data in this pattern so that you can access it by key, then doing the linear searching both during the creation and later accesses isn't optimal.

Though perhaps rather than being a separate data type that map! should be a "hint" about an access pattern that is made on a block, like:

foo: [a [b c] d [e f] ... xyz [lmno pqrs]]
hint foo [map-access]
data: select foo 'd

Anyway, that's the only major point I'd bring up.  Minor stuff might be that you're using func and /local:

categorize: func [items [block!] test [any-function! block!] /local out value target][

Using function and letting the locals be scanned for, is more readable and maintainable:

categorize: function [items [block!] test [any-function! block!]] [

Performance-wise, the difference is negligible.  Remember that the scan only happens once here:

  delta-time [foo: func [a b /local c d] [c: 10 d: 20 return a + b + c + d] loop 10000 [foo 1 2 3 4]]
  == 0:00:00.009

  delta-time [foo: function [a b] [c: 10 d: 20 return a + b + c + d] loop 10000 [foo 1 2 3 4]]
  == 0:00:00.009022

In fact, the performance difference is generally lost in the noise.  You can repeat such tests and find the FUNC version taking longer than the FUNCTION version.

out: copy []

I've been scolded for using copy [] to create blocks, because you're "wasting a series" as a template, when that series is never used.  More relevantly, I think, is that it's a habit prone to accidentally leaving off the copy and suffering the consequences.  I have a proposal out to say that block~ none would create an empty block and there would be generators for all the types of that form.  But until that happens you might want to use make block! here.


First ever webpage (Google homepage) I am currently completing "The Odin Project" and one of the first projects was to create a google homepage. I was wondering if some people could tell me all the long winded and unnecessary code I added and also tell me how I can better go about sticking the footer to the bottom of the page.

https://github.com/LHUF/google-homepage

HTML:

    <!DOCTYPE html>

        <title>Google</title>
        <link rel="stylesheet" href="style.css">

About Store

Gmail Images

        <form method="GET">
            <input type="text" id="searchbar"></input>

            <input type="submit" value="Google Search" class="searchbuttons"></input>
            <input type="submit" value="I'm Feeling Lucky" class="searchbuttons" id="imfeelinglucky"></input>
        </form>

United Kingdom

Advertising Business How Search Works

Privacy Terms Settings

CSS:

#imfeelinglucky {
    margin-left: 10px;
}

.searchbuttons {
    margin-top: 20px;
    text-align: center;
    height: 30px;
}

img {
    display: block;
    margin-left: auto;
    margin-right: auto;
}

#searchbar {
    width: 400px;
    height: 25px;
    margin-top: -30px;
}

#searcharea {
    text-align: center;
    padding-top: 200px;

}

#unitedkingdom {
    position: relative;
    bottom: 5%;
    border-top-style: solid;
    border-bottom-style: solid;
    border-top: 1px;
    border-bottom: 1px;
    padding-top: 5px;
    padding-bottom: 5px;
    margin-top: 505px;
    color: rgba(0,0,0,.54);
    background: #f2f2f2;
    padding-left: 25px;
    font-size: 15px;
}

.gmailimages {
    position: absolute;
    top: 1%;
    right: 1%;
}

.aboutstore {
    position: absolute;
    top: 1%;
    left: 1%;
}

#bottomrightoptions {
    padding-right: 25px;
    font-size: 13px;
    padding-top: 15px;
    color: rgba(0,0,0,.54);
    background: #f2f2f2;
    position: absolute;
    float: right;
    padding-left: 800px;
    bottom: 0.5%;
    right: 0%;

}

#bottomleftoptions {
    padding-left: 25px;
    font-size: 13px;
    padding-top: 15px;
    color: rgba(0,0,0,.54);
    background: #f2f2f2;
    position: absolute;
    float: left;
    padding-right: 800px;
    bottom: 0.5%;
    left: 0%;
}

#about {
    margin-right: 5px;
}

#gmail {
    margin-right: 5px;
}

.links {
    color: #000;
    text-decoration: none;
    font-size: 13px;
}

body {
    font-family: Arial, sans-serif;
    margin:0;
    padding:0;
}

while your code runs, personally I would change the formatting to start with

I noticed your search area div has a mistake in it, your H1 encloses the Anchor tag and the image but you close the H1 tag before closing the anchor tag, which is syntactically incorrect.  I also noticed that you were missing quotation marks around the value of one of your anchor tags in the gmailimages,
I will make these changes in the suggestions I post below.

Some things like your anchor tags inside of your divs could be on new lines to make the code more readable, let me show you what it looks like when I move the tags to their own lines.

        About
        Store

        Gmail
        Images

        <form method="GET">
            <input type="text" id="searchbar"></input>

            <input type="submit" value="Google Search" class="searchbuttons"></input>
            <input type="submit" value="I'm Feeling Lucky" class="searchbuttons" id="imfeelinglucky"></input>
        </form>

United Kingdom

        Advertising
        Business
        How Search Works

        Privacy
        Terms
        Settings

The next thing that I would suggest is to look into HTML5, there are nice tags for things like

Navigation Menus
Headers
Footers
etc.


Efficient resampling of time series I'm trying to create an efficient function for re-sampling time-series data.
Assumption: Both sets of time-series data have the same start and end time. (I do this in a separate step.)
Resample function (inefficient)
import numpy as np

def resample(desired_time_sequence, data_sequence):
    downsampling_indices = np.linspace(0, len(data_sequence)-1, len(desired_time_sequence)).round().astype(int)
    downsampled_array = [data_sequence[ind] for ind in downsampling_indices]
    return  downsampled_array

Speed testing
import timeit
def test_speed(): resample([1,2,3], [.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6])
print(timeit.timeit(test_speed, number=100000))
# 1.5003695999998854

Interested to hear any suggestions.

The function takes around \$41\mu s\$ on average per run on my machine. About three quarters of it (around \$ 32\mu s\$) are spent for downsampling_indices = np.linspace(...). Add another \$1.5\mu s\$ for round().astype(int), about \$1\mu s\$ for the actual sampling, plus some calling overhead, and you're there.

So if you would need to use the function several times, it would be best to precompute or cache/memoize sampling indices. If I understood your implementation correctly, the downsampling index computation is basically data independent and only depends on the length of the two sequences, so that might be actually viable.

For example you could have

import functools

...

@functools.lru_cache()
def compute_downsampling_indices_cached(n_samples, data_sequence_len):
    """Compute n_samples downsampling indices for data sequences of a given length"""
    return np.linspace(0, data_sequence_len-1, n_samples).round().astype(int)

and then do

def resample_cache(n_samples, data_sequence):
    downsampling_indices = compute_downsampling_indices_cached(n_samples, len(data_sequence))
    return [data_sequence[ind] for ind in downsampling_indices]

Note that I replaced desired_time_sequence by n_samples which would then have to be set to len(desired_time_sequence) since you don't care about the actual values in desired_time_sequence.

It might also be possible to benefit from NumPy's indexing and use return np.array(data_sequence)[downsampling_indices] for larger inputs. You will have to check that yourself.

On my machine resample_cache(...) takes \$1.7\mu s\$, which is about a decent 20x speed up.


Defining a finite set of instances of a class to check against I have a Type class that will have many instances. I get the instances from a web service. The Type class has a code instance field that uniquely ids an instance:
@Data
@AllArgsConstructor
public class Type {

 private String code;

}

Ten of these instances identify a special type. I have several places where I need to check if a Type is one of these special types:
aType.equals(new Type("specialType1")) || aType.equals(new Type("specialType2")) ||...

In other places, I need to find where aType is not a special type. To centralize these specialized types and the check, I put it in the Type class as follows:
@Data
@AllArgsConstructor
public class Type {

  private static final List<Type> specialTypes = Arrays.asList(new Type("specialType1"),...);
  private String code;

  public boolean isSpecialType() {
    return specialTypes.contains(this);
  }
}

Should specialTypes and the logic to compare a type to special types be in the Type class? Or would this type of logic be better in a service class? Should the special types be defined as enums?
Update
Type instances are created via use of open feign:
import org.springframework.cloud.openfeign.FeignClient;

@FeignClient(name="type-service")
public interface TypeClient {

   @GetMapping("/type")
   List<Type> getTypes();
}

First of all, as "being special" is obviously an attribute of your Type class, the check should go into the Type class, as you did, therefore always choose:
public class Type {
    ...
    public boolean isSpecial() {...}
}

over
aType.equals(new Type("specialType1")) || aType.equals(new Type("specialType2")) ||...

Regarding the implementation: I'm not too fond of creating a static collection to compare to. First of all, you search every time, then you use a relatively heavyweight equals method, which seems somewhat exaggerated to me, if the only distinction is a string passed in the constructor.
Personally, I'd use the underlying strings as a constant and determint the special flag in the constructor:
public class Type {
    private static final List<String> SPECIAL_CODES = Arrays.asList("this", "that", "whatever");
    private final String code;
    private final boolean special;

    public Type(String code) {
        this.code = code;
        this.special = SPECIAL_CODES.contains(code);
    }

    public boolean isSpecial() {
        return special;
    }
}

(Note that this change will not make a difference in runtime in real life, unless you check isSpecial() in a tight loop. It is more of a gut-feeling of being somewhat clearer.)


Return Data and Update Row without Multiple Lookups? I have a stored procedure that looks up an article based on the article's title. But I also need to increment a column in the same table that counts the number of times the article has been viewed.

Trying to be as efficient as possible, I wanted a way to do this without performing multiple lookups. Someone pointed out the newer OUTPUT clause, which I've used below.

I'm just wondering if I'm using it in the most efficient way, if I really made it faster, and if there are any other optimizations that could be employed.

DECLARE @Slug VARCHAR(250) -- Stored procedure argument

-- declare @UpdatedArticle table variable
DECLARE @UpdatedArticle TABLE
(
 ArtID INT,
 ArtUserID UNIQUEIDENTIFIER,
 ArtSubcategoryID INT,
 ArtTitle VARCHAR(250),
 ArtHtml VARCHAR(MAX),
 ArtDescription VARCHAR(350),
 ArtKeywords VARCHAR(250),
 ArtLicenseID VARCHAR(10),
 ArtViews BIGINT,
 ArtCreated DATETIME2(7),
 ArtUpdated DATETIME2(7)
);

UPDATE Article
 SET ArtViews = ArtViews + 1
OUTPUT
 INSERTED.ArtID,
 INSERTED.ArtUserID,
 inserted.ArtSubcategoryID,
 INSERTED.ArtTitle,
 INSERTED.ArtHtml,
 INSERTED.ArtDescription,
 INSERTED.ArtKeywords,
 INSERTED.ArtLicenseID,
 INSERTED.ArtViews,
 INSERTED.ArtUpdated,
 INSERTED.ArtCreated
INTO @UpdatedArticle
WHERE ArtSlugHash = CHECKSUM(@Slug) AND ArtSlug = @Slug AND ArtApproved = 1

SELECT a.ArtID, a.ArtUserID, a.ArtTitle, a.ArtHtml, a.ArtDescription, a.ArtKeywords, a.ArtLicenseID,
 l.licTitle, a.ArtViews, a.ArtCreated, a.ArtUpdated, s.SubID, s.SubTitle, c.CatID, c.CatTitle,
 sec.SecID, sec.SecTitle, u.UsrDisplayName AS UserName
 FROM @UpdatedArticle a
 INNER JOIN Subcategory s ON a.ArtSubcategoryID = s.SubID
 INNER JOIN Category c ON s.SubCatID = c.CatID
 INNER JOIN [Section] sec ON c.CatSectionID = sec.SecID
 INNER JOIN [User] u ON a.ArtUserID = u.UsrID
 INNER JOIN License l ON a.ArtLicenseID = l.LicID

This is used in an ASP.NET application using SQL Server 2008.

You could do it all in one step, like this.

CREATE PROCEDURE dbo.IncrementArtViews (@Slug varchar(250)) AS

UPDATE a
SET ArtViews = ArtViews + 1
OUTPUT a.ArtID, a.ArtUserID, a.ArtTitle, a.ArtHtml, a.ArtDescription,
 a.ArtKeywords, a.ArtLicenseID, l.licTitle, a.ArtViews, a.ArtCreated,
 a.ArtUpdated, s.SubID, s.SubTitle, c.CatID, c.CatTitle, sec.SecID,
 sec.SecTitle, u.UsrDisplayName AS UserName
FROM dbo.Article a
 INNER JOIN dbo.Subcategory s ON a.ArtSubcategoryID = s.SubID
 INNER JOIN dbo.Category c ON s.SubCatID = c.CatID
 INNER JOIN dbo.[Section] sec ON c.CatSectionID = sec.SecID
 INNER JOIN dbo.[User] u ON a.ArtUserID = u.UsrID
 INNER JOIN dbo.License l ON a.ArtLicenseID = l.LicID
WHERE a.ArtSlugHash = CHECKSUM(@Slug)
 AND a.ArtSlug = @Slug
 AND a.ArtApproved = 1


Tic Tac Toe in Python 3.x I made Tic Tac Toe in Python. Standard code review, please tell me ways to improve. I am very new to Python, so please do not criticize very roundabout ways. Thanks.
board = []
for x in range(3):
    board.append([' '] * 3)
def print_board(board):
    x = 1
    print('    A   B   C')
    print()
    dashes = 0
    for row in board:
        print(x, ' ', ' | '.join(row))
        if dashes <= 1:
            print(' ', ' -'*6)
            dashes += 1
        x += 1
def split(word):
    return [char for char in word]
print('This is Tic Tac Toe.')
print('Player 1 will be X and Player 2 will be O.')
win = False
while win != True:
    print()
    print_board(board)
    print()
    while True:
        p1 = input('Player 1, where would you like to place a X? ').lower()
        try:
            p1_col_let = split(p1)[0]
            p1_row = int(split(p1)[1])
            p1_col = int(ord(p1_col_let) - 96)
        except ValueError:
            print('Invalid format.')
        if board[p1_row - 1][p1_col - 1] != ' ':
            print('That spot is already taken.')
        else:
            break
    while True:
        p2 = input('Player 2, where would you like to place an O? ').lower()
        try:
            p2_col_let = split(p2)[0]
            p2_row = int(split(p2)[1])
            p2_col = int(ord(p2_col_let) - 96)
        except ValueError:
            print('Invalid format.')
        if board[p2_row - 1][p2_col - 1] != ' ' or p2_row == p1_row and p2_col == p1_col:
            print('That spot is already taken.')
        else:
            break
    board[p1_row - 1][p1_col - 1] = 'X'
    board[p2_row - 1][p2_col - 1] = 'O'
    a1 = board[0][0]
    a2 = board[1][0]
    a3 = board[2][0]
    b1 = board[0][1]
    b2 = board[1][1]
    b3 = board[2][1]
    c1 = board[0][2]
    c2 = board[1][2]
    c3 = board[2][2]
    if a1 == 'X' and a2 == 'X' and a3 == 'X' or b1 == 'X' and b2 == 'X' and b3 == 'X' or c1 == 'X' and c2 == 'X' and c3 == 'X' or a1 == 'X' and b1 == 'X' and c1 == 'X' or a2 == 'X' and b2 == 'X' and c2 == 'X' or a3 == 'X' or b3 == 'X' or c3 == 'X' or a1 == 'X' and b2 == 'X' and c3 == 'X' or c1 == 'X' and b2 == 'X' and a3 == 'X':
        print()
        print_board(board)
        print()
        print('Player 1 wins!')
        win = True
    if a1 == 'O' and  a2 == 'O' and a3 == 'O' or b1 == 'O' and b2 == 'O' and b3 == 'O' or c1 == 'O' and c2 == 'O' and c3 == 'O' or a1 == 'O' and b1 == 'O' and c1 == 'O' or a2 == 'O' and b2 == 'O' and c2 == 'O' or a3 == 'O' or b3 == 'O' or c3 == 'O' or a1 == 'O' and b2 == 'O' and c3 == 'O' or c1 == 'O' and b2 == 'O' and a3 == 'O':
        print()
        print_board(board)
        print()
        print('Player 2 wins!')
        win = True
    if a1 != ' ' and a2 != ' ' and a3 != ' ' and b1 != ' ' and b2 != ' ' and b3 != ' ' and c1 != ' ' and c2 != ' ' and c3 != ' ':
        print('It\'s a tie!')
        break

Naming of variables is perfect and you are following the python convention - and that is really good because it makes the code easier to read.
I've executed the code and I've noticed that when you insert an invalid input two things may happen

Input "1A"; Output:  "Invalid format. That spot is already taken."
Input "X"; Output: Application crash

So that's a bug that we need to address.
Biggest problems in the code are

Use of global scope (that is, putting variables outside of methods)
Code duplication

About the first one, we need to put everything inside a method. This is better because you will constraint the scope of variables, make things easier to read and the intention of each line will be clearer. Also it's always better to define all the methods at the beginning and then the code that needs to be executed (that should be in a main method, normally).
If I would do it I would create a Board class but there is no particular reason to use Object Oriented Programming here so I will try to follow your way of reasoning.
At first we are initializing the board so
def create_board():
    board = []
    for _ in range(3):
        board.append([' '] * 3)
    return board

You were not actually using the x variable in the loop so I've renamed to _, which is a convention to say that you're not actually interested in that variable. To have magic numbers, like 3 in this case, is bad because someday you may ask "Why 3?". Now it pretty obvious, but it might get more complex, an easy way to fix that is
def create_board(size=3):
    board = []
    for _ in range(size):
        board.append([' '] * size)
    return board

The behavior is the same but now everybody will know what is 3. Is quite useless to have a parametric sized board, because you can play Tic-Tac-Toe only with 3x3 boards, but I'm using that as a way to show you how to be prepared for later improvements (Connect-Four? :) ).
Now let's print the board. You're code was actually pretty good, I wanted to add a variable size board because, in your code, you're using the fact that the board is 3x3 in many hidden ways. (For example if dashes <= 1:).
def print_board(board):
    board_size = len(board)
    cols = [chr(i) for i in range(ord('A'), ord('A') + board_size)]
    print()
    print(f"    {'   '.join(cols)}")
    print()
    row_label = 1
    for row in board:
        print(row_label, ' ', ' | '.join(row))
        if row_label < board_size:
            print(' ', ' -' * (2 * board_size))
        row_label += 1
    print()

x became row_label because that what it is. I've added the print() before and after, because they are, in fact, part of the print of the board.
The split method is not useful, characters in string can be already accessed by index. So we can get rid of it and the game will work as before.
As I was saying, there a few duplications in the code, and they are in the game loop, let's try to refactor that part, starting from the user input:
def ask_value(player_name, player_symbol, board):
    board_size = len(board)
    while True:
        player_input = input(f"{player_name}, where would you like to place a {player_symbol}").lower()
        try:
            col_let = player_input[0]
            row = int(player_input[1]) - 1  # [REVIEW COMMENT] we use 0 based indexes, the user doesn't
            col = int(ord(col_let) - ord('a'))
            if (not (0 <= col < board_size)) or (not (0 <= row < board_size)):
                raise ValueError("Index out of board")
            if board[row][col] != ' ':
                print('That spot is already taken.')
                raise ValueError("Spot already taken")
            return row, col
        except (ValueError, IndexError):
            print('Invalid format.')

This method will replace the duplication of code for the input. I've added a range check to avoid going outside of the board and now the input validation bug is fixed.
Avoid to use non-zero based index; it is not the normal approach and you will get rid of all those - 1 all over the code.
The part where you check who won is a bit cumbersome and is using more developer time than cpu time (how long it took to check all the boolean conditions? :) ) also it doesn't scale with the board size (I understand that this wasn't a problem for you).
I'm not going to write how to check who won but I will suggest you a way to structure your code:
def check_winner(board, symbol):
   if check_rows(board, symbol):
       return symbol
   if check_cols(board, symbol):
       return symbol
   if check_diagonals(board, symbol):
       return symbol
   if check_tie():
       return '#'
   return None

So the main loop will become:
def main():
    board = create_board()
    print('This is Tic Tac Toe.')
    print('Player 1 will be X and Player 2 will be O.')
    players = [{"name": "Player 1", "symbol": "X"}, {"name": "Player 2", "symbol": "O"}]
    end = False
    while not end:  # [REVIEW COMMENT] this is more pythonish than "win != True"
        print_board(board)
        for player in players:
            symbol = player["symbol"]
            row, col = ask_value(player["name"], symbol, board)
            board[row][col] = symbol
            winning_symbol = check_winner(board, symbol)
            if winning_symbol == '#':
                print("It is a tie!")
                end = True
                break
            elif winning_symbol:
                print(f"{player['name']} has won!!")
                end = True
                break

if __name__ == '__main__':
    main()


Java 2 grouped thread scenario I have the following multi thread scenario and the codes. It looks kind of messy and want to know if I can make it better or if there is any flaw to fix.
When a request arrives, one thread downloads it and the remaining operations are dependent on download so it needs to wait for download to finish. But there can be many request coming and download tasks must run at the same time but each process operation need to wait for its own download task. So I did the following.
ProcessCompletedListener
public interface ProcessCompletedListener {
    void onComplete(Object object);
}

RequestListener
public interface RequestListener {
    void onRequest(Object request);
}

Receiver class where I send request for testing purpose.
public class Receiver {
private RequestListener requestListener;

public void setRequestListener(RequestListener requestListener) {
    this.requestListener = requestListener;
}

public void requestBomb() {
    String[] names = new String[]{"a", "b", "c"};

    int i = 0;
    while (i < 3) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        requestListener.onRequest(names[i]);
        i++;
    }
}
}

Custom blocking queue
public class CustomBlockingQueue {
private BlockingQueue<Object> blockingQueue = new LinkedBlockingDeque<>();
private ProcessCompletedListener processCompletedListener;

public boolean offerAndProcess(Object object) {
    if (blockingQueue.offer(object)) {
        process(object);
        return true;
    }
    return false;
}

public void setProcessCompletedListener(ProcessCompletedListener processCompletedListener) {
    this.processCompletedListener = processCompletedListener;
}

private void process(Object object) {
    new Thread(() -> {
        System.err.println(object + ": Request process started.");

        try {
            Thread.sleep(6000); // mock for real operation
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.err.println(object + ": Request process completed.");

        if (processCompletedListener != null) {
            processCompletedListener.onComplete(object);
        }
    }).start();
}
}

Demo class
public class Demo {
public CustomBlockingQueue customBlockingQueue;

public void setCustomBlockingQueue(CustomBlockingQueue customBlockingQueue) {
    this.customBlockingQueue = customBlockingQueue;
}

public CustomBlockingQueue getCustomBlockingQueue() {
    return customBlockingQueue;
}

public static void main(String[] args) {
    Demo demo = new Demo();
    demo.setCustomBlockingQueue(new CustomBlockingQueue());

    Receiver receiver = new Receiver();
    Thread t = new Thread(() -> {
        receiver.setRequestListener((request) -> {
            new Thread(() ->
                    demo.getCustomBlockingQueue().setProcessCompletedListener((Object object) -> {
                        System.err.println(object + ": post process started");
                        try {
                            Thread.sleep(1500); // mock for real operation
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.err.println(object + ": post process completed");
                    })).start();
            demo.getCustomBlockingQueue().offerAndProcess(request);
        });
    });
    t.start();
    receiver.requestBomb();
}
}

When a request arrives, one thread downloads it and the remaining operations are dependent on download so it needs to wait for download to finish. But there can be many request coming and download tasks must run at the same time but each process operation need to wait for its own download task.

That does sound like you want it in the same thread, like a "standard" webserver actually. A request is routed to a thread which handles that request from start to finish. No waiting or synchronizing involved. These threads can also easily be pooled.

    int i = 0;
    while (i < 3) {

I find it curios that you've chosen a while over for here.

        try {
            Thread.sleep(6000); // mock for real operation
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

AS a note, you most likely want to exit here. The InterruptedException is being thrown when the Thead has been asked to stop doing whatever it does, so you don't want to ignore it most of the time.

    t.start();
    receiver.requestBomb();

Mind you that the Thread might not have started at this point. start does start the thread, obviously, but whether it has already executed something when your logic reaches the next instruction is undefined. Whether receiver.setRequestListener(...) has been called when you call threadBomb is undefined, might not have been.

Your code looks and reads fine, actually. It's well written, however, it is unnecessarily complicated, or at least I'm failing to see why you need to do it this way.
What would make sense is to have a single thread pool for processing, which is being fed by a single queue. A little pseudo-code for that:
Queue processingQueue = new BlockingQueue();

class RequestReceiver {
    public void onRequestReceived(Reqquest request) {
        processingQueue.add(request);
    }
}

class ProcessingThread {
    public void run() {
        while (aliveCondition) {
            Request request = processingQueue.poll();
            download(request);
            process(request);
        }
    }
}

This scheme is in, some form or another (rather another), also used by servers and webservers to process requests. You spin up multiple ProcessingThreads and have them wait for something to do, and after being done, they go back into the idle state.
If you now require different threads for download and processing, you're just adding a another queue, like this:
Queue downloadingQueue = new BlockingQueue();
Queue processingQueue = new BlockingQueue();

class RequestReceiver {
    public void onRequestReceived(Reqquest request) {
        downloadingQueue.add(request);
    }
}

class DownloadingThread {
    public void run() {
        while (aliveCondition) {
            Request request = downlaodingQueue.poll();
            download(request);
            processingQueue.add(request);
        }
    }
}

class ProcessingThread {
    public void run() {
        while (aliveCondition) {
            Request request = processingQueue.poll();
            process(request);
        }
    }
}

    t.start();
    receiver.requestBomb();

Mind you that the Thread might not have started at this point. start does start the thread, obviously, but whether it has already executed something is undefined. The


Google People PHP best practise to get OOP I need to log in users to web app in Javascript and PHP. So I have Javascript interface which send me accessToken to PHP which stores users data to DB and log user in. But the result code does not look like OOP but like a peace of garbage. May be I dont know how to write that code, so the question is if this peace of PHP code is ok or if it is possible to rewrite it to real OOP. Cause OOP exists because of people who use the code. It should make sense for humans. It seems this make sense only for authors of the code.
$code = $_POST['code'];

try
{
    $client = new Google_Client();
    $client->setAuthConfig(__DIR__ . '/../model/JsonData/GoogleCredentials.json');
    $client->setRedirectUri( self::REDIRECT_URI );

    $accessToken = $client->fetchAccessTokenWithAuthCode( $code );

    $client->setAccessToken( $accessToken );

    $peopleService = new \Google_Service_PeopleService( $client );
    $me = $peopleService->people->get('people/me', ['personFields' => 'emailAddresses,names,metadata,coverPhotos']);  // This is extra feature which does not accept spaces....

}
catch ( \Exception $e )
{
    Debugger::log( $e );
    $this->sendJson( array( 'errCode' => 1, 'error' => 'Log in fails.' ) );
}

/** @var \Google_Service_PeopleService_EmailAddress $email */
foreach ( $me->getEmailAddresses() as $email )
{
    if ( $email->getMetadata()->getPrimary() ) $user_email = $email->value;  // Oh really? I want to log in user....
}
/** @var \Google_Service_PeopleService_Name $name */
foreach ( $me->getNames() as $name )
{
    if ( $email->getMetadata()->getPrimary() ) $user_name = $name->displayName;
}

if ( ! isset( $user_email ) ) $this->sendJson( array( 'errCode' => 0, 'error' => 'Email is required.' ) );
if ( ! isset( $user_name ) ) $this->sendJson( array( 'errCode' => 0, 'error' => 'Name is required' ) );

// Hello ID is missing. How to uniquely identify user?

May be I am wrong but is it possible to write it better way to get relevant data for log in? Where is ID which can uniquely identify user?
Look at this JS interface. This looks like OOP https://developers.google.com/identity/sign-in/web/people

If I had  to make this OOP, I'd begin by hedging concerns :

a class for user data handling (retrieving the POST and using it)
a service (that you'd dependency inject) to handle the configuration of the Google Service
a GoogleUser class for encapsulating the result of the service and abstracting all those foreach

Then, orchestrate those in some kind of controller (I guess you have already since you use a $this->sendJson method)
I like custom Exception cascade but that's just me :
try{
  //everything working well
}catch(GoogleLoginException $e){
}catch(RequiredEmailException $e){
}catch(RequiredNameException $e){
}

GoogleSDK is notoriously verbose and looks a lot like enterprise java and is always a pain to make elegant.
In the end you'd have something like :

class LoginController{
   private $google;

   public function __construct(GoogleService $google){
     $this->google = $google;
   }

   public function oauthCallbackAction($postRequest){
     $code = $postRequest->get("code");
     try{
         $authentifiedClient = $google->authentifiedClientWithCode($code);
         $googleUser = $authentifiedClient->getMe();
         return $this->json([
                              "user_name" => $googleUser->getUserName(),
                              "email" => $googleUser->getEmail(),
                            ], 200);
     }catch(GoogleAuthException $e){   //thrown by GoogleService
             return $this->json("blah", 500);
     }catch(RequiredEmailException $e){ //thrown by GoogleUser::getEmail
             return $this->json("gimme email !", 400);
     }catch(RequiredNameException $e){  //thrown by GoogleUser::getUserName
             return $this->json("gimme name !", 400);
     }

   }

}


Inverting a Scrabble letter score map Question:

  The old system stored a list of letters per score:

  1 point: "A", "E", "I", "O", "U", "L", "N", "R", "S", "T",

  2 points: "D", "G",

  3 points: "B", "C", "M", "P",

  4 points: "F", "H", "V", "W", "Y",

  5 points: "K",

  8 points: "J", "X",

  10 points: "Q", "Z",

  The shiny new scrabble system instead stores the score per letter, which makes it much faster and easier to calculate the score for a word. It also stores the letters in lower-case regardless of the case of the input letters:

  "a" is worth 1 point.

  "b" is worth 3 points.

  "c" is worth 3 points.

  "d" is worth 2 points.

  Etc.

  Your mission, should you choose to accept it, is to write a program that transforms the legacy data format to the shiny new format.

Code:

import java.util.Map;
import java.util.List;
import java.util.HashMap;

import com.google.inject.AbstractModule;

public class Etl extends AbstractModule {
  public Map transform(Map<Integer, List<String>> oldData) {
    Map<String, Integer> items = new HashMap<String, Integer>();

    for(Map.Entry<Integer, List<String>> entry: oldData.entrySet()) {
      for(String chr: entry.getValue()) {
       items.put(chr.toLowerCase(), entry.getKey());
      }
    }
    return items;
  }
}

Can the above code be made more readable and optimized?

The two things I would change:

The Map you're returning should have type parameters.
The key is best to be a Character rather than a String.

Other than that, I don't see much more to it than what you've got there already.


Python - 'if name in dict:' OR 'try:/except:' I'm writing some OO Python and I would like to know which of these methods is more pythonic, or which is generally better, or faster. This is an accessor for an attribute which is a dictionary.

Method 1: if/else

def GetSlot(self, slot_name):
    if slot_name in self.Slots:
        return self.Slots[slot_name]
    else:
        return None

Method 2: try/except

def GetSlot(self, slot_name):
    try:
        return self.Slots[slot_name]
    except KeyError:
        return None

I understand that using try/except is better use of duck typing, but will this make a positive impact on performance?

It depends.

Mostly, it depends on whether it'll generally be in the dictionary or not. If it's nearly always in the dictionary, then the try/except method would win out, while if it's in there as often as not, then checking would be somewhat faster.

However, python already anticipated your need. There's a better option:

def GetSlot(self, slot_name):
    return self.Slots.get(slot_name)

All mappings support get, which has a default optional argument that defaults to None. This should be the best option. However, that said...

GetSlot is not a PEP8-compliant name. It should be get_slot.
self.Slots should probably be self.slots.
NOTE: Since you confirmed in a comment that you are in fact using externally defined names, I think it is best practice to follow those names as well as their naming conventions when appropriate.
This should probably not be a method at all. In python, accessor functions are somewhat frowned upon. If you really need to do something like this, use properties. However, anything wanting to get slot_name from self.Slots should just use self.Slots.get(slot_name). Even if you're reaching into the object from outside. Java and other languages that advocate getter/setter methods do so because it later allows you to change how it is gotten, but Python is better and doesn't require workarounds like this to influence how you access a variable. Read more here.

A note on Python getters and setters

Python descriptors are a powerful tool. It's not actually an object, it's a protocol of how python retrieves variables when they're used as object attributes. The classic example is python's property, but in truth methods on a class are also descriptors. An object implements this protocol by having __get__, __set__ or __delete__ methods on it. Not all are required for every type of descriptors - please follow the link for in-depth classification and usages.

What this all means in practice is that python objects can change how they are retrieved. This is impossible in languages like Java, which can cause engineering issues.

Let's first figure out why people use getters and setters in Java, because the reasons they have are quite important.

So I have a class with an attribute that I want to expose to the public for usage.

(NOTE: Java users please don't take offense. I'm writing just Python here since I've honestly never used Java. All my knowledge of it is second-hand at best.)

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
        self.distance_to_orgin = pythagoras(x, y)
# Later...
instance = Point(a, b)
instance.distance_to_orgin

Now, everyone can access it. So I publish my library, and everyone's code works fine.

But then, I get a better idea - every point already knows it's x and y, so I can always calculate the distance_to_orgin if I need it.

In Java, I now have a problem. Because to calculate something on retrieval, I NEED a function - but everyone accesses it by attribute access. I cannot make this change in a backwards compatible manner. So, programmers learn to make getter and setter methods. Compare to before:

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
        self.distance_to_orgin = pythagoras(x, y)

    def get_distance_to_orgin(self):
        return self.distance_to_orgin

# Later...
instance = Point(a, b)
instance.get_distance_to_orgin()

Now, if I want to change how it works, I can just write a different get_distance_to_orgin method and I can make the change - fully backwards compatible!

So why isn't this a problem in Python?

Because we have descriptors. In 99.9% of cases, the builtin property() does everything you want. I can just amend my class definition like this:

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
        # self._distance_to_orgin = pythagoras(x, y) No longer needed

    @property
    def distance_to_orgin(self):
        return pythagoras(self.x, self.y)

# Later...
instance = Point(a, b)
instance.distance_to_orgin

And the external usage is exactly the same as it was at the very start, back when we were building a class in a naive way!

So that is why getters and setters are important for many languages, but why we don't need them in Python.


jQuery Plugin for a Form jQuery plugin for a form. Looking for constructive criticism on my code. Thanks in advance!

// form.js
// splashinn

var Form = function(options) {

    options = options || {};

    this.url = options.url;
    this.$fields = $(options.fields);
    this.$status = $(options.status);
    this.$submit = $(options.submit);
    this.$form = options.form || 'form';
    this.rFields = {
        required: /[^.*]/,
        nodigit: /^[^0-9]+$/,
        email: /^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i,
    };
    this.required = [
        this.rFields.nodigit,
        this.rFields.email,
        this.rFields.required,
        this.rFields.required
    ];
    this.errors = ['Please enter a valid name.', 'Please enter a valid email address.', 'You must enter a subject.', 'No message? But I wanted one :('];
}

Form.prototype.init = function init() {

    this.$submit.click($.proxy(function(e) {
        this.process(e);
    }, this));

};

Form.prototype.process = function process(e) {

    var filled = true;

    e.stopImmediatePropagation();
    e.preventDefault();

    this.$fields.each($.proxy(function(i, element) {

        var $element = $(element);

        if (!$element.val().match(this.required[i])) {

            $element.focus();
            this.update(this.errors[i], true);
            filled = false;
            return false;
        }
    }, this));

    filled && this.submit($(this.$form).serialize());
};

Form.prototype.update = function update(message, isError) {

    if (isError && this.$status.hasClass('error')) {

        this.$status.addClass('error')
            .text(message);

        setTimeout($.proxy(function() {
            this.$status.removeClass('shake');
        }, this), 500);
    } else {
        this.$status.fadeOut();

        setTimeout($.proxy(function() {
            this.$status.text(message)
                .fadeIn();

            if (isError) {
                this.$status.removeClass('shake')
                    .addClass('error');
            } else {
                this.$status.removeClass('error info')
                    .addClass('success');
            }
        }, this), 500);
    }
    return this;
};

Form.prototype.submit = function submit(data) {

    $.ajax({
        type: 'POST',
        url: this.url,
        data: data,
        success: $.proxy(function(msg) {
            this.$fields.fadeOut();
            this.$status
                .empty()
                .append('
Nice one! The form has been sent to the server...
')

            $(this.$submit)
                .animate({
                left: '-=500px',
                opacity: 0,
            }, 300);
        }, this)
    });
};

Creating a new form:

var exampleForm = new Form({
    url: mailUrl,
    fields: 'form input, form textarea',
    status: '#status',
    submit: '#submit'
})

exampleForm.init()

Pass in:

The url to send the form data too
The form fields all encapsulated in one string seperated by commas in the correct order of validation rules.
Selector string where you want the status message to go
The submit selector. This element will actually submit the form

It's not necessary to name your methods as below. Someone may reference to it further.

Form.prototype.submit = function submit(data) {
...
}

You can just simply define it as follows

Form.prototype.submit = function (data) {
...
}

ES6 style ;)

Form.prototype.submit = (data) => {
...
}

Form.prototype.init looks as moot method. This is exact what constructor has to do.

I take into account that you are developing a plugin so you're going to reuse it. Right? Thus your CSS classes such as "error", "info", etc. have to be configurable as well as other stuff. Or at least add unique prefix to it.


DOM cloning in a loop I'm cloning a fairly complex div/ page and changing various things in it before appending it to the DOM.  The current method seems long-winded, and I was hoping if there was a simpler way of doing it?

$.each(results.jobs, function(i, result){

var theID = result.id,
theTitle = result.title,
theDescription = result.description,
theLocation = result.location,
theType = result.type,
thePosted = posted[2]+'-'+posted[1]+'-'+posted[0],
theAgency = result.company,
theRef = result.reference,
theSalary = '',
theApps = result.number_of_applications,
hasApplied = result.applied,
theFeatured = result.featured,
theJobNo = pages*i +1;

if(result.hasOwnProperty('salary')){
    var theSalary = result.salary.information.replace(/Â/g, '');
}

var jobContainer = $('#job-container').clone(true, true).attr('data-jobno', theJobNo);

jobContainer.find('h1').text(theTitle);
jobContainer.find('#job-location').text(theLocation);
jobContainer.find('#job-type').text(theType);
jobContainer.find('#job-date').text(thePosted);
jobContainer.find('#job-agency').text(theAgency);
jobContainer.find('#job-description').html(theDescription.replace(/Â£/gi, '&pound;'));
jobContainer.find('#job-ref').text(theRef);
jobContainer.find('#job-id').text(theID);
jobContainer.find('.apply-btn, .email-btn, .save-btn').attr('id', theID);
jobContainer.find('.save-btn').text('Save Job').removeClass('job-saved');

if(localStorage["saved"]!=null && localStorage["saved"]!= '[]'){
    if(localStorage["saved"].indexOf(theID)>= 0){
        jobContainer.find('.save-btn').addClass('job-saved').text('Job Saved');
    }
}

if(theFeatured === '1'){
    jobContainer.find('#job-featured').show();
}else{
    jobContainer.find('#job-featured').hide();
}

if(localStorage["applied"]!=null && localStorage["applied"]!= '[]'){
    if(localStorage["applied"].indexOf(theID)>= 0){
        jobContainer.find('.job-alert').show();
    }
}

if(theSalary != null){
    jobContainer.find('#job-row-salary').show();
    jobContainer.find('#job-salary').text(theSalary);
}else{
    jobContainer.find('#job-row-salary').hide();
}

if(theApps === '0'){
    jobContainer.find('#job-apps').html('Be first to apply!');
}else{
    jobContainer.find('#job-apps').html(theApps);
}

viewJobList.push(jobContainer)

});

$('#slider').html('').append(viewJobList)

This is inside an Ajax success function which is made to an API. I'm cloning an existing page in the HTML and adding new information, adding it to an array/ list then appending to the DOM just the once.

This seems a textbook case for using Angular.

Assuming you do not have the time to invest in Angular, you could consider the following:
If you named all your divs after your result properties ( for example '#job-agency' -> #job-company ), then you would have to do far less extraction work and far fewer text() calls.

You could do something like:

$.each(results.jobs, function(i, result){

  var key;

  //Enhance your result object with some calculated values
  result.thePosted = posted[2]+'-'+posted[1]+'-'+posted[0];
  result.theJobNo = pages*i +1,

if(result.hasOwnProperty('salary')){
  result.theSalary = result.salary.information.replace(/Â/g, '');
}

var jobContainer = $('#job-container').clone(true, true).attr('data-jobno', theJobNo);

for( key in result )
{
  jobContainer.find( '#job-' + key ).text( result[key] );
}

jobContainer.find('.apply-btn, .email-btn, .save-btn').attr('id', theID);
jobContainer.find('.save-btn').text('Save Job').removeClass('job-saved');

etc. etc.

The rest of the code is ok once you apply consistent indenting.


A C++ wrapper for Apple CoreFoundation The wrapper makes it easy to manage memory when using CoreFoundation objects. It aims to act like a simple shared_ptr.

Here is a usage example:

cfobject_wrapper<CGImageSourceRef> imgSource(CGImageSourceCreateWithData(...), false);
...
dispatch_async(..., ^{
  foo(imgSource);
});
// no need to manually call CFRelease, and the CGImageSourceRef is automatically retained when copied to block and released when the block is destroyed.

Here is the code of the wrapper:

/** A wrapper for Apple's Core Foundation objects */
template<typename _T>
class cfobject_wrapper final
{
protected:
  _T _internal;

public:
  cfobject_wrapper()
  : _internal(nullptr)
  {
  }

public:
  explicit cfobject_wrapper(const _T& ref, bool retain = true)
  {
    if (ref && retain)
      CFRetain(ref);
    _internal = ref;
  }

public:
  cfobject_wrapper(const cfobject_wrapper<_T>& ref)
  : _internal(ref._internal)
  {
    if (_internal)
      CFRetain(_internal);
  }

public:
  cfobject_wrapper(cfobject_wrapper<_T>&& ref)
  {
    _internal = std::move(ref._internal);
    ref._internal = nullptr;
  }

public:
  cfobject_wrapper& operator=(cfobject_wrapper other)
  {
    std::swap(_internal, other._internal);
    return *this;
  }

public:
  ~cfobject_wrapper()
  {
    if (_internal)
    {
      CFRelease(_internal);
      _internal = nullptr;
    }
  }

public:
  _T internal() const
  {
    return _internal;
  }

public:
  explicit operator bool() const
  {
    return _internal != nullptr;
  }

};

This is a really great idea! I could certainly use a class like this. Here are some thoughts:
Don't Multiply Declare public
It's very odd that you're prefixing every single method with its visibility. I really hate working with code that has multiple public, private or protected sections. I expect all public methods and members to be grouped together, all protected methods and members to be grouped together, and all private methods and members to be grouped together (usually in that order). While it's not required by the language, it's a pain to constantly have to figure out which section you're in when reading, and it's a pain to write it out for every method prototype when you're writing the code.
Avoid protected Member Variables
In general protected member variables are a bad idea. They essentially mean that any other object of the class or subclass can change their value out from under you. There's a case to be made for public member variables when performance is of the utmost importance, but in general member variables should be private so you can control who's changing them and when.
Retains and Releases Should Be Matched
It's a little surprising that I can tell the constructor not to retain the object, but the destructor will still release it. This is almost like a false cognate in another language - it looks similar to another pattern where you tell the constructor of some object that you're going to manually manage the memory for a given resource, so it shouldn't delete it when destructed. I understand the reason why - some CF methods return retained objects. Still, as someone potentially reading the code, it's a little unexpected. In Objective-C you could autorelease the value when calling the constructor and not worry about the retain, but there's no such mechanism for the C interface to CoreFoundation. Furthermore, the likelihood that I'll remember to pass false to the constructor when I can't remember to do the release manually is unlikely. For that reason, I recommend at least removing the default value. I'm also left wondering if there's some better way to handle this particular case. (I guess a first-class interface to CoreFoundation from C++ would be the ideal answer, but that's not going to happen unless you write it yourself.)


Leetcode longest substring without repeating characters Link here
I'm currently learning c++ coming from a python background, so I'll include a solution in python and in c++ for the following problem statement and based on very helpful answers obtained on my previous question I made some improvements in the c++ implementation:

Given a string s, find the length of the longest substring without repeating characters.

Example 1:
Input: s = "abcabcbb"
Output: 3
Example 2:
Input: s = "bbbbb"
Output: 1
I would like to improve speed for both python and c++ implementations, and I need to improve memory consumption in the c++ implementation since I got a very high figure (600+ MB) as well as the reasons for this high consumption (if the figure is accurate), and I would like general suggestions too.
longest_substring.py
def get_longest(s: str):
    possibilities = (s[i:] for i in range(len(s)))
    maximum = 0
    for possibility in possibilities:
        end_idx = maximum
        while end_idx <= len(possibility):
            current_chunk = possibility[0:end_idx]
            end_idx += 1
            if not (current_size := len(current_chunk)) == len(set(current_chunk)):
                break
            maximum = max(current_size, maximum)
    return maximum

if __name__ == '__main__':
    print(f'Longest substring:\n{get_longest("abcabcbb")}')

Leetcode stats:

Runtime: 260 ms, faster than 19.36% of Python3 online submissions for Longest Substring Without Repeating Characters.

Memory Usage: 14.4 MB, less than 100.00% of Python3 online submissions for Longest Substring Without Repeating Characters.

longest_substring.h
#ifndef LEETCODE_LONGEST_SUBSTRING_H
#define LEETCODE_LONGEST_SUBSTRING_H

#include <string>

int longest_sub(const std::string &s);
bool check_unique(const std::string &s);

#endif //LEETCODE_LONGEST_SUBSTRING_H

longest_substring.cpp
#include "longest_substring.h"
#include <iostream>
using std::endl;
using std::cout;
using std::string;

bool check_unique(const string &s) {
    for (size_t i = 0; i < s.size() - 1; ++i) {
        for (size_t j = i + 1; j < s.size(); ++j) {
            if (s[i] == s[j])
                return false;
        }
    }
    return true;
}

int longest_sub(const string &s) {
    int maximum = 0;
    for (size_t i = 0; i < s.size(); ++i) {
        const string possibility = s.substr(i);
        auto end_idx = maximum;
        while (end_idx < possibility.size()) {
            const string current_chunk = possibility.substr(0, ++end_idx);
            if (!check_unique(current_chunk))
                break;
            auto current_size = current_chunk.size();
            if (current_size > maximum)
                maximum = current_size;
        }
    }
    return maximum;
}

int main() {
    cout << "Longest substring: " << endl;
    cout << longest_sub("abcabcbb");
}

Leetcode stats:

Runtime: 100 ms, faster than 14.88% of C++ online submissions for Longest Substring Without Repeating Characters.

Memory Usage: 604.2 MB, less than 5.02% of C++ online submissions for Longest Substring Without Repeating Characters.

Complexity
Your solution has time complexity \$\mathcal{O}(N^4)\$, which is very bad. There is a \$\mathcal{O}(N)\$ solution for this problem. Consider for example the string:
abcdecfghij

Instead of taking substrings and checking if the substring has duplicates, instead keep track of the last position seen for any possible character. This is basically an array of 256 ints, which you should initialize to -1 to indicate that you've never seen the character before. Then, iterate over the string character by character, and check if the character you look at has already been seen. If not, update its position in the array. So after processing abcde, you will have a = 0, b = 1, c = 2, d = 3, e = 4 and the rest is still -1. Then when you encounter c again, you know you have a duplicate. But instead of starting over from the second character of the string, you should start instead from the character right after the first c, so at position 3. And you know you already have a valid substring up to and including the second c. So then you can continue from there. You continue until you find a character with a recorded position that is equal to or more than the starting position of the current substring. Here is a possible implementation in C++:
#include <array>
#include <utility>

int longest_sub(const std::string &s) {
    std::array<int, 256> last_positions;
    last_positions.fill(-1);

    int min_position = 0;
    int maximum_length = 0;

    for (size_t i = 0; i < s.size(); ++i) {
        int &last_position = last_positions[static_cast<unsigned char>(s[i])];

        if (last_position >= min_position) {
            // We encountered a duplicate
            min_position = last_position + 1;
        }

        maximum_length = std::max(maximum_length, int(i + 1 - min_position));
        last_position = i;
    }

    return maximum_length;
}


Order array of objects by date property Here's a function that sorts an array of objects with a dateProp by the date. First it uses moment to convert the Date object to a unix timestamp.

var _ = require('lodash')
var moment = require('moment')

util.orderByDate = function (arr, dateProp) {
  if (!dateProp) return arr
  return _.chain(arr)
  .map(function (item) {
    item[dateProp] = moment(item[dateProp]).format('x')
    return item
  })
  .sortBy(dateProp)
  .value()
}

Firstly, Date objects can be compared directly.

Secondly, even if you did need to convert to something numeric, Date already has a getTime method, which returns an Epoch timestamp with milliseconds.

Thirdly, your function has serious side-effects. It overwrites a property on every object - which there's really no call for.

And lastly, you're missing semicolons, and should indent the chained function calls.

You should be able to get by with just:

var sorted = _.sortBy(arr, dateProp);

Or, without any libraries whatsoever:

function orderByDate(arr, dateProp) {
  return arr.slice().sort(function (a, b) {
    return a[dateProp] < b[dateProp] ? -1 : 1;
  });
}

Of course, if your dates are strings, you may need moment.js for that (though JS's Date can parse some stuff on its own). But in that case, you should probably also supply moment.js with the format it's supposed to parse, or at least have it as an optional argument.


Simple puts() function in x64 assembly I'm trying to write as simple I/O library in x64 using linux syscalls

section .text
strlen:
  xor rdx, rdx
  .loop:
  cmp [rsi + rdx], 0
  je .exit
  inc rdx
  jmp .loop
  .exit:
  ret ; value in rdx

puts:
  ; string passed through rsi
  mov rax, 1
  call strlen
  syscall

Your loop uses 2 jumps (je/jmp) on every iteration!  Jumping is expensive, so a solution that needs only 1 jump (jne) will be more effective.

strlen:
  xor rdx, rdx
  dec rdx          ; This compensates for the INC that is happening first.
.next:
  inc rdx
  cmp byte [rsi + rdx], 0
  jne .next
  ret

Do keep things logically together. There's no point in setting RAX before the call to strlen.

puts:
  ; string passed through rsi
  call strlen      ; Result is in RDX

  mov rax, 1
  syscall


Populating a ComboBox from a Range I recently had quite a lot of fun answering a Stack Overflow question, and I think I've gone a bit overboard and ended up with a fairly decent way of populating a ComboBox from a Range.

Given a simple UserForm featuring some ComboBox1 control that I'd want to populate from a ListObject on Sheet1, the calling/client code looks like this:

Option Explicit

Sub Test()
    With New UserForm1
        .PopulateFromList Sheet1.ListObjects(1)
        .Show vbModal
    End With
End Sub

(thanks to Thomas Inzina for helping with the column headings)

...the Change handler in the form's code is just to illustrate that the hidden ID column is effectively being used as the Combobox.Value:

Private Sub ComboBox1_Change()
    If Not IsNull(ComboBox1.Value) Then Debug.Print ComboBox1.Value, ComboBox1.Text
End Sub

And it works!

 3            Star Wars
 1            Lord of the Rings

Here's the rest of the form's code-behind, the actual code under review - I'm wondering what's the best way to reuse it, I haven't decided whether it's best to extract it into a class module as a utility; with a WithEvents foo As ComboBox field I could be having a dynamic control there... it's certainly not practical in the code-behind of some random form though.

I'm not very happy with GetColumnWidths, I'm sure there's a better way to do this. As for the PopulateFromXxxx methods, ...they're perfect, aren't they? ;-)

Option Explicit

Public Sub PopulateFromList(ByVal source As ListObject, Optional ByVal valueColumn As Long = 1, Optional ByVal hasHeader As Boolean = True)
    With Me.ComboBox1
        .ColumnCount = source.Range.Columns.Count
        .ColumnWidths = GetColumnWidths(source.Range)
        .ListWidth = IIf(ComboBox1.Width > source.Range.Width, ComboBox1.Width, source.Range.Width)
        .RowSource = source.Name & "[#Data]"
        .BoundColumn = valueColumn
        .ColumnHeads = hasHeader
    End With
End Sub

Public Sub PopulateFromArray(ByVal source As Range, Optional ByVal valueColumn As Long = 1, Optional ByVal hasHeader As Boolean = True)
    With Me.ComboBox1
        .ColumnCount = source.Columns.Count
        .ColumnWidths = GetColumnWidths(source)
        .ListWidth = IIf(ComboBox1.Width > source.Width, ComboBox1.Width, source.Width)
        .List = source.Range(source.Rows(IIf(hasHeader, 2, 1)).EntireRow, source.Rows(source.Rows.Count).EntireRow).Value
        .BoundColumn = valueColumn
    End With
End Sub

Private Function GetColumnWidths(ByVal source As Range) As String
    Dim cols As Long
    cols = source.Columns.Count

    Dim widths()
    ReDim widths(1 To cols)
    Dim col As Long
    For col = 1 To cols
        widths(col) = source(, col).Width
    Next
    GetColumnWidths = Join(widths, ",")
End Function

Is there any way to clean this up? The .List assignment in PopulateFromArray seems particularly painful.

I'm not sure I'd compare the .width properties of a control and a range - hear me out.

Let's start with a table of relevant measurements

Inch       .5
cm         1.27
Pixel      48
Point      36
Characters 5.38

With a Range

In the excel UI you see two numbers - characters and pixels when manually adjusting width.

Characters being the number of characters with the font (I'm using default here- calibri 11) that can fit in the width of the range (yes, that's a ridiculous measurement unit).

In the Format menu for a range, the Column Width is also characters.

In VBA

Range.Width = points (Variant Double)
Range.ColumnWidth = characters (Variant Double)

.width here is giving us points which then must be divided by .75 to get to pixels to match the UI.

With a Form

The properties of an active x combobox on a form are

ComboBox1.ColumnWidths = points (String)
ComboBox1.Width = pixels (Single)
ComboBox1.ListWidth = points (String)

In the code

.ColumnWidths = GetColumnWidths(source.Range)
.ListWidth = IIf(ComboBox1.Width > source.Range.Width, ComboBox1.Width, source.Range.Width)

Essentially you set .ColumnWidths (String in points) with a String representing points. Nice.

Then you compare pixels to points and set ListWidth (String in points) with either pixels (as Single) or points (Variant Double).

There's no avoiding this mess, so I would break out another function that returns a String of points.

Yes, it's needlessly complicated to do so, unless someone is trying to debug some more complex version of this or you're like me and you hate that excel has done this.

    Dim myControlBox As Control
    Set myControlBox = Me.ComboBox1
    With myControlBox
        .ColumnCount = source.Columns.Count
        .ColumnWidths = GetColumnWidths(source)
        .ListWidth = GetListWidth(source, myControlBox)
    End With

Private Function GetListWidth(ByVal source As Range, ByVal myControlBox As Control) As String
    If myControl.Width > source.Width Then
        GetListWidth = myControl.Width * 0.75
        Exit Function
    Else: GetListWidth = source.Width
End Function

This answer took me longer to write that I'm willing to admit.


Given 4 vertices representing a quadrilateral, divide it into N parts I am attempting to, given two dimensions (width and height) which represent a quadrilateral, partition that quad in to N parts where each part is as proportionally similar to the other as possible.

For example, imagine a sheet of paper. It consists of 4 points A, B, C, D. Now consider that the sheet of paper has the dimensions 800 x 800 and the points:

A: {0, 0}
B: {0, 800}
C: {800, 800}
D: {800, 0}

Plotting that will give you 4 points, or 3 lines with a line plot. Add an additional point E: {0, 0} to close the cell.

To my surprise, I've managed to do this programatically, for N number of cells.

How can I improve this code to make it more readable, pythonic, and as performant as possible?

from math import floor, ceil
import matplotlib.pyplot as plt

class QuadPartitioner:

    @staticmethod
    def get_factors(number):
        '''
        Takes a number and returns a list of factors
        :param number: The number for which to find the factors
        :return: a list of factors for the given number
        '''
        facts = []
        for i in range(1, number + 1):
            if number % i == 0:
                facts.append(i)
        return facts

    @staticmethod
    def get_partitions(N, quad_width, quad_height):

        '''
        Given a width and height, partition the area into N parts
        :param N: The number of partitions to generate
        :param quad_width: The width of the quadrilateral
        :param quad_height: The height of the quadrilateral
        :return: a list of a list of cells where each cell is defined as a list of 5 verticies
        '''

        # We reverse only because my brain feels more comfortable looking at a grid in this way
        factors = list(reversed(QuadPartitioner.get_factors(N)))

        # We need to find the middle of the factors so that we get cells
        # with as close to equal width and heights as possible

        factor_count = len(factors)

        # If even number of factors, we can partition both horizontally and vertically.
        # If not even, we can only partition on the X axis
        if factor_count % 2 == 0:
            split = int(factor_count/2)
            factors = factors[split-1:split+1]
        else:
            factors = []
            split = ceil(factor_count/2)
            factors.append(split)
            factors.append(split)

        # The width and height of an individual cell
        cell_width = quad_width / factors[0]
        cell_height = quad_height / factors[1]

        number_of_cells_in_a_row = factors[0]
        rows = factors[1]
        row_of_cells = []

        # We build just a single row of cells
        # then for each additional row, we just duplicate this row and offset the cells
        for n in range(0, number_of_cells_in_a_row):
                cell_points = []

                for i in range(0, 5):

                    cell_y = 0
                    cell_x = n * cell_width

                    if i == 2 or i == 3:
                        cell_x = n * cell_width + cell_width

                    if i == 1 or i == 2:
                        cell_y = cell_height

                    cell_points.append((cell_x, cell_y))

                row_of_cells.append(cell_points)

        rows_of_cells = [row_of_cells]

        # With that 1 row of cells constructed, we can simply duplicate it and offset it
        # by the height of a cell multiplied by the row number
        for index in range(1, rows):
            new_row_of_cells = [[ (point[0],point[1]+cell_height*index) for point in square] for square in row_of_cells]
            rows_of_cells.append(new_row_of_cells)

        return rows_of_cells

if __name__ == "__main__":

    QP = QuadPartitioner()
    partitions = QP.get_partitions(56, 1980,1080)

    for row_of_cells in partitions:
        for cell in row_of_cells:
            x, y = zip(*cell)
            plt.plot(x, y, marker='o')

    plt.show()

Output:

First, there is no need for this to be a class at all. Your class has only two static methods, so they might as well be stand-alone functions. In this regard Python is different from e.g. Java, where everything is supposed to be a class.

Your get_factors function can be sped-up significantly by recognizing that if k is a factor of n, then so is l = n / k. This also means you can stop looking for factors once you reach \$\sqrt{n}\$, because if it is a square number, this will be the largest factor not yet checked (and otherwise it is an upper bound). I also used a set instead of a list here so adding a factor multiple times does not matter (only relevant for square numbers, again).

from math import sqrt

def get_factors(n):
    '''
    Takes a number and returns a list of factors
    :param number: The number for which to find the factors
    :return: a list of factors for the given number
    '''
    factors = set()
    for i in range(1, int(sqrt(n)) + 1):
        if n % i == 0:
            factors.add(i)
            factors.add(n // i)
    return list(sorted(factors))  # sorted might be unnecessary

As said, this is significantly faster than your implementation, although this only starts to be relevant for about \$n > 10\$.

(Note the log scale on both axis.)

As for your main function:

First figure out how many rows and columns you will have. For this I would choose the factor that is closest to \$\sqrt{n}\$:

k = min(factors, key=lambda x: abs(sqrt(n) - x))
rows, cols = sorted([k, n //k])   # have more columns than rows

Then you can use numpy.arange to get the x- and y-coordinates of the grid:

x = np.arange(0, quad_width + 1, quad_width / cols)
y = np.arange(0, quad_height + 1, quad_height / rows)

From this you now just have to construct the cells:

def get_cells(x, y):
    for x1, x2 in zip(x, x[1:]):
        for y1, y2 in zip(y, y[1:]):
            yield [x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1]

Putting all of this together:

import numpy as np

def get_partitions(n, width, height):
    factors = get_factors(n)
    k = min(factors, key=lambda x: abs(sqrt(n) - x))
    rows, cols = sorted([k, n //k])   # have more columns than rows

    x = np.arange(0, width + 1, width / cols)
    y = np.arange(0, height + 1, height / rows)

    yield from get_cells(x, y)

if __name__ == "__main__":
    for cell in get_partitions(56, 1980, 1080):
        plt.plot(*cell, marker='o')
    plt.show()


Music management app, mashing data from various APIs I am basically getting data from various APIs and using PHP to put them together - like a web mashup. I am currently using 4 foreach statements to insert the gathered data into their individual arrays. I believe that the current code is inefficient because it takes probably around 3 seconds to load the page which is displaying the PHP data. In the past I had just one big foreach loop to go through all the data at once and also print them. But that too felt inefficient to me.

So how can I make my code more efficient in term of it processing faster? I have seen a few mashup websites such as Soundeer which load around a second. Is that becuase of their code efficiency?

The code which I am using is:

$echonest_uri = simplexml_load_file("http://developer.echonest.com/api/v4/artist/search?api_key=$APIkey&style=rap&results=10&start=$element_num&bucket=id:deezer&bucket=images&sort=familiarity-desc&format=xml");

//Swap the comments for when in UWE or not
//$echonest_xml =  new SimpleXMLElement($echonest_uri);
$echonest_xml = $echonest_uri;

$artist_name = array();
$artist_image = array();
$echonest_id = array();
$full_deezer_id = array();
$deezer_id = array();
$num_of_albums = array();

//Loop through each entries in the id_arr and make each image of the artist a link to the album page passing all the relevant information.
foreach($echonest_xml->artists->artist as $artist){
    $artist_name[] = $artist->name;
    $artist_image[] = $artist->images->image[0]->url;
    $echonest_id[] = $artist->id;
    $full_deezer_id[] = $artist->foreign_ids->foreign_id->foreign_id;
}

foreach($full_deezer_id as $key => $value){
    preg_match('#deezer:artist:([A-Z,a-z,0-9]+)#', $value, $id);
    $deezer_id[] = (string)$id[1];
}

foreach($deezer_id as $id_index => $id){
    $deezer_xml = simplexml_load_file("http://api.deezer.com/2.0/artist/$id/albums&output=xml");
    $num_of_albums[] = $deezer_xml->total;
}

//The variable which will contain the HTML code to display the artists.
$output = null;

foreach($deezer_id as $key => $value){

    $fav_count_query = "SELECT COUNT(user_index) FROM fav_artist WHERE artist_deezer_id = '$value'";
    $fav_count_resource = $mysqli->query($fav_count_query);
    $fav_count = $fav_count_resource->fetch_assoc();

    $output .=  <<<HERE

                        {$artist_name[$key]}

                    Albums:
                        {$num_of_albums[$key]}

HERE;

}

Please add the following method do your code and print the runtime of every section of your code. As indicated by my comment I guess the foreach->simplexml_load_file("...") might take a while.

//PHP <5.0.0
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$start=microtime_float();
//...
echo microtime_float()-$start;

//PHP>=5.0.0
$start=microtime(true);
//...
echo microtime(true)-$start;


Finding Kth Permutation Sequence I have written a program to display the kth permutation sequence of a string made up of letters 'O' and 'Z' . I tried optimizing it but my code have not passed test cases due to timeout issues.Looking for someone who can guide me in optimizing the code i posted below.

public static void main(String[] args) throws IOException {

    /*
     * Sample Input
     * 1
     * 3 2 // Passing two inputs as single string
     *
     * Sample Output
     * OOZ
     */

    // Scanner in = new Scanner(System.in);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = br.readLine();
    // Getting 2 integer inputs with space
    String[] k1 = br.readLine().split("\\s");

    // String b1 = br.readLine();
    String eol = System.getProperty("line.separator");
    // byte[] eolb = eol.getBytes();

    long t = Integer.parseInt(line);

    long k = Integer.parseInt(k1[0]);
    long b = Integer.parseInt(k1[1]);
    char set1[] = { 'O', 'Z' };
    ArrayList<String> prefixlist = new ArrayList<String>();
    printAllKLength(set1, k, prefixlist, b);

}

static void printAllKLength(char set[], long k,
        ArrayList<String> prefixlist, long b) throws IOException {
    int n = set.length;
    BufferedOutputStream bout = new BufferedOutputStream(System.out);
    printAllKLengthRec(set, "", n, k, prefixlist);
    Collections.sort(prefixlist);
    if (b <= prefixlist.size()
            && prefixlist.contains(prefixlist.get((int) (b + 1)))) {

        byte b1[] = String.valueOf(prefixlist.get((int) (b - 1)))
                .getBytes();
        bout.write(b1);
        bout.write(System.lineSeparator().getBytes());
        bout.flush();
        // System.out.println(prefixlist.get((int) (b - 1)));
    } else {
        byte b1[] = "-1".getBytes();
        bout.write(b1);
        bout.write(System.lineSeparator().getBytes());
        bout.flush();
    }
}

static void printAllKLengthRec(char set[], String prefix, int n, long k,
        ArrayList<String> prefixlist) {

    if (k == 0) {

        if (!prefix.contains("ZZ")) {

            prefixlist.add(prefix);
        }

        return;
    }

    for (int i = 0; i < n; ++i) {

        String newPrefix = prefix + set[i];

        printAllKLengthRec(set, newPrefix, n, k - 1, prefixlist);
    }

}

You are hitting the time limit as you are supposed to only find the k-th permutation, but you are generating all permutations instead. Look at this discussion. You will probably have to replace the term (numberOfCharacters!) with (numberOfCharacters!/countO!/countZ!) as explained here.


Using os.scandir to get all txt files in subfolders I have a parent dir that has X children dirs each with Y txt files. I want to list all txt files. Is there a way to simplify this?

for entry in os.scandir(DIR):
        for subentry in os.scandir(entry.path):
            if subentry.is_dir():
                for file in os.scandir(subentry.path):
                    if file.is_file() and file.name.endswith("txt"):
                        print(file.name)

There are (at least) two ways to easily achieve this. The first one is what @Ludisposed suggested in the comments, glob. It can also recurse down into subdirectories with ** (in Python 3):

import glob

def get_txt_files(base_dir):
    return glob.iglob(f"{base_dir}/**/*.txt", recursive=True)

Or, if you are using Windows, which uses different delimiters for paths:

def get_txt_files(base_dir):
    return glob.iglob(rf"{base_dir}\**\*.txt", recursive=True)

The other one is basically what @Mast suggested, also in the comments, make this a fully recursive function:

def get_txt_files(base_dir):
    for entry in os.scandir(base_dir):
        if entry.is_file() and entry.name.endswith(".txt"):
            yield entry.name
        elif entry.is_dir():
            yield from get_txt_files(entry.path)
        else:
            print(f"Neither a file, nor a dir: {entry.path}")

Both functions return iterators. You can print all names with a simple for loop:

for name in get_txt_files("foo"):
    print(name)

If your directory X also contains txt files, they will be yielded from these functions. You can avoid this with glob by making the depth level explicit:

def get_txt_files(base_dir):
    return glob.iglob(f"{base_dir}/*/*.txt")

A third, very similar, way is to use pathlib.Path (Python 3 only), which also has a glob method (that directly returns an iterator). This way you can be sure that this works both on UNIX and Windows:

from pathlib import Path

def get_txt_files(base_dir):
    return Path(base_dir).glob("*/*.txt")


Array is Balanced Array or Not A balanced array is defined to be an array where for every value n in the array, -n also is in the array.

{-2, 3, 2, -3} is a balanced array.
So is {-2, 2, 2, 2}. But {-5, 2, -2} is not because 5 is not in the array.

I wrote the following class BalancedArray which satisfied above condition.

public class BalancedArray {
public static void main(String args[]) {
    System.out.println("The result is: " + isBalanced(new int[]{-2, 3, 2, -3}));
}
public static boolean isBalanced(int[] a){
    boolean status = false;
    for(int i = 0; i < a.length; i++) {
        if(a[i] > 0) {
            for(int j = i+1; j < a.length; j++) {
                if(a[i] == Math.abs(a[j])) {
                    status  =  true;

                }
            }

        } else if(a[i] < 0) {
            for(int k = i+1; k < a.length; k++) {
                if(Math.abs(a[i]) == a[k]) {
                    status = true;

                }
            }

        }
        System.out.println(status);
      if(status) {
          status= true;
      } else {
          status  = false;
          break;
      }
    }

    return status;
}
}

Is this proper way to check Balanced array or I am missing some conditions to check in array??

An alternative way could be using a map, the average lookup time should be O(1) so this algorithm would be O(n) on average.

public static boolean isBalanced(int[] a)
{
    Map<Integer, Integer> map = new HashMap<>();
    for(int number : a)
    {
        int key = Math.abs(number);
        Integer value = map.get(key);

        if(value == null)
            map.put(key, number);
        else if (value != number)
            map.put(key, 0);
    }
    for (int v : map.values())
    {
        if(v != 0) return false;
    }

    return true;
}

A more readable version in java 8 ( Autoboxing and unboxing may hurt the performance a lot thought.)

 public static boolean isBalanced(int[] a)
 {

       Map<Integer, Integer> map = Arrays.stream(a)
                                         .boxed()
                                         .collect(Collectors.toMap(
                                                 Math::abs,
                                                 Function.identity(),
                                                 (x, y) -> x.equals(y)? x: 0 )
                                         );
        return map.values()
                  .stream()
                  .allMatch(p -> p == 0);
 }


Complete graph traversal algorithm in Prolog Given a table, I want to explore all possible transition between the elements in the table.
ex: for a table with size 3 [0,1,2], the output of the algorithm should be 0->1, 1->0, 0->2, 2->1, 1->2, 2->0.
I guess this can be regarded as traversing a complete graph.

I have an implementation of an algorithm doing this in Python:

def test1(n):
   theList=[]
   theList.append(0)

   for x in range(0,n+1):
      for y in range(n,x+1,-1):
         theList.append(y)
         theList.append(x)

      if x!=n:
         theList.append(x+1)

   for x in range(n,0,-1):
      theList.append(x-1)
   return theList

This code always start at element 0, and return a list of all the transitions.

But I need my algorithm i Prolog. So I have done an attempt to port the Python-code to Prolog. My main focus has been on writing readable and maintainable code. But I guess there is great room for improvement wrt. performance of the Prolog code:

:- use_module(library(clpfd)).
:- use_module(library(between)).
:- use_module(library(lists)).

dynappend( List, X ) :-
   (
   foreach( Item, List ),
   param(X,T)
   do
      var( Item ), var(T) ->
      Item = X ,
      T = 1
      ;
      true
   ).

s3(List,N):-
   LSize is N*(N+1)+1,
   length(List,LSize),

   dynappend( List, 0 ),
   (
      for(X1, 0, N ),
      param( N, List )
      do
         X1T is X1+2,
         ( between:numlist( X1T, N, YList ) -> true ; YList=[] ) ,
         lists:reverse(YList,RYList),

         (
            foreach(Y, RYList ),
            param( X1, List )
            do
               dynappend( List, Y ),
               dynappend( List, X1 )
         ),

         (   X1 #\= N -> X1T2 is X1+1, dynappend( List, X1T2 ) ;  true )
   ),
   N1 is N-1,
   numlist( 0, N1, XList ),
   lists:reverse(XList,RXList),
   (
   foreach(X2, RXList ),
   param(List)
   do
      dynappend( List, X2 )
   ).

Running the code:

| ?- s3(L, 2).
L = [0,2,0,1,2,1,0] ?
yes

Any suggestions for improvements of the Prolog code?

Consider describing what a transition is:

list_transition(List, (E1->E2)) :-
        select(E1, List, Rest),
        member(E2, Rest).

Example query:

?- list_transition([0,1,2], T).
T = (0->1) ;
T = (0->2) ;
T = (1->0) ;
T = (1->2) ;
T = (2->0) ;
T = (2->1) ;
false.

And to get all transitions:

?- findall(T, list_transition([0,1,2], T), Transitions).
Transitions = [ (0->1), (0->2), (1->0), (1->2), (2->0), (2->1)].


Lombokython - Automatic __eq__, __hash__, __repr__ I recently decided to code some in Python after coding in Java using lombok for quite some time. However, I got bit real hard when I forgot to implement __eq__, since Lombok normally does it for you.

I decided to try to implement something similar to Lombok's @EqualsAndHashCode, which I called eqhash. Then, I added a similar way to generate a __repr__ method.

How well did I follow python style? I developed this using TDD, so I would like to know how my tests are as well.

genmethods.py

def eqhash(cls: type):
    """Adds __eq__, __ne__, and __hash__ methods to the class"""
    class NewCls(cls):
        def __eq__(self, other):
            if self is other: return True
            if not isinstance(other, NewCls): return False
            svars = vars(self)
            ovars = vars(other)
            return svars == ovars

        def __ne__(self, other):
            return not (self == other)

        def __hash__(self):
            return hash(vars(self).values())

    return _pass_attrs(NewCls, cls)

def repr_(cls: type):
    """Adds a __repr__ method to the class"""
    class NewCls(cls):
        def __repr__(self):
            return '{clsname}({variables})'.format(
                clsname=cls.__name__,
                variables=', '.join(repr(o) for o in vars(self).values())
            )

    return _pass_attrs(NewCls, cls)

def _pass_attrs(new_cls, cls):
    new_cls.__name__ = cls.__name__
    new_cls.__qualname__ = cls.__qualname__
    for attr, value in vars(cls).items():
        try:
            setattr(new_cls, attr, value)
        except AttributeError:
            pass

    return new_cls

test_genmethods.py

import unittest
import genmethods

class SData:
    def __init__(self, value):
        self._value = value

    def test(self):
        return self._value

def ensure_not_mangled(self: unittest.TestCase, cls: type, name: str):
    self.assertEqual((1, 2), cls((1, 2)).test())
    self.assertEqual(name, cls.__name__)

class EqHashTest(unittest.TestCase):
    @genmethods.eqhash
    class Data(SData):
        pass

    def test_eq_genned(self):
        self.assertEqual(EqHashTest.Data((1, 2)), EqHashTest.Data((1, 2)))
        self.assertNotEqual(EqHashTest.Data((1, 2)), EqHashTest.Data((1, 4)))

    def test_hash_genned(self):
        v1 = EqHashTest.Data(1)
        v2 = EqHashTest.Data(1)
        self.assertEqual(hash(v1), hash(v2))

    def test_not_mangled(self):
        ensure_not_mangled(self, EqHashTest.Data, "Data")

class ReprTest(unittest.TestCase):
    @genmethods.repr_
    class Data(SData):
        pass

    def test_repr_genned(self):
        self.assertEqual('Data(1)', repr(ReprTest.Data(1)))

    def test_not_mangled(self):
        ensure_not_mangled(self, EqHashTest.Data, "Data")

class ReprEqHashTest(unittest.TestCase):
    @genmethods.repr_
    @genmethods.eqhash
    class Data(SData):
        pass

    def test_eq_genned(self):
        self.assertEqual(ReprEqHashTest.Data((1, 2)), ReprEqHashTest.Data((1, 2)))
        self.assertNotEqual(ReprEqHashTest.Data((1, 2)), ReprEqHashTest.Data((1, 4)))

    def test_hash_genned(self):
        v1 = ReprEqHashTest.Data(1)
        v2 = ReprEqHashTest.Data(1)
        self.assertEqual(hash(v1), hash(v2))

    def test_repr_genned(self):
        self.assertEqual('Data(1)', repr(ReprEqHashTest.Data(1)))

    def test_not_mangled(self):
        ensure_not_mangled(self, EqHashTest.Data, "Data")

class EqHashReprTest(unittest.TestCase):
    @genmethods.eqhash
    @genmethods.repr_
    class Data(SData):
        pass

    def test_eq_genned(self):
        self.assertEqual(ReprEqHashTest.Data((1, 2)), ReprEqHashTest.Data((1, 2)))
        self.assertNotEqual(ReprEqHashTest.Data((1, 2)), ReprEqHashTest.Data((1, 4)))

    def test_hash_genned(self):
        v1 = ReprEqHashTest.Data(1)
        v2 = ReprEqHashTest.Data(1)
        self.assertEqual(hash(v1), hash(v2))

    def test_repr_genned(self):
        self.assertEqual('Data(1)', repr(ReprEqHashTest.Data(1)))

    def test_not_mangled(self):
        ensure_not_mangled(self, EqHashTest.Data, "Data")

1. Review

It would be substantially simpler to use a mixin instead of a decorator.
The test for equality is not symmetric: you have a == b if b belongs to a subclass of a's class, but not vice versa. So it's possible to have a == b but b != a, which makes no sense.

There are two sensible things to do here: either (i) ignore the class relationship, so that two objects compare equal if their vars are equal, regardless of which class they belong to; or (ii) insist that classes match, so that two objects compare equal only if they belong to the same class.
The intention seems to be for eqhash objects to hash based on their instance attributes, so that instances with different attributes get different hashes. But the code doesn't work! Here are two objects with different instance attributes, but whose hashes are the same:

>>> @eqhash
... class Data(SData):
...     pass
>>> d = Data(1)
>>> hash(d)
-9223372036573986785
>>> e = Data(2)
>>> hash(e)
-9223372036573986785

And here is one object that has different hashes at different times:

>>> d = Data(1)
>>> hash(d)
-9223372036573986785
>>> vars(d).values()
dict_values([1])
>>> hash(d)
-9223372036574014153

The problem is that you are constructing a dict_values object and then taking its hash. But dict_values objects don't hash based on their contents, only on their id (see Python issue 22192), so the hash doesn't tell you anything about the values, only about the location in memory of the dict_values object.

2. Revised code

class EqHash:
    """Mixin adding __eq__, __ne__, and __hash__ methods."""
    def __eq__(self, other):
        return (self is other
                or (type(self) == type(other)
                    and vars(self) == vars(other)))

    def __ne__(self, other):
        return not (self == other)

    def __hash__(self):
        return hash(tuple(sorted(vars(self).items())))

class Repr:
    """Mixin adding a __repr__ method."""
    def __repr__(self):
        return '{name}({values})'.format(
            name=type(self).__name__,
            values=', '.join(map(repr, vars(self).values())))

3. Answers to questions

functools.total_ordering is a decorator becuase it looks at the class in order to determine which ordering method was provided. See the implementation, which calls getattr(cls, op, None). It would be much harder to do this in a mixin.
I spotted the problems by thinking carefully!

First, equality is a symmetric relation, but isinstance is asymmetric, so it was suspicious that one was implemented using the other.

Second, it only makes sense to implement __hash__ on immutable objects, but the values of a dictionary are mutable, so this was also suspicious. See the documentation for __hash__, where it says:

  If a class defines mutable objects and implements an __eq__ method, it should not implement __hash__, since the implementation of hashable collections requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).


Streaming scheduled TV shows according to a CSV playlist I'm very new to Python, and I've made a small program/script that will allow me to stream specific files at a given time.

My idea is that I have a .csv file for each day, with one row for each movie/tv show i want to watch and a cron job that checks every 30 minutes (at :00 and :30). If the job finds any row that matches the current time, then it will play the file given in the row.

As it is now, it has been configured to my installation of Mac OS High Sierra, using VLC as player. Since i've split the config out to a seperate file, I'm hoping it will be easy to setup on another environment (e.g. a Raspberry Pi)

I've split things into three seperate files (all located in the same folder), one of the files being a config file.

cron.py is the file, that is executed by the cron job.

import tvshow
import config

tvShowFile = tvshow.getTodaysFilename()
time = tvshow.getTime()
print ("Scanning file %s for %s" % (tvShowFile, time))
row = tvshow.scanFileForTime(tvShowFile, time)

if(row):
    urlColIndex = config.csvIndexes['url']
    streamUrl = row[urlColIndex]
    print("Executing app:" + config.apppath)
    print("URL:" + streamUrl)
    try:
        tvshow.show(streamUrl)
    except Exception as e:
        print ("Something went wrong:" + str(e))
else:
    print("No show at time %s" % time)

tvshow.py is the file with all the actual stuff

from datetime import datetime
import config
import csv
import config
from subprocess import call

def getTodaysFilename():
    filename = datetime.now().strftime(config.fileTimeFormat)
    return config.filepath + "/" + filename + "." + config.extension

def getTime():
    return datetime.now().strftime(config.hourMinuteFormat)

def getFileContent(filename):
    lines = []
    with open(filename) as csvfile:
        csvContent = csv.reader(csvfile, delimiter = ',')
        for row in csvContent:
            lines.append(row)
    return lines

def scanFileForTime(filename, time):
    timeColIndex = config.csvIndexes['time']
    content = getFileContent(filename)
    for row in content:
        if(row[timeColIndex] == time):
            return row

def compileArguments(streamUrl):
    arguments = []
    arguments.append(config.apppath)
    arguments.append(streamUrl)
    for item in config.appArguments:
        arguments.append(item)
    return arguments

def show(streamUrl):
    arguments = compileArguments(streamUrl)
    call(arguments)

config.py is the configuration

apppath = '/Applications/VLC.app/Contents/MacOS/VLC'
filepath = '/Users/Shared/tvshow'
fileTimeFormat = "%Y_%m_%d"
hourMinuteFormat = "%H:%M"
extension = 'csv'
csvIndexes = dict(
    time = 0,
    title = 1,
    url = 2
)
appArguments = [
    '--fullscreen',
    '--no-loop',
    '--play-and-exit'
]

Tv show listings sample file - The "title" is not used for anything and could be left out.

"19:47","Big Buck Bunny","http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4"

Any feedback on the code would be appreciated.

I'd start with a few specific points for the code style.

Consistent naming. You are mixing snake_case and camelCase. Check the section on naming conventions.
Whitespaces around keyword arguments is advised against.
Try to use str.format() or str % () modes to insert variables into strings, preferring the .format over % style. If you do decide to switch to python 3.6+ (as commented), you also have f"" (format-type strings).
Order your imports in the following suggested order:

Standard library imports.
Related third party imports.
Local application/library specific imports.

Now, to some changes you can do for optimisations, maintainability and portability.

Your entire cron.py is redundant. Since everything it does depends only on the tvshow.py, switch to only using that file. Place the code currently in cron.py inside the if __name__ == "__main__" block.
You're trying to import config twice.

import config
import csv
import config

You can have a class TVSchedule(r), which reads the config file when initialising, and sets up the file_name, time, app_path beforehand.
The performance of scanFileForTime and getFileContent will get better if the latter yields the rows :)
Make use of .extend method; so that the following:

for item in config.appArguments:
    arguments.append(item)

becomes

arguments.extend(config.appArguments)