问:
API getting called multiple times
When testing the API I have noticed that it is getting called around 6 times, there is no for/foreach loop that would make it run several times in the code so am unsure as to why this may be happening.
The API runs every time a user goes onto the Landing Screen.
Any advice would be appreciated

exports.getFilteredOpportunities = async (req, res) => {
    let mongoQuery = generateMongoQuery(req);
    try {
        const opps = await Opportunity.find(mongoQuery)
            .select(removeItems)
            .sort({
                createdAt: -1
            });
        res.status(200).json({
            opportunities: opps,
        });
    } catch (error) {
        res.status(500).json({
            status: "error",
            message: error,
        });
    }
};
```
<p>GenerateMongoQuery function</p>
<pre class="lang-js prettyprint-override"><code>const generateMongoQuery = (req) => {
    let query = {};
    // search param used searching through Title field, ie. search=food
    if (req.query.search && req.query.search.length > 0) {
        query.$or = [{}, {}];
        query.$or[0].Title = query.$or[1].Description = new RegExp(
            `${req.query.search}`,
            "i"
        );
    }
    // location param, i.e location=Glasgow,Manchester
    if (req.query.location && req.query.location.length > 0) {
        query.location = {
            $in: req.query.location.split(",")
        };
    }
    // timeslot param, i.e timeslot=Evening,Morning
    if (req.query.timeslot && req.query.timeslot.length > 0) {
        query.timeslot = {
            $in: req.query.timeslot.split(",")
        };
    }
    // category param, returning corresponding id i.e category=
    if (req.query.category && req.query.category.length > 0) {
        query.category = {
            $in: req.query.category.split(",")
        };
    }
    // Dont load expired opportunities
    query.eventDate = {
        $gte: new Date().toDateString()
    };
    // Only return non-cancelled opportunities
    query.isCancelled = false;
    return query;
};
</code>```

The landing Screen

import { useState, useEffect } from "react";
import Opportunities from "../components/molecules/Opportunities";
import Filters from "../components/organisms/Filters";
import { IOpportunity } from "../Models/IOpportunity";
import OpportunitiesClient from "../Api/opportunitiesClient";
import Utils from "../Utils/Utils";
import FiltersClient from "../Api/filtersClient";
import { IFilters } from "../Models/IFilters";
import { ISelectedFilters } from "../Models/ISelectedFilters";

import Header from "../components/atoms/Header";
import Footer from "../components/atoms/Footer";

export default function LandingScreen(props) {
    const [opportunities, setOpportunities] = useState<IOpportunity[]>([]);
    const [filters, setFilters] = useState<IFilters[]>([]);
    const [checkedFilters, setCheckedFilters] = useState<
        ISelectedFilters | undefined
    >({
        Location: [],
        Category: [],
        Timeslot: [],
    });
    const [isLoading, setLoading] = useState<boolean>(true);
    const [allResultsLoaded, setAllResultsLoaded] = useState<boolean>(false);
    let pageToGet = 1;
    const [scrollPosition, setScrollPosition] = useState(0);
    const [totalOpps, setTotalOpps] = useState(0);

    useEffect(() => {
        getOpportunities();
        getFilters();
    }, []);

    useEffect(() => {
        setTotalOpps(opportunities.length);
    }, [opportunities]);

    const handleScroll = () => {
        setScrollPosition(window.pageYOffset);
        let scrollHeight = 0;

        if ((props.scrollHeight === null) !== undefined) {
            scrollHeight = +props.scrollHeight;
        } else {
            scrollHeight = document.scrollingElement.scrollHeight;
        }

        if (
            window.innerHeight + document.documentElement.scrollTop >=
            scrollHeight
        ) {
            if (allResultsLoaded === false) {
                setLoading(true);
            }
            setTimeout(() => {
                pageToGet += 1;
                getOpportunities();
            }, 600);
        }
        window.removeEventListener("scroll", handleScroll);
    };
    window.addEventListener("scroll", handleScroll, { passive: true });

    const setItemChecked = (filtername: string, filterType: string) => {
        // reset page and results
        pageToGet = 1;
        setAllResultsLoaded(false);

        setCheckedFilters(
            Utils.setItemChecked(filtername, filterType, checkedFilters)
        );
    };

    const resetFilters = () => {
        // reset page and results
        pageToGet = 1;
        setAllResultsLoaded(false);

        checkedFilters.Location = [];
        checkedFilters.Category = [];
        checkedFilters.Timeslot = [];

        getOpportunities();
    };

    const getFilters = () => {
        FiltersClient.getAll().then((filters) => {
            setFilters(filters);
        });
    };

    const getOpportunities = () => {
        console.log(opportunities);
        OpportunitiesClient.getWithParams(
            Utils.getAxiosParams(checkedFilters)
        ).then((res) => {
            definePage(res);
            if (opportunities.length === res["opportunities"].length)
                setAllResultsLoaded(true);

            setLoading(false);
        });
    };

    const definePage = (res) => {
        if (pageToGet === 1) {
            setOpportunities(res["opportunities"].slice(0, 15));
        } else {
            setOpportunities(
                opportunities.concat(
                    res["opportunities"].slice(
                        opportunities.length,
                        opportunities.length + 15
                    )
                )
            );
        }
    };

    return (

                    {totalOpps} Results{" "}
                    {checkedFilters.Location.length > 0 ? "(Filtered)" : ""}

                    <Filters
                        filters={filters}
                        getChecked={setItemChecked}
                        urlCall={getOpportunities}
                        resetFilters={resetFilters}
                        checkedFilters={checkedFilters}
                    />

                        <Opportunities
                            items={opportunities}
                            isLoading={isLoading}
                            allResultsLoaded={allResultsLoaded}
                        />

    );
}
```

Title: API getting called multiple times

Body:
When testing the API I have noticed that it is getting called around 6 times, there is no for/foreach loop that would make it run several times in the code so am unsure as to why this may be happening.
The API runs every time a user goes onto the Landing Screen.
Any advice would be appreciated

exports.getFilteredOpportunities = async (req, res) => {
    let mongoQuery = generateMongoQuery(req);
    try {
        const opps = await Opportunity.find(mongoQuery)
            .select(removeItems)
            .sort({
                createdAt: -1
            });
        res.status(200).json({
            opportunities: opps,
        });
    } catch (error) {
        res.status(500).json({
            status: "error",
            message: error,
        });
    }
};

GenerateMongoQuery function

const generateMongoQuery = (req) => {
    let query = {};
    // search param used searching through Title field, ie. search=food
    if (req.query.search && req.query.search.length > 0) {
        query.$or = [{}, {}];
        query.$or[0].Title = query.$or[1].Description = new RegExp(
            `${req.query.search}`,
            "i"
        );
    }
    // location param, i.e location=Glasgow,Manchester
    if (req.query.location && req.query.location.length > 0) {
        query.location = {
            $in: req.query.location.split(",")
        };
    }
    // timeslot param, i.e timeslot=Evening,Morning
    if (req.query.timeslot && req.query.timeslot.length > 0) {
        query.timeslot = {
            $in: req.query.timeslot.split(",")
        };
    }
    // category param, returning corresponding id i.e category=
    if (req.query.category && req.query.category.length > 0) {
        query.category = {
            $in: req.query.category.split(",")
        };
    }
    // Dont load expired opportunities
    query.eventDate = {
        $gte: new Date().toDateString()
    };
    // Only return non-cancelled opportunities
    query.isCancelled = false;
    return query;
};

The landing Screen

import { useState, useEffect } from "react";
import Opportunities from "../components/molecules/Opportunities";
import Filters from "../components/organisms/Filters";
import { IOpportunity } from "../Models/IOpportunity";
import OpportunitiesClient from "../Api/opportunitiesClient";
import Utils from "../Utils/Utils";
import FiltersClient from "../Api/filtersClient";
import { IFilters } from "../Models/IFilters";
import { ISelectedFilters } from "../Models/ISelectedFilters";

import Header from "../components/atoms/Header";
import Footer from "../components/atoms/Footer";

export default function LandingScreen(props) {
    const [opportunities, setOpportunities] = useState<IOpportunity[]>([]);
    const [filters, setFilters] = useState<IFilters[]>([]);
    const [checkedFilters, setCheckedFilters] = useState<
        ISelectedFilters | undefined
    >({
        Location: [],
        Category: [],
        Timeslot: [],
    });
    const [isLoading, setLoading] = useState<boolean>(true);
    const [allResultsLoaded, setAllResultsLoaded] = useState<boolean>(false);
    let pageToGet = 1;
    const [scrollPosition, setScrollPosition] = useState(0);
    const [totalOpps, setTotalOpps] = useState(0);

    useEffect(() => {
        getOpportunities();
        getFilters();
    }, []);

    useEffect(() => {
        setTotalOpps(opportunities.length);
    }, [opportunities]);

    const handleScroll = () => {
        setScrollPosition(window.pageYOffset);
        let scrollHeight = 0;

        if ((props.scrollHeight === null) !== undefined) {
            scrollHeight = +props.scrollHeight;
        } else {
            scrollHeight = document.scrollingElement.scrollHeight;
        }

        if (
            window.innerHeight + document.documentElement.scrollTop >=
            scrollHeight
        ) {
            if (allResultsLoaded === false) {
                setLoading(true);
            }
            setTimeout(() => {
                pageToGet += 1;
                getOpportunities();
            }, 600);
        }
        window.removeEventListener("scroll", handleScroll);
    };
    window.addEventListener("scroll", handleScroll, { passive: true });

    const setItemChecked = (filtername: string, filterType: string) => {
        // reset page and results
        pageToGet = 1;
        setAllResultsLoaded(false);

        setCheckedFilters(
            Utils.setItemChecked(filtername, filterType, checkedFilters)
        );
    };

    const resetFilters = () => {
        // reset page and results
        pageToGet = 1;
        setAllResultsLoaded(false);

        checkedFilters.Location = [];
        checkedFilters.Category = [];
        checkedFilters.Timeslot = [];

        getOpportunities();
    };

    const getFilters = () => {
        FiltersClient.getAll().then((filters) => {
            setFilters(filters);
        });
    };

    const getOpportunities = () => {
        console.log(opportunities);
        OpportunitiesClient.getWithParams(
            Utils.getAxiosParams(checkedFilters)
        ).then((res) => {
            definePage(res);
            if (opportunities.length === res["opportunities"].length)
                setAllResultsLoaded(true);

            setLoading(false);
        });
    };

    const definePage = (res) => {
        if (pageToGet === 1) {
            setOpportunities(res["opportunities"].slice(0, 15));
        } else {
            setOpportunities(
                opportunities.concat(
                    res["opportunities"].slice(
                        opportunities.length,
                        opportunities.length + 15
                    )
                )
            );
        }
    };

    return (

                    {totalOpps} Results{" "}
                    {checkedFilters.Location.length > 0 ? "(Filtered)" : ""}

                    <Filters
                        filters={filters}
                        getChecked={setItemChecked}
                        urlCall={getOpportunities}
                        resetFilters={resetFilters}
                        checkedFilters={checkedFilters}
                    />

                        <Opportunities
                            items={opportunities}
                            isLoading={isLoading}
                            allResultsLoaded={allResultsLoaded}
                        />

    );
}


来源: stackexchange

问:
Can I make JSLint interpret .toBeDefined as a null check when testing with typescript?
My tests are riddled with TS18047 warnings.

My code usually goes like this:

```<code>expect(myVar).toBeDefined();  // or .not.toBeNull()
expect(myVar.id).toEqual(1); // <<-- TS18047
expect(myVar.title).toEqual('test'); // <<-- TS18047
</code>```

There are two "easy" solutions that I could figure out, but they both introduce unnecessary code:

Solution 1:

```<code>expect(myVar).toBeDefined();
expect(myVar?.id).toEqual(1);
expect(myVar?.title).toEqual('test');
</code>```

Solution 2:

```<code>expect(myVar).toBeDefined();
if (myVar != null) {
  expect(myVar.id).toEqual(1);
  expect(myVar.title).toEqual('test');
}
</code>```

But I feel JSLint should be able to interpret .toBeDefined() equal to null check.

Shouldn't it?

Can I make it do so?

ContentLicense: CC BY-SA 4.0

Body:
My tests are riddled with TS18047 warnings.

My code usually goes like this:

expect(myVar).toBeDefined();  // or .not.toBeNull()
expect(myVar.id).toEqual(1); // <<-- TS18047
expect(myVar.title).toEqual('test'); // <<-- TS18047

There are two "easy" solutions that I could figure out, but they both introduce unnecessary code:

Solution 1:

expect(myVar).toBeDefined();
expect(myVar?.id).toEqual(1);
expect(myVar?.title).toEqual('test');

Solution 2:

expect(myVar).toBeDefined();
if (myVar != null) {
  expect(myVar.id).toEqual(1);
  expect(myVar.title).toEqual('test');
}

But I feel JSLint should be able to interpret .toBeDefined() equal to null check.

Shouldn't it?

Can I make it do so?


问:
How can I automate OAuth authentication
I am working on an automation tool that does not support Oauth specifically, but it supports adding URLs, payloads and has the following authentication methods: Basic, Digest, API Keys.
Is there a way to work around OAuth where I only use the above information and without using a callback URL so that the process is programmatic ?

Not enough information is available on this, but I tried with Google mail and had no luck.

Title: How can I automate OAuth authentication


问:
Request URL for slack events from Azure functions
I created a python slack bot and tested it with Ngrok. I then created an azure function and connected to the code via repository. Now where can I find the URL that will replace the request URL in slack.

Below image is the azure function.

I also attached the code which I tested for slack bot.

```<code>import slack
import os
from pathlib import Path
from dotenv import load_dotenv
from flask import Flask
from slackeventsapi import SlackEventAdapter

env_path= Path('.')/'.env'
load_dotenv(dotenv_path=env_path)

app = Flask(__name__)
slack_event_adapter = SlackEventAdapter(
    os.environ['SIGNING_SECRET'], '/slack/events', app)

client = slack.WebClient(token=os.environ['SLACK_TOKEN'])
BOT_ID = client.api_call("auth.test")['user_id']

@slack_event_adapter.on('message')
def message(payload):
    event = payload.get('event', {})
    channel_id = event.get('channel')
    user_id = event.get('user')
    text = event.get('text')

    if BOT_ID != user_id:
        client.chat_postMessage(channel=channel_id, text=text)

if __name__ == "__main__":
    app.run(debug=True)
</code>```

I am very new to azure. Thanks in advance.

I tried the following URLs from the azure function but that did not work. I tried
https://demo-flask.azurewebsites.net
https://demo-flask.azurewebsites.net/slack/event

Body:
I created a python slack bot and tested it with Ngrok. I then created an azure function and connected to the code via repository. Now where can I find the URL that will replace the request URL in slack.

Below image is the azure function.

I also attached the code which I tested for slack bot.

import slack
import os
from pathlib import Path
from dotenv import load_dotenv
from flask import Flask
from slackeventsapi import SlackEventAdapter

env_path= Path('.')/'.env'
load_dotenv(dotenv_path=env_path)

app = Flask(__name__)
slack_event_adapter = SlackEventAdapter(
    os.environ['SIGNING_SECRET'], '/slack/events', app)

client = slack.WebClient(token=os.environ['SLACK_TOKEN'])
BOT_ID = client.api_call("auth.test")['user_id']

@slack_event_adapter.on('message')
def message(payload):
    event = payload.get('event', {})
    channel_id = event.get('channel')
    user_id = event.get('user')
    text = event.get('text')

    if BOT_ID != user_id:
        client.chat_postMessage(channel=channel_id, text=text)

if __name__ == "__main__":
    app.run(debug=True)

I am very new to azure. Thanks in advance.

I tried the following URLs from the azure function but that did not work. I tried
https://demo-flask.azurewebsites.net
https://demo-flask.azurewebsites.net/slack/event


问:
react router dom v6 conditional routing for logedin admin
I am building a MERN stack app and I am trying to check if the user is actually an admin. I am using the same database for two front end apps, one is a dashboard in which I have this problem and I can only check if the user is logged in but not if he is an admin.

```<code>import CssBaseline from "@mui/material/CssBaseline";
import { ThemeProvider } from "@mui/material/styles";
import { createTheme } from "@mui/material";
import { useEffect, useMemo, useState } from "react";
import { useSelector } from "react-redux";
import { themeSettings } from "theme";
import { Routes, Route, Navigate } from "react-router-dom";
import Dashboard from "scenes/dashboard";
import Layout from "scenes/layout";
import NewProduct from "scenes/newProduct/NewProduct";
import Products from "scenes/products/Products";
import SingleProduct from "scenes/singleProduct/SingleProduct";
import Login from "scenes/login/Login";
import Users from "scenes/utilisateurs/users";

function App() {
  const mode = useSelector((state) => state.global.mode);
  const theme = useMemo(() => createTheme(themeSettings(mode)), [mode]);

  const userLogin = useSelector((state) => state.userLogin)
  const { userInfo } = userLogin

  return (
    <div className="app">
      <ThemeProvider theme={theme}>
        <CssBaseline />
        <Routes>
          <Route path="/login" element={<Login />} />
          <Route
            element={userInfo ? <Layout /> : <Navigate to="/login"/>}
          >
            <Route path="/" element={<Dashboard />} />
            <Route path="/dashboard" element={<Dashboard />} />
            <Route path="/newproduct" element={<NewProduct />} />
            <Route path="/products" element={<Products />} />
            <Route path="/products/:id" element={<SingleProduct />} />
            <Route path="/utilisateurs" element={<Users />} />
          </Route>
        </Routes>
      </ThemeProvider>
    </div>
  );
}

export default App;
</code>```

so with this first code it only checked for the userinfo but when I add userInfo.isAdmin

```<code><Route element={userInfo.isAdmin ? <Layout /> : <Navigate to="/login"/>} >
</code>```

I get this error

App.js:32 Uncaught TypeError: Cannot read properties of null (reading 'isAdmin')

Although I am sure that the useInfo does have the isAdmin value in redux state and also in the localStorage.

What Im I doing wrong here?

Body:
I am building a MERN stack app and I am trying to check if the user is actually an admin. I am using the same database for two front end apps, one is a dashboard in which I have this problem and I can only check if the user is logged in but not if he is an admin.

import CssBaseline from "@mui/material/CssBaseline";
import { ThemeProvider } from "@mui/material/styles";
import { createTheme } from "@mui/material";
import { useEffect, useMemo, useState } from "react";
import { useSelector } from "react-redux";
import { themeSettings } from "theme";
import { Routes, Route, Navigate } from "react-router-dom";
import Dashboard from "scenes/dashboard";
import Layout from "scenes/layout";
import NewProduct from "scenes/newProduct/NewProduct";
import Products from "scenes/products/Products";
import SingleProduct from "scenes/singleProduct/SingleProduct";
import Login from "scenes/login/Login";
import Users from "scenes/utilisateurs/users";

function App() {
  const mode = useSelector((state) => state.global.mode);
  const theme = useMemo(() => createTheme(themeSettings(mode)), [mode]);

  const userLogin = useSelector((state) => state.userLogin)
  const { userInfo } = userLogin

  return (

      <ThemeProvider theme={theme}>
        <CssBaseline />
        <Routes>
          <Route path="/login" element={<Login />} />
          <Route
            element={userInfo ? <Layout /> : <Navigate to="/login"/>}
          >
            <Route path="/" element={<Dashboard />} />
            <Route path="/dashboard" element={<Dashboard />} />
            <Route path="/newproduct" element={<NewProduct />} />
            <Route path="/products" element={<Products />} />
            <Route path="/products/:id" element={<SingleProduct />} />
            <Route path="/utilisateurs" element={<Users />} />
          </Route>
        </Routes>
      </ThemeProvider>

  );
}

export default App;

so with this first code it only checked for the userinfo but when I add userInfo.isAdmin

<Route element={userInfo.isAdmin ? <Layout /> : <Navigate to="/login"/>} >

I get this error

App.js:32 Uncaught TypeError: Cannot read properties of null (reading 'isAdmin')

Although I am sure that the useInfo does have the isAdmin value in redux state and also in the localStorage.

What Im I doing wrong here?


问:
Problem with receiving topic notifications in Swift
I am having some problems with sending fcm notifications directly from my SwiftUI app.

When I try to send a notification to token like this, it will work ok:

```<code>let request1 = NSMutableURLRequest(url: URL(string: "https://fcm.googleapis.com/fcm/send")!)
    request1.httpMethod = "POST"
    request1.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request1.setValue("\(serverKey)", forHTTPHeaderField: "Authorization")

    let json = [
     "to": "my token",
     "notification": [
      "title": "Notification Title",
      "body": "Notification Body"
     ]
    ] as [String : Any]

    let jsonData = try! JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
    request1.httpBody = jsonData

    URLSession.shared.dataTask(with: request1 as URLRequest) { (data, response, error) in
     if error != nil {
      print(error!)
      return
     }

     print(response!)
    }.resume()
</code>```

This code works, but when I change the line "to": "my token" into

```<code>"topic": "/topics/test_topic"
</code>```

or

```<code>"topic": "test_topic"
</code>```

It won't send notification to any of the devices subscribed to topic.
Devices are connected to topic, because if I send notification to topic from Firebase console, it works OK.
Is there any way to send a notification to topic directly from a SwiftUI app?

I would like to be able to send notifications from a SwiftUI app directly, without backend servers.

Body:
I am having some problems with sending fcm notifications directly from my SwiftUI app.

When I try to send a notification to token like this, it will work ok:

let request1 = NSMutableURLRequest(url: URL(string: "https://fcm.googleapis.com/fcm/send")!)
    request1.httpMethod = "POST"
    request1.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request1.setValue("\(serverKey)", forHTTPHeaderField: "Authorization")

    let json = [
     "to": "my token",
     "notification": [
      "title": "Notification Title",
      "body": "Notification Body"
     ]
    ] as [String : Any]

    let jsonData = try! JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
    request1.httpBody = jsonData

    URLSession.shared.dataTask(with: request1 as URLRequest) { (data, response, error) in
     if error != nil {
      print(error!)
      return
     }

     print(response!)
    }.resume()

This code works, but when I change the line "to": "my token" into

"topic": "/topics/test_topic"

or

"topic": "test_topic"

It won't send notification to any of the devices subscribed to topic.
Devices are connected to topic, because if I send notification to topic from Firebase console, it works OK.
Is there any way to send a notification to topic directly from a SwiftUI app?

I would like to be able to send notifications from a SwiftUI app directly, without backend servers.


问:
seaborn uneven spacing between x ticks
I'm plotting a seaborn plot, for example:

```<code>sns.pointplot (x=[1,2,3,10], y=[2,3,4,5])
</code>```

How can I make the X ticks to have spcaing that represents their values? 10 should be further apart but the spacing is equal.

```<code>ax.xaxis.set_ticks([1,2,3,10]) does not work
</code>```

Title: seaborn uneven spacing between x ticks

Body:
I'm plotting a seaborn plot, for example:

sns.pointplot (x=[1,2,3,10], y=[2,3,4,5])

How can I make the X ticks to have spcaing that represents their values? 10 should be further apart but the spacing is equal.

ax.xaxis.set_ticks([1,2,3,10]) does not work


问:
How to hide scrollbar when Text widget is not scrollable in tkinter
I am writing a program in python in which I want the Text widget in tkinter to have a scrollbar .
this is a part of my code -

import tkinter as tk
import tkinter.font as tkfont

window = tk.Tk()

display_text = "START\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nEND"

font_1 = tkfont.Font(family='Helvetica', size=22, weight='bold')
my_label_1 = tk.Text(window, wrap=tk.WORD, bd=0, fg="white", bg="black", font=font_1, width=76, height=21,)
_scrollbar = tk.Scrollbar(window, command=my_label_1.yview)
my_label_1.configure(yscrollcommand=_scrollbar.set)
my_label_1.tag_configure("center", justify='center')
my_label_1.insert(1.0, display_text)
_scrollbar.pack(side=tk.RIGHT, ipadx=3, ipady=500, pady=(0, 48))
my_label_1.pack()

window.mainloop()
```

But I have 500 strings which I import from a csv file, when I put the strings in this code the strings go in disply_text variable, this works fine.

But for some of those strings the text widget is big enough and the scrollbar is not needed as the text widget is not scrollable for those. but the scrollbar still shows up it is disabled though but I don't want it to be there.

I know pack_forget() or grid_forget() can do this job, But Then what parameter should I put so I know that this iteration of text is not scrollable on the Text widget.

And my window is of fixed size i.e. window.resizable(False, False).

Body:
I am writing a program in python in which I want the Text widget in tkinter to have a scrollbar .
this is a part of my code -

import tkinter as tk
import tkinter.font as tkfont

window = tk.Tk()

display_text = "START\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nEND"

font_1 = tkfont.Font(family='Helvetica', size=22, weight='bold')
my_label_1 = tk.Text(window, wrap=tk.WORD, bd=0, fg="white", bg="black", font=font_1, width=76, height=21,)
_scrollbar = tk.Scrollbar(window, command=my_label_1.yview)
my_label_1.configure(yscrollcommand=_scrollbar.set)
my_label_1.tag_configure("center", justify='center')
my_label_1.insert(1.0, display_text)
_scrollbar.pack(side=tk.RIGHT, ipadx=3, ipady=500, pady=(0, 48))
my_label_1.pack()

window.mainloop()

But I have 500 strings which I import from a csv file, when I put the strings in this code the strings go in disply_text variable, this works fine.

But for some of those strings the text widget is big enough and the scrollbar is not needed as the text widget is not scrollable for those. but the scrollbar still shows up it is disabled though but I don't want it to be there.

I know pack_forget() or grid_forget() can do this job, But Then what parameter should I put so I know that this iteration of text is not scrollable on the Text widget.

And my window is of fixed size i.e. window.resizable(False, False).


问:
How can I make Excel print a different worksheet in the middle of a for loop?
First time poster long time user.

I have an Excel file that I use for printing labels at my workplace and currently I print out a separate cover sheet for each label. My question is how can I make Excel print the cover sheet in between each label. I have tried a few methods but it always seems to break my loop.

Here is the code for the Print Labels function,

```<code>`Sub PrintLabelsROI()

 Dim x As Integer
 Dim index As Integer
 Dim sheet As String
 Dim pages As Integer

 NumRows = Range("E5", Range("E5").End(xlDown)).Rows.Count
 Range("E5").Select
 For x = 1 To NumRows
     index = ActiveCell.Row
         If ActiveCell.EntireRow.Hidden = False Then
             If (Cells(index, 5).Value = "I088") Then
                 sheet = "ROI PRINT"
                 pages = Cells(index, 9).Value
             ElseIf (Left(Cells(index, 9).Value, 1) = "?") Then
                 sheet = "DF"
                 pages = Mid(Cells(index, 9).Value, 2)
             Else
                 sheet = "norm"
                 pages = Cells(index, 9).Value
             End If
        If (pages > 0) Then
            ' VB_PRINTSHEET
            Sheets(sheet).Cells(15, 6).Value = Cells(index, 6).Value
            Sheets(sheet).PrintOut Copies:=pages
        End If
    End If
    ActiveCell.Offset(1, 0).Select
Next
End Sub`
</code>```

I created a sperate SUB to print the cover sheet called VB_PRINTSHEET

```<code>Sub VB_PRINTSHEET()
    Worksheets("PICKSHEET").PrintOut , Copies:=1
End Sub`
</code>```

I then call that function in the main SUB PrintLabelsROI but no matter where I put it, my loop breaks or prints the cover sheet over and over again.

I can share the Excel.xlsm if needed.

Sorry if this isn't clear enough, please ask any questions.

Any help would be much appreciated thank you.

Body:
First time poster long time user.

I have an Excel file that I use for printing labels at my workplace and currently I print out a separate cover sheet for each label. My question is how can I make Excel print the cover sheet in between each label. I have tried a few methods but it always seems to break my loop.

Here is the code for the Print Labels function,

`Sub PrintLabelsROI()

 Dim x As Integer
 Dim index As Integer
 Dim sheet As String
 Dim pages As Integer

 NumRows = Range("E5", Range("E5").End(xlDown)).Rows.Count
 Range("E5").Select
 For x = 1 To NumRows
     index = ActiveCell.Row
         If ActiveCell.EntireRow.Hidden = False Then
             If (Cells(index, 5).Value = "I088") Then
                 sheet = "ROI PRINT"
                 pages = Cells(index, 9).Value
             ElseIf (Left(Cells(index, 9).Value, 1) = "?") Then
                 sheet = "DF"
                 pages = Mid(Cells(index, 9).Value, 2)
             Else
                 sheet = "norm"
                 pages = Cells(index, 9).Value
             End If
        If (pages > 0) Then
            ' VB_PRINTSHEET
            Sheets(sheet).Cells(15, 6).Value = Cells(index, 6).Value
            Sheets(sheet).PrintOut Copies:=pages
        End If
    End If
    ActiveCell.Offset(1, 0).Select
Next
End Sub`

I created a sperate SUB to print the cover sheet called VB_PRINTSHEET

Sub VB_PRINTSHEET()
    Worksheets("PICKSHEET").PrintOut , Copies:=1
End Sub`

I then call that function in the main SUB PrintLabelsROI but no matter where I put it, my loop breaks or prints the cover sheet over and over again.

I can share the Excel.xlsm if needed.

Sorry if this isn't clear enough, please ask any questions.

Any help would be much appreciated thank you.


问:
Try create Dynamic Widget, data from API but when I click on dropdown, radion selection not visible on UI

Try create Dynamic Widget, data from API and add widget in array
&showing in listbuilder but when I click on dropdown, radion selection
not redering. When I click on radio button value print in terminal but
changes not visible same for dropdown and filter chip also.
I want to create form dynamically based on API value like type=TextAREA so create textfield, Type=radioButton so create radio button

```<code>class ObservationCreate extends StatefulWidget {
  static String tag = '/ObservationCreate';

  @override
  ObservationCreateState createState() => ObservationCreateState();

}
class ObservationCreateState extends State<ObservationCreate> {
  PageController pageController = PageController(initialPage: 0);
  int pageNumber = 0;
  String token;
  String positionSelection;
  Animation buttonSqueezeanimation;
  AnimationController buttonController;
  var radioValue, previouseValue;
  var getDatas;
  List<EmployeeModel> employee = [];
  List<OrganizationModel> organizations = [];
  List<CommonModel> common = [];
  List<TextEditingController> controllers = [];
  List<dynamic> _selectedValues = [];
  List<MultiSelectionModel> _multiSelection = [];

  List<Widget> buildDotIndicator() {
    List<Widget> list = [];
    for (int i = 0; i <= 2; i++) {
      list.add(i == pageNumber ? indicator(isActive: true) : indicator(
          isActive: false));
    }
    return list;
  }

  List<Widget> _Widget = new List();
  List<Widget> _ObservationDetailsWidget = new List();
  List<int> _display = new List();
  var masterItems;
  bool getMaster = false, observation = false;
  SharedPreferences prefs;
  int number = 1;
  bool selected = false;

  @override
  void initState() {
    retrive();
  }

  Future<void> getMasterDatas(String controlType, var code) async {
    Response response =
    await get(getMasterData, headers: headers).timeout(
        Duration(seconds: timeOut));
    print("masterData:   " + response.body);
    getDatas = json.decode(response.body);
    String error = getDatas["error"];
    if (error != Error) {
      setState(() {
        getMaster = true;
      });
      var count = getDatas["items"].length;
      print(code+count.toString());
      if(common.length != 0){
        common.removeWhere((element) => element.id == code);
      }
      for (int i = 0; i < count; i++) {
        if (getDatas['items'][i]['masterData']['masterDataObjectId'] == code) {
          var masterData = getDatas['items'][i]['masterData'];
          var masterDataObject = getDatas['items'][i]['masterDataObject'];
          print(i);
          CommonModel commonModel = new CommonModel(masterData['value'], masterData['masterDataObjectId'],
              masterDataObject['masterDataObjectValue'], masterData['showControlIDs'] == null ? " " : masterData['showControlIDs']);
          common.add(commonModel);
        }
      }
      if(getMaster){
        print(getMaster);
        switch (controlType){
          case "RADIOBUTTON":
            _Widget.add(radioitem(code));
            break;
          case "MASTERDROPDOWN":
            _Widget.add(masterDropDownBox(code));
            break;
          case "MULTISELECT":
            _ObservationDetailsWidget.add(multiSelections(code));
            _getListings(code);
            break;
        }
      }
    } else {
      print(error);
    }
  }

  Future<void> getObsProperty() async {
    var items;
    String url = "https://webgateway.demoehswatch.com/api/observations-service/propertysettings/GetListPropertyAsync" +
        "?Authorization=bearer" + token + "&MaxResultCount=1000";
    final Uri getObsPropertyData = Uri.parse(url);
    Response response =
    await get(getObsPropertyData, headers: header).timeout(
        Duration(seconds: timeOut));
    var getData = json.decode(response.body);
    String error = getData["error"];
    if (error != Error) {
      var count = getData["items"].length;
      for (int i = 0; i < count; i++) {
        if (getData['items'][i]['providerName'] == "Observation" &&
            getData['items'][i]['visible'] &&
            getData['items'][i]['defaultDisplay'] &&
            getData['items'][i]['psDisplaySection'] == "Draft") {
          items = getData['items'][i];
          print(items["controlType"]+"   "+items["name"]);
          _display.add(items["displayOrderInView"]);
          _display.sort();
        }
      }
      for (int i = 0; i < _display.length; i++) {
        for (int j = 0; j < count; j++) {
          if (getData['items'][j]['providerName'] == "Observation" &&
              getData['items'][j]['visible'] &&
              getData['items'][j]['defaultDisplay'] &&
              getData['items'][j]['psDisplaySection'] == "Draft") {
            var ite = getData['items'][j];
            if (_display[i].toString() == ite["displayOrderInView"].toString()) {
              switch(ite['controlType']){
                case "TEXTBOX":
                  TextEditingController controller = TextEditingController();
                  controllers.add(controller);
                  setState(() {
                    if (ite['masterDataObjectCode'] == "")
                      _Widget.add(dynamicText(
                          ite['name'].toString().split("Observation.")[1],
                          textColor: textColor));
                    _Widget.add(dynamicTextbox(50, controller));
                  });
                  break;
                case "RADIOBUTTON":
                    print(ite['masterDataObjectCode']);
                    var codeRadio = ite['masterDataObjectCode'];
                    getMasterDatas(ite['controlType'], codeRadio);
                    print(getMaster);
                  break;
                case "MASTERDROPDOWN":
                    print(ite['masterDataObjectCode']);
                    var codeDropDown = ite['masterDataObjectCode'];
                    getMasterDatas(ite['controlType'], codeDropDown);
                  break;
              }
            }
          }
        }
      }
    } else {
      print(error);
    }
  }

  Future<void> getObsPropertyObservationDetails(String objectCode) async {
    var items;
    String url = "https://webgateway.demoehswatch.com/api/observations-service/propertysettings/GetListPropertyAsync" +
        "?Authorization=bearer" + token + "&MaxResultCount=1000";
    final Uri getObsPropertyData = Uri.parse(url);
    Response response =
    await get(getObsPropertyData, headers: header).timeout(
        Duration(seconds: timeOut));
    var getData = json.decode(response.body);
    String error = getData["error"];
    if (error != Error) {
      var count = getData["items"].length;
      for (int i = 0; i < count; i++) {
        if (getData['items'][i]['providerName'] == "Observation" &&
            getData['items'][i]['visible'] &&
            getData['items'][i]['psDisplaySection'] == "Draft" &&
        getData['items'][i]['masterDataObjectCode'] == objectCode) {
          items = getData['items'][i];
          print(items["controlType"]+"   "+items["name"]);
          setState(() {
            observation = true;
            number = 2;
            getMasterDatas(items["controlType"], getData['items'][i]['masterDataObjectCode']);
          });

        }
      }
    } else {
      print(error);
    }
  }

  @override
  void dispose() {
    controllers.clear();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery
        .of(context)
        .size;
    return Scaffold(
        appBar: AppBar(
          title: const Text('New Observation'),
          backgroundColor: appBar,
        ),
        /*body: Container(
          padding: const EdgeInsets.only(left: 20, right: 20),
          decoration: BoxDecoration(
            color: Color(0xFFF5F5F6),
          ),
          child: Scaffold(
            backgroundColor: Colors.transparent,
            body: Column(
              children: <Widget>[
                Expanded(
                    child: ListView.builder(
                        padding: EdgeInsets.only(top: 10),
                        itemCount: _Widget.length,
                        itemBuilder: (context, index) {
                          Widget widget = _Widget.elementAt(index);
                          return widget;
                        })
                )
              ],
            ),*/
            body: Stack(
              alignment: Alignment.bottomCenter,
              children: <Widget>[
                Container(
                  height: double.infinity,
                  color: bg,
                  child: PageView(
                    onPageChanged: (index) => setState(() {
                      pageNumber = index;
                    }),
                    controller: pageController,
                    children: <Widget>[
                      Container(
                        padding: EdgeInsets.only(left: 16, bottom: 70, right: 16, top: 30),
                        child: Column(
                          children: <Widget>[
                            Expanded(
                                child: ListView.builder(
                                    shrinkWrap: true,
                                    padding: EdgeInsets.only(top: 10),
                                    itemCount: _Widget.length,
                                    itemBuilder: (context, index) {
                                      Widget widget = _Widget.elementAt(index);
                                      return widget;
                                    })
                            ),
                          ],
                        ),
                      ),
                      if(observation)
                      Container(
                          padding: EdgeInsets.only(left: 16, bottom: 70, right: 16, top: 30),
                          child: Column(
                              children: <Widget>[
                                Expanded(
                                  child: ListView.builder(
                                      shrinkWrap: true,
                                      padding: EdgeInsets.only(top: 10),
                                      itemCount: _ObservationDetailsWidget.length,
                                      itemBuilder: (context, index) {
                                        Widget widget = _ObservationDetailsWidget.elementAt(index);
                                        return widget;
                                      })
                                    )*/
                                  ]
                                )
                      ),

                      Container(
                        padding: EdgeInsets.only(left: 16, bottom: 70, right: 16, top: 30),
                        child: SingleChildScrollView(
                          padding: EdgeInsets.only(bottom: 60),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            mainAxisAlignment: MainAxisAlignment.center,
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              Container(
                                padding: EdgeInsets.all(20),
                                decoration: boxDecoration(
                                    showShadow: true,
                                    bgColor: Colors.white,
                                    radius: 8,
                                    color: border),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Image.asset("images/app/icons/upload.png", width: 50,),
                                    SizedBox(height: 10,),
                                    Text(uploadFile, style: TextStyle(fontSize: 15),),
                                    SizedBox(height: 10,),
                                    Container(
                                      decoration: boxDecoration(
                                          showShadow: true,
                                          bgColor: textColor,
                                          radius: 8,
                                          color: border),
                                      child: RaisedButton(
                                        onPressed: () {},
                                        color: textColor,
                                        child: Text(
                                          "Upload Files",
                                          style: TextStyle(color: Colors.white),
                                        ),
                                      ),
                                    )
                                  ],
                                ),
                              )
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 16, right: 16, bottom: 20),
                  padding: EdgeInsets.only(left: 20, right: 20),
                  width: size.width,
                  height: 50,
                  decoration: BoxDecoration(color: textColor, borderRadius: BorderRadius.circular(15.0)),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      CircleAvatar(
                        backgroundColor: Colors.white,
                        radius: 15,
                        child: Text('${pageNumber + 1}', style: primaryTextStyle(size: 16, color: textColor)),
                      ),
                      Row(
                        mainAxisSize: MainAxisSize.min,
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: buildDotIndicator(),
                      ),
                      pageNumber != number
                          ? FlatButton(
                        onPressed: () {
                          pageController.nextPage(duration: Duration(milliseconds: 250), curve: Curves.fastOutSlowIn);
                        },
                        child: Text("Next", style: primaryTextStyle(size: 16, color: opBackgroundColor)),
                      )
                          : FlatButton(
                        onPressed: () {
                          finish(context);

                        },
                        child: Text("Submit", style: primaryTextStyle(size: 16, color: opBackgroundColor)),
                      )
                    ],
                  ),
                ),
              ],
            ),
          );
  }

  retrive() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final String _token = prefs.getString(TOKEN);
    setState(() => token = _token);
    print(token);
    setState(() {
      // getMemberList();
      getOrganizationDatas();
      getObsProperty();
      /*getMasterDatas();*/
    });
  }

  Widget masterDropDownBox(var code) {
    var radiolist = common.where((element) => element.id == code).toList();
    _Widget.add(dynamicText(radiolist[0].masterDataObjectValue, textColor: textColor));
    return Container(
        margin: const EdgeInsets.only(top:0, left: 0, right: 20),
        padding: const EdgeInsets.only(top:0, left: 10, right: 10),
        decoration: BoxDecoration(
            borderRadius: const BorderRadius.all(Radius.circular(10.0)),
            border: Border.all(
                color: headerColor,
                width: 2
            ),
            color: Colors.white),
        child: Column(
          children: <Widget>[
            new DropdownButton(
              isExpanded: true,
              iconEnabledColor: headerColor,
              iconDisabledColor: headerColor,
              iconSize: 40,
              dropdownColor: Colors.white,
              hint: new Text("Select Tanent", style: TextStyle(color: headerColor),),
              items: radiolist.map((e) {
                return new DropdownMenuItem(
                  child: new Text(e.value.toUpperCase(), style: TextStyle(color: headerColor),),
                  value: e.value.toString(),);
              }).toList(),
              onChanged: (newvalue) {
                setState(() {
                  positionSelection = newvalue;
                });
              },
              value: positionSelection,)
          ],
        )
    );
  }

  save(String key, String value) async{
    prefs = await SharedPreferences.getInstance();
    prefs.setString(key, value);
  }
  retriveMaster(String key) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final String value = prefs.getString(key);
    return value;
  }

  Widget multiSelect(var code){
    var radiolist = common.where((element) => element.id == code).toList();
    _Widget.add(dynamicText(radiolist[0].masterDataObjectValue, textColor: textColor));
    return Container(
      height: 200,
      decoration: boxDecoration(
        showShadow: false,
        bgColor: ehs_white,
        radius: 8,
        color: border),
      child: MultiSelectBottomSheetField(
        items: radiolist.map((e) => MultiSelectItem(e.value, e.value)).toList(),
        listType: MultiSelectListType.CHIP,
        searchable: true,
        decoration:  boxDecoration(
            showShadow: false,
            bgColor: ehs_white,
            radius: 8,
            color: border),
        onConfirm: (values) {
          setState(() {
            _selectedValues = values;
          });
        },
        chipDisplay: MultiSelectChipDisplay(
          chipColor: Colors.black,
          textStyle: TextStyle(color: Colors.red),
          items: _selectedValues.map((e) => MultiSelectItem(e, e.value)).toList(),
          onTap: (value) {
            setState(() {
              _selectedValues.remove(value);
            });
          },
        ),
      ),
    );
  }

  Widget multiSelections(var code) {
    return Wrap(
      spacing: 8,
      direction: Axis.horizontal,
      children: filterChipsList(code),
    );
  }
  List<Widget> filterChipsList(var code){
    List<Widget> chips = [];
    var radiolist = common.where((element) => element.id == code).toList();
    _ObservationDetailsWidget.add(dynamicText(radiolist[0].masterDataObjectValue, textColor: textColor));
    for(int i = 0; i < radiolist.length; i++){
      MultiSelectionModel multiSelectionModel = new MultiSelectionModel(radiolist[i].value, radiolist[i].id, radiolist[i].masterDataObjectValue, radiolist[i].controlIds, false);
      _multiSelection.add(multiSelectionModel);
    }
    for (int i = 0; i < _multiSelection.length; i++) {
      Widget item = Padding(
        padding: const EdgeInsets.only(left: 10, right: 5),
        child: FilterChip(
          label: Text(_multiSelection[i].value),
          labelStyle: const TextStyle(color: Colors.white,fontSize: 16),
          backgroundColor: Colors.grey,
          selected: _multiSelection[i].isSelected,
          onSelected: (bool value) {
            setState(() {
              print(value);
              _multiSelection[i].isSelected = value;
            });
          },
        ),
      );
      chips.add(item);
    }
    return chips;
  }

  } ```
```

Body:
Try create Dynamic Widget, data from API and add widget in array
&showing in listbuilder but when I click on dropdown, radion selection
not redering. When I click on radio button value print in terminal but
changes not visible same for dropdown and filter chip also.
I want to create form dynamically based on API value like type=TextAREA so create textfield, Type=radioButton so create radio button

class ObservationCreate extends StatefulWidget {
  static String tag = '/ObservationCreate';

  @override
  ObservationCreateState createState() => ObservationCreateState();

}
class ObservationCreateState extends State<ObservationCreate> {
  PageController pageController = PageController(initialPage: 0);
  int pageNumber = 0;
  String token;
  String positionSelection;
  Animation buttonSqueezeanimation;
  AnimationController buttonController;
  var radioValue, previouseValue;
  var getDatas;
  List<EmployeeModel> employee = [];
  List<OrganizationModel> organizations = [];
  List<CommonModel> common = [];
  List<TextEditingController> controllers = [];
  List<dynamic> _selectedValues = [];
  List<MultiSelectionModel> _multiSelection = [];

  List<Widget> buildDotIndicator() {
    List<Widget> list = [];
    for (int i = 0; i <= 2; i++) {
      list.add(i == pageNumber ? indicator(isActive: true) : indicator(
          isActive: false));
    }
    return list;
  }

  List<Widget> _Widget = new List();
  List<Widget> _ObservationDetailsWidget = new List();
  List<int> _display = new List();
  var masterItems;
  bool getMaster = false, observation = false;
  SharedPreferences prefs;
  int number = 1;
  bool selected = false;

  @override
  void initState() {
    retrive();
  }

  Future<void> getMasterDatas(String controlType, var code) async {
    Response response =
    await get(getMasterData, headers: headers).timeout(
        Duration(seconds: timeOut));
    print("masterData:   " + response.body);
    getDatas = json.decode(response.body);
    String error = getDatas["error"];
    if (error != Error) {
      setState(() {
        getMaster = true;
      });
      var count = getDatas["items"].length;
      print(code+count.toString());
      if(common.length != 0){
        common.removeWhere((element) => element.id == code);
      }
      for (int i = 0; i < count; i++) {
        if (getDatas['items'][i]['masterData']['masterDataObjectId'] == code) {
          var masterData = getDatas['items'][i]['masterData'];
          var masterDataObject = getDatas['items'][i]['masterDataObject'];
          print(i);
          CommonModel commonModel = new CommonModel(masterData['value'], masterData['masterDataObjectId'],
              masterDataObject['masterDataObjectValue'], masterData['showControlIDs'] == null ? " " : masterData['showControlIDs']);
          common.add(commonModel);
        }
      }
      if(getMaster){
        print(getMaster);
        switch (controlType){
          case "RADIOBUTTON":
            _Widget.add(radioitem(code));
            break;
          case "MASTERDROPDOWN":
            _Widget.add(masterDropDownBox(code));
            break;
          case "MULTISELECT":
            _ObservationDetailsWidget.add(multiSelections(code));
            _getListings(code);
            break;
        }
      }
    } else {
      print(error);
    }
  }

  Future<void> getObsProperty() async {
    var items;
    String url = "https://webgateway.demoehswatch.com/api/observations-service/propertysettings/GetListPropertyAsync" +
        "?Authorization=bearer" + token + "&MaxResultCount=1000";
    final Uri getObsPropertyData = Uri.parse(url);
    Response response =
    await get(getObsPropertyData, headers: header).timeout(
        Duration(seconds: timeOut));
    var getData = json.decode(response.body);
    String error = getData["error"];
    if (error != Error) {
      var count = getData["items"].length;
      for (int i = 0; i < count; i++) {
        if (getData['items'][i]['providerName'] == "Observation" &&
            getData['items'][i]['visible'] &&
            getData['items'][i]['defaultDisplay'] &&
            getData['items'][i]['psDisplaySection'] == "Draft") {
          items = getData['items'][i];
          print(items["controlType"]+"   "+items["name"]);
          _display.add(items["displayOrderInView"]);
          _display.sort();
        }
      }
      for (int i = 0; i < _display.length; i++) {
        for (int j = 0; j < count; j++) {
          if (getData['items'][j]['providerName'] == "Observation" &&
              getData['items'][j]['visible'] &&
              getData['items'][j]['defaultDisplay'] &&
              getData['items'][j]['psDisplaySection'] == "Draft") {
            var ite = getData['items'][j];
            if (_display[i].toString() == ite["displayOrderInView"].toString()) {
              switch(ite['controlType']){
                case "TEXTBOX":
                  TextEditingController controller = TextEditingController();
                  controllers.add(controller);
                  setState(() {
                    if (ite['masterDataObjectCode'] == "")
                      _Widget.add(dynamicText(
                          ite['name'].toString().split("Observation.")[1],
                          textColor: textColor));
                    _Widget.add(dynamicTextbox(50, controller));
                  });
                  break;
                case "RADIOBUTTON":
                    print(ite['masterDataObjectCode']);
                    var codeRadio = ite['masterDataObjectCode'];
                    getMasterDatas(ite['controlType'], codeRadio);
                    print(getMaster);
                  break;
                case "MASTERDROPDOWN":
                    print(ite['masterDataObjectCode']);
                    var codeDropDown = ite['masterDataObjectCode'];
                    getMasterDatas(ite['controlType'], codeDropDown);
                  break;
              }
            }
          }
        }
      }
    } else {
      print(error);
    }
  }

  Future<void> getObsPropertyObservationDetails(String objectCode) async {
    var items;
    String url = "https://webgateway.demoehswatch.com/api/observations-service/propertysettings/GetListPropertyAsync" +
        "?Authorization=bearer" + token + "&MaxResultCount=1000";
    final Uri getObsPropertyData = Uri.parse(url);
    Response response =
    await get(getObsPropertyData, headers: header).timeout(
        Duration(seconds: timeOut));
    var getData = json.decode(response.body);
    String error = getData["error"];
    if (error != Error) {
      var count = getData["items"].length;
      for (int i = 0; i < count; i++) {
        if (getData['items'][i]['providerName'] == "Observation" &&
            getData['items'][i]['visible'] &&
            getData['items'][i]['psDisplaySection'] == "Draft" &&
        getData['items'][i]['masterDataObjectCode'] == objectCode) {
          items = getData['items'][i];
          print(items["controlType"]+"   "+items["name"]);
          setState(() {
            observation = true;
            number = 2;
            getMasterDatas(items["controlType"], getData['items'][i]['masterDataObjectCode']);
          });

        }
      }
    } else {
      print(error);
    }
  }

  @override
  void dispose() {
    controllers.clear();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery
        .of(context)
        .size;
    return Scaffold(
        appBar: AppBar(
          title: const Text('New Observation'),
          backgroundColor: appBar,
        ),
        /*body: Container(
          padding: const EdgeInsets.only(left: 20, right: 20),
          decoration: BoxDecoration(
            color: Color(0xFFF5F5F6),
          ),
          child: Scaffold(
            backgroundColor: Colors.transparent,
            body: Column(
              children: <Widget>[
                Expanded(
                    child: ListView.builder(
                        padding: EdgeInsets.only(top: 10),
                        itemCount: _Widget.length,
                        itemBuilder: (context, index) {
                          Widget widget = _Widget.elementAt(index);
                          return widget;
                        })
                )
              ],
            ),*/
            body: Stack(
              alignment: Alignment.bottomCenter,
              children: <Widget>[
                Container(
                  height: double.infinity,
                  color: bg,
                  child: PageView(
                    onPageChanged: (index) => setState(() {
                      pageNumber = index;
                    }),
                    controller: pageController,
                    children: <Widget>[
                      Container(
                        padding: EdgeInsets.only(left: 16, bottom: 70, right: 16, top: 30),
                        child: Column(
                          children: <Widget>[
                            Expanded(
                                child: ListView.builder(
                                    shrinkWrap: true,
                                    padding: EdgeInsets.only(top: 10),
                                    itemCount: _Widget.length,
                                    itemBuilder: (context, index) {
                                      Widget widget = _Widget.elementAt(index);
                                      return widget;
                                    })
                            ),
                          ],
                        ),
                      ),
                      if(observation)
                      Container(
                          padding: EdgeInsets.only(left: 16, bottom: 70, right: 16, top: 30),
                          child: Column(
                              children: <Widget>[
                                Expanded(
                                  child: ListView.builder(
                                      shrinkWrap: true,
                                      padding: EdgeInsets.only(top: 10),
                                      itemCount: _ObservationDetailsWidget.length,
                                      itemBuilder: (context, index) {
                                        Widget widget = _ObservationDetailsWidget.elementAt(index);
                                        return widget;
                                      })
                                    )*/
                                  ]
                                )
                      ),

                      Container(
                        padding: EdgeInsets.only(left: 16, bottom: 70, right: 16, top: 30),
                        child: SingleChildScrollView(
                          padding: EdgeInsets.only(bottom: 60),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            mainAxisAlignment: MainAxisAlignment.center,
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              Container(
                                padding: EdgeInsets.all(20),
                                decoration: boxDecoration(
                                    showShadow: true,
                                    bgColor: Colors.white,
                                    radius: 8,
                                    color: border),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Image.asset("images/app/icons/upload.png", width: 50,),
                                    SizedBox(height: 10,),
                                    Text(uploadFile, style: TextStyle(fontSize: 15),),
                                    SizedBox(height: 10,),
                                    Container(
                                      decoration: boxDecoration(
                                          showShadow: true,
                                          bgColor: textColor,
                                          radius: 8,
                                          color: border),
                                      child: RaisedButton(
                                        onPressed: () {},
                                        color: textColor,
                                        child: Text(
                                          "Upload Files",
                                          style: TextStyle(color: Colors.white),
                                        ),
                                      ),
                                    )
                                  ],
                                ),
                              )
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 16, right: 16, bottom: 20),
                  padding: EdgeInsets.only(left: 20, right: 20),
                  width: size.width,
                  height: 50,
                  decoration: BoxDecoration(color: textColor, borderRadius: BorderRadius.circular(15.0)),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      CircleAvatar(
                        backgroundColor: Colors.white,
                        radius: 15,
                        child: Text('${pageNumber + 1}', style: primaryTextStyle(size: 16, color: textColor)),
                      ),
                      Row(
                        mainAxisSize: MainAxisSize.min,
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: buildDotIndicator(),
                      ),
                      pageNumber != number
                          ? FlatButton(
                        onPressed: () {
                          pageController.nextPage(duration: Duration(milliseconds: 250), curve: Curves.fastOutSlowIn);
                        },
                        child: Text("Next", style: primaryTextStyle(size: 16, color: opBackgroundColor)),
                      )
                          : FlatButton(
                        onPressed: () {
                          finish(context);

                        },
                        child: Text("Submit", style: primaryTextStyle(size: 16, color: opBackgroundColor)),
                      )
                    ],
                  ),
                ),
              ],
            ),
          );
  }

  retrive() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final String _token = prefs.getString(TOKEN);
    setState(() => token = _token);
    print(token);
    setState(() {
      // getMemberList();
      getOrganizationDatas();
      getObsProperty();
      /*getMasterDatas();*/
    });
  }

  Widget masterDropDownBox(var code) {
    var radiolist = common.where((element) => element.id == code).toList();
    _Widget.add(dynamicText(radiolist[0].masterDataObjectValue, textColor: textColor));
    return Container(
        margin: const EdgeInsets.only(top:0, left: 0, right: 20),
        padding: const EdgeInsets.only(top:0, left: 10, right: 10),
        decoration: BoxDecoration(
            borderRadius: const BorderRadius.all(Radius.circular(10.0)),
            border: Border.all(
                color: headerColor,
                width: 2
            ),
            color: Colors.white),
        child: Column(
          children: <Widget>[
            new DropdownButton(
              isExpanded: true,
              iconEnabledColor: headerColor,
              iconDisabledColor: headerColor,
              iconSize: 40,
              dropdownColor: Colors.white,
              hint: new Text("Select Tanent", style: TextStyle(color: headerColor),),
              items: radiolist.map((e) {
                return new DropdownMenuItem(
                  child: new Text(e.value.toUpperCase(), style: TextStyle(color: headerColor),),
                  value: e.value.toString(),);
              }).toList(),
              onChanged: (newvalue) {
                setState(() {
                  positionSelection = newvalue;
                });
              },
              value: positionSelection,)
          ],
        )
    );
  }

  save(String key, String value) async{
    prefs = await SharedPreferences.getInstance();
    prefs.setString(key, value);
  }
  retriveMaster(String key) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final String value = prefs.getString(key);
    return value;
  }

  Widget multiSelect(var code){
    var radiolist = common.where((element) => element.id == code).toList();
    _Widget.add(dynamicText(radiolist[0].masterDataObjectValue, textColor: textColor));
    return Container(
      height: 200,
      decoration: boxDecoration(
        showShadow: false,
        bgColor: ehs_white,
        radius: 8,
        color: border),
      child: MultiSelectBottomSheetField(
        items: radiolist.map((e) => MultiSelectItem(e.value, e.value)).toList(),
        listType: MultiSelectListType.CHIP,
        searchable: true,
        decoration:  boxDecoration(
            showShadow: false,
            bgColor: ehs_white,
            radius: 8,
            color: border),
        onConfirm: (values) {
          setState(() {
            _selectedValues = values;
          });
        },
        chipDisplay: MultiSelectChipDisplay(
          chipColor: Colors.black,
          textStyle: TextStyle(color: Colors.red),
          items: _selectedValues.map((e) => MultiSelectItem(e, e.value)).toList(),
          onTap: (value) {
            setState(() {
              _selectedValues.remove(value);
            });
          },
        ),
      ),
    );
  }

  Widget multiSelections(var code) {
    return Wrap(
      spacing: 8,
      direction: Axis.horizontal,
      children: filterChipsList(code),
    );
  }
  List<Widget> filterChipsList(var code){
    List<Widget> chips = [];
    var radiolist = common.where((element) => element.id == code).toList();
    _ObservationDetailsWidget.add(dynamicText(radiolist[0].masterDataObjectValue, textColor: textColor));
    for(int i = 0; i < radiolist.length; i++){
      MultiSelectionModel multiSelectionModel = new MultiSelectionModel(radiolist[i].value, radiolist[i].id, radiolist[i].masterDataObjectValue, radiolist[i].controlIds, false);
      _multiSelection.add(multiSelectionModel);
    }
    for (int i = 0; i < _multiSelection.length; i++) {
      Widget item = Padding(
        padding: const EdgeInsets.only(left: 10, right: 5),
        child: FilterChip(
          label: Text(_multiSelection[i].value),
          labelStyle: const TextStyle(color: Colors.white,fontSize: 16),
          backgroundColor: Colors.grey,
          selected: _multiSelection[i].isSelected,
          onSelected: (bool value) {
            setState(() {
              print(value);
              _multiSelection[i].isSelected = value;
            });
          },
        ),
      );
      chips.add(item);
    }
    return chips;
  }

  } ```


问:
Python lock class attribute while main thread is executing
My main script launches an API and another thread executes a daily task to update data.
Currently, the daily update can change data during an API call and it will cause an error.

I think my problem can be summarized as the following code :

```<code>import schedule
from threading import Thread, Lock
import time

def periodically_run_pending_tasks(interval=0.5):
    while True:
        schedule.run_pending()
        time.sleep(interval)

def change_state(item):
    item.a = "New state"

class MyClass:
    def __init__(self):
        self.a = "Initial state"

    def print_state_twice(self, lk):
        lk.acquire()
        print(self.a)
        time.sleep(2)
        print(self.a)
        lk.release()

my_class = MyClass()
lock = Lock()

thread = Thread(target=periodically_run_pending_tasks)
schedule.every().second.do(change_state, my_class)
thread.start()

my_class.print_state_twice(lock)
</code>```

I want this script to output either twice Initial state or twice New state but it returns Initial State New state.

I've also tried :

```<code>import schedule
from threading import Thread, Lock
import time

def periodically_run_pending_tasks(interval=0.5):
    while True:
        schedule.run_pending()
        time.sleep(interval)

def change_state(item):
    item.a = "New state"

class MyClass:
    def __init__(self):
        self.a = "Initial state"

    def print_state_twice(self, lk):
        lk.acquire()
        print(self.a)
        time.sleep(2)
        print(self.a)
        lk.release()

my_class = MyClass()
lock = Lock()

thread_2 = Thread(target=my_class.print_state_twice, args=(lock,))
thread_2.start()

thread = Thread(target=periodically_run_pending_tasks)
schedule.every().second.do(change_state, my_class)
thread.start()
</code>```

Body:
My main script launches an API and another thread executes a daily task to update data.
Currently, the daily update can change data during an API call and it will cause an error.

I think my problem can be summarized as the following code :

import schedule
from threading import Thread, Lock
import time

def periodically_run_pending_tasks(interval=0.5):
    while True:
        schedule.run_pending()
        time.sleep(interval)

def change_state(item):
    item.a = "New state"

class MyClass:
    def __init__(self):
        self.a = "Initial state"

    def print_state_twice(self, lk):
        lk.acquire()
        print(self.a)
        time.sleep(2)
        print(self.a)
        lk.release()

my_class = MyClass()
lock = Lock()

thread = Thread(target=periodically_run_pending_tasks)
schedule.every().second.do(change_state, my_class)
thread.start()

my_class.print_state_twice(lock)

I want this script to output either twice Initial state or twice New state but it returns Initial State New state.

I've also tried :

import schedule
from threading import Thread, Lock
import time

def periodically_run_pending_tasks(interval=0.5):
    while True:
        schedule.run_pending()
        time.sleep(interval)

def change_state(item):
    item.a = "New state"

class MyClass:
    def __init__(self):
        self.a = "Initial state"

    def print_state_twice(self, lk):
        lk.acquire()
        print(self.a)
        time.sleep(2)
        print(self.a)
        lk.release()

my_class = MyClass()
lock = Lock()

thread_2 = Thread(target=my_class.print_state_twice, args=(lock,))
thread_2.start()

thread = Thread(target=periodically_run_pending_tasks)
schedule.every().second.do(change_state, my_class)
thread.start()


问:
Django q process does not get cleared from memory
I have integrated Django Q in my project and i'm running a task from django q but after task ran successfully i can still see the process is in memory is there any way to clear the process from memory after it has finished the job.

Here is the django q settings

```<code>Q_CLUSTER = {
'name': 'my_app',
'workers': 8,
'recycle': 500,
'compress': True,
'save_limit': 250,
'queue_limit': 500,
'cpu_affinity': 1,
'label': 'Django Q',
'max_attempts': 1,
'attempt_count': 1,
'catch_up': False,
'redis': {
    'host': '127.0.0.1',
    'port': 6379,
    'db': 0, }}
</code>```

Body:
I have integrated Django Q in my project and i'm running a task from django q but after task ran successfully i can still see the process is in memory is there any way to clear the process from memory after it has finished the job.

Here is the django q settings

Q_CLUSTER = {
'name': 'my_app',
'workers': 8,
'recycle': 500,
'compress': True,
'save_limit': 250,
'queue_limit': 500,
'cpu_affinity': 1,
'label': 'Django Q',
'max_attempts': 1,
'attempt_count': 1,
'catch_up': False,
'redis': {
    'host': '127.0.0.1',
    'port': 6379,
    'db': 0, }}


问:
Not able to connect to public postgres from EC2
I wanted to connect to a public postgres from my EC2 machine but not able to establish a connection. I have added a outbound rule for postgress port 543 but no luck so far.

Below is the details of the postgress i am trying to connect.

```<code>conn = psycopg2.connect(
    host="hh-pgsql-public.ebi.ac.uk",
    database="pfmegrnargs",
    user="reader",
    password="NWDMCE5xdipIjRrp")
</code>```

Also attached is the outbound rule and error message for reference.

Appreciate all your help !!

Body:
I wanted to connect to a public postgres from my EC2 machine but not able to establish a connection. I have added a outbound rule for postgress port 543 but no luck so far.

Below is the details of the postgress i am trying to connect.

conn = psycopg2.connect(
    host="hh-pgsql-public.ebi.ac.uk",
    database="pfmegrnargs",
    user="reader",
    password="NWDMCE5xdipIjRrp")

Also attached is the outbound rule and error message for reference.

Appreciate all your help !!


问:
SSHJ Java Library - set cipher suit for SFTP connection
We are using SSHJ library for connecting to SFTP server using SSHv2. While connecting to the server we get below Negotiated algorithms:

```<code>net.schmizz.sshj.transport.KeyExchanger:234 - Negotiated algorithms: [ kex=diffie-hellman-group-exchange-sha256; sig=ssh-rsa; c2sCipher=aes128-cbc; s2cCipher=aes128-cbc; c2sMAC=hmac-sha1; s2cMAC=hmac-sha1; c2sComp=none; s2cComp=none; rsaSHA2Support=false ]
</code>```

Our Requirement is to set the Cipher to AEAD_AES_x_GCM
x=256,128 or AESx-CTR with HMAC-SHA2-y
x=256,192,128 and y=512,256 . I tried to set the cipher through below implementation:

```<code>Config config = new DefaultConfig();
            config.setCipherFactories(initCipherFactories());
            SSHClient client = new SSHClient(config);

protected List<Factory.Named<Cipher>> initCipherFactories() {
        List<Factory.Named<Cipher>> avail = new LinkedList<>(
                Arrays.asList(new AES256CTR.Factory(), new AES256CBC.Factory()));
        boolean warn = false;
        // Ref. https://issues.apache.org/jira/browse/SSHD-24
        // "AES256 and AES192 requires unlimited cryptography extension"
        for (Iterator<Factory.Named<Cipher>> i = avail.iterator(); i.hasNext(); ) {
            final Factory.Named<Cipher> f = i.next();
            try {
                final Cipher c = f.create();
                final byte[] key = new byte[c.getBlockSize()];
                final byte[] iv = new byte[c.getIVSize()];
                c.init(Cipher.Mode.Encrypt, key, iv);
            } catch (Exception e) {
                warn = true;
                i.remove();
                e.printStackTrace();
            }
        }
        if (warn)
            log.warn("Disabling high-strength ciphers: cipher strengths apparently limited by JCE policy");

        return avail;
    }
</code>```

Can you tell me new AES256CTR.Factory(), new AES256CBC.Factory() these are deprecated in SSHJ library so what is came in place of this?

Body:
We are using SSHJ library for connecting to SFTP server using SSHv2. While connecting to the server we get below Negotiated algorithms:

net.schmizz.sshj.transport.KeyExchanger:234 - Negotiated algorithms: [ kex=diffie-hellman-group-exchange-sha256; sig=ssh-rsa; c2sCipher=aes128-cbc; s2cCipher=aes128-cbc; c2sMAC=hmac-sha1; s2cMAC=hmac-sha1; c2sComp=none; s2cComp=none; rsaSHA2Support=false ]

Our Requirement is to set the Cipher to AEAD_AES_x_GCM
x=256,128 or AESx-CTR with HMAC-SHA2-y
x=256,192,128 and y=512,256 . I tried to set the cipher through below implementation:

Config config = new DefaultConfig();
            config.setCipherFactories(initCipherFactories());
            SSHClient client = new SSHClient(config);

protected List<Factory.Named<Cipher>> initCipherFactories() {
        List<Factory.Named<Cipher>> avail = new LinkedList<>(
                Arrays.asList(new AES256CTR.Factory(), new AES256CBC.Factory()));
        boolean warn = false;
        // Ref. https://issues.apache.org/jira/browse/SSHD-24
        // "AES256 and AES192 requires unlimited cryptography extension"
        for (Iterator<Factory.Named<Cipher>> i = avail.iterator(); i.hasNext(); ) {
            final Factory.Named<Cipher> f = i.next();
            try {
                final Cipher c = f.create();
                final byte[] key = new byte[c.getBlockSize()];
                final byte[] iv = new byte[c.getIVSize()];
                c.init(Cipher.Mode.Encrypt, key, iv);
            } catch (Exception e) {
                warn = true;
                i.remove();
                e.printStackTrace();
            }
        }
        if (warn)
            log.warn("Disabling high-strength ciphers: cipher strengths apparently limited by JCE policy");

        return avail;
    }

Can you tell me new AES256CTR.Factory(), new AES256CBC.Factory() these are deprecated in SSHJ library so what is came in place of this?


问:
Should two separate COM devices be placed into the same thread?
I'm looking to control a motion controller with a joystick. Should I place the Joystick in the same thread as the motion controller or place it in its own thread?

Currently I have a UI that send requests to a thread containing the motion controller's request manager. I use a system of queues to safely transfer data.

In the past I have made an application where all the devices had their own thread. The project quickly became clunky and messy. I am new to working with hardware. I would greatly appreciate any references or books suggestions.


问:
Spark RDD Inner Join Performance after collecting a sample of data
I have two Spark Jobs where they read two datasets from a Hadoop cluster, load the data to RDDs and perform an inner join. The difference between those two jobs is a sampling operation before the inner join, which I need it for building a histogram of the data sets. The sampling operation is performed for both data sets as rdd.sample(false, 0.01 261).collect() (I'm not using the takeSample() method). The job that integrates the sampling procedure runs much faster in terms of execution time than the other job that simply performs the inner join (the time difference is about ~-25%!).

JavaRDD<String> rddTxtFilesA = jsc.textFile("hdfs://node1:9000/user/datasetA.csv");
JavaRDD<String> rddTxtFilesB = jsc.textFile("hdfs://node1:9000/user/datasetB.csv");

JavaRDD<Tuple3<String, Double, Double>> rddTuplesA = rddTxtFilesA.map((String line) -> {
      String[] elements = line.split("\t");
      return new Tuple3<>(elements[0], Double.parseDouble(elements[1]), Double.parseDouble(elements[2]));
});

JavaRDD<Tuple3<String, Double, Double>> rddTuplesB = rddTxtFilesB.map((String line) -> {
      String[] elements = line.split("\t");
      return new Tuple3<>(elements[0], Double.parseDouble(elements[1]), Double.parseDouble(elements[2]));
});

//The different code part in the two Spark Jobs where sampling operation occurs
rddTuplesA.sample(false, 0.01, 261).collect().forEach((tuple) ->
          //build histogram
);

rddTuplesB.sample(false, 0.01, 261).collect().forEach((tuple) ->
          //build histogram
);
//sampling operation finishes

JavaPairRDD<Integer, Tuple3<String, Double, Double>> pairRDDA = rddTuplesA.flatMapToPair(...);
JavaPairRDD<Integer, Tuple3<String, Double, Double>> pairRDDB = rddTuplesB.flatMapToPair(...);

CustomPartitioner cp = ...

JavaPairRDD<Integer, Tuple2<Tuple3<String, Double, Double>, Tuple3<String, Double, Double>>> joinedRDD = pairRDDA.join(pairRDDB,cp);

long count = joinedRDD.count();
```

It seems that the collect() operation somehow affects the job and makes it running faster. Any thoughts why is this happening? I'm using the 3.1.3 version of Spark.

Note also that I have replaced the collect() method with the count() method and the same happens. Thus, I suspect that it is the action that somehow affects the job's execution time.

Body:
I have two Spark Jobs where they read two datasets from a Hadoop cluster, load the data to RDDs and perform an inner join. The difference between those two jobs is a sampling operation before the inner join, which I need it for building a histogram of the data sets. The sampling operation is performed for both data sets as rdd.sample(false, 0.01 261).collect() (I'm not using the takeSample() method). The job that integrates the sampling procedure runs much faster in terms of execution time than the other job that simply performs the inner join (the time difference is about ~-25%!).

JavaRDD<String> rddTxtFilesA = jsc.textFile("hdfs://node1:9000/user/datasetA.csv");
JavaRDD<String> rddTxtFilesB = jsc.textFile("hdfs://node1:9000/user/datasetB.csv");

JavaRDD<Tuple3<String, Double, Double>> rddTuplesA = rddTxtFilesA.map((String line) -> {
      String[] elements = line.split("\t");
      return new Tuple3<>(elements[0], Double.parseDouble(elements[1]), Double.parseDouble(elements[2]));
});

JavaRDD<Tuple3<String, Double, Double>> rddTuplesB = rddTxtFilesB.map((String line) -> {
      String[] elements = line.split("\t");
      return new Tuple3<>(elements[0], Double.parseDouble(elements[1]), Double.parseDouble(elements[2]));
});

//The different code part in the two Spark Jobs where sampling operation occurs
rddTuplesA.sample(false, 0.01, 261).collect().forEach((tuple) ->
          //build histogram
);

rddTuplesB.sample(false, 0.01, 261).collect().forEach((tuple) ->
          //build histogram
);
//sampling operation finishes

JavaPairRDD<Integer, Tuple3<String, Double, Double>> pairRDDA = rddTuplesA.flatMapToPair(...);
JavaPairRDD<Integer, Tuple3<String, Double, Double>> pairRDDB = rddTuplesB.flatMapToPair(...);

CustomPartitioner cp = ...

JavaPairRDD<Integer, Tuple2<Tuple3<String, Double, Double>, Tuple3<String, Double, Double>>> joinedRDD = pairRDDA.join(pairRDDB,cp);

long count = joinedRDD.count();

It seems that the collect() operation somehow affects the job and makes it running faster. Any thoughts why is this happening? I'm using the 3.1.3 version of Spark.

Note also that I have replaced the collect() method with the count() method and the same happens. Thus, I suspect that it is the action that somehow affects the job's execution time.


问:
Why is the pinned content of my scrolltrigger disappearing (Locomotive Scroll)
My slider component disappears when it should pin to the page. I believe it is related to the page scroll with Locomotive scroll messing up with translating the page. However, this should be possible and I found a tutorial by Greensock. I cannot get it to work though, my content keeps disappearing. Here is a minimal demo: https://codesandbox.io/s/cool-noyce-108h6c?file=/pages/index.tsx

Here is also a link to the final webpage: https://juliaweber-git-preview-inzn.vercel.app

Any help is greatly appreciated!


问:
How to implement generic methods in java 8
I want to write the following similar methods.

method1:

```<code>public void method1(A a){
 //do some thing
}
</code>```

method2:

```<code>public C method2(B b){
 //do some thing
}
</code>```

method3:

```<code>public D method3(E b){
 //do some thing
}
</code>```

the processing logic of the three interfaces method1, method2, and method3 is the same. I would like to ask whether these three methods can be abstracted by generic methods or design patterns.

Body:
I want to write the following similar methods.

method1:

public void method1(A a){
 //do some thing
}

method2:

public C method2(B b){
 //do some thing
}

method3:

public D method3(E b){
 //do some thing
}

the processing logic of the three interfaces method1, method2, and method3 is the same. I would like to ask whether these three methods can be abstracted by generic methods or design patterns.


问:
gridsearch before RFE is taking super long
I try to do a gridsearch on my dataset to know how many features i want to select in my RFE, but it is taking super long. Does anyone know if this is normal, or do i have a foult in my script?

```<code>
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import RFECV, RFE
from sklearn.model_selection import StratifiedKFold, GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
import matplotlib.pyplot as plt

#%% train-test split

data = pd.read_csv('preprocesseddata.csv')
data.drop(['Date', 'About'], axis=1, inplace=True)
y = data['Class']
X = data[['Duration_Ball Training','Duration_Match','Duration_Other','Duration_Strenght Training','Positie','Gender','Voorkeursbeen','Instroomjaar','Age','Hours Sleep','Stress','Muscle Soreness','T-test','20m Sprint','CMJ 2b','Yo Yo Result','Heart Rate (Max)','Latest Height', 'Body Fat %','Repetitive Injury','Prefered Leg','AcuteLegs_1day','AcuteCardio_1day','AcuteLegs_3days','AcuteCardio_3days','AcuteLegs_7days','AcuteCardio_7days','ChronicLegs_14days','ChronicCardio_14days','ChronicLegs_21days','ChronicCardio_21days','ChronicLegs_28days','ChronicCardio_28days','TrainingmonotonyLegs','TrainingmonotonyCardio']]

y = y.astype('category')
y = y.cat.codes

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42, stratify=y)

sc = StandardScaler()
X_train_std = sc.fit_transform(X_train)
X_test_std = sc.transform(X_test)

#%% RFE as part of pipeline

lr = LogisticRegression(solver='liblinear', random_state=123)
pipe = make_pipeline(RFE(estimator=lr, step=1), KNeighborsClassifier())

parameters = {'rfe__n_features_to_select': range(1,35), 'kneighborsclassifier__n_neighbors': range(1,30)}
grid = GridSearchCV(pipe, param_grid=parameters, cv=10, n_jobs=1)
grid.fit(X_train_std, y_train)

print('Best params:', grid.best_params_)
print('Best accuracy:', grid.best_score_)

#%% RFE

lr = LogisticRegression(solver='liblinear', random_state=123)

rfe= RFE(estimator=lr, n_features_to_select=5, step=-1)
rfe.fit(X_train_std, y_train)

X_train_sub = rfe.transform(X_train_std)

rfe.support_
</code>```

It seems to get stuck at the print best parameters line

Body:
I try to do a gridsearch on my dataset to know how many features i want to select in my RFE, but it is taking super long. Does anyone know if this is normal, or do i have a foult in my script?

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import RFECV, RFE
from sklearn.model_selection import StratifiedKFold, GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
import matplotlib.pyplot as plt

#%% train-test split

data = pd.read_csv('preprocesseddata.csv')
data.drop(['Date', 'About'], axis=1, inplace=True)
y = data['Class']
X = data[['Duration_Ball Training','Duration_Match','Duration_Other','Duration_Strenght Training','Positie','Gender','Voorkeursbeen','Instroomjaar','Age','Hours Sleep','Stress','Muscle Soreness','T-test','20m Sprint','CMJ 2b','Yo Yo Result','Heart Rate (Max)','Latest Height', 'Body Fat %','Repetitive Injury','Prefered Leg','AcuteLegs_1day','AcuteCardio_1day','AcuteLegs_3days','AcuteCardio_3days','AcuteLegs_7days','AcuteCardio_7days','ChronicLegs_14days','ChronicCardio_14days','ChronicLegs_21days','ChronicCardio_21days','ChronicLegs_28days','ChronicCardio_28days','TrainingmonotonyLegs','TrainingmonotonyCardio']]

y = y.astype('category')
y = y.cat.codes

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42, stratify=y)

sc = StandardScaler()
X_train_std = sc.fit_transform(X_train)
X_test_std = sc.transform(X_test)

#%% RFE as part of pipeline

lr = LogisticRegression(solver='liblinear', random_state=123)
pipe = make_pipeline(RFE(estimator=lr, step=1), KNeighborsClassifier())

parameters = {'rfe__n_features_to_select': range(1,35), 'kneighborsclassifier__n_neighbors': range(1,30)}
grid = GridSearchCV(pipe, param_grid=parameters, cv=10, n_jobs=1)
grid.fit(X_train_std, y_train)

print('Best params:', grid.best_params_)
print('Best accuracy:', grid.best_score_)

#%% RFE

lr = LogisticRegression(solver='liblinear', random_state=123)

rfe= RFE(estimator=lr, n_features_to_select=5, step=-1)
rfe.fit(X_train_std, y_train)

X_train_sub = rfe.transform(X_train_std)

rfe.support_

It seems to get stuck at the print best parameters line


问:
PyFlink CDC Connectors Postgres failure
Trying to follow the Flink CDC Connectors Postgres tutorial using PyFlink:

https://ververica.github.io/flink-cdc-connectors/master/content/quickstart/mysql-postgres-tutorial.html

Failing Code

    ddl = """
    CREATE TABLE shipments (
  shipment_id INT,
  order_id INT,
  origin STRING,
  destination STRING,
  is_arrived BOOLEAN
) WITH (
  'connector' = 'postgres-cdc',
  'hostname' = 'localhost',
  'port' = '5432',
  'username' = 'postgres',
  'password' = 'postgres',
  'database-name' = 'postgres',
  'schema-name' = 'public',
  'slot.name' = 'slot2',
  'table-name' = 'shipments'
);
"""
    table_env.execute_sql(ddl)
    table2: Table = table_env.sql_query("SELECT * FROM shipments")
    table2.execute().print()
```
<h2>Main Stacktrace</h2>
```Caused by: io.debezium.DebeziumException: Creation of replication slot failed
    at io.debezium.connector.postgresql.PostgresConnectorTask.start(PostgresConnectorTask.java:141)
    at io.debezium.connector.common.BaseSourceTask.start(BaseSourceTask.java:130)
    at io.debezium.embedded.EmbeddedEngine.run(EmbeddedEngine.java:759)
    at io.debezium.embedded.ConvertingEngineBuilder$2.run(ConvertingEngineBuilder.java:188)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "CREATE_REPLICATION_SLOT"
  Position: 1
```
<h2>Troubleshooting</h2>
<p>CDC needs access to Postgres' WAL, write ahead log. In order to get the WAL Postgres needs to have a replication slot. So you need to set this up in Postgres, I got that done with this command:</p>
<pre class="lang-sql prettyprint-override"><code>ALTER SYSTEM SET wal_level = logical

SELECT * FROM pg_create_logical_replication_slot('slot2', 'test_decoding')
</code>```

Then restart Postgres.

CDC code that get the replication slot

jdbcConnection.getReplicationSlotState(connectorConfig.slotName(), connectorConfig.plugin().getPostgresPluginName());
```

called from:

https://github.com/debezium/debezium/blob/main/debezium-connector-postgres/src/main/java/io/debezium/connector/postgresql/PostgresConnectorTask.java

When that doesn't work you it will try to create the replication slot and that is where the exception is thrown.

Possible version issue

Maybe the problem is that I am running Postgres 13 and Ververica CDC might only support up to Postgres 12.

Title: PyFlink CDC Connectors Postgres failure

Body:
Trying to follow the Flink CDC Connectors Postgres tutorial using PyFlink:

https://ververica.github.io/flink-cdc-connectors/master/content/quickstart/mysql-postgres-tutorial.html

Failing Code

    ddl = """
    CREATE TABLE shipments (
  shipment_id INT,
  order_id INT,
  origin STRING,
  destination STRING,
  is_arrived BOOLEAN
) WITH (
  'connector' = 'postgres-cdc',
  'hostname' = 'localhost',
  'port' = '5432',
  'username' = 'postgres',
  'password' = 'postgres',
  'database-name' = 'postgres',
  'schema-name' = 'public',
  'slot.name' = 'slot2',
  'table-name' = 'shipments'
);
"""
    table_env.execute_sql(ddl)
    table2: Table = table_env.sql_query("SELECT * FROM shipments")
    table2.execute().print()

Main Stacktrace

Caused by: io.debezium.DebeziumException: Creation of replication slot failed
    at io.debezium.connector.postgresql.PostgresConnectorTask.start(PostgresConnectorTask.java:141)
    at io.debezium.connector.common.BaseSourceTask.start(BaseSourceTask.java:130)
    at io.debezium.embedded.EmbeddedEngine.run(EmbeddedEngine.java:759)
    at io.debezium.embedded.ConvertingEngineBuilder$2.run(ConvertingEngineBuilder.java:188)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "CREATE_REPLICATION_SLOT"
  Position: 1

Troubleshooting

CDC needs access to Postgres' WAL, write ahead log. In order to get the WAL Postgres needs to have a replication slot. So you need to set this up in Postgres, I got that done with this command:

ALTER SYSTEM SET wal_level = logical

SELECT * FROM pg_create_logical_replication_slot('slot2', 'test_decoding')

Then restart Postgres.

CDC code that get the replication slot

jdbcConnection.getReplicationSlotState(connectorConfig.slotName(), connectorConfig.plugin().getPostgresPluginName());

called from:

https://github.com/debezium/debezium/blob/main/debezium-connector-postgres/src/main/java/io/debezium/connector/postgresql/PostgresConnectorTask.java

When that doesn't work you it will try to create the replication slot and that is where the exception is thrown.

Possible version issue

Maybe the problem is that I am running Postgres 13 and Ververica CDC might only support up to Postgres 12.