问:
Unit Testing a custom Web API AuthorizeAttribute
I am trying to unit test, with NUnit in C#, a custom Authorize Attribute. In particular that a particular http status code and message have been returned in the case of not being authorized.

My attribute is super simple - looks like this:

```<code>public class AuthorizationAttribute : AuthorizeAttribute
{

    public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        if (IsAuthorized(actionContext))
            return;

        HandleUnauthorizedRequest(actionContext);
    }

    protected override void HandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Forbidden, "You are not authorized to access this resource");
    }
}
</code>```

So to test this (and currently new in the field of testing) I have cobbled together the following code. In it, i am attaching a generic identity with a username and some role data.

```<code>public void Given_UserIsNotInAuthorizedRoles_When_Auhtorizing_ReturnsForbidden()
{

    // Arrange

    IPrincipal principal = new GenericPrincipal(new GenericIdentity("TestName"), new[] { "TestRole" });

    HttpActionContext mockActionContext = new HttpActionContext()
    {
        ControllerContext = new HttpControllerContext()
        {
            Request = new HttpRequestMessage(),
            RequestContext = new HttpRequestContext() { Principal = principal }
        },
        ActionArguments = { { "SomeArgument", "null" } }
    };

    mockActionContext.ControllerContext.Configuration = new HttpConfiguration();
    mockActionContext.ControllerContext.Configuration.Formatters.Add(new JsonMediaTypeFormatter());

    // Act
    AuthorizationAttribute authAttr = new AuthorizationAttribute();
    authAttr.OnAuthorization(mockActionContext);

    // Assert
    Assert.IsTrue(mockActionContext.Response.StatusCode == System.Net.HttpStatusCode.Forbidden);

}
</code>```

The mock controller looks like this:

```<code>[Authorization(Roles = "AdminRoleOnly", Users = "A user")]
internal class MockController : ApiController { }
</code>```

When I'm debugging, i am finding that if (IsAuthorized(actionContext)) is returning true - which i don't understand.

Can someone point me in the direction of why my authorize attribute is passing this identity as good?

For what it's worth, this code works in production. I'm back-filling some tests.

Somewhat related, are these ones

I've read through the question and answer on a similar question ASP.NET MVC unit testing custom AuthorizeAttribute though neither really either work nor relate, especially given they're for MVC controllers and not Api Controllers.

I've also read and tried to implement the code in the answer on this question too Mocking HttpActionContext.ActionArguments when testing Web.Api ActionFilter

The latter gets me somewhere near in that my authorization attribute is firing in a test and I am able to pass in the required context information.

答:
You need to assign a Role to the AuthorizationAttribute. In your example you have a TestRole assign to the principal. Assign any other role but that.

```<code>// Act
AuthorizationAttribute authAttr = new AuthorizationAttribute() {
    Roles = "SomeOtherRole"
};
</code>```

and the test should behave as expected when exercised.

If you review the source code for IAuthorized You will see logic that checks if there are roles assigned to the attribute. If there are none to compare it will default to true.

Body:
I am trying to unit test, with NUnit in C#, a custom Authorize Attribute. In particular that a particular http status code and message have been returned in the case of not being authorized.

My attribute is super simple - looks like this:

public class AuthorizationAttribute : AuthorizeAttribute
{

    public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        if (IsAuthorized(actionContext))
            return;

        HandleUnauthorizedRequest(actionContext);
    }

    protected override void HandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Forbidden, "You are not authorized to access this resource");
    }
}

So to test this (and currently new in the field of testing) I have cobbled together the following code. In it, i am attaching a generic identity with a username and some role data.

public void Given_UserIsNotInAuthorizedRoles_When_Auhtorizing_ReturnsForbidden()
{

    // Arrange

    IPrincipal principal = new GenericPrincipal(new GenericIdentity("TestName"), new[] { "TestRole" });

    HttpActionContext mockActionContext = new HttpActionContext()
    {
        ControllerContext = new HttpControllerContext()
        {
            Request = new HttpRequestMessage(),
            RequestContext = new HttpRequestContext() { Principal = principal }
        },
        ActionArguments = { { "SomeArgument", "null" } }
    };

    mockActionContext.ControllerContext.Configuration = new HttpConfiguration();
    mockActionContext.ControllerContext.Configuration.Formatters.Add(new JsonMediaTypeFormatter());

    // Act
    AuthorizationAttribute authAttr = new AuthorizationAttribute();
    authAttr.OnAuthorization(mockActionContext);

    // Assert
    Assert.IsTrue(mockActionContext.Response.StatusCode == System.Net.HttpStatusCode.Forbidden);

}

The mock controller looks like this:

[Authorization(Roles = "AdminRoleOnly", Users = "A user")]
internal class MockController : ApiController { }

When I'm debugging, i am finding that if (IsAuthorized(actionContext)) is returning true - which i don't understand.

Can someone point me in the direction of why my authorize attribute is passing this identity as good?

For what it's worth, this code works in production. I'm back-filling some tests.

Somewhat related, are these ones

I've read through the question and answer on a similar question ASP.NET MVC unit testing custom AuthorizeAttribute though neither really either work nor relate, especially given they're for MVC controllers and not Api Controllers.

I've also read and tried to implement the code in the answer on this question too Mocking HttpActionContext.ActionArguments when testing Web.Api ActionFilter

The latter gets me somewhere near in that my authorization attribute is firing in a test and I am able to pass in the required context information.

Body:
You need to assign a Role to the AuthorizationAttribute. In your example you have a TestRole assign to the principal. Assign any other role but that.

// Act
AuthorizationAttribute authAttr = new AuthorizationAttribute() {
    Roles = "SomeOtherRole"
};

and the test should behave as expected when exercised.

If you review the source code for IAuthorized You will see logic that checks if there are roles assigned to the attribute. If there are none to compare it will default to true.


来源: stackexchange

问:
HTML 5 Geolocalization doesn't work in Safari
I have this basic function for indentify the position of the user:

```<code>function getLocation()
{

if (navigator.geolocation)
{

navigator.geolocation.getCurrentPosition(showmyPosition, errorCallback);
}
else
{
alert("Geolocation is not supported by this browser.");
}
}

function showmyPosition(position)
{
document.location.href="/search?lat="+  position.coords.latitude +"&lng="+ position.coords.longitude +"";
}

function errorCallback(error) {
if (error.code === 1) {
alert("User denied geolocation");
}
}
</code>```

Works fine on Firefox, Chrome and Explorer but doesn't work on Safary (neither on iPhone). Why?

答: In case geolocation is turned off in configuration, it won't work. In addition it requires a secure (HTTPS) protocol. You could also have a look at JS-lib: https://code.google.com/archive/p/geo-location-javascript/</p>

ContentLicense: CC BY-SA 3.0

Body:
I have this basic function for indentify the position of the user:

function getLocation()
{

if (navigator.geolocation)
{

navigator.geolocation.getCurrentPosition(showmyPosition, errorCallback);
}
else
{
alert("Geolocation is not supported by this browser.");
}
}

function showmyPosition(position)
{
document.location.href="/search?lat="+  position.coords.latitude +"&lng="+ position.coords.longitude +"";
}

function errorCallback(error) {
if (error.code === 1) {
alert("User denied geolocation");
}
}

Works fine on Firefox, Chrome and Explorer but doesn't work on Safary (neither on iPhone). Why?


问:
Java How to constantly repaint
I'm trying to make the game Snake, although I'm currently having trouble constantly repainting the snake. I'm using a timer, and when I set the interval to 150 milliseconds, it works, although when I set it to something faster like 30 milliseconds, nothing shows on the screen. The timer w/ repaint() call are in the Game class. Thanks!

Game Class

```<code>import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;

public class Game extends JFrame{

    JPanel mainPanel = new JPanel();

    BorderLayout layout = new BorderLayout();
    //Game game;
    Snake snake = new Snake(this);

    public Game(){

        super("Snake by Alex 2017");
        setSize((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()),(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        mainPanel.setLayout(null);
        mainPanel.setSize((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()),(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
        mainPanel.setBackground(Color.BLACK);
        add(mainPanel);
        mainPanel.setVisible(true);

        start();

        addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                snake.keyPressed(e);

            }
        });
        setFocusable(true);

    }

    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        snake.paint(g2d);

        //System.out.println("hello");

    }

    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            try {
                move();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    };
    TimerTask update = new TimerTask() {

        public void run() {
            repaint();

        }
    };
    public void start(){
        timer.scheduleAtFixedRate(task, 0, 250);
        timer.schedule(update, 0, 30);

    }

    public void move() throws InterruptedException{
        snake.move();

    }

    public static void main(String[] args) throws InterruptedException{
        Game game = new Game();

    }

}
</code>```

Snake Class

```<code>import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.RepaintManager;

public class Snake {

    public int x = 60;
    public int y = 80;
    public int xa = 0;
    public int ya = 0;
    public int speed = 20;
    private Game game;

    public Snake(Game game){
        this.game = game;
    }

    public void move(){
        x += xa;
        y += ya;
    }

    public void keyPressed(KeyEvent e){
        //RIGHT
        if(e.getKeyCode() == 39){
            xa = speed;
            ya = 0;
        }
        //LEFT
        if(e.getKeyCode() == 37){
            xa = -speed;
            ya = 0;
        }
        //UP
        if(e.getKeyCode() == 38){
            ya = -speed;
            xa = 0;
        }
        //DOWN
        if(e.getKeyCode() == 40){
            ya = speed;
            xa = 0;
        }
        //move();
        //game.repaint();

    }
    public Rectangle getBounds(){
        return new Rectangle(x, y, 20, 20);
    }
    public void paint(Graphics2D g2d){
        g2d.setColor(new Color(48, 255, 55));
        g2d.fillRect(x, y, 20, 20);

    }

}
</code>```

答:
What you have to do is to create a Thread that loops all the time and renders your game, like:

```<code>public void run() {
    while (true) {
        render();
    }
}
</code>```

your run method is correct, but you also need to  extends Thread to repaint in an extra thread over and over again see this post for multithreading.

For all the details concerning BufferStrategy and more follow this Tutorial.
I think it is really good explained, and I used it my self.

Title: Java How to constantly repaint

Body:
I'm trying to make the game Snake, although I'm currently having trouble constantly repainting the snake. I'm using a timer, and when I set the interval to 150 milliseconds, it works, although when I set it to something faster like 30 milliseconds, nothing shows on the screen. The timer w/ repaint() call are in the Game class. Thanks!

Game Class

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;

public class Game extends JFrame{

    JPanel mainPanel = new JPanel();

    BorderLayout layout = new BorderLayout();
    //Game game;
    Snake snake = new Snake(this);

    public Game(){

        super("Snake by Alex 2017");
        setSize((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()),(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        mainPanel.setLayout(null);
        mainPanel.setSize((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()),(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
        mainPanel.setBackground(Color.BLACK);
        add(mainPanel);
        mainPanel.setVisible(true);

        start();

        addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                snake.keyPressed(e);

            }
        });
        setFocusable(true);

    }

    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        snake.paint(g2d);

        //System.out.println("hello");

    }

    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            try {
                move();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    };
    TimerTask update = new TimerTask() {

        public void run() {
            repaint();

        }
    };
    public void start(){
        timer.scheduleAtFixedRate(task, 0, 250);
        timer.schedule(update, 0, 30);

    }

    public void move() throws InterruptedException{
        snake.move();

    }

    public static void main(String[] args) throws InterruptedException{
        Game game = new Game();

    }

}

Snake Class

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.RepaintManager;

public class Snake {

    public int x = 60;
    public int y = 80;
    public int xa = 0;
    public int ya = 0;
    public int speed = 20;
    private Game game;

    public Snake(Game game){
        this.game = game;
    }

    public void move(){
        x += xa;
        y += ya;
    }

    public void keyPressed(KeyEvent e){
        //RIGHT
        if(e.getKeyCode() == 39){
            xa = speed;
            ya = 0;
        }
        //LEFT
        if(e.getKeyCode() == 37){
            xa = -speed;
            ya = 0;
        }
        //UP
        if(e.getKeyCode() == 38){
            ya = -speed;
            xa = 0;
        }
        //DOWN
        if(e.getKeyCode() == 40){
            ya = speed;
            xa = 0;
        }
        //move();
        //game.repaint();

    }
    public Rectangle getBounds(){
        return new Rectangle(x, y, 20, 20);
    }
    public void paint(Graphics2D g2d){
        g2d.setColor(new Color(48, 255, 55));
        g2d.fillRect(x, y, 20, 20);

    }

}

Body:
What you have to do is to create a Thread that loops all the time and renders your game, like:

public void run() {
    while (true) {
        render();
    }
}

your run method is correct, but you also need to  extends Thread to repaint in an extra thread over and over again see this post for multithreading.

For all the details concerning BufferStrategy and more follow this Tutorial.
I think it is really good explained, and I used it my self.


问:
Update from "Azure media service" to "Media Encoder Standard"
We have been using Azure Media Services for converting video, audio and video thumbnails to various formats. Recently, We discovered that Azure Media Services is not supported in Europe.

Since Window Azure Media Services is deprecated for Europe, we can not use it and have to update to Media Encoder Standard

The week of December 3, 2012, we will be deprecating and removing
  version 1.5.3 of Windows Azure Media Encoder.

We already have existing system which uses Azure Media Services for converting video, audio and video thumbnails to various formats, size etc.But As suggested by Amazon we should update and use Media Encoder Standard instead of using Azure Media Standard.

What are the difference between these two. I need to get clear idea for this to update it to Media Encoder Service. If anyone has already done this, please guide me.

Any Help would be much appreciated.

Note . Looking for comparison

https://azure.microsoft.com/en-in/blog/advanced-encoding-features-in-azure-media-encoder/

https://learn.microsoft.com/en-us/azure/media-services/media-services-mes-presets-overview

答:
To be clear, Azure Media Services is still available in the West Europe and North Europe regions - it is just that the legacy "Windows Azure Media Encoder" component has been deprecated and replaced by "Media Encoder Standard" (MES).

The latter, MES, has almost all of the features of the former - notable exclusion is that MES does not support VC-1/WMA as an output format. See https://azure.microsoft.com/en-us/blog/announcing-the-general-availability-of-media-encoder-standard/ for more details about the differences.

If you can share more details about the Presets that you were using with Windows Azure Media Encoder, we can provide help converting them to the MES schema.


问:
htaccess 301 redirect rules issue
I am trying to do a simple redirection, but I encounter this error, when I do the 301 redirect using the htaccess, it returns the redirect but with the value

.htaccess

```<code>RewriteRule ^clientes/(\w+)/?$ clientes.php?id=$1 [L]

Redirect 301 /clientes/juan http://google.es
</code>```

result:

```<code>https://www.google.es/?id=juan&gws_rd=ssl
</code>```

答:
this working, but my problem is with this line.

```<code>Rewriterule ^videos/(.*)_(.*).html$ index.php?tag=$1&page=$2 [L,NC]
Rewriterule ^videos/(.*).html$ index.php?tag=$1 [L,NC]
RewriteRule ^/videos/juan.html https://www.dominio .com/cat/conduccion-juan/ [R=301,L]
</code>```

and this not working

```<code>redirect 301 /videos/juan.html https://www.dominio.com/videos/conduccion-juan.html
</code>```

thank you!

Title: htaccess 301 redirect rules issue

Body:
I am trying to do a simple redirection, but I encounter this error, when I do the 301 redirect using the htaccess, it returns the redirect but with the value

.htaccess

RewriteRule ^clientes/(\w+)/?$ clientes.php?id=$1 [L]

Redirect 301 /clientes/juan http://google.es

result:

https://www.google.es/?id=juan&gws_rd=ssl

Body:
this working, but my problem is with this line.

Rewriterule ^videos/(.*)_(.*).html$ index.php?tag=$1&page=$2 [L,NC]
Rewriterule ^videos/(.*).html$ index.php?tag=$1 [L,NC]
RewriteRule ^/videos/juan.html https://www.dominio .com/cat/conduccion-juan/ [R=301,L]

and this not working

redirect 301 /videos/juan.html https://www.dominio.com/videos/conduccion-juan.html

thank you!


问:
.net web service hosted within my application
I'm migrating an old Delphi application that I wrote into C#. The application is a datalogger that exposes logged data requests via a SOAP web service interface.

The web service is contained with the delphi graphical windows application, i.e. no need to run a web server like IIS, etc I just run the application and it's up and running under the hood.

I'm looking to do the same in my c# Windows form application, I can find loads of resources on writing web services that are ultimately hosted within IIS but am struggling to find a solution for a self contained web service within my application.

Does anyone have any suggestions or can point me towards any resources on this?

The web service does not neceserily have to be SOAP, REST is fine (in fact probably prefered).

答:
Look into WCF Services.

Hosting and Consuming WCF Services

Hosting WCF services in a Windows Forms Application

ContentLicense: CC BY-SA 2.5


问:
similarity search API of Watson Visual Recognition related to bulk volume of images
We have a customer requirement to search similar images in a collection using Watson Visual Recognition. The documentation mentions that each collection can contain 1 million images. Thus, I have the following questions:

a) What is the maximum size of the image?

b) Each image upload takes up to 1 second and the standard plan has a limit of 25000 images per day. So, can only 25k images added to the collection/day?

c) The customer has about 2 million images. How can we upload the images faster?

d) Is there a separate plan available for bulk volumes?

答: Ravi, I see you posted your question on developerWorks too - please see my answer here: https://developer.ibm.com/answers/questions/379227/similarity-search-api-of-watson-visual-recognition/</p>


问:
How do I turn a single column table into multiple columns based solely on row number?
So, I have this data that exists in a single column. Odd rows are ID, even rows are city. Is there a way to split this into two columns?

```<code>    DECLARE @Data TABLE (
          DataRow       NVARCHAR(50)
          )

    INSERT INTO @Data VALUES

          ('1'              )
        , ('Albuquerque'    )
        , ('2'              )
        , ('Boston'         )
        , ('3'              )
        , ('Chicago'        )
        , ('4'              )
        , ('Dayton'         )
        , ('5'              )
        , ('Eumenclaw'      )
        , ('6'              )
        , ('Fresno'         )
</code>```

Right now I'm using the following code, but it seems like there should be a more efficient way using a pivot table.

```<code>    DECLARE @DataID TABLE (
            ID          INT IDENTITY
          , DataRow     NVARCHAR(50)
          )

    INSERT INTO @DataID
        SELECT * FROM @Data

    DECLARE @CityData TABLE (
          ID            INT
        , City          NVARCHAR(100)
        )

    DECLARE   @Counter      INT = 0
            , @ID           INT
            , @City         NVARCHAR(50)

    WHILE @Counter < (SELECT MAX(ID) / 2 FROM @DataID WHERE ID%2 = 0)
        BEGIN
            SET @Counter += 1
            SET @ID = (SELECT CAST(DataRow AS INT) FROM @DataID WHERE ID = @Counter * 2 - 1)
            SET @City = (SELECT DataRow FROM @DataID WHERE ID = @Counter * 2)
            INSERT INTO @CityData
                SELECT @ID, @City

        END

    SELECT * FROM @CityData
</code>```

Results:

Oh, and apologies to those of you from Washington for the misspelling. And hopefully not New Mexico.

答:
Yet another option, to ensure the proper sequence is to parse data as string with a CRLF delimiter

Consider the following:

```<code>Declare @Delimiter varchar(25) = char(13)+char(10)
Declare @String varchar(max) = '
1
Albuquerque
2
Boston
3
Chicago
4
Dayton
5
Eumenclaw
6
Fresno
'

Select ID   = max(case when RetSeq % 2 = 1 then RetVal end)
      ,City = max(case when RetSeq % 2 = 0 then RetVal end)
 From (
        Select *,Grp = (RetSeq-1) / 2
         From (
                Select RetSeq = Row_Number() over (Order By (Select null))
                      ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
                From  (Select x = Cast('<x>' + replace((Select replace(@String,@Delimiter,'§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A
                Cross Apply x.nodes('x') AS B(i)
                Where LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)'))) is not null
              ) A1
      ) A
 Group By Grp
</code>```

Returns

```<code>ID  City
1   Albuquerque
2   Boston
3   Chicago
4   Dayton
5   Eumenclaw
6   Fresno
</code>```

Body:
So, I have this data that exists in a single column. Odd rows are ID, even rows are city. Is there a way to split this into two columns?

    DECLARE @Data TABLE (
          DataRow       NVARCHAR(50)
          )

    INSERT INTO @Data VALUES

          ('1'              )
        , ('Albuquerque'    )
        , ('2'              )
        , ('Boston'         )
        , ('3'              )
        , ('Chicago'        )
        , ('4'              )
        , ('Dayton'         )
        , ('5'              )
        , ('Eumenclaw'      )
        , ('6'              )
        , ('Fresno'         )

Right now I'm using the following code, but it seems like there should be a more efficient way using a pivot table.

    DECLARE @DataID TABLE (
            ID          INT IDENTITY
          , DataRow     NVARCHAR(50)
          )

    INSERT INTO @DataID
        SELECT * FROM @Data

    DECLARE @CityData TABLE (
          ID            INT
        , City          NVARCHAR(100)
        )

    DECLARE   @Counter      INT = 0
            , @ID           INT
            , @City         NVARCHAR(50)

    WHILE @Counter < (SELECT MAX(ID) / 2 FROM @DataID WHERE ID%2 = 0)
        BEGIN
            SET @Counter += 1
            SET @ID = (SELECT CAST(DataRow AS INT) FROM @DataID WHERE ID = @Counter * 2 - 1)
            SET @City = (SELECT DataRow FROM @DataID WHERE ID = @Counter * 2)
            INSERT INTO @CityData
                SELECT @ID, @City

        END

    SELECT * FROM @CityData

Results:

Oh, and apologies to those of you from Washington for the misspelling. And hopefully not New Mexico.

Body:
Yet another option, to ensure the proper sequence is to parse data as string with a CRLF delimiter

Consider the following:

Declare @Delimiter varchar(25) = char(13)+char(10)
Declare @String varchar(max) = '
1
Albuquerque
2
Boston
3
Chicago
4
Dayton
5
Eumenclaw
6
Fresno
'

Select ID   = max(case when RetSeq % 2 = 1 then RetVal end)
      ,City = max(case when RetSeq % 2 = 0 then RetVal end)
 From (
        Select *,Grp = (RetSeq-1) / 2
         From (
                Select RetSeq = Row_Number() over (Order By (Select null))
                      ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
                From  (Select x = Cast('<x>' + replace((Select replace(@String,@Delimiter,'§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A
                Cross Apply x.nodes('x') AS B(i)
                Where LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)'))) is not null
              ) A1
      ) A
 Group By Grp

Returns

ID  City
1   Albuquerque
2   Boston
3   Chicago
4   Dayton
5   Eumenclaw
6   Fresno


问:
create and use index on OrientDB
I create a database
Class: Tab,
Property:
    id (int unique)
    idTag (int)
    tagName( String)
    type (string)
    date (datetime)
    xy (Opoint) geospatial
    z (double)

I want to make an index to speed the query

If a create an index for 1 property (like idTag)

CREATE INDEX index_idTag ON Tab (idTag) notunique;

1/
Who can I query index_idTag and type  in the same query
Something like
Select from Tab where index_idTag= 10 and type=« Personne »

2/
I want to query an index (index_idTag), but return all information (id, index_idTag (idTag), tagName, type, date, xy, z)

Ty for your help

答:
with the index defined, the query:

```<code>Select from Tab where idTag= 10 and type='Personne'
</code>```

will use the index automatically

Title: create and use index on OrientDB

Body:
with the index defined, the query:

Select from Tab where idTag= 10 and type='Personne'

will use the index automatically


问:
How to center 2 slides in Slick carousel?
I use Slick in my project and I need to center two slides. Unfortunately centerMode option works only with odd numbered slidesToShow counts. I tried to center active slides by CSS but without effects.

Do you know any solution? Thank you in advance!

Edit:

Example of my carousel: https://jsfiddle.net/lukaszflorczak/y7t64oqs/.

I want to have 1 centered item on the smallers screens, next 2, 3 etc. Not active slides have less opacity.

答:
Slick Carousel slides can be centered by setting the horizontal margins of .slick-track to auto in addition to using the centerMode attribute.

```<code>.slick-track {
  margin-left: auto;
  margin-right: auto
 }
</code>```

Body:
Slick Carousel slides can be centered by setting the horizontal margins of .slick-track to auto in addition to using the centerMode attribute.

.slick-track {
  margin-left: auto;
  margin-right: auto
 }

ContentLicense: CC BY-SA 4.0


问:
Minifiying List of ranges as integer arrays
I have code like this

```<code>public List<int[]> getMinifiedRanges(List<int[]> ranges) {
    for (int[] range : ranges) {

    }
}
</code>```

My goal is to condense the list of ranges, represented by integer arrays which contain the ranges upper & lower bounds

ex. [100, 200] [250, 350] - this example would return the same input because nothing overlaps

[100, 200] [150, 350] [400, 500] - this would return [100, 350] [400, 500] because the second ranges lower bound is inclusive in the first range so the returned range would have its upper bound extended to 350 - notice how it returns only two arrays when the input was 3 arrays.

I am having trouble figuring out how to retrieve a previous range from a current range so that I could extend either the lower or upper bound.

答:
Once you have the ranges sorted (by min, then by max), each set of overlapping ranges will be grouped together, so you can go through the list checking each range against the last merged range.</p>

```<code>public static List<int[]> getMinifiedRanges(List<int[]> ranges) {
    List<int[]> minRanges = new ArrayList<>();
    if (ranges.isEmpty()) return minRanges;

    List<int[]> sorted = new ArrayList<>(ranges); // don't modify input list
    Collections.sort(sorted, Comparator.<int[]>comparingInt(r -> r[0]).thenComparingInt(r -> r[1]));

    int[] last = sorted.get(0);
    for (int[] next : sorted.subList(1, sorted.size())) {
        if (last[1] < next[0]) {
            minRanges.add(last);
            last = next;
        } else if (next[1] > last[1]) {
            last = new int[] { last[0], next[1] };
        }
    }
    minRanges.add(last);

    return minRanges;
}
</code>```

Body:
I have code like this

public List<int[]> getMinifiedRanges(List<int[]> ranges) {
    for (int[] range : ranges) {

    }
}

My goal is to condense the list of ranges, represented by integer arrays which contain the ranges upper & lower bounds

ex. [100, 200] [250, 350] - this example would return the same input because nothing overlaps

[100, 200] [150, 350] [400, 500] - this would return [100, 350] [400, 500] because the second ranges lower bound is inclusive in the first range so the returned range would have its upper bound extended to 350 - notice how it returns only two arrays when the input was 3 arrays.

I am having trouble figuring out how to retrieve a previous range from a current range so that I could extend either the lower or upper bound.

Body:
Once you have the ranges sorted (by min, then by max), each set of overlapping ranges will be grouped together, so you can go through the list checking each range against the last merged range.

public static List<int[]> getMinifiedRanges(List<int[]> ranges) {
    List<int[]> minRanges = new ArrayList<>();
    if (ranges.isEmpty()) return minRanges;

    List<int[]> sorted = new ArrayList<>(ranges); // don't modify input list
    Collections.sort(sorted, Comparator.<int[]>comparingInt(r -> r[0]).thenComparingInt(r -> r[1]));

    int[] last = sorted.get(0);
    for (int[] next : sorted.subList(1, sorted.size())) {
        if (last[1] < next[0]) {
            minRanges.add(last);
            last = next;
        } else if (next[1] > last[1]) {
            last = new int[] { last[0], next[1] };
        }
    }
    minRanges.add(last);

    return minRanges;
}


问:
How to implement the Java comparable interface?
I am not sure how to implement a comparable interface into my abstract class. I have the following example code that I am using to try and get my head around it:

```<code>public class Animal{
    public String name;
    public int yearDiscovered;
    public String population;

    public Animal(String name, int yearDiscovered, String population){
        this.name = name;
        this.yearDiscovered = yearDiscovered;
        this.population = population; }

    public String toString(){
        String s = "Animal name: "+ name+"\nYear Discovered: "+yearDiscovered+"\nPopulation: "+population;
        return s;
    }
}
</code>```

I have a test class that will create objects of type Animal however I want to have a comparable interface inside this class so that older years of discovery rank higher than low. I have no idea on how to go about this though.

答:
Emp class needs to implement Comaparable interface so we need to Override its compateTo method. </p>

```<code>import java.util.ArrayList;
import java.util.Collections;

class Emp implements Comparable< Emp >{

    int empid;
    String name;

    Emp(int empid,String name){
         this.empid = empid;
         this.name = name;

    }

    @Override
    public String toString(){
        return empid+" "+name;
    }

    @Override
    public int compareTo(Emp o) {

     if(this.empid==o.empid){
       return 0;
     }
     else if(this.empid < o.empid){
     return 1;
     }
     else{
       return -1;
     }
    }
}
</code>```

<hr>

```<code>public class JavaApplication1 {

    public static void main(String[] args) {

    ArrayList<Emp> a= new ArrayList<Emp>();

    a.add(new Emp(10,"Mahadev"));
      a.add(new Emp(50,"Ashish"));
      a.add(new Emp(40,"Amit"));
      Collections.sort(a);
      for(Emp id:a){
      System.out.println(id);
      }
    }

}
</code>```

Body:
I am not sure how to implement a comparable interface into my abstract class. I have the following example code that I am using to try and get my head around it:

public class Animal{
    public String name;
    public int yearDiscovered;
    public String population;

    public Animal(String name, int yearDiscovered, String population){
        this.name = name;
        this.yearDiscovered = yearDiscovered;
        this.population = population; }

    public String toString(){
        String s = "Animal name: "+ name+"\nYear Discovered: "+yearDiscovered+"\nPopulation: "+population;
        return s;
    }
}

I have a test class that will create objects of type Animal however I want to have a comparable interface inside this class so that older years of discovery rank higher than low. I have no idea on how to go about this though.

Body:
Emp class needs to implement Comaparable interface so we need to Override its compateTo method.

import java.util.ArrayList;
import java.util.Collections;

class Emp implements Comparable< Emp >{

    int empid;
    String name;

    Emp(int empid,String name){
         this.empid = empid;
         this.name = name;

    }

    @Override
    public String toString(){
        return empid+" "+name;
    }

    @Override
    public int compareTo(Emp o) {

     if(this.empid==o.empid){
       return 0;
     }
     else if(this.empid < o.empid){
     return 1;
     }
     else{
       return -1;
     }
    }
}

public class JavaApplication1 {

    public static void main(String[] args) {

    ArrayList<Emp> a= new ArrayList<Emp>();

    a.add(new Emp(10,"Mahadev"));
      a.add(new Emp(50,"Ashish"));
      a.add(new Emp(40,"Amit"));
      Collections.sort(a);
      for(Emp id:a){
      System.out.println(id);
      }
    }

}


问:
log4j configuration does not work for a specific class
I have a class which I want to redirect its log output to a file. But no matter what I do, it always outputs to the console. Here is my log4j.properties file.

```<code>log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %C %l %-5p %c{1}:%L - %t - %m%n

log4j.logger.io.opentracing.impl.LoggingEventBuilder=INFO, ul
log4j.additivity.io.opentracing.impl.LoggingEventBuilder=false
log4j.appender.ul=org.apache.log4j.RollingFileAppender
log4j.appender.ul.maxFileSize=50MB
log4j.appender.ul.file=${catalina.home}/logs/mylogs.log
log4j.appender.ul.layout=org.apache.log4j.PatternLayout
log4j.appender.ul.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %t - %m%n
</code>```

Here io.opentracing.impl.LoggingEventBuilder is the class whose output I want to redirect. The interesting thing is that I tried to redirect output from other classes to a file using the same code pattern and it worked. Only for this class it does not work.

I am using slf4j-log4j12. What am I missing?

答: you have defined logger into class like this, private static final Logger logger = LoggerFactory.getLogger(LoggingEventBuilder.class); and call logger for logging link logger.info("message");.

Body:
I have a class which I want to redirect its log output to a file. But no matter what I do, it always outputs to the console. Here is my log4j.properties file.

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %C %l %-5p %c{1}:%L - %t - %m%n

log4j.logger.io.opentracing.impl.LoggingEventBuilder=INFO, ul
log4j.additivity.io.opentracing.impl.LoggingEventBuilder=false
log4j.appender.ul=org.apache.log4j.RollingFileAppender
log4j.appender.ul.maxFileSize=50MB
log4j.appender.ul.file=${catalina.home}/logs/mylogs.log
log4j.appender.ul.layout=org.apache.log4j.PatternLayout
log4j.appender.ul.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %t - %m%n

Here io.opentracing.impl.LoggingEventBuilder is the class whose output I want to redirect. The interesting thing is that I tried to redirect output from other classes to a file using the same code pattern and it worked. Only for this class it does not work.

I am using slf4j-log4j12. What am I missing?


问:
Executing a powershell script on a remote computer file system
There is a powershell script located on the C:\. The script executes a .msu file.

Invoke-Command -ComputerName [name] -FilePath C:\file.txt doesn't work because it checks locally on your system for the file to pass over to execute.

I want a powershell script that executes another remote powershell script remotely.

答:
And if you try this method?

Run this command on your local machine :

```<code>$computer = "COMP1"
([WMICLASS]"\\$computer\ROOT\CIMV2:Win32_Process").Create('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -File "script.ps1"')
</code>```

Body:
And if you try this method?

Run this command on your local machine :

$computer = "COMP1"
([WMICLASS]"\\$computer\ROOT\CIMV2:Win32_Process").Create('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -File "script.ps1"')


问:
Can anyone explain the difference between expression, ng-bind and ng-model?
I am very confuse between expression {{}}, ng-bind and ng-model. please explain with example if possible.</p>

答:
ng-bind and {{}} are basically the same. However, before your application fully loads, sometimes {{}} are visible. ng-bind usually looks a little nicer before everything finishes loading. {{}} is easier, but has some limitations that will require ng-bind.  is your friend.

There are some contexts that {{}} will not work. For example when your data is markup.

ng-model is used for input.

All three are used in the example below:

Please Enter Your Name
<input type="text" ng-model="yourName" />

Hello , this is an ng-bind!

Hello {{yourName}}, this is bind in brackets

```

Body:
ng-bind and {{}} are basically the same. However, before your application fully loads, sometimes {{}} are visible. ng-bind usually looks a little nicer before everything finishes loading. {{}} is easier, but has some limitations that will require ng-bind. <span ng-bind="something"></span> is your friend.

There are some contexts that {{}} will not work. For example when your data is markup.

ng-model is used for input.

All three are used in the example below:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <p>Please Enter Your Name</p><input type="text" ng-model="yourName" />
  <p>Hello <span ng-bind="yourName"></span>, this is an ng-bind!</p>
  <p>Hello {{yourName}}, this is bind in brackets</p>
</div>


问:
How come with JQuery Masonry, when I click "back" on the browser, it shoots the user back to the top?
Why can't it hold the position where the user left off? Is this normal for Masonry?
When the user clicks back, the browser goes to where the user left off, but weirdly, Masonry makes it SHOOT back up to the top!

By the way, refresh works fine. It moves the scroll to where the user left off. But back doesn't work.

Edit: when I click "back", it's up at the top. But then I scroll down just a tiny bit (2 pixels). And then the browser will jump back to the position of the left off place.

ures.

Edit: If I disable this line, it will work:

```<code>$('#grid').masonry({ columnWidth:250, itemSelector:".awallpost", animate:false, resizeable:false });
</code>```

答:
This will help you,

I found one post having the same senario, and testing its working

Hi,
  The website is http://www.charlotte.com/mld/charlotte/

I just checked and yes, the site is sending cache-control: no-store in the header.

I also read through the bug link you posted. Man, this thing has been documented for years.
  The particular link you posted appears to be still unresolved, and it's primary, not a dupe.

Would it help matters if I added my experience to the bug report, or should I just grin and bear it?

Body:
Why can't it hold the position where the user left off? Is this normal for Masonry?
When the user clicks back, the browser goes to where the user left off, but weirdly, Masonry makes it SHOOT back up to the top!

By the way, refresh works fine. It moves the scroll to where the user left off. But back doesn't work.

Edit: when I click "back", it's up at the top. But then I scroll down just a tiny bit (2 pixels). And then the browser will jump back to the position of the left off place.

ures.

Edit: If I disable this line, it will work:

$('#grid').masonry({ columnWidth:250, itemSelector:".awallpost", animate:false, resizeable:false });


问:
How to include sidekick and parsys in a page (AEM)
I have created a simple page and trying to include sidekick and parsys in that page. I have included init.jsp and parsys in my code. But when I load a page it does not show up. Please help me with this. Thank you.

```<code><cq:include script="/libs/wcm/core/components/init/init.jsp"/>
<cq:include path="par" resourceType="libs/foundation/components/parsys" />
</code>```

答:
the code provided for including parsys wont work .
correct one is:</p>

```<code><cq:include path="par" resourceType="/libs/foundation/components/parsys"/>
</code>```

Body:
I have created a simple page and trying to include sidekick and parsys in that page. I have included init.jsp and parsys in my code. But when I load a page it does not show up. Please help me with this. Thank you.

<cq:include script="/libs/wcm/core/components/init/init.jsp"/>
<cq:include path="par" resourceType="libs/foundation/components/parsys" />

Body:
the code provided for including parsys wont work .
correct one is:

<cq:include path="par" resourceType="/libs/foundation/components/parsys"/>


问:
Scrapy: 503 Error when scraping site using CloudFlare
When using scrapy to scrape a site, I was receiving 503 Service Unavailable as an error right away (could not even start scraping any items). After finding this thread:

How to bypass cloudflare bot/ddos protection in Scrapy?

I assumed the problem was CloudFlare, so I added the following code that uses cfscrape from one of the answers to my Spider:

```<code>def start_requests(self):
    cf_requests = []
    for url in self.start_urls:
        token, agent = cfscrape.get_tokens(url, USER_AGENT)
        #token, agent = cfscrape.get_tokens(url)
        cf_requests.append(scrapy.Request(url=url, cookies={'__cfduid': token['__cfduid']}, headers={'User-Agent': agent}))
        print "useragent in cfrequest: " , agent
        print "token in cfrequest: ", token
    return cf_requests
</code>```

Looking at the output, it seems like this workaround is indeed executing the javascript that CloudFlare uses for ddos protection, but it still gives me 503 error afterwards. Here is the debug output:

```<code>2015-11-04 23:07:12 [scrapy] INFO: Scrapy 1.0.3 started (bot: forumscrape)
2015-11-04 23:07:12 [scrapy] INFO: Optional features available: ssl, http11, boto
2015-11-04 23:07:12 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'forumscrape.spiders', 'CONCURRENT_REQUESTS_PER_DOMAIN': 1, 'CONCURRENT_REQUESTS': 1, 'SPIDER_MODULES': ['forumscrape.spiders'], 'CONCURRENT_REQUESTS_PER_IP': 1, 'BOT_NAME': 'forumscrape', 'COOKIES_ENABLED': False, 'DOWNLOAD_DELAY': 1}
2015-11-04 23:07:12 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, LogStats, CoreStats, SpiderState
2015-11-04 23:07:13 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, ChunkedTransferMiddleware, DownloaderStats
2015-11-04 23:07:13 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2015-11-04 23:07:13 [scrapy] INFO: Enabled item pipelines:
2015-11-04 23:07:13 [requests.packages.urllib3.connectionpool] INFO: Starting new HTTP connection (1): sampleforum.com
2015-11-04 23:07:13 [requests.packages.urllib3.connectionpool] DEBUG: "GET /forumdisplay.php?29-Chat HTTP/1.1" 503 None
2015-11-04 23:07:18 [requests.packages.urllib3.connectionpool] INFO: Starting new HTTP connection (1): sampleforum.com
2015-11-04 23:07:18 [requests.packages.urllib3.connectionpool] DEBUG: "GET /cdn-cgi/l/chk_jschl?jschl_answer=-8011&jschl_vc=6b1abd999393b114b8eea35ff2be9e55&pass=1446696428.397-J92apQZ8k3 HTTP/1.1" 302 165
2015-11-04 23:07:18 [requests.packages.urllib3.connectionpool] DEBUG: "GET /forumdisplay.php?29-Chat HTTP/1.1" 200 21403
useragent in cfrequest:  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0
token in cfrequest:  {'cf_clearance': '037ab6c531be7e6fa6c3d0a98c988f57d17fd781-1446696429-604800', '__cfduid': 'd2635be16360da698f9dd07e4929690ed1446696424'}
2015-11-04 23:07:18 [scrapy] INFO: Spider opened
2015-11-04 23:07:18 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2015-11-04 23:07:18 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2015-11-04 23:07:18 [scrapy] DEBUG: Retrying <GET http://sampleforum.com/forumdisplay.php?29-Chat> (failed 1 times): 503 Service Unavailable
2015-11-04 23:07:20 [scrapy] DEBUG: Retrying <GET http://sampleforum.com/forumdisplay.php?29-Chat> (failed 2 times): 503 Service Unavailable
2015-11-04 23:07:21 [scrapy] DEBUG: Gave up retrying <GET http://sampleforum.com/forumdisplay.php?29-Chat> (failed 3 times): 503 Service Unavailable
2015-11-04 23:07:21 [scrapy] DEBUG: Crawled (503) <GET http://sampleforum.com/forumdisplay.php?29-Chat> (referer: None)
2015-11-04 23:07:21 [scrapy] DEBUG: Ignoring response <503 http://sampleforum.com/forumdisplay.php?29-Chat>: HTTP status code is not handled or not allowed
2015-11-04 23:07:21 [scrapy] INFO: Closing spider (finished)
2015-11-04 23:07:21 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 828,
 downloader/request_count': 3,
'downloader/request_method_count/GET': 3,
 'downloader/response_bytes': 14276, 'downloader/response_count': 3, 'downloader/response_status_count/503': 3,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2015, 11, 5, 4, 7, 21, 363000),
'log_count/DEBUG': 9,
'log_count/INFO': 9,
'response_received_count': 1,
'scheduler/dequeued': 3,
'scheduler/dequeued/memory': 3,
'scheduler/enqueued': 3,
'scheduler/enqueued/memory': 3,
'start_time': datetime.datetime(2015, 11, 5, 4, 7, 18, 755000)}
2015-11-04 23:07:21 [scrapy] INFO: Spider closed (finished)
</code>```

The site loads fine in my browser (the same useragent being used). Other sites that I'm running similar scraping on (just picking up some text) are working. Is there another reason I'm getting 503? Any help would be appreciated.

I believe this line: DEBUG: "GET /cdn-cgi/l/chk_jschl?jschl_answer=23986&jschl_vc=2e88b65c8bcf26f39b980de3d5b198ea&pass=1446698472.387-F9OS39Peei HTTP/1.1" 302 165
Shows that the cloudflare javascript check is being done, so perhaps it's a reason besides this that is causing the 503?

答: You can try use splash for avoid cloudflare.</p>

Body:
When using scrapy to scrape a site, I was receiving 503 Service Unavailable as an error right away (could not even start scraping any items). After finding this thread:

How to bypass cloudflare bot/ddos protection in Scrapy?

I assumed the problem was CloudFlare, so I added the following code that uses cfscrape from one of the answers to my Spider:

def start_requests(self):
    cf_requests = []
    for url in self.start_urls:
        token, agent = cfscrape.get_tokens(url, USER_AGENT)
        #token, agent = cfscrape.get_tokens(url)
        cf_requests.append(scrapy.Request(url=url, cookies={'__cfduid': token['__cfduid']}, headers={'User-Agent': agent}))
        print "useragent in cfrequest: " , agent
        print "token in cfrequest: ", token
    return cf_requests

Looking at the output, it seems like this workaround is indeed executing the javascript that CloudFlare uses for ddos protection, but it still gives me 503 error afterwards. Here is the debug output:

2015-11-04 23:07:12 [scrapy] INFO: Scrapy 1.0.3 started (bot: forumscrape)
2015-11-04 23:07:12 [scrapy] INFO: Optional features available: ssl, http11, boto
2015-11-04 23:07:12 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'forumscrape.spiders', 'CONCURRENT_REQUESTS_PER_DOMAIN': 1, 'CONCURRENT_REQUESTS': 1, 'SPIDER_MODULES': ['forumscrape.spiders'], 'CONCURRENT_REQUESTS_PER_IP': 1, 'BOT_NAME': 'forumscrape', 'COOKIES_ENABLED': False, 'DOWNLOAD_DELAY': 1}
2015-11-04 23:07:12 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, LogStats, CoreStats, SpiderState
2015-11-04 23:07:13 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, ChunkedTransferMiddleware, DownloaderStats
2015-11-04 23:07:13 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2015-11-04 23:07:13 [scrapy] INFO: Enabled item pipelines:
2015-11-04 23:07:13 [requests.packages.urllib3.connectionpool] INFO: Starting new HTTP connection (1): sampleforum.com
2015-11-04 23:07:13 [requests.packages.urllib3.connectionpool] DEBUG: "GET /forumdisplay.php?29-Chat HTTP/1.1" 503 None
2015-11-04 23:07:18 [requests.packages.urllib3.connectionpool] INFO: Starting new HTTP connection (1): sampleforum.com
2015-11-04 23:07:18 [requests.packages.urllib3.connectionpool] DEBUG: "GET /cdn-cgi/l/chk_jschl?jschl_answer=-8011&jschl_vc=6b1abd999393b114b8eea35ff2be9e55&pass=1446696428.397-J92apQZ8k3 HTTP/1.1" 302 165
2015-11-04 23:07:18 [requests.packages.urllib3.connectionpool] DEBUG: "GET /forumdisplay.php?29-Chat HTTP/1.1" 200 21403
useragent in cfrequest:  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0
token in cfrequest:  {'cf_clearance': '037ab6c531be7e6fa6c3d0a98c988f57d17fd781-1446696429-604800', '__cfduid': 'd2635be16360da698f9dd07e4929690ed1446696424'}
2015-11-04 23:07:18 [scrapy] INFO: Spider opened
2015-11-04 23:07:18 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2015-11-04 23:07:18 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2015-11-04 23:07:18 [scrapy] DEBUG: Retrying <GET http://sampleforum.com/forumdisplay.php?29-Chat> (failed 1 times): 503 Service Unavailable
2015-11-04 23:07:20 [scrapy] DEBUG: Retrying <GET http://sampleforum.com/forumdisplay.php?29-Chat> (failed 2 times): 503 Service Unavailable
2015-11-04 23:07:21 [scrapy] DEBUG: Gave up retrying <GET http://sampleforum.com/forumdisplay.php?29-Chat> (failed 3 times): 503 Service Unavailable
2015-11-04 23:07:21 [scrapy] DEBUG: Crawled (503) <GET http://sampleforum.com/forumdisplay.php?29-Chat> (referer: None)
2015-11-04 23:07:21 [scrapy] DEBUG: Ignoring response <503 http://sampleforum.com/forumdisplay.php?29-Chat>: HTTP status code is not handled or not allowed
2015-11-04 23:07:21 [scrapy] INFO: Closing spider (finished)
2015-11-04 23:07:21 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 828,
 downloader/request_count': 3,
'downloader/request_method_count/GET': 3,
 'downloader/response_bytes': 14276, 'downloader/response_count': 3, 'downloader/response_status_count/503': 3,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2015, 11, 5, 4, 7, 21, 363000),
'log_count/DEBUG': 9,
'log_count/INFO': 9,
'response_received_count': 1,
'scheduler/dequeued': 3,
'scheduler/dequeued/memory': 3,
'scheduler/enqueued': 3,
'scheduler/enqueued/memory': 3,
'start_time': datetime.datetime(2015, 11, 5, 4, 7, 18, 755000)}
2015-11-04 23:07:21 [scrapy] INFO: Spider closed (finished)

The site loads fine in my browser (the same useragent being used). Other sites that I'm running similar scraping on (just picking up some text) are working. Is there another reason I'm getting 503? Any help would be appreciated.

I believe this line: DEBUG: "GET /cdn-cgi/l/chk_jschl?jschl_answer=23986&jschl_vc=2e88b65c8bcf26f39b980de3d5b198ea&pass=1446698472.387-F9OS39Peei HTTP/1.1" 302 165
Shows that the cloudflare javascript check is being done, so perhaps it's a reason besides this that is causing the 503?


问:
Use a field from a previous hop as a parameter in a Table input using Pentaho Kettle
I'm using Pentaho Kettle in order to recreate the data blending process for putting together various data sources to a single data set.

At one point I'm using a Row Filter to check if certain fails are null (in this case length and weight). Right after the true part of the filter I'm using a Table input (MSSQL) to run a query and try to find additional data in a database. Is there a way I can use a field from the Row Filter (the foreign key patient_id, in my case) to use it in the Table input to write down a query like

```<code>SELECT field1, field2, ...
FROM my_table WHERE patient_id in (
    reference_to_the_patient_id_field_from_the_row_filter
)
</code>```

I've been trying around with variables, but they seem to be environment only, not context. Also  the option Replace variables in script doesn't seem to do much for me.

Hope you can help me. Thanks for your time.

答:
Table Input is a row generator step, not an intermediate step. If you will use option "Insert data from step" it replace actual question marks with values, and will send built query to database. Which have really pour performance and error prone (what is value from previous step is null or empty string, or it might even contains sql injection?)

DatabaseJoin step fits better in such place. It uses PreparedStatement, this is much faster.

You can change sql query to pass parameters

```<code>select field1, field2 form my_table join (values(?)) as t(patient_id) on t.patient_id = my_table.patient_id
</code>```

You might consider to use DatabaseLookup step, since there is only one table is used to extract data. It has well performance, and provide cache mechanism.

Body:
I'm using Pentaho Kettle in order to recreate the data blending process for putting together various data sources to a single data set.

At one point I'm using a Row Filter to check if certain fails are null (in this case length and weight). Right after the true part of the filter I'm using a Table input (MSSQL) to run a query and try to find additional data in a database. Is there a way I can use a field from the Row Filter (the foreign key patient_id, in my case) to use it in the Table input to write down a query like

SELECT field1, field2, ...
FROM my_table WHERE patient_id in (
    reference_to_the_patient_id_field_from_the_row_filter
)

I've been trying around with variables, but they seem to be environment only, not context. Also  the option Replace variables in script doesn't seem to do much for me.

Hope you can help me. Thanks for your time.

Body:
Table Input is a row generator step, not an intermediate step. If you will use option "Insert data from step" it replace actual question marks with values, and will send built query to database. Which have really pour performance and error prone (what is value from previous step is null or empty string, or it might even contains sql injection?)

DatabaseJoin step fits better in such place. It uses PreparedStatement, this is much faster.

You can change sql query to pass parameters

select field1, field2 form my_table join (values(?)) as t(patient_id) on t.patient_id = my_table.patient_id

You might consider to use DatabaseLookup step, since there is only one table is used to extract data. It has well performance, and provide cache mechanism.


问:
How can the name of an app with many files be easily changed?
I want to change the name of my iOS App, how can I easily change the name without going through each file and possible locations that I have used the old name </p>

答: You could replace all occurrences in the project from the project navigator.


问:
Mutual ssl authentication in simple ECHO client/server [Python / sockets / ssl modules], ssl.SSLEOFError: EOF occurred in violation of protocol
I would like to have a mutual authentication in my echo client/server program. I'm using python 2.7.12 and thessl` module on

```<code>Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:        14.04
Codename:       trusty
</code>```

I've generated client's and server's certificates and keys using the openssl commands:

```<code>openssl req -new -x509 -days 365 -nodes -out client.pem -keyout client.key
openssl req -new -x509 -days 365 -nodes -out server.pem -keyout server.key
</code>```

I want the client to authenticate the server, and I want the server to authenticate the client. However, the code below shows some errors, at server's side:

```<code>Traceback (most recent call last):
  File "ssl_server.py", line 18, in <module>
    secure_sock = ssl.wrap_socket(client, server_side=True, certfile="server.pem", keyfile="server.key")
  File "/usr/lib/python2.7/ssl.py", line 933, in wrap_socket
    ciphers=ciphers)
  File "/usr/lib/python2.7/ssl.py", line 601, in __init__
    self.do_handshake()
  File "/usr/lib/python2.7/ssl.py", line 830, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:590)
</code>```

At client' side:

```<code>Traceback (most recent call last):
  File "ssl_client.py", line 18, in <module>
    secure_sock = context.wrap_socket(sock, server_hostname=HOST, server_side=False, certfile="client.pem", keyfile="client.key")
TypeError: wrap_socket() got an unexpected keyword argument 'certfile'
</code>```

Server's code:

```<code>#!/bin/usr/env python
import socket
import ssl
import pprint

#server
if __name__ == '__main__':

    HOST = '127.0.0.1'
    PORT = 1234

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind((HOST, PORT))
    server_socket.listen(10)

    client, fromaddr = server_socket.accept()
    secure_sock = ssl.wrap_socket(client, server_side=True, certfile="server.pem", keyfile="server.key")

    print repr(secure_sock.getpeername())
    print secure_sock.cipher()
    print pprint.pformat(secure_sock.getpeercert())
    cert = secure_sock.getpeercert()
    print cert

    # verify client
    if not cert or ('commonName', 'test') not in cert['subject'][4]: raise Exception("ERROR")

    try:
        data = secure_sock.read(1024)
        secure_sock.write(data)
    finally:
        secure_sock.close()
        server_socket.close()
</code>```

Client's code:

```<code>import socket
import ssl

# client
if __name__ == '__main__':

    HOST = '127.0.0.1'
    PORT = 1234

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((HOST, PORT))

    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    context.verify_mode = ssl.CERT_REQUIRED
    context.load_verify_locations('server.pem')

    if ssl.HAS_SNI:
        secure_sock = context.wrap_socket(sock, server_hostname=HOST, server_side=False, certfile="client.pem", keyfile="client.key")
    else:
        secure_sock = context.wrap_socket(sock, server_side=False, certfile="client.pem", keyfile="client.key")

    cert = secure_sock.getpeercert()
    print cert

    # verify server
    if not cert or ('commonName', 'test') not in cert['subject'][4]: raise Exception("ERROR")

    secure_sock.write('hello')
    secure_sock.read(1024)

    secure_sock.close()
    sock.close()
</code>```

Thank you.

答:
Basically the server need to share with the client his certificate and vice versa (look the ca_certs parameter). The main problem with your code is that the handshake were never executed. Also, the Common Name string position depends on how many field did specified in the certificate. I had been lazy, so my subject has only 4 fiels, and Common Name is the last of them.

Now it works (feel free to ask for further details).

Server

```<code>#!/bin/usr/env python
import socket
import ssl
import pprint

#server
if __name__ == '__main__':

    HOST = '127.0.0.1'
    PORT = 1234

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind((HOST, PORT))
    server_socket.listen(10)

    client, fromaddr = server_socket.accept()
    secure_sock = ssl.wrap_socket(client, server_side=True, ca_certs = "client.pem", certfile="server.pem", keyfile="server.key", cert_reqs=ssl.CERT_REQUIRED,
                           ssl_version=ssl.PROTOCOL_TLSv1_2)

    print repr(secure_sock.getpeername())
    print secure_sock.cipher()
    print pprint.pformat(secure_sock.getpeercert())
    cert = secure_sock.getpeercert()
    print cert

    # verify client
    if not cert or ('commonName', 'test') not in cert['subject'][3]: raise Exception("ERROR")

    try:
        data = secure_sock.read(1024)
        secure_sock.write(data)
    finally:
        secure_sock.close()
        server_socket.close()
</code>```

Client

```<code>import socket
import ssl

# client
if __name__ == '__main__':

    HOST = '127.0.0.1'
    PORT = 1234

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setblocking(1);
    sock.connect((HOST, PORT))

    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    context.verify_mode = ssl.CERT_REQUIRED
    context.load_verify_locations('server.pem')
    context.load_cert_chain(certfile="client.pem", keyfile="client.key")

    if ssl.HAS_SNI:
        secure_sock = context.wrap_socket(sock, server_side=False, server_hostname=HOST)
    else:
        secure_sock = context.wrap_socket(sock, server_side=False)

    cert = secure_sock.getpeercert()
    print cert

    # verify server
    if not cert or ('commonName', 'test') not in cert['subject'][3]: raise Exception("ERROR")

    secure_sock.write('hello')
    print secure_sock.read(1024)

    secure_sock.close()
    sock.close()
</code>```

Take a look:

Ps: I made the client print the server response.

Response to comments

On client's side you never used the context variable I've created. Does it mean it's unnecessary here?

Documentation says:

For more sophisticated applications, the ssl.SSLContext class helps manage settings and certificates, which can then be inherited by SSL sockets created through the SSLContext.wrap_socket() method.

I've updated the code to show you the differences: the server uses ssl.wrap_socket(), the client ssl.SSLContext.wrap_socket().

Second, what's the point in checking if ssl.HAS_SNI when the socket creation looks the same in if and else? With your approach I cant use server_hostname=HOST in socket wrapping method.

You are right, in the updated code I used server_hostname=HOST.

Another thing: you're using ca_certs instead of using load_verify_locations in context I created. Why? Are those 2 methods identical?

My fault, I was using ca_cert as parameter of ssl.wrap_socket(), so I didn't used the context at all. Now I use it.

And another thing: do you really need to call secure_sock.do_handshake() by yourself?

Nope, I forgot to remove it :)

The output is exactly the same.

Body:
I would like to have a mutual authentication in my echo client/server program. I'm using python 2.7.12 and thessl` module on

Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:        14.04
Codename:       trusty

I've generated client's and server's certificates and keys using the openssl commands:

openssl req -new -x509 -days 365 -nodes -out client.pem -keyout client.key
openssl req -new -x509 -days 365 -nodes -out server.pem -keyout server.key

I want the client to authenticate the server, and I want the server to authenticate the client. However, the code below shows some errors, at server's side:

Traceback (most recent call last):
  File "ssl_server.py", line 18, in <module>
    secure_sock = ssl.wrap_socket(client, server_side=True, certfile="server.pem", keyfile="server.key")
  File "/usr/lib/python2.7/ssl.py", line 933, in wrap_socket
    ciphers=ciphers)
  File "/usr/lib/python2.7/ssl.py", line 601, in __init__
    self.do_handshake()
  File "/usr/lib/python2.7/ssl.py", line 830, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:590)

At client' side:

Traceback (most recent call last):
  File "ssl_client.py", line 18, in <module>
    secure_sock = context.wrap_socket(sock, server_hostname=HOST, server_side=False, certfile="client.pem", keyfile="client.key")
TypeError: wrap_socket() got an unexpected keyword argument 'certfile'

Server's code:

#!/bin/usr/env python
import socket
import ssl
import pprint

#server
if __name__ == '__main__':

    HOST = '127.0.0.1'
    PORT = 1234

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind((HOST, PORT))
    server_socket.listen(10)

    client, fromaddr = server_socket.accept()
    secure_sock = ssl.wrap_socket(client, server_side=True, certfile="server.pem", keyfile="server.key")

    print repr(secure_sock.getpeername())
    print secure_sock.cipher()
    print pprint.pformat(secure_sock.getpeercert())
    cert = secure_sock.getpeercert()
    print cert

    # verify client
    if not cert or ('commonName', 'test') not in cert['subject'][4]: raise Exception("ERROR")

    try:
        data = secure_sock.read(1024)
        secure_sock.write(data)
    finally:
        secure_sock.close()
        server_socket.close()

Client's code:

import socket
import ssl

# client
if __name__ == '__main__':

    HOST = '127.0.0.1'
    PORT = 1234

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((HOST, PORT))

    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    context.verify_mode = ssl.CERT_REQUIRED
    context.load_verify_locations('server.pem')

    if ssl.HAS_SNI:
        secure_sock = context.wrap_socket(sock, server_hostname=HOST, server_side=False, certfile="client.pem", keyfile="client.key")
    else:
        secure_sock = context.wrap_socket(sock, server_side=False, certfile="client.pem", keyfile="client.key")

    cert = secure_sock.getpeercert()
    print cert

    # verify server
    if not cert or ('commonName', 'test') not in cert['subject'][4]: raise Exception("ERROR")

    secure_sock.write('hello')
    secure_sock.read(1024)

    secure_sock.close()
    sock.close()

Thank you.

Body:
Basically the server need to share with the client his certificate and vice versa (look the ca_certs parameter). The main problem with your code is that the handshake were never executed. Also, the Common Name string position depends on how many field did specified in the certificate. I had been lazy, so my subject has only 4 fiels, and Common Name is the last of them.

Now it works (feel free to ask for further details).

Server

#!/bin/usr/env python
import socket
import ssl
import pprint

#server
if __name__ == '__main__':

    HOST = '127.0.0.1'
    PORT = 1234

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind((HOST, PORT))
    server_socket.listen(10)

    client, fromaddr = server_socket.accept()
    secure_sock = ssl.wrap_socket(client, server_side=True, ca_certs = "client.pem", certfile="server.pem", keyfile="server.key", cert_reqs=ssl.CERT_REQUIRED,
                           ssl_version=ssl.PROTOCOL_TLSv1_2)

    print repr(secure_sock.getpeername())
    print secure_sock.cipher()
    print pprint.pformat(secure_sock.getpeercert())
    cert = secure_sock.getpeercert()
    print cert

    # verify client
    if not cert or ('commonName', 'test') not in cert['subject'][3]: raise Exception("ERROR")

    try:
        data = secure_sock.read(1024)
        secure_sock.write(data)
    finally:
        secure_sock.close()
        server_socket.close()

Client

import socket
import ssl

# client
if __name__ == '__main__':

    HOST = '127.0.0.1'
    PORT = 1234

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setblocking(1);
    sock.connect((HOST, PORT))

    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    context.verify_mode = ssl.CERT_REQUIRED
    context.load_verify_locations('server.pem')
    context.load_cert_chain(certfile="client.pem", keyfile="client.key")

    if ssl.HAS_SNI:
        secure_sock = context.wrap_socket(sock, server_side=False, server_hostname=HOST)
    else:
        secure_sock = context.wrap_socket(sock, server_side=False)

    cert = secure_sock.getpeercert()
    print cert

    # verify server
    if not cert or ('commonName', 'test') not in cert['subject'][3]: raise Exception("ERROR")

    secure_sock.write('hello')
    print secure_sock.read(1024)

    secure_sock.close()
    sock.close()

Take a look:

Ps: I made the client print the server response.

Response to comments

On client's side you never used the context variable I've created. Does it mean it's unnecessary here?

Documentation says:

For more sophisticated applications, the ssl.SSLContext class helps manage settings and certificates, which can then be inherited by SSL sockets created through the SSLContext.wrap_socket() method.

I've updated the code to show you the differences: the server uses ssl.wrap_socket(), the client ssl.SSLContext.wrap_socket().

Second, what's the point in checking if ssl.HAS_SNI when the socket creation looks the same in if and else? With your approach I cant use server_hostname=HOST in socket wrapping method.

You are right, in the updated code I used server_hostname=HOST.

Another thing: you're using ca_certs instead of using load_verify_locations in context I created. Why? Are those 2 methods identical?

My fault, I was using ca_cert as parameter of ssl.wrap_socket(), so I didn't used the context at all. Now I use it.

And another thing: do you really need to call secure_sock.do_handshake() by yourself?

Nope, I forgot to remove it :)

The output is exactly the same.


问:
Spring UriComponents thread safety
I've gone through as much of the source code as I can bear for UriComponents and it appears to be thread-safe but was hoping that someone else can confirm this.

Essentially, I am creating a single instance of UriComponents using UriComponentsBuilder. This instance is shared and used to expand and create various URI's with runtime query/path params. Is this safe?

答: UriComponents and its sublass (HierarchicalUriComponents) are immutable. Any operation that returns a UriComponents creates a new instance. The state in UriComponents is marked final. So its safe to use it with multiple threads as they wont be able to modify the state.</p>

Title: Spring UriComponents thread safety


问:
Subsetting using MultiIndex with Time
I am new to the MultiIndex in pandas but I have a situation where it would be helpful. I have a df with a MultiIndex (ON_SCENE and LAST) structured as so:

```<code>                              ID
ON_SCENE            LAST
2016-05-05 03:58:54 last1    1000
2016-05-05 17:23:39 last1    1001
2016-05-05 18:20:50 last1    1002
2016-05-05 21:30:29 last2    1003
2016-05-05 22:33:19 last2    1004
2016-05-05 23:30:23 last3    1005
2016-05-06 00:08:34 last3    1006
2016-05-06 01:33:54 last3    1007
</code>```

I want to subset this data using a date and last name as so:

```<code>df.loc[j.strftime('%Y-%m-%d'),Last_Name]
</code>```

where j is type datetime.date and Last_Name  is a str with a last name. Unfortunately, I keep getting a KeyError. I have also tried:

```<code>    df[j.strftime('%Y-%m-%d')]
    df[Last_Name]
</code>```

But these also give me a KeyErrors. Not sure what I am doing wrong?

Title: Subsetting using MultiIndex with Time

Body:
I am new to the MultiIndex in pandas but I have a situation where it would be helpful. I have a df with a MultiIndex (ON_SCENE and LAST) structured as so:

                              ID
ON_SCENE            LAST
2016-05-05 03:58:54 last1    1000
2016-05-05 17:23:39 last1    1001
2016-05-05 18:20:50 last1    1002
2016-05-05 21:30:29 last2    1003
2016-05-05 22:33:19 last2    1004
2016-05-05 23:30:23 last3    1005
2016-05-06 00:08:34 last3    1006
2016-05-06 01:33:54 last3    1007

I want to subset this data using a date and last name as so:

df.loc[j.strftime('%Y-%m-%d'),Last_Name]

where j is type datetime.date and Last_Name  is a str with a last name. Unfortunately, I keep getting a KeyError. I have also tried:

    df[j.strftime('%Y-%m-%d')]
    df[Last_Name]

But these also give me a KeyErrors. Not sure what I am doing wrong?

Body:
In [103]: x.loc[('2016-05-05', 'last2'), :]
Out[103]:
                             ID
ON_SCENE            LAST
2016-05-05 21:30:29 last2  1003
2016-05-05 22:33:19 last2  1004

or using pd.IndexSlice:

In [104]: idx = pd.IndexSlice

In [105]: x.loc[idx['2016-05-05':'2016-05-06', 'last3'], :]
Out[105]:
                             ID
ON_SCENE            LAST
2016-05-05 23:30:23 last3  1005
2016-05-06 00:08:34 last3  1006
2016-05-06 01:33:54 last3  1007

Pandas Documentation with examples


问:
Ruby on Rails: split string into hash and insert it to a json
I have a model that looks something like:

```<code>  :created_at => Tue, 06 Jun 2017 15:34:32 UTC +00:00,
  :updated_at => Tue, 06 Jun 2017 15:34:35 UTC +00:00,
     :metrics => {
                "metric_a" => 10756,
                "metric_b" => "",
                "metric_c" => 6.67267607666759
},
  :status => "working"
</code>```

With the  metrics field as a json.

And I want to split a string such as:

```<code>"Home/Personal Finance=37.00%=55,Computers/Internet/Web Design and Development=13.77%=48,News=8.64%=45,Arts/Television=5.23%=42,Arts/Music=2.99%=39,News/Current Events=2.66%=38,Society/Issues=2.47%=38,Computers/Internet/On the Web=2.46%=38,Business/Investing=1.76%=36,Society=1.70%=36"
</code>```

To be named categories under the metrics field so that each category will hold two numbers, the percentage and the number that follows it.
So in the example above it would look like:

```<code>  :created_at => Tue, 06 Jun 2017 15:34:32 UTC +00:00,
  :updated_at => Tue, 06 Jun 2017 15:34:35 UTC +00:00,
     :metrics => {
                "metric_a" => 10756,
                "metric_b" => "",
                "metric_c" => 6.67267607666759
                "categories" => {
            "Home/Personal Finance": [37.00,55],
            "Computers/Internet/Web Design and Development": [13.77,48],
            ...
            }

},
  :status => "working"
</code>```

Basically I want to save the two numbers for each category (maybe there's a better way to store them by the way?).

Either way can split the string with .split(",") but what next?

答:
If the structure of your string is always the same, you could do something like this to get the "categories" hash:

```<code>str = "Home/Personal Finance=37.00%=55,Computers/Internet/Web Design and Development=13.77%=48,News=8.64%=45,Arts/Television=5.23%=42,Arts/Music=2.99%=39,News/Current Events=2.66%=38,Society/Issues=2.47%=38,Computers/Internet/On the Web=2.46%=38,Business/Investing=1.76%=36,Society=1.70%=36"

str.split(",").each_with_object({}) do |line, categories|
  items = line.split("=")
  categories[items[0]] = [items[1].gsub("%", "").to_f, items[2].to_i]
end
</code>```

Output:

```<code>{
  "Home/Personal Finance"=>[37.0, 55],
  "Computers/Internet/Web Design and Development"=>[13.77, 48],
  "News"=>[8.64, 45],
  "Arts/Television"=>[5.23, 42],
  "Arts/Music"=>[2.99, 39],
  "News/Current Events"=>[2.66, 38],
  "Society/Issues"=>[2.47, 38],
  "Computers/Internet/On the Web"=>[2.46, 38],
  "Business/Investing"=>[1.76, 36],
  "Society"=>[1.7, 36]
}
</code>```

Body:
I have a model that looks something like:

  :created_at => Tue, 06 Jun 2017 15:34:32 UTC +00:00,
  :updated_at => Tue, 06 Jun 2017 15:34:35 UTC +00:00,
     :metrics => {
                "metric_a" => 10756,
                "metric_b" => "",
                "metric_c" => 6.67267607666759
},
  :status => "working"

With the  metrics field as a json.

And I want to split a string such as:

"Home/Personal Finance=37.00%=55,Computers/Internet/Web Design and Development=13.77%=48,News=8.64%=45,Arts/Television=5.23%=42,Arts/Music=2.99%=39,News/Current Events=2.66%=38,Society/Issues=2.47%=38,Computers/Internet/On the Web=2.46%=38,Business/Investing=1.76%=36,Society=1.70%=36"

To be named categories under the metrics field so that each category will hold two numbers, the percentage and the number that follows it.
So in the example above it would look like:

  :created_at => Tue, 06 Jun 2017 15:34:32 UTC +00:00,
  :updated_at => Tue, 06 Jun 2017 15:34:35 UTC +00:00,
     :metrics => {
                "metric_a" => 10756,
                "metric_b" => "",
                "metric_c" => 6.67267607666759
                "categories" => {
            "Home/Personal Finance": [37.00,55],
            "Computers/Internet/Web Design and Development": [13.77,48],
            ...
            }

},
  :status => "working"

Basically I want to save the two numbers for each category (maybe there's a better way to store them by the way?).

Either way can split the string with .split(",") but what next?

Body:
If the structure of your string is always the same, you could do something like this to get the "categories" hash:

str = "Home/Personal Finance=37.00%=55,Computers/Internet/Web Design and Development=13.77%=48,News=8.64%=45,Arts/Television=5.23%=42,Arts/Music=2.99%=39,News/Current Events=2.66%=38,Society/Issues=2.47%=38,Computers/Internet/On the Web=2.46%=38,Business/Investing=1.76%=36,Society=1.70%=36"

str.split(",").each_with_object({}) do |line, categories|
  items = line.split("=")
  categories[items[0]] = [items[1].gsub("%", "").to_f, items[2].to_i]
end

Output:

{
  "Home/Personal Finance"=>[37.0, 55],
  "Computers/Internet/Web Design and Development"=>[13.77, 48],
  "News"=>[8.64, 45],
  "Arts/Television"=>[5.23, 42],
  "Arts/Music"=>[2.99, 39],
  "News/Current Events"=>[2.66, 38],
  "Society/Issues"=>[2.47, 38],
  "Computers/Internet/On the Web"=>[2.46, 38],
  "Business/Investing"=>[1.76, 36],
  "Society"=>[1.7, 36]
}


问:
Installing scipy for python 2.7
I cannot seem to get SciPy working for Python 2.7 on a 32 bit Windows XP platform. I'd rather not build my own copy. Does anyone know where I could find a installer for it? I tried this site, but it didn't work.</p>

答:
Install miniconda
 Install python 2.7.13 or whatever the latest version of 2.x by running
conda install python=2.7.13
switch to python 2.x in your IDE (in my case pyzo) from the miniconda directory
conda install scipy
and it worked for me. regular pip install scipy didnt work for 2.x

Title: Installing scipy for python 2.7


问:
Can't center div in bootstrap row
I can't center this div class = "light" in my bootstrap row. I've tried using .center-block and .text-center. Here's my HTML and CSS. It makes an orb that I want centered. </p>

```<code><div class = "container lightcontainer">
        <div class = "row">
                <div class = "col-xs-12">
                    <div class = "light"></div>
                </div>
        </div>
    </div>

.light {
    display: none;
    width: 100px;
    height: 100px;
    background-color: #fff;
    z-index: 10;
    box-shadow:
        inset 0 0 50px #fff,      /* inner white */
        inset 20px 0 80px #f0f,   /* inner left magenta short */
        inset -20px 0 80px #0ff,  /* inner right cyan short */
        inset 20px 0 300px #f0f,  /* inner left magenta broad */
        inset -20px 0 300px #0ff, /* inner right cyan broad */
        0 0 50px #fff,            /* outer white */
        -10px 0 80px #f0f,        /* outer left magenta */
        10px 0 80px #0ff;
    position: absolute;
    top: 0;
    left: 0;
    border-radius: 40%;
    margin: 0 auto;
}
</code>```

答:
To center element:

```<code>.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  /* bring your own prefixes */
  transform: translate(-50%, -50%);
}
</code>```

REF: How To Center an Object Exactly In The Center

.light {
  //display: none;
  width: 100px;
  height: 100px;
  top: 50vh;
  background-color: #fff;
  z-index: 10;
  box-shadow: inset 0 0 50px #fff, /* inner white */
  inset 20px 0 80px #f0f, /* inner left magenta short */
  inset -20px 0 80px #0ff, /* inner right cyan short */
  inset 20px 0 300px #f0f, /* inner left magenta broad */
  inset -20px 0 300px #0ff, /* inner right cyan broad */
  0 0 50px #fff, /* outer white */
  -10px 0 80px #f0f, /* outer left magenta */
  10px 0 80px #0ff;
  position: relative;
  top: 0;
  left: 0;
  border-radius: 40%;
  margin: 0 auto;
}

.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  /* bring your own prefixes */
  transform: translate(-50%, -50%);
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="container lightcontainer">
  <div class="row">
    <div class="col-xs-12 centered">
      <div class="light"></div>
    </div>
  </div>
</div></code>```

Title: Can't center div in bootstrap row

Body:
I can't center this div class = "light" in my bootstrap row. I've tried using .center-block and .text-center. Here's my HTML and CSS. It makes an orb that I want centered.

.light {
    display: none;
    width: 100px;
    height: 100px;
    background-color: #fff;
    z-index: 10;
    box-shadow:
        inset 0 0 50px #fff,      /* inner white */
        inset 20px 0 80px #f0f,   /* inner left magenta short */
        inset -20px 0 80px #0ff,  /* inner right cyan short */
        inset 20px 0 300px #f0f,  /* inner left magenta broad */
        inset -20px 0 300px #0ff, /* inner right cyan broad */
        0 0 50px #fff,            /* outer white */
        -10px 0 80px #f0f,        /* outer left magenta */
        10px 0 80px #0ff;
    position: absolute;
    top: 0;
    left: 0;
    border-radius: 40%;
    margin: 0 auto;
}

Body:
To center element:

.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  /* bring your own prefixes */
  transform: translate(-50%, -50%);
}

REF: How To Center an Object Exactly In The Center

.light {
  //display: none;
  width: 100px;
  height: 100px;
  top: 50vh;
  background-color: #fff;
  z-index: 10;
  box-shadow: inset 0 0 50px #fff, /* inner white */
  inset 20px 0 80px #f0f, /* inner left magenta short */
  inset -20px 0 80px #0ff, /* inner right cyan short */
  inset 20px 0 300px #f0f, /* inner left magenta broad */
  inset -20px 0 300px #0ff, /* inner right cyan broad */
  0 0 50px #fff, /* outer white */
  -10px 0 80px #f0f, /* outer left magenta */
  10px 0 80px #0ff;
  position: relative;
  top: 0;
  left: 0;
  border-radius: 40%;
  margin: 0 auto;
}

.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  /* bring your own prefixes */
  transform: translate(-50%, -50%);
}

<div class="container lightcontainer">
  <div class="row">
    <div class="col-xs-12 centered">
      <div class="light"></div>
    </div>
  </div>
</div>


问:
Unable to access EC2 instance
I am using AWS and i created Auto scaling launch configuration using shell Script:

```<code>#!/bin/sh
curl -L https://us-west-2-aws-training.s3.amazonaws.com/awsu-spl/spl03-working-elb/static/bootstrap-elb.sh | sh
</code>```

After creating this and the load balancer, two instances were created. I then copied the DNS Name and pasted it in browser, but it says:

```<code>This site can’t be reached

internal-elb-asg-167368762.us-east-1.elb.amazonaws.com took too long to respond.
Go to http://amazonaws.com/
Search Google for internal elb asg 167368762 east amazonaws
ERR_CONNECTION_TIMED_OUT
</code>```

答:
EDIT

I followed your steps and it failed.

You have to change this part of the User Data:

```<code>#!/bin/sh curl -L https://us-west-2-aws-training.s3.amazonaws.com/awsu-spl/spl03-working-elb/static/bootstrap-elb.sh | sh
</code>```

With this:

```<code>#!/bin/sh
curl -L https://us-west-2-aws-training.s3.amazonaws.com/awsu-spl/spl03-working-elb/static/bootstrap-elb.sh | sh
</code>```

Edit: As @john-rotenstein mentioned is not necessary to use sudo.

Also, check this:

You have the correct security groups on EC2 and with your ELB.

Check if you are listening to the port 80 in you ELB.

The port 80 must be opened in your EC2 security group to your ELB security group and the port 80 must be opened worldwide (0.0.0.0) in your ELB security group.

Finally, are you sure that you are not using an internal load balancer right?

Hope it helps you.

Title: Unable to access EC2 instance

Body:
I am using AWS and i created Auto scaling launch configuration using shell Script:

#!/bin/sh
curl -L https://us-west-2-aws-training.s3.amazonaws.com/awsu-spl/spl03-working-elb/static/bootstrap-elb.sh | sh

After creating this and the load balancer, two instances were created. I then copied the DNS Name and pasted it in browser, but it says:

This site can’t be reached

internal-elb-asg-167368762.us-east-1.elb.amazonaws.com took too long to respond.
Go to http://amazonaws.com/
Search Google for internal elb asg 167368762 east amazonaws
ERR_CONNECTION_TIMED_OUT

Body:
EDIT

I followed your steps and it failed.

You have to change this part of the User Data:

#!/bin/sh curl -L https://us-west-2-aws-training.s3.amazonaws.com/awsu-spl/spl03-working-elb/static/bootstrap-elb.sh | sh

With this:

#!/bin/sh
curl -L https://us-west-2-aws-training.s3.amazonaws.com/awsu-spl/spl03-working-elb/static/bootstrap-elb.sh | sh

Edit: As @john-rotenstein mentioned is not necessary to use sudo.

Also, check this:

You have the correct security groups on EC2 and with your ELB.

Check if you are listening to the port 80 in you ELB.

The port 80 must be opened in your EC2 security group to your ELB security group and the port 80 must be opened worldwide (0.0.0.0) in your ELB security group.

Finally, are you sure that you are not using an internal load balancer right?

Hope it helps you.


问:
How to find month no,name from week number using php
How to find month no,name from week number using php</p>

答:
If you have the ISO week number, then to get the month (of the start of the week) you can use strtotime like:

```<code>// F = full name of month, n = month number without leading zero
echo date('F n', strtotime('2010-W50'));
</code>```

Bear in mind that the ISO week might not be the same as your meaning of week, so read on.

If you want to count the whole weeks since January 1st of this year (regardless of what day of the week that is) then you could do as Adnan mentioned:

```<code>echo date('F n', strtotime('1 Jan + 50 weeks'));
</code>```

Body:
If you have the ISO week number, then to get the month (of the start of the week) you can use strtotime like:

// F = full name of month, n = month number without leading zero
echo date('F n', strtotime('2010-W50'));

Bear in mind that the ISO week might not be the same as your meaning of week, so read on.

If you want to count the whole weeks since January 1st of this year (regardless of what day of the week that is) then you could do as Adnan mentioned:

echo date('F n', strtotime('1 Jan + 50 weeks'));


问:
SQL Server : Insert Trigger for Multi-Tenant Default Entries
I have a table of products that is shared across all tenants. Each tenant can, however, have their own ratings for each product stored in the ProductRating table. When new products are inserted into the Product table, I want a trigger to insert a default rating, for each tenant, into the ProductRating table for each product inserted.

I am thinking this isn't as difficult as my brain is making it, but I just can't get it.

Any help is appreciated.

For simplicity:

```<code>|-----------------------|
|       Tenant          |
|-----------------------|
| TenantId | TenantName |
|-----------------------|

|--------------------------|
|         Product          |
|--------------------------|
| Product ID | ProductName |
|--------------------------|

|-------------------------------|
|          ProductRating        |
|-------------------------------|
| TenantId | ProductId | Rating |
|-------------------------------|
</code>```

J

答:
You probably will need at least two triggers - one for when a product is added to give it a default rating for all existing tenants, and one for when a new tenant is added to give it a default rating for all existing products.

```<code>CREATE TRIGGER DefaultRatingsByNewTenant ON Tenant
FOR INSERT

AS

Begin

    INSERT into ProductRating(TenantID, ProductID, Rating)

    SELECT
        i.TenantID
        , p.ProductID
        , 5 as defaultRating
    FROM
        inserted i
        cross join Product p

end
</code>```

and the other is easy enough to work out from there...

```<code>CREATE TRIGGER DefaultRatingsByNewProduct ON Product
FOR INSERT

AS

Begin

    INSERT into ProductRating(TenantID, ProductID, Rating)

    SELECT
        t.TenantID
        , i.ProductID
        , 5 as defaultRating
    FROM
        inserted i
        cross join tenant t

end
</code>```

Body:
I have a table of products that is shared across all tenants. Each tenant can, however, have their own ratings for each product stored in the ProductRating table. When new products are inserted into the Product table, I want a trigger to insert a default rating, for each tenant, into the ProductRating table for each product inserted.

I am thinking this isn't as difficult as my brain is making it, but I just can't get it.

Any help is appreciated.

For simplicity:

|-----------------------|
|       Tenant          |
|-----------------------|
| TenantId | TenantName |
|-----------------------|

|--------------------------|
|         Product          |
|--------------------------|
| Product ID | ProductName |
|--------------------------|

|-------------------------------|
|          ProductRating        |
|-------------------------------|
| TenantId | ProductId | Rating |
|-------------------------------|

J

Body:
You probably will need at least two triggers - one for when a product is added to give it a default rating for all existing tenants, and one for when a new tenant is added to give it a default rating for all existing products.

CREATE TRIGGER DefaultRatingsByNewTenant ON Tenant
FOR INSERT

AS

Begin

    INSERT into ProductRating(TenantID, ProductID, Rating)

    SELECT
        i.TenantID
        , p.ProductID
        , 5 as defaultRating
    FROM
        inserted i
        cross join Product p

end

and the other is easy enough to work out from there...

CREATE TRIGGER DefaultRatingsByNewProduct ON Product
FOR INSERT

AS

Begin

    INSERT into ProductRating(TenantID, ProductID, Rating)

    SELECT
        t.TenantID
        , i.ProductID
        , 5 as defaultRating
    FROM
        inserted i
        cross join tenant t

end