问:
How to dockerize Slanger for a Ruby on Rails app
Slanger is an open source server implementation of the Pusher protocol written in Ruby.

According to the Slanger project README.md, Slanger is not designed to run inside a Rails.

It is not designed to run inside a Rails or sinatra app

It would be well engineered to dockerize it along with a Ruby on Rails application. What would be the steps to implement that?

答:
You can find below the steps in details. And for your reference, all the code is available on a GitHub repository.

Configure a Redis container

Slanger depends on Redis. So first of all, you need a Redis container to run alongside.

In your docker-compose.yml, add (as an example):

```<code>redis:
  image: redis:4.0-alpine
</code>```

Dockerize Slanger

Add a Dockerfile (e.g., Dockerfile-slanger) for Slanger

```<code>FROM ruby:2.3.3
RUN gem install slanger
CMD slanger --app_key $APPKEY --secret $APPSECRET -r redis://$REDIS_HOST:$REDIS_PORT/$REDIS_DB
</code>```

Update your docker-compose.yml

```<code>slanger:
  build:
    context: .
    dockerfile: Dockerfile-slanger
  depends_on:
    - redis
  ports:
    - "8080:8080"
  env_file:
    - .env.slanger
</code>```

The environment file .env.slanger looks like this:

```<code>APPKEY=application_key
APPSECRET=the_secret_for_the_application
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_DB=Slanger
</code>```

Also in your docker-compose.yml, add slanger to your Rails application's dependency list:

```<code>web:
  build: .
  command: bundle exec rails s -p 3000 -b '0.0.0.0'
  volumes:
    - .:/myapp
  ports:
    - "3000:3000"
  depends_on:
    - db
    - slanger
  env_file:
    - .env
</code>```

Use Pusher to talk to Slanger

Now you need to add the pusher gem to your Gemfile

```<code>gem 'pusher'
</code>```

Next you need to configure Slanger in your Rails app by adding file config/initializers/pusher.rb

```<code>Pusher.app_id='application_id'
Pusher.key='application_key'
Pusher.secret='the_secret_for_the_application'
Pusher.host='slanger'
Pusher.port=4567
Pusher.logger=Rails.logger
</code>```

That's it. Now you can use Pusher in Rails to publish messages to a channel of your Slanger server via Slanger API port, and also use Pusher in JavaScript to subscribe the channel of your Slanger server via WebSocket. Please find below the example.

Rails

```<code>class MessageController < ApplicationController
  def subscribe
    puts 'Subscribed'
  end

  def publish
    message = search_params.require(:message)

    Pusher.trigger('MY_CHANNEL', 'AN_EVENT', message: message)
    puts 'Published'
  end

  private

  def search_params
    params.permit(:message)
  end
end
</code>```

JavaScript

```<code><script src="https://js.pusher.com/4.2/pusher.min.js"></script>

<p>Have subscribed to a Slanger channel</p>
<p>Listening via WebSocket...</p>

<script type="text/javascript">
    var pusher = new Pusher('application_key', {
        wsHost: "localhost",
        wsPort: "8080",
        wssPort: "8080",
        enabledTransports: ['ws', 'flash']
    });

    var channel = pusher.subscribe('MY_CHANNEL');
    channel.bind('AN_EVENT', function(data) {
        console.log('data: ', data);
    });

    Pusher.log = function(data) {
        console.log('\t\t', data);
    };
</script>
</code>```

Build and run with docker compose

Once everything is in place, you can build docker images and run your app with docker compose:

```<code>docker-compose build
docker-compose up

$ docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                    NAMES
9e3996f55e7c        helloslanger_web       "bundle exec rails..."   13 seconds ago      Up 10 seconds       0.0.0.0:3000->3000/tcp   helloslanger_web_1
24c8b05f22a5        helloslanger_slanger   "/bin/sh -c 'slang..."   About an hour ago   Up 13 seconds       0.0.0.0:8080->8080/tcp   helloslanger_slanger_1
6baa9eec8c5a        redis:4.0-alpine       "docker-entrypoint..."   4 days ago          Up 14 seconds       6379/tcp                 helloslanger_redis_1
46bc3498a5bf        postgres               "docker-entrypoint..."   4 days ago          Up 14 seconds       5432/tcp                 helloslanger_db_1
</code>```

Test

Now open the subscriber URL:

```<code>FROM ruby:2.3.3
RUN gem install slanger
CMD slanger --app_key $APPKEY --secret $APPSECRET -r redis://$REDIS_HOST:$REDIS_PORT/$REDIS_DB
</code>```0

And publish a message:

```<code>FROM ruby:2.3.3
RUN gem install slanger
CMD slanger --app_key $APPKEY --secret $APPSECRET -r redis://$REDIS_HOST:$REDIS_PORT/$REDIS_DB
</code>```1

Then in the subscriber's browser console, you'll see:

```<code>FROM ruby:2.3.3
RUN gem install slanger
CMD slanger --app_key $APPKEY --secret $APPSECRET -r redis://$REDIS_HOST:$REDIS_PORT/$REDIS_DB
</code>```2

Hope it helps.

By the way, if you would like to find out how to dockerize your Rails application using docker compose, refer to docker compose documentation.

Body:
You can find below the steps in details. And for your reference, all the code is available on a GitHub repository.

Configure a Redis container

Slanger depends on Redis. So first of all, you need a Redis container to run alongside.

In your docker-compose.yml, add (as an example):

redis:
  image: redis:4.0-alpine

Dockerize Slanger

Add a Dockerfile (e.g., Dockerfile-slanger) for Slanger

FROM ruby:2.3.3
RUN gem install slanger
CMD slanger --app_key $APPKEY --secret $APPSECRET -r redis://$REDIS_HOST:$REDIS_PORT/$REDIS_DB

Update your docker-compose.yml

slanger:
  build:
    context: .
    dockerfile: Dockerfile-slanger
  depends_on:
    - redis
  ports:
    - "8080:8080"
  env_file:
    - .env.slanger

The environment file .env.slanger looks like this:

APPKEY=application_key
APPSECRET=the_secret_for_the_application
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_DB=Slanger

Also in your docker-compose.yml, add slanger to your Rails application's dependency list:

web:
  build: .
  command: bundle exec rails s -p 3000 -b '0.0.0.0'
  volumes:
    - .:/myapp
  ports:
    - "3000:3000"
  depends_on:
    - db
    - slanger
  env_file:
    - .env

Use Pusher to talk to Slanger

Now you need to add the pusher gem to your Gemfile

gem 'pusher'

Next you need to configure Slanger in your Rails app by adding file config/initializers/pusher.rb

Pusher.app_id='application_id'
Pusher.key='application_key'
Pusher.secret='the_secret_for_the_application'
Pusher.host='slanger'
Pusher.port=4567
Pusher.logger=Rails.logger

That's it. Now you can use Pusher in Rails to publish messages to a channel of your Slanger server via Slanger API port, and also use Pusher in JavaScript to subscribe the channel of your Slanger server via WebSocket. Please find below the example.

Rails

class MessageController < ApplicationController
  def subscribe
    puts 'Subscribed'
  end

  def publish
    message = search_params.require(:message)

    Pusher.trigger('MY_CHANNEL', 'AN_EVENT', message: message)
    puts 'Published'
  end

  private

  def search_params
    params.permit(:message)
  end
end

JavaScript

<script src="https://js.pusher.com/4.2/pusher.min.js"></script>

<p>Have subscribed to a Slanger channel</p>
<p>Listening via WebSocket...</p>

<script type="text/javascript">
    var pusher = new Pusher('application_key', {
        wsHost: "localhost",
        wsPort: "8080",
        wssPort: "8080",
        enabledTransports: ['ws', 'flash']
    });

    var channel = pusher.subscribe('MY_CHANNEL');
    channel.bind('AN_EVENT', function(data) {
        console.log('data: ', data);
    });

    Pusher.log = function(data) {
        console.log('\t\t', data);
    };
</script>

Build and run with docker compose

Once everything is in place, you can build docker images and run your app with docker compose:

docker-compose build
docker-compose up

$ docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                    NAMES
9e3996f55e7c        helloslanger_web       "bundle exec rails..."   13 seconds ago      Up 10 seconds       0.0.0.0:3000->3000/tcp   helloslanger_web_1
24c8b05f22a5        helloslanger_slanger   "/bin/sh -c 'slang..."   About an hour ago   Up 13 seconds       0.0.0.0:8080->8080/tcp   helloslanger_slanger_1
6baa9eec8c5a        redis:4.0-alpine       "docker-entrypoint..."   4 days ago          Up 14 seconds       6379/tcp                 helloslanger_redis_1
46bc3498a5bf        postgres               "docker-entrypoint..."   4 days ago          Up 14 seconds       5432/tcp                 helloslanger_db_1

Test

Now open the subscriber URL:

http://localhost:3000/message/subscribe

And publish a message:

http://localhost:3000/message/publish/Slanger%20is%20cool

Then in the subscriber's browser console, you'll see:

Pusher : Event recd : {"event":"AN_EVENT","data":{"message":"Slanger is cool"},"channel":"MY_CHANNEL"}
data:  {message: "Slanger is cool"}

Hope it helps.

By the way, if you would like to find out how to dockerize your Rails application using docker compose, refer to docker compose documentation.


来源: stackexchange

问:
Cannot assign foreign key with item ID
I am trying to auto assign a value to a foreign key, so that the model is automatically associated with another model. This is done when an entry is made by a form. I get the following error

```<code>ValueError at /nodiso/createaction/23/
Cannot assign "'23'": "LeadActions.lead" must be a "Leads" instance.
</code>```

This is the two models:

```<code>class Leads(models.Model):
    company = models.ManyToManyField(Company)
    user = models.ManyToManyField(settings.AUTH_USER_MODEL)
    name = models.CharField(max_length=265)
    email = models.EmailField(max_length=265)
    tel = models.IntegerField()
    dateenq = models.DateField(auto_now_add=True,null=True)

    def get_absolute_url(self):
      return reverse('nodisoapp:leadlist')

    def __str__(self):
       return self.name

class LeadActions(models.Model):
    lead = models.ForeignKey(Leads)
    name = models.CharField(max_length=265)
    crdate = models.DateField(auto_now_add=True)
    Duedate = models.DateField()
    creator = models.CharField(max_length=265)
    overdue = models.IntegerField(null=True,blank=True)

    def get_absolute_url(self):
      return reverse('nodisoapp:leadlist')

    def __str__(self):
      return self.name
</code>```

This is the View

```<code>class ActionCreateView(LoginRequiredMixin, generic.CreateView):
    login_url = '/scrty/login/'
    template_name = "nodiso/actioncreate.html"
    form_class = forms.LeadActionCreateForm
    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.lead = self.kwargs['pk']
        self.object.creator = self.request.user
        self.object.save()
        return super(LeadCreateView, self).form_valid(form)
</code>```

This is the model form

```<code>class LeadActionCreateForm(forms.ModelForm):
    class Meta:
        model = models.LeadActions
        fields = ['name','Duedate']
</code>```

I would appreciate the help.

答:
Try this:

```<code>self.object.lead_id = self.kwargs['pk']
</code>```

Since you are using the key value directly, use need to reference the variable with '_id'.

Another way is:

```<code>self.object.lead_id = Lead.objects.get(pk=self.kwargs['pk'])
</code>```

Which in this case is not required, since you have the key value.

ContentLicense: CC BY-SA 3.0

Title: Cannot assign foreign key with item ID

Body:
I am trying to auto assign a value to a foreign key, so that the model is automatically associated with another model. This is done when an entry is made by a form. I get the following error

ValueError at /nodiso/createaction/23/
Cannot assign "'23'": "LeadActions.lead" must be a "Leads" instance.

This is the two models:

class Leads(models.Model):
    company = models.ManyToManyField(Company)
    user = models.ManyToManyField(settings.AUTH_USER_MODEL)
    name = models.CharField(max_length=265)
    email = models.EmailField(max_length=265)
    tel = models.IntegerField()
    dateenq = models.DateField(auto_now_add=True,null=True)

    def get_absolute_url(self):
      return reverse('nodisoapp:leadlist')

    def __str__(self):
       return self.name

class LeadActions(models.Model):
    lead = models.ForeignKey(Leads)
    name = models.CharField(max_length=265)
    crdate = models.DateField(auto_now_add=True)
    Duedate = models.DateField()
    creator = models.CharField(max_length=265)
    overdue = models.IntegerField(null=True,blank=True)

    def get_absolute_url(self):
      return reverse('nodisoapp:leadlist')

    def __str__(self):
      return self.name

This is the View

class ActionCreateView(LoginRequiredMixin, generic.CreateView):
    login_url = '/scrty/login/'
    template_name = "nodiso/actioncreate.html"
    form_class = forms.LeadActionCreateForm
    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.lead = self.kwargs['pk']
        self.object.creator = self.request.user
        self.object.save()
        return super(LeadCreateView, self).form_valid(form)

This is the model form

class LeadActionCreateForm(forms.ModelForm):
    class Meta:
        model = models.LeadActions
        fields = ['name','Duedate']

I would appreciate the help.

Body:
Try this:

self.object.lead_id = self.kwargs['pk']

Since you are using the key value directly, use need to reference the variable with '_id'.

Another way is:

self.object.lead_id = Lead.objects.get(pk=self.kwargs['pk'])

Which in this case is not required, since you have the key value.


问:
apache2 .htaccess redirect subdirectory to domain without including path
I am attempting to redirect a subfolder on an apache2 server to the root of an entirely different domain, without including the path of the folder in the new domain.

So, in mysite.com/folder-1/folder-2 I have a .htaccess file like so:

```<code>RedirectMatch 301 / http://a-totally-different-site.com/
</code>```

This sort of works, but includes the path in the redirect. So, when you visit mysite.com/folder-1/folder-2 it goes to a-totally-different-site.com/folder-1/folder-2

What I wish to happen, is for the subfolder to redirect to the root, so visiting mysite.com/folder-1/folder-2 will go to a-totally-different-site.com

Is this possible with .htaccess? I have tried a few different approaches I found, but none are working.

答:
Simple rewrite rule should do:</p>

```<code>RewriteEngine On
RewriteRule ^ http://a-totally-different-site.com [R=301]
</code>```

Body:
I am attempting to redirect a subfolder on an apache2 server to the root of an entirely different domain, without including the path of the folder in the new domain.

So, in mysite.com/folder-1/folder-2 I have a .htaccess file like so:

RedirectMatch 301 / http://a-totally-different-site.com/

This sort of works, but includes the path in the redirect. So, when you visit mysite.com/folder-1/folder-2 it goes to a-totally-different-site.com/folder-1/folder-2

What I wish to happen, is for the subfolder to redirect to the root, so visiting mysite.com/folder-1/folder-2 will go to a-totally-different-site.com

Is this possible with .htaccess? I have tried a few different approaches I found, but none are working.

Body:
Simple rewrite rule should do:

RewriteEngine On
RewriteRule ^ http://a-totally-different-site.com [R=301]


问:
Deprecated constructor for BigDecimal.setScale(int, int) and RoundingMode enums
I'm trying to specify the precision of a BigDecimal value with the following: new BigDecimal(12.99).setScale(2, BigDecimal.ROUND_HALF_EVEN. However, the compiler is telling me that setScale(int, int) is deprecated.

When I've tried to use the RoundingMode enums, it's telling me that the enum may not have been initialised. How do I correctly use the enums to instantiate the BigDecimals with setScale(int, RoundingMode)?

Thanks!

答:
According to the docs, setScale(int, int), has not been recommended since Java 1.5, when enums were first introduced:

The new setScale(int, RoundingMode) method should be used in preference to this legacy method.

It was finally deprecated in Java 9.

You should call setScale(2, RoundingMode.HALF_EVEN) instead. It makes error checking much simpler, since you can't pass in an undefined enum, but you can certainly pass in an integer mode which is undefined.


问:
Webpack writes the absolute path info in bundle when Babel-loader is used
Here is a piece of my webpack config file:
`

// webpack.config.js
  entry: path.resolve(__dirname, 'src/app.js'),
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist/js/'),
    library: 'app',
    publicPath: '/js/',
    pathinfo: NODE_ENV === 'development',
  },
  //...
    module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: [
          {
            loader: 'babel',
          },
          {
            loader: 'eslint',
          },
        ],
      },
      //...
    ],
  },

 // .babelrc
 {
  "presets": [
    [
      "env",
      {
        "targets": {
          "browsers": [
            "last 1 Chrome versions",
            "last 1 Firefox versions",
            "last 1 Edge versions",
            "last 1 iOS versions"
          ]
        },
        "modules": false
      }
    ]
  ],
  "plugins": [
    ["istanbul", {
      "exclude": [
        "./test/unit/**/*.js"
      ]
    }]
  ]
}```

The Problem: I get the full local path to my js file like this:
path: '/Users/username/MyProjects/projectname/src/view/dialog/Messages.js.
Which don't like to have in my production bundle.

I get this only using babel-loader for js files.

I use "webpack": "3.8.1".

Please help!

答:
this might help. </p>

```<code>const path = require('path');
path.join(process.cwd()

ex:
"rxjs/AsyncSubject": path.join(process.cwd(), "/node_modules/rxjs/_esm5/AsyncSubject.js")
</code>```

Body:
Here is a piece of my webpack config file:
`

// webpack.config.js
  entry: path.resolve(__dirname, 'src/app.js'),
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist/js/'),
    library: 'app',
    publicPath: '/js/',
    pathinfo: NODE_ENV === 'development',
  },
  //...
    module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: [
          {
            loader: 'babel',
          },
          {
            loader: 'eslint',
          },
        ],
      },
      //...
    ],
  },

 // .babelrc
 {
  "presets": [
    [
      "env",
      {
        "targets": {
          "browsers": [
            "last 1 Chrome versions",
            "last 1 Firefox versions",
            "last 1 Edge versions",
            "last 1 iOS versions"
          ]
        },
        "modules": false
      }
    ]
  ],
  "plugins": [
    ["istanbul", {
      "exclude": [
        "./test/unit/**/*.js"
      ]
    }]
  ]
}

The Problem: I get the full local path to my js file like this:
path: '/Users/username/MyProjects/projectname/src/view/dialog/Messages.js.
Which don't like to have in my production bundle.

I get this only using babel-loader for js files.

I use "webpack": "3.8.1".

Please help!

Body:
this might help.

const path = require('path');
path.join(process.cwd()

ex:
"rxjs/AsyncSubject": path.join(process.cwd(), "/node_modules/rxjs/_esm5/AsyncSubject.js")


问:
Generate XML file and redirect to other view with Rails
I have a piece of code that generates an XML file. What I want to, and didn't find the solution, is to generate the XML file and ALSO redirect to another page, to give a feedback message.

My code is

```<code>def exportFiles
    @files=FileToExport.getComponentToExport
    recursive_tree= GitHubRepositorioService.getRecursiveTree('master')
    GitHubService.updateFiles(@files, recursive_tree)
    xml = Builder::XmlMarkup.new(:target=>$stdout, :indent=>2)
    respond_to do |format|
     format.xml { send_data render_to_string(:exportFiles), filename: 'exported_module.xml', type: 'application/xml', disposition: 'attachment' }
    end
    FileToExport.setComponentToExport(nil)
  end
</code>```

As I already use "respond_to" I can't use another redirect sentence... so, there is a way to generate (downloading) that file and redirect to other view?

答:
Unfortunately, this is not possible via the controller as you can't send two responses.

But you could do this via javascript for instance. See this topic for more info Rails how do I - export data with send_data then redirect_to a new page?

Body:
I have a piece of code that generates an XML file. What I want to, and didn't find the solution, is to generate the XML file and ALSO redirect to another page, to give a feedback message.

My code is

def exportFiles
    @files=FileToExport.getComponentToExport
    recursive_tree= GitHubRepositorioService.getRecursiveTree('master')
    GitHubService.updateFiles(@files, recursive_tree)
    xml = Builder::XmlMarkup.new(:target=>$stdout, :indent=>2)
    respond_to do |format|
     format.xml { send_data render_to_string(:exportFiles), filename: 'exported_module.xml', type: 'application/xml', disposition: 'attachment' }
    end
    FileToExport.setComponentToExport(nil)
  end

As I already use "respond_to" I can't use another redirect sentence... so, there is a way to generate (downloading) that file and redirect to other view?


问:
Building a map with a list of functions in Clojure
I'm relatively new to Clojure, so I'm having trouble wrapping my mind around how to make the following work.

Starting with a Java method (from the reflect package), I want to extract various properties and end up with a map that looks something like the following:

```<code>{ :name "test-method
  :return-type "String"
  :public true
}
</code>```

Since the logic of building the keys can be rather complex, I'd like to chain a series of functions that take the current map and the method object and either modify the map or return it as is. Something like:

```<code>(defn build-public
[acc method]
(if (is-public? method) (assoc acc :public true) acc))
</code>```

Which might be called like:

```<code>(make-method-map method [build-public build-return-type build-name])
</code>```

I've tried a couple of different approaches but can't seem to make it work, any suggestions would be greatly appreciated.

答:
I like the reduce fns option, but here is an alternative with a bit different function signatures:

```<code>(defn make-name  [method] {:name   (str (:a method) "-" (:b method))})
(defn make-other [method] {:a-is-3 (= (:a method) 3)})

(defn make-method-map [method fns]
  (into {} ((apply juxt fns) method)))

(defn make-method-map [method fns]
  (reduce merge ((apply juxt fns) method)))

(defn make-method-map [method fns]
  (apply merge ((apply juxt fns) method)))

(make-method-map {:a 1 :b 2} [make-name make-other])
; {:name "1-2", :a-is-3 false}
</code>```

juxt returns a vector of function return values, here the acc is not taken as an input argument but instead into or reduce is used to "merge" these hash-maps into the final result. Originally I thought of using comp but these styles of functions aren't that "composable".

Body:
I'm relatively new to Clojure, so I'm having trouble wrapping my mind around how to make the following work.

Starting with a Java method (from the reflect package), I want to extract various properties and end up with a map that looks something like the following:

{ :name "test-method
  :return-type "String"
  :public true
}

Since the logic of building the keys can be rather complex, I'd like to chain a series of functions that take the current map and the method object and either modify the map or return it as is. Something like:

(defn build-public
[acc method]
(if (is-public? method) (assoc acc :public true) acc))

Which might be called like:

(make-method-map method [build-public build-return-type build-name])

I've tried a couple of different approaches but can't seem to make it work, any suggestions would be greatly appreciated.

Body:
I like the reduce fns option, but here is an alternative with a bit different function signatures:

(defn make-name  [method] {:name   (str (:a method) "-" (:b method))})
(defn make-other [method] {:a-is-3 (= (:a method) 3)})

(defn make-method-map [method fns]
  (into {} ((apply juxt fns) method)))

(defn make-method-map [method fns]
  (reduce merge ((apply juxt fns) method)))

(defn make-method-map [method fns]
  (apply merge ((apply juxt fns) method)))

(make-method-map {:a 1 :b 2} [make-name make-other])
; {:name "1-2", :a-is-3 false}

juxt returns a vector of function return values, here the acc is not taken as an input argument but instead into or reduce is used to "merge" these hash-maps into the final result. Originally I thought of using comp but these styles of functions aren't that "composable".


问:
Cannot Crawl Web Page Data Using ScrapySharp
I am facing a technical issue, I browsed several articles to find the answer but I couldn’t get a proper answer from any web site.

I am using ScrapySharp for my project to crawl web page data. This issue came when I try to crawl data from the http://edition.cnn.com/POLITICS website.

Firstly, I loaded the page via IE, and I selected Developer tools to inspect the tags. After the I selected the tag that I need for my code "//div[@class='cd__content']", Moreover when I load the above mentioned web page through ScrapySharp

```<code>ScrapingBrowser browser = new ScrapingBrowser();
WebPage rootPage = browser.NavigateToPageAsync(new Uri(url));
HtmlNodeCollection rootNodes = rootPage.Html.SelectNodes("//div[@class='cd__content']");
</code>```

The result for rootNodes shows as null

When I investigate deep, What I saw is the above-mentioned cd__content is inside the "SECTION" tag when the page loads the “SECTION” tag is empty. But when I Inspect via IE or Chrome all tags are filled with information that’s why I could able to pick the element, but when I load the page programmatically it won’t.
My question is, how can I load the page with filling all information using ScrapySharp.

Experts, Please help on this.

答:
If you analyse the network traffic for the page, you'll see that the javascript makes a number of calls to load content from http://edition.cnn.com/data/ocs/section/politics/index.html for each "content zone" on the page. The response to those requests contains the HTML and content that appears in the page.

You would need to review that and make similar requests yourself, or see if one or more of their RSS feeds met your needs and provided you with a more parse-able set of content - for example: http://rss.cnn.com/rss/cnn_allpolitics.rss

Body:
I am facing a technical issue, I browsed several articles to find the answer but I couldn’t get a proper answer from any web site.

I am using ScrapySharp for my project to crawl web page data. This issue came when I try to crawl data from the http://edition.cnn.com/POLITICS website.

Firstly, I loaded the page via IE, and I selected Developer tools to inspect the tags. After the I selected the tag that I need for my code "//div[@class='cd__content']", Moreover when I load the above mentioned web page through ScrapySharp

ScrapingBrowser browser = new ScrapingBrowser();
WebPage rootPage = browser.NavigateToPageAsync(new Uri(url));
HtmlNodeCollection rootNodes = rootPage.Html.SelectNodes("//div[@class='cd__content']");

The result for rootNodes shows as null

When I investigate deep, What I saw is the above-mentioned cd__content is inside the "SECTION" tag when the page loads the “SECTION” tag is empty. But when I Inspect via IE or Chrome all tags are filled with information that’s why I could able to pick the element, but when I load the page programmatically it won’t.
My question is, how can I load the page with filling all information using ScrapySharp.

Experts, Please help on this.


问:
Run long Oracle stored procedure from PHP
I have a stored procedure which I run from PHP using:

```<code>//Request does not change
$sql = 'BEGIN SP_GET_MY_DATA(:POP, :SEG, :DUR, :VIEW, :PAGE, :OUTPUT_CUR); END;';

//Statement does not change
$stmt = oci_parse($conn,$sql);
oci_bind_by_name($stmt,':POP',$pop);
oci_bind_by_name($stmt,':SEG',$seg);
oci_bind_by_name($stmt,':DUR',$dur);
oci_bind_by_name($stmt,':VIEW',$view);
oci_bind_by_name($stmt,':PAGE',$page);

//But BEFORE statement, Create your cursor
$cursor = oci_new_cursor($conn)

// On your code add the latest parameter to bind the cursor resource to the Oracle argument
oci_bind_by_name($stmt,":OUTPUT_CUR", $cursor,-1,OCI_B_CURSOR);

// Execute the statement as in your first try
oci_execute($stmt);

// and now, execute the cursor
oci_execute($cursor);

// Use OCIFetchinto in the same way as you would with SELECT
while ($data = oci_fetch_assoc($cursor, OCI_RETURN_LOBS )) {
    print_r($data}
}
</code>```

The problem is I have millions of rows and complex logic in stored procedure. When I execute SP_GET_MY_DATA through SQL developer, it takes around 2 hours to complete it.

PHP is timing out when I do it. I cannot increase the max_execution_time in PHP as well.

How can I run this on Oracle or using PHP without timing out? Please help.

答:
I answered how to use Oracle Scheduler to run a long running procedure asynchronously pretty comprehensively in this answer on the DBA stack exchange.  See https://dba.stackexchange.com/a/67913/38772

TL;DR is

```<code>-- submit this as a background job
BEGIN
  dbms_scheduler.create_job (
      job_name => 'MY_BACKGROUND_JOB'
    , job_type => 'STORED_PROCEDURE'
    , job_action => 'SP_GET_MY_DATA'
    , enabled => TRUE
    , auto_drop => TRUE
  );
END;
</code>```

You'll have to do a little more work if you want to pass in parameters to the procedure.  You may find this answer helpful https://dba.stackexchange.com/q/42119/38772/

For additional reference with all the gory details, the relevant chapter from Oracle's documentation is at https://docs.oracle.com/database/121/ADMIN/scheduse.htm

Body:
I have a stored procedure which I run from PHP using:

//Request does not change
$sql = 'BEGIN SP_GET_MY_DATA(:POP, :SEG, :DUR, :VIEW, :PAGE, :OUTPUT_CUR); END;';

//Statement does not change
$stmt = oci_parse($conn,$sql);
oci_bind_by_name($stmt,':POP',$pop);
oci_bind_by_name($stmt,':SEG',$seg);
oci_bind_by_name($stmt,':DUR',$dur);
oci_bind_by_name($stmt,':VIEW',$view);
oci_bind_by_name($stmt,':PAGE',$page);

//But BEFORE statement, Create your cursor
$cursor = oci_new_cursor($conn)

// On your code add the latest parameter to bind the cursor resource to the Oracle argument
oci_bind_by_name($stmt,":OUTPUT_CUR", $cursor,-1,OCI_B_CURSOR);

// Execute the statement as in your first try
oci_execute($stmt);

// and now, execute the cursor
oci_execute($cursor);

// Use OCIFetchinto in the same way as you would with SELECT
while ($data = oci_fetch_assoc($cursor, OCI_RETURN_LOBS )) {
    print_r($data}
}

The problem is I have millions of rows and complex logic in stored procedure. When I execute SP_GET_MY_DATA through SQL developer, it takes around 2 hours to complete it.

PHP is timing out when I do it. I cannot increase the max_execution_time in PHP as well.

How can I run this on Oracle or using PHP without timing out? Please help.

Body:
I answered how to use Oracle Scheduler to run a long running procedure asynchronously pretty comprehensively in this answer on the DBA stack exchange.  See https://dba.stackexchange.com/a/67913/38772

TL;DR is

-- submit this as a background job
BEGIN
  dbms_scheduler.create_job (
      job_name => 'MY_BACKGROUND_JOB'
    , job_type => 'STORED_PROCEDURE'
    , job_action => 'SP_GET_MY_DATA'
    , enabled => TRUE
    , auto_drop => TRUE
  );
END;

You'll have to do a little more work if you want to pass in parameters to the procedure.  You may find this answer helpful https://dba.stackexchange.com/q/42119/38772/

For additional reference with all the gory details, the relevant chapter from Oracle's documentation is at https://docs.oracle.com/database/121/ADMIN/scheduse.htm


问:
Android Proximity Alert not working
I know many guys asked this question but I feel helpless because I tried everything I know but still not working.

I provide my last tried code that worked once after that it doesn't.

```<code>public class ProximityAlertActivity extends Activity {

private static final long POINT_RADIUS = 100; // in Meters
private static final long PROX_ALERT_EXPIRATION = -1; // It will never expire
private static final String PROX_ALERT_INTENT = "com.androidmyway.demo.ProximityAlert";
private LocationManager locationManager;
private EditText latitudeEditText;
private EditText longitudeEditText;
private Button addAlertButton;

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_proxymity);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        latitudeEditText = (EditText) findViewById(R.id.point_latitude);
        longitudeEditText = (EditText) findViewById(R.id.point_longitude);
        addAlertButton = (Button) findViewById(R.id.add_alert_button);

        addAlertButton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                     addProximityAlert();
              }
        });

}

private void addProximityAlert() {

       double latitude = Double.parseDouble(latitudeEditText.getText().toString());
       double longitude = Double.parseDouble(longitudeEditText.getText().toString());
       Intent intent = new Intent(PROX_ALERT_INTENT);
       PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
       locationManager.addProximityAlert(
              latitude, // the latitude of the central point of the alert region
              longitude, // the longitude of the central point of the alert region
              POINT_RADIUS, // the radius of the central point of the alert region, in meters
              PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no                           expiration
              proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
       );
}
</code>```

/****** BroadcastReceiver to display notification ******/

```<code>public class ProximityIntentReceiver extends BroadcastReceiver {
   private static final int NOTIFICATION_ID = 1000;

   @SuppressWarnings("deprecation")
   @Override
   public void onReceive(Context context, Intent intent) {
       String key = LocationManager.KEY_PROXIMITY_ENTERING;
       Boolean entering = intent.getBooleanExtra(key, false);
       if (entering) {
                 Log.d(getClass().getSimpleName(), "entering");
          }else {
                 Log.d(getClass().getSimpleName(), "exiting");
          }
          NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

          Intent notificationIntent = new Intent(context, ProximityAlertActivity.class);
          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
          Notification notification = createNotification();
          notification.setLatestEventInfo(context, "Proximity Alert!", "You are near your point of interest."+key, pendingIntent);

          notificationManager.notify(NOTIFICATION_ID, notification);
  }

   private Notification createNotification() {
          Notification notification = new Notification();
          notification.icon = R.drawable.ic_launcher;
          notification.when = System.currentTimeMillis();
          notification.flags |= Notification.FLAG_AUTO_CANCEL;
          notification.flags |= Notification.FLAG_SHOW_LIGHTS;
          notification.defaults |= Notification.DEFAULT_ALL;
          notification.defaults |= Notification.DEFAULT_LIGHTS;
          notification.ledARGB = Color.WHITE;
          notification.ledOnMS = 1500;
          notification.ledOffMS = 1500;
          return notification;
    }
</code>```

}

/****** Manifestfile ******/

```<code><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidmyway.demo.proxymityalert"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
    <activity
        android:name=".ProximityAlertActivity"
    >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="com.androidmyway.demo.proxymityalert.ProximityIntentReceiver"
              android:exported="false"
              android:enabled="true" >
        <intent-filter>
            <action android:name="com.androidmyway.demo.ProximityAlert" />
        </intent-filter>

    </receiver>

</application>
</code>```

Please give me some working solution. Thanks.

答: Code looks fine don't know how you testing, IF you use any FAKE GPS for testing it will not work.</p>

Title: Android Proximity Alert not working

Body:
I know many guys asked this question but I feel helpless because I tried everything I know but still not working.

I provide my last tried code that worked once after that it doesn't.

public class ProximityAlertActivity extends Activity {

private static final long POINT_RADIUS = 100; // in Meters
private static final long PROX_ALERT_EXPIRATION = -1; // It will never expire
private static final String PROX_ALERT_INTENT = "com.androidmyway.demo.ProximityAlert";
private LocationManager locationManager;
private EditText latitudeEditText;
private EditText longitudeEditText;
private Button addAlertButton;

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_proxymity);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        latitudeEditText = (EditText) findViewById(R.id.point_latitude);
        longitudeEditText = (EditText) findViewById(R.id.point_longitude);
        addAlertButton = (Button) findViewById(R.id.add_alert_button);

        addAlertButton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                     addProximityAlert();
              }
        });

}

private void addProximityAlert() {

       double latitude = Double.parseDouble(latitudeEditText.getText().toString());
       double longitude = Double.parseDouble(longitudeEditText.getText().toString());
       Intent intent = new Intent(PROX_ALERT_INTENT);
       PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
       locationManager.addProximityAlert(
              latitude, // the latitude of the central point of the alert region
              longitude, // the longitude of the central point of the alert region
              POINT_RADIUS, // the radius of the central point of the alert region, in meters
              PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no                           expiration
              proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
       );
}

/****** BroadcastReceiver to display notification ******/

public class ProximityIntentReceiver extends BroadcastReceiver {
   private static final int NOTIFICATION_ID = 1000;

   @SuppressWarnings("deprecation")
   @Override
   public void onReceive(Context context, Intent intent) {
       String key = LocationManager.KEY_PROXIMITY_ENTERING;
       Boolean entering = intent.getBooleanExtra(key, false);
       if (entering) {
                 Log.d(getClass().getSimpleName(), "entering");
          }else {
                 Log.d(getClass().getSimpleName(), "exiting");
          }
          NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

          Intent notificationIntent = new Intent(context, ProximityAlertActivity.class);
          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
          Notification notification = createNotification();
          notification.setLatestEventInfo(context, "Proximity Alert!", "You are near your point of interest."+key, pendingIntent);

          notificationManager.notify(NOTIFICATION_ID, notification);
  }

   private Notification createNotification() {
          Notification notification = new Notification();
          notification.icon = R.drawable.ic_launcher;
          notification.when = System.currentTimeMillis();
          notification.flags |= Notification.FLAG_AUTO_CANCEL;
          notification.flags |= Notification.FLAG_SHOW_LIGHTS;
          notification.defaults |= Notification.DEFAULT_ALL;
          notification.defaults |= Notification.DEFAULT_LIGHTS;
          notification.ledARGB = Color.WHITE;
          notification.ledOnMS = 1500;
          notification.ledOffMS = 1500;
          return notification;
    }

}

/****** Manifestfile ******/

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidmyway.demo.proxymityalert"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
    <activity
        android:name=".ProximityAlertActivity"
    >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="com.androidmyway.demo.proxymityalert.ProximityIntentReceiver"
              android:exported="false"
              android:enabled="true" >
        <intent-filter>
            <action android:name="com.androidmyway.demo.ProximityAlert" />
        </intent-filter>

    </receiver>

</application>

Please give me some working solution. Thanks.


问:
How to create new column from existing column value in R dynamically
i have data frame called df,how to create new column from existing list column data frame.

my data frame.

```<code>Policy             Item

Checked           list(Processed = "Valid", Gmail = "yy@gmail", Information = list(list(Descrption = "T1, R1", VID = "YUY")))

Sample            list(Processed = "Valid", Gmail = "tt@gmail", Information = list(list(Descrption = "D3, Y3", VID = "RT")))

Processed         list(Processed = "Valid", Gmail = "pp@gmail", Information = list(list(Descrption = "Y2, LE", VID = "UIU")))
</code>```

my expected data frame.

```<code>Policy          Processed    Gmail        Descrption  VID

Checked           Valid      yy@gmail       "T1,R1"  "YUY"

Sample            Valid      tt@gmail       "D3,Y3"  "RT"

Processed         Valid      pp@gmail       "Y2,LE"  "UIU"
</code>```

i'm using below code to get my expected dataframe .

```<code>na_if_null <- function(x) if (is.null(x)) NA else x

new_cols <- lapply(
  Filter(is.list, df),
  function(list_col) {
    names_ <- setNames(nm = unique(do.call(c, lapply(list_col, names))))
    lapply(names_, function(name) sapply(list_col, function(x)
      trimws(na_if_null(as.list(x)[[name]]))))
  }
)

res <- do.call(
  data.frame,
  c(
    list(df, check.names = FALSE, stringsAsFactors = FALSE),
    do.call(c, new_cols)
  )
)
</code>```

But i'm getting below Data frame.please help me to done my post.

```<code>Policy       Item                                                                                Item.Processed    Item.Gmail     Item.Information

Checked      list(Processed = "Valid", Gmail = "yy@gmail", Information = list(list(Descrption = "T1, R1", VID = "YUY")))    Processed        yy@gmail      list(Descrption = "T1, R1", VID = "YUY")

Sample       list(Processed = "Valid", Gmail = "tt@gmail", Information = list(list(Descrption = "D3, Y3", VID = "RT")))     Processed        tt@gmail      list(Descrption = "D3, Y3", VID = "RT")

Processed    list(Processed = "Valid", Gmail = "pp@gmail", Information = list(list(Descrption = "Y2, LE", VID = "UIU")))    Processed        pp@gmail      list(Descrption = "Y2, LE", VID = "UIU")
</code>```

dput

```<code>    structure(list(Policy = c("Checked", "Sample", "Processed"), Item = list(
    structure(list(Processed = "Valid", Gmail = "yy@gmail", Information = list(
        structure(list(Descrption = "T1, R1", VID = "YUY"), .Names = c("Descrption",
        "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
    "Gmail", "Information"), class = "data.frame", row.names = 1L),
    structure(list(Processed = "Valid", Gmail = "tt@gmail", Information = list(
        structure(list(Descrption = "D3, Y3", VID = "RT"), .Names = c("Descrption",
        "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
    "Gmail", "Information"), class = "data.frame", row.names = 1L),
    structure(list(Processed = "Valid", Gmail = "pp@gmail", Information = list(
        structure(list(Descrption = "Y2, LE", VID = "UIU"), .Names = c("Descrption",
        "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
    "Gmail", "Information"), class = "data.frame", row.names = 1L))), row.names = c(NA,
3L), class = "data.frame", .Names = c("Policy", "Item"))
</code>```

Sample data frame

```<code>Policy             colval                                 Item

Checked         list(PID="4",Bdetail ="ui,89")      list(Processed = "Valid", Gmail = "yy@gmail", Information = list(list(Descrption = "T1, R1", VID = "YUY")))

Sample          list(PID="7",Bdetail ="ju,78")      list(Processed = "Valid", Gmail = "tt@gmail", Information = list(list(Descrption = "D3, Y3", VID = "RT")))

Processed       list(PID ="8",Bdetail ="nj,45")     list(Processed = "Valid", Gmail = "pp@gmail", Information = list(list(Descrption = "Y2, LE", VID = "UIU")))
</code>```

答:
Easily done with unnest from tidyr:

```<code>library(dplyr)
library(tidyr)

df %>%
  unnest() %>%
  unnest()
</code>```

Result:

```<code>     Policy Processed    Gmail Descrption VID
1   Checked     Valid yy@gmail     T1, R1 YUY
2    Sample     Valid tt@gmail     D3, Y3  RT
3 Processed     Valid pp@gmail     Y2, LE UIU
</code>```

Data:

```<code>df =     structure(list(Policy = c("Checked", "Sample", "Processed"), Item = list(
  structure(list(Processed = "Valid", Gmail = "yy@gmail", Information = list(
    structure(list(Descrption = "T1, R1", VID = "YUY"), .Names = c("Descrption",
                                                                   "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
                                                                                "Gmail", "Information"), class = "data.frame", row.names = 1L),
  structure(list(Processed = "Valid", Gmail = "tt@gmail", Information = list(
    structure(list(Descrption = "D3, Y3", VID = "RT"), .Names = c("Descrption",
                                                                  "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
                                                                                "Gmail", "Information"), class = "data.frame", row.names = 1L),
  structure(list(Processed = "Valid", Gmail = "pp@gmail", Information = list(
    structure(list(Descrption = "Y2, LE", VID = "UIU"), .Names = c("Descrption",
                                                                   "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
                                                                                "Gmail", "Information"), class = "data.frame", row.names = 1L))), row.names = c(NA,
                                                                                3L), class = "data.frame", .Names = c("Policy", "Item"))
</code>```

Note:

Notice I used two passes of unnest because there are two levels of lists in your original dataframe. unnest automatically flattens all lists in the dataframe and reuses the names, but it does not do it recursively, so you will have to have as many unnest as there are list levels.

Body:
i have data frame called df,how to create new column from existing list column data frame.

my data frame.

Policy             Item

Checked           list(Processed = "Valid", Gmail = "yy@gmail", Information = list(list(Descrption = "T1, R1", VID = "YUY")))

Sample            list(Processed = "Valid", Gmail = "tt@gmail", Information = list(list(Descrption = "D3, Y3", VID = "RT")))

Processed         list(Processed = "Valid", Gmail = "pp@gmail", Information = list(list(Descrption = "Y2, LE", VID = "UIU")))

my expected data frame.

Policy          Processed    Gmail        Descrption  VID

Checked           Valid      yy@gmail       "T1,R1"  "YUY"

Sample            Valid      tt@gmail       "D3,Y3"  "RT"

Processed         Valid      pp@gmail       "Y2,LE"  "UIU"

i'm using below code to get my expected dataframe .

na_if_null <- function(x) if (is.null(x)) NA else x

new_cols <- lapply(
  Filter(is.list, df),
  function(list_col) {
    names_ <- setNames(nm = unique(do.call(c, lapply(list_col, names))))
    lapply(names_, function(name) sapply(list_col, function(x)
      trimws(na_if_null(as.list(x)[[name]]))))
  }
)

res <- do.call(
  data.frame,
  c(
    list(df, check.names = FALSE, stringsAsFactors = FALSE),
    do.call(c, new_cols)
  )
)

But i'm getting below Data frame.please help me to done my post.

Policy       Item                                                                                Item.Processed    Item.Gmail     Item.Information

Checked      list(Processed = "Valid", Gmail = "yy@gmail", Information = list(list(Descrption = "T1, R1", VID = "YUY")))    Processed        yy@gmail      list(Descrption = "T1, R1", VID = "YUY")

Sample       list(Processed = "Valid", Gmail = "tt@gmail", Information = list(list(Descrption = "D3, Y3", VID = "RT")))     Processed        tt@gmail      list(Descrption = "D3, Y3", VID = "RT")

Processed    list(Processed = "Valid", Gmail = "pp@gmail", Information = list(list(Descrption = "Y2, LE", VID = "UIU")))    Processed        pp@gmail      list(Descrption = "Y2, LE", VID = "UIU")

dput

    structure(list(Policy = c("Checked", "Sample", "Processed"), Item = list(
    structure(list(Processed = "Valid", Gmail = "yy@gmail", Information = list(
        structure(list(Descrption = "T1, R1", VID = "YUY"), .Names = c("Descrption",
        "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
    "Gmail", "Information"), class = "data.frame", row.names = 1L),
    structure(list(Processed = "Valid", Gmail = "tt@gmail", Information = list(
        structure(list(Descrption = "D3, Y3", VID = "RT"), .Names = c("Descrption",
        "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
    "Gmail", "Information"), class = "data.frame", row.names = 1L),
    structure(list(Processed = "Valid", Gmail = "pp@gmail", Information = list(
        structure(list(Descrption = "Y2, LE", VID = "UIU"), .Names = c("Descrption",
        "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
    "Gmail", "Information"), class = "data.frame", row.names = 1L))), row.names = c(NA,
3L), class = "data.frame", .Names = c("Policy", "Item"))

Sample data frame

Policy             colval                                 Item

Checked         list(PID="4",Bdetail ="ui,89")      list(Processed = "Valid", Gmail = "yy@gmail", Information = list(list(Descrption = "T1, R1", VID = "YUY")))

Sample          list(PID="7",Bdetail ="ju,78")      list(Processed = "Valid", Gmail = "tt@gmail", Information = list(list(Descrption = "D3, Y3", VID = "RT")))

Processed       list(PID ="8",Bdetail ="nj,45")     list(Processed = "Valid", Gmail = "pp@gmail", Information = list(list(Descrption = "Y2, LE", VID = "UIU")))

Body:
Easily done with unnest from tidyr:

library(dplyr)
library(tidyr)

df %>%
  unnest() %>%
  unnest()

Result:

     Policy Processed    Gmail Descrption VID
1   Checked     Valid yy@gmail     T1, R1 YUY
2    Sample     Valid tt@gmail     D3, Y3  RT
3 Processed     Valid pp@gmail     Y2, LE UIU

Data:

df =     structure(list(Policy = c("Checked", "Sample", "Processed"), Item = list(
  structure(list(Processed = "Valid", Gmail = "yy@gmail", Information = list(
    structure(list(Descrption = "T1, R1", VID = "YUY"), .Names = c("Descrption",
                                                                   "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
                                                                                "Gmail", "Information"), class = "data.frame", row.names = 1L),
  structure(list(Processed = "Valid", Gmail = "tt@gmail", Information = list(
    structure(list(Descrption = "D3, Y3", VID = "RT"), .Names = c("Descrption",
                                                                  "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
                                                                                "Gmail", "Information"), class = "data.frame", row.names = 1L),
  structure(list(Processed = "Valid", Gmail = "pp@gmail", Information = list(
    structure(list(Descrption = "Y2, LE", VID = "UIU"), .Names = c("Descrption",
                                                                   "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
                                                                                "Gmail", "Information"), class = "data.frame", row.names = 1L))), row.names = c(NA,
                                                                                3L), class = "data.frame", .Names = c("Policy", "Item"))

Note:

Notice I used two passes of unnest because there are two levels of lists in your original dataframe. unnest automatically flattens all lists in the dataframe and reuses the names, but it does not do it recursively, so you will have to have as many unnest as there are list levels.


答:
The other answers give good guidance regarding rounding modes - but to a certain degree they miss another crucial point. From the javadoc:

The results of this constructor can be somewhat unpredictable.

The whole point is: when you pass a double value, then floating point semantics kick in.

In other words: you intend to use a special, expensive "overhead" object to deal with arbitrary floating point numbers. But you start the thing by basing it on a value that is restricted to Java double semantics.

Go for new BigDecimal("12.99") instead, or new BigDecimal(1299, 2).

Title: Deprecated constructor for BigDecimal.setScale(int, int) and RoundingMode enums

Body:
I'm trying to specify the precision of a BigDecimal value with the following: new BigDecimal(12.99).setScale(2, BigDecimal.ROUND_HALF_EVEN. However, the compiler is telling me that setScale(int, int) is deprecated.

When I've tried to use the RoundingMode enums, it's telling me that the enum may not have been initialised. How do I correctly use the enums to instantiate the BigDecimals with setScale(int, RoundingMode)?

Thanks!


问:
C++ Variadic parameters with out first one
it's any possibility to use a variadic parameters with out specification of first one?

For example:

This code is perfectly fine:

```<code>void something(const char* layoutAndButton, ...)
{
    va_list ap;
    va_start(ap, layoutAndButton);
    std::map<std::string, std::string> data;
    while (*layoutAndButton != '\0') {
        std::string layout = va_arg(ap, const char*);
        ++layoutAndButton;
        std::string button = va_arg(ap, const char*);
        ++layoutAndButton;
        data.insert(std::make_pair(layout, button));
    }
    for (auto const& x : data)
    {
        std::cout << x.first << ':' << x.second << std::endl;
    }
    va_end(ap);
}
</code>```

But I would like to have the something function in this way:

```<code>void something(const char*...)
</code>```

It's any possibility to do something like that? and after that to access the members? if yes, how?

Thanks

答:
As state in comment std::initializer_list seems to do the job

```<code>void something(std::initializer_list<std::pair<std::string, std::string>> layoutAndButtons)
{
    // std::map<std::string, std::string> m(layoutAndButtons); // potentially
    for (auto const& p : layoutAndButtons) {
        std::cout << p.first << ':' << p.second << std::endl;
    }
}
</code>```

or even, if you really need a map:

```<code>void something(const std::map<std::string, std::string>& layoutAndButtons)
    for (auto const& p : layoutAndButtons) {
        std::cout << p.first << ':' << p.second << std::endl;
    }
}
</code>```

With usage similar to:

```<code>something({{"Some", "things"}, {"are", "done"}});
</code>```

If you really want variadic template, I suggest:

```<code>template<typename... Args>
void something(Args... args)
{
    static_assert(sizeof...(Args) % 2 == 0, "wrong number of argument");
    const char* layoutAndButtons[] = {args...};

    std::map<std::string, std::string> m;
    for (auto it = std::begin(layoutAndButtons);
         it != std::end(layoutAndButtons);
         it += 2) {
        auto layout = *it;
        auto button = *(it + 1);
        m.emplace(layout, button);
    }
    for (auto const& p : m)
    {
        std::cout << p.first << ':' << p.second << std::endl;
    }
}
</code>```

Body:
it's any possibility to use a variadic parameters with out specification of first one?

For example:

This code is perfectly fine:

void something(const char* layoutAndButton, ...)
{
    va_list ap;
    va_start(ap, layoutAndButton);
    std::map<std::string, std::string> data;
    while (*layoutAndButton != '\0') {
        std::string layout = va_arg(ap, const char*);
        ++layoutAndButton;
        std::string button = va_arg(ap, const char*);
        ++layoutAndButton;
        data.insert(std::make_pair(layout, button));
    }
    for (auto const& x : data)
    {
        std::cout << x.first << ':' << x.second << std::endl;
    }
    va_end(ap);
}

But I would like to have the something function in this way:

void something(const char*...)

It's any possibility to do something like that? and after that to access the members? if yes, how?

Thanks

Body:
As state in comment std::initializer_list seems to do the job

void something(std::initializer_list<std::pair<std::string, std::string>> layoutAndButtons)
{
    // std::map<std::string, std::string> m(layoutAndButtons); // potentially
    for (auto const& p : layoutAndButtons) {
        std::cout << p.first << ':' << p.second << std::endl;
    }
}

or even, if you really need a map:

void something(const std::map<std::string, std::string>& layoutAndButtons)
    for (auto const& p : layoutAndButtons) {
        std::cout << p.first << ':' << p.second << std::endl;
    }
}

With usage similar to:

something({{"Some", "things"}, {"are", "done"}});

If you really want variadic template, I suggest:

template<typename... Args>
void something(Args... args)
{
    static_assert(sizeof...(Args) % 2 == 0, "wrong number of argument");
    const char* layoutAndButtons[] = {args...};

    std::map<std::string, std::string> m;
    for (auto it = std::begin(layoutAndButtons);
         it != std::end(layoutAndButtons);
         it += 2) {
        auto layout = *it;
        auto button = *(it + 1);
        m.emplace(layout, button);
    }
    for (auto const& p : m)
    {
        std::cout << p.first << ':' << p.second << std::endl;
    }
}


问:
Xamarin.Forms .NET Standard confusing references
I've created two different Xamarin.Forms solutions from templates. The first one is created with Prism template pack (version 2.0.5) the other one is created with the built-in Xamarin template that comes with Visual Studio 2017 (15.5.0 Preview). Both solutions utilize .net standard 2.0 and build fine on all three platforms. Now to the confusing part. As seen in this image:

the Android-project references of the two solutions is quite different. Why don't the first project need all the references of that the second do?

答: Because the Prism Template Pack is using the CopyNuGetImplementations feature of NuGet. This means that your projects do not need to add the references again.  They will automatically get any NuGet references you have added to the main XF project.  Much easier to manage.</p>


问:
RocketChat call webService on android client
since 3 days ago i'm trying to find which file or implementation on android client is for RocketChat REST web services, for example:

```<code>/api/v1/login
</code>```

unfortunately i cant find implementation classes for them to learn that for developing client application, Thanks in advance

答: The currently Android App (version 1.0.25) is not using REST API for authentication (or any other method). It only uses Web Socket for that. Rocket.Chat is working in a Kotlin SDK that will implement this REST API calls: https://github.com/RocketChat/Rocket.Chat.Kotlin.SDK.</p>

Body:
since 3 days ago i'm trying to find which file or implementation on android client is for RocketChat REST web services, for example:

/api/v1/login

unfortunately i cant find implementation classes for them to learn that for developing client application, Thanks in advance


问:
Android-NDK build system (building hello-gl2)
I downloaded and installed Android-NDK on my Archlinux, using this package.

Got no issues building (and running) the sample hello-jni following the guide on this page, but if I try to do the same with hello-gl2 I get some errors; it looks like it cannot find some header files:

```<code>$ ../../ndk-build
/usr/bin/make -f /opt/android-ndk/build/core/build-local.mk
Compile++ thumb  : gl2jni <= gl_code.cpp
/opt/android-ndk/samples/hello-gl2/jni/gl_code.cpp:22:23: error: GLES2/gl2.h: No such file or directory
/opt/android-ndk/samples/hello-gl2/jni/gl_code.cpp:23:26: error: GLES2/gl2ext.h: No such file or directory
--- SNIP ---
</code>```

GLES2/gl2.h and GLES2/gl2ext.h are however present in $(NDK)/platforms/android-4/arch-arm/usr/include/, and it looks like$(NDK)/build/core/setup-toolchain.mk should set such include path.

Has anybody encountered the same issue? How can I compile this sample?

And then, are there other options to easily build Android-NDK applications? This build system seems quite complicated to me, and I'd prefer using cmake to build my applications.

答: You need to use a higher SDK. Level 5 is the minimum to build OpenGL ES 2 code.</p>

ContentLicense: CC BY-SA 2.5

Body:
I downloaded and installed Android-NDK on my Archlinux, using this package.

Got no issues building (and running) the sample hello-jni following the guide on this page, but if I try to do the same with hello-gl2 I get some errors; it looks like it cannot find some header files:

$ ../../ndk-build
/usr/bin/make -f /opt/android-ndk/build/core/build-local.mk
Compile++ thumb  : gl2jni <= gl_code.cpp
/opt/android-ndk/samples/hello-gl2/jni/gl_code.cpp:22:23: error: GLES2/gl2.h: No such file or directory
/opt/android-ndk/samples/hello-gl2/jni/gl_code.cpp:23:26: error: GLES2/gl2ext.h: No such file or directory
--- SNIP ---

GLES2/gl2.h and GLES2/gl2ext.h are however present in $(NDK)/platforms/android-4/arch-arm/usr/include/, and it looks like$(NDK)/build/core/setup-toolchain.mk should set such include path.

Has anybody encountered the same issue? How can I compile this sample?

And then, are there other options to easily build Android-NDK applications? This build system seems quite complicated to me, and I'd prefer using cmake to build my applications.


问:
HLK Studio does not show my driver in selection tab
I am trying to do HLK testing for my own driver by this instruction https://msdn.microsoft.com/ru-ru/library/windows/hardware/dn915002(v=vs.85).aspx , but I have problems.

I can not do step 5 as I do not see my driver in the list of .sys files I can select for testing in HLK Studio selection tab.
My driver is stored on test machine in location C:\MyDriver\mydriver64.sys. It is 64-bit kernel mode driver used by my application for RAM analysis.

What can I do to add my driver to the selection list and run HLK tests for it?

Thank you.

答:
Sometimes HCK Infrastucture Service crashing, but HLK Communication service still works. You need restart HCK Infrastructure service too. On my job, we had a problem with very long start time of this service and Windows throws timeout error for service start. Because of this, service never starts. We solved this problem, by setting bigger time for service start in Windows registry.
Registry key is HKLM\SYSTEM\CurrentControlSet\Control\ServicesPipeTimeout


问:
R: How to create clusters based on row strings
I m trying to create clusters from data based on the string value of each row. I m using the R langage. What I m calling a "cluster" is a big thematic (= family) that can define each keywords. I imagine something autogenearated based on the keyword, maybe by using lemmatization or ngram.

For example both keywords "cloud services" and "the cloud service" should be in the "service" cluster.

Here is my input vector:

```<code>keywords_df <- c("cloud storage", "cloud computing", "google cloud storage", "the cloud service",
        "free cloud storage", "what is cloud computing", "best cloud storage","cloud computing definition",
        "amazon cloud services", "cloud service providers", "cloud services", "google cloud computing", "cloud computing services", "benefits of cloud computing")
</code>```

Here is the expected output dataframe:

```<code>| Keyword                   |  Thematic |
|---------------------------|:---------:|
|cloud storage              |storage  |
|cloud computing            |computing|
|google cloud storage       |storage  |
|the cloud service          |service  |
|free cloud storage         |storage  |
|what is cloud computing    |computing|
|best cloud storage         |storage  |
|cloud computing definition |computing|
|amazon cloud service       |service |
|cloud service providers        |services |
|cloud service              |service |
|google cloud computing     |computing|
|cloud computing services   |service |
|benefits of cloud computing|computing|
</code>```

The goal is to clean up the data in the "keyword" column and auto extract a kind of lemm or ngram.

Here is what I have done for now :

Create the "Thematic" column based on keyword column:

```<code>keywords_df <- mutate(keywords_df,Thematic=Keyword)
keywords_df$Thematic <- as.character(keywords_df$Thematic)
</code>```

Remove Stopwords:

```<code>stopwords_list<-(c("cloud")) #Remove the main word
stopwords <- stopwords(kind = "en")
stopwords <- append(stopwords,stopwords_list)
x  = keywords_df$Thematic
x  =  removeWords(x,stopwords)
keywords_df$Thematic <- x
</code>```

答:
You can check the presence of certain words like storage, computing and service by using grepl(). This way, you can check for the presence of a given word in df:

```<code>fams   <- c("storage", "computing", "service")
family <- rep("emtpy_fam", length(df))

for(fam in fams){
  family[grepl(fam, Keywords)] <- fam
}
cbind(df, family)
#      Keywords                      family
# [1,] "cloud storage"               "storage"
# [2,] "cloud computing"             "computing"
---
#[13,] "cloud computing services"    "service"
#[14,] "benefits of cloud computing" "computing"
</code>```

There are certainly nicer ways of doing this, though

Edit: Nicer way to do it, using the stringr package

```<code>library(stringr)
family <- str_extract(df, pattern="storage|computing|service")
cbind(df, family)
</code>```

Edit2: I see your latest edit, indicating that you are looking for non pre-specified family descriptions. The first method I think of in such a case is Latent Dirichlet Allocation (LDA - not to be confused with Linear Discriminant Analysis, though).

LDA analyzes a corpus of documents and identifies latent topics as a distribution of words (found like terms(lda.output) below) and identifies which document belongs to which topic (found like topic(lda.output) below):

```<code>library(topicmodels)
library(tm)

# Preliminary textmining
corpus <- Corpus(VectorSource(df))
corpus <- tm_map(corpus, removeWords, "cloud")
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, stemDocument)

# Term Frequency matrix
TF <- DocumentTermMatrix(corpus, control = list(weighting = weightTf))

lda.output <- LDA(TF, k=3)
terms(lda.output)
# Topic 1  Topic 2  Topic 3
# "servic" "comput" "storag"

cbind(df, terms(lda.output)[topics(lda.output)])
#            df
#Topic 3 "cloud storage"               "storag"
#Topic 2 "cloud computing"             "comput"
#Topic 3 "google cloud storage"        "storag"
#Topic 1 "cloud services"              "servic"
#Topic 3 "free cloud storage"          "storag"
#Topic 2 "what is cloud computing"     "comput"
#Topic 3 "best cloud storage"          "storag"
#Topic 1 "cloud computing definition"  "servic"
#Topic 1 "amazon cloud services"       "servic"
#Topic 3 "cloud service providers"     "storag"
#Topic 2 "google cloud services"       "comput"
#Topic 2 "google cloud computing"      "comput"
#Topic 1 "cloud computing services"    "servic"
#Topic 2 "benefits of cloud computing" "comput"
</code>```

Final note: If you wish to get "computing" instead of "comput" etc., you should change the stemming part in the text-mining. You can also leave this out, but then "service" and "services" will not be recognised as the same word. You could, however, manually replace "service" with "services" or vice versa.

Body:
I m trying to create clusters from data based on the string value of each row. I m using the R langage. What I m calling a "cluster" is a big thematic (= family) that can define each keywords. I imagine something autogenearated based on the keyword, maybe by using lemmatization or ngram.

For example both keywords "cloud services" and "the cloud service" should be in the "service" cluster.

Here is my input vector:

keywords_df <- c("cloud storage", "cloud computing", "google cloud storage", "the cloud service",
        "free cloud storage", "what is cloud computing", "best cloud storage","cloud computing definition",
        "amazon cloud services", "cloud service providers", "cloud services", "google cloud computing", "cloud computing services", "benefits of cloud computing")

Here is the expected output dataframe:

| Keyword                   |  Thematic |
|---------------------------|:---------:|
|cloud storage              |storage  |
|cloud computing            |computing|
|google cloud storage       |storage  |
|the cloud service          |service  |
|free cloud storage         |storage  |
|what is cloud computing    |computing|
|best cloud storage         |storage  |
|cloud computing definition |computing|
|amazon cloud service       |service |
|cloud service providers        |services |
|cloud service              |service |
|google cloud computing     |computing|
|cloud computing services   |service |
|benefits of cloud computing|computing|

The goal is to clean up the data in the "keyword" column and auto extract a kind of lemm or ngram.

Here is what I have done for now :

Create the "Thematic" column based on keyword column:

keywords_df <- mutate(keywords_df,Thematic=Keyword)
keywords_df$Thematic <- as.character(keywords_df$Thematic)

Remove Stopwords:

stopwords_list<-(c("cloud")) #Remove the main word
stopwords <- stopwords(kind = "en")
stopwords <- append(stopwords,stopwords_list)
x  = keywords_df$Thematic
x  =  removeWords(x,stopwords)
keywords_df$Thematic <- x

Body:
You can check the presence of certain words like storage, computing and service by using grepl(). This way, you can check for the presence of a given word in df:

fams   <- c("storage", "computing", "service")
family <- rep("emtpy_fam", length(df))

for(fam in fams){
  family[grepl(fam, Keywords)] <- fam
}
cbind(df, family)
#      Keywords                      family
# [1,] "cloud storage"               "storage"
# [2,] "cloud computing"             "computing"
---
#[13,] "cloud computing services"    "service"
#[14,] "benefits of cloud computing" "computing"

There are certainly nicer ways of doing this, though

Edit: Nicer way to do it, using the stringr package

library(stringr)
family <- str_extract(df, pattern="storage|computing|service")
cbind(df, family)

Edit2: I see your latest edit, indicating that you are looking for non pre-specified family descriptions. The first method I think of in such a case is Latent Dirichlet Allocation (LDA - not to be confused with Linear Discriminant Analysis, though).

LDA analyzes a corpus of documents and identifies latent topics as a distribution of words (found like terms(lda.output) below) and identifies which document belongs to which topic (found like topic(lda.output) below):

library(topicmodels)
library(tm)

# Preliminary textmining
corpus <- Corpus(VectorSource(df))
corpus <- tm_map(corpus, removeWords, "cloud")
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, stemDocument)

# Term Frequency matrix
TF <- DocumentTermMatrix(corpus, control = list(weighting = weightTf))

lda.output <- LDA(TF, k=3)
terms(lda.output)
# Topic 1  Topic 2  Topic 3
# "servic" "comput" "storag"

cbind(df, terms(lda.output)[topics(lda.output)])
#            df
#Topic 3 "cloud storage"               "storag"
#Topic 2 "cloud computing"             "comput"
#Topic 3 "google cloud storage"        "storag"
#Topic 1 "cloud services"              "servic"
#Topic 3 "free cloud storage"          "storag"
#Topic 2 "what is cloud computing"     "comput"
#Topic 3 "best cloud storage"          "storag"
#Topic 1 "cloud computing definition"  "servic"
#Topic 1 "amazon cloud services"       "servic"
#Topic 3 "cloud service providers"     "storag"
#Topic 2 "google cloud services"       "comput"
#Topic 2 "google cloud computing"      "comput"
#Topic 1 "cloud computing services"    "servic"
#Topic 2 "benefits of cloud computing" "comput"

Final note: If you wish to get "computing" instead of "comput" etc., you should change the stemming part in the text-mining. You can also leave this out, but then "service" and "services" will not be recognised as the same word. You could, however, manually replace "service" with "services" or vice versa.


问:
mysql running total, order by date
I have been trying to do a running total "order by date". The problem is that if you have two entries with the same date, then it shows the same total on both rows.

Table structure is something like this

```<code>sipID|Date|Amount
1|2017-11-07|2
2|2017-11-09|27
3|2017-11-07|24
</code>```

So I know how to do a running total by id like this.

```<code>SELECT `sipID`,DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`,`Amount`,
(SELECT SUM(`Amount`) FROM `salesinvoice_payments` as `Lin` WHERE `Lin`.`sipID`<=`Lout`.`sipID` && `salesinvoice_id`=115) as `Balance`
FROM `salesinvoice_payments` as `Lout`
WHERE `salesinvoice_id`=115
ORDER BY `sipID`
</code>```

But I wanted it to be ordered by date ascending so I did this

```<code>SELECT `sipID`,DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`,`Amount`,
(SELECT SUM(`Amount`) FROM `salesinvoice_payments` as `Lin` WHERE `Lin`.`Date`<=`Lout`.`Date` && `salesinvoice_id`=115) as `Balance`
FROM `salesinvoice_payments` as `Lout`
WHERE `salesinvoice_id`=115
ORDER BY `Date` ASC
</code>```

Now this does work. The problem happens when you have 2 rows with the same date. What happens then is that both rows have the same running total.
eg

```<code>07 November 2017    2.00    24.00
07 November 2017    22.00   24.00
09 November 2017    3.00    27.00
</code>```

What I want is this

```<code>07 November 2017    2.00    2.00
07 November 2017    22.00   24.00
09 November 2017    3.00    27.00
</code>```

Is there a workaround so that I can have a proper running total, and have it ordered by date without it getting the same total on the same date?

EDIT:
@Strawberry:
I have finally worked out how sqlfiddle works, and put this for my original question.
http://www.sqlfiddle.com/#!9/c6dc75/1

答:
Approach using user variables: DEMO with  nod to Juan for setup of tables/data

```<code>SELECT `sipID`
     , DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`
     ,`Amount`
     ,@Bal:=@Bal+`amount` as `Balance`
FROM `salesinvoice_payments` as `Lout`
CROSS JOIN (SELECT @Bal:=0) z --initializes and declares variable in select
WHERE `salesinvoice_id`=115
ORDER BY `Date`, `sipID`
</code>```

Or using pagination

```<code>SELECT *
FROM (SELECT `sipID`
           , DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`
           ,`Amount`
           ,@Bal:=@Bal+`amount` as `Balance`
      FROM `salesinvoice_payments` as `Lout`
      CROSS JOIN (SELECT @Bal:=0) z --initializes and declares variable in select
      WHERE `salesinvoice_id`=115
      ORDER BY `Date`, `sipID`) Z
ORDER BY `DateFormat`, `sipID`
LIMIT 5,10;  -- Retrieve rows 6-15  Skip 5 retrieve next 10.
</code>```

In this approach the variables have already been resolved in set {Z} thus the user variables are not impacted by the Limit/offset.

Example using your SQL Fiddle but we would need more sample data to really see the pagination impact.

Title: mysql running total, order by date

Body:
I have been trying to do a running total "order by date". The problem is that if you have two entries with the same date, then it shows the same total on both rows.

Table structure is something like this

sipID|Date|Amount
1|2017-11-07|2
2|2017-11-09|27
3|2017-11-07|24

So I know how to do a running total by id like this.

SELECT `sipID`,DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`,`Amount`,
(SELECT SUM(`Amount`) FROM `salesinvoice_payments` as `Lin` WHERE `Lin`.`sipID`<=`Lout`.`sipID` && `salesinvoice_id`=115) as `Balance`
FROM `salesinvoice_payments` as `Lout`
WHERE `salesinvoice_id`=115
ORDER BY `sipID`

But I wanted it to be ordered by date ascending so I did this

SELECT `sipID`,DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`,`Amount`,
(SELECT SUM(`Amount`) FROM `salesinvoice_payments` as `Lin` WHERE `Lin`.`Date`<=`Lout`.`Date` && `salesinvoice_id`=115) as `Balance`
FROM `salesinvoice_payments` as `Lout`
WHERE `salesinvoice_id`=115
ORDER BY `Date` ASC

Now this does work. The problem happens when you have 2 rows with the same date. What happens then is that both rows have the same running total.
eg

07 November 2017    2.00    24.00
07 November 2017    22.00   24.00
09 November 2017    3.00    27.00

What I want is this

07 November 2017    2.00    2.00
07 November 2017    22.00   24.00
09 November 2017    3.00    27.00

Is there a workaround so that I can have a proper running total, and have it ordered by date without it getting the same total on the same date?

EDIT:
@Strawberry:
I have finally worked out how sqlfiddle works, and put this for my original question.
http://www.sqlfiddle.com/#!9/c6dc75/1

Body:
Approach using user variables: DEMO with  nod to Juan for setup of tables/data

SELECT `sipID`
     , DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`
     ,`Amount`
     ,@Bal:=@Bal+`amount` as `Balance`
FROM `salesinvoice_payments` as `Lout`
CROSS JOIN (SELECT @Bal:=0) z --initializes and declares variable in select
WHERE `salesinvoice_id`=115
ORDER BY `Date`, `sipID`

Or using pagination

SELECT *
FROM (SELECT `sipID`
           , DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`
           ,`Amount`
           ,@Bal:=@Bal+`amount` as `Balance`
      FROM `salesinvoice_payments` as `Lout`
      CROSS JOIN (SELECT @Bal:=0) z --initializes and declares variable in select
      WHERE `salesinvoice_id`=115
      ORDER BY `Date`, `sipID`) Z
ORDER BY `DateFormat`, `sipID`
LIMIT 5,10;  -- Retrieve rows 6-15  Skip 5 retrieve next 10.

In this approach the variables have already been resolved in set {Z} thus the user variables are not impacted by the Limit/offset.

Example using your SQL Fiddle but we would need more sample data to really see the pagination impact.


问:
Add existing code to existing project using git
I am working on a IOS App, and had an existing project on bitbucket. I then downloaded the project, and started working on it, changing a lot of the files. Now, my question is, how do I connect my local project, to the existing bitbucket project? All I can find is creating a new repository, or connecting from an existing, which I'm afraid will overwrite my changes.

EDIT:
So I did the following and here is what I get.

```<code>git init
git add .
git commit -m "changing stuff"
git remote add origin https.bitbucket.org/myrepository
git push -u origin master
</code>```

and the error I get is,

hint: Updates were rejected because the remote contains work that you do
not have locally. This is usually caused by another repository pushing
to the same ref. You may want to first integrate the remote changes (e.g., 'git pull ...') before pushing again. See the 'Note about fast-forwards' in 'git push --help' for details.

答:
Check for git remote --help.

git remote add command then git push on the remote you added should do the job.

Body:
I am working on a IOS App, and had an existing project on bitbucket. I then downloaded the project, and started working on it, changing a lot of the files. Now, my question is, how do I connect my local project, to the existing bitbucket project? All I can find is creating a new repository, or connecting from an existing, which I'm afraid will overwrite my changes.

EDIT:
So I did the following and here is what I get.

git init
git add .
git commit -m "changing stuff"
git remote add origin https.bitbucket.org/myrepository
git push -u origin master

and the error I get is,

hint: Updates were rejected because the remote contains work that you do
not have locally. This is usually caused by another repository pushing
to the same ref. You may want to first integrate the remote changes (e.g., 'git pull ...') before pushing again. See the 'Note about fast-forwards' in 'git push --help' for details.


问:
create vb project for consuming XML Web Service
I need to implement a project that will consume XML Web Service from other company. A user will retrieve data from this application. They provide a interface layout for the web service with all the functions.

Here I am to trying to create a project with VS2010 as VB project. This application will constantly pull data from the XML web service to sync with the latest data from their server.

I am confusing what types of project that I have to create and add for this whole solution with VS2010. I was thinking create a VB windows form application project first and add WCF service application to this solution.

Please, suggest me to have a right choice.
Thanks.

<---Edit----->
When the data is pulled from the web service and it will be saved in SQL server on our side. When it is updated it will be updated in our SQL server and to their server thru XML Web Service.

答: Right click on any of your projects where you want to reference your webservice. Look for "Add Service Reference" (VS2010), input the url to the WSDL or just the asmx page and you're done!</p>


问:
mysql get error 1064 when i try to prepare from
I trying to find lastday table and export to another folder, code is:

```<code>select @lastday := date_sub(curdate(),interval 1 day);
    SET @datanewname = '_m2_raw_Data';
    SELECT @datalasttablename:=concat(@lastday,@datanewname);
    set @path = ' into outfile ''C:\\Db\\test.csv''' ;
    set @field = ' FIELDS TERMINATED BY '',''' ;
    set @enclosed = ' OPTIONALLY ENCLOSED BY ''"''' ;
    set @lines = ' LINES TERMINATED BY ''\\n''';
    set @select = ' select * from';
    select @query:=concat(@select,@datalasttablename,@path,@field,@enclosed,@lines);
    select @query;
    prepare a from @query;
</code>```

the result of select @query is :

```<code> select * from2017-11-12_m2_raw_Data into outfile 'C:\Db\test.csv'
 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED
 BY '\n'
</code>```

but the error is comes with

Error Code: 1064. You have an error in your SQL syntax; check the
  manual that corresponds to your MariaDB server version for the right
  syntax to use near '@query' at line 1 0.000 sec

答:
This statement is invalid

```<code>prepare a @query;
</code>```

This is what mysql expects

```<code>PREPARE stmt_name FROM preparable_stmt
</code>```

Judging from what I can see, you might be wanting something like this:

```<code>PREPARE a FROM @query;
</code>```

Read more about preparing statements here.

Body:
I trying to find lastday table and export to another folder, code is:

select @lastday := date_sub(curdate(),interval 1 day);
    SET @datanewname = '_m2_raw_Data';
    SELECT @datalasttablename:=concat(@lastday,@datanewname);
    set @path = ' into outfile ''C:\\Db\\test.csv''' ;
    set @field = ' FIELDS TERMINATED BY '',''' ;
    set @enclosed = ' OPTIONALLY ENCLOSED BY ''"''' ;
    set @lines = ' LINES TERMINATED BY ''\\n''';
    set @select = ' select * from';
    select @query:=concat(@select,@datalasttablename,@path,@field,@enclosed,@lines);
    select @query;
    prepare a from @query;

the result of select @query is :

 select * from2017-11-12_m2_raw_Data into outfile 'C:\Db\test.csv'
 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED
 BY '\n'

but the error is comes with

Error Code: 1064. You have an error in your SQL syntax; check the
  manual that corresponds to your MariaDB server version for the right
  syntax to use near '@query' at line 1 0.000 sec

Body:
This statement is invalid

prepare a @query;

This is what mysql expects

PREPARE stmt_name FROM preparable_stmt

Judging from what I can see, you might be wanting something like this:

PREPARE a FROM @query;

Read more about preparing statements here.


问:
How to remove unused css in Rails without getting ActionController::RoutingError?
I got the View called Welcome. I created this manually. And since I don't need css/js files for this view, I do not want to create and put them onto the assets folder. But, I then I got this error:

```<code>Started GET "/javascripts/welcome.js" for ::1 at 2017-11-13 14:55:39 +0000

ActionController::RoutingError (No route matches [GET] "/javascripts/welcome.js"):

actionpack (5.0.0.rc2) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
</code>```

And this error:

```<code>ActionController::RoutingError (No route matches [GET] "/stylesheets/welcome.css"):
  Rendering /Users/ryzal/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc2/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout

actionpack (5.0.0.rc2) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
</code>```

I could make these errors go away by simply creating the welcome.js and welcome.css files and put them on the assets. BUT again, I do want to do this as I know I do not want and never want to use these files.

So how do I solve this?

FYI, my Rails app did not fail. It's all working fine. The errors that I got were appeared on my Terminal. See below:

And here is the only thing in my config/initializer/assets.rb

```<code>Rails.application.config.assets.version = '1.0'
</code>```

答:
There is probably a tidier way, but I would use layouts.

Assign the layout to the pages you want in your controller, and in those layouts require the files that you need:

http://brandonhilkert.com/blog/page-specific-javascript-in-rails/

Body:
I got the View called Welcome. I created this manually. And since I don't need css/js files for this view, I do not want to create and put them onto the assets folder. But, I then I got this error:

Started GET "/javascripts/welcome.js" for ::1 at 2017-11-13 14:55:39 +0000

ActionController::RoutingError (No route matches [GET] "/javascripts/welcome.js"):

actionpack (5.0.0.rc2) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'

And this error:

ActionController::RoutingError (No route matches [GET] "/stylesheets/welcome.css"):
  Rendering /Users/ryzal/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc2/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout

actionpack (5.0.0.rc2) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'

I could make these errors go away by simply creating the welcome.js and welcome.css files and put them on the assets. BUT again, I do want to do this as I know I do not want and never want to use these files.

So how do I solve this?

FYI, my Rails app did not fail. It's all working fine. The errors that I got were appeared on my Terminal. See below:

And here is the only thing in my config/initializer/assets.rb

Rails.application.config.assets.version = '1.0'


问:
Log in using Scrapy not working
I am trying to log in to site using scrapy. I took an example site and checked. It is working for that site. Then i took another site and checked. Its not working. I just changed the urls and ran the code. But not working. What would be the problem?

```<code>    # -*- coding: utf-8 -*-
import scrapy
from scrapy.http import FormRequest
from scrapy.utils.response import open_in_browser

class QuoteSpider(scrapy.Spider):
    name = 'Quote'
    allowed_domains = ["quotes.toscrape.com"]
    start_urls = (
        'http://quotes.toscrape.com/login',
    )

    def parse(self, response):
        token=response.xpath('//input[@name="csrf_token"]/@value').extract_first()

        return FormRequest.from_response(response,formdata={'csrf_token':token,'password':'foo','username':'foo'},callback=self.scape_home_page)

    def scape_home_page(self, response):
        open_in_browser(response)
</code>```

This is working fine. THe other one is not.

```<code>    # -*- coding: utf-8 -*-
import scrapy
from scrapy.http import FormRequest
from scrapy.utils.response import open_in_browser

class BucketsSpider(scrapy.Spider):
    name = 'buckets'
    allowed_domains = ['http://collegekart.in/login']
    start_urls = ['http://collegekart.in/login/']

    def parse(self, response):
        token=response.xpath('//meta[@name="csrf-token"]/@content').extract_first()
        print(token)
        return FormRequest.from_response(response,formdata={'csrf-token':token,'password':'*********','username':'**************'},callback=self.scape_home_page)

    def scape_home_page(self, response):
        open_in_browser(response)
        print("yes")
</code>```

This is not working. The response is not opening in browser. Please suggest me some ways to solve this.

答: I think the problem is with your allowed_domains = ['http://collegekart.in/login'] please try changing it to allowed_domains = ['collegekart.in'] the other response is probably getting filtered.

Title: Log in using Scrapy not working

Body:
I am trying to log in to site using scrapy. I took an example site and checked. It is working for that site. Then i took another site and checked. Its not working. I just changed the urls and ran the code. But not working. What would be the problem?

    # -*- coding: utf-8 -*-
import scrapy
from scrapy.http import FormRequest
from scrapy.utils.response import open_in_browser

class QuoteSpider(scrapy.Spider):
    name = 'Quote'
    allowed_domains = ["quotes.toscrape.com"]
    start_urls = (
        'http://quotes.toscrape.com/login',
    )

    def parse(self, response):
        token=response.xpath('//input[@name="csrf_token"]/@value').extract_first()

        return FormRequest.from_response(response,formdata={'csrf_token':token,'password':'foo','username':'foo'},callback=self.scape_home_page)

    def scape_home_page(self, response):
        open_in_browser(response)

This is working fine. THe other one is not.

    # -*- coding: utf-8 -*-
import scrapy
from scrapy.http import FormRequest
from scrapy.utils.response import open_in_browser

class BucketsSpider(scrapy.Spider):
    name = 'buckets'
    allowed_domains = ['http://collegekart.in/login']
    start_urls = ['http://collegekart.in/login/']

    def parse(self, response):
        token=response.xpath('//meta[@name="csrf-token"]/@content').extract_first()
        print(token)
        return FormRequest.from_response(response,formdata={'csrf-token':token,'password':'*********','username':'**************'},callback=self.scape_home_page)

    def scape_home_page(self, response):
        open_in_browser(response)
        print("yes")

This is not working. The response is not opening in browser. Please suggest me some ways to solve this.


问:
Getting Access-Control-Allow-Headers in preflight error
I have created angular js app in which I have integrate twitch api , the api is

```<code> return $http({
        url: "https://api.twitch.tv/kraken/streams",
        method: "GET",
        params: { channel: channel, limit: 1 },
        headers: { "Client-Id": "XXXXXXXXXXXXXXX" }
    })
</code>```

the problem is when I reload the page the api is working but when my state changes without page reload I am getting cross origin error from this api.
the error is

Failed to load https://api.twitch.tv/kraken/streams?channel=eliazOne&limit=1: Request header field RefreshToken is not allowed by Access-Control-Allow-Headers in preflight response.

anyone has idea how to resolve cross error

答:
I don't know if this is something Angular does on its own by default or if it's a bug elsewhere in your code, but you or Angular is sending a RefreshToken header as part of your failing request, which is not allowed per Access-Control-Allow-Headers response header in the pre-flight OPTIONS request.

```<code>$ curl -XOPTIONS https://api.twitch.tv/kraken/streams -is | grep Headers
Access-Control-Allow-Headers: Accept, Accept-Language, Authorization, Client-Id, Twitch-Api-Token, X-Forwarded-Proto, X-Requested-With, X-Csrf-Token, Content-Type, X-Device-Id
</code>```

This is something all cross-origin requests do. The browser sends an OPTIONS request to make sure the real request it's about to make is allowed by the criteria set by the cross-origin server.

Body:
I have created angular js app in which I have integrate twitch api , the api is

 return $http({
        url: "https://api.twitch.tv/kraken/streams",
        method: "GET",
        params: { channel: channel, limit: 1 },
        headers: { "Client-Id": "XXXXXXXXXXXXXXX" }
    })

the problem is when I reload the page the api is working but when my state changes without page reload I am getting cross origin error from this api.
the error is

Failed to load https://api.twitch.tv/kraken/streams?channel=eliazOne&limit=1: Request header field RefreshToken is not allowed by Access-Control-Allow-Headers in preflight response.

anyone has idea how to resolve cross error

Body:
I don't know if this is something Angular does on its own by default or if it's a bug elsewhere in your code, but you or Angular is sending a RefreshToken header as part of your failing request, which is not allowed per Access-Control-Allow-Headers response header in the pre-flight OPTIONS request.

$ curl -XOPTIONS https://api.twitch.tv/kraken/streams -is | grep Headers
Access-Control-Allow-Headers: Accept, Accept-Language, Authorization, Client-Id, Twitch-Api-Token, X-Forwarded-Proto, X-Requested-With, X-Csrf-Token, Content-Type, X-Device-Id

This is something all cross-origin requests do. The browser sends an OPTIONS request to make sure the real request it's about to make is allowed by the criteria set by the cross-origin server.