问:
Which methods should be inside a Page class in POM design pattern?
I am creating a UI test automation framework using POM design pattern. After reading SeleniumHQ page for Page Objects, I am contemplating what all methods should I create inside a Page Object.

Let's take a simple example of login page object consisting of username, password textboxes and submit button. SeleniumHQ link has created the below methods :

```<code>1. typeUsername(String username)
2. typePassword(String password)
3. submitLogin()
4. submitLoginExceptionFailure()
5. loginAs(String username, String password)
</code>```

I am a bit perplexed while looking at these methods. Why should I create first 3 methods(typeUsername, typePassword,submitLogin), when I already am creating a loginAs method. Any thoughts around it ?

SeleniumHQ Link - https://github.com/SeleniumHQ/selenium/wiki/PageObjects

Pasting the code PageObject code for LoginPage :

```<code>public class LoginPage {
    private final WebDriver driver;

    public LoginPage(WebDriver driver) {
        this.driver = driver;

        // Check that we're on the right page.
        if (!"Login".equals(driver.getTitle())) {
            // Alternatively, we could navigate to the login page, perhaps logging out first
            throw new IllegalStateException("This is not the login page");
        }
    }

    // The login page contains several HTML elements that will be represented as WebElements.
    // The locators for these elements should only be defined once.
        By usernameLocator = By.id("username");
        By passwordLocator = By.id("passwd");
        By loginButtonLocator = By.id("login");

    // The login page allows the user to type their username into the username field
    public LoginPage typeUsername(String username) {
        // This is the only place that "knows" how to enter a username
        driver.findElement(usernameLocator).sendKeys(username);

        // Return the current page object as this action doesn't navigate to a page represented by another PageObject
        return this;
    }

    // The login page allows the user to type their password into the password field
    public LoginPage typePassword(String password) {
        // This is the only place that "knows" how to enter a password
        driver.findElement(passwordLocator).sendKeys(password);

        // Return the current page object as this action doesn't navigate to a page represented by another PageObject
        return this;
    }

    // The login page allows the user to submit the login form
    public HomePage submitLogin() {
        // This is the only place that submits the login form and expects the destination to be the home page.
        // A seperate method should be created for the instance of clicking login whilst expecting a login failure.
        driver.findElement(loginButtonLocator).submit();

        // Return a new page object representing the destination. Should the login page ever
        // go somewhere else (for example, a legal disclaimer) then changing the method signature
        // for this method will mean that all tests that rely on this behaviour won't compile.
        return new HomePage(driver);
    }

    // The login page allows the user to submit the login form knowing that an invalid username and / or password were entered
    public LoginPage submitLoginExpectingFailure() {
        // This is the only place that submits the login form and expects the destination to be the login page due to login failure.
        driver.findElement(loginButtonLocator).submit();

        // Return a new page object representing the destination. Should the user ever be navigated to the home page after submiting a login with credentials
        // expected to fail login, the script will fail when it attempts to instantiate the LoginPage PageObject.
        return new LoginPage(driver);
    }

    // Conceptually, the login page offers the user the service of being able to "log into"
    // the application using a user name and password.
    public HomePage loginAs(String username, String password) {
        // The PageObject methods that enter username, password & submit login have already defined and should not be repeated here.
        typeUsername(username);
        typePassword(password);
        return submitLogin();
    }
}
</code>```

答:
Finely chopping your code into more methods allows for greater atomization. Nothing stops you from grouping the typeUsername(), typePassword() and submitLogin() into login() method.

The upside of doing so is knowing more precisely that your login failed at, say, typing password as opposed to "somewhere on the login page".

Body:
I am creating a UI test automation framework using POM design pattern. After reading SeleniumHQ page for Page Objects, I am contemplating what all methods should I create inside a Page Object.

Let's take a simple example of login page object consisting of username, password textboxes and submit button. SeleniumHQ link has created the below methods :

1. typeUsername(String username)
2. typePassword(String password)
3. submitLogin()
4. submitLoginExceptionFailure()
5. loginAs(String username, String password)

I am a bit perplexed while looking at these methods. Why should I create first 3 methods(typeUsername, typePassword,submitLogin), when I already am creating a loginAs method. Any thoughts around it ?

SeleniumHQ Link - https://github.com/SeleniumHQ/selenium/wiki/PageObjects

Pasting the code PageObject code for LoginPage :

public class LoginPage {
    private final WebDriver driver;

    public LoginPage(WebDriver driver) {
        this.driver = driver;

        // Check that we're on the right page.
        if (!"Login".equals(driver.getTitle())) {
            // Alternatively, we could navigate to the login page, perhaps logging out first
            throw new IllegalStateException("This is not the login page");
        }
    }

    // The login page contains several HTML elements that will be represented as WebElements.
    // The locators for these elements should only be defined once.
        By usernameLocator = By.id("username");
        By passwordLocator = By.id("passwd");
        By loginButtonLocator = By.id("login");

    // The login page allows the user to type their username into the username field
    public LoginPage typeUsername(String username) {
        // This is the only place that "knows" how to enter a username
        driver.findElement(usernameLocator).sendKeys(username);

        // Return the current page object as this action doesn't navigate to a page represented by another PageObject
        return this;
    }

    // The login page allows the user to type their password into the password field
    public LoginPage typePassword(String password) {
        // This is the only place that "knows" how to enter a password
        driver.findElement(passwordLocator).sendKeys(password);

        // Return the current page object as this action doesn't navigate to a page represented by another PageObject
        return this;
    }

    // The login page allows the user to submit the login form
    public HomePage submitLogin() {
        // This is the only place that submits the login form and expects the destination to be the home page.
        // A seperate method should be created for the instance of clicking login whilst expecting a login failure.
        driver.findElement(loginButtonLocator).submit();

        // Return a new page object representing the destination. Should the login page ever
        // go somewhere else (for example, a legal disclaimer) then changing the method signature
        // for this method will mean that all tests that rely on this behaviour won't compile.
        return new HomePage(driver);
    }

    // The login page allows the user to submit the login form knowing that an invalid username and / or password were entered
    public LoginPage submitLoginExpectingFailure() {
        // This is the only place that submits the login form and expects the destination to be the login page due to login failure.
        driver.findElement(loginButtonLocator).submit();

        // Return a new page object representing the destination. Should the user ever be navigated to the home page after submiting a login with credentials
        // expected to fail login, the script will fail when it attempts to instantiate the LoginPage PageObject.
        return new LoginPage(driver);
    }

    // Conceptually, the login page offers the user the service of being able to "log into"
    // the application using a user name and password.
    public HomePage loginAs(String username, String password) {
        // The PageObject methods that enter username, password & submit login have already defined and should not be repeated here.
        typeUsername(username);
        typePassword(password);
        return submitLogin();
    }
}


来源: stackexchange

问:
Extract value in Python
My Code:

```<code>import requests
import json

web_page = requests.get("http://api.bart.gov/api/etd.aspx?cmd=etd&orig=mont&key=MW9S-E7SL-26DU-VV8V&json=y")
response = web_page.text
parsed_json = json.loads(response)
#print(parsed_json)

print(parsed_json['root']['date'])
print(parsed_json['root']['time'])
print(parsed_json['root']['station']['name'])

</code>```

How to extract value of destination and minutes from below in Python.

```<code>[{'name': 'Montgomery St.', 'abbr': 'MONT', 'etd': [{'destination': 'Daly City', 'abbreviation': 'DALY', 'limited': '0', 'estimate': [{'minutes': '39', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'WHITE', 'hexcolor': '#ffffff', 'bikeflag': '1', 'delay': '220'}]}, {'destination': 'SF Airport', 'abbreviation': 'SFIA', 'limited': '0', 'estimate': [{'minutes': '16', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'YELLOW', 'hexcolor': '#ffff33', 'bikeflag': '1', 'delay': '132'}, {'minutes': '26', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'BLUE', 'hexcolor': '#0099cc', 'bikeflag': '1', 'delay': '69'}]}]}]

</code>```

答:
Try this:

```<code>json_obj = {'name': 'Montgomery St.', 'abbr': 'MONT', 'etd': [{'destination': 'Antioch', 'abbreviation': 'ANTC', 'limited': '0', 'estimate': [{'minutes': '1', 'platform': '2', 'direction': 'North', 'length': '10', 'color': 'YELLOW', 'hexcolor': '#ffff33', 'bikeflag': '1', 'delay': '254'}]},

{'destination': 'Daly City', 'abbreviation': 'DALY', 'limited': '0', 'estimate': [{'minutes': '39', 'platform': '1', 'direction': 'South', 'length': '0', 'color': 'BLUE', 'hexcolor': '#0099cc', 'bikeflag': '1', 'delay': '0'}]},

{'destination': 'SF Airport', 'abbreviation': 'SFIA', 'limited': '0', 'estimate': [{'minutes': '38', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'YELLOW', 'hexcolor': '#ffff33', 'bikeflag': '1', 'delay': '0'}]}]}

for item in json_obj['etd']:
    dest = item['destination']
    minute = item['estimate'][0]['minutes']
    print(dest, minute)
</code>```

Output:

```<code>Antioch 1
Daly City 39
SF Airport 38
</code>```

ContentLicense: CC BY-SA 4.0

Title: Extract value in Python

Body:
My Code:

import requests
import json

web_page = requests.get("http://api.bart.gov/api/etd.aspx?cmd=etd&orig=mont&key=MW9S-E7SL-26DU-VV8V&json=y")
response = web_page.text
parsed_json = json.loads(response)
#print(parsed_json)

print(parsed_json['root']['date'])
print(parsed_json['root']['time'])
print(parsed_json['root']['station']['name'])

How to extract value of destination and minutes from below in Python.

[{'name': 'Montgomery St.', 'abbr': 'MONT', 'etd': [{'destination': 'Daly City', 'abbreviation': 'DALY', 'limited': '0', 'estimate': [{'minutes': '39', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'WHITE', 'hexcolor': '#ffffff', 'bikeflag': '1', 'delay': '220'}]}, {'destination': 'SF Airport', 'abbreviation': 'SFIA', 'limited': '0', 'estimate': [{'minutes': '16', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'YELLOW', 'hexcolor': '#ffff33', 'bikeflag': '1', 'delay': '132'}, {'minutes': '26', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'BLUE', 'hexcolor': '#0099cc', 'bikeflag': '1', 'delay': '69'}]}]}]

Body:
Try this:

json_obj = {'name': 'Montgomery St.', 'abbr': 'MONT', 'etd': [{'destination': 'Antioch', 'abbreviation': 'ANTC', 'limited': '0', 'estimate': [{'minutes': '1', 'platform': '2', 'direction': 'North', 'length': '10', 'color': 'YELLOW', 'hexcolor': '#ffff33', 'bikeflag': '1', 'delay': '254'}]},

{'destination': 'Daly City', 'abbreviation': 'DALY', 'limited': '0', 'estimate': [{'minutes': '39', 'platform': '1', 'direction': 'South', 'length': '0', 'color': 'BLUE', 'hexcolor': '#0099cc', 'bikeflag': '1', 'delay': '0'}]},

{'destination': 'SF Airport', 'abbreviation': 'SFIA', 'limited': '0', 'estimate': [{'minutes': '38', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'YELLOW', 'hexcolor': '#ffff33', 'bikeflag': '1', 'delay': '0'}]}]}

for item in json_obj['etd']:
    dest = item['destination']
    minute = item['estimate'][0]['minutes']
    print(dest, minute)

Output:

Antioch 1
Daly City 39
SF Airport 38


问:
How to read DICOM private tags without reading/loading pixel data?
I would like to read DICOM private tags. These private tags are under hexadecimal tag x7fe11001.

I know one of the pydicom configurations that read till pixel data starts (so the memory is not loaded up).

```<code>pydicom.dcmread(raw, defer_size="2 MB", stop_before_pixels=True)
</code>```

But the private tags I am trying to read are after the pixel data. So I am ending loading complete file in memory which is not optimal. What are the other ways to read it in an optimal way?

I know there is a config param for the above method, called specific_tags. But I could not find any examples of how to use it.

Any suggestions to read DICOM metadata without loading pixel data into memory would be awesome.

答:
You are right, specific_tags is the correct way to do this:

```<code>ds = pydicom.dcmread(raw, specific_tags=[Tag(0x7fe1, 0x1001)]
</code>```

In this case, ds shall contain only your private tag and the Specific Character Set tag (which is always read).

As DICOM is a sequential format, the other tags still have to be skipped over one by one, but their value is not read.

Note that you can put any number of tags into the specific_tags argument.

Body:
I would like to read DICOM private tags. These private tags are under hexadecimal tag x7fe11001.

I know one of the pydicom configurations that read till pixel data starts (so the memory is not loaded up).

pydicom.dcmread(raw, defer_size="2 MB", stop_before_pixels=True)

But the private tags I am trying to read are after the pixel data. So I am ending loading complete file in memory which is not optimal. What are the other ways to read it in an optimal way?

I know there is a config param for the above method, called specific_tags. But I could not find any examples of how to use it.

Any suggestions to read DICOM metadata without loading pixel data into memory would be awesome.

Body:
You are right, specific_tags is the correct way to do this:

ds = pydicom.dcmread(raw, specific_tags=[Tag(0x7fe1, 0x1001)]

In this case, ds shall contain only your private tag and the Specific Character Set tag (which is always read).

As DICOM is a sequential format, the other tags still have to be skipped over one by one, but their value is not read.

Note that you can put any number of tags into the specific_tags argument.


问:
Change many values with specific
I have data like this:

```<code>data.frame(text1 = c("Amazon", "Google", "other"))
</code>```

Add I would like to replace whatever is in the list with a specific value

```<code>mylist <- c("Amazon", "Google")
</code>```

replace what is in the mylist with stock value

Expected output:

```<code>data.frame(text1 = c("stock", "stock", "other"))
</code>```

答:
Another base R solution using gsub

```<code>df$text1<- gsub(paste0(paste0("\\b",mylist,"\\b"),collapse = "|"),"stock",df$text1)
</code>```

such that

```<code>> df
  text1
1 stock
2 stock
3 other
</code>```

DATA

```<code>df <- data.frame(text1 = c("Amazon", "Google", "other"))

mylist <- c("Amazon", "Google")
</code>```

Title: Change many values with specific

Body:
I have data like this:

data.frame(text1 = c("Amazon", "Google", "other"))

Add I would like to replace whatever is in the list with a specific value

mylist <- c("Amazon", "Google")

replace what is in the mylist with stock value

Expected output:

data.frame(text1 = c("stock", "stock", "other"))

Body:
Another base R solution using gsub

df$text1<- gsub(paste0(paste0("\\b",mylist,"\\b"),collapse = "|"),"stock",df$text1)

such that

> df
  text1
1 stock
2 stock
3 other

DATA

df <- data.frame(text1 = c("Amazon", "Google", "other"))

mylist <- c("Amazon", "Google")


问:
Creating and pushing elements in an new array depends on conditions
My Data Array

```<code>data:[
0:{
   id:1 ,.....
   competetion:[
          0:{
              id: 1....,
              match:[
                      0:{id: 1 ......},
                      1:{same}
                      .
                      .
                    ]
          1:{same}]},
          2:{same}
          .
          .
        ]
  },
1:{same},
2:{same}
.
.
.
]

</code>```

For data[] i able to create a new array(sportarr[]) with pushing elements but i want to create for the same as competetion[] and match[] in the same array sportarr[]
If there any other way to do it please Help me...

My Code: Here i am looping it:

```<code>this.sportsSidebar = response.data;   // My data Array (this.sportsSidebar)
const arrlength = response.data.length;
for (let i = 0; i < arrlength; i++) {
    this.sportarr.push({       // I declared a new array where i want to push the element
      id: i,
      value: false
    });
}
</code>```

答:
i am not sure if you are trying to create an array for each nasted array (competetion and match) or not, but here you go</p>

```<code>this.sportsSidebar = response.data;   // My data Array (this.sportsSidebar)

const competetionList = // Array of competetions
    [].concat.apply([], this.sportsSidebar.map(item => item.competetion));

const matchList = // Array of matchs
    [].concat.apply([], competetionList .map(item => item.match));
}
</code>```

Body:
My Data Array

data:[
0:{
   id:1 ,.....
   competetion:[
          0:{
              id: 1....,
              match:[
                      0:{id: 1 ......},
                      1:{same}
                      .
                      .
                    ]
          1:{same}]},
          2:{same}
          .
          .
        ]
  },
1:{same},
2:{same}
.
.
.
]

For data[] i able to create a new array(sportarr[]) with pushing elements but i want to create for the same as competetion[] and match[] in the same array sportarr[]
If there any other way to do it please Help me...

My Code: Here i am looping it:

this.sportsSidebar = response.data;   // My data Array (this.sportsSidebar)
const arrlength = response.data.length;
for (let i = 0; i < arrlength; i++) {
    this.sportarr.push({       // I declared a new array where i want to push the element
      id: i,
      value: false
    });
}

Body:
i am not sure if you are trying to create an array for each nasted array (competetion and match) or not, but here you go

this.sportsSidebar = response.data;   // My data Array (this.sportsSidebar)

const competetionList = // Array of competetions
    [].concat.apply([], this.sportsSidebar.map(item => item.competetion));

const matchList = // Array of matchs
    [].concat.apply([], competetionList .map(item => item.match));
}


问:
Batch script to dynamically pass inputs to a .exe and run recursively
I am trying to run a batch script where it will be fetching Values from SQL Server table result set and pass that as an input in a loop to an .EXE in the batch script and run recursively.

Example:

```<code>SQL Query:
select Name from sys.tables

SQL Query output:
a
b
c

/*Now I have to pass the output of sql query values(a,b,c) I got above as an
input to Test.exe parameter 3 times in a for or while loop specific to batch
script one value at a time and executes the exe.

Example of values passed to exe looks as below.

Test.exe a  -- 1st run in a loop
Test.exe b  -- 2nd run in a loop
Test.exe c  -- 3rd run in a loop

-- My code overview or assumption, I would need the below code snippet to be replaced
and rewritten in a way it executes successfully using batch script.*/

declare @name int
set @name = count(Name) from sys.tables

DECLARE @Counter INT

SET @Counter=1

WHILE ( @Counter <= @name)

BEGIN

    C:\Users> .\Test.exe /Input_From_Sql_result_as_argument @Name

    SET @Counter  = @Counter  + 1
END
</code>```

I am new to batch scripting, if you can shed some ideas on implementing this in batch script it would be really helpful. Please let me know if I have to share any details..

答:
The content of each line can be Set to an indexed variable, much like an array, with the aid of Setlocal EnableDelayedExpansion and a basic For /F loop

Depending on the content / structure of your file/s you may not need to bother with the Array approach - Just perform your action using the For /F loop Variable/s (%%G in this example).

Again, content dependent, you may want to modify the options in the for loop to retrieve specific tokens. (type For /F in cmd.exe for full syntax - or search and read through the numerous other questions on SO regarding processing file content)

Once you've established the array, you can use a For /L loop to iterate through each line and insert the commands to be enacted using the value of each line.

If, by running recursively you mean to run this on each file in folder and subfolders (the typical meaning of the term recursively where batch is concerned, Nest the For /F loop inside a For /R Loop, and exchange %inputfile% with %%A in the For /F loop like so:

CD (Change Directory) / PUSHD to the parent folder to be iterated over / or / Ensure the batch is run from the parent folder

For /R %%A In (*.YOURextension) Do (For /F "delims=" %%G in (%%A) Do (commands))

``

```<code>@ECHO OFF
Setlocal EnableDelayedExpansion
Set index=0
Set "inputFile=Your File Path.extension"
For /f "delims=" %%G in (%inputfile%) DO (
REM Perform your action on the content here using %%G
    ECHO(%%G
REM Optional - assign value of each line to indexed variable
    Set /a "index+=1"
    Set "Line[!index!]=%%G"
)

For /L %%I in (1,1,!index!) DO (
REM - Alternate - Perfom your action on the content here using !Line[%%I]!
    ECHO(!Line[%%I]!
)
</code>```

Body:
I am trying to run a batch script where it will be fetching Values from SQL Server table result set and pass that as an input in a loop to an .EXE in the batch script and run recursively.

Example:

SQL Query:
select Name from sys.tables

SQL Query output:
a
b
c

/*Now I have to pass the output of sql query values(a,b,c) I got above as an
input to Test.exe parameter 3 times in a for or while loop specific to batch
script one value at a time and executes the exe.

Example of values passed to exe looks as below.

Test.exe a  -- 1st run in a loop
Test.exe b  -- 2nd run in a loop
Test.exe c  -- 3rd run in a loop

-- My code overview or assumption, I would need the below code snippet to be replaced
and rewritten in a way it executes successfully using batch script.*/

declare @name int
set @name = count(Name) from sys.tables

DECLARE @Counter INT

SET @Counter=1

WHILE ( @Counter <= @name)

BEGIN

    C:\Users> .\Test.exe /Input_From_Sql_result_as_argument @Name

    SET @Counter  = @Counter  + 1
END

I am new to batch scripting, if you can shed some ideas on implementing this in batch script it would be really helpful. Please let me know if I have to share any details..

Body:
The content of each line can be Set to an indexed variable, much like an array, with the aid of Setlocal EnableDelayedExpansion and a basic For /F loop

Depending on the content / structure of your file/s you may not need to bother with the Array approach - Just perform your action using the For /F loop Variable/s (%%G in this example).

Again, content dependent, you may want to modify the options in the for loop to retrieve specific tokens. (type For /F in cmd.exe for full syntax - or search and read through the numerous other questions on SO regarding processing file content)

Once you've established the array, you can use a For /L loop to iterate through each line and insert the commands to be enacted using the value of each line.

If, by running recursively you mean to run this on each file in folder and subfolders (the typical meaning of the term recursively where batch is concerned, Nest the For /F loop inside a For /R Loop, and exchange %inputfile% with %%A in the For /F loop like so:

CD (Change Directory) / PUSHD to the parent folder to be iterated over / or / Ensure the batch is run from the parent folder

For /R %%A In (*.YOURextension) Do (For /F "delims=" %%G in (%%A) Do (commands))

``

@ECHO OFF
Setlocal EnableDelayedExpansion
Set index=0
Set "inputFile=Your File Path.extension"
For /f "delims=" %%G in (%inputfile%) DO (
REM Perform your action on the content here using %%G
    ECHO(%%G
REM Optional - assign value of each line to indexed variable
    Set /a "index+=1"
    Set "Line[!index!]=%%G"
)

For /L %%I in (1,1,!index!) DO (
REM - Alternate - Perfom your action on the content here using !Line[%%I]!
    ECHO(!Line[%%I]!
)


问:
Capitalise first letter in each string of an array and remove '_' character
So I have an array which looks something like:

```<code>["image", "image_link", "images_carousel", "images_collection", "video", "video_carousel", "video_collection", "video_link"]
</code>```

I need to capitalise the first letter of each word and remove the "_" between some words.
I've managed to do it for a string but I can't get it for this array. some help? Thanks.

答:
yourArray.map(word => `${word.charAt[0].toUpperCase()}${word.slice(1)}`.replace(/_/g, " "));
```

Body:
So I have an array which looks something like:

["image", "image_link", "images_carousel", "images_collection", "video", "video_carousel", "video_collection", "video_link"]

I need to capitalise the first letter of each word and remove the "_" between some words.
I've managed to do it for a string but I can't get it for this array. some help? Thanks.


问:
Code to find first occurence and return 0 if no occurence and then append new column onto csv (python)
I am trying to extract values inside a column in a csv. Here is my code so far:

```<code>```code```
import csv
with open('Cash_Statement_Pier21 accounts 2019c.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
    for line in row:
        index = line.find('@')
        if index != -1:
            print(line[index+2:index+7])
        else:
            index = line.find('RATE')
            if index != -1:
                print(line[index +2])
</code>```

However, this code fails for lines with multiple "@"s and it only prints out the list in python. How
would I append this code such that it a) only takes into account the first "@" on every line and b)
creates a new column in my csv?

Thanks so much in advance!

EDIT SAMPLE INPUT (  seperated lines for readability)
NOVO NORDISK A/S    216757.000 SHS @ 5.15000000 EXDTE-22MAR19 PAYDTE-26MAR19 TAXABLE RECLAIMABLE TAXED AT .270000%

NOVO NORDISK A/S    205395.000 SHS @ 3.00000000 EXDTE-16AUG19 PAYDTE-20AUG19 TAXABLE RECLAIMABLE TAXED AT .270000%

NOVO NORDISK A/S    TAX RECLAIM PAID 79000.000 EX-DT:20MAR15 PY-DT:24MAR15 CURRENCY TIP # 1150790014348

PING AN INSURANCE GROUP CO OF HK/02318   1184000.000 SHS @ 1.28414399EXDTE-23MAY19 PAYDTE-28JUN19 TAXABLE AT ISSUE RATE .100000%

```<code>BANK CENTRAL ASIA TBK PT    3314200.000 SHS @ 100.00000000 EXDTE-06DEC19 PAYDTE-20DEC19 TAXABLE AT ISSUE RATE .200000%
</code>```

答:
There are 2 different problems here.

How to only process first @ or each line?

I will assume that despite what you have shown, you are actually processing a true csv file with more than one field per row and only want to process the first field containing a @ caracter. If you do not want to search for RATE if you have found @, it is trivial: just add a break statement:

```<code>for row in csv_reader:
    for line in row:
        index = line.find('@')
        if index != -1:
            print(line[index+2:index+7])
            break                         // ignore any other field in that row
        else:
            ...
</code>```

If you want to process the first @ and the first RATE on each row, you will have to use a boolean to ignore any @ but the first, but still continue up to a RATE:

```<code>for row in csv_reader:
    first = True
    for line in row:
        if first:
            index = line.find('@')
            if index != -1:
                print(line[index+2:index+7])
                first = False                         // ignore any other field in that row
       else:
            ...
</code>```

How to add a new field to a CSV?

A CSV is a text file. The only reliable way to change it is to rewrite everything on a temporary file and when done remove the old file (or rename it as a backup) and rename the temporary to the original name. More or less (only showing high level):

```<code>ok = False
with open('Cash_Statement_Pier21 accounts 2019c.csv') as csv_file:
    with open('Cash_Statement_Pier21 accounts 2019c.tmp', 'w',', newline='') as out_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        csv_writer = csv.writer(out_file, delimiter=')
        for row in csv_reader:
            ...
            row.append(new_column_value)
            csv_writer.writerow(row)
    ok = True
if ok:     // do not rename if an error occured...
    os.remove('Cash_Statement_Pier21 accounts 2019c.csv')
    os.rename('Cash_Statement_Pier21 accounts 2019c.tmp',
              'Cash_Statement_Pier21 accounts 2019c.csv)'
</code>```

We have to wait after the end of the with block to execute the rename, because some OS may refuse to remove or rename an open file.

Body:
I am trying to extract values inside a column in a csv. Here is my code so far:

```code```
import csv
with open('Cash_Statement_Pier21 accounts 2019c.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
    for line in row:
        index = line.find('@')
        if index != -1:
            print(line[index+2:index+7])
        else:
            index = line.find('RATE')
            if index != -1:
                print(line[index +2])

However, this code fails for lines with multiple "@"s and it only prints out the list in python. How
would I append this code such that it a) only takes into account the first "@" on every line and b)
creates a new column in my csv?

Thanks so much in advance!

EDIT SAMPLE INPUT (  seperated lines for readability)
NOVO NORDISK A/S    216757.000 SHS @ 5.15000000 EXDTE-22MAR19 PAYDTE-26MAR19 TAXABLE RECLAIMABLE TAXED AT .270000%

NOVO NORDISK A/S    205395.000 SHS @ 3.00000000 EXDTE-16AUG19 PAYDTE-20AUG19 TAXABLE RECLAIMABLE TAXED AT .270000%

NOVO NORDISK A/S    TAX RECLAIM PAID 79000.000 EX-DT:20MAR15 PY-DT:24MAR15 CURRENCY TIP # 1150790014348

PING AN INSURANCE GROUP CO OF HK/02318   1184000.000 SHS @ 1.28414399EXDTE-23MAY19 PAYDTE-28JUN19 TAXABLE AT ISSUE RATE .100000%

BANK CENTRAL ASIA TBK PT    3314200.000 SHS @ 100.00000000 EXDTE-06DEC19 PAYDTE-20DEC19 TAXABLE AT ISSUE RATE .200000%

Body:
There are 2 different problems here.

How to only process first @ or each line?

I will assume that despite what you have shown, you are actually processing a true csv file with more than one field per row and only want to process the first field containing a @ caracter. If you do not want to search for RATE if you have found @, it is trivial: just add a break statement:

for row in csv_reader:
    for line in row:
        index = line.find('@')
        if index != -1:
            print(line[index+2:index+7])
            break                         // ignore any other field in that row
        else:
            ...

If you want to process the first @ and the first RATE on each row, you will have to use a boolean to ignore any @ but the first, but still continue up to a RATE:

for row in csv_reader:
    first = True
    for line in row:
        if first:
            index = line.find('@')
            if index != -1:
                print(line[index+2:index+7])
                first = False                         // ignore any other field in that row
       else:
            ...

How to add a new field to a CSV?

A CSV is a text file. The only reliable way to change it is to rewrite everything on a temporary file and when done remove the old file (or rename it as a backup) and rename the temporary to the original name. More or less (only showing high level):

ok = False
with open('Cash_Statement_Pier21 accounts 2019c.csv') as csv_file:
    with open('Cash_Statement_Pier21 accounts 2019c.tmp', 'w',', newline='') as out_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        csv_writer = csv.writer(out_file, delimiter=')
        for row in csv_reader:
            ...
            row.append(new_column_value)
            csv_writer.writerow(row)
    ok = True
if ok:     // do not rename if an error occured...
    os.remove('Cash_Statement_Pier21 accounts 2019c.csv')
    os.rename('Cash_Statement_Pier21 accounts 2019c.tmp',
              'Cash_Statement_Pier21 accounts 2019c.csv)'

We have to wait after the end of the with block to execute the rename, because some OS may refuse to remove or rename an open file.


问:
How to parse json efficiently in Snowpipe with ON_ERROR=CONTINUE
I'm setting up a snowpipe to load data from s3 bucket to snowflake schema.
S3 contains files in NDJOSN format. One file can contain multiple records and I want to process all of them. Even if one record is broken.

To do so, I need to add on_error='continue' option to pipe creation and use csv file format as stated in official snowflake docs here.

That way I receive raw strings of JSON that I need to parse to access data. And since snowpipes do not support nested selects the only way to do that is to parse it for each column individually.

Resulting in this copy statement:

```<code>copy into MY_TABLE
from (select parse_json($1):id, parse_json($1):name, parse_json($1):status
from @MY_STAGE_CONNECTED_TO_S3)
on_error = 'continue'
</code>```

This code needs to parse json 3 times for each row.

I have a table with ~40 col's and for it, this query works ~5 times slower than a more straightforward solution that uses file format option to parse JSON but unfortunately does not support on_error=continue option.

```<code>copy into HQO_DEVELOPMENT.PUBLIC.DIM_TENANT
from (select $1:id, $1:name, $1:status
from @HQO_DEVELOPMENT.PUBLIC.DIM_TENANT_STAGE_NN)
file_format = (type = 'json')
</code>```

What was tried

Using nested select like this: : is not supported

```<code>copy into HQO_DEVELOPMENT.PUBLIC.DIM_TENANT from
(select $1:id, $1:name, $1:status from (
select parse_json($1) from
@HQO_DEVELOPMENT.PUBLIC.DIM_TENANT_STAGE))
 on_error = 'continue'
</code>```

Using JSON type on stage and omit on the pipe: won't help the issue

Is there a way to have the benefit of on_error=continue and don't parse JSON for every column?

答:
Snowflake documentation says: Both CSV and semi-structured file types are supported; however, even when loading semi-structured data (e.g. JSON), you should set CSV as the file format type (default value). You can use the corresponding file format (e.g. JSON), but any error in the transformation will stop the COPY operation, even if you set the ON_ERROR option to continue or skip the file.

https://docs.snowflake.net/manuals/sql-reference/sql/copy-into-table.html

On the other hand, I see on_error option works for NDJSON files, at least when you set file type on stage level. For testing,

I created the following NDJSON file for testing:

```<code>{ "id":1, "name":"Gokhan", "location":"Netherlands" }
{ "id":2, "name":"Hans", location:Germany -- broken json #1 }
{ "id":3, "name":"Joe", "location":"UK" }
{ broken json #2 }
{ "id":4, "name":"Mike", "location":"US" }
</code>```

I created a file type object, and created a stage using this file type (you can alter your existing stage and set a file type):

```<code>CREATE FILE FORMAT myformat TYPE = json;
CREATE STAGE mystage FILE_FORMAT = (FORMAT_NAME = myformat);
</code>```

I uploaded my sample NDJSON file to this stage, created a snowpipe to load it:

```<code>CREATE PIPE mypipe AS
COPY INTO mytable
FROM (SELECT $1:id, $1:name, $1:location FROM @mystage)
ON_ERROR = CONTINUE;
</code>```

When I refreshed the pipe, it successfully loaded the "valid" (3) records from the file.

Body:
I'm setting up a snowpipe to load data from s3 bucket to snowflake schema.
S3 contains files in NDJOSN format. One file can contain multiple records and I want to process all of them. Even if one record is broken.

To do so, I need to add on_error='continue' option to pipe creation and use csv file format as stated in official snowflake docs here.

That way I receive raw strings of JSON that I need to parse to access data. And since snowpipes do not support nested selects the only way to do that is to parse it for each column individually.

Resulting in this copy statement:

copy into MY_TABLE
from (select parse_json($1):id, parse_json($1):name, parse_json($1):status
from @MY_STAGE_CONNECTED_TO_S3)
on_error = 'continue'

This code needs to parse json 3 times for each row.

I have a table with ~40 col's and for it, this query works ~5 times slower than a more straightforward solution that uses file format option to parse JSON but unfortunately does not support on_error=continue option.

copy into HQO_DEVELOPMENT.PUBLIC.DIM_TENANT
from (select $1:id, $1:name, $1:status
from @HQO_DEVELOPMENT.PUBLIC.DIM_TENANT_STAGE_NN)
file_format = (type = 'json')

What was tried

Using nested select like this: : is not supported

copy into HQO_DEVELOPMENT.PUBLIC.DIM_TENANT from
(select $1:id, $1:name, $1:status from (
select parse_json($1) from
@HQO_DEVELOPMENT.PUBLIC.DIM_TENANT_STAGE))
 on_error = 'continue'

Using JSON type on stage and omit on the pipe: won't help the issue

Is there a way to have the benefit of on_error=continue and don't parse JSON for every column?

Body:
Snowflake documentation says: Both CSV and semi-structured file types are supported; however, even when loading semi-structured data (e.g. JSON), you should set CSV as the file format type (default value). You can use the corresponding file format (e.g. JSON), but any error in the transformation will stop the COPY operation, even if you set the ON_ERROR option to continue or skip the file.

https://docs.snowflake.net/manuals/sql-reference/sql/copy-into-table.html

On the other hand, I see on_error option works for NDJSON files, at least when you set file type on stage level. For testing,

I created the following NDJSON file for testing:

{ "id":1, "name":"Gokhan", "location":"Netherlands" }
{ "id":2, "name":"Hans", location:Germany -- broken json #1 }
{ "id":3, "name":"Joe", "location":"UK" }
{ broken json #2 }
{ "id":4, "name":"Mike", "location":"US" }

I created a file type object, and created a stage using this file type (you can alter your existing stage and set a file type):

CREATE FILE FORMAT myformat TYPE = json;
CREATE STAGE mystage FILE_FORMAT = (FORMAT_NAME = myformat);

I uploaded my sample NDJSON file to this stage, created a snowpipe to load it:

CREATE PIPE mypipe AS
COPY INTO mytable
FROM (SELECT $1:id, $1:name, $1:location FROM @mystage)
ON_ERROR = CONTINUE;

When I refreshed the pipe, it successfully loaded the "valid" (3) records from the file.


问:
How to select and copy files with specific file names from one directory to another?
I have a directory with two different types of filenames daily/monthly:

```<code> report_20-10-2019.csv
 report_21-10-2019.csv
 report_22-10-2019.csv

 report_09-2019.csv
 report_10-2019.csv
 report_11-2019.csv
</code>```

I'm trying to copy only daily files to another directory. So far I'm able to copy all the files with the below code:

```<code>import shutil
import os
import glob

source_daily = '/path/to/files/to/copy/*.csv'
dest1 = 'path/to/directory/where/i/paste/my/files/'

files = os.listdir(source)

for file in glob.glob(source):
shutil.copy(file, dest1);
</code>```

Would someone be able to help with this? Thanks in advance!

答:
Using Regular Expressions should work for you:</p>

```<code>import re

import shutil
import os
import glob

source_daily = '/path/to/files/to/copy/*.csv'
dest1 = 'path/to/directory/where/i/paste/my/files/'

files = os.listdir(source)

pattern = re.compile('\w+_[0-3]\d-[0-1]\d-\d{4}.csv') #naming-pattern

for file in glob.glob(source):
    if pattern.match(file):
        shutil.copy(file, dest1);
</code>```

Body:
I have a directory with two different types of filenames daily/monthly:

 report_20-10-2019.csv
 report_21-10-2019.csv
 report_22-10-2019.csv

 report_09-2019.csv
 report_10-2019.csv
 report_11-2019.csv

I'm trying to copy only daily files to another directory. So far I'm able to copy all the files with the below code:

import shutil
import os
import glob

source_daily = '/path/to/files/to/copy/*.csv'
dest1 = 'path/to/directory/where/i/paste/my/files/'

files = os.listdir(source)

for file in glob.glob(source):
shutil.copy(file, dest1);

Would someone be able to help with this? Thanks in advance!

Body:
Using Regular Expressions should work for you:

import re

import shutil
import os
import glob

source_daily = '/path/to/files/to/copy/*.csv'
dest1 = 'path/to/directory/where/i/paste/my/files/'

files = os.listdir(source)

pattern = re.compile('\w+_[0-3]\d-[0-1]\d-\d{4}.csv') #naming-pattern

for file in glob.glob(source):
    if pattern.match(file):
        shutil.copy(file, dest1);


问:
Google Slides API returning a 500 server error
Using the Javascript client, I'm submitting a large number of requests to batchUpdate(). The code below was working perfectly until a few hours ago. I've tried reducing the number of requests as suggested here but that seems to make no difference. See below for the code I'm using, it's pretty simple. The template being copied is open so it should run assuming you put a client_id and api key in there.

```<code><style>
  #loadingSpinner {display:none;};
</style>
<!--Add buttons to initiate auth sequence and sign out-->
<button id="authorize_button" style="display: none;" class="button">Authorize Google Slides</button>
<button id="create_button" onclick="copyAndMergePresso()" class="button" style="display: none;">Build Google Slides</button>

<pre id="content" style="white-space: pre-wrap;font-family:georgia;font-size:1.1em;">```

```
<button id="reload_button" onclick="reloadPage()" class="button alert" style="display: none;">Reload Page</button>

<img src="/images/ajax-loaders/small-black-circle.gif" id="loadingSpinner">

</code>```

I have also changed the code so the requests array is the bare minimum:

```<code>var requests = [{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{ta-title}}"},"replaceText":"Sheep Farming"}},{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{ta-date}}"},"replaceText":"14 February 2020"}},{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{ta-creator}}"},"replaceText":"Edward Chanter"}},{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{ta-creator-org}}"},"replaceText":"Collaborative Connections Ltd."}},{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{search_query}}"},"replaceText":"sheep OR mutton OR lamb"}},{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{33-text}}"},"replaceText":"Future vantage points on emerging commercial changes"}},{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{total_forecasts}}"},"replaceText":"176 forecasts"}},{"replaceAllShapesWithImage":{"containsText":{"matchCase":true,"text":"{{ta-image}}"},"replaceMethod":"CENTER_CROP","imageUrl":"https://pixabay.com/get/57e8d7414b51ab14f6da8c7dda79367c153cdde65a506c4870277ad3974dc45ab8_1280.jpg"}}];
</code>```

Nothing seems to work and I can't figure out why. The response I'm getting from the Google Slides API is this:

{
  "error": {
    "code": 500,
    "message": "Internal error encountered.",
    "status": "INTERNAL"
  }
}

As I said before, this was working all of last week and this morning until I added some additional requests. Taking the stuff I added out hasn't made a difference.

Update It isn't working consistently, I've been testing with another user and some reports work whilst others fail. Some fail for me and not him and others fail for him and not me. I'm submitting over 200 requests so I'm starting to think this is related to a quota issue but I don't understand how the quotas work.

答: Following a dialog with Google, it turns out the error was caused by a broken image URL. I was using the direct pixabay URL. Changing that to download the image and serve it from my system. solved the problem. </p>

Body:
Using the Javascript client, I'm submitting a large number of requests to batchUpdate(). The code below was working perfectly until a few hours ago. I've tried reducing the number of requests as suggested here but that seems to make no difference. See below for the code I'm using, it's pretty simple. The template being copied is open so it should run assuming you put a client_id and api key in there.

<button id="authorize_button" style="display: none;" class="button">Authorize Google Slides</button>
<button id="create_button" onclick="copyAndMergePresso()" class="button" style="display: none;">Build Google Slides</button>

<button id="reload_button" onclick="reloadPage()" class="button alert" style="display: none;">Reload Page</button>

I have also changed the code so the requests array is the bare minimum:

var requests = [{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{ta-title}}"},"replaceText":"Sheep Farming"}},{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{ta-date}}"},"replaceText":"14 February 2020"}},{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{ta-creator}}"},"replaceText":"Edward Chanter"}},{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{ta-creator-org}}"},"replaceText":"Collaborative Connections Ltd."}},{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{search_query}}"},"replaceText":"sheep OR mutton OR lamb"}},{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{33-text}}"},"replaceText":"Future vantage points on emerging commercial changes"}},{"replaceAllText":{"containsText":{"matchCase":true,"text":"{{total_forecasts}}"},"replaceText":"176 forecasts"}},{"replaceAllShapesWithImage":{"containsText":{"matchCase":true,"text":"{{ta-image}}"},"replaceMethod":"CENTER_CROP","imageUrl":"https://pixabay.com/get/57e8d7414b51ab14f6da8c7dda79367c153cdde65a506c4870277ad3974dc45ab8_1280.jpg"}}];

Nothing seems to work and I can't figure out why. The response I'm getting from the Google Slides API is this:

{
  "error": {
    "code": 500,
    "message": "Internal error encountered.",
    "status": "INTERNAL"
  }
}

As I said before, this was working all of last week and this morning until I added some additional requests. Taking the stuff I added out hasn't made a difference.

Update It isn't working consistently, I've been testing with another user and some reports work whilst others fail. Some fail for me and not him and others fail for him and not me. I'm submitting over 200 requests so I'm starting to think this is related to a quota issue but I don't understand how the quotas work.


问:
Binding local variable
I have read the answers for similar question but I still don't know how to do what I want.
I have:

public partial class DbDataToEdit : Window
{
    public SomeDBDataSet.SomeTableRow RowToModify {get;set;}
    public DbDataToEdit(SomeDBDataSet.SomeTableRow row)
    {
           RowToModify = row;
           .....
```

and
fields in XAML: TextBlock, and DatePicker which i want to bind with fields in "SomeTableRow"(someNumber , someDate). SomeTableRow is a some data[one row] from DB.

Is it possible?

optionally: How to connect elements from one row of the database table to the appropriate fields?

答:
Try to set the datacontext to a DataRowView instead of DataRow

public partial class DbDataToEdit : Window
{
     public DbDataToEdit ()
        {
            DataContext = RowToModify;
            InitializeComponent();
        }
    public DataRowView RowToModify {get;set;}
    public DbDataToEdit(SomeDBDataSet.SomeTableRow row)
       {
           RowToModify = row.Table.DefaultView[row.Table.Rows.IndexOf(row)];
       }
}
```

<p>and in your xaml,connect your UI elements like this</p>

<pre class="lang-xml prettyprint-override"><code><TextBlock Text="{Binding someNumber}"/>
</code>```

ContentLicense: CC BY-SA 3.0

Title: Binding local variable

Body:
I have read the answers for similar question but I still don't know how to do what I want.
I have:

public partial class DbDataToEdit : Window
{
    public SomeDBDataSet.SomeTableRow RowToModify {get;set;}
    public DbDataToEdit(SomeDBDataSet.SomeTableRow row)
    {
           RowToModify = row;
           .....

and
fields in XAML: TextBlock, and DatePicker which i want to bind with fields in "SomeTableRow"(someNumber , someDate). SomeTableRow is a some data[one row] from DB.

Is it possible?

optionally: How to connect elements from one row of the database table to the appropriate fields?

Body:
Try to set the datacontext to a DataRowView instead of DataRow

public partial class DbDataToEdit : Window
{
     public DbDataToEdit ()
        {
            DataContext = RowToModify;
            InitializeComponent();
        }
    public DataRowView RowToModify {get;set;}
    public DbDataToEdit(SomeDBDataSet.SomeTableRow row)
       {
           RowToModify = row.Table.DefaultView[row.Table.Rows.IndexOf(row)];
       }
}

and in your xaml,connect your UI elements like this

<TextBlock Text="{Binding someNumber}"/>


问:
Docker-compose api and database configuration
I have a problem with connecting Api with MySQL database running in containers. I have Dockerfile for Golang Api:

```<code>FROM golang:latest
 WORKDIR /app
 COPY go.mod go.sum ./
RUN go mod download
 COPY . .
WORKDIR /app/bin
 EXPOSE 8080
 RUN go run ./../cmd/web/
</code>```

I usually connect with database in the application using database/sql:

```<code>dsn = "user1:pass@tcp(wpmysql:3306)/wp?parseTime=true"
db, err := sql.Open("mysql", dsn)
</code>```

My docker-compose.yml:

```<code>version: '3'
services:
  db:
    image: mysql:5.7
    container_name: ${MYSQL_CONTAINER_NAME}
    ports:
        - 3306:3306
    command: --init-file /usr/src/app/init.sql
    volumes:
        - ./init.sql:/usr/src/app/init.sql
    environment:
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASS}
        - MYSQL_DATABASE=${MYSQL_DB}
        - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
    networks:
        - fullstack
  web:
    container_name: wpapi
    build: .
    ports:
        - 8080:8080
    restart: on-failure
    volumes:
        - .:/usr/src/app/
    depends_on:
        - db
    networks:
        - fullstack

networks:
    fullstack:
        driver: bridge
</code>```

In the same directory as docker-compose.yml is file .env:

```<code>DB_PASSWORD=pass
MYSQL_PORT=3306
MYSQL_USER=user1
MYSQL_PASS=pass
MYSQL_DB=wp
MYSQL_CONTAINER_NAME=wpmysql
</code>```

After call commends:

```<code>$ docker-compose up -d db
$ docker-compose build web
</code>```

I get error ERROR main.go:46: dial tcp: lookup wpmysql on 37.8.214.2:53: no such host. List of containers looks like:

```<code>CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
9fbaf67df5bf        2778fcda2046        "/bin/sh -c 'go run …"   14 seconds ago      Up 13 seconds       8080/tcp                            mystifying_shannon
7f6c76cc9c4f        mysql:5.7           "docker-entrypoint.s…"   40 minutes ago      Up About a minute   0.0.0.0:3306->3306/tcp, 33060/tcp   wpmysql
</code>```

Moreover when I try to connect in application by dsn = "user1:pass@tcp(localhost:3306)/wp?parseTime=true" or dsn = "root:pass@tcp(localhost:3306)/wp?parseTime=true" I get another error:
dial tcp 127.0.0.1:3306: connect: connection refused although I can go into container (docker exec -it wpmysql bash -l) and sign in with root and user1 credentials

答:
In your docker file you have:

RUN go run ./../cmd/web/

This will attempt to build AND run your executable during the build process. The network fullstack is not available at this time. I think you probably meant to use:

CMD go run ../cmd/web/

This will set the default command run when you start (i.e. docker-compose up) the container to go run ../cmd/web/. Even better would be:

```<code>RUN go build ../cmd/web/
CMD ../cmd/web/web
</code>```

This will build your application as part of the process of building the container and then set the executable produced as the default command. The benefit of doing this is that compile errors become apparent when you build the image (and it means the application is not built every time you start the container).

Body:
I have a problem with connecting Api with MySQL database running in containers. I have Dockerfile for Golang Api:

FROM golang:latest
 WORKDIR /app
 COPY go.mod go.sum ./
RUN go mod download
 COPY . .
WORKDIR /app/bin
 EXPOSE 8080
 RUN go run ./../cmd/web/

I usually connect with database in the application using database/sql:

dsn = "user1:pass@tcp(wpmysql:3306)/wp?parseTime=true"
db, err := sql.Open("mysql", dsn)

My docker-compose.yml:

version: '3'
services:
  db:
    image: mysql:5.7
    container_name: ${MYSQL_CONTAINER_NAME}
    ports:
        - 3306:3306
    command: --init-file /usr/src/app/init.sql
    volumes:
        - ./init.sql:/usr/src/app/init.sql
    environment:
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASS}
        - MYSQL_DATABASE=${MYSQL_DB}
        - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
    networks:
        - fullstack
  web:
    container_name: wpapi
    build: .
    ports:
        - 8080:8080
    restart: on-failure
    volumes:
        - .:/usr/src/app/
    depends_on:
        - db
    networks:
        - fullstack

networks:
    fullstack:
        driver: bridge

In the same directory as docker-compose.yml is file .env:

DB_PASSWORD=pass
MYSQL_PORT=3306
MYSQL_USER=user1
MYSQL_PASS=pass
MYSQL_DB=wp
MYSQL_CONTAINER_NAME=wpmysql

After call commends:

$ docker-compose up -d db
$ docker-compose build web

I get error ERROR main.go:46: dial tcp: lookup wpmysql on 37.8.214.2:53: no such host. List of containers looks like:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
9fbaf67df5bf        2778fcda2046        "/bin/sh -c 'go run …"   14 seconds ago      Up 13 seconds       8080/tcp                            mystifying_shannon
7f6c76cc9c4f        mysql:5.7           "docker-entrypoint.s…"   40 minutes ago      Up About a minute   0.0.0.0:3306->3306/tcp, 33060/tcp   wpmysql

Moreover when I try to connect in application by dsn = "user1:pass@tcp(localhost:3306)/wp?parseTime=true" or dsn = "root:pass@tcp(localhost:3306)/wp?parseTime=true" I get another error:
dial tcp 127.0.0.1:3306: connect: connection refused although I can go into container (docker exec -it wpmysql bash -l) and sign in with root and user1 credentials

Body:
In your docker file you have:

RUN go run ./../cmd/web/

This will attempt to build AND run your executable during the build process. The network fullstack is not available at this time. I think you probably meant to use:

CMD go run ../cmd/web/

This will set the default command run when you start (i.e. docker-compose up) the container to go run ../cmd/web/. Even better would be:

RUN go build ../cmd/web/
CMD ../cmd/web/web

This will build your application as part of the process of building the container and then set the executable produced as the default command. The benefit of doing this is that compile errors become apparent when you build the image (and it means the application is not built every time you start the container).


问:
Basic threading with single backgroundworker for multiple tasks
I'd want to guarantee the order of execution in a single backgroundworker thread like this</p>

```<code>One BackgroundWorker DoWork =
{
  First();
  Second();
  Third();
  ...
}

Function First()
{
   try{
      X();
      Y();
      Z(); //etc
   }catch
}
</code>```

答:
The question is marked for C#. Assuming the code is a representation of what you want to achieve in C# consider:</p>

```<code>private void button1_Click(object sender, EventArgs e)
{
    //If you need a return result
    //Task.FromResult(MainThread().ConfigureAwait(false));
    Task.Run(MainThread);
}

private async Task MainThread()
{
     var result1 = await Task1().ConfigureAwait(false);

     await Task2().ConfigureAwait(false);

     var result3 = await Task3().ConfigureAwait(false);
}

private async Task<int> Task1()
{
    await Task.Delay(2000).ConfigureAwait(false);
    Debug.WriteLine("Executed task 1");
    return 1;
}

private async Task Task2()
{
    await Task.Delay(100).ConfigureAwait(false);
    Debug.WriteLine("Executed task 2");
}

private async Task<int> Task3()
{
    await Task.Delay(1000).ConfigureAwait(false);
    Debug.WriteLine("Executed task 3");
    return 3;
}
</code>```

Body:
I'd want to guarantee the order of execution in a single backgroundworker thread like this

One BackgroundWorker DoWork =
{
  First();
  Second();
  Third();
  ...
}

Function First()
{
   try{
      X();
      Y();
      Z(); //etc
   }catch
}

Body:
The question is marked for C#. Assuming the code is a representation of what you want to achieve in C# consider:

private void button1_Click(object sender, EventArgs e)
{
    //If you need a return result
    //Task.FromResult(MainThread().ConfigureAwait(false));
    Task.Run(MainThread);
}

private async Task MainThread()
{
     var result1 = await Task1().ConfigureAwait(false);

     await Task2().ConfigureAwait(false);

     var result3 = await Task3().ConfigureAwait(false);
}

private async Task<int> Task1()
{
    await Task.Delay(2000).ConfigureAwait(false);
    Debug.WriteLine("Executed task 1");
    return 1;
}

private async Task Task2()
{
    await Task.Delay(100).ConfigureAwait(false);
    Debug.WriteLine("Executed task 2");
}

private async Task<int> Task3()
{
    await Task.Delay(1000).ConfigureAwait(false);
    Debug.WriteLine("Executed task 3");
    return 3;
}


问:
Tell us about "clientToWorld"
"clientToWorld" is used to identify and mark members and coordinates.
We are also considering implementing a function to display attribute information of the selected members.
However, the dbid of the member specified by "clientToWorld" is the Leaf Object's one, and the correct attribute information cannot be obtained.

When selecting a member by simply clicking the mouse, Leaf or First is selected by "SelectionMode". Isn't the same control possible with "clientToWorld"?

答:
Try find the node id per the desired selection mode after getting the dbid from clientToWorld:

```<code>const result = NOP_VIEWER.clientToWorld(x,y);
NOP_VIEWER.model.getInstanceTree().findNodeForSelection(result.dbId,Autodesk.Viewing.SelectionMode["LEAF_OBJECT"])
</code>```

Title: Tell us about "clientToWorld"

Body:
Try find the node id per the desired selection mode after getting the dbid from clientToWorld:

const result = NOP_VIEWER.clientToWorld(x,y);
NOP_VIEWER.model.getInstanceTree().findNodeForSelection(result.dbId,Autodesk.Viewing.SelectionMode["LEAF_OBJECT"])


问:
How to access instance variable inside @classmethod?
I have two class as below:

```<code>class A:
    def __init__(self, a):
        self.a = a

class B(A):
    @classmethod
    def good(cls):
        return cls.a

a = B('good')
</code>```

When I run print(a.good())
It shows AttributeError: type object 'B' has no attribute 'a'

How to access a variable from good method?

答:
In no way.

@classmethods do not have access to instances of the class.

Body:
I have two class as below:

class A:
    def __init__(self, a):
        self.a = a

class B(A):
    @classmethod
    def good(cls):
        return cls.a

a = B('good')

When I run print(a.good())
It shows AttributeError: type object 'B' has no attribute 'a'

How to access a variable from good method?


问:
R's paste function equivalent in BASH?
I'm trying to translate this piece of R code into bash:

```<code>year <- 2010
emmissions <- paste("Emmision table from ",year, sep="")
em_table <- read.table(emmissions, head=F, as.is=T)

for (i in 1:nrow(em_table)) {
        print(em_table[i,1])
}
</code>```

But can't figure out how to translate paste function to concatenate string with variable. Expected outcome would be this script translated into bash code.

答:
Using perhaps echo "Emmision table from  ${year}" inside your for loop something like below:

```<code>for var in 2001 2003 2004;
do
    echo "Emission table ${var}"
done
</code>```

Updated on OP's request:

for sequence generation in bash , one can do : for i in {1..5} for a sequence of 1 to 5 or {0..20..4} for a step size of 4 from a sequence of 0 to 20

Assuming a column like structure, one can do this:
Assuming a table which has two column saved in a text file

```<code>col1 col2
1 2
3 4
5 6

while read col1 col2
do
    echo "Col1 : $col1"
    echo "Col2 : $col2"
done < table.txt
</code>```

Title: R's paste function equivalent in BASH?

Body:
I'm trying to translate this piece of R code into bash:

year <- 2010
emmissions <- paste("Emmision table from ",year, sep="")
em_table <- read.table(emmissions, head=F, as.is=T)

for (i in 1:nrow(em_table)) {
        print(em_table[i,1])
}

But can't figure out how to translate paste function to concatenate string with variable. Expected outcome would be this script translated into bash code.

Body:
Using perhaps echo "Emmision table from  ${year}" inside your for loop something like below:

for var in 2001 2003 2004;
do
    echo "Emission table ${var}"
done

Updated on OP's request:

for sequence generation in bash , one can do : for i in {1..5} for a sequence of 1 to 5 or {0..20..4} for a step size of 4 from a sequence of 0 to 20

Assuming a column like structure, one can do this:
Assuming a table which has two column saved in a text file

col1 col2
1 2
3 4
5 6

while read col1 col2
do
    echo "Col1 : $col1"
    echo "Col2 : $col2"
done < table.txt


问:
Date format and time zone JS
I would like to explain my problem of the day.

In the following code, everything works properly. The problem is I receive the "created" in the following format:

```<code>"created": "2020-02-18T13:45:01.652Z"
</code>```

but I would only like to recover the time if possible without the seconds:

```<code>postbackend = () => {
  const config = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      ...this.state,
      items: newItems,
      created: new
      Date().toISOString()
    }),
  };
  const url = entrypoint + "/alluserpls";
  fetch(url, config)
    .then(res => res.json())
    .then(res => {
      if (res.error) {
        alert(res.error);
      } else {
        alert(`ajouté avec l'ID ${res}!`);
      }
    }).catch(e => {
      console.error(e);
    }).finally(() => this.setState({ redirect: true }));
}
</code>```

Do you have an idea of how to fix this? Neff

答:
You are using the method Date().toISOString() which returns an ISO 8601 conform date string as explained in the official MDN documentation.

If you want to format the date without seconds, you should use another date format. In your case, you could define your own date format to make it look like the ISO string but without seconds and timezone for instance:

```<code>let today = new Date().toISOString().slice(0,16);
console.info(today);
</code>```

(adapted from this StackOverflow post)

If you just want to receive the time without the date, use this instead:

```<code>let today = new Date().toISOString().slice(11,16);
console.info(today);
</code>```

Title: Date format and time zone JS

Body:
I would like to explain my problem of the day.

In the following code, everything works properly. The problem is I receive the "created" in the following format:

"created": "2020-02-18T13:45:01.652Z"

but I would only like to recover the time if possible without the seconds:

postbackend = () => {
  const config = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      ...this.state,
      items: newItems,
      created: new
      Date().toISOString()
    }),
  };
  const url = entrypoint + "/alluserpls";
  fetch(url, config)
    .then(res => res.json())
    .then(res => {
      if (res.error) {
        alert(res.error);
      } else {
        alert(`ajouté avec l'ID ${res}!`);
      }
    }).catch(e => {
      console.error(e);
    }).finally(() => this.setState({ redirect: true }));
}

Do you have an idea of how to fix this? Neff

Body:
You are using the method Date().toISOString() which returns an ISO 8601 conform date string as explained in the official MDN documentation.

If you want to format the date without seconds, you should use another date format. In your case, you could define your own date format to make it look like the ISO string but without seconds and timezone for instance:

let today = new Date().toISOString().slice(0,16);
console.info(today);

(adapted from this StackOverflow post)

If you just want to receive the time without the date, use this instead:

let today = new Date().toISOString().slice(11,16);
console.info(today);


问:
Optaplanner ConstraintsStream penalize with negative integer
In my Optaplanner project with constraints-stream score calculation the user can define the penaltyScore of the constraints which could be negative or positive.

Is there a neutral variant of penalize (which only accepts nonnegative integer as i understand it) or is it necessary to always have sth. ugly like:

```<code>if(penaltyScore>0){
  ... penalize(...);
} else{
  ... reward(...);
}
</code>```

答: We're fixing this by allowing negative arguments too for a new method, see and track https://issues.redhat.com/browse/PLANNER-1864</p>

Body:
In my Optaplanner project with constraints-stream score calculation the user can define the penaltyScore of the constraints which could be negative or positive.

Is there a neutral variant of penalize (which only accepts nonnegative integer as i understand it) or is it necessary to always have sth. ugly like:

if(penaltyScore>0){
  ... penalize(...);
} else{
  ... reward(...);
}


问:
Attempting to download json file from DnDBeyond gives 403 error
I am making a D&D bot which downloads character sheets from DnDBeyond. Unfortunately, attempting to download the json file for a publicly-available character keeps returning 403 error.

I'm trying to download this character sheet:
https://www.dndbeyond.com/characters/22738330/json

which is publicly available and should return a json file.

I used the following code:

```<code>import requests
url = "https://www.dndbeyond.com/characters/22738330/json"
response = requests.get(url)
print(response.json())
</code>```

Which keeps returning 403 error despite the fact that I am able to load the character on my browser by simply clicking on it. Where am I going wrong?

答:
Try adding a valid User-Agent and other headers to make it seem like you're accessing via a browser.

Something like this:

```<code>headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3835.0 Safari/537.36',
       'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Host':'www.dndbeyond.com',
       'Accept-Language': 'en-US,en;q=0.5',
       'Accept-Encoding': 'gzip, deflate',
       'Upgrade-Insecure-Requests': '1',
       'Connection': 'close'
    }

response = requests.get(url, headers=headers)
</code>```

Body:
I am making a D&D bot which downloads character sheets from DnDBeyond. Unfortunately, attempting to download the json file for a publicly-available character keeps returning 403 error.

I'm trying to download this character sheet:
https://www.dndbeyond.com/characters/22738330/json

which is publicly available and should return a json file.

I used the following code:

import requests
url = "https://www.dndbeyond.com/characters/22738330/json"
response = requests.get(url)
print(response.json())

Which keeps returning 403 error despite the fact that I am able to load the character on my browser by simply clicking on it. Where am I going wrong?

Body:
Try adding a valid User-Agent and other headers to make it seem like you're accessing via a browser.

Something like this:

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3835.0 Safari/537.36',
       'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Host':'www.dndbeyond.com',
       'Accept-Language': 'en-US,en;q=0.5',
       'Accept-Encoding': 'gzip, deflate',
       'Upgrade-Insecure-Requests': '1',
       'Connection': 'close'
    }

response = requests.get(url, headers=headers)


问:
How to query between two x amount of days using Laravel and Carbon?
I'm trying to retrieve data from the past 30 to 60 days. Which means I'm leaving out days 0 to 30 and everything past day 60.

My current progress is as such:

```<code>$previousMonth = DB::table('table')
->select('table.*')
->whereBetween('date', [Carbon::now()->subDays(30), Carbon::now()->subDays(60)])
->get();

return $previousMonth;
</code>```

"date" has the datatype of DATETIME in mysql database.

答:
Change the order, because Carbon::now()->subDays(30) is greater than Carbon::now()->subDays(60), and you can use startOfDay() to get start of day:

->whereBetween('date', [Carbon::now()->subDays(60)->startOfDay(), Carbon::now()->subDays(30)->startOfDay()])
```

Body:
I'm trying to retrieve data from the past 30 to 60 days. Which means I'm leaving out days 0 to 30 and everything past day 60.

My current progress is as such:

$previousMonth = DB::table('table')
->select('table.*')
->whereBetween('date', [Carbon::now()->subDays(30), Carbon::now()->subDays(60)])
->get();

return $previousMonth;

"date" has the datatype of DATETIME in mysql database.


问:
Save Jupyter notebook before nbconvert
I want to export a jupyter notebook within the notebook itself and use this command:

```<code>!jupyter nbconvert --to html MyNotebook.ipynb --output MyNotebook.html
</code>```

This works correct, except that I manually need to save the notebook first for the latest outputs to be in the final html copy. Is there a similar command to save the notebook first (like hitting the save button) before doing the nbconvert?

答:
```<code>import IPython

%%javascript
IPython.notebook.save_notebook()
</code>```

Title: Save Jupyter notebook before nbconvert

Body:
I want to export a jupyter notebook within the notebook itself and use this command:

!jupyter nbconvert --to html MyNotebook.ipynb --output MyNotebook.html

This works correct, except that I manually need to save the notebook first for the latest outputs to be in the final html copy. Is there a similar command to save the notebook first (like hitting the save button) before doing the nbconvert?


问:
SQLite3 import tsv with one line

Set Up

Ubuntu 18.04 Google VM

Objective

I'm trying to create a for loop that will import various client files of data into the relevant client table.

Query test.sh

```<code>#!/bin/bash

set -eu

date=$(date "+%Y%m%d")

for f in $(find $date*.tsv); do
     client=$(echo $f | sed -Ee 's/.csv.tsv//' -e 's/[0-9]+_stuff_stuff_//')
     sqlite3 mydatabase.db "create table if not exists $client (Date integer, Viewability integer, id1 integer, id2 integer, id3 integer, id4 integer, id5 integer, id6 integer, id7 integer, id8 integer, id9 integer, id10 integer, id11 integer, id12 integer, id13 integer, id14 integer, id15 integer, id16 integer, id17 integer, id18 integer, id19 integer, id20 integer);"
     printf ".mode tsv\n.import $f $client\n" | sqlite3 mydatabase.db
done
</code>```

updated test.sh

```<code>#!/bin/bash

set -eu

date=$(date "+%Y%m%d")

for f in $date*.tsv; do
    client=$(echo "$f" | sed -Ee 's/.csv.tsv//' -e 's/[0-9]+_stuff_stuff_//')
sqlite3 -batch mydatabase.db
<<EOF
"create table if not exists $client (Date integer, Viewability integer, id1 integer, id2 integer, id3 integer, id4 integer, id5 integer, id6 integer, id7 integer, id8 integer, id9 integer, id10 integer, id11 integer, id12 integer, id13 integer, id14 integer, id15 integer, id16 integer, id17 integer, id18 integer, id19 integer, id20 integer);"
.mode tabs
.import "$f" $client
EOF
done
</code>```

Error

```<code>    expected 22 columns but found 1 - filling the rest with NULL
</code>```

From what I understood 'sqlite3' doesn't treat new lines in the same way as my initial query looked to run everything on one line with with each command following the other or with ";" separators for new lines. Having researched online people we mentioning that to get the code onto one line I have to change the format to something like what I've used with intentional \n breaks. However when I run the code it seems to not read my files as a tsv file and as such only finds 1 column. Can someone confirm what I seem to be missing with my code or misunderstanding? I can only assume that it is the format as I can do a manual import without issue but I'm struggling to see why the new code wouldn't be accepted in this format. Any help is appreciated.

答:
tsv is not a mode that the sqlite3 shell understands. What you should be doing to import a simple tab-delimited file into an existing table is use the tabs mode:

```<code>for f in $date*.tsv; do
    client=$(echo "$f" | sed -Ee 's/.csv.tsv//' -e 's/[0-9]+_stuff_stuff_//')
    sqlite3 -batch mydatabase.db <<EOF
create table if not exists $client (Date integer, Viewability integer, id1 integer, id2 integer, id3 integer, id4 integer, id5 integer, id6 integer, id7 integer, id8 integer, id9 integer, id10 integer, id11 integer, id12 integer, id13 integer, id14 integer, id15 integer, id16 integer, id17 integer, id18 integer, id19 integer, id20 integer);
.mode tabs
.import "$f" $client
EOF
done
</code>```

(I prefer using heredocs over piping the results of a printf or echo into the shell; makes it a lot easier to read).

In the shell you can use .help mode to see the available modes:

```<code> sqlite> .help mode
.mode MODE ?TABLE?       Set output mode
   MODE is one of:
     ascii    Columns/rows delimited by 0x1F and 0x1E
     csv      Comma-separated values
     column   Left-aligned columns.  (See .width)
     html     HTML <table> code
     insert   SQL insert statements for TABLE
     line     One value per line
     list     Values delimited by "|"
     quote    Escape answers as for SQL
     tabs     Tab-separated values
     tcl      TCL list elements
</code>```

Title: SQLite3 import tsv with one line

Body:
Set Up

Ubuntu 18.04 Google VM

Objective

I'm trying to create a for loop that will import various client files of data into the relevant client table.

Query test.sh

#!/bin/bash

set -eu

date=$(date "+%Y%m%d")

for f in $(find $date*.tsv); do
     client=$(echo $f | sed -Ee 's/.csv.tsv//' -e 's/[0-9]+_stuff_stuff_//')
     sqlite3 mydatabase.db "create table if not exists $client (Date integer, Viewability integer, id1 integer, id2 integer, id3 integer, id4 integer, id5 integer, id6 integer, id7 integer, id8 integer, id9 integer, id10 integer, id11 integer, id12 integer, id13 integer, id14 integer, id15 integer, id16 integer, id17 integer, id18 integer, id19 integer, id20 integer);"
     printf ".mode tsv\n.import $f $client\n" | sqlite3 mydatabase.db
done

updated test.sh

#!/bin/bash

set -eu

date=$(date "+%Y%m%d")

for f in $date*.tsv; do
    client=$(echo "$f" | sed -Ee 's/.csv.tsv//' -e 's/[0-9]+_stuff_stuff_//')
sqlite3 -batch mydatabase.db
<<EOF
"create table if not exists $client (Date integer, Viewability integer, id1 integer, id2 integer, id3 integer, id4 integer, id5 integer, id6 integer, id7 integer, id8 integer, id9 integer, id10 integer, id11 integer, id12 integer, id13 integer, id14 integer, id15 integer, id16 integer, id17 integer, id18 integer, id19 integer, id20 integer);"
.mode tabs
.import "$f" $client
EOF
done

Error

    expected 22 columns but found 1 - filling the rest with NULL

From what I understood 'sqlite3' doesn't treat new lines in the same way as my initial query looked to run everything on one line with with each command following the other or with ";" separators for new lines. Having researched online people we mentioning that to get the code onto one line I have to change the format to something like what I've used with intentional \n breaks. However when I run the code it seems to not read my files as a tsv file and as such only finds 1 column. Can someone confirm what I seem to be missing with my code or misunderstanding? I can only assume that it is the format as I can do a manual import without issue but I'm struggling to see why the new code wouldn't be accepted in this format. Any help is appreciated.

Body:
tsv is not a mode that the sqlite3 shell understands. What you should be doing to import a simple tab-delimited file into an existing table is use the tabs mode:

for f in $date*.tsv; do
    client=$(echo "$f" | sed -Ee 's/.csv.tsv//' -e 's/[0-9]+_stuff_stuff_//')
    sqlite3 -batch mydatabase.db <<EOF
create table if not exists $client (Date integer, Viewability integer, id1 integer, id2 integer, id3 integer, id4 integer, id5 integer, id6 integer, id7 integer, id8 integer, id9 integer, id10 integer, id11 integer, id12 integer, id13 integer, id14 integer, id15 integer, id16 integer, id17 integer, id18 integer, id19 integer, id20 integer);
.mode tabs
.import "$f" $client
EOF
done

(I prefer using heredocs over piping the results of a printf or echo into the shell; makes it a lot easier to read).

In the shell you can use .help mode to see the available modes:

 sqlite> .help mode
.mode MODE ?TABLE?       Set output mode
   MODE is one of:
     ascii    Columns/rows delimited by 0x1F and 0x1E
     csv      Comma-separated values
     column   Left-aligned columns.  (See .width)
     html     HTML <table> code
     insert   SQL insert statements for TABLE
     line     One value per line
     list     Values delimited by "|"
     quote    Escape answers as for SQL
     tabs     Tab-separated values
     tcl      TCL list elements


问:
How to Connect models in Ruby on Rails
I'm new to Ruby on Rails, I'm trying to build a simple shop app that let the user those 2 basic shop options:

Add CUSTOMERS (name, purchased PRODUCTS from the shop, and preferred_payment (can be set to 'cash' or 'credit' only).

Add PRODUCTS to the shop (name, price, and all the CUSTOMERS who bought this)

I'm aware to has_many and belongs_to, but in this case there are many from both.
I thought about using $ 'rails g scaffold' for both of them, but what should be the best way to connect between the customers and products from your experience?

And how can I render the 'cash' / 'credit' selection as a simple dropdown menu at the new customer form?

答:
you can generate a new model that connects the customer with his purchased products

```<code>rails g model cutomer_products customer:references product:references payment_method:{string or references if you has a payment method table}
</code>```

ensure that the cutomer_product model is like this:

```<code>class CutomerProduct < ApplicationRecord
  has_many :products
  has_many :customers
end
</code>```

Title: How to Connect models in Ruby on Rails

Body:
you can generate a new model that connects the customer with his purchased products

rails g model cutomer_products customer:references product:references payment_method:{string or references if you has a payment method table}

ensure that the cutomer_product model is like this:

class CutomerProduct < ApplicationRecord
  has_many :products
  has_many :customers
end


问:
How to make application of Flex for Android?
Actually i have created one dashboard in flex. But now i want to integrate this dashboard or make same dashboard for Android.

How i can achieve this? I want to make dashboard for Android.

Thanks in Advance.

答:
There is a wonderful "Flex for Android " tutorial here

You may want a good Intro to AIR for Mobile :

Watch it here


问:
Could not create cudnn handle: CUDNN STATUS INTERNAL ERROR
I'm trying to create machinelearing in python 3.
but then i trying to compile my code i got this error in Cuda 10.0/cuDNN 7.5.0,
can some one help me with this?

RTX 2080

I'm on:
Keras (2.2.4)
tf-nightly-gpu (1.14.1.dev20190510)

Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR

Code erorr:
tensorflow.python.framework.errors_impl.UnknownError: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.

Here is my code:

```<code>model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(50, 50, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(1, activation='softmax'))

model.summary()

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])
model.fit(x, y, epochs=1, batch_size=n_batch)
</code>```

OOM when allocating tensor with shape[24946,32,48,48] and type float
  on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc

答:
Roko's answer should work if you're using Tensorflow 2.0.

If you wanna set the exact amount of memory to limit(e.g. 1024MB or 2GB, etc.), there's another way to restrict your GPU memory usage.

Use this code:

```<code>gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
  try:
    tf.config.experimental.set_virtual_device_configuration(gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)])
  except RuntimeError as e:
    print(e)
</code>```

This code will limit your 1st GPU’s memory usage up to 1024MB. Just change the index of gpus and memory_limit as you want.

Body:
I'm trying to create machinelearing in python 3.
but then i trying to compile my code i got this error in Cuda 10.0/cuDNN 7.5.0,
can some one help me with this?

RTX 2080

I'm on:
Keras (2.2.4)
tf-nightly-gpu (1.14.1.dev20190510)

Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR

Code erorr:
tensorflow.python.framework.errors_impl.UnknownError: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.

Here is my code:

model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(50, 50, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(1, activation='softmax'))

model.summary()

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])
model.fit(x, y, epochs=1, batch_size=n_batch)

OOM when allocating tensor with shape[24946,32,48,48] and type float
  on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc

Body:
Roko's answer should work if you're using Tensorflow 2.0.

If you wanna set the exact amount of memory to limit(e.g. 1024MB or 2GB, etc.), there's another way to restrict your GPU memory usage.

Use this code:

gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
  try:
    tf.config.experimental.set_virtual_device_configuration(gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)])
  except RuntimeError as e:
    print(e)

This code will limit your 1st GPU’s memory usage up to 1024MB. Just change the index of gpus and memory_limit as you want.


Title: Date format and time zone JS

Body:
var today = "2020-02-18T13:45:01.652Z";
console.log(today.split('').splice(11,5).join(""));

Try this.


问:
Antd design - Heap out of memory - webpacker
I am using rails gem mina for deploying my RubyonRails & React application into  production

added antd npm  package and deployed. so far no issue

when I try to import something from antd  eg:

```<code>import { Icon, List } from 'antd'
</code>```

Below issue is coming

```<code>FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
        1: node::Abort() [/usr/bin/node]
        2: 0x8cd14c [/usr/bin/node]
        3: v8::Utils::ReportOOMFailure(char const*, bool) [/usr/bin/node]
</code>```

答:
Which version of node, webpack-dev-server are you running? Some older versions of webpack-dev-server had a memory leak

You might want to specifically allow the V8 engine more RAM with something like this :

node --max-old-space-size=4096 yourFile.js
```

<p>or in your package.json if you're using create-react-app :</p>

```"start": "react-scripts --max_old_space_size=4096 start"
```

(I put 4096 for 4GB of RAM but you can go higher if needed obviously).

Body:
I am using rails gem mina for deploying my RubyonRails & React application into  production

added antd npm  package and deployed. so far no issue

when I try to import something from antd  eg:

import { Icon, List } from 'antd'

Below issue is coming

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
        1: node::Abort() [/usr/bin/node]
        2: 0x8cd14c [/usr/bin/node]
        3: v8::Utils::ReportOOMFailure(char const*, bool) [/usr/bin/node]

Body:
Which version of node, webpack-dev-server are you running? Some older versions of webpack-dev-server had a memory leak

You might want to specifically allow the V8 engine more RAM with something like this :

node --max-old-space-size=4096 yourFile.js

or in your package.json if you're using create-react-app :

"start": "react-scripts --max_old_space_size=4096 start"

(I put 4096 for 4GB of RAM but you can go higher if needed obviously).


问:
Flutter: call function during scroll
I have a scroll controller which checks if the scroll position passed the threshold and then call the function:

```<code>final _scrollController = ScrollController();
final _scrollThreshold = 200.0;
_scrollController.addListener(_onScroll);

void _onScroll() {
  final maxScroll = _scrollController.position.maxScrollExtent;
  final currentScroll = _scrollController.position.pixels;
  if (maxScroll - currentScroll <= _scrollThreshold) {
    dummyFunction();
  }
}
</code>```

Currently, dummyFunction() is called multiple times, How to make sure it is going to be called once?

答:
Use a boolean storing wheather the function was called or not :

```<code>final _scrollController = ScrollController();
final _scrollThreshold = 200.0;
_scrollController.addListener(_onScroll);
final _hasReachedPoint = false;

void _onScroll() {
  final maxScroll = _scrollController.position.maxScrollExtent;
  final currentScroll = _scrollController.position.pixels;
  if (maxScroll - currentScroll <= _scrollThreshold && !_hasReachedPoint) {
    _hasReachedPoint = true;
    dummyFunction();
  }
}
</code>```

Title: Flutter: call function during scroll

Body:
I have a scroll controller which checks if the scroll position passed the threshold and then call the function:

final _scrollController = ScrollController();
final _scrollThreshold = 200.0;
_scrollController.addListener(_onScroll);

void _onScroll() {
  final maxScroll = _scrollController.position.maxScrollExtent;
  final currentScroll = _scrollController.position.pixels;
  if (maxScroll - currentScroll <= _scrollThreshold) {
    dummyFunction();
  }
}

Currently, dummyFunction() is called multiple times, How to make sure it is going to be called once?

Body:
Use a boolean storing wheather the function was called or not :

final _scrollController = ScrollController();
final _scrollThreshold = 200.0;
_scrollController.addListener(_onScroll);
final _hasReachedPoint = false;

void _onScroll() {
  final maxScroll = _scrollController.position.maxScrollExtent;
  final currentScroll = _scrollController.position.pixels;
  if (maxScroll - currentScroll <= _scrollThreshold && !_hasReachedPoint) {
    _hasReachedPoint = true;
    dummyFunction();
  }
}


问:
How can I set up default startup commands in iPython notebooks?
I want to put couple of cells with commands I need in almost every new notebook in every new notebook I create.

For example when I create a new notebook it should put a

```<code>%matplotlib inline
import matplotlib.pyplot as plt
</code>```

in a cell by default but not execute it.
How could I set something like that up?

答:
This will work for both terminal based IPython shell and Browser based Notebook:

Navigate to ~/.ipython/profile_default

Create a folder called startup if it’s not already there

Add a new Python file called start.py

Put your favorite imports (and custom functions may be) in this file

Launch IPython or a Jupyter Notebook and your favorite libraries will be automatically loaded every time!

Here is my sample for start.py:

Another Source

Body:
I want to put couple of cells with commands I need in almost every new notebook in every new notebook I create.

For example when I create a new notebook it should put a

%matplotlib inline
import matplotlib.pyplot as plt

in a cell by default but not execute it.
How could I set something like that up?


问:
How can I wait until a HttpServer has finished (i.e. join its background thread)?
I have this code:

import com.sun.net.httpserver.HttpServer;

...

DatabaseConnection dbConnection = ...
HttpServer server = HttpServer.create();
server.createContext("/endpoint", new MyHandler(dbConnection));
server.start();

// What should I do here in order to wait until `server` stops?

dbConnection.close()
```

The com.sun.net.httpserver.HttpServer#start documentation states: "Starts this server in a new background thread."

I would like to call dbConnection.close() after server's background thread stops executing (which may happen e.g. because I press Ctrl+C). But the HttpServer documentation doesn't specify any way to access the background thread, so I can't figure out how to .join() it.

How can I wait until the HttpServer's background thread stops? Or is there another way I can ensure that dbConnection.close() is called when my app shuts down?

答:
The server will not stop by itself. You need to tell it to stop. According to documentation, there is a stop(int delay) method for that.

So your question should rather be "how do I wait in my program until I tell the server to stop?"

Few options:

Wait for user to press a key. Upside: very simple. Downside: cannot be used for background services.

Wait for Java process to exit (use shutdown hooks); note however that this is not the best as you have limited control over the order in which shutdown hooks are being called, and JVM might be hard-killed anyway so it might not be possible to achieve what you want.

Wait for some other external event - request on special URL, timeout, OS signal, appearance of a designated file in a designated directory etc.

Use some kind of inter-process communication (shared memory, named pipes, sockets, streams, JMS etc.) to communicate with your program externally. This way, you can make a separate program that will connect to the socket and send "EXIT" command to your server program. When you receive the "EXIT" command in your server program, you shutdown the server, close the connection and exit the main method.

The last option is the best and the cleanest, but it requires a bit more work. It's also very versatile because you can expand the command set to include other useful stuff - reloading the server configuration file, add or remove URL mappings at runtime, get service statistics and so on. This is for example how Apache works.

Body:
I have this code:

import com.sun.net.httpserver.HttpServer;

...

DatabaseConnection dbConnection = ...
HttpServer server = HttpServer.create();
server.createContext("/endpoint", new MyHandler(dbConnection));
server.start();

// What should I do here in order to wait until `server` stops?

dbConnection.close()

The com.sun.net.httpserver.HttpServer#start documentation states: "Starts this server in a new background thread."

I would like to call dbConnection.close() after server's background thread stops executing (which may happen e.g. because I press Ctrl+C). But the HttpServer documentation doesn't specify any way to access the background thread, so I can't figure out how to .join() it.

How can I wait until the HttpServer's background thread stops? Or is there another way I can ensure that dbConnection.close() is called when my app shuts down?


问:
Removing Google Cloud Storage notifications
I am wondering if there is some way to view current existing webhook notifications channels from Google Cloud Storage and remove them using the gsutil notification stopchannel command.

As part of my development process i have opened a few channels without closing them afterwards by mistake, and have since lost the UUID channel and resouce identifiers and so i am wondering where i can see active notifications.

I am using the GoogleClousStorage python JSON API v1 so start a webhook channel from GCS into my appengine.

thanks,

etay

答:
You can use gsutil notification list -o gs://my-bucket/ to list current webhooks on a bucket.
It returns something like:

```<code>Notification channel 1:
                Channel identifier: someid-905e-905e-905e-1fdca170d689
                Resource identifier: myIdentifierXXXfncnI0SuU-U
                Application URL: url=https://my-project.appspot.com/myWebHook&appEngine=true
                Created by:
                Creation time: 2015-09-08 11:44:26.775000
</code>```

Then you can use the command gsutil notification stopchannel someid-905e-905e-905e-1fdca170d689 myIdentifierXXXfncnI0SuU-U to delete it.

I'm currently having issues with old webhooks, created without service accounts but with service accounts, this works perfectly.

Body:
You can use gsutil notification list -o gs://my-bucket/ to list current webhooks on a bucket.
It returns something like:

Notification channel 1:
                Channel identifier: someid-905e-905e-905e-1fdca170d689
                Resource identifier: myIdentifierXXXfncnI0SuU-U
                Application URL: url=https://my-project.appspot.com/myWebHook&appEngine=true
                Created by:
                Creation time: 2015-09-08 11:44:26.775000

Then you can use the command gsutil notification stopchannel someid-905e-905e-905e-1fdca170d689 myIdentifierXXXfncnI0SuU-U to delete it.

I'm currently having issues with old webhooks, created without service accounts but with service accounts, this works perfectly.


问:
ExtJS4 - MVC Store, retrieve data
I have a controller with store, model and view (toolbar) :

```<code>Ext.define('Cc.controller.Headers', {
  extend: 'Ext.app.Controller',

  stores: ['User'],
  models: ['Agent'],
  views: ['Header'],

  refs: [
    { ref: 'navigation', selector: 'navigation' },
    { ref: 'tabPanel', selector: 'tabpanel' },
    { ref: 'head', selector: 'head' },
    { ref: 'logoutButton', selector: 'head button[action=logout]'},
    { ref: 'helpButton', selector: 'head button[action=help]'}
  ],

  init: function() {
    this.control({
      'head button[action=logout]': {
        beforerender: this.initLogoutButton,
        click: this.onClickLogoutButton
      },
      'head button[action=help]':{
        click: this.onClickHelpButton
      }
    });
  },

  initLogoutButton: function(a){
    var store = this.getUserStore(),
      button = this.getLogoutButton();
    store.load();
    var user = this.getAgentModel();
    button.setText('Logout ['+user.get('lastname')+']');
    console.log(store.count());
  },

  onClickLogoutButton: function(view, record, item, index, e){
    alert('déconnexion');
  },

  onClickHelpButton: function(view, record, item, index, e){
    alert('help');
  }
});
</code>```

my problem is in the initLogoutButton function. I need to retrieve the only instance of user I get from ajax request (always only one instance). I can see the request in js console, all is good, but I can't set variable or array with this data !

In addition, the store.count() function return 0.

答:
If you are using a proxy to load data into your store, you should note that the request asynchronous! you need to set the button text after the store is loaded with data.

```<code>store.load({
    scope   : this,
    callback: function(records, operation, success) {
        // check for success and set the value of button
    }
});
</code>```

The callback function is called after the store is loaded and then you set the button value.

Title: ExtJS4 - MVC Store, retrieve data

Body:
I have a controller with store, model and view (toolbar) :

Ext.define('Cc.controller.Headers', {
  extend: 'Ext.app.Controller',

  stores: ['User'],
  models: ['Agent'],
  views: ['Header'],

  refs: [
    { ref: 'navigation', selector: 'navigation' },
    { ref: 'tabPanel', selector: 'tabpanel' },
    { ref: 'head', selector: 'head' },
    { ref: 'logoutButton', selector: 'head button[action=logout]'},
    { ref: 'helpButton', selector: 'head button[action=help]'}
  ],

  init: function() {
    this.control({
      'head button[action=logout]': {
        beforerender: this.initLogoutButton,
        click: this.onClickLogoutButton
      },
      'head button[action=help]':{
        click: this.onClickHelpButton
      }
    });
  },

  initLogoutButton: function(a){
    var store = this.getUserStore(),
      button = this.getLogoutButton();
    store.load();
    var user = this.getAgentModel();
    button.setText('Logout ['+user.get('lastname')+']');
    console.log(store.count());
  },

  onClickLogoutButton: function(view, record, item, index, e){
    alert('déconnexion');
  },

  onClickHelpButton: function(view, record, item, index, e){
    alert('help');
  }
});

my problem is in the initLogoutButton function. I need to retrieve the only instance of user I get from ajax request (always only one instance). I can see the request in js console, all is good, but I can't set variable or array with this data !

In addition, the store.count() function return 0.

Body:
If you are using a proxy to load data into your store, you should note that the request asynchronous! you need to set the button text after the store is loaded with data.

store.load({
    scope   : this,
    callback: function(records, operation, success) {
        // check for success and set the value of button
    }
});

The callback function is called after the store is loaded and then you set the button value.