问:
Error "error Couldn't find package "XXX" when building project on CI pipeline and when trying to add packages via Yarn
After returning to our project after the weekend my team was met with the error "error Couldn't find package "3d-view@^2.0.0" required by "gl-plot3d@^2.4.2" on the "npm" registry." on our CI pipeline during the install phase. Additionally, this error occurred when trying to add packages with yarn, terminating the process.

This error is happening on the front-end side of our project and doesn't show up upon starting it normally via yarn start. On the last push before the weekend everything went normal without any errors.

Log of our CI job starting at the install command:

```<code>$ yarn install
 yarn install v1.17.3
 info No lockfile found.
 [1/4] Resolving packages...
 warning @material-ui/core > popper.js@1.16.1: You can find the new Popper v2 at @popperjs/core, this package is dedicated to the legacy v1
 warning moments > myconf > babel > babel-core > minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
 warning moments > myconf > babel > babel-core > core-js@1.2.7: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3.
 warning plotly.js > regl-splom > left-pad@1.3.0: use String.prototype.padStart()
 warning plotly.js > ndarray-fill > cwise > static-module > through2 > xtend > object-keys@0.4.0:
 warning plotly.js > point-cluster > bubleify > buble > os-homedir@2.0.0: This is not needed anymore. Use `require('os').homedir()` instead.
 error Couldn't find package "3d-view@^2.0.0" required by "gl-plot3d@^2.4.2" on the "npm" registry.
 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
</code>```

Our .gitlab-ci.yml code:

```<code>stages:
  - build
#  - test
#  - deploy

before_script:
#  - echo `pwd` # debug
#  - echo "$CI_BUILD_NAME, $CI_COMMIT_REF_NAME $CI_BUILD_STAGE" # debug
  - export GRADLE_USER_HOME=`pwd`/.gradle
  - unset CI

cache:
  paths:
    - .gradle/wrapper
    - .gradle/caches

build backend:
  image: gradle:6.1-jdk8
  stage: build
  script:
    - cd backend
    - gradle war

  artifacts:
    paths:
      - backend/build/libs/*.jar
    expire_in: 1 week

build frontend:
  image: node:10.16.3
  stage: build
  script:
    - cd frontend
    - yarn install
    - export NODE_OPTIONS=--max_old_space_size=4096
    - yarn build
</code>```

Dependencies in package.json:

```<code> "dependencies": {
    "@material-ui/core": "^4.9.0",
    "@material-ui/icons": "^4.5.1",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "axios": "^0.19.2",
    "moments": "^0.0.2",
    "plotly.js": "^1.52.1",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-plotly.js": "^2.4.0",
    "react-scripts": "3.3.0"
  }
</code>```

We are clueless of how this could happen, as nobody pushed anything to the branch in the meantime and was noticed when a team member was pushing a cleaned up version of our code and another was trying to install a new package via Yarn.

答:
It seems the issue has been addressed and fixed already but here are some viable solutions found to other answers and our own research in case this happens again:

1) Add 3d-view to yarn.lock before installing it and adding Plotly.js or any other packages:

```<code>    "3d-view@^2.0.0":
  version "2.0.0"
  resolved "https://registry.yarnpkg.com/3d-view/-/3d-view-2.0.0.tgz#831ae942d7508c50801e3e06fafe1e8c574e17be"
  integrity sha1-gxrpQtdQjFCAHj4G+v4ejFdOF74=
  dependencies:
    matrix-camera-controller "^2.1.1"
    orbit-camera-controller "^4.0.0"
    turntable-camera-controller "^3.0.0"
</code>```

2) Manually install the package with a registry added:

```<code>yarn add 3d-view@2.0.0 --registry https://yarn.npmjs.org
</code>```

Thanks to the user masseyb

```<code>npm i git://github.com/mikolalysenko/3d-view
</code>```

Thanks to the user Diogo Falcão

Body:
After returning to our project after the weekend my team was met with the error "error Couldn't find package "3d-view@^2.0.0" required by "gl-plot3d@^2.4.2" on the "npm" registry." on our CI pipeline during the install phase. Additionally, this error occurred when trying to add packages with yarn, terminating the process.

This error is happening on the front-end side of our project and doesn't show up upon starting it normally via yarn start. On the last push before the weekend everything went normal without any errors.

Log of our CI job starting at the install command:

$ yarn install
 yarn install v1.17.3
 info No lockfile found.
 [1/4] Resolving packages...
 warning @material-ui/core > popper.js@1.16.1: You can find the new Popper v2 at @popperjs/core, this package is dedicated to the legacy v1
 warning moments > myconf > babel > babel-core > minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
 warning moments > myconf > babel > babel-core > core-js@1.2.7: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3.
 warning plotly.js > regl-splom > left-pad@1.3.0: use String.prototype.padStart()
 warning plotly.js > ndarray-fill > cwise > static-module > through2 > xtend > object-keys@0.4.0:
 warning plotly.js > point-cluster > bubleify > buble > os-homedir@2.0.0: This is not needed anymore. Use `require('os').homedir()` instead.
 error Couldn't find package "3d-view@^2.0.0" required by "gl-plot3d@^2.4.2" on the "npm" registry.
 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

Our .gitlab-ci.yml code:

stages:
  - build
#  - test
#  - deploy

before_script:
#  - echo `pwd` # debug
#  - echo "$CI_BUILD_NAME, $CI_COMMIT_REF_NAME $CI_BUILD_STAGE" # debug
  - export GRADLE_USER_HOME=`pwd`/.gradle
  - unset CI

cache:
  paths:
    - .gradle/wrapper
    - .gradle/caches

build backend:
  image: gradle:6.1-jdk8
  stage: build
  script:
    - cd backend
    - gradle war

  artifacts:
    paths:
      - backend/build/libs/*.jar
    expire_in: 1 week

build frontend:
  image: node:10.16.3
  stage: build
  script:
    - cd frontend
    - yarn install
    - export NODE_OPTIONS=--max_old_space_size=4096
    - yarn build

Dependencies in package.json:

 "dependencies": {
    "@material-ui/core": "^4.9.0",
    "@material-ui/icons": "^4.5.1",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "axios": "^0.19.2",
    "moments": "^0.0.2",
    "plotly.js": "^1.52.1",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-plotly.js": "^2.4.0",
    "react-scripts": "3.3.0"
  }

We are clueless of how this could happen, as nobody pushed anything to the branch in the meantime and was noticed when a team member was pushing a cleaned up version of our code and another was trying to install a new package via Yarn.

Body:
It seems the issue has been addressed and fixed already but here are some viable solutions found to other answers and our own research in case this happens again:

1) Add 3d-view to yarn.lock before installing it and adding Plotly.js or any other packages:

    "3d-view@^2.0.0":
  version "2.0.0"
  resolved "https://registry.yarnpkg.com/3d-view/-/3d-view-2.0.0.tgz#831ae942d7508c50801e3e06fafe1e8c574e17be"
  integrity sha1-gxrpQtdQjFCAHj4G+v4ejFdOF74=
  dependencies:
    matrix-camera-controller "^2.1.1"
    orbit-camera-controller "^4.0.0"
    turntable-camera-controller "^3.0.0"

2) Manually install the package with a registry added:

yarn add 3d-view@2.0.0 --registry https://yarn.npmjs.org

Thanks to the user masseyb

npm i git://github.com/mikolalysenko/3d-view

Thanks to the user Diogo Falcão


来源: stackexchange

问:
How to resolve eta 0:00:01Killed?
I was downloading torch and after 99% completion of the download, I am getting this error.
I am using Ubuntu, on an 8gb ram system.

the memory allocation

```<code>ankita@ankita-HP-Laptop-15-bs0xx:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:           7.7G        2.0G        1.6G        495M        4.1G        5.0G
Swap:            0B          0B          0B
</code>```

答:
I've also experienced this problem due to lack of RAM. You may try increasing the swap space. Try methods mentioned here.

The best solution is to install pytorch using conda, rather than pip. You won't encounter memory error then.

ContentLicense: CC BY-SA 4.0

Title: How to resolve eta 0:00:01Killed?

Body:
I was downloading torch and after 99% completion of the download, I am getting this error.
I am using Ubuntu, on an 8gb ram system.

the memory allocation

ankita@ankita-HP-Laptop-15-bs0xx:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:           7.7G        2.0G        1.6G        495M        4.1G        5.0G
Swap:            0B          0B          0B


问:
PHPFOX : Which function finally generate password in https://github.com/Criotin/phpfox/blob/master/PF.Src/Core/Hash.php
Can anyone tell me which function finally we should call to generate encrypted password from bellow link?

https://github.com/Criotin/phpfox/blob/master/PF.Src/Core/Hash.php

For example my password is " gauravjain "

how and which function i should use to finally generate encrypted password to save in dataase? I want to migrate old custom users to phpfox v4

can anyone give example code here using my example passowrd

答:
```<code>$password = (new Core\Hash())->make($aVals['password']);
</code>```

I hope this is the code you need.

Body:
$password = (new Core\Hash())->make($aVals['password']);

I hope this is the code you need.


问:
navigationBarTitle works on iOS not MacOS apps
the following code compiles and runs just fine on iOS and Catalyst.  But when I run it in a macOS app it get an error.  Is this a known bug or am I doing something wrong.  macOS Catalina version 10.15.3, Xcode version 11.3.1.

```<code>import SwiftUI

struct ContentView: View {
    var body: some View {

        NavigationView {
            List {
                Text("Item #1")
                Text("Item #2")
            }
            .navigationBarTitle("Title", displayMode: .inline)
            .navigationBarItems(leading: Text("Add"))
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
</code>```

here is the error that I get,

Value of type 'List<Never, TupleView<(Text, Text)>>' has no member 'navigationBarTitle'

答:
Title bar is not available on macOS. Here is interface declaration:

```<code>@available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)
@available(OSX, unavailable)
extension View {

    /// Hides the navigation bar for this view.
    ///
    /// This modifier only takes effect when this view is inside of and visible
    /// within a `NavigationView`.
    ///
    /// - Parameters:
    ///     - hidden: A Boolean value that indicates whether to hide the
    ///       navigation bar.
    @available(OSX, unavailable)
    public func navigationBarHidden(_ hidden: Bool) -> some View

    /// Configures the title in the navigation bar for this view.
    ///
    /// This modifier only takes effect when this view is inside of and visible
    /// within a `NavigationView`.
    ///
    /// - Parameters:
    ///     - title: A description of this view to display in the navigation
    ///       bar.
    @available(OSX, unavailable)
    public func navigationBarTitle(_ title: Text) -> some View

    /// Configures the title in the navigation bar for this view.
    ///
    /// This modifier only takes effect when this view is inside of and visible
    /// within a `NavigationView`.
    ///
    /// - Parameters:
    ///     - titleKey: A key to a localized description of this view to display
    ///       in the navigation bar.
    @available(OSX, unavailable)
    public func navigationBarTitle(_ titleKey: LocalizedStringKey) -> some View
</code>```

Body:
the following code compiles and runs just fine on iOS and Catalyst.  But when I run it in a macOS app it get an error.  Is this a known bug or am I doing something wrong.  macOS Catalina version 10.15.3, Xcode version 11.3.1.

import SwiftUI

struct ContentView: View {
    var body: some View {

        NavigationView {
            List {
                Text("Item #1")
                Text("Item #2")
            }
            .navigationBarTitle("Title", displayMode: .inline)
            .navigationBarItems(leading: Text("Add"))
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

here is the error that I get,

Value of type 'List<Never, TupleView<(Text, Text)>>' has no member 'navigationBarTitle'

Body:
Title bar is not available on macOS. Here is interface declaration:

@available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)
@available(OSX, unavailable)
extension View {

    /// Hides the navigation bar for this view.
    ///
    /// This modifier only takes effect when this view is inside of and visible
    /// within a `NavigationView`.
    ///
    /// - Parameters:
    ///     - hidden: A Boolean value that indicates whether to hide the
    ///       navigation bar.
    @available(OSX, unavailable)
    public func navigationBarHidden(_ hidden: Bool) -> some View

    /// Configures the title in the navigation bar for this view.
    ///
    /// This modifier only takes effect when this view is inside of and visible
    /// within a `NavigationView`.
    ///
    /// - Parameters:
    ///     - title: A description of this view to display in the navigation
    ///       bar.
    @available(OSX, unavailable)
    public func navigationBarTitle(_ title: Text) -> some View

    /// Configures the title in the navigation bar for this view.
    ///
    /// This modifier only takes effect when this view is inside of and visible
    /// within a `NavigationView`.
    ///
    /// - Parameters:
    ///     - titleKey: A key to a localized description of this view to display
    ///       in the navigation bar.
    @available(OSX, unavailable)
    public func navigationBarTitle(_ titleKey: LocalizedStringKey) -> some View


问:
MahApps MetroWindow Topmost property Binding issue
I am using MahApps for my WPF application and when the metro window is used, the TopMost property is not able to bind. If i set it as a static value it works though.

```<code><Controls:MetroWindow x:Class="SabreContractSearch.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Topmost= "{Binding Pin}" Title="{Binding Title}" Height="350" Width="525" Name="FrmMainWindow" >
</code>```

Pin is a boolean property here.

If i remove the metro window and use the normal window tag for WPF, it works. Anything additionally need to add for metro window to work?

答:
It was a bug in ControlzEx v4.2.1. There is a Topmost hack which doesn't set the Topmost property of the Window using the SetCurrentValue.

WPF0041 Set mutable dependency properties using SetCurrentValue.

Setting the value of dependency properties using the CLR accessor calls SetValue(Property, value). This will kill one-way bindings on the property. This rule is probably most relevant in library code where accidentally setting a value can be a hard to track down bug.

Body:
I am using MahApps for my WPF application and when the metro window is used, the TopMost property is not able to bind. If i set it as a static value it works though.

<Controls:MetroWindow x:Class="SabreContractSearch.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Topmost= "{Binding Pin}" Title="{Binding Title}" Height="350" Width="525" Name="FrmMainWindow" >

Pin is a boolean property here.

If i remove the metro window and use the normal window tag for WPF, it works. Anything additionally need to add for metro window to work?


问:
Life cycle transform in hooks for a functional component
I have this componentWillReceiveProps life cycle in my code and I want to write it for a functional component. As I saw, this is possible only with React Hooks. The problem is I did not understood the very well and I need some help.

So, how would be this written in a functional component?

I saw some examples, but not exactly like this case.

```<code>componentWillReceiveProps = (newProps) => {
    const apiData = newProps.apiData;

    if (apiData.articles) {
        this.setState(() => ({
            pageLoading: false,
            articles: apiData.articles.articles,
        }), () => {
            //this.filterDisplayedArticles()
        })
    } else if (apiData.articleSearch && apiData.articleSearch.success) {
        let articles = apiData.articleSearch.articles;
        this.setState(() => ({
            pageLoading: false,
            articles: articles
        }))
    }
}
</code>```

答:
you can use the useEffect hook here to apply a change based on the parameters needed

And use the useState hook to track your state

```<code>import React, { useEffect, useState } from "react";

function DoSomething({ apiData }) {
  const { articles, articleSearch } = apiData;
  const { state, setState } = useState({ pageLoading: true, articles: [] });

  useEffect(() => {
    if (articles) {
      setState({
        pageLoading: false,
        articles: apiData.articles.articles
      });
    } else if (articleSearch && articleSearch.success) {
      setState({
        pageLoading: false,
        articles: articleSearch.articles
      });
    }
  }, [articles, articleSearch]);

  return <div>I'm {state.pageLoading ? "loading" : "done loading"}</div>;
}
</code>```

Play with it live :)

Body:
I have this componentWillReceiveProps life cycle in my code and I want to write it for a functional component. As I saw, this is possible only with React Hooks. The problem is I did not understood the very well and I need some help.

So, how would be this written in a functional component?

I saw some examples, but not exactly like this case.

componentWillReceiveProps = (newProps) => {
    const apiData = newProps.apiData;

    if (apiData.articles) {
        this.setState(() => ({
            pageLoading: false,
            articles: apiData.articles.articles,
        }), () => {
            //this.filterDisplayedArticles()
        })
    } else if (apiData.articleSearch && apiData.articleSearch.success) {
        let articles = apiData.articleSearch.articles;
        this.setState(() => ({
            pageLoading: false,
            articles: articles
        }))
    }
}

Body:
you can use the useEffect hook here to apply a change based on the parameters needed

And use the useState hook to track your state

import React, { useEffect, useState } from "react";

function DoSomething({ apiData }) {
  const { articles, articleSearch } = apiData;
  const { state, setState } = useState({ pageLoading: true, articles: [] });

  useEffect(() => {
    if (articles) {
      setState({
        pageLoading: false,
        articles: apiData.articles.articles
      });
    } else if (articleSearch && articleSearch.success) {
      setState({
        pageLoading: false,
        articles: articleSearch.articles
      });
    }
  }, [articles, articleSearch]);

  return <div>I'm {state.pageLoading ? "loading" : "done loading"}</div>;
}

Play with it live :)


问:
Is this Ruby code using threads, thread pools, and concurrency correctly
I am what I now consider part 3 of completing a task of pinging a very large list of URLs (which number in the thousands) and retrieving a URL's x509 certificate associated with it. Part 1 is here (How do I properly use threads to ping a URL) and Part 2 is here (Why won't my connection pool implement my thread code).

Since I asked these two questions, I have now ended up with the following code:

```<code>###### This is the code that pings a url and grabs its x509 cert #####

class SslClient
  attr_reader :url, :port, :timeout

  def initialize(url, port = '443')
    @url = url
    @port = port
  end

  def ping_for_certificate_info
    context = OpenSSL::SSL::SSLContext.new
    tcp_client = TCPSocket.new(url, port)
    ssl_client = OpenSSL::SSL::SSLSocket.new tcp_client, context
    ssl_client.hostname = url
    ssl_client.sync_close = true
    ssl_client.connect
    certificate = ssl_client.peer_cert
    verify_result = ssl_client.verify_result
    tcp_client.close
    {certificate: certificate, verify_result: verify_result }
  rescue => error
    {certificate: nil, verify_result: nil }
  end
end
</code>```

The above code is paramount that I retrieve the ssl_client.peer_cert. Below I have the following code that is the snippet that makes multiple HTTP pings to URLs for their certs:

```<code>  pool = Concurrent::CachedThreadPool.new
  pool.post do
    [LARGE LIST OF URLS TO PING].each do |struct|
       ssl_client = SslClient.new(struct.domain.gsub("*.", "www."), struct.scan_port)
       cert_info = ssl_client.ping_for_certificate_info
       struct.x509_cert = cert_info[:certificate]
       struct.verify_result = cert_info[:verify_result]
     end
   end

   pool.shutdown
   pool.wait_for_termination

   #Do some rails code with the database depending on the results.
</code>```

So far when I run this code, it is unbelievably slow. I thought that by creating a thread pool with threads, the code would go much faster. That doesn't seem the case and I'm not sure why. A lot of it was because I didn't know the nuances of threads, pools, starvation, locks, etc. However, after implementing the above code, I read some more to try to speed it up and once again I'm confused and could use some clarification as to how I can make the code faster.

For starters, in this excellent article here (ruby-concurrency-parallelism) . We get the following definitions and concepts:

Concurrency vs. Parallelism
  These terms are used loosely, but they do have distinct meanings.

Concurrency: The art of doing many tasks, one at a time. By switching
  between them quickly, it may appear to the user as though they happen
  simultaneously. Parallelism: Doing many tasks at literally the same
  time. Instead of appearing simultaneous, they are simultaneous.
  Concurrency is most often used for applications that are IO heavy. For
  example, a web app may regularly interact with a database or make lots
  of network requests. By using concurrency, we can keep our application
  responsive, even while we wait for the database to respond to our
  query.

This is possible because the Ruby VM allows other threads to run while
  one is waiting during IO. Even if a program has to make dozens of
  requests, if we use concurrency, the requests will be made at
  virtually the same time.

Parallelism, on the other hand, is not currently supported by Ruby.

So from this piece of the article, I understand that what I want to do needs to be done concurrently because I am pinging URLs on the network and that Parallelism is not currently supported by Ruby.

Next is where things get confused for me. From my part 1 question on Stack Overflow, I learned the following in a comment given to me that I should do the following:

Use a thread pool; don't just create a thousand concurrent threads. For something like
  connecting to a URL where there will be a lot of waiting you can
  oversubscribe the number of threads per CPU core, but not by a huge
  amount. You'll have to experiment.

Another user says this:

You'd not spawn thousands of threads, use a connection pool
  (e.g https://github.com/mperham/connection_pool) so you have maximum
  20-30 concurrent requests going (this maximum number should be
  determined by testing at which point network performance drops and you
  get these timeouts)

So for this part, I turned to concurrent-ruby and implemented both a CachedThreadPool and a FixedThreadPool with10 threads. I chose a `CachedThreadPool because it seemed to me that the number of threads needed would be taken care of for me by the Threadpool. Now in concurrent ruby's documentation for a pool, I see this:

```<code>pool = Concurrent::CachedThreadPool.new
pool.post do
  # some parallel work
end
</code>```

I thought we just established in the first article that parallelism is not supported in Ruby, so what is the thread pool doing? Is it working concurrently or in parallel? What exactly is going on? Do I need a thread pool or not? Also at this point in time I thought connection pools and thread pools were the same just used interchangeably. What is the difference between the two pools and which one do I need?

In another excellent article How to Perform Concurrent HTTP Requests in Ruby and Rails, this article introduces the Concurrent::Promises class form concurrent ruby to avoid locks and have thread safety with two api calls. Here is a snippet of code below with the following description:

```<code>def get_all_conversations
  groups_thread = Thread.new do
    get_groups_list
  end

  channels_thread = Thread.new do
    get_channels_list
  end

  [groups_thread, channels_thread].map(&:value).flatten
end
</code>```

Every request is executed it its own thread, which can run in parallel because it is a blocking I/O. But can you see a catch here?

In the above code, another mention of parallelism which we just said didn't exist in ruby. Below is the approach with Concurrent::Promise

```<code>def get_all_conversations
  groups_promise = Concurrent::Promise.execute do
    get_groups_list
  end

  channels_promise = Concurrent::Promise.execute do
    get_channels_list
  end

  [groups_promise, channels_promise].map(&:value!).flatten
end
</code>```

So according to this article, these requests are being made 'in parallel'. Are we still talking about concurrency at this point?

Finally, in these two articles, they talk about using Futures for concurrent http requests. I won't go into the details but I'll paste the links here.

1.Using Concurrent Ruby in a Ruby on Rails Application
2. Learn Concurrency by Implementing Futures in Ruby

Again, what's talked about in the article looks to me like the Concurrent::Promise functionality. I just want to note that the examples show how to use the concepts for two different API calls that need to be combined together. This is not what I need. I just need to make thousands of API calls fast and log the results.

In conclusion, I just want to know what I need to do to make my code faster and thread safe to make it run concurrently. What exactly am I missing to make the code go faster because right now it is going so slow that I might as well not have used threads in the first place.

Summary

I have to ping thousands of URLs using threads to speed up the process. The code is slow and I am confused if I am using threads, thread pools, and concurrency correctly.

答:
Let us look at the problems you have described and try to solve these one at a time:

You have two pieces of code, SslClient and the script which uses this ssl client. From my understanding of the threadpool, the way you have used the threadpool needs to be changed a bit.

From:

```<code>pool = Concurrent::CachedThreadPool.new
pool.post do
 [LARGE LIST OF URLS TO PING].each do |struct|
    ssl_client = SslClient.new(struct.domain.gsub("*.", "www."), struct.scan_port)
    cert_info = ssl_client.ping_for_certificate_info
    struct.x509_cert = cert_info[:certificate]
    struct.verify_result = cert_info[:verify_result]
  end
end

pool.shutdown
pool.wait_for_termination
</code>```

to:

```<code>pool = Concurrent::FixedThreadPool.new(10)

[LARGE LIST OF URLS TO PING].each do | struct |
  pool.post do
   ssl_client = SslClient.new(struct.domain.gsub("*.", "www."), struct.scan_port)
   cert_info = ssl_client.ping_for_certificate_info
   struct.x509_cert = cert_info[:certificate]
   struct.verify_result = cert_info[:verify_result]
  end
end

pool.shutdown
pool.wait_form
</code>```

In the initial version, there is only one unit of work that is posted to the pool. In the second version, we are posting as many units of work to the pool as there are items in LARGE LIST OF URLS TO PING.

To add a bit more about Concurrency vs Parallelism in Ruby, it is true that Ruby doesn't support true parallelism due to GIL (Global Interpreter Lock), but this only applies when we are actually doing any amount of work on the CPU. In case of a network request, CPU bound work duration is very negligible compared to the IO bound work, which means that your usecase is a very good candidate for using threads.

Also by using a threadpool, we can minimize the overhead of thread creation incurred by the CPU. When we use a threadpool, like in the case of Concurrent::FixedThreadPool.new(10), we are literally restricting the number of threads that are available in the pool, for an unbound threadpool, new threads are created for everytime when a unit of work is present, but rest of thre threads in the pool are busy.

In the first article, there was a need to collect the result returned by each individual workers and also to act meaningfully in case of an exception (I am the author). You should be able to use the class given in that blog without any change.

Lets try rewriting your code using Concurrent::Future since in your case too, we need the results.

```<code>
thread_pool = Concurrent::FixedThreadPool.new(20)

executors = [LARGE LIST OF URLS TO PING].map do | struct |
  Concurrent::Future.execute({ executor: thread_pool }) do
    ssl_client = SslClient.new(struct.domain.gsub("*.", "www."), struct.scan_port)
    cert_info = ssl_client.ping_for_certificate_info
    struct.x509_cert = cert_info[:certificate]
    struct.verify_result = cert_info[:verify_result]
    struct
  end
end

executors.map(&:value)
</code>```

I hope this helps. In case of questions, please ask in comments, I shall modify this write up to answer those.

Body:
I am what I now consider part 3 of completing a task of pinging a very large list of URLs (which number in the thousands) and retrieving a URL's x509 certificate associated with it. Part 1 is here (How do I properly use threads to ping a URL) and Part 2 is here (Why won't my connection pool implement my thread code).

Since I asked these two questions, I have now ended up with the following code:

###### This is the code that pings a url and grabs its x509 cert #####

class SslClient
  attr_reader :url, :port, :timeout

  def initialize(url, port = '443')
    @url = url
    @port = port
  end

  def ping_for_certificate_info
    context = OpenSSL::SSL::SSLContext.new
    tcp_client = TCPSocket.new(url, port)
    ssl_client = OpenSSL::SSL::SSLSocket.new tcp_client, context
    ssl_client.hostname = url
    ssl_client.sync_close = true
    ssl_client.connect
    certificate = ssl_client.peer_cert
    verify_result = ssl_client.verify_result
    tcp_client.close
    {certificate: certificate, verify_result: verify_result }
  rescue => error
    {certificate: nil, verify_result: nil }
  end
end

The above code is paramount that I retrieve the ssl_client.peer_cert. Below I have the following code that is the snippet that makes multiple HTTP pings to URLs for their certs:

  pool = Concurrent::CachedThreadPool.new
  pool.post do
    [LARGE LIST OF URLS TO PING].each do |struct|
       ssl_client = SslClient.new(struct.domain.gsub("*.", "www."), struct.scan_port)
       cert_info = ssl_client.ping_for_certificate_info
       struct.x509_cert = cert_info[:certificate]
       struct.verify_result = cert_info[:verify_result]
     end
   end

   pool.shutdown
   pool.wait_for_termination

   #Do some rails code with the database depending on the results.

So far when I run this code, it is unbelievably slow. I thought that by creating a thread pool with threads, the code would go much faster. That doesn't seem the case and I'm not sure why. A lot of it was because I didn't know the nuances of threads, pools, starvation, locks, etc. However, after implementing the above code, I read some more to try to speed it up and once again I'm confused and could use some clarification as to how I can make the code faster.

For starters, in this excellent article here (ruby-concurrency-parallelism) . We get the following definitions and concepts:

Concurrency vs. Parallelism
  These terms are used loosely, but they do have distinct meanings.

Concurrency: The art of doing many tasks, one at a time. By switching
  between them quickly, it may appear to the user as though they happen
  simultaneously. Parallelism: Doing many tasks at literally the same
  time. Instead of appearing simultaneous, they are simultaneous.
  Concurrency is most often used for applications that are IO heavy. For
  example, a web app may regularly interact with a database or make lots
  of network requests. By using concurrency, we can keep our application
  responsive, even while we wait for the database to respond to our
  query.

This is possible because the Ruby VM allows other threads to run while
  one is waiting during IO. Even if a program has to make dozens of
  requests, if we use concurrency, the requests will be made at
  virtually the same time.

Parallelism, on the other hand, is not currently supported by Ruby.

So from this piece of the article, I understand that what I want to do needs to be done concurrently because I am pinging URLs on the network and that Parallelism is not currently supported by Ruby.

Next is where things get confused for me. From my part 1 question on Stack Overflow, I learned the following in a comment given to me that I should do the following:

Use a thread pool; don't just create a thousand concurrent threads. For something like
  connecting to a URL where there will be a lot of waiting you can
  oversubscribe the number of threads per CPU core, but not by a huge
  amount. You'll have to experiment.

Another user says this:

You'd not spawn thousands of threads, use a connection pool
  (e.g https://github.com/mperham/connection_pool) so you have maximum
  20-30 concurrent requests going (this maximum number should be
  determined by testing at which point network performance drops and you
  get these timeouts)

So for this part, I turned to concurrent-ruby and implemented both a CachedThreadPool and a FixedThreadPool with10 threads. I chose a `CachedThreadPool because it seemed to me that the number of threads needed would be taken care of for me by the Threadpool. Now in concurrent ruby's documentation for a pool, I see this:

pool = Concurrent::CachedThreadPool.new
pool.post do
  # some parallel work
end

I thought we just established in the first article that parallelism is not supported in Ruby, so what is the thread pool doing? Is it working concurrently or in parallel? What exactly is going on? Do I need a thread pool or not? Also at this point in time I thought connection pools and thread pools were the same just used interchangeably. What is the difference between the two pools and which one do I need?

In another excellent article How to Perform Concurrent HTTP Requests in Ruby and Rails, this article introduces the Concurrent::Promises class form concurrent ruby to avoid locks and have thread safety with two api calls. Here is a snippet of code below with the following description:

def get_all_conversations
  groups_thread = Thread.new do
    get_groups_list
  end

  channels_thread = Thread.new do
    get_channels_list
  end

  [groups_thread, channels_thread].map(&:value).flatten
end

Every request is executed it its own thread, which can run in parallel because it is a blocking I/O. But can you see a catch here?

In the above code, another mention of parallelism which we just said didn't exist in ruby. Below is the approach with Concurrent::Promise

def get_all_conversations
  groups_promise = Concurrent::Promise.execute do
    get_groups_list
  end

  channels_promise = Concurrent::Promise.execute do
    get_channels_list
  end

  [groups_promise, channels_promise].map(&:value!).flatten
end

So according to this article, these requests are being made 'in parallel'. Are we still talking about concurrency at this point?

Finally, in these two articles, they talk about using Futures for concurrent http requests. I won't go into the details but I'll paste the links here.

1.Using Concurrent Ruby in a Ruby on Rails Application
2. Learn Concurrency by Implementing Futures in Ruby

Again, what's talked about in the article looks to me like the Concurrent::Promise functionality. I just want to note that the examples show how to use the concepts for two different API calls that need to be combined together. This is not what I need. I just need to make thousands of API calls fast and log the results.

In conclusion, I just want to know what I need to do to make my code faster and thread safe to make it run concurrently. What exactly am I missing to make the code go faster because right now it is going so slow that I might as well not have used threads in the first place.

Summary

I have to ping thousands of URLs using threads to speed up the process. The code is slow and I am confused if I am using threads, thread pools, and concurrency correctly.

Body:
Let us look at the problems you have described and try to solve these one at a time:

You have two pieces of code, SslClient and the script which uses this ssl client. From my understanding of the threadpool, the way you have used the threadpool needs to be changed a bit.

From:

pool = Concurrent::CachedThreadPool.new
pool.post do
 [LARGE LIST OF URLS TO PING].each do |struct|
    ssl_client = SslClient.new(struct.domain.gsub("*.", "www."), struct.scan_port)
    cert_info = ssl_client.ping_for_certificate_info
    struct.x509_cert = cert_info[:certificate]
    struct.verify_result = cert_info[:verify_result]
  end
end

pool.shutdown
pool.wait_for_termination

to:

pool = Concurrent::FixedThreadPool.new(10)

[LARGE LIST OF URLS TO PING].each do | struct |
  pool.post do
   ssl_client = SslClient.new(struct.domain.gsub("*.", "www."), struct.scan_port)
   cert_info = ssl_client.ping_for_certificate_info
   struct.x509_cert = cert_info[:certificate]
   struct.verify_result = cert_info[:verify_result]
  end
end

pool.shutdown
pool.wait_form

In the initial version, there is only one unit of work that is posted to the pool. In the second version, we are posting as many units of work to the pool as there are items in LARGE LIST OF URLS TO PING.

To add a bit more about Concurrency vs Parallelism in Ruby, it is true that Ruby doesn't support true parallelism due to GIL (Global Interpreter Lock), but this only applies when we are actually doing any amount of work on the CPU. In case of a network request, CPU bound work duration is very negligible compared to the IO bound work, which means that your usecase is a very good candidate for using threads.

Also by using a threadpool, we can minimize the overhead of thread creation incurred by the CPU. When we use a threadpool, like in the case of Concurrent::FixedThreadPool.new(10), we are literally restricting the number of threads that are available in the pool, for an unbound threadpool, new threads are created for everytime when a unit of work is present, but rest of thre threads in the pool are busy.

In the first article, there was a need to collect the result returned by each individual workers and also to act meaningfully in case of an exception (I am the author). You should be able to use the class given in that blog without any change.

Lets try rewriting your code using Concurrent::Future since in your case too, we need the results.

thread_pool = Concurrent::FixedThreadPool.new(20)

executors = [LARGE LIST OF URLS TO PING].map do | struct |
  Concurrent::Future.execute({ executor: thread_pool }) do
    ssl_client = SslClient.new(struct.domain.gsub("*.", "www."), struct.scan_port)
    cert_info = ssl_client.ping_for_certificate_info
    struct.x509_cert = cert_info[:certificate]
    struct.verify_result = cert_info[:verify_result]
    struct
  end
end

executors.map(&:value)

I hope this helps. In case of questions, please ask in comments, I shall modify this write up to answer those.


问:
why the following code does not return match object?
```<code>txt = 'is worth 12$'
pattern = re.compile('12$')
match = pattern.search(txt)
if match != None:
    print('ys')
else:
    print('no')
</code>```

It prints no. I want to know why the string '12$' is not being matched.

答:
$ is a special character in a regular expression. You should escape it like that:

import re
txt='is worth 12$'
pattern=re.compile('12\$')
match=pattern.search(txt)
if match!=None:
    print('ys')
else:
    print('no')
```

Body:
txt = 'is worth 12$'
pattern = re.compile('12$')
match = pattern.search(txt)
if match != None:
    print('ys')
else:
    print('no')

It prints no. I want to know why the string '12$' is not being matched.


问:
Error "void is an invalid type for the variable passwordMethod1"

Discription: This program check password between 7 to 16

characters which contain only characters, numeric digits and underscore

and first character*/

import java.util.*;

```<code>public class PasswordValidationOne {
 /**my hints on how to solve this problem
 * the length of the string must not less 7 and must not more than 16 character
 **/
public static void main (String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter a password");
      String password = scan.nextLine();

      public  void  passwordMethod1( password) {
            int lengthPassword = password.length(); // getting the length of the string password
            boolean containsDigit = password.matches("*.\\d*.");
            boolean containsUnderscore = password.contains("_");
            char firstCharacter = password.charAt(0);
            if((lengthPassword >= 7&& lengthPassword<=16)&& (containsDigit == true) && (containsUnderscore == true)&& (firstCharacter >= 'A' && firstCharacter <= 'z') )
                System.out.println("Password Accepted! You can proceed");
            else
                System.out.println("Password not accepted! Retry");
      } //end of method passwordMethod1()

}// end of main method
</code>```

}//end of the class

答:
You have missed the datatype of password in the function declaration.

Try using

```<code>public void passwordMethod1(String password) {
</code>```

Instead of

```<code>public void passwordMethod1(password) {
</code>```

Body:
Discription: This program check password between 7 to 16

characters which contain only characters, numeric digits and underscore

and first character*/

import java.util.*;

public class PasswordValidationOne {
 /**my hints on how to solve this problem
 * the length of the string must not less 7 and must not more than 16 character
 **/
public static void main (String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter a password");
      String password = scan.nextLine();

      public  void  passwordMethod1( password) {
            int lengthPassword = password.length(); // getting the length of the string password
            boolean containsDigit = password.matches("*.\\d*.");
            boolean containsUnderscore = password.contains("_");
            char firstCharacter = password.charAt(0);
            if((lengthPassword >= 7&& lengthPassword<=16)&& (containsDigit == true) && (containsUnderscore == true)&& (firstCharacter >= 'A' && firstCharacter <= 'z') )
                System.out.println("Password Accepted! You can proceed");
            else
                System.out.println("Password not accepted! Retry");
      } //end of method passwordMethod1()

}// end of main method

}//end of the class

Body:
You have missed the datatype of password in the function declaration.

Try using

public void passwordMethod1(String password) {

Instead of

public void passwordMethod1(password) {


答:
you can use useState hook for state management and componentwillrecieveprops,didmount,and willmount for useEffect hook lets see below code for functional component</p>

```<code>import React,{useState,useEffect} from 'react'

const App =() =>{
  const [pageLoading,setLoading] = useState(false)
const [articles,setarticles] = useState([])
useEffect((newProps) => {
  const apiData = newProps.apiData;

if (apiData.articles) {
  setLoading(false)
  setarticles(apiData.articles.articles)

} else if (apiData.articleSearch && apiData.articleSearch.success) {
 let articles = apiData.articleSearch.articles;
  setLoading(false)
 setarticles(articles)
}
 }, [pageLoading,articles])
 return (
   ....child
 )
}
  export default App
</code>```

Title: Life cycle transform in hooks for a functional component

Body:
you can use useState hook for state management and componentwillrecieveprops,didmount,and willmount for useEffect hook lets see below code for functional component

import React,{useState,useEffect} from 'react'

const App =() =>{
  const [pageLoading,setLoading] = useState(false)
const [articles,setarticles] = useState([])
useEffect((newProps) => {
  const apiData = newProps.apiData;

if (apiData.articles) {
  setLoading(false)
  setarticles(apiData.articles.articles)

} else if (apiData.articleSearch && apiData.articleSearch.success) {
 let articles = apiData.articleSearch.articles;
  setLoading(false)
 setarticles(articles)
}
 }, [pageLoading,articles])
 return (
   ....child
 )
}
  export default App


问:
Unit Test inconsistent pass or fail result
One of my unit tests seems to randomly pass or fail when I run it. The only thing that makes sense to me for why this is happening is if the data in the database is getting into a different state each time the test is ran, but I use transactions to rollback the database in each test - unless it's not working right. Here's my base unit test class and the unit test class that's having the problem. Can you see anything I might be missing or what else I should look for?

This happens with TestDriven.Net and the Visual Studio Unit Test Framework.

```<code>Partial Public MustInherit Class TestBase

    Private _scope As Transactions.TransactionScope

    <DebuggerStepThrough()> _
    <TestInitialize()> _
    Public Sub Setup()

            //'Start the Distribution Transaction Coordinator, if it's not already running.
            Using dtcService As New System.ServiceProcess.ServiceController("Distributed Transaction Coordinator", My.Computer.Name)
                    If dtcService.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
                            dtcService.Start()
                    End If
            End Using

            _scope = New TransactionScope(TransactionScopeOption.RequiresNew, New TimeSpan(0))
    End Sub

    <DebuggerStepThrough()> _
    <TestCleanup()>
    Public Sub Teardown()
        If _scope IsNot Nothing Then
            _scope.Dispose()
        End If
    End Sub

    <System.Diagnostics.DebuggerStepThrough()> _
    Public Shared Function ExecSql(ByVal sql As String) As System.Data.DataTable

        Dim connStr = GlobalSettings.GetConnectionString()
        Using conn As New System.Data.SqlClient.SqlConnection(connStr)
            conn.Open()

            Dim cmd As New System.Data.SqlClient.SqlCommand(sql.ToString, conn)
            Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(cmd)
            Dim dt As System.Data.DataTable = New System.Data.DataTable
            adapter.Fill(dt)

            Return dt

            If conn.State <> System.Data.ConnectionState.Closed Then
                conn.Close()
            End If
            conn.Dispose()
        End Using
    End Function
End Class
</code>```

And here's my unit test:

```<code>Partial Public Class CampaignEmailSendLimitServiceTests
    Inherits TestBase

    Private _service = ServiceManager.Current.MyService

    <TestMethod()> _
    Public Sub MyTest()
        //' Set the pre-test condition.
        ExecSql("update base.dbo.tblTableA set someDate = '2010-06-28' where id = 56937 ")

        //' Run the service
        _service.Process()

       //' Verify the expected result
       Dim dt = ExecSql("select deliveryDate from tblTableB ")
       Assert.AreEqual(False, dt.Rows(0).IsNull("deliveryDate"))

    End Sub
End Class
</code>```

答:
This has always worked for fine for me.  The main difference I see is using TransactionScopeOption.Required.</p>

```<code>[TestClass()]
public class MyTest: DatabaseTestClass
{

    public MyTest()
    {
        InitializeComponent();
    }

    TransactionScope ambientTransaction;

    [TestInitialize()]
    public void TestInitialize()
    {
        ambientTransaction = new TransactionScope(TransactionScopeOption.Required);
        base.InitializeTest();
    }

    [TestCleanup()]
    public void TestCleanup()
    {
        ambientTransaction.Dispose();
        base.CleanupTest();
    }
}
</code>```

ContentLicense: CC BY-SA 3.0

Body:
One of my unit tests seems to randomly pass or fail when I run it. The only thing that makes sense to me for why this is happening is if the data in the database is getting into a different state each time the test is ran, but I use transactions to rollback the database in each test - unless it's not working right. Here's my base unit test class and the unit test class that's having the problem. Can you see anything I might be missing or what else I should look for?

This happens with TestDriven.Net and the Visual Studio Unit Test Framework.

Partial Public MustInherit Class TestBase

    Private _scope As Transactions.TransactionScope

    <DebuggerStepThrough()> _
    <TestInitialize()> _
    Public Sub Setup()

            //'Start the Distribution Transaction Coordinator, if it's not already running.
            Using dtcService As New System.ServiceProcess.ServiceController("Distributed Transaction Coordinator", My.Computer.Name)
                    If dtcService.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
                            dtcService.Start()
                    End If
            End Using

            _scope = New TransactionScope(TransactionScopeOption.RequiresNew, New TimeSpan(0))
    End Sub

    <DebuggerStepThrough()> _
    <TestCleanup()>
    Public Sub Teardown()
        If _scope IsNot Nothing Then
            _scope.Dispose()
        End If
    End Sub

    <System.Diagnostics.DebuggerStepThrough()> _
    Public Shared Function ExecSql(ByVal sql As String) As System.Data.DataTable

        Dim connStr = GlobalSettings.GetConnectionString()
        Using conn As New System.Data.SqlClient.SqlConnection(connStr)
            conn.Open()

            Dim cmd As New System.Data.SqlClient.SqlCommand(sql.ToString, conn)
            Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(cmd)
            Dim dt As System.Data.DataTable = New System.Data.DataTable
            adapter.Fill(dt)

            Return dt

            If conn.State <> System.Data.ConnectionState.Closed Then
                conn.Close()
            End If
            conn.Dispose()
        End Using
    End Function
End Class

And here's my unit test:

Partial Public Class CampaignEmailSendLimitServiceTests
    Inherits TestBase

    Private _service = ServiceManager.Current.MyService

    <TestMethod()> _
    Public Sub MyTest()
        //' Set the pre-test condition.
        ExecSql("update base.dbo.tblTableA set someDate = '2010-06-28' where id = 56937 ")

        //' Run the service
        _service.Process()

       //' Verify the expected result
       Dim dt = ExecSql("select deliveryDate from tblTableB ")
       Assert.AreEqual(False, dt.Rows(0).IsNull("deliveryDate"))

    End Sub
End Class

Body:
This has always worked for fine for me.  The main difference I see is using TransactionScopeOption.Required.

[TestClass()]
public class MyTest: DatabaseTestClass
{

    public MyTest()
    {
        InitializeComponent();
    }

    TransactionScope ambientTransaction;

    [TestInitialize()]
    public void TestInitialize()
    {
        ambientTransaction = new TransactionScope(TransactionScopeOption.Required);
        base.InitializeTest();
    }

    [TestCleanup()]
    public void TestCleanup()
    {
        ambientTransaction.Dispose();
        base.CleanupTest();
    }
}


问:
Spark Dataframe: Group and rank rows on a certain column value
I am trying to rank a column when the "ID" column numbering starts from 1 to max and then resets from 1.

So, the first three rows have a continuous numbering on "ID"; hence these should be grouped with group rank =1. Rows four and five are in another group, group rank = 2.

The rows are sorted by "rownum" column. I am aware of the row_number window function but I don't think I can apply for this use case as there is no constant window. I can only think of looping through each row in the dataframe but not sure how I can update a column when number resets to 1.

val df = Seq(
    (1, 1 ),
    (2, 2 ),
    (3, 3 ),
    (4, 1),
    (5, 2),
    (6, 1),
    (7, 1),
    (8, 2)
  ).toDF("rownum", "ID")
  df.show()

Expected result is below:

答:
You can do it with 2 window-functions, the first one to flag the state, the second one to calculate a running sum:

```<code>df
  .withColumn("increase", $"ID" > lag($"ID",1).over(Window.orderBy($"rownum")))
  .withColumn("group_rank_of_ID",sum(when($"increase",lit(0)).otherwise(lit(1))).over(Window.orderBy($"rownum")))
  .drop($"increase")
  .show()
</code>```

gives:

```<code>+------+---+----------------+
|rownum| ID|group_rank_of_ID|
+------+---+----------------+
|     1|  1|               1|
|     2|  2|               1|
|     3|  3|               1|
|     4|  1|               2|
|     5|  2|               2|
|     6|  1|               3|
|     7|  1|               4|
|     8|  2|               4|
+------+---+----------------+
</code>```

Body:
You can do it with 2 window-functions, the first one to flag the state, the second one to calculate a running sum:

df
  .withColumn("increase", $"ID" > lag($"ID",1).over(Window.orderBy($"rownum")))
  .withColumn("group_rank_of_ID",sum(when($"increase",lit(0)).otherwise(lit(1))).over(Window.orderBy($"rownum")))
  .drop($"increase")
  .show()

gives:

+------+---+----------------+
|rownum| ID|group_rank_of_ID|
+------+---+----------------+
|     1|  1|               1|
|     2|  2|               1|
|     3|  3|               1|
|     4|  1|               2|
|     5|  2|               2|
|     6|  1|               3|
|     7|  1|               4|
|     8|  2|               4|
+------+---+----------------+


问:
Python slicing list of tuples and converting values to integers - Confusion regarding why it's possible with numpy arrays but not list of tuples
I am trying to slice the first and last values from a list of list then convert these values to integers. I found a solution but I am confused why this solution works and am wondering if there is a simpler way as it seems rather inelegant.

The two ways I found to slice a single value from a list of a list are either to use list comprehension and then slice or convert to a numpy array then slice in two dimensions. Both of these take two steps.

Question 1: is there a single step solution to slice a single value out of a list of a list?

Question 2: Why is a mixed list (some values being numbers and some letters) not made up of integers and strings vs all tuples? Do numpy arrays or another approach allow mixed lists? Sorry if I am not using the correct terminology...

List of a list slicing - list comprehension method

```<code>#Say I have a list of a list with mixed numbers and letters.

>>>a=[('1', 'T', ''), ('2', 'R', 'S'), ('3', 'E', 'S'), ('4', 'T', 'S')]
#list comprehension slice 1D
>>>b=[list[0:1] for list in a]
>>> print(b):
[('1',), ('2',), ('3',), ('4',)]

>>> start=(b[0:1])
>>> print(start):
[('1',)]
>>> end=b[len(b)-1:len(b)]
>>> print(end)
[('4',)]
</code>```

List of a list slicing - numpy array method
    #Say I have a list of a list with mixed numbers and letters.

```<code>>>>a=[('1', 'T', ''), ('2', 'R', 'S'), ('3', 'E', 'S'), ('4', 'T', 'S')]
>>> b_array=numpy.array(a)
>>> print(b_array)
array([['1', 'T', ''],
       ['2', 'R', 'S'],
       ['3', 'E', 'S'],
       ['4', 'T', 'S']], dtype='<U1')

>>>start_array = b_array[0:1,0:1]
>>>print(start_array)
array([['1']], dtype='<U1')

>>>end_array = b_array[len(b_array)-1:len(b_array),0:1]
>>>print(end_array)
array([['4']], dtype='<U1')
</code>```

Both approaches work but what confuses me is they behave differently if I call int() on them
List comprehension
If I either use list comprehension to try to turn all of b into integers

```<code>>>>b_int= [int(i) for i in b]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
</code>```

or turn start or end into integers

```<code>>>>start=int(b[0:1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
</code>```

I get thrown similar errors. But the second says it is a list even though it's a single value in a 1x1 list.

Alternatively if I do the same for start_array it's fine

```<code>>>>start_array=int(b_array[0:1,0:1])
>>>print(start_array)
1
</code>```

So in summary, I figured out a working solution but it feels clunky and I don't understand why this works in numpy arrays but not list of lists.

Question 3: Why can I convert values in numpy arrays into integers not list of lists?

Question 4: Is there a cleaner or easier way to do this in python?

Thanks

edit: So my misunderstanding was that I thought I was dealing with a list of lists. I was not, instead I was working with a list of tuples. I changed the title but not the body. My second problem was I was slice indexing and not scalar indexing. This resulted in not removing my elements from the list. For solutions I could either:

```<code>#List comprehension to reduce dimensionality
a=[('1', 'T', ''), ('2', 'R', 'S'), ('3', 'E', 'S'), ('4', 'T','S')]
b=[x[0] for x in a]
start=int(b[0])
end=int(b[-1])

#Directly index with a scalar
start=int(b[0][0])
end=int(b[-1][0])
</code>```

I will directly index with a scalar as it's simpler. Though if I wanted to index several numbers list comprehension would be more useful.

答:
If you just want the first and last number to be converted to integer, you can simply do this:

```<code>a = [('1', 'T', ''), ('2', 'R', 'S'), ('3', 'E', 'S'), ('4', 'T', 'S')]

firstNum = int(a[0][0])
lastNum = int(a[-1][0])

print(firstNum)
print(lastNum)
</code>```

There is no need for slicing or other complicated logic.
And you are getting error because you cannot convert a tuple into integer, select the item inside tuple and then convert it

Body:
I am trying to slice the first and last values from a list of list then convert these values to integers. I found a solution but I am confused why this solution works and am wondering if there is a simpler way as it seems rather inelegant.

The two ways I found to slice a single value from a list of a list are either to use list comprehension and then slice or convert to a numpy array then slice in two dimensions. Both of these take two steps.

Question 1: is there a single step solution to slice a single value out of a list of a list?

Question 2: Why is a mixed list (some values being numbers and some letters) not made up of integers and strings vs all tuples? Do numpy arrays or another approach allow mixed lists? Sorry if I am not using the correct terminology...

List of a list slicing - list comprehension method

#Say I have a list of a list with mixed numbers and letters.

>>>a=[('1', 'T', ''), ('2', 'R', 'S'), ('3', 'E', 'S'), ('4', 'T', 'S')]
#list comprehension slice 1D
>>>b=[list[0:1] for list in a]
>>> print(b):
[('1',), ('2',), ('3',), ('4',)]

>>> start=(b[0:1])
>>> print(start):
[('1',)]
>>> end=b[len(b)-1:len(b)]
>>> print(end)
[('4',)]

List of a list slicing - numpy array method
    #Say I have a list of a list with mixed numbers and letters.

>>>a=[('1', 'T', ''), ('2', 'R', 'S'), ('3', 'E', 'S'), ('4', 'T', 'S')]
>>> b_array=numpy.array(a)
>>> print(b_array)
array([['1', 'T', ''],
       ['2', 'R', 'S'],
       ['3', 'E', 'S'],
       ['4', 'T', 'S']], dtype='<U1')

>>>start_array = b_array[0:1,0:1]
>>>print(start_array)
array([['1']], dtype='<U1')

>>>end_array = b_array[len(b_array)-1:len(b_array),0:1]
>>>print(end_array)
array([['4']], dtype='<U1')

Both approaches work but what confuses me is they behave differently if I call int() on them
List comprehension
If I either use list comprehension to try to turn all of b into integers

>>>b_int= [int(i) for i in b]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

or turn start or end into integers

>>>start=int(b[0:1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

I get thrown similar errors. But the second says it is a list even though it's a single value in a 1x1 list.

Alternatively if I do the same for start_array it's fine

>>>start_array=int(b_array[0:1,0:1])
>>>print(start_array)
1

So in summary, I figured out a working solution but it feels clunky and I don't understand why this works in numpy arrays but not list of lists.

Question 3: Why can I convert values in numpy arrays into integers not list of lists?

Question 4: Is there a cleaner or easier way to do this in python?

Thanks

edit: So my misunderstanding was that I thought I was dealing with a list of lists. I was not, instead I was working with a list of tuples. I changed the title but not the body. My second problem was I was slice indexing and not scalar indexing. This resulted in not removing my elements from the list. For solutions I could either:

#List comprehension to reduce dimensionality
a=[('1', 'T', ''), ('2', 'R', 'S'), ('3', 'E', 'S'), ('4', 'T','S')]
b=[x[0] for x in a]
start=int(b[0])
end=int(b[-1])

#Directly index with a scalar
start=int(b[0][0])
end=int(b[-1][0])

I will directly index with a scalar as it's simpler. Though if I wanted to index several numbers list comprehension would be more useful.

Body:
If you just want the first and last number to be converted to integer, you can simply do this:

a = [('1', 'T', ''), ('2', 'R', 'S'), ('3', 'E', 'S'), ('4', 'T', 'S')]

firstNum = int(a[0][0])
lastNum = int(a[-1][0])

print(firstNum)
print(lastNum)

There is no need for slicing or other complicated logic.
And you are getting error because you cannot convert a tuple into integer, select the item inside tuple and then convert it


问:
How to change the output color of echo in Linux
I am trying to print a text in the terminal using echo command.

I want to print the text in a red color. How can I do that?

Body:
I'm using this for color printing

#!/bin/bash
#--------------------------------------------------------------------+
#Color picker, usage: printf $BLD$CUR$RED$BBLU'Hello World!'$DEF     |
#-------------------------+--------------------------------+---------+
#       Text color        |       Background color         |         |
#-----------+-------------+--------------+-----------------+         |
# Base color|Lighter shade| Base color   | Lighter shade   |         |
#-----------+-------------+--------------+-----------------+         |
BLK='\e[30m'; blk='\e[90m'; BBLK='\e[40m'; bblk='\e[100m' #| Black   |
RED='\e[31m'; red='\e[91m'; BRED='\e[41m'; bred='\e[101m' #| Red     |
GRN='\e[32m'; grn='\e[92m'; BGRN='\e[42m'; bgrn='\e[102m' #| Green   |
YLW='\e[33m'; ylw='\e[93m'; BYLW='\e[43m'; bylw='\e[103m' #| Yellow  |
BLU='\e[34m'; blu='\e[94m'; BBLU='\e[44m'; bblu='\e[104m' #| Blue    |
MGN='\e[35m'; mgn='\e[95m'; BMGN='\e[45m'; bmgn='\e[105m' #| Magenta |
CYN='\e[36m'; cyn='\e[96m'; BCYN='\e[46m'; bcyn='\e[106m' #| Cyan    |
WHT='\e[37m'; wht='\e[97m'; BWHT='\e[47m'; bwht='\e[107m' #| White   |
#-------------------------{ Effects }----------------------+---------+
DEF='\e[0m'   #Default color and effects                             |
BLD='\e[1m'   #Bold\brighter                                         |
DIM='\e[2m'   #Dim\darker                                            |
CUR='\e[3m'   #Italic font                                           |
UND='\e[4m'   #Underline                                             |
INV='\e[7m'   #Inverted                                              |
COF='\e[?25l' #Cursor Off                                            |
CON='\e[?25h' #Cursor On                                             |
#------------------------{ Functions }-------------------------------+
# Text positioning, usage: XY 10 10 'Hello World!'                   |
XY () { printf "\e[$2;${1}H$3"; }                                   #|
# Print line, usage: line - 10 | line -= 20 | line 'Hello World!' 20 |
line () { printf -v _L %$2s; printf -- "${_L// /$1}"; }             #|
# Create sequence like {0..(X-1)}                                    |
que () { printf -v _N %$1s; _N=(${_N// / 1}); printf "${!_N[*]}"; } #|
#--------------------------------------------------------------------+

All basic colors set as vars and also there are some usefull functions: XY, line and que. Source this script in one of yours and use all color vars and functions.


问:
panic: unable to bootstrap orderer. Error reading genesis block file: open /etc/hyperledger/fabric/genesisblock: no such file or directory
I am working on hyperledger blockchain. I am running 4 organizations network, I have developed docker-compose.yml file. When is start docker-compose all containers are starting but orderer.example.com container is not starting i.e., the container is exiting with code 2.

Below is the orderer.example.com docker-compose configuration details and logs of the orderer.example.com container.

docker-compose configuration details

```<code>###########################################
# Orderer Docker Container Config
###########################################
  orderer.example.com:
  container_name: orderer.example.com
  image: hyperledger/fabric-orderer
  environment:
    - ORDERER_GENERAL_LOGLEVEL=debug
    - FABRIC_LOGGING_SPEC=info
    - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
    - ORDERER_GENERAL_GENESISMETHOD=file
    - OERDERER_GENERAL_GENESISFILE=/etc/hyperledger/configtx/genesis.block
    - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
    - ORDERER_GENERAL_LOCALMSPDIR=/etc/hyperledger/msp/orderer/msp

  working_dir: /opt/gopath/src/github.com/hyperledger/fabric/orderer

  command: orderer

  ports:
  - 7050:7050

  volumes:
    - ./channel-artifacts/:/etc/hyperledger/configtx
    - ./crypto-config/ordererOrganizations/example.com/orderers/OrdererPeer.example.com/:/etc/hyperledger/msp/orderer
    - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/:/etc/hyperledger/msp/peerOrg1/peer0
    - ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/:/etc/hyperledger/msp/peerOrg2/peer0
    - ./crypto-config/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/:/etc/hyperledger/msp/peerOrg3/peer0
    - ./crypto-config/peerOrganizations/org4.example.com/peers/peer0.org4.example.com/:/etc/hyperledger/msp/peerOrg4/peer0

  networks:
    - basic
</code>```

Error logs of orderer.example.com container

```<code>2019-10-18 08:08:51.429 UTC [localconfig] completeInitialization -> INFO 001 Kafka.Version unset, setting to 0.10.2.0
2019-10-18 08:08:51.556 UTC [orderer.common.server] prettyPrintStruct -> INFO 002 Orderer config values:
    General.LedgerType = "file"
    General.ListenAddress = "0.0.0.0"
    General.ListenPort = 7050
    General.TLS.Enabled = false
    General.TLS.PrivateKey = "/etc/hyperledger/fabric/tls/server.key"
    General.TLS.Certificate = "/etc/hyperledger/fabric/tls/server.crt"
    General.TLS.RootCAs = [/etc/hyperledger/fabric/tls/ca.crt]
    General.TLS.ClientAuthRequired = false
    General.TLS.ClientRootCAs = []
    General.Cluster.ListenAddress = ""
    General.Cluster.ListenPort = 0
    General.Cluster.ServerCertificate = ""
    General.Cluster.ServerPrivateKey = ""
    General.Cluster.ClientCertificate = ""
    General.Cluster.ClientPrivateKey = ""
    General.Cluster.RootCAs = []
    General.Cluster.DialTimeout = 5s
    General.Cluster.RPCTimeout = 7s
    General.Cluster.ReplicationBufferSize = 20971520
    General.Cluster.ReplicationPullTimeout = 5s
    General.Cluster.ReplicationRetryTimeout = 5s
    General.Cluster.ReplicationBackgroundRefreshInterval = 5m0s
    General.Cluster.ReplicationMaxRetries = 12
    General.Cluster.SendBufferSize = 10
    General.Cluster.CertExpirationWarningThreshold = 168h0m0s
    General.Cluster.TLSHandshakeTimeShift = 0s
    General.Keepalive.ServerMinInterval = 1m0s
    General.Keepalive.ServerInterval = 2h0m0s
    General.Keepalive.ServerTimeout = 20s
    General.ConnectionTimeout = 0s
    General.GenesisMethod = "file"
    General.GenesisProfile = "SampleInsecureSolo"
    General.SystemChannel = "test-system-channel-name"
    General.GenesisFile = "/etc/hyperledger/fabric/genesisblock"
    General.Profile.Enabled = false
    General.Profile.Address = "0.0.0.0:6060"
    General.LocalMSPDir = "/etc/hyperledger/msp/orderer/msp"
    General.LocalMSPID = "OrdererMSP"
    General.BCCSP.ProviderName = "SW"
    General.BCCSP.SwOpts.SecLevel = 256
    General.BCCSP.SwOpts.HashFamily = "SHA2"
    General.BCCSP.SwOpts.Ephemeral = false
    General.BCCSP.SwOpts.FileKeystore.KeyStorePath = "/etc/hyperledger/msp/orderer/msp/keystore"
    General.BCCSP.SwOpts.DummyKeystore =
    General.BCCSP.SwOpts.InmemKeystore =
    General.BCCSP.PluginOpts =
    General.Authentication.TimeWindow = 15m0s
    General.Authentication.NoExpirationChecks = false
    FileLedger.Location = "/var/hyperledger/production/orderer"
    FileLedger.Prefix = "hyperledger-fabric-ordererledger"
    RAMLedger.HistorySize = 1000
    Kafka.Retry.ShortInterval = 5s
    Kafka.Retry.ShortTotal = 10m0s
    Kafka.Retry.LongInterval = 5m0s
    Kafka.Retry.LongTotal = 12h0m0s
    Kafka.Retry.NetworkTimeouts.DialTimeout = 10s
    Kafka.Retry.NetworkTimeouts.ReadTimeout = 10s
    Kafka.Retry.NetworkTimeouts.WriteTimeout = 10s
    Kafka.Retry.Metadata.RetryMax = 3
    Kafka.Retry.Metadata.RetryBackoff = 250ms
    Kafka.Retry.Producer.RetryMax = 3
    Kafka.Retry.Producer.RetryBackoff = 100ms
    Kafka.Retry.Consumer.RetryBackoff = 2s
    Kafka.Verbose = false
    Kafka.Version = 0.10.2.0
    Kafka.TLS.Enabled = false
    Kafka.TLS.PrivateKey = ""
    Kafka.TLS.Certificate = ""
    Kafka.TLS.RootCAs = []
    Kafka.TLS.ClientAuthRequired = false
    Kafka.TLS.ClientRootCAs = []
    Kafka.SASLPlain.Enabled = false
    Kafka.SASLPlain.User = ""
    Kafka.SASLPlain.Password = ""
    Kafka.Topic.ReplicationFactor = 3
    Debug.BroadcastTraceDir = ""
    Debug.DeliverTraceDir = ""
    Consensus = map[WALDir:/var/hyperledger/production/orderer/etcdraft/wal SnapDir:/var/hyperledger/production/orderer/etcdraft/snapshot]
    Operations.ListenAddress = "127.0.0.1:8443"
    Operations.TLS.Enabled = false
    Operations.TLS.PrivateKey = ""
    Operations.TLS.Certificate = ""
    Operations.TLS.RootCAs = []
    Operations.TLS.ClientAuthRequired = false
    Operations.TLS.ClientRootCAs = []
    Metrics.Provider = "disabled"
    Metrics.Statsd.Network = "udp"
    Metrics.Statsd.Address = "127.0.0.1:8125"
    Metrics.Statsd.WriteInterval = 30s
    Metrics.Statsd.Prefix = ""
panic: unable to bootstrap orderer. Error reading genesis block file: open /etc/hyperledger/fabric/genesisblock: no such file or directory

goroutine 1 [running]:
github.com/hyperledger/fabric/orderer/common/bootstrap/file.(*fileBootstrapper).GenesisBlock(0xc0002bb2b0, 0xc0002bb2b0)
    /opt/gopath/src/github.com/hyperledger/fabric/orderer/common/bootstrap/file/bootstrap.go:39 +0x1d0
github.com/hyperledger/fabric/orderer/common/server.extractBootstrapBlock(0xc000444900, 0x0)
    /opt/gopath/src/github.com/hyperledger/fabric/orderer/common/server/main.go:532 +0x1bd
github.com/hyperledger/fabric/orderer/common/server.Start(0x1018e03, 0x5, 0xc000444900)
    /opt/gopath/src/github.com/hyperledger/fabric/orderer/common/server/main.go:96 +0x43
github.com/hyperledger/fabric/orderer/common/server.Main()
    /opt/gopath/src/github.com/hyperledger/fabric/orderer/common/server/main.go:91 +0x1ce
main.main()
    /opt/gopath/src/github.com/hyperledger/fabric/orderer/main.go:15 +0x20
</code>```

When I got this error I made changes to the code follows as below:

```<code>environment:
  - OERDERER_GENERAL_GENESISFILE=/etc/hyperledger/fabric/genesis.block
volumes:
  - ./channel-artifacts/:/etc/hyperledger/fabric
</code>```

But the I got the same error below is the error logs:

```<code>2019-10-18 07:49:44.841 UTC [orderer.common.server] Main -> ERRO 001 failed to parse config:  Error reading configuration: Unsupported Config Type ""
</code>```

Please help me with this issue. please ignore is there are any indentation error

答:
In your volume, you have to specify your path to block.genesis
And you have to give the same link in environment genesis file path
environment:
  - OERDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.pb
volumes:
  - ./channel-artifacts/:/etc/hyperledger/fabric
 - ./orderer/genesis_block.pb:/var/hyperledger/orderer/orderer.genesis.pb</p>

Body:
I am working on hyperledger blockchain. I am running 4 organizations network, I have developed docker-compose.yml file. When is start docker-compose all containers are starting but orderer.example.com container is not starting i.e., the container is exiting with code 2.

Below is the orderer.example.com docker-compose configuration details and logs of the orderer.example.com container.

docker-compose configuration details

###########################################
# Orderer Docker Container Config
###########################################
  orderer.example.com:
  container_name: orderer.example.com
  image: hyperledger/fabric-orderer
  environment:
    - ORDERER_GENERAL_LOGLEVEL=debug
    - FABRIC_LOGGING_SPEC=info
    - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
    - ORDERER_GENERAL_GENESISMETHOD=file
    - OERDERER_GENERAL_GENESISFILE=/etc/hyperledger/configtx/genesis.block
    - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
    - ORDERER_GENERAL_LOCALMSPDIR=/etc/hyperledger/msp/orderer/msp

  working_dir: /opt/gopath/src/github.com/hyperledger/fabric/orderer

  command: orderer

  ports:
  - 7050:7050

  volumes:
    - ./channel-artifacts/:/etc/hyperledger/configtx
    - ./crypto-config/ordererOrganizations/example.com/orderers/OrdererPeer.example.com/:/etc/hyperledger/msp/orderer
    - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/:/etc/hyperledger/msp/peerOrg1/peer0
    - ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/:/etc/hyperledger/msp/peerOrg2/peer0
    - ./crypto-config/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/:/etc/hyperledger/msp/peerOrg3/peer0
    - ./crypto-config/peerOrganizations/org4.example.com/peers/peer0.org4.example.com/:/etc/hyperledger/msp/peerOrg4/peer0

  networks:
    - basic

Error logs of orderer.example.com container

2019-10-18 08:08:51.429 UTC [localconfig] completeInitialization -> INFO 001 Kafka.Version unset, setting to 0.10.2.0
2019-10-18 08:08:51.556 UTC [orderer.common.server] prettyPrintStruct -> INFO 002 Orderer config values:
    General.LedgerType = "file"
    General.ListenAddress = "0.0.0.0"
    General.ListenPort = 7050
    General.TLS.Enabled = false
    General.TLS.PrivateKey = "/etc/hyperledger/fabric/tls/server.key"
    General.TLS.Certificate = "/etc/hyperledger/fabric/tls/server.crt"
    General.TLS.RootCAs = [/etc/hyperledger/fabric/tls/ca.crt]
    General.TLS.ClientAuthRequired = false
    General.TLS.ClientRootCAs = []
    General.Cluster.ListenAddress = ""
    General.Cluster.ListenPort = 0
    General.Cluster.ServerCertificate = ""
    General.Cluster.ServerPrivateKey = ""
    General.Cluster.ClientCertificate = ""
    General.Cluster.ClientPrivateKey = ""
    General.Cluster.RootCAs = []
    General.Cluster.DialTimeout = 5s
    General.Cluster.RPCTimeout = 7s
    General.Cluster.ReplicationBufferSize = 20971520
    General.Cluster.ReplicationPullTimeout = 5s
    General.Cluster.ReplicationRetryTimeout = 5s
    General.Cluster.ReplicationBackgroundRefreshInterval = 5m0s
    General.Cluster.ReplicationMaxRetries = 12
    General.Cluster.SendBufferSize = 10
    General.Cluster.CertExpirationWarningThreshold = 168h0m0s
    General.Cluster.TLSHandshakeTimeShift = 0s
    General.Keepalive.ServerMinInterval = 1m0s
    General.Keepalive.ServerInterval = 2h0m0s
    General.Keepalive.ServerTimeout = 20s
    General.ConnectionTimeout = 0s
    General.GenesisMethod = "file"
    General.GenesisProfile = "SampleInsecureSolo"
    General.SystemChannel = "test-system-channel-name"
    General.GenesisFile = "/etc/hyperledger/fabric/genesisblock"
    General.Profile.Enabled = false
    General.Profile.Address = "0.0.0.0:6060"
    General.LocalMSPDir = "/etc/hyperledger/msp/orderer/msp"
    General.LocalMSPID = "OrdererMSP"
    General.BCCSP.ProviderName = "SW"
    General.BCCSP.SwOpts.SecLevel = 256
    General.BCCSP.SwOpts.HashFamily = "SHA2"
    General.BCCSP.SwOpts.Ephemeral = false
    General.BCCSP.SwOpts.FileKeystore.KeyStorePath = "/etc/hyperledger/msp/orderer/msp/keystore"
    General.BCCSP.SwOpts.DummyKeystore =
    General.BCCSP.SwOpts.InmemKeystore =
    General.BCCSP.PluginOpts =
    General.Authentication.TimeWindow = 15m0s
    General.Authentication.NoExpirationChecks = false
    FileLedger.Location = "/var/hyperledger/production/orderer"
    FileLedger.Prefix = "hyperledger-fabric-ordererledger"
    RAMLedger.HistorySize = 1000
    Kafka.Retry.ShortInterval = 5s
    Kafka.Retry.ShortTotal = 10m0s
    Kafka.Retry.LongInterval = 5m0s
    Kafka.Retry.LongTotal = 12h0m0s
    Kafka.Retry.NetworkTimeouts.DialTimeout = 10s
    Kafka.Retry.NetworkTimeouts.ReadTimeout = 10s
    Kafka.Retry.NetworkTimeouts.WriteTimeout = 10s
    Kafka.Retry.Metadata.RetryMax = 3
    Kafka.Retry.Metadata.RetryBackoff = 250ms
    Kafka.Retry.Producer.RetryMax = 3
    Kafka.Retry.Producer.RetryBackoff = 100ms
    Kafka.Retry.Consumer.RetryBackoff = 2s
    Kafka.Verbose = false
    Kafka.Version = 0.10.2.0
    Kafka.TLS.Enabled = false
    Kafka.TLS.PrivateKey = ""
    Kafka.TLS.Certificate = ""
    Kafka.TLS.RootCAs = []
    Kafka.TLS.ClientAuthRequired = false
    Kafka.TLS.ClientRootCAs = []
    Kafka.SASLPlain.Enabled = false
    Kafka.SASLPlain.User = ""
    Kafka.SASLPlain.Password = ""
    Kafka.Topic.ReplicationFactor = 3
    Debug.BroadcastTraceDir = ""
    Debug.DeliverTraceDir = ""
    Consensus = map[WALDir:/var/hyperledger/production/orderer/etcdraft/wal SnapDir:/var/hyperledger/production/orderer/etcdraft/snapshot]
    Operations.ListenAddress = "127.0.0.1:8443"
    Operations.TLS.Enabled = false
    Operations.TLS.PrivateKey = ""
    Operations.TLS.Certificate = ""
    Operations.TLS.RootCAs = []
    Operations.TLS.ClientAuthRequired = false
    Operations.TLS.ClientRootCAs = []
    Metrics.Provider = "disabled"
    Metrics.Statsd.Network = "udp"
    Metrics.Statsd.Address = "127.0.0.1:8125"
    Metrics.Statsd.WriteInterval = 30s
    Metrics.Statsd.Prefix = ""
panic: unable to bootstrap orderer. Error reading genesis block file: open /etc/hyperledger/fabric/genesisblock: no such file or directory

goroutine 1 [running]:
github.com/hyperledger/fabric/orderer/common/bootstrap/file.(*fileBootstrapper).GenesisBlock(0xc0002bb2b0, 0xc0002bb2b0)
    /opt/gopath/src/github.com/hyperledger/fabric/orderer/common/bootstrap/file/bootstrap.go:39 +0x1d0
github.com/hyperledger/fabric/orderer/common/server.extractBootstrapBlock(0xc000444900, 0x0)
    /opt/gopath/src/github.com/hyperledger/fabric/orderer/common/server/main.go:532 +0x1bd
github.com/hyperledger/fabric/orderer/common/server.Start(0x1018e03, 0x5, 0xc000444900)
    /opt/gopath/src/github.com/hyperledger/fabric/orderer/common/server/main.go:96 +0x43
github.com/hyperledger/fabric/orderer/common/server.Main()
    /opt/gopath/src/github.com/hyperledger/fabric/orderer/common/server/main.go:91 +0x1ce
main.main()
    /opt/gopath/src/github.com/hyperledger/fabric/orderer/main.go:15 +0x20

When I got this error I made changes to the code follows as below:

environment:
  - OERDERER_GENERAL_GENESISFILE=/etc/hyperledger/fabric/genesis.block
volumes:
  - ./channel-artifacts/:/etc/hyperledger/fabric

But the I got the same error below is the error logs:

2019-10-18 07:49:44.841 UTC [orderer.common.server] Main -> ERRO 001 failed to parse config:  Error reading configuration: Unsupported Config Type ""

Please help me with this issue. please ignore is there are any indentation error


问:
IoT Hub - how to register for lifecycle events - disconnect and connect?

This is still not solved. Can someone please check my comments below? Anyone?

How can I subscribe for when an IoT device connects and disconnects to the IoT Hub?

I checked out these couple of links - and that might solve the issue - but I was hoping it was more simple than having to set up an entire Cosmos Database (I don't have one yet) and an Event Grid:

https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-how-to-order-connection-state-events

https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-event-grid

I already have an Event Hub set up and message routing from my IoT Hub to get notifications when a device twin changes and that works great. Can't I just expand on that somehow?

答: Event subscriptions on your IoT Hub supports many different endpoints. If you already have an Event Hub set up you can use that in combination with an event subscription. To do this from the portal, you go to your hub and to the Events blade. Add a new subscription that listens to 'Device Connected' and 'Device Disconnected' events and use the Endpoint Type 'Event Hubs'. Then simply select your existing hub. Screenshot below.


问:
How to capitalize the first letter of the three letters month name date in Java 8
I have the following string that represents a date like this "20190123" and I want to to convert it into this format "25 Dic 2019" the month name should be in Spanish and the first letter should be in uppercase so far I have this

```<code>    String input = "12252013";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MMddyyyy" );
    LocalDate localDate = LocalDate.parse( input , formatter );
    Locale spanishLocale=new Locale("es", "ES");
    String dateInSpanish=localDate.format(DateTimeFormatter.ofPattern("dd MMM, yyyy",spanishLocale));
    System.out.println("'2016-01-01' in Spanish: "+dateInSpanish);
</code>```

this prints "25 dic, 2013" I want it to be like this "25 Dic 2019"

答:
```<code>dateString = "14/feb/2020";
        dateString = UtilsDate.capitalizeDate(dateString);
</code>```

<p>...</p>

```<code>    public static String capitalizeDate(String dateString) {

    StringBuilder dateStringBuilder = new StringBuilder(dateString.toLowerCase());

    for (int i = 0; i < dateStringBuilder.length(); i++) {

        char c = dateStringBuilder.charAt(i);

        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {

            dateStringBuilder.setCharAt(i, Character.toUpperCase(dateStringBuilder.charAt(i)));

            break;
        }
    }

    return dateStringBuilder.toString();
}
</code>```

Body:
I have the following string that represents a date like this "20190123" and I want to to convert it into this format "25 Dic 2019" the month name should be in Spanish and the first letter should be in uppercase so far I have this

    String input = "12252013";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MMddyyyy" );
    LocalDate localDate = LocalDate.parse( input , formatter );
    Locale spanishLocale=new Locale("es", "ES");
    String dateInSpanish=localDate.format(DateTimeFormatter.ofPattern("dd MMM, yyyy",spanishLocale));
    System.out.println("'2016-01-01' in Spanish: "+dateInSpanish);

this prints "25 dic, 2013" I want it to be like this "25 Dic 2019"

Body:
dateString = "14/feb/2020";
        dateString = UtilsDate.capitalizeDate(dateString);

...

    public static String capitalizeDate(String dateString) {

    StringBuilder dateStringBuilder = new StringBuilder(dateString.toLowerCase());

    for (int i = 0; i < dateStringBuilder.length(); i++) {

        char c = dateStringBuilder.charAt(i);

        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {

            dateStringBuilder.setCharAt(i, Character.toUpperCase(dateStringBuilder.charAt(i)));

            break;
        }
    }

    return dateStringBuilder.toString();
}


问:
Any backbone.js books available?
I've been very excited with backbone.js recently, but all I can find was minimal tutorials on the web. Have been searching for a backbone.js book, but since it just recently came out, wasn't able to find any. Was hoping you guys know currently in production, perhaps a beta available somewhere?</p>

答:
I do not think Backbone is big enough to require a complete book on it. It is only 4 base JS classes. I would suggest you look for books on MVC and design patterns related to it. This is really the base. Maybe some guides on restful web apps and HTML.

The best book is to read the annoted source here: http://documentcloud.github.com/backbone/docs/backbone.html

Title: Any backbone.js books available?


问:
ERROR: Please install mysql2 package manually
When using the sequalize   db:migrate command I am getting the following error. Looking at some of the previous comments on similar issues people said it is a dependency issue but whenever I run

npm install mysql2

or

npm install -g mysql2

I get the same error.

```<code>larry@DESKTOP-NSSNPRR:/mnt/c/Users/larry/Desktop/node/AAF-NodeJS$
sequelize db:migrate

Sequelize CLI [Node: 10.1.0, CLI: 4.0.0, ORM: 4.37.10]

(node:2241) ExperimentalWarning: The fs.promises API is experimental
Loaded configuration file "db/config/database.json".
Using environment "development".

ERROR: Please install mysql2 package manually
</code>```

Here is my package.json like some of you have asked for.

```<code>{
  "name": "aaf-website-node",
  "version": "0.0.1",
  "description": "Nodejs implementation of the achieve anything website",
  "main": "app.js",
  "scripts": {
    "start": "node bin/www",
    "dev": "node node_modules/nodemon/bin/nodemon.js bin/www",
    "test": "jasmine",
    "console": "node console.js"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/AchieveGirl/AAF-NodeJS.git"
  },
  "keywords": [
    "nodejs",
    "express"
  ],
  "author": "Larry Cherry",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/AchieveGirl/AAF-NodeJS/issues"
  },
  "homepage": "https://github.com/AchieveGirl/AAF-NodeJS/blob/master/README.md",
  "dependencies": {
    "body-parser": "^1.18.2",
    "bootstrap": "^4.0.0",
    "compression": "^1.7.2",
    "cookie-parser": "^1.4.3",
    "dotenv": "^5.0.1",
    "ejs": "^2.5.7",
    "eslint": "^4.19.1",
    "express": "^4.16.3",
    "express-ejs-layouts": "^2.3.1",
    "express-minify": "^1.0.0",
    "express-minify-html": "^0.12.0",
    "jquery": "^3.3.1",
    "lodash": "^4.17.5",
    "morgan": "^1.9.0",
    "mysql2": "^1.5.3",
    "node-fetch": "^2.1.2",
    "node-minify": "^2.4.1",
    "popper.js": "^1.14.3",
    "sequelize": "^4.37.10",
    "sequelize-cli": "^4.0.0",
    "serve-favicon": "^2.4.5",
    "webpack": "^4.1.1",
    "webpack-cli": "^2.0.12"
  },
  "devDependencies": {
    "nodemon": "^1.17.2",
    "pryjs": "^1.0.3"
  }
}
</code>```

答:
Since version 5.2.0 nodejs comes alongside with an utility called npx, which is destined to be used as the "package runner", as it first look packages inside your node_modules you should not have any more troubles running commands with it.

NPM now should be used as a dependency manager instead of a command runner.

You can check differences of both here

Body:
When using the sequalize   db:migrate command I am getting the following error. Looking at some of the previous comments on similar issues people said it is a dependency issue but whenever I run

npm install mysql2

or

npm install -g mysql2

I get the same error.

larry@DESKTOP-NSSNPRR:/mnt/c/Users/larry/Desktop/node/AAF-NodeJS$
sequelize db:migrate

Sequelize CLI [Node: 10.1.0, CLI: 4.0.0, ORM: 4.37.10]

(node:2241) ExperimentalWarning: The fs.promises API is experimental
Loaded configuration file "db/config/database.json".
Using environment "development".

ERROR: Please install mysql2 package manually

Here is my package.json like some of you have asked for.

{
  "name": "aaf-website-node",
  "version": "0.0.1",
  "description": "Nodejs implementation of the achieve anything website",
  "main": "app.js",
  "scripts": {
    "start": "node bin/www",
    "dev": "node node_modules/nodemon/bin/nodemon.js bin/www",
    "test": "jasmine",
    "console": "node console.js"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/AchieveGirl/AAF-NodeJS.git"
  },
  "keywords": [
    "nodejs",
    "express"
  ],
  "author": "Larry Cherry",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/AchieveGirl/AAF-NodeJS/issues"
  },
  "homepage": "https://github.com/AchieveGirl/AAF-NodeJS/blob/master/README.md",
  "dependencies": {
    "body-parser": "^1.18.2",
    "bootstrap": "^4.0.0",
    "compression": "^1.7.2",
    "cookie-parser": "^1.4.3",
    "dotenv": "^5.0.1",
    "ejs": "^2.5.7",
    "eslint": "^4.19.1",
    "express": "^4.16.3",
    "express-ejs-layouts": "^2.3.1",
    "express-minify": "^1.0.0",
    "express-minify-html": "^0.12.0",
    "jquery": "^3.3.1",
    "lodash": "^4.17.5",
    "morgan": "^1.9.0",
    "mysql2": "^1.5.3",
    "node-fetch": "^2.1.2",
    "node-minify": "^2.4.1",
    "popper.js": "^1.14.3",
    "sequelize": "^4.37.10",
    "sequelize-cli": "^4.0.0",
    "serve-favicon": "^2.4.5",
    "webpack": "^4.1.1",
    "webpack-cli": "^2.0.12"
  },
  "devDependencies": {
    "nodemon": "^1.17.2",
    "pryjs": "^1.0.3"
  }
}