问:
Can't discover any services, but can connect to device
I'm able to connect to a BLE device, but .discoverServices() doesn't work. My logs show an unhelpfully generic error: [CoreBluetooth] XPC connection invalid

Connecting to it does work:

```<code>    func connectToPeripheral(id: Int) -> Bool {
        guard id >= 0 && id <= peripherals.count else {
            return false
        }

        let arrayItem = peripherals[id]
        connectedPeripheral = arrayItem.peripheral
        connectedPeripheral!.delegate = self

        myCentral.connect(connectedPeripheral!, options: nil)
        print("connected to \(arrayItem.name)")

        myCentral.stopScan()
        return true
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Connected to \(String(describing: peripheral.name)) \(peripheral.description)!")
        print(peripheral.services as Any)
        peripheral.discoverServices(nil)
    }
</code>```

But this function never gets called. Why????

```<code>    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        print("Services:")
        print(peripheral.services as Any)
    }
</code>```

Not sure what I'm doing wrong, help?

Here's the full code:

```<code>import Foundation
import CoreBluetooth

class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeripheralDelegate {

    var myCentral: CBCentralManager!

    @Published var isSwitchedOn = false
    @Published var peripherals = [Peripheral]()
    @Published var connectedPeripheral: CBPeripheral?

    override init() {
        super.init()

        myCentral = CBCentralManager(delegate: self, queue: nil)
        myCentral.delegate = self
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        var peripheralName: String!

        if let name = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
            peripheralName = name
        } else {
            peripheralName = "Unknown"
        }

        if peripherals.contains(where: { p in p.name == peripheralName }) {
            return
        }

        let newPeripheral = Peripheral(id: peripherals.count, name: peripheralName, rssi: RSSI.intValue, peripheral: peripheral)
        peripherals.append(newPeripheral)
    }

    func startScanning() {
        print("startScanning")
        peripherals.removeAll()
        myCentral.scanForPeripherals(withServices: nil, options: nil)
    }

    func stopScanning() {
        print("stopScanning")
        myCentral.stopScan()
    }

    func connectToPeripheral(id: Int) -> Bool {
        guard id >= 0 && id <= peripherals.count else {
            return false
        }

        let arrayItem = peripherals[id]
        connectedPeripheral = arrayItem.peripheral
        connectedPeripheral!.delegate = self

        myCentral.connect(connectedPeripheral!, options: nil)
        print("connected to \(arrayItem.name)")

        myCentral.stopScan()
        return true
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Connected to \(String(describing: peripheral.name)) \(peripheral.description)!")
        print(peripheral.services as Any)
        peripheral.discoverServices(nil)
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        print("Services:")
        print(peripheral.services as Any)
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        print("got Characteristics...")
    }

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        print("centralManagerDidUpdateState: poweredOn=\(central.state == .poweredOn)")

        if central.state == .poweredOn {
            //if it wasn't previously on, start scanning
            if !isSwitchedOn {
                isSwitchedOn = true
                startScanning()
            }
        } else {
            isSwitchedOn = false
        }
    }
}

struct Peripheral: Identifiable {
    let id: Int
    let name: String
    let rssi: Int
    let peripheral: CBPeripheral
}

</code>```

答:
I figured out two bugs in my code.

I was calling startScanning() when centralManagerDidUpdateState() was called with central.state == .poweredOn. This causing scanning to start again right after calling myCentral.connect(connectedPeripheral!, options: nil)

Calling myCentral.stopScan() after myCentral.connect(connectedPeripheral!, options: nil) was causing disconnections

I don't understand why either of those were happening, but fixing them allowed me to now connect and get services and characteristics!

Body:
I'm able to connect to a BLE device, but .discoverServices() doesn't work. My logs show an unhelpfully generic error: [CoreBluetooth] XPC connection invalid

Connecting to it does work:

    func connectToPeripheral(id: Int) -> Bool {
        guard id >= 0 && id <= peripherals.count else {
            return false
        }

        let arrayItem = peripherals[id]
        connectedPeripheral = arrayItem.peripheral
        connectedPeripheral!.delegate = self

        myCentral.connect(connectedPeripheral!, options: nil)
        print("connected to \(arrayItem.name)")

        myCentral.stopScan()
        return true
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Connected to \(String(describing: peripheral.name)) \(peripheral.description)!")
        print(peripheral.services as Any)
        peripheral.discoverServices(nil)
    }

But this function never gets called. Why????

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        print("Services:")
        print(peripheral.services as Any)
    }

Not sure what I'm doing wrong, help?

Here's the full code:

import Foundation
import CoreBluetooth

class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeripheralDelegate {

    var myCentral: CBCentralManager!

    @Published var isSwitchedOn = false
    @Published var peripherals = [Peripheral]()
    @Published var connectedPeripheral: CBPeripheral?

    override init() {
        super.init()

        myCentral = CBCentralManager(delegate: self, queue: nil)
        myCentral.delegate = self
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        var peripheralName: String!

        if let name = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
            peripheralName = name
        } else {
            peripheralName = "Unknown"
        }

        if peripherals.contains(where: { p in p.name == peripheralName }) {
            return
        }

        let newPeripheral = Peripheral(id: peripherals.count, name: peripheralName, rssi: RSSI.intValue, peripheral: peripheral)
        peripherals.append(newPeripheral)
    }

    func startScanning() {
        print("startScanning")
        peripherals.removeAll()
        myCentral.scanForPeripherals(withServices: nil, options: nil)
    }

    func stopScanning() {
        print("stopScanning")
        myCentral.stopScan()
    }

    func connectToPeripheral(id: Int) -> Bool {
        guard id >= 0 && id <= peripherals.count else {
            return false
        }

        let arrayItem = peripherals[id]
        connectedPeripheral = arrayItem.peripheral
        connectedPeripheral!.delegate = self

        myCentral.connect(connectedPeripheral!, options: nil)
        print("connected to \(arrayItem.name)")

        myCentral.stopScan()
        return true
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Connected to \(String(describing: peripheral.name)) \(peripheral.description)!")
        print(peripheral.services as Any)
        peripheral.discoverServices(nil)
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        print("Services:")
        print(peripheral.services as Any)
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        print("got Characteristics...")
    }

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        print("centralManagerDidUpdateState: poweredOn=\(central.state == .poweredOn)")

        if central.state == .poweredOn {
            //if it wasn't previously on, start scanning
            if !isSwitchedOn {
                isSwitchedOn = true
                startScanning()
            }
        } else {
            isSwitchedOn = false
        }
    }
}

struct Peripheral: Identifiable {
    let id: Int
    let name: String
    let rssi: Int
    let peripheral: CBPeripheral
}


来源: stackexchange

问:
Advise on how to structure code in relation to models in Angular/TypeScript
In my C# backend I have classes for my entities and entity lists. When I need a function/method on that entity or entity list, I just add it to the class.

In Angular/TypeScript models are defined as interfaces in every example I come across. How should I organize the functions that operate on objects of these interfaces?

I'm using NgRx, and it is tempting to put much of this code alongside the selectors, but I feel that it will quickly become quite messy.

答:
The same rules apply as in C#. The models don't have to be interfaces, they can also be a class if they need to include functionality.

For example,

```<code>export class Person {
  firstName: string;
  lastName: string

  get fullName(): string {
    return `${firstName} ${lastName}`;
  }
}
</code>```

However, since you're using NgRx, it's recommended to not store objects like this in the store - that's why most use interfaces.

In that case, I would create a new "selectors" file, and store the logic there to retrieve the full name. Easy to test your business logic there as well.

ContentLicense: CC BY-SA 4.0

Body:
The same rules apply as in C#. The models don't have to be interfaces, they can also be a class if they need to include functionality.

For example,

export class Person {
  firstName: string;
  lastName: string

  get fullName(): string {
    return `${firstName} ${lastName}`;
  }
}

However, since you're using NgRx, it's recommended to not store objects like this in the store - that's why most use interfaces.

In that case, I would create a new "selectors" file, and store the logic there to retrieve the full name. Easy to test your business logic there as well.


问:
How to implement hive sql's map() equivalent in BigQuery?
Need help converting hive sql's map() function in BigQuery. I have example map()

Hive SQL:

```<code>with da as (select '1' key, 'one' value union all select '2' key, 'two' value union all select '2' key, 'tw0' value)
select map(key,value) as keyValue, keyValue from da;
</code>```

Result:

```<code>keyvalue
{"1":"one"}
{"2":"two"}
{"2":"tw0"}
</code>```

How to implement this map(k,v) and accessing value through corresponding key like map[key]?

答:
There is no automatic solution. There are 2 options you can use, depending on what you need for the final data

This is the data you are using as example:

```<code>    With da AS (
   (SELECT 1 AS key, 'one' AS value) UNION ALL
   (SELECT 2 AS key, 'two' AS value) UNION ALL
   (SELECT 2 AS key, 'tw0' AS value)
)

</code>```

First option is to use the concat function. This returns the data just with the values.

```<code>select concat('{"',key,'":"',value,'"}')  from da
</code>```

You can access one key value. You can see this example.

```<code>select concat('{"',key,'":"',value,'"}')  from da where key='2'
</code>```

Second Option is to use the TO_JSON_STRING function in BigQuery. You can see more documentation about this command. This returns the data in JSON format.

To display all the data you need to execute this query;

```<code>SELECT TO_JSON_STRING(t) AS json_objects
FROM da AS t
</code>```

If you want to access only one key, you need to filter like this example:

```<code>SELECT TO_JSON_STRING(t) AS json_objects
FROM da AS t where t.key=2;
</code>```

Body:
Need help converting hive sql's map() function in BigQuery. I have example map()

Hive SQL:

with da as (select '1' key, 'one' value union all select '2' key, 'two' value union all select '2' key, 'tw0' value)
select map(key,value) as keyValue, keyValue from da;

Result:

keyvalue
{"1":"one"}
{"2":"two"}
{"2":"tw0"}

How to implement this map(k,v) and accessing value through corresponding key like map[key]?

Body:
There is no automatic solution. There are 2 options you can use, depending on what you need for the final data

This is the data you are using as example:

    With da AS (
   (SELECT 1 AS key, 'one' AS value) UNION ALL
   (SELECT 2 AS key, 'two' AS value) UNION ALL
   (SELECT 2 AS key, 'tw0' AS value)
)

First option is to use the concat function. This returns the data just with the values.

select concat('{"',key,'":"',value,'"}')  from da

You can access one key value. You can see this example.

select concat('{"',key,'":"',value,'"}')  from da where key='2'

Second Option is to use the TO_JSON_STRING function in BigQuery. You can see more documentation about this command. This returns the data in JSON format.

To display all the data you need to execute this query;

SELECT TO_JSON_STRING(t) AS json_objects
FROM da AS t

If you want to access only one key, you need to filter like this example:

SELECT TO_JSON_STRING(t) AS json_objects
FROM da AS t where t.key=2;


问:
Hot reloading in a Laravel + Vue project running with docker-compose not working
For the last few hours I have been trying to implement hot reloading with browserSync but I have not found a way to do that. The browser does not refresh automatically, I still have to refresh manually to see the changes in my Vue components. Here is my configuration:

docker-compose.yml

```<code>version: '3.7'
services:
  app:
    build:
      context: .
      dockerfile: .docker/Dockerfile
    container_name: app
    image: digitalocean.com/php
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./.docker/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: always
    tty: true
    ports:
      - "80:80"
      - "443"
    volumes:
      - ./:/var/www
      - ./.docker/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network
  mongo:
    image: mongo
    restart: always
    container_name: mongodb
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: root
    networks:
      - app-network
  mongo-express:
    image: mongo-express
    container_name: mongoexpress
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: root
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge
</code>```

Dockerfile

```<code>FROM php:8.1.2-fpm

COPY composer.lock composer.json /var/www/

WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    libzip-dev \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl \
    libcurl4-openssl-dev \
    pkg-config \
    libssl-dev \
    software-properties-common \
    npm \
    && docker-php-ext-configure gd \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql \
    && docker-php-ext-install mysqli \
    && docker-php-ext-install zip \
    && docker-php-source delete

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

RUN pecl install mongodb
RUN echo "extension=mongodb.so" >> /usr/local/etc/php/php.ini

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install nodejs and npm
ENV NODE_VERSION=16.13.2
RUN apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN node --version
RUN npm --version

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
</code>```

webpack.mix.js

```<code>mix.js('resources/js/app.js', 'public/js');
mix.browserSync({
    host: 'localhost',
    proxy: 'app',
    port: 80,
    open: false,
});
</code>```

I also added this line at the end of the  in my app.blade.php (main view, contains the
 ):

```<code><script src="{{mix('js/app.js')}}"></script>
</code>```

Im running npm run hot inside the app container using docker exec -it app /bin/bash.

Can anyone help me out? Maybe browser sync is not really needed and there is another (better) way to have hot reloading?

答:
I finally found a way to make it work. Here's what I did

Expose port 3000 in my app container:

```<code>  app:
    build:
      context: .
      dockerfile: .docker/Dockerfile
    container_name: app
    image: digitalocean.com/php
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./.docker/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network
    ports:
      - "3000:3000"
</code>```

I also exposed the port from the Dockerfile:

```<code>...
COPY --chown=www:www . /var/www

# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000

EXPOSE 3000 //here
CMD ["php-fpm"]
</code>```

webpack.mix.js:

```<code>mix.js('resources/js/app.js', 'public/js').vue();
mix.browserSync({
    host: "localhost",
    proxy: 'webserver', //here
    open: false,
});
</code>```

That's it. npm run watch and go to localhost:3000

Body:
For the last few hours I have been trying to implement hot reloading with browserSync but I have not found a way to do that. The browser does not refresh automatically, I still have to refresh manually to see the changes in my Vue components. Here is my configuration:

docker-compose.yml

version: '3.7'
services:
  app:
    build:
      context: .
      dockerfile: .docker/Dockerfile
    container_name: app
    image: digitalocean.com/php
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./.docker/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: always
    tty: true
    ports:
      - "80:80"
      - "443"
    volumes:
      - ./:/var/www
      - ./.docker/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network
  mongo:
    image: mongo
    restart: always
    container_name: mongodb
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: root
    networks:
      - app-network
  mongo-express:
    image: mongo-express
    container_name: mongoexpress
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: root
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge

Dockerfile

FROM php:8.1.2-fpm

COPY composer.lock composer.json /var/www/

WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    libzip-dev \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl \
    libcurl4-openssl-dev \
    pkg-config \
    libssl-dev \
    software-properties-common \
    npm \
    && docker-php-ext-configure gd \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql \
    && docker-php-ext-install mysqli \
    && docker-php-ext-install zip \
    && docker-php-source delete

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

RUN pecl install mongodb
RUN echo "extension=mongodb.so" >> /usr/local/etc/php/php.ini

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install nodejs and npm
ENV NODE_VERSION=16.13.2
RUN apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN node --version
RUN npm --version

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

webpack.mix.js

mix.js('resources/js/app.js', 'public/js');
mix.browserSync({
    host: 'localhost',
    proxy: 'app',
    port: 80,
    open: false,
});

I also added this line at the end of the  in my app.blade.php (main view, contains the
 ):

Im running npm run hot inside the app container using docker exec -it app /bin/bash.

Can anyone help me out? Maybe browser sync is not really needed and there is another (better) way to have hot reloading?

Body:
I finally found a way to make it work. Here's what I did

Expose port 3000 in my app container:

  app:
    build:
      context: .
      dockerfile: .docker/Dockerfile
    container_name: app
    image: digitalocean.com/php
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./.docker/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network
    ports:
      - "3000:3000"

I also exposed the port from the Dockerfile:

...
COPY --chown=www:www . /var/www

# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000

EXPOSE 3000 //here
CMD ["php-fpm"]

webpack.mix.js:

mix.js('resources/js/app.js', 'public/js').vue();
mix.browserSync({
    host: "localhost",
    proxy: 'webserver', //here
    open: false,
});

That's it. npm run watch and go to localhost:3000


问:
Why does click event handler fire immediately upon page load?
I playing around with a function that I want to bind to all the links. At the present the function fires when the page loads, instead of when I click on the link.

Here's my code. (I can post the function showDiv(), if you need to see it.) Can you tell if I'm doing something wrong or stupid here?

```<code>$(document).ready(function(){

    $('a.test').bind("click", showDiv());

});
</code>```

Thanks!

答:
Use the below line.  showDiv() will call the function rigth away when that line is executed.

```<code>$('a.test').bind("click", showDiv);
</code>```

Body:
I playing around with a function that I want to bind to all the links. At the present the function fires when the page loads, instead of when I click on the link.

Here's my code. (I can post the function showDiv(), if you need to see it.) Can you tell if I'm doing something wrong or stupid here?

$(document).ready(function(){

    $('a.test').bind("click", showDiv());

});

Thanks!

Body:
Use the below line.  showDiv() will call the function rigth away when that line is executed.

$('a.test').bind("click", showDiv);

ContentLicense: CC BY-SA 3.0


问:
R packages "terra" and "raster" make R crash when charging library
I first had the same issue than this post : R packages "raster" fail to upload while searching for "terra" last version
(problem with terra updating) but now, with the solutions I've got there I face a new problem :

When I charge the library of terra and so raster (and so lidR) my R crash without any explanation.

terra version: 1.5.17 (caught with install.packages('terra', repos='https://rspatial.r-universe.dev')

raster version : 3.5-15

I'm on Win 10, fresh and reinstalled R 4.1.2, R studio and R tools. I've deleted all files during the multiple reinstallation process i've made.

答:
These problems are typically caused by loading an old session, or a package that depends on the package you want to install. It is best to try to fix this by using in R-gui or command-line R; and avoid R-Studio.

Before you do anything else, run unlink(".Rdata") and close R answer "no" to the question "save workspace image" (I think it is a design flaw to even ask that question). And then first install terra, followed by raster.

But if you must use R-Studio, first close all files in R-studio, run unlink(".Rdata"), exit R-Studio and open it again so that you start with a clean environment. Check sessionInfo() to make sure than only a few base packages are loaded. And ls() to make sure that there are no R objects in memory.

There should be no need for R-tools as you can install the package from CRAN (install.packages("terra"), or, for the development versions, from the R-universe (install.packages('terra', repos='https://rspatial.r-universe.dev').


问:
Efficient online linear regression algorithm in python
I got a 2-D dataset with two columns x and y. I would like to get the linear regression coefficients and interception dynamically when new data feed in. Using scikit-learn I could calculate all current available data like this:

```<code>from sklearn.linear_model import LinearRegression
regr = LinearRegression()
x = np.arange(100)
y = np.arange(100)+10*np.random.random_sample((100,))
regr.fit(x,y)
print(regr.coef_)
print(regr.intercept_)
</code>```

However, I got quite big dataset (more than 10k rows in total) and I want to calculate coefficient and intercept as fast as possible whenever there's new rows coming in. Currently calculate 10k rows takes about 600 microseconds, and I want to accelerate this process.

Scikit-learn looks like does not have online update function for linear regression module. Is there any better ways to do this?

答:
You can use accelerated libraries that implement faster algorithms - particularly
https://github.com/intel/scikit-learn-intelex

For linear regression you would get much better performance

First install package

pip install scikit-learn-intelex
```
<p>And then add in your python script</p>
<pre class="lang-py prettyprint-override"><code>from sklearnex import patch_sklearn
patch_sklearn()
</code>```

Body:
I got a 2-D dataset with two columns x and y. I would like to get the linear regression coefficients and interception dynamically when new data feed in. Using scikit-learn I could calculate all current available data like this:

from sklearn.linear_model import LinearRegression
regr = LinearRegression()
x = np.arange(100)
y = np.arange(100)+10*np.random.random_sample((100,))
regr.fit(x,y)
print(regr.coef_)
print(regr.intercept_)

However, I got quite big dataset (more than 10k rows in total) and I want to calculate coefficient and intercept as fast as possible whenever there's new rows coming in. Currently calculate 10k rows takes about 600 microseconds, and I want to accelerate this process.

Scikit-learn looks like does not have online update function for linear regression module. Is there any better ways to do this?

Body:
You can use accelerated libraries that implement faster algorithms - particularly
https://github.com/intel/scikit-learn-intelex

For linear regression you would get much better performance

First install package

pip install scikit-learn-intelex

And then add in your python script

from sklearnex import patch_sklearn
patch_sklearn()


问:
SwiftUI Search bar - Please help my simplify my code
again maybe a beginner question.
I've implemented a search function in my app. But I'm very unhappy with my code. I know I have to have to put my searchText into the .searchable() {...}
But also with help from different tutorials my knowledge isn't good enough to do this.
Could you please habe a look into my code and give me a hint so I could learn from your solution?</p>
```<code>//
//  FavoriteView.swift
//  Blindzeln_Prototyp
//
//  Created by Michael Ecke on 25.01.22.
//

import SwiftUI

struct FavoriteView: View {

    @EnvironmentObject var blindzeln: BLINDzeln
    @State private var searchText = ""

    var body: some View {

        NavigationView {
            List{
                ForEach(blindzeln.favorites, id: \.entryID) { item in

                    if searchText==""{

                    NavigationLink(destination: FavoriteDetailView(item: item)) {
                        VStack(alignment: .leading, spacing: 20.0) {
                            Text(item.title)
                                .font(.largeTitle)
                                .foregroundColor(.primary)
                            Text(item.textBody)
                                .font(.body)
                                .foregroundColor(.secondary)
                                .lineLimit(2)
                            BigDivider()
                        }
                    }.listRowSeparatorTint(.primary)
                        .listRowSeparator(.hidden)
                }
                else {
                    if item.title.localizedCaseInsensitiveContains(searchText) || item.textBody.localizedCaseInsensitiveContains(searchText){
                        NavigationLink(destination: FavoriteDetailView(item: item)) {
                            VStack(alignment: .leading, spacing: 20.0) {
                                Text(item.title)
                                    .font(.largeTitle)
                                    .foregroundColor(.primary)
                                Text(item.textBody)
                                    .font(.body)
                                    .foregroundColor(.secondary)
                                    .lineLimit(2)
                                BigDivider()
                            }
                        }.listRowSeparatorTint(.primary)
                            .listRowSeparator(.hidden)
                    }
                }

                }
                .onDelete(perform: delete)
                .onMove(perform: onMove)
            }
            .searchable(text: $searchText) {}
            ```
</code>```

答:
Without the rest of the code to test, I can't be sure, but you should be able to condense your code like so:

```<code>struct FavoriteView: View {

    @EnvironmentObject var blindzeln: BLINDzeln
    @State private var searchText = ""

    var body: some View {

        NavigationView {
            List{
                ForEach(blindzeln.favorites.filter { searchText.isEmpty ||($0.title.localizedCaseInsensitiveContains(searchText) || $0.textBody.localizedCaseInsensitiveContains(searchText)) }, id: \.entryID){ item in
                    NavigationLink(destination: FavoriteDetailView(item: item)) {
                        VStack(alignment: .leading, spacing: 20.0) {
                            Text(item.title)
                                .font(.largeTitle)
                                .foregroundColor(.primary)
                            Text(item.textBody)
                                .font(.body)
                                .foregroundColor(.secondary)
                                .lineLimit(2)
                            BigDivider()
                        }
                    }.listRowSeparatorTint(.primary)
                        .listRowSeparator(.hidden)
                }
            }
            .onDelete(perform: delete)
            .onMove(perform: onMove)
        }
        .searchable(text: $searchText) {}
        // Nothing changed past here...
    }
}
</code>```

Essentially the filter I set up is this:

if searchText is empty, return TRUE so the item is used;

if searchText is not empty, evaluate the other side of the OR which is 2 conditions with an OR. If either title OR textBody contains searchText, return TRUE so item is used;

if everything returns false, don't use item.

One last thing, rename your entryID in your model struct to id, make the model struct conform to Identifiable and then your ForEach (leaving out the .filter can be this:

```<code>ForEach(blindzeln.favorites) { item in
</code>```

as an Identifiable struct does not need to use id: in a ForEach initializer.

Body:
again maybe a beginner question.
I've implemented a search function in my app. But I'm very unhappy with my code. I know I have to have to put my searchText into the .searchable() {...}
But also with help from different tutorials my knowledge isn't good enough to do this.
Could you please habe a look into my code and give me a hint so I could learn from your solution?

//
//  FavoriteView.swift
//  Blindzeln_Prototyp
//
//  Created by Michael Ecke on 25.01.22.
//

import SwiftUI

struct FavoriteView: View {

    @EnvironmentObject var blindzeln: BLINDzeln
    @State private var searchText = ""

    var body: some View {

        NavigationView {
            List{
                ForEach(blindzeln.favorites, id: \.entryID) { item in

                    if searchText==""{

                    NavigationLink(destination: FavoriteDetailView(item: item)) {
                        VStack(alignment: .leading, spacing: 20.0) {
                            Text(item.title)
                                .font(.largeTitle)
                                .foregroundColor(.primary)
                            Text(item.textBody)
                                .font(.body)
                                .foregroundColor(.secondary)
                                .lineLimit(2)
                            BigDivider()
                        }
                    }.listRowSeparatorTint(.primary)
                        .listRowSeparator(.hidden)
                }
                else {
                    if item.title.localizedCaseInsensitiveContains(searchText) || item.textBody.localizedCaseInsensitiveContains(searchText){
                        NavigationLink(destination: FavoriteDetailView(item: item)) {
                            VStack(alignment: .leading, spacing: 20.0) {
                                Text(item.title)
                                    .font(.largeTitle)
                                    .foregroundColor(.primary)
                                Text(item.textBody)
                                    .font(.body)
                                    .foregroundColor(.secondary)
                                    .lineLimit(2)
                                BigDivider()
                            }
                        }.listRowSeparatorTint(.primary)
                            .listRowSeparator(.hidden)
                    }
                }

                }
                .onDelete(perform: delete)
                .onMove(perform: onMove)
            }
            .searchable(text: $searchText) {}
            ```

Body:
Without the rest of the code to test, I can't be sure, but you should be able to condense your code like so:

struct FavoriteView: View {

    @EnvironmentObject var blindzeln: BLINDzeln
    @State private var searchText = ""

    var body: some View {

        NavigationView {
            List{
                ForEach(blindzeln.favorites.filter { searchText.isEmpty ||($0.title.localizedCaseInsensitiveContains(searchText) || $0.textBody.localizedCaseInsensitiveContains(searchText)) }, id: \.entryID){ item in
                    NavigationLink(destination: FavoriteDetailView(item: item)) {
                        VStack(alignment: .leading, spacing: 20.0) {
                            Text(item.title)
                                .font(.largeTitle)
                                .foregroundColor(.primary)
                            Text(item.textBody)
                                .font(.body)
                                .foregroundColor(.secondary)
                                .lineLimit(2)
                            BigDivider()
                        }
                    }.listRowSeparatorTint(.primary)
                        .listRowSeparator(.hidden)
                }
            }
            .onDelete(perform: delete)
            .onMove(perform: onMove)
        }
        .searchable(text: $searchText) {}
        // Nothing changed past here...
    }
}

Essentially the filter I set up is this:

if searchText is empty, return TRUE so the item is used;

if searchText is not empty, evaluate the other side of the OR which is 2 conditions with an OR. If either title OR textBody contains searchText, return TRUE so item is used;

if everything returns false, don't use item.

One last thing, rename your entryID in your model struct to id, make the model struct conform to Identifiable and then your ForEach (leaving out the .filter can be this:

ForEach(blindzeln.favorites) { item in

as an Identifiable struct does not need to use id: in a ForEach initializer.


问:
How to add google authentication in an iframe?
I'm trying to implement google OAuth into an iframe, but i saw that it has blocked due to security issues.

Is there any workaround/customization that can be done to make it work.
Note: I have used keycloak google auth as well.
So it would be great if it can be made possible from keycloak.

答:
You can not.  You need to use a pop-up window.

accounts.google.com sets X-Frame-Options to deny


问:
How to configure gce to route paths (rewrite-target in Nginx)
I had a Kibana that was previously running behind the NGINX ingress controller using this Ingress configuration:

```<code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: es-kibana-ing
  annotations:
    nginx.ingress.kubernetes.io/backend-protocol: HTTPS
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
    - host: "example.com"
      http:
        paths:
          - path: /kibana(/|$)(.*)
            pathType: Prefix
            backend:
              serviceName: es-kibana-svc
              servicePort: 443
  tls:
    - hosts:
      - example.com
      secretName: example-tls
</code>```

With this configuration you had to go to www.example.com/kibana to access the kibana.
Since then we migrated to GCP and now I'm trying to achieve the same using the GCE ingress controller. For now I figured how to serve the kibana on path "/*" :

```<code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: es-kibana-ing
  annotations:
    kubernetes.io/ingress.class: gce
    kubernetes.io/ingress.global-static-ip-name: kibana-static-ip
    networking.gke.io/managed-certificates: managed-cert
spec:
  rules:
    - host: "example.com"
      http:
        paths:
          - path: /*
            pathType: ImplementationSpecific
            backend:
              service:
                name: es-kibana-svc
                port:
                  number: 443
</code>```

Instead I would like to serve the Kibana on the /kibana (as in the previous Nginx configuration), but I can't find an equivalent to rewrite-target for the gce controller. Any idea how this can be done?

答:
It seems you may be using a different NGINX ingress controller and therefore annotations don't work as expected. You may find explanation of differences here.

Plus this closed GitHub issue seems to be very similar to yours so hopefully you can try using the solution mentioned there.

Body:
I had a Kibana that was previously running behind the NGINX ingress controller using this Ingress configuration:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: es-kibana-ing
  annotations:
    nginx.ingress.kubernetes.io/backend-protocol: HTTPS
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
    - host: "example.com"
      http:
        paths:
          - path: /kibana(/|$)(.*)
            pathType: Prefix
            backend:
              serviceName: es-kibana-svc
              servicePort: 443
  tls:
    - hosts:
      - example.com
      secretName: example-tls

With this configuration you had to go to www.example.com/kibana to access the kibana.
Since then we migrated to GCP and now I'm trying to achieve the same using the GCE ingress controller. For now I figured how to serve the kibana on path "/*" :

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: es-kibana-ing
  annotations:
    kubernetes.io/ingress.class: gce
    kubernetes.io/ingress.global-static-ip-name: kibana-static-ip
    networking.gke.io/managed-certificates: managed-cert
spec:
  rules:
    - host: "example.com"
      http:
        paths:
          - path: /*
            pathType: ImplementationSpecific
            backend:
              service:
                name: es-kibana-svc
                port:
                  number: 443

Instead I would like to serve the Kibana on the /kibana (as in the previous Nginx configuration), but I can't find an equivalent to rewrite-target for the gce controller. Any idea how this can be done?


问:
Count occurrences of a given integer in an array using recursion
Without using a loop, I'm trying to count the number of times a given integer is in an array using recursion. I keep getting a StackOverflow error and I can't figure out why.

```<code>public static int countOccurrences(int[] arr, int n) {
    if (arr.length == 0) {
        return 0;
    }
    if (arr[0] == n) {
        return 1 + countOccurrences(arr, n - 1);
    }
    return countOccurrences(arr, n - 1);
}
</code>```

}

答:
If you can use only two parameters, then try:</p>
```<code>    public static int countOccurrences(int[] arr, int n) {
        if (arr.length == 0) {
            return 0;
        }
        if (arr[0] == n) {
            return 1 + countOccurrences(Arrays.copyOfRange(arr, 1, arr.length), n);
        }
        return countOccurrences(Arrays.copyOfRange(arr, 1, arr.length), n);
    }
</code>```

Body:
Without using a loop, I'm trying to count the number of times a given integer is in an array using recursion. I keep getting a StackOverflow error and I can't figure out why.

public static int countOccurrences(int[] arr, int n) {
    if (arr.length == 0) {
        return 0;
    }
    if (arr[0] == n) {
        return 1 + countOccurrences(arr, n - 1);
    }
    return countOccurrences(arr, n - 1);
}

}

Body:
If you can use only two parameters, then try:

    public static int countOccurrences(int[] arr, int n) {
        if (arr.length == 0) {
            return 0;
        }
        if (arr[0] == n) {
            return 1 + countOccurrences(Arrays.copyOfRange(arr, 1, arr.length), n);
        }
        return countOccurrences(Arrays.copyOfRange(arr, 1, arr.length), n);
    }


问:
Merge dictionary with several levels
My goal is to merge following four Jsons into one:

```<code>'{}'

'{"A (1)": {"B (2)": {"C (3)": [{"fewfesfz": 48, "qwerty": 2}]}}}'

'{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}}'

'{"A (1)": {"E (2)": {"F (3)": [{"blablabla": 20}]}}}'
</code>```

The requirements are:

It shall be written in Python 3.

Dictionary to far right: only values are allowed to be overwritten.

All dictionaries keys are NOT allowed to be
overwritten nor deleted. This is also the keys for dictionaries far right. Keys are not allowed to be lost when merging two jsons.

When merging two dictionaries with same keys, then the values are appended/added.

The final result shall look like this:

```<code>{
"A (1)": {
    "B (2)": {
        "C (3)": [
            {
                "fewfesfz": 48,
                "qwerty": 2
            }
        ],
        "D (3)": [
            {
                "blablabla": 1
            }
        ]
    },
    "E (2)": {
        "F (3)": [
            {
                "blablabla": 20
            }
        ]
    }
}
</code>```

}

My solution is the following:

```<code>import json

s = json.loads('{}')
x = json.loads('{"A (1)": {"B (2)": {"C (3)": [{"fewfesfz": 48, "qwerty": 2}]}}}')
y = json.loads('{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}}')
z = json.loads('{"A (1)": {"E (2)": {"F (3)": [{"blablabla": 20}]}}}')

def merge_json(json1, json2):
    try:
        json1['A (1)']['B (2)'].update(json2['A (1)']['B (2)'])
        return json1
    except KeyError:
        return {**json1, **json2}

result = merge_json(x, s)
result = merge_json(result, y)
result['A (1)'].update(z['A (1)'])
print(json.dumps(x, indent=4))
</code>```

As you can see, it is very hard to manage long term.
Specially, if the depths gets changed from 3 to 4 nodes or from 3 to 2 nodes.
There is no way of knowing, in which order the big letters (A, B, C, D...) come.
So far, I can only come up with extremely complex functions with lot of if statements to test if the nodes exists or not. Only then I know, if I can make full merge or need to call the update dictionary method.

Have anyone a better solution how to solve this problem, then I would be very grateful.

EDIT 1 (Added more examples below):

If the following two json are merged

```<code>json1 = '{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}}'
json2 = '{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 2}]}}}'
</code>```

Result would be:

```<code>'{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 2}]}}}'
</code>```

Only the value for the key "blablabla" changed from 1 to 2.
This happens because it was processed later.

If following two json are merged:

```<code>json1 = '{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}}'
json2 = '{"A (1)": {"D (3)": [{"blablabla": 1}]}}'
</code>```

Result would be:

```<code>{'A (1)': {'B (2)': {'D (3)': [{'blablabla': 1}]}, 'D (3)': [{'blablabla': 1}]}}
</code>```

The input structure will remain the same in output.

答:
The real answer is here: https://stackoverflow.com/a/20666342/218663 but to demonstrate that you would get the results you seek try:

def merge(source, destination):
    for key, value in source.items():
        if isinstance(value, dict):
            merge(value, destination.setdefault(key, {}))
        else:
            destination[key] = value
    return destination

tests = [
    {
        "given": [
            {},
            {"A (1)": {"B (2)": {"C (3)": [{"fewfesfz": 48, "qwerty": 2}]}}},
            {"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}},
            {"A (1)": {"E (2)": {"F (3)": [{"blablabla": 20}]}}}
        ],
        "expected": {
            "A (1)": {
                "B (2)": {
                    "C (3)": [{"fewfesfz": 48,"qwerty": 2}],
                    "D (3)": [{"blablabla": 1}]
                    },
                "E (2)": {
                    "F (3)": [{"blablabla": 20}]
                }
            }
        }
    },
    {
        "given": [
            {"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}},
            {"A (1)": {"B (2)": {"D (3)": [{"blablabla": 2}]}}}
        ],
        "expected": {"A (1)": {"B (2)": {"D (3)": [{"blablabla": 2}]}}}
    },
    {
        "given": [
            {"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}},
            {"A (1)": {"D (3)": [{"blablabla": 1}]}}
        ],
        "expected": {'A (1)': {'B (2)': {'D (3)': [{'blablabla': 1}]}, 'D (3)': [{'blablabla': 1}]}}
    }
]

for test in tests:
    result = {}
    for item in test["given"]:
        merge(item, result)
    print(result==test["expected"])
```
<p>Should give you:</p>
```True
True
True
```

If this fits your needs, feel free to delete this question rather than accepting this as an answer :-)

Title: Merge dictionary with several levels

Body:
My goal is to merge following four Jsons into one:

'{}'

'{"A (1)": {"B (2)": {"C (3)": [{"fewfesfz": 48, "qwerty": 2}]}}}'

'{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}}'

'{"A (1)": {"E (2)": {"F (3)": [{"blablabla": 20}]}}}'

The requirements are:

It shall be written in Python 3.

Dictionary to far right: only values are allowed to be overwritten.

All dictionaries keys are NOT allowed to be
overwritten nor deleted. This is also the keys for dictionaries far right. Keys are not allowed to be lost when merging two jsons.

When merging two dictionaries with same keys, then the values are appended/added.

The final result shall look like this:

{
"A (1)": {
    "B (2)": {
        "C (3)": [
            {
                "fewfesfz": 48,
                "qwerty": 2
            }
        ],
        "D (3)": [
            {
                "blablabla": 1
            }
        ]
    },
    "E (2)": {
        "F (3)": [
            {
                "blablabla": 20
            }
        ]
    }
}

}

My solution is the following:

import json

s = json.loads('{}')
x = json.loads('{"A (1)": {"B (2)": {"C (3)": [{"fewfesfz": 48, "qwerty": 2}]}}}')
y = json.loads('{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}}')
z = json.loads('{"A (1)": {"E (2)": {"F (3)": [{"blablabla": 20}]}}}')

def merge_json(json1, json2):
    try:
        json1['A (1)']['B (2)'].update(json2['A (1)']['B (2)'])
        return json1
    except KeyError:
        return {**json1, **json2}

result = merge_json(x, s)
result = merge_json(result, y)
result['A (1)'].update(z['A (1)'])
print(json.dumps(x, indent=4))

As you can see, it is very hard to manage long term.
Specially, if the depths gets changed from 3 to 4 nodes or from 3 to 2 nodes.
There is no way of knowing, in which order the big letters (A, B, C, D...) come.
So far, I can only come up with extremely complex functions with lot of if statements to test if the nodes exists or not. Only then I know, if I can make full merge or need to call the update dictionary method.

Have anyone a better solution how to solve this problem, then I would be very grateful.

EDIT 1 (Added more examples below):

If the following two json are merged

json1 = '{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}}'
json2 = '{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 2}]}}}'

Result would be:

'{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 2}]}}}'

Only the value for the key "blablabla" changed from 1 to 2.
This happens because it was processed later.

If following two json are merged:

json1 = '{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}}'
json2 = '{"A (1)": {"D (3)": [{"blablabla": 1}]}}'

Result would be:

{'A (1)': {'B (2)': {'D (3)': [{'blablabla': 1}]}, 'D (3)': [{'blablabla': 1}]}}

The input structure will remain the same in output.

Body:
The real answer is here: https://stackoverflow.com/a/20666342/218663 but to demonstrate that you would get the results you seek try:

def merge(source, destination):
    for key, value in source.items():
        if isinstance(value, dict):
            merge(value, destination.setdefault(key, {}))
        else:
            destination[key] = value
    return destination

tests = [
    {
        "given": [
            {},
            {"A (1)": {"B (2)": {"C (3)": [{"fewfesfz": 48, "qwerty": 2}]}}},
            {"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}},
            {"A (1)": {"E (2)": {"F (3)": [{"blablabla": 20}]}}}
        ],
        "expected": {
            "A (1)": {
                "B (2)": {
                    "C (3)": [{"fewfesfz": 48,"qwerty": 2}],
                    "D (3)": [{"blablabla": 1}]
                    },
                "E (2)": {
                    "F (3)": [{"blablabla": 20}]
                }
            }
        }
    },
    {
        "given": [
            {"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}},
            {"A (1)": {"B (2)": {"D (3)": [{"blablabla": 2}]}}}
        ],
        "expected": {"A (1)": {"B (2)": {"D (3)": [{"blablabla": 2}]}}}
    },
    {
        "given": [
            {"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}},
            {"A (1)": {"D (3)": [{"blablabla": 1}]}}
        ],
        "expected": {'A (1)': {'B (2)': {'D (3)': [{'blablabla': 1}]}, 'D (3)': [{'blablabla': 1}]}}
    }
]

for test in tests:
    result = {}
    for item in test["given"]:
        merge(item, result)
    print(result==test["expected"])

Should give you:

True
True
True

If this fits your needs, feel free to delete this question rather than accepting this as an answer :-)


问:
How to return the object in an array object in mongoose
I currently have a UserModel and a House model where the house model contains an array of user objects. However, when I return I want to return an array of user objects instead of just users objectId. How do I accomplish this?

For example, if I run

HouseModel.findbyId("asdf")
it returns:

```<code>_id: "asdf"
"name": "name",
    "users": [
        "620044aa7811fb4ab4619e44",
"620044aa7811fb4ab4619e45"
    ],
</code>```

Any help would be appreciated

but I want to return:

```<code>``
_id: "asdf"
"name": "name",
    "users": [
        {_id: "620044aa7811fb4ab4619e44", name: "bob" age: 7,},
{_id: "620044aa7811fb4ab4619e45", name: "bob" age: 7,}
    ],
``
</code>```

HouseSchema

```<code>const houseSchema = new mongoose.Schema({
    name: {
        type: String,
        required: false
    },
    users: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User",
            default: [],
            required: true,
        }
    ],
</code>```

UserSchema

```<code>const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        unique: true,
    },
    age: {
        type: Number,
        required: false,
        unique: true,
    },
</code>```

答:
use populate

HouseModel.findbyId("asdf").populate('users')

Body:
I currently have a UserModel and a House model where the house model contains an array of user objects. However, when I return I want to return an array of user objects instead of just users objectId. How do I accomplish this?

For example, if I run

HouseModel.findbyId("asdf")
it returns:

_id: "asdf"
"name": "name",
    "users": [
        "620044aa7811fb4ab4619e44",
"620044aa7811fb4ab4619e45"
    ],

Any help would be appreciated

but I want to return:

``
_id: "asdf"
"name": "name",
    "users": [
        {_id: "620044aa7811fb4ab4619e44", name: "bob" age: 7,},
{_id: "620044aa7811fb4ab4619e45", name: "bob" age: 7,}
    ],
``

HouseSchema

const houseSchema = new mongoose.Schema({
    name: {
        type: String,
        required: false
    },
    users: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User",
            default: [],
            required: true,
        }
    ],

UserSchema

const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        unique: true,
    },
    age: {
        type: Number,
        required: false,
        unique: true,
    },


问:
Extbase Query filter by child in ObjectStorage
So I have a Item model which has a Recipe 1:1 relation.

The Recipe (model) has an ObjectStorage containing 1:n CraftingMaterials (model) which are needed for crafting. Each CraftingMaterial has an amount field and one relation to an Item (model)

Model Dump:

public function findItemUsedIn($item)
{
    $query = $this->createQuery();
    $query->matching(
        $query->logicalAnd(
            $query->in('material.item', [$item])
        )
    );
    return $query->execute();
}
```

material.item should be an ObjectStorage/Collection of Items.

So my goal is to filter if they contain the $item to find all recipes which got this item assigned, but Extbase cannot find any results. The backend runs fine and I can assign all these items.

Somehow Extbase hang up materials being an object storage and tries to match the id of the material relation and not the item one level deeper.

答:
I think you're confusing in()and contains(). As in() checks if a single-valued property exists in a multi-value operand, the latter method contains() checks if a multi-valued property contains a single-valued operand.

```<code>public function findItemUsedIn($item)
{
    $query = $this->createQuery();
    $query->matching(
        $query->logicalAnd(
            $query->contains('material', $item)
        )
    );
    return $query->execute();
}
</code>```

Body:
So I have a Item model which has a Recipe 1:1 relation.

The Recipe (model) has an ObjectStorage containing 1:n CraftingMaterials (model) which are needed for crafting. Each CraftingMaterial has an amount field and one relation to an Item (model)

Model Dump:

public function findItemUsedIn($item)
{
    $query = $this->createQuery();
    $query->matching(
        $query->logicalAnd(
            $query->in('material.item', [$item])
        )
    );
    return $query->execute();
}

material.item should be an ObjectStorage/Collection of Items.

So my goal is to filter if they contain the $item to find all recipes which got this item assigned, but Extbase cannot find any results. The backend runs fine and I can assign all these items.

Somehow Extbase hang up materials being an object storage and tries to match the id of the material relation and not the item one level deeper.

Body:
I think you're confusing in()and contains(). As in() checks if a single-valued property exists in a multi-value operand, the latter method contains() checks if a multi-valued property contains a single-valued operand.

public function findItemUsedIn($item)
{
    $query = $this->createQuery();
    $query->matching(
        $query->logicalAnd(
            $query->contains('material', $item)
        )
    );
    return $query->execute();
}


问:
Get input of txt file from user in Console.net in CSharp
I'm using csharp in console.net
I want to get a txt file from the user as input
But I do not want the text inside the txt file to be displayed to the user
Instead, I want the text to be hidden and the number of lines to be displayed instead</p>

答:
If I understood you fine, you want to count the number of lines in the txt file, right?
A basic approach could be:</p>
```<code>string[] fileLines = System.IO.File.ReadAllLines("<path to the file>");
if (fileLines != null)
Console.WriteLine($"The file has {fileLines.Length} lines.");
</code>```

Body:
If I understood you fine, you want to count the number of lines in the txt file, right?
A basic approach could be:

string[] fileLines = System.IO.File.ReadAllLines("<path to the file>");
if (fileLines != null)
Console.WriteLine($"The file has {fileLines.Length} lines.");


问:
How do I remove json data from a list and then flatten into columns in Python?
I have this json data from Amazon..

```<code>[{'sellingProgramName': 'AMAZON_PRIME_NOW', 'salesMetricsByProgram': []},
 {'sellingProgramName': 'AMAZON_FRESH', 'salesMetricsByProgram': []},
 {'sellingProgramName': 'AMAZON_RETAIL',
  'salesMetricsByProgram': [{'asin': 'B001U81442',
    'salesMetricsByAsin': [{'reportingDate': '2021-01-20',
      'salesMetricsByDate': {'shippedRevenue': {'amount': 9.99,
        'currencyCode': 'USD'},
       'shippedCOGS': {'amount': 5.25, 'currencyCode': 'USD'},
       'shippedUnits': 1,
       'customerReturns': 0,
       'freeReplacements': 0,
       'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedRevenue': {'amount': 9.99, 'currencyCode': 'USD'},
       'orderedUnits': 1,
       'preOrderedUnits': 0,
       'shippedSubcategorySalesRank': 84,
       'orderedSubcategorySalesRank': 83,
       'replenishableOutOfStock': 0.0,
       'lostBuyBox': 0.0,
       'glanceViews': 3,
       'conversionRate': 0.3333}},
     {'reportingDate': '2021-01-21',
      'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0,
        'currencyCode': 'USD'},
       'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'},
       'shippedUnits': 0,
       'customerReturns': 0,
       'freeReplacements': 0,
       'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedUnits': 0,
       'preOrderedUnits': 0,
       'shippedSubcategorySalesRank': None,
       'orderedSubcategorySalesRank': 83,
       'replenishableOutOfStock': 0.0,
       'lostBuyBox': 0.0,
       'glanceViews': 0,
       'conversionRate': 0.0}}]},
   {'asin': 'B003SVYRNE',
    'salesMetricsByAsin': [{'reportingDate': '2021-06-28',
      'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0,
        'currencyCode': 'USD'},
       'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'},
       'shippedUnits': 0,
       'customerReturns': 1,
       'freeReplacements': 0,
       'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedUnits': 0,
       'preOrderedUnits': 0,
       'shippedSubcategorySalesRank': None,
       'orderedSubcategorySalesRank': None,
       'replenishableOutOfStock': 0.0,
       'lostBuyBox': 0.0,
       'glanceViews': 0,
       'conversionRate': 0.0}},
     {'reportingDate': '2021-06-29',
      'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0,
        'currencyCode': 'USD'},
       'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'},
       'shippedUnits': 0,
       'customerReturns': 0,
       'freeReplacements': 0,
       'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedUnits': 0,
       'preOrderedUnits': 0,
       'shippedSubcategorySalesRank': None,
       'orderedSubcategorySalesRank': None,
       'replenishableOutOfStock': 0.0,
       'lostBuyBox': 0.0,
       'glanceViews': 0,
       'conversionRate': 0.0}}]},

</code>```

I've used json_normalize to get the data into this dataframe using this code

```<code>df = pd.json_normalize(data=j,record_path=['salesMetricsByProgram'],errors='ignore')
</code>```

and I get this dataframe

```<code>    asin        salesMetricsByAsin
0   B001U81442  [{'reportingDate': '2021-01-20', 'salesMetricsByDate': {'shippedRevenue': {'amount': 9.99, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 5.25, 'currencyCode': 'USD'}, 'shippedUnits': 1, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 9.99, 'currencyCode': 'USD'}, 'orderedUnits': 1, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': 84, 'orderedSubcategorySalesRank': 83, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 3, 'conversionRate': 0.3333}}, {'reportingDate': '2021-01-21', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': 83, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}]    AMAZON_RETAIL
1   B003SVYRNE  [{'reportingDate': '2021-06-28', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 1, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': None, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}, {'reportingDate': '2021-06-29', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': None, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}]    AMAZON_RE

</code>```

I'm trying to normalize the SalesMetricsByAsin column by using this code, while keeping the asin column

```<code>df['salesMetricsByAsin'] = pd.json_normalize(df['salesMetricsByAsin'],errors = 'ignore')

</code>```

and I get this error

AttributeError: 'list' object has no attribute 'values'

It looks like the json data is nested in a list.  How do I remove the data from the list  so I can flatten the data into separate columns, while keeping the asin column?

答:
You are executing json_normalize on a list of dictionaries or with at least one dictionary. You could try this:

```<code>df['salesMetricsByAsin'] = [pd.json_normalize(d, errors='ignore') for d in df['salesMetricsByAsin']]
</code>```

But the output would be a list too, but a list of JSON data. So you'd have to think wether you are going to consume this column as a list or spread it.

Body:
I have this json data from Amazon..

[{'sellingProgramName': 'AMAZON_PRIME_NOW', 'salesMetricsByProgram': []},
 {'sellingProgramName': 'AMAZON_FRESH', 'salesMetricsByProgram': []},
 {'sellingProgramName': 'AMAZON_RETAIL',
  'salesMetricsByProgram': [{'asin': 'B001U81442',
    'salesMetricsByAsin': [{'reportingDate': '2021-01-20',
      'salesMetricsByDate': {'shippedRevenue': {'amount': 9.99,
        'currencyCode': 'USD'},
       'shippedCOGS': {'amount': 5.25, 'currencyCode': 'USD'},
       'shippedUnits': 1,
       'customerReturns': 0,
       'freeReplacements': 0,
       'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedRevenue': {'amount': 9.99, 'currencyCode': 'USD'},
       'orderedUnits': 1,
       'preOrderedUnits': 0,
       'shippedSubcategorySalesRank': 84,
       'orderedSubcategorySalesRank': 83,
       'replenishableOutOfStock': 0.0,
       'lostBuyBox': 0.0,
       'glanceViews': 3,
       'conversionRate': 0.3333}},
     {'reportingDate': '2021-01-21',
      'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0,
        'currencyCode': 'USD'},
       'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'},
       'shippedUnits': 0,
       'customerReturns': 0,
       'freeReplacements': 0,
       'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedUnits': 0,
       'preOrderedUnits': 0,
       'shippedSubcategorySalesRank': None,
       'orderedSubcategorySalesRank': 83,
       'replenishableOutOfStock': 0.0,
       'lostBuyBox': 0.0,
       'glanceViews': 0,
       'conversionRate': 0.0}}]},
   {'asin': 'B003SVYRNE',
    'salesMetricsByAsin': [{'reportingDate': '2021-06-28',
      'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0,
        'currencyCode': 'USD'},
       'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'},
       'shippedUnits': 0,
       'customerReturns': 1,
       'freeReplacements': 0,
       'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedUnits': 0,
       'preOrderedUnits': 0,
       'shippedSubcategorySalesRank': None,
       'orderedSubcategorySalesRank': None,
       'replenishableOutOfStock': 0.0,
       'lostBuyBox': 0.0,
       'glanceViews': 0,
       'conversionRate': 0.0}},
     {'reportingDate': '2021-06-29',
      'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0,
        'currencyCode': 'USD'},
       'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'},
       'shippedUnits': 0,
       'customerReturns': 0,
       'freeReplacements': 0,
       'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'},
       'orderedUnits': 0,
       'preOrderedUnits': 0,
       'shippedSubcategorySalesRank': None,
       'orderedSubcategorySalesRank': None,
       'replenishableOutOfStock': 0.0,
       'lostBuyBox': 0.0,
       'glanceViews': 0,
       'conversionRate': 0.0}}]},

I've used json_normalize to get the data into this dataframe using this code

df = pd.json_normalize(data=j,record_path=['salesMetricsByProgram'],errors='ignore')

and I get this dataframe

    asin        salesMetricsByAsin
0   B001U81442  [{'reportingDate': '2021-01-20', 'salesMetricsByDate': {'shippedRevenue': {'amount': 9.99, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 5.25, 'currencyCode': 'USD'}, 'shippedUnits': 1, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 9.99, 'currencyCode': 'USD'}, 'orderedUnits': 1, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': 84, 'orderedSubcategorySalesRank': 83, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 3, 'conversionRate': 0.3333}}, {'reportingDate': '2021-01-21', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': 83, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}]    AMAZON_RETAIL
1   B003SVYRNE  [{'reportingDate': '2021-06-28', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 1, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': None, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}, {'reportingDate': '2021-06-29', 'salesMetricsByDate': {'shippedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedCOGS': {'amount': 0.0, 'currencyCode': 'USD'}, 'shippedUnits': 0, 'customerReturns': 0, 'freeReplacements': 0, 'preOrderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedRevenue': {'amount': 0.0, 'currencyCode': 'USD'}, 'orderedUnits': 0, 'preOrderedUnits': 0, 'shippedSubcategorySalesRank': None, 'orderedSubcategorySalesRank': None, 'replenishableOutOfStock': 0.0, 'lostBuyBox': 0.0, 'glanceViews': 0, 'conversionRate': 0.0}}]    AMAZON_RE

I'm trying to normalize the SalesMetricsByAsin column by using this code, while keeping the asin column

df['salesMetricsByAsin'] = pd.json_normalize(df['salesMetricsByAsin'],errors = 'ignore')

and I get this error

AttributeError: 'list' object has no attribute 'values'

It looks like the json data is nested in a list.  How do I remove the data from the list  so I can flatten the data into separate columns, while keeping the asin column?

Body:
You are executing json_normalize on a list of dictionaries or with at least one dictionary. You could try this:

df['salesMetricsByAsin'] = [pd.json_normalize(d, errors='ignore') for d in df['salesMetricsByAsin']]

But the output would be a list too, but a list of JSON data. So you'd have to think wether you are going to consume this column as a list or spread it.


问:
Month by Month Sales breakdown for top 10 Product for specific year Using Adventure Works Database in SQL Server
This is what I used to find the top 10 products for 2013 by total sum of sales. What is the easiest way to get a monthly breakdown of sales for a specific product ID for the year?

```<code>SELECT TOP 10
    sod.ProductID, prd.Name, SUM(LineTotal) AS SumOfSales
FROM
    Sales.SalesOrderDetail AS SOD
JOIN
    Sales.SalesOrderHeader AS SOH ON SOD.SalesOrderID = SOH.SalesOrderID
JOIN
    Production.Product prd ON prd.ProductID = sod.ProductID
WHERE
    SOH.OrderDate >= '01/01/2013'
    AND SOH.OrderDate <= '12/31/2013'
GROUP BY
    sod.ProductID, prd.Name
-- HAVING SUM(LineTotal) >= 2000000
ORDER BY
    SUM(LineTotal) DESC
</code>```

Image of SQL output

This is what I found after research but it says date_format is invalid

```<code>select date_format(sdate,'%M-%Y') as sdate,
sum(LineTotal) as 'netsales',
from Sales.SalesOrderDetail
where ProductID=782 and ModifiedDate >= '01/01/2013' and ModifiedDate <= '12/31/2013'
group by MONTH(sdate)
order by MONTH(sdate);
</code>```

答:
Full list:

```<code>select *
from Sales.SalesOrderDetail
where ProductID=782 and DATEPART(YEAR,ModifiedDate) = 2013
</code>```

You can use "*" or column names

Mothly Group List:

```<code>select datepart(month,ModifiedDate) as sdate,
sum(LineTotal) as 'netsales',
from Sales.SalesOrderDetail
group by datepart(month,ModifiedDate)
</code>```

but you will see month number in sdate column.

Body:
This is what I used to find the top 10 products for 2013 by total sum of sales. What is the easiest way to get a monthly breakdown of sales for a specific product ID for the year?

SELECT TOP 10
    sod.ProductID, prd.Name, SUM(LineTotal) AS SumOfSales
FROM
    Sales.SalesOrderDetail AS SOD
JOIN
    Sales.SalesOrderHeader AS SOH ON SOD.SalesOrderID = SOH.SalesOrderID
JOIN
    Production.Product prd ON prd.ProductID = sod.ProductID
WHERE
    SOH.OrderDate >= '01/01/2013'
    AND SOH.OrderDate <= '12/31/2013'
GROUP BY
    sod.ProductID, prd.Name
-- HAVING SUM(LineTotal) >= 2000000
ORDER BY
    SUM(LineTotal) DESC

Image of SQL output

This is what I found after research but it says date_format is invalid

select date_format(sdate,'%M-%Y') as sdate,
sum(LineTotal) as 'netsales',
from Sales.SalesOrderDetail
where ProductID=782 and ModifiedDate >= '01/01/2013' and ModifiedDate <= '12/31/2013'
group by MONTH(sdate)
order by MONTH(sdate);

Body:
Full list:

select *
from Sales.SalesOrderDetail
where ProductID=782 and DATEPART(YEAR,ModifiedDate) = 2013

You can use "*" or column names

Mothly Group List:

select datepart(month,ModifiedDate) as sdate,
sum(LineTotal) as 'netsales',
from Sales.SalesOrderDetail
group by datepart(month,ModifiedDate)

but you will see month number in sdate column.


问:
org.apache.hadoop.security.AccessControlException: SIMPLE authentication is not enabled. Available:[TOKEN, KERBEROS]
I am trying to execute the hive table query in presto  using Kerberos authentication, But it failed

Caused by: com.facebook.presto.spi.PrestoException: Error opening Hive split hdfs://ip-10-13-9-237.xxxx.com:8020/user/hive/warehouse/extended_test_db_hive2.db/table_with_all_data_types/000000_0_copy_2 (offset=0, length=124) using org.apache.hadoop.mapred.TextInputFormat: org.apache.hadoop.security.AccessControlException: SIMPLE authentication is not enabled.  Available:[TOKEN, KERBEROS]

Anyone please help

答:
This error message tells me that you aren't actually using the correct authentication mechanism.  You presented [nothing] or [user/name password] and it's expecting [Kerberos] or [token].  You likely need to use a different connection string to make sure you connect via kerberos.

Usually you have to change your connection to something like:

```<code>jdbc:hive2://[my_hive_server]:[port]/[database];principal=myPrincipel@EXAMPLE.COM
</code>```

or adjust your jaas config to something like:

```<code>com.sun.security.jgss.initiate {
com.sun.security.auth.module.Krb5LoginModule required
useTicketCache=false
doNotPrompt=true
useKeyTab=true
keyTab="<QuerySurge Install Dir>/QuerySurge/agent/mykeytab.keytab"
principal="myPrincipel@EXAMPLE.COM"
};
</code>```

Body:
This error message tells me that you aren't actually using the correct authentication mechanism.  You presented [nothing] or [user/name password] and it's expecting [Kerberos] or [token].  You likely need to use a different connection string to make sure you connect via kerberos.

Usually you have to change your connection to something like:

jdbc:hive2://[my_hive_server]:[port]/[database];principal=myPrincipel@EXAMPLE.COM

or adjust your jaas config to something like:

com.sun.security.jgss.initiate {
com.sun.security.auth.module.Krb5LoginModule required
useTicketCache=false
doNotPrompt=true
useKeyTab=true
keyTab="<QuerySurge Install Dir>/QuerySurge/agent/mykeytab.keytab"
principal="myPrincipel@EXAMPLE.COM"
};


问:
How to change the introduction duration of an App Store subscription?
I have an App Store subscription with an introductory price with a duration of 3 days. I can see this by checking my SKProduct's introductoryPrice property, which has a subscription period of 3 days:

```<code>let product: SKProduct
...
print(product.introductoryPrice.subscriptionPeriod)
</code>```

I can also check this in App Store Connect:

It says 'free for the first 3 days', but there is no way to edit that. I can only click on 'no end date' and set an end date, which is not what I want. Is that editable and if yes how?

答:
According to the official docs on Set an introductory offer for an auto-renewable subscription, only the end date can be edited once it's available to customers.

Once you create an introductory price or free trial, only the End Date of the in effect introductory offer can be edited once it's available to customers. For future scheduled introductory offers, the start and end date can be edited. To edit the duration or price, you must create a new introductory offer.

Body:
I have an App Store subscription with an introductory price with a duration of 3 days. I can see this by checking my SKProduct's introductoryPrice property, which has a subscription period of 3 days:

let product: SKProduct
...
print(product.introductoryPrice.subscriptionPeriod)

I can also check this in App Store Connect:

It says 'free for the first 3 days', but there is no way to edit that. I can only click on 'no end date' and set an end date, which is not what I want. Is that editable and if yes how?


问:
Make divs within a container only take up necessary space
I'm trying to created some images with dashes between them and where each div only takes up the necessary space within the container. However, from what I've attempted they all take up an even amount of space, when this shouldn't be the case for the last div. The width of the hr is 100% so in some cases this no longer shows up if the width of the div isn't defined.

How could I fix this?

Current result:

Code:

.hr {
border-top: 10px dashed black;
background-color:transparent;

.div2 {
align-items: center;
padding: 0;
flex-grow: 1;
margin-bottom: 0;
display: inline-flex;
}

.div1 {
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
text-align: center;
position: absolute;
display: flex;

}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="div1">
 <div class="div2">
  <img src="circle.svg">
  <hr class="hr">
 </div>
 <div class="div2">
  <img src="circle.svg">
  <hr class="hr">
 </div>
 <div class="div2">
  <img src="circle.svg">
  <hr class="hr">
 </div>
 <div class="div2">
  <img src="circle.svg">
 </div>
</div></code>```

答: Add flex-shrink: 1; to .div2 to allow them to get smaller


问:
how to calculate running median efficiently
I borrowed some code trying to implement a function to calculate the running median for a ton of data. The current one is too slow for me (The tricky part is that I need to exclude all zeros from the running box). Below is the code:

```<code>from itertools import islice
from collections import deque
from bisect import bisect_left,insort

def median(s):
    sp = [nz for nz in s if nz!=0]
    print sp
    Mnow = len(sp)
    if Mnow == 0:
        return 0
    else:
        return np.median(sp)

def RunningMedian(seq, M):
    seq = iter(seq)
    s = []

    # Set up list s (to be sorted) and load deque with first window of seq
    s = [item for item in islice(seq,M)]
    d = deque(s)

    # Sort it in increasing order and extract the median ("center" of the sorted window)
    s.sort()
    medians = [median(s)]
    for item in seq:
        old = d.popleft()          # pop oldest from left
        d.append(item)             # push newest in from right
        del s[bisect_left(s, old)] # locate insertion point and then remove old
        insort(s, item)            # insert newest such that new sort is not required
        medians.append(median(s))
    return medians
</code>```

It works well, the only drawback is that it is too slow. Any one could help me to improve the code to be more efficient?

After I explored all the possibilities, the following simple code can calculate comparably efficiently:

```<code>def RunningMedian(x,N):
    idx = np.arange(N) + np.arange(len(x)-N+1)[:,None]
    b = [row[row>0] for row in x[idx]]
    return np.array(map(np.median,b))
    #return np.array([np.median(c) for c in b])  # This also works
</code>```

答:
I faced this problem but not as a programmer. I wanted dedicated hardware to do a moving median of a sequence, with a fairly large moving window.

I did it with the two heaps as suggested above, but I also noticed that all the data movement and comparisons within a heap can be done in parallel. So the time required to insert a datum into either heap or to move a datum from either heap was  O(1), a constant.

Body:
I borrowed some code trying to implement a function to calculate the running median for a ton of data. The current one is too slow for me (The tricky part is that I need to exclude all zeros from the running box). Below is the code:

from itertools import islice
from collections import deque
from bisect import bisect_left,insort

def median(s):
    sp = [nz for nz in s if nz!=0]
    print sp
    Mnow = len(sp)
    if Mnow == 0:
        return 0
    else:
        return np.median(sp)

def RunningMedian(seq, M):
    seq = iter(seq)
    s = []

    # Set up list s (to be sorted) and load deque with first window of seq
    s = [item for item in islice(seq,M)]
    d = deque(s)

    # Sort it in increasing order and extract the median ("center" of the sorted window)
    s.sort()
    medians = [median(s)]
    for item in seq:
        old = d.popleft()          # pop oldest from left
        d.append(item)             # push newest in from right
        del s[bisect_left(s, old)] # locate insertion point and then remove old
        insort(s, item)            # insert newest such that new sort is not required
        medians.append(median(s))
    return medians

It works well, the only drawback is that it is too slow. Any one could help me to improve the code to be more efficient?

After I explored all the possibilities, the following simple code can calculate comparably efficiently:

def RunningMedian(x,N):
    idx = np.arange(N) + np.arange(len(x)-N+1)[:,None]
    b = [row[row>0] for row in x[idx]]
    return np.array(map(np.median,b))
    #return np.array([np.median(c) for c in b])  # This also works


问:
Why am I not able to install Android SDK?
I installed android sdk. When I click the android sdk manager. Then the the following error message is displayed in an alert box

Failed to fetch URL
  https://dl-ssl.google.com/android/repository/repository.xml,
  reason:
  sun. Security. Validator. ValidatorException:
  PKIX path validation failed:
  java.security.cert.certPathValidatorException:
  timestamp check failed

Please help me to solve the issues.

答: Try to use http instead of https in the settings</p>


问:
How to check if a value in the list exists in the dataframe?
Got a data frame that has 5 columns and a list that contains 20 values.

if the value in the list exactly matches any value in the columns then it has to append the value (list value) to an empty column.

```<code>list=["siper","glock","tip",............]
</code>```

INPUT (DATAFRAME ) DF1:

DESIRED OUTPUT:

My code to check if the value in the list exist in data frame.

```<code>list=["siper","glock","tip",............]
df2=[]
for i in list:
  mask=np.column_stack([df[col]==i for col in df])
  df2.append(df.loc[mask.any(axis=1)])
</code>```

The above code gives a list of all rows in the data frame if the value in the list matches any column but I am not sure how to append values in the list to column1 if there is any match. Also, I want to add "Unknow" to column1 if there is no match.

答:
Try str.extract:

```<code>lst = ['glock', 'siper']

df['D'] = df.apply(lambda x: x.str.extract(fr"\b({'|'.join(lst)})\b")
                              .bfill().iloc[0].fillna('unknown'), axis=1)
print(df)

# Output
                  A                B                       C        D
0            lfkdjs            siper              ldjkslkdjq    siper
1  the glock hammer     ldksqjflsdkj            dljkfdslkfjs    glock
2     lfdkslkdfjsdl    dflskjfsdlkjf                  tipper  unknown
3     fdlsjkfsldkjf  dlfjksdflkdsjfs  The glockmaster hammer  unknown
</code>```

Body:
Got a data frame that has 5 columns and a list that contains 20 values.

if the value in the list exactly matches any value in the columns then it has to append the value (list value) to an empty column.

list=["siper","glock","tip",............]

INPUT (DATAFRAME ) DF1:

DESIRED OUTPUT:

My code to check if the value in the list exist in data frame.

list=["siper","glock","tip",............]
df2=[]
for i in list:
  mask=np.column_stack([df[col]==i for col in df])
  df2.append(df.loc[mask.any(axis=1)])

The above code gives a list of all rows in the data frame if the value in the list matches any column but I am not sure how to append values in the list to column1 if there is any match. Also, I want to add "Unknow" to column1 if there is no match.

Body:
Try str.extract:

lst = ['glock', 'siper']

df['D'] = df.apply(lambda x: x.str.extract(fr"\b({'|'.join(lst)})\b")
                              .bfill().iloc[0].fillna('unknown'), axis=1)
print(df)

# Output
                  A                B                       C        D
0            lfkdjs            siper              ldjkslkdjq    siper
1  the glock hammer     ldksqjflsdkj            dljkfdslkfjs    glock
2     lfdkslkdfjsdl    dflskjfsdlkjf                  tipper  unknown
3     fdlsjkfsldkjf  dlfjksdflkdsjfs  The glockmaster hammer  unknown


问:
C#: Creating a generic extension method with a type parameter for a generic class without specifying the class type parameter
I would like to have a generic immutable data class with a list of extensions, which can be accessed using extension methods. I can do this with the following code:

```<code>public class GenericData<T> where T : unmanaged
{
    public GenericData(T data, IList<IDataExtension> extensions)
    {
        Data = data;
        Extensions = extensions.ToImmutableList();
    }

    public T Data { get; }

    public IImmutableList<IDataExtension> Extensions { get; }
}

public interface IDataExtension { }

public static class GenericDataExtensions
{
    public static TExtension GetExtension<TExtension, TDataType>(this GenericData<TDataType> data)
        where TExtension : IDataExtension
        where TDataType : unmanaged
    {
        return data.Extensions.OfType<TExtension>().First();
    }
}
</code>```

This works fine, but requires me to pass in TDataType every time I want to get an extension, even though the data type isn't needed for the extension method implementation.

```<code>public interface IEvenlySpacedXAxisExtension : IDataExtension
{
    EvenlySpacedXAxisInfo Info { get; }
}

// later...

var xAxisInfo = data.GetExtension<IEvenlySpacedXAxisExtension, float>().Info;
</code>```

I could solve this by putting GetExtension() behind an interface and making GenericData implement it:

```<code>public class GenericData<T> : IHaveDataExtensions where T : unmanaged
{
    public GenericData(T data, IList<IDataExtension> extensions)
    {
        Data = data;
        Extensions = extensions.ToImmutableList();
    }

    public T Data { get; }

    public IImmutableList<IDataExtension> Extensions { get; }
}

public interface IDataExtension { }

public interface IHaveDataExtensions
{
    IImmutableList<IDataExtension> Extensions { get; }
}

public static class IHaveExtensionsExtensions
{
    public static TExtension GetExtension<TExtension>(this IHaveDataExtensions objectWithExtensions)
        where TExtension : IDataExtension
    {
        return objectWithExtensions.Extensions.OfType<TExtension>().First();
    }
}
</code>```

This works, but requires an unnecessary layer of abstraction and looks pretty messy. Is there a cleaner way to achieve the same result?

答:
My use case was similar and what worked for me is writing an extension for type T. The extension class also has a static property to hold the "immutable list of extensions".

```<code>public static class GenericDataExtensions
{
    public static TExtension GetExtension<T>(this T any)
    {
        Console.WriteLine($"Requesting extension for type '{any.GetType().Name}'");
        if(ImmutableListOfExtensions.TryGetValue(typeof(T), out TExtension tExtension))
        {
            return tExtension;
        }
        return default(TExtension); // To do: Handle case of 'not found'.
    }
    internal static IReadOnlyDictionary<Type, TExtension>
}
</code>```

The mock of the TExtension class goes like so:

```<code>public class TExtension
{
    public TExtension(string name) => Name = name;
    public string Name { get; }
}
</code>```

And here is a simple test driver that initializes the dictionary and retrieves an extension:

```<code>[TestMethod]
public void InferredExtensionGetterTest()
{
    // Initialize the static extension class
    var initializer = new Dictionary<Type, TExtension>
    {
        {typeof(A), new TExtension("Extension for class A") },
        {typeof(B), new TExtension("Extension for class B") },
        {typeof(C), new TExtension("Extension for class C") },
    };
    GenericDataExtensions.ImmutableListOfExtensions = initializer;

    var classA = new A();
    var classB = new B();
    var classC = new C();

    TExtension extension;
    extension = classA.GetExtension();
    Console.WriteLine($"Retrieved: {extension.Name}{Environment.NewLine}");

    extension = classB.GetExtension();
    Console.WriteLine($"Retrieved: {extension.Name}{Environment.NewLine}");

    extension = classC.GetExtension();
    Console.WriteLine($"Retrieved: {extension.Name}{Environment.NewLine}");
}
</code>```

Body:
I would like to have a generic immutable data class with a list of extensions, which can be accessed using extension methods. I can do this with the following code:

public class GenericData<T> where T : unmanaged
{
    public GenericData(T data, IList<IDataExtension> extensions)
    {
        Data = data;
        Extensions = extensions.ToImmutableList();
    }

    public T Data { get; }

    public IImmutableList<IDataExtension> Extensions { get; }
}

public interface IDataExtension { }

public static class GenericDataExtensions
{
    public static TExtension GetExtension<TExtension, TDataType>(this GenericData<TDataType> data)
        where TExtension : IDataExtension
        where TDataType : unmanaged
    {
        return data.Extensions.OfType<TExtension>().First();
    }
}

This works fine, but requires me to pass in TDataType every time I want to get an extension, even though the data type isn't needed for the extension method implementation.

public interface IEvenlySpacedXAxisExtension : IDataExtension
{
    EvenlySpacedXAxisInfo Info { get; }
}

// later...

var xAxisInfo = data.GetExtension<IEvenlySpacedXAxisExtension, float>().Info;

I could solve this by putting GetExtension() behind an interface and making GenericData implement it:

public class GenericData<T> : IHaveDataExtensions where T : unmanaged
{
    public GenericData(T data, IList<IDataExtension> extensions)
    {
        Data = data;
        Extensions = extensions.ToImmutableList();
    }

    public T Data { get; }

    public IImmutableList<IDataExtension> Extensions { get; }
}

public interface IDataExtension { }

public interface IHaveDataExtensions
{
    IImmutableList<IDataExtension> Extensions { get; }
}

public static class IHaveExtensionsExtensions
{
    public static TExtension GetExtension<TExtension>(this IHaveDataExtensions objectWithExtensions)
        where TExtension : IDataExtension
    {
        return objectWithExtensions.Extensions.OfType<TExtension>().First();
    }
}

This works, but requires an unnecessary layer of abstraction and looks pretty messy. Is there a cleaner way to achieve the same result?

Body:
My use case was similar and what worked for me is writing an extension for type T. The extension class also has a static property to hold the "immutable list of extensions".

public static class GenericDataExtensions
{
    public static TExtension GetExtension<T>(this T any)
    {
        Console.WriteLine($"Requesting extension for type '{any.GetType().Name}'");
        if(ImmutableListOfExtensions.TryGetValue(typeof(T), out TExtension tExtension))
        {
            return tExtension;
        }
        return default(TExtension); // To do: Handle case of 'not found'.
    }
    internal static IReadOnlyDictionary<Type, TExtension>
}

The mock of the TExtension class goes like so:

public class TExtension
{
    public TExtension(string name) => Name = name;
    public string Name { get; }
}

And here is a simple test driver that initializes the dictionary and retrieves an extension:

[TestMethod]
public void InferredExtensionGetterTest()
{
    // Initialize the static extension class
    var initializer = new Dictionary<Type, TExtension>
    {
        {typeof(A), new TExtension("Extension for class A") },
        {typeof(B), new TExtension("Extension for class B") },
        {typeof(C), new TExtension("Extension for class C") },
    };
    GenericDataExtensions.ImmutableListOfExtensions = initializer;

    var classA = new A();
    var classB = new B();
    var classC = new C();

    TExtension extension;
    extension = classA.GetExtension();
    Console.WriteLine($"Retrieved: {extension.Name}{Environment.NewLine}");

    extension = classB.GetExtension();
    Console.WriteLine($"Retrieved: {extension.Name}{Environment.NewLine}");

    extension = classC.GetExtension();
    Console.WriteLine($"Retrieved: {extension.Name}{Environment.NewLine}");
}


问:
Databricks repos - unable to use dbutils.notebook.run with absolute path
I'm unable to get an absolute path working with dbutils.notebook.run(). Using the absolute path with dbutils.fs.ls(with "file:/Workspace/Repos/user_email/Datalake/databricks/models/notebook") returns a FileInfo object. Running the same filepath string with dbutils.notebook.run(path) does start running a job, however with an error that the notebook was not found. The error includes a path which is the combination of the relative filepath (of the notebook from which I run the dbutils.notebook.run() command) concatenated with the path I to the referenced notebook.

Seems that dbutils.notebook.run is not able to access notebooks absolute with repos?

Thanks in advance.

答:
/Workspace/Repos/user_email/... is the path on the driver machine, that is local to your cluster node.  It works with dbutils.fs.ls because it works with files of different types.  This path is a special replication of actual notebook/file that resides at path /Repos/user_email/... inside the workspace

On other hand, dbutils.notebook.run works with notebooks in the workspace, not with files, so you need either to specify that path as /Repos/user_email/..., but it's better to use relative paths, because it's more portable, for example, if someone else will use your code from the repository.


问:
Argument when using .next() with takeUntil
I've noticed recently after upgrading my rxjs version that you can't use the .next() method this.ngUnsubscribe$.next(); as it is anymore as you would below:

```<code>export class TakeUntilComponent implements OnDestroy {
  // Our magical observable that will be passed to takeUntil()
  private readonly ngUnsubscribe$: Subject<void> = new Subject<void>();

  // Our subject that we will subscribe to
  subjectA$: Subject<number> = new Subject<number>();

  constructor() {
    this.subjectA$
      .pipe(takeUntil(this.ngUnsubscribe$))
      .subscribe(value => {
      // logic goes here ...
    });
  }

  ngOnDestroy(){
    // Emit a value so that takeUntil will handle the closing of our subscriptions;
    this.ngUnsubscribe$.next();
    // Unsubscribe from our unsubscriber to avoid creating a memory leak
    this.ngUnsubscribe$.unsubscribe();
  }

}
</code>```

But now you must send an argument to it like this:

```<code>this.ngUnsubscribe$.next(null);
</code>```

or

```<code>this.ngUnsubscribe$.next(true);
</code>```

My question is why? And what value would you know to send?

答:
Another approach to fix this would be to use the ng-neat npm package:
https://github.com/ngneat/until-destroy

Just by adding @UntilDestroy({ checkProperties: true }) to the top of your class you can remove all the takeUntils(this.unsubscribe) out of the code and remove the ngOnDestroy method. Just think it looks a lot cleaner and you will have less imports.

Body:
I've noticed recently after upgrading my rxjs version that you can't use the .next() method this.ngUnsubscribe$.next(); as it is anymore as you would below:

export class TakeUntilComponent implements OnDestroy {
  // Our magical observable that will be passed to takeUntil()
  private readonly ngUnsubscribe$: Subject<void> = new Subject<void>();

  // Our subject that we will subscribe to
  subjectA$: Subject<number> = new Subject<number>();

  constructor() {
    this.subjectA$
      .pipe(takeUntil(this.ngUnsubscribe$))
      .subscribe(value => {
      // logic goes here ...
    });
  }

  ngOnDestroy(){
    // Emit a value so that takeUntil will handle the closing of our subscriptions;
    this.ngUnsubscribe$.next();
    // Unsubscribe from our unsubscriber to avoid creating a memory leak
    this.ngUnsubscribe$.unsubscribe();
  }

}

But now you must send an argument to it like this:

this.ngUnsubscribe$.next(null);

or

this.ngUnsubscribe$.next(true);

My question is why? And what value would you know to send?