How to make conditional with bat script and random number

Question: I want to make a conditional with random number in BAT. If the number is smaller than 10 I want to execute something, otherwise execute other thing but my code below is not working.
IF %RANDOM% * 100 / 32768 + 1 GEQ 10 start program1.exe
ELSE start program2.exe

Why this does not work?
EDIT:
SO FAR I GOT THIS:
SET /A numero_aleatorio=%RANDOM%*100/32768+1

IF %numero_aleatorio% GEQ 50 (start firefox1.exe)
ELSE (start firefox2.exe)

The problem is that windows is saying ELSE IS NOT RECOGNIZED. WHY?
Answer: You need to do the maths in advance by a set /A command, like:
set /A numero_aleatorio=%RANDOM%*100/32768+1

Then compare the result, like:
if %numero_aleatorio% GEQ 10 (start program1.exe) else (start program2.exe)

For the if/else block to work you must state the else clause in the same line as the ). The following would provide the same result as the above:
if %numero_aleatorio% GEQ 10 (
    start program1.exe
) else (
    start program2.exe
)

Or also this:
if %numero_aleatorio% GEQ 10 (start program1.exe
) else start program2.exe

The conditional command block before else must be within parentheses, otherwise the parser does not know where it ends and thinks the else keyword is part of that block.


Using an IF statement in my SiteMaster.cs file

Question: In my SiteMaster.cs file I want to be able to run an IF statement based on the current page the user is on.
I'm currently getting the file name using: currentPage.Text = this.Page.ToString().Substring(4, this.Page.ToString().Substring(4).Length - 5) + ".aspx"; but I'd like to then use this to run an IF statement.
It would basically be like (if currentPage == "default.aspx") { // do this }
I'm very new to .NET and taking on a existing project.
Can anyone point me in the right direction to achieve this?
Many thanks
Answer: You need to get the page name from the Request.Url.AbsoluteUri.
if(Request.Url.AbsolutePath.Contains("Default.aspx"))

Or, below will return Default.aspx
Request.Url.AbsolutePath.Substring(Request.Url.AbsolutePath.LastIndexOf('/')+1)

Comment: This worked perfectly, Muhammad. Many thanks once more for your speedy and patience with me. Thank you
Answer: Its alot better to compare type.  Every page is a class.
if(Page is _Default)
{
    // do work
}

If you have to import a namespace and you have visual studio 2010 you can highlight _Default and holding CTRL + .
Comment: Hi, unfortunately I could not get this working. I get the error: `The type or namespace name 'Default' could not be found (are you missing a using directive or an assembly reference?)`
Comment: Default is an example of what your page class name could be.  also if you have your pages in a different folder/namespace than your master page then you may need to add a using statement.  Look at the code behind of the page you want to check for and see what its class type is.  Also not the namespace it is in.
Comment: Apologies. I'm very new to this. In 'Default.aspx.cs' I have: `public partial class _Default : System.Web.UI.Page` so does this mean my page class name is '_Default'? I have the master and 'Default.aspx' in the same folder. Thanks again


Right unit implies left unit if elimination laws hold

Question: Let $(C, \otimes, I)$ be a set $C$ with an associative multiplication $\otimes:C\times C\to C $, and $I\in C$ satisfying the right unit law, $c I=c$ for all $c\in C$. If $\otimes$ satisfies elimination laws on two sides (i.e., $ab=ac$ or $ba=ca$  implies $b=c$), then $Ic=c$ for all $c$.
I think the proof should be simple, but I failed to procedure one.
Comment: Consider $cIc$.
Comment: oh, that's trivial. thanks a lot.
Comment: Now you can write up an answer, and post it (the software may make you wait a while). Then later you can accept it. That helps clear up the Unanswered Questions list.
Answer: Let $c \in C$.
$cc = cc$
(By hypothesis: $c = cI$)
$ \iff (cI)c = c(cI)$
(By associativity:)
$ \iff c(Ic) = c(cI)$
(By left-cancellation:)
$ \iff Ic = cI$
(By hypothesis: $cI = c$)
$ \iff Ic = c$
Thus, a cancellable semigroup with a left identity is a cancellable monoid.
Similarly, a cancellable semigroup with a right identity is a cancellable monoid.
$\blacksquare$


Angular 2 @ContentChildren vs @ViewChildren vs @Query

Question: I know that @ContentChildren selects elements from a <ng-content> element and @ViewChildren from 'outside' of <ng-content> element, e.g. what's inside of you HTML template.
How is @Query different from them and why it can be used in a constructor and the two above cannot?
(What is better to use?)
Answer: @Query and @ViewQuery are deprecated and replaced by @ContentChildren and @ViewChildren
See also

https://github.com/angular/angular/blob/master/modules/angular2/src/core/metadata.dart#L162
https://github.com/angular/angular/blob/master/modules/angular2/src/core/metadata.dart#L137
Comment: Links are not working @Gunter,,
Comment: I can't find where're `@Query` and `@ViewQuery` marked as deprecated. Am I looking at the right place? https://angular.io/docs/ts/latest/api/core/ViewQuery-var.html


Store with MixedCollection data

Question: Is it possible to do this process in ExtJS 4.1.x ?
var myMixedCollection = myStore.queryBy(...);
var anotherStore = Ext.create('Ext.data.Store', { data: myMixedCollection, ... });
var myGrid = Ext.create('Ext.grid.Panel', { store: anotherStore, ... });

Because my grid displays nothing or just one empty line.
When I log my myMixedCollection there is no problem all data are here but when I open my anotherStore with Firebug i can see there is only one empty line in my data store.
Answer: myMixedCollection will be a collection of records (model instances) and as long as the new store hast the same model set this will work! So the answer is Yes
Well, for sure you need to call getRange() on the myMixedCollection instance
Here is a working example
 // Set up a model to use in our Store
 Ext.define('Simpson', {
     extend: 'Ext.data.Model',
     fields: [
         {name: 'name', type: 'string'},
         {name: 'email', type: 'string'},
         {name: 'phone', type: 'string'}
     ]
 });

var s1 = Ext.create('Ext.data.Store', {
    model:'Simpson',
    storeId:'simpsonsStore',
    fields:['name', 'email', 'phone'],
    data:{'items':[
        { 'name': 'Lisa',  "email":"email@example.com",  "phone":"555-111-1224"  },
        { 'name': 'Bart',  "email":"firstname.lastname@example.com",  "phone":"555-222-1234" },
        { 'name': 'Homer', "email":"email@example.com",  "phone":"555-222-1244"  },
        { 'name': 'Marge', "email":"firstname.lastname@example.com", "phone":"555-222-1254"  }
    ]},
    proxy: {
        type: 'memory',
        reader: {
            type: 'json',
            root: 'items'
        }
    }
});

var mixed = s1.queryBy(function(rec){
     if(rec.data.name == 'Lisa')
         return true;
});

var s1 = Ext.create('Ext.data.Store', {
    model:'Simpson',
    storeId:'simpsonsStore2',
    fields:['name', 'email', 'phone'],
    data: mixed.getRange(),
    proxy: {
        type: 'memory',
        reader: {
            type: 'json'
        }
    }
});

Ext.create('Ext.grid.Panel', {
    title: 'Simpsons',
    store: Ext.data.StoreManager.lookup('simpsonsStore2'),
    columns: [
        { text: 'Name',  dataIndex: 'name' },
        { text: 'Email', dataIndex: 'email', flex: 1 },
        { text: 'Phone', dataIndex: 'phone' }
    ],
    height: 200,
    width: 400,
    renderTo: Ext.getBody()
});

and the JSFiddle
Comment: Yes it works, I forget to use the getRange() method. Thx sra.
Answer: Yes, it is possible.
Try this:
//this is the model we will be using in the store
Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'id',    type: 'int'},
        {name: 'name',  type: 'string'},
        {name: 'phone', type: 'string', mapping: 'phoneNumber'}
    ]
});

var data = new Ext.util.MixedCollection();
data.add('key1', {
    id: 1,
    name: 'Ed Spencer',
    phoneNumber: '555 1234'
});
data.add('key2', {
    id: 2,
    name: 'Abe Elias',
    phoneNumber: '666 1234'
});

//note how we set the 'root' in the reader to match the data structure above
var store = Ext.create('Ext.data.Store', {
    model: 'User',
    data : data.items,
    proxy: {
        type: 'memory',
        reader: {
            type: 'json',
            root: 'users'
        }
    }
});

store.each(function(record){
    console.log(record.get("name"));
});

You can see it woking on jsfiddle here: http://jsfiddle.net/lontivero/Lf9yv/1/
Comment: Lol, so well here is the proof then ;-) +1 for all the same answer.  Dunno if your repurtation is already enough, but you can see the upvote/downvote rate if you tip on the score. Yours is now 1/1 ;) last but not least, no I didn't.
Comment: awww, -1 trolls are about again? here is a +1 to make them go away :)

Store with MixedCollection data


How to register middlware with illuminate/routing standalone package?

Question: Docs say to register middleware in app/Http/Kernel.php. How do I register middleware outside of the framework?
Comment: Why would you say it's outside the framework?
Comment: what I mean is I am not using the whole framework, but just the routing component in a non-laravel application.
Answer: I'm not quite sure what you mean by outside of the framework but it pretty much comes down to this:
// Get your Illuminate\Routing\Router from somewhere.
// This could either be from the container instance or
// an instance you instantiated yourself somewhere
$router = new Illuminate\Routing\Router(....
// or in case you plan on doing this in a service provider for example
$router = $this->app['router'];

// Now you add your middleware using the following syntax
$router->middleware('name', My\Middleware::class);

// It's also possible to add a middleware group
$router->middlewareGroup('group_name', [
    'name',
    My\Other\Middleware::class
]);

// It's also possible to do this using the Laravel Facade's
Route::middleware(...
Route::middlewareGroup(...

Comment: Yes, this is what I was looking for. Thanks! Btw, I also figured out that I could use the full name of the class when assigning middleware to the route, i.e. `Route::group(['middleware' => \My\Middleware::class], function () { ...`. The usage of aliases throughout the documentation confused me here.
Comment: Awesome, did not know that either! :)


valid nodejs code in string to javascript object

Question: My Goal: I am trying to encrypt .js files into .jse and decrypt only when it is running (obfuscate nodejs code).

var ffi = require('ffi');

//libpcrypt.so is a library to encrypt and decrypt files
var crypt = ffi.Library('./libpcrypt', {
  'decrypt' : [ 'string', ['string', 'string']]
});

require.extensions[".jse"] = function (module) {
   module.exports = (crypt.decrypt(module.filename, 'out'));
};

console.log(require('./routes.jse'));

I know, with cosole.log() source code can be printed out.
Problem: Decrypted code is a plain string, I am not able to convert it into a valid javascript object for exports. Is there a way to export the code string I decrypted?
Answer: Here's your solution (not tested):
require.extensions['.jse'] = function(module, filename) {
  var content = crypt.decrypt(fs.readFileSync(filename), 'out')
  return module._compile(content, filename);
};

Happy debugging encrypted modules ;)
Comment: This code should work, because this code stolen from working library. Check your sources you are trying to _compile.
Comment: Thanks. I saw this http://stackoverflow.com/a/9163557/458816 too. But I get `SyntaxError: Unexpected token ILLEGAL`. I thought it is encoding issue and converted to utf-8. but no use.
Comment: I am still struggling with the error. Do you think it can be encoding issues? Any other suggestions?.
Comment: What do you see on `console.log(content);`? Expected source code? What if replace real code with some simple example like `module.exports = 2 + 2;`
Comment: I see real code. And I tried as simple as `var x = 10`. Still same problem.
Comment: This is the correct solution. I had some other issues in .so.
Answer: module.exports is an object you can assign to (ie: module.exports.newFunc = someFunction;)
JSON.parse(crypt.decrypt(module.filename, 'out'));

EDIT So you should make your encrypted file a JSON class OR check out this answer to a similar question Load "Vanilla" Javascript Libraries into Node.js
Comment: @mv945 - So you should make your encrypted file a JSON class OR check out this answer to a similar question http://stackoverflow.com/questions/5171213/load-vanilla-javascript-libraries-into-node-js
Comment: it didn't work. Because the string is plain and not JSON.stringify generated.
Parse Error:  `SyntaxError: Unexpected token e`
Comment: you pointed me right direction. thanks.. But I cannot accept your answer unless you edit it :)


Port translation with iptables

Question: I need to port forward with translation on a range of ports.
If I do:
<code>iptables -t nat -A PREROUTING -i ppp0 -p tcp --dport 2000:3000 -j DNAT --to 192.168.1.41:4000-5000
</code>
it doesn't work as it should. If I connect to WANIP:2001 it tries to connect to 192.168.1.41:4000 instead that 192.168.1.41:4001.
Is there any way to do this without creating 1000 iptables rules?
Comment: What do you need this for?
Comment: I need to make a router
Comment: What?  I am sorry, but that saying you want a router did not  clear up things at all.  Is there some reason why you can't just NAT everything to 192.168.1.41?
Comment: because it's not a DMZ :)
Comment: It's not working as you *expected*, but it's working as it *should*. NAT to a range will select *any* unused port in the range, and *not* do a 1-to-1 mapping.
Answer: There is no way to do range mapping like you are trying to do. You could write a 3 line script to generate the rules for you, but that would be a bad hack. I really think you are on the wrong track here. @Zoredache already tried to suggest this might not be the right solution at all. Please be more explicit with your situation so we can recommend something.
You probably do need some kind of NAT, but there are several kinds including full pass throughs that can route traffic over the full range of ports both directions to one machine, and other machines can tag along for one direction. Give us your architecture and we can suggest a proper solution. Setting up 1000 iptables rules isn't it.

Port translation with iptables


Testing for the significance of the difference-in-differences of adj. R²

Question: Is there a way to test for the significance of the difference-in-differences in adj. R²s in Stata?
Let's say I have four subgroups: pre-treatment, pre-control, post-treatment, post-control and I want to figure out whether the difference-in-differences of the adj. R² of the same regression (e.g., R² of Y = b0 + b1*X + e) conducted for each of the four subgroups is signficant.
In other words, is (adj. R²(post-treatment) - adj. R²(pre-treatment)) - ((adj. R²(post-control) - adj. R²(pre-control)) signficant?
I read somewhere that it might be possible to bootstrap the adj. R² of each regression and then test for the difference-in-differences, but I am not sure if this would be an appropriate approach?
Thanks a lot!
Answer: Whilst it is true that you can bootstrap $\text{R}^2$ and $\overline{\text{R}}^2$ (see Ohtani (2001) "Bootstrapping $\text{R}^2$ and adjusted $\text{R}^2$ in regression analysis") the procedure you propose will result in having 4 numbers, i.e. the four $\text{R}^2$ or $\overline{\text{R}}^2$ from the regressions of $Y$ on $X$ for the treatment and control groups in the two time periods. In this case you end up like the OP of this earlier question who tried to achieve exactly that but finally figured out that it could not be done. So even if you can construct a standard error or confidence interval for each coefficient of determination itself, with four numbers there is no degree of freedom left in order to calculate a standard error for the difference in differences.
Comment: So if I understand correctly, I would need the standard error/confidence interval (distribution) for the differences between two groups (e.g., R² treatment - R² control) which is probably not computeable for R² (e.g., by random sampling of the differences), or am I wrong here?
Comment: Actually you would need the standard error of the difference in differences. If you had a sequence of each R² measure this is feasible but not with only 4 numbers only.
Comment: Is it feasable to create a sequence of R² difference-in-differences by drawing random observations from the full sample? Let's say, I randomly draw a number of treatment obs. and (matched) non-treatment obs., calculate the difference-in-differences of their R², and repeat that x times to get the distribution of the difference-in-differences?
Comment: I can see where you are coming from though I'm somewhat hesitant towards this procedure, yet I haven't found a good reason against it so far. How do you intend to obtain a standard error for the difference in differences estimate in this way?
Comment: Let's say I randomly draw a number of matched pairs in the pre-period and then calculate an R² for these observations, but seperately for each "bucket" (pre-treat; pre-control; post-trest; post-control). This way I'm able to calculate a diff-in-diff R². Repeating this procedure x times I'd get the distribution, or?


Erro no TypeScript: Object is possibly 'null'

Question: Eu tenho o seguinte componente em Angular:
import {Component, OnInit} from '@angular/core';
import {ActivatedRoute} from '@angular/router';

@Component({
  templateUrl: './course-info.component.html'
})

export class CourseInfoComponent implements OnInit {
  courseId = 0;
  constructor(private activatedRoute: ActivatedRoute) {  }
  ngOnInit(): void {
    this.courseId = +this.activatedRoute.snapshot.paramMap.get('id');
}

O TS está me dando o seguinte erro no trecho this.activatedRoute.snapshot.paramMap.get('id'):

TS2531: Object is possibly 'null'.

Alguém sabe como eu posso tratar isso?
Comment: Não sei se entendi o problema, mas não seria apenas usar o operador **OU `(||)`** depois da expressão?! Ficando assim: `this.courseId = +this.activatedRoute.snapshot.paramMap.get('id') || 123`
Comment: Adicionar "optional chaining" resolve? `+this.activatedRoute?.snapshot?.paramMap?.get('id')` , apenas sugerindo.
Comment: Não, fica com o mesmo erro
Comment: Entao tente adicionar o `!` no final, `+this.activatedRoute.snapshot.paramMap.get('id')!`, quando voce tem certeza que esse objeto existe, voce afirma isso para o TS e então ele "confia" em voce. Tente isso.
Comment: ou `+this.activatedRoute!.snapshot!.paramMap!.get('id')`
Comment: Quando faço isso dá certo mas aí fica dando o seguinte erro no this: Forbidden non null assertion(no-non-null-assertion)
Comment: Isto responde à sua pergunta? [Como lidar com união com undefined (ou null) em TypeScript?](https://pt.stackoverflow.com/questions/493095/como-lidar-com-uni%c3%a3o-com-undefined-ou-null-em-typescript)
Answer: O problema é que pode acontecer de você tentar converter um null para Number com o operador unário +, e o TypeScript interpreta isso como um possível erro de lógica.
Caso seja "tudo bem" converter null para 0, isso pode ser resolvido com o uso do construtor Number(variável) para a conversão do valor, porque o construtor Number() aceita qualquer valor (any). Caso contrário, faça um tratamento para o valor null.
const teste = null;
console.log(+teste); // Aqui aparece o erro

console.log(Number(teste)); // Aqui não há erro

// Mas repare que o comportamento em JS de ambos os casos é o mesmo

Veja no Playground

Esse erro acontece no operador unário + pois na chamada inteira abaixo, o único possível retorno null é no final da corrente, no .get().
+this.activatedRoute.snapshot.paramMap.get('id')

Então fazer uso do optional chaining, conforme sugerido nos comentários, seria algo desnecessário e não resolveria o problema, visto que nem activatedRoute, snapshot ou paramMap podem retornar valores null. Isso pode ser observado através da própria documentação do Angular:
interface ActivatedRoute {
  snapshot: ActivatedRouteSnapshot
  // ...
}

interface ActivatedRouteSnapshot {
  paramMap: ParamMap
  // ...
}

interface ParamMap {
  get(name: string): string | null  // Aqui pode retornar null
  // ...
}

O TypeScript também aponta o uso de non null assertion (o uso de !) como um erro porque você simplesmente estaria ignorando um caso (o retorno null) que pode ou não resultar em um comportamento inesperado na sua aplicação.
Answer: Perfeito o que o Cmte Cardeal falou! Acrescente um "!" após essa parte do código que irá passar perfeitamente --> +this.activatedRoute.snapshot.paramMap.get('id')!


Mocking os.path.exists and os.makedirs returning AssertionError

Question: I have a function like below.
# in retrieve_data.py

import os

def create_output_csv_file_path_and_name(output_folder='outputs') -> str:
    """
    Creates an output folder in the project root if it doesn't already exist.
    Then returns the path and name of the output CSV file, which will be used
    to write the data.
    """
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
        logging.info(f"New folder created for output file: " f"{output_folder}")

    return os.path.join(output_folder, 'results.csv')

I also created a unit test file like below.
# in test_retrieve_data.py

class OutputCSVFilePathAndNameCreationTest(unittest.TestCase):

    @patch('path.to.retrieve_data.os.path.exists')
    @patch('path.to.retrieve_data.os.makedirs')
    def test_create_output_csv_file_path_and_name_calls_exists_and_makedirs_once_when_output_folder_is_not_created_yet(
            self,
            os_path_exists_mock,
            os_makedirs_mock
    ):
        os_path_exists_mock.return_value = False
        retrieve_cradle_profile_details.create_output_csv_file_path_and_name()
        os_path_exists_mock.assert_called_once()
        os_makedirs_mock.assert_called_once()

But when I run the above unit test, I get the following error.
def assert_called_once(self):
    """assert that the mock was called only once.
    """
    if not self.call_count == 1:
        msg = ("Expected '%s' to have been called once. Called %s times.%s"
               % (self._mock_name or 'mock',
                  self.call_count,
                  self._calls_repr()))
         raise AssertionError(msg)
         AssertionError: Expected 'makedirs' to have been called once. Called 0 times.

I tried poking around with pdb.set_trace() in create_output_csv_file_path_and_name method and I'm sure it is receiving a mocked object for os.path.exists(), but the code never go pasts that os.path.exists(output_folder) check (output_folder was already created in the program folder but I do not use it for unit testing purpose and want to keep it alone). What could I possibly be doing wrong here to mock os.path.exists() and os.makedirs()? Thank you in advance for your answers!
Comment: Not a direct answer to your problem, but have you considered using [pyfakefs](https://pypi.org/project/pyfakefs/)?  It mocks the whole filesystem, so you don't need to focus your tests on the implementation, but rather test the behavior and the end results.
Comment: I'd suggest looking into getting approval, we have similar restrictions but it usually just requires talking to someone. If not I would probably write your own fixture utilizing [TemporaryDirectory](https://docs.python.org/3/library/tempfile.html#tempfile.TemporaryDirectory) instead of mocking all of the os calls. That will lead to more flexible tests resistant to having changes in implementation breaking them.
Comment: @flakes Thank you for the suggestion. I am writing code for a corporation and it doesn't allow me to integrate/use unapproved packages (meaning, the available Python packages are limited).
Answer: You have the arguments to your test function reversed. When you have stacked decorators, like:
@patch("retrieve_data.os.path.exists")
@patch("retrieve_data.os.makedirs")
def test_create_output_csv_file_path_...():

They apply bottom to top, so you need to write:
@patch("retrieve_data.os.path.exists")
@patch("retrieve_data.os.makedirs")
def test_create_output_csv_file_path_and_name_calls_exists_and_makedirs_once_when_output_folder_is_not_created_yet(
    self, os_makedirs_mock, os_path_exists_mock
):

With this change, if I have this in retrieve_data.py:
import os
import logging

def create_output_csv_file_path_and_name(output_folder='outputs') -> str:
    """
    Creates an output folder in the project root if it doesn't already exist.
    Then returns the path and name of the output CSV file, which will be used
    to write the data.
    """
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
        logging.info(f"New folder created for output file: " f"{output_folder}")

    return os.path.join(output_folder, 'results.csv')

And this is test_retrieve_data.py:
import unittest
from unittest.mock import patch

import retrieve_data

class OutputCSVFilePathAndNameCreationTest(unittest.TestCase):
    @patch("retrieve_data.os.path.exists")
    @patch("retrieve_data.os.makedirs")
    def test_create_output_csv_file_path_and_name_calls_exists_and_makedirs_once_when_output_folder_is_not_created_yet(
        self, os_makedirs_mock, os_path_exists_mock
    ):
        os_path_exists_mock.return_value = False
        retrieve_data.create_output_csv_file_path_and_name()

        os_path_exists_mock.assert_called_once()
        os_makedirs_mock.assert_called_once()

Then the tests run successfully:
$ python -m unittest -v
test_create_output_csv_file_path_and_name_calls_exists_and_makedirs_once_when_output_folder_is_not_created_yet (test_retrieve_data.OutputCSVFilePathAndNameCreationTest.test_create_output_csv_file_path_and_name_calls_exists_and_makedirs_once_when_output_folder_is_not_created_yet) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

Update I wanted to leave a comment on the diagnostics I performed here, because I didn't initially spot the reversed arguments, either, but the problem became immediately apparent when I added a breakpoint() the beginning of the test and printed out the values of the mocks:
(Pdb) p os_path_exists_mock
<MagicMock name='makedirs' id='140113966613456'>
(Pdb) p os_makedirs_mock
<MagicMock name='exists' id='140113966621072'>

The fact that the names were swapped made the underlying problem easy to spot.
Comment: That is it! I can't believe I struggled for more than an hour because I didn't know that `@patch` is applied in reverse order.  Thank you so much!


Angular 4 w/ AngularFire 2 and Auth Guard

Question: I have started creating a simple app that has a pre-login and post-login section.  I'm using FireBase (with AngularFire2) as my authentication provider and I'd like to "Guard" the post-login pages using canActivate() in an auth guard service.
My problem is, I don't know the order or placement of each piece of code.  I've simplified what I have below:
app.component.ts
constructor(private auth: Authentication) {
    this.auth.subscribe();
}

authentication.ts
export class Authentication {

    state: Observable<firebase.User>;
    user: any;
    isLoggedIn: boolean;

    constructor(private afAuth: AngularFireAuth, private router: Router) {
        this.state = afAuth.authState;
    }

    subscribe() {
        this.state.subscribe((auth) => {
            if(auth) {
                this.user = auth;
                console.log(this.user);
                this.isLoggedIn = true;
                return true;
            } else {
                this.isLoggedIn = false;
                return false;
            }
        });
    }

    login() {
        this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
    }

    logout() {
        this.afAuth.auth.signOut();
    }
}

auth-guard.service.ts
export class AuthGuard implements CanActivate {

    constructor(private auth: Authentication, private router: Router) {}

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        let url: string = state.url;
        return this.checkLogin(url);
    }

    checkLogin(url: string): boolean {
        if(this.auth.isLoggedIn) {
            console.log("Can Activate True");
            return true;
        } else {
            console.log("Can Activate False");
            return false;
        }
    }
}

Right now it's pretty much working.  When I start fresh, I get a screen with all of my links on the top of the screen, a login and a logout button.  When I click on the links at the top of the screen, I get the console.log saying "Can Activate False", which is perfect.  I'm not logged in yet.
When I log in, then try the links again, I get the "Can Activate True" which is also perfect.
When I log out again, and without refreshing, I try to click on the links above and I don't get any console.log back.
I feel like something is wrong with my subscribe or just everything in general.  What could I be doing wrong?
Answer: Try this:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
  return this.af.authState.map(auth => {
    if (isNullOrUndefined(auth)) {
      this.router.navigate(['/login']);
      return false;
    } else {
      return true;
    }
  });
}

It is much easier. No messing around with aync and sync stuff.
Comment: I'm getting an error `Property 'map' does not exist on type 'Observable'`.  I'm taking a look into it
Comment: this is with angularfire2 4.0.0-rc.0
Comment: Correct, that's what I'm using: `"angularfire2": "^4.0.0-rc.0",`
Comment: and firebase ^3.9.0
Comment: Try adding `import 'rxjs/add/operator/map';`
Comment: So, it seems to act the same.  When I start fresh (not logged in) If I try to go to the guarded pages, it keeps me on the login page.  When I log in, I get a good auth, and I can click on one of the guarded pages.  I use a console.log in your statement (above the return) and I see that a single time.  When I click on another link, I don't get the console.log again, nor when I log out.  It's like the `return false` (or something) is making the guard stop working
Comment: I think I found the problem - I wasn't redirecting on logout, so I was technically still in the guarded components when logged out.  I'm not sure if this is the real answer, but when I `router.navigate([]);` to the login page, I'm not able to access the auths anymore.
Answer: You can use the AngularFireAuthGuard for that case.
Add the lines below to your routing.module.ts:
const redirectUnauthorizedToLogin = () => redirectUnauthorizedTo(['login']);

    path: '',
    component: UserDashboardComponent,
    //data: { title: 'user.dashboard' },
    canActivate: [AngularFireAuthGuard],
    data: {authGuardPipe: redirectUnauthorizedToLogin}

This code-snipped will do a redirect if the User is not authenticated. And furthermore its easy to implement.


Question on the harmony in Sweet Child o'mine

Question: Sweet child o'mine has this progression in the intro:
Db (sus4)-Db-Db(sus4)-Db-Ebm11/Cb-Gb (add2)-Db (sus4)-Db-Db(sus4)-Db
And this progression in the verse:
Db-Cb-Gb-Db
How would you explain this progression (I-VIIb-IV)?
The intro and verse has this same progression but differ in one place. The intro has Ebm11/Cb. How would you explain this chord?
Should this progression be explained with classical music theory or with more modern music theory?
I remember a teacher saying that classical music is sometimes based on moving chords in thirds or sonething similar rather than being based on I-IV-V-I. Does this apply to this song/tune?
Answer: No, this is not classical. I see it as a mixture of the folky (minor / Dorian) ⅰ - Ⅶ alternation and several blues idioms. Either you interpret it as Ⅰ - _ - Ⅳ - Ⅰ with the folk-Ⅶ in the gap borrowed from the parallel minor key (borrowing from the parallel minor is always an option in bluesy music), or you start right with the blues progression Ⅴ - Ⅳ - Ⅰ and look at it from the dominant key (Mixolydian mode).
As for the ⅱ¹¹/6, I'd not interpret too much into that. It's just the result of playing the chord progression underneath that lead guitar ostinato, which technically speaking doesn't fit over the Ⅶ♭ chord, but hey, are we lawyers, doing maths, or just playing Rock'n'Roll?
(FWIW, that attitude is actually not foreign in classical music, where at least pedal points are sometimes kept even when the environment has modulated to a completely foreign key.)
Answer: You could explain it as 'cycle of 4ths'.   Make your way home through a string of plagal cadences -  a series of 'secondary subdominants' rather than the more usual secondary dominants.
It's not uncommon.  Think of the riff in the Beatles tune 'Get Back'.
Comment: Other Beatles examples include With A Little Help From My Friends, the end of Hey Jude and You've Got To Hide You're Love Away.


Scrape web data from mouseover popup

Question: I'm trying to use JavaScript to scrape data from the following page, specifically the "free shipping free returns" text that appears when you hover your mouse over the cart icon:

Whenever I hover over the cart icon, new HTML is added to the DOM.

And when I move my mouse away, the the previously added HTML goes away. I want to be able to parse data from the HTML that gets added without having the popup visible. How would I be able to scrape this text data even if someone does not hover over the cart icon? Is there a way to access all the HTML data at once?
Answer: You can try to catch the JavaScript function being executed when you hover your mouse over the cart icon. You can do this via the developer tools. Add break points to code execution if the DOM changes (on the parent element in which the new element is added).
Once you get the function, just execute it directly on that page and you'll probably be able to see the popup and extract it's contents.
You could also try to simulate a hover as explained in these answers: How do I simulate a mouseover in pure JavaScript that activates the CSS ":hover"?
Answer: Scraping a page for data is not usually recommended since they can change over time (especially ones not written directly in HTML, but are rather generated (usually they have CSS classes like 8h2H1)).
If this is not supposed to be a long-term solution, the above answer by @nvkrjn is a good answer. Or, you can just check for an element with the id name free-shipping-label.

But, if this is supposed to be a long-term solution, then I would suggest using an API (this site doesn't seem to have one) or querying the database like how to Javascript does. Also, if you're using a non-browser environment (eg BeautifulSoup), it may not run the JS required to get the data.

Scrape web data from mouseover popup


ABC, compute Bayes factor from posteriors

Question: I am pretty new to ABC stuff so I may be saying dumb things.
My question is: I ran an ABC with two models $M_1$ and $M_2$ and now I have an approximation of the posterior distribution for both model.
If I do a posterior check by re-runing enough simulation for which I sample the parameters from the posteriors, is it possible to get back the likelihood of both model in order to calculate their Bayes factor?
I was thinking (probably wrongly) that taking the ratio between the number of simulations falling under a small threshold $\epsilon$ may do something related to the Bayes factor, something that may be written like: $$ BF_{1,2}=\frac{P(M1 | d(M1,D) < \epsilon)}{P(M2 | d(M2,D) < \epsilon)}$$ (where $d()$ is the distance function used for the orginal ABC)
Answer: Since the Bayes factor is given by$$B_{12}(D) = \frac{\text{Pr}(M_1|D)}{\text{Pr}(M_2|D)}\Big/\frac{\text{Pr}(M_1)}{\text{Pr}(M_2)}$$the ratio of frequencies of simulations from $M_1$ and $M_2$ that are accepted need be divided by the prior probabilities of $M_1$ and $M_2$ if these reflect the number of times each model is simulated. Apart from this, the approximation is valid.
In a series of papers we pointed out the dangers of using summary statistics in this setting:

Lack of confidence in approximate Bayesian computation model choice
Relevant statistics for ABC model choice
Comment: interesting, why the _time taken to simulate_ should enter into consideration here? What if the real data results from generative process acting at really large time scale? Could it be possible to say that, even if one model take much more time than the other (let say ~ 10 time in my case), they are both equal wrt the original process ( ~ 4 millions times)? (By the way: thank you for your answers, not only this one but all the one you  give on Cross Validate they are an amazingly rich source of learning people like my. It's awesome to see that you take time to do so.)
Comment: My remark is a simple one, namely that, to save computing time, the number of simulations from the most expensive model may be cut down with the ABC Bayes factor still available by the correction. Thanks for your nice comments!
Comment: Thanks for your answer. Thus if both models are ran the same number of time this normalizing by the prior's ratio should'nt mater right? And thanks for the paper. I new 1 but not 2, and though my summary statistics raise a full world of problem and question that I may ask when I have more time!
Comment: Yes, keeping the same number of simulations for both models avoids the correction by the prior weights, assuming both models take about the same time to simulate.
Comment: Oh right! In that case the prior would be useful, I see. Thanks again.


Good test statistic

Question: If we have a hypothesis $H_0$ and alternative hypothesis $H_1$, a test statistic $T$ and a data set $x_1, ..., x_n$ taken from some random sample $X_1, ..., X_n$, we use $T(x_1, ..., x_n) = t$ to decide whether or not to reject $H_0$. What I thought is, if we choose adequate $T$, we can make the probability of observing an event at least as extreme as $t$ ( $P(T>t)$, for example ), be low, and thus in favor of $H_1$. So if we choose an appropriate $T$ we can deny $H_0$. My question is: is this an easy job, is it reasonable to search for such $T$ and how can we say that a test statistic is efficient?
Comment: You are asking some good questions about the choice of test statistics.  Check out similar Questions at sister site CrossValidated.SE using search terms "power" and "test statistic".  There are tradeoffs involving the sample size, population distribution, and risks of Type I vs. Type II errors, and you might be best served by reading an introductory reference for statistical probability.
Comment: You haven´t marked as accepted any of the provided answers to your previous questions. That makes me feel that you do not appreciate the work of the users who take time to answer.
Comment: Will do! Thanks :)
Comment: Ooh, I did not know that I have to mark them. I will mark them in a bit! :)
Comment: @Peter There is no need to hurry. It is sufficient if you do it in a period of few hours or so.
Comment: Pedantry: you want to choose an appropriate $t$ rather than $T$ and especially for a  a discrete distribution "as extreme or more" translates to $P(T \ge t)$ rather than $P(T \gt t)$
Comment: @Peter I´ve noticed that you´ve accepted a few of the answers. Good job. Is the answer of [BruceET](https://math.stackexchange.com/users/221800/bruceet) too sophisticated? You can ask questions if something is unclear.
Answer: Simple example. Suppose you suspect a die may give 1s with probability less than $1/6.$ So you test $H_0: p = 1/6$ vs $H_a: p < 1/6.$  You roll the die $n = 120$ times and count then number $X$ of 1's. This test statistic is the sum of 120 Bernoulli random variables. Under $H_0$ (that is, assuming $H_0$ to be true), we have $X \sim \mathsf{Binom}(100, 1/6).$
By intuition, if $X$ is very small (much smaller than $E(X) = np = 20,$ anyhow) we would reject $H_0$ in favor of $H_a.$ We want the significance level of the test to be something like 5%. That means we'd reject $H_0$ about 5% of the time when the die is fine and $p = 1/6.$
Some experimentation in R (where pbinom is a binomial CDF and qbinom is the inverse quantile function) we find that we can have a test at significance level $0.0501$ or at level $0.0275.$
qbinom(.05, 120, 1/6)
[1] 13
pbinom(12:13, 120, 1/6)
[1] 0.02753237 0.05013085

Suppose we choose level $0.0501$ so that the 'critical value' of the test
is $c = 13.$  That is, we reject $H_0,$ if $X \le c = 13,$ otherwise not.
The consequence is that we will make a Type I Error (rejecting $H_0$ when it is true) with probability very nearly 5%.
Now, one way to judge whether this is a 'good' test is to look at its 'power'. In particular, we may ask what is the rejection probability
if the die has $p = 1/9.$ Formally, we seek $P(X \le 13 | p = 1/9) = 0.5344$ --- more than half the time.  We say the power against the alternative $p = 1/9$ is $\pi(1/9) = 0.5344.$ The power is the probability of not making a Type II error.
pbinom(13, 120, 1/9)
[1] 0.5343601

Similarly $\pi(1/12) = 0.8741.$
pbinom(13, 120, 1/12)
[1] 0.8740746

One can make a 'power curve' for powers of a test at level 5% against
various alternative values: With alternatives values of $p$ in $(0, 1/6)$ the rejection probabilities can be graphed as follows.
p = seq(.001, .166, by=.001)
pwr = pbinom(13, 120, p)
plot(p, pwr, type="l", ylim=c(0,1), main="Power Curve: Level 0.05")
abline(v=c(0,1/6), col="green2")
abline(h = .05, col="red")
points(c(1/9, 1/12), c(.5344, .8741), col="blue", pch=19)

In this figure the horizontal red line shows the significance level $\alpha = 0.05.$ The two heavy blue dots represent the specific
power values (for alternatives $p = 1/9$ and $p = 1/12),$ computed above.

Notes: (1) For a test at the 5% level, the only way to get better power against a particular alternative is to increase the sample size $n$.
(2) Some texts frame the hypothesis and alternative as $H_0: p \ge 1/6$ vs $H_a: p < 1/6.$ But then $p = 1/6$ is still used for the null distribution.
Answer: You can not accept H0 only deny. So the hypotese we want to prove true is what we call H1. And to say that the test statics is efficient you need the significance level.

Good test statistic


populate taxonomy from another taxonomy

Question: I have 2 exposed filters in a search page "drop down lists", I want to populate the second exposed filer "taxonomy" from the first one:
example
vehicle {car, plane, ship}
and the second should be {BMW, Honda, etc}
only if vehicle car is selected in the first drop down list.
thanks
Comment: Is that other taxonomy or a Level ?
Comment: A level or a child for a taxonomy term.
Comment: In question you mentioned populate **taxonomy from _another_ taxonomy**
Answer: Found the solution for this, I used Hierarchical Select
and it worked like a charm.

populate taxonomy from another taxonomy


Unable to write to nfs on centos6

Question: I have 2 webservers and 3 backend servers. I made one of the directory(from webserver 2) as NFS. I successfully mounted the directory '/mnt/products/files' and had it visible from all the servers(including 3 backend servers) under /projects/www/share on all the servers, except the server which mounted the directory.
I created 'webadmin' user and 'webadmingroup' on all the servers. 'webadmin' user belongs to 'webadmingroup'.
I changed the ownership of '/mnt/products/files' as 'webadmin' and 'webadmingroup' and changed the permission with 755.
The problem is that I am unable to write to the NFS directory from client servers with 'webadmin' user. I am unable to write to it with 'root' user, but if possible, i don't want to use 'root'.
What should I do to be able to write NFS directory '/projects/www/share' from all the servers?  Even though I am using the same user, I can't.
Is there anything that I need to do?
Thanks.
Answer: NFS write access is a bit tricky at the first time :-)
1: Please check if your user and group webadmin has the same user ID and group ID as on the webservers. NFS access is based on the IDs. In a multi-user setting the first thing most admins do is somehow synchronize user ids to user names and group ids to group names over the machines. E.g. with NIS, LDAP or automated /etc/group and /etc/passwd.  (Command:   id webadmin)
2: The entry on /etc/exports on the NFS server must be rw (not ro)
Good thing: NFS is still the same on Linux distributions (and UNIX types, too).
I've found following resources best to understand the config:
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/storage_administration_guide/nfs-serverconfig
https://help.ubuntu.com/community/SettingUpNFSHowTo
If you still encounter problems, it might be a good idea to post your /etc/exports (of the NFS server) and your /etc/fstab (of the NFS client).
Comment: I certainly would recommend also to sync the gid! (Even if it works now without, it will bite you in the least convenient time).
Comment: thanks for the response..

Here is the /etc/exports in the server.
/mnt/products/files           xx.xx.xx.xxx(rw,sync,no_root_squash,no_subtree_check)

This is fstab.
xx.xx.xx.xxx://mnt/products/files   /projects/www/share   nfs      auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0

I checked the uid and gid. uid and gid in webadmin in the server 500, but it's in 501 in the client server.
I changed only the uid to match with the one from the serer. Do i need to change the gid as well?

Unable to write to nfs on centos6


Exponential Distribution PDF: Why multiply by lambda?

Question: Hoping you can help me understand the probability density function for the exponential distribution.
Given that the distribution's PDF is described as follows when x > 0:
$$\lambda e^{-\lambda x}$$
Why do we multiply by λ? Sure I'm missing something basic here, but seems that if we're modeling time to event, using e and the exponents would be enough. Is it because we might expect the event to happen multiple times in the time interval?
Thank you in advance.
Answer: So that when you integrate the pdf between 0 and infinity the probability density sums to 1 as required under the axioms of probability.
Comment: A PDF by definition has to satisfy that its integral over all possible x is 1. The intuition is that of all possible events, something has to happen. Now, when you integrate e^-lambda (without the lambda multiplied beforehand) over 0 to infinity (which are all possible outcomes), you will easily find that it evaluates to 1/lambda. If you then multiply by lambda, you get the desired area (=probability) of 1.
Comment: Aha, got it! Was missing the integral = 1/lambda. Thank you both.
Comment: Thank you -- definitely understand that this works, but feel I'm still missing a basic understanding of *why* this particular way of manipulating the formula makes sense. Have followed proofs through the CDF to the PDF and still feel I'm missing that intuitive understanding.
Comment: Yes Julian is correct.  You also may want to look at this question if you want a deeper understanding of where this distribution comes from: http://stats.stackexchange.com/questions/2092/relationship-between-poisson-and-exponential-distribution :)


How to calculate the number of all combinations of all permutations?

Question: I have a set n with m elements. I aim for the number of all combinations of all permutations of all elements of the set.
An minimal example would be with a two element set [1,2]:
All permutations of all combinations:
[1]
[2]
[2,1]
[1,2]
I know the number gets huge with only a little number of elements in the set. At 10 elements its over 100 000 possibilities.
But how to calculate how many there are?
Comment: I'd recommend writing out all possible combinations of sets of 3 and 4. You'll see a pattern when you write it out in a systematic way. Then you can generalize. Think about using factorials and combinations (i.e. choose notation).
Answer: If your set has $m$ elements, then:

we have $m$ one-element subsets, each of which contributes a single permutation
we have ${m\choose 2}$ two-element subsets, each of which contributes $2$ permutations
we have ${m \choose 3}$ three-element subsets, each of which contributes $3!=6$ permutations
and so forth.

So we sum over all possible cardinalities of subsets ($k=1, \dots, m$) to obtain:
$$\begin{align*}\sum_{k=1}^m{m\choose k}k!
= & \sum_{k=1}^m \frac{m!}{(m-k)!k!}k! \\
= & \sum_{k=1}^m \frac{m!}{(m-k)!} \\
= & \sum_{k=0}^m \frac{m!}{(m-k)!} -1\\
= & \sum_{j=0}^m \frac{m!}{j!} -1\\
= & e\Gamma(m+1,1) -1\\
= & \lfloor m!e\rfloor-1
\end{align*}$$
The last two identities are equations (35) and (36) in the Wolfram page on Binomial Sums (and called "another interesting sum", which I fully agree with). This is almost OEIS series A000522 "Total number of arrangements of a set with n elements" - "almost", since you don't seem to want to count the single permutation contributed by the empty subset, which accounts for the $-1$ term above.
Answer: Perhaps it's easier to see the combinations in a geometric arrangement. There are m elements in the set. For simplicity, my example will use the set ${1, 2, 3, 4}$, so $m=4$.
Sampling a times with replacement, where order is important: $(m)^a$
<code>(1,1) (1,2) (1,3) (1,4)
(2,1) (2,2) (2,3) (2,4)
(3,1) (3,2) (3,3) (3,4)
(4,1) (4,2) (4,3) (4,4)
</code>
Sampling a times with replacement, order not important: $\frac {m(m+1)} {2}$
<code>(1,1) (1,2) (1,3) (1,4)
      (2,2) (2,3) (2,4)
            (3,3) (3,4)
                  (4,4)
</code>
Sampling a times without replacement, order important: m permute a = $\frac {m!} {(m-a)!}$
<code>      (1,2) (1,3) (1,4)
(2,1)       (2,3) (2,4)
(3,1) (3,2)       (3,4)
(4,1) (4,2) (4,3)
</code>
Sampling a times without replacement, order not important: m choose a = $  \binom n a = \frac {m!} {a!(m-a)!}$
<code>      (1,2) (1,3) (1,4)
            (2,3) (2,4)
                  (3,4)
</code>


Sort Colour / Color Values

Question: Im looking to align the following array of colours as precisely as possible.
After searching & trying many solutions suggested on Stackoverflow, the pusher.color library has the best solution, however, it too is far from perfect. I would like to hear solutions on how we can align them perfectly.
JSFIDDLE LINK : http://jsfiddle.net/dxux7y3e/
Code:
var coloursArray=['#FFE9E9','#B85958','#FFB1AE','#FFC2BF','#C55E58','#FFC7C4','#FF9A94','#FF9D96','#FA9790','#A78B88','#A78B88','#CE675B','#DB8073','#FF9D90','#FF7361','#FFD6D1','#F9A092','#FF7B67','#EBACA2','#FF806D','#DD6D5B','#D16654','#ED8673','#FFC4B8','#E2725B','#ED7A64','#8F3926','#BD492F','#9D3C27','#AD533E','#BF4024','#FFC9BC','#6B6766','#E1CDC8','#C2654C','#B3978F','#FFC7B8','#CE2B00','#C2654C','#A24D34','#FF926D','#E78667','#FFB198','#8C756D','#9E6D5B','#FFC7B0','#FFBEA4','#D2B9AF','#FFB193','#632710','#B26746','#976854','#F44900','#E79873','#EFA27F','#532510','#BC866B','#FDE5D9','#FF5B00','#D18C67','#FF5B00','#9E4312','#763713','#BB6B39','#B5622E','#CC7742','#6D4227','#B56B38','#FF7518','#F3B080','#995C30','#995C30','#FF6A00','#D89769','#71472A','#EDAC7B','#EEAB79','#EBCFB9','#FBE3D1','#E19255','#5E381B','#FFDCC1','#FFF0E4','#F68D39','#7B5B40','#FF8313','#FFCEA4','#AA8667','#975414','#CB9867','#8C5B2B','#FFCE9E','#7B4714','#FFF3E7','#FFA449','#CEAF90','#CDB69E','#EFD6BC','#DDA66B','#B27737','#B88A57','#CE9B61','#F4C38B','#543817','#BC9C78','#DBB07A','#FF8E04','#F6EADB','#DBC2A4','#C49B64','#CBA26B','#80551E','#FF9200','#FFECD3','#FFC87C','#FFB755','#DBB680','#D2D0CD','#EFDBBE','#E5C18B','#FFE5BC','#F2EADB','#885F12','#FFE7B6','#825A08','#906712','#F2D18E','#C8C6C2','#FFB000','#FFC243','#C6BEAD','#D0C3A4','#916800','#8C6700','#F4E9CA','#FFF0C5','#FFE080','#FFEBA8','#846600','#FFE692','#F5F0DB','#433F2F','#BBB394','#FFEFAA','#FFE76D','#FFFAE0','#3E3B28','#554900','#E1E0D8','#74725C','#605F54','#F8F7DD','#A5A467','#DDDDDA','#FFFFEE','#A3A39D','#E0E0D7','#BEBEB9','#E8E8E5','#454531','#ACACAA','#E9E9DF','#FFFFDC','#EBEBE7','#979831','#C5C6BE','#B9C866','#898D72','#F3FAD1','#616452','#CED5B0','#A1A787','#595C4E','#B0BB8C','#EEFFB6','#ACB78E','#8FA359','#858F6C','#86916E','#374912','#AEB0AA','#79904C','#627739','#747F60','#9FA98E','#E7F9CB','#E1F9BE','#495637','#8A9978','#4E5F39','#86996E','#C3CEB7','#78866B','#CEDDC1','#B5CEA2','#536149','#D6E6CC','#D6E6CC','#809873','#4F564C','#4F6C45','#555F52','#4F7942','#5F705B','#D0DFCD','#2B3929','#F0F7EF','#AAD5A4','#99BC95','#B6D4B4','#869E86','#618661','#006700','#E9EEE9','#739E73','#005B06','#EDF7EE','#D0E0D2','#809784','#ABCEB1','#C0E0C8','#3A5241','#435549','#E6ECE8','#E3EAE6','#3B604C','#00602F','#92B7A5','#2F5B49','#318061','#30745B','#316955','#00A275','#C2D1CE','#80A7A0','#00A082','#C2D1CF','#5C6E6C','#607473','#EDF7F7','#1E8285','#D5E7E8','#AADEE1','#188086','#107F87','#566364','#007B86','#66949A','#CAE2E5','#18656F','#004F61','#0C5B6C','#668E98','#BBD0DA','#91B4C5','#AFC3CD','#738A99','#3A5467','#476174','#244967','#556C80','#667A8C','#516D87','#1E4263','#7C8791','#849CB6','#738CAA','#1E3A5F','#1E3655','#9EB0CE','#B6BAC2','#67738D','#BEC1CD','#555559','#616180','#000049','#000031','#F8F8FC','#938BA4','#47375D','#F7F6F8','#3D0067','#514C53','#9566A2','#7F5482','#A279A4','#6D1261','#A06492','#925582','#945B80','#CE94BA','#ECCFE1','#A20058','#A6005B','#BC0061','#BB0061','#F3CEE1','#B3005B','#AB165F','#8A184D','#AA185B','#F3DAE4','#DB3779','#E71261','#E74F86','#FFD6E5','#BE9BA7','#D0396A','#DB1855','#F798B6','#9C294A','#D62B5B','#DE3969','#BC1641','#E7547A','#D52756','#9C7D85','#DB244F','#A1354F','#C22443','#FFBDCA','#8B6D73','#DC3D5B','#FF738C','#F13154','#BC4055','#FED4DB','#FFCFD6','#CB4E61','#ED455A','#F36C7B','#C94F5B','#F3959D','#A8444C','#FFCCD0','#735B5D','#D15D67','#B44B52','#FD868D','#FFD5D8','#C3767B','#FF8087','#C8242B','#FFEAEB','#F95A61','#E96D73','#E6656B','#FF6D73','#FF555B','#A35A5B','#FFD3D4','#B84B4D'];
        var body=document.getElementsByTagName('body')[0];

        function hexToRgb(hex) {
            hex = hex.substring(1, hex.length);
            var r = parseInt((hex).substring(0, 2), 16);
            var g = parseInt((hex).substring(2, 4), 16);
            var b = parseInt((hex).substring(4, 6), 16);

            return r + "," + g + "," + b;
        }

        function rgbToHex(r, g, b) {
            return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
        }

        var rgbArr=new Array();
        var div=document.createElement('div');
        div.id='Original';

        body.appendChild(div);
        for(var color in coloursArray){
            color=coloursArray[color];
            displayColor(color,div);
            rgbArr.push(hexToRgb(color));
        }

        var hslArr=new Array();
        for(var i=0;i<rgbArr.length;i++){
            //Transforming rgb to hsl
            //`hslArr[i][1]` (`i`) is a reference to the rgb color, in order to retrieve it later
            hslArr[i]=[rgbToHsl(rgbArr[i]),i];
        }
        var sortedHslArr=new Array();
        //Sorting `hslArr` into `sortedHslArr`
        outerloop:
            for(var i=0;i<hslArr.length;i++){
            for(var j=0;j<sortedHslArr.length;j++){
                if(sortedHslArr[j][0][0]>hslArr[i][0][0]){
                    sortedHslArr.splice(j,0,hslArr[i]);
                    continue outerloop;
                }
            }
            sortedHslArr.push(hslArr[i]);
        }
        var sortedRgbArr=new Array();
        //Retrieving rgb colors
        for(var i=0;i<sortedHslArr.length;i++){
            sortedRgbArr[i]=rgbArr[sortedHslArr[i][1]];
        }

        function displayColor(color,parent){
            var div;

            div=document.createElement('div');
            div.style.backgroundColor=color;
            div.style.width='22px';
            div.style.height='22px';
            div.style.cssFloat='left';
            div.style.position='relative';
            parent.appendChild(div);
        }

        var finalArray=new Array();
        var div=document.createElement('div');
        div.id='Sorted';
        body.appendChild(div);
        for(var color in sortedRgbArr){
            color=sortedRgbArr[color];
            color=color.split(',');
            color=rgbToHex(parseInt(color[0]),parseInt(color[1]),parseInt(color[2]));
            displayColor(color,div);
            finalArray.push(color);
        }

        function rgbToHsl(c){
            var r = c[0]/255, g = c[1]/255, b = c[2]/255;
            var max = Math.max(r, g, b), min = Math.min(r, g, b);
            var h, s, l = (max + min) / 2;

            if(max == min){
                h = s = 0; // achromatic
            }else{
                var d = max - min;
                s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
                switch(max){
                    case r: h = (g - b) / d + (g < b ? 6 : 0); break;
                    case g: h = (b - r) / d + 2; break;
                    case b: h = (r - g) / d + 4; break;
                }
                h /= 6;
            }
            return new Array(h * 360, s * 100, l * 100);
        }

        var sorted = coloursArray.sort(function(colorA, colorB) {
            return pusher.color(colorA).hue() - pusher.color(colorB).hue();
        });
      //  console.log(sorted);

        var div=document.createElement('div');
        div.id='Pusher';
        body.appendChild(div);
        for(var color in sorted){
            color=sorted[color];
            displayColor(color,div);
        }

        var div=document.createElement('div');
        body.appendChild(div);
        var str='';
        for(var color in sorted){
            color=sorted[color];
            str+='\''+color+'\',';
        }
        div.innerHTML=str;

        function sorthueColors (colors) {
            for (var c = 0; c < colors.length; c++) {
                /* Get the hex value without hash symbol. */
                var hex = colors[c].substring(1);
                //var hex = colors[c].hex.substring(1);

                /* Get the RGB values to calculate the Hue. */
                var r = parseInt(hex.substring(0,2),16)/255;
                var g = parseInt(hex.substring(2,4),16)/255;
                var b = parseInt(hex.substring(4,6),16)/255;

                /* Getting the Max and Min values for Chroma. */
                var max = Math.max.apply(Math, [r,g,b]);
                var min = Math.min.apply(Math, [r,g,b]);

                /* Variables for HSV value of hex color. */
                var chr = max-min;
                var hue = 0;
                var val = max;
                var sat = 0;

                if (val > 0) {
                    /* Calculate Saturation only if Value isn't 0. */
                    sat = chr/val;
                    if (sat > 0) {
                        if (r == max) {
                            hue = 60*(((g-min)-(b-min))/chr);
                            if (hue < 0) {hue += 360;}
                        } else if (g == max) {
                            hue = 120+60*(((b-min)-(r-min))/chr);
                        } else if (b == max) {
                            hue = 240+60*(((r-min)-(g-min))/chr);
                        }
                    }
                }

                /* Modifies existing objects by adding HSV values. */
                colors[c].hue = hue;
                colors[c].sat = sat;
                colors[c].val = val;
            }

            /* Sort by Hue. */
            return colors.sort(function(a,b){return a.hue - b.hue;});
        }

Comment: Does this answer your question? [Javascript - Sort rgb values](https://stackoverflow.com/questions/11923659/javascript-sort-rgb-values)
Answer: Problem is, sorting requires a well-defined order - in other words, you need to map all colors to one dimension. While there are some approaches to display the color space in two dimensions, I am not aware of any that would display it in one dimension and still make sense to the human eye.
However, if you don't insist on some universal ordering and you merely want to place a given list of colors in a way that looks nice, then some clustering approach might produce better results. I tried the naive approach, the idea here is to put similar colors into the same cluster and to merge these clusters until you only have one. Here is the code I've got:
function colorDistance(color1, color2) {
    // This is actually the square of the distance but
    // this doesn't matter for sorting.
    var result = 0;
    for (var i = 0; i < color1.length; i++)
        result += (color1[i] - color2[i]) * (color1[i] - color2[i]);
    return result;
}

function sortColors(colors) {
    // Calculate distance between each color
    var distances = [];
    for (var i = 0; i < colors.length; i++) {
        distances[i] = [];
        for (var j = 0; j < i; j++)
            distances.push([colors[i], colors[j], colorDistance(colors[i], colors[j])]);
    }
    distances.sort(function(a, b) {
        return a[2] - b[2];
    });

    // Put each color into separate cluster initially
    var colorToCluster = {};
    for (var i = 0; i < colors.length; i++)
        colorToCluster[colors[i]] = [colors[i]];

    // Merge clusters, starting with lowest distances
    var lastCluster;
    for (var i = 0; i < distances.length; i++) {
        var color1 = distances[i][0];
        var color2 = distances[i][1];
        var cluster1 = colorToCluster[color1];
        var cluster2 = colorToCluster[color2];
        if (!cluster1 || !cluster2 || cluster1 == cluster2)
            continue;

        // Make sure color1 is at the end of its cluster and
        // color2 at the beginning.
        if (color1 != cluster1[cluster1.length - 1])
            cluster1.reverse();
        if (color2 != cluster2[0])
            cluster2.reverse();

        // Merge cluster2 into cluster1
        cluster1.push.apply(cluster1, cluster2);
        delete colorToCluster[color1];
        delete colorToCluster[color2];
        colorToCluster[cluster1[0]] = cluster1;
        colorToCluster[cluster1[cluster1.length - 1]] = cluster1;
        lastCluster = cluster1;
    }

    // By now all colors should be in one cluster
    return lastCluster;
}

Complete fiddle
The colorDistance() function works with RGB colors that are expressed as arrays with three elements. There are certainly much better approaches to do this and they might produce results that look better. Note also that this isn't the most efficient algorithm because it calculates the distance between each and every color (O(n2)) so if you have lots of colors you might want to optimize it.
Comment: @Kayote: Feel free to implement a `colorDistance()` function that matches your criteria better - you didn't specify these criteria in the question, and my mind reading skills are a bit rusty. The rest of the algorithm shouldn't need any changes, no matter what comparison function you choose.
Comment: thank you for the effort, however, while it certainly seems to align the colour values based on lightness, the hues are all over the place & would cause a lot of grief. I think HSL or HSV is the way to go with these if I can figure out a link between when to jump from one hue to another...
Comment: Wanted to mention +1 for demo.
Answer: Assuming there is no best solution. Here is how i would find what i like the most.
First i would use color distance function:
var balance = [10, 0, 0.01];
function colorDistance(color1, color2) {
    var result = 0;
    color1 = rgbToHsl(color1[0], color1[1], color1[2]);
    color2 = rgbToHsl(color2[0], color2[1], color2[2]);
    for (var i = 0; i < color1.length; i++)
        result += (color1[i] - color2[i]) * (color1[i] - color2[i]) * balance[i];
    return result;
}

I would use balance function :

hue distance var balance = [1, 0, 0];

saturation distance var balance = [0, 1, 0];

lightness distance var balance = [0, 0, 1];

After that you can just experiment and pick what you like. Note that values are not normalized to appear between 0 to 1 but other then that it should work well. You can also you another distance function this is just most trivial and well known one that gets decent results.
This approach does not give you the best solution, but describes a way to fiddle with it and get result you where looking for. Have fun.
Answer: Using the Hue of a color is usually a pleasant way to map the color on a one-dimensional scale. You could convert the rgb color to hsv using the answers here: RGB to HSV color in javascript?
Comment: The code in the question appears to be a very convoluted way of doing exactly what you are proposing. The result looks wrong however, my guess would be that the custom sorting algorithm implemented there is buggy.
Answer: This is an improved code based on pusher.color lib.

https://jsfiddle.net/kt1zv6g4/3/

Sort Colour / Color Values


Calcular porcentagens diversas

Question: Eu tenho um sistema de vendas, onde cada usuário tem uma porcentagem X sobre sua venda, no momento tenho 15 usuários, sendo 10 deles com 5% de lucro sobre cada venda, e os outros 5 tem 8% de lucro sobre cada venda.
Tenho então minha página de administração onde vejo quanto terei que pagar a todos eles se somar todas as porcentagens de ganho, o problema é que tenho que fazer o calculo venda por venda, ou seja, se o usuário tem 5% de lucro e o outro tem 8%, tenho que no script fazer o seguinte:
$Valor += $ValorDoProduto * ($GanhoDoUsuario / 100);

Assim, se eu somar todos os usuários, terei um valor X que representa o valor que pagarei se somar todos os ganhos de cada usuário.
Mas gostaria de fazer isso mais simples, de forma que eu calculasse todas as vendas, me retornando então um valor X, esse valor X é as vendas sem tirar o ganho do vendedor, então, quando eu tiver o valor X que é a soma de todos os produtos vendidos eu iria somar todas as porcentagens, e então subtrair e ter o mesmo valor que teria usando o cálculo lá em cima.
Explicando melhor:
Tenho uma tabela de usuários no MySQL, cada usuário tem um ganho em porcentagem pra cada venda, existe a possibilidade de 'somar' todas as porcentagens e então fazer um cálculo único pra determinar o quanto terei que pagar de comissão à todos eles?
Answer: Não é possível. Se somares os valores de todos os produtos deixas de saber que parte dos lucros totais corresponde aos ganhos de 5% ou aos de 8%.
No máximo podes somar todas as vendas dos utilizadores de cada subgrupo (sendo um subgrupo o conjunto de utilizadores que recebem 5% ou 8% de uma venda). No final aplicas a percentagem à soma de cada um dos subgrupos para saberes quanto tens que pagar a cada vendedor. Assim só tens que fazer um calculo por subgrupo (no teu caso 2) em vez de um por cada utilizador.
Comment: Entendi, eu pensei que teria como... obrigado!

Calcular porcentagens diversas


Laravel HREF techniques for multiple pages

Question:

             Home

                     View Photo

                     About

                      Contact

             Upload

                     Upload Photo

                     View Photo List

I am still new to this but basically, I have 5 pages (View Photo, About, Contact, Upload Photo, View Photo List). Currently, I want to show this HTML in all 5 pages so that it will be standardized. If I change the "picture/about" in one page to "photograph/about" for example, the 5 pages will need to reflect this change. This gets more complex as the number of pages increases.
Is there any way or technique in Laravel to solve this issue? Thanks.
Answer: Create a template file like Laravel provides app.blade.php in resources/template/ directory, use extends, sections etc for designing, please read Laravel Documentation for Templates
Comment: I read the part about templates and have used it in my project. However, using template would be impossible for me because each HEADER MENU is different. Each of them would have the different  depending on what page I am on
Comment: I guess you'll have problem in showing menu `active` right
Answer: You can use partials for this.
If your issue is adding the active class to the correct link then you can just use request()->is() to check the current url:

is('picture/externalphotoviewer') ? 'active' : '' }}">

         View Photo

is('picture/about') ? 'active' : '' }}">

         About

is('picture/contact') ? 'active' : '' }}">

         Contact

Simply create a blade file inside your resources/views directory and then use @include e.g.
reasources/views/partials/header.blade.php:

                 Home

is('picture/externalphotoviewer') ? 'active' : '' }}">

                         View Photo

is('picture/about') ? 'active' : '' }}">

                         About

is('picture/contact') ? 'active' : '' }}">

                         Contact

                 Upload

                         Upload Photo

                         View Photo List

Then replace the header code in your other blade files with:
@include('partials.header')

NB
If you change the location of the header you will need to update the @include's as well e.g.
e.g. resources/views/layouts/main/header.blade.php would be @include('layouts.main.header')
Hope this helps!


What constitutes "readable" (kqueue/epoll)

Question: I know that if the remote host gracefully shuts down a connection, epoll will report EPOLLIN, and calling read or recv will not block, and will return 0 bytes (i.e. end of stream).
However, if the connection is not closed gracefully, and a write or send operation fails, does this cause epoll to subsequently return EPOLLIN for that socket, producing the same/similar end of stream scenario?
I've tried to find documentation on this behaviour, but have not succeeded, and while I could test it, I'm not interested in what happens on a specific distribution with a specific kernel version.
Answer: It is indeed not entirely obvious from the specification, but it works as follows for poll():

If there is data available to be read, even if the connection is closed, POLLIN is returned.
If neither reading or writing is possible because of a closed connection, POLLHUP or POLLERR is returned.
If reading is no longer possible but writing is (such as if the other side did shutdown(SHUT_WR)), POLLIN is returned and POLLHUP and POLLERR are not returned. (This allows waiting for POLLOUT normally.)

The simple thing to do is to try a read when any of POLLIN, POLLHUP and POLLERR are set.
In kqueue(), there is just an EVFILT_READ filter that may be triggered. This is described in the man page and should be clear enough.
Note that if you don't enable TCP keepalives (FreeBSD enables them by default but most other operating systems do not), waiting for data to read may get stuck forever if the network breaks in certain ways. Even if TCP keepalives are on, it tends to take a few hours to detect a broken connection.
Comment: @EJP TCP can distinguish between the other side doing `SHUT_WR` while still reading data and an error such as a timeout or a packet with RST set that prevents both reading and writing. However, `close` appears to the other side the same way as `SHUT_WR` until an attempt is made to send data, and `SHUT_RD` only affects the other side when it attempts to send data.
Comment: Point 2 is inconsistent with point 3. TCP can't distinguish between an incoming close and an incoming SHUT_WR. Both are a FIN, and both must therefore produce EPOLLIN.
Answer: It may not return EPOLLIN when the peer machine is closed unexpectly. In the past, I encounted this kind of phenomenon by VirtualBox as following steps:

Launch server on one VM.
Launch client on the other VM, connect the server and keep the connection without doing anything.
Save client VM state (something like hibernate).

And I saw the connection was still established in Server VM by
netstat -anp --tcp

In other words, EPOLLIN was not triggered in server.
http://tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/ says that it will keep about 7200 seconds by default.
Of course, you can change keep alive timeout value by setsockopt or kernel parameters.
But some books says the better solution is to detect it in application layer, e.g. design the protocol that make sure sending some dummy messages periodically to detect the connection state.
Comment: My question was more specific though. If the connection is closed as in your example, and then a write fails, will EPOLLIN be returned now that it is known that the pipe is broken.
Answer:
However, if the connection is not closed gracefully, and a write or send operation fails, does this cause epoll to subsequently return EPOLLIN for that socket, producing the same/similar end of stream scenario?

No. That would imply receipt of a FIN, which means normal termination of the connection, which didn't happen. I would expect you would get an EPOLLERR or maybe EPOLLHUP.
But I'm curious why you wouldn't have already closed the socket on getting the write error, and why you would still be polling it. That's not correct behaviour.
Comment: I want to make sure all the data available on the socket is read.  If I kill the socket when an error occurs writing, is it not possible that while the connection has terminated, there is still data to read?
Answer: epoll() is basically poll() but it scales better when you increase number of fds. I am not sure what it does when you are using it as edge-triggered interface. But for level triggered - yes, it will always return EPOLLIN, provided you are listening to this event, if end of stream is detected.
Though you must know TCP is not perfect. If connection is terminated abnormally (physycal link is down) by the other side, your side may never detect this until you write to the socket. TCP_KEEPALIVE may help, but not much.
Comment: He's not asking about end of stream. He's asking about an ungracefully closed connection.
Comment: @EJP, tell me please what is the differene? There is no such thing as "connection closed gacefully". Higher level protocols determine such things.


Extracting file names from WebRequestMethods.Ftp.ListDirectoryDetails

Question: I have an application that does the following: directory listing, download file, download all.
I have an issue with getting the file names from WebRequestMethods.Ftp.ListDirectoryDetails. It seems that it is not possible to do so for every scenario.
WebRequestMethods.Ftp.ListDirectoryDetails returns a lineItem in the following manner:
"-rw-r--r-- 1 ftp ftp          39979 Aug 01 16:02 db to pc 2014-08-05 07-30-00.csv"
I am using the first character to determine if it is a file or directory. Then I split the file on space and get the file name after a fixed index amount within the split. The issue in my implementation is that if a file has multiple spaces then it would be incorrectly referenced with less spaces and the file would not be found when attempting to download it.
I am not able use the split.last() since a file name can have spaces nor WebRequestMethods.Ftp.ListDirectory since it does not allow us to distinguish between a Directory and a file without an extension. Nor a regex since a file name can have a date in it. Any assistance in finding a solution that completely covers all cases would be great.
bool isDirectory = line.Substring(0,1).Equals("d", System.StringComparison.OrdinalIgnoreCase);

string[] itemNames = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).Select((value, index) => new { value, index }).Where(i => i.index > 7).Select(i => i.value).ToArray();
string val = string.Join(" ", itemNames);

Answer: The final solution was to use regex and split it using groups. This resolved all issues and allowed me to get the file / directory name and whether it was a directory or file.
string regex =
@"^" +                          //# Start of line
@"(?<dir>[\-ld])" +             //# File size
@"(?<permission>[\-rwx]{9})" +  //# Whitespace          \n
@"\s+" +                        //# Whitespace          \n
@"(?<filecode>\d+)" +
@"\s+" +                        //# Whitespace          \n
@"(?<owner>\w+)" +
@"\s+" +                        //# Whitespace          \n
@"(?<group>\w+)" +
@"\s+" +                        //# Whitespace          \n
@"(?<size>\d+)" +
@"\s+" +                        //# Whitespace          \n
@"(?<month>\w{3})" +            //# Month (3 letters)   \n
@"\s+" +                        //# Whitespace          \n
@"(?<day>\d{1,2})" +            //# Day (1 or 2 digits) \n
@"\s+" +                        //# Whitespace          \n
@"(?<timeyear>[\d:]{4,5})" +    //# Time or year        \n
@"\s+" +                        //# Whitespace          \n
@"(?<filename>(.*))" +          //# Filename            \n
@"$";                           //# End of line

var split = new Regex(regex).Match(line);
string dir = split.Groups["dir"].ToString();
string filename = split.Groups["filename"].ToString();
bool isDirectory = !string.IsNullOrWhiteSpace(dir) && dir.Equals("d", StringComparison.OrdinalIgnoreCase);

Thanks to: http://blogs.msdn.com/b/adarshk/archive/2004/09/15/sample-code-for-parsing-ftpwebrequest-response-for-listdirectorydetails.aspx for providing the regex.
Comment: I can't get permission details by using WebRequestMethods.Ftp.ListDirectoryDetails. Do I need to provide permission to get permission details of file/folder. Kindly assist.
Answer: Simpler solution could be:
isDirectory=line[0]=='d';
filename = line.Split(new char[] {' '}, 9,StringSplitOptions.RemoveEmptyEntries)[8]

Comment: this is awesome. Short and simple. Thank you for that.
Answer: Based on  Sasa's suggestion, the following code can be used to get a list of files.
List<string> availableFiles = new List<string>();
string line = string.Empty;

ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
using (FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse())
{
    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
    {
        line = streamReader.ReadLine();
        while (!string.IsNullOrEmpty(line))
        {
            if (line[0] != 'd')
            {
                availableFiles.Add(line.Split(new char[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries)[8]);
            }

            line = streamReader.ReadLine();
        }
    }
}


SSH tunnel doesn't work

Question: I am trying to use my server as a "proxy" with ssh.
However, setting up tunneling with
ssh -D localhost:8000 user@myserver

does not work.
I tested this on various machines with ssh and putty - It connects just fine, but when I set my browser settings accordingly, I just get an error "Connection has been reset".
I tried monitoring the traffic with wireshark, but I didn't even see some tunnel-traffic.
I explicitly set AllowTcpForwarding to "yes" but I still can't use the tunnel.
When running ssh in verbose mode, I don't get any errors but
debug1: Connection to port 8000 forwarding to socks port 0 requested.
debug1: channel 3: new [dynamic-tcpip]
debug1: channel 3: free: dynamic-tcpip, nchannels 4

What am I doing wrong?
Comment: I suspect that you config your browser at `HTTP Proxy` line instead of `SOCKS host`.
Comment: I always made sure to set the SOCKS option, no matter which browser I was using.
Comment: Can you do tcpdump at the server (root/sudo access)? Pick an IP address reveal page, get it's IP address, watch that IP for traffic at the server, and try to visit it from your browser. FYI, the OP's ssh command works for me, from my desktop to my remote server.
Comment: Hi! I already tried capturing with wireshark and it turns out that data never arrives at the server. When loading pages with the proxy activated, just no packets are sent at all. How could that be?
Answer: It's an old thread, but I think it's worth posting possible solution.
I had the same issue with several browsers and it turned out to be configuration issue. If that's the case, the following command should work and show IP address of remote machine:
links -socks-proxy localhost:8000 www.myipaddress.com

First comment on the question is correct in that browser might try to use HTTP proxy instead of SOCKS proxy, in which case I see exactly the same output from ssh as in the OP's post. And although I did set SOCKS proxy the problem was that I didn't unset HTTP proxy. Once that was done, everything started working as expected.
Comment: I had the same problem. No matter what I did, the accepted answer didn't work. Your additional answer solved everything for me. Listen up everyone! Don't forget to UNSET the other proxy settings, as well as the "No Proxy for..." addresses like this: http://imgur.com/h4Q28ek
Comment: This took me days to figure out! UNSET other proxies, only SOCKS
Comment: how to _unset_ the other proxies for firefox?
Comment: @javadba, clear proxy address field. I think port number can stay, but you can clear it too.
Answer: The correct command is:
ssh user@server -D 8000

In the case you still can't connect check if another process is using this port. The most easy way to do this is with:
netstat --listen |grep 8000

If that is the case just use another port.
Comment: this doesn't work either. "Connection Reset", same thing
Comment: will update my answer
Comment: I made sure to use unused ports. I tried different ones, still the same. I am also running the ssh client as root, so permissions are not a problem, either.
Comment: you shouldn't be running it as root for other reasons, but I'm really baffled a bit. Do you have any iptables running on your server?
Comment: No iptables whatsoever, neither on the server nor client-side
Comment: What OS are you using?
Comment: It's really strange. I am using Debian Linux, but I also tested this with Putty on Windows. The server is running Linux.
Comment: what type of linux is the server running?
Comment: Debian Linux, 2.6.32-4-pve (most likely in an openvz container)
Comment: I do what the OP does (on Slackware and Ubuntu) using non-root users on both ends, and it works for me. I just tried it and verified it works. I used the OPs ssh command with only my remote user@host substituted. Then I started a new browser instance and configured it to use SOCKS and connect to 127.0.0.1 port 8000 in proxy settings. Then I went to an IP address reveal page and it showed my server address. I would focus on the OPs server side (maybe configured to disallow this) or browser client (maybe not connecting right).
Answer: I had the same issue, my fix is to add -g option to the command:
$ ssh -D 12324 localhost:8000 user@myserver -g

Answer: Also enabling in sshd_config on the ssh server:
PermitTunnel yes

And of course:
service ssh restart

SSH tunnel doesn't work


python3 doesn't execute getbootstrap html code

Question: If i run the following code (extract from carousel template in getbootstrap) from a browser it is executed successfully:
<!DOCTYPE html>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="generator" content="Jekyll v3.8.5">
    <title>Carousel Template · Bootstrap</title>

    <link rel="canonical" href="https://xxxxx/">

    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/jumbotron.css" >
    <link rel="stylesheet" type="text/css" href="css/carousel.css" rel="stylesheet">
    <link href="./Carousel Template · Bootstrap_files/bootstrap.min.css" rel="stylesheet"  crossorigin="anonymous">

    XXXXXXX
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">

    </button>

          Home (current)

          Main menu

            Sign In
            Register
            About

          Sign In

          Register

          Disabled

      <form class="form-inline mt-2 mt-md-0">
        <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </form>

As i replace all href with {{ url_for('static',filename='javascript name or css name')}}
and invoke the page with render_template('testnewhome.html')
I don't get any error in wsgi but look and feel is really different.
Just the let you understand:
html code executed from browser looks this way
good looking
If i run it from python it looks this way
bad looking
Any clue? I m really stuck
Comment: Have you examined the HTML source of each page? It looks like the path to the CSS is incorrect. The best way to determine that is to look at the HTML source itself.
Comment: there is only one page, i checked it out carefully. why do you say path to css is incorrect?
Answer: it's sorted. There was no problem with the code, you can use it as a reference.
I simply removed all css files from ccs folder and copied them over. It looks like the getbootstrap ditribution files are not good. So i suggest to everyone to download a working page and use the downloaded css and js to create a new page.
HIH


Are primes $p$ such that $2^p-1$ is prime for seemingly random?

Question: Are the prime exponents $p$ such that $2^p-1$ is prime for seemingly random?
So suppose you were choosing two 300-digit primes $p, p_2$ for encryption purposes. If these $p, p_2$ were chosen from, say a list of primes $p$ such that $2^p-1$ is prime for, would these be cryptography secure? (I know finding primes these large and also $2^p-1$ is prime would be basically impossible, but assume this is the case that only you know the list.) Thanks for explanations and help.
Comment: At the risk of sounding snarky, I think if there were a pattern to such primes, someone would have surely noticed it by now...
Comment: I don't understand the question. If your rsa key is $2^{30}$ bits long then the attacker will doubt you used Mersenne primes, as the Miller Rabin pseudo-prime generation needs something like $\mathcal{O}(2^{120})$ operations at that range.
Comment: Regardless of whether $2^p-1$ is sufficiently random, you don't want your prime numbers for cryptography to come from any, at least to an outsider, discernible family of choices.
Comment: The list of such $p$ starts 2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 86243, 110503, 132049, 216091, 756839, 859433, 1257787, 1398269, 2976221, 3021377, 6972593, 13466917, 20996011, 24036583, 25964951, 30402457, 32582657, 37156667. Seems fairly random to me.
Comment: To date it is not known whether or not there are infinitely many Mersenne primes.
Answer: The word "seemingly" makes this question very difficult. Or maybe very easy. Those exponents seem random to me, so yeah, okay, they are seemingly random.
Just to be perfectly clear, though, they are not actually random. Last year, two mathematicians from Stanford found that among the first billion primes, a prime with 1 as its least significant digit is much more likely to be followed with a prime ending in 3, 7 or 9 than by another one ending in 1. I read about this in Scientific American, there's also an ArXiV preprint.
This also holds for the known Mersenne prime exponents. 31 is followed by 61 but 61 is followed by 89, 521 is followed by 607, 2281 is followed by 3217, 9941 is followed by 11213, 21701 is followed by 23209, 216091 is followed by 756839, 2976221 is followed by 3021377, 20996011 is followed by 24036583 and 25964951 is followed by 30402457.
Currently, the three highest known exponents end in 7. And even though there is also 107 followed by 127, there is still that general aversion to repeating the final digit.
In short: they are not actually random but they sure seem to be.
Comment: +1 for the interesting fact about successive primes' unit digits
Comment: Here's an MSE post on [_"Mathematicians shocked to find pattern in 'random' prime numbers"_](https://math.stackexchange.com/questions/1698116/mathematicians-shocked-to-find-pattern-in-prime-numbers)


Problema ao atualizar pip no CMD

Question:
estou tentando utilizar Robot Framework e um dos passos é fazer funcionar esse pip no cmd....
Comment: A resposta está equivocada, Veja a tradução do texto acima: *DEPRECIADO: Python 2.7 alcançou o fim de sua vida em 1 Janeiro de 2020. Por favor atualiza o seu Python pois o Python 2.7 está descontinuado. pip 2.0 irá remover o suporte para Python 2.7 em Janeiro 2021. Para mais detalhes sobre o suporte Python 2 no pip podem serem encontrados em https://pip.pypa/............(url no final da frase)*
Answer: Seu pip está na última versão, ou seja, já foi atualizado. O comando python -m pip install --upgrade pip não surte efeito quando seu pip já está na última versão.

Problema ao atualizar pip no CMD


Can I make an array out of a range of countif functions?

Question: A truncated version of my data is in the form shown in the screenshot below: three columns of 5 unique names. The names appear in any order and in any position but never repeat in a single row.
My goal is to create an array that contains the number of times Adam appears in each row. I can fill down the formula<code>=countif(A2:C2,$I$2)</code> in a new column, or if I write the array manually for each row, it looks like:
<code>={countif(A2:C2,$I$2);countif(A3:C3,$I$2);countif(A4:C4,$I$2);countif(A5:C5,$I$2);countif(A6:C6,$I$2)}
</code>
Where cell I2 contains "Adam". Of course, this is not feasible for large data sets.
I know that arrays are effectively cells turned into ranges, but my main issue is that the cell I'm trying to transform already references a range, and I don't know how to tell the software to apply the countif down each row (i.e. I intuitively would like to do something like countif((A2:C2):(A99:C99),"Adam") but understand that's not how spreadsheets work).
My goal is ultimately to perform some operations on the corresponding array but I think I'm comfortable enough with that once I can get the array formula I'm looking for.
Answer: try:
<code>=ARRAYFORMULA(IF(A2:A="",,MMULT(IF(A2:C="Adam", 1, 0), {1;1;1})))
</code>
Comment: Thanks, this does exactly what I was looking for! If you can, could you walk me through what the formula is doing? I don't quite understand the IF statement and what matrices are being multiplied (and why). It looks like you found a creative solution however and I appreciate it.
Comment: sure. IF A2:C equals Adam it will replace it with 1. if it does not equal Adam then it will be 0. this is then passed into MMULT which sums each column (represented as {1;1;1}) of those previously computed zeros and ones. the outer IF is just cosmetic to get rid of zeros till end of the column.


What is the reasoning behind max entropy constraints for the gamma distribution?

Question: The max entropy method is a way of deriving a probability distribution given only the information you know about how the data is distributed and nothing more. For example the normal distribution can be derived by constraining the mean and variance to be fixed.  This is a very intuitive and principled way of choosing how to model your data.
I'm trying to wrap my head around the constraints for the gamma distribution, which does not seem as intuitive. These constraints are
$$\mathbf E[x] = k\theta,$$
$$\mathbf E[\log(x)] = \psi(k) + \log(\theta),$$
where $k$ is the shape parameter, $\theta$ is the scale parameter, and $\psi$ is the digamma function. There is also the implicit constraint that $x \ge 0$.
One possibility I see here is that perhaps it's less about the specifics of what values these constraints take, and more about the fact that they are fixed -- the values are circumstantial. But then is there any intuition behind these values?  If the specific values do matter, under what circumstances would you assume the mean to be a product of two parameters? Given the scale parameter is included, are we implicitly making a constraint on the variance?
Also, I've noticed that the second term looks like one of the constraints for the max entropy that generates the beta distribution. What is the connection there?
Any insight here would be helpful. I've scoured the web and have found little to help me out here.
Answer: This touches on some interesting ideas in statistics.
Firstly, you are correct, the way that we choose to parameterize the family of distributions doesn't really matter. Given an invertible mapping from one set of parameters to another, $\eta = F(\theta)$, any distribution $p_\theta(x|\theta)$ can also be expressed as
$$ p_\eta(x | \eta) = p_\theta(x| F^{-1}( \eta) ).$$
We can remap the parameters.
For example, for a gaussian, rather that mean $\mu$ and variance $\sigma^2$, we could write $\eta_1 =\frac{\mu}{\sigma^2}$, $\eta_2 = -\frac{1}{2\sigma^2}$, so the gaussian pdf becomes:
$$ p(x | \eta_1, \eta_2) \propto \exp( \eta_2 x^2 + \eta_1 x) $$
Even though the parameters are arbitrary, there are two pretty natural ways to write them from the perspective of the maximum entropy problem. Writing the max entropy problem:
$$ \max_{p(x)} \int -p(x) \log p(x) dx \quad \textrm{such that} \quad \forall i \int p(x) T_i(x) dx = \mu_i$$
We get a solution:
$$ p(x) \propto \exp \left( \sum_i \lambda_i T_i(x)\right)$$
Where $T_i(x)$ are the 'sufficient statistics' functions that we are constraining (eg. $T_1(x) = x$ for mean $T_2(x) = x^2$ for variance), $\mu_i$ are the values of these sufficient statistics, and $\lambda_i$ are the Lagrange multipliers for these constraints.
The $\mu_i$ are the 'moment parameters': they tell us about the values of our constraints. The $\lambda_i$ are the 'natural parameters': they are nice to calculate with (and, as Lagrange multipliers, give us 'prices' of constraint violation). For a gaussian, $\mu$ and $\sigma^2$ are moment parameters, while the $\eta_1$ and $\eta_2$ derived above are the natural parameters. The relationship between moment and natural parameters is the constraint value $\leftrightarrow$ Lagrange multiplier relationship, and is encoded in the partition function. See exponential families for more information and information geometry for a theory built on these types of observations.
For the particular case of the gamma distribution, the relationship of parameters $k$ and $\theta$ to the natural parameters is more intuitive than the relationship to mean parameters $k= \eta_1 +1$, $\theta = -\frac{1}{\eta_2}$. The beta distribution is usually written in terms of natural parameters as well and also imposes a constraint on $\mathbb{E}[\log x]$.


Java programming to interfaces shall I use it all the time, especially for collections

Question: As suggested almost everywhere shall I be using interface all the time, especially when working with collections.
// Using interfaces
List<Integer> list = new LinkedList<Integer>();
((LinkedList) list).offerFirst(num);

//Using concrete class
LinkedList list = new LinkedList<Integer>();
list.offerFirst(num);

In first approach compiler gives warning and even syntax seems cumbersome.
warning: [unchecked] unchecked call to offerFirst(E) as a member of the raw type LinkedList

Answer: If you are using the offerFirst method, perhaps you should be programming to the Deque interface (which represents double ended queue) instead of the List interface:
Deque<Integer> deque = new LinkedList<Integer>();
deque.offerFirst(num);

Comment: @AkshayNaik If you are using a method specific to an implementation, and there is no suitable interface containing that method, you should use the implementation type (i.e. the class). It's pointless to program to interface if you are always going to cast it back to the implementation.
Comment: Okay for this case I can use Deque, in case I want to use method which is specific to the implementation then which approach I should take, first or second?
Answer: This normally is best:
List<Integer> list = new LinkedList<>();
list.add(42);

Deque<Integer> list = new LinkedList<>();
list.offerFirst(num);

While sometimes there is no choice.
LinkedList<Integer> list = new LinkedList<>();
list.add(42);
list.offerFirst(num);

The interface approach gives a more general algorithm with freedom of implementation,
reimplementation in the future, a greater applicability.
However that only goes as far generalisation goes.


How to make universal library /9 .a file) in iOS using xcode?

Question: How to make universal Static Library ( .a file) in iOS using xCode? iOS portfolios
Comment: http://stackoverflow.com/questions/3520977/build-fat-static-library-device-simulator-using-xcode-and-sdk-4 and http://stackoverflow.com/questions/12402092/file-is-universal-3-slices-but-does-not-contain-an-armv7s-slice-error-for-st
Answer: Follow this screenshot:

You can create a static library by creating a new Project > Cocoa Touch Static Library.
Let me know it works or not.
Thanks,
Hemang.