问:
JS onunload event won't always work
I want to count how much time visitors spend on a certain page and store it in my MySQL DB.

I thought of just starting a timer on window.onload like this:

```<code>window.onload= startCount;
window.onunload= sendCount;

var b=0;
var y;
function startCount() {
    document.getElementById('livecount').innerHTML=b;
    b=b+1;
    y=setTimeout("startCount()",1000);
}
</code>```

and after the visitor leaves the page (window.onunload), I'm sending the time through an XMLHttpRequest to a PHP file, which will store it in my DB:

```<code>function sendCount() {
    clearTimeout(y);
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","count.php?q="+b,true);
xmlhttp.send();
}
</code>```

The problem is it doesn't always work. I would say it works like 3 out of 10 times I try it.
Could it be that there is not enough time left for the PHP and SQL to be completely executed?

答:
As onunload is unreliable, I would consider an AJAX solution.  I am not an expert in this at all, but the thought that comes to mind is a ping to your counter every so often.  I wouldn't ping your counter every second, as that seems excessive.

I would break down times into durations you care about, such as <5 seconds, <30 seconds, <1 minute, <5 minutes, 5+ minutes.

Then I would write a piece of JavaScript like so:

```<code>window.onload = soCounter;

var iter=0;
var times=[0,5,30,60,300];  // Your duration choices

function doCounter() {

    sendCountPing(times(iter));

    iter++;

    if(iter < times.length) {
        setTimeout(doCounter, times[iter]*1000);
    }
}

function sendCountPing(dur) {
    if (window.XMLHttpRequest)
    {  // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {  // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET","count.php?q="+b,true);
    xmlhttp.send();

}
</code>```

The trick is that for every count.php?q=30 that gets sent, you need to subtract one from the count.php?q=5 count.  This does not have to be programmatic, it is easily post-processed.

To clarify, if you had 3 visitors, one spending 3 seconds, one spending 20 seconds, and one spending 30 minutes, you would see the following counts:

```<code>  0:  3   // 3 people spent at least 0 seconds
  5:  2   // 2 people spent at least 5 seconds
 30:  1
 60:  1
300:  1   // 1 person spent at least 5 minutes
</code>```

Make sense?

Title: JS onunload event won't always work

Body:
I want to count how much time visitors spend on a certain page and store it in my MySQL DB.

I thought of just starting a timer on window.onload like this:

window.onload= startCount;
window.onunload= sendCount;

var b=0;
var y;
function startCount() {
    document.getElementById('livecount').innerHTML=b;
    b=b+1;
    y=setTimeout("startCount()",1000);
}

and after the visitor leaves the page (window.onunload), I'm sending the time through an XMLHttpRequest to a PHP file, which will store it in my DB:

function sendCount() {
    clearTimeout(y);
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","count.php?q="+b,true);
xmlhttp.send();
}

The problem is it doesn't always work. I would say it works like 3 out of 10 times I try it.
Could it be that there is not enough time left for the PHP and SQL to be completely executed?

Body:
As onunload is unreliable, I would consider an AJAX solution.  I am not an expert in this at all, but the thought that comes to mind is a ping to your counter every so often.  I wouldn't ping your counter every second, as that seems excessive.

I would break down times into durations you care about, such as <5 seconds, <30 seconds, <1 minute, <5 minutes, 5+ minutes.

Then I would write a piece of JavaScript like so:

window.onload = soCounter;

var iter=0;
var times=[0,5,30,60,300];  // Your duration choices

function doCounter() {

    sendCountPing(times(iter));

    iter++;

    if(iter < times.length) {
        setTimeout(doCounter, times[iter]*1000);
    }
}

function sendCountPing(dur) {
    if (window.XMLHttpRequest)
    {  // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {  // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET","count.php?q="+b,true);
    xmlhttp.send();

}

The trick is that for every count.php?q=30 that gets sent, you need to subtract one from the count.php?q=5 count.  This does not have to be programmatic, it is easily post-processed.

To clarify, if you had 3 visitors, one spending 3 seconds, one spending 20 seconds, and one spending 30 minutes, you would see the following counts:

  0:  3   // 3 people spent at least 0 seconds
  5:  2   // 2 people spent at least 5 seconds
 30:  1
 60:  1
300:  1   // 1 person spent at least 5 minutes

Make sense?


来源: stackexchange

问:
Camel route with intermediate processing
I need to write a Camel route that would send a file to a web service. Before sending the file to the endpoint, I have to query a database for some information and send the file to the endpoint along with additional information. I would also have to move the file to another directory after the entire route has completed. I was able to create the individual parts of the route independently. I wanted to know how I could do this in a single route.</p>

答:
It's your design decision how to do that.
You can have your individual parts as sub-routes (that is my preferable way).

It makes route more functional structured and at least more readable.
 Then you can pass your message(file) to them one after another or in parallel by using multicast component.

in XML DSL it will look like:

```<code><route id="main-route">
    <from uri="..." />
    <!-- DB processing -->
    <to uri="direct:db-route-endpoint"/>
    <multicast parallelProcessing="false">
    <!-- No parallel processing: file will be stored after Web Service call completed
         or for parallel parallelProcessing="true" -->"
        <to uri="...web service endpoint... "/>
        <to uri="direct:store-file-endpoint"/>
    </multicast>
</route>

<route id="db-route">
    <from uri="direct:db-route-endpoint" />
    ... DB processing ...
</route>

<route id="store-file-route">
    <from uri="direct:store-file-endpoint" />
    ... save file to another directory ...
</route>
</code>```

ContentLicense: CC BY-SA 3.0

Body:
It's your design decision how to do that.
You can have your individual parts as sub-routes (that is my preferable way).

It makes route more functional structured and at least more readable.
 Then you can pass your message(file) to them one after another or in parallel by using multicast component.

in XML DSL it will look like:

<route id="main-route">
    <from uri="..." />
    <!-- DB processing -->
    <to uri="direct:db-route-endpoint"/>
    <multicast parallelProcessing="false">
    <!-- No parallel processing: file will be stored after Web Service call completed
         or for parallel parallelProcessing="true" -->"
        <to uri="...web service endpoint... "/>
        <to uri="direct:store-file-endpoint"/>
    </multicast>
</route>

<route id="db-route">
    <from uri="direct:db-route-endpoint" />
    ... DB processing ...
</route>

<route id="store-file-route">
    <from uri="direct:store-file-endpoint" />
    ... save file to another directory ...
</route>


问:
How to put filter of renderDataTable at the top in R shiny
I am using renderDataTable in R & Shiny to present my data, but the filter is positioned at the bottom of table. Is there any way to put it at the top of the table? Thanks in advance!

BTW, where can I find the option list of renderDataTable?

```<code>  mainPanel(
    tabsetPanel(
      tabPanel("Table",tableOutput("values")),
      tabPanel("Tree Plot",plotOutput("plot")),
      tabPanel("Segment Data",dataTableOutput("obs"))
</code>```

I'm not familiar with CSS, can anyone give me some advice on how to edit CSS inside R function?

答:
I know very little of css and I prefer to do as many as possible from the core script that I don't need to worry about making changes to the cosmetic thing here or there later. So I used the following approach by setting "filter = 'top'" when calling renderDataTable() on server. No CSS required at all.

```<code>mainPanel("Data",
           dataTableOutput("datatbl"))
</code>```

server.R

```<code># create data table output
output$datatbl <- DT::renderDataTable(

  #data
  dt2(), rownames = FALSE,

  # column filter on the top
  filter = 'top', server = TRUE,

  # autoWidth
  options = list(autoWidth = TRUE))
</code>```

Note that I only use renderDataTable() to call the function, to use arguments in the function we cannot write it as programming codes in {}, I guess. Let me know if it works for you.

Body:
I am using renderDataTable in R & Shiny to present my data, but the filter is positioned at the bottom of table. Is there any way to put it at the top of the table? Thanks in advance!

BTW, where can I find the option list of renderDataTable?

  mainPanel(
    tabsetPanel(
      tabPanel("Table",tableOutput("values")),
      tabPanel("Tree Plot",plotOutput("plot")),
      tabPanel("Segment Data",dataTableOutput("obs"))

I'm not familiar with CSS, can anyone give me some advice on how to edit CSS inside R function?

Body:
I know very little of css and I prefer to do as many as possible from the core script that I don't need to worry about making changes to the cosmetic thing here or there later. So I used the following approach by setting "filter = 'top'" when calling renderDataTable() on server. No CSS required at all.

mainPanel("Data",
           dataTableOutput("datatbl"))

server.R

# create data table output
output$datatbl <- DT::renderDataTable(

  #data
  dt2(), rownames = FALSE,

  # column filter on the top
  filter = 'top', server = TRUE,

  # autoWidth
  options = list(autoWidth = TRUE))

Note that I only use renderDataTable() to call the function, to use arguments in the function we cannot write it as programming codes in {}, I guess. Let me know if it works for you.


问:
How to switch between maven version in mac
I have maven3.1.1 and maven3.2.5 installed on my mac.

When I do mvn -version it shows maven3.1.1

How can I switch to the other maven version 3.2.5 ?

答:
Using SDKMan might be a simpler way to switch between multiple Maven versions.

1) Install SDKMan which is a one-off thing: http://sdkman.io/install.html

2) Do sdk install maven yourVersion

3) To switch: sdk install maven anotherVersion

4) To switch again: sdk use maven yourVersion

SDKMan should simplify such operations for several other software frameworks like Java, Groovy, Spring, Gradle, etc.


问:
32-bit to 16-bit Floating Point Conversion
I need a cross-platform library/algorithm that will convert between 32-bit and 16-bit floating point numbers.  I don't need to perform math with the 16-bit numbers; I just need to decrease the size of the 32-bit floats so they can be sent over the network.  I am working in C++.

I understand how much precision I would be losing, but that's OK for my application.

The IEEE 16-bit format would be great.

答:
This code converts a 32-bit floating point number to 16-bits and back.

```<code>#include <x86intrin.h>
#include <iostream>

int main()
{
    float f32;
    unsigned short f16;
    f32 = 3.14159265358979323846;
    f16 = _cvtss_sh(f32, 0);
    std::cout << f32 << std::endl;
    f32 = _cvtsh_ss(f16);
    std::cout << f32 << std::endl;
    return 0;
}
</code>```

I tested with the Intel icpc 16.0.2:

```<code>$ icpc a.cpp
</code>```

g++ 7.3.0:

```<code>$ g++ -march=native a.cpp
</code>```

and clang++ 6.0.0:

```<code>$ clang++ -march=native a.cpp
</code>```

It prints:

```<code>$ ./a.out
3.14159
3.14062
</code>```

Documentation about these intrinsics is available at:

https://software.intel.com/en-us/node/524287

https://clang.llvm.org/doxygen/f16cintrin_8h.html

ContentLicense: CC BY-SA 2.5

Body:
This code converts a 32-bit floating point number to 16-bits and back.

#include <x86intrin.h>
#include <iostream>

int main()
{
    float f32;
    unsigned short f16;
    f32 = 3.14159265358979323846;
    f16 = _cvtss_sh(f32, 0);
    std::cout << f32 << std::endl;
    f32 = _cvtsh_ss(f16);
    std::cout << f32 << std::endl;
    return 0;
}

I tested with the Intel icpc 16.0.2:

$ icpc a.cpp

g++ 7.3.0:

$ g++ -march=native a.cpp

and clang++ 6.0.0:

$ clang++ -march=native a.cpp

It prints:

$ ./a.out
3.14159
3.14062

Documentation about these intrinsics is available at:

https://software.intel.com/en-us/node/524287

https://clang.llvm.org/doxygen/f16cintrin_8h.html

ContentLicense: CC BY-SA 4.0


问:
REST interface for finding average
Suppose I want to create a REST interface to find the average of a list of numbers. Assume that the numbers are submitted one at a time. How would you do this?

POST a number to https://example.com/api/average

If this is the first number a hash will be returned

POST a number to https://example.com/api/average/hash
....

GET https://example.com/api/average/hash to find the average

DELETE https://example.com/api/average/hash since we don't need it any more

Is this the right way to do it? Any suggestions?

答:
It makes more sense to think of the list of numbers as the resource. Suppose each list's resource URL is /list/{id} where {id} is a placeholder for the list's ID. Then:

POST /list creates a new list, the server generates a list ID (or 'hash') and specifies the /list/{id} URL in the response's Location header.

POST /list/{id} adds a number to the list

GET /list/{id}/average returns the average

DELETE /list/{id} deletes the list.

An alternative to GET /list/{id}/average would be for GET /list/{id} to return the list as structured data, e.g. XML, that includes the average as a generated property.

OwnerDisplayName: Guruk

Title: REST interface for finding average

OwnerDisplayName: Peter Hilton

LastEditorDisplayName: Peter Hilton


问:
ADODB strange behavior
Recently I had a very strange problem.
The application is written in classic ASP, but I guess it is the same case for every connection that uses ADO/OLEDB.

Those are connection parameters.

```<code>conn=Server.CreateObject("ADODB.Connection");
conn.Provider="Microsoft.Jet.OLEDB.4.0";
conn.Open("D:/db/testingDb.mdb");
</code>```

In short this code:

```<code>conn.Open("myconnection");
bigQuery = "...";
rs = conn.execute(bigQuery);
while (!rs.eof) {
    ...
    smallQuery = "..."
    rssmall = conn.execute(smallQuery);
    ...
    rssmall.close();
    ...
    rs.movenext();
}
rs.close();
conn.close();
</code>```

Doesn't work if bigQuery returns more than certain number of rows (in my case ~20).
But if I use one more connection for inner loop like stealthyninja suggested:

```<code>conn.Open("myconnection");
conn2.Open("myconnection")

bigQuery = "...";
rs = conn.execute(bigQuery);
while (!rs.eof) {
    ...
    smallQuery = "..."
    rssmall = conn2.execute(smallQuery);
    ...
    rssmall.close();
    ...
    rs.movenext();
}
rs.close();
conn2.close();
conn.close();
</code>```

Problem vanishes.

I am using Access database and IIS7 if that matters.

Does anyone has a logical explanation for this?

答:
Michael Todd's comment has it.  ADODB doesn't support MARS (Multiple Active Result Sets) which is what you are trying to do.  The reason it seems to work with only 20 records is because that's how much it is initially transmitting to the client-side.

Standard solutions to this are

Retrieve the whole outer rowset into a holding structure or cache first, then process it and execute the inner queries, or

Use two different connections, as you have demonstrated.

Title: ADODB strange behavior

Body:
Recently I had a very strange problem.
The application is written in classic ASP, but I guess it is the same case for every connection that uses ADO/OLEDB.

Those are connection parameters.

conn=Server.CreateObject("ADODB.Connection");
conn.Provider="Microsoft.Jet.OLEDB.4.0";
conn.Open("D:/db/testingDb.mdb");

In short this code:

conn.Open("myconnection");
bigQuery = "...";
rs = conn.execute(bigQuery);
while (!rs.eof) {
    ...
    smallQuery = "..."
    rssmall = conn.execute(smallQuery);
    ...
    rssmall.close();
    ...
    rs.movenext();
}
rs.close();
conn.close();

Doesn't work if bigQuery returns more than certain number of rows (in my case ~20).
But if I use one more connection for inner loop like stealthyninja suggested:

conn.Open("myconnection");
conn2.Open("myconnection")

bigQuery = "...";
rs = conn.execute(bigQuery);
while (!rs.eof) {
    ...
    smallQuery = "..."
    rssmall = conn2.execute(smallQuery);
    ...
    rssmall.close();
    ...
    rs.movenext();
}
rs.close();
conn2.close();
conn.close();

Problem vanishes.

I am using Access database and IIS7 if that matters.

Does anyone has a logical explanation for this?


问:
Swift: determinate NSProgressIndicator, async refreshing and waiting for return
Working in Swift3; I've got a pretty expensive operation running in a loop iterating through stuff and building it into an array that on return would be used as the content for an NSTableView.

I wanted a modal sheet showing progress for this so people don't think the app is frozen. By googling, looking around in here and not a small amount of trial and error I've managed to implement my progressbar and have it show progress adequately as the loop progresses.

The problem right now? Even though the sheet (implemented as an NSAlert, the progress bar is in the accesory view) works exactly as expected, the whole thing returns before the loop is finished.

Here's the code, hoping somebody can tell me what am I doing wrong:

```<code>class ProgressBar: NSAlert {
    var progressBar = NSProgressIndicator()
    var totalItems: Double = 0
    var countItems: Double = 0
    override init() {
        progressBar.isIndeterminate = false
        progressBar.style = .barStyle
        super.init()
        self.messageText = ""
        self.informativeText = "Loading..."
        self.accessoryView = NSView(frame: NSRect(x:0, y:0, width: 290, height: 16))
        self.accessoryView?.addSubview(progressBar)
        self.layout()
        self.accessoryView?.setFrameOrigin(NSPoint(x:(self.accessoryView?.frame)!.minX,y:self.window.frame.maxY))
        self.addButton(withTitle: "")
        progressBar.sizeToFit()
        progressBar.setFrameSize(NSSize(width:290, height: 16))
        progressBar.usesThreadedAnimation = true
        self.beginSheetModal(for: ControllersRef.sharedInstance.thePrefPane!.mainCustomView.window!, completionHandler: nil)
    }
}

static var allUTIs: [SWDAContentItem] = {
    var wrappedUtis: [SWDAContentItem] = []
    let utis = LSWrappers.UTType.copyAllUTIs()

        let a = ProgressBar()

        a.totalItems = Double(utis.keys.count)
        a.progressBar.maxValue = a.totalItems

        DispatchQueue.global(qos: .default).async {
            for uti in Array(utis.keys) {
                a.countItems += 1.0
                wrappedUtis.append(SWDAContentItem(type:SWDAContentType(rawValue: "UTI")!, uti))
                Thread.sleep(forTimeInterval:0.0001)

                DispatchQueue.main.async {
                    a.progressBar.doubleValue = a.countItems
                    if (a.countItems >= a.totalItems && a.totalItems != 0) {
                        ControllersRef.sharedInstance.thePrefPane!.mainCustomView.window?.endSheet(a.window)
                    }
                }
            }
        }
    Swift.print("We'll return now...")
    return wrappedUtis // This returns before the loop is finished.
}()
</code>```

答:
In short, you're returning wrappedUtis before the asynchronous code has had a chance to finish. You cannot have the initialization closure return a value if the update process itself is happening asynchronously.

You clearly successfully diagnosed a performance problem in the initialization of allUTIs, and while doing this asynchronously is prudent, you shouldn't be doing that in that initialization block of the allUTIs property. Move this code that initiates the update of allUTIs into a separate function.

Looking at ProgressBar, it's really an alert, so I'd call it ProgressAlert to make that clear, but expose the necessary methods to update the NSProgressIndicator within that alert:

```<code>class ProgressAlert: NSAlert {
    private let progressBar = NSProgressIndicator()

    override init() {
        super.init()

        messageText = ""
        informativeText = "Loading..."
        accessoryView = NSView(frame: NSRect(x:0, y:0, width: 290, height: 16))
        accessoryView?.addSubview(progressBar)
        self.layout()
        accessoryView?.setFrameOrigin(NSPoint(x:(self.accessoryView?.frame)!.minX,y:self.window.frame.maxY))
        addButton(withTitle: "")

        progressBar.isIndeterminate = false
        progressBar.style = .barStyle
        progressBar.sizeToFit()
        progressBar.setFrameSize(NSSize(width:290, height: 16))
        progressBar.usesThreadedAnimation = true
    }

    /// Increment progress bar in this alert.

    func increment(by value: Double) {
        progressBar.increment(by: value)
    }

    /// Set/get `maxValue` for the progress bar in this alert

    var maxValue: Double {
        get {
            return progressBar.maxValue
        }
        set {
            progressBar.maxValue = newValue
        }
    }
}
</code>```

Note, this doesn't present the UI. That's the job of whomever presented it.

Then, rather than initiating this asynchronous population in the initialization closure (because initialization should always be synchronous), create a separate routine to populate it:

```<code>var allUTIs: [SWDAContentItem]?

private func populateAllUTIs(in window: NSWindow, completionHandler: @escaping () -> Void) {
    let progressAlert = ProgressAlert()
    progressAlert.beginSheetModal(for: window, completionHandler: nil)

    var wrappedUtis = [SWDAContentItem]()
    let utis = LSWrappers.UTType.copyAllUTIs()

    progressAlert.maxValue = Double(utis.keys.count)

    DispatchQueue.global(qos: .default).async {
        for uti in Array(utis.keys) {
            wrappedUtis.append(SWDAContentItem(type:SWDAContentType(rawValue: "UTI")!, uti))

            DispatchQueue.main.async { [weak progressAlert] in
                progressAlert?.increment(by: 1)
            }
        }
        DispatchQueue.main.async { [weak self, weak window] in
            self?.allUTIs = wrappedUtis
            window?.endSheet(progressAlert.window)
            completionHandler()
        }
    }
}
</code>```

Now, you declared allUTIs to be static, so you can tweak the above to do that, too, but it seems like it's more appropriate to make it an instance variable.

Anyway, you can then populate that array with something like:

```<code>populateAllUTIs(in: view.window!) {
    // do something
    print("done")
}
</code>```

Below, you said:

In practice, this means allUTIs is only actually initiated when the appropriate TabViewItem is selected for the first time (which is why I initialize it with a closure like that). So, I'm not really sure how to refactor this, or where should I move the actual initialization. Please keep in mind that I'm pretty much a newbie; this is my first Swift (also Cocoa) project, and I've been learning both for a couple of weeks.

If you want to instantiate this when the tab is selected, then hook into the child view controllers viewDidLoad. Or you can do it in the tab view controller's tabView(_:didSelect:)

But if the population of allUTIs is so slow, are you sure you want to do this lazily? Why not trigger this instantiation sooner, so that there's less likely to be a delay when the user selects that tab. In that case, you might trigger it the tab view controller's own viewDidLoad, so that the tab that needs those UTIs is more likely to have them.

So, if I were considering a more radical redesign, I might first change my model object to further isolate its update process from any specific UI, but rather to simply return (and update) a Progress object.

```<code>class Model {

    var allUTIs: [SWDAContentItem]?

    func startUTIRetrieval(completionHandler: (() -> Void)? = nil) -> Progress {
        var wrappedUtis = [SWDAContentItem]()
        let utis = LSWrappers.UTType.copyAllUTIs()

        let progress = Progress(totalUnitCount: Int64(utis.keys.count))

        DispatchQueue.global(qos: .default).async {
            for uti in Array(utis.keys) {
                wrappedUtis.append(SWDAContentItem(type:SWDAContentType(rawValue: "UTI")!, uti))

                DispatchQueue.main.async {
                    progress.completedUnitCount += 1
                }
            }

            DispatchQueue.main.async { [weak self] in
                self?.allUTIs = wrappedUtis
                completionHandler?()
            }
        }

        return progress
    }

}
</code>```

Then, I might have the tab bar controller instantiate this and share the progress with whatever view controller needed it:

```<code>class TabViewController: NSTabViewController {

    var model: Model!

    var progress: Progress?

    override func viewDidLoad() {
        super.viewDidLoad()

        model = Model()
        progress = model.startUTIRetrieval()

        tabView.delegate = self
    }

    override func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
        super.tabView(tabView, didSelect: tabViewItem)

        if let item = tabViewItem, let controller = childViewControllers[tabView.indexOfTabViewItem(item)] as? ViewController {
            controller.progress = progress
        }
    }

}
</code>```

Then the view controller could observe this Progress object, to figure out whether it needs to update its UI to reflect this:

```<code>class ViewController: NSViewController {

    weak var progress: Progress? { didSet { startObserving() } }

    weak var progressAlert: ProgressAlert?

    private var observerContext = 0

    private func startObserving() {
        guard let progress = progress, progress.completedUnitCount < progress.totalUnitCount else { return }

        let alert = ProgressAlert()
        alert.beginSheetModal(for: view.window!)
        progressAlert = alert
        progress.addObserver(self, forKeyPath: "fractionCompleted", context: &observerContext)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        guard let progress = object as? Progress, context == &observerContext else {
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
            return
        }

        dispatchPrecondition(condition: .onQueue(.main))

        if progress.completedUnitCount < progress.totalUnitCount {
            progressAlert?.doubleValue = progress.fractionCompleted * 100
        } else {
            progress.removeObserver(self, forKeyPath: "fractionCompleted")
            view.window?.endSheet(progressAlert!.window)
        }
    }

    deinit {
        progress?.removeObserver(self, forKeyPath: "fractionCompleted")
    }

}
</code>```

And, in this case, the ProgressAlert only would worry about doubleValue:

```<code>class ProgressAlert: NSAlert {
    private let progressBar = NSProgressIndicator()

    override init() {
        super.init()

        messageText = ""
        informativeText = "Loading..."
        accessoryView = NSView(frame: NSRect(x:0, y:0, width: 290, height: 16))
        accessoryView?.addSubview(progressBar)
        self.layout()
        accessoryView?.setFrameOrigin(NSPoint(x:(self.accessoryView?.frame)!.minX,y:self.window.frame.maxY))
        addButton(withTitle: "")

        progressBar.isIndeterminate = false
        progressBar.style = .barStyle
        progressBar.sizeToFit()
        progressBar.setFrameSize(NSSize(width: 290, height: 16))
        progressBar.usesThreadedAnimation = true
    }

    /// Set/get `maxValue` for the progress bar in this alert

    var doubleValue: Double {
        get {
            return progressBar.doubleValue
        }
        set {
            progressBar.doubleValue = newValue
        }
    }
}
</code>```

I must note, though, that if these UTIs are only needed for that one tab, it raises the question as to whether you should be using a NSAlert based UI at all. The alert blocks the whole window, and you may want to block interaction with only that one tab.

Body:
Working in Swift3; I've got a pretty expensive operation running in a loop iterating through stuff and building it into an array that on return would be used as the content for an NSTableView.

I wanted a modal sheet showing progress for this so people don't think the app is frozen. By googling, looking around in here and not a small amount of trial and error I've managed to implement my progressbar and have it show progress adequately as the loop progresses.

The problem right now? Even though the sheet (implemented as an NSAlert, the progress bar is in the accesory view) works exactly as expected, the whole thing returns before the loop is finished.

Here's the code, hoping somebody can tell me what am I doing wrong:

class ProgressBar: NSAlert {
    var progressBar = NSProgressIndicator()
    var totalItems: Double = 0
    var countItems: Double = 0
    override init() {
        progressBar.isIndeterminate = false
        progressBar.style = .barStyle
        super.init()
        self.messageText = ""
        self.informativeText = "Loading..."
        self.accessoryView = NSView(frame: NSRect(x:0, y:0, width: 290, height: 16))
        self.accessoryView?.addSubview(progressBar)
        self.layout()
        self.accessoryView?.setFrameOrigin(NSPoint(x:(self.accessoryView?.frame)!.minX,y:self.window.frame.maxY))
        self.addButton(withTitle: "")
        progressBar.sizeToFit()
        progressBar.setFrameSize(NSSize(width:290, height: 16))
        progressBar.usesThreadedAnimation = true
        self.beginSheetModal(for: ControllersRef.sharedInstance.thePrefPane!.mainCustomView.window!, completionHandler: nil)
    }
}

static var allUTIs: [SWDAContentItem] = {
    var wrappedUtis: [SWDAContentItem] = []
    let utis = LSWrappers.UTType.copyAllUTIs()

        let a = ProgressBar()

        a.totalItems = Double(utis.keys.count)
        a.progressBar.maxValue = a.totalItems

        DispatchQueue.global(qos: .default).async {
            for uti in Array(utis.keys) {
                a.countItems += 1.0
                wrappedUtis.append(SWDAContentItem(type:SWDAContentType(rawValue: "UTI")!, uti))
                Thread.sleep(forTimeInterval:0.0001)

                DispatchQueue.main.async {
                    a.progressBar.doubleValue = a.countItems
                    if (a.countItems >= a.totalItems && a.totalItems != 0) {
                        ControllersRef.sharedInstance.thePrefPane!.mainCustomView.window?.endSheet(a.window)
                    }
                }
            }
        }
    Swift.print("We'll return now...")
    return wrappedUtis // This returns before the loop is finished.
}()

Body:
In short, you're returning wrappedUtis before the asynchronous code has had a chance to finish. You cannot have the initialization closure return a value if the update process itself is happening asynchronously.

You clearly successfully diagnosed a performance problem in the initialization of allUTIs, and while doing this asynchronously is prudent, you shouldn't be doing that in that initialization block of the allUTIs property. Move this code that initiates the update of allUTIs into a separate function.

Looking at ProgressBar, it's really an alert, so I'd call it ProgressAlert to make that clear, but expose the necessary methods to update the NSProgressIndicator within that alert:

class ProgressAlert: NSAlert {
    private let progressBar = NSProgressIndicator()

    override init() {
        super.init()

        messageText = ""
        informativeText = "Loading..."
        accessoryView = NSView(frame: NSRect(x:0, y:0, width: 290, height: 16))
        accessoryView?.addSubview(progressBar)
        self.layout()
        accessoryView?.setFrameOrigin(NSPoint(x:(self.accessoryView?.frame)!.minX,y:self.window.frame.maxY))
        addButton(withTitle: "")

        progressBar.isIndeterminate = false
        progressBar.style = .barStyle
        progressBar.sizeToFit()
        progressBar.setFrameSize(NSSize(width:290, height: 16))
        progressBar.usesThreadedAnimation = true
    }

    /// Increment progress bar in this alert.

    func increment(by value: Double) {
        progressBar.increment(by: value)
    }

    /// Set/get `maxValue` for the progress bar in this alert

    var maxValue: Double {
        get {
            return progressBar.maxValue
        }
        set {
            progressBar.maxValue = newValue
        }
    }
}

Note, this doesn't present the UI. That's the job of whomever presented it.

Then, rather than initiating this asynchronous population in the initialization closure (because initialization should always be synchronous), create a separate routine to populate it:

var allUTIs: [SWDAContentItem]?

private func populateAllUTIs(in window: NSWindow, completionHandler: @escaping () -> Void) {
    let progressAlert = ProgressAlert()
    progressAlert.beginSheetModal(for: window, completionHandler: nil)

    var wrappedUtis = [SWDAContentItem]()
    let utis = LSWrappers.UTType.copyAllUTIs()

    progressAlert.maxValue = Double(utis.keys.count)

    DispatchQueue.global(qos: .default).async {
        for uti in Array(utis.keys) {
            wrappedUtis.append(SWDAContentItem(type:SWDAContentType(rawValue: "UTI")!, uti))

            DispatchQueue.main.async { [weak progressAlert] in
                progressAlert?.increment(by: 1)
            }
        }
        DispatchQueue.main.async { [weak self, weak window] in
            self?.allUTIs = wrappedUtis
            window?.endSheet(progressAlert.window)
            completionHandler()
        }
    }
}

Now, you declared allUTIs to be static, so you can tweak the above to do that, too, but it seems like it's more appropriate to make it an instance variable.

Anyway, you can then populate that array with something like:

populateAllUTIs(in: view.window!) {
    // do something
    print("done")
}

Below, you said:

In practice, this means allUTIs is only actually initiated when the appropriate TabViewItem is selected for the first time (which is why I initialize it with a closure like that). So, I'm not really sure how to refactor this, or where should I move the actual initialization. Please keep in mind that I'm pretty much a newbie; this is my first Swift (also Cocoa) project, and I've been learning both for a couple of weeks.

If you want to instantiate this when the tab is selected, then hook into the child view controllers viewDidLoad. Or you can do it in the tab view controller's tabView(_:didSelect:)

But if the population of allUTIs is so slow, are you sure you want to do this lazily? Why not trigger this instantiation sooner, so that there's less likely to be a delay when the user selects that tab. In that case, you might trigger it the tab view controller's own viewDidLoad, so that the tab that needs those UTIs is more likely to have them.

So, if I were considering a more radical redesign, I might first change my model object to further isolate its update process from any specific UI, but rather to simply return (and update) a Progress object.

class Model {

    var allUTIs: [SWDAContentItem]?

    func startUTIRetrieval(completionHandler: (() -> Void)? = nil) -> Progress {
        var wrappedUtis = [SWDAContentItem]()
        let utis = LSWrappers.UTType.copyAllUTIs()

        let progress = Progress(totalUnitCount: Int64(utis.keys.count))

        DispatchQueue.global(qos: .default).async {
            for uti in Array(utis.keys) {
                wrappedUtis.append(SWDAContentItem(type:SWDAContentType(rawValue: "UTI")!, uti))

                DispatchQueue.main.async {
                    progress.completedUnitCount += 1
                }
            }

            DispatchQueue.main.async { [weak self] in
                self?.allUTIs = wrappedUtis
                completionHandler?()
            }
        }

        return progress
    }

}

Then, I might have the tab bar controller instantiate this and share the progress with whatever view controller needed it:

class TabViewController: NSTabViewController {

    var model: Model!

    var progress: Progress?

    override func viewDidLoad() {
        super.viewDidLoad()

        model = Model()
        progress = model.startUTIRetrieval()

        tabView.delegate = self
    }

    override func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
        super.tabView(tabView, didSelect: tabViewItem)

        if let item = tabViewItem, let controller = childViewControllers[tabView.indexOfTabViewItem(item)] as? ViewController {
            controller.progress = progress
        }
    }

}

Then the view controller could observe this Progress object, to figure out whether it needs to update its UI to reflect this:

class ViewController: NSViewController {

    weak var progress: Progress? { didSet { startObserving() } }

    weak var progressAlert: ProgressAlert?

    private var observerContext = 0

    private func startObserving() {
        guard let progress = progress, progress.completedUnitCount < progress.totalUnitCount else { return }

        let alert = ProgressAlert()
        alert.beginSheetModal(for: view.window!)
        progressAlert = alert
        progress.addObserver(self, forKeyPath: "fractionCompleted", context: &observerContext)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        guard let progress = object as? Progress, context == &observerContext else {
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
            return
        }

        dispatchPrecondition(condition: .onQueue(.main))

        if progress.completedUnitCount < progress.totalUnitCount {
            progressAlert?.doubleValue = progress.fractionCompleted * 100
        } else {
            progress.removeObserver(self, forKeyPath: "fractionCompleted")
            view.window?.endSheet(progressAlert!.window)
        }
    }

    deinit {
        progress?.removeObserver(self, forKeyPath: "fractionCompleted")
    }

}

And, in this case, the ProgressAlert only would worry about doubleValue:

class ProgressAlert: NSAlert {
    private let progressBar = NSProgressIndicator()

    override init() {
        super.init()

        messageText = ""
        informativeText = "Loading..."
        accessoryView = NSView(frame: NSRect(x:0, y:0, width: 290, height: 16))
        accessoryView?.addSubview(progressBar)
        self.layout()
        accessoryView?.setFrameOrigin(NSPoint(x:(self.accessoryView?.frame)!.minX,y:self.window.frame.maxY))
        addButton(withTitle: "")

        progressBar.isIndeterminate = false
        progressBar.style = .barStyle
        progressBar.sizeToFit()
        progressBar.setFrameSize(NSSize(width: 290, height: 16))
        progressBar.usesThreadedAnimation = true
    }

    /// Set/get `maxValue` for the progress bar in this alert

    var doubleValue: Double {
        get {
            return progressBar.doubleValue
        }
        set {
            progressBar.doubleValue = newValue
        }
    }
}

I must note, though, that if these UTIs are only needed for that one tab, it raises the question as to whether you should be using a NSAlert based UI at all. The alert blocks the whole window, and you may want to block interaction with only that one tab.


问:
xUnit test project connection string
I would like to know the recommended approach to getting a connection string from a config file for my xUnit .net core test project.

I have set up a test project using the new Visual Studio 2017 xUnit Test project template for .net core.  This project will run my integration tests that reference 2 different .net core class library projects - one of which will talk to the database using EF Core.

I understand that normally the connection string should not be set or accessed in a class library project - it should be the application that consumes the class library that should set the connection string.

However, in this case it appears that the xUnit test project is treated somewhat like a class library project.  I have not seen any examples of how to set up some sort of config file and access that from the test project.  How do I access  the connection string from a config file so that my test project can consume my Datalayer class library project and pass in the appropriate connection string?

答:
I was able to access the connection string from my xUnit test project by creating a DbOptionsFactory class that returns a DbContextOptions object initialized with a connection string that it reads from an appsettings.json configuration file.

This requires a dependency on Microsoft.Extensions.Configuration

```<code>public static class DbOptionsFactory
{
    static DbOptionsFactory()
    {
        var config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();
        var connectionString = config["Data:DefaultConnection:ConnectionString"];

        DbContextOptions = new DbContextOptionsBuilder<MyDbContext>()
            .UseSqlServer(connectionString)
            .Options;
    }

    public static DbContextOptions<MyDbContext> DbContextOptions { get; }

}
</code>```

appsettings.json

```<code>{
  "Data": {
  "DefaultConnection": {
      "Name": "MyDbContext",
      "ConnectionString": "connection string goes here"
   }
 }
}
</code>```

When instantiating my DbContext I pass in the optionsBuilder object that has the connection string from the configuration file like so:

```<code>using (var context = new MyDbContext(DbOptionsFactory.DbContextOptions))
{
     // access db here
}
</code>```

Hope this helps anyone else that runs into the same issue.

Title: xUnit test project connection string

Body:
I was able to access the connection string from my xUnit test project by creating a DbOptionsFactory class that returns a DbContextOptions object initialized with a connection string that it reads from an appsettings.json configuration file.

This requires a dependency on Microsoft.Extensions.Configuration

public static class DbOptionsFactory
{
    static DbOptionsFactory()
    {
        var config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();
        var connectionString = config["Data:DefaultConnection:ConnectionString"];

        DbContextOptions = new DbContextOptionsBuilder<MyDbContext>()
            .UseSqlServer(connectionString)
            .Options;
    }

    public static DbContextOptions<MyDbContext> DbContextOptions { get; }

}

appsettings.json

{
  "Data": {
  "DefaultConnection": {
      "Name": "MyDbContext",
      "ConnectionString": "connection string goes here"
   }
 }
}

When instantiating my DbContext I pass in the optionsBuilder object that has the connection string from the configuration file like so:

using (var context = new MyDbContext(DbOptionsFactory.DbContextOptions))
{
     // access db here
}

Hope this helps anyone else that runs into the same issue.


问:
Tagging and Training NER dataset
I have a data set and I want to tag it for Named Entity Recognition. My dataset is in Persian.
I want to know how should I tag expressions like :

*** آقای مهدی کاظمی  = Mr Mehdi Kazemi / Mr will Smith. >>> (names with titles) should I tag all as a person or just the first name and last name should be tagged? (I mean should i also tag "Mr")

Mr  >>  b_per     ||     Mr >> O

Mehdi  >>   i_per || Mehdi >> b_per

Kazemi  >> i_per || Kazemi >> i_per

*** بیمارستان نور = Noor hospital >>> Should I tag the name only or the name and hospital both as Named Entity?

*** Eiffel tower / The Ministry of Defense (I mean the us DOD for example) >>> in Persian it is called :
وزارت دفاع    (vezarate defa)
should I only tag Defense ? or all together?

There are many more examples for schools, movies, cities, countries and.... since we use the entity class before the named entity.

I would appreciate if you can help me with tagging this dataset.

答:
I'll give you some examples from the CoNLL 2003 training data:

"Mr." is not tagged as part of the person, so titles are ignored.

"Columbia Presbyterian Hospital" is tagged as (LOC, LOC, LOC)

"a New York hospital" (O, LOC, LOC, O)

"Ministry of Commerce" is (ORG, ORG, ORG)

I think "Eiffel Tower" should be (LOC, LOC)

Title: Tagging and Training NER dataset


问:
Any tools or methods to export SQL queries of oracle biee reports into plain text files?
Toad has built-in method (Database|Export|Export DDL) to export all objects (i.e. tables, triggers, functions, stored procedures, packages etc) to plain text files

One of my project has all reports written in Oracle BIEE publisher.  I would like to export all data models of these reports into plain text so that I could import them into an Eclipse project for ease of searching of some strings or sql patterns.

Is there any method or tool of achieving this?

Thank you very much.

答:
Here's what you need to do:

While logged into xmlpserver (BI Publisher), enter the Catalog, then do the following:

Download the parent folder containing all the data models you will need.

Once downloaded, rename the downloaded .xdmz file to a .zip.
You can now view the "guts" of the files.

Write a script in Linux or Windows to recursively rename as .ZIP all the nested XDMZ files underneath the parent folder.

Extract all of the _datamodel.xdm files underneath the data models' XDMZ files (now renamed as ZIP files).

Below is a sample _datamodel.xdm file. (Scroll beneath it for steps 5, etc.)

Note that the Data Model's Data Set SQL is located between the <sql></sql>tags:

```<code>   <sql dataSourceRef="ApplicationDB_APP">
      <![CDATA[select * from table]]>
   </sql>
</code>```

Sample _datamodel.xdm file:

```<code><?xml version = '1.0' encoding = 'utf-8'?>
<dataModel xmlns="http://xmlns.oracle.com/oxp/xmlp" version="2.0" xmlns:xdm="http://xmlns.oracle.com/oxp/xmlp" xmlns:xsd="http://wwww.w3.org/2001/XMLSchema" defaultDataSourceRef="demo">
   <dataProperties>
      <property name="include_parameters" value="true"/>
      <property name="include_null_Element" value="false"/>
      <property name="include_rowsettag" value="false"/>
      <property name="xml_tag_case" value="upper"/>
   </dataProperties>
   <dataSets>
      <dataSet name="asd" type="complex">
         <sql dataSourceRef="ApplicationDB_APP">
            <![CDATA[select * from table]]>
         </sql>
      </dataSet>
   </dataSets>
   <output rootName="DATA_DS" uniqueRowName="false">
      <nodeList name="data-structure">
         <dataStructure tagName="DATA_DS">
            <group name="G_1" label="G_1" source="asd">
               <element name="ENTITY_ID" value="ENTITY_ID" label="ENTITY_ID" dataType="xsd:double" breakOrder="" fieldOrder="1"/>
               <element name="ENTITY_NUMBER" value="ENTITY_NUMBER" label="ENTITY_NUMBER" dataType="xsd:string" breakOrder="" fieldOrder="2"/>
**[...more XML defining the table generated by the SQL...]**
            </group>
         </dataStructure>
      </nodeList>
   </output>
   <eventTriggers/>
   <lexicals/>
   <valueSets/>
   <parameters/>
   <bursting/>
   <display>
      <layouts>
         <layout name="asd" left="280px" top="0px"/>
         <layout name="DATA_DS" left="0px" top="32px"/>
      </layouts>
      <groupLinks/>
   </display>
</dataModel>
</code>```

Now you need to parse these files to extract the SQL. (Use XPATH).

Or rather than writing a script to do this, you could do each file individually using copy/paste into your Eclipse.

I hope this helps.

Body:
Here's what you need to do:

While logged into xmlpserver (BI Publisher), enter the Catalog, then do the following:

Download the parent folder containing all the data models you will need.

Once downloaded, rename the downloaded .xdmz file to a .zip.
You can now view the "guts" of the files.

Write a script in Linux or Windows to recursively rename as .ZIP all the nested XDMZ files underneath the parent folder.

Extract all of the _datamodel.xdm files underneath the data models' XDMZ files (now renamed as ZIP files).

Below is a sample _datamodel.xdm file. (Scroll beneath it for steps 5, etc.)

Note that the Data Model's Data Set SQL is located between the <sql></sql>tags:

   <sql dataSourceRef="ApplicationDB_APP">
      <![CDATA[select * from table]]>
   </sql>

Sample _datamodel.xdm file:

<?xml version = '1.0' encoding = 'utf-8'?>
<dataModel xmlns="http://xmlns.oracle.com/oxp/xmlp" version="2.0" xmlns:xdm="http://xmlns.oracle.com/oxp/xmlp" xmlns:xsd="http://wwww.w3.org/2001/XMLSchema" defaultDataSourceRef="demo">
   <dataProperties>
      <property name="include_parameters" value="true"/>
      <property name="include_null_Element" value="false"/>
      <property name="include_rowsettag" value="false"/>
      <property name="xml_tag_case" value="upper"/>
   </dataProperties>
   <dataSets>
      <dataSet name="asd" type="complex">
         <sql dataSourceRef="ApplicationDB_APP">
            <![CDATA[select * from table]]>
         </sql>
      </dataSet>
   </dataSets>
   <output rootName="DATA_DS" uniqueRowName="false">
      <nodeList name="data-structure">
         <dataStructure tagName="DATA_DS">
            <group name="G_1" label="G_1" source="asd">
               <element name="ENTITY_ID" value="ENTITY_ID" label="ENTITY_ID" dataType="xsd:double" breakOrder="" fieldOrder="1"/>
               <element name="ENTITY_NUMBER" value="ENTITY_NUMBER" label="ENTITY_NUMBER" dataType="xsd:string" breakOrder="" fieldOrder="2"/>
**[...more XML defining the table generated by the SQL...]**
            </group>
         </dataStructure>
      </nodeList>
   </output>
   <eventTriggers/>
   <lexicals/>
   <valueSets/>
   <parameters/>
   <bursting/>
   <display>
      <layouts>
         <layout name="asd" left="280px" top="0px"/>
         <layout name="DATA_DS" left="0px" top="32px"/>
      </layouts>
      <groupLinks/>
   </display>
</dataModel>

Now you need to parse these files to extract the SQL. (Use XPATH).

Or rather than writing a script to do this, you could do each file individually using copy/paste into your Eclipse.

I hope this helps.


问:
NumPy: better way to multiply a matrix by an array in-place?
I'm doing some work with with NumPy arrays, but occasionally I need to multiply them by arrays.

Right now, I'm doing something like:

```<code>rotation_matrix = np.matrix([ ... ])
for vector in vectors:
    rotated_vec_mat = vector.T * rotation_matrix
    vector[:] = np.array(rotated_vec_mat)[0]
</code>```

But that's ugly (and slow?).

Is there a cleaner way of doing it?

答:
Might make more sense to do this:

```<code>vector_arr = np.concatenate([vector[np.newaxis, :] for vector in vectors], axis=0)
rotated_vector_arr = np.dot(vector_arr, rotation_matrix)
</code>```

Then the rows of rotated_vector_arr are what you want them to be. You can treat the whole thing as one matrix product and have the looping done in C/Fortran by the BLAS library.

There's no need to use the matrix() class to do matrix multiplication, arrays work fine. matrix() overloads the * operator but I find it just confuses things.

Body:
I'm doing some work with with NumPy arrays, but occasionally I need to multiply them by arrays.

Right now, I'm doing something like:

rotation_matrix = np.matrix([ ... ])
for vector in vectors:
    rotated_vec_mat = vector.T * rotation_matrix
    vector[:] = np.array(rotated_vec_mat)[0]

But that's ugly (and slow?).

Is there a cleaner way of doing it?

Body:
Might make more sense to do this:

vector_arr = np.concatenate([vector[np.newaxis, :] for vector in vectors], axis=0)
rotated_vector_arr = np.dot(vector_arr, rotation_matrix)

Then the rows of rotated_vector_arr are what you want them to be. You can treat the whole thing as one matrix product and have the looping done in C/Fortran by the BLAS library.

There's no need to use the matrix() class to do matrix multiplication, arrays work fine. matrix() overloads the * operator but I find it just confuses things.


问:
How to use a regex for the field separator in AWK?
I read another answer that show how one can set the field separator using the -F flag:

```<code>awk -F 'INFORMATION DATA ' '{print $2}' t
</code>```

Now I'm curious how I can use a regex for the field separator. My attempt can be seen below:

```<code>$ echo "1 2 foo\n2 3 bar\n42 2 baz"
1 2 foo
2 3 bar
42 2 baz
$ echo "1 2 foo\n2 3 bar\n42 2 baz" | awk -F '\d+ \d+ ' '{ print $2 }'
# 3 blank lines
</code>```

I was expecting to get the following output:

```<code>foo
bar
baz
</code>```

This is because my regex \d+ \d+ matches "the first 2 numbers separated by a space, followed by a space". But I'm printing the second record. As shown on rubular:

How do I use a regex as the awk field separator?

答:
Just replace \d with [0-9]:

With this you can print all the fields and you can see the fields immediatelly:

```<code>$ echo -e "1 2 foo\n2 3 bar\n42 2 baz" |awk -v FS="[0-9]+ [0-9]+" '{for (k=1;k<=NF;k++) print k,$k}'
1
2  foo
1
2  bar
1
2  baz
</code>```

So just use [0-9] in your command:

```<code>$ echo -e "1 2 foo\n2 3 bar\n42 2 baz" |awk -v FS="[0-9]+ [0-9]+" '{print $2}'
 foo
 bar
 baz
</code>```

Body:
I read another answer that show how one can set the field separator using the -F flag:

awk -F 'INFORMATION DATA ' '{print $2}' t

Now I'm curious how I can use a regex for the field separator. My attempt can be seen below:

$ echo "1 2 foo\n2 3 bar\n42 2 baz"
1 2 foo
2 3 bar
42 2 baz
$ echo "1 2 foo\n2 3 bar\n42 2 baz" | awk -F '\d+ \d+ ' '{ print $2 }'
# 3 blank lines

I was expecting to get the following output:

foo
bar
baz

This is because my regex \d+ \d+ matches "the first 2 numbers separated by a space, followed by a space". But I'm printing the second record. As shown on rubular:

How do I use a regex as the awk field separator?

Body:
Just replace \d with [0-9]:

With this you can print all the fields and you can see the fields immediatelly:

$ echo -e "1 2 foo\n2 3 bar\n42 2 baz" |awk -v FS="[0-9]+ [0-9]+" '{for (k=1;k<=NF;k++) print k,$k}'
1
2  foo
1
2  bar
1
2  baz

So just use [0-9] in your command:

$ echo -e "1 2 foo\n2 3 bar\n42 2 baz" |awk -v FS="[0-9]+ [0-9]+" '{print $2}'
 foo
 bar
 baz


问:
how to write a function in bash_profile
how can we write a simple regular function which i can put in my bashprofile
which can be used to secure console to any host i want.

but my secure console has to go through a jump host. that is the issue.

```<code>function func_name () {
 ssh jumphostname;
 sc $hostname # from jump host secure console to another host given as input from terminal
}
</code>```

this function only making to login in to jump host but not to secureconsole in to another host from there.

-bash-4.1$func_name host.me.com
should give me console to host.me.com via jumphost

is function for this not possible?
do i have to write a script?

答:
Its because you dont leave a whitespace between the if and the [...
The the correct sintax you want is...

```<code>function con.sole
{
    if [ ssh jumphostname ]; then
        sc $1;
    else
        echo "host not available"
    fi
}
</code>```

Greetings from Mexico!

Title: how to write a function in bash_profile

Body:
how can we write a simple regular function which i can put in my bashprofile
which can be used to secure console to any host i want.

but my secure console has to go through a jump host. that is the issue.

function func_name () {
 ssh jumphostname;
 sc $hostname # from jump host secure console to another host given as input from terminal
}

this function only making to login in to jump host but not to secureconsole in to another host from there.

-bash-4.1$func_name host.me.com
should give me console to host.me.com via jumphost

is function for this not possible?
do i have to write a script?

Body:
Its because you dont leave a whitespace between the if and the [...
The the correct sintax you want is...

function con.sole
{
    if [ ssh jumphostname ]; then
        sc $1;
    else
        echo "host not available"
    fi
}

Greetings from Mexico!


问:
jQGrid, how to make a column editable in the add dialog but not during (inline) edits
I have a jQGrid with a column that I only want to be editable when adding a new row.

I've seen examples of how to do this when edits and adds are both happening in a dialog but is there a way to do this with in-line editing?

I've tried using grid.setColProp() in beforeShowForm:, but this doesn't work ( the column remains read only and is not present in the add dialog).

Example of dialog based column enable/disable:

http://www.ok-soft-gmbh.com/jqGrid/CustomFormEdit.htm

答:
Because you use the example from my old answers (this and this) I feel that I should answer also on your question.

In the old example all fields, which can be modified during Add or Edit dialogs, has property  editable:true. The fields which should be shown only in the Add dialog will be made hidden inside of beforeShowForm event handle. In the same way we can temporary switch some fields to editable:false before call of the editRow method and reset back to the editable:true immediately after the call:

```<code>onSelectRow: function(id) {
    if (id && id !== lastSel) {
        grid.jqGrid('restoreRow',lastSel);
        var cm = grid.jqGrid('getColProp','Name');
        cm.editable = false;
        grid.jqGrid('editRow', id, true, null, null, 'clientArray');
        cm.editable = true;
        lastSel = id;
    }
}
</code>```

You can see this live here.

UPDATE: Free jqGrid allows to define editable as callback function. See the wiki article. It allows to make the column editable in some rows and holding non-editable for other rows.

Body:
Because you use the example from my old answers (this and this) I feel that I should answer also on your question.

In the old example all fields, which can be modified during Add or Edit dialogs, has property  editable:true. The fields which should be shown only in the Add dialog will be made hidden inside of beforeShowForm event handle. In the same way we can temporary switch some fields to editable:false before call of the editRow method and reset back to the editable:true immediately after the call:

onSelectRow: function(id) {
    if (id && id !== lastSel) {
        grid.jqGrid('restoreRow',lastSel);
        var cm = grid.jqGrid('getColProp','Name');
        cm.editable = false;
        grid.jqGrid('editRow', id, true, null, null, 'clientArray');
        cm.editable = true;
        lastSel = id;
    }
}

You can see this live here.

UPDATE: Free jqGrid allows to define editable as callback function. See the wiki article. It allows to make the column editable in some rows and holding non-editable for other rows.


问:
In Matplotlib, how can I clear an axes' contents without erasing its axis labels?
Is there an alternative to axes.clear() that leaves the axis labels untouched, while erasing the axes' contents?

Context:
I have an interactive script that flips through some flow images, and for each image, plots it using axes.quiver(). If I don't call axes.clear() between calls to axes.quiver(), each quiver() call just adds more arrows to the plot without first erasing the previously added arrows. However, when I call axes.clear(), it nukes the axes labels. I can re-set them, but it's a bit annoying.

答:
You can remove the artists from an axes using  the remove() of the artists. Below is a code showing two options to do so.

```<code>import matplotlib.pyplot as plt
import numpy as np

X, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))
U = np.cos(X)
V = np.sin(Y)

plt.figure()
plt.title('Arrows scale with plot width, not view')
plt.xlabel('xlabel')
plt.xlabel('ylabel')

Q = plt.quiver(X, Y, U, V, units='width')
l, = plt.plot(X[0,:], U[4,:]+2)

# option 1, remove single artists
#Q.remove()
#l.remove()

# option 2, remove all lines and collections
for artist in plt.gca().lines + plt.gca().collections:
    artist.remove()

plt.show()
</code>```

Body:
You can remove the artists from an axes using  the remove() of the artists. Below is a code showing two options to do so.

import matplotlib.pyplot as plt
import numpy as np

X, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))
U = np.cos(X)
V = np.sin(Y)

plt.figure()
plt.title('Arrows scale with plot width, not view')
plt.xlabel('xlabel')
plt.xlabel('ylabel')

Q = plt.quiver(X, Y, U, V, units='width')
l, = plt.plot(X[0,:], U[4,:]+2)

# option 1, remove single artists
#Q.remove()
#l.remove()

# option 2, remove all lines and collections
for artist in plt.gca().lines + plt.gca().collections:
    artist.remove()

plt.show()


问:
How to import leaflet-routing-machine into an Ionic2 project?
I don't quite get how to import LRM into a ts file. After installing via npm install leaflet-routing-machine, I defined Routing like this:

```<code>var Routing = require('leaflet-routing-machine');
var newRoute = Routing.control({Options});
</code>```

which didn't help me and I got:

```<code>Error caused by: Routing.control is not a function
</code>```

Here is my Ionic information:

```<code>Cordova CLI: 6.4.0
Ionic Framework Version: 2.0.1
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.1.0
Node Version: v6.3.1
</code>```

BTW, I don't have any problem with leaflet itself.

答:
We solved this problem by adding the following line before declaring our component.

declare var L: any;

myclass.component.ts

```<code>import { Component, OnInit } from '@angular/core';
...

// Leaflet and Leaflet Routing Machine have been installed with npm
import 'leaflet-routing-machine';
import 'leaflet-easybutton';

// Add this line to remove typescript errors
declare var L: any;

@Component({
  ...
})
export class MyClass extends OnInit {
  ...

  constructor(...) {
    ...
  }

  ngOnInit() {

    ...

    // The example snippet is now working
    L.Routing.control({
      waypoints: [
        L.latLng(57.74, 11.94),
        L.latLng(57.6792, 11.949)
      ]
    }).addTo(myMap);

    ...

  }

  ...

}
</code>```

As mentioned in this post, typescript seems to be causing problems to add properties to the global L object of Leaflet, but in our case declaring L of type any was enough to make it work.

Body:
I don't quite get how to import LRM into a ts file. After installing via npm install leaflet-routing-machine, I defined Routing like this:

var Routing = require('leaflet-routing-machine');
var newRoute = Routing.control({Options});

which didn't help me and I got:

Error caused by: Routing.control is not a function

Here is my Ionic information:

Cordova CLI: 6.4.0
Ionic Framework Version: 2.0.1
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.1.0
Node Version: v6.3.1

BTW, I don't have any problem with leaflet itself.

Body:
We solved this problem by adding the following line before declaring our component.

declare var L: any;

myclass.component.ts

import { Component, OnInit } from '@angular/core';
...

// Leaflet and Leaflet Routing Machine have been installed with npm
import 'leaflet-routing-machine';
import 'leaflet-easybutton';

// Add this line to remove typescript errors
declare var L: any;

@Component({
  ...
})
export class MyClass extends OnInit {
  ...

  constructor(...) {
    ...
  }

  ngOnInit() {

    ...

    // The example snippet is now working
    L.Routing.control({
      waypoints: [
        L.latLng(57.74, 11.94),
        L.latLng(57.6792, 11.949)
      ]
    }).addTo(myMap);

    ...

  }

  ...

}

As mentioned in this post, typescript seems to be causing problems to add properties to the global L object of Leaflet, but in our case declaring L of type any was enough to make it work.


答:
First of all echo doesn't auto escape and outputs a literal \n. So you'll need to add -e to enable escapes. Second of all awk doesn't support \d so you have to use [0-9] or [[:digit:]].

```<code>echo -e "1 2 foo\n2 3 bar\n42 2 baz" | awk -F '[0-9]+ [0-9]+ ' '{ print $2 }'
</code>```

or

```<code>echo -e "1 2 foo\n2 3 bar\n42 2 baz" | awk -F '[[:digit:]]+ [[:digit:]]+ ' '{ print $2 }'
</code>```

Both outputs:

```<code>foo
bar
baz
</code>```

Title: How to use a regex for the field separator in AWK?

Body:
First of all echo doesn't auto escape and outputs a literal \n. So you'll need to add -e to enable escapes. Second of all awk doesn't support \d so you have to use [0-9] or [[:digit:]].

echo -e "1 2 foo\n2 3 bar\n42 2 baz" | awk -F '[0-9]+ [0-9]+ ' '{ print $2 }'

or

echo -e "1 2 foo\n2 3 bar\n42 2 baz" | awk -F '[[:digit:]]+ [[:digit:]]+ ' '{ print $2 }'

Both outputs:

foo
bar
baz


问:
R Create a new variable name by partially matching a string from other variable names
I have multiple variable names that I need to combine into a single variable based on a common string of text. My sample data are:

```<code>structure(list(And = c(10L, NA, 10L), and = c(20L, 10L, 10L),
andbc = c(1L, NA, NA), baNdc = c(4L, NA, 5L), ban = c(1L,
NA, 1L)), .Names = c("And", "and", "andbc", "baNdc", "ban"), class = "data.frame", row.names = c(NA, -3L))
</code>```

I would like to create a new variable x, the value of which would be a row sum of the values of the other variables that share the common text string "and" ignoring the case of any of the letters in that string.

I attempted creating the variable by specifying the permutations, which I'm hoping to avoid:

```<code>names1[, 1:5][is.na(names1[, 1:5])] <- 0
names1$x <- sum(names1[which(grepl("And|and|aNd", names(names1)))])
</code>```

The result I get for values of x is a sum total of all values for the variables that meet the text string criteria:

```<code>structure(list(And = c(10, 0, 10), and = c(20L, 10L, 10L), andbc = c(1, 0, 0), baNdc = c(4, 0, 5), ban = c(1, 0, 1), x = c(70, 70, 70)), .Names = c("And", "and", "andbc", "baNdc", "ban", "x"), row.names = c(NA, -3L), class ="data.frame"
</code>```

How can I obtain the row sums based on the text string criteria and avoid having to specify the permutations of upper or lower case?

答:
The following would do the trick</p>

```<code>df <- structure(list(And = c(10L, NA, 10L), and = c(20L, 10L, 10L),
           andbc = c(1L, NA, NA), baNdc = c(4L, NA, 5L), ban = c(1L,
                                                                 NA, 1L)), .Names = c("And", "and", "andbc", "baNdc", "ban"), class = "data.frame", row.names = c(NA, -3L))

x <- rowSums(df[, grep("and", tolower(colnames(df)))], na.rm = TRUE)
</code>```

Body:
I have multiple variable names that I need to combine into a single variable based on a common string of text. My sample data are:

structure(list(And = c(10L, NA, 10L), and = c(20L, 10L, 10L),
andbc = c(1L, NA, NA), baNdc = c(4L, NA, 5L), ban = c(1L,
NA, 1L)), .Names = c("And", "and", "andbc", "baNdc", "ban"), class = "data.frame", row.names = c(NA, -3L))

I would like to create a new variable x, the value of which would be a row sum of the values of the other variables that share the common text string "and" ignoring the case of any of the letters in that string.

I attempted creating the variable by specifying the permutations, which I'm hoping to avoid:

names1[, 1:5][is.na(names1[, 1:5])] <- 0
names1$x <- sum(names1[which(grepl("And|and|aNd", names(names1)))])

The result I get for values of x is a sum total of all values for the variables that meet the text string criteria:

structure(list(And = c(10, 0, 10), and = c(20L, 10L, 10L), andbc = c(1, 0, 0), baNdc = c(4, 0, 5), ban = c(1, 0, 1), x = c(70, 70, 70)), .Names = c("And", "and", "andbc", "baNdc", "ban", "x"), row.names = c(NA, -3L), class ="data.frame"

How can I obtain the row sums based on the text string criteria and avoid having to specify the permutations of upper or lower case?

Body:
The following would do the trick

df <- structure(list(And = c(10L, NA, 10L), and = c(20L, 10L, 10L),
           andbc = c(1L, NA, NA), baNdc = c(4L, NA, 5L), ban = c(1L,
                                                                 NA, 1L)), .Names = c("And", "and", "andbc", "baNdc", "ban"), class = "data.frame", row.names = c(NA, -3L))

x <- rowSums(df[, grep("and", tolower(colnames(df)))], na.rm = TRUE)


问:
How to use Roboto Thin in Google Docs
Is there a way to use Roboto Thin font in a "Google Docs" document?

I fount a way to add Roboto font (via "More Fonts...") but I have not found a way to set the Thin or Light version.

答: It is now possible to use intermediate (i.e. light/semibold) font weights in Google docs. Click the fonts menu, and then click the arrow to the right of your desired font to select the weight you would like (this arrow is only present on fonts with other available weights).

Title: How to use Roboto Thin in Google Docs


问:
Caffe: How to load image data for semantic segmentation
I'm trying to use Caffe for a simple semantic image segmentation task (i.e. classifying each pixel in an image as belonging to one of 2 classes).

I am stuck with two issues: 1) Data preparation, and 2) network layer definition.

I have tried to read some examples.

Labels in Caffe as Images

How to feed caffe multi label data in HDF5 format?

Although the links are useful, they don't specifically apply to semantic segmentation with 2D images.

I would extremely appreciate (even brief) code examples for the following in a smooth pipeline:

Prepare the image label (2D array) in the correct format. An example using MemoryData or HDF5 as input would be perfect!

Define the network prototxt correctly to input the data and the above label.

Thanks!

答:
While there aren't any tutorials yet on the Caffe master thread on this, there are quite a few tutorials on doing semantic segmentation in Caffe. For starters, You should look into the tutorials for the Fully Convolutional Networks master as well as the tutorial on using SegNet (GitHub separately here) or using DeepLab. These are all state of the art methods that use Caffe for semantic segmentation.

To answer your question more directly,

1) Data preparation:
As someone who has showed interest in more recent deep learning approaches, you will probably have found that there is no one way to do data preparation. They depend both on what is mathematically possible (networks with fully connected layers on the end require images of the same ratio, and usually the same size), and on what improves the performance (mean subtraction). That being said, there are a few techniques that are commonplace (I will assume at this point on for simplicity sake that you can use images of varying scales as with Fully Convolutional Networks. if you want to see how cropping works, there is a good explanation of that type of data preparation in the ImageNet tutorial on Caffe). Using the Transformer class, most people do the following:

```<code>transformer.set_transpose('data', (2,0,1))  # move image channels to outermost dimension
transformer.set_mean('data', mu)            # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255)      # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2,1,0))  # swap channels from RGB to BGR
</code>```

In the context of segmentation, that is all you need to do. The semantic labels are in the form of images themselves (usually). For example, in the Pascal VOC Caffe example, you read in the labels as

```<code>n.data, n.label = L.Python(module = 'pascal_multilabel_datalayers', layer = datalayer, ntop = 2, param_str=str(data_layer_params))
</code>```

**2) network layer definition **

For network layer definition, remember that one of the brilliant bits  about neural networks is that aside from the inputs and outputs, they can handle a wide variety of data types. As such, all of your intermediate layers will be the same, and actually in your case so is the input is the same. What you need at the end is something with which to evaluate the cross entropy loss relative to the image. For DeepLab, they wrote an "Interp" layer which does this. SegNet on the other hand wrote an "Upsample" layer type, which they use before the softmax to make the  network output the same size as the label, and then simply use a Softmax

I guess the downside takeaway to all of this is that there isn't one clear-cut way to do this correctly yet in Caffe, but the good thing is that there are a lot of examples of it done successfully. Hope this helped

Body:
While there aren't any tutorials yet on the Caffe master thread on this, there are quite a few tutorials on doing semantic segmentation in Caffe. For starters, You should look into the tutorials for the Fully Convolutional Networks master as well as the tutorial on using SegNet (GitHub separately here) or using DeepLab. These are all state of the art methods that use Caffe for semantic segmentation.

To answer your question more directly,

1) Data preparation:
As someone who has showed interest in more recent deep learning approaches, you will probably have found that there is no one way to do data preparation. They depend both on what is mathematically possible (networks with fully connected layers on the end require images of the same ratio, and usually the same size), and on what improves the performance (mean subtraction). That being said, there are a few techniques that are commonplace (I will assume at this point on for simplicity sake that you can use images of varying scales as with Fully Convolutional Networks. if you want to see how cropping works, there is a good explanation of that type of data preparation in the ImageNet tutorial on Caffe). Using the Transformer class, most people do the following:

transformer.set_transpose('data', (2,0,1))  # move image channels to outermost dimension
transformer.set_mean('data', mu)            # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255)      # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2,1,0))  # swap channels from RGB to BGR

In the context of segmentation, that is all you need to do. The semantic labels are in the form of images themselves (usually). For example, in the Pascal VOC Caffe example, you read in the labels as

n.data, n.label = L.Python(module = 'pascal_multilabel_datalayers', layer = datalayer, ntop = 2, param_str=str(data_layer_params))

**2) network layer definition **

For network layer definition, remember that one of the brilliant bits  about neural networks is that aside from the inputs and outputs, they can handle a wide variety of data types. As such, all of your intermediate layers will be the same, and actually in your case so is the input is the same. What you need at the end is something with which to evaluate the cross entropy loss relative to the image. For DeepLab, they wrote an "Interp" layer which does this. SegNet on the other hand wrote an "Upsample" layer type, which they use before the softmax to make the  network output the same size as the label, and then simply use a Softmax

I guess the downside takeaway to all of this is that there isn't one clear-cut way to do this correctly yet in Caffe, but the good thing is that there are a lot of examples of it done successfully. Hope this helped


问:
Get a Javascript variable on the click of button
That's my code:

```<code>afficherListe();
function afficherListe()
{
 $.post('afficher_liste.php', function(data)
 {
    var obj = jQuery.parseJSON(data);

    if(data!="")
    {
        $.each(obj, function(index, value){
            $('<li><button onclick="supprimer_article()">X</button>'+value.nomArticle+' '+value.prixArticle+'</li>').insertAfter($('#liste_boissons'));
        });
    }
    else
    {
        alert('no data');
    }
 });
}

function supprimer_article(article_selectionne)
{
    alert(article_selectionne);
}
</code>```

Problem : I want to put my variable in supprimer_article() function, to have it in parameter and to continue my work in the function.
Example : If I click on "X" Vodka button (view picture), I would find inalert(article_selectionne); "Vodka"

In a primary step, I've put the variable like that :
$('
<buttononclick="supprimer_article('+value.prixArticle+')">X</button>'+value.nomArticle+' '+value.prixArticle+'
').insertAfter($('#liste_boissons'));

But, the result in alert(article_selectionne); was : UNDEFINED

If you think you have the solution, it would be very nice and helpful to share it for me !

To have an idea of what do this function now (the final goal is that this function will remove the selected article from the DB after the onclick event....) :

答:
You need to place the value within the function itself. Change:

```<code>$('<li><button onclick="supprimer_article()">X</button>'+value.nomArticle+' '+value.prixArticle+'</li>').insertAfter($('#liste_boissons'));
</code>```

to

```<code>$('<li><button onclick="supprimer_article('"+value.nomArticle+"')">X</button>'+value.nomArticle+' '+value.prixArticle+'</li>').insertAfter($('#liste_boissons'));
</code>```

Body:
That's my code:

afficherListe();
function afficherListe()
{
 $.post('afficher_liste.php', function(data)
 {
    var obj = jQuery.parseJSON(data);

    if(data!="")
    {
        $.each(obj, function(index, value){
            $('
<button onclick="supprimer_article()">X</button>'+value.nomArticle+' '+value.prixArticle+'
').insertAfter($('#liste_boissons'));
        });
    }
    else
    {
        alert('no data');
    }
 });
}

function supprimer_article(article_selectionne)
{
    alert(article_selectionne);
}

Problem : I want to put my variable in supprimer_article() function, to have it in parameter and to continue my work in the function.
Example : If I click on "X" Vodka button (view picture), I would find inalert(article_selectionne); "Vodka"

In a primary step, I've put the variable like that :
$('
<buttononclick="supprimer_article('+value.prixArticle+')">X</button>'+value.nomArticle+' '+value.prixArticle+'
').insertAfter($('#liste_boissons'));

But, the result in alert(article_selectionne); was : UNDEFINED

If you think you have the solution, it would be very nice and helpful to share it for me !

To have an idea of what do this function now (the final goal is that this function will remove the selected article from the DB after the onclick event....) :

Body:
You need to place the value within the function itself. Change:

$('<li><button onclick="supprimer_article()">X</button>'+value.nomArticle+' '+value.prixArticle+'</li>').insertAfter($('#liste_boissons'));

to

$('<li><button onclick="supprimer_article('"+value.nomArticle+"')">X</button>'+value.nomArticle+' '+value.prixArticle+'</li>').insertAfter($('#liste_boissons'));


问:
Physically moving cursor to a Web Element in Python
I would like to physically move the cursor to a web element (not simulate it) and Hold shift key and click another element by an offset.

I have got the hold shift key and clicking the 2nd element, using pyautogui:

```<code>import pyautogui

pyautogui.keyDown('shift')
pyautogui.moveRel(0, 62)
pyautogui.click()
pyautogui.keyUp('shift')
</code>```

My problem is moving the mouse physically to that original web element, then performing the above code. Pyautogui gives the option to move to absolute x,y coordinators, but it won't work in my case because the web element's absolute position changes.

Any idea how can I do this? preferably in python (pyautogui, selenium, etc)?

Browser is chrome and OS is OS X.

答:
If you want to continue to use PyAutoGUI, I would recommend using the image recognition feature. Take a screenshot of the element in the webpage and crop the screenshot to just the element (using MS Paint or even an online cropping tool).

Then run:

```<code>>>> x, y = pyautogui.locateCenterOnScreen('croppedscreenshot.png')
>>> pyautogui.moveTo(x, y)
>>> pyautogui.keyDown('shift')
>>> pyautogui.click(x, y)
>>> pyautogui.keyUp('shift')
</code>```

This is, of course, fragile and error-prone. It relies on the element being visible on the screen, and will likely not work if you are using a different browser than the screenshot was made from.

It's a much better idea to use Selenium or another such tool.

Body:
I would like to physically move the cursor to a web element (not simulate it) and Hold shift key and click another element by an offset.

I have got the hold shift key and clicking the 2nd element, using pyautogui:

import pyautogui

pyautogui.keyDown('shift')
pyautogui.moveRel(0, 62)
pyautogui.click()
pyautogui.keyUp('shift')

My problem is moving the mouse physically to that original web element, then performing the above code. Pyautogui gives the option to move to absolute x,y coordinators, but it won't work in my case because the web element's absolute position changes.

Any idea how can I do this? preferably in python (pyautogui, selenium, etc)?

Browser is chrome and OS is OS X.

Body:
If you want to continue to use PyAutoGUI, I would recommend using the image recognition feature. Take a screenshot of the element in the webpage and crop the screenshot to just the element (using MS Paint or even an online cropping tool).

Then run:

>>> x, y = pyautogui.locateCenterOnScreen('croppedscreenshot.png')
>>> pyautogui.moveTo(x, y)
>>> pyautogui.keyDown('shift')
>>> pyautogui.click(x, y)
>>> pyautogui.keyUp('shift')

This is, of course, fragile and error-prone. It relies on the element being visible on the screen, and will likely not work if you are using a different browser than the screenshot was made from.

It's a much better idea to use Selenium or another such tool.