Find duplicates in a list of tuples You are given information about users of your website. The information
  includes username, a phone number and/or an email. Write a program
  that takes in a list of tuples where each tuple represents information
  for a particular user and returns a list of lists where each sublist
  contains the indices of tuples containing information about the same
  person. For example:

  Input:

[("MLGuy42", "andrew@gmail.com", "123-4567"),
("CS229DungeonMaster", "123-4567", "ml@stanford.edu"),
("Doomguy", "john@oculus.com", "carmack@gmail.com"),
("andrew26", "andrew@gmail.com", "mlguy@gmail.com")]

  Output:

[[0, 1, 3], [2]]

  Since "MLGuy42", "CS229DungeonMaster" and "andrew26" are all the same
  person.

  Each sublist in the output should be sorted and the outer list should
  be sorted by the first element in the sublist.

Below is the code snippet that I did for this problem. It seems to work fine, but I'm wondering if there is a better/optimized solution.

def find_duplicates(user_info):
    results = list()
    seen = dict()
    for i, user in enumerate(user_info):
        first_seen = True
        key_info = None
        for info in user:
            if info in seen:
                first_seen = False
                key_info = info
                break
        if first_seen:
            results.append([i])
            pos = len(results) - 1
        else:
            index = seen[key_info]
            results[index].append(i)
            pos = index
        for info in user:
            seen[info] = pos
    return results

There is a way to increase readability.  A for-else statement can be used to replace flags that are only set on break.  Click here to learn more.  The else clause is only called if the loop completes without break being called.

def find_duplicates(user_info):
    results = list()
    seen = dict()
    for i, user in enumerate(user_info):
        for info in user:
            if info in seen:
                index = seen[info]
                results[index].append(i)
                pos = index
                break
        else:
            results.append([i])
            pos = len(results) - 1
        for info in user:
            seen[info] = pos
    return results

How I tested speeds

Your code, as it currently stands, will work exactly the same with integers. So, I just randomly generated large data sets this way (and played along with MAXVALUE and range a bit.

from random import randrange
from timeit import timeit
MAXVALUE = 1000

for a in range(5): #I wanted to make sure I checked 5 to make sure I don't get a outlier data set that effects my ability to use timeit reasonably.
    user_info = [[randrange(MAXVALUE) for i in range(3)] for _ in range(1000)]
    print(timeit(lambda: find_duplicates(user_info), number=10000))

Credit for improving code: Maarten Fabré


Scanning a directory for plugins and loading them I'm working on a simple dictionary tool, with a base class that can be extended by plugins to represent different dictionaries. The plugins are organized in the filesystem like this:

plugins/
├── dict1
│   ├── ...
│   └── dict1.py
└── dict2
    ├── ...
    └── dict2.py

Each plugin defines a class named Dictionary, which happens to extend a BaseDictionary, in case that's relevant.

I implemented discovering and loading the plugins like this:

import os
from imp import find_module, load_module

BASE_DIR = os.path.dirname(__file__)
PLUGINS_PATH = os.path.join(BASE_DIR, 'plugins')

def discover_dictionaries():
    import plugins
    for plugin_name in os.listdir(PLUGINS_PATH):
        plugin_path = os.path.join(PLUGINS_PATH, plugin_name, plugin_name + '.py')
        if os.path.isfile(plugin_path):
            try:
                fp, pathname, description = find_module(plugin_name, plugins.__path__)
                m1 = load_module(plugin_name, fp, pathname, description)
                fp, pathname, description = find_module(plugin_name, m1.__path__)
                m2 = load_module(plugin_name, fp, pathname, description)
                class_ = getattr(m2, 'Dictionary')
                yield plugin_name, class_()
            except ImportError:
                print('Error: could not import Dictionary from {0}'.format(plugin_path))

This works, but it's kind of ugly, and probably I'm doing it all wrong. How to do this the "right way," or at least better?

If you want to play with the code to see if your refactoring ideas work, clone the project from GitHub, and run this as a sanity check:

# should output: dummy
python -c 'import util; print util.discover_dictionaries().next()[0]'

I started a project with a structure similar to the one you detailed:

My first order of business was to move the import plugins out of the discover_dictionaries() function and to the top of the file.

My second order of business was to create an iterator which would iterate over the discover_dictionaries() generator and print what it had found:

for next_plugin in discover_dictionaries():
    print next_plugin

My third order of business was to create empty classes in each of the dict* paths, which looked similar to:

import os

class Dictionary:
    def __init__(self):
        pass

Following that, I added a print to the generator to see what it saw:

def discover_dictionaries():
    for plugin_name in os.listdir(PLUGINS_PATH):
        plugin_path = os.path.join(PLUGINS_PATH, plugin_name, plugin_name + '.py')
        print plugin_path

This gave me the following output:

C:/Users/jsanc623/PycharmProjects/AutoloadModules\plugins\dict1\dict1.py
C:/Users/jsanc623/PycharmProjects/AutoloadModules\plugins\dict2\dict2.py
C:/Users/jsanc623/PycharmProjects/AutoloadModules\plugins\dict3\dict3.py
C:/Users/jsanc623/PycharmProjects/AutoloadModules\plugins\__init__.py\__init__.py.py
C:/Users/jsanc623/PycharmProjects/AutoloadModules\plugins\__init__.pyc\__init__.pyc.py

If I put the same print statement inside the if os.path.isfile(plugin_path): block, the output was:

('dict1', <dict1.Dictionary instance at 0x0000000001FABA48>)
('dict2', <dict2.Dictionary instance at 0x0000000001FB04C8>)
('dict3', <dict3.Dictionary instance at 0x0000000001FB0F08>)

So, it does work. Now, let's see how we can clean it up.

The first thing I did was to get rid of BASE_DIR:

BASE_DIR = os.path.dirname(__file__)
PLUGINS_PATH = os.path.join(BASE_DIR, 'plugins')

as follows:

PLUGINS_PATH = os.path.join(os.path.dirname(__file__), 'plugins')

Next, we can get rid of class_ by replacing the yield statement from:

class_ = getattr(m2, 'Dictionary')
yield plugin_name, class_()

To a much simpler:

yield plugin_name, getattr(m2, 'Dictionary')()

Next, I added an if statement to return if it encountered a 'pyc' or 'init' file:

for plugin_name in os.listdir(PLUGINS_PATH):
    if 'pyc' in plugin_name or 'init' in plugin_name:
        return
    else:

We can also get rid of m1, by casting pathname:

fp, pathname, description = find_module(plugin_name, plugins.__path__)
fp, pathname, description = find_module(plugin_name, [pathname])
m2 = load_module(plugin_name, fp, pathname, description)

But, we can get even sneakier and get rid of the assignment on the first find_module by doing so:

fp, pt, dc = find_module(plugin_name, [find_module(plugin_name, plugins.__path__)[1]])
m2 = load_module(plugin_name, fp, pt, dc)

Which, granted makes our lines a little long. However, let's get a little crazier with our yield:

yield plugin_name, getattr(load_module(plugin_name, fp, pt, dc), 'Dictionary')()

So, in the end, the original would now look like:

import os
from imp import find_module, load_module
import plugins

PLUGINS_PATH = os.path.join(os.path.dirname(__file__), 'plugins')

def discover_dictionaries():
    for plugin_name in os.listdir(PLUGINS_PATH):
        if 'pyc' in plugin_name or 'init' in plugin_name:
            return
        else:
            plugin_path = os.path.join(PLUGINS_PATH, plugin_name, plugin_name + '.py')
            if os.path.isfile(plugin_path):
                try:
                    f, p, d = find_module(plugin_name, [find_module(plugin_name, plugins.__path__)[1]])
                    yield plugin_name, getattr(load_module(plugin_name, f, p, d), 'Dictionary')()
                except ImportError:
                    print('Error: could not import Dictionary from {0}'.format(plugin_path))

And it would still function. If you want to make it a bit more concise at the cost of iterating over 'pyc' and 'init' files, you can omit the if/else statement:

import os
from imp import find_module, load_module
import plugins

PLUGINS_PATH = os.path.join(os.path.dirname(__file__), 'plugins')

def discover_dictionaries():
    for plugin_name in os.listdir(PLUGINS_PATH):
        plugin_path = os.path.join(PLUGINS_PATH, plugin_name, plugin_name + '.py')
        if os.path.isfile(plugin_path):
            try:
                f, p, d = find_module(plugin_name, [find_module(plugin_name, plugins.__path__)[1]])
                yield plugin_name, getattr(load_module(plugin_name, f, p, d), 'Dictionary')()
            except ImportError:
                print('Error: could not import Dictionary from {0}'.format(plugin_path))

Readability out of the window, but we save assignments?


Creating a list from combinations of the original data I am trying to create a list based on some data, but the code I am using is very slow when I run it on large data. So I suspect I am not using all of the Python power for this task. Is there a more efficient and faster way of doing this in Python?

Here an explanantion of the code:

  You can think of this problem as a list of games (list_type) each with a list of participating teams and the scores for each team in the game (list_xx).For each of the pairs in the current game it first calculate the sum of the differences in score from the previous competitions (win_comp_past_difs); including only the pairs in the current game.  Then it update each pair in the current game with the difference in scores. Using a defaultdict keeps track of the scores for each pair in each game and update this score as each game is played.

In the example below, based on some data, there are for-loops used to create a new variable list_zz.

The data and the for-loop code:

import pandas as pd
import numpy as np
from collections import defaultdict
from itertools import permutations

list_type = [['A', 'B'], ['B'], ['A', 'B', 'C', 'D', 'E'], ['B'], ['A', 'B', 'C'], ['A'], ['B', 'C'], ['A', 'B'], ['C', 'A', 'B'], ['A'], ['B', 'C']]

list_xx = [[1.0, 5.0], [3.0], [2.0, 7.0, 3.0, 1.0, 6.0], [3.0], [5.0, 2.0, 3.0], [1.0], [9.0, 3.0], [2.0, 7.0], [3.0, 6.0, 8.0], [2.0], [7.0, 9.0]]

list_zz= []
#for-loop
wd = defaultdict(float)
for i, x in zip(list_type, list_xx):
    # staff 1
    if len(i) == 1:
        #print('NaN')
        list_zz.append(np.nan)
        continue
    # Pairs and difference generator for current game (i)
    pairs = list(permutations(i, 2))
    dgen = (value[0] - value[1] for value in permutations(x, 2))
    # Sum of differences from previous games incluiding only pair of teams in the current game
    for team, result in zip(i, x):
        win_comp_past_difs = sum(wd[key] for key in pairs if key[0] == team)
        #print(win_comp_past_difs)
        list_zz.append(win_comp_past_difs)
    # Update pair differences for current game
    for pair, diff in zip(pairs, dgen):
        wd[pair] += diff
print(list_zz)

Which looks like this:

[0.0,
 0.0,
 nan,
 -4.0,
 4.0,
 0.0,
 0.0,
 0.0,
 nan,
 -10.0,
 13.0,
 -3.0,
 nan,
 3.0,
 -3.0,
 -6.0,
 6.0,
 -10.0,
 -10.0,
 20.0,
 nan,
 14.0,
 -14.0]

If you could elaborate on the code to make it more efficient and execute faster, I would really appreciate it.

The main way to do this faster is to use fewer loops and to do more in each loop. (AFAICT)

loops in there...

A (for event)
A.1 (make team pairs)
A.2 (make dgen)
A.3 (for team, result in event)
A.3.1.1 (for key in pairs)
A.3.1.2 (sum)
A.4 (for pair diff)

You have a lot more flexibility if you use the for loop instead of mostly comprehensions and permutations. And, permutations is wasteful, here, (as discussed, below) because it prevents a few optimisations.

A key change is we can do A vs B and B vs A in the same pass of the loop. So, we can change our loop from "for each team go through all teams" to "for each team go through all following teams". It's a big improvement.

For the line-up A, B, C, D, our iterations look like:

AB & BA
AC & CA
AD & DA

.

BC & CB
BD & DB

.

CD & DC
(done)

So, the algorithm might end up something like (in pseudo code):

past_score_differences = defaultDict(float)

for each event:

    team_outcomes = defaultDict(float)

    teams_len = len(teams in event)
    for i in range(teams_len - 1):
        home_team = teams[i]
        home_score = scores[i]

        for j in range(i+1, teams_len):
            away_team = teams[j]
            away_score = scores[j]

            team_outcomes[home_team] += past_score_differences[(home_team, away_team)]
            team_outcomes[away_team] += past_score_differences[(away_team, home_team)]

            past_score_differences[(home_team, away_team)] += home_score - away_score
            past_score_differences[(away_team, home_team)] += away_score - home_score

        list_zz.append(team_outcomes[home_team])

    # the last team doesn't go through the outer loop
    list_zz.append(team_outcomes[teams[-1]])

Still a lot of loops, but far fewer and with fewer iterations for the innermost loop.

If there is a lot of consistency in the team IDs, then you might consider not using a tuple as the key but instead using a dict within a dict, e.g.

example_past_differences = {
    'A': {
        'B': 10,
        'C': 11,
        'Etc...': 99
    }
}

# later, when accessing

home_team = ...
home_score = ...
home_past_differences = past_score_differences[home_team]
for ...
    away_past_differences = past_score_differences[away_team]
    ...
    team_outcomes[home_team] += home_past_differences[away_team]
    team_outcomes[away_team] += away_past_differences[home_team]

One last thing, your variable names are not easy to understand. I made mine way too long for accesibility to understand, but generally I think the guidance in these slides is very useful: https://talks.golang.org/2014/names.slide#1


Using self-hosted OWIN I have implemented the following in order to handle JS XHR requests coming from HTML pages. Is there a better way to do this with OWIN?

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseFileServer(new FileServerOptions
        {
            FileSystem = new PhysicalFileSystem(string.Format("{0}{1}Web{1}Dashboard", Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), Path.DirectorySeparatorChar))
        });
        app.Use<Middleware>();
    }
}

public class Middleware : OwinMiddleware
{
    private readonly List<string> _knownRequests = new List<string>
    {
        "Request"
    };

    public Middleware(OwinMiddleware next)
        : base(next)
    {
    }

    public override async Task Invoke(IOwinContext context)
    {
        #region Invoke Next Context

        await Next.Invoke(context);

        #endregion

        #region Handle Request

        var request = context.Request;
        var response = context.Response;
        var path = request.Path.Value.TrimStart('/');
        if (!_knownRequests.Contains(path))
            return;

        response.StatusCode = 200;

        switch (path)
        {
            case "Request":
            {
                await response.WriteAsync("RESPONSE!");
                break;
            }
        }

        #endregion
    }
}

Only focusing on the code in question (not on OWIN)

Using region outside of methods is discussed controversially but using region's inside a method is clearly a code smell. It indicates that a method is too big or worse that a method has too many responsibilities.

Instead of adding "Request" to the List and using the string "Request"  in the switch you should declare a const holding "Request" and which you should add to the List and use it in the switch to reduce the possibility of spelling errors.

Using a switch for a single case should be better changed to a simple if statement.

You should use braces {} for single statements if's too. This will make your code less error prone.

Variables should be declared as near as possible to their usage. So changing

var request = context.Request;
var response = context.Response;
var path = request.Path.Value.TrimStart('/');
if (!_knownRequests.Contains(path))
    return;

response.StatusCode = 200;

to

var request = context.Request;
var path = request.Path.Value.TrimStart('/');
if (!_knownRequests.Contains(path))
{
    return;
}

var response = context.Response;
response.StatusCode = 200;

would be better.

Using the var keyword should be only done when the type of the right hand side of the declaration is obvious.
This is only true for the last usage here:

var request = context.Request;
var response = context.Response;
var path = request.Path.Value.TrimStart('/');

so better change it to

IOwinRequest request = context.Request;
IOwinResponse response = context.Response;
var path = request.Path.Value.TrimStart('/');

but using the request does not add to much value here,because it is only used once, so you could just change it to

IOwinResponse response = context.Response;
var path = context.Request.Path.Value.TrimStart('/');


C++ Circular buffer through Circular iterator I saw some question on SO lately involving circular buffer, like a chain of descriptors for data transfer. All solutions involved lots of lines.

I wanted an implementation with as few lines as possible, as inspired by this answer. The idea is not to have a circular buffer but a usual container and a specific iterator able to wrap.

Question: Is it possible to achieve this with as few lines as in the example below or are there hidden pitfalls ?

This code was intended to spin forever on a container which size do not change, more information about iterator validity under containers changes

Additional information after first answers / comments

Similar 2009 question on SO mentioned by Mercury Dime (the question uses the term cyclic or circulator and not circular)

EDIT: Corrections of the more evident errors / typos based on incomputable's answer

#include <iostream>
#include <list>

template <class BaseIter>
class CircularIterator:public BaseIter {
        //inspired from https://stackoverflow.com/a/947754/3972710
        BaseIter begin,end;
    public:
        CircularIterator(BaseIter b, BaseIter e ):BaseIter(b), begin(b), end(e) {}
        CircularIterator & operator ++(void)
        {
            BaseIter::operator++();
            if(*this == end)
                BaseIter::operator=(begin);
            return *this;
        }
        const CircularIterator   operator ++(int)
        {
            const auto oldValue = *this;
            this->operator++();
            return oldValue;
        };

        CircularIterator & operator --(void) = delete;
        const CircularIterator   operator --(int)  = delete;
};

int main()
{
    std::list<int> intList = { 1, 2, 3};
    CircularIterator<std::list<int>::iterator> circIter(intList.begin(),intList.end());

    auto it = circIter++;

    std::cout << *(it++) << "\n";
    std::cout << *(it++) << "\n";
    std::cout << *(it++) << "\n";
    std::cout << *(it++) << "\n";
    std::cout << "..." << "\n";
    std::cout << "Hello world!\n";
    return 0;
}

Original Code

#include <iostream>
#include <list>
using namespace std;
//C++11 at least

template <class baseIter>
class circularIterator:public baseIter {
        //inspired from https://stackoverflow.com/a/947754/3972710
        baseIter begin,end;
    public:
        circularIterator(baseIter b, baseIter e ):baseIter(b), begin(b), end(e) {}
        baseIter & operator ++(void) {  baseIter::operator++();
                                        if(*this == end) baseIter::operator=(begin);
                                        return *this;}
        baseIter & operator ++(int) = delete;
};

int main()
{
    list<int> intList = { 1, 2, 3};
    circularIterator<list<int>::iterator> circIter(intList.begin(),intList.end());

    cout << *circIter << endl;
    cout << *(++circIter) << endl;
    cout << *(++circIter) << endl;
    cout << *(++circIter) << endl;
    cout << *(++circIter) << endl;
    cout << "..." << endl;

    cout << "Hello world!" << endl;
    return 0;
}

High level overview:

The idea is solid, but creating an iterator adapter to make the container behave differently just sounds weird. It would be better to have container adapters, as std::stack<> and std::queue<> do.

Implementation overview:

The code has a serious issues. Mainly the following:

It has a chance to break on some algorithms:

auto second = ++first;

where first is circularIterator<> will make second to be baseIter.  The most dangerous ones are sorting algorithms, especially those that use divide and conquer. The template type parameter is usually only one, e.g. only one type of iterators are allowed, as a result the compiler will issue substitution failure error.

Will not work on idiomatic while (*first++ != last) or similar.

The postfix increment is deleted, thus it disables probably most of the standard library.

The class pretends to be an iterator, but is a range.

Usually iterator should know about its own internal state only. Knowing end iterator should be last resort. Actually, the reason for storing the range is because the code doesn't provide a function to create a pair of circularIterators. If such function would be provided, the operator==() of parent iterator would still work, as they mostly take it by reference.

Cosmetics:

Not really important, but here they are:

Usually people name types (and classes, as a result) in CamelCase or snake_case. camelCase starting from regular letter are used for variable names.
Template type parameters are named using CamelCase, since in the future they will probably become Concepts.
Indentation and brace placing is very weird.

Misc:

using namespace std; is bad. It causes name clashes and in general confuses other mechanisms of C++.
std::endl is a performance drainer. If done in a tight loop, can cause serious issues. Bjarne Stroustrup himself didn't use it for a decade.


Generating pattern similar to QR code using strings I wrote this class to generate a random "image," similar to a QR code, using strings. How can I make it better (I am a beginner in programming)?

import random

""" N is the length of squared field to be generated"""
class genImg():
  def __init__(self, N):
    self.N = N
    self.field = []

  def genField(self):
    for i in range(self.N):
      row = []
      for j in range(self.N):
        row.append("|-|")
      self.field.append(row[:])

  def getField(self):
    return self.field

  def fillField(self):
    field = self.getField()
    result = ""
    for i in field:
      row = ""
      for j in i:
        randNum = random.randint(1,100)
        if randNum % 2 == 0 or randNum % 3 == 0:
          j = '|0|'
        row += str(j) + " "
      result += row[:-1] + "\n"
    print(result)

You are doing unnecessary work by first looping over each row and each column to initialise the field to empty squares; and then looping over each row and each column to change the state based on a random condition. You can do it in one looping by assigning the "empty" or "filled" state based on said condition.

And while we're at it, you could simplify the condition as there is exactly 67 numbers that are either multiple of two or three between 1 and 100. So you could at least test if randNum <= 67 which would yield the same probability. But this probability is pretty close to \$\frac{2}{3}\$ so we can come up with an other approach: use a tupple composed of two " filled" patterns and an "empty" one and use random.choice instead.

You could also benefit a lot from the str.join method instead of concatenating the chosen pattern and a separator to a temporary string. It will help improve your memory usage. Just put the selected patterns in a list and use the join method of your separator to create the whole string at once. Better yet, join over a list-comprehension or a generator expression.

Lastly, you don't need a class to solve this problem as you don't have any state. The various attributes are only there to store temporary variables. A simple function will do:

import random

def generate_image(size, *, filled='|0|', empty='|-|'):
    patterns = filled, filled, empty

    return '\n'.join(
            ' '.join(random.choice(patterns) for _ in range(size))
            for _ in range(size)
    )

if __name__ == '__main__':
    print(generate_image(20))


Live-streaming tweets and plot its sentimental value in colors on a world map I want to have someone reviewing and helping me get some good ideas in how to improve my current project.

All information about the project is in the following github link.

I would also appreciate ideas on how to plot in a more efficient way.

Code:

import tweepy, json
from tweepy.api import API
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from textblob import TextBlob
import re

""" Variables
================"""
searches  = ['trump']    # max 250 input searches in a list of strings
maxTweets = 100000         # max number of tweets to analyse.

# Get the Authentication keys from another file. (Easier to push to git)
with open("/Users/se/Documents/Lek med Python/pyCharmProjects/dataScienceFromScratch/src/twitterKeys.txt","r") as file:
    CONSUMER_KEY        = file.readline().rstrip()
    CONSUMER_SECRET     = file.readline().rstrip()
    ACCESS_TOKEN        = file.readline().rstrip()
    ACCESS_TOKEN_SECRET = file.readline().rstrip()

# Twitter API needs to be validated
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

# The class takes in a stream of tweets and filter out some specific properties.
# It is a live streaming so it will be kind of slow and in this purpose i filter
# the Coordinates which only around 3-4% of all tweets have.
class Stream2Screen(tweepy.StreamListener):
    def __init__(self, api=None):
        self.api = api or API()
        self.counter = 0            # Counts amount of tweets already got

    # This is the method that recieves the tweet in the name of data.
    def on_data(self, data):
        datadict = json.loads(data)     # easier to handle in a json file

        # Checks so that the coordinate is in the tweet
        if (datadict.get('place') != None):
            coordinate = calcCenterOfPolygone(datadict['place']['bounding_box']['coordinates'][0])
            tweet = cleanTweets(datadict['text'])
            sentiment = sentimentAnalysis(tweet)
            plotOnMap(coordinate, sentiment.polarity)
            print(tweet)
            print(sentiment, "\n")
            self.counter += 1
            if(self.counter % 100 == 0):
                print(self.counter)

        # if we sent a False as return value we close the "pipeline" of streams
        if self.counter < maxTweets:
            return True
        else:
            return False

    def on_error(self, status_code):
        print(status_code)
        return True

    # Trial to get sentimental value of a tweet using TextBlob
def sentimentAnalysis(tweet):
    tweet = TextBlob(cleanTweets(tweet))
    return tweet.sentiment

def cleanTweets(tweet):
    return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())

# Inputs 4 Coordinates in a list and calc the mid point.
def calcCenterOfPolygone(coordinates):
    return [sum([x[0] for x in coordinates]) / 4,
            sum([x[1] for x in coordinates]) / 4]

# Plots the coordinate on the map
def plotOnMap(centerPoint, polarity):
    plt.ion()
    color = 'black'
    if (polarity < 0): color='r'
    elif (polarity > 0): color='g'
    m.plot(centerPoint[0], centerPoint[1], marker='o', markersize=5, color=color)
    draw_map(m)
    plt.pause(0.000001)

# draws the map the first time
def draw_map(m, scale=0.2):
    m.shadedrelief(scale=scale)

fig = plt.figure(figsize=(12, 6))
m = Basemap(projection='cyl', resolution=None,
            llcrnrlat=-90, urcrnrlat=90,
            llcrnrlon=-180, urcrnrlon=180,)

# Starts the twitter feed and searches for the keyword
stream = tweepy.streaming.Stream(auth, Stream2Screen())

while True:
    try:
        stream.filter(track=searches)
    except:
        print("error"*10)
        continue

draw_map(m)
plt.show()
plt.draw()

Alright time to give this a shot!

Code style

I won't go in deep details, but the PEP8 standard is that methods should use snake_case not camelCase. You can do whatever you want with this information.

Comments

In my opinion, there are way too many comments in your code. Most of them can be removed with some refactoring. I'll try to target the different reasons why some things shouldn't be commented and the associated refactors for them.

There's one for which I don't have a refactor, because it's an old-school method of doing this that, I believe, shouldn't be done anymore :

""" Variables
================"""

Don't do this. People that will read your code will figure out that variables are following. If you change your code over time and keep this comment it's probable that this comment will become obsolete.

There's also the case of this comment : # if we sent a False as return value we close the "pipeline" of streams which just brings a load of confusion in the code. This comment probably doesn't need to exist as when we will see where the method on_data is called, we'll figure it out ourself.

Refactor 1 : Replace comments with better variable names.

maxTweets = 100000 # max number of tweets to analyse.

If you feel the need to put this comment, it's probably because your variable isn't well named. If you named your variable maxTweetsToAnalayse you wouldn't need the comment.

self.counter = 0            # Counts amount of tweets already got

Same here, you should rename your variable, maybe loadedTweetsCount or something like that?

# Checks so that the coordinate is in the tweet
if (datadict.get('place') != None):

Could be replaced by :

coordinateIsInTweet = datadict.get('place') != None
if coordinateIsInTweet:

Refactor 2 : Write validation rules for comments stating rules.

# max 250 input searches in a list of strings

That's something your code should be aware of. Instead of this comment, why not validate it in the code?

if len(searches) > 250:
   raise ValueError("Exceeded maximum searches inputs")

Refactor 3 : Write a method for a comment explaining a bunch of code lines.

# Get the Authentication keys from another file. (Easier to push to git)
# Twitter API needs to be validated

Instead of having this comments, why not do this :

def getTwitterAuthToken():
    with open("/Users/se/Documents/Lek med Python/pyCharmProjects/dataScienceFromScratch/src/twitterKeys.txt","r") as file:
        CONSUMER_KEY        = file.readline().rstrip()
        CONSUMER_SECRET     = file.readline().rstrip()
        ACCESS_TOKEN        = file.readline().rstrip()
        ACCESS_TOKEN_SECRET = file.readline().rstrip()

    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

    return auth

The first comment was unnecessary, there are plenty of reasons why a configuration file is better than having hardcoded credentials in your code and your code is clear enough that well... the comment isn't necessary.

Refactor 4 : Use documentation instead of single line comments

# The class takes in a stream of tweets and filter out some specific properties.
# It is a live streaming so it will be kind of slow and in this purpose i filter
# the Coordinates which only around 3-4% of all tweets have.

This explains what the class does, so it should be inside the class. You can create class documentation by doing this (More info here):

class Stream2Screen(tweepy.StreamListener):

    """The class takes in a stream of tweets and filter out some specific properties.
    It is a live streaming so it will be kind of slow and in this purpose i filter
    the Coordinates which only around 3-4% of all tweets have."""

Same can be done for methods :

# This is the method that receives the tweet in the name of data.
def on_data(self, data):

Becomes

def on_data(self, data):
    """This is the method that receives the tweet in the name of data."""

That's about it for comments, I think this was the biggest "weakness" in the code and those refactorings will help make the code much better.


Many to Many Relationship using EF Core I've implemented Many-To-Many relationship and wonder if this is the optimal way for it. Since I do not have extensive EF Core experience I would love if someone with more experience could check it out.
Scenario:

Many users
Many organizations
User can belong to multiple organizations
Organization can hold multiple users

// Organization.cs
public class Organization
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual IEnumerable<OrganizationUser> OrganizationUsers { get; set; }
}

// OrganizationUser.cs
public class OrganizationUser
{
    public Guid OrganizationId { get; set; }
    public virtual Organization Organization { get; set; }
    public Guid UserId { get; set; }
    public virtual User User { get; set; }

    public Role Role { get; set; }
}

public enum Role
{
    Owner,
    Admin,
    User
}

// User.cs
public class User
{
    public Guid Id { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public virtual IEnumerable<OrganizationUser> OrganizationUsers { get; set; }

    [NotMapped]
    public virtual IEnumerable<UserOrganizationOutputDto> Organizations
    {
        get
        {
            return OrganizationUsers.Select(x => new UserOrganizationOutputDto
            {
                Id = x.OrganizationId.ToString(),
                Name = x.Organization.Name,
                Role = x.Role
            });
        }
    }
}

// Database Context code that is related
modelBuilder.Entity<Organization>()
    .HasMany<OrganizationUser>()
    .WithOne(x => x.Organization);
modelBuilder.Entity<User>()
    .HasMany<OrganizationUser>()
    .WithOne(x => x.User);

modelBuilder.Entity<OrganizationUser>()
    .HasKey(x => new {x.OrganizationId, x.UserId});
modelBuilder.Entity<OrganizationUser>()
    .HasOne(x => x.User)
    .WithMany(x => x.OrganizationUsers)
    .HasForeignKey(x => x.UserId);
modelBuilder.Entity<OrganizationUser>()
    .HasOne(x => x.Organization)
    .WithMany(x => x.OrganizationUsers)
    .HasForeignKey(x => x.OrganizationId);

```

Couple of pointers. You are mixing data notation and fluent mapping.
replace NotMapped attribute with .Ignore fluent mapping.
Also I think its cleaner to move each entity into its own mapping config class using IEntityTypeConfiguration<T>
Dont mix DTO and entities like that. Best practice is to project your entities to DTOs.


Moving around in a 2D grid Please tell me ways I can improve this code to be more efficient.

#Moving Character in 2D Plane
'''
Made by a beginner
'''
arena = [] # This is a map file

def make_board(place): #Generates map terrain
    for i in range(5):
        place.append(["O"] * 5)

def list_to_string(alist): #Makes map easier to read
    for i in alist:
        print(" ".join(i))

make_board(arena) #Calling the function

prev_char_y = 2 # Keeping track of player's last movement
prev_char_x = 2 #Keeping track of player's last movement
char_y = 2 #Keeping track of player's current movements
char_x = 2 #Keeping track of player's current movements
arena[char_x][char_y] = "X" #What is used to show player pos.
list_to_string(arena) #Shows map with player
go = True # Loop :)
while True:
    try:
        while go:
            print("You are: \nX") #Remind player where he or she is (\n means new line)
            control = input("Move left, right, up, down, or stop?") #Ask what player wants to do
            control = control.lower() #Converts player input to lowercase
            if control == "left": #If left
                char_x = char_x - 1
            elif control == "right":# If right
                char_x = char_x + 1
            elif control == "up": #If up
                char_y = char_y - 1
            elif control == "down": #If down
                char_y = char_y + 1
            elif control == "stop": #If player wants to stop
                go = False
            elif control is not "left" or control is not "right" or control is not "down" or control is not "up":
                print("Please enter a proper direction") #If it's not one of those commands
            arena[prev_char_y][prev_char_x] = "O" #Removes previous player position marker
            arena[char_y][char_x] = "X" #Adds current player position marker (Y, X) instead of (X, Y)
            prev_char_y = char_y #Sets the previous y to the current y
            prev_char_x = char_x #Sets the previous x to current x
            list_to_string(arena) #Prints map
    except IndexError:
        print("That's out of range, please pick somewhere else to move")
        char_y = prev_char_y
        char_x = prev_char_x
        arena[char_y][char_x] = "X"
        list_to_string(arena)

First and foremost, remember DRY: Don't Repeat Yourself. Secondly, group your related functions (preferably in classes; see #1 below). Thirdly, keep your variable names simple and easy to read.

Here are a few more points and examples, elaborating on the above:

You can create a class for your Board. This can easily be done by subclassing the builtin list type, because your original arena is just a normal list.

class Board(list):

    def __str__(self):
        return "\n".join(" ".join(row) for row in self)

This class has one method, __str__, which modifies the superclass' method. It returns the board in the format you want; space-delimited cells and linefeed-delimited rows.

Now, to create our default board, we'll use a list comprehension:

[["O"] * 5 for _ in range(5)]

We can get rid of repeated literals such as your various "X", "O", and 2s
by creating constant variables:

MARKER_X = "X"
MARKER_O = "O"
START = [2, 2]

We can also put the previous and current player-coordinates into lists. Note that [:] takes the contents of a list; in this case, that list is START.

curr_pos = START[:]
prev_pos = START[:]

Group your functions and other code chunks under a class; I'll call mine Game. I'll split this point into several smaller pieces.

Let's subclass the object builtin. Note that, in Python 3, we can omit (object) because it's implicit.

class Game(object):

Now let's move in those constant variables we created earlier. These are the class variables that we can access via the Game object (i.e. Game.MARKER_X).

class Game(object):

    MARKER_X = "X"
    MARKER_O = "O"
    CTRLS = [
        "left",
        None,
        "right",
        "up",
        None,
        "down",
    ]
    EXIT = "stop"
    START = [2, 2]
    DEFAULT = [["O"] * 5 for _ in range(5)]

I'll explain the CTRLS and EXIT variables farther down.
We need to initialize the game, so we'll write an __init__ method.

class Game(object):

    ... # Existing code

    def __init__(self):
        self.flag = True
        self.arena = Board(Game.DEFAULT)
        self.prev_pos = Game.START[:]
        self.curr_pos = Game.START[:]
        self.move_player()

Now we have:

created a variable, flag which we'll use as an exit flag (the same as your go variable).
arena: a Board created from our defaults.
moved our curr_pos and prev_pos variables (from #2) inside our class. These are instance variables that we can access via self.
setup the player's initial position (explained in detail, below).

Let's handle our player's movements. I'm going to call this move_player, which I mentioned in the previous point. We're going to use the instance's curr_pos and prev_pos variables for this. See the code comments for detailed explanations.

class Game(object):

    ... # Existing code

    def move_player(self):
        px, py = self.prev_pos                        # Split apart the previous x and y coordinates.
        cx, cy = self.curr_pos                        # Split apart the current x and y coordinates.
        if (-1 < cx < 5) and (-1 < cy < 5):           # Check to make sure our coordinates are valid.
            self.arena[py][px] = Game.MARKER_O        # Set the previous position to empty.
            self.arena[cy][cx] = Game.MARKER_X        # Set our current position to our marker.
        else:                                         # If the coordinates are invalid, don't do anything.
            print("Please enter a proper direction.") # Notify the player.
            self.curr_pos = self.prev_pos[:]          # The "new" position becomes the old position.
            self.move_player()                        # Move the player back.

Finally let's add in our user interface. I'm going to call this the play method. I've added code comments for a detailed explanation.

class Game(object):

    ... # Existing code

    def play(self):
        print("You are: \nX")
        while self.flag:
            print(str(self.arena))
            ctrl = input("Move left, right, up, down, or stop?").lower()
            if ctrl in Game.CTRLS:                              # Check to see if the command is valid.
                d = Game.CTRLS.index(ctrl)                      # Find our command's index in the command-list. This is important.
                self.prev_pos = self.curr_pos[:]                # The current position becomes the previous position.

                self.curr_pos[d > 2] += d - (1 if d < 3 else 4) # Here, we take the command's index in the command-list, and determine:
                                                                #   1: whether we're moving horizontally or vertically.
                                                                #   2: which direction we're moving (i.e. left or right).
                                                                # Then, we take the command's index and compensate to move either +1 or -1.
                                                                # For example, if the command is "up", we take the index, 3, and subtract 4 to get -1.

                self.move_player()                              # Try moving the player to his new postion.
            elif ctrl == Game.EXIT:                             # Check to see if the command is to exit.
                self.flag = False                               # Set the flag to False. This will end the game.
            else:                                               # Check if the player used an invalid command.
                print("Please enter a proper direction.")       # Notify the player.

Now we can play the game.

my_game = Game() # Create a game.
my_game.play()   # Start the game.

Here is our final code:

class Board(list):

    def __str__(self):
        return "\n".join(" ".join(row) for row in self)

class Game(object):

    MARKER_X = "X"
    MARKER_O = "O"
    CTRLS = [
        "left",
        None,
        "right",
        "up",
        None,
        "down",
    ]
    EXIT = "stop"
    START = [2, 2]
    DEFAULT = [["O"] * 5 for _ in range(5)]

    def __init__(self):
        self.flag = True
        self.arena = Board(Game.DEFAULT)
        self.curr_pos = Game.START[:]
        self.prev_pos = Game.START[:]
        self.move_player()

    def move_player(self):
        px, py = self.prev_pos
        cx, cy = self.curr_pos
        if (-1 < cx < 5) and (-1 < cy < 5):
            self.arena[py][px] = Game.MARKER_O
            self.arena[cy][cx] = Game.MARKER_X
        else:
            print("Please enter a proper direction.")
            self.curr_pos = self.prev_pos[:]
            self.move_player()

    def play(self):
        print("You are: \nX")
        while self.flag:
            print(str(self.arena))
            ctrl = input("Move left, right, up, down, or stop?").lower()
            if ctrl in Game.CTRLS:
                d = Game.CTRLS.index(ctrl)
                self.prev_pos = self.curr_pos[:]
                self.curr_pos[d > 2] += d - (1 if d < 3 else 4)
                self.move_player()
            elif ctrl == Game.EXIT:
                self.flag = False
            else:
                print("Please enter a proper direction.")

my_game = Game()
my_game.play()


Account-Creation form How can I optimize the following PHP/JS code?

It works like a charm but I think it is too long and I'm sure it could be optimized. If I have another form, I'll need to copy paste some lines from my PHP code and it'll be quickly overkill.

JavaScript:

<script>
$(document).ready(function() {
    $('#createAccount').on('submit', function() {
        var Email = $('#Email').val();
        var Password = $('#Password').val();
        var PasswordRepeat = $('#PasswordRepeat').val();

        $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            data: $(this).serialize(),
            dataType: 'json',
            success: function(json) {
                $('.alert-message').text(json.reponse).show();
            }
    });
    return false;
    });
});
</script>

PHP:

<?php
$reponse="";
if(isset($_POST['Email']) && isset($_POST['Password']) && isset($_POST['PasswordRepeat'])) {

    // Si tous les champs ne sont pas renseignés
    if($_POST['Email'] == '' || $_POST['Password'] == '' || $_POST['PasswordRepeat'] == '') {
            $reponse = "Tous les champs doivent être remplis.";
    }

    // Si les mots de passe ne sont pas similaires
    if($_POST['Password'] != $_POST['PasswordRepeat']) {
            $reponse = "Les mots de passe ne sont pas similaires.";
    }

    // Si ce n'est pas une adresse de courriel
    if(!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['Email'])) {
            $reponse = "L'adresse de courriel ne semble pas conforme.";
    }

    // Si le mot de passe est trop court
    if(strlen($_POST['Password']) <= 5) {
            $reponse = "Le mot de passe être top court.";
    }

    // Si tout est ok
    if(($_POST['Password'] == $_POST['PasswordRepeat']) && ($_POST['Email'] != '' && $_POST['Password'] != '' && $_POST['PasswordRepeat'] != '') && (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['Email'])) && (strlen($_POST['Password']) >= 6)) {
            $reponse = "Nice";
    }

}
else {
    $reponse = "Une erreur s'est produite.";
}

$array['reponse'] = $reponse;
echo json_encode($array);
?>

At least for the PHP part:
You should make it into a function where you return status instead of going over every possibility even if you have a fail.
function validate_post() {
   if ( ... )
      return "no";
   if ( ... )
      return "yes";
}

$array['reponse'] = validate_post();

By this you would eliminate the last // Si tout est ok part as well, as by the response would be OK.

If you truly want to eliminate possible steps, you could also negate the isset() checks and use OR instead of AND, as it then will stop on first false.
if (!isset($_POST['Email']) || !isset($_POST['Password']) ... )
   return "Une erreur s'est produite.";

The length check for password require less resources and as such could be moved above the email check. (Which would be the most expensive.)

For the Javascript part
it becomes a question on what and if you need to optimize.
It is likely a process rarely taken. Manual action by user (hopefully) not in a loop ;). When it get submitted you are to have a rather big form for it to have a noticeable effect.
That said jQuery gives you a overhead, but you might/likely do not want to use pure Javascript.

Edit:
Had another look at this, and also had to check if I was missing something regards to your variables in the Javascript. But, unless you have some omitted code within the "submit" function – you have some unused variables:
var Email = $('#Email').val();
var Password = $('#Password').val();
var PasswordRepeat = $('#PasswordRepeat').val();

As you use:
data : $(this).serialize(),

Which in effect serializes all form fields with a name attribute, and not something like:
data : $.param({
          Email : Email,
          Password : Email,
          PasswordRepeat : PasswordRepeat
       }),

By which you could also use:
data : $.param({
    Email : $('#Email').val(),
    Password : $('#Password').val(),
    PasswordRepeat : $('#PasswordRepeat').val()
}),

It depends on use. If for some reason your form has lot of fields that you do not want to post the latter would be to prefer, though that would also bind it much tighter together and make it a possible spot for bugs in future changes to the code.

As a side-note you also should most likely have this:
$('#form').on('submit', function(event) {
    event.preventDefault();  // Prevent form submit.
    ...


Cross product to find Lorentz force in C++ User enters velocity vector \$v\$, charge \$q\$ of particle and magnetic field vector \$B\$ and the code prints the Lorentz force \$F = q * (v * B)\$

#include <iostream>

using namespace std;

int main()
{
    double v[3];
    double B[3];
    double q;
    int i;
    double F = (0.0, 0.0, 0.0);
    double i_X, j_X, k_X;
    cout << "Enter in components of velocity vector v:\n";

    for(i=0; i<3; i++)
    {
        cout << "Component " << i+1 << ": ";
        cin >> v[i];
    }

    cout << "Enter in charge of particle q: ";
    cin >> q;

    cout << "Enter in components of magnetic field vector B:\n";

    for(i=0; i<3; i++)
    {
        cout << "Component " << i+1 << ": ";
        cin >> B[i];
    }

    i_X= (v[1]*B[2])-(v[2]*B[1]);
    j_X= (v[2]*B[0])-(v[0]*B[2]);
    k_X= (v[0]*B[1])-(v[1]*B[0]);

    cout << endl;
    cout << endl;

        cout << "F =  "<< q* i_X << "i" << " + " << q* j_X << "j" << " + " << q* k_X << "k" << endl;

     return 0;
}

is this the correct way to do this or is there a more succinct way?

There are a few things here that could improve your code (whether the code becomes more succinct or not is a matter of debate, but it will definitely become more readable):

Do not use using namespace std, it is considered bad practice as it can lead to subtle name resolution errors and makes the code (in my opinion) more difficult to read because you now have globals, for example cout and cin in your program which are not easily recognizable.
You do not need to define all variables at the top of each function, and, in fact, should favor to declare your variables as locally as possible, because this helps people keep track of what is currently important while reading your code. Nobody wants to keep track of several different, obscure variables whose meaning and usage is only introduced several lines deeper down.
It is completely unclear to me why you wrote double F = (0.0, 0.0, 0.0); which is equivalent to double F = 0.0; but very obscure.
Even more obscure, why do you even have a variable named F if you never use it? Remove it.
Rethink operator spacing. Ultimately, this issue comes don to personal preference, but please consider adding a space before and after each binary operator. Lines such as i_X= (v[1]*B[2])-(v[2]*B[1]); are much harder to parse quickly and correctly than i_X = (v[1] * B[2]) - (v[2] * B[1]);.
Do not use std::endl unless you have a good reason to, as it not only creates a line break but also flushes the stream, which can be detrimental to performance. Although it is unlikely to matter in your case, you might find yourself working with situations where it does matter, so you should make a habit out of preferring '\n'.
Concatenate adjacent string literals when writing to std::cout. You could get away with fewer calls to operator<< if you wrote cout << "F =  " << q * i_X << "i + " << q * j_X << "j + " << q * k_X << "k\n"; instead of cout << "F =  "<< q* i_X << "i" << " + " << q* j_X << "j" << " + " << q* k_X << "k" << endl;.
Omit return 0;. The compiler automatically inserts it for you (although only in main).
You do no input checking at all. If a user enters anything that can not be parsed as a double by cin you will be running into undefined behavior because of uninitialized variables. Depending on your use case, this might be fine, but if you ever intend to have somebody else use this program, you should add some checks to make sure you actually receive valid input.
You should consider letting the user input all three values of a vector on the same line, not only to make your code a little more succinct, but also to make it a little more user-friendly.


First-class objects in Python As a beginner, in order to cement my knowledge of functions in Python, I’ve prepared this Jupyter notebook First-Class Functions. The code in it is working, but before moving on to closures and decorators I would like you to comment on my code please.

def cube(x):
    return x ** 3

cube_two = cube(2)    # Call the function, set cube_two to the value returned

print(cube)            # It says cube is a function
print(cube_two)       # The value stored in cube_two

# <function cube at 0x010E71E0>
# 8

Returned values as list elements and dict values:

nums = [4, cube(4), cube(4) / cube(4) ** 0.5]    # returned values as list elements
print(nums)

cube_dict = {'two_cubed': cube(2), 'five_cubed': cube(5)}   # as dict values
print(cube_dict)

# [4, 64, 8.0]
# {'two_cubed': 8, 'five_cubed': 125}

Setting a variable to the cube() function:

make_cube = cube    # Take out those parentheses

print(cube)
print(make_cube)    # make_cube points to the cube() function

# <function cube at 0x010E71E0>
# <function cube at 0x010E71E0>

the variable make_cube can be used as a function, like so:

print(cube)
print(make_cube(4))

# <function cube at 0x010E71E0>
# 64

map taking a function (int) as an argument:

str_nums = ['1', '2', '3']
one, two, three = map(int, str_nums)  # similar to [int(n) for n in str_nums] which is pythonic

print(str_nums)
print([one, two, three])

# ['1', '2', '3']
# [1, 2, 3]

reduce is another example: it applies a function of two arguments cumulatively to the items of an iterable.

from functools import reduce

cumu_diff = reduce(lambda x, y: x - y, [3, 2, 1])    # cumulative difference
print(cumu_diff)

# 0

Creating a map-like function (well, kind of).

def map_it(func, seq):    # func (without parentheses) as an argument
    for x in seq:
        yield func(x)

str_nums = ['1', '2', '3']
one, two, three = map_it(int, str_nums)    # same as map(int, str_nums)

print([one, two, three])

# [1, 2, 3]

Returning a function from another function:

def greet(name):

    def print_message():
        print("Hello, " + name + "!")     # print_message() gets the name from greet()

    return print_message                  # returning the function, not calling it

greet_et = greet('E.T.')    # greet_et points to print_message()

print(greet)
print(greet_et)
print(greet_et())

# <function greet at 0x01090C90>
# <function greet.<locals>.print_message at 0x010B36A8>
# Hello, E.T.!
# None

Here's another example.

def make_paragraph(tag):

    def wrapper(text):
        return '<{0}>{1}</{0}>'.format(tag, text)

    return wrapper

para_one = make_paragraph('p')

print(para_one('This is a small paragraph.'))   # returns '
This is a small paragraph.
'

#
This is a small paragraph.

There are a few general improvements I would suggest for readability and clarity and apply throughout.

Use docstring to document your functions, modules and classes.
Since Python 3, you can now use type hints for your function and method signatures, and use static code analysis tools. This also makes the code easier to read for humans.

Applying these to your cube function, you would get something like this:

def cube(number: int) -> int:
    """Return the cube of a number,
    i.e. the number to the power of 3."""
    return number ** 3

Note that I renamed the x argument to a more descriptive number argument. What the above means is that the function expects number to be an integer, and that the function should return an integer.

Another problem I see with this function is that it does not check for correct input. If you type cube("hello") then the program just crashes with an error TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'. It would be better to handle the error. There are at least two ways to do this.

Using isinstance:

def cube(number: int) -> int:
    if not isinstance(number, int):
        return TypeError("{0} is not an integer".format(number))
    return number ** 3

Using try and except:

def cube(number: int) -> int:
    try:
        return number ** 3
    except TypeError as err:
        return TypeErrorTypeError("{0} is not an integer".format(number))

Using the latter, you could also just except TypeError as err and return err which will return the normal error listed above.

You can also use assert which will instead raise an AssertionError if the type is incorrect, for instance assert(isinstance(number, int)). This is particularly useful when setting up tests for your code.

I personally find that the first method is simpler to use, and you can use it to check multiple types at once, but the latter is the standard way to handle something that may cause an error. Choose whichever is more appropriate for the situation.

Applying the same ideas to map_it, this is slightly more complicated since the type hints have to be imported from typing library. Again, I renamed x to a more descriptive name elem.

from typing import Callable, Sequence, Iterable

def map_it(func: Callable, seq: Sequence) -> Iterable:
    """Applies a given function to a sequence and yields the result."""
    for elem in seq:
        yield func(elem)

You could also test using instanceof here if you liked, which may or may not be necessary depending on your use cases.

Your greet function can also have type errors, if you call it with a non-string type, e.g., greet(42). The simple fix for this (and I recommend to use it any time you need to put variables/arguments into strings) is to use .format() instead of +'ing string and values.

from typing import Callable

def greet(name: str) -> Callable:
    """Returns a callable greeting with given name."""
    def print_message() -> None:
        """Callable to print a greeting to stdout."""
        print("Hello, {0}!".format(name))
    return print_message

One more thing, I noticed your code is peppered with # line comments, in general comments like that should only be used to clarify or explain why something is done, in most cases they are not needed as the code speaks for itself.


Searching for three positive integers as a solution to an equation I recently ran into a question on M.SE asking,

  For positive integers x, y, z find a solution s.t. \$ \frac{x}{y+z} + \frac{y}{x+z} + \frac{z}{x+y} = 4 \$

so rather than think, I made a quick brute force c++ program to check for solutions:

unsigned lim = 251;
for(double x=1; x<lim; x++) {
    for(double y=1; y<lim; y++) {
        for(double z=1; z<lim; z++) {
            printf("x=%.0f, y=%.0f, z=%.0f\n",x,y,z);
            if( std::abs((x/(y+z) + y/(x+z) + z/(y+x)) - 4) <= 1E-15) {
                printf("solution: x=%.0f, y=%.0f, z=%.0f\n",x,y,z);
            }
        }
    }
}

All of this being within main, of course. This is obviously slow so I was looking for optimizations. A couple of my thoughts were to remove the std::abs call because the inside expression must be positive, perhaps I could simply check == 4 rather than account for precision. I couldn't figure out a way to drop a for loop, because you can't isolate a variable. Otherwise I'm not sure what to do.

Questions:

How can this code be sped up? My goal is lim=1E3 in under a minute. As of now it takes 31.0643 s for lim=251.
Can the for-loops be reduced? As of now formatting is not really an issue for me, but can this be simplified down to a single expression and/or loop? Or maybe look a little cleaner, without sacrificing optimization?

Update: After some research I'm now aware that the smallest known solution to this has numbers with 81 digits. I'm not concerned with this, just the above questions.

Here are some simple optimizations you can make that can push your limit a bit higher:

Don't use floating point arithmetic. Floating point arithmetic is slow compared to integer arithmetic (this is not always true with the advent of things like FPUs, but it is a good rule of thumb). Even worse, floating point arithmetic is prone to things like rounding errors, which means you might find solutions which don't work and miss solutions that do work.

One way to avoid using floating point arithmetic here is to expand out the equation into the form:

\$x(x+y)(x+z) + y(y+x)(y+z) + z(z+x)(z+y) = 4(y+z)(x+z)(x+y)\$

In this form you can check whether the equation is satisfied just using integer arithmetic (make sure you use a integer type large enough so that neither side overflows, however).
Make use of symmetry (thanks @Deduplicator). Another simple observation you can make is that this equation is symmetric in \$x\$, \$y\$, and \$z\$, i.e. if you have a solution \$(x, y, z)\$, then any permutation of this solution also works. One way to use this fact is to only loop over \$1 \leq y \leq x\$ and \$1 \leq z \leq y\$. This cuts down on your total number of iterations by a factor of \$3! = 6\$.
Reduce number of degrees of freedom. One interesting observation is that, once you fix \$x\$ and \$y\$, you don't have many choices left for \$z\$. So instead of looping over all \$z\$ in the range \$[1, L]\$, you can more efficiently compute the possible values of \$z\$. (This removes one of your for loops).

How do you find the possible values of \$z\$? Well, if we know what \$x\$ and \$y\$ are, the above equation reduces to a cubic in \$z\$; i.e., something for the form \$c_3z^3 + c_2z^2 + c_1z + c_0 = 0\$. There are a couple ways to solve this for \$z\$; the easiest way I can think of (but definitely not the fastest) is to try all the factors of \$c_0\$ (by the Rational Root Theorem, any integer \$z\$ that satisfies this must divide \$c_0\$). This does involve factoring \$c_0\$, however, which can take a while depending on how you implement it.

A faster method is to use binary search to find the roots, but you have to be a bit careful here, since the cubic might not be monotone increasing/decreasing over the entire interval. The correct way to do this is to first find the roots of the derivative of the cubic (the derivative of the cubic is a quadratic, so you can do this with just the quadratic formula); these are the critical points of the cubic, so the cubic is monotone increasing/decreasing on the intervals between these points, and you can binary search here.
Learn about elliptic curves. So it turns out that the smallest solutions to this Diophantine equation are immense, and it's pretty much impossible to find any via any method like this (I forget how large they are, but I believe each of x, y, and z is at least 10 digits long). The question then becomes, how did people find this answer in the first place?

The trick here is that the cubic above (in fact, pretty much any homogeneous cubic in three variables) is an instance of a mathematical object known as an elliptic curve, and that these objects have a ton of nice properties. One of these nice properties is that there is a method whereby you can take any two integer solutions to this equation and "add" them to get a third integer solution. So one approach which works is to start with an integer solution (but not a positive integer solution) such as (11, 9, -5), and repeatedly "add" it to itself until you end up with a positive integer solution (as far as I know, there's no guarantee you ever will end up with a positive integer solution, but in this case you do after a couple of steps).

EDIT:

I ran some tests to compare the above optimizations (see here). On my machine for limit=1000:

OP's code (removing the printf per iteration) takes ~14.5 seconds.
OP's code with optimization 1 takes ~4.2 seconds.
OP's code with optimizations 1 + 2 takes ~0.7 seconds.
OP's code with optimizations 1 + 2 + 3 takes ~0.25 seconds.

For larger limits, these differences are more pronounced. For limit=5000:

OP's code with optimizations 1 + 2 + 3 takes ~7.9 seconds.
OP's code with optimizations 1 + 2 takes ~86 seconds.
The other two cases each took at least 5 minutes.


Python CSV file import and object manipulation I am trying to import a .csv file and save data in objects for later manipulation in Python. I am very new to coding so please do let me know my fundamental errors.

import csv
import os
import time
import numpy
import itertools

def __init__(p):
    p.playerFile = csv.reader(open("dk.csv", "rb"))
    p.playerList = []
    p.playerName = ''
    p.playerPosition = ''
    p.PlayerGameInfo = ''
    p.playerPrice = 0

def loadCSV(p):
    for row in p.playerFile:
        playerObject = Player()
        playerObject.setPlayerName(row[1])     # Store player name
        playerObject.setPlayerPosition(row[0]) # Store player position
        playerObject.setPlayerGameInfo(row[3]) # Store player team
        playerObject.setPlayerPrice(row[2])    # Store player price

def getPlayerName(p):
    return p.PlayerName

def getPosition(p):
    return p.playerPosition

def getGameInfo(p):
    return p.PlayerGameInfo

def getPrice(p):
    return p.PlayerPrice

def setPlayerName(p, player):
    p.playerName = name

def setPlayerPosition(p, playerPosition):
    p.playerPosition = position

def setPlayerGameInfo(p, PlayerGameInfo):
    p.PlayerGameInfo = team

def setPlayerPrice(p, playerPrice):
    p.playerPrice = price

with open('~/dk.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        print (row)

Style

Use snake_case instead of mixedCase when in Python. In general, follow PEP 8.

Implementation

It seems you have a Player class defined somewhere. Then, you use global functions as getters and setters. This has two problems:

In OOP, getters and setters should be instance methods, e.g. of the Player class.
Python is not Java. Pythonic code wouldn't normally use getters and setters.

Design

Judging by loadCSV, each row of a CSV file will become a Player object. However, all Players do is store attributes for getting and setting. Save the hassle by using a dictionary for a player. Then just store all your dictionaries in a list.

To understand this code, you will need to understand comprehensions.

import csv

# Map row to dictionary (dictionary comprehension)
def player(column_names, row):
    return {column_names[column]: data for column, data in enumerate(row) if column < len(column_names)}

# Map CSV file to list of dictionaries (list comprehension)
players = [player(['name', 'position', 'game_info', 'price'], row) for row in csv.reader(open('dk.csv', 'r'))]

Running this code on the following CSV file

Louis Fernando,Batter,Washington,400
Hose Ricardo,Pitcher,San Fernando,900

sets players to a list of dictionaries as below.

[{'game_info': 'Washington',
  'name': 'Louis Fernando',
  'position': 'Batter',
  'price': '400'},
 {'game_info': 'San Fernando',
  'name': 'Hose Ricardo',
  'position': 'Pitcher',
  'price': '900'}]

Then player objects can be accessed like so

print(players[0]['game_info']) # prints Washington
players[1]['name'] = "James Jones"

Notes

Types

This doesn't take care of types, e.g. the price field maps to a string, not a number. I'm not exactly sure what you're doing overall, so I'll leave that to you.

CSV Format

Columns in your CSV file beyond those named in column_names will be ignored.

e.g. passing four names (['name', 'position', 'game_info', 'price']) to player when a row in the CSV file contains five columns.

Louis Fernando,Batter,Washington,400,lol
Hose Ricardo,Pitcher,San Fernando,900

will produce the same result as the first example.


Consolidate list of ranges that overlap I wanted to implemented an algorithm in Python 2.4 (so the odd construction for the conditional assignment) where given a list of ranges, the function returns another list with all ranges that overlap in the same entry.

I would appreciate other options (more elegant or efficient) than the one I took.

ranges = [(1,4),(1,9),(3,7),(10,15),(1,2),(8,17),(16,20),(100,200)]

def remove_overlap(rs):
        rs.sort()
        def process (rs,deviation):
                start = len(rs) - deviation - 1
                if start < 1:
                        return rs
                if rs[start][0] > rs[start-1][1]:
                        return process(rs,deviation+1)
                else:
                        rs[start-1] = ((rs[start][0],rs[start-1][0])[rs[start-1][0] < rs[start][0]],(rs[start][1],rs[start-1][1])[rs[start-1][1] > rs[start][1]])
                        del rs[start]
                        return process(rs,0)
        return process(rs,0)

print remove_overlap(ranges)

Firstly, modify or return, don't do both.

Your process method returns lists as well as modify them. Either you should construct a new list and return that, or modify the existing list and return nothing.

Your remove_overlap function does the same thing. It should either modify the incoming list, or return a new list not both.

You index [0] and [1] on the tuples a lot to fetch the start and end. That's best avoided because its not easy to tell whats going on.

rs[start-1] = ((rs[start][0],rs[start-1][0])[rs[start-1][0] < rs[start][0]],(rs[start][1],rs[start-1][1])[rs[start-1][1] > rs[start][1]])

Ouch! That'd be much better off broken into several lines. You shouldn't need to check which of the starts is lower because sorting the array should mean that the earlier one is always lower. I'd also use the max function to select the larger item, (if you don't  have it in your version of python, I'd just define it)

Your loop is backwards, working from the end. That complicates the code and makes it harder to follow. I'd suggest reworking it work from the front.

return process(rs,0)

You start the checking process over again whenever you merge two ranges. But that's not so great because you'll end up rechecking all the segments over and over again. Since you've already verified them you shouldn't check them again.

Your recursion process can be easily rewritten as a while loop. All you're doing is moving an index forward, and you don't really need recursion.

This is my implementation:

def remove_overlap(ranges):
    result = []
    current_start = -1
    current_stop = -1

    for start, stop in sorted(ranges):
        if start > current_stop:
            # this segment starts after the last segment stops
            # just add a new segment
            result.append( (start, stop) )
            current_start, current_stop = start, stop
        else:
            # segments overlap, replace
            result[-1] = (current_start, stop)
            # current_start already guaranteed to be lower
            current_stop = max(current_stop, stop)

    return result


Array choice based on template specialization I've got these two template functions responsible of adding/removing actors in-game in GameEngine.h:

template<typename T>
void add(T actor);
template<typename T>
void remove(T actor);

And these arrays:

std::set<std::shared_ptr<Tickable>> tickables;
std::set<std::shared_ptr<Hitbox>> hitboxes;
std::set<std::shared_ptr<sf::Drawable>> drawables;
std::set<std::shared_ptr<EventCatch>> eventCatchers;

Currently, the way i fill these is really... horrible, here's GameEngine.cpp:

template<>
void GameEngine::add<std::shared_ptr<sf::Drawable>>(std::shared_ptr<sf::Drawable> actor)
{
    drawables.insert(actor);
}

template<>
void GameEngine::add<std::shared_ptr<Hitbox>>(std::shared_ptr<Hitbox> actor)
{
    hitboxes.insert(actor);
}

template<>
void GameEngine::add<std::shared_ptr<Tickable>>(std::shared_ptr<Tickable> actor)
{
    tickables.insert(actor);
}

template<>
void GameEngine::add<std::shared_ptr<EventCatch>>(std::shared_ptr<EventCatch> actor)
{
    eventCatchers.insert(actor);
}

template<>
void GameEngine::remove<std::shared_ptr<sf::Drawable>>(std::shared_ptr<sf::Drawable> actor)
{
    drawables.erase(actor);
}

template<>
void GameEngine::remove<std::shared_ptr<Hitbox>>(std::shared_ptr<Hitbox> actor)
{
    hitboxes.erase(actor);
}

template<>
void GameEngine::remove<std::shared_ptr<Tickable>>(std::shared_ptr<Tickable> actor)
{
    tickables.erase(actor);
}

template<>
void GameEngine::remove<std::shared_ptr<EventCatch>>(std::shared_ptr<EventCatch> actor)
{
    eventCatchers.erase(actor);
}

My knowledge about template / smart pointer is limited(yet), is there a better way to do this?
Even if that modifies the arrays, as long as i can call everything is the Loop:

The game loop looks like that in GameEngine.cpp:

void GameEngine::GameLoop()
{
    sf::Clock timer;
    sf::Time tickRate;
    bool run = true;
    Level level(this);

    while (run) {

        clear();

        for (std::shared_ptr<sf::Drawable> drawable : drawables)
            draw(*drawable);

        display();

        sf::Event event;
        while (pollEvent(event))
        {
            switch (event.type) {
            case sf::Event::Closed:
                close();
                run = false;
                break;
            default:
                for (std::shared_ptr<EventCatch> evt : eventCatchers)
                    evt->onEvent(event);
                break;
            }
        }

        for (std::shared_ptr<Tickable> tick : tickables)
            tick->onTick(tickRate.asMicroseconds());

        tickRate = timer.restart();
    }
}

Small rant about question / description

  And these arrays: [..]

Be as precise as necessary when describing things. Generalize ("containers") when it helps to make things more clear. But don't say wrong things. These are no arrays, these are sets.

Accessing the sets based on their content type.

std::set<std::shared_ptr<Tickable>> tickables;

The fact that this set contains "tickables" is encoded twice in the line above: The type of that std::set contains the type Tickable and the name (informally) tells us the same. While accessing this set as tickablesis a good and very readable option, this does not lend itself to template programming that much, because the compiler doesn't understand "tickables". It's just another identifier, the meaning (that we humans are able to understand) is lost.

Thus I suggest accessing your sets like set_of<Tickable>(). This is still readable to us, but also provides information for the compiler about the content of the set.

To be able to pull this off I wrap the sets in a helper struct:

template <typename Content>
struct SetWith {
  std::set<Content> set;
};

This only holds a single set (with a single content type). I generalize to multiple content types (and multiple sets) via variable template arguments:

template <typename... Contents>
struct SetsContainer {};

template <typename Content, typename... OtherContents>
struct SetsContainer<Content, OtherContents...>
  : public SetsContainer<OtherContents...>,
    public SetWith<Content>
{};

The first is the template definition that will be used when the template specialization (the second definition above) doesn't match, i.e. when there's no (more) content: SetsContainer<>.

The specialization inherits from the previously defined wrapper (and thus gets a member of type std::set<Content>) and also inherits from a variation of its own type with the Content "removed".

Accessing the sets is now almost trivial:

template <typename C, typename Container>
std::set<C> & set_of(Container & sets) {
    return static_cast<SetWith<C> *>(&sets)->set;
}

// Use:
SetsContainer<int, bool, char> sets;
set_of<bool>(sets).insert(true);

Of course you can also make set_of an (template) member function of a class containing a SetsContainer named sets and drop the parameter.

Your add is then:

template <typename T>
void add(T actor) {
  set_of<typename std::decay<T>::type>().insert(std::forward<T>(actor));
}

Here's an ideone link to play with the above.

Don't throw away useful information

What if you add the very same Tickable twice? Do you want to be informed about this? Because if its added twice when you didn't intent to, this could help you find bugs.

std::set::insert returns useful information, so why throw it away? Better make your add return e.g. the bool returned by insert.

Heavy copies ahead!

The "new" for syntax has its issues, IMO. Do you know that every time you run that (or any of the other) loop ...

for (std::shared_ptr<Tickable> tick : tickables) // ...

... you're creating a copy of every smart pointer in the set. This is not as bad as making a copy of the Tickables, but it still degrades performance: Every time a smart pointer is created it needs to increment the reference counter, and on every destruction it decrements it again.

Better add a reference to avoid those copies:

for (std::shared_ptr<Tickable> & tick : tickables) // ...

This isn't facebook, don't share that much ;)

This is just a guess, but I'd assume that you misuse std::shared_pointer. Think carefully about who owns (for example) a Tickable. Who controls its lifetime?

If it's the GameEngine, then you are far better of using std::set<Tickable>.

The sets are useless

That said, putting std::shared_pointers inside a std::set (with the default compare) means that its operator< is used by the set to prevent duplicates. But, from above link (emphasis mine):

  Note that the comparison operators for shared_ptr simply compare pointer values; the actual objects pointed to are not compared.

Thus you can happily write (assuming a copy constructor) ...

auto foo = std::make_shared<Tickable>();
auto bar = std::make_shared<Tickable>(*foo); // Calls copy constructor
add(foo);
add(bar);

... and have two copies of a Tickable in your set.


Registering layers in a web mapping library. API design I am working on a web mapping library, that makes it possible to create a map with SVG and JavaScript. As every map shall consist of SVG and Image elements, which can be grouped in layers, which in turn can be set invisible, I need an API to reflect this functionality. I started with two classes/prototypes:

Map()                      // the map constructor
.add(object)               // method to add a layer or element

Layer()                    // the layer constructor
.add(object)               // method to add an element
.setVisibility(boolean)    // mothod to set the visibilty

Some days later I extended the Map with a method to register event handlers that I modeled after the Jquery on/off API:

Map()
.add(object)
.on(events, handler)       // method to register event handlers
.off(events)               // … and to unregsiter handlers

The trick with the events parameter is, that it is a string containing both an event and an optinal namespace to group the event handlers like click.namespace

A classmate then gave me the advice to remove the layer class completely and add "add()" and "remove()" methods that work like the on and off methods to group elements to layers. So the following API emerged:

Map()
.add(namespace, object)
.remove(namespace)
.on(events, handler)
.off(events)

What I find somehow cumbersome, is, that events describe both an event and an optinal namespace, whereas namespace only describes a namespace. So

The Question is:

How to design such an API in a simple, but still consistent way so that I can group elements and event handlers in a similar manner?

Since namespaces are always optional, you can follow the common JavaScript convention and put them at the end of the arguments list:

Map()
.add(object, namespace1)
.remove(namespace1)
.on(events, handler, namespace2)
.off(events)
.on(otherEvents, handler)

As you say, events describe both an event and a namespace. More specifically, they describe an event on a namespace or the whole map, which is why it makes sense to add back the "layer" object. This is the simplest way to show the difference between "event on a layer" and "event on all layers":

var l = Layer()            // the layer constructor
.add(object)               // method to add an element
.setVisibility(boolean)    // mothod to set the visibilty
.on("click", handler)      // do XXX when clicking on an object of this layer
.off("click")

var m = Map()              // the map constructor
.add(l)                    // method to add a layer or element
.on("hover", handler)      // do XXX when hovering the whole map

You should also wonder what you're going to do with bubbling events. If there's an event which is not handled in the layer, is it going to bubble up and be handled by the map? For all browsers? Those issues (and many more) are already covered by jQuery, which is why I don't suggest rewriting half of their event-handling code and simply use it directly. Why not writing your API as a jQuery plugin?


Cubic "bezier" curve of grade n This is code I wrote for calculating bezier curves as quickly and RAM efficiently as possible.

I would like to know if there are faster ways to optimize anything, because I am very new to C++ and efficiency methods.

Lastly, if there is a variable that could be more explicitly named, also let me know.

#include <vector>

double* bezier_interp (double* points, double* temp_space, int temp_space_size, int points_total, double t_step_t) {
    memcpy(temp_space, points, temp_space_size);
    for (int pointsi = points_total; pointsi > 1; --pointsi){
        for (int pointsj = 1; pointsj < pointsi; pointsj++) {
            points[pointsj-1] -= (points[pointsj-1] - points[pointsj]) * t_step_t;
        }
    }
    return points;
}

double* bezier_t (double* numbers, int points_total, int definition) {
    double *result = new double[definition];

    double t_step = 1 / ((double) definition - 1);
    double t_step_t;
    double* temp_space = new double[points_total];
    int temp_space_size = sizeof(*temp_space) * points_total;
    for (int t = 0; t < definition; t++) {
        t_step_t = t * t_step;
        result[t] = *bezier_interp(numbers, temp_space, temp_space_size, points_total, t_step_t);
    }
    delete[] temp_space;
    return result;
}

#include<iostream>

int main() {
    double numbers_x[] = {
        0, 0, 4, 4
    };
    double numbers_y[] = {
        0, 4, 4, 0
    };
    int definition = 8;
    double* xs = bezier_t(numbers_x, 4, definition);
    double* ys = bezier_t(numbers_y, 4, definition);
    for (int dix = 0; dix < definition; dix++){ std::cout << xs[dix] << "\t"; }
    std::cout << "\n";
    for (int diy = 0; diy < definition; diy++){ std::cout << ys[diy] << "\t"; }
    std::cin.get();
    return 0;
}

#include <vector> is pointless. There is no vectors in the code.
Unless I am missing something obvious, temp_space plays no role in the calculations.
pointsi and pointsj do not sound right (too much of visual noise makes it hard to tell them apart). It is one of the rear cases where I'd go for plain i and j.
t_step_t is confusing, and strictly speaking unnecessary. You may calculate it at the point of invocation as

result[t] = *besier_interp(..., t * t_step);


Avoiding Duplicates in a Ruby on Rails Factory Girl Factory with Fake I have a Factory Girl factory that needs to generate a unique name using Faker. Unfortunately, Factory Girl generates duplicates relatively frequently, which causes intermittent test errors. I previously added a random integer after the Faker name, but I am now checking the database to make sure that a potential name from Faker doesn't already exist.

FactoryGirl.define do
  factory :company do
    name do
      name = Faker::Company.name
      name = Faker::Company.name while Company.exists?(name: name)
      name
    end
  end
end

The above code works but seems inelegant. I'm assigning name in the first row of the block so that only one DB lookup (instead of two) will be necessary in the most common case where a Fake name is unique.

I end the block with name as otherwise the while statement returns nil when the loop ends.

The simplest thing to do is probably to use FactoryGirl's sequence:

FactoryGirl.define do
  factory :company do
    sequence(:name) { |n| "#{Faker::Company.name} #{n}" }
  end
end

sequence basically gives you an auto-incrementing integer, so you can avoid uniqueness issues. Sure, it'll generate some slightly odd company names, but for testing that shouldn't matter much.

In general though, I don't feel too great a need for something like Faker. Something like this should work just as well:

FactoryGirl.define do
  factory :company do
    sequence(:name) { |n| "Acme Inc. #{n}" }
  end
end

If it's only your specs looking at the data, it doesn't really matter if the company name's "Acme", "foobar 42", or "ACC8B1B7-7EC0-4F05-AB2A-B487C134F6BF".

Besides, your current solution is:

non-deterministic, so it may never exit the while loop if it just happens to keep picking the same couple names again and again,
but even if we ignore that, the act of adding more tests that use company records, will mean more name collisions and longer time spent in the loop. I don't know how Faker works, but (unlikely but technically possible) you might even end up exhausting every name Faker can come up with, and end up looping forever.


Convert integer to decimal string I have a need to do some conversions in a loop to display the results using C++98 on Linux, which doesn't have any itoa() to complement atoi(). I opted for this instead of snprintf(). This is for an embedded device and the conversions will be done in a few loops, so speed is important.

It's based on some code I found on GitHub.  I modified it to write to a user-supplied buffer, so that it's reentrant.

Please let me know if anyone has a better method for small numeric conversions to ascii using base-10 only, or any recommended changes. Thanks.

/*
  Modified from:
  https://gist.github.com/madex/c5cd5c6a23965a845d6e

  This only works for up to 9 digits and only converts to base 10 ascii,
  but no division is used and this method is very fast.
*/

void itoaBase10(int32_t num, char *string, int lengthOfString)
{
    if (!string || num > 9999999999)
    return;
    memset(string, 0, lengthOfString);
    static uint32_t subtractors[10] =
     { 1000000000, 100000000, 10000000, 1000000, 100000, 10000,
       1000, 100, 10, 1 };
    char n, *str = string, sign = num < 0 ? '-' : ' ';
    uint32_t *sub = subtractors;
    uint32_t u = num < 0 ? (uint32_t)-num : (uint32_t)num;
    uint8_t  i = 10;
    while (i > 1 && u < *sub)
    {
        i--;
        sub++;
    }
    str++;
    *(str - 1) = sign;
    while (i--)
    {
        n = '0';
        while (u >= *sub)
        {
            u -= *sub;
            n++;
        }
        *str++ = n;
        sub++;
    }
}

Since the function may bail out without computing the result, it must signal the caller that something got wrong. Consider returning an error code.
Since you are dealing with negative numbers, testing for num > 9999999999 is not sufficient.
Along the same line, -num may overflow.
The lines

    str++;
    *(str - 1) = sign;

are a long way to say

    *str++ = sign;

That said, you seem to force a sign (a whitespace) into the output even for positives. Strictly speaking, it is not an itoa compatible behavior.
memset seems superfluous. *str = 0; after the conversion is done suffices.
I don't think that the benefits of a subtraction loop vs division/modulo are measurable. Did you profile?
The only reason to prefer itoa to sprintf is that the former supports arbitrary conversion bases. It you only need decimal conversion, I strongly recommend to stick with the latter.


Is this a reasonable way to implement promises in node.js? I'm using passport to setup login in an express site. The site is just a playground for me to learn. The code inside login was exhibiting the arrow anti pattern so I decided it was time to learn to use promises.

But I don't know if what I've arrived at is good, bad, idiomatic, or idiotic. Any and all feedback appreciated!

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var Promise = require('bluebird');
var bcrypt = require('bcrypt');
var db = require('./db').db; //users are stored in mongo

//I'm using bluebird.js for promises
var users = Promise.promisifyAll(db.users);
var compare = Promise.promisify(bcrypt.compare);

//I think this helps when responding to the db user load completing
function NoMatchedUserError(message) {
  this.name = "NoMatchedUserError";
  this.message = message || "Incorrect username.";
}
NoMatchedUserError.prototype = new Error();
NoMatchedUserError.prototype.constructor = NoMatchedUserError;

//this strategy is used by passport to handle logins
module.exports.localStrategy = new LocalStrategy(function(username, password, done) {
  var matchedUser;

  var comparePassword = function(user){
    if(!user) {
      throw new NoMatchedUserError();
    }

    //memoise the loaded user so it can be returned below
    matchedUser = user;
    return compare(password, matchedUser.password);
  };

  users.findOneAsync({ username: username })
    .then(comparePassword)
    .then(function(isMatch) {
      return isMatch
        ? done(null, matchedUser)
        : done(null, false, { message: 'Incorrect password.' });
    })
    .catch(NoMatchedUserError, function() {
      return done(null, false, { message: 'Incorrect username.' });
    })
    .error(function(err) {
      return done(err);
    });
});

Define all your error types globally in some file for convenience, like the built-in errors are defined.

Mixing promises and callbacks is a big anti-pattern, just think in promises and let .nodeify handle the mapping to callback equivalent at the end.

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var Promise = require('bluebird');
var bcrypt = require('bcrypt');
var db = require('./db').db; //users are stored in mongo

//I'm using bluebird.js for promises
var users = Promise.promisifyAll(db.users);
var compare = Promise.promisify(bcrypt.compare);

// This strategy is used by passport to handle logins
module.exports.localStrategy = new LocalStrategy(function(username, password, done) {
  users.findOneAsync({username: username}).bind({})
    .then(function(user) {
        if (!user) {
          throw new NoMatchedUserError('Incorrect username.');
        }
        this.user = user;
        return compare(password, user.password);
    })
    .then(function(isMatch) {
      if (isMatch) {
        return this.user;
      }
      else {
        throw { message: 'Incorrect password.' };
      }
    })
    .nodeify(done);
});


Storing the contents of text fields and inserting the stored values on button-click I've created a little GUI where the text entered in the text-fields is stored in a property of the currently selected button. If a different button is clicked, the contents stored for that button are inserted into the text-fields.
For storing the information I'm currently using a class where each text-field's contents represent a separate attribute:
from PyQt5.QtWidgets import *

class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        v_layout = QVBoxLayout()
        cent_widget = QWidget()
        cent_widget.setLayout(v_layout)
        self.setCentralWidget(cent_widget)

        # Buttons
        h_layout = QHBoxLayout()
        v_layout.addLayout(h_layout)

        self.btn_group = QButtonGroup()
        self.btn_group.setExclusive(True)

        btn_1 = QPushButton('Button 1')
        btn_2 = QPushButton('Button 2')
        btn_3 = QPushButton('Button 3')

        for w in [btn_1, btn_2, btn_3]:
            w.setProperty('data', Storage())
            w.setCheckable(True)
            w.clicked.connect(self.set_data)
            self.btn_group.addButton(w)
            h_layout.addWidget(w)
        btn_1.setChecked(True)

        # Text Fields
        self.tf_1 = QLineEdit()
        self.tf_2 = QSpinBox()
        self.tf_3 = QLineEdit()

        for w in [self.tf_1, self.tf_2, self.tf_3]:
            w.editingFinished.connect(lambda x=w: self.update_storage(x))
            v_layout.addWidget(w)

    def update_storage(self, widget):
        active_storage = self.btn_group.checkedButton().property('data')

        if widget == self.tf_1:
            active_storage.field1 = self.tf_1.text()
        elif widget == self.tf_2:
            active_storage.field2 = self.tf_2.value()
        elif widget == self.tf_3:
            active_storage.field3 = self.tf_3.text()

    def set_data(self):
        btn = self.btn_group.checkedButton()
        storage = btn.property('data')

        self.tf_1.setText(storage.field1)
        self.tf_2.setValue(storage.field2)
        self.tf_3.setText(storage.field3)

class Storage:
    def __init__(self):
        self.field1 = ''
        self.field2 = -1
        self.field3 = ''

if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

I have a feeling like I'm working against Qt here or I'm at least not using its full potential, because I think Qt could include some tools to make this more efficient and I just did not find them.

Your implementation of Storage lends itself nicely to using a dataclass.
from dataclasses import dataclass

@dataclass
class Storage:
    field1: str = ""
    field2: int = -1
    field3: str = ""

This makes your class definition really concise and readable and provides improved scalability and convenient implementations of built-in functions if you ever need them.
You should also consider giving fields and widgets more meaningful names (maybe even changing button text to match their functionality) to improve readability and usability.
Further material on dataclasses:

Data Classes in Python 3.7+ (Guide) on RealPython
Python dataclasses on Youtube by mCoding

The way you create the three buttons does not scale well, changing this part will have to be done by hand and is therefore error-prone. I'm talking about this code snippet:
btn_1 = QPushButton('Button 1')
btn_2 = QPushButton('Button 2')
btn_3 = QPushButton('Button 3')

for w in [btn_1, btn_2, btn_3]:
    w.setProperty('data', Storage())
    w.setCheckable(True)
    w.clicked.connect(self.set_data)
    self.btn_group.addButton(w)
    h_layout.addWidget(w)

I find the following approach to be a lot cleaner, while producing the same result. You basically only need to change the stop argument in range(start, stop) if you want to change the amount of buttons:
for i in range(start=1, stop=4):
    btn = QPushButton(f"Button {i}")
    btn.setProperty('data', Storage())
    btn.setCheckable(True)
    btn.clicked.connect(self.set_data)

    self.btn_group.addButton(btn)
    h_layout.addWidget(btn)

    setattr(self, f"btn_{i}", btn)

I'm sure there's a better way to set up update_storage and set_data without hardcoding the widgets, but I didn't think of a satisfactory approach yet.


Vue.js v-if for a checkbox selection of data from a csv I'm creating an option checkbox for an Organization data only if they have Organization data to submit from their CSV. I am creating a
 and my <select> and <option> are wrapped inside of the
.

Do I need to create an input ? I'm in Vue.js and don't see any current methods or v-if nested in the right way. I want this selection of this checkbox to be done upon upload of the CSV and prior to submit.

HTML

  <input type="checkbox" id="organization" ref="Organization" v-model="checkbox" :v-if="field in requiredOrganizationFields"

 Organization Fields

    {{field}}
    <select :id="field" :name="field" v-model="mapping[field]" required>
      <option value="">Please Select</option>
      <option v-for="columnName in columnNames" :name="columnName" :value="columnName"  value="">{{columnName}}</option>
    </select>

JavaScript

    data: null,
          columnNames: [],

  file: null,
  requiredContactFields: [
    'contact_account_name',
    'contact_first_name',
    'contact_last_name'
      ],
  optionalContactFields: [
    'contact_email',
    'contact_phone',
    'contact_address',
    'contact_city',
    'contact_region',
    'contact_postal_code'
      ],
  requiredOrganizationFields: [
      'organization_account_name',
      'organization_name'
      ],
  optionalOrganizationFields: [
      'organization_email',
      'organization_phone',
      'organization_address',
      'organization_city',
      'organization_region',
      'organization_postal_code'

      ]

}

getColumnNames() {
  let formData = new FormData();
  formData.append('file', this.file);
  let self = this;
  axios
  .post('http://localhost:8080/api/contacts/csv-column-names',
  formData,
  {
    headers: {
      'Content-Type': 'multipart/form-data'

    }
  }

  )
  .then(function(response){
    console.log('SUCCESS!!');
    console.log(response);
    //Need array of column names you get from the back-end
    self.columnNames = response.data.column_names;

  })
  .catch(function(response){
    console.log('FAILURE!!');
    console.log(response);

  })

},

If you want to allow a user to be able to input an optional field after checking that check box it will go like this

You bind the field you want to make optional in your v-model. Upon the click event that optional field should dropdown

HTML

  <input type="checkbox" name="show_organization_fields" v-model="showOrganizationFields">

Organization Fields

    {{field}}
    <select :id="field" :name="field" v-model="mapping[field]" required>
      <option value="">Please Select</option>
      <option v-for="columnName in columnNames" :name="columnName" :value="columnName"  value="">{{columnName}}</option>
    </select>

Javascript

data: null,
      columnNames: [],

      showOrganizationFields: false,

      file: null,
      requiredContactFields: [
        'contact_account_name',
        'contact_first_name',
        'contact_last_name'
          ],
      optionalContactFields: [
        'contact_email',
        'contact_phone',
        'contact_address',
        'contact_city',
        'contact_region',
        'contact_postal_code'
          ],
      requiredOrganizationFields: [
          'organization_account_name',
          'organization_name'
          ],
      optionalOrganizationFields: [
          'organization_email',
          'organization_phone',
          'organization_address',
          'organization_city',
          'organization_region',
          'organization_postal_code'

          ]