问:
RXJS expand operator
```<code>var offset = 1;
var limit = 500;

var list = new Promise(function (resolve, reject) {
  rets.getAutoLogoutClient(config.clientSettings, (client) => {
    var results = client.search.query(SearchType, Class, Query, {
      limit: limit,
      offset: offset
    });
    resolve(results);
  });
});

var source = Rx.Observable.fromPromise(list);

source.subscribe(results => console.log(results.count));
</code>```

I am doing a real estate site, using RETS.

What I am trying to do my query are limited from the RETS server, is run this in a loop increasing my Offset until I have all my data. I don't know what the count is until I run the query and find the count value.

I have tried to use expand but I have no clue of how exactly it works. Tried to do these multiple ways, even using the old fashion while loop, which while doesn't work with .then method. So I have turned to RXJS since I been using it in Angular 4.

This is done in express. I need to eventually run corn jobs to fetch for updated properties, but my problem is fetching all the data and increasing the offset each time if the count is higher than my offset. So for example, run a query with an offset of 1 with a limit of 500. Total here is 1690. So next go around my offset would be:

```<code>offset += limit
</code>```

Once I have my data, I need to save it to MongoDB. Which I already been able successfully to do. It's just finding a way to get all the data without having to manually set my offset.

Note the server limit is 2500, yes I can fetch all this in one shot but there are also other data, such as media, which could have well over 2500.

Any suggestions?

答:
This is actually a fairly common use case for RxJS, as there are a lot of paginated data sources, or sources that are otherwise limited in what you can request at one time.

My two cents

To my mind expand is probably the best operator for this given that you are paginating against an unknown data source and you require at least one query in order to determine the final count. If you knew how much data you were going to be querying a more easier option would be to use something like mergeScan, but I digress.

Proposed Solution

This may take a little effort to wrap your head around so I have added annotations wherever possible to break down how this all works. Note I haven't actually tested this, so forgive me any syntax errors.

```<code>// Your constant limit for any one query
const limit = 500;

// RxJS helper method that wraps the async call into an Observable
// I am basing this on what I saw of your sample which leads me to believe
// that this should work.
const clientish = Rx.Observable.bindCallback(rets.getAutoLogoutClient);

// A method wrapper around your query call that wraps the resulting promise
// into a defer.
const queryish = (client, params) =>
  // Note the use of defer here is deliberate, since the query returns
  // a promise that will begin executing immediately, this prevents that behavior
  // And forces execution on subscription.
  Rx.Observable.defer(() => client.search.query(SearchType, Class, Query, params));

// This does the actual expansion function
// Note this is a higher order function because the client and the parameters
// are available at different times
const expander = (client) => ({limit, count}) =>
  // Invoke the query method
  queryish(client, {limit, count})
    // Remap the results, update offset and count and forward the whole
    // package down stream
    .map(results => ({
      limit,
      count: results.count,
      offset: offset + limit,
      results
    }));

// Start the stream by constructing the client
clientish(config.clientSettings)
  .switchMap(client =>
     // This are the arguments for the initial call
     Rx.Observable.of({limit, offset: 0})
       // Call the expander function with the client
       // The second argument is the max concurrency, you can change that if needed
       .expand(expander(client), 1)

       // Expand will keep recursing unless you tell it to stop
       // This will halt the execution once offset exceeds count, i.e. you have
       // all the data
       .takeWhile(({count, offset}) => offset < count)

       // Further downstream you only care about the results
       // So extract them from the message body and only forward them
       .pluck('results')
  )
  .subscribe(results => /*Do stuff with results*/);
</code>```

Title: RXJS expand operator

Body:
var offset = 1;
var limit = 500;

var list = new Promise(function (resolve, reject) {
  rets.getAutoLogoutClient(config.clientSettings, (client) => {
    var results = client.search.query(SearchType, Class, Query, {
      limit: limit,
      offset: offset
    });
    resolve(results);
  });
});

var source = Rx.Observable.fromPromise(list);

source.subscribe(results => console.log(results.count));

I am doing a real estate site, using RETS.

What I am trying to do my query are limited from the RETS server, is run this in a loop increasing my Offset until I have all my data. I don't know what the count is until I run the query and find the count value.

I have tried to use expand but I have no clue of how exactly it works. Tried to do these multiple ways, even using the old fashion while loop, which while doesn't work with .then method. So I have turned to RXJS since I been using it in Angular 4.

This is done in express. I need to eventually run corn jobs to fetch for updated properties, but my problem is fetching all the data and increasing the offset each time if the count is higher than my offset. So for example, run a query with an offset of 1 with a limit of 500. Total here is 1690. So next go around my offset would be:

offset += limit

Once I have my data, I need to save it to MongoDB. Which I already been able successfully to do. It's just finding a way to get all the data without having to manually set my offset.

Note the server limit is 2500, yes I can fetch all this in one shot but there are also other data, such as media, which could have well over 2500.

Any suggestions?

Body:
This is actually a fairly common use case for RxJS, as there are a lot of paginated data sources, or sources that are otherwise limited in what you can request at one time.

My two cents

To my mind expand is probably the best operator for this given that you are paginating against an unknown data source and you require at least one query in order to determine the final count. If you knew how much data you were going to be querying a more easier option would be to use something like mergeScan, but I digress.

Proposed Solution

This may take a little effort to wrap your head around so I have added annotations wherever possible to break down how this all works. Note I haven't actually tested this, so forgive me any syntax errors.

// Your constant limit for any one query
const limit = 500;

// RxJS helper method that wraps the async call into an Observable
// I am basing this on what I saw of your sample which leads me to believe
// that this should work.
const clientish = Rx.Observable.bindCallback(rets.getAutoLogoutClient);

// A method wrapper around your query call that wraps the resulting promise
// into a defer.
const queryish = (client, params) =>
  // Note the use of defer here is deliberate, since the query returns
  // a promise that will begin executing immediately, this prevents that behavior
  // And forces execution on subscription.
  Rx.Observable.defer(() => client.search.query(SearchType, Class, Query, params));

// This does the actual expansion function
// Note this is a higher order function because the client and the parameters
// are available at different times
const expander = (client) => ({limit, count}) =>
  // Invoke the query method
  queryish(client, {limit, count})
    // Remap the results, update offset and count and forward the whole
    // package down stream
    .map(results => ({
      limit,
      count: results.count,
      offset: offset + limit,
      results
    }));

// Start the stream by constructing the client
clientish(config.clientSettings)
  .switchMap(client =>
     // This are the arguments for the initial call
     Rx.Observable.of({limit, offset: 0})
       // Call the expander function with the client
       // The second argument is the max concurrency, you can change that if needed
       .expand(expander(client), 1)

       // Expand will keep recursing unless you tell it to stop
       // This will halt the execution once offset exceeds count, i.e. you have
       // all the data
       .takeWhile(({count, offset}) => offset < count)

       // Further downstream you only care about the results
       // So extract them from the message body and only forward them
       .pluck('results')
  )
  .subscribe(results => /*Do stuff with results*/);


来源: stackexchange

问:
npm5 missing build script
Using NPM 5, run npm test:

```<code>"scripts": {
    "build": "babel src -d dist",
    "test": "npm run build; mocha --require 'babel-polyfill' --compilers js:babel-register './tests/**/*.spec.js'"
  },
</code>```

I get this error:

```<code>npm ERR! missing script: build;
</code>```

Using NPM 2 without any problem

If I replace npm run build; => babel src -d dist, I got this error:

```<code>mocha doesn't exist. './tests/**/*.spec.js' doesn't exist
</code>```

but mocha is installed and tests folder exixts.

答:
Tried to check your error, and created this code:

```<code>{
 ...
 "scripts": {
   "build": "echo 'building'",
   "test": "npm run build && echo 'testing'"
 },
...
}
</code>```

then I simply ran:

```<code>npm test
</code>```

and this was my output:

```<code>building
testing
</code>```

npm - 5.0.3

So try maybe replacing ";" with "&&" in npm test

ContentLicense: CC BY-SA 3.0

Title: npm5 missing build script

Body:
Using NPM 5, run npm test:

"scripts": {
    "build": "babel src -d dist",
    "test": "npm run build; mocha --require 'babel-polyfill' --compilers js:babel-register './tests/**/*.spec.js'"
  },

I get this error:

npm ERR! missing script: build;

Using NPM 2 without any problem

If I replace npm run build; => babel src -d dist, I got this error:

mocha doesn't exist. './tests/**/*.spec.js' doesn't exist

but mocha is installed and tests folder exixts.

Body:
Tried to check your error, and created this code:

{
 ...
 "scripts": {
   "build": "echo 'building'",
   "test": "npm run build && echo 'testing'"
 },
...
}

then I simply ran:

npm test

and this was my output:

building
testing

npm - 5.0.3

So try maybe replacing ";" with "&&" in npm test


答:
SELECT SUM(cn.connection) sumresult FROM connection  AS cn WHERE cn.member_id

IN (SELECT em.id FROM member AS em WHERE em.status=1);

I hope it can help you as your require

Title: sql select sum with condition 2 tables?

Body:
I have 2 table

Table Connection
----------------------------------------
|  ID       |  Member_ID  |  Connection|
----------------------------------------
|  1        |  100        |  22        |
----------------------------------------
|  2        |  101        |  15        |
----------------------------------------
|  3        |  102        |  19        |
----------------------------------------
|  4        |  103        |  21        |

Table Member
---------------------------
|  ID         |  Status     |
---------------------------
|  100        |  0          |
----------------------------
|  101        |  1          |
----------------------------
|  102        |  1          |
---------------------------
|  103        |  0          |

I want to get sum of Connection has status =1.
The result in example should be 15+19 = 34.


问:
How to implement threading to run two bash shell commands in python?
I have to record a wav file and at the same time I have to analyze it with sox. I am using fifo type file for this operation.

So here I need to start 2 threads at the same time but even if I use the threads I am not able to achieve what I wanna do. Always one executing first and then the other. I want them to be in parallel so that I can do some stuff.

```<code>#this should be in one thread
def test_wav(self):
    """ analyze the data """
    bashCommand = "sox {} -n stat".format(self.__rawfile)
    while self.__rec_thread.is_alive():
        process = subprocess.Popen(bashCommand.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        wav_output = process.communicate()[1] #sox outputs the details in stderr
        #do something and return

#this should be in another thread
def record_wav(self):
    bashCommand = "arecord -d 10 -c 2 -r 48000 -f S32_LE > {}".format(self.__rawfile)
    pid = subprocess.Popen(bashCommand.split())
    pid.wait()
    if pid.returncode != 0:
        raise RecordException("Failed while recording with error {}".format(pid.returncode))
</code>```

I tried the following code to make them threads but failed(Always one executing first and then the other. I want them to be in parallel so that I can do some stuff).
imported from threading import Thread

```<code>self.__rec_thread = Thread(target = self.record_wav())
amp_thread = Thread(target = self.test_wav())
self.__rec_thread.start()
amp_thread.start()
</code>```

EDIT: First its executing the record(it minimum takes 10 sec because of the option -d 10) function completely and then the test wav function. Its like calling them one after another.

答:
```<code>... target = self.record_wav() ...
</code>```

is calling record_wav():  it executes immediately, and the program doesn't proceed until record_wav() completes.  You almost always want to pass a function (or method) object to target=, almost never the result of executing the function/method.  So just lose the parentheses:

```<code>... target = self.record_wav ...
</code>```

Body:
I have to record a wav file and at the same time I have to analyze it with sox. I am using fifo type file for this operation.

So here I need to start 2 threads at the same time but even if I use the threads I am not able to achieve what I wanna do. Always one executing first and then the other. I want them to be in parallel so that I can do some stuff.

#this should be in one thread
def test_wav(self):
    """ analyze the data """
    bashCommand = "sox {} -n stat".format(self.__rawfile)
    while self.__rec_thread.is_alive():
        process = subprocess.Popen(bashCommand.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        wav_output = process.communicate()[1] #sox outputs the details in stderr
        #do something and return

#this should be in another thread
def record_wav(self):
    bashCommand = "arecord -d 10 -c 2 -r 48000 -f S32_LE > {}".format(self.__rawfile)
    pid = subprocess.Popen(bashCommand.split())
    pid.wait()
    if pid.returncode != 0:
        raise RecordException("Failed while recording with error {}".format(pid.returncode))

I tried the following code to make them threads but failed(Always one executing first and then the other. I want them to be in parallel so that I can do some stuff).
imported from threading import Thread

self.__rec_thread = Thread(target = self.record_wav())
amp_thread = Thread(target = self.test_wav())
self.__rec_thread.start()
amp_thread.start()

EDIT: First its executing the record(it minimum takes 10 sec because of the option -d 10) function completely and then the test wav function. Its like calling them one after another.

Body:
... target = self.record_wav() ...

is calling record_wav():  it executes immediately, and the program doesn't proceed until record_wav() completes.  You almost always want to pass a function (or method) object to target=, almost never the result of executing the function/method.  So just lose the parentheses:

... target = self.record_wav ...


问:
PDF htaccess redirect
I have used the rule below which redirects every PDF file to a single page. What I would like to do is redirect a certain PDF to a certain page instead of every PDF going to one page.

Example: carolina-application.pdf redirects to //carolina-gun-laws.com

Is it possible to modify the code below to do this?

Place this rule at top of your .htaccess (just below RewriteEngine line):

```<code>RewriteRule ^.+\.pdf$ http://example2.com/ [L,NC,R=301]
</code>```

答:
You should place specific redirect rule before generic redirect rule:</p>

```<code>RewriteEngine On

RewriteRule ^carolina-application\.pdf$ http://carolina-gun-laws.com/ [L,NC,R=301]

RewriteRule ^.+\.pdf$ http://example2.com/ [L,NC,R=301]
</code>```

Title: PDF htaccess redirect

Body:
I have used the rule below which redirects every PDF file to a single page. What I would like to do is redirect a certain PDF to a certain page instead of every PDF going to one page.

Example: carolina-application.pdf redirects to //carolina-gun-laws.com

Is it possible to modify the code below to do this?

Place this rule at top of your .htaccess (just below RewriteEngine line):

RewriteRule ^.+\.pdf$ http://example2.com/ [L,NC,R=301]

Body:
You should place specific redirect rule before generic redirect rule:

RewriteEngine On

RewriteRule ^carolina-application\.pdf$ http://carolina-gun-laws.com/ [L,NC,R=301]

RewriteRule ^.+\.pdf$ http://example2.com/ [L,NC,R=301]


问:
Open Cygwin at a specific folder
How can I create a Cygwin shortcut that will open Cygwin at a specific folder? This would obviate having to type

```<code>cd /cygdrive/c/Users/Tom/Desktop/
</code>```

each time I launch Cygwin.

答:
Create a bash file say move.sh which has the following code
C:\cygwin64\bin\run.exe -p /bin bash runFile.sh
This starts the Cygwin and executes the runFile.sh present in the bin directory of Cygwin
Suppose you want to navigate to a specific directory say E:\code
then runFile.sh has the following code cd E:
cd code

Title: Open Cygwin at a specific folder

Body:
How can I create a Cygwin shortcut that will open Cygwin at a specific folder? This would obviate having to type

cd /cygdrive/c/Users/Tom/Desktop/

each time I launch Cygwin.


问:
iterate on scala list and treating last iteration differently
I have a list of objects that I am calling toString on, and would like to treat the last object differently as follows:

```<code>o1 = Array(...)
o2 = Array(...) // same length as o1
sb = new StringBuilder()
for (i <- 0 to o1.size() - 1)
  sb.append(o1.get(i).toString() + " & " o2.get(i).toString())
  // if not last iteration then append ", "
</code>```

is there a simple way to write this in scala rather than checking value of i etc?

答:
Give this a try.

```<code>o1.zip(o2).map(t => s"${t._1} & ${t._2}").mkString(", ")
</code>```

Zip the arrays together, turn each pair into the desired string, let mkString() insert the commas.

Body:
I have a list of objects that I am calling toString on, and would like to treat the last object differently as follows:

o1 = Array(...)
o2 = Array(...) // same length as o1
sb = new StringBuilder()
for (i <- 0 to o1.size() - 1)
  sb.append(o1.get(i).toString() + " & " o2.get(i).toString())
  // if not last iteration then append ", "

is there a simple way to write this in scala rather than checking value of i etc?

Body:
Give this a try.

o1.zip(o2).map(t => s"${t._1} & ${t._2}").mkString(", ")

Zip the arrays together, turn each pair into the desired string, let mkString() insert the commas.


问:
Why does redeclaring a function identifier within a try block throw a SyntaxError?
The following lines of JavaScript

```<code>try {
    function _free() {}
    var _free = 1;
} finally { }
</code>```

result in the following error:

```<code> Uncaught SyntaxError: Identifier '_free' has already been declared
</code>```

However, the following two blocks of JavaScript code don't:

Without the try block scope:

```<code>function _free() {}
var _free = 1;
</code>```

Within a function scope:

```<code>function a() {
    function _free() {}
    var _free = 1;
}
</code>```

But why?

(Testing environment: Chromium 61.0.3126.0)

答: Because block-scoped function declarations are a new ES6 feature and were made safe (i.e. throw an error on name collisions, similar to let and const), but the other cases (which are programmer mistakes regardless) needed to stay backwards compatible and silently overwrite the function.

Body:
The following lines of JavaScript

try {
    function _free() {}
    var _free = 1;
} finally { }

result in the following error:

 Uncaught SyntaxError: Identifier '_free' has already been declared

However, the following two blocks of JavaScript code don't:

Without the try block scope:

function _free() {}
var _free = 1;

Within a function scope:

function a() {
    function _free() {}
    var _free = 1;
}

But why?

(Testing environment: Chromium 61.0.3126.0)


问:
Elasticsearch: Already re-mapping but It still wont show all fields
I'm just trying to sync my mongodb with ElasticSearch. I've done the sync with river, and river already worked. But the ES won't show all fields, it only show "_ts" field in the object of "_source":

Request:
GET localhost:9200/test/orders/_search
Response:

```<code>{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 137,
        "max_score": 1,
        "hits": [
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58a3251f761f35a107724add",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58a340467f39c50f3a54c614",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b8ec806f34179d7c7b2431",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b8eff56f3417670f7b244a",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b8f0af6f3417fb207b244c",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b8f19a6f341761337b23da",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b9320c6f3417bc1c7b23c7",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b9339f6f341777237b23c6",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b934ab6f341778237b23c7",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b96ef76f34174a4b7b23c8",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            }
        ]
    }
}
</code>```

I already did the remapping, and It shows the updated mapping. See this:
request:
GET localhost:9200/test/orders/_mapping
response:

```<code>{
    "test": {
        "mappings": {
            "orders": {
                "properties": {
                    "_ts": {
                        "type": "long"
                    },
                    "activeDate": {
                        "type": "text"
                    },
                    "awbNumber": {
                        "type": "text"
                    },
                    "batchID": {
                        "type": "text"
                    },
                    "consignee": {
                        "properties": {
                            "id": {
                                "type": "text"
                            },
                            "name": {
                                "type": "text"
                            },
                            "phoneNumber": {
                                "type": "text"
                            }
                        }
                    },
                    "consigner": {
                        "properties": {
                            "id": {
                                "type": "text"
                            },
                            "name": {
                                "type": "text"
                            },
                            "phoneNumber": {
                                "type": "text"
                            }
                        }
                    },
                    "courier": {
                        "properties": {
                            "actualRate": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "id": {
                                "type": "integer"
                            },
                            "max_day": {
                                "type": "integer"
                            },
                            "min_day": {
                                "type": "integer"
                            },
                            "name": {
                                "type": "text"
                            },
                            "rate": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "rate_id": {
                                "type": "integer"
                            },
                            "rate_name": {
                                "type": "text"
                            },
                            "shipmentType": {
                                "type": "integer"
                            }
                        }
                    },
                    "creationDate": {
                        "type": "text"
                    },
                    "destination": {
                        "properties": {
                            "address": {
                                "type": "text"
                            },
                            "cityID": {
                                "type": "integer"
                            },
                            "cityName": {
                                "type": "text"
                            },
                            "id": {
                                "type": "integer"
                            },
                            "provinceID": {
                                "type": "integer"
                            },
                            "provinceName": {
                                "type": "text"
                            }
                        }
                    },
                    "driver": {
                        "properties": {
                            "feedback": {
                                "properties": {
                                    "comment": {
                                        "type": "text"
                                    },
                                    "score": {
                                        "type": "long"
                                    }
                                }
                            },
                            "id": {
                                "type": "long"
                            },
                            "isPaymentCollected": {
                                "type": "integer"
                            },
                            "name": {
                                "type": "text"
                            },
                            "phoneNumber": {
                                "type": "text"
                            },
                            "vehicleNumber": {
                                "type": "text"
                            },
                            "vehicleType": {
                                "type": "text"
                            }
                        }
                    },
                    "externalID": {
                        "type": "text"
                    },
                    "groupID": {
                        "type": "integer"
                    },
                    "id": {
                        "type": "text"
                    },
                    "isActive": {
                        "type": "long"
                    },
                    "isAutoTrack": {
                        "type": "integer"
                    },
                    "isCustomAWB": {
                        "type": "integer"
                    },
                    "isEscrow": {
                        "type": "integer"
                    },
                    "isLabelPrinted": {
                        "type": "integer"
                    },
                    "lastUpdatedDate": {
                        "type": "text"
                    },
                    "origin": {
                        "properties": {
                            "address": {
                                "type": "text"
                            },
                            "cityID": {
                                "type": "integer"
                            },
                            "cityName": {
                                "type": "text"
                            },
                            "id": {
                                "type": "integer"
                            },
                            "provinceID": {
                                "type": "integer"
                            },
                            "provinceName": {
                                "type": "text"
                            }
                        }
                    },
                    "package": {
                        "properties": {
                            "content": {
                                "type": "text"
                            },
                            "contents": {
                                "type": "integer"
                            },
                            "cubicalWeight": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "dimension": {
                                "properties": {
                                    "height": {
                                        "properties": {
                                            "UoM": {
                                                "type": "text"
                                            },
                                            "value": {
                                                "type": "integer"
                                            }
                                        }
                                    },
                                    "length": {
                                        "properties": {
                                            "UoM": {
                                                "type": "text"
                                            },
                                            "value": {
                                                "type": "integer"
                                            }
                                        }
                                    },
                                    "width": {
                                        "properties": {
                                            "UoM": {
                                                "type": "text"
                                            },
                                            "value": {
                                                "type": "integer"
                                            }
                                        }
                                    }
                                }
                            },
                            "fragile": {
                                "type": "integer"
                            },
                            "isConfirmed": {
                                "type": "integer"
                            },
                            "itemName": {
                                "type": "text"
                            },
                            "itemSubtype": {
                                "type": "integer"
                            },
                            "itemType": {
                                "type": "integer"
                            },
                            "pictureURL": {
                                "type": "text"
                            },
                            "price": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "type": {
                                "type": "integer"
                            },
                            "weight": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            }
                        }
                    },
                    "paymentType": {
                        "type": "text"
                    },
                    "pickUpTime": {
                        "type": "text"
                    },
                    "rates": {
                        "properties": {
                            "actualInsurance": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "long"
                                    }
                                }
                            },
                            "actualShipment": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "escrowCost": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "fulfillmentCost": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "insurance": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "long"
                                    }
                                }
                            },
                            "itemPrice": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "liability": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "shipment": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            }
                        }
                    },
                    "readyTime": {
                        "type": "text"
                    },
                    "shipmentStatus": {
                        "properties": {
                            "description": {
                                "type": "text"
                            },
                            "name": {
                                "type": "text"
                            },
                            "statusCode": {
                                "type": "integer"
                            },
                            "updateDate": {
                                "type": "text"
                            },
                            "updatedBy": {
                                "type": "text"
                            }
                        }
                    },
                    "source": {
                        "type": "text"
                    },
                    "specialID": {
                        "type": "text"
                    },
                    "stickerNumber": {
                        "type": "text"
                    },
                    "useInsurance": {
                        "type": "integer"
                    }
                }
            }
        }
    }
}
</code>```

I'm expecting I can get the whole fields (not just a single "_ts" field) in the "_source" field which it should be the same with what I've mapped.

I've tried to delete the index and recreate one, still not worked. Any clue of this kind of issue. I really really need help, thank you so much.

答:
ElasticSearch has a behavior of creating fields on its own. So if you do

```<code>PUT document/index/1
{
    "id" : "1",
    "name" : "kashish",
    "phoneNumber" : "9740683281"
}
</code>```

This will automatically create fields for your index. What I am illustrating is if explicitly defining of fields is not working for you for some reason , you can empty the index(if contains dummy data) and then just put your json which ES will pickup automatically.

Body:
I'm just trying to sync my mongodb with ElasticSearch. I've done the sync with river, and river already worked. But the ES won't show all fields, it only show "_ts" field in the object of "_source":

Request:
GET localhost:9200/test/orders/_search
Response:

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 137,
        "max_score": 1,
        "hits": [
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58a3251f761f35a107724add",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58a340467f39c50f3a54c614",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b8ec806f34179d7c7b2431",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b8eff56f3417670f7b244a",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b8f0af6f3417fb207b244c",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b8f19a6f341761337b23da",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b9320c6f3417bc1c7b23c7",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b9339f6f341777237b23c6",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b934ab6f341778237b23c7",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            },
            {
                "_index": "test",
                "_type": "orders",
                "_id": "58b96ef76f34174a4b7b23c8",
                "_score": 1,
                "_source": {
                    "_ts": 6438761296509796000
                }
            }
        ]
    }
}

I already did the remapping, and It shows the updated mapping. See this:
request:
GET localhost:9200/test/orders/_mapping
response:

{
    "test": {
        "mappings": {
            "orders": {
                "properties": {
                    "_ts": {
                        "type": "long"
                    },
                    "activeDate": {
                        "type": "text"
                    },
                    "awbNumber": {
                        "type": "text"
                    },
                    "batchID": {
                        "type": "text"
                    },
                    "consignee": {
                        "properties": {
                            "id": {
                                "type": "text"
                            },
                            "name": {
                                "type": "text"
                            },
                            "phoneNumber": {
                                "type": "text"
                            }
                        }
                    },
                    "consigner": {
                        "properties": {
                            "id": {
                                "type": "text"
                            },
                            "name": {
                                "type": "text"
                            },
                            "phoneNumber": {
                                "type": "text"
                            }
                        }
                    },
                    "courier": {
                        "properties": {
                            "actualRate": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "id": {
                                "type": "integer"
                            },
                            "max_day": {
                                "type": "integer"
                            },
                            "min_day": {
                                "type": "integer"
                            },
                            "name": {
                                "type": "text"
                            },
                            "rate": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "rate_id": {
                                "type": "integer"
                            },
                            "rate_name": {
                                "type": "text"
                            },
                            "shipmentType": {
                                "type": "integer"
                            }
                        }
                    },
                    "creationDate": {
                        "type": "text"
                    },
                    "destination": {
                        "properties": {
                            "address": {
                                "type": "text"
                            },
                            "cityID": {
                                "type": "integer"
                            },
                            "cityName": {
                                "type": "text"
                            },
                            "id": {
                                "type": "integer"
                            },
                            "provinceID": {
                                "type": "integer"
                            },
                            "provinceName": {
                                "type": "text"
                            }
                        }
                    },
                    "driver": {
                        "properties": {
                            "feedback": {
                                "properties": {
                                    "comment": {
                                        "type": "text"
                                    },
                                    "score": {
                                        "type": "long"
                                    }
                                }
                            },
                            "id": {
                                "type": "long"
                            },
                            "isPaymentCollected": {
                                "type": "integer"
                            },
                            "name": {
                                "type": "text"
                            },
                            "phoneNumber": {
                                "type": "text"
                            },
                            "vehicleNumber": {
                                "type": "text"
                            },
                            "vehicleType": {
                                "type": "text"
                            }
                        }
                    },
                    "externalID": {
                        "type": "text"
                    },
                    "groupID": {
                        "type": "integer"
                    },
                    "id": {
                        "type": "text"
                    },
                    "isActive": {
                        "type": "long"
                    },
                    "isAutoTrack": {
                        "type": "integer"
                    },
                    "isCustomAWB": {
                        "type": "integer"
                    },
                    "isEscrow": {
                        "type": "integer"
                    },
                    "isLabelPrinted": {
                        "type": "integer"
                    },
                    "lastUpdatedDate": {
                        "type": "text"
                    },
                    "origin": {
                        "properties": {
                            "address": {
                                "type": "text"
                            },
                            "cityID": {
                                "type": "integer"
                            },
                            "cityName": {
                                "type": "text"
                            },
                            "id": {
                                "type": "integer"
                            },
                            "provinceID": {
                                "type": "integer"
                            },
                            "provinceName": {
                                "type": "text"
                            }
                        }
                    },
                    "package": {
                        "properties": {
                            "content": {
                                "type": "text"
                            },
                            "contents": {
                                "type": "integer"
                            },
                            "cubicalWeight": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "dimension": {
                                "properties": {
                                    "height": {
                                        "properties": {
                                            "UoM": {
                                                "type": "text"
                                            },
                                            "value": {
                                                "type": "integer"
                                            }
                                        }
                                    },
                                    "length": {
                                        "properties": {
                                            "UoM": {
                                                "type": "text"
                                            },
                                            "value": {
                                                "type": "integer"
                                            }
                                        }
                                    },
                                    "width": {
                                        "properties": {
                                            "UoM": {
                                                "type": "text"
                                            },
                                            "value": {
                                                "type": "integer"
                                            }
                                        }
                                    }
                                }
                            },
                            "fragile": {
                                "type": "integer"
                            },
                            "isConfirmed": {
                                "type": "integer"
                            },
                            "itemName": {
                                "type": "text"
                            },
                            "itemSubtype": {
                                "type": "integer"
                            },
                            "itemType": {
                                "type": "integer"
                            },
                            "pictureURL": {
                                "type": "text"
                            },
                            "price": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "type": {
                                "type": "integer"
                            },
                            "weight": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            }
                        }
                    },
                    "paymentType": {
                        "type": "text"
                    },
                    "pickUpTime": {
                        "type": "text"
                    },
                    "rates": {
                        "properties": {
                            "actualInsurance": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "long"
                                    }
                                }
                            },
                            "actualShipment": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "escrowCost": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "fulfillmentCost": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "insurance": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "long"
                                    }
                                }
                            },
                            "itemPrice": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "liability": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "shipment": {
                                "properties": {
                                    "UoM": {
                                        "type": "text"
                                    },
                                    "value": {
                                        "type": "integer"
                                    }
                                }
                            }
                        }
                    },
                    "readyTime": {
                        "type": "text"
                    },
                    "shipmentStatus": {
                        "properties": {
                            "description": {
                                "type": "text"
                            },
                            "name": {
                                "type": "text"
                            },
                            "statusCode": {
                                "type": "integer"
                            },
                            "updateDate": {
                                "type": "text"
                            },
                            "updatedBy": {
                                "type": "text"
                            }
                        }
                    },
                    "source": {
                        "type": "text"
                    },
                    "specialID": {
                        "type": "text"
                    },
                    "stickerNumber": {
                        "type": "text"
                    },
                    "useInsurance": {
                        "type": "integer"
                    }
                }
            }
        }
    }
}

I'm expecting I can get the whole fields (not just a single "_ts" field) in the "_source" field which it should be the same with what I've mapped.

I've tried to delete the index and recreate one, still not worked. Any clue of this kind of issue. I really really need help, thank you so much.

Body:
ElasticSearch has a behavior of creating fields on its own. So if you do

PUT document/index/1
{
    "id" : "1",
    "name" : "kashish",
    "phoneNumber" : "9740683281"
}

This will automatically create fields for your index. What I am illustrating is if explicitly defining of fields is not working for you for some reason , you can empty the index(if contains dummy data) and then just put your json which ES will pickup automatically.


问:
socket: operation not permitted when connecting to cloudsql
When trying to connect with the following settings I get a socket:operation not permitted error on app engine, but not locally when using cloud-sql-proxy. The following code works locally but not with app engine:

```<code>connString = "user=USER_NAME password=PASSWORD host=/cloudsql/INSTANCE_NAME dbname=DB_NAME"
DBCon, err2 = sql.Open("postgres", connString)
</code>```

Both the app engine and cloud sql are in the same project. Does anyone have any insight on what I am missing?

答:
Unfortunately, Cloud SQL+postgres is not supported on AppEngine standard yet:

"Note: Connection from an App Engine standard environment application to a PostgreSQL instance is not supported"

https://cloud.google.com/sql/docs/postgres/connect-app-engine

Body:
When trying to connect with the following settings I get a socket:operation not permitted error on app engine, but not locally when using cloud-sql-proxy. The following code works locally but not with app engine:

connString = "user=USER_NAME password=PASSWORD host=/cloudsql/INSTANCE_NAME dbname=DB_NAME"
DBCon, err2 = sql.Open("postgres", connString)

Both the app engine and cloud sql are in the same project. Does anyone have any insight on what I am missing?


问:
Id for Spring Bean/Repository
I have a repository B which I extend from another repository A.

Repository A:

```<code>@Repository
public class RepositoryA implements Repository {

}
</code>```

Repository  B:

```<code>@Repository
public class RepositoryB extends  RepositoryA implements RepositoryAux{
}
</code>```

But when I try to inject the repository I am having the following error:

No unique bean of type [Repository] is defined: expected single matching bean but found 2: [RepositoryA , RepositoryB]

I understand that I can solve it using the annotation @Qualifier. But, my question is:

it's right Overwrite the annotation @Repository in the child class ?

Can I assign a new identifier to the child class? Instead of using annotation @Qualifier, Something like @Id or @Name

Thank's in advance

答:
it's right Overwrite the annotation @Repository in the child class ?

There are a few things you should consider:

a. If you intend to use RepositoryA as a repository, hen you need @Repository on both.

b. If you intent to use RepositoryA just to provide basic functionality so RepositoryB (or other classes) can extend it, then make RepositoryA abstract and annotate with NoRepositoryBean

c. Alternatively, you could use "composition over inheritance" principle. In short you could inject RepositoryA into RepositoryB and create facade or wrapper methods to access the methods on RepositoryB instance.

Can I assign a new identifier to the child class? Instead of using annotation @Qualifier, Something like @Id or @Name

I think you mean @Named from javax.inject.Named which is basically equivalent to @Qualifier

I think you mean <bean id='...' which in this case @Qualifier is the way to do with annotations. See spring's documentation

Title: Id for Spring Bean/Repository

Body:
I have a repository B which I extend from another repository A.

Repository A:

@Repository
public class RepositoryA implements Repository {

}

Repository  B:

@Repository
public class RepositoryB extends  RepositoryA implements RepositoryAux{
}

But when I try to inject the repository I am having the following error:

No unique bean of type [Repository] is defined: expected single matching bean but found 2: [RepositoryA , RepositoryB]

I understand that I can solve it using the annotation @Qualifier. But, my question is:

it's right Overwrite the annotation @Repository in the child class ?

Can I assign a new identifier to the child class? Instead of using annotation @Qualifier, Something like @Id or @Name

Thank's in advance


问:
Python itertools.groupby length key?
using python 3.6

Is there a way to return the length of a groupby generator as its key?

so this:

```<code>from itertools import groupby

x = [1,1,1,1,1,
     2,2,2,2,
     3,3,3,
     4,4,
     5]

for key, group in groupby(x): # Some lambda function here?
    print(key,'|', group)
</code>```

would output:

```<code>5 | <itertools._grouper object at 0x0000016D0FFD45C0>
4 | <itertools._grouper object at 0x0000016D0FFD4668>
3 | <itertools._grouper object at 0x0000016D0FFD4400>
2 | <itertools._grouper object at 0x0000016D0FFD45C0>
1 | <itertools._grouper object at 0x0000016D0FFD4668>
</code>```

Cheers.

答:
If you don't mind loading the generators into lists, you could do that and check their length

```<code>for key, group in groupby(x):
    l = list(group)
    print(len(l))
    do_something_else_with_list(l)
</code>```

If you meant that you want to check how long the groupby itself is, the you can do the same with that:

```<code>groupby_list = list(groupby(x))
print(len(groupby_list))
</code>```

Just be careful, once you turned a generator into a list the generator itself cannot be iterated over again, and you should only use the list (or re-create the generator)

Title: Python itertools.groupby length key?

Body:
using python 3.6

Is there a way to return the length of a groupby generator as its key?

so this:

from itertools import groupby

x = [1,1,1,1,1,
     2,2,2,2,
     3,3,3,
     4,4,
     5]

for key, group in groupby(x): # Some lambda function here?
    print(key,'|', group)

would output:

5 | <itertools._grouper object at 0x0000016D0FFD45C0>
4 | <itertools._grouper object at 0x0000016D0FFD4668>
3 | <itertools._grouper object at 0x0000016D0FFD4400>
2 | <itertools._grouper object at 0x0000016D0FFD45C0>
1 | <itertools._grouper object at 0x0000016D0FFD4668>

Cheers.

Body:
If you don't mind loading the generators into lists, you could do that and check their length

for key, group in groupby(x):
    l = list(group)
    print(len(l))
    do_something_else_with_list(l)

If you meant that you want to check how long the groupby itself is, the you can do the same with that:

groupby_list = list(groupby(x))
print(len(groupby_list))

Just be careful, once you turned a generator into a list the generator itself cannot be iterated over again, and you should only use the list (or re-create the generator)


问:
Slow processing of all images by google face API?
I am making an android app which is using the Google face API to detect faces of all the images in the gallery. It is taking a long time to process all the images and hence the apps get stuck for a long time. Any workaround?
I tried reducing the size of the image and then process, but it gives a faulty answer on it.</p>

答:
If you look in the documentation of the FaceDetector.Builder you will see that you can set some properties that will increase the speed.

E.g.:

```<code>public FaceDetector.Builder setProminentFaceOnly (boolean prominentFaceOnly)
</code>```

Body:
If you look in the documentation of the FaceDetector.Builder you will see that you can set some properties that will increase the speed.

E.g.:

public FaceDetector.Builder setProminentFaceOnly (boolean prominentFaceOnly)


问:
Search mysql database by phrase or seperate key words
Ok so when a user types their search phrase in the search input, I would like it to match the exact phrase or the key words entered.
I.e
So the title of a post is "Search the Database"

```<code>$searchVal = "Search database";
WHERE post_title LIKE '%" . $searchVal . "%'
</code>```

The above code doesn't find a match as the title has "the" between Search Database.

How could I get it to match.
I thought maybe using explode but Im getting an error:

```<code>$sVals = explode(" ", $searchVal);
foreach ($sVals as $s) {
    $querySearchVals .= "OR post_title LIKE '%" . $s . "%'";
}
</code>```

Hope that makes sense.
Cheers

答:
Maybe this could help

```<code>$search_key = $_POST['searchkey'];  //take the search text from the post method
$search_words = array();
$search_words = explode(" ",$search_key);
$q_string = "";
$last_index = intval(count($search_Words)) - 1;
for($i = 0; $i<count($search_Words);$i++)
{
$q_string = $q_string . " post_title LIKE '%$search_words[$i]%' ";
if($i != $last_index)
  $q_string = $q_string . " OR ";
}
</code>```

And to make it even more accurate, you may skip the articles like A, The, An etc

Body:
Ok so when a user types their search phrase in the search input, I would like it to match the exact phrase or the key words entered.
I.e
So the title of a post is "Search the Database"

$searchVal = "Search database";
WHERE post_title LIKE '%" . $searchVal . "%'

The above code doesn't find a match as the title has "the" between Search Database.

How could I get it to match.
I thought maybe using explode but Im getting an error:

$sVals = explode(" ", $searchVal);
foreach ($sVals as $s) {
    $querySearchVals .= "OR post_title LIKE '%" . $s . "%'";
}

Hope that makes sense.
Cheers

Body:
Maybe this could help

$search_key = $_POST['searchkey'];  //take the search text from the post method
$search_words = array();
$search_words = explode(" ",$search_key);
$q_string = "";
$last_index = intval(count($search_Words)) - 1;
for($i = 0; $i<count($search_Words);$i++)
{
$q_string = $q_string . " post_title LIKE '%$search_words[$i]%' ";
if($i != $last_index)
  $q_string = $q_string . " OR ";
}

And to make it even more accurate, you may skip the articles like A, The, An etc


问:
How to use tf.reset_default_graph()
Whenever I try to use tf.reset_default_graph(), I get this error: IndexError: list index out of range or ``. At which part of my code should I use this? When should I be using this?

Edit:

I updated the code, but the error still occurs.

def evaluate():
    with tf.name_scope("loss"):
        global x # x is a tf.placeholder()
        xentropy = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=neural_network(x))
        loss = tf.reduce_mean(xentropy, name="loss")

    with tf.name_scope("train"):
        optimizer = tf.train.AdamOptimizer()
        training_op = optimizer.minimize(loss)

    with tf.name_scope("exec"):
        with tf.Session() as sess:
            for i in range(1, 2):
                sess.run(tf.global_variables_initializer())
                sess.run(training_op, feed_dict={x: np.array(train_data).reshape([-1, 1]), y: label})
                print "Training " + str(i)
                saver = tf.train.Saver()
                saver.save(sess, "saved_models/testing")
                print "Model Saved."

def predict():
    with tf.name_scope("predict"):
        tf.reset_default_graph()
        with tf.Session() as sess:
            saver = tf.train.import_meta_graph("saved_models/testing.meta")
            saver.restore(sess, "saved_models/testing")
            output_ = tf.get_default_graph().get_tensor_by_name('output_layer:0')
            print sess.run(output_, feed_dict={x: np.array([12003]).reshape([-1, 1])})

def main():
    print "Starting Program..."
    evaluate()
    writer = tf.summary.FileWriter("mygraph/logs", tf.get_default_graph())
    predict()
```

If I remove the tf.reset_default_graph() from the updated code, I get this error:  ValueError: cannot add op with name hidden_layer1/kernel/Adam as that name is already used

From my current understanding, tf.reset_default_graph() removes all graphs, hence I avoided the error I mention above(ValueError: cannot add op with name hidden_layer1/kernel/Adam as that name is already used)

答:
This is probably how you use it:

```<code>import tensorflow as tf
a = tf.constant(1)
with tf.Session() as sess:
    tf.reset_default_graph()
</code>```

You get an error because you use it in a session. From the tf.reset_default_graph() documentation:

Calling this function while a tf.Session or tf.InteractiveSession is
  active will result in undefined behavior. Using any previously created
  tf.Operation or tf.Tensor objects after calling this function will
  result in undefined behavior

tf.reset_default_graph() can be helpful (at least for me) during the testing phase while I experiment in jupyter notebook. However, I have never used it in production and do not see how it would be helpful there.

Here is an example that could be in a notebook:

```<code>import tensorflow as tf
# create some graph
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print sess.run(...)
</code>```

Now I do not need this stuff anymore, but if I create another graph and visualize it in tensorboard I will see old nodes and the new nodes. To solve this, I could restart the kernel and run only the next cell. However, I can just do:

```<code>tf.reset_default_graph()
# create a new graph
with tf.Session() as sess:
    print sess.run(...)
</code>```

Edit after OP added his code:

```<code>with tf.name_scope("predict"):
    tf.reset_default_graph()
</code>```

Here is what approximately happens. Your code fails because tf.name_scope already added something to a graph. While being inside of this "adding something to the graph", you tell  TF to remove the graph completely, but it can't because it is busy adding something.

ContentLicense: CC BY-SA 4.0

Title: How to use tf.reset_default_graph()

Body:
Whenever I try to use tf.reset_default_graph(), I get this error: IndexError: list index out of range or ``. At which part of my code should I use this? When should I be using this?

Edit:

I updated the code, but the error still occurs.

def evaluate():
    with tf.name_scope("loss"):
        global x # x is a tf.placeholder()
        xentropy = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=neural_network(x))
        loss = tf.reduce_mean(xentropy, name="loss")

    with tf.name_scope("train"):
        optimizer = tf.train.AdamOptimizer()
        training_op = optimizer.minimize(loss)

    with tf.name_scope("exec"):
        with tf.Session() as sess:
            for i in range(1, 2):
                sess.run(tf.global_variables_initializer())
                sess.run(training_op, feed_dict={x: np.array(train_data).reshape([-1, 1]), y: label})
                print "Training " + str(i)
                saver = tf.train.Saver()
                saver.save(sess, "saved_models/testing")
                print "Model Saved."

def predict():
    with tf.name_scope("predict"):
        tf.reset_default_graph()
        with tf.Session() as sess:
            saver = tf.train.import_meta_graph("saved_models/testing.meta")
            saver.restore(sess, "saved_models/testing")
            output_ = tf.get_default_graph().get_tensor_by_name('output_layer:0')
            print sess.run(output_, feed_dict={x: np.array([12003]).reshape([-1, 1])})

def main():
    print "Starting Program..."
    evaluate()
    writer = tf.summary.FileWriter("mygraph/logs", tf.get_default_graph())
    predict()

If I remove the tf.reset_default_graph() from the updated code, I get this error:  ValueError: cannot add op with name hidden_layer1/kernel/Adam as that name is already used

From my current understanding, tf.reset_default_graph() removes all graphs, hence I avoided the error I mention above(ValueError: cannot add op with name hidden_layer1/kernel/Adam as that name is already used)

Body:
This is probably how you use it:

import tensorflow as tf
a = tf.constant(1)
with tf.Session() as sess:
    tf.reset_default_graph()

You get an error because you use it in a session. From the tf.reset_default_graph() documentation:

Calling this function while a tf.Session or tf.InteractiveSession is
  active will result in undefined behavior. Using any previously created
  tf.Operation or tf.Tensor objects after calling this function will
  result in undefined behavior

tf.reset_default_graph() can be helpful (at least for me) during the testing phase while I experiment in jupyter notebook. However, I have never used it in production and do not see how it would be helpful there.

Here is an example that could be in a notebook:

import tensorflow as tf
# create some graph
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print sess.run(...)

Now I do not need this stuff anymore, but if I create another graph and visualize it in tensorboard I will see old nodes and the new nodes. To solve this, I could restart the kernel and run only the next cell. However, I can just do:

tf.reset_default_graph()
# create a new graph
with tf.Session() as sess:
    print sess.run(...)

Edit after OP added his code:

with tf.name_scope("predict"):
    tf.reset_default_graph()

Here is what approximately happens. Your code fails because tf.name_scope already added something to a graph. While being inside of this "adding something to the graph", you tell  TF to remove the graph completely, but it can't because it is busy adding something.


问:
ADO Recordset Not Staying Open in VBA - "Operation is not allowed when the object is closed"
I don't really understand what has happened here. I'm using Excel VBA to connect to a SQL Server Express database and return an ADO Recordset. I had it working initially, but my code was a bit messy so I created a new module and copied the code across, tidying it up as I went along.

Now, when I try to run the Sub just to return a the recordcount, I get the error "The operation is not allowed when the object is closed." The code breaks on the MsgBox line.

Here is the simplified code:

```<code>Dim Server As String
Server = "ServerName"

Dim Database As String
Database = "DatabaseName"

Dim UserID As String
UserID = "UserID"

Dim Pwd As String
Pwd = "Password"

Dim StoredProcedure As String
StoredProcedure = "StoredProcedureName"

Dim conn As New ADODB.Connection
conn.ConnectionString = "Driver={SQL Server};Server=" & Server & "; Database=" & Database & "; UID = " & UserID & "; PWD=" & Pwd & ""
conn.Open

Dim Cmd As New ADODB.Command
Cmd.ActiveConnection = conn
Cmd.CommandText = StoredProcedure
Cmd.CommandType = adCmdStoredProc

Dim params() As String
ReDim Preserve params(4, 2)

params(0, 0) = "Param1"
params(1, 0) = CStr(adInteger)
params(2, 0) = CStr(adParamInput)
params(4, 0) = CStr(6)

params(0, 1) = "Param2"
params(1, 1) = CStr(adInteger)
params(2, 1) = CStr(adParamInput)
params(4, 1) = CStr(6)

params(0, 2) = "Param3"
params(1, 2) = CStr(adInteger)
params(2, 2) = CStr(adParamInput)
params(4, 2) = CStr(15)

Dim sParam4 as String
If Not sParam4 = "" Then
    ReDim Preserve params(4, UBound(params, 2) + 1)
    params(0, UBound(params, 2)) = "Param4"
    params(1, UBound(params, 2)) = CStr(adChar)
    params(2, UBound(params, 2)) = CStr(adParamInput)
    params(3, UBound(params, 2)) = "1"
    params(4, UBound(params, 2)) = sParam4
End If

Dim sParam5 as String
If Not sParam5 = "" Then
    ReDim Preserve params(4, UBound(params, 2) + 1)
    params(0, UBound(params, 2)) = "Param5"
    params(1, UBound(params, 2)) = CStr(adChar)
    params(2, UBound(params, 2)) = CStr(adParamInput)
    params(3, UBound(params, 2)) = Len(sParam5)
    params(4, UBound(params, 2)) = sParam5
End If

Dim Prm As ADODB.Parameter
Set Prm = New ADODB.Parameter
Dim i As Integer
For i = 0 To UBound(params, 2)
    If params(1, i) = CStr(adChar) Then
        Set Prm = Cmd.CreateParameter(params(0, i), CInt(params(1, i)), CInt(params(2, i)), CInt(params(3, i)))
        Cmd.Parameters.Append Prm
        Cmd.Parameters(params(0, i)).Value = params(4, i)
    Else
        Set Prm = Cmd.CreateParameter(params(0, i), CInt(params(1, i)), CInt(params(2, i)))
        Cmd.Parameters.Append Prm
        Cmd.Parameters(params(0, i)).Value = CInt(params(4, i))
    End If
Next i

Dim rs As New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open Cmd, , adOpenStatic, adLockOptimistic

MsgBox ("Success! " & rs.RecordCount & " Records Returned!")
</code>```

When I look at the rs variable in the Locals window, all of the properties are listed as showing the same error message. To me, it seems as if the recordset is opening correctly, but then immediately closing itself.

What's weird is that the original (messy) sub now also doesn't work, throwing the same error. I don't think that I changed anything there, just copied it to the new sub.

I've commented out the entire module that the old sub was in, just in case there was any sort of conflicting variable situation going on. This didn't make any difference.

I just can't see what's wrong with it! I've done lots of research and reading, and to my untrained-but-enthusiastic eye, it all seems fine.

Any help would be much appreciated.

EDIT: Here is the Stored Procedure:

```<code>USE [MyDatabase]
GO
/****** Object:  StoredProcedure [dbo].[MyProcedure]    Script Date: 14/09/2015 11:39:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[MyProcedure]
    -- Add the parameters for the stored procedure here
    @Param1 int, @Param2 int, @Param3 int, @Param4 char(1), @Param5 varchar(20) = NULL, @Param6 varchar(20) = NULL, @Param7 varchar(20) = NULL
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    SET ANSI_WARNINGS OFF;

    -- Insert statements for procedure here
    DECLARE @cols AS NVARCHAR(MAX), @query  AS NVARCHAR(MAX)

SET @cols = '';
SELECT @cols = @cols + ',' + QUOTENAME([Field3]) FROM View1
WHERE Field2 = @Param2 AND Field1 = @Param1

SET @cols = STUFF(@cols,1,1,'');

IF @Param5 IS NOT NULL
   SET @query = 'SELECT Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12, Field13, Field14,' + @cols + ' FROM
(
    SELECT
        Table1.Field1,
        Table1.Field2,
        Table1.Field3,
        Table1.Field4,
        Table1.Field5,
        Table1.Field6,
        Table1.Field7,
        Table1.Field8,
        Table1.Field9,
        Table1.Field10,
        Table1.Field11,
        Table1.Field12,
        Table1.Field13,
        Table1.Field14,
        Table2.Field2,
        Table3.Field1
    FROM Table3
        LEFT OUTER JOIN Table2 ON Table3.Field1 = Table2.Field1
            AND Table3.Field2 = Table2.Field2
        LEFT OUTER JOIN Table4 ON Table3.Field4 = Table4.Field1
            AND Table3.Field2 = Table4.Field3
        RIGHT OUTER JOIN Table1 ON Table2.Field1 = Table1.Field1
            AND Table2.Field2 = Table1.Field15
    WHERE Table1.Field2 = ' + CAST(@Param3 AS char(1)) + '
        AND Table1.Field12 = ''' + @Param5 + '''
        AND Table1.Field15 = ' + CAST(@Param1 AS char(2)) + '
)
AS UP
PIVOT(MAX(Field2) FOR UP.ID IN (' + @cols + ')) AS PVT '
ELSE IF @Param6 IS NOT NULL
   SET @query = 'SELECT Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12, Field13, Field14,' + @cols + ' FROM
(
    SELECT
        Table1.Field1,
        Table1.Field2,
        Table1.Field3,
        Table1.Field4,
        Table1.Field5,
        Table1.Field6,
        Table1.Field7,
        Table1.Field8,
        Table1.Field9,
        Table1.Field10,
        Table1.Field11,
        Table1.Field12,
        Table1.Field13,
        Table1.Field14,
        Table2.Field2,
        Table3.Field1
    FROM Table3
        LEFT OUTER JOIN Table2 ON Table3.Field1 = Table2.Field1
            AND  Table3.Field2 = Table2.Field2
        LEFT OUTER JOIN Table4 ON Table3.Field4 = Table4.Field1
            AND Table3.Field2 = Table4.Field3
        RIGHT OUTER JOIN Table1 ON Table2.Field1 = Table1.Field1
            AND Table2.Field2 = Table1.Field15
    WHERE Table1.Field2 = ' + CAST(@Param3 AS char(1)) + '
        AND Table1.Field13 = ''' + @Param6 + '''
        AND Table1.Field15 = ' + CAST(@Param1 AS char(2)) + '
)
AS UP
PIVOT(MAX(Field2) FOR UP.ID IN (' + @cols + ')) AS PVT '

ELSE IF @Param7 IS NOT NULL

    SET @query = 'SELECT Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12, Field13, Field14,' + @cols + ' FROM
(
    SELECT
        Table1.Field1,
        Table1.Field2,
        Table1.Field3,
        Table1.Field4,
        Table1.Field5,
        Table1.Field6,
        Table1.Field7,
        Table1.Field8,
        Table1.Field9,
        Table1.Field10,
        Table1.Field11,
        Table1.Field12,
        Table1.Field13,
        Table1.Field14,
        Table2.Field2,
        Table3.Field1
    FROM Table3
        LEFT OUTER JOIN Table2 ON Table3.Field1 = Table2.Field1
            AND Table3.Field2 = Table2.Field2
        LEFT OUTER JOIN Table4 ON Table3.Field4 = Table4.Field1
            AND Table3.Field2 = Table4.Field3
        RIGHT OUTER JOIN Table1 ON Table2.Field1 = Table1.Field1
            AND Table2.Field2 = Table1.Field15
    WHERE Table1.Field2 = ' + CAST(@Param3 AS char(1)) + '
        AND Table1.Field14 = ''' + @Param7 + '''
        AND Table1.Field15 = ' + CAST(@Param1 AS char(2)) + '
) AS UP

PIVOT(MAX(Field2) FOR UP.ID IN (' + @cols + ')) AS PVT '

ELSE

    SET @query = 'SELECT Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12, Field13, Field14,' + @cols + ' FROM
(
SELECT
    Table1.Field1,
    Table1.Field2,
    Table1.Field3,
    Table1.Field4,
    Table1.Field5,
    Table1.Field6,
    Table1.Field7,
    Table1.Field8,
    Table1.Field9,
    Table1.Field10,
    Table1.Field11,
    Table1.Field12,
    Table1.Field13,
    Table1.Field14,
    Table2.Field2,
    Table3.Field1
FROM Table3
    LEFT OUTER JOIN Table2 ON Table3.Field1 = Table2.Field1
        AND Table3.Field2 = Table2.Field2
    LEFT OUTER JOIN Table4 ON Table3.Field4 = Table4.Field1
        AND Table3.Field2 = Table4.Field3
    RIGHT OUTER JOIN Table1 ON Table2.Field1 = Table1.Field1
        AND Table2.Field2 = Table1.Field15
WHERE Table1.Field2 = ' + CAST(@Param3 AS char(1)) + '
    AND Table1.Field3 = ''' + @Param4 + '''
    AND Table1.Field15 = ' + CAST(@Param1 AS char(2)) + '
) AS UP

PIVOT(MAX(Field2) FOR UP.ID IN (' + @cols + ')) AS PVT '

EXECUTE (@query)

Set NOCOUNT OFF;
END
</code>```

I know that the logic behind the parameters isn't very... logical.. but it works the way I need it to for now. It is something I'll change as I'm working on it.

I should also say that I omitted the section of my VBA code that handles the parameters for the SP when I first posted the question. I'm just leaving work now, but I'll add it in once I get home. I don't think the problem lies there though. I basically work through a string array of the parameters and append them to the Cmd object.

EDIT: I've now added the code for creating the parameters and passing them to the Cmd object. Again, it's a bit convoluted, but it's what came out of my head at the time and it does work. I'll revisit the logic once the recordset is populated.

答:
I had a similar issue and found two things that killed the query results over ODBC

Count of intermediate values for processing before the final output. Fixed with "set nocount on" at the start of the query

null values being aggregated away - running the query directly on SQL Server showed the message "Warning: Null value is eliminated by an aggregate or other SET operation." Fixed by chasing down each of them and either replacing with empty strings, zeros or whatever low value makes sense and doesn't impact the query results.

I think in both cases the warning message was output before the records, so the storage recordset was 'filled' with that warning, but no data. On SQL Server, these warnings silently sit in the message log but don't impact the query results, so the temptation is to let them be and move on to higher priority work.

Body:
I don't really understand what has happened here. I'm using Excel VBA to connect to a SQL Server Express database and return an ADO Recordset. I had it working initially, but my code was a bit messy so I created a new module and copied the code across, tidying it up as I went along.

Now, when I try to run the Sub just to return a the recordcount, I get the error "The operation is not allowed when the object is closed." The code breaks on the MsgBox line.

Here is the simplified code:

Dim Server As String
Server = "ServerName"

Dim Database As String
Database = "DatabaseName"

Dim UserID As String
UserID = "UserID"

Dim Pwd As String
Pwd = "Password"

Dim StoredProcedure As String
StoredProcedure = "StoredProcedureName"

Dim conn As New ADODB.Connection
conn.ConnectionString = "Driver={SQL Server};Server=" & Server & "; Database=" & Database & "; UID = " & UserID & "; PWD=" & Pwd & ""
conn.Open

Dim Cmd As New ADODB.Command
Cmd.ActiveConnection = conn
Cmd.CommandText = StoredProcedure
Cmd.CommandType = adCmdStoredProc

Dim params() As String
ReDim Preserve params(4, 2)

params(0, 0) = "Param1"
params(1, 0) = CStr(adInteger)
params(2, 0) = CStr(adParamInput)
params(4, 0) = CStr(6)

params(0, 1) = "Param2"
params(1, 1) = CStr(adInteger)
params(2, 1) = CStr(adParamInput)
params(4, 1) = CStr(6)

params(0, 2) = "Param3"
params(1, 2) = CStr(adInteger)
params(2, 2) = CStr(adParamInput)
params(4, 2) = CStr(15)

Dim sParam4 as String
If Not sParam4 = "" Then
    ReDim Preserve params(4, UBound(params, 2) + 1)
    params(0, UBound(params, 2)) = "Param4"
    params(1, UBound(params, 2)) = CStr(adChar)
    params(2, UBound(params, 2)) = CStr(adParamInput)
    params(3, UBound(params, 2)) = "1"
    params(4, UBound(params, 2)) = sParam4
End If

Dim sParam5 as String
If Not sParam5 = "" Then
    ReDim Preserve params(4, UBound(params, 2) + 1)
    params(0, UBound(params, 2)) = "Param5"
    params(1, UBound(params, 2)) = CStr(adChar)
    params(2, UBound(params, 2)) = CStr(adParamInput)
    params(3, UBound(params, 2)) = Len(sParam5)
    params(4, UBound(params, 2)) = sParam5
End If

Dim Prm As ADODB.Parameter
Set Prm = New ADODB.Parameter
Dim i As Integer
For i = 0 To UBound(params, 2)
    If params(1, i) = CStr(adChar) Then
        Set Prm = Cmd.CreateParameter(params(0, i), CInt(params(1, i)), CInt(params(2, i)), CInt(params(3, i)))
        Cmd.Parameters.Append Prm
        Cmd.Parameters(params(0, i)).Value = params(4, i)
    Else
        Set Prm = Cmd.CreateParameter(params(0, i), CInt(params(1, i)), CInt(params(2, i)))
        Cmd.Parameters.Append Prm
        Cmd.Parameters(params(0, i)).Value = CInt(params(4, i))
    End If
Next i

Dim rs As New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open Cmd, , adOpenStatic, adLockOptimistic

MsgBox ("Success! " & rs.RecordCount & " Records Returned!")

When I look at the rs variable in the Locals window, all of the properties are listed as showing the same error message. To me, it seems as if the recordset is opening correctly, but then immediately closing itself.

What's weird is that the original (messy) sub now also doesn't work, throwing the same error. I don't think that I changed anything there, just copied it to the new sub.

I've commented out the entire module that the old sub was in, just in case there was any sort of conflicting variable situation going on. This didn't make any difference.

I just can't see what's wrong with it! I've done lots of research and reading, and to my untrained-but-enthusiastic eye, it all seems fine.

Any help would be much appreciated.

EDIT: Here is the Stored Procedure:

USE [MyDatabase]
GO
/****** Object:  StoredProcedure [dbo].[MyProcedure]    Script Date: 14/09/2015 11:39:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[MyProcedure]
    -- Add the parameters for the stored procedure here
    @Param1 int, @Param2 int, @Param3 int, @Param4 char(1), @Param5 varchar(20) = NULL, @Param6 varchar(20) = NULL, @Param7 varchar(20) = NULL
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    SET ANSI_WARNINGS OFF;

    -- Insert statements for procedure here
    DECLARE @cols AS NVARCHAR(MAX), @query  AS NVARCHAR(MAX)

SET @cols = '';
SELECT @cols = @cols + ',' + QUOTENAME([Field3]) FROM View1
WHERE Field2 = @Param2 AND Field1 = @Param1

SET @cols = STUFF(@cols,1,1,'');

IF @Param5 IS NOT NULL
   SET @query = 'SELECT Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12, Field13, Field14,' + @cols + ' FROM
(
    SELECT
        Table1.Field1,
        Table1.Field2,
        Table1.Field3,
        Table1.Field4,
        Table1.Field5,
        Table1.Field6,
        Table1.Field7,
        Table1.Field8,
        Table1.Field9,
        Table1.Field10,
        Table1.Field11,
        Table1.Field12,
        Table1.Field13,
        Table1.Field14,
        Table2.Field2,
        Table3.Field1
    FROM Table3
        LEFT OUTER JOIN Table2 ON Table3.Field1 = Table2.Field1
            AND Table3.Field2 = Table2.Field2
        LEFT OUTER JOIN Table4 ON Table3.Field4 = Table4.Field1
            AND Table3.Field2 = Table4.Field3
        RIGHT OUTER JOIN Table1 ON Table2.Field1 = Table1.Field1
            AND Table2.Field2 = Table1.Field15
    WHERE Table1.Field2 = ' + CAST(@Param3 AS char(1)) + '
        AND Table1.Field12 = ''' + @Param5 + '''
        AND Table1.Field15 = ' + CAST(@Param1 AS char(2)) + '
)
AS UP
PIVOT(MAX(Field2) FOR UP.ID IN (' + @cols + ')) AS PVT '
ELSE IF @Param6 IS NOT NULL
   SET @query = 'SELECT Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12, Field13, Field14,' + @cols + ' FROM
(
    SELECT
        Table1.Field1,
        Table1.Field2,
        Table1.Field3,
        Table1.Field4,
        Table1.Field5,
        Table1.Field6,
        Table1.Field7,
        Table1.Field8,
        Table1.Field9,
        Table1.Field10,
        Table1.Field11,
        Table1.Field12,
        Table1.Field13,
        Table1.Field14,
        Table2.Field2,
        Table3.Field1
    FROM Table3
        LEFT OUTER JOIN Table2 ON Table3.Field1 = Table2.Field1
            AND  Table3.Field2 = Table2.Field2
        LEFT OUTER JOIN Table4 ON Table3.Field4 = Table4.Field1
            AND Table3.Field2 = Table4.Field3
        RIGHT OUTER JOIN Table1 ON Table2.Field1 = Table1.Field1
            AND Table2.Field2 = Table1.Field15
    WHERE Table1.Field2 = ' + CAST(@Param3 AS char(1)) + '
        AND Table1.Field13 = ''' + @Param6 + '''
        AND Table1.Field15 = ' + CAST(@Param1 AS char(2)) + '
)
AS UP
PIVOT(MAX(Field2) FOR UP.ID IN (' + @cols + ')) AS PVT '

ELSE IF @Param7 IS NOT NULL

    SET @query = 'SELECT Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12, Field13, Field14,' + @cols + ' FROM
(
    SELECT
        Table1.Field1,
        Table1.Field2,
        Table1.Field3,
        Table1.Field4,
        Table1.Field5,
        Table1.Field6,
        Table1.Field7,
        Table1.Field8,
        Table1.Field9,
        Table1.Field10,
        Table1.Field11,
        Table1.Field12,
        Table1.Field13,
        Table1.Field14,
        Table2.Field2,
        Table3.Field1
    FROM Table3
        LEFT OUTER JOIN Table2 ON Table3.Field1 = Table2.Field1
            AND Table3.Field2 = Table2.Field2
        LEFT OUTER JOIN Table4 ON Table3.Field4 = Table4.Field1
            AND Table3.Field2 = Table4.Field3
        RIGHT OUTER JOIN Table1 ON Table2.Field1 = Table1.Field1
            AND Table2.Field2 = Table1.Field15
    WHERE Table1.Field2 = ' + CAST(@Param3 AS char(1)) + '
        AND Table1.Field14 = ''' + @Param7 + '''
        AND Table1.Field15 = ' + CAST(@Param1 AS char(2)) + '
) AS UP

PIVOT(MAX(Field2) FOR UP.ID IN (' + @cols + ')) AS PVT '

ELSE

    SET @query = 'SELECT Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12, Field13, Field14,' + @cols + ' FROM
(
SELECT
    Table1.Field1,
    Table1.Field2,
    Table1.Field3,
    Table1.Field4,
    Table1.Field5,
    Table1.Field6,
    Table1.Field7,
    Table1.Field8,
    Table1.Field9,
    Table1.Field10,
    Table1.Field11,
    Table1.Field12,
    Table1.Field13,
    Table1.Field14,
    Table2.Field2,
    Table3.Field1
FROM Table3
    LEFT OUTER JOIN Table2 ON Table3.Field1 = Table2.Field1
        AND Table3.Field2 = Table2.Field2
    LEFT OUTER JOIN Table4 ON Table3.Field4 = Table4.Field1
        AND Table3.Field2 = Table4.Field3
    RIGHT OUTER JOIN Table1 ON Table2.Field1 = Table1.Field1
        AND Table2.Field2 = Table1.Field15
WHERE Table1.Field2 = ' + CAST(@Param3 AS char(1)) + '
    AND Table1.Field3 = ''' + @Param4 + '''
    AND Table1.Field15 = ' + CAST(@Param1 AS char(2)) + '
) AS UP

PIVOT(MAX(Field2) FOR UP.ID IN (' + @cols + ')) AS PVT '

EXECUTE (@query)

Set NOCOUNT OFF;
END

I know that the logic behind the parameters isn't very... logical.. but it works the way I need it to for now. It is something I'll change as I'm working on it.

I should also say that I omitted the section of my VBA code that handles the parameters for the SP when I first posted the question. I'm just leaving work now, but I'll add it in once I get home. I don't think the problem lies there though. I basically work through a string array of the parameters and append them to the Cmd object.

EDIT: I've now added the code for creating the parameters and passing them to the Cmd object. Again, it's a bit convoluted, but it's what came out of my head at the time and it does work. I'll revisit the logic once the recordset is populated.