Python logging handler I'm working on a logger that has a name of the module that called the logger (when I create an instance of a logger in my program, I call LoggingHandler(__name__)) to send all the messages, including info and debug, to the log file, and print the messages specified by max_level to console (so, by default, it will not print info and debug messages to console, but will still write them into file).

The problem came when I was managing levels. If I set level in basicConfig to "WARNING", then it will not print info and debug to file, even though I've set fh.setLevel(logging.DEBUG). It just won't go to levels lower than the one specified in basicConfig. Okay, I could just go ahead and specify filename in basicConfig to make it output to file, but I want a RotatingFileHandler to take care of it (because I require its rollover functionality). So, I've set level in basicConfig to "NOTSET", the lowest one possible. Things go better now except one problem. The output to console doubles. It prints

[2016-08-29 10:58:20,976] __main__: logging_handler.py[LINE:51]# WARNING   hello
[2016-08-29 10:58:20,976] __main__: logging_handler.py[LINE:51]# WARNING   hello
[2016-08-29 10:58:20,977] __main__: logging_handler.py[LINE:48]# ERROR     hola
[2016-08-29 10:58:20,977] __main__: logging_handler.py[LINE:48]# ERROR     hola
[2016-08-29 10:58:20,977] __main__: logging_handler.py[LINE:54]# INFO      info message
[2016-08-29 10:58:20,977] __main__: logging_handler.py[LINE:57]# DEBUG     debug message

So, the global logger does the output and the StreamHandler does. I need to prevent the global logger from outputting anything. So I redirect its output to a "dummy" class Devnull. Now the code works exactly as I need, but it feels like such approach is what they call "bodging". So, I'd like to know if there's a better way to write this code.

#!/usr/bin/python3 -u
# -*- coding: utf-8 -*-
import logging
from logging.handlers import RotatingFileHandler
from os import path, makedirs

LOGS_DIR = "logs"
DEBUG_FILE_NAME = 'debug.log'
MAX_LOG_SIZE = 10*1024*1024
BACKUP_FILES_AMOUNT = 3
LOG_FORMAT = u'[%(asctime)s] %(name)s: %(filename)s[LINE:%(lineno)d]# %(levelname)-8s  %(message)s'

class Devnull(object):
    def write(self, *_, **__): pass

class LoggingHandler:
    def __init__(self, logger_name, max_level="WARNING"):
        makedirs(LOGS_DIR, exist_ok=True)

        logging.basicConfig(format=LOG_FORMAT,
                            level="NOTSET",
                            stream=Devnull(),
                            )

        self.main_logger = logging.getLogger(logger_name)

        # create file handler which logs even debug messages
        fh = RotatingFileHandler(path.join(LOGS_DIR, DEBUG_FILE_NAME),
                                 maxBytes=MAX_LOG_SIZE, backupCount=BACKUP_FILES_AMOUNT)
        fh.setLevel(logging.DEBUG)
        # create console handler with a higher log level
        ch = logging.StreamHandler()
        ch.setLevel(max_level)
        # create formatter and add it to the handlers
        fmter = logging.Formatter(LOG_FORMAT)
        fh.setFormatter(fmter)
        ch.setFormatter(fmter)
        # add the handlers to the logger
        self.main_logger.addHandler(fh)
        self.main_logger.addHandler(ch)

    def error(self, message):
        self.main_logger.error(message)

    def warning(self, message):
        self.main_logger.warning(message)

    def info(self, message):
        self.main_logger.info(message)

    def debug(self, message):
        self.main_logger.debug(message)

if __name__ == '__main__':
    # Tests
    log = LoggingHandler(__name__)
    log.warning("hello")
    log.error("hola")
    log.info("info message")
    log.debug("debug message")

You simply need to understand what's going on when you write:

    logging.basicConfig(format=LOG_FORMAT,
                        level="NOTSET",
                        stream=Devnull(),
                        )

This creates a root logger with level NOTSET and a default handler which won't log anything thanks to Devnull. I think this also has the added benefit of settings the level of all future loggers to NOTSET.

But you don't want a root logger! You're defining your own logger with your own handlers and so on. So basicConfig() is doing more harm than good here. Remove it!

Now there's one remaining issue: if you do this, you won't see debug logs, because the default log level is WARNING. So you need to add self.main_logger.setLevel(logging.DEBUG): the logger will accept all log levels, and each handler will decide whether or not to process the log record depending on the log level of the record and its own log level. (In other words, you're back to the original code you used which worked very well.)


Building database from file As the question reads, I am building a database with 3 tables. Now these tables are going to be used to store names.

Table 1 will store First Names
Table 2 will store Last Names
Table 3 will be a one to one table linking the First names to the Last names

Now all this data will be coming from a text file in the format of:

  Firstname MI Lastname

I estimate that their will be over 100 million records. I also don't want to do a insert for every name it will be an update record on duplicate Key. So what is the most optimal way to enter this into my database. By the way it is a Innodb so the whole table gets locked so I can't do multiple insert updates at a time

Now this whole process will be done via C# using mysql connection and I have:

sqlRequest += "START TRANSACTION ;" +
    "UPDATE firstName SET LastUpdated = CURRENT_TIMESTAMP WHERE first= '" + countSplit[0] +"' ;" +
    "INSERT INTO firstName(first, LastUpdated) SELECT '" + countSplit[0] + "' AS first, CURRENT_TIMESTAMP AS LastUpdated FROM dual WHERE NOT EXISTS ( SELECT * FROM firstName d WHERE d.first= '" + countSplit[0] + "') ;" +
    "COMMIT ;";
sqlRequest += "START TRANSACTION ;" +
    "UPDATE lastName SET LastUpdated = CURRENT_TIMESTAMP WHERE last = '" + countSplit[2] + "' ;" +
    "INSERT INTO lastName (last, LastUpdated) SELECT '" + countSplit[2] + "' AS last, CURRENT_TIMESTAMP AS LastUpdated FROM dual WHERE NOT EXISTS ( SELECT * FROM lastName d WHERE d.last = '" + countSplit[2] + "') ;" +
    "COMMIT ;";
sqlRequest += "START TRANSACTION ;" +
    "INSERT INTO first_to_last " +
    "(firstid,lastid,LastUpdated) VALUES "+
    "((SELECT firstid FROM firstName WHERE first='" + countSplit[0] + "')," +
    "(SELECT lastid FROM lastName WHERE last='" + countSplit[2] + "' )," +
    "CURRENT_TIMESTAMP)"+
    "ON DUPLICATE KEY UPDATE LastUpdated = CURRENT_TIMESTAMP;"+
    "COMMIT ;";

So do you think that this is the best way? Or do you think there is something better?

From the looks of this, you should be using a Stored Procedure, not entirely familiar with how that works in MySQL but I imagine it is similar to SQL Server Stored Procedures.

Using a Stored Procedure would be the easiest way to do this.

You create the stored procedure with the parameters that you need, then in the C# you call the stored procedure giving it the parameters in the specified manner.  I haven't tried to do with with MySQL but I imagine it is much the same as with SQL Server ( I know I said that once already )

You are trying to do the Database's work with C#,  keep them separate.

Cleanse the input, create a command for the Stored Procedure, give it the parameters, execute the command.

This way you can loop through a list of names in C# sending the names to the stored procedure, or create the stored procedure to accept a list of names.

Creating the Stored Procedure will also allow you to use it other places in the C# application, and if you need to change the Query, it is easily maintainable, you change the query and don't have to recompile the application as long as the input stays the same.


Recursively convert a list of lists into a dict of dicts As a challenge to myself, I wrote a solution to this Stack Overflow question. For completeness, the bulk of the question is as follows:

Problem

Input:

[
  ['key1', 'value1'],
  ['key2',
     [
       ['key3', 'value3'],
       ['key4',
          [
            ...
          ]
       ],
       ['key5', 'value5']
     ]
  ],
  ['key6', 'value6'],
]

Ouput:

{
  "key1": "value1",
  "key2": {
     "key3": "value3",
     "key4": ...,
     "key5": "value5"
  },
  "key6": "value6"
}

Solution

Please note that this is my first attempt.

import pprint

def main():
    lt = [ \
            ['key1', 'value1'], \
            ['key2', \
                [ \
                    ['key3', 'value3'], \
                    ['key4',  \
                        ['key7','value7'] \
                    ], \
                    ['key5', 'value5'] \
                ] \
            ], \
            ['key6', 'value6'] \
        ]
    print('Input:')
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(lt)
    dt = lists2dict(lt, pp)
    print('-' * 80)
    print('Output:')
    pp.pprint(dt)

def lists2dict(lt, pp):
    # form a dict from a list of lists,
    # if a key's value is a single-element list, just add the element
    dt = {l[0]: l[1:] if len(l[1:]) > 1 else l[1] for l in lt}
    # print('-' * 70)
    # pp.pprint(dt)
    for k,v in dt.items(): # step through the dict, looking for places to recurse
        # print('k=%r, v=%r' %(k,v))
        if isinstance(v, list): # a value which is a list needs to be converted to a dict
            if isinstance(v[1], list): # only recurse if a to-be-parsed value has a list
                dt[k] = lists2dict(v, pp)
            else: # just a single k/v pair to parse
                dt[k] = {v[0]: v[1]}
    return dt

if __name__ == '__main__':
    main()

If anyone has improvements, or a more efficient and/or Pythonic implementation, please share it.

You could simplify your function:

def nested_pairs2dict(pairs):
    d = {}
    for k, v in pairs:
        if isinstance(v, list): # assumes v is also list of pairs
           v = nested_pairs2dict(v)
        d[k] = v
    return d

It seems to work.


Name comparison using fuzzy string matching I'm somewhat new to python and wrote this piece of code to do a string comparison of accounts that are being requested for import into our data base against accounts that are already present. The issue is that the accounts currently in our DB is over 65K and I'm comparing over 5K accounts for import causing this code to take over 5 hours to run. I suspect this has to do with the loop I'm using but I'm not certain how to improve it.

TLDR; I need help optimizing this code so it has a shorter run time.

from fuzzywuzzy import fuzz
from fuzzywuzzy import process

accounts_DB = pd.read_csv("file.csv") #65,000 rows and 15 columns
accounts_SF = pd.read_csv("Requested Import.csv") #5,000 rows and 30 columns

def NameComparison(DB_account, choices):
    """Function uses fuzzywuzzy module to perform Levenshtein distance string comparison"""
    return(process.extractBests(DB_account, choices, score_cutoff= 95))

options = accounts_sf["Account Name"]
a_list = []
for i in range(len(accounts_db)):
    a_list.append(NameComparison(accounts_db.at[i,"Company Name"], options))
b_list = pd.DataFrame(a_list)
b_list.to_csv("Matched Accounts.csv")

To apply the same function to each row of a dataframe column, you usually use pd.Series.map or pd.Series.apply. You can thus simplify your code to:

from functools import partial
from fuzzywuzzy import process

accounts_DB = pd.read_csv("file.csv") #65,000 rows and 15 columns
accounts_SF = pd.read_csv("Requested Import.csv") #5,000 rows and 30 columns

best_matches = partial(process.extractBests, choices=accounts_SF['Account Name'], score_cutoff=95)
accounts_DB['Company Name'].map(best_matches).to_csv("Matched Accounts.csv")


Boggle solver in Rust - Looking for speedup I recently made a boggle solver in Rust to compensate for the fact that I'm really bad at boggle. This has also been a good Rust learning experience. The code does what it's supposed to quite well but it is a bit slow, even when compiling with the  -O flag. Since I'm new to Rust, I'm not sure where the slow spots could be. I'm using a hashset to lookup the words, so I assume the lookup isn't the bottleneck. Any tips? (Also, if I'm doing any kind of bad practice, you can let me know). Thanks.

use std::collections::HashSet;
use std::fs;

const MAX_WORD_LEN: usize = 12;

#[derive(Clone, Debug, Copy)]
struct Coordinate {
    row: i64,
    col: i64
}

impl Coordinate {
    // Returns a new coordinate if the move is valid. None otherwise.
    fn move_c(&self, a: i64, b: i64, h: i64, w: i64) -> Option<Coordinate> {
        let h_range = 0..h;
        let w_range = 0..w;
        if h_range.contains(&(self.row + a)) && w_range.contains(&(self.col + b)) {
            Some(Coordinate {row: self.row + a, col: self.col + b})
        }
        else {
            None
        }
    }
}

impl PartialEq for Coordinate {
    fn eq(&self, other: &Self) -> bool {
        self.row == other.row && self.col == other.col
    }
}

fn boggle_solve(grid: &Vec<Vec<char>>, dict: &HashSet<String>) {
    for (i, row) in grid.iter().enumerate() {
        for (j, ch) in row.iter().enumerate() {
            boggle_solve_help(grid, dict, ch.to_string(), Coordinate{row: i as i64, col: j as i64}, vec![Coordinate{row: i as i64, col: j as i64}])
        }
    }

}

fn boggle_solve_help(grid: &Vec<Vec<char>>, dict: &HashSet<String>, word: String, c: Coordinate, path: Vec<Coordinate>) {
    // Base case.
    // No more work if the string is long enough
    if word.len() > MAX_WORD_LEN {
        return;
    }

    if word.len() > 3 && dict.contains(&word) {
        println!("{}", word);
    }

    // For every possible direction the path can take, if it is valid, take it.
    for a in -1..=1 {
        for b in -1..=1 {
           match c.move_c(a, b, grid.len() as i64, grid[0].len() as i64) {

                Some(new_c) if !path.contains(&new_c) => {
                    let mut new_path = path.clone();
                    new_path.push(new_c);
                    let new_word = format!("{}{}", &word, &grid[new_c.row as usize][new_c.col as usize]);

                    boggle_solve_help(grid, dict, new_word, new_c, new_path);
                },

                Some(_) => (),

                None => ()

           }
        }
    }
}

fn main() {
    let contents = fs::read_to_string("words_alpha.txt").unwrap();
    let dict: HashSet<String> = contents.split_whitespace().map(|s| s.to_string()).collect();

    let grid: Vec<Vec<char>> = vec![
        vec!['c', 's', 't', 'e', 't'],
        vec!['a', 'i', 'r', 'l', 's'],
        vec!['p', 'd', 'a', 'e', 's'],
        vec!['u', 'e', 'c', 's', 'e'],
        vec!['r', 'o', 't', 'r', 'i']
    ];

    boggle_solve(&grid, &dict)

}

The main thing that's going to get you a speed boost is returning early from the search if there's no word that starts with what you have so far.

One simple way to do this is to keep a HashSet of all the prefixes of words in the dictionary. Or, better yet, a HashMap where the value indicates whether the prefix is actually a word or not.

struct PrefixMap<'a>(HashMap<&'a str, bool>);

impl<'a> PrefixMap<'a> {
    fn new(words: impl IntoIterator<Item = &'a str>) -> Self {
        let mut map = HashMap::new();
        for word in words {
            // note that this gets every prefix except the whole word
            for (ending, _) in word.char_indices() {
                map.entry(&word[..ending]).or_insert(false);
            }
            map.insert(word, true);
        }
        Self(map)
    }

    fn is_word(&self, word: &str) -> bool {
        self.0.get(word).copied().unwrap_or(false)
    }

    fn is_prefix(&self, prefix: &str) -> bool {
        self.0.contains_key(prefix)
    }
}

Then, in the first part of boggle_solve_help, we can use is_word and is_prefix to determine whether to print the word or return early.

if !dict.is_prefix(&word) {
    return;
}

if word.len() > 3 && dict.is_word(&word) {
    println!("{}", word);
}

While this isn't the most efficient thing possible, that simple step makes the program run almost instantly on my computer (less than a second). If you still want to improve more, look up tries for a memory efficient and quick way to do the same thing. Since creating the index of words takes a little while, you might try to cache it between runs using serde or something similar.

Now for some more suggestions.

Derive PartialEq. Right now you implement PartialEq for Coordinate manually, but the same implementation can be done by adding PartialEq to the derive attribute.
Use descriptive variable names. The main offenders are the arguments to move_c and later on c in boggle_solve_help. I know sometimes this is annoying, but even just changing a and b to dy and dx (calculus shorthand for "change in y/x") makes everything clearer. It's probably worth spelling out height and width to make it clear that that's what you mean.
Don't forget to use cargo fmt. It's a simple step and will instantly make your code more standardized and easier to read for other Rust programmers.
Encapsulate the grid into its own type. Right now, you're using Vec<Vec<char>> directly, which is fine, but means that lots of annoying details are strewn about the functions that use it. The type can still be Vec<Vec<char>> under the surface, but having a method like grid.get(coordinate) rather than grid[coordinate.row as usize][coordinate.col as usize] makes things easier to read and make sense of. As a plus, this will also silence Clippy's complaint about using &Vec<_> instead of &[_].
Consider switching to an iterative algorithm. You won't run into stack overflow here since you have a hard cap on how deep the recursion can go. Still, if you want a slightly faster algorithm, a depth-first search can be memory efficient and avoid the repeated String allocations you currently have (with format!).
Bonus: don't show duplicate words. Right now, a word will be duplicated if can be spelled with a different path of letters on the board. For example, "trees" can be written three different ways near the bottom right corner of the board so it gets printed 3 times. It also shows up in a few other places. I might suggest keeping a HashSet of all the words you've found so far to eliminate duplicates.

Here's the code after applying the early return and the first three of my suggestions. I'll let you figure out the other two.

use std::collections::HashMap;
use std::fs;

const MAX_WORD_LEN: usize = 12;

#[derive(Clone, Debug, Copy, PartialEq)]
struct Coordinate {
    row: i64,
    col: i64,
}

impl Coordinate {
    // Returns a new coordinate if the move is valid. None otherwise.
    fn move_coord(&self, dy: i64, dx: i64, height: i64, width: i64) -> Option<Coordinate> {
        let height_range = 0..height;
        let width_range = 0..width;
        if height_range.contains(&(self.row + dy)) && width_range.contains(&(self.col + dx)) {
            Some(Coordinate {
                row: self.row + dy,
                col: self.col + dx,
            })
        } else {
            None
        }
    }
}

// Keys consist of all prefixes of words
// Values say whether the prefix is a word
// Since we keep references rather than `String`s,
// the lifetime of the map can't exceed the lifetime of wherever
// the `&str`s come from.
// If you decide to make this more persistent, using `String` rather than `&str`
// might be appropriate.
struct PrefixMap<'a>(HashMap<&'a str, bool>);

impl<'a> PrefixMap<'a> {
    // We can produce this from any iterator of `&str`s.
    fn new(words: impl IntoIterator<Item = &'a str>) -> Self {
        let mut map = HashMap::new();
        for word in words {
            // note that this gets every prefix except the whole word
            for (ending, _) in word.char_indices() {
                map.entry(&word[..ending]).or_insert(false);
            }
            map.insert(word, true);
        }
        Self(map)
    }

    fn is_word(&self, word: &str) -> bool {
        self.0.get(word).copied().unwrap_or(false)
    }

    fn is_prefix(&self, prefix: &str) -> bool {
        self.0.contains_key(prefix)
    }
}

fn boggle_solve(grid: &Vec<Vec<char>>, dict: &PrefixMap) {
    for (i, row) in grid.iter().enumerate() {
        for (j, ch) in row.iter().enumerate() {
            boggle_solve_help(
                grid,
                dict,
                ch.to_string(),
                Coordinate {
                    row: i as i64,
                    col: j as i64,
                },
                vec![Coordinate {
                    row: i as i64,
                    col: j as i64,
                }],
            )
        }
    }
}

fn boggle_solve_help(
    grid: &Vec<Vec<char>>,
    dict: &PrefixMap,
    word: String,
    coord: Coordinate,
    path: Vec<Coordinate>,
) {
    // Base case.
    // No more work if the string is long enough
    if word.len() > MAX_WORD_LEN {
        return;
    }

    if !dict.is_prefix(&word) {
        return;
    }

    if word.len() > 3 && dict.is_word(&word) {
        println!("{}", word);
    }

    // For every possible direction the path can take, if it is valid, take it.
    for dy in -1..=1 {
        for dx in -1..=1 {
            match coord.move_coord(dy, dx, grid.len() as i64, grid[0].len() as i64) {
                Some(new_coord) if !path.contains(&new_coord) => {
                    let mut new_path = path.clone();
                    new_path.push(new_coord);
                    let new_word = format!(
                        "{}{}",
                        &word, &grid[new_coord.row as usize][new_coord.col as usize]
                    );

                    boggle_solve_help(grid, dict, new_word, new_coord, new_path);
                }

                Some(_) => (),

                None => (),
            }
        }
    }
}

fn main() {
    let contents = fs::read_to_string("words_alpha.txt").unwrap();
    let dict = PrefixMap::new(contents.split_whitespace());

    let grid: Vec<Vec<char>> = vec![
        vec!['c', 's', 't', 'e', 't'],
        vec!['a', 'i', 'r', 'l', 's'],
        vec!['p', 'd', 'a', 'e', 's'],
        vec!['u', 'e', 'c', 's', 'e'],
        vec!['r', 'o', 't', 'r', 'i'],
    ];

    boggle_solve(&grid, &dict)
}

(playground - but note that words_alpha.txt doesn't exist, so you can't run it)


Least amount of HTML and CSS to create a bottom border for every section? I am doing a redesign and have some previously existing content in HTML that is similar to

Bacon ipsum dolor sit amet aliquip est tail occaecat bacon ad qui short loin exercitation.Strip steak sint dolore et ut, elit kielbasa quis.

&ndash;Name Surname

And those classes were manually added to every testimonial. So I have so far wrapped everything in a
 and then every testimonial is wrapped in a div because I wanted to leverage inheritance so that a new testimonial later did not need the classes nor the
 but of course to prevent a future
 within the
 that might be necessary in the future by another dev, I decided to use > p . How do these classes look for keeping code as lean as possible?

HTML

Frankfurter meatloaf quis chuck, sunt jerky voluptate veniam ex aute commodo duis laboris labore. Sint dolor enim boudin pancetta culpa ham hock, laboris brisket veniam doner short loin velit qui duis.

        &ndash Piggy McPiggy

Frankfurter meatloaf quis chuck, sunt jerky voluptate veniam ex aute commodo duis laboris labore. Sint dolor enim boudin pancetta culpa ham hock, laboris brisket veniam doner short loin velit qui duis.

        &ndash Oinky Sausagebreath

Frankfurter meatloaf quis chuck, sunt jerky voluptate veniam ex aute commodo duis laboris labore. Sint dolor enim boudin pancetta culpa ham hock, laboris brisket veniam doner short loin velit qui duis.

        &ndash Pigster Pickled

CSS

div#testimonials > div {
    border-bottom: solid 1px #666;
}

div#testimonials p {
    font-size: 16px;
}

div#testimonials span {
    font-weight: bold;
    color: red;
}

My concern is that if we perhaps need to add a span in the paragraph, that I might need to do something else to make sure that the persons name is still styled as intended. Something about this just does not seem Kosher.

Something did feel wrong about your code

IDs are unique

A bit of redundancy is this: div#testimonials

IDs are unique in the page and should only be one element. You can just to #testimonials and still get that div.

Span? Which one?

Another is the span's usage. Suppose you wanted to style a phrase in the testimonial, you'd use span to style it. But your author style definition calls for all spans: #testimonials span. This would affect all spans, not just the author's.

I suggest you do place a class, like "author", to indicate that which span you are trying to style as the author:

#testimonials span.author {
  font-weight: bold;
  color: red;
}

Set the font in the parent

It would be better if you set the default font size on the parent, rather than on each element, unless that element should differ from the others. Also, as far as I know, the browser's default font size is around 16px. You could omit the size declaration.

Aesthetics: Removing the last border

This is just for aesthetics, but I suggest you remove the last entry's border. But since only IE9 supports :last-child, we change the approach. Instead, add a top border and let :first-child, supported in IE7+, remove the very first child's border.

#testimonials > div {
  border-top: solid 1px #666;
}

#testimonials > div:first-child {
  border-top: none;
}

Semantics: It's a list!

If you take a look at your content, it's a list. And if semantics is to be followed, you should use a list.

Worried about size?

You shouldn't. Though semantics might need you to use more code, it's not deployed in this state. Use an automation system like Grunt and minify everything. And make sure to gzip your transfers from the server.

Now this makes more sense.. at least for me:

HTML

Frankfurter meatloaf quis chuck, sunt jerky voluptate veniam ex aute commodo duis laboris labore. Sint dolor enim boudin pancetta culpa ham hock, laboris brisket veniam doner short loin velit qui duis.

        Piggy McPiggy

Frankfurter meatloaf quis chuck, sunt jerky voluptate veniam ex aute commodo duis laboris labore. Sint dolor enim boudin pancetta culpa ham hock, laboris brisket veniam doner short loin velit qui duis.

        Oinky Sausagebreath

Frankfurter meatloaf quis chuck, sunt jerky voluptate veniam ex aute commodo duis laboris labore. Sint dolor enim boudin pancetta culpa ham hock, laboris brisket veniam doner short loin velit qui duis.

        Pigster Pickled

CSS

/* removing the default styles of the list */
#testimonials{
    list-style:none;
    margin: 0;
    padding: 0;
}

/*unless your testimonials contain lists, this should be fine */
#testimonials li {
    border-top: solid 1px #666;
}

#testimonials li:first-child {
    border-top: none;
}

#testimonials span.author {
    font-weight: bold;
    color: red;
}


Python lazy dictionary Here is a lazy dictionary with test cases.

"""
A dictionary whose values are only evaluated on access.
"""
from functools import partial

from UserDict import IterableUserDict

class LazyDict(IterableUserDict, object):
    """
    A lazy dictionary implementation which will try
    to evaluate all values on access and cache the
    result for later access.
    """

    def set_lazy(self, key, item, *args, **kwargs):
        """
        Allow the setting of a callable and arguments
        as value of dictionary.
        """
        if callable(item):
            item = partial(item, *args, **kwargs)
        super(LazyDict, self).__setitem__(key, item)

    def __getitem__(self, key):
        item = super(LazyDict, self).__getitem__(key)
        try:
            self[key] = item = item()
        except TypeError:
            pass
        return item

def test_lazy_dict():
    """
    Simple test cases for `LazyDict`.
    """

    lazy_dict = LazyDict({1: 1, 2: lambda: 2})
    assert lazy_dict[2] == 2
    lazy_dict[3] = 3
    assert lazy_dict[3] == 3

    def joiner(*args, **kwargs):
        sep = kwargs.pop('sep', ' ')
        kwargs = [
            '%s=%s' % (k, v)
            for k, v in sorted(kwargs.iteritems())]
        return sep.join(list(args) + kwargs)

    lazy_dict.set_lazy(
        4, joiner, 'foo', 'bar', name='test', other='muah', sep=' ')
    assert lazy_dict[4] == 'foo bar name=test other=muah'

    assert lazy_dict.get('5') is None

    # Test caching functionality.
    def call_at_max(count):
        counter = [0]

        def inner():
            counter[0] += 1
            if counter[0] > count:
                raise AssertionError('Called more than once')
            return 'happy'
        return inner

    call_once = call_at_max(1)
    lazy_dict[5] = call_once
    assert lazy_dict[5] == 'happy'
    assert lazy_dict[5] == 'happy'

    # Test for helper function.
    try:
        call_once()
    except AssertionError:
        assert True

Per the documentation for UserDict:

  The need for this class has been largely supplanted by the ability to subclass directly from dict (a feature that became available starting with Python version 2.2).

Rather than:

class LazyDict(IterableUserDict, object):

therefore, unless you have a really good reason to want to support 2.1 and earlier, I would use:

class LazyDict(dict):

or base it on collections.MutableMapping. This also makes compatibility with 3.x (where UserDict doesn't exist) less complex.

An edge case you may not have thought of: what if the function stored in the dictionary returns a callable object? Then you will get different results depending on when you access the dictionary. If this is intentional, it should be documented. If not, one solution would be to create a LazyCallable class (effectively a custom partial) to store the function and its arguments, so you can check if isinstance(item, LazyCallable) to distinguish between items added via set_lazy and any other callable values.

Overall, I'm not convinced I see the point to this. The function only gets called once, but I'm not sure the layer of additional complexity needed for:

lazy_dict.set_lazy(4, joiner, 'foo', 'bar', name='test', other='muah', sep=' ')

is better than:

vanilla_dict[4] = joiner('foo', 'bar', name='test', other='muah', sep=' ')

In both, the function only gets called once (at slightly different times, admittedly), and the latter doesn't require the reader to know about LazyDict. This also doesn't provide the functionality (that e.g. regular "memoization" does) to dynamically store results of calls to the function with different arguments, so it's only called once for each set of arguments.

I suppose one advantage would be in cases where you aren't sure, when you add the function to the dictionary, whether or not you will ever need to call it. If the function is very computationally complex but not actually needed, you can optimise one call down to zero, but there are probably easier ways to do that. There's no reason the user couldn't put a partial into the dictionary themselves.

Perhaps off-topic, but:

# Test caching functionality.
def call_at_max(count):
    counter = [0]

    def inner():
        counter[0] += 1
        if counter[0] > count:
            raise AssertionError('Called more than once')
        return 'happy'
    return inner

You have hard-coded 'Called more than once', which won't make sense for count != 1. Also, using a list to make a "mutable integer" isn't very neat. I would either make it non-generic (i.e. hard-code the 1, too), or implement as something like:

MULTIPLES = {1: 'once', 2: 'twice'}  # add 3: 'thrice' if you like!

# Test caching functionality.
def call_at_most(times):
    def inner():
        inner.counter += 1
        if inner.counter > times:
            raise AssertionError(
                'Called more than {}'.format(
                    MULTIPLES.get(times, '{} times').format(times)
                )
            )
        return 'happy'
    inner.counter = 0
    return inner

In use:

>>> test = call_at_most(2)
>>> test()
'happy'
>>> test()
'happy'
>>> test()

Traceback (most recent call last):
  File "<pyshell#51>", line 1, in <module>
    test()
  File "<pyshell#47>", line 8, in inner
    MULTIPLES.get(times, '{} times').format(times)
AssertionError: Called more than twice

I'd also replace assert True with pass.


Simple console application that changes text color based on user input I am learning C# like 1 or 2 months and I wanted to know if there is any possibility to shorten this code.

It is simple. It only changes color of the console text with while loop if you enter wrong input. When you enter it correctly it changes the color + asks you if you want to continue.

It looks weird for me when I have for each color duplicity of the code written before (in if and switch statements) But I have no idea if there is any way to make it cleaner and shorter, but I guess there is. Also I would like to know where is worth to use arrays and where normal strings and integers + if there is any reasonable difference. Also I find it very hard for orientation when I have a lot of strings or ints inside array, but easy to maintain. Thanks for answers

static void Main(string[] args)
{
    string[] Arrays = new string[3]{
        "Hello!", "Enter a color\n1 = blue\n2 = red\n3 = Magenta", "Render numbers"

    Console.WriteLine(Arrays[0]);

    bool Continue = true;
    while(Continue) {
        Console.WriteLine(Arrays[1]);

        string[] color = new string[4]{
            "Blue", "Red", "Magenta", "Render numbers : "
        };
        char EnterColor = Console.ReadKey().KeyChar;
        Console.WriteLine();
        switch (EnterColor) {
            case '1':
                Console.ForegroundColor = ConsoleColor.Blue;

                Console.WriteLine("Do you want to continue? y/n");
                string NextTask = Console.ReadLine();
                if (NextTask == "y") {
                    // set loop to repeat
                    Continue = true;
                }
                else {
                    Continue = false;

                }
                break;
            case '2':
                Console.ForegroundColor = ConsoleColor.Red;
                Continue = false;
                Console.WriteLine("Do you want to continue? y/n");
                string NextTaskRed = Console.ReadLine();
                if (NextTaskRed == "y") {
                    Continue = true;
                }
                else {
                    Continue = false;

                }
                break;
            case '3':
                Console.ForegroundColor = ConsoleColor.Magenta;
                Continue = false;
                Console.WriteLine("Do you want to continue? y/n");
                string NextTasMagenta = Console.ReadLine();
                if (NextTasMagenta == "y") {
                    // set loop to repeat
                    Continue = true;
                }
                else {
                    Continue = false;

                }
                break;
            // there will be render nums option in next case using for loop
        }
    }
}

Review

Whitespace
Use whitespace between the method name and opening parenthesis: Main (string[] args) instead of Main(string[] args). The same convention applies to statements such as while: while (Continue) instead of while(Continue). And also to curly braces: new string[4] { instead of new string[4]{.

New Line
Prefer using the new line of the system over a fixed format: use Environment.NewLine instead of \n.

Comments
Avoid useless comments such as //strings. We all know these are string instances: "Hello!", "Enter a color\n1 = blue\n2 = red\n3 = Magenta", "Render numbers". Don't comment out code that you don't use, remove it entirely: //string Nums = "";.

Naming conventions
Use pascalCase for variable names: string[] arrays instead of string[] Arrays. But what does arrays mean? Use a meaningful instead: string[] promptMessages for instance.

Using an array or not
There is no reason to wrap the messages in an array. Console.WriteLine(Arrays[1]); is a disaster for readability. Console.WriteLine("Enter a color .."); is much cleaner. On the other hand, string[] color could have been declared as ConsoleColor[] colors. This way, you could have avoided all that repetitive code. You should try to write DRY code.

var colors = new ConsoleColor[] {
    ConsoleColor.Blue,
    ConsoleColor.Red,
    ConsoleColor.Magenta
};

// NOTE: this is a trivial parsing, you should perform some validation..
// 0 for Blue, 1 for Red, 2 for Magenta
char userColor = int.Parse(Console.ReadKey().KeyChar.ToString());

Console.ForegroundColor = colors[userColor];
Console.WriteLine("Do you want to continue? y/n");
var nextTask = Console.ReadKey().KeyChar;
Continue = nextTask == 'y';

Console input
Don't read an entire line when you only need a single character: string NextTask = Console.ReadLine(); if (NextTask == "y") { ... You can read a single character: string NextTask = Console.ReadKey().KeyChar;. Unlike ReadLine, ReadKey does not wait for Enter key to be pressed, but immediately returns the pressed key.


More functional way of writing this palindrome extractor? I wrote this palindrome extractor.  And even though it works, and I can solve the challenge with it, it feels very Java-like. I was wondering what adjustments I could make in order for it to be more functional.

import collection.mutable._

object Level1 {
    def palindrome(input:String) = input.reverse == input

    def extractPalindromes(input:String) =
    {
        var counter = 0
        val palindromes = new ListBuffer[String]()
        while(counter < input.length)
        {
            var localCounter = 2
            while((counter + localCounter) < input.length)
            {
                //println("counter:"+counter+" localCounter:"+localCounter)
                val tempString = input.substring(counter, input.length - localCounter)
                if(palindrome(tempString)) palindromes += tempString
                localCounter += 1
            }
            counter += 1
        }
        palindromes
    }

    def main(args:Array[String]) = {
        val input = "I like racecars that go fast"
        extractPalindromes(input).filter(_.length > 4).foreach(println)
    }
}

It can be as simple as:

scala> val str =  "I like racecars that go fast"
str: java.lang.String = I like racecars that go fast

scala> for { i <- 2 to str.size; s <- str.sliding(i) if s == s.reverse} yield s
res5: scala.collection.immutable.IndexedSeq[String] = Vector(cec, aceca, racecar)


Getting the last post of all threads My MySQL query is loading very slowly (over 30 secs) and I was wandering what tweaks I can make to optimize it.

The query should return the last post with the string "?" of all threads.

SELECT FeedbackId, ParentFeedbackId, PageId, FeedbackTitle, FeedbackText, FeedbackDate
FROM ReaderFeedback as c
LEFT JOIN (SELECT max(FeedbackId) as MaxFeedbackId FROM ReaderFeedback where ParentFeedbackId IS NOT NULL GROUP BY ParentFeedbackId) as d ON d.MaxFeedbackId = c.FeedbackId
WHERE ParentFeedbackId IS NOT NULL
AND FeedbackText LIKE '%?%' GROUP BY ParentFeedbackId
ORDER BY d.MaxFeedbackId DESC LIMIT 50

Hard to give full answer without understanding the ReaderFeedback table definition.  I will point to some areas of concern.

A LIKE clause that has a wildcard at its beginning will never be able to use a typical B-TREE index on the field you are comparing against.  This will always require a full table scan.  For this reason, you probably will need to look at using full text search with appropriate full text index.
You are using sub-selects, which oftentimes introduce performance problems. Can this be designed away?
It appears you are implementing a parent-child tree hierarchy, which typically is a little painful to work with in MySQL (or most any other relational database).  You might want to check out this article: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/  on managing hierarchical data in MySQL.  You might for example find better read performance from using the nested sets model shown there (though nested sets are typically more complex when it comes to inserting/updating nodes).


Sign-up wizard structure seems too repetitive I think my current structure is way too repetitive.  I feel like I must be missing something that would easily make this sign up wizard work far better.

Step 1

Step 2

Step 3

        <form ng-submit="registerSubmit()">
        </form>

        <form>

        </form>

        <form>

        </form>

app.controller('registerCtrl', function($scope, $http, $location, FlashService){

    $scope.step1 = {active: true, done:false, todo: false};
    $scope.step2 = {active: false, done:false, todo: true};
    $scope.step3 = {active: false, done:false, todo: true};

    $scope.wiz = function(step){
        $scope.step = step;
        switch (step) {
            case 'step1':
                $scope.step1 = {active: true, done:false, todo: false};
                $scope.step2.active = false;
                $scope.step3.active = false;
                $scope.step2.todo = true;
                $scope.step3.todo = true;
                break;
            case 'step2':
                $scope.step1.active = false;
                $scope.step1.todo = true;
                $scope.step2 = {active: true, done:false, todo: false};
                $scope.step3.active = false;
                $scope.step3.active = false;
                break;
            case 'step3':
                $scope.step1.active = false;
                $scope.step2.active = false;
                $scope.step1.todo = true;
                $scope.step2.todo = true;
                $scope.step3 = {active: true, done:false, todo: false};
                break
            default:
                $scope.step1 = {active: true, done:false, todo: false};
                $scope.step2 = {active: false, done:false, todo: true};
                $scope.step3 = {active: false, done:false, todo: true};
        }
    }

    $scope.registerSubmit = function(){
        $http.post()
            .success(function(data, status, headers, config){
                $scope.step = 'step2';
                $scope.step1 = {active: false, done:true , todo:false}
                $scope.step2.active = true;
                if (data.msg !='') {
                      $scope.msgs.push(data.msg);
                  } else {
                       $scope.errors.push(data.error);
                   }
              }).error(function(data, status){
                      FlashService.show(response.flash);
                    $scope.errors.push(status);
                })
    };
});

I'm worried that the switch and ngclass statements are way more than I need. Is there a simpler way to accomplish this? This just doesn't feel DRY at all.

You can make this DRYer if you can assume that :

You keep naming each step stepx
No other variables in $scope start with step

In that case you could use a function that sets in a first step the right scope active and then the other steps inactive:

function updateScopeSteps( $scope , step )
{
   //Default to step1
   step = step || 'step1';
   //Set step
   $scope.step = step;
   //Activate step
   $scope[ step ] = {active: true, done:false, todo: false};
   //De-activate all other steps
   for( var name in $scope ){
     if( name.substring(0,4) == 'step' && name != step ){
       $scope[name].active = false;
       $scope[name].todo = true;
     }
   }
}


Searching in an array in less than O(n) time I have an array where each element is either one less or one greater than the preceding element \$\{x_i = x_{i-1} \pm 1\}\$. I wish to find an element in it in less than \$O(n)\$ time. I've implemented it like this:

public int searchArray(int[] arr, int i, int elem) {

    if (i > arr.length - 1 || i < 0) {
        return -1;
    }

    if (arr[i] == elem) {
            return i;

    } else {
            int diff = Math.abs(elem - arr[i]);
            int index = searchArray(arr, i + diff, elem);
            if (index == -1) {
                index = searchArray(arr, i - diff, elem);
                if (index == -1) {
                    return -1;
                }
            }
            return index;
    }
}

And calling it like:

int[] arr = {2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 6, 5, 4, 3, 4};
int index = searchArray(arr, 0, 3);

It is working fine, but can it be improved? Specifically, is there a way to do it iteratively? And is the current algorithm less than \$O(n)\$. I guess it is, but I'm not sure.

While other answers make good points, I have to wonder why you are using recursion. This is such a simple problem to solve with a for loop.

I assume that you are not supposed to start from any index other than index 0, so consider the following routine:

public int searchArray(int[] arr, int elem) {

    for (int i = 0; i < arr.length; ) {
        if (arr[i] == elem) {
            return i;
        }
        i += Math.abs(elem - arr[i]);
    }
    return -1;
}

(If you need to start the search part-way through the array then you can add the offset input parameter again and start i from that).

The bottom line is that recursion is overkill, this system is \$O(n)\$, but the cost of each cycle is less than the same thing using recursion.

I am not aware of any way to solve the given problem with a system of better than \$O(n)\$ complexity.

Discussion on complexity - why this is \$O(n)\$

This answer has generated a lot of discussion about complexity, that this method only ever scans, at most, half the members of the input array, and thus it should be complexity \$O \left( \frac{n}{2} \right )\$ instead of \$O(n)\$. The argument given is something like:

  Consider the worst-case data 1,2,1,2,1,2,1,2,1,2,1,2 and the search term 3. For this situation the method will start at data[0], and then skip to data[2], then data[4], and so on. It will never inspect data[1], and other odd-indexed data points. If the search term is even more 'different' than the actual data (e.g. 100) then the method will do only one comparison at data[0] and will then return 'not found' -1.

This is an interesting observation, that the method only ever needs to scan half the data at most. This is especially interesting considering a 'naive' method which just scans the data one-member-at-a-time and returns when it finds the value. That 'naive' method most certainly has \$ O\left(n\right) \$ 'performance' and complexity, and the 'skip-method' will be more than twice as fast.

The important thing to note though, is how the algorithms scale relative to the amount of data, not relative to each other!

So, consider a hypothetical set of worst-case data 1,2,1,2,1,2,.... and the search-term 3. This hypothetical happens to be searched in 4 milliseconds by the skip-method, and in 8 milliseconds by the naive-method. Now we double the amount of data, what happens? The processing time for both methods will double!

In both cases, the performance of the algorithms will double for each doubling of the data volume. This is what makes both algorithms \$ O(n) \$ complexity. From Wikipedia:

  In computer science, big O notation is used to classify algorithms by how they respond (e.g., in their processing time or working space requirements) to changes in input size.

Reversing the argument, by suggesting the skip-method has \$O \left( \frac{n}{2} \right) \$ complexity, I would then expect that, if I double the data, the execution time would only increase by a half, or 50%. This is 'obviously' not true for the skip-method (nor the naive-method).

Both methods have \$O(n)\$ complexity because they both scale the same way with increasing volumes of data.

But, just because they scale the same way, does not mean that one method is not better than the other... obviously.


Comparing a list of messages with a list inside an object int i = 0
for ( expectedErrors in errors ) {
    final List<String> actualErrors = new ArrayList<>()
    actualRequestMessages?.get(i++)?.errorData?.errors?.each {
        item -> actualErrors.add("${item.message}")
    }
    assert [ ] == ((actualErrors - expectedErrors) + (expectedErrors - actualErrors)), [expectedErrors, actualErrors, "Expected Errors doesn't match actual"]
}

Currently, I am comparing actual error messages inside actualRequestMessages.errorData.errors with a list of expectedErrors as mentioned above. Is there a better way of doing it in Groovy?

Errors come from spock test in the form of a list of errors:

errors
[[error1],[error1,error2,error3]]

Instead of this:

final List<String> actualErrors = new ArrayList<>()

I believe you can write simply:

final actualErrors = []

I don't really understand the point of this:

assert [ ] == ((actualErrors - expectedErrors) + (expectedErrors - actualErrors))

The only way this will be true is if this is true:

assert actualErrors == expectedErrors

Which is a lot simpler.

Btw, this is not idiomatic in Groovy:

assert [ ] == something

This is the idiomatic way:

assert !something

(because non-empty collections are true)


Swapping dynamically populated images on hover/touch I'm using jQuery to swap images on hover. There's a main image, and a gallery of thumbs. When the thumb is hovered over/touched, the larger version of it populates the main image, and the smaller version of the original populates the thumb. This happens via swapping src and data- attributes.

This all works, but being relatively new to JS/jQuery, the code is probably a little clunky and could use a keener eye. I don't fully grasp callbacks (among other JS things) yet, so I imagine there'll be some streamlining/chuckling with that.

function large_to_thumb(imageHovered, mainLarge, mainThumb){
    $(imageHovered).attr('src', mainThumb).data('large', mainLarge);
}

function thumb_to_large(image, mainLarge, mainThumb){
    var mainImage = $('.main-image');
    // 1. put gallery image data-large into .main-large src
    mainImage.attr('src', $(image).data('large'));

    // 2. put gallery image src into .main-large data-gallery-image
    mainImage.data('gallery-image', $(image).attr('src'));
    large_to_thumb(image, mainLarge, mainThumb);
}

$('.product-gallery img').on('mouseenter touch', function(){
    var mainLarge = $('.product-image .main-image').attr('src');
    var mainThumb = $('.product-image .main-image').data('gallery-image');
    thumb_to_large(this, mainLarge, mainThumb);
});
li {
  display:inline;
}

Some good suggestions already from @konijn, so I won't rehash any of the content from that answer.

I would consider writing this a a jQuery plugin.  This will allow you to properly namespace your code and allow you to define your own variables for re-use within this scope (i.e. to prevent need to re-query DOM with every event action.  This will also make your solution re-usable across various websites and DOM configurations.

That might look like

(function( $ ) {

    $.fn.galleryThumbSwapper = function(options) {

        // Extend our default options with those provided.
        // Note that the first argument to extend is an empty
        // object – this is to keep from overriding our "defaults" object.
        var opts = $extend({},$.fn.galleryThumbSwapper.defaults, options);

        // do our DOM selections once
        // define wrapper based on element this function was called on
        var $wrapper = $(this);

        // define main image and gallery image items
        // here we only find element within the wrapper such that
        // you could use multiple galleries on a page without
        // worry about interaction problems between galleries so
        // long as each gallery is contained within the wrapper
        var $mainImage = $wrapper.find(opts.mainImageSelector);
        var $galleryImages = $wrapper.find(opts.galleryImageSelector);

        // set click handler
        $galleryImages.on('click', swapImages);

        // define callback for click handler
        function swapImages() {
            // wrapper for triggered element
            $triggerElement = $(this);

            // get current main image info
            var currentMainSrc = $mainImage.attr('src');
            var currentMainData = $mainImage.data('gallery-image');

            // get triggered thumbnail info
            var newMainSrc = $triggerElement.data('large');
            var newMainData = $triggerElement.attr('src');

            // perform the swap
            $mainImage.attr('src', newMainSrc);
            $mainImage.data('gallery-image', newMainData);

            $triggerElement.attr('src', currentMainSrc);
            $triggerElement.data('large', currentMainData);
        }

        // return this for use in chaining
        return this;
    }

    // define default values for plug-in
    // you could also possibly add default values for the data fields here
    // so as to make these configurable
    $.fn.galleryThumbSwapper.defaults = {
        mainImageSelector = '.main-class',
        galleryImageSelector = '.product-gallery img'
    };
}( jQuery ));

// usage
$(document).ready(
    $('.product-image').galleryThumbSwapper();
);

// or, with overriding default options
$(document).ready(
    $('.product-image').galleryThumbSwapper({
        mainImageSelector = '#main_img',
        galleryImageSelector = '.thumb_img'
    });
);

A few other thoughts:

You might consider using id instead of or in addition to class for the main wrapper DOM element. i.e. product-image.  This would guarantee uniqueness, particularly if you used the jQuery plug-in approach I am suggesting.
You might consider your data attribute naming of 'large' which doesn't seem to make much sense as what you are storing in there is a URL.  I actually don't see why you can't use the same data-attribute name on the main image and the gallery images.
Seems like a bit of an odd user experience to have thumbnails potentially be changed in order based on how the user triggers these image swaps.  To me as a user, I would expect to be able to hover across the thumbs, without it impacting the order of the thumbnails.
Consider having images have their own class as well. This would address current inconsistency in having some elements be included in logic based on class while others become part of the logic based on element type.


AngularJS: Http API Service - any improvements / remarks? Could you please review the below AngularJS service. What it does is centralize all (relevant) HTTP action calls.

Do you see any chances for improvement?:

"use strict";

angular.module("services.api")
    .factory("apiService", [
        "$http",
        "$q",
        "coreUtilityService",
        "constantsServiceNames",
        "constantsApiRequestFor",
        function (
            $http,
            $q,
            coreUtilityService,
            constantsServiceNames,
            constantsApiRequestFor
            ) {
            // === START SERVICE === //

            var self = {};

            self.Name = constantsServiceNames.ApiService;

            self.ExecuteRequest = function(verb, fullUri, data) {
                var defer = $q.defer();
                verb = verb.toLowerCase();

                //start with the uri
                var httpArgs = [fullUri];
                if (verb.match(/post|put/)) {
                    httpArgs.push(data);
                }

                $http[verb].apply(null, httpArgs)
                .success(function (response) {
                    defer.resolve(response);
                })
                .error(function (response, status) {
                    defer.reject("HTTP Error: " + status);
                });

                return defer.promise;
            }

            /*
            Do a request to any RESTful receiver. Meant for external use only, use DoViewRequest
            for internal requests.
            */
            self.DoRequest = function (verb, uri, data) {
                var fullUrl = coreUtilityService.GetBaseUrl(constantsApiRequestFor.WebAPI, uri);

                return self.ExecuteRequest(verb, fullUrl, data);
            };

            /*
            This will target an MVC View of the client application. The url will be different.
            */
            self.DoViewRequest = function (verb, uri) {
                var fullUrl = coreUtilityService.GetBaseUrl(constantsApiRequestFor.View, uri);

                return self.ExecuteRequest(verb, fullUrl, null);
            };

            // === PUBLIC === //

            return {
                Get: function (uri) {
                    return self.DoRequest("get", uri);
                },
                Post: function (uri, data) {
                    return self.DoRequest("post", uri, data);
                },
                Put: function (uri, data) {
                    return self.DoRequest("put", uri, data);
                },
                Delete: function (uri) {
                    return self.DoRequest("delete", uri);
                },
                View: function (uri) {
                    return self.DoViewRequest("get", uri);
                }
            }

            // === END SERVICE === //
        }]);

A few ideas - nothing major :

Encapsulating the methods in self doesn't really add anything. A set of function statements or function expressions would do the job and self. could be purged all through.
The explicit promise construction antipattern should be purged.
"method" would be better understood than "verb".
Does DoRequest() not throw when data is not passed? Do range checking in doRequest() instead of ExecuteRequest().
Possibly allow for omitted uri, defaulting to empty string (browser will use current page's url). Could be useful in some use cases.
By convention Initial-caps are reserved for constructors. Change to lower-case all through.
Possibly use Function.prototype.bind() in the exposer. (Check that browser compatibility is acceptable).

angular.module("services.api").factory("apiService", [
"$http",
"$q",
"coreUtilityService",
"constantsServiceNames",
"constantsApiRequestFor",
function (
    $http,
    $q,
    coreUtilityService,
    constantsServiceNames,
    constantsApiRequestFor
) {
    // === START SERVICE === //
    // var name = constantsServiceNames.ApiService; // necessary?
    var executeRequest = function(method, fullUri, data) {
        method = method.toLowerCase();
        var httpArgs = [fullUri];
        if (data) {
            httpArgs.push(data);
        }
        return $http[method].apply(null, httpArgs).then(null, function (response) {
            throw new Error("HTTP Error: " + response.status);
        });
    };
    /*
    Do a request to any RESTful receiver. Meant for external use only, use DoViewRequest
    for internal requests.
    */
    var doRequest = function (method, uri, data) {
        uri = uri || '';
        data = (!data || !method.match(/post|put/)) ? null : data; // or similar
        return ExecuteRequest(method, coreUtilityService.GetBaseUrl(constantsApiRequestFor.WebAPI, uri), data);
    };
    /*
    This will target an MVC View of the client application. The url will be different.
    */
    var doViewRequest = function (method, uri) {
        uri = uri || '';
        var fullUrl = coreUtilityService.GetBaseUrl(constantsApiRequestFor.View, uri);
        return ExecuteRequest(method, fullUrl, null);
    };
    // === PUBLIC === //
    return {
        'get':    doRequest.bind(null, 'get'),
        'post':   doRequest.bind(null, 'post'),
        'put':    doRequest.bind(null, 'put'),
        'delete': doRequest.bind(null, 'delete'),
        'view':   doViewRequest.bind(null, 'get')
    };
    // === END SERVICE === //
}]);


Knuth-Morris-Pratt Search Algorithm 2 Python I tried again to implement the algorithm. Just need someone to check my work. I hope I didn't miss anything but my long work hours recently have been pretty taxing on my brain so I honestly wouldn't be surprised if I messed up somehow.

def KMP_table(wrd):
    #example: B A A B D C B A A A C A B -> [-1, 0, 0, -1, 1, 2, -1, 0, 0, 0, 4, 5, -1]
    table = []
    position, candidate = 0, 0
    while position < len(wrd):
        if wrd[candidate] == wrd[position]:
            table.append(-1)
            candidate += (position - candidate)
        elif wrd[position] == wrd[position - candidate] or candidate == 0:
            table.append(0)
        else:
            table.append(position - candidate)
        position += 1

    return table

def KMP_search(wrd, str):

    if not wrd or not str:
        raise ValueError("Empty lists")

    w, s = 0, 0
    lenWrd, lenStr = len(wrd), len(str)
    wrdPos = []

    table = KMP_table(wrd)
    while (s + lenWrd-1) < lenStr:
        if wrd[w] == str[w + s]:
            if w == lenWrd - 1:
               wrdPos.append(s)
               s += 1
               w = 0
            else:
                w += 1
        else:
            if table[w] > 0:
                s += (w - table[w])
            elif w == 0:
                s += 1
            else:
                s += w
            w = 0

    return wrdPos

naming

The name of a variable is part of the documentation of your code. Try to use meaningful names, that don't shadow built-ins.

wrd: sub (as in str.find)
str: whole
w: sub_index
s: whole_index

Generators

Instead of filling a list and returning that list, I prefer to work with generators in almost every case. If getting the result in a collection afterwards is very simple

Looping

in KMP_table you are essentially looping by index (position). Instead, loop over the word and enumerate

small things

candidate += (position - candidate) is essentially candidate = position
lenWrd is used twice, lenStr is used once, so they an be inline

result

def KMP_table(whole):
    # example: B A A B D C B A A A C A B -> [-1, 0, 0, -1, 1, 2, -1, 0, 0, 0, 4, 5, -1]
    candidate = 0
    for position, wrd_position in enumerate(whole):
        diff = position - candidate
        if whole[candidate] == wrd_position:
            yield -1
            candidate = position
        elif wrd_position == whole[diff] or candidate == 0:
            yield 0
        else:
            yield diff

def KMP_search(word, sub):
    if not word or not sub:
        raise ValueError("Empty lists")

    word_index, sub_index = 0, 0
    table = tuple(KMP_table(word))
    while (sub_index + len(word) - 1) < len(sub):
        if word[word_index] == sub[word_index + sub_index]:
            if word_index == len(word) - 1:
                yield sub_index
                sub_index += 1
                word_index = 0
            else:
                word_index += 1
        else:
            if table[word_index] > 0:
                sub_index += (word_index - table[word_index])
            elif word_index == 0:
                sub_index += 1
            else:
                sub_index += word_index
            word_index = 0

tuple(KMP_search('aba', 'aaababa'))

(2, 4)


Reducing array to average readings Taking the payload below, I need to output the average of each reading.

For example the readings for key 2 are 1, 4, 7.  The average is (1+4+7)/3 = 4.

The data is formatted in multi-dimensional arrays, such that each sub-array contains pairs with a key and a reading value. For example:

[
  [
    [2, 1],
    [4, 2],
    [6, 3]
  ],
  [
    [2, 4],
    [4, 5],
    [6, 6]
  ],
  [
    [2, 7],
    [4, 8],
    [6, 9]
  ]
]

The final result should be:

[
[2, 4],
[4, 5],
[6, 6]
]

My effort so far seems to work.

const payload = [
  [
    [2, 1],
    [4, 2],
    [6, 3]
  ],
  [
    [2, 4],
    [4, 5],
    [6, 6]
  ],
  [
    [2, 7],
    [4, 8],
    [6, 9]
  ]
]

const result = payload.reduce((accumulator, currentValue, currentIndex, array) => {

    if(currentIndex === 0){
        return accumulator
    }
    return currentValue.reduce((a, c, i) => {
        accumulator[i][1] += c[1]
        return accumulator
    }, accumulator)

 }).map((currentValue, index, array) => {
    return [currentValue[0], currentValue[1] / array.length]
})

Average calculation issue

The example data happens to be a 3x3 matrix of values, so the calculation of the average appears to be correct. However, try using a 2x3 matrix of values (i.e. remove the 3rd sub-array of values) - it should become clear that the divisor used in the calculation of the average is incorrect.

One should divide the sum by the number of readings corresponding to each key, not the number of keys in each sub-array- i.e. divide by payload.length instead of array.length in the callback to Array.map().

Original Array Modification

le_m is correct with the statement in the comment:

  Above payload.reduce(...) mutates the first element of payload - that is probably unexpected behavior and could cause bugs further on.

This is because the initialValue parameter of Array.reduce() is omitted, which causes payload[0] to be used as accumulator the first time 1. This can be observed by inspecting the values of currentIndex in the callback - for the sample data, it starts with 1 and ends with 2.

In order to not mutate the first element of payload, a deep clone of the first element can be used (e.g. with JSON.parse() and JSON.stringify()).

let initialValue = [];
if (payload.length) {
    initialValue = JSON.parse(JSON.stringify(payload[0]));
}
const result = payload.reduce((accumulator, currentValue, currentIndex) => {
    /*...*/
}, initialValue)).map((currentValue, index, array) => {
    return [currentValue[0], currentValue[1] / array.length]
});

1https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Parameters


Intersection of words I get a map from DB called doc1 and I also have the arrayList called someWord.  I will find the subscription of doc1 and someWord and store it in doc2.  I will also add the intersecting words with zero index in doc2.

This code consumes too much time:

public void setDocuments() {
    // Neighbor contain the arrayList of Documents
    neighbors.add(new Neighbor<Integer>("sky"));
    neighbors.add(new Neighbor<Integer>("earth"));

    Map<Long, String> docInfo;
    // Document is a Map
    Document<String> doc1 = new Document<String>();
    Document<Integer> doc2;
    Map<Integer, Long> sorted;
    ArrayList<String> intersectionWords;
    HashSet<String> different;
    try {
        docInfo = dbDocMeta.getDocInfo();
        String family;
        int index = 0;
        for (Long id : docInfo.keySet()) {
            family = docInfo.get(id).contains("sky") ? "sky" : "earth";
            for (Neighbor<Integer> neighbor : neighbors) {
                // check if the document in the Neighbour family
                if (neighbor.getFamily().equalsIgnoreCase(family)) {
                    different = new HashSet<String>();
                    different.addAll(someWord);
                    doc1 = dbWords.getNeighbors(id);
                    doc2 = new Document<Integer>();
                    intersectionWords = new ArrayList<String>();
                    for (String word : doc1.getMap().keySet()) {
                        index = dbWords.getIndex(word);
                        if (selectedFeatures.contains(word)) {
                            doc2.add(index, doc1.getAttributes().get(word));
                            intersectionWords.add(word);
                        }
                    }
                    if (!doc2.getMap().isEmpty()) {
                        different.removeAll(intersectionWords);
                        Iterator<String> iterator = different.iterator();
                        while (iterator.hasNext()) {
                            String s = iterator.next();
                            index = dbWords.getIndex(s);
                            doc2.add(index, 0);
                        }
                        sorted = new TreeMap<Integer, Long>(doc2.getAttributes());
                        doc2.setMap(sorted);
                        neighbor.addDocument(doc2);
                        break;
                    }
                }
            }
        }
    } catch (InterruptedException e) {
        logger.error("InterruptedException msg : {} \n {}", e.getMessage(), e.getStackTrace());
    } catch (ClassNotFoundException e) {
        logger.error("ClassNotFoundException msg : {} \n {}", e.getMessage(), e.getStackTrace());
    }
}

Neighbour variables are:

private ArrayList<Document<T>>  documents;
private int                     docCount;
private String                  family;

It also contains some setters and getters, and dbDocMeta contains the ID of all Documents.

You should declare variables in the smallest possible scope. E.g. instead of

String family;
for  (...) {
    family = ...;
    ...
}

we could do

for (...) {
    String family = ...;
    ...
}

The same applies to docInfo, doc1, doc2, sorted, intersectionWords, different, and index.

You have this snippet:

Iterator<String> iterator = different.iterator();
while (iterator.hasNext()) {
    String s = iterator.next();
    ...
}

which could be abbreviated to for (String s : different) { ... }.

If we clean up these two points, we end up with this:

public void setDocunents() {
    // Neighbor contain the arrayList of Documents
    neighbors.add(new Neighbor<Integer>("sky"));
    neighbors.add(new Neighbor<Integer>("earth"));

    try {
        Map<Long, String> docInfo = dbDocMeta.getDocInfo();
        for (Long id : docInfo.keySet()) {
            String family = docInfo.get(id).contains("sky") ? "sky" : "earth";
            for (Neighbor<Integer> neighbor : neighbors) {
                // check if the document in the Neighbour family
                if (neighbor.getFamily().equalsIgnoreCase(family)) {
                    HashSet<String> different = new HashSet<String>();
                    different.addAll(someWord);
                    Document<String> doc1 = dbWords.getNeighbors(id);
                    Document<Integer> doc2 = new Document<Integer>();
                    ArrayList<String> intersectionWords = new ArrayList<String>();
                    for (String word : doc1.getMap().keySet()) {
                        int index = dbWords.getIndex(word);
                        if (selectedFeatures.contains(word)) {
                            doc2.add(index, doc1.getAttributes().get(word));
                            intersectionWords.add(word);
                        }
                    }
                    if (!doc2.getMap().isEmpty()) {
                        different.removeAll(intersectionWords);
                        for (String s : different) {
                            int index = dbWords.getIndex(s);
                            doc2.add(index, 0);
                        }
                        Map<Integer, Long> sorted = new TreeMap<Integer, Long>(doc2.getAttributes());
                        doc2.setMap(sorted);
                        neighbor.addDocument(doc2);
                        break;
                    }
                }
            }
        }
    } catch (InterruptedException e) {
        logger.error("InterruptedException msg : {} \n {}", e.getMessage(), e.getStackTrace());
    } catch (ClassNotFoundException e) {
        logger.error("ClassNotFoundException msg : {} \n {}", e.getMessage(), e.getStackTrace());
    }
}

In a next step, we remove calculations from inner loops that don't have to be recalculated each time. E.g. doc1 = dbWords.getNeighbors(id) depends only on id, and probably does not have to be called for each neighbor. On the other hand, different is only needed inside a specific if branch.

Furthermore, doc2 will be discarded unless that branch is entered, so we will build it only in that case (assuming doc2.getMap().isEmpty() is equivalent to intersectionWords.isEmpty().

We can further remove unnecessary variables – their types just add clutter to the code.

A bare break inside nested loops can be a bit confusing, so we'll use the labelled for: break neighbors.

The HashSet constructor can directly take a collection, there is no need to separately call addAll.

If the whole body of a loop us subject to an if (cond), it is often easier to read when at the beginning of a loop, we if (!cond) continue.

Unrelated implementation details like calculating the intersection of two sets should be factored out into helper functions.

public void setDocunents() {
    // Neighbor contain the arrayList of Documents
    neighbors.add(new Neighbor<Integer>("sky"));
    neighbors.add(new Neighbor<Integer>("earth"));

    try {
        Map<Long, String> docInfo = dbDocMeta.getDocInfo();
        for (Long id : docInfo.keySet()) {
            String family = docInfo.get(id).contains("sky") ? "sky" : "earth";
            Document<String> doc1 = dbWords.getNeighbors(id);

            neighbors:
            for (Neighbor<Integer> neighbor : neighbors) {
                // check if the document in the Neighbour family
                if (!neighbor.getFamily().equalsIgnoreCase(family)) {
                    continue neighbors;
                }

                Set<String> intersectionWords = intersection<String>(doc1.getMap().keySet(), selectedFeatures);
                if (intersectionWords.isEmpty()) {
                    continue neighbors;
                }

                HashSet<String> different = new HashSet<String>(someWord);
                Document<Integer> doc2 = new Document<Integer>();
                for (String s : intersectionWords) {
                    doc2.add(dbWords.getIndex(word), doc1.getAttributes().get(word));
                    different.remove(s);
                }
                for (String s : different) {
                    doc2.add(dbWords.getIndex(s), 0);
                }
                // due to the choice of a TreeMap, the elements are sorted
                doc2.setMap(new TreeMap<Integer, Long>(doc2.getAttributes()));
                neighbor.addDocument(doc2);

                break neighbors;
            }
        }
    } catch (InterruptedException e) {
        logger.error("InterruptedException msg : {} \n {}", e.getMessage(), e.getStackTrace());
    } catch (ClassNotFoundException e) {
        logger.error("ClassNotFoundException msg : {} \n {}", e.getMessage(), e.getStackTrace());
    }
}

private static <A> Set<A> intersection(final Set<A> xs, final Set<A> ys) {
    // make sure that xs is the smaller set
    if (ys.size() < xs.size()) {
        return intersection<A>(ys, xs);
    }

    final HashSet<A> result = new HashSet<>();
    for (A x : xs) {
        if (ys.contains(x)) {
            result.add(x)
        }
    }

    return result;
}

This could be optimized further, e.g. when doc2.add(…) has no side effects except setting the underlying map, or if the composition of someWord is known.


Python client to fetch JSON data from REST API, retrying on failure As a beginner, I'd like to see if anyone can suggest any better way of coding the below, as I'm duplicating code and nesting identical if statements as part of the following problem.

I'm grabbing data from a REST API, which is returned in JSON format. However, the query may fail. One cause of failure is that the authorization token obtained earlier has expired. In this case, I want to get a new token, and retry the query. Then I need to reprocess the response, which may still be a failure (but, this time, not because of an expired token).

So, I've written this, but I'm thinking there might be a more elegant way to write it to avoid code repeats:

# Firstly, I get the data, which returns a dict from a JSON response
response = get_data(authorization)
if response["responseStatus"] == "FAILURE":
    # I believe I need to check this in a separate if statement
    # because even though there's always a responseStatus key
    # it will only contain an errors key if the former equals FAILURE
    if response["errors"][0]["type"] == 'INVALID_SESSION_ID':
        print("Session expired, initiating new session.")
        # I call the function that grabs a new Authorization token
        authorization = get_authorization(username, password)
        # Then I call again the function that gets the data
        response = get_data(authorization)
        # And now I need to look again for a possible failure
        if response["responseStatus"] == "FAILURE":
            print(response["errors"])
    else:
        print(response["errors"])
# Here I couldn't do a simple else because I need to process
# success for either calls of get_data() above (before or
# inside the if block)
if response["responseStatus"] == "SUCCESS":
    process_data()

EDIT: Answer updated to use a while-loop rather than recursion, as noted in the comments. I also recently learned about the max recursion depth in Python being a good argument against recursion in the first place.

One way you could solve this is by separating the error handling logic into its own function, and then repeatedly calling a connection function:

# Handles an error in the given response. Returns True if we should try connecting again
def handle_error(response):
    if response["errors"][0]["type"] == 'INVALID_SESSION_ID':
        print("Session expired, initiating new session.")
        authorization = get_authorization(username, password)
        return True
    return False

def connect():
    attempt_connection = True
    while attempt_connection:
        response = get_data(authorization)
        if response["responseStatus"] == "SUCCESS":
            attempt_connection = False
            process_data()
        elif response["responseStatus"] == "FAILURE":
            attempt_connection = handle_error(response)
            if not attempt_connection:
                print(response["errors"])

handle_error could, of course, be split up into more functions, one for each potential error. Depending on the response format, you may also want to check for each key in response before accessing it.

You could also specify retry as a number of times to reconnect before giving up, i.e if you were attempting to connect over multiple proxies. Then you would just decrement the counter after each failure, and print the error when the counter hits zero.


Unity DI Composition Root vs XML I'm having trouble figuring out the best approach with DI. After doing more research, Mark Seeman recommends using the Composition Root approach. In my example, I am using XML configuration to design my mappings which allows me to drop any new dll that implements an interface without having to perform a build on the whole solution. If I use a composition root, doesn't it become a violation of Liskov's principle since I have to manually make changes and then perform a rebuild?

Here is a link to my GitHub project

Interface Shared By all Projects

namespace UnityRnD.Core
{
    public interface IGreeting
    {
        string GetGreeting();
    }
}

Main Application

namespace UnityRnD.Application
{
    class Program
    {
        private static void Main(string[] args)
        {

            var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unitySingleNamed");
            IUnityContainer container = new UnityContainer();
            container.LoadConfiguration(section);

            var letsTalk = container.Resolve<IGreeting>("tom");
            Console.WriteLine(letsTalk.GetGreeting());
        }
    }
}

The Plan

The plan is to create a bunch of projects that represent a Line of Business. Each project will have a class that implements IGreeting. I can then drop that library into the bin and add a Unity config setting to point to that project. On the resolve where it says "tom" this can become a variable which will remove that hard reference to the string "tom."

Stacey and Tom projects

namespace UnityRnD.Tom
{
    public class Greetings : IGreeting
    {
        public string GetGreeting()
        {
            return "Hi, my name is Tom.";
        }
    }
}

Michael Project
notice the class name changes.

namespace UnityRnD.Michael
{
    public class ChattyKathy : IGreeting
    {
        public string GetGreeting()
        {
            return "Hi I'm Mike and I like pizza. Lets talk about pizza for a moment.";
        }
    }
}

I'm looking for help on whether to create a new project for a Composition Root to hold all the references.  Does the main application need hard references to all of them, or does using the XML configuration suffice?

The composition root is just the place in your code where you'll compose the application and resolve the entire dependency graph at once. You'll want that as close as possible to the application's entry point - the Main method is perfect for this in a console application.

Regardless of your IoC container, the first thing you need to do, is to configure it - to tell it what implementation to supply for the constructor-requested interfaces.

That's the key right here: constructor injection. Your IoC container is the object responsible for owning the instances of everything under its wing. The resolution phase resolves services, with their own dependencies; you don't resolve a string, you resolve an interface that presents a string.

Your entire program lives and dies inside the Main method - your example is too trivial to demonstrate the benefits of DI/IoC, I don't think it's a good example.

Imagine you have a data layer, a business layer and a presentation layer. In a non-IoC code base the BLL will have a dependency on the DAL, and will consume its API; the presentation layer will probably run the show, and have a dependency on the BLL.

With IoC you revert the dependencies. The BLL doesn't need to know about a DAL. Instead the DAL has a dependency on the BLL, and it implements the interfaces that the BLL is coded against.

The presentation layer is the logical place to compose the application, if your application's entry point is typically right there.

The composer assembly needs to have references to all the assemblies that basically provide the implementations - nothing forbids creating a separate assembly just for wiring up the application and starting it.

If the composition root is in its own assembly, it's all you need to recompile. I like defining my mappings with code, because I get IntelliSense and compile-time validity.

Unfortunately there's not much code to review in your question, ...we can't review what you haven't written yet! Good luck!

Please come back when you've implemented a functionality, right now I don't think your project isn't ready for peer review.

(cough) that was too long for a comment ;)


Find all paths in a graph My code does a DFS on the given graph and prints all possible paths for a given start and end node. My question is how can this be improved?

Can some form of memoization be used here? There are cases where a path from a certain source - destination is already found and will be an intermediate path for a different source - destination but it is still computed again.
As it is a generator will that make memoization different?
Is there an already existing text book algorithm that I am missing? Does not have to be specifically DFS.
Pythonic / Idiomatic code suggestions are also welcome.

This is my code
from itertools import product

def find_path(g, src, dst):
    """Prints all possible path for a graph `g` for all pairs in `src` and `dst`

    Args:
        g (list): 2d list, graph
        src (list): list of nodes that will be the source
        dst (list): list of nodes that will be the destination
    """

    graph = {}

    # constructing a graph
    for from_, to_ in g:
        graph.setdefault(from_, set()).add(to_)
        graph.setdefault(to_, set()).add(from_) # undirected

    def dfs(src, dst, path, seen):
        """Recursive generator that yields all paths from `src` to `dst`

        Args:
            src (int): source node
            dst (int): destination node
            path (list): path so far
            seen (set): set of visited nodes

        Yields:
            list: all paths from `src` to `dst`
        """
        if src in seen:
            return

        if src == dst:
            yield path + [src]
            return

        seen.add(src)

        for i in graph.get(src, []):
            yield from dfs(i, dst, path + [src], seen)
        seen.remove(src)

    # start finding path for all `src` `dst` pairs
    for source, destination in product(src, dst):
        print(source, destination, list(dfs(source, destination, [], set())))

g = list(product(range(1, 5), repeat=2)) # a graph for testing purpose
src = dst = range(1, 5) # source and destination, in actual code this will be a list
find_path(g, src, dst)

I don't think memoization will be useful here. Any correct algorithm is going to be O(n!), nothing you do can reduce that fundamentally because you still have to output all the paths.
Trying to cache stuff you already output is just going to make the space-costs worse, it won't make the processing-time any better.
The advantages of using a generator are that the caller doesn't have to wait for the whole computation to finish before handling the early outputs, and that we never have to store the results of the whole computation at once. This is a good choice for the task at hand!
It doesn't affect the point in (1.) though.
You seem to have already found the textbook algorithm.
There are various things to make the code prettier, but you'd have to do some testing to see if they make it work better.

Types are nice. They do nothing at runtime, but they let MyPy check for many kinds of mistakes.
I don't like the redundancy of path and seen, and I don't like that you're mutating seen the way you are, but I suspect the way you've written it is actually the most efficient for larger graphs.
The expression path + [src] makes two calls to the list constructor, and it's happening inside the for loop. That can be improved!
Since you never mutate a value of path, it can be a tuple instead of a list.
The way you have it set up now, your adjacencies dict will always contain src, so you don't need to use get.

I'm ignoring your outer wrapper, and I've made no effort to test this:
from typing import Iterable, Mapping, MutableSet, Sequence, TypeVar
N = TypeVar('N')
def dfs(source: N,  # The current node
        destination: N,  # The destination node
        path: Sequence[N],  # The path taken to the current node
        seen: MutableSet[N],  # The nodes visited on the path
        graph: Mapping[N, Iterable[N]]  # The graph being explored, as an adjacency dict
) -> Iterable[Sequence[N]]:
    new_path = (*path, source)
    if source == destination:
        yield new_path
    else:
        seen.add(source)
        for n in graph[source]:
            if n not in seen:
                yield from dfs(n, destination, new_path, seen, graph)
        seen.remove(source)


Good way to write extend method of a contructor? I've been following along a blog post on JavaScript inheritance. I wanted to slightly adapt this and try to write something more like I've seen in Backbone (extend method). See I set about it, and run into problems. Then I came across this SO post. It was so much simpler. So after some minor tweaks to that I set about writing a way each extended constructor could have their own extend method (inherited from Base). This is what I have:

var Base = function(name){
    this.name = name;
}
Base.prototype.extend = function(new_constructor){
    //new_constructor.prototype = new this.constructor();
    new_constructor.prototype = Object.create(this.constructor.prototype);
    new_constructor.prototype.constructor = new_constructor;

    return new_constructor;
}

var Robot = Base.prototype.extend(function(name, material){
    Base.call(this, name)
    this.material = material
    this.type = 'robot';
});

var T1000 = Robot.prototype.extend(function(options){
    Robot.call(this, options.name, options.material)
    this.guns = options.guns;
    this.type = 'killer robot';
});

robot = new Robot("Boutros", "metal");
t1000 = new T1000({name: "Arnie", material:"flesh/metal", guns:3});

console.log(robot)
console.log(t1000)

Anyway seems to work, but I'm a little new to this. Can anyone suggest any limitations with this approach. Also, I'd like to move the [Constructor].call out of the new constructor function and into the extend function so this is handled automatically, not sure if I can. Would appreciate your feedback.

Btw, here is my jsFiddle

I think you cannot move parent constructor call out of child constructor. At least because you have different parameters, extend function cannot guess what kind of parameters it should pass.

But even if you standardize all constructors, you will have to make a "smart" constructor in Base class, that know about possible "sub-constructors". I don't think its a good idea ))

But what i would do - remove any mention of parent-class from children constructors'.

var Base = function(name){
    this.name = name;
}
Base.prototype.extend = function(new_constructor){
    //new_constructor.prototype = new this.constructor();
    new_constructor.prototype = Object.create(this.constructor.prototype);
    new_constructor.prototype.constructor = new_constructor;
    new_constructor.prototype.parentConstructor = this.constructor;

    return new_constructor;
}

var Robot = Base.prototype.extend(function(name, material){
    Robot.prototype.parentConstructor.call(this, name)
    this.material = material
    this.type = 'robot';
});

var T1000 = Robot.prototype.extend(function(options){
    T1000.prototype.parentConstructor.call(this, options.name, options.material)
    this.guns = options.guns;
    this.type = 'killer robot';
});

robot = new Robot("Boutros", "metal");
t1000 = new T1000({name: "Arnie", material:"flesh/metal", guns:3});

console.log(robot)
console.log(t1000)


Extracting the longest line from standard input I'm reading the K&R book and I'm interested in a program which finds the longest line on the input. I've written the same code as in the book, but I wanted to do it myself. I tried to write my own code to do same thing. I think it's working, but my question is whether my code is "good" or if it's written in a "bad way" and can be written easily.

K&R Code:

#include <stdio.h>
#define MAXLINE 1000

int getline(char line[], int maxline);
void copy(char to[], char from[]);

main () {

 int len;
 int max;
 char line[MAXLINE];
 char longest[MAXLINE];

 max = 0;

 while ((len = getline(line, MAXLINE)) > 0) {

   if(len > max) {

     max = len;
     copy(longest, line);

   }

 }

 if(max > 0) printf("%s", longest);

 return 0;
}

int getline (char line[], int limit) {

 int i, c;

 for (i = 0;  i < limit - 1 && (c = getchar()) != EOF && c != '\n';  i++) line[i] = c;

 if (c == '\n') {

  line[i] = c;
  i++;

 }

 line[i] = '\0';

 return i;
}

void copy(char to[], char from[]) {

  int i;

  i = 0;
  while((to[i] = from[i]) != '\0')
    i++;
}

My code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1000

int main()
{
    int ch; //char input
    int length; //length of the line
    int max=0; //length of the longest line
    int j=0;
    int i=0;
    char line[MAX_LINE]; //line input
    char help [MAX_LINE];
    char longest [MAX_LINE]; //longest line

    while ((ch=getchar())!='\n'){ //first line
        longest[i]=ch;
        ++i;
    }

    max=i;
    i=0;
    length=0;
    while ((ch=getchar())!=EOF){
        line[i]=ch;
        help[j]=ch;
        ++j;
        ++i;
        ++length;
        if (ch=='\n'){
                help[j]='\0';
            if (length-1>max){
                max=length-1;
                j=0;
                printf("Longest -> %s",help);
                }
            else {
                memset(help,0,100); //erase elements
            }
            length=0;
        }

    }
    return 0;
}

I see some things that may help you improve your code.

Eliminate unused variables

Unused variables are a sign of poor quality code, and you don't want to write poor quality code. In this code, line and longest are unused -- their values are set but then never referenced again.  Your compiler is smart enough to tell you about this if you ask it nicely.

Think carefully about variable use

After the first line is read, the variable i isn't really needed.  The way it is now, it could instead simply increment max directly.

Think carefully about the task

If you need to save the longest line, you can't overwrite it every time.  The difference between yours and the K&R version is that yours prints multiple times and only the last time it prints is actually the longest line.

Check for unusual inputs

What happens if the file consists of a single long line?   The existing code will fail because the code that reads the first line fails to stop at the end of the file.  Also none of the code checks for lines that are too long.

Break up the code into smaller functions

The main() function does a series of identifiable steps.  Rather than having everything in one long function, it would be easier to read and maintain if each discrete step were its own function.

Don't repeat yourself

There's really no need to handle the first line any differently than any other line.  Instead, you could simply run the main loop.

Minimize work

There's no reason to clear the entire line array.  Instead of the memset, your code could simply do this:

help[0] = '\0';

Choose better variable names

The name help is not at all appropriate to its actual function.  I'd recommend using line for everywhere that help is currently used.

Use modern syntax

The old K&R style was to declare all variables at the top of the function, but that's not any longer either required or desired.  Instead, current practice is to declare variables near their first use and to keep their scope small.

Use modern C idioms

Right now the code contains these lines:

help[j]=ch;
++j;

However, that's more idiomatically written like this:

help[j++] = ch;

Fix your formatting

There are inconsistent spaces in variable declarations, and inconsistent indentation. Being consistent helps others read and understand your code.

Use more whitespace

Instead of writing a line like this:

while ((ch=getchar())!=EOF){

use a bit more whitespace and write it like this:

while ((ch = getchar()) != EOF) {

Doing so will make it easier to read and understand your program.

Eliminate return 0 at the end of main

Since C99, the compiler automatically generates the code corresponding to return 0 at the end of main so there is no need to explicitly write it.


Extracting spreadsheet cells using Apache POI I'm working with Apache POI framework.

This code go through sheets, row and get value of cells. I will try to get value from directly indicated column. I need this because I want convert only some information from xls to csv.

FileOutputStream fos = new FileOutputStream(outputFile);

Workbook workbook = null;
for (File input : inputFile) {
  String ext = FilenameUtils.getExtension(input.toString());

  if (ext.equalsIgnoreCase("xlsx")) {
    workbook = new XSSFWorkbook(new FileInputStream(input));
  } else if (ext.equalsIgnoreCase("xls")) {
    workbook = new HSSFWorkbook(new FileInputStream(input));
  }

  int numberOfSheets = workbook.getNumberOfSheets();
  Row row;
  Cell cell;
  Sheet sheet = null;
  int[] kolumny = {1, 2, 3, 4, 5, 6, 7, 50, 51, 52, 53, 54, 55};

  for (int i = 1; i < numberOfSheets; i++) {
    sheet = workbook.getSheetAt(i);
    if (sheet.getLastRowNum() > 24) {
      Iterator<Row> rowIterator = sheet.iterator();
      while (rowIterator.hasNext()) {
        row = rowIterator.next();
        for (int j = 23; j < sheet.getLastRowNum(); j++) {
          if (row.getRowNum() == j) {
            if (row.getCell(1) == null || row.getCell(1).getCellType() == Cell.CELL_TYPE_BLANK) {
              break;
            }
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
              cell = cellIterator.next();
              for (int kolumna : kolumny) {
                if (cell.getColumnIndex() == kolumna) {
                  if (cell != null) {
                    switch (cell.getCellType()) {
                      case Cell.CELL_TYPE_BOOLEAN:
                        data.append(cell.getBooleanCellValue() + ",");
                        break;
                      case Cell.CELL_TYPE_NUMERIC:
                        data.append(cell.getNumericCellValue() + ",");
                        break;
                      case Cell.CELL_TYPE_STRING:
                        data.append(cell.getStringCellValue() + ",");
                        break;
                      case Cell.CELL_TYPE_BLANK:
                        data.append("" + ",");
                        break;
                      default:
                        data.append(cell + ",");
                    }
                  }
                }
              }
            }
            data.append('\n');
          }
        }
      }
    }
  }
}

You could extract the content of inner loops to methods with speaking names.

E.g. You could introduce a method like

public static void processSheet(Sheet sheet)
{
  if (sheet.getLastRowNum() > 24) {
      Iterator<Row> rowIterator = sheet.iterator();
      while (rowIterator.hasNext()) {
        row = rowIterator.next();
  ...

}

Within this method you can extract another method e.g.

public static void processRow(Row row){
  for (int j = 23; j < sheet.getLastRowNum(); j++) {
    if (row.getRowNum() == j) {
      if (row.getCell(1) == null || row.getCell(1).getCellType() == Cell.CELL_TYPE_BLANK) {
        break;
    ...

}

And so on until you have only short methods with speaking names. This makes the code easier to read and understand.